blob: 752bdae116cb743761226989121517d1a1ed52c4 [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];
Bram Moolenaard595a192018-06-17 15:01:04 +020066#ifdef AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER
67 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 Moolenaard595a192018-06-17 15:01:04 +0200102#ifdef AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER
103 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
133#ifdef FEAT_MBYTE
134 if (input_conv.vc_type != CONV_NONE)
135 str = string_convert(&input_conv, str, &len);
136#endif
137
138 if (str)
139 clip_yank_selection(motion_type, str, len, cbd);
140
141#ifdef FEAT_MBYTE
142 if (input_conv.vc_type != CONV_NONE)
143 vim_free(str);
144#endif
145
146releasepool:
147 [pool release];
148}
149
150
151/*
152 * Send the current selection to the clipboard.
153 */
154 void
155clip_mch_set_selection(VimClipboard *cbd)
156{
157 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
158
159 /* If the '*' register isn't already filled in, fill it in now. */
160 cbd->owned = TRUE;
161 clip_get_selection(cbd);
162 cbd->owned = FALSE;
Bram Moolenaard43848c2010-07-14 14:28:26 +0200163
Bram Moolenaar164fca32010-07-14 13:58:07 +0200164 /* Get the text to put on the pasteboard. */
165 long_u llen = 0; char_u *str = 0;
166 int motion_type = clip_convert_selection(&str, &llen, cbd);
167 if (motion_type < 0)
168 goto releasepool;
169
170 /* TODO: Avoid overflow. */
171 int len = (int)llen;
172#ifdef FEAT_MBYTE
173 if (output_conv.vc_type != CONV_NONE)
174 {
175 char_u *conv_str = string_convert(&output_conv, str, &len);
176 if (conv_str)
177 {
178 vim_free(str);
179 str = conv_str;
180 }
181 }
182#endif
183
184 if (len > 0)
185 {
186 NSString *string = [[NSString alloc]
187 initWithBytes:str length:len encoding:NSUTF8StringEncoding];
188
189 /* See clip_mch_request_selection() for info on pasteboard types. */
190 NSPasteboard *pb = [NSPasteboard generalPasteboard];
Bram Moolenaard595a192018-06-17 15:01:04 +0200191#ifdef AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER
192 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
193 NSPasteboardTypeString, nil];
194#else
Bram Moolenaar164fca32010-07-14 13:58:07 +0200195 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
196 NSStringPboardType, nil];
Bram Moolenaard595a192018-06-17 15:01:04 +0200197#endif
Bram Moolenaar164fca32010-07-14 13:58:07 +0200198 [pb declareTypes:supportedTypes owner:nil];
199
200 NSNumber *motion = [NSNumber numberWithInt:motion_type];
201 NSArray *plist = [NSArray arrayWithObjects:motion, string, nil];
202 [pb setPropertyList:plist forType:VimPboardType];
203
Bram Moolenaard595a192018-06-17 15:01:04 +0200204#ifdef AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER
205 [pb setString:string forType:NSPasteboardTypeString];
206#else
Bram Moolenaar164fca32010-07-14 13:58:07 +0200207 [pb setString:string forType:NSStringPboardType];
Bram Moolenaard595a192018-06-17 15:01:04 +0200208#endif
Bram Moolenaard43848c2010-07-14 14:28:26 +0200209
Bram Moolenaar164fca32010-07-14 13:58:07 +0200210 [string release];
211 }
212
213 vim_free(str);
214releasepool:
215 [pool release];
216}
217
218#endif /* FEAT_CLIPBOARD */
Bram Moolenaarf536bf62018-03-06 17:55:01 +0100219
220/* Lift the compiler warning suppression. */
221#if defined(__clang__) && defined(__STRICT_ANSI__)
222# pragma clang diagnostic pop
223# pragma clang diagnostic pop
224#endif