1 ;;; x-win.el --- parse switches controlling interface with X window system
3 ;; Copyright (C) 1993, 1994, 2001 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)))
78 (if (fboundp 'new-fontset)
81 (defvar x-invocation-args)
83 (defvar x-command-line-resources nil)
85 ;; Handler for switches of the form "-switch value" or "-switch".
86 (defun x-handle-switch (switch)
87 (let ((aelt (assoc switch command-line-x-option-alist)))
89 (let ((param (nth 3 aelt))
92 (setq default-frame-alist
93 (cons (cons param value)
95 (setq default-frame-alist
97 (car x-invocation-args))
99 x-invocation-args (cdr x-invocation-args)))))))
101 ;; Handler for switches of the form "-switch n"
102 (defun x-handle-numeric-switch (switch)
103 (let ((aelt (assoc switch command-line-x-option-alist)))
105 (let ((param (nth 3 aelt)))
106 (setq default-frame-alist
108 (string-to-int (car x-invocation-args)))
111 (cdr x-invocation-args))))))
113 ;; Make -iconic apply only to the initial frame!
114 (defun x-handle-iconic (switch)
115 (setq initial-frame-alist
116 (cons '(visibility . icon) initial-frame-alist)))
118 ;; Handle the -xrm option.
119 (defun x-handle-xrm-switch (switch)
120 (unless (consp x-invocation-args)
121 (error "%s: missing argument to `%s' option" (invocation-name) switch))
122 (setq x-command-line-resources
123 (if (null x-command-line-resources)
124 (car x-invocation-args)
125 (concat x-command-line-resources "\n" (car x-invocation-args))))
126 (setq x-invocation-args (cdr x-invocation-args)))
128 ;; Handle the geometry option
129 (defun x-handle-geometry (switch)
130 (let ((geo (x-parse-geometry (car x-invocation-args))))
131 (setq initial-frame-alist
132 (append initial-frame-alist
133 (if (or (assq 'left geo) (assq 'top geo))
134 '((user-position . t)))
135 (if (or (assq 'height geo) (assq 'width geo))
138 x-invocation-args (cdr x-invocation-args))))
140 ;; Handle the -name option. Set the variable x-resource-name
141 ;; to the option's operand; set the name of
142 ;; the initial frame, too.
143 (defun x-handle-name-switch (switch)
144 (or (consp x-invocation-args)
145 (error "%s: missing argument to `%s' option" (invocation-name) switch))
146 (setq x-resource-name (car x-invocation-args)
147 x-invocation-args (cdr x-invocation-args))
148 (setq initial-frame-alist (cons (cons 'name x-resource-name)
149 initial-frame-alist)))
151 (defvar x-display-name nil
152 "The X display name specifying server and X frame.")
154 (defun x-handle-display (switch)
155 (setq x-display-name (car x-invocation-args)
156 x-invocation-args (cdr x-invocation-args))
157 ;; Make subshell programs see the same DISPLAY value Emacs really uses.
158 ;; Note that this isn't completely correct, since Emacs can use
159 ;; multiple displays. However, there is no way to tell an already
160 ;; running subshell which display the user is currently typing on.
161 (setenv "DISPLAY" x-display-name))
163 (defun x-handle-args (args)
164 "Process the X-related command line options in ARGS.
165 This is done before the user's startup file is loaded. They are copied to
166 `x-invocation-args', from which the X-related things are extracted, first
167 the switch (e.g., \"-fg\") in the following code, and possible values
168 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
169 This function returns ARGS minus the arguments that have been processed."
170 ;; We use ARGS to accumulate the args that we don't handle here, to return.
171 (setq x-invocation-args args
173 (while (and x-invocation-args
174 (not (equal (car x-invocation-args) "--")))
175 (let* ((this-switch (car x-invocation-args))
176 (orig-this-switch this-switch)
177 completion argval aelt handler)
178 (setq x-invocation-args (cdr x-invocation-args))
179 ;; Check for long options with attached arguments
180 ;; and separate out the attached option argument into argval.
181 (if (string-match "^--[^=]*=" this-switch)
182 (setq argval (substring this-switch (match-end 0))
183 this-switch (substring this-switch 0 (1- (match-end 0)))))
184 ;; Complete names of long options.
185 (if (string-match "^--" this-switch)
187 (setq completion (try-completion this-switch command-line-x-option-alist))
188 (if (eq completion t)
189 ;; Exact match for long option.
191 (if (stringp completion)
192 (let ((elt (assoc completion command-line-x-option-alist)))
193 ;; Check for abbreviated long option.
195 (error "Option `%s' is ambiguous" this-switch))
196 (setq this-switch completion))))))
197 (setq aelt (assoc this-switch command-line-x-option-alist))
198 (if aelt (setq handler (nth 2 aelt)))
201 (let ((x-invocation-args
202 (cons argval x-invocation-args)))
203 (funcall handler this-switch))
204 (funcall handler this-switch))
205 (setq args (cons orig-this-switch args)))))
206 (nconc (nreverse args) x-invocation-args))
209 ;; Standard X cursor shapes, courtesy of Mr. Fox, who wanted ALL of them.
212 (defconst x-pointer-X-cursor 0)
213 (defconst x-pointer-arrow 2)
214 (defconst x-pointer-based-arrow-down 4)
215 (defconst x-pointer-based-arrow-up 6)
216 (defconst x-pointer-boat 8)
217 (defconst x-pointer-bogosity 10)
218 (defconst x-pointer-bottom-left-corner 12)
219 (defconst x-pointer-bottom-right-corner 14)
220 (defconst x-pointer-bottom-side 16)
221 (defconst x-pointer-bottom-tee 18)
222 (defconst x-pointer-box-spiral 20)
223 (defconst x-pointer-center-ptr 22)
224 (defconst x-pointer-circle 24)
225 (defconst x-pointer-clock 26)
226 (defconst x-pointer-coffee-mug 28)
227 (defconst x-pointer-cross 30)
228 (defconst x-pointer-cross-reverse 32)
229 (defconst x-pointer-crosshair 34)
230 (defconst x-pointer-diamond-cross 36)
231 (defconst x-pointer-dot 38)
232 (defconst x-pointer-dotbox 40)
233 (defconst x-pointer-double-arrow 42)
234 (defconst x-pointer-draft-large 44)
235 (defconst x-pointer-draft-small 46)
236 (defconst x-pointer-draped-box 48)
237 (defconst x-pointer-exchange 50)
238 (defconst x-pointer-fleur 52)
239 (defconst x-pointer-gobbler 54)
240 (defconst x-pointer-gumby 56)
241 (defconst x-pointer-hand1 58)
242 (defconst x-pointer-hand2 60)
243 (defconst x-pointer-heart 62)
244 (defconst x-pointer-icon 64)
245 (defconst x-pointer-iron-cross 66)
246 (defconst x-pointer-left-ptr 68)
247 (defconst x-pointer-left-side 70)
248 (defconst x-pointer-left-tee 72)
249 (defconst x-pointer-leftbutton 74)
250 (defconst x-pointer-ll-angle 76)
251 (defconst x-pointer-lr-angle 78)
252 (defconst x-pointer-man 80)
253 (defconst x-pointer-middlebutton 82)
254 (defconst x-pointer-mouse 84)
255 (defconst x-pointer-pencil 86)
256 (defconst x-pointer-pirate 88)
257 (defconst x-pointer-plus 90)
258 (defconst x-pointer-question-arrow 92)
259 (defconst x-pointer-right-ptr 94)
260 (defconst x-pointer-right-side 96)
261 (defconst x-pointer-right-tee 98)
262 (defconst x-pointer-rightbutton 100)
263 (defconst x-pointer-rtl-logo 102)
264 (defconst x-pointer-sailboat 104)
265 (defconst x-pointer-sb-down-arrow 106)
266 (defconst x-pointer-sb-h-double-arrow 108)
267 (defconst x-pointer-sb-left-arrow 110)
268 (defconst x-pointer-sb-right-arrow 112)
269 (defconst x-pointer-sb-up-arrow 114)
270 (defconst x-pointer-sb-v-double-arrow 116)
271 (defconst x-pointer-shuttle 118)
272 (defconst x-pointer-sizing 120)
273 (defconst x-pointer-spider 122)
274 (defconst x-pointer-spraycan 124)
275 (defconst x-pointer-star 126)
276 (defconst x-pointer-target 128)
277 (defconst x-pointer-tcross 130)
278 (defconst x-pointer-top-left-arrow 132)
279 (defconst x-pointer-top-left-corner 134)
280 (defconst x-pointer-top-right-corner 136)
281 (defconst x-pointer-top-side 138)
282 (defconst x-pointer-top-tee 140)
283 (defconst x-pointer-trek 142)
284 (defconst x-pointer-ul-angle 144)
285 (defconst x-pointer-umbrella 146)
286 (defconst x-pointer-ur-angle 148)
287 (defconst x-pointer-watch 150)
288 (defconst x-pointer-xterm 152)
294 (defvar x-colors '("LightGreen"
893 "LightGoldenrodYellow"
894 "light goldenrod yellow"
911 "medium spring green"
1046 "The list of X colors from the `rgb.txt' file.
1047 XConsortium: rgb.txt,v 10.41 94/02/20 18:39:36 rws Exp")
1049 (defun xw-defined-colors (&optional frame)
1050 "Internal function called by `defined-colors', which see."
1051 (or frame (setq frame (selected-frame)))
1052 (let ((all-colors x-colors)
1054 (defined-colors nil))
1056 (setq this-color (car all-colors)
1057 all-colors (cdr all-colors))
1058 (and (color-supported-p this-color frame t)
1059 (setq defined-colors (cons this-color defined-colors))))
1064 (defun iconify-or-deiconify-frame ()
1065 "Iconify the selected frame, or deiconify if it's currently an icon."
1067 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
1069 (make-frame-visible)))
1071 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
1074 ;; Map certain keypad keys into ASCII characters
1075 ;; that people usually expect.
1076 (define-key function-key-map [backspace] [127])
1077 (define-key function-key-map [delete] [127])
1078 (define-key function-key-map [tab] [?\t])
1079 (define-key function-key-map [linefeed] [?\n])
1080 (define-key function-key-map [clear] [?\C-l])
1081 (define-key function-key-map [return] [?\C-m])
1082 (define-key function-key-map [escape] [?\e])
1083 (define-key function-key-map [M-backspace] [?\M-\d])
1084 (define-key function-key-map [M-delete] [?\M-\d])
1085 (define-key function-key-map [M-tab] [?\M-\t])
1086 (define-key function-key-map [M-linefeed] [?\M-\n])
1087 (define-key function-key-map [M-clear] [?\M-\C-l])
1088 (define-key function-key-map [M-return] [?\M-\C-m])
1089 (define-key function-key-map [M-escape] [?\M-\e])
1090 (define-key function-key-map [iso-lefttab] [backtab])
1092 ;; These tell read-char how to convert
1093 ;; these special chars to ASCII.
1094 (put 'backspace 'ascii-character 127)
1095 (put 'delete 'ascii-character 127)
1096 (put 'tab 'ascii-character ?\t)
1097 (put 'linefeed 'ascii-character ?\n)
1098 (put 'clear 'ascii-character 12)
1099 (put 'return 'ascii-character 13)
1100 (put 'escape 'ascii-character ?\e)
1102 (defun vendor-specific-keysyms (vendor)
1103 "Return the appropriate value of system-key-alist for VENDOR.
1104 VENDOR is a string containing the name of the X Server's vendor,
1105 as returned by (x-server-vendor)."
1106 (cond ((string-equal vendor "Apollo Computer Inc.")
1128 ((or (string-equal vendor "Hewlett-Packard Incorporated")
1129 (string-equal vendor "Hewlett-Packard Company"))
1130 '(( 168 . mute-acute)
1132 ( 170 . mute-asciicircum)
1133 ( 171 . mute-diaeresis)
1134 ( 172 . mute-asciitilde)
1143 (65392 . insertline)
1144 (65393 . deleteline)
1145 (65394 . insertchar)
1146 (65395 . deletechar)
1148 (65397 . kp-backtab)))
1149 ((or (string-equal vendor "X11/NeWS - Sun Microsystems Inc.")
1150 (string-equal vendor "X Consortium"))
1154 ;; These are for Sun under X11R6
1162 ;; This is used by DEC's X server.
1163 '((65280 . remove)))))
1166 ;;;; Selections and cut buffers
1168 ;;; We keep track of the last text selected here, so we can check the
1169 ;;; current selection against it, and avoid passing back our own text
1170 ;;; from x-cut-buffer-or-selection-value.
1171 (defvar x-last-selected-text nil)
1173 ;;; It is said that overlarge strings are slow to put into the cut buffer.
1174 ;;; Note this value is overridden below.
1175 (defvar x-cut-buffer-max 20000
1176 "Max number of characters to put in the cut buffer.")
1178 (defcustom x-select-enable-clipboard nil
1179 "Non-nil means cutting and pasting uses the clipboard.
1180 This is in addition to, but in preference to, the primary selection."
1184 ;;; Make TEXT, a string, the primary X selection.
1185 ;;; Also, set the value of X cut buffer 0, for backward compatibility
1186 ;;; with older X applications.
1187 ;;; gildea@lcs.mit.edu says it's not desirable to put kills
1188 ;;; in the clipboard.
1189 (defun x-select-text (text &optional push)
1190 ;; Don't send the cut buffer too much text.
1191 ;; It becomes slow, and if really big it causes errors.
1192 (if (< (length text) x-cut-buffer-max)
1193 (x-set-cut-buffer text push)
1194 (x-set-cut-buffer "" push))
1195 (x-set-selection 'PRIMARY text)
1196 (if x-select-enable-clipboard
1197 (x-set-selection 'CLIPBOARD text))
1198 (setq x-last-selected-text text))
1200 ;;; Return the value of the current X selection.
1201 ;;; Consult the selection, then the cut buffer. Treat empty strings
1202 ;;; as if they were unset.
1203 ;;; If this function is called twice and finds the same text,
1204 ;;; it returns nil the second time. This is so that a single
1205 ;;; selection won't be added to the kill ring over and over.
1206 (defun x-cut-buffer-or-selection-value ()
1208 (when x-select-enable-clipboard
1211 (setq text (x-get-selection 'CLIPBOARD 'COMPOUND_TEXT))
1215 (setq text (x-get-selection 'CLIPBOARD 'STRING))
1217 (if (string= text "") (setq text nil)))
1219 ;; Don't die if x-get-selection signals an error.
1222 (setq text (x-get-selection 'PRIMARY 'COMPOUND_TEXT))
1226 (setq text (x-get-selection 'PRIMARY 'STRING))
1228 (if (string= text "") (setq text nil))
1230 (or text (setq text (x-get-cut-buffer 0)))
1231 (if (string= text "") (setq text nil))
1235 ((eq text x-last-selected-text) nil)
1236 ((string= text x-last-selected-text)
1237 ;; Record the newer string, so subsequent calls can use the `eq' test.
1238 (setq x-last-selected-text text)
1241 (setq x-last-selected-text text)))))
1244 ;;; Do the actual X Windows setup here; the above code just defines
1245 ;;; functions and variables that we use now.
1247 (setq command-line-args (x-handle-args command-line-args))
1249 ;;; Make sure we have a valid resource name.
1250 (or (stringp x-resource-name)
1252 (setq x-resource-name (invocation-name))
1254 ;; Change any . or * characters in x-resource-name to hyphens,
1255 ;; so as not to choke when we use it in X resource queries.
1256 (while (setq i (string-match "[.*]" x-resource-name))
1257 (aset x-resource-name i ?-))))
1259 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
1260 ;; the same lisp directory, don't pass the third argument unless we seem
1261 ;; to have the multi-display support.
1262 (if (fboundp 'x-close-connection)
1263 (x-open-connection (or x-display-name
1264 (setq x-display-name (getenv "DISPLAY")))
1265 x-command-line-resources
1266 ;; Exit Emacs with fatal error if this fails.
1268 (x-open-connection (or x-display-name
1269 (setq x-display-name (getenv "DISPLAY")))
1270 x-command-line-resources))
1272 (setq frame-creation-function 'x-create-frame-with-faces)
1274 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
1277 (if (fboundp 'new-fontset)
1279 ;; Create the standard fontset.
1280 (create-fontset-from-fontset-spec standard-fontset-spec t)
1282 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1, ...).
1283 (create-fontset-from-x-resource)
1285 ;; Try to create a fontset from a font specification which comes
1286 ;; from initial-frame-alist, default-frame-alist, or X resource.
1287 ;; A font specification in command line argument (i.e. -fn XXXX)
1288 ;; should be already in default-frame-alist as a `font'
1289 ;; parameter. However, any font specifications in site-start
1290 ;; library, user's init file (.emacs), and default.el are not
1291 ;; yet handled here.
1293 (let ((font (or (cdr (assq 'font initial-frame-alist))
1294 (cdr (assq 'font default-frame-alist))
1295 (x-get-resource "font" "Font")))
1296 xlfd-fields resolved-name)
1298 (not (query-fontset font))
1299 (setq resolved-name (x-resolve-font-name font))
1300 (setq xlfd-fields (x-decompose-font-name font)))
1301 (if (string= "fontset"
1302 (aref xlfd-fields xlfd-regexp-registry-subnum))
1303 (new-fontset font (x-complement-fontset-spec xlfd-fields nil))
1304 ;; Create a fontset from FONT. The fontset name is
1305 ;; generated from FONT.
1306 (create-fontset-from-ascii-font font
1307 resolved-name "startup"))))))
1309 ;; Sun expects the menu bar cut and paste commands to use the clipboard.
1310 ;; This has ,? to match both on Sunos and on Solaris.
1311 (if (string-match "Sun Microsystems,? Inc\\."
1313 (menu-bar-enable-clipboard))
1315 ;; Apply a geometry resource to the initial frame. Put it at the end
1316 ;; of the alist, so that anything specified on the command line takes
1318 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
1322 (setq parsed (x-parse-geometry res-geometry))
1323 ;; If the resource specifies a position,
1324 ;; call the position and size "user-specified".
1325 (if (or (assq 'top parsed) (assq 'left parsed))
1326 (setq parsed (cons '(user-position . t)
1327 (cons '(user-size . t) parsed))))
1328 ;; All geometry parms apply to the initial frame.
1329 (setq initial-frame-alist (append initial-frame-alist parsed))
1330 ;; The size parms apply to all frames.
1331 (if (assq 'height parsed)
1332 (setq default-frame-alist
1333 (cons (cons 'height (cdr (assq 'height parsed)))
1334 default-frame-alist)))
1335 (if (assq 'width parsed)
1336 (setq default-frame-alist
1337 (cons (cons 'width (cdr (assq 'width parsed)))
1338 default-frame-alist))))))
1340 ;; Check the reverseVideo resource.
1341 (let ((case-fold-search t))
1342 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
1344 (string-match "^\\(true\\|yes\\|on\\)$" rv))
1345 (setq default-frame-alist
1346 (cons '(reverse . t) default-frame-alist)))))
1348 ;; Set x-selection-timeout, measured in milliseconds.
1349 (let ((res-selection-timeout
1350 (x-get-resource "selectionTimeout" "SelectionTimeout")))
1351 (setq x-selection-timeout 20000)
1352 (if res-selection-timeout
1353 (setq x-selection-timeout (string-to-number res-selection-timeout))))
1355 (defun x-win-suspend-error ()
1356 (error "Suspending an emacs running under X makes no sense"))
1357 (add-hook 'suspend-hook 'x-win-suspend-error)
1359 ;;; Arrange for the kill and yank functions to set and check the clipboard.
1360 (setq interprogram-cut-function 'x-select-text)
1361 (setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
1363 ;;; Turn off window-splitting optimization; X is usually fast enough
1364 ;;; that this is only annoying.
1365 (setq split-window-keep-point t)
1367 ;; Don't show the frame name; that's redundant with X.
1368 (setq-default mode-line-frame-identification " ")
1370 ;;; Motif direct handling of f10 wasn't working right,
1371 ;;; So temporarily we've turned it off in lwlib-Xm.c
1372 ;;; and turned the Emacs f10 back on.
1373 ;;; ;; Motif normally handles f10 itself, so don't try to handle it a second time.
1374 ;;; (if (featurep 'motif)
1375 ;;; (global-set-key [f10] 'ignore))
1377 ;;; x-win.el ends here