1 ;;; x-win.el --- parse switches controlling interface with X window system
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; X-win.el: this file is loaded from ../lisp/startup.el when it recognizes
28 ;; that X windows are to be used. Command line switches are parsed and those
29 ;; pertaining to X are processed and removed from the command line. The
30 ;; X display is opened and hooks are set for popping up the initial window.
32 ;; startup.el will then examine startup files, and eventually call the hooks
33 ;; which create the first window (s).
37 ;; These are the standard X switches from the Xt Initialize.c file of
40 ;; Command line Resource Manager string
43 ;; +synchronous *synchronous
44 ;; -background *background
47 ;; -bordercolor *borderColor
48 ;; -borderwidth .borderWidth
54 ;; -foreground *foreground
55 ;; -geometry .geometry
60 ;; -reverse *reverseVideo
62 ;; -selectionTimeout .selectionTimeout
63 ;; -synchronous *synchronous
66 ;; An alist of X options and the function which handles them. See
69 (if (not (eq window-system 'x))
70 (error "%s: Loading x-win.el but not compiled for X" (invocation-name)))
79 (defvar x-invocation-args)
81 (defvar x-command-line-resources nil)
83 ;; Handler for switches of the form "-switch value" or "-switch".
84 (defun x-handle-switch (switch)
85 (let ((aelt (assoc switch command-line-x-option-alist)))
87 (let ((param (nth 3 aelt))
90 (setq default-frame-alist
91 (cons (cons param value)
93 (setq default-frame-alist
95 (car x-invocation-args))
97 x-invocation-args (cdr x-invocation-args)))))))
99 ;; Handler for switches of the form "-switch n"
100 (defun x-handle-numeric-switch (switch)
101 (let ((aelt (assoc switch command-line-x-option-alist)))
103 (let ((param (nth 3 aelt)))
104 (setq default-frame-alist
106 (string-to-int (car x-invocation-args)))
109 (cdr x-invocation-args))))))
111 ;; Make -iconic apply only to the initial frame!
112 (defun x-handle-iconic (switch)
113 (setq initial-frame-alist
114 (cons '(visibility . icon) initial-frame-alist)))
116 ;; Handle the -xrm option.
117 (defun x-handle-xrm-switch (switch)
118 (or (consp x-invocation-args)
119 (error "%s: missing argument to `%s' option" (invocation-name) switch))
120 (setq x-command-line-resources (car x-invocation-args))
121 (setq x-invocation-args (cdr x-invocation-args)))
123 ;; Handle the geometry option
124 (defun x-handle-geometry (switch)
125 (let ((geo (x-parse-geometry (car x-invocation-args))))
126 (setq initial-frame-alist
127 (append initial-frame-alist
128 (if (or (assq 'left geo) (assq 'top geo))
129 '((user-position . t)))
130 (if (or (assq 'height geo) (assq 'width geo))
133 x-invocation-args (cdr x-invocation-args))))
135 ;; Handle the -name and -rn options. Set the variable x-resource-name
136 ;; to the option's operand; if the switch was `-name', set the name of
137 ;; the initial frame, too.
138 (defun x-handle-name-rn-switch (switch)
139 (or (consp x-invocation-args)
140 (error "%s: missing argument to `%s' option" (invocation-name) switch))
141 (setq x-resource-name (car x-invocation-args)
142 x-invocation-args (cdr x-invocation-args))
143 (if (string= switch "-name")
144 (setq initial-frame-alist (cons (cons 'name x-resource-name)
145 initial-frame-alist))))
147 (defvar x-display-name nil
148 "The X display name specifying server and X frame.")
150 (defun x-handle-display (switch)
151 (setq x-display-name (car x-invocation-args)
152 x-invocation-args (cdr x-invocation-args))
153 ;; Make subshell programs see the same DISPLAY value Emacs really uses.
154 ;; Note that this isn't completely correct, since Emacs can use
155 ;; multiple displays. However, there is no way to tell an already
156 ;; running subshell which display the user is currently typing on.
157 (setenv "DISPLAY" x-display-name))
159 (defun x-handle-args (args)
160 "Process the X-related command line options in ARGS.
161 This is done before the user's startup file is loaded. They are copied to
162 `x-invocation-args', from which the X-related things are extracted, first
163 the switch (e.g., \"-fg\") in the following code, and possible values
164 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
165 This function returns ARGS minus the arguments that have been processed."
166 ;; We use ARGS to accumulate the args that we don't handle here, to return.
167 (setq x-invocation-args args
169 (while x-invocation-args
170 (let* ((this-switch (car x-invocation-args))
171 (orig-this-switch this-switch)
172 completion argval aelt handler)
173 (setq x-invocation-args (cdr x-invocation-args))
174 ;; Check for long options with attached arguments
175 ;; and separate out the attached option argument into argval.
176 (if (string-match "^--[^=]*=" this-switch)
177 (setq argval (substring this-switch (match-end 0))
178 this-switch (substring this-switch 0 (1- (match-end 0)))))
179 ;; Complete names of long options.
180 (if (string-match "^--" this-switch)
182 (setq completion (try-completion this-switch command-line-x-option-alist))
183 (if (eq completion t)
184 ;; Exact match for long option.
186 (if (stringp completion)
187 (let ((elt (assoc completion command-line-x-option-alist)))
188 ;; Check for abbreviated long option.
190 (error "Option `%s' is ambiguous" this-switch))
191 (setq this-switch completion))))))
192 (setq aelt (assoc this-switch command-line-x-option-alist))
193 (if aelt (setq handler (nth 2 aelt)))
196 (let ((x-invocation-args
197 (cons argval x-invocation-args)))
198 (funcall handler this-switch))
199 (funcall handler this-switch))
200 (setq args (cons orig-this-switch args)))))
204 ;; Standard X cursor shapes, courtesy of Mr. Fox, who wanted ALL of them.
207 (defconst x-pointer-X-cursor 0)
208 (defconst x-pointer-arrow 2)
209 (defconst x-pointer-based-arrow-down 4)
210 (defconst x-pointer-based-arrow-up 6)
211 (defconst x-pointer-boat 8)
212 (defconst x-pointer-bogosity 10)
213 (defconst x-pointer-bottom-left-corner 12)
214 (defconst x-pointer-bottom-right-corner 14)
215 (defconst x-pointer-bottom-side 16)
216 (defconst x-pointer-bottom-tee 18)
217 (defconst x-pointer-box-spiral 20)
218 (defconst x-pointer-center-ptr 22)
219 (defconst x-pointer-circle 24)
220 (defconst x-pointer-clock 26)
221 (defconst x-pointer-coffee-mug 28)
222 (defconst x-pointer-cross 30)
223 (defconst x-pointer-cross-reverse 32)
224 (defconst x-pointer-crosshair 34)
225 (defconst x-pointer-diamond-cross 36)
226 (defconst x-pointer-dot 38)
227 (defconst x-pointer-dotbox 40)
228 (defconst x-pointer-double-arrow 42)
229 (defconst x-pointer-draft-large 44)
230 (defconst x-pointer-draft-small 46)
231 (defconst x-pointer-draped-box 48)
232 (defconst x-pointer-exchange 50)
233 (defconst x-pointer-fleur 52)
234 (defconst x-pointer-gobbler 54)
235 (defconst x-pointer-gumby 56)
236 (defconst x-pointer-hand1 58)
237 (defconst x-pointer-hand2 60)
238 (defconst x-pointer-heart 62)
239 (defconst x-pointer-icon 64)
240 (defconst x-pointer-iron-cross 66)
241 (defconst x-pointer-left-ptr 68)
242 (defconst x-pointer-left-side 70)
243 (defconst x-pointer-left-tee 72)
244 (defconst x-pointer-leftbutton 74)
245 (defconst x-pointer-ll-angle 76)
246 (defconst x-pointer-lr-angle 78)
247 (defconst x-pointer-man 80)
248 (defconst x-pointer-middlebutton 82)
249 (defconst x-pointer-mouse 84)
250 (defconst x-pointer-pencil 86)
251 (defconst x-pointer-pirate 88)
252 (defconst x-pointer-plus 90)
253 (defconst x-pointer-question-arrow 92)
254 (defconst x-pointer-right-ptr 94)
255 (defconst x-pointer-right-side 96)
256 (defconst x-pointer-right-tee 98)
257 (defconst x-pointer-rightbutton 100)
258 (defconst x-pointer-rtl-logo 102)
259 (defconst x-pointer-sailboat 104)
260 (defconst x-pointer-sb-down-arrow 106)
261 (defconst x-pointer-sb-h-double-arrow 108)
262 (defconst x-pointer-sb-left-arrow 110)
263 (defconst x-pointer-sb-right-arrow 112)
264 (defconst x-pointer-sb-up-arrow 114)
265 (defconst x-pointer-sb-v-double-arrow 116)
266 (defconst x-pointer-shuttle 118)
267 (defconst x-pointer-sizing 120)
268 (defconst x-pointer-spider 122)
269 (defconst x-pointer-spraycan 124)
270 (defconst x-pointer-star 126)
271 (defconst x-pointer-target 128)
272 (defconst x-pointer-tcross 130)
273 (defconst x-pointer-top-left-arrow 132)
274 (defconst x-pointer-top-left-corner 134)
275 (defconst x-pointer-top-right-corner 136)
276 (defconst x-pointer-top-side 138)
277 (defconst x-pointer-top-tee 140)
278 (defconst x-pointer-trek 142)
279 (defconst x-pointer-ul-angle 144)
280 (defconst x-pointer-umbrella 146)
281 (defconst x-pointer-ur-angle 148)
282 (defconst x-pointer-watch 150)
283 (defconst x-pointer-xterm 152)
289 (defvar x-colors '("aquamarine"
347 "medium spring green"
427 "The list of X colors from the `rgb.txt' file.")
429 (defun x-defined-colors (&optional frame)
430 "Return a list of colors supported for a particular frame.
431 The argument FRAME specifies which frame to try.
432 The value may be different for frames on different X displays."
433 (or frame (setq frame (selected-frame)))
434 (let ((all-colors x-colors)
436 (defined-colors nil))
438 (setq this-color (car all-colors)
439 all-colors (cdr all-colors))
440 (and (face-color-supported-p frame this-color t)
441 (setq defined-colors (cons this-color defined-colors))))
446 (defun iconify-or-deiconify-frame ()
447 "Iconify the selected frame, or deiconify if it's currently an icon."
449 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
451 (make-frame-visible)))
453 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
456 ;; Map certain keypad keys into ASCII characters
457 ;; that people usually expect.
458 (define-key function-key-map [backspace] [127])
459 (define-key function-key-map [delete] [127])
460 (define-key function-key-map [tab] [?\t])
461 (define-key function-key-map [linefeed] [?\n])
462 (define-key function-key-map [clear] [?\C-l])
463 (define-key function-key-map [return] [?\C-m])
464 (define-key function-key-map [escape] [?\e])
465 (define-key function-key-map [M-backspace] [?\M-\d])
466 (define-key function-key-map [M-delete] [?\M-\d])
467 (define-key function-key-map [M-tab] [?\M-\t])
468 (define-key function-key-map [M-linefeed] [?\M-\n])
469 (define-key function-key-map [M-clear] [?\M-\C-l])
470 (define-key function-key-map [M-return] [?\M-\C-m])
471 (define-key function-key-map [M-escape] [?\M-\e])
473 ;; These tell read-char how to convert
474 ;; these special chars to ASCII.
475 (put 'backspace 'ascii-character 127)
476 (put 'delete 'ascii-character 127)
477 (put 'tab 'ascii-character ?\t)
478 (put 'linefeed 'ascii-character ?\n)
479 (put 'clear 'ascii-character 12)
480 (put 'return 'ascii-character 13)
481 (put 'escape 'ascii-character ?\e)
483 (defun vendor-specific-keysyms (vendor)
484 "Return the appropriate value of system-key-alist for VENDOR.
485 VENDOR is a string containing the name of the X Server's vendor,
486 as returned by (x-server-vendor)."
487 (cond ((string-equal vendor "Apollo Computer Inc.")
509 ((or (string-equal vendor "Hewlett-Packard Incorporated")
510 (string-equal vendor "Hewlett-Packard Company"))
511 '(( 168 . mute-acute)
513 ( 170 . mute-asciicircum)
514 ( 171 . mute-diaeresis)
515 ( 172 . mute-asciitilde)
529 (65397 . kp-backtab)))
530 ((or (string-equal vendor "X11/NeWS - Sun Microsystems Inc.")
531 (string-equal vendor "X Consortium"))
535 ;; These are for Sun under X11R6
543 ;; This is used by DEC's X server.
544 '((65280 . remove)))))
547 ;;;; Selections and cut buffers
549 ;;; We keep track of the last text selected here, so we can check the
550 ;;; current selection against it, and avoid passing back our own text
551 ;;; from x-cut-buffer-or-selection-value.
552 (defvar x-last-selected-text nil)
554 ;;; It is said that overlarge strings are slow to put into the cut buffer.
555 ;;; Note this value is overridden below.
556 (defvar x-cut-buffer-max 20000
557 "Max number of characters to put in the cut buffer.")
559 (defvar x-select-enable-clipboard nil
560 "Non-nil means cutting and pasting uses the clipboard.
561 This is in addition to the primary selection.")
563 ;;; Make TEXT, a string, the primary X selection.
564 ;;; Also, set the value of X cut buffer 0, for backward compatibility
565 ;;; with older X applications.
566 ;;; gildea@lcs.mit.edu says it's not desirable to put kills
567 ;;; in the clipboard.
568 (defun x-select-text (text &optional push)
569 ;; Don't send the cut buffer too much text.
570 ;; It becomes slow, and if really big it causes errors.
571 (if (< (length text) x-cut-buffer-max)
572 (x-set-cut-buffer text push)
573 (x-set-cut-buffer "" push))
574 (x-set-selection 'PRIMARY text)
575 (if x-select-enable-clipboard
576 (x-set-selection 'CLIPBOARD text))
577 (setq x-last-selected-text text))
579 ;;; Return the value of the current X selection.
580 ;;; Consult the selection, then the cut buffer. Treat empty strings
581 ;;; as if they were unset.
582 (defun x-cut-buffer-or-selection-value ()
585 ;; Don't die if x-get-selection signals an error.
587 (setq text (x-get-selection 'PRIMARY))
589 (if (string= text "") (setq text nil))
591 (if x-select-enable-clipboard
593 (setq text (x-get-selection 'CLIPBOARD))
595 (if (string= text "") (setq text nil))
596 (or text (setq text (x-get-cut-buffer 0)))
597 (if (string= text "") (setq text nil))
601 ((eq text x-last-selected-text) nil)
602 ((string= text x-last-selected-text)
603 ;; Record the newer string, so subsequent calls can use the `eq' test.
604 (setq x-last-selected-text text)
607 (setq x-last-selected-text text)))))
610 ;;; Do the actual X Windows setup here; the above code just defines
611 ;;; functions and variables that we use now.
613 (setq command-line-args (x-handle-args command-line-args))
615 ;;; Make sure we have a valid resource name.
616 (or (stringp x-resource-name)
618 (setq x-resource-name (invocation-name))
620 ;; Change any . or * characters in x-resource-name to hyphens,
621 ;; so as not to choke when we use it in X resource queries.
622 (while (setq i (string-match "[.*]" x-resource-name))
623 (aset x-resource-name i ?-))))
625 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
626 ;; the same lisp directory, don't pass the third argument unless we seem
627 ;; to have the multi-display support.
628 (if (fboundp 'x-close-connection)
629 (x-open-connection (or x-display-name
630 (setq x-display-name (getenv "DISPLAY")))
631 x-command-line-resources
632 ;; Exit Emacs with fatal error if this fails.
634 (x-open-connection (or x-display-name
635 (setq x-display-name (getenv "DISPLAY")))
636 x-command-line-resources))
638 (setq frame-creation-function 'x-create-frame-with-faces)
640 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
643 ;; Sun expects the menu bar cut and paste commands to use the clipboard.
644 ;; This has ,? to match both on Sunos and on Solaris.
645 (if (string-match "Sun Microsystems,? Inc\\."
647 (menu-bar-enable-clipboard))
649 ;; Apply a geometry resource to the initial frame. Put it at the end
650 ;; of the alist, so that anything specified on the command line takes
652 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
656 (setq parsed (x-parse-geometry res-geometry))
657 ;; If the resource specifies a position,
658 ;; call the position and size "user-specified".
659 (if (or (assq 'top parsed) (assq 'left parsed))
660 (setq parsed (cons '(user-position . t)
661 (cons '(user-size . t) parsed))))
662 ;; All geometry parms apply to the initial frame.
663 (setq initial-frame-alist (append initial-frame-alist parsed))
664 ;; The size parms apply to all frames.
665 (if (assq 'height parsed)
666 (setq default-frame-alist
667 (cons (cons 'height (cdr (assq 'height parsed)))
668 default-frame-alist)))
669 (if (assq 'width parsed)
670 (setq default-frame-alist
671 (cons (cons 'width (cdr (assq 'width parsed)))
672 default-frame-alist))))))
674 ;; Check the reverseVideo resource.
675 (let ((case-fold-search t))
676 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
678 (string-match "^\\(true\\|yes\\|on\\)$" rv))
679 (setq default-frame-alist
680 (cons '(reverse . t) default-frame-alist)))))
682 ;; Set x-selection-timeout, measured in milliseconds.
683 (let ((res-selection-timeout
684 (x-get-resource "selectionTimeout" "SelectionTimeout")))
685 (setq x-selection-timeout 20000)
686 (if res-selection-timeout
687 (setq x-selection-timeout (string-to-number res-selection-timeout))))
689 (defun x-win-suspend-error ()
690 (error "Suspending an emacs running under X makes no sense"))
691 (add-hook 'suspend-hook 'x-win-suspend-error)
693 ;;; Arrange for the kill and yank functions to set and check the clipboard.
694 (setq interprogram-cut-function 'x-select-text)
695 (setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
697 ;;; Turn off window-splitting optimization; X is usually fast enough
698 ;;; that this is only annoying.
699 (setq split-window-keep-point t)
701 ;; Don't show the frame name; that's redundant with X.
702 (setq-default mode-line-buffer-identification '("Emacs: %12b"))
704 ;;; x-win.el ends here