blob: 6fd72fd28045a1e4d509a80789961b220c02280f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaard0573012017-10-28 21:11:06 +020011 * os_macosx.m -- Mac specific things for Mac OS X.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012 */
13
Bram Moolenaar423f9722010-10-10 17:08:43 +020014/* Avoid a conflict for the definition of Boolean between Mac header files and
15 * X11 header files. */
16#define NO_X11_INCLUDES
17
Bram Moolenaar164fca32010-07-14 13:58:07 +020018#include "vim.h"
Bram Moolenaard0573012017-10-28 21:11:06 +020019#import <AppKit/AppKit.h>
Bram Moolenaar164fca32010-07-14 13:58:07 +020020
21
Bram Moolenaare00289d2010-08-14 21:56:42 +020022/*
23 * Clipboard support for the console.
24 * Don't include this when building the GUI version, the functions in
Bram Moolenaar10d46642010-08-15 12:57:37 +020025 * gui_mac.c are used then. TODO: remove those instead?
Bram Moolenaar9a4d7fd2011-06-13 02:04:00 +020026 * But for MacVim we do need these ones.
Bram Moolenaare00289d2010-08-14 21:56:42 +020027 */
Bram Moolenaar9a4d7fd2011-06-13 02:04:00 +020028#if defined(FEAT_CLIPBOARD) && (!defined(FEAT_GUI_ENABLED) || defined(FEAT_GUI_MACVIM))
Bram Moolenaar164fca32010-07-14 13:58:07 +020029
Bram Moolenaar66bd1c92010-07-14 20:31:44 +020030/* Used to identify clipboard data copied from Vim. */
31
32NSString *VimPboardType = @"VimPboardType";
33
Bram Moolenaar164fca32010-07-14 13:58:07 +020034 void
Bram Moolenaar448a2252016-01-31 18:08:34 +010035clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar164fca32010-07-14 13:58:07 +020036{
37}
38
39
40 int
Bram Moolenaar448a2252016-01-31 18:08:34 +010041clip_mch_own_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar164fca32010-07-14 13:58:07 +020042{
43 /* This is called whenever there is a new selection and 'guioptions'
44 * contains the "a" flag (automatically copy selection). Return TRUE, else
45 * the "a" flag does nothing. Note that there is no concept of "ownership"
46 * of the clipboard in Mac OS X.
47 */
48 return TRUE;
49}
50
51
52 void
53clip_mch_request_selection(VimClipboard *cbd)
54{
55 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
56
57 NSPasteboard *pb = [NSPasteboard generalPasteboard];
58 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
59 NSStringPboardType, nil];
60 NSString *bestType = [pb availableTypeFromArray:supportedTypes];
61 if (!bestType) goto releasepool;
62
Bram Moolenaar54b08a52011-06-20 00:25:44 +020063 int motion_type = MAUTO;
Bram Moolenaar164fca32010-07-14 13:58:07 +020064 NSString *string = nil;
65
66 if ([bestType isEqual:VimPboardType])
67 {
68 /* This type should consist of an array with two objects:
69 * 1. motion type (NSNumber)
70 * 2. text (NSString)
71 * If this is not the case we fall back on using NSStringPboardType.
72 */
73 id plist = [pb propertyListForType:VimPboardType];
74 if ([plist isKindOfClass:[NSArray class]] && [plist count] == 2)
75 {
76 id obj = [plist objectAtIndex:1];
77 if ([obj isKindOfClass:[NSString class]])
78 {
79 motion_type = [[plist objectAtIndex:0] intValue];
80 string = obj;
81 }
82 }
83 }
84
85 if (!string)
86 {
Bram Moolenaar54b08a52011-06-20 00:25:44 +020087 /* Use NSStringPboardType. The motion type is detected automatically.
Bram Moolenaar164fca32010-07-14 13:58:07 +020088 */
89 NSMutableString *mstring =
90 [[pb stringForType:NSStringPboardType] mutableCopy];
91 if (!mstring) goto releasepool;
92
93 /* Replace unrecognized end-of-line sequences with \x0a (line feed). */
94 NSRange range = { 0, [mstring length] };
95 unsigned n = [mstring replaceOccurrencesOfString:@"\x0d\x0a"
96 withString:@"\x0a" options:0
97 range:range];
98 if (0 == n)
99 {
100 n = [mstring replaceOccurrencesOfString:@"\x0d" withString:@"\x0a"
101 options:0 range:range];
102 }
Bram Moolenaard43848c2010-07-14 14:28:26 +0200103
Bram Moolenaar164fca32010-07-14 13:58:07 +0200104 string = mstring;
105 }
106
Bram Moolenaar54b08a52011-06-20 00:25:44 +0200107 /* Default to MAUTO, uses MCHAR or MLINE depending on trailing NL. */
Bram Moolenaar164fca32010-07-14 13:58:07 +0200108 if (!(MCHAR == motion_type || MLINE == motion_type || MBLOCK == motion_type
109 || MAUTO == motion_type))
Bram Moolenaar54b08a52011-06-20 00:25:44 +0200110 motion_type = MAUTO;
Bram Moolenaar164fca32010-07-14 13:58:07 +0200111
112 char_u *str = (char_u*)[string UTF8String];
113 int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
114
115#ifdef FEAT_MBYTE
116 if (input_conv.vc_type != CONV_NONE)
117 str = string_convert(&input_conv, str, &len);
118#endif
119
120 if (str)
121 clip_yank_selection(motion_type, str, len, cbd);
122
123#ifdef FEAT_MBYTE
124 if (input_conv.vc_type != CONV_NONE)
125 vim_free(str);
126#endif
127
128releasepool:
129 [pool release];
130}
131
132
133/*
134 * Send the current selection to the clipboard.
135 */
136 void
137clip_mch_set_selection(VimClipboard *cbd)
138{
139 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
140
141 /* If the '*' register isn't already filled in, fill it in now. */
142 cbd->owned = TRUE;
143 clip_get_selection(cbd);
144 cbd->owned = FALSE;
Bram Moolenaard43848c2010-07-14 14:28:26 +0200145
Bram Moolenaar164fca32010-07-14 13:58:07 +0200146 /* Get the text to put on the pasteboard. */
147 long_u llen = 0; char_u *str = 0;
148 int motion_type = clip_convert_selection(&str, &llen, cbd);
149 if (motion_type < 0)
150 goto releasepool;
151
152 /* TODO: Avoid overflow. */
153 int len = (int)llen;
154#ifdef FEAT_MBYTE
155 if (output_conv.vc_type != CONV_NONE)
156 {
157 char_u *conv_str = string_convert(&output_conv, str, &len);
158 if (conv_str)
159 {
160 vim_free(str);
161 str = conv_str;
162 }
163 }
164#endif
165
166 if (len > 0)
167 {
168 NSString *string = [[NSString alloc]
169 initWithBytes:str length:len encoding:NSUTF8StringEncoding];
170
171 /* See clip_mch_request_selection() for info on pasteboard types. */
172 NSPasteboard *pb = [NSPasteboard generalPasteboard];
173 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
174 NSStringPboardType, nil];
175 [pb declareTypes:supportedTypes owner:nil];
176
177 NSNumber *motion = [NSNumber numberWithInt:motion_type];
178 NSArray *plist = [NSArray arrayWithObjects:motion, string, nil];
179 [pb setPropertyList:plist forType:VimPboardType];
180
181 [pb setString:string forType:NSStringPboardType];
Bram Moolenaard43848c2010-07-14 14:28:26 +0200182
Bram Moolenaar164fca32010-07-14 13:58:07 +0200183 [string release];
184 }
185
186 vim_free(str);
187releasepool:
188 [pool release];
189}
190
191#endif /* FEAT_CLIPBOARD */