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