blob: 0ac59b4ccde9ec79d895a8c9864ca73db05ae09f [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 Moolenaar040f9752020-08-11 23:08:48 +020036#if defined(FEAT_CLIPBOARD) && (!defined(FEAT_GUI_ENABLED))
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 Moolenaar2fc39ae2019-06-14 23:27:29 +020043clip_mch_lose_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar164fca32010-07-14 13:58:07 +020044{
45}
46
47
48 int
Bram Moolenaar2fc39ae2019-06-14 23:27:29 +020049clip_mch_own_selection(Clipboard_T *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
Bram Moolenaar2fc39ae2019-06-14 23:27:29 +020061clip_mch_request_selection(Clipboard_T *cbd)
Bram Moolenaar164fca32010-07-14 13:58:07 +020062{
63 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
64
65 NSPasteboard *pb = [NSPasteboard generalPasteboard];
Bram Moolenaar1d3dbcf2018-10-08 20:07:39 +020066#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Bram Moolenaard595a192018-06-17 15:01:04 +020067 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
68 NSPasteboardTypeString, nil];
69#else
Bram Moolenaar164fca32010-07-14 13:58:07 +020070 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
71 NSStringPboardType, nil];
Bram Moolenaard595a192018-06-17 15:01:04 +020072#endif
Bram Moolenaar164fca32010-07-14 13:58:07 +020073 NSString *bestType = [pb availableTypeFromArray:supportedTypes];
74 if (!bestType) goto releasepool;
75
Bram Moolenaar54b08a52011-06-20 00:25:44 +020076 int motion_type = MAUTO;
Bram Moolenaar164fca32010-07-14 13:58:07 +020077 NSString *string = nil;
78
79 if ([bestType isEqual:VimPboardType])
80 {
81 /* This type should consist of an array with two objects:
82 * 1. motion type (NSNumber)
83 * 2. text (NSString)
Bram Moolenaard595a192018-06-17 15:01:04 +020084 * If this is not the case we fall back on using NSPasteboardTypeString.
Bram Moolenaar164fca32010-07-14 13:58:07 +020085 */
86 id plist = [pb propertyListForType:VimPboardType];
87 if ([plist isKindOfClass:[NSArray class]] && [plist count] == 2)
88 {
89 id obj = [plist objectAtIndex:1];
90 if ([obj isKindOfClass:[NSString class]])
91 {
92 motion_type = [[plist objectAtIndex:0] intValue];
93 string = obj;
94 }
95 }
96 }
97
98 if (!string)
99 {
Bram Moolenaard595a192018-06-17 15:01:04 +0200100 /* Use NSPasteboardTypeString. The motion type is detected automatically.
Bram Moolenaar164fca32010-07-14 13:58:07 +0200101 */
Bram Moolenaar1d3dbcf2018-10-08 20:07:39 +0200102#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Bram Moolenaard595a192018-06-17 15:01:04 +0200103 NSMutableString *mstring =
104 [[pb stringForType:NSPasteboardTypeString] mutableCopy];
105#else
Bram Moolenaar164fca32010-07-14 13:58:07 +0200106 NSMutableString *mstring =
107 [[pb stringForType:NSStringPboardType] mutableCopy];
Bram Moolenaard595a192018-06-17 15:01:04 +0200108#endif
Bram Moolenaar164fca32010-07-14 13:58:07 +0200109 if (!mstring) goto releasepool;
110
111 /* Replace unrecognized end-of-line sequences with \x0a (line feed). */
112 NSRange range = { 0, [mstring length] };
113 unsigned n = [mstring replaceOccurrencesOfString:@"\x0d\x0a"
114 withString:@"\x0a" options:0
115 range:range];
116 if (0 == n)
117 {
118 n = [mstring replaceOccurrencesOfString:@"\x0d" withString:@"\x0a"
119 options:0 range:range];
120 }
Bram Moolenaard43848c2010-07-14 14:28:26 +0200121
Bram Moolenaar164fca32010-07-14 13:58:07 +0200122 string = mstring;
123 }
124
Bram Moolenaar54b08a52011-06-20 00:25:44 +0200125 /* Default to MAUTO, uses MCHAR or MLINE depending on trailing NL. */
Bram Moolenaar164fca32010-07-14 13:58:07 +0200126 if (!(MCHAR == motion_type || MLINE == motion_type || MBLOCK == motion_type
127 || MAUTO == motion_type))
Bram Moolenaar54b08a52011-06-20 00:25:44 +0200128 motion_type = MAUTO;
Bram Moolenaar164fca32010-07-14 13:58:07 +0200129
130 char_u *str = (char_u*)[string UTF8String];
131 int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
132
Bram Moolenaar164fca32010-07-14 13:58:07 +0200133 if (input_conv.vc_type != CONV_NONE)
134 str = string_convert(&input_conv, str, &len);
Bram Moolenaar164fca32010-07-14 13:58:07 +0200135
136 if (str)
137 clip_yank_selection(motion_type, str, len, cbd);
138
Bram Moolenaar164fca32010-07-14 13:58:07 +0200139 if (input_conv.vc_type != CONV_NONE)
140 vim_free(str);
Bram Moolenaar164fca32010-07-14 13:58:07 +0200141
142releasepool:
143 [pool release];
144}
145
146
147/*
148 * Send the current selection to the clipboard.
149 */
150 void
Bram Moolenaar2fc39ae2019-06-14 23:27:29 +0200151clip_mch_set_selection(Clipboard_T *cbd)
Bram Moolenaar164fca32010-07-14 13:58:07 +0200152{
153 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
154
155 /* If the '*' register isn't already filled in, fill it in now. */
156 cbd->owned = TRUE;
157 clip_get_selection(cbd);
158 cbd->owned = FALSE;
Bram Moolenaard43848c2010-07-14 14:28:26 +0200159
Bram Moolenaar164fca32010-07-14 13:58:07 +0200160 /* Get the text to put on the pasteboard. */
161 long_u llen = 0; char_u *str = 0;
162 int motion_type = clip_convert_selection(&str, &llen, cbd);
163 if (motion_type < 0)
164 goto releasepool;
165
166 /* TODO: Avoid overflow. */
167 int len = (int)llen;
Bram Moolenaar164fca32010-07-14 13:58:07 +0200168 if (output_conv.vc_type != CONV_NONE)
169 {
170 char_u *conv_str = string_convert(&output_conv, str, &len);
171 if (conv_str)
172 {
173 vim_free(str);
174 str = conv_str;
175 }
176 }
Bram Moolenaar164fca32010-07-14 13:58:07 +0200177
178 if (len > 0)
179 {
180 NSString *string = [[NSString alloc]
181 initWithBytes:str length:len encoding:NSUTF8StringEncoding];
182
183 /* See clip_mch_request_selection() for info on pasteboard types. */
184 NSPasteboard *pb = [NSPasteboard generalPasteboard];
Bram Moolenaar1d3dbcf2018-10-08 20:07:39 +0200185#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Bram Moolenaard595a192018-06-17 15:01:04 +0200186 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
187 NSPasteboardTypeString, nil];
188#else
Bram Moolenaar164fca32010-07-14 13:58:07 +0200189 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
190 NSStringPboardType, nil];
Bram Moolenaard595a192018-06-17 15:01:04 +0200191#endif
Bram Moolenaar164fca32010-07-14 13:58:07 +0200192 [pb declareTypes:supportedTypes owner:nil];
193
194 NSNumber *motion = [NSNumber numberWithInt:motion_type];
195 NSArray *plist = [NSArray arrayWithObjects:motion, string, nil];
196 [pb setPropertyList:plist forType:VimPboardType];
197
Bram Moolenaar1d3dbcf2018-10-08 20:07:39 +0200198#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Bram Moolenaard595a192018-06-17 15:01:04 +0200199 [pb setString:string forType:NSPasteboardTypeString];
200#else
Bram Moolenaar164fca32010-07-14 13:58:07 +0200201 [pb setString:string forType:NSStringPboardType];
Bram Moolenaard595a192018-06-17 15:01:04 +0200202#endif
Bram Moolenaard43848c2010-07-14 14:28:26 +0200203
Bram Moolenaar164fca32010-07-14 13:58:07 +0200204 [string release];
205 }
206
207 vim_free(str);
208releasepool:
209 [pool release];
210}
211
212#endif /* FEAT_CLIPBOARD */
Bram Moolenaarf536bf62018-03-06 17:55:01 +0100213
214/* Lift the compiler warning suppression. */
215#if defined(__clang__) && defined(__STRICT_ANSI__)
216# pragma clang diagnostic pop
217# pragma clang diagnostic pop
218#endif