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