blob: 0299fe413483e6f995ba6fc77b8f90f3e12fd9e6 [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.
Bram Moolenaare00289d2010-08-14 21:56:42 +020032 */
Bram Moolenaarbe7529e2020-08-13 21:05:39 +020033#if defined(FEAT_CLIPBOARD)
Bram Moolenaar164fca32010-07-14 13:58:07 +020034
Bram Moolenaar66bd1c92010-07-14 20:31:44 +020035/* Used to identify clipboard data copied from Vim. */
36
37NSString *VimPboardType = @"VimPboardType";
38
Bram Moolenaar164fca32010-07-14 13:58:07 +020039 void
Bram Moolenaar2fc39ae2019-06-14 23:27:29 +020040clip_mch_lose_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar164fca32010-07-14 13:58:07 +020041{
42}
43
44
45 int
Bram Moolenaar2fc39ae2019-06-14 23:27:29 +020046clip_mch_own_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar164fca32010-07-14 13:58:07 +020047{
48 /* This is called whenever there is a new selection and 'guioptions'
49 * contains the "a" flag (automatically copy selection). Return TRUE, else
50 * the "a" flag does nothing. Note that there is no concept of "ownership"
51 * of the clipboard in Mac OS X.
52 */
53 return TRUE;
54}
55
56
57 void
Bram Moolenaar2fc39ae2019-06-14 23:27:29 +020058clip_mch_request_selection(Clipboard_T *cbd)
Bram Moolenaar164fca32010-07-14 13:58:07 +020059{
60 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
61
62 NSPasteboard *pb = [NSPasteboard generalPasteboard];
Bram Moolenaar1d3dbcf2018-10-08 20:07:39 +020063#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Bram Moolenaard595a192018-06-17 15:01:04 +020064 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
65 NSPasteboardTypeString, nil];
66#else
Bram Moolenaar164fca32010-07-14 13:58:07 +020067 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
68 NSStringPboardType, nil];
Bram Moolenaard595a192018-06-17 15:01:04 +020069#endif
Bram Moolenaar164fca32010-07-14 13:58:07 +020070 NSString *bestType = [pb availableTypeFromArray:supportedTypes];
71 if (!bestType) goto releasepool;
72
Bram Moolenaar54b08a52011-06-20 00:25:44 +020073 int motion_type = MAUTO;
Bram Moolenaar164fca32010-07-14 13:58:07 +020074 NSString *string = nil;
75
76 if ([bestType isEqual:VimPboardType])
77 {
78 /* This type should consist of an array with two objects:
79 * 1. motion type (NSNumber)
80 * 2. text (NSString)
Bram Moolenaard595a192018-06-17 15:01:04 +020081 * If this is not the case we fall back on using NSPasteboardTypeString.
Bram Moolenaar164fca32010-07-14 13:58:07 +020082 */
83 id plist = [pb propertyListForType:VimPboardType];
84 if ([plist isKindOfClass:[NSArray class]] && [plist count] == 2)
85 {
86 id obj = [plist objectAtIndex:1];
87 if ([obj isKindOfClass:[NSString class]])
88 {
89 motion_type = [[plist objectAtIndex:0] intValue];
90 string = obj;
91 }
92 }
93 }
94
95 if (!string)
96 {
Bram Moolenaard595a192018-06-17 15:01:04 +020097 /* Use NSPasteboardTypeString. The motion type is detected automatically.
Bram Moolenaar164fca32010-07-14 13:58:07 +020098 */
Bram Moolenaar1d3dbcf2018-10-08 20:07:39 +020099#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Bram Moolenaard595a192018-06-17 15:01:04 +0200100 NSMutableString *mstring =
101 [[pb stringForType:NSPasteboardTypeString] mutableCopy];
102#else
Bram Moolenaar164fca32010-07-14 13:58:07 +0200103 NSMutableString *mstring =
104 [[pb stringForType:NSStringPboardType] mutableCopy];
Bram Moolenaard595a192018-06-17 15:01:04 +0200105#endif
Bram Moolenaar164fca32010-07-14 13:58:07 +0200106 if (!mstring) goto releasepool;
107
108 /* Replace unrecognized end-of-line sequences with \x0a (line feed). */
109 NSRange range = { 0, [mstring length] };
110 unsigned n = [mstring replaceOccurrencesOfString:@"\x0d\x0a"
111 withString:@"\x0a" options:0
112 range:range];
113 if (0 == n)
114 {
115 n = [mstring replaceOccurrencesOfString:@"\x0d" withString:@"\x0a"
116 options:0 range:range];
117 }
Bram Moolenaard43848c2010-07-14 14:28:26 +0200118
Bram Moolenaar164fca32010-07-14 13:58:07 +0200119 string = mstring;
120 }
121
Bram Moolenaar54b08a52011-06-20 00:25:44 +0200122 /* Default to MAUTO, uses MCHAR or MLINE depending on trailing NL. */
Bram Moolenaar164fca32010-07-14 13:58:07 +0200123 if (!(MCHAR == motion_type || MLINE == motion_type || MBLOCK == motion_type
124 || MAUTO == motion_type))
Bram Moolenaar54b08a52011-06-20 00:25:44 +0200125 motion_type = MAUTO;
Bram Moolenaar164fca32010-07-14 13:58:07 +0200126
127 char_u *str = (char_u*)[string UTF8String];
128 int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
129
Bram Moolenaar164fca32010-07-14 13:58:07 +0200130 if (input_conv.vc_type != CONV_NONE)
131 str = string_convert(&input_conv, str, &len);
Bram Moolenaar164fca32010-07-14 13:58:07 +0200132
133 if (str)
134 clip_yank_selection(motion_type, str, len, cbd);
135
Bram Moolenaar164fca32010-07-14 13:58:07 +0200136 if (input_conv.vc_type != CONV_NONE)
137 vim_free(str);
Bram Moolenaar164fca32010-07-14 13:58:07 +0200138
139releasepool:
140 [pool release];
141}
142
143
144/*
145 * Send the current selection to the clipboard.
146 */
147 void
Bram Moolenaar2fc39ae2019-06-14 23:27:29 +0200148clip_mch_set_selection(Clipboard_T *cbd)
Bram Moolenaar164fca32010-07-14 13:58:07 +0200149{
150 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
151
152 /* If the '*' register isn't already filled in, fill it in now. */
153 cbd->owned = TRUE;
154 clip_get_selection(cbd);
155 cbd->owned = FALSE;
Bram Moolenaard43848c2010-07-14 14:28:26 +0200156
Bram Moolenaar164fca32010-07-14 13:58:07 +0200157 /* Get the text to put on the pasteboard. */
158 long_u llen = 0; char_u *str = 0;
159 int motion_type = clip_convert_selection(&str, &llen, cbd);
160 if (motion_type < 0)
161 goto releasepool;
162
163 /* TODO: Avoid overflow. */
164 int len = (int)llen;
Bram Moolenaar164fca32010-07-14 13:58:07 +0200165 if (output_conv.vc_type != CONV_NONE)
166 {
167 char_u *conv_str = string_convert(&output_conv, str, &len);
168 if (conv_str)
169 {
170 vim_free(str);
171 str = conv_str;
172 }
173 }
Bram Moolenaar164fca32010-07-14 13:58:07 +0200174
175 if (len > 0)
176 {
177 NSString *string = [[NSString alloc]
178 initWithBytes:str length:len encoding:NSUTF8StringEncoding];
179
180 /* See clip_mch_request_selection() for info on pasteboard types. */
181 NSPasteboard *pb = [NSPasteboard generalPasteboard];
Bram Moolenaar1d3dbcf2018-10-08 20:07:39 +0200182#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Bram Moolenaard595a192018-06-17 15:01:04 +0200183 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
184 NSPasteboardTypeString, nil];
185#else
Bram Moolenaar164fca32010-07-14 13:58:07 +0200186 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
187 NSStringPboardType, nil];
Bram Moolenaard595a192018-06-17 15:01:04 +0200188#endif
Bram Moolenaar164fca32010-07-14 13:58:07 +0200189 [pb declareTypes:supportedTypes owner:nil];
190
191 NSNumber *motion = [NSNumber numberWithInt:motion_type];
192 NSArray *plist = [NSArray arrayWithObjects:motion, string, nil];
193 [pb setPropertyList:plist forType:VimPboardType];
194
Bram Moolenaar1d3dbcf2018-10-08 20:07:39 +0200195#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Bram Moolenaard595a192018-06-17 15:01:04 +0200196 [pb setString:string forType:NSPasteboardTypeString];
197#else
Bram Moolenaar164fca32010-07-14 13:58:07 +0200198 [pb setString:string forType:NSStringPboardType];
Bram Moolenaard595a192018-06-17 15:01:04 +0200199#endif
Bram Moolenaard43848c2010-07-14 14:28:26 +0200200
Bram Moolenaar164fca32010-07-14 13:58:07 +0200201 [string release];
202 }
203
204 vim_free(str);
205releasepool:
206 [pool release];
207}
208
209#endif /* FEAT_CLIPBOARD */
Bram Moolenaarf536bf62018-03-06 17:55:01 +0100210
211/* Lift the compiler warning suppression. */
212#if defined(__clang__) && defined(__STRICT_ANSI__)
213# pragma clang diagnostic pop
214# pragma clang diagnostic pop
215#endif