1 |
#! perl -w |
2 |
|
3 |
# http://triplefusion.net/system/macosx-clipboard |
4 |
|
5 |
# ---------------------------------------------------------------------- |
6 |
# File: macosx-clipboard-native |
7 |
# ---------------------------------------------------------------------- |
8 |
# |
9 |
# All portions of code are copyright by their respective author/s. |
10 |
# Copyright (c) 2006 Samuel Ljungkvist <salj@triplefusion.net> |
11 |
# |
12 |
# 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 |
14 |
# the Free Software Foundation; either version 2 of the License, or |
15 |
# (at your option) any later version. |
16 |
# |
17 |
# This program is distributed in the hope that it will be useful, |
18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
# GNU General Public License for more details. |
21 |
# |
22 |
# You should have received a copy of the GNU General Public License |
23 |
# along with this program; if not, write to the Free Software |
24 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 |
# ---------------------------------------------------------------------- |
26 |
|
27 |
# Usage: |
28 |
|
29 |
# URxvt.perl-ext-common: macosx-clipboard |
30 |
# URxvt.keysym.M-c: perl:macosx-clipboard:copy |
31 |
# URxvt.keysym.M-v: perl:macosx-clipboard:paste |
32 |
|
33 |
use Fcntl (); |
34 |
use Foundation; |
35 |
|
36 |
our $appkit = NSBundle->alloc->init->initWithPath_('/System/Library/Frameworks/AppKit.framework'); |
37 |
$appkit->load if $appkit; |
38 |
|
39 |
if ($appkit->isLoaded) { |
40 |
no strict 'refs'; |
41 |
for my $class (qw(NSPasteboard)) { |
42 |
@{$class . '::ISA'} = 'PerlObjCBridge'; |
43 |
} |
44 |
} else { |
45 |
undef $appkit; |
46 |
} |
47 |
|
48 |
our $pasteboard = NSPasteboard->generalPasteboard; |
49 |
|
50 |
sub copy { |
51 |
my ($self) = @_; |
52 |
|
53 |
$pasteboard->declareTypes_owner_(NSArray->arrayWithObject_('NSStringPboardType'), undef); |
54 |
$pasteboard->setString_forType_($self->selection, 'NSStringPboardType'); |
55 |
|
56 |
() |
57 |
} |
58 |
|
59 |
sub paste { |
60 |
my ($self) = @_; |
61 |
my ($type, $str); |
62 |
|
63 |
$type = $pasteboard->availableTypeFromArray_(NSArray->arrayWithObject_('NSStringPboardType')); |
64 |
if ($type->isEqual_('NSStringPboardType')){ |
65 |
$str = $pasteboard->stringForType_($type)->UTF8String; |
66 |
$str =~ tr/\n/\r/; |
67 |
utf8::decode($str); |
68 |
$self->tt_write($self->locale_encode($str)); |
69 |
} |
70 |
|
71 |
() |
72 |
} |
73 |
|
74 |
sub on_user_command { |
75 |
my ($self, $cmd) = @_; |
76 |
|
77 |
if ($cmd eq "macosx-clipboard:copy") { |
78 |
$self->copy; |
79 |
} |
80 |
|
81 |
if ($cmd eq "macosx-clipboard:paste") { |
82 |
$self->paste; |
83 |
} |
84 |
|
85 |
() |
86 |
} |
87 |
|