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