1 ;;; mac-win.el --- parse switches controlling interface with Mac window system -*-coding: iso-2022-7bit;-*-
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Andrew Choi <akochoi@mac.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Mac-win.el: this file is loaded from ../lisp/startup.el when it recognizes
29 ;; that Mac windows are to be used. Command line switches are parsed and those
30 ;; pertaining to Mac are processed and removed from the command line. The
31 ;; Mac display is opened and hooks are set for popping up the initial window.
33 ;; startup.el will then examine startup files, and eventually call the hooks
34 ;; which create the first window(s).
38 ;; These are the standard X switches from the Xt Initialize.c file of
41 ;; Command line Resource Manager string
44 ;; +synchronous *synchronous
45 ;; -background *background
48 ;; -bordercolor *borderColor
49 ;; -borderwidth .borderWidth
55 ;; -foreground *foreground
56 ;; -geometry .geometry
59 ;; -reverse *reverseVideo
61 ;; -selectionTimeout .selectionTimeout
62 ;; -synchronous *synchronous
65 ;; An alist of X options and the function which handles them. See
68 (if (not (eq window-system
'mac
))
69 (error "%s: Loading mac-win.el but not compiled for Mac" (invocation-name)))
79 (eval-when-compile (require 'url
))
81 (defvar mac-charset-info-alist
)
82 (defvar mac-service-selection
)
83 (defvar mac-system-script-code
)
84 (defvar mac-apple-event-map
)
85 (defvar mac-atsu-font-table
)
86 (defvar mac-font-panel-mode
)
87 (defvar mac-ts-active-input-overlay
)
88 (defvar x-invocation-args
)
90 (defvar x-command-line-resources nil
)
92 ;; Handler for switches of the form "-switch value" or "-switch".
93 (defun x-handle-switch (switch)
94 (let ((aelt (assoc switch command-line-x-option-alist
)))
96 (let ((param (nth 3 aelt
))
99 (setq default-frame-alist
100 (cons (cons param value
)
101 default-frame-alist
))
102 (setq default-frame-alist
104 (car x-invocation-args
))
106 x-invocation-args
(cdr x-invocation-args
)))))))
108 ;; Handler for switches of the form "-switch n"
109 (defun x-handle-numeric-switch (switch)
110 (let ((aelt (assoc switch command-line-x-option-alist
)))
112 (let ((param (nth 3 aelt
)))
113 (setq default-frame-alist
115 (string-to-number (car x-invocation-args
)))
118 (cdr x-invocation-args
))))))
120 ;; Handle options that apply to initial frame only
121 (defun x-handle-initial-switch (switch)
122 (let ((aelt (assoc switch command-line-x-option-alist
)))
124 (let ((param (nth 3 aelt
))
125 (value (nth 4 aelt
)))
127 (setq initial-frame-alist
128 (cons (cons param value
)
129 initial-frame-alist
))
130 (setq initial-frame-alist
132 (car x-invocation-args
))
134 x-invocation-args
(cdr x-invocation-args
)))))))
136 ;; Make -iconic apply only to the initial frame!
137 (defun x-handle-iconic (switch)
138 (setq initial-frame-alist
139 (cons '(visibility . icon
) initial-frame-alist
)))
141 ;; Handle the -xrm option.
142 (defun x-handle-xrm-switch (switch)
143 (unless (consp x-invocation-args
)
144 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
145 (setq x-command-line-resources
146 (if (null x-command-line-resources
)
147 (car x-invocation-args
)
148 (concat x-command-line-resources
"\n" (car x-invocation-args
))))
149 (setq x-invocation-args
(cdr x-invocation-args
)))
151 ;; Handle the geometry option
152 (defun x-handle-geometry (switch)
153 (let* ((geo (x-parse-geometry (car x-invocation-args
)))
154 (left (assq 'left geo
))
155 (top (assq 'top geo
))
156 (height (assq 'height geo
))
157 (width (assq 'width geo
)))
158 (if (or height width
)
159 (setq default-frame-alist
160 (append default-frame-alist
162 (if height
(list height
))
163 (if width
(list width
)))
165 (append initial-frame-alist
167 (if height
(list height
))
168 (if width
(list width
)))))
170 (setq initial-frame-alist
171 (append initial-frame-alist
172 '((user-position . t
))
173 (if left
(list left
))
174 (if top
(list top
)))))
175 (setq x-invocation-args
(cdr x-invocation-args
))))
177 ;; Handle the -name option. Set the variable x-resource-name
178 ;; to the option's operand; set the name of
179 ;; the initial frame, too.
180 (defun x-handle-name-switch (switch)
181 (or (consp x-invocation-args
)
182 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
183 (setq x-resource-name
(car x-invocation-args
)
184 x-invocation-args
(cdr x-invocation-args
))
185 (setq initial-frame-alist
(cons (cons 'name x-resource-name
)
186 initial-frame-alist
)))
188 (defvar x-display-name nil
189 "The display name specifying server and frame.")
191 (defun x-handle-display (switch)
192 (setq x-display-name
(car x-invocation-args
)
193 x-invocation-args
(cdr x-invocation-args
)))
195 (defun x-handle-args (args)
196 "Process the X-related command line options in ARGS.
197 This is done before the user's startup file is loaded. They are copied to
198 `x-invocation-args', from which the X-related things are extracted, first
199 the switch (e.g., \"-fg\") in the following code, and possible values
200 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
201 This function returns ARGS minus the arguments that have been processed."
202 ;; We use ARGS to accumulate the args that we don't handle here, to return.
203 (setq x-invocation-args args
205 (while (and x-invocation-args
206 (not (equal (car x-invocation-args
) "--")))
207 (let* ((this-switch (car x-invocation-args
))
208 (orig-this-switch this-switch
)
209 completion argval aelt handler
)
210 (setq x-invocation-args
(cdr x-invocation-args
))
211 ;; Check for long options with attached arguments
212 ;; and separate out the attached option argument into argval.
213 (if (string-match "^--[^=]*=" this-switch
)
214 (setq argval
(substring this-switch
(match-end 0))
215 this-switch
(substring this-switch
0 (1- (match-end 0)))))
216 ;; Complete names of long options.
217 (if (string-match "^--" this-switch
)
219 (setq completion
(try-completion this-switch command-line-x-option-alist
))
220 (if (eq completion t
)
221 ;; Exact match for long option.
223 (if (stringp completion
)
224 (let ((elt (assoc completion command-line-x-option-alist
)))
225 ;; Check for abbreviated long option.
227 (error "Option `%s' is ambiguous" this-switch
))
228 (setq this-switch completion
))))))
229 (setq aelt
(assoc this-switch command-line-x-option-alist
))
230 (if aelt
(setq handler
(nth 2 aelt
)))
233 (let ((x-invocation-args
234 (cons argval x-invocation-args
)))
235 (funcall handler this-switch
))
236 (funcall handler this-switch
))
237 (setq args
(cons orig-this-switch args
)))))
238 (nconc (nreverse args
) x-invocation-args
))
242 ;; Standard Mac cursor shapes
245 (defconst mac-pointer-arrow
0)
246 (defconst mac-pointer-copy-arrow
1)
247 (defconst mac-pointer-alias-arrow
2)
248 (defconst mac-pointer-contextual-menu-arrow
3)
249 (defconst mac-pointer-I-beam
4)
250 (defconst mac-pointer-cross
5)
251 (defconst mac-pointer-plus
6)
252 (defconst mac-pointer-watch
7)
253 (defconst mac-pointer-closed-hand
8)
254 (defconst mac-pointer-open-hand
9)
255 (defconst mac-pointer-pointing-hand
10)
256 (defconst mac-pointer-counting-up-hand
11)
257 (defconst mac-pointer-counting-down-hand
12)
258 (defconst mac-pointer-counting-up-and-down-hand
13)
259 (defconst mac-pointer-spinning
14)
260 (defconst mac-pointer-resize-left
15)
261 (defconst mac-pointer-resize-right
16)
262 (defconst mac-pointer-resize-left-right
17)
263 ;; Mac OS X 10.2 and later
264 (defconst mac-pointer-not-allowed
18)
265 ;; Mac OS X 10.3 and later
266 (defconst mac-pointer-resize-up
19)
267 (defconst mac-pointer-resize-down
20)
268 (defconst mac-pointer-resize-up-down
21)
269 (defconst mac-pointer-poof
22)
272 ;; Standard X cursor shapes that have Mac counterparts
275 (defconst x-pointer-left-ptr mac-pointer-arrow
)
276 (defconst x-pointer-xterm mac-pointer-I-beam
)
277 (defconst x-pointer-crosshair mac-pointer-cross
)
278 (defconst x-pointer-plus mac-pointer-plus
)
279 (defconst x-pointer-watch mac-pointer-watch
)
280 (defconst x-pointer-hand2 mac-pointer-pointing-hand
)
281 (defconst x-pointer-left-side mac-pointer-resize-left
)
282 (defconst x-pointer-right-side mac-pointer-resize-right
)
283 (defconst x-pointer-sb-h-double-arrow mac-pointer-resize-left-right
)
284 (defconst x-pointer-top-side mac-pointer-resize-up
)
285 (defconst x-pointer-bottom-side mac-pointer-resize-down
)
286 (defconst x-pointer-sb-v-double-arrow mac-pointer-resize-up-down
)
293 (defvar x-colors
'("LightGreen"
892 "LightGoldenrodYellow"
893 "light goldenrod yellow"
910 "medium spring green"
1045 "The list of X colors from the `rgb.txt' file.
1046 XConsortium: rgb.txt,v 10.41 94/02/20 18:39:36 rws Exp")
1048 (defun xw-defined-colors (&optional frame
)
1049 "Internal function called by `defined-colors', which see."
1050 (or frame
(setq frame
(selected-frame)))
1051 (let ((all-colors x-colors
)
1053 (defined-colors nil
))
1055 (setq this-color
(car all-colors
)
1056 all-colors
(cdr all-colors
))
1057 (and (color-supported-p this-color frame t
)
1058 (setq defined-colors
(cons this-color defined-colors
))))
1063 (substitute-key-definition 'suspend-emacs
'iconify-or-deiconify-frame
1066 ;; Map certain keypad keys into ASCII characters
1067 ;; that people usually expect.
1068 (define-key function-key-map
[backspace] [?\d])
1069 (define-key function-key-map [delete] [?\d])
1070 (define-key function-key-map [tab] [?\t])
1071 (define-key function-key-map [linefeed] [?\n])
1072 (define-key function-key-map [clear] [?\C-l])
1073 (define-key function-key-map [return] [?\C-m])
1074 (define-key function-key-map [escape] [?\e])
1075 (define-key function-key-map [M-backspace] [?\M-\d])
1076 (define-key function-key-map [M-delete] [?\M-\d])
1077 (define-key function-key-map [M-tab] [?\M-\t])
1078 (define-key function-key-map [M-linefeed] [?\M-\n])
1079 (define-key function-key-map [M-clear] [?\M-\C-l])
1080 (define-key function-key-map [M-return] [?\M-\C-m])
1081 (define-key function-key-map [M-escape] [?\M-\e])
1083 ;; These tell read-char how to convert
1084 ;; these special chars to ASCII.
1085 (put 'backspace 'ascii-character ?\d)
1086 (put 'delete 'ascii-character ?\d)
1087 (put 'tab 'ascii-character ?\t)
1088 (put 'linefeed 'ascii-character ?\n)
1089 (put 'clear 'ascii-character ?\C-l)
1090 (put 'return 'ascii-character ?\C-m)
1091 (put 'escape 'ascii-character ?\e)
1093 ;; Modifier name `ctrl' is an alias of `control'.
1094 (put 'ctrl 'modifier-value (get 'control 'modifier-value))
1097 ;;;; Script codes and coding systems
1098 (defconst mac-script-code-coding-systems
1099 '((0 . mac-roman) ; smRoman
1100 (1 . japanese-shift-jis) ; smJapanese
1101 (2 . chinese-big5) ; smTradChinese
1102 (3 . korean-iso-8bit) ; smKorean
1103 (7 . mac-cyrillic) ; smCyrillic
1104 (25 . chinese-iso-8bit) ; smSimpChinese
1105 (29 . mac-centraleurroman) ; smCentralEuroRoman
1107 "Alist of Mac script codes vs Emacs coding systems.")
1109 (defun mac-add-charset-info (xlfd-charset mac-text-encoding)
1110 "Add a character set to display with Mac fonts.
1111 Create an entry in `mac-charset-info-alist'.
1112 XLFD-CHARSET is a string which will appear in the XLFD font name
1113 to identify the character set. MAC-TEXT-ENCODING is the
1114 correspoinding TextEncodingBase value."
1115 (add-to-list 'mac-charset-info-alist
1116 (list xlfd-charset mac-text-encoding
1117 (cdr (assq mac-text-encoding
1118 mac-script-code-coding-systems)))))
1120 (setq mac-charset-info-alist nil)
1121 (mac-add-charset-info "mac-roman" 0)
1122 (mac-add-charset-info "jisx0208.1983-sjis" 1)
1123 (mac-add-charset-info "jisx0201.1976-0" 1)
1124 (mac-add-charset-info "big5-0" 2)
1125 (mac-add-charset-info "ksc5601.1989-0" 3)
1126 (mac-add-charset-info "mac-cyrillic" 7)
1127 (mac-add-charset-info "gb2312.1980-0" 25)
1128 (mac-add-charset-info "mac-centraleurroman" 29)
1129 (mac-add-charset-info "mac-symbol" 33)
1130 (mac-add-charset-info "adobe-fontspecific" 33) ; for X-Symbol
1131 (mac-add-charset-info "mac-dingbats" 34)
1132 (mac-add-charset-info "iso10646-1" 126) ; for ATSUI
1134 (cp-make-coding-system
1136 [?\\e,AD\e(B ?\\e$,1 \e(B ?\\e$,1 !\e(B ?\\e,AI\e(B ?\\e$,1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&|\e(B ?\\e$,1&}\e(B ?\\e$,1&\x7f\e(B
1168 ?\\e$,1' \e(B ?\\e$,1&x\e(B ?\\e$,1'!\e(B ?\\e$,1'#\e(B ?\\e$,1'$\e(B ?\\e$,1'%\e(B ?\\e$,1'B\e(B ?\\e$,1')\e(B ?\\e$,1&~\e(B ?\\e$,1'(\e(B ?\\e$,1&v\e(B ?\[ ?\\e$,1xT\e(B ?\] ?\\e$,1ye\e(B ?\_
1169 ?\\e$,3bE\e(B ?\\e$,1'1\e(B ?\\e$,1'2\e(B ?\\e$,1'G\e(B ?\\e$,1'4\e(B ?\\e$,1'5\e(B ?\\e$,1'F\e(B ?\\e$,1'3\e(B ?\\e$,1'7\e(B ?\\e$,1'9\e(B ?\\e$,1'U\e(B ?\\e$,1':\e(B ?\\e$,1';\e(B ?\\e$,1'<\e(B ?\\e$,1'=\e(B ?\\e$,1'?\e(B
1170 ?\\e$,1'@\e(B ?\\e$,1'8\e(B ?\\e$,1'A\e(B ?\\e$,1'C\e(B ?\\e$,1'D\e(B ?\\e$,1'E\e(B ?\\e$,1'V\e(B ?\\e$,1'I\e(B ?\\e$,1'>\e(B ?\\e$,1'H\e(B ?\\e$,1'6\e(B ?\{ ?\| ?\} ?\\e$,1x\\e(B]
1171 (make-vector (- 160 127) nil)
1172 ;; mac-symbol (160..254) -> emacs-mule mapping
1173 ;; Mapping of the following characters are changed from the
1175 ;; 0xE2 0x00AE+0xF87F -> 0x00AE # REGISTERED SIGN, alternate: sans serif
1176 ;; 0xE3 0x00A9+0xF87F -> 0x00A9 # COPYRIGHT SIGN, alternate: sans serif
1177 ;; 0xE4 0x2122+0xF87F -> 0x2122 # TRADE MARK SIGN, alternate: sans serif
1178 [?\\e$,1tL\e(B ?\\e$,1'R\e(B ?\\e$,1s2\e(B ?\\e$,1y$\e(B ?\\e$,1sD\e(B ?\\e$,1x>\e(B ?\\e$,1!R\e(B ?\\e$,2#c\e(B ?\\e$,2#f\e(B ?\\e$,2#e\e(B ?\\e$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\e$,1{ \e(B ?\\e$,1|N\e(B ?\\e$,1{!\e(B ?\\e$,1|>\e(B ?\\e$,1|?\e(B ?\\e$,1|@\e(B ?\\e$,1|D\e(B ?\\e$,1|E\e(B ?\\e$,1|F\e(B ?\\e$,1|K\e(B ?\\e$,1|L\e(B ?\\e$,1|M\e(B
1186 (setq translation-table
1187 (make-translation-table-from-vector encoding-vector))
1188 ;; (define-translation-table 'mac-symbol-decoder translation-table)
1189 (define-translation-table 'mac-symbol-encoder
1190 (char-table-extra-slot translation-table 0)))
1195 (make-vector 32 nil)
1196 ;; mac-dingbats (32..126) -> emacs-mule mapping
1197 [?\ ?\\e$,2%A\e(B ?\\e$,2%B\e(B ?\\e$,2%C\e(B ?\\e$,2%D\e(B ?\\e$,2"n\e(B ?\\e$,2%F\e(B ?\\e$,2%G\e(B ?\\e$,2%H\e(B ?\\e$,2%I\e(B ?\\e$,2"{\e(B ?\\e$,2"~\e(B ?\\e$,2%L\e(B ?\\e$,2%M\e(B ?\\e$,2%N\e(B ?\\e$,2%O\e(B
1198 ?\\e$,2%P\e(B ?\\e$,2%Q\e(B ?\\e$,2%R\e(B ?\\e$,2%S\e(B ?\\e$,2%T\e(B ?\\e$,2%U\e(B ?\\e$,2%V\e(B ?\\e$,2%W\e(B ?\\e$,2%X\e(B ?\\e$,2%Y\e(B ?\\e$,2%Z\e(B ?\\e$,2%[\e(B ?\\e$,2%\\e(B ?\\e$,2%]\e(B ?\\e$,2%^\e(B ?\\e$,2%_\e(B
1199 ?\\e$,2%`\e(B ?\\e$,2%a\e(B ?\\e$,2%b\e(B ?\\e$,2%c\e(B ?\\e$,2%d\e(B ?\\e$,2%e\e(B ?\\e$,2%f\e(B ?\\e$,2%g\e(B ?\\e$,2"e\e(B ?\\e$,2%i\e(B ?\\e$,2%j\e(B ?\\e$,2%k\e(B ?\\e$,2%l\e(B ?\\e$,2%m\e(B ?\\e$,2%n\e(B ?\\e$,2%o\e(B
1200 ?\\e$,2%p\e(B ?\\e$,2%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/\e(B
1214 ;; mac-dingbats (241..254) -> emacs-mule mapping
1215 ?\\e$,2'1\e(B ?\\e$,2'2\e(B ?\\e$,2'3\e(B ?\\e$,2'4\e(B ?\\e$,2'5\e(B ?\\e$,2'6\e(B ?\\e$,2'7\e(B ?\\e$,2'8\e(B ?\\e$,2'9\e(B ?\\e$,2':\e(B ?\\e$,2';\e(B ?\\e$,2'<\e(B ?\\e$,2'=\e(B ?\\e$,2'>\e(B
1218 (setq translation-table
1219 (make-translation-table-from-vector encoding-vector))
1220 ;; (define-translation-table 'mac-dingbats-decoder translation-table)
1221 (define-translation-table 'mac-dingbats-encoder
1222 (char-table-extra-slot translation-table 0)))
1224 (defconst mac-system-coding-system
1225 (let ((base (or (cdr (assq mac-system-script-code
1226 mac-script-code-coding-systems))
1228 (if (eq system-type 'darwin)
1230 (coding-system-change-eol-conversion base 'mac)))
1231 "Coding system derived from the system script code.")
1233 (set-selection-coding-system mac-system-coding-system)
1236 ;;;; Keyboard layout/language change events
1237 (defun mac-handle-language-change (event)
1238 "Set keyboard coding system to what is specified in EVENT."
1240 (let ((coding-system
1241 (cdr (assq (car (cadr event)) mac-script-code-coding-systems))))
1242 (set-keyboard-coding-system (or coding-system 'mac-roman))
1243 ;; MacJapanese maps reverse solidus to ?\x80.
1244 (if (eq coding-system 'japanese-shift-jis)
1245 (define-key key-translation-map [?\x80] "\\"))))
1247 (define-key special-event-map [language-change] 'mac-handle-language-change)
1250 ;;;; Conversion between common flavors and Lisp string.
1252 (defconst mac-text-encoding-ascii #x600
1253 "ASCII text encoding.")
1255 (defconst mac-text-encoding-mac-japanese-basic-variant #x20001
1256 "MacJapanese text encoding without Apple double-byte extensions.")
1258 (defun mac-utxt-to-string (data &optional coding-system)
1259 (or coding-system (setq coding-system mac-system-coding-system))
1261 (and (eq system-type 'darwin)
1262 (eq (coding-system-base coding-system) 'japanese-shift-jis)
1263 mac-text-encoding-mac-japanese-basic-variant))
1264 (str (and (fboundp 'mac-code-convert-string)
1265 (mac-code-convert-string data nil
1266 (or encoding coding-system)))))
1268 (setq str (decode-coding-string str coding-system))
1269 (if (eq encoding mac-text-encoding-mac-japanese-basic-variant)
1270 ;; Does it contain Apple one-byte extensions other than
1272 (if (string-match "[\xa0\xfd-\xff]" str)
1275 (unless (mac-code-convert-string data nil mac-text-encoding-ascii)
1276 (subst-char-in-string ?\x5c ?\\e(J\\e(B str t)
1277 (subst-char-in-string ?\x80 ?\\ str t)))))
1279 (decode-coding-string data
1280 (if (eq (byteorder) ?B) 'utf-16be 'utf-16le)))))
1282 (defun mac-string-to-utxt (string &optional coding-system)
1283 (or coding-system (setq coding-system mac-system-coding-system))
1284 (let (data encoding)
1285 (when (and (fboundp 'mac-code-convert-string)
1286 (memq (coding-system-base coding-system)
1287 (find-coding-systems-string string)))
1289 (coding-system-change-eol-conversion coding-system 'mac))
1291 (when (and (eq system-type 'darwin)
1292 (eq coding-system 'japanese-shift-jis-mac))
1293 (setq encoding mac-text-encoding-mac-japanese-basic-variant)
1294 (setq str (subst-char-in-string ?\\ ?\x80 str))
1295 (subst-char-in-string ?\\e(J\\e(B ?\x5c str t)
1297 (if (string-match "\\`[\x00-\x7f]*\\'" str)
1300 (setq data (mac-code-convert-string
1301 (encode-coding-string str coding-system)
1302 (or encoding coding-system) nil)))))
1303 (or data (encode-coding-string string (if (eq (byteorder) ?B)
1307 (defun mac-TEXT-to-string (data &optional coding-system)
1308 (or coding-system (setq coding-system mac-system-coding-system))
1309 (prog1 (setq data (decode-coding-string data coding-system))
1310 (when (eq (coding-system-base coding-system) 'japanese-shift-jis)
1311 ;; (subst-char-in-string ?\x5c ?\\e(J\\e(B data t)
1312 (subst-char-in-string ?\x80 ?\\ data t))))
1314 (defun mac-string-to-TEXT (string &optional coding-system)
1315 (or coding-system (setq coding-system mac-system-coding-system))
1316 (let ((encodables (find-coding-systems-string string))
1317 (rest mac-script-code-coding-systems))
1318 (unless (memq (coding-system-base coding-system) encodables)
1319 (while (and rest (not (memq (cdar rest) encodables)))
1320 (setq rest (cdr rest)))
1322 (setq coding-system (cdar rest)))))
1324 (coding-system-change-eol-conversion coding-system 'mac))
1325 (when (eq coding-system 'japanese-shift-jis-mac)
1326 ;; (setq string (subst-char-in-string ?\\ ?\x80 string))
1327 (setq string (subst-char-in-string ?\\e(J\\e(B ?\x5c string)))
1328 (encode-coding-string string coding-system))
1330 (defun mac-furl-to-string (data)
1331 ;; Remove a trailing nul character.
1332 (let ((len (length data)))
1333 (if (and (> len 0) (= (aref data (1- len)) ?\0))
1334 (substring data 0 (1- len))
1337 (defun mac-TIFF-to-string (data &optional text)
1338 (prog1 (or text (setq text (copy-sequence " ")))
1339 (put-text-property 0 (length text) 'display (create-image data 'tiff t)
1344 ;;; We keep track of the last text selected here, so we can check the
1345 ;;; current selection against it, and avoid passing back our own text
1346 ;;; from x-get-selection-value.
1347 (defvar x-last-selected-text-clipboard nil
1348 "The value of the CLIPBOARD selection last time we selected or
1350 (defvar x-last-selected-text-primary nil
1351 "The value of the PRIMARY X selection last time we selected or
1354 (defcustom x-select-enable-clipboard t
1355 "*Non-nil means cutting and pasting uses the clipboard.
1356 This is in addition to the primary selection."
1360 ;;; Make TEXT, a string, the primary X selection.
1361 (defun x-select-text (text &optional push)
1362 (x-set-selection 'PRIMARY text)
1363 (setq x-last-selected-text-primary text)
1364 (if (not x-select-enable-clipboard)
1365 (setq x-last-selected-text-clipboard nil)
1366 (x-set-selection 'CLIPBOARD text)
1367 (setq x-last-selected-text-clipboard text))
1370 (defun x-get-selection (&optional type data-type)
1371 "Return the value of a selection.
1372 The argument TYPE (default `PRIMARY') says which selection,
1373 and the argument DATA-TYPE (default `STRING') says
1374 how to convert the data.
1376 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
1377 only a few symbols are commonly used. They conventionally have
1378 all upper-case names. The most often used ones, in addition to
1379 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
1381 DATA-TYPE is usually `STRING', but can also be one of the symbols
1382 in `selection-converter-alist', which see."
1383 (let ((data (x-get-selection-internal (or type 'PRIMARY)
1384 (or data-type 'STRING)))
1385 (coding (or next-selection-coding-system
1386 selection-coding-system)))
1387 (when (and (stringp data)
1388 (setq data-type (get-text-property 0 'foreign-selection data)))
1389 (cond ((eq data-type 'public.utf16-plain-text)
1390 (setq data (mac-utxt-to-string data coding)))
1391 ((eq data-type 'com.apple.traditional-mac-plain-text)
1392 (setq data (mac-TEXT-to-string data coding)))
1393 ((eq data-type 'public.file-url)
1394 (setq data (mac-furl-to-string data))))
1395 (put-text-property 0 (length data) 'foreign-selection data-type data))
1398 (defun x-selection-value (type)
1399 (let ((data-types '(public.utf16-plain-text
1400 com.apple.traditional-mac-plain-text
1403 (while (and (null text) data-types)
1404 (setq text (condition-case nil
1405 (x-get-selection type (car data-types))
1407 (setq data-types (cdr data-types)))
1409 (remove-text-properties 0 (length text) '(foreign-selection nil) text))
1410 (setq tiff-image (condition-case nil
1411 (x-get-selection type 'public.tiff)
1414 (remove-text-properties 0 (length tiff-image)
1415 '(foreign-selection nil) tiff-image)
1416 (setq text (mac-TIFF-to-string tiff-image text)))
1419 ;;; Return the value of the current selection.
1420 ;;; Treat empty strings as if they were unset.
1421 ;;; If this function is called twice and finds the same text,
1422 ;;; it returns nil the second time. This is so that a single
1423 ;;; selection won't be added to the kill ring over and over.
1424 (defun x-get-selection-value ()
1425 (let (clip-text primary-text)
1426 (if (not x-select-enable-clipboard)
1427 (setq x-last-selected-text-clipboard nil)
1428 (setq clip-text (x-selection-value 'CLIPBOARD))
1429 (if (string= clip-text "") (setq clip-text nil))
1431 ;; Check the CLIPBOARD selection for 'newness', is it different
1432 ;; from what we remebered them to be last time we did a
1433 ;; cut/paste operation.
1435 (cond;; check clipboard
1436 ((or (not clip-text) (string= clip-text ""))
1437 (setq x-last-selected-text-clipboard nil))
1438 ((eq clip-text x-last-selected-text-clipboard) nil)
1439 ((string= clip-text x-last-selected-text-clipboard)
1440 ;; Record the newer string,
1441 ;; so subsequent calls can use the `eq' test.
1442 (setq x-last-selected-text-clipboard clip-text)
1445 (setq x-last-selected-text-clipboard clip-text))))
1448 (setq primary-text (x-selection-value 'PRIMARY))
1449 ;; Check the PRIMARY selection for 'newness', is it different
1450 ;; from what we remebered them to be last time we did a
1451 ;; cut/paste operation.
1453 (cond;; check primary selection
1454 ((or (not primary-text) (string= primary-text ""))
1455 (setq x-last-selected-text-primary nil))
1456 ((eq primary-text x-last-selected-text-primary) nil)
1457 ((string= primary-text x-last-selected-text-primary)
1458 ;; Record the newer string,
1459 ;; so subsequent calls can use the `eq' test.
1460 (setq x-last-selected-text-primary primary-text)
1463 (setq x-last-selected-text-primary primary-text))))
1465 ;; As we have done one selection, clear this now.
1466 (setq next-selection-coding-system nil)
1468 ;; At this point we have recorded the current values for the
1469 ;; selection from clipboard (if we are supposed to) and primary,
1470 ;; So return the first one that has changed (which is the first
1472 (or clip-text primary-text)
1475 (put 'CLIPBOARD 'mac-scrap-name "com.apple.scrap.clipboard")
1476 (when (eq system-type 'darwin)
1477 (put 'FIND 'mac-scrap-name "com.apple.scrap.find")
1478 (put 'PRIMARY 'mac-scrap-name
1479 (format "org.gnu.Emacs.%d.selection.PRIMARY" (emacs-pid))))
1480 (put 'com.apple.traditional-mac-plain-text 'mac-ostype "TEXT")
1481 (put 'public.utf16-plain-text 'mac-ostype "utxt")
1482 (put 'public.tiff 'mac-ostype "TIFF")
1483 (put 'public.file-url 'mac-ostype "furl")
1485 (defun mac-select-convert-to-string (selection type value)
1486 (let ((str (cdr (xselect-convert-to-string selection nil value)))
1487 (coding (or next-selection-coding-system selection-coding-system)))
1489 ;; If TYPE is nil, this is a local request, thus return STR as
1490 ;; is. Otherwise, encode STR.
1493 (let ((inhibit-read-only t))
1494 (remove-text-properties 0 (length str) '(composition nil) str)
1496 ((eq type 'public.utf16-plain-text)
1497 (setq str (mac-string-to-utxt str coding)))
1498 ((eq type 'com.apple.traditional-mac-plain-text)
1499 (setq str (mac-string-to-TEXT str coding)))
1501 (error "Unknown selection type: %S" type))
1504 (setq next-selection-coding-system nil)
1507 (defun mac-select-convert-to-file-url (selection type value)
1508 (let ((filename (xselect-convert-to-filename selection type value))
1509 (coding (or file-name-coding-system default-file-name-coding-system)))
1510 (if (and filename coding)
1511 (setq filename (encode-coding-string filename coding)))
1513 (concat "file://localhost"
1514 (mapconcat 'url-hexify-string
1515 (split-string filename "/") "/")))))
1517 (setq selection-converter-alist
1519 '((public.utf16-plain-text . mac-select-convert-to-string)
1520 (com.apple.traditional-mac-plain-text . mac-select-convert-to-string)
1521 ;; This is not enabled by default because the `Import Image'
1522 ;; menu makes Emacs crash or hang for unknown reasons.
1523 ;; (public.tiff . nil)
1524 (public.file-url . mac-select-convert-to-file-url)
1526 selection-converter-alist))
1528 ;;;; Apple events, HICommand events, and Services menu
1531 (put 'core-event 'mac-apple-event-class "aevt") ; kCoreEventClass
1532 (put 'internet-event 'mac-apple-event-class "GURL") ; kAEInternetEventClass
1536 (put 'open-application 'mac-apple-event-id "oapp") ; kAEOpenApplication
1537 (put 'reopen-application 'mac-apple-event-id "rapp") ; kAEReopenApplication
1538 (put 'open-documents 'mac-apple-event-id "odoc") ; kAEOpenDocuments
1539 (put 'print-documents 'mac-apple-event-id "pdoc") ; kAEPrintDocuments
1540 (put 'open-contents 'mac-apple-event-id "ocon") ; kAEOpenContents
1541 (put 'quit-application 'mac-apple-event-id "quit") ; kAEQuitApplication
1542 (put 'application-died 'mac-apple-event-id "obit") ; kAEApplicationDied
1543 (put 'show-preferences 'mac-apple-event-id "pref") ; kAEShowPreferences
1544 (put 'autosave-now 'mac-apple-event-id "asav") ; kAEAutosaveNow
1545 ;; kAEInternetEventClass
1546 (put 'get-url 'mac-apple-event-id "GURL") ; kAEGetURL
1547 ;; Converted HI command events
1548 (put 'about 'mac-apple-event-id "abou") ; kHICommandAbout
1550 (defmacro mac-event-spec (event)
1553 (defmacro mac-event-ae (event)
1556 (defun mac-ae-parameter (ae &optional keyword type)
1557 (or keyword (setq keyword "----")) ;; Direct object.
1558 (if (not (and (consp ae) (equal (car ae) "aevt")))
1559 (error "Not an Apple event: %S" ae)
1560 (let ((type-data (cdr (assoc keyword (cdr ae))))
1562 (when (and type type-data (not (equal type (car type-data))))
1563 (setq data (mac-coerce-ae-data (car type-data) (cdr type-data) type))
1564 (setq type-data (if data (cons type data) nil)))
1567 (defun mac-ae-list (ae &optional keyword type)
1568 (or keyword (setq keyword "----")) ;; Direct object.
1569 (let ((desc (mac-ae-parameter ae keyword "list")))
1572 ((not (equal (car desc) "list"))
1573 (error "Parameter for \"%s\" is not a list" keyword))
1579 (mac-coerce-ae-data (car type-data) (cdr type-data) type))
1582 (defun mac-ae-number (ae keyword)
1583 (let ((type-data (mac-ae-parameter ae keyword))
1586 (setq str (mac-coerce-ae-data (car type-data)
1587 (cdr type-data) "TEXT")))
1588 (string-to-number str)
1591 (defun mac-bytes-to-integer (bytes &optional from to)
1592 (or from (setq from 0))
1593 (or to (setq to (length bytes)))
1594 (let* ((len (- to from))
1595 (extended-sign-len (- (1+ (ceiling (log most-positive-fixnum 2)))
1599 (setq result (logior (lsh result 8)
1600 (aref bytes (+ from (if (eq (byteorder) ?B) i
1602 (if (> extended-sign-len 0)
1603 (ash (lsh result extended-sign-len) (- extended-sign-len))
1606 (defun mac-ae-selection-range (ae)
1607 ;; #pragma options align=mac68k
1608 ;; typedef struct SelectionRange {
1609 ;; short unused1; // 0 (not used)
1610 ;; short lineNum; // line to select (<0 to specify range)
1611 ;; long startRange; // start of selection range (if line < 0)
1612 ;; long endRange; // end of selection range (if line < 0)
1613 ;; long unused2; // 0 (not used)
1614 ;; long theDate; // modification date/time
1615 ;; } SelectionRange;
1616 ;; #pragma options align=reset
1617 (let ((range-bytes (cdr (mac-ae-parameter ae "kpos" "TEXT"))))
1619 (list (mac-bytes-to-integer range-bytes 2 4)
1620 (mac-bytes-to-integer range-bytes 4 8)
1621 (mac-bytes-to-integer range-bytes 8 12)
1622 (mac-bytes-to-integer range-bytes 16 20)))))
1624 ;; On Mac OS X 10.4 and later, the `open-document' event contains an
1625 ;; optional parameter keyAESearchText from the Spotlight search.
1626 (defun mac-ae-text-for-search (ae)
1627 (let ((utf8-text (cdr (mac-ae-parameter ae "stxt" "utf8"))))
1629 (decode-coding-string utf8-text 'utf-8))))
1631 (defun mac-ae-text (ae)
1632 (or (cdr (mac-ae-parameter ae nil "TEXT"))
1633 (error "No text in Apple event.")))
1635 (defun mac-ae-frame (ae &optional keyword type)
1636 (let ((bytes (cdr (mac-ae-parameter ae keyword type))))
1637 (if (or (null bytes) (/= (length bytes) 4))
1638 (error "No window reference in Apple event.")
1639 (let ((window-id (mac-coerce-ae-data "long" bytes "TEXT"))
1642 (while (and (null frame) rest)
1643 (if (string= (frame-parameter (car rest) 'window-id) window-id)
1644 (setq frame (car rest)))
1645 (setq rest (cdr rest)))
1648 (defun mac-ae-script-language (ae keyword)
1649 ;; struct WritingCode {
1650 ;; ScriptCode theScriptCode;
1651 ;; LangCode theLangCode;
1653 (let ((bytes (cdr (mac-ae-parameter ae keyword "intl"))))
1655 (cons (mac-bytes-to-integer bytes 0 2)
1656 (mac-bytes-to-integer bytes 2 4)))))
1658 (defun mac-bytes-to-text-range (bytes &optional from to)
1659 ;; struct TextRange {
1662 ;; short fHiliteStyle;
1664 (or from (setq from 0))
1665 (or to (setq to (length bytes)))
1666 (and (= (- to from) (+ 4 4 2))
1667 (list (mac-bytes-to-integer bytes from (+ from 4))
1668 (mac-bytes-to-integer bytes (+ from 4) (+ from 8))
1669 (mac-bytes-to-integer bytes (+ from 8) to))))
1671 (defun mac-ae-text-range-array (ae keyword)
1672 ;; struct TextRangeArray {
1673 ;; short fNumOfRanges;
1674 ;; TextRange fRange[1];
1676 (let* ((bytes (cdr (mac-ae-parameter ae keyword "tray")))
1677 (len (length bytes))
1679 (when (and bytes (>= len 2)
1681 (setq nranges (mac-bytes-to-integer bytes 0 2))
1682 (= len (+ 2 (* nranges 10)))))
1683 (setq result (make-vector nranges nil))
1684 (dotimes (i nranges)
1686 (mac-bytes-to-text-range bytes (+ (* i 10) 2)
1690 (defun mac-ae-open-documents (event)
1691 "Open the documents specified by the Apple event EVENT."
1693 (let ((ae (mac-event-ae event)))
1694 (dolist (file-name (mac-ae-list ae nil 'undecoded-file-name))
1696 (dnd-open-local-file
1698 (mapconcat 'url-hexify-string
1699 (split-string file-name "/") "/")) nil)))
1700 (let ((selection-range (mac-ae-selection-range ae))
1701 (search-text (mac-ae-text-for-search ae)))
1702 (cond (selection-range
1703 (let ((line (car selection-range))
1704 (start (cadr selection-range))
1705 (end (nth 2 selection-range)))
1708 (if (and (> start 0) (> end 0))
1709 (progn (set-mark start)
1710 (goto-char end))))))
1711 ((stringp search-text)
1713 (mapconcat 'regexp-quote (split-string search-text) "\\|")
1715 (select-frame-set-input-focus (selected-frame)))
1717 (defun mac-ae-get-url (event)
1718 "Open the URL specified by the Apple event EVENT.
1719 Currently the `mailto' scheme is supported."
1721 (let* ((ae (mac-event-ae event))
1722 (parsed-url (url-generic-parse-url (mac-ae-text ae))))
1723 (if (string= (url-type parsed-url) "mailto")
1724 (url-mailto parsed-url)
1725 (mac-resume-apple-event ae t))))
1727 (setq mac-apple-event-map (make-sparse-keymap))
1729 ;; Received when Emacs is launched without associated documents.
1730 ;; Accept it as an Apple event, but no Emacs event is generated so as
1731 ;; not to erase the splash screen.
1732 (define-key mac-apple-event-map [core-event open-application] 0)
1734 ;; Received when a dock or application icon is clicked and Emacs is
1735 ;; already running. Simply ignored. Another idea is to make a new
1736 ;; frame if all frames are invisible.
1737 (define-key mac-apple-event-map [core-event reopen-application] 'ignore)
1739 (define-key mac-apple-event-map [core-event open-documents]
1740 'mac-ae-open-documents)
1741 (define-key mac-apple-event-map [core-event show-preferences] 'customize)
1742 (define-key mac-apple-event-map [core-event quit-application]
1743 'save-buffers-kill-emacs)
1745 (define-key mac-apple-event-map [internet-event get-url] 'mac-ae-get-url)
1747 (define-key mac-apple-event-map [hi-command about] 'display-splash-screen)
1749 ;;; Converted Carbon Events
1750 (defun mac-handle-toolbar-switch-mode (event)
1751 "Toggle visibility of tool-bars in response to EVENT.
1752 With no keyboard modifiers, it toggles the visibility of the
1753 frame where the tool-bar toggle button was pressed. With some
1754 modifiers, it changes global tool-bar visibility setting."
1756 (let* ((ae (mac-event-ae event))
1757 (modifiers (cdr (mac-ae-parameter ae "kmod"))))
1758 (if (and modifiers (not (string= modifiers "\000\000\000\000")))
1759 ;; Globally toggle tool-bar-mode if some modifier key is pressed.
1761 (let ((frame (mac-ae-frame ae)))
1762 (set-frame-parameter frame 'tool-bar-lines
1763 (if (= (frame-parameter frame 'tool-bar-lines) 0)
1766 ;; kEventClassWindow/kEventWindowToolbarSwitchMode
1767 (define-key mac-apple-event-map [window toolbar-switch-mode]
1768 'mac-handle-toolbar-switch-mode)
1771 (when (fboundp 'mac-set-font-panel-visibility)
1773 (define-minor-mode mac-font-panel-mode
1774 "Toggle use of the font panel.
1775 With numeric ARG, display the font panel if and only if ARG is positive."
1779 (mac-set-font-panel-visibility mac-font-panel-mode))
1781 (defun mac-handle-font-panel-closed (event)
1782 "Update internal status in response to font panel closed EVENT."
1784 ;; Synchronize with the minor mode variable.
1785 (mac-font-panel-mode 0))
1787 (defun mac-handle-font-selection (event)
1788 "Change default face attributes according to font selection EVENT."
1790 (let* ((ae (mac-event-ae event))
1791 (fm-font-size (mac-ae-number ae "fmsz"))
1792 (atsu-font-id (mac-ae-number ae "auid"))
1793 (attribute-values (gethash atsu-font-id mac-atsu-font-table)))
1795 (setq attribute-values
1796 `(:height ,(* 10 fm-font-size) ,@attribute-values)))
1797 (apply 'set-face-attribute 'default (selected-frame) attribute-values)))
1799 ;; kEventClassFont/kEventFontPanelClosed
1800 (define-key mac-apple-event-map [font panel-closed]
1801 'mac-handle-font-panel-closed)
1802 ;; kEventClassFont/kEventFontSelection
1803 (define-key mac-apple-event-map [font selection] 'mac-handle-font-selection)
1805 (define-key-after menu-bar-showhide-menu [mac-font-panel-mode]
1806 (menu-bar-make-mm-toggle mac-font-panel-mode
1808 "Show the font panel as a floating dialog")
1811 ) ;; (fboundp 'mac-set-font-panel-visibility)
1814 (defvar mac-ts-active-input-buf ""
1815 "Byte sequence of the current Mac TSM active input area.")
1816 (defvar mac-ts-update-active-input-area-seqno 0
1817 "Number of processed update-active-input-area events.")
1818 (setq mac-ts-active-input-overlay (make-overlay 0 0))
1820 (defface mac-ts-caret-position
1821 '((t :inverse-video t))
1822 "Face for caret position in Mac TSM active input area.
1823 This is used when the active input area is displayed either in
1824 the echo area or in a buffer where the cursor is not displayed."
1827 (defface mac-ts-raw-text
1829 "Face for raw text in Mac TSM active input area."
1832 (defface mac-ts-selected-raw-text
1834 "Face for selected raw text in Mac TSM active input area."
1837 (defface mac-ts-converted-text
1838 '((((background dark)) :underline "gray20")
1839 (t :underline "gray80"))
1840 "Face for converted text in Mac TSM active input area."
1843 (defface mac-ts-selected-converted-text
1845 "Face for selected converted text in Mac TSM active input area."
1848 (defface mac-ts-block-fill-text
1850 "Face for block fill text in Mac TSM active input area."
1853 (defface mac-ts-outline-text
1855 "Face for outline text in Mac TSM active input area."
1858 (defface mac-ts-selected-text
1860 "Face for selected text in Mac TSM active input area."
1863 (defface mac-ts-no-hilite
1864 '((t :inherit default))
1865 "Face for no hilite in Mac TSM active input area."
1868 (defconst mac-ts-hilite-style-faces
1869 '((2 . mac-ts-raw-text) ; kTSMHiliteRawText
1870 (3 . mac-ts-selected-raw-text) ; kTSMHiliteSelectedRawText
1871 (4 . mac-ts-converted-text) ; kTSMHiliteConvertedText
1872 (5 . mac-ts-selected-converted-text) ; kTSMHiliteSelectedConvertedText
1873 (6 . mac-ts-block-fill-text) ; kTSMHiliteBlockFillText
1874 (7 . mac-ts-outline-text) ; kTSMHiliteOutlineText
1875 (8 . mac-ts-selected-text) ; kTSMHiliteSelectedText
1876 (9 . mac-ts-no-hilite)) ; kTSMHiliteNoHilite
1877 "Alist of Mac TSM hilite style vs Emacs face.")
1879 (defun mac-ts-update-active-input-buf (text fix-len hilite-rng update-rng)
1880 (let ((buf-len (length mac-ts-active-input-buf))
1882 (if (or (null update-rng)
1883 (/= (% (length update-rng) 2) 0))
1884 ;; The parameter is missing (or in a bad format). The
1885 ;; existing inline input session is completely replaced with
1887 (setq mac-ts-active-input-buf text)
1888 ;; Otherwise, the current subtext specified by the (2*j)-th
1889 ;; range is replaced with the new subtext specified by the
1890 ;; (2*j+1)-th range.
1891 (let ((tail buf-len)
1892 (i (length update-rng))
1896 (setq rng (aref update-rng i))
1897 (if (and (<= 0 (cadr rng)) (< (cadr rng) tail)
1900 (cons (substring mac-ts-active-input-buf (cadr rng) tail)
1902 (setq tail (car rng))
1903 (setq rng (aref update-rng (1+ i)))
1904 (if (and (<= 0 (car rng)) (< (car rng) (cadr rng))
1905 (<= (cadr rng) (length text)))
1907 (cons (substring text (car rng) (cadr rng))
1909 (if (and (< 0 tail) (<= tail buf-len))
1911 (cons (substring mac-ts-active-input-buf 0 tail)
1913 (setq mac-ts-active-input-buf (apply 'concat segments))))
1914 (setq buf-len (length mac-ts-active-input-buf))
1915 ;; Confirm (a part of) inline input session.
1916 (cond ((< fix-len 0)
1917 ;; Entire inline session is being confirmed.
1918 (setq confirmed mac-ts-active-input-buf)
1919 (setq mac-ts-active-input-buf ""))
1921 ;; None of the text is being confirmed (yet).
1922 (setq confirmed ""))
1924 (if (> fix-len buf-len)
1925 (setq fix-len buf-len))
1926 (setq confirmed (substring mac-ts-active-input-buf 0 fix-len))
1927 (setq mac-ts-active-input-buf
1928 (substring mac-ts-active-input-buf fix-len))))
1929 (setq buf-len (length mac-ts-active-input-buf))
1930 ;; Update highlighting and the caret position in the new inline
1932 (remove-text-properties 0 buf-len '(cursor nil) mac-ts-active-input-buf)
1934 (cond ((and (= (nth 2 rng) 1) ; kTSMHiliteCaretPosition
1935 (<= 0 (car rng)) (< (car rng) buf-len))
1936 (put-text-property (car rng) buf-len
1937 'cursor t mac-ts-active-input-buf))
1938 ((and (<= 0 (car rng)) (< (car rng) (cadr rng))
1939 (<= (cadr rng) buf-len))
1940 (put-text-property (car rng) (cadr rng) 'face
1941 (cdr (assq (nth 2 rng)
1942 mac-ts-hilite-style-faces))
1943 mac-ts-active-input-buf))))
1947 (defun mac-split-string-by-property-change (string)
1948 (let ((tail (length string))
1951 (while (setq head (previous-property-change tail string)
1952 result (cons (substring string (or head 0) tail) result)
1956 (defun mac-replace-untranslated-utf-8-chars (string &optional to-string)
1957 (or to-string (setq to-string "\e$,3u=\e(B"))
1960 (if (get-text-property 0 'untranslated-utf-8 str) to-string str))
1961 (mac-split-string-by-property-change string)
1964 (defun mac-ts-update-active-input-area (event)
1965 "Update Mac TSM active input area according to EVENT.
1966 The confirmed text is converted to Emacs input events and pushed
1967 into `unread-command-events'. The unconfirmed text is displayed
1968 either in the current buffer or in the echo area."
1970 (let* ((ae (mac-event-ae event))
1971 (type-text (mac-ae-parameter ae "tstx"))
1972 (text (or (cdr type-text) ""))
1973 (decode-fun (if (equal (car type-text) "TEXT")
1974 'mac-TEXT-to-string 'mac-utxt-to-string))
1975 (script-language (mac-ae-script-language ae "tssl"))
1976 (coding (or (cdr (assq (car script-language)
1977 mac-script-code-coding-systems))
1979 (fix-len (mac-ae-number ae "tsfx"))
1980 ;; Optional parameters
1981 (hilite-rng (mac-ae-text-range-array ae "tshi"))
1982 (update-rng (mac-ae-text-range-array ae "tsup"))
1983 ;;(pin-rng (mac-bytes-to-text-range (cdr (mac-ae-parameter ae "tspn" "txrn"))))
1984 ;;(clause-offsets (cdr (mac-ae-parameter ae "tscl" "ofay")))
1985 (seqno (mac-ae-number ae "tsSn"))
1987 (unless (= seqno mac-ts-update-active-input-area-seqno)
1988 ;; Reset internal states if sequence number is out of sync.
1989 (setq mac-ts-active-input-buf ""))
1991 (mac-ts-update-active-input-buf text fix-len hilite-rng update-rng))
1992 (let ((use-echo-area
1994 (and cursor-in-echo-area (current-message))
1995 ;; Overlay strings are not shown in some cases.
1996 (get-char-property (point) 'invisible)
1998 (or (and (get-char-property (point) 'display)
1999 (eq (get-char-property (1- (point)) 'display)
2000 (get-char-property (point) 'display)))
2001 (and (get-char-property (point) 'composition)
2002 (eq (get-char-property (1- (point)) 'composition)
2003 (get-char-property (point) 'composition)))))))
2004 active-input-string caret-seen)
2005 ;; Decode the active input area text with inheriting faces and
2006 ;; the caret position.
2007 (setq active-input-string
2010 (let ((decoded (funcall decode-fun str coding)))
2011 (put-text-property 0 (length decoded) 'face
2012 (get-text-property 0 'face str) decoded)
2013 (when (and (not caret-seen)
2014 (get-text-property 0 'cursor str))
2016 (if (or use-echo-area (null cursor-type))
2017 (put-text-property 0 1 'face 'mac-ts-caret-position
2019 (put-text-property 0 1 'cursor t decoded)))
2021 (mac-split-string-by-property-change mac-ts-active-input-buf)
2023 (put-text-property 0 (length active-input-string)
2024 'mac-ts-active-input-string t active-input-string)
2026 (let ((msg (current-message))
2029 ;; Don't get confused by previously displayed
2030 ;; `active-input-string'.
2031 (null (get-text-property 0 'mac-ts-active-input-string
2033 (setq msg (propertize msg 'display
2034 (concat msg active-input-string)))
2035 (setq msg active-input-string))
2037 (overlay-put mac-ts-active-input-overlay 'before-string nil))
2038 (move-overlay mac-ts-active-input-overlay
2039 (point) (point) (current-buffer))
2040 (overlay-put mac-ts-active-input-overlay 'before-string
2041 active-input-string))
2042 ;; Unread confirmed characters and insert them in a keyboard
2043 ;; macro being defined.
2044 (apply 'isearch-unread
2045 (append (mac-replace-untranslated-utf-8-chars
2046 (funcall decode-fun confirmed coding)) '())))
2047 ;; The event is successfully processed. Sync the sequence number.
2048 (setq mac-ts-update-active-input-area-seqno (1+ seqno))))
2050 (defun mac-ts-unicode-for-key-event (event)
2051 "Convert Unicode key EVENT to Emacs key events and unread them."
2053 (let* ((ae (mac-event-ae event))
2054 (text (cdr (mac-ae-parameter ae "tstx" "utxt")))
2055 (script-language (mac-ae-script-language ae "tssl"))
2056 (coding (or (cdr (assq (car script-language)
2057 mac-script-code-coding-systems))
2059 ;; Unread characters and insert them in a keyboard macro being
2061 (apply 'isearch-unread
2062 (append (mac-replace-untranslated-utf-8-chars
2063 (mac-utxt-to-string text coding)) '()))))
2065 ;; kEventClassTextInput/kEventTextInputUpdateActiveInputArea
2066 (define-key mac-apple-event-map [text-input update-active-input-area]
2067 'mac-ts-update-active-input-area)
2068 ;; kEventClassTextInput/kEventTextInputUnicodeForKeyEvent
2069 (define-key mac-apple-event-map [text-input unicode-for-key-event]
2070 'mac-ts-unicode-for-key-event)
2073 (defun mac-service-open-file ()
2074 "Open the file specified by the selection value for Services."
2076 (find-file-existing (x-selection-value mac-service-selection)))
2078 (defun mac-service-open-selection ()
2079 "Create a new buffer containing the selection value for Services."
2081 (switch-to-buffer (generate-new-buffer "*untitled*"))
2082 (insert (x-selection-value mac-service-selection))
2084 (save-buffer) ; It pops up the save dialog.
2087 (defun mac-service-mail-selection ()
2088 "Prepare a mail buffer containing the selection value for Services."
2093 (insert (x-selection-value mac-service-selection) "\n"))
2095 (defun mac-service-mail-to ()
2096 "Prepare a mail buffer to be sent to the selection value for Services."
2098 (compose-mail (x-selection-value mac-service-selection)))
2100 (defun mac-service-insert-text ()
2101 "Insert the selection value for Services."
2103 (let ((text (x-selection-value mac-service-selection)))
2104 (if (not buffer-read-only)
2108 (substitute-command-keys
2109 "The text from the Services menu can be accessed with \\[yank]")))))
2111 ;; kEventClassService/kEventServicePaste
2112 (define-key mac-apple-event-map [service paste] 'mac-service-insert-text)
2113 ;; kEventClassService/kEventServicePerform
2114 (define-key mac-apple-event-map [service perform open-file]
2115 'mac-service-open-file)
2116 (define-key mac-apple-event-map [service perform open-selection]
2117 'mac-service-open-selection)
2118 (define-key mac-apple-event-map [service perform mail-selection]
2119 'mac-service-mail-selection)
2120 (define-key mac-apple-event-map [service perform mail-to]
2121 'mac-service-mail-to)
2123 (defun mac-dispatch-apple-event (event)
2124 "Dispatch EVENT according to the keymap `mac-apple-event-map'."
2126 (let* ((binding (lookup-key mac-apple-event-map (mac-event-spec event)))
2127 (ae (mac-event-ae event))
2128 (service-message (and (keymapp binding)
2129 (cdr (mac-ae-parameter ae "svmg")))))
2130 (when service-message
2131 (setq service-message
2132 (intern (decode-coding-string service-message 'utf-8)))
2133 (setq binding (lookup-key binding (vector service-message))))
2134 ;; Replace (cadr event) with a dummy position so that event-start
2136 (setcar (cdr event) (list (selected-window) (point) '(0 . 0) 0))
2137 (if (null (mac-ae-parameter ae 'emacs-suspension-id))
2138 (command-execute binding nil (vector event) t)
2141 (command-execute binding nil (vector event) t)
2142 (mac-resume-apple-event ae))
2144 (mac-ae-set-reply-parameter ae "errs"
2145 (cons "TEXT" (error-message-string err)))
2146 (mac-resume-apple-event ae -10000)))))) ; errAEEventFailed
2148 (define-key special-event-map [mac-apple-event] 'mac-dispatch-apple-event)
2150 ;; Processing of Apple events are deferred at the startup time. For
2151 ;; example, files dropped onto the Emacs application icon can only be
2152 ;; processed when the initial frame has been created: this is where
2153 ;; the files should be opened.
2154 (add-hook 'after-init-hook 'mac-process-deferred-apple-events)
2156 (run-with-idle-timer 5 t 'mac-cleanup-expired-apple-events)
2161 (defcustom mac-dnd-types-alist
2162 '(("furl" . mac-dnd-handle-furl)
2163 ("hfs " . mac-dnd-handle-hfs)
2164 ("utxt" . mac-dnd-insert-utxt)
2165 ("TEXT" . mac-dnd-insert-TEXT)
2166 ("TIFF" . mac-dnd-insert-TIFF))
2167 "Which function to call to handle a drop of that type.
2168 The function takes three arguments, WINDOW, ACTION and DATA.
2169 WINDOW is where the drop occured, ACTION is always `private' on
2170 Mac. DATA is the drop data. Unlike the x-dnd counterpart, the
2171 return value of the function is not significant.
2173 See also `mac-dnd-known-types'."
2178 (defun mac-dnd-handle-furl (window action data)
2179 (dnd-handle-one-url window action (mac-furl-to-string data)))
2181 (defun mac-dnd-handle-hfs (window action data)
2182 ;; struct HFSFlavor {
2184 ;; OSType fileCreator;
2188 (let* ((file-name (mac-coerce-ae-data "fss " (substring data 10)
2189 'undecoded-file-name))
2190 (url (concat "file://"
2191 (mapconcat 'url-hexify-string
2192 (split-string file-name "/") "/"))))
2193 (dnd-handle-one-url window action url)))
2195 (defun mac-dnd-insert-utxt (window action data)
2196 (dnd-insert-text window action (mac-utxt-to-string data)))
2198 (defun mac-dnd-insert-TEXT (window action data)
2199 (dnd-insert-text window action (mac-TEXT-to-string data)))
2201 (defun mac-dnd-insert-TIFF (window action data)
2202 (dnd-insert-text window action (mac-TIFF-to-string data)))
2204 (defun mac-dnd-drop-data (event frame window data type)
2205 (let* ((type-info (assoc type mac-dnd-types-alist))
2206 (handler (cdr type-info))
2208 (w (posn-window (event-start event))))
2210 (if (and (windowp w) (window-live-p w)
2211 (not (window-minibuffer-p w))
2212 (not (window-dedicated-p w)))
2213 ;; If dropping in an ordinary window which we could use,
2214 ;; let dnd-open-file-other-window specify what to do.
2216 (when (not mouse-yank-at-point)
2217 (goto-char (posn-point (event-start event))))
2218 (funcall handler window action data))
2219 ;; If we can't display the file here,
2220 ;; make a new window for it.
2221 (let ((dnd-open-file-other-window t))
2222 (select-frame frame)
2223 (funcall handler window action data))))))
2225 (defun mac-dnd-handle-drag-n-drop-event (event)
2226 "Receive drag and drop events."
2228 (let ((window (posn-window (event-start event))))
2229 (when (windowp window) (select-window window))
2230 (dolist (item (mac-ae-list (mac-event-ae event)))
2231 (if (not (equal (car item) "null"))
2232 (mac-dnd-drop-data event (selected-frame) window
2233 (cdr item) (car item)))))
2234 (select-frame-set-input-focus (selected-frame)))
2236 ;;; Do the actual Windows setup here; the above code just defines
2237 ;;; functions and variables that we use now.
2239 (setq command-line-args (x-handle-args command-line-args))
2241 ;;; Make sure we have a valid resource name.
2242 (or (stringp x-resource-name)
2244 (setq x-resource-name (invocation-name))
2246 ;; Change any . or * characters in x-resource-name to hyphens,
2247 ;; so as not to choke when we use it in X resource queries.
2248 (while (setq i (string-match "[.*]" x-resource-name))
2249 (aset x-resource-name i ?-))))
2251 (if (x-display-list)
2252 ;; On Mac OS 8/9, Most coding systems used in code conversion for
2253 ;; font names are not ready at the time when the terminal frame is
2254 ;; created. So we reconstruct font name table for the initial
2256 (mac-clear-font-name-table)
2257 (x-open-connection "Mac"
2258 x-command-line-resources
2259 ;; Exit Emacs with fatal error if this fails.
2262 (setq frame-creation-function 'x-create-frame-with-faces)
2264 (defvar mac-font-encoder-list
2265 '(("mac-roman" mac-roman-encoder
2266 ccl-encode-mac-roman-font "%s")
2267 ("mac-centraleurroman" encode-mac-centraleurroman
2268 ccl-encode-mac-centraleurroman-font "%s ce")
2269 ("mac-cyrillic" encode-mac-cyrillic
2270 ccl-encode-mac-cyrillic-font "%s cy")
2271 ("mac-symbol" mac-symbol-encoder
2272 ccl-encode-mac-symbol-font "symbol")
2273 ("mac-dingbats" mac-dingbats-encoder
2274 ccl-encode-mac-dingbats-font "zapf dingbats")))
2277 (mapcar (lambda (lst) (nth 1 lst)) mac-font-encoder-list))
2280 latin-iso8859-3 latin-iso8859-4
2281 cyrillic-iso8859-5 greek-iso8859-7 hebrew-iso8859-8
2282 latin-iso8859-9 latin-iso8859-14 latin-iso8859-15)))
2283 (dolist (encoder encoder-list)
2284 (let ((table (get encoder 'translation-table)))
2285 (dolist (charset charset-list)
2287 (let* ((c (make-char charset (+ i 32)))
2288 (mu (aref ucs-mule-to-mule-unicode c))
2289 (mac-encoded (and mu (aref table mu))))
2291 (aset table c mac-encoded))))))))
2293 ;; We assume none of official dim2 charsets (0x90..0x99) are encoded
2296 (define-ccl-program ccl-encode-mac-roman-font
2299 (translate-character mac-roman-encoder r0 r1)
2302 (translate-character mac-roman-encoder r0 r1))))
2303 "CCL program for Mac Roman font")
2305 (define-ccl-program ccl-encode-mac-centraleurroman-font
2308 (translate-character encode-mac-centraleurroman r0 r1)
2311 (translate-character encode-mac-centraleurroman r0 r1))))
2312 "CCL program for Mac Central European Roman font")
2314 (define-ccl-program ccl-encode-mac-cyrillic-font
2317 (translate-character encode-mac-cyrillic r0 r1)
2320 (translate-character encode-mac-cyrillic r0 r1))))
2321 "CCL program for Mac Cyrillic font")
2323 (define-ccl-program ccl-encode-mac-symbol-font
2326 (translate-character mac-symbol-encoder r0 r1)
2329 (translate-character mac-symbol-encoder r0 r1))))
2330 "CCL program for Mac Symbol font")
2332 (define-ccl-program ccl-encode-mac-dingbats-font
2335 (translate-character mac-dingbats-encoder r0 r1)
2338 (translate-character mac-dingbats-encoder r0 r1))))
2339 "CCL program for Mac Dingbats font")
2342 (setq font-ccl-encoder-alist
2344 (mapcar (lambda (lst) (cons (nth 0 lst) (nth 2 lst)))
2345 mac-font-encoder-list)
2346 font-ccl-encoder-alist))
2348 (defconst mac-char-fontspec-list
2349 ;; Directly operate on a char-table instead of a fontset so that it
2350 ;; may not create a dummy fontset.
2351 (let ((template (make-char-table 'fontset)))
2355 (mapcar (lambda (lst)
2356 (cons (cons (nth 3 lst) (nth 0 lst)) (nth 1 lst)))
2357 mac-font-encoder-list)))
2358 (let ((font (car font-encoder))
2359 (encoder (cdr font-encoder)))
2363 (generic-char-p key)
2364 (memq (char-charset key)
2365 '(ascii eight-bit-control eight-bit-graphic))
2366 (aset template key font)))
2367 (get encoder 'translation-table))))
2369 ;; Like fontset-info, but extend a range only if its "to" part is
2370 ;; the predecessor of the current char.
2371 (let* ((last '((0 nil)))
2373 last-char-or-range last-char last-elt)
2377 (setq last-char-or-range (car (car last))
2378 last-char (if (consp last-char-or-range)
2379 (cdr last-char-or-range)
2381 last-elt (cdr (car last)))
2382 (if (and (eq elt last-elt)
2383 (= char (1+ last-char))
2384 (eq (char-charset char) (char-charset last-char)))
2385 (if (consp last-char-or-range)
2386 (setcdr last-char-or-range char)
2387 (setcar (car last) (cons last-char char)))
2388 (setcdr last (list (cons char elt)))
2389 (setq last (cdr last)))))
2391 (cdr accumulator))))
2393 (defun fontset-add-mac-fonts (fontset &optional base-family)
2394 "Add font-specs for Mac fonts to FONTSET.
2395 The added font-specs are determined by BASE-FAMILY and the value
2396 of `mac-char-fontspec-list', which is a list
2397 of (CHARACTER-OR-RANGE . (FAMILY-FORMAT . REGISTRY)). If
2398 BASE-FAMILY is nil, the font family in the added font-specs is
2399 also nil. If BASE-FAMILY is a string, `%s' in FAMILY-FORMAT is
2400 replaced with the string. Otherwise, `%s' in FAMILY-FORMAT is
2401 replaced with the ASCII font family name in FONTSET."
2403 (if (stringp base-family)
2404 (setq base-family (downcase base-family))
2405 (let ((ascii-font (fontset-font fontset (charset-id 'ascii))))
2408 (aref (x-decompose-font-name
2409 (downcase (x-resolve-font-name ascii-font)))
2410 xlfd-regexp-family-subnum))))))
2411 (let (fontspec-cache fontspec)
2412 (dolist (char-fontspec mac-char-fontspec-list)
2413 (setq fontspec (cdr (assq (cdr char-fontspec) fontspec-cache)))
2414 (when (null fontspec)
2416 (cons (and base-family
2417 (format (car (cdr char-fontspec)) base-family))
2418 (cdr (cdr char-fontspec))))
2419 (setq fontspec-cache (cons (cons (cdr char-fontspec) fontspec)
2421 (set-fontset-font fontset (car char-fontspec) fontspec))))
2423 (defun create-fontset-from-mac-roman-font (font &optional resolved-font
2425 "Create a fontset from a Mac roman font FONT.
2427 Optional 1st arg RESOLVED-FONT is a resolved name of FONT. If
2428 omitted, `x-resolve-font-name' is called to get the resolved name. At
2429 this time, if FONT is not available, error is signaled.
2431 Optional 2nd arg FONTSET-NAME is a string to be used in
2432 `<CHARSET_ENCODING>' fields of a new fontset name. If it is omitted,
2433 an appropriate name is generated automatically.
2435 It returns a name of the created fontset."
2437 (create-fontset-from-ascii-font font resolved-font fontset-name)))
2438 (fontset-add-mac-fonts fontset t)
2441 ;; Adjust Courier font specifications in x-fixed-font-alist.
2442 (let ((courier-fonts (assoc "Courier" x-fixed-font-alist)))
2444 (dolist (label-fonts (cdr courier-fonts))
2448 (if (string-match "\\`-adobe-courier-\\([^-]*\\)-\\(.\\)-\\(.*\\)-iso8859-1\\'" font)
2450 (if (string= (match-string 2 font) "o")
2451 "-*-courier-\\1-i-\\3-*-*"
2452 "-*-courier-\\1-\\2-\\3-*-*")
2455 (cdr label-fonts))))))
2457 ;; Setup the default fontset.
2458 (setup-default-fontset)
2459 (cond ((x-list-fonts "*-iso10646-1")
2460 ;; Use ATSUI (if available) for the following charsets.
2462 (charset '(latin-iso8859-1
2463 latin-iso8859-2 latin-iso8859-3 latin-iso8859-4
2464 thai-tis620 greek-iso8859-7 arabic-iso8859-6
2465 hebrew-iso8859-8 cyrillic-iso8859-5
2466 latin-iso8859-9 latin-iso8859-15 latin-iso8859-14
2467 japanese-jisx0212 chinese-sisheng ipa
2468 vietnamese-viscii-lower vietnamese-viscii-upper
2469 lao ethiopic tibetan))
2470 (set-fontset-font nil charset '(nil . "iso10646-1"))))
2471 ((null (x-list-fonts "*-iso8859-1"))
2472 ;; Add Mac-encoding fonts unless ETL fonts are installed.
2473 (fontset-add-mac-fonts "fontset-default")))
2475 ;; Create a fontset that uses mac-roman font. With this fontset,
2476 ;; characters decoded from mac-roman encoding (ascii, latin-iso8859-1,
2477 ;; and mule-unicode-xxxx-yyyy) are displayed by a mac-roman font.
2478 (create-fontset-from-fontset-spec
2479 "-etl-fixed-medium-r-normal-*-16-*-*-*-*-*-fontset-standard,
2480 ascii:-*-Monaco-*-*-*-*-12-*-*-*-*-*-mac-roman")
2481 (fontset-add-mac-fonts "fontset-standard" t)
2483 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1, ...).
2484 (create-fontset-from-x-resource)
2486 ;; Try to create a fontset from a font specification which comes
2487 ;; from initial-frame-alist, default-frame-alist, or X resource.
2488 ;; A font specification in command line argument (i.e. -fn XXXX)
2489 ;; should be already in default-frame-alist as a `font'
2490 ;; parameter. However, any font specifications in site-start
2491 ;; library, user's init file (.emacs), and default.el are not
2492 ;; yet handled here.
2494 (let ((font (or (cdr (assq 'font initial-frame-alist))
2495 (cdr (assq 'font default-frame-alist))
2496 (x-get-resource "font" "Font")))
2497 xlfd-fields resolved-name)
2499 (not (query-fontset font))
2500 (setq resolved-name (x-resolve-font-name font))
2501 (setq xlfd-fields (x-decompose-font-name font)))
2502 (if (string= "fontset" (aref xlfd-fields xlfd-regexp-registry-subnum))
2503 (new-fontset font (x-complement-fontset-spec xlfd-fields nil))
2504 ;; Create a fontset from FONT. The fontset name is
2505 ;; generated from FONT.
2506 (if (and (string= "mac" (aref xlfd-fields xlfd-regexp-registry-subnum))
2507 (string= "roman" (aref xlfd-fields xlfd-regexp-encoding-subnum)))
2508 (create-fontset-from-mac-roman-font font resolved-name "startup")
2509 (create-fontset-from-ascii-font font resolved-name "startup")))))
2511 ;; Apply a geometry resource to the initial frame. Put it at the end
2512 ;; of the alist, so that anything specified on the command line takes
2514 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
2518 (setq parsed (x-parse-geometry res-geometry))
2519 ;; If the resource specifies a position,
2520 ;; call the position and size "user-specified".
2521 (if (or (assq 'top parsed) (assq 'left parsed))
2522 (setq parsed (cons '(user-position . t)
2523 (cons '(user-size . t) parsed))))
2524 ;; All geometry parms apply to the initial frame.
2525 (setq initial-frame-alist (append initial-frame-alist parsed))
2526 ;; The size parms apply to all frames.
2527 (if (assq 'height parsed)
2528 (setq default-frame-alist
2529 (cons (cons 'height (cdr (assq 'height parsed)))
2530 default-frame-alist)))
2531 (if (assq 'width parsed)
2532 (setq default-frame-alist
2533 (cons (cons 'width (cdr (assq 'width parsed)))
2534 default-frame-alist))))))
2536 ;; Check the reverseVideo resource.
2537 (let ((case-fold-search t))
2538 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
2540 (string-match "^\\(true\\|yes\\|on\\)$" rv))
2541 (setq default-frame-alist
2542 (cons '(reverse . t) default-frame-alist)))))
2544 (defun x-win-suspend-error ()
2545 (error "Suspending an Emacs running under Mac makes no sense"))
2546 (add-hook 'suspend-hook 'x-win-suspend-error)
2548 ;;; Arrange for the kill and yank functions to set and check the clipboard.
2549 (setq interprogram-cut-function 'x-select-text)
2550 (setq interprogram-paste-function 'x-get-selection-value)
2552 (defalias 'x-cut-buffer-or-selection-value 'x-get-selection-value)
2554 ;;; Turn off window-splitting optimization; Mac is usually fast enough
2555 ;;; that this is only annoying.
2556 (setq split-window-keep-point t)
2558 ;; Don't show the frame name; that's redundant.
2559 (setq-default mode-line-frame-identification " ")
2561 ;; Turn on support for mouse wheels.
2562 (mouse-wheel-mode 1)
2565 ;; Enable CLIPBOARD copy/paste through menu bar commands.
2566 (menu-bar-enable-clipboard)
2568 ;; Initiate drag and drop
2570 (define-key special-event-map [drag-n-drop] 'mac-dnd-handle-drag-n-drop-event)
2571 (define-key special-event-map [M-drag-n-drop] 'mac-dnd-handle-drag-n-drop-event)
2574 ;;;; Non-toolkit Scroll bars
2576 (unless x-toolkit-scroll-bars
2579 ;; (defun mac-handle-scroll-bar-event (event) (interactive "e") (princ event))
2581 ;;(global-set-key [vertical-scroll-bar mouse-1] 'mac-handle-scroll-bar-event)
2584 [vertical-scroll-bar down-mouse-1]
2585 'mac-handle-scroll-bar-event)
2587 (global-unset-key [vertical-scroll-bar drag-mouse-1])
2588 (global-unset-key [vertical-scroll-bar mouse-1])
2590 (defun mac-handle-scroll-bar-event (event)
2591 "Handle scroll bar EVENT to emulate Mac Toolbox style scrolling."
2593 (let* ((position (event-start event))
2594 (window (nth 0 position))
2595 (bar-part (nth 4 position)))
2596 (select-window window)
2599 (goto-char (window-start window))
2600 (mac-scroll-down-line))
2601 ((eq bar-part 'above-handle)
2603 ((eq bar-part 'handle)
2604 (scroll-bar-drag event))
2605 ((eq bar-part 'below-handle)
2607 ((eq bar-part 'down)
2608 (goto-char (window-start window))
2609 (mac-scroll-up-line)))))
2611 (defun mac-scroll-ignore-events ()
2612 ;; Ignore confusing non-mouse events
2613 (while (not (memq (car-safe (read-event))
2614 '(mouse-1 double-mouse-1 triple-mouse-1))) nil))
2616 (defun mac-scroll-down ()
2618 (mac-scroll-ignore-events)
2621 (defun mac-scroll-down-line ()
2623 (mac-scroll-ignore-events)
2626 (defun mac-scroll-up ()
2628 (mac-scroll-ignore-events)
2631 (defun mac-scroll-up-line ()
2633 (mac-scroll-ignore-events)
2641 (unless (eq system-type 'darwin)
2642 ;; This variable specifies the Unix program to call (as a process) to
2643 ;; determine the amount of free space on a file system (defaults to
2644 ;; df). If it is not set to nil, ls-lisp will not work correctly
2645 ;; unless an external application df is implemented on the Mac.
2646 (setq directory-free-space-program nil)
2648 ;; Set this so that Emacs calls subprocesses with "sh" as shell to
2649 ;; expand filenames Note no subprocess for the shell is actually
2650 ;; started (see run_mac_command in sysdep.c).
2651 (setq shell-file-name "sh")
2653 ;; Some system variables are encoded with the system script code.
2654 (dolist (v '(system-name
2655 emacs-build-system ; Mac OS 9 version cannot dump
2656 user-login-name user-real-login-name user-full-name))
2657 (set v (decode-coding-string (symbol-value v) mac-system-coding-system))))
2659 ;; Now the default directory is changed to the user's home directory
2660 ;; in emacs.c if invoked from the WindowServer (with -psn_* option).
2661 ;; (if (string= default-directory "/")
2664 ;; Darwin 6- pty breakage is now controlled from the C code so that
2665 ;; it applies to all builds on darwin. See s/darwin.h PTY_ITERATION.
2666 ;; (setq process-connection-type t)
2668 ;; Assume that fonts are always scalable on the Mac. This sometimes
2669 ;; results in characters with jagged edges. However, without it,
2670 ;; fonts with both truetype and bitmap representations but no italic
2671 ;; or bold bitmap versions will not display these variants correctly.
2672 (setq scalable-fonts-allowed t)
2674 ;; arch-tag: 71dfcd14-cde8-4d66-b05c-85ec94fb23a6
2675 ;;; mac-win.el ends here