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. |
… | |
… | |
28 | |
27 | |
29 | # URxvt.perl-ext-common: macosx-clipboard |
28 | # URxvt.perl-ext-common: macosx-clipboard |
30 | # URxvt.keysym.M-c: perl:macosx-clipboard:copy |
29 | # URxvt.keysym.M-c: perl:macosx-clipboard:copy |
31 | # URxvt.keysym.M-v: perl:macosx-clipboard:paste |
30 | # URxvt.keysym.M-v: perl:macosx-clipboard:paste |
32 | |
31 | |
33 | use Fcntl (); |
32 | use Mac::Pasteboard; |
34 | use Foundation; |
|
|
35 | use locale; |
|
|
36 | use utf8; |
|
|
37 | |
33 | |
38 | our($appkit, $pasteboard); |
34 | my $pasteboard = new Mac::Pasteboard; |
39 | |
35 | |
40 | sub copy { |
36 | sub copy { |
41 | my ($self) = @_; |
37 | my ($self) = @_; |
42 | |
38 | |
43 | $pasteboard->declareTypes_owner_(NSArray->arrayWithObject_('NSStringPboardType'), undef); |
39 | $pasteboard->clear; |
44 | $pasteboard->setString_forType_($self->selection, 'NSStringPboardType'); |
40 | $pasteboard->copy ($self->selection); |
|
|
41 | |
45 | () |
42 | () |
46 | } |
43 | } |
47 | |
44 | |
48 | sub paste { |
45 | sub paste { |
49 | my ($self) = @_; |
46 | my ($self) = @_; |
50 | my ($type, $str); |
|
|
51 | |
47 | |
52 | $type = $pasteboard->availableTypeFromArray_(NSArray->arrayWithObject_('NSStringPboardType')); |
|
|
53 | if ($type->isEqual_('NSStringPboardType')){ |
|
|
54 | $str = $pasteboard->stringForType_($type)->UTF8String; |
48 | # $str = $pasteboard->stringForType_($type)->UTF8String; |
55 | $str =~ tr/\n/\r/; |
49 | my $str = $pasteboard->paste; |
56 | utf8::decode($str); |
50 | utf8::decode $str; |
57 | $self->tt_write($self->locale_encode($str)); |
51 | $self->tt_write ($self->locale_encode ($str)); |
58 | } |
|
|
59 | () |
|
|
60 | } |
|
|
61 | |
52 | |
62 | sub on_start { |
|
|
63 | my ($self) = @_; |
|
|
64 | |
|
|
65 | $appkit = NSBundle->alloc->init->initWithPath_('/System/Library/Frameworks/AppKit.framework'); |
|
|
66 | $appkit->load if $appkit; |
|
|
67 | |
|
|
68 | if ($appkit->isLoaded) { |
|
|
69 | no strict 'refs'; |
|
|
70 | for my $class (qw(NSPasteboard)) { |
|
|
71 | @{$class . '::ISA'} = 'PerlObjCBridge'; |
|
|
72 | } |
|
|
73 | } else { |
|
|
74 | undef $appkit; |
|
|
75 | } |
|
|
76 | |
|
|
77 | $pasteboard = NSPasteboard->generalPasteboard; |
|
|
78 | () |
53 | () |
79 | } |
54 | } |
80 | |
55 | |
81 | sub on_user_command { |
56 | sub on_user_command { |
82 | my ($self, $cmd) = @_; |
57 | my ($self, $cmd) = @_; |
… | |
… | |
86 | } |
61 | } |
87 | |
62 | |
88 | if ($cmd eq "macosx-clipboard:paste") { |
63 | if ($cmd eq "macosx-clipboard:paste") { |
89 | $self->paste; |
64 | $self->paste; |
90 | } |
65 | } |
|
|
66 | |
91 | () |
67 | () |
92 | } |
68 | } |
93 | |
69 | |