]> code.delx.au - gnu-emacs/blob - lisp/term/mac-win.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / term / mac-win.el
1 ;;; mac-win.el --- parse switches controlling interface with Mac window system -*-coding: utf-8-*-
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Andrew Choi <akochoi@mac.com>
7 ;; Keywords: terminals
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
32
33 ;; startup.el will then examine startup files, and eventually call the hooks
34 ;; which create the first window(s).
35
36 ;;; Code:
37 \f
38 ;; These are the standard X switches from the Xt Initialize.c file of
39 ;; Release 4.
40
41 ;; Command line Resource Manager string
42
43 ;; +rv *reverseVideo
44 ;; +synchronous *synchronous
45 ;; -background *background
46 ;; -bd *borderColor
47 ;; -bg *background
48 ;; -bordercolor *borderColor
49 ;; -borderwidth .borderWidth
50 ;; -bw .borderWidth
51 ;; -display .display
52 ;; -fg *foreground
53 ;; -fn *font
54 ;; -font *font
55 ;; -foreground *foreground
56 ;; -geometry .geometry
57 ;; -iconic .iconic
58 ;; -name .name
59 ;; -reverse *reverseVideo
60 ;; -rv *reverseVideo
61 ;; -selectionTimeout .selectionTimeout
62 ;; -synchronous *synchronous
63 ;; -xrm
64
65 ;; An alist of X options and the function which handles them. See
66 ;; ../startup.el.
67
68 ;; (if (not (eq window-system 'mac))
69 ;; (error "%s: Loading mac-win.el but not compiled for Mac" (invocation-name)))
70
71 (require 'frame)
72 (require 'mouse)
73 (require 'scroll-bar)
74 (require 'faces)
75 (require 'select)
76 (require 'menu-bar)
77 (require 'fontset)
78 (require 'dnd)
79
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 mac-ts-active-input-buf)
87 (defvar x-invocation-args)
88 (declare-function mac-code-convert-string "mac.c")
89 (declare-function mac-coerce-ae-data "mac.c")
90 (declare-function mac-resume-apple-event "macselect.c")
91 ;; Suppress warning when compiling on non-Mac.
92 (declare-function mac-font-panel-mode "mac-win.el")
93 (declare-function mac-atsu-font-face-attributes "macfns.c")
94 (declare-function mac-ae-set-reply-parameter "macselect.c")
95 (declare-function mac-clear-font-name-table "macfns.c")
96
97 (defvar x-command-line-resources nil)
98
99 ;; Handler for switches of the form "-switch value" or "-switch".
100 (defun x-handle-switch (switch)
101 (let ((aelt (assoc switch command-line-x-option-alist)))
102 (if aelt
103 (let ((param (nth 3 aelt))
104 (value (nth 4 aelt)))
105 (if value
106 (setq default-frame-alist
107 (cons (cons param value)
108 default-frame-alist))
109 (setq default-frame-alist
110 (cons (cons param
111 (car x-invocation-args))
112 default-frame-alist)
113 x-invocation-args (cdr x-invocation-args)))))))
114
115 ;; Handler for switches of the form "-switch n"
116 (defun x-handle-numeric-switch (switch)
117 (let ((aelt (assoc switch command-line-x-option-alist)))
118 (if aelt
119 (let ((param (nth 3 aelt)))
120 (setq default-frame-alist
121 (cons (cons param
122 (string-to-number (car x-invocation-args)))
123 default-frame-alist)
124 x-invocation-args
125 (cdr x-invocation-args))))))
126
127 ;; Handle options that apply to initial frame only
128 (defun x-handle-initial-switch (switch)
129 (let ((aelt (assoc switch command-line-x-option-alist)))
130 (if aelt
131 (let ((param (nth 3 aelt))
132 (value (nth 4 aelt)))
133 (if value
134 (setq initial-frame-alist
135 (cons (cons param value)
136 initial-frame-alist))
137 (setq initial-frame-alist
138 (cons (cons param
139 (car x-invocation-args))
140 initial-frame-alist)
141 x-invocation-args (cdr x-invocation-args)))))))
142
143 ;; Make -iconic apply only to the initial frame!
144 (defun x-handle-iconic (switch)
145 (setq initial-frame-alist
146 (cons '(visibility . icon) initial-frame-alist)))
147
148 ;; Handle the -xrm option.
149 (defun x-handle-xrm-switch (switch)
150 (unless (consp x-invocation-args)
151 (error "%s: missing argument to `%s' option" (invocation-name) switch))
152 (setq x-command-line-resources
153 (if (null x-command-line-resources)
154 (car x-invocation-args)
155 (concat x-command-line-resources "\n" (car x-invocation-args))))
156 (setq x-invocation-args (cdr x-invocation-args)))
157
158 ;; Handle the geometry option
159 (defun x-handle-geometry (switch)
160 (let* ((geo (x-parse-geometry (car x-invocation-args)))
161 (left (assq 'left geo))
162 (top (assq 'top geo))
163 (height (assq 'height geo))
164 (width (assq 'width geo)))
165 (if (or height width)
166 (setq default-frame-alist
167 (append default-frame-alist
168 '((user-size . t))
169 (if height (list height))
170 (if width (list width)))
171 initial-frame-alist
172 (append initial-frame-alist
173 '((user-size . t))
174 (if height (list height))
175 (if width (list width)))))
176 (if (or left top)
177 (setq initial-frame-alist
178 (append initial-frame-alist
179 '((user-position . t))
180 (if left (list left))
181 (if top (list top)))))
182 (setq x-invocation-args (cdr x-invocation-args))))
183
184 ;; Handle the -name option. Set the variable x-resource-name
185 ;; to the option's operand; set the name of
186 ;; the initial frame, too.
187 (defun x-handle-name-switch (switch)
188 (or (consp x-invocation-args)
189 (error "%s: missing argument to `%s' option" (invocation-name) switch))
190 (setq x-resource-name (car x-invocation-args)
191 x-invocation-args (cdr x-invocation-args))
192 (setq initial-frame-alist (cons (cons 'name x-resource-name)
193 initial-frame-alist)))
194
195 (defvar x-display-name nil
196 "The display name specifying server and frame.")
197
198 (defun x-handle-display (switch)
199 (setq x-display-name (car x-invocation-args)
200 x-invocation-args (cdr x-invocation-args)))
201
202 (defun x-handle-args (args)
203 "Process the X-related command line options in ARGS.
204 This is done before the user's startup file is loaded. They are copied to
205 `x-invocation-args', from which the X-related things are extracted, first
206 the switch (e.g., \"-fg\") in the following code, and possible values
207 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
208 This function returns ARGS minus the arguments that have been processed."
209 ;; We use ARGS to accumulate the args that we don't handle here, to return.
210 (setq x-invocation-args args
211 args nil)
212 (while (and x-invocation-args
213 (not (equal (car x-invocation-args) "--")))
214 (let* ((this-switch (car x-invocation-args))
215 (orig-this-switch this-switch)
216 completion argval aelt handler)
217 (setq x-invocation-args (cdr x-invocation-args))
218 ;; Check for long options with attached arguments
219 ;; and separate out the attached option argument into argval.
220 (if (string-match "^--[^=]*=" this-switch)
221 (setq argval (substring this-switch (match-end 0))
222 this-switch (substring this-switch 0 (1- (match-end 0)))))
223 ;; Complete names of long options.
224 (if (string-match "^--" this-switch)
225 (progn
226 (setq completion (try-completion this-switch command-line-x-option-alist))
227 (if (eq completion t)
228 ;; Exact match for long option.
229 nil
230 (if (stringp completion)
231 (let ((elt (assoc completion command-line-x-option-alist)))
232 ;; Check for abbreviated long option.
233 (or elt
234 (error "Option `%s' is ambiguous" this-switch))
235 (setq this-switch completion))))))
236 (setq aelt (assoc this-switch command-line-x-option-alist))
237 (if aelt (setq handler (nth 2 aelt)))
238 (if handler
239 (if argval
240 (let ((x-invocation-args
241 (cons argval x-invocation-args)))
242 (funcall handler this-switch))
243 (funcall handler this-switch))
244 (setq args (cons orig-this-switch args)))))
245 (nconc (nreverse args) x-invocation-args))
246
247 \f
248 ;;
249 ;; Standard Mac cursor shapes
250 ;;
251
252 (defconst mac-pointer-arrow 0)
253 (defconst mac-pointer-copy-arrow 1)
254 (defconst mac-pointer-alias-arrow 2)
255 (defconst mac-pointer-contextual-menu-arrow 3)
256 (defconst mac-pointer-I-beam 4)
257 (defconst mac-pointer-cross 5)
258 (defconst mac-pointer-plus 6)
259 (defconst mac-pointer-watch 7)
260 (defconst mac-pointer-closed-hand 8)
261 (defconst mac-pointer-open-hand 9)
262 (defconst mac-pointer-pointing-hand 10)
263 (defconst mac-pointer-counting-up-hand 11)
264 (defconst mac-pointer-counting-down-hand 12)
265 (defconst mac-pointer-counting-up-and-down-hand 13)
266 (defconst mac-pointer-spinning 14)
267 (defconst mac-pointer-resize-left 15)
268 (defconst mac-pointer-resize-right 16)
269 (defconst mac-pointer-resize-left-right 17)
270 ;; Mac OS X 10.2 and later
271 (defconst mac-pointer-not-allowed 18)
272 ;; Mac OS X 10.3 and later
273 (defconst mac-pointer-resize-up 19)
274 (defconst mac-pointer-resize-down 20)
275 (defconst mac-pointer-resize-up-down 21)
276 (defconst mac-pointer-poof 22)
277
278 ;;
279 ;; Standard X cursor shapes that have Mac counterparts
280 ;;
281
282 (defconst x-pointer-left-ptr mac-pointer-arrow)
283 (defconst x-pointer-xterm mac-pointer-I-beam)
284 (defconst x-pointer-crosshair mac-pointer-cross)
285 (defconst x-pointer-plus mac-pointer-plus)
286 (defconst x-pointer-watch mac-pointer-watch)
287 (defconst x-pointer-hand2 mac-pointer-pointing-hand)
288 (defconst x-pointer-left-side mac-pointer-resize-left)
289 (defconst x-pointer-right-side mac-pointer-resize-right)
290 (defconst x-pointer-sb-h-double-arrow mac-pointer-resize-left-right)
291 (defconst x-pointer-top-side mac-pointer-resize-up)
292 (defconst x-pointer-bottom-side mac-pointer-resize-down)
293 (defconst x-pointer-sb-v-double-arrow mac-pointer-resize-up-down)
294
295 \f
296 ;;
297 ;; Available colors
298 ;;
299
300 (defvar x-colors '("LightGreen"
301 "light green"
302 "DarkRed"
303 "dark red"
304 "DarkMagenta"
305 "dark magenta"
306 "DarkCyan"
307 "dark cyan"
308 "DarkBlue"
309 "dark blue"
310 "DarkGray"
311 "dark gray"
312 "DarkGrey"
313 "dark grey"
314 "grey100"
315 "gray100"
316 "grey99"
317 "gray99"
318 "grey98"
319 "gray98"
320 "grey97"
321 "gray97"
322 "grey96"
323 "gray96"
324 "grey95"
325 "gray95"
326 "grey94"
327 "gray94"
328 "grey93"
329 "gray93"
330 "grey92"
331 "gray92"
332 "grey91"
333 "gray91"
334 "grey90"
335 "gray90"
336 "grey89"
337 "gray89"
338 "grey88"
339 "gray88"
340 "grey87"
341 "gray87"
342 "grey86"
343 "gray86"
344 "grey85"
345 "gray85"
346 "grey84"
347 "gray84"
348 "grey83"
349 "gray83"
350 "grey82"
351 "gray82"
352 "grey81"
353 "gray81"
354 "grey80"
355 "gray80"
356 "grey79"
357 "gray79"
358 "grey78"
359 "gray78"
360 "grey77"
361 "gray77"
362 "grey76"
363 "gray76"
364 "grey75"
365 "gray75"
366 "grey74"
367 "gray74"
368 "grey73"
369 "gray73"
370 "grey72"
371 "gray72"
372 "grey71"
373 "gray71"
374 "grey70"
375 "gray70"
376 "grey69"
377 "gray69"
378 "grey68"
379 "gray68"
380 "grey67"
381 "gray67"
382 "grey66"
383 "gray66"
384 "grey65"
385 "gray65"
386 "grey64"
387 "gray64"
388 "grey63"
389 "gray63"
390 "grey62"
391 "gray62"
392 "grey61"
393 "gray61"
394 "grey60"
395 "gray60"
396 "grey59"
397 "gray59"
398 "grey58"
399 "gray58"
400 "grey57"
401 "gray57"
402 "grey56"
403 "gray56"
404 "grey55"
405 "gray55"
406 "grey54"
407 "gray54"
408 "grey53"
409 "gray53"
410 "grey52"
411 "gray52"
412 "grey51"
413 "gray51"
414 "grey50"
415 "gray50"
416 "grey49"
417 "gray49"
418 "grey48"
419 "gray48"
420 "grey47"
421 "gray47"
422 "grey46"
423 "gray46"
424 "grey45"
425 "gray45"
426 "grey44"
427 "gray44"
428 "grey43"
429 "gray43"
430 "grey42"
431 "gray42"
432 "grey41"
433 "gray41"
434 "grey40"
435 "gray40"
436 "grey39"
437 "gray39"
438 "grey38"
439 "gray38"
440 "grey37"
441 "gray37"
442 "grey36"
443 "gray36"
444 "grey35"
445 "gray35"
446 "grey34"
447 "gray34"
448 "grey33"
449 "gray33"
450 "grey32"
451 "gray32"
452 "grey31"
453 "gray31"
454 "grey30"
455 "gray30"
456 "grey29"
457 "gray29"
458 "grey28"
459 "gray28"
460 "grey27"
461 "gray27"
462 "grey26"
463 "gray26"
464 "grey25"
465 "gray25"
466 "grey24"
467 "gray24"
468 "grey23"
469 "gray23"
470 "grey22"
471 "gray22"
472 "grey21"
473 "gray21"
474 "grey20"
475 "gray20"
476 "grey19"
477 "gray19"
478 "grey18"
479 "gray18"
480 "grey17"
481 "gray17"
482 "grey16"
483 "gray16"
484 "grey15"
485 "gray15"
486 "grey14"
487 "gray14"
488 "grey13"
489 "gray13"
490 "grey12"
491 "gray12"
492 "grey11"
493 "gray11"
494 "grey10"
495 "gray10"
496 "grey9"
497 "gray9"
498 "grey8"
499 "gray8"
500 "grey7"
501 "gray7"
502 "grey6"
503 "gray6"
504 "grey5"
505 "gray5"
506 "grey4"
507 "gray4"
508 "grey3"
509 "gray3"
510 "grey2"
511 "gray2"
512 "grey1"
513 "gray1"
514 "grey0"
515 "gray0"
516 "thistle4"
517 "thistle3"
518 "thistle2"
519 "thistle1"
520 "MediumPurple4"
521 "MediumPurple3"
522 "MediumPurple2"
523 "MediumPurple1"
524 "purple4"
525 "purple3"
526 "purple2"
527 "purple1"
528 "DarkOrchid4"
529 "DarkOrchid3"
530 "DarkOrchid2"
531 "DarkOrchid1"
532 "MediumOrchid4"
533 "MediumOrchid3"
534 "MediumOrchid2"
535 "MediumOrchid1"
536 "plum4"
537 "plum3"
538 "plum2"
539 "plum1"
540 "orchid4"
541 "orchid3"
542 "orchid2"
543 "orchid1"
544 "magenta4"
545 "magenta3"
546 "magenta2"
547 "magenta1"
548 "VioletRed4"
549 "VioletRed3"
550 "VioletRed2"
551 "VioletRed1"
552 "maroon4"
553 "maroon3"
554 "maroon2"
555 "maroon1"
556 "PaleVioletRed4"
557 "PaleVioletRed3"
558 "PaleVioletRed2"
559 "PaleVioletRed1"
560 "LightPink4"
561 "LightPink3"
562 "LightPink2"
563 "LightPink1"
564 "pink4"
565 "pink3"
566 "pink2"
567 "pink1"
568 "HotPink4"
569 "HotPink3"
570 "HotPink2"
571 "HotPink1"
572 "DeepPink4"
573 "DeepPink3"
574 "DeepPink2"
575 "DeepPink1"
576 "red4"
577 "red3"
578 "red2"
579 "red1"
580 "OrangeRed4"
581 "OrangeRed3"
582 "OrangeRed2"
583 "OrangeRed1"
584 "tomato4"
585 "tomato3"
586 "tomato2"
587 "tomato1"
588 "coral4"
589 "coral3"
590 "coral2"
591 "coral1"
592 "DarkOrange4"
593 "DarkOrange3"
594 "DarkOrange2"
595 "DarkOrange1"
596 "orange4"
597 "orange3"
598 "orange2"
599 "orange1"
600 "LightSalmon4"
601 "LightSalmon3"
602 "LightSalmon2"
603 "LightSalmon1"
604 "salmon4"
605 "salmon3"
606 "salmon2"
607 "salmon1"
608 "brown4"
609 "brown3"
610 "brown2"
611 "brown1"
612 "firebrick4"
613 "firebrick3"
614 "firebrick2"
615 "firebrick1"
616 "chocolate4"
617 "chocolate3"
618 "chocolate2"
619 "chocolate1"
620 "tan4"
621 "tan3"
622 "tan2"
623 "tan1"
624 "wheat4"
625 "wheat3"
626 "wheat2"
627 "wheat1"
628 "burlywood4"
629 "burlywood3"
630 "burlywood2"
631 "burlywood1"
632 "sienna4"
633 "sienna3"
634 "sienna2"
635 "sienna1"
636 "IndianRed4"
637 "IndianRed3"
638 "IndianRed2"
639 "IndianRed1"
640 "RosyBrown4"
641 "RosyBrown3"
642 "RosyBrown2"
643 "RosyBrown1"
644 "DarkGoldenrod4"
645 "DarkGoldenrod3"
646 "DarkGoldenrod2"
647 "DarkGoldenrod1"
648 "goldenrod4"
649 "goldenrod3"
650 "goldenrod2"
651 "goldenrod1"
652 "gold4"
653 "gold3"
654 "gold2"
655 "gold1"
656 "yellow4"
657 "yellow3"
658 "yellow2"
659 "yellow1"
660 "LightYellow4"
661 "LightYellow3"
662 "LightYellow2"
663 "LightYellow1"
664 "LightGoldenrod4"
665 "LightGoldenrod3"
666 "LightGoldenrod2"
667 "LightGoldenrod1"
668 "khaki4"
669 "khaki3"
670 "khaki2"
671 "khaki1"
672 "DarkOliveGreen4"
673 "DarkOliveGreen3"
674 "DarkOliveGreen2"
675 "DarkOliveGreen1"
676 "OliveDrab4"
677 "OliveDrab3"
678 "OliveDrab2"
679 "OliveDrab1"
680 "chartreuse4"
681 "chartreuse3"
682 "chartreuse2"
683 "chartreuse1"
684 "green4"
685 "green3"
686 "green2"
687 "green1"
688 "SpringGreen4"
689 "SpringGreen3"
690 "SpringGreen2"
691 "SpringGreen1"
692 "PaleGreen4"
693 "PaleGreen3"
694 "PaleGreen2"
695 "PaleGreen1"
696 "SeaGreen4"
697 "SeaGreen3"
698 "SeaGreen2"
699 "SeaGreen1"
700 "DarkSeaGreen4"
701 "DarkSeaGreen3"
702 "DarkSeaGreen2"
703 "DarkSeaGreen1"
704 "aquamarine4"
705 "aquamarine3"
706 "aquamarine2"
707 "aquamarine1"
708 "DarkSlateGray4"
709 "DarkSlateGray3"
710 "DarkSlateGray2"
711 "DarkSlateGray1"
712 "cyan4"
713 "cyan3"
714 "cyan2"
715 "cyan1"
716 "turquoise4"
717 "turquoise3"
718 "turquoise2"
719 "turquoise1"
720 "CadetBlue4"
721 "CadetBlue3"
722 "CadetBlue2"
723 "CadetBlue1"
724 "PaleTurquoise4"
725 "PaleTurquoise3"
726 "PaleTurquoise2"
727 "PaleTurquoise1"
728 "LightCyan4"
729 "LightCyan3"
730 "LightCyan2"
731 "LightCyan1"
732 "LightBlue4"
733 "LightBlue3"
734 "LightBlue2"
735 "LightBlue1"
736 "LightSteelBlue4"
737 "LightSteelBlue3"
738 "LightSteelBlue2"
739 "LightSteelBlue1"
740 "SlateGray4"
741 "SlateGray3"
742 "SlateGray2"
743 "SlateGray1"
744 "LightSkyBlue4"
745 "LightSkyBlue3"
746 "LightSkyBlue2"
747 "LightSkyBlue1"
748 "SkyBlue4"
749 "SkyBlue3"
750 "SkyBlue2"
751 "SkyBlue1"
752 "DeepSkyBlue4"
753 "DeepSkyBlue3"
754 "DeepSkyBlue2"
755 "DeepSkyBlue1"
756 "SteelBlue4"
757 "SteelBlue3"
758 "SteelBlue2"
759 "SteelBlue1"
760 "DodgerBlue4"
761 "DodgerBlue3"
762 "DodgerBlue2"
763 "DodgerBlue1"
764 "blue4"
765 "blue3"
766 "blue2"
767 "blue1"
768 "RoyalBlue4"
769 "RoyalBlue3"
770 "RoyalBlue2"
771 "RoyalBlue1"
772 "SlateBlue4"
773 "SlateBlue3"
774 "SlateBlue2"
775 "SlateBlue1"
776 "azure4"
777 "azure3"
778 "azure2"
779 "azure1"
780 "MistyRose4"
781 "MistyRose3"
782 "MistyRose2"
783 "MistyRose1"
784 "LavenderBlush4"
785 "LavenderBlush3"
786 "LavenderBlush2"
787 "LavenderBlush1"
788 "honeydew4"
789 "honeydew3"
790 "honeydew2"
791 "honeydew1"
792 "ivory4"
793 "ivory3"
794 "ivory2"
795 "ivory1"
796 "cornsilk4"
797 "cornsilk3"
798 "cornsilk2"
799 "cornsilk1"
800 "LemonChiffon4"
801 "LemonChiffon3"
802 "LemonChiffon2"
803 "LemonChiffon1"
804 "NavajoWhite4"
805 "NavajoWhite3"
806 "NavajoWhite2"
807 "NavajoWhite1"
808 "PeachPuff4"
809 "PeachPuff3"
810 "PeachPuff2"
811 "PeachPuff1"
812 "bisque4"
813 "bisque3"
814 "bisque2"
815 "bisque1"
816 "AntiqueWhite4"
817 "AntiqueWhite3"
818 "AntiqueWhite2"
819 "AntiqueWhite1"
820 "seashell4"
821 "seashell3"
822 "seashell2"
823 "seashell1"
824 "snow4"
825 "snow3"
826 "snow2"
827 "snow1"
828 "thistle"
829 "MediumPurple"
830 "medium purple"
831 "purple"
832 "BlueViolet"
833 "blue violet"
834 "DarkViolet"
835 "dark violet"
836 "DarkOrchid"
837 "dark orchid"
838 "MediumOrchid"
839 "medium orchid"
840 "orchid"
841 "plum"
842 "violet"
843 "magenta"
844 "VioletRed"
845 "violet red"
846 "MediumVioletRed"
847 "medium violet red"
848 "maroon"
849 "PaleVioletRed"
850 "pale violet red"
851 "LightPink"
852 "light pink"
853 "pink"
854 "DeepPink"
855 "deep pink"
856 "HotPink"
857 "hot pink"
858 "red"
859 "OrangeRed"
860 "orange red"
861 "tomato"
862 "LightCoral"
863 "light coral"
864 "coral"
865 "DarkOrange"
866 "dark orange"
867 "orange"
868 "LightSalmon"
869 "light salmon"
870 "salmon"
871 "DarkSalmon"
872 "dark salmon"
873 "brown"
874 "firebrick"
875 "chocolate"
876 "tan"
877 "SandyBrown"
878 "sandy brown"
879 "wheat"
880 "beige"
881 "burlywood"
882 "peru"
883 "sienna"
884 "SaddleBrown"
885 "saddle brown"
886 "IndianRed"
887 "indian red"
888 "RosyBrown"
889 "rosy brown"
890 "DarkGoldenrod"
891 "dark goldenrod"
892 "goldenrod"
893 "LightGoldenrod"
894 "light goldenrod"
895 "gold"
896 "yellow"
897 "LightYellow"
898 "light yellow"
899 "LightGoldenrodYellow"
900 "light goldenrod yellow"
901 "PaleGoldenrod"
902 "pale goldenrod"
903 "khaki"
904 "DarkKhaki"
905 "dark khaki"
906 "OliveDrab"
907 "olive drab"
908 "ForestGreen"
909 "forest green"
910 "YellowGreen"
911 "yellow green"
912 "LimeGreen"
913 "lime green"
914 "GreenYellow"
915 "green yellow"
916 "MediumSpringGreen"
917 "medium spring green"
918 "chartreuse"
919 "green"
920 "LawnGreen"
921 "lawn green"
922 "SpringGreen"
923 "spring green"
924 "PaleGreen"
925 "pale green"
926 "LightSeaGreen"
927 "light sea green"
928 "MediumSeaGreen"
929 "medium sea green"
930 "SeaGreen"
931 "sea green"
932 "DarkSeaGreen"
933 "dark sea green"
934 "DarkOliveGreen"
935 "dark olive green"
936 "DarkGreen"
937 "dark green"
938 "aquamarine"
939 "MediumAquamarine"
940 "medium aquamarine"
941 "CadetBlue"
942 "cadet blue"
943 "LightCyan"
944 "light cyan"
945 "cyan"
946 "turquoise"
947 "MediumTurquoise"
948 "medium turquoise"
949 "DarkTurquoise"
950 "dark turquoise"
951 "PaleTurquoise"
952 "pale turquoise"
953 "PowderBlue"
954 "powder blue"
955 "LightBlue"
956 "light blue"
957 "LightSteelBlue"
958 "light steel blue"
959 "SteelBlue"
960 "steel blue"
961 "LightSkyBlue"
962 "light sky blue"
963 "SkyBlue"
964 "sky blue"
965 "DeepSkyBlue"
966 "deep sky blue"
967 "DodgerBlue"
968 "dodger blue"
969 "blue"
970 "RoyalBlue"
971 "royal blue"
972 "MediumBlue"
973 "medium blue"
974 "LightSlateBlue"
975 "light slate blue"
976 "MediumSlateBlue"
977 "medium slate blue"
978 "SlateBlue"
979 "slate blue"
980 "DarkSlateBlue"
981 "dark slate blue"
982 "CornflowerBlue"
983 "cornflower blue"
984 "NavyBlue"
985 "navy blue"
986 "navy"
987 "MidnightBlue"
988 "midnight blue"
989 "LightGray"
990 "light gray"
991 "LightGrey"
992 "light grey"
993 "grey"
994 "gray"
995 "LightSlateGrey"
996 "light slate grey"
997 "LightSlateGray"
998 "light slate gray"
999 "SlateGrey"
1000 "slate grey"
1001 "SlateGray"
1002 "slate gray"
1003 "DimGrey"
1004 "dim grey"
1005 "DimGray"
1006 "dim gray"
1007 "DarkSlateGrey"
1008 "dark slate grey"
1009 "DarkSlateGray"
1010 "dark slate gray"
1011 "black"
1012 "white"
1013 "MistyRose"
1014 "misty rose"
1015 "LavenderBlush"
1016 "lavender blush"
1017 "lavender"
1018 "AliceBlue"
1019 "alice blue"
1020 "azure"
1021 "MintCream"
1022 "mint cream"
1023 "honeydew"
1024 "seashell"
1025 "LemonChiffon"
1026 "lemon chiffon"
1027 "ivory"
1028 "cornsilk"
1029 "moccasin"
1030 "NavajoWhite"
1031 "navajo white"
1032 "PeachPuff"
1033 "peach puff"
1034 "bisque"
1035 "BlanchedAlmond"
1036 "blanched almond"
1037 "PapayaWhip"
1038 "papaya whip"
1039 "AntiqueWhite"
1040 "antique white"
1041 "linen"
1042 "OldLace"
1043 "old lace"
1044 "FloralWhite"
1045 "floral white"
1046 "gainsboro"
1047 "WhiteSmoke"
1048 "white smoke"
1049 "GhostWhite"
1050 "ghost white"
1051 "snow")
1052 "The list of X colors from the `rgb.txt' file.
1053 XConsortium: rgb.txt,v 10.41 94/02/20 18:39:36 rws Exp")
1054
1055 (defun xw-defined-colors (&optional frame)
1056 "Internal function called by `defined-colors', which see."
1057 (or frame (setq frame (selected-frame)))
1058 (let ((all-colors x-colors)
1059 (this-color nil)
1060 (defined-colors nil))
1061 (while all-colors
1062 (setq this-color (car all-colors)
1063 all-colors (cdr all-colors))
1064 (and (color-supported-p this-color frame t)
1065 (setq defined-colors (cons this-color defined-colors))))
1066 defined-colors))
1067 \f
1068 ;;;; Function keys
1069
1070 (defun x-setup-function-keys (frame)
1071 "Setup Function Keys for mac."
1072 ;; Don't do this twice on the same display, or it would break
1073 ;; normal-erase-is-backspace-mode.
1074 (unless (terminal-parameter frame 'x-setup-function-keys)
1075 (with-selected-frame frame
1076 ;; Map certain keypad keys into ASCII characters
1077 ;; that people usually expect.
1078 (define-key local-function-key-map [backspace] [?\d])
1079 (define-key local-function-key-map [delete] [?\d])
1080 (define-key local-function-key-map [tab] [?\t])
1081 (define-key local-function-key-map [linefeed] [?\n])
1082 (define-key local-function-key-map [clear] [?\C-l])
1083 (define-key local-function-key-map [return] [?\C-m])
1084 (define-key local-function-key-map [escape] [?\e])
1085 (define-key local-function-key-map [M-backspace] [?\M-\d])
1086 (define-key local-function-key-map [M-delete] [?\M-\d])
1087 (define-key local-function-key-map [M-tab] [?\M-\t])
1088 (define-key local-function-key-map [M-linefeed] [?\M-\n])
1089 (define-key local-function-key-map [M-clear] [?\M-\C-l])
1090 (define-key local-function-key-map [M-return] [?\M-\C-m])
1091 (define-key local-function-key-map [M-escape] [?\M-\e])
1092 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
1093 local-function-key-map global-map))
1094 (set-terminal-parameter frame 'x-setup-function-keys t)))
1095
1096 ;; These tell read-char how to convert
1097 ;; these special chars to ASCII.
1098 (put 'backspace 'ascii-character ?\d)
1099 (put 'delete 'ascii-character ?\d)
1100 (put 'tab 'ascii-character ?\t)
1101 (put 'linefeed 'ascii-character ?\n)
1102 (put 'clear 'ascii-character ?\C-l)
1103 (put 'return 'ascii-character ?\C-m)
1104 (put 'escape 'ascii-character ?\e)
1105
1106 ;; Modifier name `ctrl' is an alias of `control'.
1107 (put 'ctrl 'modifier-value (get 'control 'modifier-value))
1108
1109 \f
1110 ;;;; Script codes and coding systems
1111 (defconst mac-script-code-coding-systems
1112 '((0 . mac-roman) ; smRoman
1113 (1 . japanese-shift-jis) ; smJapanese
1114 (2 . chinese-big5) ; smTradChinese
1115 (3 . korean-iso-8bit) ; smKorean
1116 (7 . mac-cyrillic) ; smCyrillic
1117 (25 . chinese-iso-8bit) ; smSimpChinese
1118 (29 . mac-centraleurroman) ; smCentralEuroRoman
1119 )
1120 "Alist of Mac script codes vs Emacs coding systems.")
1121
1122 (defun mac-add-charset-info (xlfd-charset mac-text-encoding)
1123 "Add a character set to display with Mac fonts.
1124 Create an entry in `mac-charset-info-alist'.
1125 XLFD-CHARSET is a string which will appear in the XLFD font name
1126 to identify the character set. MAC-TEXT-ENCODING is the
1127 correspoinding TextEncodingBase value."
1128 (add-to-list 'mac-charset-info-alist
1129 (list xlfd-charset mac-text-encoding
1130 (cdr (assq mac-text-encoding
1131 mac-script-code-coding-systems)))))
1132
1133 (setq mac-charset-info-alist nil)
1134 (mac-add-charset-info "mac-roman" 0)
1135 (mac-add-charset-info "jisx0208.1983-sjis" 1)
1136 (mac-add-charset-info "jisx0201.1976-0" 1)
1137 (mac-add-charset-info "big5-0" 2)
1138 (mac-add-charset-info "ksc5601.1989-0" 3)
1139 (mac-add-charset-info "mac-cyrillic" 7)
1140 (mac-add-charset-info "gb2312.1980-0" 25)
1141 (mac-add-charset-info "mac-centraleurroman" 29)
1142 (mac-add-charset-info "mac-symbol" 33)
1143 (mac-add-charset-info "adobe-fontspecific" 33) ; for X-Symbol
1144 (mac-add-charset-info "mac-dingbats" 34)
1145 (mac-add-charset-info "iso10646-1" 126) ; for ATSUI
1146
1147 (define-charset 'mac-centraleurroman
1148 "Mac Central European Roman"
1149 :short-name "Mac CE"
1150 :ascii-compatible-p t
1151 :code-space [0 255]
1152 :map
1153 (let ((tbl
1154 [?\Ä ?\Ā ?\ā ?\É ?\Ą ?\Ö ?\Ü ?\á ?\ą ?\Č ?\ä ?\č ?\Ć ?\ć ?\é ?\Ź
1155 ?\ź ?\Ď ?\í ?\ď ?\Ē ?\ē ?\Ė ?\ó ?\ė ?\ô ?\ö ?\õ ?\ú ?\Ě ?\ě ?\ü
1156 ?\† ?\° ?\Ę ?\£ ?\§ ?\• ?\¶ ?\ß ?\® ?\© ?\™ ?\ę ?\¨ ?\≠ ?\ģ ?\Į
1157 ?\į ?\Ī ?\≤ ?\≥ ?\ī ?\Ķ ?\∂ ?\∑ ?\ł ?\Ļ ?\ļ ?\Ľ ?\ľ ?\Ĺ ?\ĺ ?\Ņ
1158 ?\ņ ?\Ń ?\¬ ?\√ ?\ń ?\Ň ?\∆ ?\« ?\» ?\… ?\  ?\ň ?\Ő ?\Õ ?\ő ?\Ō
1159 ?\– ?\— ?\“ ?\” ?\‘ ?\’ ?\÷ ?\◊ ?\ō ?\Ŕ ?\ŕ ?\Ř ?\‹ ?\› ?\ř ?\Ŗ
1160 ?\ŗ ?\Š ?\‚ ?\„ ?\š ?\Ś ?\ś ?\Á ?\Ť ?\ť ?\Í ?\Ž ?\ž ?\Ū ?\Ó ?\Ô
1161 ?\ū ?\Ů ?\Ú ?\ů ?\Ű ?\ű ?\Ų ?\ų ?\Ý ?\ý ?\ķ ?\Ż ?\Ł ?\ż ?\Ģ ?\ˇ])
1162 (map (make-vector 512 nil)))
1163 (or (= (length tbl) 128)
1164 (error "Invalid vector length: %d" (length tbl)))
1165 (dotimes (i 128)
1166 (aset map (* i 2) i)
1167 (aset map (1+ (* i 2)) i))
1168 (dotimes (i 128)
1169 (aset map (+ 256 (* i 2)) (+ 128 i))
1170 (aset map (+ 256 (1+ (* i 2))) (aref tbl i)))
1171 map))
1172
1173 (define-coding-system 'mac-centraleurroman
1174 "Mac Central European Roman Encoding (MIME:x-mac-centraleurroman)."
1175 :coding-type 'charset
1176 :mnemonic ?*
1177 :charset-list '(mac-centraleurroman)
1178 :mime-charset 'x-mac-centraleurroman)
1179
1180 (define-charset 'mac-cyrillic
1181 "Mac Cyrillic"
1182 :short-name "Mac CYRILLIC"
1183 :ascii-compatible-p t
1184 :code-space [0 255]
1185 :map
1186 (let ((tbl
1187 [?\А ?\Б ?\В ?\Г ?\Д ?\Е ?\Ж ?\З ?\И ?\Й ?\К ?\Л ?\М ?\Н ?\О ?\П
1188 ?\Р ?\С ?\Т ?\У ?\Ф ?\Х ?\Ц ?\Ч ?\Ш ?\Щ ?\Ъ ?\Ы ?\Ь ?\Э ?\Ю ?\Я
1189 ?\† ?\° ?\Ґ ?\£ ?\§ ?\• ?\¶ ?\І ?\® ?\© ?\™ ?\Ђ ?\ђ ?\≠ ?\Ѓ ?\ѓ
1190 ?\∞ ?\± ?\≤ ?\≥ ?\і ?\µ ?\ґ ?\Ј ?\Є ?\є ?\Ї ?\ї ?\Љ ?\љ ?\Њ ?\њ
1191 ?\ј ?\Ѕ ?\¬ ?\√ ?\ƒ ?\≈ ?\∆ ?\« ?\» ?\… ?\  ?\Ћ ?\ћ ?\Ќ ?\ќ ?\ѕ
1192 ?\– ?\— ?\“ ?\” ?\‘ ?\’ ?\÷ ?\„ ?\Ў ?\ў ?\Џ ?\џ ?\№ ?\Ё ?\ё ?\я
1193 ?\а ?\б ?\в ?\г ?\д ?\е ?\ж ?\з ?\и ?\й ?\к ?\л ?\м ?\н ?\о ?\п
1194 ?\р ?\с ?\т ?\у ?\ф ?\х ?\ц ?\ч ?\ш ?\щ ?\ъ ?\ы ?\ь ?\э ?\ю ?\€])
1195 (map (make-vector 512 nil)))
1196 (or (= (length tbl) 128)
1197 (error "Invalid vector length: %d" (length tbl)))
1198 (dotimes (i 128)
1199 (aset map (* i 2) i)
1200 (aset map (1+ (* i 2)) i))
1201 (dotimes (i 128)
1202 (aset map (+ 256 (* i 2)) (+ 128 i))
1203 (aset map (+ 256 (1+ (* i 2))) (aref tbl i)))
1204 map))
1205
1206 (define-coding-system 'mac-cyrillic
1207 "Mac Cyrillic Encoding (MIME:x-mac-cyrillic)."
1208 :coding-type 'charset
1209 :mnemonic ?*
1210 :charset-list '(mac-cyrillic)
1211 :mime-charset 'x-mac-cyrillic)
1212
1213 (define-charset 'mac-symbol
1214 "Mac Symbol"
1215 :short-name "Mac SYMBOL"
1216 :code-space [32 254]
1217 :map
1218 (let ((tbl-32-126
1219 [?\ ?\! ?\∀ ?\# ?\∃ ?\% ?\& ?\∍ ?\( ?\) ?\∗ ?\+ ?\, ?\− ?\. ?\/
1220 ?\0 ?\1 ?\2 ?\3 ?\4 ?\5 ?\6 ?\7 ?\8 ?\9 ?\: ?\; ?\< ?\= ?\> ?\?
1221 ?\≅ ?\Α ?\Β ?\Χ ?\Δ ?\Ε ?\Φ ?\Γ ?\Η ?\Ι ?\ϑ ?\Κ ?\Λ ?\Μ ?\Ν ?\Ο
1222 ?\Π ?\Θ ?\Ρ ?\Σ ?\Τ ?\Υ ?\ς ?\Ω ?\Ξ ?\Ψ ?\Ζ ?\[ ?\∴ ?\] ?\⊥ ?\_
1223 ?\ ?\α ?\β ?\χ ?\δ ?\ε ?\φ ?\γ ?\η ?\ι ?\ϕ ?\κ ?\λ ?\μ ?\ν ?\ο
1224 ?\π ?\θ ?\ρ ?\σ ?\τ ?\υ ?\ϖ ?\ω ?\ξ ?\ψ ?\ζ ?\{ ?\| ?\} ?\∼])
1225 (map-32-126 (make-vector (* (1+ (- 126 32)) 2) nil))
1226 (tbl-160-254
1227 ;; Mapping of the following characters are changed from the
1228 ;; original one:
1229 ;; 0xE2 0x00AE+0xF87F->0x00AE # REGISTERED SIGN, alternate: sans serif
1230 ;; 0xE3 0x00A9+0xF87F->0x00A9 # COPYRIGHT SIGN, alternate: sans serif
1231 ;; 0xE4 0x2122+0xF87F->0x2122 # TRADE MARK SIGN, alternate: sans serif
1232 [?\€ ?\ϒ ?\′ ?\≤ ?\⁄ ?\∞ ?\ƒ ?\♣ ?\♦ ?\♥ ?\♠ ?\↔ ?\← ?\↑ ?\→ ?\↓
1233 ?\° ?\± ?\″ ?\≥ ?\× ?\∝ ?\∂ ?\• ?\÷ ?\≠ ?\≡ ?\≈ ?\… ?\⏐ ?\⎯ ?\↵
1234 ?\ℵ ?\ℑ ?\ℜ ?\℘ ?\⊗ ?\⊕ ?\∅ ?\∩ ?\∪ ?\⊃ ?\⊇ ?\⊄ ?\⊂ ?\⊆ ?\∈ ?\∉
1235 ?\∠ ?\∇ ?\® ?\© ?\™ ?\∏ ?\√ ?\⋅ ?\¬ ?\∧ ?\∨ ?\⇔ ?\⇐ ?\⇑ ?\⇒ ?\⇓
1236 ?\◊ ?\〈 ?\® ?\© ?\™ ?\∑ ?\⎛ ?\⎜ ?\⎝ ?\⎡ ?\⎢ ?\⎣ ?\⎧ ?\⎨ ?\⎩ ?\⎪
1237 ?\ ?\〉 ?\∫ ?\⌠ ?\⎮ ?\⌡ ?\⎞ ?\⎟ ?\⎠ ?\⎤ ?\⎥ ?\⎦ ?\⎫ ?\⎬ ?\⎭])
1238 (map-160-254 (make-vector (* (1+ (- 254 160)) 2) nil)))
1239 (dotimes (i (1+ (- 126 32)))
1240 (aset map-32-126 (* i 2) (+ 32 i))
1241 (aset map-32-126 (1+ (* i 2)) (aref tbl-32-126 i)))
1242 (dotimes (i (1+ (- 254 160)))
1243 (aset map-160-254 (* i 2) (+ 160 i))
1244 (aset map-160-254 (1+ (* i 2)) (aref tbl-160-254 i)))
1245 (vconcat map-32-126 map-160-254)))
1246
1247 (define-charset 'mac-dingbats
1248 "Mac Dingbats"
1249 :short-name "Mac Dingbats"
1250 :code-space [32 254]
1251 :map
1252 (let ((tbl-32-126
1253 [?\ ?\✁ ?\✂ ?\✃ ?\✄ ?\☎ ?\✆ ?\✇ ?\✈ ?\✉ ?\☛ ?\☞ ?\✌ ?\✍ ?\✎ ?\✏
1254 ?\✐ ?\✑ ?\✒ ?\✓ ?\✔ ?\✕ ?\✖ ?\✗ ?\✘ ?\✙ ?\✚ ?\✛ ?\✜ ?\✝ ?\✞ ?\✟
1255 ?\✠ ?\✡ ?\✢ ?\✣ ?\✤ ?\✥ ?\✦ ?\✧ ?\★ ?\✩ ?\✪ ?\✫ ?\✬ ?\✭ ?\✮ ?\✯
1256 ?\✰ ?\✱ ?\✲ ?\✳ ?\✴ ?\✵ ?\✶ ?\✷ ?\✸ ?\✹ ?\✺ ?\✻ ?\✼ ?\✽ ?\✾ ?\✿
1257 ?\❀ ?\❁ ?\❂ ?\❃ ?\❄ ?\❅ ?\❆ ?\❇ ?\❈ ?\❉ ?\❊ ?\❋ ?\● ?\❍ ?\■ ?\❏
1258 ?\❐ ?\❑ ?\❒ ?\▲ ?\▼ ?\◆ ?\❖ ?\◗ ?\❘ ?\❙ ?\❚ ?\❛ ?\❜ ?\❝ ?\❞])
1259 (map-32-126 (make-vector (* (1+ (- 126 32)) 2) nil))
1260 (tbl-128-141
1261 [?\❨ ?\❩ ?\❪ ?\❫ ?\❬ ?\❭ ?\❮ ?\❯ ?\❰ ?\❱ ?\❲ ?\❳ ?\❴ ?\❵])
1262 (map-128-141 (make-vector (* (1+ (- 141 128)) 2) nil))
1263 (tbl-161-239
1264 [?\❡ ?\❢ ?\❣ ?\❤ ?\❥ ?\❦ ?\❧ ?\♣ ?\♦ ?\♥ ?\♠ ?\① ?\② ?\③ ?\④
1265 ?\⑤ ?\⑥ ?\⑦ ?\⑧ ?\⑨ ?\⑩ ?\❶ ?\❷ ?\❸ ?\❹ ?\❺ ?\❻ ?\❼ ?\❽ ?\❾ ?\❿
1266 ?\➀ ?\➁ ?\➂ ?\➃ ?\➄ ?\➅ ?\➆ ?\➇ ?\➈ ?\➉ ?\➊ ?\➋ ?\➌ ?\➍ ?\➎ ?\➏
1267 ?\➐ ?\➑ ?\➒ ?\➓ ?\➔ ?\→ ?\↔ ?\↕ ?\➘ ?\➙ ?\➚ ?\➛ ?\➜ ?\➝ ?\➞ ?\➟
1268 ?\➠ ?\➡ ?\➢ ?\➣ ?\➤ ?\➥ ?\➦ ?\➧ ?\➨ ?\➩ ?\➪ ?\➫ ?\➬ ?\➭ ?\➮ ?\➯])
1269 (map-161-239 (make-vector (* (1+ (- 239 161)) 2) nil))
1270 (tbl-241-254
1271 [?\➱ ?\➲ ?\➳ ?\➴ ?\➵ ?\➶ ?\➷ ?\➸ ?\➹ ?\➺ ?\➻ ?\➼ ?\➽ ?\➾])
1272 (map-241-254 (make-vector (* (1+ (- 254 241)) 2) nil)))
1273 (dotimes (i (1+ (- 126 32)))
1274 (aset map-32-126 (* i 2) (+ 32 i))
1275 (aset map-32-126 (1+ (* i 2)) (aref tbl-32-126 i)))
1276 (dotimes (i (1+ (- 141 128)))
1277 (aset map-128-141 (* i 2) (+ 128 i))
1278 (aset map-128-141 (1+ (* i 2)) (aref tbl-128-141 i)))
1279 (dotimes (i (1+ (- 239 161)))
1280 (aset map-161-239 (* i 2) (+ 161 i))
1281 (aset map-161-239 (1+ (* i 2)) (aref tbl-161-239 i)))
1282 (dotimes (i (1+ (- 254 241)))
1283 (aset map-241-254 (* i 2) (+ 241 i))
1284 (aset map-241-254 (1+ (* i 2)) (aref tbl-241-254 i)))
1285 (vconcat map-32-126 map-128-141 map-161-239 map-241-254)))
1286
1287 (defconst mac-system-coding-system
1288 (let ((base (or (cdr (assq mac-system-script-code
1289 mac-script-code-coding-systems))
1290 'mac-roman)))
1291 (if (eq system-type 'darwin)
1292 base
1293 (coding-system-change-eol-conversion base 'mac)))
1294 "Coding system derived from the system script code.")
1295
1296 (set-selection-coding-system mac-system-coding-system)
1297
1298 \f
1299 ;;;; Keyboard layout/language change events
1300 (defun mac-handle-language-change (event)
1301 "Set keyboard coding system to what is specified in EVENT."
1302 (interactive "e")
1303 (let ((coding-system
1304 (cdr (assq (car (cadr event)) mac-script-code-coding-systems))))
1305 (set-keyboard-coding-system (or coding-system 'mac-roman))
1306 ;; MacJapanese maps reverse solidus to ?\x80.
1307 (if (eq coding-system 'japanese-shift-jis)
1308 (define-key key-translation-map [?\x80] "\\"))))
1309
1310 (define-key special-event-map [language-change] 'mac-handle-language-change)
1311
1312 \f
1313 ;;;; Conversion between common flavors and Lisp string.
1314
1315 (defconst mac-text-encoding-ascii #x600
1316 "ASCII text encoding.")
1317
1318 (defconst mac-text-encoding-mac-japanese-basic-variant #x20001
1319 "MacJapanese text encoding without Apple double-byte extensions.")
1320
1321 (defun mac-utxt-to-string (data &optional coding-system)
1322 (or coding-system (setq coding-system mac-system-coding-system))
1323 (let* ((encoding
1324 (and (eq system-type 'darwin)
1325 (eq (coding-system-base coding-system) 'japanese-shift-jis)
1326 mac-text-encoding-mac-japanese-basic-variant))
1327 (str (and (fboundp 'mac-code-convert-string)
1328 (mac-code-convert-string data nil
1329 (or encoding coding-system)))))
1330 (when str
1331 (setq str (decode-coding-string str coding-system))
1332 (if (eq encoding mac-text-encoding-mac-japanese-basic-variant)
1333 ;; Does it contain Apple one-byte extensions other than
1334 ;; reverse solidus?
1335 (if (string-match "[\xa0\xfd-\xff]" str)
1336 (setq str nil)
1337 ;; ASCII-only?
1338 (unless (mac-code-convert-string data nil mac-text-encoding-ascii)
1339 (subst-char-in-string ?\x5c ?\¥ str t)
1340 (subst-char-in-string ?\x80 ?\\ str t)))))
1341 (or str
1342 (decode-coding-string data
1343 (if (eq (byteorder) ?B) 'utf-16be 'utf-16le)))))
1344
1345 (defun mac-string-to-utxt (string &optional coding-system)
1346 (or coding-system (setq coding-system mac-system-coding-system))
1347 (let (data encoding)
1348 (when (and (fboundp 'mac-code-convert-string)
1349 (memq (coding-system-base coding-system)
1350 (find-coding-systems-string string)))
1351 (setq coding-system
1352 (coding-system-change-eol-conversion coding-system 'mac))
1353 (let ((str string))
1354 (when (and (eq system-type 'darwin)
1355 (eq coding-system 'japanese-shift-jis-mac))
1356 (setq encoding mac-text-encoding-mac-japanese-basic-variant)
1357 (setq str (subst-char-in-string ?\\ ?\x80 str))
1358 (subst-char-in-string ?\¥ ?\x5c str t)
1359 ;; ASCII-only?
1360 (if (string-match "\\`[\x00-\x7f]*\\'" str)
1361 (setq str nil)))
1362 (and str
1363 (setq data (mac-code-convert-string
1364 (encode-coding-string str coding-system)
1365 (or encoding coding-system) nil)))))
1366 (or data (encode-coding-string string (if (eq (byteorder) ?B)
1367 'utf-16be-mac
1368 'utf-16le-mac)))))
1369
1370 (defun mac-TEXT-to-string (data &optional coding-system)
1371 (or coding-system (setq coding-system mac-system-coding-system))
1372 (prog1 (setq data (decode-coding-string data coding-system))
1373 (when (eq (coding-system-base coding-system) 'japanese-shift-jis)
1374 ;; (subst-char-in-string ?\x5c ?\¥ data t)
1375 (subst-char-in-string ?\x80 ?\\ data t))))
1376
1377 (defun mac-string-to-TEXT (string &optional coding-system)
1378 (or coding-system (setq coding-system mac-system-coding-system))
1379 (let ((encodables (find-coding-systems-string string))
1380 (rest mac-script-code-coding-systems))
1381 (unless (memq (coding-system-base coding-system) encodables)
1382 (while (and rest (not (memq (cdar rest) encodables)))
1383 (setq rest (cdr rest)))
1384 (if rest
1385 (setq coding-system (cdar rest)))))
1386 (setq coding-system
1387 (coding-system-change-eol-conversion coding-system 'mac))
1388 (when (eq coding-system 'japanese-shift-jis-mac)
1389 ;; (setq string (subst-char-in-string ?\\ ?\x80 string))
1390 (setq string (subst-char-in-string ?\¥ ?\x5c string)))
1391 (encode-coding-string string coding-system))
1392
1393 (defun mac-furl-to-string (data)
1394 ;; Remove a trailing nul character.
1395 (let ((len (length data)))
1396 (if (and (> len 0) (= (aref data (1- len)) ?\0))
1397 (substring data 0 (1- len))
1398 data)))
1399
1400 (defun mac-TIFF-to-string (data &optional text)
1401 (prog1 (or text (setq text (copy-sequence " ")))
1402 (put-text-property 0 (length text) 'display (create-image data 'tiff t)
1403 text)))
1404 \f
1405 ;;;; Selections
1406
1407 ;;; We keep track of the last text selected here, so we can check the
1408 ;;; current selection against it, and avoid passing back our own text
1409 ;;; from x-get-selection-value.
1410 (defvar x-last-selected-text-clipboard nil
1411 "The value of the CLIPBOARD selection last time we selected or
1412 pasted text.")
1413 (defvar x-last-selected-text-primary nil
1414 "The value of the PRIMARY X selection last time we selected or
1415 pasted text.")
1416
1417 (defcustom x-select-enable-clipboard t
1418 "*Non-nil means cutting and pasting uses the clipboard.
1419 This is in addition to the primary selection."
1420 :type 'boolean
1421 :group 'killing)
1422
1423 ;;; Make TEXT, a string, the primary X selection.
1424 (defun x-select-text (text &optional push)
1425 (x-set-selection 'PRIMARY text)
1426 (setq x-last-selected-text-primary text)
1427 (if (not x-select-enable-clipboard)
1428 (setq x-last-selected-text-clipboard nil)
1429 (x-set-selection 'CLIPBOARD text)
1430 (setq x-last-selected-text-clipboard text))
1431 )
1432
1433 (defun x-get-selection (&optional type data-type)
1434 "Return the value of a selection.
1435 The argument TYPE (default `PRIMARY') says which selection,
1436 and the argument DATA-TYPE (default `STRING') says
1437 how to convert the data.
1438
1439 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
1440 only a few symbols are commonly used. They conventionally have
1441 all upper-case names. The most often used ones, in addition to
1442 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
1443
1444 DATA-TYPE is usually `STRING', but can also be one of the symbols
1445 in `selection-converter-alist', which see."
1446 (let ((data (x-get-selection-internal (or type 'PRIMARY)
1447 (or data-type 'STRING)))
1448 (coding (or next-selection-coding-system
1449 selection-coding-system)))
1450 (when (and (stringp data)
1451 (setq data-type (get-text-property 0 'foreign-selection data)))
1452 (cond ((eq data-type 'public.utf16-plain-text)
1453 (setq data (mac-utxt-to-string data coding)))
1454 ((eq data-type 'com.apple.traditional-mac-plain-text)
1455 (setq data (mac-TEXT-to-string data coding)))
1456 ((eq data-type 'public.file-url)
1457 (setq data (mac-furl-to-string data))))
1458 (put-text-property 0 (length data) 'foreign-selection data-type data))
1459 data))
1460
1461 (defun x-selection-value (type)
1462 (let ((data-types '(public.utf16-plain-text
1463 com.apple.traditional-mac-plain-text
1464 public.file-url))
1465 text tiff-image)
1466 (while (and (null text) data-types)
1467 (setq text (condition-case nil
1468 (x-get-selection type (car data-types))
1469 (error nil)))
1470 (setq data-types (cdr data-types)))
1471 (if text
1472 (remove-text-properties 0 (length text) '(foreign-selection nil) text))
1473 (setq tiff-image (condition-case nil
1474 (x-get-selection type 'public.tiff)
1475 (error nil)))
1476 (when tiff-image
1477 (remove-text-properties 0 (length tiff-image)
1478 '(foreign-selection nil) tiff-image)
1479 (setq text (mac-TIFF-to-string tiff-image text)))
1480 text))
1481
1482 ;;; Return the value of the current selection.
1483 ;;; Treat empty strings as if they were unset.
1484 ;;; If this function is called twice and finds the same text,
1485 ;;; it returns nil the second time. This is so that a single
1486 ;;; selection won't be added to the kill ring over and over.
1487 (defun x-get-selection-value ()
1488 (let (clip-text primary-text)
1489 (if (not x-select-enable-clipboard)
1490 (setq x-last-selected-text-clipboard nil)
1491 (setq clip-text (x-selection-value 'CLIPBOARD))
1492 (if (string= clip-text "") (setq clip-text nil))
1493
1494 ;; Check the CLIPBOARD selection for 'newness', is it different
1495 ;; from what we remebered them to be last time we did a
1496 ;; cut/paste operation.
1497 (setq clip-text
1498 (cond;; check clipboard
1499 ((or (not clip-text) (string= clip-text ""))
1500 (setq x-last-selected-text-clipboard nil))
1501 ((eq clip-text x-last-selected-text-clipboard) nil)
1502 ((string= clip-text x-last-selected-text-clipboard)
1503 ;; Record the newer string,
1504 ;; so subsequent calls can use the `eq' test.
1505 (setq x-last-selected-text-clipboard clip-text)
1506 nil)
1507 (t
1508 (setq x-last-selected-text-clipboard clip-text))))
1509 )
1510
1511 (setq primary-text (x-selection-value 'PRIMARY))
1512 ;; Check the PRIMARY selection for 'newness', is it different
1513 ;; from what we remebered them to be last time we did a
1514 ;; cut/paste operation.
1515 (setq primary-text
1516 (cond;; check primary selection
1517 ((or (not primary-text) (string= primary-text ""))
1518 (setq x-last-selected-text-primary nil))
1519 ((eq primary-text x-last-selected-text-primary) nil)
1520 ((string= primary-text x-last-selected-text-primary)
1521 ;; Record the newer string,
1522 ;; so subsequent calls can use the `eq' test.
1523 (setq x-last-selected-text-primary primary-text)
1524 nil)
1525 (t
1526 (setq x-last-selected-text-primary primary-text))))
1527
1528 ;; As we have done one selection, clear this now.
1529 (setq next-selection-coding-system nil)
1530
1531 ;; At this point we have recorded the current values for the
1532 ;; selection from clipboard (if we are supposed to) and primary,
1533 ;; So return the first one that has changed (which is the first
1534 ;; non-null one).
1535 (or clip-text primary-text)
1536 ))
1537
1538 (put 'CLIPBOARD 'mac-scrap-name "com.apple.scrap.clipboard")
1539 (when (eq system-type 'darwin)
1540 (put 'FIND 'mac-scrap-name "com.apple.scrap.find")
1541 (put 'PRIMARY 'mac-scrap-name
1542 (format "org.gnu.Emacs.%d.selection.PRIMARY" (emacs-pid))))
1543 (put 'com.apple.traditional-mac-plain-text 'mac-ostype "TEXT")
1544 (put 'public.utf16-plain-text 'mac-ostype "utxt")
1545 (put 'public.tiff 'mac-ostype "TIFF")
1546 (put 'public.file-url 'mac-ostype "furl")
1547
1548 (defun mac-select-convert-to-string (selection type value)
1549 (let ((str (cdr (xselect-convert-to-string selection nil value)))
1550 (coding (or next-selection-coding-system selection-coding-system)))
1551 (when str
1552 ;; If TYPE is nil, this is a local request, thus return STR as
1553 ;; is. Otherwise, encode STR.
1554 (if (not type)
1555 str
1556 (let ((inhibit-read-only t))
1557 (remove-text-properties 0 (length str) '(composition nil) str)
1558 (cond
1559 ((eq type 'public.utf16-plain-text)
1560 (setq str (mac-string-to-utxt str coding)))
1561 ((eq type 'com.apple.traditional-mac-plain-text)
1562 (setq str (mac-string-to-TEXT str coding)))
1563 (t
1564 (error "Unknown selection type: %S" type))
1565 )))
1566
1567 (setq next-selection-coding-system nil)
1568 (cons type str))))
1569
1570 (defun mac-select-convert-to-file-url (selection type value)
1571 (let ((filename (xselect-convert-to-filename selection type value))
1572 (coding (or file-name-coding-system default-file-name-coding-system)))
1573 (if (and filename coding)
1574 (setq filename (encode-coding-string filename coding)))
1575 (and filename
1576 (concat "file://localhost"
1577 (mapconcat 'url-hexify-string
1578 (split-string filename "/") "/")))))
1579
1580 (setq selection-converter-alist
1581 (nconc
1582 '((public.utf16-plain-text . mac-select-convert-to-string)
1583 (com.apple.traditional-mac-plain-text . mac-select-convert-to-string)
1584 ;; This is not enabled by default because the `Import Image'
1585 ;; menu makes Emacs crash or hang for unknown reasons.
1586 ;; (public.tiff . nil)
1587 (public.file-url . mac-select-convert-to-file-url)
1588 )
1589 selection-converter-alist))
1590 \f
1591 ;;;; Apple events, HICommand events, and Services menu
1592
1593 ;;; Event classes
1594 (put 'core-event 'mac-apple-event-class "aevt") ; kCoreEventClass
1595 (put 'internet-event 'mac-apple-event-class "GURL") ; kAEInternetEventClass
1596
1597 ;;; Event IDs
1598 ;; kCoreEventClass
1599 (put 'open-application 'mac-apple-event-id "oapp") ; kAEOpenApplication
1600 (put 'reopen-application 'mac-apple-event-id "rapp") ; kAEReopenApplication
1601 (put 'open-documents 'mac-apple-event-id "odoc") ; kAEOpenDocuments
1602 (put 'print-documents 'mac-apple-event-id "pdoc") ; kAEPrintDocuments
1603 (put 'open-contents 'mac-apple-event-id "ocon") ; kAEOpenContents
1604 (put 'quit-application 'mac-apple-event-id "quit") ; kAEQuitApplication
1605 (put 'application-died 'mac-apple-event-id "obit") ; kAEApplicationDied
1606 (put 'show-preferences 'mac-apple-event-id "pref") ; kAEShowPreferences
1607 (put 'autosave-now 'mac-apple-event-id "asav") ; kAEAutosaveNow
1608 ;; kAEInternetEventClass
1609 (put 'get-url 'mac-apple-event-id "GURL") ; kAEGetURL
1610 ;; Converted HI command events
1611 (put 'about 'mac-apple-event-id "abou") ; kHICommandAbout
1612 (put 'show-hide-font-panel 'mac-apple-event-id "shfp") ; kHICommandShowHideFontPanel
1613
1614 (defmacro mac-event-spec (event)
1615 `(nth 1 ,event))
1616
1617 (defmacro mac-event-ae (event)
1618 `(nth 2 ,event))
1619
1620 (defun mac-ae-parameter (ae &optional keyword type)
1621 (or keyword (setq keyword "----")) ;; Direct object.
1622 (if (not (and (consp ae) (equal (car ae) "aevt")))
1623 (error "Not an Apple event: %S" ae)
1624 (let ((type-data (cdr (assoc keyword (cdr ae))))
1625 data)
1626 (when (and type type-data (not (equal type (car type-data))))
1627 (setq data (mac-coerce-ae-data (car type-data) (cdr type-data) type))
1628 (setq type-data (if data (cons type data) nil)))
1629 type-data)))
1630
1631 (defun mac-ae-list (ae &optional keyword type)
1632 (or keyword (setq keyword "----")) ;; Direct object.
1633 (let ((desc (mac-ae-parameter ae keyword "list")))
1634 (cond ((null desc)
1635 nil)
1636 ((not (equal (car desc) "list"))
1637 (error "Parameter for \"%s\" is not a list" keyword))
1638 (t
1639 (if (null type)
1640 (cdr desc)
1641 (mapcar
1642 (lambda (type-data)
1643 (mac-coerce-ae-data (car type-data) (cdr type-data) type))
1644 (cdr desc)))))))
1645
1646 (defun mac-ae-number (ae keyword)
1647 (let ((type-data (mac-ae-parameter ae keyword))
1648 str)
1649 (if (and type-data
1650 (setq str (mac-coerce-ae-data (car type-data)
1651 (cdr type-data) "TEXT")))
1652 (let ((num (string-to-number str)))
1653 ;; Mac OS Classic may return "0e+0" as the coerced value for
1654 ;; the type "magn" and the data "\000\000\000\000".
1655 (if (= num 0.0) 0 num))
1656 nil)))
1657
1658 (defun mac-bytes-to-integer (bytes &optional from to)
1659 (or from (setq from 0))
1660 (or to (setq to (length bytes)))
1661 (let* ((len (- to from))
1662 (extended-sign-len (- (1+ (ceiling (log most-positive-fixnum 2)))
1663 (* 8 len)))
1664 (result 0))
1665 (dotimes (i len)
1666 (setq result (logior (lsh result 8)
1667 (aref bytes (+ from (if (eq (byteorder) ?B) i
1668 (- len i 1)))))))
1669 (if (> extended-sign-len 0)
1670 (ash (lsh result extended-sign-len) (- extended-sign-len))
1671 result)))
1672
1673 (defun mac-ae-selection-range (ae)
1674 ;; #pragma options align=mac68k
1675 ;; typedef struct SelectionRange {
1676 ;; short unused1; // 0 (not used)
1677 ;; short lineNum; // line to select (<0 to specify range)
1678 ;; long startRange; // start of selection range (if line < 0)
1679 ;; long endRange; // end of selection range (if line < 0)
1680 ;; long unused2; // 0 (not used)
1681 ;; long theDate; // modification date/time
1682 ;; } SelectionRange;
1683 ;; #pragma options align=reset
1684 (let ((range-bytes (cdr (mac-ae-parameter ae "kpos" "TEXT"))))
1685 (and range-bytes
1686 (list (mac-bytes-to-integer range-bytes 2 4)
1687 (mac-bytes-to-integer range-bytes 4 8)
1688 (mac-bytes-to-integer range-bytes 8 12)
1689 (mac-bytes-to-integer range-bytes 16 20)))))
1690
1691 ;; On Mac OS X 10.4 and later, the `open-document' event contains an
1692 ;; optional parameter keyAESearchText from the Spotlight search.
1693 (defun mac-ae-text-for-search (ae)
1694 (let ((utf8-text (cdr (mac-ae-parameter ae "stxt" "utf8"))))
1695 (and utf8-text
1696 (decode-coding-string utf8-text 'utf-8))))
1697
1698 (defun mac-ae-text (ae)
1699 (or (cdr (mac-ae-parameter ae nil "TEXT"))
1700 (error "No text in Apple event.")))
1701
1702 (defun mac-ae-frame (ae &optional keyword type)
1703 (let ((bytes (cdr (mac-ae-parameter ae keyword type))))
1704 (if (or (null bytes) (/= (length bytes) 4))
1705 (error "No window reference in Apple event.")
1706 (let ((window-id (mac-coerce-ae-data "long" bytes "TEXT"))
1707 (rest (frame-list))
1708 frame)
1709 (while (and (null frame) rest)
1710 (if (string= (frame-parameter (car rest) 'window-id) window-id)
1711 (setq frame (car rest)))
1712 (setq rest (cdr rest)))
1713 frame))))
1714
1715 (defun mac-ae-script-language (ae keyword)
1716 ;; struct WritingCode {
1717 ;; ScriptCode theScriptCode;
1718 ;; LangCode theLangCode;
1719 ;; };
1720 (let ((bytes (cdr (mac-ae-parameter ae keyword "intl"))))
1721 (and bytes
1722 (cons (mac-bytes-to-integer bytes 0 2)
1723 (mac-bytes-to-integer bytes 2 4)))))
1724
1725 (defun mac-bytes-to-text-range (bytes &optional from to)
1726 ;; struct TextRange {
1727 ;; long fStart;
1728 ;; long fEnd;
1729 ;; short fHiliteStyle;
1730 ;; };
1731 (or from (setq from 0))
1732 (or to (setq to (length bytes)))
1733 (and (= (- to from) (+ 4 4 2))
1734 (list (mac-bytes-to-integer bytes from (+ from 4))
1735 (mac-bytes-to-integer bytes (+ from 4) (+ from 8))
1736 (mac-bytes-to-integer bytes (+ from 8) to))))
1737
1738 (defun mac-ae-text-range-array (ae keyword)
1739 ;; struct TextRangeArray {
1740 ;; short fNumOfRanges;
1741 ;; TextRange fRange[1];
1742 ;; };
1743 (let* ((bytes (cdr (mac-ae-parameter ae keyword "tray")))
1744 (len (length bytes))
1745 nranges result)
1746 (when (and bytes (>= len 2)
1747 (progn
1748 (setq nranges (mac-bytes-to-integer bytes 0 2))
1749 (= len (+ 2 (* nranges 10)))))
1750 (setq result (make-vector nranges nil))
1751 (dotimes (i nranges)
1752 (aset result i
1753 (mac-bytes-to-text-range bytes (+ (* i 10) 2)
1754 (+ (* i 10) 12)))))
1755 result))
1756
1757 (defconst mac-keyboard-modifier-mask-alist
1758 (mapcar
1759 (lambda (modifier-bit)
1760 (cons (car modifier-bit) (lsh 1 (cdr modifier-bit))))
1761 '((command . 8) ; cmdKeyBit
1762 (shift . 9) ; shiftKeyBit
1763 (option . 11) ; optionKeyBit
1764 (control . 12) ; controlKeyBit
1765 (function . 17))) ; kEventKeyModifierFnBit
1766 "Alist of Mac keyboard modifier symbols vs masks.")
1767
1768 (defun mac-ae-keyboard-modifiers (ae)
1769 (let ((modifiers-value (mac-ae-number ae "kmod"))
1770 modifiers)
1771 (if modifiers-value
1772 (dolist (modifier-mask mac-keyboard-modifier-mask-alist)
1773 (if (/= (logand modifiers-value (cdr modifier-mask)) 0)
1774 (setq modifiers (cons (car modifier-mask) modifiers)))))
1775 modifiers))
1776
1777 (defun mac-ae-reopen-application (event)
1778 "Show some frame in response to the Apple event EVENT.
1779 The frame to be shown is chosen from visible or iconified frames
1780 if possible. If there's no such frame, a new frame is created."
1781 (interactive "e")
1782 (unless (frame-visible-p (selected-frame))
1783 (let ((frame (or (car (visible-frame-list))
1784 (car (filtered-frame-list 'frame-visible-p)))))
1785 (if frame
1786 (select-frame frame)
1787 (switch-to-buffer-other-frame "*scratch*"))))
1788 (select-frame-set-input-focus (selected-frame)))
1789
1790 (defun mac-ae-open-documents (event)
1791 "Open the documents specified by the Apple event EVENT."
1792 (interactive "e")
1793 (let ((ae (mac-event-ae event)))
1794 (dolist (file-name (mac-ae-list ae nil 'undecoded-file-name))
1795 (if file-name
1796 (dnd-open-local-file
1797 (concat "file://"
1798 (mapconcat 'url-hexify-string
1799 (split-string file-name "/") "/")) nil)))
1800 (let ((selection-range (mac-ae-selection-range ae))
1801 (search-text (mac-ae-text-for-search ae)))
1802 (cond (selection-range
1803 (let ((line (car selection-range))
1804 (start (cadr selection-range))
1805 (end (nth 2 selection-range)))
1806 (if (>= line 0)
1807 (goto-line (1+ line))
1808 (if (and (>= start 0) (>= end 0))
1809 (progn (set-mark (1+ start))
1810 (goto-char (1+ end)))))))
1811 ((stringp search-text)
1812 (re-search-forward
1813 (mapconcat 'regexp-quote (split-string search-text) "\\|")
1814 nil t)))))
1815 (select-frame-set-input-focus (selected-frame)))
1816
1817 (defun mac-ae-quit-application (event)
1818 "Quit the application Emacs with the Apple event EVENT."
1819 (interactive "e")
1820 (let ((ae (mac-event-ae event)))
1821 (unwind-protect
1822 (save-buffers-kill-emacs)
1823 ;; Reaches here if the user has canceled the quit.
1824 (mac-resume-apple-event ae -128)))) ; userCanceledErr
1825
1826 ;; url-generic-parse-url is autoloaded from url-parse.
1827 (declare-function url-type "url-parse" t t) ; defstruct
1828
1829 (defun mac-ae-get-url (event)
1830 "Open the URL specified by the Apple event EVENT.
1831 Currently the `mailto' scheme is supported."
1832 (interactive "e")
1833 (let* ((ae (mac-event-ae event))
1834 (parsed-url (url-generic-parse-url (mac-ae-text ae))))
1835 (if (string= (url-type parsed-url) "mailto")
1836 (progn
1837 (url-mailto parsed-url)
1838 (select-frame-set-input-focus (selected-frame)))
1839 (mac-resume-apple-event ae t))))
1840
1841 (setq mac-apple-event-map (make-sparse-keymap))
1842
1843 ;; Received when Emacs is launched without associated documents.
1844 ;; Accept it as an Apple event, but no Emacs event is generated so as
1845 ;; not to erase the splash screen.
1846 (define-key mac-apple-event-map [core-event open-application] 0)
1847
1848 ;; Received when a dock or application icon is clicked and Emacs is
1849 ;; already running.
1850 (define-key mac-apple-event-map [core-event reopen-application]
1851 'mac-ae-reopen-application)
1852
1853 (define-key mac-apple-event-map [core-event open-documents]
1854 'mac-ae-open-documents)
1855 (define-key mac-apple-event-map [core-event show-preferences] 'customize)
1856 (define-key mac-apple-event-map [core-event quit-application]
1857 'mac-ae-quit-application)
1858
1859 (define-key mac-apple-event-map [internet-event get-url] 'mac-ae-get-url)
1860
1861 (define-key mac-apple-event-map [hi-command about] 'about-emacs)
1862
1863 ;;; Converted Carbon Events
1864 (defun mac-handle-toolbar-switch-mode (event)
1865 "Toggle visibility of tool-bars in response to EVENT.
1866 With no keyboard modifiers, it toggles the visibility of the
1867 frame where the tool-bar toggle button was pressed. With some
1868 modifiers, it changes the global tool-bar visibility setting."
1869 (interactive "e")
1870 (let ((ae (mac-event-ae event)))
1871 (if (mac-ae-keyboard-modifiers ae)
1872 ;; Globally toggle tool-bar-mode if some modifier key is pressed.
1873 (tool-bar-mode 'toggle)
1874 (let ((frame (mac-ae-frame ae)))
1875 (set-frame-parameter frame 'tool-bar-lines
1876 (if (= (frame-parameter frame 'tool-bar-lines) 0)
1877 1 0))))))
1878
1879 ;; kEventClassWindow/kEventWindowToolbarSwitchMode
1880 (define-key mac-apple-event-map [window toolbar-switch-mode]
1881 'mac-handle-toolbar-switch-mode)
1882
1883 ;;; Font panel
1884 (when (fboundp 'mac-set-font-panel-visible-p)
1885
1886 (define-minor-mode mac-font-panel-mode
1887 "Toggle use of the font panel.
1888 With numeric ARG, display the font panel if and only if ARG is positive."
1889 :init-value nil
1890 :global t
1891 :group 'mac
1892 (mac-set-font-panel-visible-p mac-font-panel-mode))
1893
1894 (defun mac-handle-font-panel-closed (event)
1895 "Update internal status in response to font panel closed EVENT."
1896 (interactive "e")
1897 ;; Synchronize with the minor mode variable.
1898 (mac-font-panel-mode 0))
1899
1900 (defun mac-handle-font-selection (event)
1901 "Change default face attributes according to font selection EVENT."
1902 (interactive "e")
1903 (let* ((ae (mac-event-ae event))
1904 (fm-font-size (mac-ae-number ae "fmsz"))
1905 (atsu-font-id (mac-ae-number ae "auid"))
1906 (attribute-values (and atsu-font-id
1907 (mac-atsu-font-face-attributes atsu-font-id))))
1908 (if fm-font-size
1909 (setq attribute-values
1910 `(:height ,(* 10 fm-font-size) ,@attribute-values)))
1911 (apply 'set-face-attribute 'default (selected-frame) attribute-values)))
1912
1913 ;; kEventClassFont/kEventFontPanelClosed
1914 (define-key mac-apple-event-map [font panel-closed]
1915 'mac-handle-font-panel-closed)
1916 ;; kEventClassFont/kEventFontSelection
1917 (define-key mac-apple-event-map [font selection] 'mac-handle-font-selection)
1918 (define-key mac-apple-event-map [hi-command show-hide-font-panel]
1919 'mac-font-panel-mode)
1920
1921 (define-key-after menu-bar-showhide-menu [mac-font-panel-mode]
1922 (menu-bar-make-mm-toggle mac-font-panel-mode
1923 "Font Panel"
1924 "Show the font panel as a floating dialog")
1925 'showhide-speedbar)
1926
1927 ) ;; (fboundp 'mac-set-font-panel-visible-p)
1928
1929 ;;; Text Services
1930 (defvar mac-ts-update-active-input-area-seqno 0
1931 "Number of processed update-active-input-area events.")
1932 (setq mac-ts-active-input-overlay (make-overlay 0 0))
1933
1934 (defface mac-ts-caret-position
1935 '((t :inverse-video t))
1936 "Face for caret position in Mac TSM active input area.
1937 This is used when the active input area is displayed either in
1938 the echo area or in a buffer where the cursor is not displayed."
1939 :group 'mac)
1940
1941 (defface mac-ts-raw-text
1942 '((t :underline t))
1943 "Face for raw text in Mac TSM active input area."
1944 :group 'mac)
1945
1946 (defface mac-ts-selected-raw-text
1947 '((t :underline t))
1948 "Face for selected raw text in Mac TSM active input area."
1949 :group 'mac)
1950
1951 (defface mac-ts-converted-text
1952 '((((background dark)) :underline "gray20")
1953 (t :underline "gray80"))
1954 "Face for converted text in Mac TSM active input area."
1955 :group 'mac)
1956
1957 (defface mac-ts-selected-converted-text
1958 '((t :underline t))
1959 "Face for selected converted text in Mac TSM active input area."
1960 :group 'mac)
1961
1962 (defface mac-ts-block-fill-text
1963 '((t :underline t))
1964 "Face for block fill text in Mac TSM active input area."
1965 :group 'mac)
1966
1967 (defface mac-ts-outline-text
1968 '((t :underline t))
1969 "Face for outline text in Mac TSM active input area."
1970 :group 'mac)
1971
1972 (defface mac-ts-selected-text
1973 '((t :underline t))
1974 "Face for selected text in Mac TSM active input area."
1975 :group 'mac)
1976
1977 (defface mac-ts-no-hilite
1978 '((t :inherit default))
1979 "Face for no hilite in Mac TSM active input area."
1980 :group 'mac)
1981
1982 (defconst mac-ts-hilite-style-faces
1983 '((2 . mac-ts-raw-text) ; kTSMHiliteRawText
1984 (3 . mac-ts-selected-raw-text) ; kTSMHiliteSelectedRawText
1985 (4 . mac-ts-converted-text) ; kTSMHiliteConvertedText
1986 (5 . mac-ts-selected-converted-text) ; kTSMHiliteSelectedConvertedText
1987 (6 . mac-ts-block-fill-text) ; kTSMHiliteBlockFillText
1988 (7 . mac-ts-outline-text) ; kTSMHiliteOutlineText
1989 (8 . mac-ts-selected-text) ; kTSMHiliteSelectedText
1990 (9 . mac-ts-no-hilite)) ; kTSMHiliteNoHilite
1991 "Alist of Mac TSM hilite style vs Emacs face.")
1992
1993 (defun mac-ts-update-active-input-buf (text fix-len hilite-rng update-rng)
1994 (let ((buf-len (length mac-ts-active-input-buf))
1995 confirmed)
1996 (if (or (null update-rng)
1997 (/= (% (length update-rng) 2) 0))
1998 ;; The parameter is missing (or in a bad format). The
1999 ;; existing inline input session is completely replaced with
2000 ;; the new text.
2001 (setq mac-ts-active-input-buf text)
2002 ;; Otherwise, the current subtext specified by the (2*j)-th
2003 ;; range is replaced with the new subtext specified by the
2004 ;; (2*j+1)-th range.
2005 (let ((tail buf-len)
2006 (i (length update-rng))
2007 segments rng)
2008 (while (> i 0)
2009 (setq i (- i 2))
2010 (setq rng (aref update-rng i))
2011 (if (and (<= 0 (cadr rng)) (< (cadr rng) tail)
2012 (<= tail buf-len))
2013 (setq segments
2014 (cons (substring mac-ts-active-input-buf (cadr rng) tail)
2015 segments)))
2016 (setq tail (car rng))
2017 (setq rng (aref update-rng (1+ i)))
2018 (if (and (<= 0 (car rng)) (< (car rng) (cadr rng))
2019 (<= (cadr rng) (length text)))
2020 (setq segments
2021 (cons (substring text (car rng) (cadr rng))
2022 segments))))
2023 (if (and (< 0 tail) (<= tail buf-len))
2024 (setq segments
2025 (cons (substring mac-ts-active-input-buf 0 tail)
2026 segments)))
2027 (setq mac-ts-active-input-buf (apply 'concat segments))))
2028 (setq buf-len (length mac-ts-active-input-buf))
2029 ;; Confirm (a part of) inline input session.
2030 (cond ((< fix-len 0)
2031 ;; Entire inline session is being confirmed.
2032 (setq confirmed mac-ts-active-input-buf)
2033 (setq mac-ts-active-input-buf ""))
2034 ((= fix-len 0)
2035 ;; None of the text is being confirmed (yet).
2036 (setq confirmed ""))
2037 (t
2038 (if (> fix-len buf-len)
2039 (setq fix-len buf-len))
2040 (setq confirmed (substring mac-ts-active-input-buf 0 fix-len))
2041 (setq mac-ts-active-input-buf
2042 (substring mac-ts-active-input-buf fix-len))))
2043 (setq buf-len (length mac-ts-active-input-buf))
2044 ;; Update highlighting and the caret position in the new inline
2045 ;; input session.
2046 (remove-text-properties 0 buf-len '(cursor nil) mac-ts-active-input-buf)
2047 (mapc (lambda (rng)
2048 (cond ((and (= (nth 2 rng) 1) ; kTSMHiliteCaretPosition
2049 (<= 0 (car rng)) (< (car rng) buf-len))
2050 (put-text-property (car rng) buf-len
2051 'cursor t mac-ts-active-input-buf))
2052 ((and (<= 0 (car rng)) (< (car rng) (cadr rng))
2053 (<= (cadr rng) buf-len))
2054 (put-text-property (car rng) (cadr rng) 'face
2055 (cdr (assq (nth 2 rng)
2056 mac-ts-hilite-style-faces))
2057 mac-ts-active-input-buf))))
2058 hilite-rng)
2059 confirmed))
2060
2061 (defun mac-split-string-by-property-change (string)
2062 (let ((tail (length string))
2063 head result)
2064 (unless (= tail 0)
2065 (while (setq head (previous-property-change tail string)
2066 result (cons (substring string (or head 0) tail) result)
2067 tail head)))
2068 result))
2069
2070 (defun mac-replace-untranslated-utf-8-chars (string &optional to-string)
2071 (or to-string (setq to-string "\e$,3u=\e(B"))
2072 (mapconcat
2073 (lambda (str)
2074 (if (get-text-property 0 'untranslated-utf-8 str) to-string str))
2075 (mac-split-string-by-property-change string)
2076 ""))
2077
2078 (defun mac-keyboard-translate-char (ch)
2079 (if (and (characterp ch)
2080 (or (char-table-p keyboard-translate-table)
2081 (and (or (stringp keyboard-translate-table)
2082 (vectorp keyboard-translate-table))
2083 (> (length keyboard-translate-table) ch))))
2084 (or (aref keyboard-translate-table ch) ch)
2085 ch))
2086
2087 (defun mac-unread-string (string)
2088 ;; Unread characters and insert them in a keyboard macro being
2089 ;; defined.
2090 (apply 'isearch-unread
2091 (mapcar 'mac-keyboard-translate-char
2092 (mac-replace-untranslated-utf-8-chars string))))
2093
2094 (defun mac-ts-update-active-input-area (event)
2095 "Update Mac TSM active input area according to EVENT.
2096 The confirmed text is converted to Emacs input events and pushed
2097 into `unread-command-events'. The unconfirmed text is displayed
2098 either in the current buffer or in the echo area."
2099 (interactive "e")
2100 (let* ((ae (mac-event-ae event))
2101 (type-text (mac-ae-parameter ae "tstx"))
2102 (text (or (cdr type-text) ""))
2103 (decode-fun (if (equal (car type-text) "TEXT")
2104 'mac-TEXT-to-string 'mac-utxt-to-string))
2105 (script-language (mac-ae-script-language ae "tssl"))
2106 (coding (or (cdr (assq (car script-language)
2107 mac-script-code-coding-systems))
2108 'mac-roman))
2109 (fix-len (mac-ae-number ae "tsfx"))
2110 ;; Optional parameters
2111 (hilite-rng (mac-ae-text-range-array ae "tshi"))
2112 (update-rng (mac-ae-text-range-array ae "tsup"))
2113 ;;(pin-rng (mac-bytes-to-text-range (cdr (mac-ae-parameter ae "tspn" "txrn"))))
2114 ;;(clause-offsets (cdr (mac-ae-parameter ae "tscl" "ofay")))
2115 (seqno (mac-ae-number ae "tsSn"))
2116 confirmed)
2117 (unless (= seqno mac-ts-update-active-input-area-seqno)
2118 ;; Reset internal states if sequence number is out of sync.
2119 (setq mac-ts-active-input-buf ""))
2120 (setq confirmed
2121 (mac-ts-update-active-input-buf text fix-len hilite-rng update-rng))
2122 (let ((use-echo-area
2123 (or isearch-mode
2124 (and cursor-in-echo-area (current-message))
2125 ;; Overlay strings are not shown in some cases.
2126 (get-char-property (point) 'invisible)
2127 (and (not (bobp))
2128 (or (and (get-char-property (point) 'display)
2129 (eq (get-char-property (1- (point)) 'display)
2130 (get-char-property (point) 'display)))
2131 (and (get-char-property (point) 'composition)
2132 (eq (get-char-property (1- (point)) 'composition)
2133 (get-char-property (point) 'composition)))))))
2134 active-input-string caret-seen)
2135 ;; Decode the active input area text with inheriting faces and
2136 ;; the caret position.
2137 (setq active-input-string
2138 (mapconcat
2139 (lambda (str)
2140 (let ((decoded (funcall decode-fun str coding)))
2141 (put-text-property 0 (length decoded) 'face
2142 (get-text-property 0 'face str) decoded)
2143 (when (and (not caret-seen)
2144 (get-text-property 0 'cursor str))
2145 (setq caret-seen t)
2146 (if (or use-echo-area (null cursor-type))
2147 (put-text-property 0 1 'face 'mac-ts-caret-position
2148 decoded)
2149 (put-text-property 0 1 'cursor t decoded)))
2150 decoded))
2151 (mac-split-string-by-property-change mac-ts-active-input-buf)
2152 ""))
2153 (put-text-property 0 (length active-input-string)
2154 'mac-ts-active-input-string t active-input-string)
2155 (if use-echo-area
2156 (let ((msg (current-message))
2157 message-log-max)
2158 (if (and msg
2159 ;; Don't get confused by previously displayed
2160 ;; `active-input-string'.
2161 (null (get-text-property 0 'mac-ts-active-input-string
2162 msg)))
2163 (setq msg (propertize msg 'display
2164 (concat msg active-input-string)))
2165 (setq msg active-input-string))
2166 (message "%s" msg)
2167 (overlay-put mac-ts-active-input-overlay 'before-string nil))
2168 (move-overlay mac-ts-active-input-overlay
2169 (point) (point) (current-buffer))
2170 (overlay-put mac-ts-active-input-overlay 'before-string
2171 active-input-string))
2172 (mac-unread-string (funcall decode-fun confirmed coding)))
2173 ;; The event is successfully processed. Sync the sequence number.
2174 (setq mac-ts-update-active-input-area-seqno (1+ seqno))))
2175
2176 (defun mac-ts-unicode-for-key-event (event)
2177 "Convert Unicode key EVENT to Emacs key events and unread them."
2178 (interactive "e")
2179 (let* ((ae (mac-event-ae event))
2180 (text (cdr (mac-ae-parameter ae "tstx" "utxt")))
2181 (script-language (mac-ae-script-language ae "tssl"))
2182 (coding (or (cdr (assq (car script-language)
2183 mac-script-code-coding-systems))
2184 'mac-roman)))
2185 (if text
2186 (mac-unread-string (mac-utxt-to-string text coding)))))
2187
2188 ;; kEventClassTextInput/kEventTextInputUpdateActiveInputArea
2189 (define-key mac-apple-event-map [text-input update-active-input-area]
2190 'mac-ts-update-active-input-area)
2191 ;; kEventClassTextInput/kEventTextInputUnicodeForKeyEvent
2192 (define-key mac-apple-event-map [text-input unicode-for-key-event]
2193 'mac-ts-unicode-for-key-event)
2194
2195 ;;; Services
2196 (defun mac-service-open-file ()
2197 "Open the file specified by the selection value for Services."
2198 (interactive)
2199 ;; The selection seems not to contain the file name as
2200 ;; public.utf16-plain-text data on Mac OS X 10.4.
2201 (dnd-open-file (x-get-selection mac-service-selection 'public.file-url) nil))
2202
2203 (defun mac-service-open-selection ()
2204 "Create a new buffer containing the selection value for Services."
2205 (interactive)
2206 (switch-to-buffer (generate-new-buffer "*untitled*"))
2207 (insert (x-selection-value mac-service-selection))
2208 (sit-for 0)
2209 (save-buffer) ; It pops up the save dialog.
2210 )
2211
2212 (defun mac-service-mail-selection ()
2213 "Prepare a mail buffer containing the selection value for Services."
2214 (interactive)
2215 (compose-mail)
2216 (rfc822-goto-eoh)
2217 (forward-line 1)
2218 (insert (x-selection-value mac-service-selection) "\n"))
2219
2220 (defun mac-service-mail-to ()
2221 "Prepare a mail buffer to be sent to the selection value for Services."
2222 (interactive)
2223 (compose-mail (x-selection-value mac-service-selection)))
2224
2225 (defun mac-service-insert-text ()
2226 "Insert the selection value for Services."
2227 (interactive)
2228 (let ((text (x-selection-value mac-service-selection)))
2229 (if (not buffer-read-only)
2230 (insert text)
2231 (kill-new text)
2232 (message "%s"
2233 (substitute-command-keys
2234 "The text from the Services menu can be accessed with \\[yank]")))))
2235
2236 ;; kEventClassService/kEventServicePaste
2237 (define-key mac-apple-event-map [service paste] 'mac-service-insert-text)
2238 ;; kEventClassService/kEventServicePerform
2239 (define-key mac-apple-event-map [service perform open-file]
2240 'mac-service-open-file)
2241 (define-key mac-apple-event-map [service perform open-selection]
2242 'mac-service-open-selection)
2243 (define-key mac-apple-event-map [service perform mail-selection]
2244 'mac-service-mail-selection)
2245 (define-key mac-apple-event-map [service perform mail-to]
2246 'mac-service-mail-to)
2247
2248 (defun mac-dispatch-apple-event (event)
2249 "Dispatch EVENT according to the keymap `mac-apple-event-map'."
2250 (interactive "e")
2251 (let* ((binding (lookup-key mac-apple-event-map (mac-event-spec event)))
2252 (ae (mac-event-ae event))
2253 (service-message (and (keymapp binding)
2254 (cdr (mac-ae-parameter ae "svmg")))))
2255 (when service-message
2256 (setq service-message
2257 (intern (decode-coding-string service-message 'utf-8)))
2258 (setq binding (lookup-key binding (vector service-message))))
2259 ;; Replace (cadr event) with a dummy position so that event-start
2260 ;; returns it.
2261 (setcar (cdr event) (list (selected-window) (point) '(0 . 0) 0))
2262 (if (null (mac-ae-parameter ae 'emacs-suspension-id))
2263 (command-execute binding nil (vector event) t)
2264 (condition-case err
2265 (progn
2266 (command-execute binding nil (vector event) t)
2267 (mac-resume-apple-event ae))
2268 (error
2269 (mac-ae-set-reply-parameter ae "errs"
2270 (cons "TEXT" (error-message-string err)))
2271 (mac-resume-apple-event ae -10000)))))) ; errAEEventFailed
2272
2273 (define-key special-event-map [mac-apple-event] 'mac-dispatch-apple-event)
2274
2275 ;; Processing of Apple events are deferred at the startup time. For
2276 ;; example, files dropped onto the Emacs application icon can only be
2277 ;; processed when the initial frame has been created: this is where
2278 ;; the files should be opened.
2279 (add-hook 'after-init-hook 'mac-process-deferred-apple-events)
2280
2281 (run-with-idle-timer 5 t 'mac-cleanup-expired-apple-events)
2282
2283 \f
2284 ;;;; Drag and drop
2285
2286 (defcustom mac-dnd-types-alist
2287 '(("furl" . mac-dnd-handle-furl)
2288 ("hfs " . mac-dnd-handle-hfs)
2289 ("utxt" . mac-dnd-insert-utxt)
2290 ("TEXT" . mac-dnd-insert-TEXT)
2291 ("TIFF" . mac-dnd-insert-TIFF))
2292 "Which function to call to handle a drop of that type.
2293 The function takes three arguments, WINDOW, ACTION and DATA.
2294 WINDOW is where the drop occurred, ACTION is always `private' on
2295 Mac. DATA is the drop data. Unlike the x-dnd counterpart, the
2296 return value of the function is not significant.
2297
2298 See also `mac-dnd-known-types'."
2299 :version "22.1"
2300 :type 'alist
2301 :group 'mac)
2302
2303 (defun mac-dnd-handle-furl (window action data)
2304 (dnd-handle-one-url window action (mac-furl-to-string data)))
2305
2306 (defun mac-dnd-handle-hfs (window action data)
2307 ;; struct HFSFlavor {
2308 ;; OSType fileType;
2309 ;; OSType fileCreator;
2310 ;; UInt16 fdFlags;
2311 ;; FSSpec fileSpec;
2312 ;; };
2313 (let* ((file-name (mac-coerce-ae-data "fss " (substring data 10)
2314 'undecoded-file-name))
2315 (url (concat "file://"
2316 (mapconcat 'url-hexify-string
2317 (split-string file-name "/") "/"))))
2318 (dnd-handle-one-url window action url)))
2319
2320 (defun mac-dnd-insert-utxt (window action data)
2321 (dnd-insert-text window action (mac-utxt-to-string data)))
2322
2323 (defun mac-dnd-insert-TEXT (window action data)
2324 (dnd-insert-text window action (mac-TEXT-to-string data)))
2325
2326 (defun mac-dnd-insert-TIFF (window action data)
2327 (dnd-insert-text window action (mac-TIFF-to-string data)))
2328
2329 (defun mac-dnd-drop-data (event frame window data type &optional action)
2330 (or action (setq action 'private))
2331 (let* ((type-info (assoc type mac-dnd-types-alist))
2332 (handler (cdr type-info))
2333 (w (posn-window (event-start event))))
2334 (when handler
2335 (if (and (window-live-p w)
2336 (not (window-minibuffer-p w))
2337 (not (window-dedicated-p w)))
2338 ;; If dropping in an ordinary window which we could use,
2339 ;; let dnd-open-file-other-window specify what to do.
2340 (progn
2341 (when (not mouse-yank-at-point)
2342 (goto-char (posn-point (event-start event))))
2343 (funcall handler window action data))
2344 ;; If we can't display the file here,
2345 ;; make a new window for it.
2346 (let ((dnd-open-file-other-window t))
2347 (select-frame frame)
2348 (funcall handler window action data))))))
2349
2350 (defun mac-dnd-handle-drag-n-drop-event (event)
2351 "Receive drag and drop events."
2352 (interactive "e")
2353 (let ((window (posn-window (event-start event)))
2354 (ae (mac-event-ae event))
2355 action)
2356 (when (windowp window) (select-window window))
2357 (if (memq 'option (mac-ae-keyboard-modifiers ae))
2358 (setq action 'copy))
2359 (dolist (item (mac-ae-list ae))
2360 (if (not (equal (car item) "null"))
2361 (mac-dnd-drop-data event (selected-frame) window
2362 (cdr item) (car item) action)))))
2363 \f
2364 (setq font-encoding-alist
2365 (append
2366 '(("mac-roman" . mac-roman)
2367 ("mac-centraleurroman" . mac-centraleurroman)
2368 ("mac-cyrillic" . mac-cyrillic)
2369 ("mac-symbol" . mac-symbol)
2370 ("mac-dingbats" . mac-dingbats))
2371 font-encoding-alist))
2372
2373 (defun fontset-add-mac-fonts (fontset &optional base-family)
2374 (dolist (elt `((latin . (,(or base-family "Monaco") . "mac-roman"))
2375 (mac-roman . (,base-family . "mac-roman"))
2376 (mac-centraleurroman . (,base-family . "mac-centraleurroman"))
2377 (mac-cyrillic . (,base-family . "mac-cyrillic"))
2378 (mac-symbol . (,base-family . "mac-symbol"))
2379 (mac-dingbats . (,base-family . "mac-dingbats"))))
2380 (set-fontset-font fontset (car elt) (cdr elt))))
2381
2382 (defun create-fontset-from-mac-roman-font (font &optional resolved-font
2383 fontset-name)
2384 "Create a fontset from a Mac roman font FONT.
2385
2386 Optional 1st arg RESOLVED-FONT is a resolved name of FONT. If
2387 omitted, `x-resolve-font-name' is called to get the resolved name. At
2388 this time, if FONT is not available, error is signaled.
2389
2390 Optional 2nd arg FONTSET-NAME is a string to be used in
2391 `<CHARSET_ENCODING>' fields of a new fontset name. If it is omitted,
2392 an appropriate name is generated automatically.
2393
2394 It returns a name of the created fontset."
2395 (or resolved-font
2396 (setq resolved-font (x-resolve-font-name font)))
2397 (let ((base-family (aref (x-decompose-font-name resolved-font)
2398 xlfd-regexp-family-subnum)))
2399 (if (string= base-family "*")
2400 (setq base-family nil))
2401 (new-fontset fontset-name (list (cons 'ascii resolved-font)))
2402 (fontset-add-mac-fonts fontset-name base-family)))
2403
2404 (defun x-win-suspend-error ()
2405 (error "Suspending an Emacs running under Mac makes no sense"))
2406
2407 (defalias 'x-cut-buffer-or-selection-value 'x-get-selection-value)
2408
2409 (defvar mac-initialized nil
2410 "Non-nil if the w32 window system has been initialized.")
2411
2412 (defun mac-initialize-window-system ()
2413 "Initialize Emacs for Mac GUI frames."
2414
2415 ;;; Do the actual Windows setup here; the above code just defines
2416 ;;; functions and variables that we use now.
2417
2418 (setq command-line-args (x-handle-args command-line-args))
2419
2420 ;;; Make sure we have a valid resource name.
2421 (or (stringp x-resource-name)
2422 (let (i)
2423 (setq x-resource-name (invocation-name))
2424
2425 ;; Change any . or * characters in x-resource-name to hyphens,
2426 ;; so as not to choke when we use it in X resource queries.
2427 (while (setq i (string-match "[.*]" x-resource-name))
2428 (aset x-resource-name i ?-))))
2429
2430 (if (x-display-list)
2431 ;; On Mac OS 8/9, Most coding systems used in code conversion for
2432 ;; font names are not ready at the time when the terminal frame is
2433 ;; created. So we reconstruct font name table for the initial
2434 ;; frame.
2435 (mac-clear-font-name-table)
2436 (x-open-connection "Mac"
2437 x-command-line-resources
2438 ;; Exit Emacs with fatal error if this fails.
2439 t))
2440
2441 (add-hook 'suspend-hook 'x-win-suspend-error)
2442
2443 ;;; Arrange for the kill and yank functions to set and check the clipboard.
2444 (setq interprogram-cut-function 'x-select-text)
2445 (setq interprogram-paste-function 'x-get-selection-value)
2446
2447
2448
2449
2450 ;;; Turn off window-splitting optimization; Mac is usually fast enough
2451 ;;; that this is only annoying.
2452 (setq split-window-keep-point t)
2453
2454 ;; Don't show the frame name; that's redundant.
2455 (setq-default mode-line-frame-identification " ")
2456
2457 ;; Turn on support for mouse wheels.
2458 (mouse-wheel-mode 1)
2459
2460
2461 ;; Enable CLIPBOARD copy/paste through menu bar commands.
2462 (menu-bar-enable-clipboard)
2463
2464
2465 ;; Initiate drag and drop
2466
2467 (define-key special-event-map [drag-n-drop] 'mac-dnd-handle-drag-n-drop-event)
2468
2469 \f
2470 ;;;; Non-toolkit Scroll bars
2471
2472 (unless x-toolkit-scroll-bars
2473
2474 ;; for debugging
2475 ;; (defun mac-handle-scroll-bar-event (event) (interactive "e") (princ event))
2476
2477 ;;(global-set-key [vertical-scroll-bar mouse-1] 'mac-handle-scroll-bar-event)
2478
2479 (global-set-key
2480 [vertical-scroll-bar down-mouse-1]
2481 'mac-handle-scroll-bar-event)
2482
2483 (global-unset-key [vertical-scroll-bar drag-mouse-1])
2484 (global-unset-key [vertical-scroll-bar mouse-1])
2485
2486 ;; Adjust Courier font specifications in x-fixed-font-alist.
2487 (let ((courier-fonts (assoc "Courier" x-fixed-font-alist)))
2488 (if courier-fonts
2489 (dolist (label-fonts (cdr courier-fonts))
2490 (setcdr label-fonts
2491 (mapcar
2492 (lambda (font)
2493 (if (string-match "\\`-adobe-courier-\\([^-]*\\)-\\(.\\)-\\(.*\\)-iso8859-1\\'" font)
2494 (replace-match
2495 (if (string= (match-string 2 font) "o")
2496 "-*-courier-\\1-i-\\3-*-*"
2497 "-*-courier-\\1-\\2-\\3-*-*")
2498 t nil font)
2499 font))
2500 (cdr label-fonts))))))
2501
2502 ;; Setup the default fontset.
2503 (setup-default-fontset)
2504
2505 ;; Create a fontset that uses mac-roman font. With this fontset,
2506 ;; characters belonging to mac-roman charset (that contains ASCII and
2507 ;; more Latin characters) are displayed by a mac-roman font.
2508 (create-fontset-from-mac-roman-font
2509 "-*-Monaco-*-*-*-*-12-*-*-*-*-*-mac-roman" nil
2510 "-apple-Monaco-normal-r-*-*-12-*-*-*-*-*-fontset-standard")
2511
2512 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1, ...).
2513 (create-fontset-from-x-resource)
2514
2515 ;; Apply a geometry resource to the initial frame. Put it at the end
2516 ;; of the alist, so that anything specified on the command line takes
2517 ;; precedence.
2518 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
2519 parsed)
2520 (if res-geometry
2521 (progn
2522 (setq parsed (x-parse-geometry res-geometry))
2523 ;; If the resource specifies a position,
2524 ;; call the position and size "user-specified".
2525 (if (or (assq 'top parsed) (assq 'left parsed))
2526 (setq parsed (cons '(user-position . t)
2527 (cons '(user-size . t) parsed))))
2528 ;; All geometry parms apply to the initial frame.
2529 (setq initial-frame-alist (append initial-frame-alist parsed))
2530 ;; The size parms apply to all frames. Don't set it if there are
2531 ;; sizes there already (from command line).
2532 (if (and (assq 'height parsed)
2533 (not (assq 'height default-frame-alist)))
2534 (setq default-frame-alist
2535 (cons (cons 'height (cdr (assq 'height parsed)))
2536 default-frame-alist)))
2537 (if (and (assq 'width parsed)
2538 (not (assq 'width default-frame-alist)))
2539 (setq default-frame-alist
2540 (cons (cons 'width (cdr (assq 'width parsed)))
2541 default-frame-alist))))))
2542
2543 ;; Check the reverseVideo resource.
2544 (let ((case-fold-search t))
2545 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
2546 (if (and rv
2547 (string-match "^\\(true\\|yes\\|on\\)$" rv))
2548 (setq default-frame-alist
2549 (cons '(reverse . t) default-frame-alist)))))
2550
2551 (setq mac-initialized t)))
2552
2553 (defun mac-handle-scroll-bar-event (event)
2554 "Handle scroll bar EVENT to emulate Mac Toolbox style scrolling."
2555 (interactive "e")
2556 (let* ((position (event-start event))
2557 (window (nth 0 position))
2558 (bar-part (nth 4 position)))
2559 (select-window window)
2560 (cond
2561 ((eq bar-part 'up)
2562 (goto-char (window-start window))
2563 (mac-scroll-down-line))
2564 ((eq bar-part 'above-handle)
2565 (mac-scroll-down))
2566 ((eq bar-part 'handle)
2567 (scroll-bar-drag event))
2568 ((eq bar-part 'below-handle)
2569 (mac-scroll-up))
2570 ((eq bar-part 'down)
2571 (goto-char (window-start window))
2572 (mac-scroll-up-line)))))
2573
2574 (defun mac-scroll-ignore-events ()
2575 ;; Ignore confusing non-mouse events
2576 (while (not (memq (car-safe (read-event))
2577 '(mouse-1 double-mouse-1 triple-mouse-1))) nil))
2578
2579 (defun mac-scroll-down ()
2580 (track-mouse
2581 (mac-scroll-ignore-events)
2582 (scroll-down)))
2583
2584 (defun mac-scroll-down-line ()
2585 (track-mouse
2586 (mac-scroll-ignore-events)
2587 (scroll-down 1)))
2588
2589 (defun mac-scroll-up ()
2590 (track-mouse
2591 (mac-scroll-ignore-events)
2592 (scroll-up)))
2593
2594 (defun mac-scroll-up-line ()
2595 (track-mouse
2596 (mac-scroll-ignore-events)
2597 (scroll-up 1)))
2598
2599
2600 \f
2601 ;;;; Others
2602
2603 (unless (eq system-type 'darwin)
2604 ;; This variable specifies the Unix program to call (as a process) to
2605 ;; determine the amount of free space on a file system (defaults to
2606 ;; df). If it is not set to nil, ls-lisp will not work correctly
2607 ;; unless an external application df is implemented on the Mac.
2608 (setq directory-free-space-program nil)
2609
2610 ;; Set this so that Emacs calls subprocesses with "sh" as shell to
2611 ;; expand filenames Note no subprocess for the shell is actually
2612 ;; started (see run_mac_command in sysdep.c).
2613 (setq shell-file-name "sh")
2614
2615 ;; Some system variables are encoded with the system script code.
2616 (dolist (v '(system-name
2617 emacs-build-system ; Mac OS 9 version cannot dump
2618 user-login-name user-real-login-name user-full-name))
2619 (set v (decode-coding-string (symbol-value v) mac-system-coding-system))))
2620
2621 ;; Now the default directory is changed to the user's home directory
2622 ;; in emacs.c if invoked from the WindowServer (with -psn_* option).
2623 ;; (if (string= default-directory "/")
2624 ;; (cd "~"))
2625
2626 ;; Darwin 6- pty breakage is now controlled from the C code so that
2627 ;; it applies to all builds on darwin. See s/darwin.h PTY_ITERATION.
2628 ;; (setq process-connection-type t)
2629
2630 ;; Assume that fonts are always scalable on the Mac. This sometimes
2631 ;; results in characters with jagged edges. However, without it,
2632 ;; fonts with both truetype and bitmap representations but no italic
2633 ;; or bold bitmap versions will not display these variants correctly.
2634 (setq scalable-fonts-allowed t)
2635
2636 (add-to-list 'handle-args-function-alist '(mac . x-handle-args))
2637 (add-to-list 'frame-creation-function-alist '(mac . x-create-frame-with-faces))
2638 (add-to-list 'window-system-initialization-alist '(mac . mac-initialize-window-system))
2639
2640 (provide 'mac-win)
2641
2642 ;; arch-tag: 71dfcd14-cde8-4d66-b05c-85ec94fb23a6
2643 ;;; mac-win.el ends here