blob: bc646677bf5f99e2a4a687d3ad7611e502fa30bb [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar164fca32010-07-14 13:58:07 +020011 * os_macosx.m -- Mac specific things for Mac OS/X.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012 */
13
Bram Moolenaar164fca32010-07-14 13:58:07 +020014#ifndef MACOS_X_UNIX
Bram Moolenaar9372a112005-12-06 19:59:18 +000015 Error: MACOS 9 is no longer supported in Vim 7
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#endif
17
Bram Moolenaar423f9722010-10-10 17:08:43 +020018/* Avoid a conflict for the definition of Boolean between Mac header files and
19 * X11 header files. */
20#define NO_X11_INCLUDES
Bram Moolenaar9a4d7fd2011-06-13 02:04:00 +020021#define BalloonEval int /* used in header files */
Bram Moolenaar423f9722010-10-10 17:08:43 +020022
Bram Moolenaar164fca32010-07-14 13:58:07 +020023#include "vim.h"
24#import <Cocoa/Cocoa.h>
25
26
Bram Moolenaare00289d2010-08-14 21:56:42 +020027/*
28 * Clipboard support for the console.
29 * Don't include this when building the GUI version, the functions in
Bram Moolenaar10d46642010-08-15 12:57:37 +020030 * gui_mac.c are used then. TODO: remove those instead?
Bram Moolenaar9a4d7fd2011-06-13 02:04:00 +020031 * But for MacVim we do need these ones.
Bram Moolenaare00289d2010-08-14 21:56:42 +020032 */
Bram Moolenaar9a4d7fd2011-06-13 02:04:00 +020033#if defined(FEAT_CLIPBOARD) && (!defined(FEAT_GUI_ENABLED) || defined(FEAT_GUI_MACVIM))
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
40clip_mch_lose_selection(VimClipboard *cbd)
41{
42}
43
44
45 int
46clip_mch_own_selection(VimClipboard *cbd)
47{
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
58clip_mch_request_selection(VimClipboard *cbd)
59{
60 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
61
62 NSPasteboard *pb = [NSPasteboard generalPasteboard];
63 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
64 NSStringPboardType, nil];
65 NSString *bestType = [pb availableTypeFromArray:supportedTypes];
66 if (!bestType) goto releasepool;
67
68 int motion_type = MCHAR;
69 NSString *string = nil;
70
71 if ([bestType isEqual:VimPboardType])
72 {
73 /* This type should consist of an array with two objects:
74 * 1. motion type (NSNumber)
75 * 2. text (NSString)
76 * If this is not the case we fall back on using NSStringPboardType.
77 */
78 id plist = [pb propertyListForType:VimPboardType];
79 if ([plist isKindOfClass:[NSArray class]] && [plist count] == 2)
80 {
81 id obj = [plist objectAtIndex:1];
82 if ([obj isKindOfClass:[NSString class]])
83 {
84 motion_type = [[plist objectAtIndex:0] intValue];
85 string = obj;
86 }
87 }
88 }
89
90 if (!string)
91 {
92 /* Use NSStringPboardType. The motion type is set to line-wise if the
93 * string contains at least one EOL character, otherwise it is set to
94 * character-wise (block-wise is never used).
95 */
96 NSMutableString *mstring =
97 [[pb stringForType:NSStringPboardType] mutableCopy];
98 if (!mstring) goto releasepool;
99
100 /* Replace unrecognized end-of-line sequences with \x0a (line feed). */
101 NSRange range = { 0, [mstring length] };
102 unsigned n = [mstring replaceOccurrencesOfString:@"\x0d\x0a"
103 withString:@"\x0a" options:0
104 range:range];
105 if (0 == n)
106 {
107 n = [mstring replaceOccurrencesOfString:@"\x0d" withString:@"\x0a"
108 options:0 range:range];
109 }
Bram Moolenaard43848c2010-07-14 14:28:26 +0200110
Bram Moolenaar164fca32010-07-14 13:58:07 +0200111 /* Scan for newline character to decide whether the string should be
112 * pasted line-wise or character-wise.
113 */
114 motion_type = MCHAR;
115 if (0 < n || NSNotFound != [mstring rangeOfString:@"\n"].location)
116 motion_type = MLINE;
117
118 string = mstring;
119 }
120
121 if (!(MCHAR == motion_type || MLINE == motion_type || MBLOCK == motion_type
122 || MAUTO == motion_type))
123 motion_type = MCHAR;
124
125 char_u *str = (char_u*)[string UTF8String];
126 int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
127
128#ifdef FEAT_MBYTE
129 if (input_conv.vc_type != CONV_NONE)
130 str = string_convert(&input_conv, str, &len);
131#endif
132
133 if (str)
134 clip_yank_selection(motion_type, str, len, cbd);
135
136#ifdef FEAT_MBYTE
137 if (input_conv.vc_type != CONV_NONE)
138 vim_free(str);
139#endif
140
141releasepool:
142 [pool release];
143}
144
145
146/*
147 * Send the current selection to the clipboard.
148 */
149 void
150clip_mch_set_selection(VimClipboard *cbd)
151{
152 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
153
154 /* If the '*' register isn't already filled in, fill it in now. */
155 cbd->owned = TRUE;
156 clip_get_selection(cbd);
157 cbd->owned = FALSE;
Bram Moolenaard43848c2010-07-14 14:28:26 +0200158
Bram Moolenaar164fca32010-07-14 13:58:07 +0200159 /* Get the text to put on the pasteboard. */
160 long_u llen = 0; char_u *str = 0;
161 int motion_type = clip_convert_selection(&str, &llen, cbd);
162 if (motion_type < 0)
163 goto releasepool;
164
165 /* TODO: Avoid overflow. */
166 int len = (int)llen;
167#ifdef FEAT_MBYTE
168 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 }
177#endif
178
179 if (len > 0)
180 {
181 NSString *string = [[NSString alloc]
182 initWithBytes:str length:len encoding:NSUTF8StringEncoding];
183
184 /* See clip_mch_request_selection() for info on pasteboard types. */
185 NSPasteboard *pb = [NSPasteboard generalPasteboard];
186 NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType,
187 NSStringPboardType, nil];
188 [pb declareTypes:supportedTypes owner:nil];
189
190 NSNumber *motion = [NSNumber numberWithInt:motion_type];
191 NSArray *plist = [NSArray arrayWithObjects:motion, string, nil];
192 [pb setPropertyList:plist forType:VimPboardType];
193
194 [pb setString:string forType:NSStringPboardType];
Bram Moolenaard43848c2010-07-14 14:28:26 +0200195
Bram Moolenaar164fca32010-07-14 13:58:07 +0200196 [string release];
197 }
198
199 vim_free(str);
200releasepool:
201 [pool release];
202}
203
204#endif /* FEAT_CLIPBOARD */