blob: b0d29d947755437cafd6087f77bc2254dc497394 [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 Moolenaarf536bf62018-03-06 17:55:01 +010014/* Suppress compiler warnings to non-C89 code. */
15#if defined(__clang__) && defined(__STRICT_ANSI__)
16# pragma clang diagnostic push
17# pragma clang diagnostic ignored "-Wc99-extensions"
18# pragma clang diagnostic push
19# pragma clang diagnostic ignored "-Wdeclaration-after-statement"
20#endif
21
Bram Moolenaar423f9722010-10-10 17:08:43 +020022/* Avoid a conflict for the definition of Boolean between Mac header files and
23 * X11 header files. */
24#define NO_X11_INCLUDES
25
Bram Moolenaar164fca32010-07-14 13:58:07 +020026#include "vim.h"
Bram Moolenaard0573012017-10-28 21:11:06 +020027#import <AppKit/AppKit.h>
Bram Moolenaar164fca32010-07-14 13:58:07 +020028
29
Bram Moolenaare00289d2010-08-14 21:56:42 +020030/*
31 * Clipboard support for the console.
32 * Don't include this when building the GUI version, the functions in
Bram Moolenaar10d46642010-08-15 12:57:37 +020033 * gui_mac.c are used then. TODO: remove those instead?
Bram Moolenaar9a4d7fd2011-06-13 02:04:00 +020034 * But for MacVim we do need these ones.
Bram Moolenaare00289d2010-08-14 21:56:42 +020035 */
Bram Moolenaar9a4d7fd2011-06-13 02:04:00 +020036#if defined(FEAT_CLIPBOARD) && (!defined(FEAT_GUI_ENABLED) || defined(FEAT_GUI_MACVIM))
Bram Moolenaar164fca32010-07-14 13:58:07 +020037
Bram Moolenaar66bd1c92010-07-14 20:31:44 +020038/* Used to identify clipboard data copied from Vim. */
39
40NSString *VimPboardType = @"VimPboardType";
41
Bram Moolenaar164fca32010-07-14 13:58:07 +020042 void
Bram Moolenaar448a2252016-01-31 18:08:34 +010043clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar164fca32010-07-14 13:58:07 +020044{
45}
46
47
48 int
Bram Moolenaar448a2252016-01-31 18:08:34 +010049clip_mch_own_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar164fca32010-07-14 13:58:07 +020050{
51 /* This is called whenever there is a new selection and 'guioptions'
52 * contains the "a" flag (automatically copy selection). Return TRUE, else
53 * the "a" flag does nothing. Note that there is no concept of "ownership"
54 * of the clipboard in Mac OS X.
55 */
56 return TRUE;
57}
58
59
60 void
61clip_mch_request_selection(VimClipboard *cbd)
62{
63 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
64
65 NSPasteboard *pb = [NSPasteboard generalPasteboard];
66 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
67 NSStringPboardType, nil];
68 NSString *bestType = [pb availableTypeFromArray:supportedTypes];
69 if (!bestType) goto releasepool;
70
Bram Moolenaar54b08a52011-06-20 00:25:44 +020071 int motion_type = MAUTO;
Bram Moolenaar164fca32010-07-14 13:58:07 +020072 NSString *string = nil;
73
74 if ([bestType isEqual:VimPboardType])
75 {
76 /* This type should consist of an array with two objects:
77 * 1. motion type (NSNumber)
78 * 2. text (NSString)
79 * If this is not the case we fall back on using NSStringPboardType.
80 */
81 id plist = [pb propertyListForType:VimPboardType];
82 if ([plist isKindOfClass:[NSArray class]] && [plist count] == 2)
83 {
84 id obj = [plist objectAtIndex:1];
85 if ([obj isKindOfClass:[NSString class]])
86 {
87 motion_type = [[plist objectAtIndex:0] intValue];
88 string = obj;
89 }
90 }
91 }
92
93 if (!string)
94 {
Bram Moolenaar54b08a52011-06-20 00:25:44 +020095 /* Use NSStringPboardType. The motion type is detected automatically.
Bram Moolenaar164fca32010-07-14 13:58:07 +020096 */
97 NSMutableString *mstring =
98 [[pb stringForType:NSStringPboardType] mutableCopy];
99 if (!mstring) goto releasepool;
100
101 /* Replace unrecognized end-of-line sequences with \x0a (line feed). */
102 NSRange range = { 0, [mstring length] };
103 unsigned n = [mstring replaceOccurrencesOfString:@"\x0d\x0a"
104 withString:@"\x0a" options:0
105 range:range];
106 if (0 == n)
107 {
108 n = [mstring replaceOccurrencesOfString:@"\x0d" withString:@"\x0a"
109 options:0 range:range];
110 }
Bram Moolenaard43848c2010-07-14 14:28:26 +0200111
Bram Moolenaar164fca32010-07-14 13:58:07 +0200112 string = mstring;
113 }
114
Bram Moolenaar54b08a52011-06-20 00:25:44 +0200115 /* Default to MAUTO, uses MCHAR or MLINE depending on trailing NL. */
Bram Moolenaar164fca32010-07-14 13:58:07 +0200116 if (!(MCHAR == motion_type || MLINE == motion_type || MBLOCK == motion_type
117 || MAUTO == motion_type))
Bram Moolenaar54b08a52011-06-20 00:25:44 +0200118 motion_type = MAUTO;
Bram Moolenaar164fca32010-07-14 13:58:07 +0200119
120 char_u *str = (char_u*)[string UTF8String];
121 int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
122
123#ifdef FEAT_MBYTE
124 if (input_conv.vc_type != CONV_NONE)
125 str = string_convert(&input_conv, str, &len);
126#endif
127
128 if (str)
129 clip_yank_selection(motion_type, str, len, cbd);
130
131#ifdef FEAT_MBYTE
132 if (input_conv.vc_type != CONV_NONE)
133 vim_free(str);
134#endif
135
136releasepool:
137 [pool release];
138}
139
140
141/*
142 * Send the current selection to the clipboard.
143 */
144 void
145clip_mch_set_selection(VimClipboard *cbd)
146{
147 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
148
149 /* If the '*' register isn't already filled in, fill it in now. */
150 cbd->owned = TRUE;
151 clip_get_selection(cbd);
152 cbd->owned = FALSE;
Bram Moolenaard43848c2010-07-14 14:28:26 +0200153
Bram Moolenaar164fca32010-07-14 13:58:07 +0200154 /* Get the text to put on the pasteboard. */
155 long_u llen = 0; char_u *str = 0;
156 int motion_type = clip_convert_selection(&str, &llen, cbd);
157 if (motion_type < 0)
158 goto releasepool;
159
160 /* TODO: Avoid overflow. */
161 int len = (int)llen;
162#ifdef FEAT_MBYTE
163 if (output_conv.vc_type != CONV_NONE)
164 {
165 char_u *conv_str = string_convert(&output_conv, str, &len);
166 if (conv_str)
167 {
168 vim_free(str);
169 str = conv_str;
170 }
171 }
172#endif
173
174 if (len > 0)
175 {
176 NSString *string = [[NSString alloc]
177 initWithBytes:str length:len encoding:NSUTF8StringEncoding];
178
179 /* See clip_mch_request_selection() for info on pasteboard types. */
180 NSPasteboard *pb = [NSPasteboard generalPasteboard];
181 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
182 NSStringPboardType, nil];
183 [pb declareTypes:supportedTypes owner:nil];
184
185 NSNumber *motion = [NSNumber numberWithInt:motion_type];
186 NSArray *plist = [NSArray arrayWithObjects:motion, string, nil];
187 [pb setPropertyList:plist forType:VimPboardType];
188
189 [pb setString:string forType:NSStringPboardType];
Bram Moolenaard43848c2010-07-14 14:28:26 +0200190
Bram Moolenaar164fca32010-07-14 13:58:07 +0200191 [string release];
192 }
193
194 vim_free(str);
195releasepool:
196 [pool release];
197}
198
199#endif /* FEAT_CLIPBOARD */
Bram Moolenaarf536bf62018-03-06 17:55:01 +0100200
201/* Lift the compiler warning suppression. */
202#if defined(__clang__) && defined(__STRICT_ANSI__)
203# pragma clang diagnostic pop
204# pragma clang diagnostic pop
205#endif