comparison lisp/term/mac-win.el @ 61778:1b44d34506e4

Require select. Set selection-coding-system to mac-system-coding-system. Call menu-bar-enable-clipboard. (x-last-selected-text-clipboard, x-last-selected-text-primary) (x-select-enable-clipboard): New variables. (x-select-text, x-get-selection, x-selection-value) (x-get-selection-value, mac-select-convert-to-string) (mac-services-open-file, mac-services-open-selection) (mac-services-insert-text): New functions. (CLIPBOARD, FIND): Put mac-scrap-name property. (com.apple.traditional-mac-plain-text, public.utf16-plain-text) (public.tiff): Put mac-ostype property. (selection-converter-alist): Add entries for them. (mac-application-menu-map): New keymap. (interprogram-cut-function, interprogram-paste-function): Set to x-select-text and x-get-selection-value, respectively. (split-window-keep-point): Set to t.
author YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp>
date Sun, 24 Apr 2005 05:59:52 +0000
parents 7bd93022cd3b
children e5deb8b3a701 08185296b491
comparison
equal deleted inserted replaced
61777:c323711ae68f 61778:1b44d34506e4
72 72
73 (require 'frame) 73 (require 'frame)
74 (require 'mouse) 74 (require 'mouse)
75 (require 'scroll-bar) 75 (require 'scroll-bar)
76 (require 'faces) 76 (require 'faces)
77 ;;(require 'select) 77 (require 'select)
78 (require 'menu-bar) 78 (require 'menu-bar)
79 (require 'fontset) 79 (require 'fontset)
80 (require 'dnd) 80 (require 'dnd)
81 81
82 (defvar x-invocation-args) 82 (defvar x-invocation-args)
1141 (if (eq coding-system 'japanese-shift-jis) 1141 (if (eq coding-system 'japanese-shift-jis)
1142 (define-key key-translation-map [?\x80] "\\")))) 1142 (define-key key-translation-map [?\x80] "\\"))))
1143 1143
1144 (define-key special-event-map [language-change] 'mac-handle-language-change) 1144 (define-key special-event-map [language-change] 'mac-handle-language-change)
1145 1145
1146 ;;;; Selections and cut buffers 1146 ;;;; Selections and Services menu
1147 1147
1148 ;; Setup to use the Mac clipboard. The functions mac-cut-function and 1148 ;; Setup to use the Mac clipboard.
1149 ;; mac-paste-function are defined in mac.c. 1149 (set-selection-coding-system mac-system-coding-system)
1150 (set-selection-coding-system 'compound-text-mac) 1150
1151 1151 ;;; We keep track of the last text selected here, so we can check the
1152 (setq interprogram-cut-function 1152 ;;; current selection against it, and avoid passing back our own text
1153 '(lambda (str push) 1153 ;;; from x-get-selection-value.
1154 (mac-cut-function 1154 (defvar x-last-selected-text-clipboard nil
1155 (encode-coding-string str selection-coding-system t) push))) 1155 "The value of the CLIPBOARD selection last time we selected or
1156 1156 pasted text.")
1157 (setq interprogram-paste-function 1157 (defvar x-last-selected-text-primary nil
1158 '(lambda () 1158 "The value of the PRIMARY X selection last time we selected or
1159 (let ((clipboard (mac-paste-function))) 1159 pasted text.")
1160 (if clipboard 1160
1161 (decode-coding-string clipboard selection-coding-system t))))) 1161 (defcustom x-select-enable-clipboard t
1162 1162 "*Non-nil means cutting and pasting uses the clipboard.
1163 This is in addition to the primary selection."
1164 :type 'boolean
1165 :group 'killing)
1166
1167 ;;; Make TEXT, a string, the primary X selection.
1168 (defun x-select-text (text &optional push)
1169 (x-set-selection 'PRIMARY text)
1170 (setq x-last-selected-text-primary text)
1171 (when x-select-enable-clipboard
1172 (x-set-selection 'CLIPBOARD text)
1173 (setq x-last-selected-text-clipboard text))
1174 )
1175
1176 (defun x-get-selection (&optional type data-type)
1177 "Return the value of a selection.
1178 The argument TYPE (default `PRIMARY') says which selection,
1179 and the argument DATA-TYPE (default `STRING') says
1180 how to convert the data.
1181
1182 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
1183 only a few symbols are commonly used. They conventionally have
1184 all upper-case names. The most often used ones, in addition to
1185 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
1186
1187 DATA-TYPE is usually `STRING', but can also be one of the symbols
1188 in `selection-converter-alist', which see."
1189 (let ((data (x-get-selection-internal (or type 'PRIMARY)
1190 (or data-type 'STRING)))
1191 (coding (or next-selection-coding-system
1192 selection-coding-system)))
1193 (when (and (stringp data)
1194 (setq data-type (get-text-property 0 'foreign-selection data)))
1195 (cond ((eq data-type 'public.utf16-plain-text)
1196 (let ((encoded (and (fboundp 'mac-code-convert-string)
1197 (mac-code-convert-string data
1198 'utf-16 coding))))
1199 (if encoded
1200 (let ((coding-save last-coding-system-used))
1201 (setq data (decode-coding-string encoded coding))
1202 (setq last-coding-system-used coding-save))
1203 (setq data
1204 (decode-coding-string data 'utf-16)))))
1205 ((eq data-type 'com.apple.traditional-mac-plain-text)
1206 (setq data (decode-coding-string data coding))))
1207 (put-text-property 0 (length data) 'foreign-selection data-type data))
1208 data))
1209
1210 (defun x-selection-value (type)
1211 (let (text tiff-image)
1212 (setq text (condition-case nil
1213 (x-get-selection type 'public.utf16-plain-text)
1214 (error nil)))
1215 (if (not text)
1216 (setq text (condition-case nil
1217 (x-get-selection type
1218 'com.apple.traditional-mac-plain-text)
1219 (error nil))))
1220 (if text
1221 (remove-text-properties 0 (length text) '(foreign-selection nil) text))
1222 (setq tiff-image (condition-case nil
1223 (x-get-selection type 'public.tiff)
1224 (error nil)))
1225 (when tiff-image
1226 (remove-text-properties 0 (length tiff-image)
1227 '(foreign-selection nil) tiff-image)
1228 (setq tiff-image (create-image tiff-image 'tiff t))
1229 (or text (setq text " "))
1230 (put-text-property 0 (length text) 'display tiff-image text))
1231 text))
1232
1233 ;;; Return the value of the current selection.
1234 ;;; Treat empty strings as if they were unset.
1235 ;;; If this function is called twice and finds the same text,
1236 ;;; it returns nil the second time. This is so that a single
1237 ;;; selection won't be added to the kill ring over and over.
1238 (defun x-get-selection-value ()
1239 (let (clip-text primary-text)
1240 (when x-select-enable-clipboard
1241 (setq clip-text (x-selection-value 'CLIPBOARD))
1242 (if (string= clip-text "") (setq clip-text nil))
1243
1244 ;; Check the CLIPBOARD selection for 'newness', is it different
1245 ;; from what we remebered them to be last time we did a
1246 ;; cut/paste operation.
1247 (setq clip-text
1248 (cond;; check clipboard
1249 ((or (not clip-text) (string= clip-text ""))
1250 (setq x-last-selected-text-clipboard nil))
1251 ((eq clip-text x-last-selected-text-clipboard) nil)
1252 ((string= clip-text x-last-selected-text-clipboard)
1253 ;; Record the newer string,
1254 ;; so subsequent calls can use the `eq' test.
1255 (setq x-last-selected-text-clipboard clip-text)
1256 nil)
1257 (t
1258 (setq x-last-selected-text-clipboard clip-text))))
1259 )
1260
1261 (setq primary-text (x-selection-value 'PRIMARY))
1262 ;; Check the PRIMARY selection for 'newness', is it different
1263 ;; from what we remebered them to be last time we did a
1264 ;; cut/paste operation.
1265 (setq primary-text
1266 (cond;; check primary selection
1267 ((or (not primary-text) (string= primary-text ""))
1268 (setq x-last-selected-text-primary nil))
1269 ((eq primary-text x-last-selected-text-primary) nil)
1270 ((string= primary-text x-last-selected-text-primary)
1271 ;; Record the newer string,
1272 ;; so subsequent calls can use the `eq' test.
1273 (setq x-last-selected-text-primary primary-text)
1274 nil)
1275 (t
1276 (setq x-last-selected-text-primary primary-text))))
1277
1278 ;; As we have done one selection, clear this now.
1279 (setq next-selection-coding-system nil)
1280
1281 ;; At this point we have recorded the current values for the
1282 ;; selection from clipboard (if we are supposed to) and primary,
1283 ;; So return the first one that has changed (which is the first
1284 ;; non-null one).
1285 (or clip-text primary-text)
1286 ))
1287
1288 (put 'CLIPBOARD 'mac-scrap-name "com.apple.scrap.clipboard")
1289 (if (eq system-type 'darwin)
1290 (put 'FIND 'mac-scrap-name "com.apple.scrap.find"))
1291 (put 'com.apple.traditional-mac-plain-text 'mac-ostype "TEXT")
1292 (put 'public.utf16-plain-text 'mac-ostype "utxt")
1293 (put 'public.tiff 'mac-ostype "TIFF")
1294
1295 (defun mac-select-convert-to-string (selection type value)
1296 (let ((str (cdr (xselect-convert-to-string selection nil value)))
1297 coding)
1298 (setq coding (or next-selection-coding-system selection-coding-system))
1299 (if coding
1300 (setq coding (coding-system-base coding))
1301 (setq coding 'raw-text))
1302 (when str
1303 ;; If TYPE is nil, this is a local request, thus return STR as
1304 ;; is. Otherwise, encode STR.
1305 (if (not type)
1306 str
1307 (let ((inhibit-read-only t))
1308 (remove-text-properties 0 (length str) '(composition nil) str)
1309 (cond
1310 ((eq type 'public.utf16-plain-text)
1311 (let (s)
1312 (when (and (fboundp 'mac-code-convert-string)
1313 (memq coding (find-coding-systems-string str)))
1314 (setq coding (coding-system-change-eol-conversion coding 'mac))
1315 (setq s (mac-code-convert-string
1316 (encode-coding-string str coding)
1317 coding 'utf-16)))
1318 (setq str (or s (encode-coding-string str 'utf-16-mac)))))
1319 ((eq type 'com.apple.traditional-mac-plain-text)
1320 (setq coding (coding-system-change-eol-conversion coding 'mac))
1321 (setq str (encode-coding-string str coding)))
1322 (t
1323 (error "Unknown selection type: %S" type))
1324 )))
1325
1326 (setq next-selection-coding-system nil)
1327 (cons type str))))
1328
1329 (setq selection-converter-alist
1330 (nconc
1331 '((public.utf16-plain-text . mac-select-convert-to-string)
1332 (com.apple.traditional-mac-plain-text . mac-select-convert-to-string)
1333 ;; This is not enabled by default because the `Import Image'
1334 ;; menu makes Emacs crash or hang for unknown reasons.
1335 ;; (public.tiff . nil)
1336 )
1337 selection-converter-alist))
1338
1339 (defun mac-services-open-file ()
1340 (interactive)
1341 (find-file-existing (x-selection-value mac-services-selection)))
1342
1343 (defun mac-services-open-selection ()
1344 (interactive)
1345 (switch-to-buffer (generate-new-buffer "*untitled*"))
1346 (insert (x-selection-value mac-services-selection))
1347 (sit-for 0)
1348 (save-buffer) ; It pops up the save dialog.
1349 )
1350
1351 (defun mac-services-insert-text ()
1352 (interactive)
1353 (let ((text (x-selection-value mac-services-selection)))
1354 (if (not buffer-read-only)
1355 (insert text)
1356 (kill-new text)
1357 (message
1358 (substitute-command-keys
1359 "The text from the Services menu can be accessed with \\[yank]")))))
1360
1361 (defvar mac-application-menu-map (make-sparse-keymap))
1362 (define-key mac-application-menu-map [quit] 'save-buffers-kill-emacs)
1363 (define-key mac-application-menu-map [services perform open-file]
1364 'mac-services-open-file)
1365 (define-key mac-application-menu-map [services perform open-selection]
1366 'mac-services-open-selection)
1367 (define-key mac-application-menu-map [services paste]
1368 'mac-services-insert-text)
1369 (define-key mac-application-menu-map [preferences] 'customize)
1370 (define-key mac-application-menu-map [about] 'display-splash-screen)
1371 (global-set-key [menu-bar application] mac-application-menu-map)
1163 1372
1164 ;;; Do the actual Windows setup here; the above code just defines 1373 ;;; Do the actual Windows setup here; the above code just defines
1165 ;;; functions and variables that we use now. 1374 ;;; functions and variables that we use now.
1166 1375
1167 (setq command-line-args (x-handle-args command-line-args)) 1376 (setq command-line-args (x-handle-args command-line-args))
1392 (generic-char-p key) 1601 (generic-char-p key)
1393 (memq (char-charset key) 1602 (memq (char-charset key)
1394 '(ascii eight-bit-control eight-bit-graphic)) 1603 '(ascii eight-bit-control eight-bit-graphic))
1395 (set-fontset-font fontset key font))) 1604 (set-fontset-font fontset key font)))
1396 (get encoder 'translation-table))))) 1605 (get encoder 'translation-table)))))
1397 1606
1398 (defun create-fontset-from-mac-roman-font (font &optional resolved-font 1607 (defun create-fontset-from-mac-roman-font (font &optional resolved-font
1399 fontset-name) 1608 fontset-name)
1400 "Create a fontset from a Mac roman font FONT. 1609 "Create a fontset from a Mac roman font FONT.
1401 1610
1402 Optional 1st arg RESOLVED-FONT is a resolved name of FONT. If 1611 Optional 1st arg RESOLVED-FONT is a resolved name of FONT. If
1487 1696
1488 (defun x-win-suspend-error () 1697 (defun x-win-suspend-error ()
1489 (error "Suspending an Emacs running under Mac makes no sense")) 1698 (error "Suspending an Emacs running under Mac makes no sense"))
1490 (add-hook 'suspend-hook 'x-win-suspend-error) 1699 (add-hook 'suspend-hook 'x-win-suspend-error)
1491 1700
1701 ;;; Arrange for the kill and yank functions to set and check the clipboard.
1702 (setq interprogram-cut-function 'x-select-text)
1703 (setq interprogram-paste-function 'x-get-selection-value)
1704
1705
1706 ;;; Turn off window-splitting optimization; Mac is usually fast enough
1707 ;;; that this is only annoying.
1708 (setq split-window-keep-point t)
1709
1492 ;; Don't show the frame name; that's redundant. 1710 ;; Don't show the frame name; that's redundant.
1493 (setq-default mode-line-frame-identification " ") 1711 (setq-default mode-line-frame-identification " ")
1494 1712
1495 ;; Turn on support for mouse wheels. 1713 ;; Turn on support for mouse wheels.
1496 (mouse-wheel-mode 1) 1714 (mouse-wheel-mode 1)
1715
1716
1717 ;; Enable CLIPBOARD copy/paste through menu bar commands.
1718 (menu-bar-enable-clipboard)
1497 1719
1498 (defun mac-drag-n-drop (event) 1720 (defun mac-drag-n-drop (event)
1499 "Edit the files listed in the drag-n-drop EVENT. 1721 "Edit the files listed in the drag-n-drop EVENT.
1500 Switch to a buffer editing the last file dropped." 1722 Switch to a buffer editing the last file dropped."
1501 (interactive "e") 1723 (interactive "e")