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