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