Require cl only when compiling.
[emacs.git] / lisp / term / w32-win.el
blobade04585ec71565008dd3053ddd5ff4c46697559
1 ;;; win32-win.el --- parse switches controlling interface with win32
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Kevin Gallo
6 ;; Keywords: terminals
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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;; win32-win.el: this file is loaded from ../lisp/startup.el when it recognizes
28 ;; that win32 windows are to be used. Command line switches are parsed and those
29 ;; pertaining to win32 are processed and removed from the command line. The
30 ;; win32 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).
35 ;;; Code:
38 ;; These are the standard X switches from the Xt Initialize.c file of
39 ;; Release 4.
41 ;; Command line Resource Manager string
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 ;; -i .iconType
58 ;; -itype .iconType
59 ;; -iconic .iconic
60 ;; -name .name
61 ;; -reverse *reverseVideo
62 ;; -rv *reverseVideo
63 ;; -selectionTimeout .selectionTimeout
64 ;; -synchronous *synchronous
65 ;; -xrm
67 ;; An alist of X options and the function which handles them. See
68 ;; ../startup.el.
70 (if (not (eq window-system 'win32))
71 (error "%s: Loading win32-win.el but not compiled for win32" (invocation-name)))
73 (require 'frame)
74 (require 'mouse)
75 (require 'scroll-bar)
76 (require 'faces)
77 (require 'select)
78 (require 'menu-bar)
80 ;; Disable until scrollbars are fully functional
81 (scroll-bar-mode nil)
83 (defvar x-invocation-args)
85 (defvar x-command-line-resources nil)
87 (defconst x-option-alist
88 '(("-bw" . x-handle-numeric-switch)
89 ("-d" . x-handle-display)
90 ("-display" . x-handle-display)
91 ("-name" . x-handle-name-rn-switch)
92 ("-rn" . x-handle-name-rn-switch)
93 ("-T" . x-handle-switch)
94 ("-r" . x-handle-switch)
95 ("-rv" . x-handle-switch)
96 ("-reverse" . x-handle-switch)
97 ("-fn" . x-handle-switch)
98 ("-font" . x-handle-switch)
99 ("-ib" . x-handle-numeric-switch)
100 ("-g" . x-handle-geometry)
101 ("-geometry" . x-handle-geometry)
102 ("-fg" . x-handle-switch)
103 ("-foreground". x-handle-switch)
104 ("-bg" . x-handle-switch)
105 ("-background". x-handle-switch)
106 ("-ms" . x-handle-switch)
107 ("-itype" . x-handle-switch)
108 ("-i" . x-handle-switch)
109 ("-iconic" . x-handle-iconic)
110 ("-xrm" . x-handle-xrm-switch)
111 ("-cr" . x-handle-switch)
112 ("-vb" . x-handle-switch)
113 ("-hb" . x-handle-switch)
114 ("-bd" . x-handle-switch)))
116 (defconst x-long-option-alist
117 '(("--border-width" . "-bw")
118 ("--display" . "-d")
119 ("--name" . "-name")
120 ("--title" . "-T")
121 ("--reverse-video" . "-reverse")
122 ("--font" . "-font")
123 ("--internal-border" . "-ib")
124 ("--geometry" . "-geometry")
125 ("--foreground-color" . "-fg")
126 ("--background-color" . "-bg")
127 ("--mouse-color" . "-ms")
128 ("--icon-type" . "-itype")
129 ("--iconic" . "-iconic")
130 ("--xrm" . "-xrm")
131 ("--cursor-color" . "-cr")
132 ("--vertical-scroll-bars" . "-vb")
133 ("--border-color" . "-bd")))
135 (defconst x-switch-definitions
136 '(("-name" name)
137 ("-T" name)
138 ("-r" reverse t)
139 ("-rv" reverse t)
140 ("-reverse" reverse t)
141 ("-fn" font)
142 ("-font" font)
143 ("-ib" internal-border-width)
144 ("-fg" foreground-color)
145 ("-foreground" foreground-color)
146 ("-bg" background-color)
147 ("-background" background-color)
148 ("-ms" mouse-color)
149 ("-cr" cursor-color)
150 ("-itype" icon-type t)
151 ("-i" icon-type t)
152 ("-vb" vertical-scroll-bars t)
153 ("-hb" horizontal-scroll-bars t)
154 ("-bd" border-color)
155 ("-bw" border-width)))
157 ;; Handler for switches of the form "-switch value" or "-switch".
158 (defun x-handle-switch (switch)
159 (let ((aelt (assoc switch x-switch-definitions)))
160 (if aelt
161 (if (nth 2 aelt)
162 (setq default-frame-alist
163 (cons (cons (nth 1 aelt) (nth 2 aelt))
164 default-frame-alist))
165 (setq default-frame-alist
166 (cons (cons (nth 1 aelt)
167 (car x-invocation-args))
168 default-frame-alist)
169 x-invocation-args (cdr x-invocation-args))))))
171 ;; Make -iconic apply only to the initial frame!
172 (defun x-handle-iconic (switch)
173 (setq initial-frame-alist
174 (cons '(visibility . icon) initial-frame-alist)))
176 ;; Handler for switches of the form "-switch n"
177 (defun x-handle-numeric-switch (switch)
178 (let ((aelt (assoc switch x-switch-definitions)))
179 (if aelt
180 (setq default-frame-alist
181 (cons (cons (nth 1 aelt)
182 (string-to-int (car x-invocation-args)))
183 default-frame-alist)
184 x-invocation-args
185 (cdr x-invocation-args)))))
187 ;; Handle the -xrm option.
188 (defun x-handle-xrm-switch (switch)
189 (or (consp x-invocation-args)
190 (error "%s: missing argument to `%s' option" (invocation-name) switch))
191 (setq x-command-line-resources (car x-invocation-args))
192 (setq x-invocation-args (cdr x-invocation-args)))
194 ;; Handle the geometry option
195 (defun x-handle-geometry (switch)
196 (let ((geo (x-parse-geometry (car x-invocation-args))))
197 (setq initial-frame-alist
198 (append initial-frame-alist
199 (if (or (assq 'left geo) (assq 'top geo))
200 '((user-position . t)))
201 (if (or (assq 'height geo) (assq 'width geo))
202 '((user-size . t)))
203 geo)
204 x-invocation-args (cdr x-invocation-args))))
206 ;; Handle the -name and -rn options. Set the variable x-resource-name
207 ;; to the option's operand; if the switch was `-name', set the name of
208 ;; the initial frame, too.
209 (defun x-handle-name-rn-switch (switch)
210 (or (consp x-invocation-args)
211 (error "%s: missing argument to `%s' option" (invocation-name) switch))
212 (setq x-resource-name (car x-invocation-args)
213 x-invocation-args (cdr x-invocation-args))
214 (if (string= switch "-name")
215 (setq initial-frame-alist (cons (cons 'name x-resource-name)
216 initial-frame-alist))))
218 (defvar x-display-name nil
219 "The display name specifying server and frame.")
221 (defun x-handle-display (switch)
222 (setq x-display-name (car x-invocation-args)
223 x-invocation-args (cdr x-invocation-args)))
225 (defvar x-invocation-args nil)
227 (defun x-handle-args (args)
228 "Process the X-related command line options in ARGS.
229 This is done before the user's startup file is loaded. They are copied to
230 x-invocation args from which the X-related things are extracted, first
231 the switch (e.g., \"-fg\") in the following code, and possible values
232 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
233 This returns ARGS with the arguments that have been processed removed."
234 (message "%s" args)
235 (setq x-invocation-args args
236 args nil)
237 (while x-invocation-args
238 (let* ((this-switch (car x-invocation-args))
239 (orig-this-switch this-switch)
240 completion argval aelt)
241 (setq x-invocation-args (cdr x-invocation-args))
242 ;; Check for long options with attached arguments
243 ;; and separate out the attached option argument into argval.
244 (if (string-match "^--[^=]*=" this-switch)
245 (setq argval (substring this-switch (match-end 0))
246 this-switch (substring this-switch 0 (1- (match-end 0)))))
247 (setq completion (try-completion this-switch x-long-option-alist))
248 (if (eq completion t)
249 ;; Exact match for long option.
250 (setq this-switch (cdr (assoc this-switch x-long-option-alist)))
251 (if (stringp completion)
252 (let ((elt (assoc completion x-long-option-alist)))
253 ;; Check for abbreviated long option.
254 (or elt
255 (error "Option `%s' is ambiguous" this-switch))
256 (setq this-switch (cdr elt)))
257 ;; Check for a short option.
258 (setq argval nil this-switch orig-this-switch)))
259 (setq aelt (assoc this-switch x-option-alist))
260 (if aelt
261 (if argval
262 (let ((x-invocation-args
263 (cons argval x-invocation-args)))
264 (funcall (cdr aelt) this-switch))
265 (funcall (cdr aelt) this-switch))
266 (setq args (cons this-switch args)))))
267 (setq args (nreverse args)))
272 ;; Available colors
275 (defvar x-colors '("aquamarine"
276 "Aquamarine"
277 "medium aquamarine"
278 "MediumAquamarine"
279 "black"
280 "Black"
281 "blue"
282 "Blue"
283 "cadet blue"
284 "CadetBlue"
285 "cornflower blue"
286 "CornflowerBlue"
287 "dark slate blue"
288 "DarkSlateBlue"
289 "light blue"
290 "LightBlue"
291 "light steel blue"
292 "LightSteelBlue"
293 "medium blue"
294 "MediumBlue"
295 "medium slate blue"
296 "MediumSlateBlue"
297 "midnight blue"
298 "MidnightBlue"
299 "navy blue"
300 "NavyBlue"
301 "navy"
302 "Navy"
303 "sky blue"
304 "SkyBlue"
305 "slate blue"
306 "SlateBlue"
307 "steel blue"
308 "SteelBlue"
309 "coral"
310 "Coral"
311 "cyan"
312 "Cyan"
313 "firebrick"
314 "Firebrick"
315 "brown"
316 "Brown"
317 "gold"
318 "Gold"
319 "goldenrod"
320 "Goldenrod"
321 "green"
322 "Green"
323 "dark green"
324 "DarkGreen"
325 "dark olive green"
326 "DarkOliveGreen"
327 "forest green"
328 "ForestGreen"
329 "lime green"
330 "LimeGreen"
331 "medium sea green"
332 "MediumSeaGreen"
333 "medium spring green"
334 "MediumSpringGreen"
335 "pale green"
336 "PaleGreen"
337 "sea green"
338 "SeaGreen"
339 "spring green"
340 "SpringGreen"
341 "yellow green"
342 "YellowGreen"
343 "dark slate grey"
344 "DarkSlateGrey"
345 "dark slate gray"
346 "DarkSlateGray"
347 "dim grey"
348 "DimGrey"
349 "dim gray"
350 "DimGray"
351 "light grey"
352 "LightGrey"
353 "light gray"
354 "LightGray"
355 "gray"
356 "grey"
357 "Gray"
358 "Grey"
359 "khaki"
360 "Khaki"
361 "magenta"
362 "Magenta"
363 "maroon"
364 "Maroon"
365 "orange"
366 "Orange"
367 "orchid"
368 "Orchid"
369 "dark orchid"
370 "DarkOrchid"
371 "medium orchid"
372 "MediumOrchid"
373 "pink"
374 "Pink"
375 "plum"
376 "Plum"
377 "red"
378 "Red"
379 "indian red"
380 "IndianRed"
381 "medium violet red"
382 "MediumVioletRed"
383 "orange red"
384 "OrangeRed"
385 "violet red"
386 "VioletRed"
387 "salmon"
388 "Salmon"
389 "sienna"
390 "Sienna"
391 "tan"
392 "Tan"
393 "thistle"
394 "Thistle"
395 "turquoise"
396 "Turquoise"
397 "dark turquoise"
398 "DarkTurquoise"
399 "medium turquoise"
400 "MediumTurquoise"
401 "violet"
402 "Violet"
403 "blue violet"
404 "BlueViolet"
405 "wheat"
406 "Wheat"
407 "white"
408 "White"
409 "yellow"
410 "Yellow"
411 "green yellow"
412 "GreenYellow")
413 "The full list of X colors from the `rgb.text' file.")
415 (defun x-defined-colors (&optional frame)
416 "Return a list of colors supported for a particular frame.
417 The argument FRAME specifies which frame to try.
418 The value may be different for frames on different X displays."
419 (or frame (setq frame (selected-frame)))
420 (let ((all-colors x-colors)
421 (this-color nil)
422 (defined-colors nil))
423 (while all-colors
424 (setq this-color (car all-colors)
425 all-colors (cdr all-colors))
426 (and (face-color-supported-p frame this-color t)
427 (setq defined-colors (cons this-color defined-colors))))
428 defined-colors))
430 ;;;; Function keys
432 (defun iconify-or-deiconify-frame ()
433 "Iconify the selected frame, or deiconify if it's currently an icon."
434 (interactive)
435 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
436 (iconify-frame)
437 (make-frame-visible)))
439 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
440 global-map)
442 ;; Map certain keypad keys into ASCII characters
443 ;; that people usually expect.
444 (define-key function-key-map [backspace] [127])
445 (define-key function-key-map [delete] [127])
446 (define-key function-key-map [tab] [?\t])
447 (define-key function-key-map [linefeed] [?\n])
448 (define-key function-key-map [clear] [11])
449 (define-key function-key-map [return] [13])
450 (define-key function-key-map [escape] [?\e])
451 (define-key function-key-map [M-backspace] [?\M-\d])
452 (define-key function-key-map [M-delete] [?\M-\d])
453 (define-key function-key-map [M-tab] [?\M-\t])
454 (define-key function-key-map [M-linefeed] [?\M-\n])
455 (define-key function-key-map [M-clear] [?\M-\013])
456 (define-key function-key-map [M-return] [?\M-\015])
457 (define-key function-key-map [M-escape] [?\M-\e])
459 ;; These tell read-char how to convert
460 ;; these special chars to ASCII.
461 (put 'backspace 'ascii-character 127)
462 (put 'delete 'ascii-character 127)
463 (put 'tab 'ascii-character ?\t)
464 (put 'linefeed 'ascii-character ?\n)
465 (put 'clear 'ascii-character 12)
466 (put 'return 'ascii-character 13)
467 (put 'escape 'ascii-character ?\e)
470 ;;;; Selections and cut buffers
472 ;;; We keep track of the last text selected here, so we can check the
473 ;;; current selection against it, and avoid passing back our own text
474 ;;; from x-cut-buffer-or-selection-value.
475 (defvar x-last-selected-text nil)
477 ;;; It is said that overlarge strings are slow to put into the cut buffer.
478 ;;; Note this value is overridden below.
479 (defvar x-cut-buffer-max 20000
480 "Max number of characters to put in the cut buffer.")
482 (defvar x-select-enable-clipboard t
483 "Non-nil means cutting and pasting uses the clipboard.
484 This is in addition to the primary selection.")
486 (defun x-select-text (text &optional push)
487 (if x-select-enable-clipboard
488 (win32-set-clipboard-data text)))
490 ;;; Return the value of the current selection.
491 ;;; Consult the selection, then the cut buffer. Treat empty strings
492 ;;; as if they were unset.
493 (defun x-get-selection-value ()
494 (if x-select-enable-clipboard
495 (let (text)
496 ;; Don't die if x-get-selection signals an error.
497 (condition-case c
498 (setq text (win32-get-clipboard-data))
499 (error (message "win32-get-clipboard-data:%s" c)))
500 (if (string= text "") (setq text nil))
501 text)))
503 ;;; Do the actual Windows setup here; the above code just defines
504 ;;; functions and variables that we use now.
506 (setq command-line-args (x-handle-args command-line-args))
508 ;;; Make sure we have a valid resource name.
509 (or (stringp x-resource-name)
510 (let (i)
511 (setq x-resource-name (invocation-name))
513 ;; Change any . or * characters in x-resource-name to hyphens,
514 ;; so as not to choke when we use it in X resource queries.
515 (while (setq i (string-match "[.*]" x-resource-name))
516 (aset x-resource-name i ?-))))
518 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
519 ;; the same lisp directory, don't pass the third argument unless we seem
520 ;; to have the multi-display support.
521 (if (fboundp 'x-close-connection)
522 (x-open-connection ""
523 x-command-line-resources
524 ;; Exit Emacs with fatal error if this fails.
526 (x-open-connection ""
527 x-command-line-resources))
529 (setq frame-creation-function 'x-create-frame-with-faces)
531 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
532 x-cut-buffer-max))
534 ;; Win32 expects the menu bar cut and paste commands to use the clipboard.
535 ;; This has ,? to match both on Sunos and on Solaris.
536 (menu-bar-enable-clipboard)
538 ;; Apply a geometry resource to the initial frame. Put it at the end
539 ;; of the alist, so that anything specified on the command line takes
540 ;; precedence.
541 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
542 parsed)
543 (if res-geometry
544 (progn
545 (setq parsed (x-parse-geometry res-geometry))
546 ;; If the resource specifies a position,
547 ;; call the position and size "user-specified".
548 (if (or (assq 'top parsed) (assq 'left parsed))
549 (setq parsed (cons '(user-position . t)
550 (cons '(user-size . t) parsed))))
551 ;; All geometry parms apply to the initial frame.
552 (setq initial-frame-alist (append initial-frame-alist parsed))
553 ;; The size parms apply to all frames.
554 (if (assq 'height parsed)
555 (setq default-frame-alist
556 (cons (cons 'height (cdr (assq 'height parsed)))
557 default-frame-alist)))
558 (if (assq 'width parsed)
559 (setq default-frame-alist
560 (cons (cons 'width (cdr (assq 'width parsed)))
561 default-frame-alist))))))
563 ;; Check the reverseVideo resource.
564 (let ((case-fold-search t))
565 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
566 (if (and rv
567 (string-match "^\\(true\\|yes\\|on\\)$" rv))
568 (setq default-frame-alist
569 (cons '(reverse . t) default-frame-alist)))))
571 ;; Set x-selection-timeout, measured in milliseconds.
572 (let ((res-selection-timeout
573 (x-get-resource "selectionTimeout" "SelectionTimeout")))
574 (setq x-selection-timeout 20000)
575 (if res-selection-timeout
576 (setq x-selection-timeout (string-to-number res-selection-timeout))))
578 (defun x-win-suspend-error ()
579 (error "Suspending an emacs running under Win32 makes no sense"))
580 (add-hook 'suspend-hook 'x-win-suspend-error)
582 ;;; Arrange for the kill and yank functions to set and check the clipboard.
583 (setq interprogram-cut-function 'x-select-text)
584 (setq interprogram-paste-function 'x-get-selection-value)
586 ;;; Turn off window-splitting optimization; win32 is usually fast enough
587 ;;; that this is only annoying.
588 (setq split-window-keep-point t)
590 ;; Don't show the frame name; that's redundant.
591 (setq-default mode-line-buffer-identification '("Emacs: %12b"))
593 ;;; Set to a system sound if you want a fancy bell.
594 (set-message-beep 'ok)
596 ;; Remap some functions to call win32 common dialogs
598 (defun internal-face-interactive (what &optional bool)
599 (let* ((fn (intern (concat "face-" what)))
600 (prompt (concat "Set " what " of face"))
601 (face (read-face-name (concat prompt ": ")))
602 (default (if (fboundp fn)
603 (or (funcall fn face (selected-frame))
604 (funcall fn 'default (selected-frame)))))
605 (fn-win (intern (concat (symbol-name window-system) "-select-" what)))
606 (value
607 (if (fboundp fn-win)
608 (funcall fn-win)
609 (if bool
610 (y-or-n-p (concat "Should face " (symbol-name face)
611 " be " bool "? "))
612 (read-string (concat prompt " " (symbol-name face) " to: ")
613 default)))))
614 (list face (if (equal value "") nil value))))
616 ;; Redefine the font selection to use the Win32 dialog
618 (defun mouse-set-font (&rest fonts)
619 (interactive)
620 (set-default-font (win32-select-font)))
622 ;;; win32-win.el ends here