Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 11 | * os_macosx.m -- Mac specific things for Mac OS X. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Bram Moolenaar | f536bf6 | 2018-03-06 17:55:01 +0100 | [diff] [blame] | 14 | /* 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 Moolenaar | 423f972 | 2010-10-10 17:08:43 +0200 | [diff] [blame] | 22 | /* Avoid a conflict for the definition of Boolean between Mac header files and |
| 23 | * X11 header files. */ |
| 24 | #define NO_X11_INCLUDES |
| 25 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 26 | #include "vim.h" |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 27 | #import <AppKit/AppKit.h> |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 28 | |
| 29 | |
Bram Moolenaar | e00289d | 2010-08-14 21:56:42 +0200 | [diff] [blame] | 30 | /* |
| 31 | * Clipboard support for the console. |
Bram Moolenaar | e00289d | 2010-08-14 21:56:42 +0200 | [diff] [blame] | 32 | */ |
Bram Moolenaar | be7529e | 2020-08-13 21:05:39 +0200 | [diff] [blame] | 33 | #if defined(FEAT_CLIPBOARD) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 34 | |
Bram Moolenaar | 66bd1c9 | 2010-07-14 20:31:44 +0200 | [diff] [blame] | 35 | /* Used to identify clipboard data copied from Vim. */ |
| 36 | |
| 37 | NSString *VimPboardType = @"VimPboardType"; |
| 38 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 39 | void |
Bram Moolenaar | 2fc39ae | 2019-06-14 23:27:29 +0200 | [diff] [blame] | 40 | clip_mch_lose_selection(Clipboard_T *cbd UNUSED) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | int |
Bram Moolenaar | 2fc39ae | 2019-06-14 23:27:29 +0200 | [diff] [blame] | 46 | clip_mch_own_selection(Clipboard_T *cbd UNUSED) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 2fc39ae | 2019-06-14 23:27:29 +0200 | [diff] [blame] | 58 | clip_mch_request_selection(Clipboard_T *cbd) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 59 | { |
| 60 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 61 | |
| 62 | NSPasteboard *pb = [NSPasteboard generalPasteboard]; |
Bram Moolenaar | 1d3dbcf | 2018-10-08 20:07:39 +0200 | [diff] [blame] | 63 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 64 | NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, |
| 65 | NSPasteboardTypeString, nil]; |
| 66 | #else |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 67 | NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, |
| 68 | NSStringPboardType, nil]; |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 69 | #endif |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 70 | NSString *bestType = [pb availableTypeFromArray:supportedTypes]; |
| 71 | if (!bestType) goto releasepool; |
| 72 | |
Bram Moolenaar | 54b08a5 | 2011-06-20 00:25:44 +0200 | [diff] [blame] | 73 | int motion_type = MAUTO; |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 74 | 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 Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 81 | * If this is not the case we fall back on using NSPasteboardTypeString. |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 82 | */ |
| 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 Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 97 | /* Use NSPasteboardTypeString. The motion type is detected automatically. |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 98 | */ |
Bram Moolenaar | 1d3dbcf | 2018-10-08 20:07:39 +0200 | [diff] [blame] | 99 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 100 | NSMutableString *mstring = |
| 101 | [[pb stringForType:NSPasteboardTypeString] mutableCopy]; |
| 102 | #else |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 103 | NSMutableString *mstring = |
| 104 | [[pb stringForType:NSStringPboardType] mutableCopy]; |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 105 | #endif |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 106 | 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 Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 118 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 119 | string = mstring; |
| 120 | } |
| 121 | |
Bram Moolenaar | 54b08a5 | 2011-06-20 00:25:44 +0200 | [diff] [blame] | 122 | /* Default to MAUTO, uses MCHAR or MLINE depending on trailing NL. */ |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 123 | if (!(MCHAR == motion_type || MLINE == motion_type || MBLOCK == motion_type |
| 124 | || MAUTO == motion_type)) |
Bram Moolenaar | 54b08a5 | 2011-06-20 00:25:44 +0200 | [diff] [blame] | 125 | motion_type = MAUTO; |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 126 | |
| 127 | char_u *str = (char_u*)[string UTF8String]; |
| 128 | int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
| 129 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 130 | if (input_conv.vc_type != CONV_NONE) |
| 131 | str = string_convert(&input_conv, str, &len); |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 132 | |
| 133 | if (str) |
| 134 | clip_yank_selection(motion_type, str, len, cbd); |
| 135 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 136 | if (input_conv.vc_type != CONV_NONE) |
| 137 | vim_free(str); |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 138 | |
| 139 | releasepool: |
| 140 | [pool release]; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* |
| 145 | * Send the current selection to the clipboard. |
| 146 | */ |
| 147 | void |
Bram Moolenaar | 2fc39ae | 2019-06-14 23:27:29 +0200 | [diff] [blame] | 148 | clip_mch_set_selection(Clipboard_T *cbd) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 149 | { |
| 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 Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 156 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 157 | /* 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 Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 165 | 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 Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 174 | |
| 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 Moolenaar | 1d3dbcf | 2018-10-08 20:07:39 +0200 | [diff] [blame] | 182 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 183 | NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, |
| 184 | NSPasteboardTypeString, nil]; |
| 185 | #else |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 186 | NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, |
| 187 | NSStringPboardType, nil]; |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 188 | #endif |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 189 | [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 Moolenaar | 1d3dbcf | 2018-10-08 20:07:39 +0200 | [diff] [blame] | 195 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 196 | [pb setString:string forType:NSPasteboardTypeString]; |
| 197 | #else |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 198 | [pb setString:string forType:NSStringPboardType]; |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 199 | #endif |
Bram Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 200 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 201 | [string release]; |
| 202 | } |
| 203 | |
| 204 | vim_free(str); |
| 205 | releasepool: |
| 206 | [pool release]; |
| 207 | } |
| 208 | |
| 209 | #endif /* FEAT_CLIPBOARD */ |
Bram Moolenaar | f536bf6 | 2018-03-06 17:55:01 +0100 | [diff] [blame] | 210 | |
| 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 |