1 | #! perl -w |
1 | #! perl -w |
2 | |
|
|
3 | # http://triplefusion.net/system/macosx-clipboard |
|
|
4 | |
2 | |
5 | # ---------------------------------------------------------------------- |
3 | # ---------------------------------------------------------------------- |
6 | # File: macosx-clipboard |
4 | # File: macosx-clipboard |
7 | # ---------------------------------------------------------------------- |
5 | # ---------------------------------------------------------------------- |
8 | # |
6 | # |
9 | # All portions of code are copyright by their respective author/s. |
7 | # All portions of code are copyright by their respective author/s. |
10 | # Copyright (c) 2006 Samuel Ljungkvist <salj@triplefusion.net> |
8 | # Copyright (c) 2006 Samuel Ljungkvist <salj@triplefusion.net> |
|
|
9 | # 2009 Reza Jelveh <reza.jelveh@gmail.com> |
11 | # |
10 | # |
12 | # This program is free software; you can redistribute it and/or modify |
11 | # This program is free software; you can redistribute it and/or modify |
13 | # it under the terms of the GNU General Public License as published by |
12 | # it under the terms of the GNU General Public License as published by |
14 | # the Free Software Foundation; either version 2 of the License, or |
13 | # the Free Software Foundation; either version 2 of the License, or |
15 | # (at your option) any later version. |
14 | # (at your option) any later version. |
… | |
… | |
22 | # You should have received a copy of the GNU General Public License |
21 | # You should have received a copy of the GNU General Public License |
23 | # along with this program; if not, write to the Free Software |
22 | # along with this program; if not, write to the Free Software |
24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | # ---------------------------------------------------------------------- |
24 | # ---------------------------------------------------------------------- |
26 | |
25 | |
27 | # Usage: |
26 | =head1 NAME |
28 | |
27 | |
29 | # URxvt.perl-ext-common: macosx-clipboard |
28 | macosx-clipboard - Mac OS X clipboard support |
30 | # URxvt.keysym.M-c: perl:macosx-clipboard:copy |
|
|
31 | # URxvt.keysym.M-v: perl:macosx-clipboard:paste |
|
|
32 | |
29 | |
33 | use Fcntl (); |
30 | =head1 SYNOPSIS |
34 | use Foundation; |
|
|
35 | use locale; |
|
|
36 | use utf8; |
|
|
37 | |
31 | |
38 | our($appkit, $pasteboard); |
32 | urxvt -pe macosx-clipboard |
39 | |
33 | |
40 | $appkit = NSBundle->alloc->init->initWithPath_('/System/Library/Frameworks/AppKit.framework'); |
34 | =head1 DESCRIPTION |
41 | $appkit->load if $appkit; |
|
|
42 | |
35 | |
43 | if ($appkit->isLoaded) { |
36 | This extension implements commands to interact with the Mac OS X |
44 | no strict 'refs'; |
37 | clipboard and requires the C<Mac::Pasteboard> module. It is used like |
45 | for my $class (qw(NSPasteboard)) { |
38 | this: |
46 | @{$class . '::ISA'} = 'PerlObjCBridge'; |
|
|
47 | } |
|
|
48 | } else { |
|
|
49 | undef $appkit; |
|
|
50 | } |
|
|
51 | |
39 | |
52 | $pasteboard = NSPasteboard->generalPasteboard; |
40 | URxvt.keysym.M-c: perl:macosx-clipboard:copy |
|
|
41 | URxvt.keysym.M-v: perl:macosx-clipboard:paste |
|
|
42 | |
|
|
43 | =cut |
|
|
44 | |
|
|
45 | use Mac::Pasteboard; |
|
|
46 | |
|
|
47 | my $pasteboard = new Mac::Pasteboard; |
53 | |
48 | |
54 | sub copy { |
49 | sub copy { |
55 | my ($self) = @_; |
50 | my ($self) = @_; |
56 | |
51 | |
57 | $pasteboard->declareTypes_owner_(NSArray->arrayWithObject_('NSStringPboardType'), undef); |
52 | $pasteboard->clear; |
58 | $pasteboard->setString_forType_($self->selection, 'NSStringPboardType'); |
53 | $pasteboard->copy ($self->selection); |
|
|
54 | |
59 | () |
55 | () |
60 | } |
56 | } |
61 | |
57 | |
62 | sub paste { |
58 | sub paste { |
63 | my ($self) = @_; |
59 | my ($self) = @_; |
64 | my ($type, $str); |
|
|
65 | |
60 | |
66 | $type = $pasteboard->availableTypeFromArray_(NSArray->arrayWithObject_('NSStringPboardType')); |
61 | my $str = $pasteboard->paste; |
67 | if ($type->isEqual_('NSStringPboardType')){ |
|
|
68 | $str = $pasteboard->stringForType_($type)->UTF8String; |
|
|
69 | $str =~ tr/\n/\r/; |
|
|
70 | utf8::decode($str); |
62 | utf8::decode $str; |
71 | $self->tt_write($self->locale_encode($str)); |
63 | $self->tt_write ($self->locale_encode ($str)); |
72 | } |
64 | |
73 | () |
65 | () |
74 | } |
66 | } |
75 | |
67 | |
76 | sub on_user_command { |
68 | sub on_user_command { |
77 | my ($self, $cmd) = @_; |
69 | my ($self, $cmd) = @_; |
… | |
… | |
81 | } |
73 | } |
82 | |
74 | |
83 | if ($cmd eq "macosx-clipboard:paste") { |
75 | if ($cmd eq "macosx-clipboard:paste") { |
84 | $self->paste; |
76 | $self->paste; |
85 | } |
77 | } |
|
|
78 | |
86 | () |
79 | () |
87 | } |
80 | } |
88 | |
81 | |