(save-match-data): Use save-match-data-internal
[emacs.git] / lisp / term / w32-win.el
blob559075cf3135cf21bfa7c617f0cee57864b75569
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 ;; 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 win32-handle-scroll-bar-event (event)
85 "Handle Win32 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 win32-handle-scroll-bar-event (event) (interactive "e") (princ event))
112 (global-set-key [vertical-scroll-bar mouse-1] 'win32-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 ((all-colors x-colors)
454 (this-color nil)
455 (defined-colors nil))
456 (while all-colors
457 (setq this-color (car all-colors)
458 all-colors (cdr all-colors))
459 (and (face-color-supported-p frame this-color t)
460 (setq defined-colors (cons this-color defined-colors))))
461 defined-colors))
463 ;;;; Function keys
465 (defun iconify-or-deiconify-frame ()
466 "Iconify the selected frame, or deiconify if it's currently an icon."
467 (interactive)
468 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
469 (iconify-frame)
470 (make-frame-visible)))
472 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
473 global-map)
475 ;; Map certain keypad keys into ASCII characters
476 ;; that people usually expect.
477 (define-key function-key-map [tab] [?\t])
478 (define-key function-key-map [linefeed] [?\n])
479 (define-key function-key-map [clear] [11])
480 (define-key function-key-map [return] [13])
481 (define-key function-key-map [escape] [?\e])
482 (define-key function-key-map [M-tab] [?\M-\t])
483 (define-key function-key-map [M-linefeed] [?\M-\n])
484 (define-key function-key-map [M-clear] [?\M-\013])
485 (define-key function-key-map [M-return] [?\M-\015])
486 (define-key function-key-map [M-escape] [?\M-\e])
488 ;; These don't do the right thing (voelker)
489 ;(define-key function-key-map [backspace] [127])
490 ;(define-key function-key-map [delete] [127])
491 ;(define-key function-key-map [M-backspace] [?\M-\d])
492 ;(define-key function-key-map [M-delete] [?\M-\d])
494 ;; These tell read-char how to convert
495 ;; these special chars to ASCII.
496 (put 'tab 'ascii-character ?\t)
497 (put 'linefeed 'ascii-character ?\n)
498 (put 'clear 'ascii-character 12)
499 (put 'return 'ascii-character 13)
500 (put 'escape 'ascii-character ?\e)
501 ;; These don't seem to be necessary (voelker)
502 ;(put 'backspace 'ascii-character 127)
503 ;(put 'delete 'ascii-character 127)
506 ;;;; Selections and cut buffers
508 ;;; We keep track of the last text selected here, so we can check the
509 ;;; current selection against it, and avoid passing back our own text
510 ;;; from x-cut-buffer-or-selection-value.
511 (defvar x-last-selected-text nil)
513 ;;; It is said that overlarge strings are slow to put into the cut buffer.
514 ;;; Note this value is overridden below.
515 (defvar x-cut-buffer-max 20000
516 "Max number of characters to put in the cut buffer.")
518 (defvar x-select-enable-clipboard t
519 "Non-nil means cutting and pasting uses the clipboard.
520 This is in addition to the primary selection.")
522 (defun x-select-text (text &optional push)
523 (if x-select-enable-clipboard
524 (win32-set-clipboard-data text))
525 (setq x-last-selected-text text))
527 ;;; Return the value of the current selection.
528 ;;; Consult the selection, then the cut buffer. Treat empty strings
529 ;;; as if they were unset.
530 (defun x-get-selection-value ()
531 (if x-select-enable-clipboard
532 (let (text)
533 ;; Don't die if x-get-selection signals an error.
534 (condition-case c
535 (setq text (win32-get-clipboard-data))
536 (error (message "win32-get-clipboard-data:%s" c)))
537 (if (string= text "") (setq text nil))
538 (cond
539 ((not text) nil)
540 ((eq text x-last-selected-text) nil)
541 ((string= text x-last-selected-text)
542 ;; Record the newer string, so subsequent calls can use the 'eq' test.
543 (setq x-last-selected-text text)
544 nil)
546 (setq x-last-selected-text text))))))
548 ;;; Do the actual Windows setup here; the above code just defines
549 ;;; functions and variables that we use now.
551 (setq command-line-args (x-handle-args command-line-args))
553 ;;; Make sure we have a valid resource name.
554 (or (stringp x-resource-name)
555 (let (i)
556 (setq x-resource-name (invocation-name))
558 ;; Change any . or * characters in x-resource-name to hyphens,
559 ;; so as not to choke when we use it in X resource queries.
560 (while (setq i (string-match "[.*]" x-resource-name))
561 (aset x-resource-name i ?-))))
563 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
564 ;; the same lisp directory, don't pass the third argument unless we seem
565 ;; to have the multi-display support.
566 (if (fboundp 'x-close-connection)
567 (x-open-connection ""
568 x-command-line-resources
569 ;; Exit Emacs with fatal error if this fails.
571 (x-open-connection ""
572 x-command-line-resources))
574 (setq frame-creation-function 'x-create-frame-with-faces)
576 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
577 x-cut-buffer-max))
579 ;; Win32 expects the menu bar cut and paste commands to use the clipboard.
580 ;; This has ,? to match both on Sunos and on Solaris.
581 (menu-bar-enable-clipboard)
583 ;; Apply a geometry resource to the initial frame. Put it at the end
584 ;; of the alist, so that anything specified on the command line takes
585 ;; precedence.
586 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
587 parsed)
588 (if res-geometry
589 (progn
590 (setq parsed (x-parse-geometry res-geometry))
591 ;; If the resource specifies a position,
592 ;; call the position and size "user-specified".
593 (if (or (assq 'top parsed) (assq 'left parsed))
594 (setq parsed (cons '(user-position . t)
595 (cons '(user-size . t) parsed))))
596 ;; All geometry parms apply to the initial frame.
597 (setq initial-frame-alist (append initial-frame-alist parsed))
598 ;; The size parms apply to all frames.
599 (if (assq 'height parsed)
600 (setq default-frame-alist
601 (cons (cons 'height (cdr (assq 'height parsed)))
602 default-frame-alist)))
603 (if (assq 'width parsed)
604 (setq default-frame-alist
605 (cons (cons 'width (cdr (assq 'width parsed)))
606 default-frame-alist))))))
608 ;; Check the reverseVideo resource.
609 (let ((case-fold-search t))
610 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
611 (if (and rv
612 (string-match "^\\(true\\|yes\\|on\\)$" rv))
613 (setq default-frame-alist
614 (cons '(reverse . t) default-frame-alist)))))
616 ;; Set x-selection-timeout, measured in milliseconds.
617 (let ((res-selection-timeout
618 (x-get-resource "selectionTimeout" "SelectionTimeout")))
619 (setq x-selection-timeout 20000)
620 (if res-selection-timeout
621 (setq x-selection-timeout (string-to-number res-selection-timeout))))
623 (defun x-win-suspend-error ()
624 (error "Suspending an emacs running under Win32 makes no sense"))
625 (add-hook 'suspend-hook 'x-win-suspend-error)
627 ;;; Arrange for the kill and yank functions to set and check the clipboard.
628 (setq interprogram-cut-function 'x-select-text)
629 (setq interprogram-paste-function 'x-get-selection-value)
631 ;;; Turn off window-splitting optimization; win32 is usually fast enough
632 ;;; that this is only annoying.
633 (setq split-window-keep-point t)
635 ;; Don't show the frame name; that's redundant.
636 (setq-default mode-line-buffer-identification '("Emacs: %12b"))
638 ;;; Set to a system sound if you want a fancy bell.
639 (set-message-beep 'ok)
641 ;; Remap some functions to call win32 common dialogs
643 (defun internal-face-interactive (what &optional bool)
644 (let* ((fn (intern (concat "face-" what)))
645 (prompt (concat "Set " what " of face"))
646 (face (read-face-name (concat prompt ": ")))
647 (default (if (fboundp fn)
648 (or (funcall fn face (selected-frame))
649 (funcall fn 'default (selected-frame)))))
650 (fn-win (intern (concat (symbol-name window-system) "-select-" what)))
651 (value
652 (if (fboundp fn-win)
653 (funcall fn-win)
654 (if bool
655 (y-or-n-p (concat "Should face " (symbol-name face)
656 " be " bool "? "))
657 (read-string (concat prompt " " (symbol-name face) " to: ")
658 default)))))
659 (list face (if (equal value "") nil value))))
661 ;; Redefine the font selection to use the Win32 dialog
663 (defun mouse-set-font (&rest fonts)
664 (interactive)
665 (set-default-font (win32-select-font)))
667 ;;; win32-win.el ends here