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 ;; Handle options that apply to initial frame only
114 (defun x-handle-initial-switch (switch)
115 (let ((aelt (assoc switch command-line-x-option-alist)))
117 (let ((param (nth 3 aelt))
118 (value (nth 4 aelt)))
120 (setq initial-frame-alist
121 (cons (cons param value)
122 initial-frame-alist))
123 (setq initial-frame-alist
125 (car x-invocation-args))
127 x-invocation-args (cdr x-invocation-args)))))))
129 ;; Make -iconic apply only to the initial frame!
130 (defun x-handle-iconic (switch)
131 (setq initial-frame-alist
132 (cons '(visibility . icon) initial-frame-alist)))
134 ;; Handle the -xrm option.
135 (defun x-handle-xrm-switch (switch)
136 (unless (consp x-invocation-args)
137 (error "%s: missing argument to `%s' option" (invocation-name) switch))
138 (setq x-command-line-resources
139 (if (null x-command-line-resources)
140 (car x-invocation-args)
141 (concat x-command-line-resources "\n" (car x-invocation-args))))
142 (setq x-invocation-args (cdr x-invocation-args)))
144 ;; Handle the geometry option
145 (defun x-handle-geometry (switch)
146 (let* ((geo (x-parse-geometry (car x-invocation-args)))
147 (left (assq 'left geo))
148 (top (assq 'top geo))
149 (height (assq 'height geo))
150 (width (assq 'width geo)))
151 (if (or height width)
152 (setq default-frame-alist
153 (append default-frame-alist
155 (if height (list height))
156 (if width (list width)))
158 (append initial-frame-alist
160 (if height (list height))
161 (if width (list width)))))
163 (setq initial-frame-alist
164 (append initial-frame-alist
165 '((user-position . t))
166 (if left (list left))
167 (if top (list top)))))
168 (setq x-invocation-args (cdr x-invocation-args))))
170 ;; Handle the -name option. Set the variable x-resource-name
171 ;; to the option's operand; set the name of
172 ;; the initial frame, too.
173 (defun x-handle-name-switch (switch)
174 (or (consp x-invocation-args)
175 (error "%s: missing argument to `%s' option" (invocation-name) switch))
176 (setq x-resource-name (car x-invocation-args)
177 x-invocation-args (cdr x-invocation-args))
178 (setq initial-frame-alist (cons (cons 'name x-resource-name)
179 initial-frame-alist)))
181 (defvar x-display-name nil
182 "The X display name specifying server and X frame.")
184 (defun x-handle-display (switch)
185 (setq x-display-name (car x-invocation-args)
186 x-invocation-args (cdr x-invocation-args))
187 ;; Make subshell programs see the same DISPLAY value Emacs really uses.
188 ;; Note that this isn't completely correct, since Emacs can use
189 ;; multiple displays. However, there is no way to tell an already
190 ;; running subshell which display the user is currently typing on.
191 (setenv "DISPLAY" x-display-name))
193 (defun x-handle-args (args)
194 "Process the X-related command line options in ARGS.
195 This is done before the user's startup file is loaded. They are copied to
196 `x-invocation-args', from which the X-related things are extracted, first
197 the switch (e.g., \"-fg\") in the following code, and possible values
198 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
199 This function returns ARGS minus the arguments that have been processed."
200 ;; We use ARGS to accumulate the args that we don't handle here, to return.
201 (setq x-invocation-args args
203 (while (and x-invocation-args
204 (not (equal (car x-invocation-args) "--")))
205 (let* ((this-switch (car x-invocation-args))
206 (orig-this-switch this-switch)
207 completion argval aelt handler)
208 (setq x-invocation-args (cdr x-invocation-args))
209 ;; Check for long options with attached arguments
210 ;; and separate out the attached option argument into argval.
211 (if (string-match "^--[^=]*=" this-switch)
212 (setq argval (substring this-switch (match-end 0))
213 this-switch (substring this-switch 0 (1- (match-end 0)))))
214 ;; Complete names of long options.
215 (if (string-match "^--" this-switch)
217 (setq completion (try-completion this-switch command-line-x-option-alist))
218 (if (eq completion t)
219 ;; Exact match for long option.
221 (if (stringp completion)
222 (let ((elt (assoc completion command-line-x-option-alist)))
223 ;; Check for abbreviated long option.
225 (error "Option `%s' is ambiguous" this-switch))
226 (setq this-switch completion))))))
227 (setq aelt (assoc this-switch command-line-x-option-alist))
228 (if aelt (setq handler (nth 2 aelt)))
231 (let ((x-invocation-args
232 (cons argval x-invocation-args)))
233 (funcall handler this-switch))
234 (funcall handler this-switch))
235 (setq args (cons orig-this-switch args)))))
236 (nconc (nreverse args) x-invocation-args))
239 ;; Standard X cursor shapes, courtesy of Mr. Fox, who wanted ALL of them.
242 (defconst x-pointer-X-cursor 0)
243 (defconst x-pointer-arrow 2)
244 (defconst x-pointer-based-arrow-down 4)
245 (defconst x-pointer-based-arrow-up 6)
246 (defconst x-pointer-boat 8)
247 (defconst x-pointer-bogosity 10)
248 (defconst x-pointer-bottom-left-corner 12)
249 (defconst x-pointer-bottom-right-corner 14)
250 (defconst x-pointer-bottom-side 16)
251 (defconst x-pointer-bottom-tee 18)
252 (defconst x-pointer-box-spiral 20)
253 (defconst x-pointer-center-ptr 22)
254 (defconst x-pointer-circle 24)
255 (defconst x-pointer-clock 26)
256 (defconst x-pointer-coffee-mug 28)
257 (defconst x-pointer-cross 30)
258 (defconst x-pointer-cross-reverse 32)
259 (defconst x-pointer-crosshair 34)
260 (defconst x-pointer-diamond-cross 36)
261 (defconst x-pointer-dot 38)
262 (defconst x-pointer-dotbox 40)
263 (defconst x-pointer-double-arrow 42)
264 (defconst x-pointer-draft-large 44)
265 (defconst x-pointer-draft-small 46)
266 (defconst x-pointer-draped-box 48)
267 (defconst x-pointer-exchange 50)
268 (defconst x-pointer-fleur 52)
269 (defconst x-pointer-gobbler 54)
270 (defconst x-pointer-gumby 56)
271 (defconst x-pointer-hand1 58)
272 (defconst x-pointer-hand2 60)
273 (defconst x-pointer-heart 62)
274 (defconst x-pointer-icon 64)
275 (defconst x-pointer-iron-cross 66)
276 (defconst x-pointer-left-ptr 68)
277 (defconst x-pointer-left-side 70)
278 (defconst x-pointer-left-tee 72)
279 (defconst x-pointer-leftbutton 74)
280 (defconst x-pointer-ll-angle 76)
281 (defconst x-pointer-lr-angle 78)
282 (defconst x-pointer-man 80)
283 (defconst x-pointer-middlebutton 82)
284 (defconst x-pointer-mouse 84)
285 (defconst x-pointer-pencil 86)
286 (defconst x-pointer-pirate 88)
287 (defconst x-pointer-plus 90)
288 (defconst x-pointer-question-arrow 92)
289 (defconst x-pointer-right-ptr 94)
290 (defconst x-pointer-right-side 96)
291 (defconst x-pointer-right-tee 98)
292 (defconst x-pointer-rightbutton 100)
293 (defconst x-pointer-rtl-logo 102)
294 (defconst x-pointer-sailboat 104)
295 (defconst x-pointer-sb-down-arrow 106)
296 (defconst x-pointer-sb-h-double-arrow 108)
297 (defconst x-pointer-sb-left-arrow 110)
298 (defconst x-pointer-sb-right-arrow 112)
299 (defconst x-pointer-sb-up-arrow 114)
300 (defconst x-pointer-sb-v-double-arrow 116)
301 (defconst x-pointer-shuttle 118)
302 (defconst x-pointer-sizing 120)
303 (defconst x-pointer-spider 122)
304 (defconst x-pointer-spraycan 124)
305 (defconst x-pointer-star 126)
306 (defconst x-pointer-target 128)
307 (defconst x-pointer-tcross 130)
308 (defconst x-pointer-top-left-arrow 132)
309 (defconst x-pointer-top-left-corner 134)
310 (defconst x-pointer-top-right-corner 136)
311 (defconst x-pointer-top-side 138)
312 (defconst x-pointer-top-tee 140)
313 (defconst x-pointer-trek 142)
314 (defconst x-pointer-ul-angle 144)
315 (defconst x-pointer-umbrella 146)
316 (defconst x-pointer-ur-angle 148)
317 (defconst x-pointer-watch 150)
318 (defconst x-pointer-xterm 152)
324 (defvar x-colors '("LightGreen"
923 "LightGoldenrodYellow"
924 "light goldenrod yellow"
941 "medium spring green"
1076 "The list of X colors from the `rgb.txt' file.
1077 XConsortium: rgb.txt,v 10.41 94/02/20 18:39:36 rws Exp")
1079 (defun xw-defined-colors (&optional frame)
1080 "Internal function called by `defined-colors', which see."
1081 (or frame (setq frame (selected-frame)))
1082 (let ((all-colors x-colors)
1084 (defined-colors nil))
1086 (setq this-color (car all-colors)
1087 all-colors (cdr all-colors))
1088 (and (color-supported-p this-color frame t)
1089 (setq defined-colors (cons this-color defined-colors))))
1094 (defun iconify-or-deiconify-frame ()
1095 "Iconify the selected frame, or deiconify if it's currently an icon."
1097 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
1099 (make-frame-visible)))
1101 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
1104 ;; Map certain keypad keys into ASCII characters
1105 ;; that people usually expect.
1106 (define-key function-key-map [backspace] [127])
1107 (define-key function-key-map [delete] [127])
1108 (define-key function-key-map [tab] [?\t])
1109 (define-key function-key-map [linefeed] [?\n])
1110 (define-key function-key-map [clear] [?\C-l])
1111 (define-key function-key-map [return] [?\C-m])
1112 (define-key function-key-map [escape] [?\e])
1113 (define-key function-key-map [M-backspace] [?\M-\d])
1114 (define-key function-key-map [M-delete] [?\M-\d])
1115 (define-key function-key-map [M-tab] [?\M-\t])
1116 (define-key function-key-map [M-linefeed] [?\M-\n])
1117 (define-key function-key-map [M-clear] [?\M-\C-l])
1118 (define-key function-key-map [M-return] [?\M-\C-m])
1119 (define-key function-key-map [M-escape] [?\M-\e])
1120 (define-key function-key-map [iso-lefttab] [backtab])
1122 ;; These tell read-char how to convert
1123 ;; these special chars to ASCII.
1124 (put 'backspace 'ascii-character 127)
1125 (put 'delete 'ascii-character 127)
1126 (put 'tab 'ascii-character ?\t)
1127 (put 'linefeed 'ascii-character ?\n)
1128 (put 'clear 'ascii-character 12)
1129 (put 'return 'ascii-character 13)
1130 (put 'escape 'ascii-character ?\e)
1132 (defun vendor-specific-keysyms (vendor)
1133 "Return the appropriate value of system-key-alist for VENDOR.
1134 VENDOR is a string containing the name of the X Server's vendor,
1135 as returned by (x-server-vendor)."
1136 (cond ((string-equal vendor "Apollo Computer Inc.")
1158 ((or (string-equal vendor "Hewlett-Packard Incorporated")
1159 (string-equal vendor "Hewlett-Packard Company"))
1160 '(( 168 . mute-acute)
1162 ( 170 . mute-asciicircum)
1163 ( 171 . mute-diaeresis)
1164 ( 172 . mute-asciitilde)
1173 (65392 . insertline)
1174 (65393 . deleteline)
1175 (65394 . insertchar)
1176 (65395 . deletechar)
1178 (65397 . kp-backtab)))
1179 ((or (string-equal vendor "X11/NeWS - Sun Microsystems Inc.")
1180 (string-equal vendor "X Consortium"))
1184 ;; These are for Sun under X11R6
1192 ;; This is used by DEC's X server.
1193 '((65280 . remove)))))
1196 ;;;; Selections and cut buffers
1198 ;;; We keep track of the last text selected here, so we can check the
1199 ;;; current selection against it, and avoid passing back our own text
1200 ;;; from x-cut-buffer-or-selection-value.
1201 (defvar x-last-selected-text nil)
1203 ;;; It is said that overlarge strings are slow to put into the cut buffer.
1204 ;;; Note this value is overridden below.
1205 (defvar x-cut-buffer-max 20000
1206 "Max number of characters to put in the cut buffer.")
1208 (defcustom x-select-enable-clipboard nil
1209 "Non-nil means cutting and pasting uses the clipboard.
1210 This is in addition to, but in preference to, the primary selection."
1214 ;;; Make TEXT, a string, the primary X selection.
1215 ;;; Also, set the value of X cut buffer 0, for backward compatibility
1216 ;;; with older X applications.
1217 ;;; gildea@stop.mail-abuse.org says it's not desirable to put kills
1218 ;;; in the clipboard.
1219 (defun x-select-text (text &optional push)
1220 ;; Don't send the cut buffer too much text.
1221 ;; It becomes slow, and if really big it causes errors.
1222 (if (< (length text) x-cut-buffer-max)
1223 (x-set-cut-buffer text push)
1224 (x-set-cut-buffer "" push))
1225 (x-set-selection 'PRIMARY text)
1226 (if x-select-enable-clipboard
1227 (x-set-selection 'CLIPBOARD text))
1228 (setq x-last-selected-text text))
1230 ;;; Return the value of the current X selection.
1231 ;;; Consult the selection, then the cut buffer. Treat empty strings
1232 ;;; as if they were unset.
1233 ;;; If this function is called twice and finds the same text,
1234 ;;; it returns nil the second time. This is so that a single
1235 ;;; selection won't be added to the kill ring over and over.
1236 (defun x-cut-buffer-or-selection-value ()
1238 (when x-select-enable-clipboard
1241 (setq text (x-get-selection 'CLIPBOARD 'COMPOUND_TEXT))
1245 (setq text (x-get-selection 'CLIPBOARD 'STRING))
1247 (if (string= text "") (setq text nil)))
1249 ;; Don't die if x-get-selection signals an error.
1252 (setq text (x-get-selection 'PRIMARY 'COMPOUND_TEXT))
1256 (setq text (x-get-selection 'PRIMARY 'STRING))
1258 (if (string= text "") (setq text nil))
1260 (or text (setq text (x-get-cut-buffer 0)))
1261 (if (string= text "") (setq text nil))
1265 ((eq text x-last-selected-text) nil)
1266 ((string= text x-last-selected-text)
1267 ;; Record the newer string, so subsequent calls can use the `eq' test.
1268 (setq x-last-selected-text text)
1271 (setq x-last-selected-text text)))))
1274 ;;; Do the actual X Windows setup here; the above code just defines
1275 ;;; functions and variables that we use now.
1277 (setq command-line-args (x-handle-args command-line-args))
1279 ;;; Make sure we have a valid resource name.
1280 (or (stringp x-resource-name)
1282 (setq x-resource-name (invocation-name))
1284 ;; Change any . or * characters in x-resource-name to hyphens,
1285 ;; so as not to choke when we use it in X resource queries.
1286 (while (setq i (string-match "[.*]" x-resource-name))
1287 (aset x-resource-name i ?-))))
1289 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
1290 ;; the same lisp directory, don't pass the third argument unless we seem
1291 ;; to have the multi-display support.
1292 (if (fboundp 'x-close-connection)
1293 (x-open-connection (or x-display-name
1294 (setq x-display-name (getenv "DISPLAY")))
1295 x-command-line-resources
1296 ;; Exit Emacs with fatal error if this fails.
1298 (x-open-connection (or x-display-name
1299 (setq x-display-name (getenv "DISPLAY")))
1300 x-command-line-resources))
1302 (setq frame-creation-function 'x-create-frame-with-faces)
1304 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
1307 (if (fboundp 'new-fontset)
1309 ;; Create the standard fontset.
1310 (create-fontset-from-fontset-spec standard-fontset-spec t)
1312 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1, ...).
1313 (create-fontset-from-x-resource)
1315 ;; Try to create a fontset from a font specification which comes
1316 ;; from initial-frame-alist, default-frame-alist, or X resource.
1317 ;; A font specification in command line argument (i.e. -fn XXXX)
1318 ;; should be already in default-frame-alist as a `font'
1319 ;; parameter. However, any font specifications in site-start
1320 ;; library, user's init file (.emacs), and default.el are not
1321 ;; yet handled here.
1323 (let ((font (or (cdr (assq 'font initial-frame-alist))
1324 (cdr (assq 'font default-frame-alist))
1325 (x-get-resource "font" "Font")))
1326 xlfd-fields resolved-name)
1328 (not (query-fontset font))
1329 (setq resolved-name (x-resolve-font-name font))
1330 (setq xlfd-fields (x-decompose-font-name font)))
1331 (if (string= "fontset"
1332 (aref xlfd-fields xlfd-regexp-registry-subnum))
1333 (new-fontset font (x-complement-fontset-spec xlfd-fields nil))
1334 ;; Create a fontset from FONT. The fontset name is
1335 ;; generated from FONT.
1336 (create-fontset-from-ascii-font font
1337 resolved-name "startup"))))))
1339 ;; Sun expects the menu bar cut and paste commands to use the clipboard.
1340 ;; This has ,? to match both on Sunos and on Solaris.
1341 (if (string-match "Sun Microsystems,? Inc\\."
1343 (menu-bar-enable-clipboard))
1345 ;; Apply a geometry resource to the initial frame. Put it at the end
1346 ;; of the alist, so that anything specified on the command line takes
1348 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
1352 (setq parsed (x-parse-geometry res-geometry))
1353 ;; If the resource specifies a position,
1354 ;; call the position and size "user-specified".
1355 (if (or (assq 'top parsed) (assq 'left parsed))
1356 (setq parsed (cons '(user-position . t)
1357 (cons '(user-size . t) parsed))))
1358 ;; All geometry parms apply to the initial frame.
1359 (setq initial-frame-alist (append initial-frame-alist parsed))
1360 ;; The size parms apply to all frames.
1361 (if (assq 'height parsed)
1362 (setq default-frame-alist
1363 (cons (cons 'height (cdr (assq 'height parsed)))
1364 default-frame-alist)))
1365 (if (assq 'width parsed)
1366 (setq default-frame-alist
1367 (cons (cons 'width (cdr (assq 'width parsed)))
1368 default-frame-alist))))))
1370 ;; Check the reverseVideo resource.
1371 (let ((case-fold-search t))
1372 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
1374 (string-match "^\\(true\\|yes\\|on\\)$" rv))
1375 (setq default-frame-alist
1376 (cons '(reverse . t) default-frame-alist)))))
1378 ;; Set x-selection-timeout, measured in milliseconds.
1379 (let ((res-selection-timeout
1380 (x-get-resource "selectionTimeout" "SelectionTimeout")))
1381 (setq x-selection-timeout 20000)
1382 (if res-selection-timeout
1383 (setq x-selection-timeout (string-to-number res-selection-timeout))))
1385 (defun x-win-suspend-error ()
1386 (error "Suspending an emacs running under X makes no sense"))
1387 (add-hook 'suspend-hook 'x-win-suspend-error)
1389 ;;; Arrange for the kill and yank functions to set and check the clipboard.
1390 (setq interprogram-cut-function 'x-select-text)
1391 (setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
1393 ;;; Turn off window-splitting optimization; X is usually fast enough
1394 ;;; that this is only annoying.
1395 (setq split-window-keep-point t)
1397 ;; Don't show the frame name; that's redundant with X.
1398 (setq-default mode-line-frame-identification " ")
1400 ;;; Motif direct handling of f10 wasn't working right,
1401 ;;; So temporarily we've turned it off in lwlib-Xm.c
1402 ;;; and turned the Emacs f10 back on.
1403 ;;; ;; Motif normally handles f10 itself, so don't try to handle it a second time.
1404 ;;; (if (featurep 'motif)
1405 ;;; (global-set-key [f10] 'ignore))
1407 ;;; x-win.el ends here