1 ;;; mac-win.el --- parse switches controlling interface with Mac window system -*-coding: utf-8
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 3, 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)))
80 (defvar mac-charset-info-alist
)
81 (defvar mac-service-selection
)
82 (defvar mac-system-script-code
)
83 (defvar mac-apple-event-map
)
84 (defvar mac-font-panel-mode
)
85 (defvar mac-ts-active-input-overlay
)
86 (defvar x-invocation-args
)
87 (declare-function mac-code-convert-string
"mac.c")
88 (declare-function mac-coerce-ae-data
"mac.c")
89 (declare-function mac-resume-apple-event
"macselect.c")
90 ;; Suppress warning when compiling on non-Mac.
91 (declare-function mac-font-panel-mode
"mac-win.el")
92 (declare-function mac-atsu-font-face-attributes
"macfns.c")
93 (declare-function mac-ae-set-reply-parameter
"macselect.c")
94 (declare-function mac-clear-font-name-table
"macfns.c")
96 (defvar x-command-line-resources nil
)
98 ;; Handler for switches of the form "-switch value" or "-switch".
99 (defun x-handle-switch (switch)
100 (let ((aelt (assoc switch command-line-x-option-alist
)))
102 (let ((param (nth 3 aelt
))
103 (value (nth 4 aelt
)))
105 (setq default-frame-alist
106 (cons (cons param value
)
107 default-frame-alist
))
108 (setq default-frame-alist
110 (car x-invocation-args
))
112 x-invocation-args
(cdr x-invocation-args
)))))))
114 ;; Handler for switches of the form "-switch n"
115 (defun x-handle-numeric-switch (switch)
116 (let ((aelt (assoc switch command-line-x-option-alist
)))
118 (let ((param (nth 3 aelt
)))
119 (setq default-frame-alist
121 (string-to-number (car x-invocation-args
)))
124 (cdr x-invocation-args
))))))
126 ;; Handle options that apply to initial frame only
127 (defun x-handle-initial-switch (switch)
128 (let ((aelt (assoc switch command-line-x-option-alist
)))
130 (let ((param (nth 3 aelt
))
131 (value (nth 4 aelt
)))
133 (setq initial-frame-alist
134 (cons (cons param value
)
135 initial-frame-alist
))
136 (setq initial-frame-alist
138 (car x-invocation-args
))
140 x-invocation-args
(cdr x-invocation-args
)))))))
142 ;; Make -iconic apply only to the initial frame!
143 (defun x-handle-iconic (switch)
144 (setq initial-frame-alist
145 (cons '(visibility . icon
) initial-frame-alist
)))
147 ;; Handle the -xrm option.
148 (defun x-handle-xrm-switch (switch)
149 (unless (consp x-invocation-args
)
150 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
151 (setq x-command-line-resources
152 (if (null x-command-line-resources
)
153 (car x-invocation-args
)
154 (concat x-command-line-resources
"\n" (car x-invocation-args
))))
155 (setq x-invocation-args
(cdr x-invocation-args
)))
157 ;; Handle the geometry option
158 (defun x-handle-geometry (switch)
159 (let* ((geo (x-parse-geometry (car x-invocation-args
)))
160 (left (assq 'left geo
))
161 (top (assq 'top geo
))
162 (height (assq 'height geo
))
163 (width (assq 'width geo
)))
164 (if (or height width
)
165 (setq default-frame-alist
166 (append default-frame-alist
168 (if height
(list height
))
169 (if width
(list width
)))
171 (append initial-frame-alist
173 (if height
(list height
))
174 (if width
(list width
)))))
176 (setq initial-frame-alist
177 (append initial-frame-alist
178 '((user-position . t
))
179 (if left
(list left
))
180 (if top
(list top
)))))
181 (setq x-invocation-args
(cdr x-invocation-args
))))
183 ;; Handle the -name option. Set the variable x-resource-name
184 ;; to the option's operand; set the name of
185 ;; the initial frame, too.
186 (defun x-handle-name-switch (switch)
187 (or (consp x-invocation-args
)
188 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
189 (setq x-resource-name
(car x-invocation-args
)
190 x-invocation-args
(cdr x-invocation-args
))
191 (setq initial-frame-alist
(cons (cons 'name x-resource-name
)
192 initial-frame-alist
)))
194 (defvar x-display-name nil
195 "The display name specifying server and frame.")
197 (defun x-handle-display (switch)
198 (setq x-display-name
(car x-invocation-args
)
199 x-invocation-args
(cdr x-invocation-args
)))
201 (defun x-handle-args (args)
202 "Process the X-related command line options in ARGS.
203 This is done before the user's startup file is loaded. They are copied to
204 `x-invocation-args', from which the X-related things are extracted, first
205 the switch (e.g., \"-fg\") in the following code, and possible values
206 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
207 This function returns ARGS minus the arguments that have been processed."
208 ;; We use ARGS to accumulate the args that we don't handle here, to return.
209 (setq x-invocation-args args
211 (while (and x-invocation-args
212 (not (equal (car x-invocation-args
) "--")))
213 (let* ((this-switch (car x-invocation-args
))
214 (orig-this-switch this-switch
)
215 completion argval aelt handler
)
216 (setq x-invocation-args
(cdr x-invocation-args
))
217 ;; Check for long options with attached arguments
218 ;; and separate out the attached option argument into argval.
219 (if (string-match "^--[^=]*=" this-switch
)
220 (setq argval
(substring this-switch
(match-end 0))
221 this-switch
(substring this-switch
0 (1- (match-end 0)))))
222 ;; Complete names of long options.
223 (if (string-match "^--" this-switch
)
225 (setq completion
(try-completion this-switch command-line-x-option-alist
))
226 (if (eq completion t
)
227 ;; Exact match for long option.
229 (if (stringp completion
)
230 (let ((elt (assoc completion command-line-x-option-alist
)))
231 ;; Check for abbreviated long option.
233 (error "Option `%s' is ambiguous" this-switch
))
234 (setq this-switch completion
))))))
235 (setq aelt
(assoc this-switch command-line-x-option-alist
))
236 (if aelt
(setq handler
(nth 2 aelt
)))
239 (let ((x-invocation-args
240 (cons argval x-invocation-args
)))
241 (funcall handler this-switch
))
242 (funcall handler this-switch
))
243 (setq args
(cons orig-this-switch args
)))))
244 (nconc (nreverse args
) x-invocation-args
))
248 ;; Standard Mac cursor shapes
251 (defconst mac-pointer-arrow
0)
252 (defconst mac-pointer-copy-arrow
1)
253 (defconst mac-pointer-alias-arrow
2)
254 (defconst mac-pointer-contextual-menu-arrow
3)
255 (defconst mac-pointer-I-beam
4)
256 (defconst mac-pointer-cross
5)
257 (defconst mac-pointer-plus
6)
258 (defconst mac-pointer-watch
7)
259 (defconst mac-pointer-closed-hand
8)
260 (defconst mac-pointer-open-hand
9)
261 (defconst mac-pointer-pointing-hand
10)
262 (defconst mac-pointer-counting-up-hand
11)
263 (defconst mac-pointer-counting-down-hand
12)
264 (defconst mac-pointer-counting-up-and-down-hand
13)
265 (defconst mac-pointer-spinning
14)
266 (defconst mac-pointer-resize-left
15)
267 (defconst mac-pointer-resize-right
16)
268 (defconst mac-pointer-resize-left-right
17)
269 ;; Mac OS X 10.2 and later
270 (defconst mac-pointer-not-allowed
18)
271 ;; Mac OS X 10.3 and later
272 (defconst mac-pointer-resize-up
19)
273 (defconst mac-pointer-resize-down
20)
274 (defconst mac-pointer-resize-up-down
21)
275 (defconst mac-pointer-poof
22)
278 ;; Standard X cursor shapes that have Mac counterparts
281 (defconst x-pointer-left-ptr mac-pointer-arrow
)
282 (defconst x-pointer-xterm mac-pointer-I-beam
)
283 (defconst x-pointer-crosshair mac-pointer-cross
)
284 (defconst x-pointer-plus mac-pointer-plus
)
285 (defconst x-pointer-watch mac-pointer-watch
)
286 (defconst x-pointer-hand2 mac-pointer-pointing-hand
)
287 (defconst x-pointer-left-side mac-pointer-resize-left
)
288 (defconst x-pointer-right-side mac-pointer-resize-right
)
289 (defconst x-pointer-sb-h-double-arrow mac-pointer-resize-left-right
)
290 (defconst x-pointer-top-side mac-pointer-resize-up
)
291 (defconst x-pointer-bottom-side mac-pointer-resize-down
)
292 (defconst x-pointer-sb-v-double-arrow mac-pointer-resize-up-down
)
299 (defvar x-colors
'("LightGreen"
898 "LightGoldenrodYellow"
899 "light goldenrod yellow"
916 "medium spring green"
1051 "The list of X colors from the `rgb.txt' file.
1052 XConsortium: rgb.txt,v 10.41 94/02/20 18:39:36 rws Exp")
1054 (defun xw-defined-colors (&optional frame
)
1055 "Internal function called by `defined-colors', which see."
1056 (or frame
(setq frame
(selected-frame)))
1057 (let ((all-colors x-colors
)
1059 (defined-colors nil
))
1061 (setq this-color
(car all-colors
)
1062 all-colors
(cdr all-colors
))
1063 (and (color-supported-p this-color frame t
)
1064 (setq defined-colors
(cons this-color defined-colors
))))
1069 (defun x-setup-function-keys (frame)
1070 "Setup Function Keys for mac."
1071 ;; Don't do this twice on the same display, or it would break
1072 ;; normal-erase-is-backspace-mode.
1073 (unless (terminal-parameter frame
'x-setup-function-keys
)
1074 (with-selected-frame frame
1075 ;; Map certain keypad keys into ASCII characters
1076 ;; that people usually expect.
1077 (define-key local-function-key-map
[backspace] [?\d])
1078 (define-key local-function-key-map [delete] [?\d])
1079 (define-key local-function-key-map [tab] [?\t])
1080 (define-key local-function-key-map [linefeed] [?\n])
1081 (define-key local-function-key-map [clear] [?\C-l])
1082 (define-key local-function-key-map [return] [?\C-m])
1083 (define-key local-function-key-map [escape] [?\e])
1084 (define-key local-function-key-map [M-backspace] [?\M-\d])
1085 (define-key local-function-key-map [M-delete] [?\M-\d])
1086 (define-key local-function-key-map [M-tab] [?\M-\t])
1087 (define-key local-function-key-map [M-linefeed] [?\M-\n])
1088 (define-key local-function-key-map [M-clear] [?\M-\C-l])
1089 (define-key local-function-key-map [M-return] [?\M-\C-m])
1090 (define-key local-function-key-map [M-escape] [?\M-\e])
1091 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
1092 local-function-key-map global-map))
1093 (set-terminal-parameter frame 'x-setup-function-keys t)))
1095 ;; These tell read-char how to convert
1096 ;; these special chars to ASCII.
1097 (put 'backspace 'ascii-character ?\d)
1098 (put 'delete 'ascii-character ?\d)
1099 (put 'tab 'ascii-character ?\t)
1100 (put 'linefeed 'ascii-character ?\n)
1101 (put 'clear 'ascii-character ?\C-l)
1102 (put 'return 'ascii-character ?\C-m)
1103 (put 'escape 'ascii-character ?\e)
1105 ;; Modifier name `ctrl' is an alias of `control'.
1106 (put 'ctrl 'modifier-value (get 'control 'modifier-value))
1109 ;;;; Script codes and coding systems
1110 (defconst mac-script-code-coding-systems
1111 '((0 . mac-roman) ; smRoman
1112 (1 . japanese-shift-jis) ; smJapanese
1113 (2 . chinese-big5) ; smTradChinese
1114 (3 . korean-iso-8bit) ; smKorean
1115 (7 . mac-cyrillic) ; smCyrillic
1116 (25 . chinese-iso-8bit) ; smSimpChinese
1117 (29 . mac-centraleurroman) ; smCentralEuroRoman
1119 "Alist of Mac script codes vs Emacs coding systems.")
1121 (defun mac-add-charset-info (xlfd-charset mac-text-encoding)
1122 "Add a character set to display with Mac fonts.
1123 Create an entry in `mac-charset-info-alist'.
1124 XLFD-CHARSET is a string which will appear in the XLFD font name
1125 to identify the character set. MAC-TEXT-ENCODING is the
1126 correspoinding TextEncodingBase value."
1127 (add-to-list 'mac-charset-info-alist
1128 (list xlfd-charset mac-text-encoding
1129 (cdr (assq mac-text-encoding
1130 mac-script-code-coding-systems)))))
1132 (setq mac-charset-info-alist nil)
1133 (mac-add-charset-info "mac-roman" 0)
1134 (mac-add-charset-info "jisx0208.1983-sjis" 1)
1135 (mac-add-charset-info "jisx0201.1976-0" 1)
1136 (mac-add-charset-info "big5-0" 2)
1137 (mac-add-charset-info "ksc5601.1989-0" 3)
1138 (mac-add-charset-info "mac-cyrillic" 7)
1139 (mac-add-charset-info "gb2312.1980-0" 25)
1140 (mac-add-charset-info "mac-centraleurroman" 29)
1141 (mac-add-charset-info "mac-symbol" 33)
1142 (mac-add-charset-info "adobe-fontspecific" 33) ; for X-Symbol
1143 (mac-add-charset-info "mac-dingbats" 34)
1144 (mac-add-charset-info "iso10646-1" 126) ; for ATSUI
1146 (define-charset 'mac-centraleurroman
1147 "Mac Central European Roman"
1148 :short-name "Mac CE"
1149 :ascii-compatible-p t
1153 [?\Ä ?\Ā ?\ā ?\É ?\Ą ?\Ö ?\Ü ?\á ?\ą ?\Č ?\ä ?\č ?\Ć ?\ć ?\é ?\Ź
1154 ?\ź ?\Ď ?\í ?\ď ?\Ē ?\ē ?\Ė ?\ó ?\ė ?\ô ?\ö ?\õ ?\ú ?\Ě ?\ě ?\ü
1155 ?\† ?\° ?\Ę ?\£ ?\§ ?\• ?\¶ ?\ß ?\® ?\© ?\™ ?\ę ?\¨ ?\≠ ?\ģ ?\Į
1156 ?\į ?\Ī ?\≤ ?\≥ ?\ī ?\Ķ ?\∂ ?\∑ ?\ł ?\Ļ ?\ļ ?\Ľ ?\ľ ?\Ĺ ?\ĺ ?\Ņ
1157 ?\ņ ?\Ń ?\¬ ?\√ ?\ń ?\Ň ?\∆ ?\« ?\» ?\… ?\ ?\ň ?\Ő ?\Õ ?\ő ?\Ō
1158 ?\– ?\— ?\“ ?\” ?\‘ ?\’ ?\÷ ?\◊ ?\ō ?\Ŕ ?\ŕ ?\Ř ?\‹ ?\› ?\ř ?\Ŗ
1159 ?\ŗ ?\Š ?\‚ ?\„ ?\š ?\Ś ?\ś ?\Á ?\Ť ?\ť ?\Í ?\Ž ?\ž ?\Ū ?\Ó ?\Ô
1160 ?\ū ?\Ů ?\Ú ?\ů ?\Ű ?\ű ?\Ų ?\ų ?\Ý ?\ý ?\ķ ?\Ż ?\Ł ?\ż ?\Ģ ?\ˇ])
1161 (map (make-vector 512 nil)))
1162 (or (= (length tbl) 128)
1163 (error "Invalid vector length: %d" (length tbl)))
1165 (aset map (* i 2) i)
1166 (aset map (1+ (* i 2)) i))
1168 (aset map (+ 256 (* i 2)) (+ 128 i))
1169 (aset map (+ 256 (1+ (* i 2))) (aref tbl i)))
1172 (define-coding-system 'mac-centraleurroman
1173 "Mac Central European Roman Encoding (MIME:x-mac-centraleurroman)."
1174 :coding-type 'charset
1176 :charset-list '(mac-centraleurroman)
1177 :mime-charset 'x-mac-centraleurroman)
1179 (define-charset 'mac-cyrillic
1181 :short-name "Mac CYRILLIC"
1182 :ascii-compatible-p t
1186 [?\А ?\Б ?\В ?\Г ?\Д ?\Е ?\Ж ?\З ?\И ?\Й ?\К ?\Л ?\М ?\Н ?\О ?\П
1187 ?\Р ?\С ?\Т ?\У ?\Ф ?\Х ?\Ц ?\Ч ?\Ш ?\Щ ?\Ъ ?\Ы ?\Ь ?\Э ?\Ю ?\Я
1188 ?\† ?\° ?\Ґ ?\£ ?\§ ?\• ?\¶ ?\І ?\® ?\© ?\™ ?\Ђ ?\ђ ?\≠ ?\Ѓ ?\ѓ
1189 ?\∞ ?\± ?\≤ ?\≥ ?\і ?\µ ?\ґ ?\Ј ?\Є ?\є ?\Ї ?\ї ?\Љ ?\љ ?\Њ ?\њ
1190 ?\ј ?\Ѕ ?\¬ ?\√ ?\ƒ ?\≈ ?\∆ ?\« ?\» ?\… ?\ ?\Ћ ?\ћ ?\Ќ ?\ќ ?\ѕ
1191 ?\– ?\— ?\“ ?\” ?\‘ ?\’ ?\÷ ?\„ ?\Ў ?\ў ?\Џ ?\џ ?\№ ?\Ё ?\ё ?\я
1192 ?\а ?\б ?\в ?\г ?\д ?\е ?\ж ?\з ?\и ?\й ?\к ?\л ?\м ?\н ?\о ?\п
1193 ?\р ?\с ?\т ?\у ?\ф ?\х ?\ц ?\ч ?\ш ?\щ ?\ъ ?\ы ?\ь ?\э ?\ю ?\€])
1194 (map (make-vector 512 nil)))
1195 (or (= (length tbl) 128)
1196 (error "Invalid vector length: %d" (length tbl)))
1198 (aset map (* i 2) i)
1199 (aset map (1+ (* i 2)) i))
1201 (aset map (+ 256 (* i 2)) (+ 128 i))
1202 (aset map (+ 256 (1+ (* i 2))) (aref tbl i)))
1205 (define-coding-system 'mac-cyrillic
1206 "Mac Cyrillic Encoding (MIME:x-mac-cyrillic)."
1207 :coding-type 'charset
1209 :charset-list '(mac-cyrillic)
1210 :mime-charset 'x-mac-cyrillic)
1212 (define-charset 'mac-symbol
1214 :short-name "Mac SYMBOL"
1215 :code-space [32 254]
1218 [?\ ?\! ?\∀ ?\# ?\∃ ?\% ?\& ?\∍ ?\( ?\) ?\∗ ?\+ ?\, ?\− ?\. ?\/
1219 ?\0 ?\1 ?\2 ?\3 ?\4 ?\5 ?\6 ?\7 ?\8 ?\9 ?\: ?\; ?\< ?\= ?\> ?\?
1220 ?\≅ ?\Α ?\Β ?\Χ ?\Δ ?\Ε ?\Φ ?\Γ ?\Η ?\Ι ?\ϑ ?\Κ ?\Λ ?\Μ ?\Ν ?\Ο
1221 ?\Π ?\Θ ?\Ρ ?\Σ ?\Τ ?\Υ ?\ς ?\Ω ?\Ξ ?\Ψ ?\Ζ ?\[ ?\∴ ?\] ?\⊥ ?\_
1222 ?\ ?\α ?\β ?\χ ?\δ ?\ε ?\φ ?\γ ?\η ?\ι ?\ϕ ?\κ ?\λ ?\μ ?\ν ?\ο
1223 ?\π ?\θ ?\ρ ?\σ ?\τ ?\υ ?\ϖ ?\ω ?\ξ ?\ψ ?\ζ ?\{ ?\| ?\} ?\∼])
1224 (map-32-126 (make-vector (* (1+ (- 126 32)) 2) nil))
1226 ;; Mapping of the following characters are changed from the
1228 ;; 0xE2 0x00AE+0xF87F->0x00AE # REGISTERED SIGN, alternate: sans serif
1229 ;; 0xE3 0x00A9+0xF87F->0x00A9 # COPYRIGHT SIGN, alternate: sans serif
1230 ;; 0xE4 0x2122+0xF87F->0x2122 # TRADE MARK SIGN, alternate: sans serif
1231 [?\€ ?\ϒ ?\′ ?\≤ ?\⁄ ?\∞ ?\ƒ ?\♣ ?\♦ ?\♥ ?\♠ ?\↔ ?\← ?\↑ ?\→ ?\↓
1232 ?\° ?\± ?\″ ?\≥ ?\× ?\∝ ?\∂ ?\• ?\÷ ?\≠ ?\≡ ?\≈ ?\… ?\⏐ ?\⎯ ?\↵
1233 ?\ℵ ?\ℑ ?\ℜ ?\℘ ?\⊗ ?\⊕ ?\∅ ?\∩ ?\∪ ?\⊃ ?\⊇ ?\⊄ ?\⊂ ?\⊆ ?\∈ ?\∉
1234 ?\∠ ?\∇ ?\® ?\© ?\™ ?\∏ ?\√ ?\⋅ ?\¬ ?\∧ ?\∨ ?\⇔ ?\⇐ ?\⇑ ?\⇒ ?\⇓
1235 ?\◊ ?\〈 ?\® ?\© ?\™ ?\∑ ?\⎛ ?\⎜ ?\⎝ ?\⎡ ?\⎢ ?\⎣ ?\⎧ ?\⎨ ?\⎩ ?\⎪
1236 ?\ ?\〉 ?\∫ ?\⌠ ?\⎮ ?\⌡ ?\⎞ ?\⎟ ?\⎠ ?\⎤ ?\⎥ ?\⎦ ?\⎫ ?\⎬ ?\⎭])
1237 (map-160-254 (make-vector (* (1+ (- 254 160)) 2) nil)))
1238 (dotimes (i (1+ (- 126 32)))
1239 (aset map-32-126 (* i 2) (+ 32 i))
1240 (aset map-32-126 (1+ (* i 2)) (aref tbl-32-126 i)))
1241 (dotimes (i (1+ (- 254 160)))
1242 (aset map-160-254 (* i 2) (+ 160 i))
1243 (aset map-160-254 (1+ (* i 2)) (aref tbl-160-254 i)))
1244 (vconcat map-32-126 map-160-254)))
1246 (define-charset 'mac-dingbats
1248 :short-name "Mac Dingbats"
1249 :code-space [32 254]
1252 [?\ ?\✁ ?\✂ ?\✃ ?\✄ ?\☎ ?\✆ ?\✇ ?\✈ ?\✉ ?\☛ ?\☞ ?\✌ ?\✍ ?\✎ ?\✏
1253 ?\✐ ?\✑ ?\✒ ?\✓ ?\✔ ?\✕ ?\✖ ?\✗ ?\✘ ?\✙ ?\✚ ?\✛ ?\✜ ?\✝ ?\✞ ?\✟
1254 ?\✠ ?\✡ ?\✢ ?\✣ ?\✤ ?\✥ ?\✦ ?\✧ ?\★ ?\✩ ?\✪ ?\✫ ?\✬ ?\✭ ?\✮ ?\✯
1255 ?\✰ ?\✱ ?\✲ ?\✳ ?\✴ ?\✵ ?\✶ ?\✷ ?\✸ ?\✹ ?\✺ ?\✻ ?\✼ ?\✽ ?\✾ ?\✿
1256 ?\❀ ?\❁ ?\❂ ?\❃ ?\❄ ?\❅ ?\❆ ?\❇ ?\❈ ?\❉ ?\❊ ?\❋ ?\● ?\❍ ?\■ ?\❏
1257 ?\❐ ?\❑ ?\❒ ?\▲ ?\▼ ?\◆ ?\❖ ?\◗ ?\❘ ?\❙ ?\❚ ?\❛ ?\❜ ?\❝ ?\❞])
1258 (map-32-126 (make-vector (* (1+ (- 126 32)) 2) nil))
1260 [?\❨ ?\❩ ?\❪ ?\❫ ?\❬ ?\❭ ?\❮ ?\❯ ?\❰ ?\❱ ?\❲ ?\❳ ?\❴ ?\❵])
1261 (map-128-141 (make-vector (* (1+ (- 141 128)) 2) nil))
1263 [?\❡ ?\❢ ?\❣ ?\❤ ?\❥ ?\❦ ?\❧ ?\♣ ?\♦ ?\♥ ?\♠ ?\① ?\② ?\③ ?\④
1264 ?\⑤ ?\⑥ ?\⑦ ?\⑧ ?\⑨ ?\⑩ ?\❶ ?\❷ ?\❸ ?\❹ ?\❺ ?\❻ ?\❼ ?\❽ ?\❾ ?\❿
1265 ?\➀ ?\➁ ?\➂ ?\➃ ?\➄ ?\➅ ?\➆ ?\➇ ?\➈ ?\➉ ?\➊ ?\➋ ?\➌ ?\➍ ?\➎ ?\➏
1266 ?\➐ ?\➑ ?\➒ ?\➓ ?\➔ ?\→ ?\↔ ?\↕ ?\➘ ?\➙ ?\➚ ?\➛ ?\➜ ?\➝ ?\➞ ?\➟
1267 ?\➠ ?\➡ ?\➢ ?\➣ ?\➤ ?\➥ ?\➦ ?\➧ ?\➨ ?\➩ ?\➪ ?\➫ ?\➬ ?\➭ ?\➮ ?\➯])
1268 (map-161-239 (make-vector (* (1+ (- 239 161)) 2) nil))
1270 [?\➱ ?\➲ ?\➳ ?\➴ ?\➵ ?\➶ ?\➷ ?\➸ ?\➹ ?\➺ ?\➻ ?\➼ ?\➽ ?\➾])
1271 (map-241-254 (make-vector (* (1+ (- 254 241)) 2) nil)))
1272 (dotimes (i (1+ (- 126 32)))
1273 (aset map-32-126 (* i 2) (+ 32 i))
1274 (aset map-32-126 (1+ (* i 2)) (aref tbl-32-126 i)))
1275 (dotimes (i (1+ (- 141 128)))
1276 (aset map-128-141 (* i 2) (+ 128 i))
1277 (aset map-128-141 (1+ (* i 2)) (aref tbl-128-141 i)))
1278 (dotimes (i (1+ (- 239 161)))
1279 (aset map-161-239 (* i 2) (+ 161 i))
1280 (aset map-161-239 (1+ (* i 2)) (aref tbl-161-239 i)))
1281 (dotimes (i (1+ (- 254 241)))
1282 (aset map-241-254 (* i 2) (+ 241 i))
1283 (aset map-241-254 (1+ (* i 2)) (aref tbl-241-254 i)))
1284 (vconcat map-32-126 map-128-141 map-161-239 map-241-254)))
1286 (defconst mac-system-coding-system
1287 (let ((base (or (cdr (assq mac-system-script-code
1288 mac-script-code-coding-systems))
1290 (if (eq system-type 'darwin)
1292 (coding-system-change-eol-conversion base 'mac)))
1293 "Coding system derived from the system script code.")
1295 (set-selection-coding-system mac-system-coding-system)
1298 ;;;; Keyboard layout/language change events
1299 (defun mac-handle-language-change (event)
1300 "Set keyboard coding system to what is specified in EVENT."
1302 (let ((coding-system
1303 (cdr (assq (car (cadr event)) mac-script-code-coding-systems))))
1304 (set-keyboard-coding-system (or coding-system 'mac-roman))
1305 ;; MacJapanese maps reverse solidus to ?\x80.
1306 (if (eq coding-system 'japanese-shift-jis)
1307 (define-key key-translation-map [?\x80] "\\"))))
1309 (define-key special-event-map [language-change] 'mac-handle-language-change)
1312 ;;;; Conversion between common flavors and Lisp string.
1314 (defconst mac-text-encoding-ascii #x600
1315 "ASCII text encoding.")
1317 (defconst mac-text-encoding-mac-japanese-basic-variant #x20001
1318 "MacJapanese text encoding without Apple double-byte extensions.")
1320 (defun mac-utxt-to-string (data &optional coding-system)
1321 (or coding-system (setq coding-system mac-system-coding-system))
1323 (and (eq system-type 'darwin)
1324 (eq (coding-system-base coding-system) 'japanese-shift-jis)
1325 mac-text-encoding-mac-japanese-basic-variant))
1326 (str (and (fboundp 'mac-code-convert-string)
1327 (mac-code-convert-string data nil
1328 (or encoding coding-system)))))
1330 (setq str (decode-coding-string str coding-system))
1331 (if (eq encoding mac-text-encoding-mac-japanese-basic-variant)
1332 ;; Does it contain Apple one-byte extensions other than
1334 (if (string-match "[\xa0\xfd-\xff]" str)
1337 (unless (mac-code-convert-string data nil mac-text-encoding-ascii)
1338 (subst-char-in-string ?\x5c ?\¥ str t)
1339 (subst-char-in-string ?\x80 ?\\ str t)))))
1341 (decode-coding-string data
1342 (if (eq (byteorder) ?B) 'utf-16be 'utf-16le)))))
1344 (defun mac-string-to-utxt (string &optional coding-system)
1345 (or coding-system (setq coding-system mac-system-coding-system))
1346 (let (data encoding)
1347 (when (and (fboundp 'mac-code-convert-string)
1348 (memq (coding-system-base coding-system)
1349 (find-coding-systems-string string)))
1351 (coding-system-change-eol-conversion coding-system 'mac))
1353 (when (and (eq system-type 'darwin)
1354 (eq coding-system 'japanese-shift-jis-mac))
1355 (setq encoding mac-text-encoding-mac-japanese-basic-variant)
1356 (setq str (subst-char-in-string ?\\ ?\x80 str))
1357 (subst-char-in-string ?\¥ ?\x5c str t)
1359 (if (string-match "\\`[\x00-\x7f]*\\'" str)
1362 (setq data (mac-code-convert-string
1363 (encode-coding-string str coding-system)
1364 (or encoding coding-system) nil)))))
1365 (or data (encode-coding-string string (if (eq (byteorder) ?B)
1369 (defun mac-TEXT-to-string (data &optional coding-system)
1370 (or coding-system (setq coding-system mac-system-coding-system))
1371 (prog1 (setq data (decode-coding-string data coding-system))
1372 (when (eq (coding-system-base coding-system) 'japanese-shift-jis)
1373 ;; (subst-char-in-string ?\x5c ?\¥ data t)
1374 (subst-char-in-string ?\x80 ?\\ data t))))
1376 (defun mac-string-to-TEXT (string &optional coding-system)
1377 (or coding-system (setq coding-system mac-system-coding-system))
1378 (let ((encodables (find-coding-systems-string string))
1379 (rest mac-script-code-coding-systems))
1380 (unless (memq (coding-system-base coding-system) encodables)
1381 (while (and rest (not (memq (cdar rest) encodables)))
1382 (setq rest (cdr rest)))
1384 (setq coding-system (cdar rest)))))
1386 (coding-system-change-eol-conversion coding-system 'mac))
1387 (when (eq coding-system 'japanese-shift-jis-mac)
1388 ;; (setq string (subst-char-in-string ?\\ ?\x80 string))
1389 (setq string (subst-char-in-string ?\¥ ?\x5c string)))
1390 (encode-coding-string string coding-system))
1392 (defun mac-furl-to-string (data)
1393 ;; Remove a trailing nul character.
1394 (let ((len (length data)))
1395 (if (and (> len 0) (= (aref data (1- len)) ?\0))
1396 (substring data 0 (1- len))
1399 (defun mac-TIFF-to-string (data &optional text)
1400 (prog1 (or text (setq text (copy-sequence " ")))
1401 (put-text-property 0 (length text) 'display (create-image data 'tiff t)
1406 ;;; We keep track of the last text selected here, so we can check the
1407 ;;; current selection against it, and avoid passing back our own text
1408 ;;; from x-get-selection-value.
1409 (defvar x-last-selected-text-clipboard nil
1410 "The value of the CLIPBOARD selection last time we selected or
1412 (defvar x-last-selected-text-primary nil
1413 "The value of the PRIMARY X selection last time we selected or
1416 (defcustom x-select-enable-clipboard t
1417 "*Non-nil means cutting and pasting uses the clipboard.
1418 This is in addition to the primary selection."
1422 ;;; Make TEXT, a string, the primary X selection.
1423 (defun x-select-text (text &optional push)
1424 (x-set-selection 'PRIMARY text)
1425 (setq x-last-selected-text-primary text)
1426 (if (not x-select-enable-clipboard)
1427 (setq x-last-selected-text-clipboard nil)
1428 (x-set-selection 'CLIPBOARD text)
1429 (setq x-last-selected-text-clipboard text))
1432 (defun x-get-selection (&optional type data-type)
1433 "Return the value of a selection.
1434 The argument TYPE (default `PRIMARY') says which selection,
1435 and the argument DATA-TYPE (default `STRING') says
1436 how to convert the data.
1438 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
1439 only a few symbols are commonly used. They conventionally have
1440 all upper-case names. The most often used ones, in addition to
1441 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
1443 DATA-TYPE is usually `STRING', but can also be one of the symbols
1444 in `selection-converter-alist', which see."
1445 (let ((data (x-get-selection-internal (or type 'PRIMARY)
1446 (or data-type 'STRING)))
1447 (coding (or next-selection-coding-system
1448 selection-coding-system)))
1449 (when (and (stringp data)
1450 (setq data-type (get-text-property 0 'foreign-selection data)))
1451 (cond ((eq data-type 'public.utf16-plain-text)
1452 (setq data (mac-utxt-to-string data coding)))
1453 ((eq data-type 'com.apple.traditional-mac-plain-text)
1454 (setq data (mac-TEXT-to-string data coding)))
1455 ((eq data-type 'public.file-url)
1456 (setq data (mac-furl-to-string data))))
1457 (put-text-property 0 (length data) 'foreign-selection data-type data))
1460 (defun x-selection-value (type)
1461 (let ((data-types '(public.utf16-plain-text
1462 com.apple.traditional-mac-plain-text
1465 (while (and (null text) data-types)
1466 (setq text (condition-case nil
1467 (x-get-selection type (car data-types))
1469 (setq data-types (cdr data-types)))
1471 (remove-text-properties 0 (length text) '(foreign-selection nil) text))
1472 (setq tiff-image (condition-case nil
1473 (x-get-selection type 'public.tiff)
1476 (remove-text-properties 0 (length tiff-image)
1477 '(foreign-selection nil) tiff-image)
1478 (setq text (mac-TIFF-to-string tiff-image text)))
1481 ;;; Return the value of the current selection.
1482 ;;; Treat empty strings as if they were unset.
1483 ;;; If this function is called twice and finds the same text,
1484 ;;; it returns nil the second time. This is so that a single
1485 ;;; selection won't be added to the kill ring over and over.
1486 (defun x-get-selection-value ()
1487 (let (clip-text primary-text)
1488 (if (not x-select-enable-clipboard)
1489 (setq x-last-selected-text-clipboard nil)
1490 (setq clip-text (x-selection-value 'CLIPBOARD))
1491 (if (string= clip-text "") (setq clip-text nil))
1493 ;; Check the CLIPBOARD selection for 'newness', is it different
1494 ;; from what we remebered them to be last time we did a
1495 ;; cut/paste operation.
1497 (cond;; check clipboard
1498 ((or (not clip-text) (string= clip-text ""))
1499 (setq x-last-selected-text-clipboard nil))
1500 ((eq clip-text x-last-selected-text-clipboard) nil)
1501 ((string= clip-text x-last-selected-text-clipboard)
1502 ;; Record the newer string,
1503 ;; so subsequent calls can use the `eq' test.
1504 (setq x-last-selected-text-clipboard clip-text)
1507 (setq x-last-selected-text-clipboard clip-text))))
1510 (setq primary-text (x-selection-value 'PRIMARY))
1511 ;; Check the PRIMARY selection for 'newness', is it different
1512 ;; from what we remebered them to be last time we did a
1513 ;; cut/paste operation.
1515 (cond;; check primary selection
1516 ((or (not primary-text) (string= primary-text ""))
1517 (setq x-last-selected-text-primary nil))
1518 ((eq primary-text x-last-selected-text-primary) nil)
1519 ((string= primary-text x-last-selected-text-primary)
1520 ;; Record the newer string,
1521 ;; so subsequent calls can use the `eq' test.
1522 (setq x-last-selected-text-primary primary-text)
1525 (setq x-last-selected-text-primary primary-text))))
1527 ;; As we have done one selection, clear this now.
1528 (setq next-selection-coding-system nil)
1530 ;; At this point we have recorded the current values for the
1531 ;; selection from clipboard (if we are supposed to) and primary,
1532 ;; So return the first one that has changed (which is the first
1534 (or clip-text primary-text)
1537 (put 'CLIPBOARD 'mac-scrap-name "com.apple.scrap.clipboard")
1538 (when (eq system-type 'darwin)
1539 (put 'FIND 'mac-scrap-name "com.apple.scrap.find")
1540 (put 'PRIMARY 'mac-scrap-name
1541 (format "org.gnu.Emacs.%d.selection.PRIMARY" (emacs-pid))))
1542 (put 'com.apple.traditional-mac-plain-text 'mac-ostype "TEXT")
1543 (put 'public.utf16-plain-text 'mac-ostype "utxt")
1544 (put 'public.tiff 'mac-ostype "TIFF")
1545 (put 'public.file-url 'mac-ostype "furl")
1547 (defun mac-select-convert-to-string (selection type value)
1548 (let ((str (cdr (xselect-convert-to-string selection nil value)))
1549 (coding (or next-selection-coding-system selection-coding-system)))
1551 ;; If TYPE is nil, this is a local request, thus return STR as
1552 ;; is. Otherwise, encode STR.
1555 (let ((inhibit-read-only t))
1556 (remove-text-properties 0 (length str) '(composition nil) str)
1558 ((eq type 'public.utf16-plain-text)
1559 (setq str (mac-string-to-utxt str coding)))
1560 ((eq type 'com.apple.traditional-mac-plain-text)
1561 (setq str (mac-string-to-TEXT str coding)))
1563 (error "Unknown selection type: %S" type))
1566 (setq next-selection-coding-system nil)
1569 (defun mac-select-convert-to-file-url (selection type value)
1570 (let ((filename (xselect-convert-to-filename selection type value))
1571 (coding (or file-name-coding-system default-file-name-coding-system)))
1572 (if (and filename coding)
1573 (setq filename (encode-coding-string filename coding)))
1575 (concat "file://localhost"
1576 (mapconcat 'url-hexify-string
1577 (split-string filename "/") "/")))))
1579 (setq selection-converter-alist
1581 '((public.utf16-plain-text . mac-select-convert-to-string)
1582 (com.apple.traditional-mac-plain-text . mac-select-convert-to-string)
1583 ;; This is not enabled by default because the `Import Image'
1584 ;; menu makes Emacs crash or hang for unknown reasons.
1585 ;; (public.tiff . nil)
1586 (public.file-url . mac-select-convert-to-file-url)
1588 selection-converter-alist))
1590 ;;;; Apple events, HICommand events, and Services menu
1593 (put 'core-event 'mac-apple-event-class "aevt") ; kCoreEventClass
1594 (put 'internet-event 'mac-apple-event-class "GURL") ; kAEInternetEventClass
1598 (put 'open-application 'mac-apple-event-id "oapp") ; kAEOpenApplication
1599 (put 'reopen-application 'mac-apple-event-id "rapp") ; kAEReopenApplication
1600 (put 'open-documents 'mac-apple-event-id "odoc") ; kAEOpenDocuments
1601 (put 'print-documents 'mac-apple-event-id "pdoc") ; kAEPrintDocuments
1602 (put 'open-contents 'mac-apple-event-id "ocon") ; kAEOpenContents
1603 (put 'quit-application 'mac-apple-event-id "quit") ; kAEQuitApplication
1604 (put 'application-died 'mac-apple-event-id "obit") ; kAEApplicationDied
1605 (put 'show-preferences 'mac-apple-event-id "pref") ; kAEShowPreferences
1606 (put 'autosave-now 'mac-apple-event-id "asav") ; kAEAutosaveNow
1607 ;; kAEInternetEventClass
1608 (put 'get-url 'mac-apple-event-id "GURL") ; kAEGetURL
1609 ;; Converted HI command events
1610 (put 'about 'mac-apple-event-id "abou") ; kHICommandAbout
1611 (put 'show-hide-font-panel 'mac-apple-event-id "shfp") ; kHICommandShowHideFontPanel
1613 (defmacro mac-event-spec (event)
1616 (defmacro mac-event-ae (event)
1619 (defun mac-ae-parameter (ae &optional keyword type)
1620 (or keyword (setq keyword "----")) ;; Direct object.
1621 (if (not (and (consp ae) (equal (car ae) "aevt")))
1622 (error "Not an Apple event: %S" ae)
1623 (let ((type-data (cdr (assoc keyword (cdr ae))))
1625 (when (and type type-data (not (equal type (car type-data))))
1626 (setq data (mac-coerce-ae-data (car type-data) (cdr type-data) type))
1627 (setq type-data (if data (cons type data) nil)))
1630 (defun mac-ae-list (ae &optional keyword type)
1631 (or keyword (setq keyword "----")) ;; Direct object.
1632 (let ((desc (mac-ae-parameter ae keyword "list")))
1635 ((not (equal (car desc) "list"))
1636 (error "Parameter for \"%s\" is not a list" keyword))
1642 (mac-coerce-ae-data (car type-data) (cdr type-data) type))
1645 (defun mac-ae-number (ae keyword)
1646 (let ((type-data (mac-ae-parameter ae keyword))
1649 (setq str (mac-coerce-ae-data (car type-data)
1650 (cdr type-data) "TEXT")))
1651 (let ((num (string-to-number str)))
1652 ;; Mac OS Classic may return "0e+0" as the coerced value for
1653 ;; the type "magn" and the data "\000\000\000\000".
1654 (if (= num 0.0) 0 num))
1657 (defun mac-bytes-to-integer (bytes &optional from to)
1658 (or from (setq from 0))
1659 (or to (setq to (length bytes)))
1660 (let* ((len (- to from))
1661 (extended-sign-len (- (1+ (ceiling (log most-positive-fixnum 2)))
1665 (setq result (logior (lsh result 8)
1666 (aref bytes (+ from (if (eq (byteorder) ?B) i
1668 (if (> extended-sign-len 0)
1669 (ash (lsh result extended-sign-len) (- extended-sign-len))
1672 (defun mac-ae-selection-range (ae)
1673 ;; #pragma options align=mac68k
1674 ;; typedef struct SelectionRange {
1675 ;; short unused1; // 0 (not used)
1676 ;; short lineNum; // line to select (<0 to specify range)
1677 ;; long startRange; // start of selection range (if line < 0)
1678 ;; long endRange; // end of selection range (if line < 0)
1679 ;; long unused2; // 0 (not used)
1680 ;; long theDate; // modification date/time
1681 ;; } SelectionRange;
1682 ;; #pragma options align=reset
1683 (let ((range-bytes (cdr (mac-ae-parameter ae "kpos" "TEXT"))))
1685 (list (mac-bytes-to-integer range-bytes 2 4)
1686 (mac-bytes-to-integer range-bytes 4 8)
1687 (mac-bytes-to-integer range-bytes 8 12)
1688 (mac-bytes-to-integer range-bytes 16 20)))))
1690 ;; On Mac OS X 10.4 and later, the `open-document' event contains an
1691 ;; optional parameter keyAESearchText from the Spotlight search.
1692 (defun mac-ae-text-for-search (ae)
1693 (let ((utf8-text (cdr (mac-ae-parameter ae "stxt" "utf8"))))
1695 (decode-coding-string utf8-text 'utf-8))))
1697 (defun mac-ae-text (ae)
1698 (or (cdr (mac-ae-parameter ae nil "TEXT"))
1699 (error "No text in Apple event.")))
1701 (defun mac-ae-frame (ae &optional keyword type)
1702 (let ((bytes (cdr (mac-ae-parameter ae keyword type))))
1703 (if (or (null bytes) (/= (length bytes) 4))
1704 (error "No window reference in Apple event.")
1705 (let ((window-id (mac-coerce-ae-data "long" bytes "TEXT"))
1708 (while (and (null frame) rest)
1709 (if (string= (frame-parameter (car rest) 'window-id) window-id)
1710 (setq frame (car rest)))
1711 (setq rest (cdr rest)))
1714 (defun mac-ae-script-language (ae keyword)
1715 ;; struct WritingCode {
1716 ;; ScriptCode theScriptCode;
1717 ;; LangCode theLangCode;
1719 (let ((bytes (cdr (mac-ae-parameter ae keyword "intl"))))
1721 (cons (mac-bytes-to-integer bytes 0 2)
1722 (mac-bytes-to-integer bytes 2 4)))))
1724 (defun mac-bytes-to-text-range (bytes &optional from to)
1725 ;; struct TextRange {
1728 ;; short fHiliteStyle;
1730 (or from (setq from 0))
1731 (or to (setq to (length bytes)))
1732 (and (= (- to from) (+ 4 4 2))
1733 (list (mac-bytes-to-integer bytes from (+ from 4))
1734 (mac-bytes-to-integer bytes (+ from 4) (+ from 8))
1735 (mac-bytes-to-integer bytes (+ from 8) to))))
1737 (defun mac-ae-text-range-array (ae keyword)
1738 ;; struct TextRangeArray {
1739 ;; short fNumOfRanges;
1740 ;; TextRange fRange[1];
1742 (let* ((bytes (cdr (mac-ae-parameter ae keyword "tray")))
1743 (len (length bytes))
1745 (when (and bytes (>= len 2)
1747 (setq nranges (mac-bytes-to-integer bytes 0 2))
1748 (= len (+ 2 (* nranges 10)))))
1749 (setq result (make-vector nranges nil))
1750 (dotimes (i nranges)
1752 (mac-bytes-to-text-range bytes (+ (* i 10) 2)
1756 (defconst mac-keyboard-modifier-mask-alist
1758 (lambda (modifier-bit)
1759 (cons (car modifier-bit) (lsh 1 (cdr modifier-bit))))
1760 '((command . 8) ; cmdKeyBit
1761 (shift . 9) ; shiftKeyBit
1762 (option . 11) ; optionKeyBit
1763 (control . 12) ; controlKeyBit
1764 (function . 17))) ; kEventKeyModifierFnBit
1765 "Alist of Mac keyboard modifier symbols vs masks.")
1767 (defun mac-ae-keyboard-modifiers (ae)
1768 (let ((modifiers-value (mac-ae-number ae "kmod"))
1771 (dolist (modifier-mask mac-keyboard-modifier-mask-alist)
1772 (if (/= (logand modifiers-value (cdr modifier-mask)) 0)
1773 (setq modifiers (cons (car modifier-mask) modifiers)))))
1776 (defun mac-ae-reopen-application (event)
1777 "Show some frame in response to the Apple event EVENT.
1778 The frame to be shown is chosen from visible or iconified frames
1779 if possible. If there's no such frame, a new frame is created."
1781 (unless (frame-visible-p (selected-frame))
1782 (let ((frame (or (car (visible-frame-list))
1783 (car (filtered-frame-list 'frame-visible-p)))))
1785 (select-frame frame)
1786 (switch-to-buffer-other-frame "*scratch*"))))
1787 (select-frame-set-input-focus (selected-frame)))
1789 (defun mac-ae-open-documents (event)
1790 "Open the documents specified by the Apple event EVENT."
1792 (let ((ae (mac-event-ae event)))
1793 (dolist (file-name (mac-ae-list ae nil 'undecoded-file-name))
1795 (dnd-open-local-file
1797 (mapconcat 'url-hexify-string
1798 (split-string file-name "/") "/")) nil)))
1799 (let ((selection-range (mac-ae-selection-range ae))
1800 (search-text (mac-ae-text-for-search ae)))
1801 (cond (selection-range
1802 (let ((line (car selection-range))
1803 (start (cadr selection-range))
1804 (end (nth 2 selection-range)))
1807 (if (and (> start 0) (> end 0))
1808 (progn (set-mark start)
1809 (goto-char end))))))
1810 ((stringp search-text)
1812 (mapconcat 'regexp-quote (split-string search-text) "\\|")
1814 (select-frame-set-input-focus (selected-frame)))
1816 (defun mac-ae-quit-application (event)
1817 "Quit the application Emacs with the Apple event EVENT."
1819 (let ((ae (mac-event-ae event)))
1821 (save-buffers-kill-emacs)
1822 ;; Reaches here if the user has canceled the quit.
1823 (mac-resume-apple-event ae -128)))) ; userCanceledErr
1825 ;; url-generic-parse-url is autoloaded from url-parse.
1826 (declare-function url-type "url-parse" t t) ; defstruct
1828 (defun mac-ae-get-url (event)
1829 "Open the URL specified by the Apple event EVENT.
1830 Currently the `mailto' scheme is supported."
1832 (let* ((ae (mac-event-ae event))
1833 (parsed-url (url-generic-parse-url (mac-ae-text ae))))
1834 (if (string= (url-type parsed-url) "mailto")
1836 (url-mailto parsed-url)
1837 (select-frame-set-input-focus (selected-frame)))
1838 (mac-resume-apple-event ae t))))
1840 (setq mac-apple-event-map (make-sparse-keymap))
1842 ;; Received when Emacs is launched without associated documents.
1843 ;; Accept it as an Apple event, but no Emacs event is generated so as
1844 ;; not to erase the splash screen.
1845 (define-key mac-apple-event-map [core-event open-application] 0)
1847 ;; Received when a dock or application icon is clicked and Emacs is
1849 (define-key mac-apple-event-map [core-event reopen-application]
1850 'mac-ae-reopen-application)
1852 (define-key mac-apple-event-map [core-event open-documents]
1853 'mac-ae-open-documents)
1854 (define-key mac-apple-event-map [core-event show-preferences] 'customize)
1855 (define-key mac-apple-event-map [core-event quit-application]
1856 'mac-ae-quit-application)
1858 (define-key mac-apple-event-map [internet-event get-url] 'mac-ae-get-url)
1860 (define-key mac-apple-event-map [hi-command about] 'about-emacs)
1862 ;;; Converted Carbon Events
1863 (defun mac-handle-toolbar-switch-mode (event)
1864 "Toggle visibility of tool-bars in response to EVENT.
1865 With no keyboard modifiers, it toggles the visibility of the
1866 frame where the tool-bar toggle button was pressed. With some
1867 modifiers, it changes the global tool-bar visibility setting."
1869 (let ((ae (mac-event-ae event)))
1870 (if (mac-ae-keyboard-modifiers ae)
1871 ;; Globally toggle tool-bar-mode if some modifier key is pressed.
1872 (tool-bar-mode 'toggle)
1873 (let ((frame (mac-ae-frame ae)))
1874 (set-frame-parameter frame 'tool-bar-lines
1875 (if (= (frame-parameter frame 'tool-bar-lines) 0)
1878 ;; kEventClassWindow/kEventWindowToolbarSwitchMode
1879 (define-key mac-apple-event-map [window toolbar-switch-mode]
1880 'mac-handle-toolbar-switch-mode)
1883 (when (fboundp 'mac-set-font-panel-visible-p)
1885 (define-minor-mode mac-font-panel-mode
1886 "Toggle use of the font panel.
1887 With numeric ARG, display the font panel if and only if ARG is positive."
1891 (mac-set-font-panel-visible-p mac-font-panel-mode))
1893 (defun mac-handle-font-panel-closed (event)
1894 "Update internal status in response to font panel closed EVENT."
1896 ;; Synchronize with the minor mode variable.
1897 (mac-font-panel-mode 0))
1899 (defun mac-handle-font-selection (event)
1900 "Change default face attributes according to font selection EVENT."
1902 (let* ((ae (mac-event-ae event))
1903 (fm-font-size (mac-ae-number ae "fmsz"))
1904 (atsu-font-id (mac-ae-number ae "auid"))
1905 (attribute-values (and atsu-font-id
1906 (mac-atsu-font-face-attributes atsu-font-id))))
1908 (setq attribute-values
1909 `(:height ,(* 10 fm-font-size) ,@attribute-values)))
1910 (apply 'set-face-attribute 'default (selected-frame) attribute-values)))
1912 ;; kEventClassFont/kEventFontPanelClosed
1913 (define-key mac-apple-event-map [font panel-closed]
1914 'mac-handle-font-panel-closed)
1915 ;; kEventClassFont/kEventFontSelection
1916 (define-key mac-apple-event-map [font selection] 'mac-handle-font-selection)
1917 (define-key mac-apple-event-map [hi-command show-hide-font-panel]
1918 'mac-font-panel-mode)
1920 (define-key-after menu-bar-showhide-menu [mac-font-panel-mode]
1921 (menu-bar-make-mm-toggle mac-font-panel-mode
1923 "Show the font panel as a floating dialog")
1926 ) ;; (fboundp 'mac-set-font-panel-visible-p)
1929 (defvar mac-ts-active-input-buf ""
1930 "Byte sequence of the current Mac TSM active input area.")
1931 (defvar mac-ts-update-active-input-area-seqno 0
1932 "Number of processed update-active-input-area events.")
1933 (setq mac-ts-active-input-overlay (make-overlay 0 0))
1935 (defface mac-ts-caret-position
1936 '((t :inverse-video t))
1937 "Face for caret position in Mac TSM active input area.
1938 This is used when the active input area is displayed either in
1939 the echo area or in a buffer where the cursor is not displayed."
1942 (defface mac-ts-raw-text
1944 "Face for raw text in Mac TSM active input area."
1947 (defface mac-ts-selected-raw-text
1949 "Face for selected raw text in Mac TSM active input area."
1952 (defface mac-ts-converted-text
1953 '((((background dark)) :underline "gray20")
1954 (t :underline "gray80"))
1955 "Face for converted text in Mac TSM active input area."
1958 (defface mac-ts-selected-converted-text
1960 "Face for selected converted text in Mac TSM active input area."
1963 (defface mac-ts-block-fill-text
1965 "Face for block fill text in Mac TSM active input area."
1968 (defface mac-ts-outline-text
1970 "Face for outline text in Mac TSM active input area."
1973 (defface mac-ts-selected-text
1975 "Face for selected text in Mac TSM active input area."
1978 (defface mac-ts-no-hilite
1979 '((t :inherit default))
1980 "Face for no hilite in Mac TSM active input area."
1983 (defconst mac-ts-hilite-style-faces
1984 '((2 . mac-ts-raw-text) ; kTSMHiliteRawText
1985 (3 . mac-ts-selected-raw-text) ; kTSMHiliteSelectedRawText
1986 (4 . mac-ts-converted-text) ; kTSMHiliteConvertedText
1987 (5 . mac-ts-selected-converted-text) ; kTSMHiliteSelectedConvertedText
1988 (6 . mac-ts-block-fill-text) ; kTSMHiliteBlockFillText
1989 (7 . mac-ts-outline-text) ; kTSMHiliteOutlineText
1990 (8 . mac-ts-selected-text) ; kTSMHiliteSelectedText
1991 (9 . mac-ts-no-hilite)) ; kTSMHiliteNoHilite
1992 "Alist of Mac TSM hilite style vs Emacs face.")
1994 (defun mac-ts-update-active-input-buf (text fix-len hilite-rng update-rng)
1995 (let ((buf-len (length mac-ts-active-input-buf))
1997 (if (or (null update-rng)
1998 (/= (% (length update-rng) 2) 0))
1999 ;; The parameter is missing (or in a bad format). The
2000 ;; existing inline input session is completely replaced with
2002 (setq mac-ts-active-input-buf text)
2003 ;; Otherwise, the current subtext specified by the (2*j)-th
2004 ;; range is replaced with the new subtext specified by the
2005 ;; (2*j+1)-th range.
2006 (let ((tail buf-len)
2007 (i (length update-rng))
2011 (setq rng (aref update-rng i))
2012 (if (and (<= 0 (cadr rng)) (< (cadr rng) tail)
2015 (cons (substring mac-ts-active-input-buf (cadr rng) tail)
2017 (setq tail (car rng))
2018 (setq rng (aref update-rng (1+ i)))
2019 (if (and (<= 0 (car rng)) (< (car rng) (cadr rng))
2020 (<= (cadr rng) (length text)))
2022 (cons (substring text (car rng) (cadr rng))
2024 (if (and (< 0 tail) (<= tail buf-len))
2026 (cons (substring mac-ts-active-input-buf 0 tail)
2028 (setq mac-ts-active-input-buf (apply 'concat segments))))
2029 (setq buf-len (length mac-ts-active-input-buf))
2030 ;; Confirm (a part of) inline input session.
2031 (cond ((< fix-len 0)
2032 ;; Entire inline session is being confirmed.
2033 (setq confirmed mac-ts-active-input-buf)
2034 (setq mac-ts-active-input-buf ""))
2036 ;; None of the text is being confirmed (yet).
2037 (setq confirmed ""))
2039 (if (> fix-len buf-len)
2040 (setq fix-len buf-len))
2041 (setq confirmed (substring mac-ts-active-input-buf 0 fix-len))
2042 (setq mac-ts-active-input-buf
2043 (substring mac-ts-active-input-buf fix-len))))
2044 (setq buf-len (length mac-ts-active-input-buf))
2045 ;; Update highlighting and the caret position in the new inline
2047 (remove-text-properties 0 buf-len '(cursor nil) mac-ts-active-input-buf)
2049 (cond ((and (= (nth 2 rng) 1) ; kTSMHiliteCaretPosition
2050 (<= 0 (car rng)) (< (car rng) buf-len))
2051 (put-text-property (car rng) buf-len
2052 'cursor t mac-ts-active-input-buf))
2053 ((and (<= 0 (car rng)) (< (car rng) (cadr rng))
2054 (<= (cadr rng) buf-len))
2055 (put-text-property (car rng) (cadr rng) 'face
2056 (cdr (assq (nth 2 rng)
2057 mac-ts-hilite-style-faces))
2058 mac-ts-active-input-buf))))
2062 (defun mac-split-string-by-property-change (string)
2063 (let ((tail (length string))
2066 (while (setq head (previous-property-change tail string)
2067 result (cons (substring string (or head 0) tail) result)
2071 (defun mac-replace-untranslated-utf-8-chars (string &optional to-string)
2072 (or to-string (setq to-string "\e$,3u=\e(B"))
2075 (if (get-text-property 0 'untranslated-utf-8 str) to-string str))
2076 (mac-split-string-by-property-change string)
2079 (defun mac-keyboard-translate-char (ch)
2080 (if (and (char-valid-p ch)
2081 (or (char-table-p keyboard-translate-table)
2082 (and (or (stringp keyboard-translate-table)
2083 (vectorp keyboard-translate-table))
2084 (> (length keyboard-translate-table) ch))))
2085 (or (aref keyboard-translate-table ch) ch)
2088 (defun mac-unread-string (string)
2089 ;; Unread characters and insert them in a keyboard macro being
2091 (apply 'isearch-unread
2092 (mapcar 'mac-keyboard-translate-char
2093 (mac-replace-untranslated-utf-8-chars string))))
2095 (defun mac-ts-update-active-input-area (event)
2096 "Update Mac TSM active input area according to EVENT.
2097 The confirmed text is converted to Emacs input events and pushed
2098 into `unread-command-events'. The unconfirmed text is displayed
2099 either in the current buffer or in the echo area."
2101 (let* ((ae (mac-event-ae event))
2102 (type-text (mac-ae-parameter ae "tstx"))
2103 (text (or (cdr type-text) ""))
2104 (decode-fun (if (equal (car type-text) "TEXT")
2105 'mac-TEXT-to-string 'mac-utxt-to-string))
2106 (script-language (mac-ae-script-language ae "tssl"))
2107 (coding (or (cdr (assq (car script-language)
2108 mac-script-code-coding-systems))
2110 (fix-len (mac-ae-number ae "tsfx"))
2111 ;; Optional parameters
2112 (hilite-rng (mac-ae-text-range-array ae "tshi"))
2113 (update-rng (mac-ae-text-range-array ae "tsup"))
2114 ;;(pin-rng (mac-bytes-to-text-range (cdr (mac-ae-parameter ae "tspn" "txrn"))))
2115 ;;(clause-offsets (cdr (mac-ae-parameter ae "tscl" "ofay")))
2116 (seqno (mac-ae-number ae "tsSn"))
2118 (unless (= seqno mac-ts-update-active-input-area-seqno)
2119 ;; Reset internal states if sequence number is out of sync.
2120 (setq mac-ts-active-input-buf ""))
2122 (mac-ts-update-active-input-buf text fix-len hilite-rng update-rng))
2123 (let ((use-echo-area
2125 (and cursor-in-echo-area (current-message))
2126 ;; Overlay strings are not shown in some cases.
2127 (get-char-property (point) 'invisible)
2129 (or (and (get-char-property (point) 'display)
2130 (eq (get-char-property (1- (point)) 'display)
2131 (get-char-property (point) 'display)))
2132 (and (get-char-property (point) 'composition)
2133 (eq (get-char-property (1- (point)) 'composition)
2134 (get-char-property (point) 'composition)))))))
2135 active-input-string caret-seen)
2136 ;; Decode the active input area text with inheriting faces and
2137 ;; the caret position.
2138 (setq active-input-string
2141 (let ((decoded (funcall decode-fun str coding)))
2142 (put-text-property 0 (length decoded) 'face
2143 (get-text-property 0 'face str) decoded)
2144 (when (and (not caret-seen)
2145 (get-text-property 0 'cursor str))
2147 (if (or use-echo-area (null cursor-type))
2148 (put-text-property 0 1 'face 'mac-ts-caret-position
2150 (put-text-property 0 1 'cursor t decoded)))
2152 (mac-split-string-by-property-change mac-ts-active-input-buf)
2154 (put-text-property 0 (length active-input-string)
2155 'mac-ts-active-input-string t active-input-string)
2157 (let ((msg (current-message))
2160 ;; Don't get confused by previously displayed
2161 ;; `active-input-string'.
2162 (null (get-text-property 0 'mac-ts-active-input-string
2164 (setq msg (propertize msg 'display
2165 (concat msg active-input-string)))
2166 (setq msg active-input-string))
2168 (overlay-put mac-ts-active-input-overlay 'before-string nil))
2169 (move-overlay mac-ts-active-input-overlay
2170 (point) (point) (current-buffer))
2171 (overlay-put mac-ts-active-input-overlay 'before-string
2172 active-input-string))
2173 (mac-unread-string (funcall decode-fun confirmed coding)))
2174 ;; The event is successfully processed. Sync the sequence number.
2175 (setq mac-ts-update-active-input-area-seqno (1+ seqno))))
2177 (defun mac-ts-unicode-for-key-event (event)
2178 "Convert Unicode key EVENT to Emacs key events and unread them."
2180 (let* ((ae (mac-event-ae event))
2181 (text (cdr (mac-ae-parameter ae "tstx" "utxt")))
2182 (script-language (mac-ae-script-language ae "tssl"))
2183 (coding (or (cdr (assq (car script-language)
2184 mac-script-code-coding-systems))
2187 (mac-unread-string (mac-utxt-to-string text coding)))))
2189 ;; kEventClassTextInput/kEventTextInputUpdateActiveInputArea
2190 (define-key mac-apple-event-map [text-input update-active-input-area]
2191 'mac-ts-update-active-input-area)
2192 ;; kEventClassTextInput/kEventTextInputUnicodeForKeyEvent
2193 (define-key mac-apple-event-map [text-input unicode-for-key-event]
2194 'mac-ts-unicode-for-key-event)
2197 (defun mac-service-open-file ()
2198 "Open the file specified by the selection value for Services."
2200 (find-file-existing (x-selection-value mac-service-selection)))
2202 (defun mac-service-open-selection ()
2203 "Create a new buffer containing the selection value for Services."
2205 (switch-to-buffer (generate-new-buffer "*untitled*"))
2206 (insert (x-selection-value mac-service-selection))
2208 (save-buffer) ; It pops up the save dialog.
2211 (defun mac-service-mail-selection ()
2212 "Prepare a mail buffer containing the selection value for Services."
2217 (insert (x-selection-value mac-service-selection) "\n"))
2219 (defun mac-service-mail-to ()
2220 "Prepare a mail buffer to be sent to the selection value for Services."
2222 (compose-mail (x-selection-value mac-service-selection)))
2224 (defun mac-service-insert-text ()
2225 "Insert the selection value for Services."
2227 (let ((text (x-selection-value mac-service-selection)))
2228 (if (not buffer-read-only)
2232 (substitute-command-keys
2233 "The text from the Services menu can be accessed with \\[yank]")))))
2235 ;; kEventClassService/kEventServicePaste
2236 (define-key mac-apple-event-map [service paste] 'mac-service-insert-text)
2237 ;; kEventClassService/kEventServicePerform
2238 (define-key mac-apple-event-map [service perform open-file]
2239 'mac-service-open-file)
2240 (define-key mac-apple-event-map [service perform open-selection]
2241 'mac-service-open-selection)
2242 (define-key mac-apple-event-map [service perform mail-selection]
2243 'mac-service-mail-selection)
2244 (define-key mac-apple-event-map [service perform mail-to]
2245 'mac-service-mail-to)
2247 (defun mac-dispatch-apple-event (event)
2248 "Dispatch EVENT according to the keymap `mac-apple-event-map'."
2250 (let* ((binding (lookup-key mac-apple-event-map (mac-event-spec event)))
2251 (ae (mac-event-ae event))
2252 (service-message (and (keymapp binding)
2253 (cdr (mac-ae-parameter ae "svmg")))))
2254 (when service-message
2255 (setq service-message
2256 (intern (decode-coding-string service-message 'utf-8)))
2257 (setq binding (lookup-key binding (vector service-message))))
2258 ;; Replace (cadr event) with a dummy position so that event-start
2260 (setcar (cdr event) (list (selected-window) (point) '(0 . 0) 0))
2261 (if (null (mac-ae-parameter ae 'emacs-suspension-id))
2262 (command-execute binding nil (vector event) t)
2265 (command-execute binding nil (vector event) t)
2266 (mac-resume-apple-event ae))
2268 (mac-ae-set-reply-parameter ae "errs"
2269 (cons "TEXT" (error-message-string err)))
2270 (mac-resume-apple-event ae -10000)))))) ; errAEEventFailed
2272 (define-key special-event-map [mac-apple-event] 'mac-dispatch-apple-event)
2274 ;; Processing of Apple events are deferred at the startup time. For
2275 ;; example, files dropped onto the Emacs application icon can only be
2276 ;; processed when the initial frame has been created: this is where
2277 ;; the files should be opened.
2278 (add-hook 'after-init-hook 'mac-process-deferred-apple-events)
2280 (run-with-idle-timer 5 t 'mac-cleanup-expired-apple-events)
2285 (defcustom mac-dnd-types-alist
2286 '(("furl" . mac-dnd-handle-furl)
2287 ("hfs " . mac-dnd-handle-hfs)
2288 ("utxt" . mac-dnd-insert-utxt)
2289 ("TEXT" . mac-dnd-insert-TEXT)
2290 ("TIFF" . mac-dnd-insert-TIFF))
2291 "Which function to call to handle a drop of that type.
2292 The function takes three arguments, WINDOW, ACTION and DATA.
2293 WINDOW is where the drop occurred, ACTION is always `private' on
2294 Mac. DATA is the drop data. Unlike the x-dnd counterpart, the
2295 return value of the function is not significant.
2297 See also `mac-dnd-known-types'."
2302 (defun mac-dnd-handle-furl (window action data)
2303 (dnd-handle-one-url window action (mac-furl-to-string data)))
2305 (defun mac-dnd-handle-hfs (window action data)
2306 ;; struct HFSFlavor {
2308 ;; OSType fileCreator;
2312 (let* ((file-name (mac-coerce-ae-data "fss " (substring data 10)
2313 'undecoded-file-name))
2314 (url (concat "file://"
2315 (mapconcat 'url-hexify-string
2316 (split-string file-name "/") "/"))))
2317 (dnd-handle-one-url window action url)))
2319 (defun mac-dnd-insert-utxt (window action data)
2320 (dnd-insert-text window action (mac-utxt-to-string data)))
2322 (defun mac-dnd-insert-TEXT (window action data)
2323 (dnd-insert-text window action (mac-TEXT-to-string data)))
2325 (defun mac-dnd-insert-TIFF (window action data)
2326 (dnd-insert-text window action (mac-TIFF-to-string data)))
2328 (defun mac-dnd-drop-data (event frame window data type &optional action)
2329 (or action (setq action 'private))
2330 (let* ((type-info (assoc type mac-dnd-types-alist))
2331 (handler (cdr type-info))
2332 (w (posn-window (event-start event))))
2334 (if (and (window-live-p w)
2335 (not (window-minibuffer-p w))
2336 (not (window-dedicated-p w)))
2337 ;; If dropping in an ordinary window which we could use,
2338 ;; let dnd-open-file-other-window specify what to do.
2340 (when (not mouse-yank-at-point)
2341 (goto-char (posn-point (event-start event))))
2342 (funcall handler window action data))
2343 ;; If we can't display the file here,
2344 ;; make a new window for it.
2345 (let ((dnd-open-file-other-window t))
2346 (select-frame frame)
2347 (funcall handler window action data))))))
2349 (defun mac-dnd-handle-drag-n-drop-event (event)
2350 "Receive drag and drop events."
2352 (let ((window (posn-window (event-start event)))
2353 (ae (mac-event-ae event))
2355 (when (windowp window) (select-window window))
2356 (if (memq 'option (mac-ae-keyboard-modifiers ae))
2357 (setq action 'copy))
2358 (dolist (item (mac-ae-list ae))
2359 (if (not (equal (car item) "null"))
2360 (mac-dnd-drop-data event (selected-frame) window
2361 (cdr item) (car item) action)))))
2363 (setq font-encoding-alist
2365 '(("mac-roman" . mac-roman)
2366 ("mac-centraleurroman" . mac-centraleurroman)
2367 ("mac-cyrillic" . mac-cyrillic)
2368 ("mac-symbol" . mac-symbol)
2369 ("mac-dingbats" . mac-dingbats))
2370 font-encoding-alist))
2372 (defun fontset-add-mac-fonts (fontset &optional base-family)
2373 (dolist (elt `((latin . (,(or base-family "Monaco") . "mac-roman"))
2374 (mac-roman . (,base-family . "mac-roman"))
2375 (mac-centraleurroman . (,base-family . "mac-centraleurroman"))
2376 (mac-cyrillic . (,base-family . "mac-cyrillic"))
2377 (mac-symbol . (,base-family . "mac-symbol"))
2378 (mac-dingbats . (,base-family . "mac-dingbats"))))
2379 (set-fontset-font fontset (car elt) (cdr elt))))
2381 (defun create-fontset-from-mac-roman-font (font &optional resolved-font
2383 "Create a fontset from a Mac roman font FONT.
2385 Optional 1st arg RESOLVED-FONT is a resolved name of FONT. If
2386 omitted, `x-resolve-font-name' is called to get the resolved name. At
2387 this time, if FONT is not available, error is signaled.
2389 Optional 2nd arg FONTSET-NAME is a string to be used in
2390 `<CHARSET_ENCODING>' fields of a new fontset name. If it is omitted,
2391 an appropriate name is generated automatically.
2393 It returns a name of the created fontset."
2395 (setq resolved-font (x-resolve-font-name font)))
2396 (let ((base-family (aref (x-decompose-font-name resolved-font)
2397 xlfd-regexp-family-subnum)))
2398 (if (string= base-family "*")
2399 (setq base-family nil))
2400 (new-fontset fontset-name (list (cons 'ascii resolved-font)))
2401 (fontset-add-mac-fonts fontset-name base-family)))
2403 (defun x-win-suspend-error ()
2404 (error "Suspending an Emacs running under Mac makes no sense"))
2406 (defalias 'x-cut-buffer-or-selection-value 'x-get-selection-value)
2408 (defvar mac-initialized nil
2409 "Non-nil if the w32 window system has been initialized.")
2411 (defun mac-initialize-window-system ()
2412 "Initialize Emacs for Mac GUI frames."
2414 ;;; Do the actual Windows setup here; the above code just defines
2415 ;;; functions and variables that we use now.
2417 (setq command-line-args (x-handle-args command-line-args))
2419 ;;; Make sure we have a valid resource name.
2420 (or (stringp x-resource-name)
2422 (setq x-resource-name (invocation-name))
2424 ;; Change any . or * characters in x-resource-name to hyphens,
2425 ;; so as not to choke when we use it in X resource queries.
2426 (while (setq i (string-match "[.*]" x-resource-name))
2427 (aset x-resource-name i ?-))))
2429 (if (x-display-list)
2430 ;; On Mac OS 8/9, Most coding systems used in code conversion for
2431 ;; font names are not ready at the time when the terminal frame is
2432 ;; created. So we reconstruct font name table for the initial
2434 (mac-clear-font-name-table)
2435 (x-open-connection "Mac"
2436 x-command-line-resources
2437 ;; Exit Emacs with fatal error if this fails.
2440 (add-hook 'suspend-hook 'x-win-suspend-error)
2442 ;;; Arrange for the kill and yank functions to set and check the clipboard.
2443 (setq interprogram-cut-function 'x-select-text)
2444 (setq interprogram-paste-function 'x-get-selection-value)
2449 ;;; Turn off window-splitting optimization; Mac is usually fast enough
2450 ;;; that this is only annoying.
2451 (setq split-window-keep-point t)
2453 ;; Don't show the frame name; that's redundant.
2454 (setq-default mode-line-frame-identification " ")
2456 ;; Turn on support for mouse wheels.
2457 (mouse-wheel-mode 1)
2460 ;; Enable CLIPBOARD copy/paste through menu bar commands.
2461 (menu-bar-enable-clipboard)
2464 ;; Initiate drag and drop
2466 (define-key special-event-map [drag-n-drop] 'mac-dnd-handle-drag-n-drop-event)
2469 ;;;; Non-toolkit Scroll bars
2471 (unless x-toolkit-scroll-bars
2474 ;; (defun mac-handle-scroll-bar-event (event) (interactive "e") (princ event))
2476 ;;(global-set-key [vertical-scroll-bar mouse-1] 'mac-handle-scroll-bar-event)
2479 [vertical-scroll-bar down-mouse-1]
2480 'mac-handle-scroll-bar-event)
2482 (global-unset-key [vertical-scroll-bar drag-mouse-1])
2483 (global-unset-key [vertical-scroll-bar mouse-1])
2485 ;; Adjust Courier font specifications in x-fixed-font-alist.
2486 (let ((courier-fonts (assoc "Courier" x-fixed-font-alist)))
2488 (dolist (label-fonts (cdr courier-fonts))
2492 (if (string-match "\\`-adobe-courier-\\([^-]*\\)-\\(.\\)-\\(.*\\)-iso8859-1\\'" font)
2494 (if (string= (match-string 2 font) "o")
2495 "-*-courier-\\1-i-\\3-*-*"
2496 "-*-courier-\\1-\\2-\\3-*-*")
2499 (cdr label-fonts))))))
2501 ;; Setup the default fontset.
2502 (setup-default-fontset)
2504 ;; Create a fontset that uses mac-roman font. With this fontset,
2505 ;; characters belonging to mac-roman charset (that contains ASCII and
2506 ;; more Latin characters) are displayed by a mac-roman font.
2507 (create-fontset-from-mac-roman-font
2508 "-*-Monaco-*-*-*-*-12-*-*-*-*-*-mac-roman" nil
2509 "-apple-Monaco-normal-r-*-*-12-*-*-*-*-*-fontset-standard")
2511 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1, ...).
2512 (create-fontset-from-x-resource)
2514 ;; Apply a geometry resource to the initial frame. Put it at the end
2515 ;; of the alist, so that anything specified on the command line takes
2517 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
2521 (setq parsed (x-parse-geometry res-geometry))
2522 ;; If the resource specifies a position,
2523 ;; call the position and size "user-specified".
2524 (if (or (assq 'top parsed) (assq 'left parsed))
2525 (setq parsed (cons '(user-position . t)
2526 (cons '(user-size . t) parsed))))
2527 ;; All geometry parms apply to the initial frame.
2528 (setq initial-frame-alist (append initial-frame-alist parsed))
2529 ;; The size parms apply to all frames. Don't set it if there are
2530 ;; sizes there already (from command line).
2531 (if (and (assq 'height parsed)
2532 (not (assq 'height default-frame-alist)))
2533 (setq default-frame-alist
2534 (cons (cons 'height (cdr (assq 'height parsed)))
2535 default-frame-alist)))
2536 (if (and (assq 'width parsed)
2537 (not (assq 'width default-frame-alist)))
2538 (setq default-frame-alist
2539 (cons (cons 'width (cdr (assq 'width parsed)))
2540 default-frame-alist))))))
2542 ;; Check the reverseVideo resource.
2543 (let ((case-fold-search t))
2544 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
2546 (string-match "^\\(true\\|yes\\|on\\)$" rv))
2547 (setq default-frame-alist
2548 (cons '(reverse . t) default-frame-alist)))))
2550 (setq mac-initialized t)))
2552 (defun mac-handle-scroll-bar-event (event)
2553 "Handle scroll bar EVENT to emulate Mac Toolbox style scrolling."
2555 (let* ((position (event-start event))
2556 (window (nth 0 position))
2557 (bar-part (nth 4 position)))
2558 (select-window window)
2561 (goto-char (window-start window))
2562 (mac-scroll-down-line))
2563 ((eq bar-part 'above-handle)
2565 ((eq bar-part 'handle)
2566 (scroll-bar-drag event))
2567 ((eq bar-part 'below-handle)
2569 ((eq bar-part 'down)
2570 (goto-char (window-start window))
2571 (mac-scroll-up-line)))))
2573 (defun mac-scroll-ignore-events ()
2574 ;; Ignore confusing non-mouse events
2575 (while (not (memq (car-safe (read-event))
2576 '(mouse-1 double-mouse-1 triple-mouse-1))) nil))
2578 (defun mac-scroll-down ()
2580 (mac-scroll-ignore-events)
2583 (defun mac-scroll-down-line ()
2585 (mac-scroll-ignore-events)
2588 (defun mac-scroll-up ()
2590 (mac-scroll-ignore-events)
2593 (defun mac-scroll-up-line ()
2595 (mac-scroll-ignore-events)
2602 (unless (eq system-type 'darwin)
2603 ;; This variable specifies the Unix program to call (as a process) to
2604 ;; determine the amount of free space on a file system (defaults to
2605 ;; df). If it is not set to nil, ls-lisp will not work correctly
2606 ;; unless an external application df is implemented on the Mac.
2607 (setq directory-free-space-program nil)
2609 ;; Set this so that Emacs calls subprocesses with "sh" as shell to
2610 ;; expand filenames Note no subprocess for the shell is actually
2611 ;; started (see run_mac_command in sysdep.c).
2612 (setq shell-file-name "sh")
2614 ;; Some system variables are encoded with the system script code.
2615 (dolist (v '(system-name
2616 emacs-build-system ; Mac OS 9 version cannot dump
2617 user-login-name user-real-login-name user-full-name))
2618 (set v (decode-coding-string (symbol-value v) mac-system-coding-system))))
2620 ;; Now the default directory is changed to the user's home directory
2621 ;; in emacs.c if invoked from the WindowServer (with -psn_* option).
2622 ;; (if (string= default-directory "/")
2625 ;; Darwin 6- pty breakage is now controlled from the C code so that
2626 ;; it applies to all builds on darwin. See s/darwin.h PTY_ITERATION.
2627 ;; (setq process-connection-type t)
2629 ;; Assume that fonts are always scalable on the Mac. This sometimes
2630 ;; results in characters with jagged edges. However, without it,
2631 ;; fonts with both truetype and bitmap representations but no italic
2632 ;; or bold bitmap versions will not display these variants correctly.
2633 (setq scalable-fonts-allowed t)
2635 (add-to-list 'handle-args-function-alist '(mac . x-handle-args))
2636 (add-to-list 'frame-creation-function-alist '(mac . x-create-frame-with-faces))
2637 (add-to-list 'window-system-initialization-alist '(mac . mac-initialize-window-system))
2641 ;; arch-tag: 71dfcd14-cde8-4d66-b05c-85ec94fb23a6
2642 ;;; mac-win.el ends here