Fix case of "GNUstep".
[bpt/emacs.git] / lisp / term / ns-win.el
CommitLineData
c0642f6d
GM
1;;; ns-win.el --- lisp side of interface with NeXT/Open/GNUstep/MacOS X window system
2
a5e1066d
GM
3;; Copyright (C) 1993, 1994, 2005, 2006, 2007, 2008
4;; Free Software Foundation, Inc.
c0642f6d 5
82a330df
CY
6;; Authors: Carl Edman, Christian Limpach, Scott Bender,
7;; Christophe de Dinechin, Adrian Robert
c0642f6d
GM
8;; Keywords: terminals
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
edfda783
AR
24
25;;; Commentary:
26
a5a1b464
CY
27;; ns-win.el: this file is loaded from ../lisp/startup.el when it
28;; recognizes that Nextstep windows are to be used. Command line
29;; switches are parsed and those pertaining to Nextstep are processed
30;; and removed from the command line. The Nextstep display is opened
31;; and hooks are set for popping up the initial window.
edfda783
AR
32
33;; startup.el will then examine startup files, and eventually call the hooks
34;; which create the first window (s).
35
a5a1b464
CY
36;; A number of other Nextstep convenience functions are defined in
37;; this file, which works in close coordination with src/nsfns.m.
edfda783
AR
38
39;;; Code:
40
41
601fb9b8 42(if (not (featurep 'ns))
3dcdb6ea 43 (error "%s: Loading ns-win.el but not compiled for GNUstep/MacOS"
edfda783
AR
44 (invocation-name)))
45
ebe68042
SM
46(eval-when-compile (require 'cl))
47
edfda783
AR
48;; Documentation-purposes only: actually loaded in loadup.el
49(require 'frame)
50(require 'mouse)
51(require 'faces)
52(require 'easymenu)
53(require 'menu-bar)
54(require 'fontset)
55
ebe68042
SM
56;; Not needed?
57;;(require 'ispell)
edfda783 58
c0642f6d
GM
59;; nsterm.m
60(defvar ns-version-string)
61(defvar ns-expand-space)
c0642f6d
GM
62(defvar ns-alternate-modifier)
63
edfda783
AR
64;;;; Command line argument handling.
65
66(defvar ns-invocation-args nil)
67(defvar ns-command-line-resources nil)
68
69;; Handler for switches of the form "-switch value" or "-switch".
d377ef4a 70(defun ns-handle-switch (switch &optional numeric)
edfda783
AR
71 (let ((aelt (assoc switch command-line-ns-option-alist)))
72 (if aelt
d377ef4a
GM
73 (setq default-frame-alist
74 (cons (cons (nth 3 aelt)
75 (if numeric
76 (string-to-number (pop ns-invocation-args))
77 (or (nth 4 aelt) (pop ns-invocation-args))))
78 default-frame-alist)))))
edfda783
AR
79
80;; Handler for switches of the form "-switch n"
81(defun ns-handle-numeric-switch (switch)
d377ef4a 82 (ns-handle-switch switch t))
edfda783
AR
83
84;; Make -iconic apply only to the initial frame!
85(defun ns-handle-iconic (switch)
86 (setq initial-frame-alist
87 (cons '(visibility . icon) initial-frame-alist)))
88
82a330df 89;; Handle the -name option, set the name of the initial frame.
edfda783
AR
90(defun ns-handle-name-switch (switch)
91 (or (consp ns-invocation-args)
92 (error "%s: missing argument to `%s' option" (invocation-name) switch))
d377ef4a
GM
93 (setq initial-frame-alist (cons (cons 'name (pop ns-invocation-args))
94 initial-frame-alist)))
95
96;; Set (but not used?) in frame.el.
9e50ff0c 97(defvar x-display-name nil
a5a1b464 98 "The name of the Nextstep display on which Emacs was started.")
edfda783 99
c0642f6d
GM
100;; nsterm.m.
101(defvar ns-input-file)
102
edfda783
AR
103(defun ns-handle-nxopen (switch)
104 (setq unread-command-events (append unread-command-events '(ns-open-file))
d377ef4a 105 ns-input-file (append ns-input-file (list (pop ns-invocation-args)))))
edfda783
AR
106
107(defun ns-handle-nxopentemp (switch)
d377ef4a
GM
108 (setq unread-command-events (append unread-command-events
109 '(ns-open-temp-file))
110 ns-input-file (append ns-input-file (list (pop ns-invocation-args)))))
edfda783 111
edfda783
AR
112(defun ns-ignore-1-arg (switch)
113 (setq ns-invocation-args (cdr ns-invocation-args)))
114(defun ns-ignore-2-arg (switch)
115 (setq ns-invocation-args (cddr ns-invocation-args)))
116
117(defun ns-handle-args (args)
a5a1b464 118 "Process Nextstep-related command line options.
82a330df 119This is run before the user's startup file is loaded.
a5a1b464
CY
120The options in ARGS are copied to `ns-invocation-args'.
121The Nextstep-related settings are then applied using the handlers
82a330df 122defined in `command-line-ns-option-alist'.
a5a1b464 123The return value is ARGS minus the number of arguments processed."
edfda783
AR
124 ;; We use ARGS to accumulate the args that we don't handle here, to return.
125 (setq ns-invocation-args args
126 args nil)
127 (while ns-invocation-args
d377ef4a 128 (let* ((this-switch (pop ns-invocation-args))
edfda783
AR
129 (orig-this-switch this-switch)
130 completion argval aelt handler)
edfda783
AR
131 ;; Check for long options with attached arguments
132 ;; and separate out the attached option argument into argval.
133 (if (string-match "^--[^=]*=" this-switch)
134 (setq argval (substring this-switch (match-end 0))
135 this-switch (substring this-switch 0 (1- (match-end 0)))))
136 ;; Complete names of long options.
137 (if (string-match "^--" this-switch)
138 (progn
139 (setq completion (try-completion this-switch
140 command-line-ns-option-alist))
141 (if (eq completion t)
142 ;; Exact match for long option.
143 nil
144 (if (stringp completion)
145 (let ((elt (assoc completion command-line-ns-option-alist)))
146 ;; Check for abbreviated long option.
147 (or elt
148 (error "Option `%s' is ambiguous" this-switch))
149 (setq this-switch completion))))))
150 (setq aelt (assoc this-switch command-line-ns-option-alist))
151 (if aelt (setq handler (nth 2 aelt)))
152 (if handler
153 (if argval
154 (let ((ns-invocation-args
155 (cons argval ns-invocation-args)))
156 (funcall handler this-switch))
157 (funcall handler this-switch))
158 (setq args (cons orig-this-switch args)))))
159 (nreverse args))
160
489382c5 161(defun ns-parse-geometry (geom)
ba0c843d 162 "Parse a Nextstep-style geometry string GEOM.
edfda783
AR
163Returns an alist of the form ((top . TOP), (left . LEFT) ... ).
164The properties returned may include `top', `left', `height', and `width'."
a5a1b464
CY
165 (when (string-match "\\([0-9]+\\)\\( \\([0-9]+\\)\\( \\([0-9]+\\)\
166\\( \\([0-9]+\\) ?\\)?\\)?\\)?"
167 geom)
168 (apply
169 'append
170 (list
171 (list (cons 'top (string-to-number (match-string 1 geom))))
172 (if (match-string 3 geom)
173 (list (cons 'left (string-to-number (match-string 3 geom)))))
174 (if (match-string 5 geom)
175 (list (cons 'height (string-to-number (match-string 5 geom)))))
176 (if (match-string 7 geom)
177 (list (cons 'width (string-to-number (match-string 7 geom)))))))))
edfda783
AR
178
179;;;; Keyboard mapping.
180
6742a9d2
AR
181;; These tell read-char how to convert these special chars to ASCII.
182;;TODO: all terms have these, and at least the return mapping is necessary
183;; for tramp to recognize the enter key.
184;; Perhaps they should be moved into common code somewhere
185;; (when a window system is active).
55e8d9a5 186;; Remove if no problems for some time after 2008-08-06.
6742a9d2
AR
187(put 'backspace 'ascii-character 127)
188(put 'delete 'ascii-character 127)
189(put 'tab 'ascii-character ?\t)
190(put 'S-tab 'ascii-character (logior 16 ?\t))
191(put 'linefeed 'ascii-character ?\n)
192(put 'clear 'ascii-character 12)
193(put 'return 'ascii-character 13)
194(put 'escape 'ascii-character ?\e)
55e8d9a5
AR
195
196
197(defvar ns-alternatives-map
198 (let ((map (make-sparse-keymap)))
199 ;; Map certain keypad keys into ASCII characters
200 ;; that people usually expect.
201 (define-key map [backspace] [?\d])
202 (define-key map [delete] [?\d])
203 (define-key map [tab] [?\t])
204 (define-key map [S-tab] [25])
205 (define-key map [linefeed] [?\n])
206 (define-key map [clear] [?\C-l])
207 (define-key map [return] [?\C-m])
208 (define-key map [escape] [?\e])
209 (define-key map [M-backspace] [?\M-\d])
210 (define-key map [M-delete] [?\M-\d])
211 (define-key map [M-tab] [?\M-\t])
212 (define-key map [M-linefeed] [?\M-\n])
213 (define-key map [M-clear] [?\M-\C-l])
214 (define-key map [M-return] [?\M-\C-m])
215 (define-key map [M-escape] [?\M-\e])
216 map)
217 "Keymap of alternative meanings for some keys under NS.")
edfda783 218
a5a1b464 219;; Here are some Nextstep-like bindings for command key sequences.
edfda783
AR
220(define-key global-map [?\s-,] 'ns-popup-prefs-panel)
221(define-key global-map [?\s-'] 'next-multiframe-window)
222(define-key global-map [?\s-`] 'other-frame)
223(define-key global-map [?\s--] 'center-line)
224(define-key global-map [?\s-:] 'ispell)
225(define-key global-map [?\s-\;] 'ispell-next)
226(define-key global-map [?\s-?] 'info)
227(define-key global-map [?\s-^] 'kill-some-buffers)
228(define-key global-map [?\s-&] 'kill-this-buffer)
229(define-key global-map [?\s-C] 'ns-popup-color-panel)
230(define-key global-map [?\s-D] 'dired)
231(define-key global-map [?\s-E] 'edit-abbrevs)
232(define-key global-map [?\s-L] 'shell-command)
233(define-key global-map [?\s-M] 'manual-entry)
234(define-key global-map [?\s-S] 'ns-write-file-using-panel)
235(define-key global-map [?\s-a] 'mark-whole-buffer)
236(define-key global-map [?\s-c] 'ns-copy-including-secondary)
237(define-key global-map [?\s-d] 'isearch-repeat-backward)
238(define-key global-map [?\s-e] 'isearch-yank-kill)
239(define-key global-map [?\s-f] 'isearch-forward)
240(define-key global-map [?\s-g] 'isearch-repeat-forward)
241(define-key global-map [?\s-h] 'ns-do-hide-emacs)
242(define-key global-map [?\s-H] 'ns-do-hide-others)
243(define-key global-map [?\s-j] 'exchange-point-and-mark)
244(define-key global-map [?\s-k] 'kill-this-buffer)
245(define-key global-map [?\s-l] 'goto-line)
246(define-key global-map [?\s-m] 'iconify-frame)
247(define-key global-map [?\s-n] 'make-frame)
248(define-key global-map [?\s-o] 'ns-open-file-using-panel)
249(define-key global-map [?\s-p] 'ns-print-buffer)
250(define-key global-map [?\s-q] 'save-buffers-kill-emacs)
251(define-key global-map [?\s-s] 'save-buffer)
252(define-key global-map [?\s-t] 'ns-popup-font-panel)
253(define-key global-map [?\s-u] 'revert-buffer)
254(define-key global-map [?\s-v] 'yank)
255(define-key global-map [?\s-w] 'delete-frame)
256(define-key global-map [?\s-x] 'kill-region)
257(define-key global-map [?\s-y] 'ns-paste-secondary)
258(define-key global-map [?\s-z] 'undo)
259(define-key global-map [?\s-|] 'shell-command-on-region)
260(define-key global-map [s-kp-bar] 'shell-command-on-region)
ebe68042 261;; (as in Terminal.app)
edfda783
AR
262(define-key global-map [s-right] 'ns-next-frame)
263(define-key global-map [s-left] 'ns-prev-frame)
264
265(define-key global-map [home] 'beginning-of-buffer)
266(define-key global-map [end] 'end-of-buffer)
267(define-key global-map [kp-home] 'beginning-of-buffer)
268(define-key global-map [kp-end] 'end-of-buffer)
269(define-key global-map [kp-prior] 'scroll-down)
270(define-key global-map [kp-next] 'scroll-up)
271
55e8d9a5
AR
272;;; Allow shift-clicks to work similarly to under Nextstep
273(define-key global-map [S-mouse-1] 'mouse-save-then-kill)
274(global-unset-key [S-down-mouse-1])
275
edfda783 276
a5a1b464 277;; Special Nextstep-generated events are converted to function keys. Here
edfda783
AR
278;; are the bindings for them.
279(define-key global-map [ns-power-off]
ebe68042 280 (lambda () (interactive) (save-buffers-kill-emacs t)))
edfda783
AR
281(define-key global-map [ns-open-file] 'ns-find-file)
282(define-key global-map [ns-open-temp-file] [ns-open-file])
283(define-key global-map [ns-drag-file] 'ns-insert-file)
284(define-key global-map [ns-drag-color] 'ns-set-foreground-at-mouse)
285(define-key global-map [S-ns-drag-color] 'ns-set-background-at-mouse)
286(define-key global-map [ns-drag-text] 'ns-insert-text)
287(define-key global-map [ns-change-font] 'ns-respond-to-change-font)
288(define-key global-map [ns-open-file-line] 'ns-open-file-select-line)
289(define-key global-map [ns-insert-working-text] 'ns-insert-working-text)
290(define-key global-map [ns-delete-working-text] 'ns-delete-working-text)
291(define-key global-map [ns-spi-service-call] 'ns-spi-service-call)
4e622592 292(define-key global-map [ns-new-frame] 'make-frame)
edfda783
AR
293
294
295
b90cc058 296;; Functions to set environment variables by running a subshell.
a5a1b464
CY
297;;; Idea based on Nextstep 4.2 distribution, this version of code
298;;; based on mac-read-environment-vars-from-shell () by David Reitter.
b90cc058
CY
299;;; Mostly used only under ns-extended-platform-support-mode.
300
301(defun ns-make-command-string (cmdlist)
a5e1066d 302 (mapconcat 'identity cmdlist " ; "))
b90cc058
CY
303
304;;;###autoload
305(defun ns-grabenv (&optional shell-path startup)
306 "Set the Emacs environment using the output of a shell command.
307This runs a shell subprocess, and interpret its output as a
308series of environment variables to insert into the emacs
309environment.
310SHELL-PATH gives the path to the shell; if nil, this defaults to
311the current setting of `shell-file-name'.
312STARTUP is a list of commands for the shell to execute; if nil,
313this defaults to \"printenv\"."
314 (interactive)
315 (with-temp-buffer
316 (let ((shell-file-name (if shell-path shell-path shell-file-name))
317 (cmd (ns-make-command-string (if startup startup '("printenv")))))
318 (shell-command cmd t)
319 (while (search-forward-regexp "^\\([A-Za-z_0-9]+\\)=\\(.*\\)$" nil t)
320 (setenv (match-string 1)
321 (if (equal (match-string 1) "PATH")
322 (concat (getenv "PATH") ":" (match-string 2))
323 (match-string 2)))))))
2f93961f
CY
324
325;; Set up a number of aliases and other layers to pretend we're using
326;; the Choi/Mitsuharu Carbon port.
327
328(defvaralias 'mac-allow-anti-aliasing 'ns-antialias-text)
329(defvaralias 'mac-command-modifier 'ns-command-modifier)
330(defvaralias 'mac-control-modifier 'ns-control-modifier)
331(defvaralias 'mac-option-modifier 'ns-option-modifier)
332(defvaralias 'mac-function-modifier 'ns-function-modifier)
583ff3c3
AR
333(defalias 'do-applescript 'ns-do-applescript)
334
edfda783 335
c0642f6d
GM
336(defvar menu-bar-ns-file-menu) ; below
337
a5a1b464
CY
338;; Toggle some additional Nextstep-like features that may interfere
339;; with users' expectations coming from emacs on other platforms.
edfda783 340(define-minor-mode ns-extended-platform-support-mode
a5a1b464 341 "Toggle Nextstep extended platform support features.
edfda783 342 When this mode is active (no modeline indicator):
38f4308d 343 - File menu is altered slightly in keeping with conventions.
38f4308d
AR
344 - Screen position is preserved in scrolling.
345 - Transient mark mode is activated"
edfda783
AR
346 :init-value nil
347 :global t
348 :group 'ns
349 (if ns-extended-platform-support-mode
350 (progn
7f192970
AR
351 (defun ns-show-manual () "Show Emacs.app manual" (interactive) (info "ns-emacs"))
352 (setq where-is-preferred-modifier 'super)
ebe68042
SM
353 (setq scroll-preserve-screen-position t)
354 (transient-mark-mode 1)
355
a5a1b464
CY
356 ;; Change file menu to simplify and add a couple of
357 ;; Nextstep-specific items
ebe68042
SM
358 (easy-menu-remove-item global-map '("menu-bar") 'file)
359 (easy-menu-add-item global-map '(menu-bar)
7f192970
AR
360 (cons "File" menu-bar-ns-file-menu) 'edit)
361 (define-key menu-bar-help-menu [ns-manual]
362 '(menu-item "Emacs.app Manual" ns-show-manual)))
edfda783 363 (progn
ebe68042 364 ;; Undo everything above.
7f192970
AR
365 (fmakunbound 'ns-show-manual)
366 (setq where-is-preferred-modifier 'nil)
ebe68042
SM
367 (setq scroll-preserve-screen-position nil)
368 (transient-mark-mode 0)
369 (easy-menu-remove-item global-map '("menu-bar") 'file)
370 (easy-menu-add-item global-map '(menu-bar)
7f192970
AR
371 (cons "File" menu-bar-file-menu) 'edit)
372 (easy-menu-remove-item global-map '("menu-bar" "help-menu") 'ns-manual)
373)))
edfda783
AR
374
375
376(defun x-setup-function-keys (frame)
a5a1b464 377 "Set up function Keys for Nextstep for frame FRAME."
edfda783
AR
378 (unless (terminal-parameter frame 'x-setup-function-keys)
379 (with-selected-frame frame
9e50ff0c
DN
380 (setq interprogram-cut-function 'x-select-text
381 interprogram-paste-function 'x-cut-buffer-or-selection-value)
55e8d9a5
AR
382 (let ((map (copy-keymap ns-alternatives-map)))
383 (set-keymap-parent map (keymap-parent local-function-key-map))
384 (set-keymap-parent local-function-key-map map))
ebe68042
SM
385 (setq system-key-alist
386 (list
387 (cons (logior (lsh 0 16) 1) 'ns-power-off)
388 (cons (logior (lsh 0 16) 2) 'ns-open-file)
389 (cons (logior (lsh 0 16) 3) 'ns-open-temp-file)
390 (cons (logior (lsh 0 16) 4) 'ns-drag-file)
391 (cons (logior (lsh 0 16) 5) 'ns-drag-color)
392 (cons (logior (lsh 0 16) 6) 'ns-drag-text)
393 (cons (logior (lsh 0 16) 7) 'ns-change-font)
394 (cons (logior (lsh 0 16) 8) 'ns-open-file-line)
395 (cons (logior (lsh 0 16) 9) 'ns-insert-working-text)
396 (cons (logior (lsh 0 16) 10) 'ns-delete-working-text)
397 (cons (logior (lsh 0 16) 11) 'ns-spi-service-call)
4e622592 398 (cons (logior (lsh 0 16) 12) 'ns-new-frame)
ebe68042
SM
399 (cons (logior (lsh 1 16) 32) 'f1)
400 (cons (logior (lsh 1 16) 33) 'f2)
401 (cons (logior (lsh 1 16) 34) 'f3)
402 (cons (logior (lsh 1 16) 35) 'f4)
403 (cons (logior (lsh 1 16) 36) 'f5)
404 (cons (logior (lsh 1 16) 37) 'f6)
405 (cons (logior (lsh 1 16) 38) 'f7)
406 (cons (logior (lsh 1 16) 39) 'f8)
407 (cons (logior (lsh 1 16) 40) 'f9)
408 (cons (logior (lsh 1 16) 41) 'f10)
409 (cons (logior (lsh 1 16) 42) 'f11)
410 (cons (logior (lsh 1 16) 43) 'f12)
411 (cons (logior (lsh 1 16) 44) 'kp-insert)
412 (cons (logior (lsh 1 16) 45) 'kp-delete)
413 (cons (logior (lsh 1 16) 46) 'kp-home)
414 (cons (logior (lsh 1 16) 47) 'kp-end)
415 (cons (logior (lsh 1 16) 48) 'kp-prior)
416 (cons (logior (lsh 1 16) 49) 'kp-next)
417 (cons (logior (lsh 1 16) 50) 'print-screen)
418 (cons (logior (lsh 1 16) 51) 'scroll-lock)
419 (cons (logior (lsh 1 16) 52) 'pause)
420 (cons (logior (lsh 1 16) 53) 'system)
421 (cons (logior (lsh 1 16) 54) 'break)
422 (cons (logior (lsh 1 16) 56) 'please-tell-carl-what-this-key-is-called-56)
423 (cons (logior (lsh 1 16) 61) 'please-tell-carl-what-this-key-is-called-61)
424 (cons (logior (lsh 1 16) 62) 'please-tell-carl-what-this-key-is-called-62)
425 (cons (logior (lsh 1 16) 63) 'please-tell-carl-what-this-key-is-called-63)
426 (cons (logior (lsh 1 16) 64) 'please-tell-carl-what-this-key-is-called-64)
427 (cons (logior (lsh 1 16) 69) 'please-tell-carl-what-this-key-is-called-69)
428 (cons (logior (lsh 1 16) 70) 'please-tell-carl-what-this-key-is-called-70)
429 (cons (logior (lsh 1 16) 71) 'please-tell-carl-what-this-key-is-called-71)
430 (cons (logior (lsh 1 16) 72) 'please-tell-carl-what-this-key-is-called-72)
431 (cons (logior (lsh 1 16) 73) 'please-tell-carl-what-this-key-is-called-73)
432 (cons (logior (lsh 2 16) 3) 'kp-enter)
433 (cons (logior (lsh 2 16) 9) 'kp-tab)
434 (cons (logior (lsh 2 16) 28) 'kp-quit)
435 (cons (logior (lsh 2 16) 35) 'kp-hash)
436 (cons (logior (lsh 2 16) 42) 'kp-multiply)
437 (cons (logior (lsh 2 16) 43) 'kp-add)
438 (cons (logior (lsh 2 16) 44) 'kp-separator)
439 (cons (logior (lsh 2 16) 45) 'kp-subtract)
440 (cons (logior (lsh 2 16) 46) 'kp-decimal)
441 (cons (logior (lsh 2 16) 47) 'kp-divide)
442 (cons (logior (lsh 2 16) 48) 'kp-0)
443 (cons (logior (lsh 2 16) 49) 'kp-1)
444 (cons (logior (lsh 2 16) 50) 'kp-2)
445 (cons (logior (lsh 2 16) 51) 'kp-3)
446 (cons (logior (lsh 2 16) 52) 'kp-4)
447 (cons (logior (lsh 2 16) 53) 'kp-5)
448 (cons (logior (lsh 2 16) 54) 'kp-6)
449 (cons (logior (lsh 2 16) 55) 'kp-7)
450 (cons (logior (lsh 2 16) 56) 'kp-8)
451 (cons (logior (lsh 2 16) 57) 'kp-9)
452 (cons (logior (lsh 2 16) 60) 'kp-less)
453 (cons (logior (lsh 2 16) 61) 'kp-equal)
454 (cons (logior (lsh 2 16) 62) 'kp-more)
455 (cons (logior (lsh 2 16) 64) 'kp-at)
456 (cons (logior (lsh 2 16) 92) 'kp-backslash)
457 (cons (logior (lsh 2 16) 96) 'kp-backtick)
458 (cons (logior (lsh 2 16) 124) 'kp-bar)
459 (cons (logior (lsh 2 16) 126) 'kp-tilde)
460 (cons (logior (lsh 2 16) 157) 'kp-mu)
461 (cons (logior (lsh 2 16) 165) 'kp-yen)
462 (cons (logior (lsh 2 16) 167) 'kp-paragraph)
463 (cons (logior (lsh 2 16) 172) 'left)
464 (cons (logior (lsh 2 16) 173) 'up)
465 (cons (logior (lsh 2 16) 174) 'right)
466 (cons (logior (lsh 2 16) 175) 'down)
467 (cons (logior (lsh 2 16) 176) 'kp-ring)
468 (cons (logior (lsh 2 16) 201) 'kp-square)
469 (cons (logior (lsh 2 16) 204) 'kp-cube)
470 (cons (logior (lsh 3 16) 8) 'backspace)
471 (cons (logior (lsh 3 16) 9) 'tab)
472 (cons (logior (lsh 3 16) 10) 'linefeed)
473 (cons (logior (lsh 3 16) 11) 'clear)
474 (cons (logior (lsh 3 16) 13) 'return)
475 (cons (logior (lsh 3 16) 18) 'pause)
476 (cons (logior (lsh 3 16) 25) 'S-tab)
477 (cons (logior (lsh 3 16) 27) 'escape)
478 (cons (logior (lsh 3 16) 127) 'delete)
55e8d9a5
AR
479 )))
480 (set-terminal-parameter frame 'x-setup-function-keys t)))
edfda783
AR
481
482
483
ebe68042 484;; Must come after keybindings.
edfda783
AR
485
486(fmakunbound 'clipboard-yank)
487(fmakunbound 'clipboard-kill-ring-save)
488(fmakunbound 'clipboard-kill-region)
489(fmakunbound 'menu-bar-enable-clipboard)
490
491;; Add a couple of menus and rearrange some others; easiest just to redo toplvl
492;; Note keymap defns must be given last-to-first
493(define-key global-map [menu-bar] (make-sparse-keymap "menu-bar"))
494
ebe68042
SM
495(setq menu-bar-final-items
496 (cond ((eq system-type 'darwin)
497 '(buffer windows services help-menu))
498 ;; Otherwise, GNUstep.
499 (t
500 '(buffer windows services hide-app quit))))
edfda783 501
ebe68042
SM
502;; Add standard top-level items to GNUstep menu.
503(unless (eq system-type 'darwin)
504 (define-key global-map [menu-bar quit] '("Quit" . save-buffers-kill-emacs))
505 (define-key global-map [menu-bar hide-app] '("Hide" . ns-do-hide-emacs)))
edfda783
AR
506
507(define-key global-map [menu-bar services]
508 (cons "Services" (make-sparse-keymap "Services")))
509(define-key global-map [menu-bar windows] (make-sparse-keymap "Windows"))
510(define-key global-map [menu-bar buffer]
511 (cons "Buffers" global-buffers-menu-map))
512;; (cons "Buffers" (make-sparse-keymap "Buffers")))
513(define-key global-map [menu-bar tools] (cons "Tools" menu-bar-tools-menu))
514(define-key global-map [menu-bar options] (cons "Options" menu-bar-options-menu))
515(define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
516(define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu))
517
518;; If running under GNUstep, rename "Help" to "Info"
519(cond ((eq system-type 'darwin)
520 (define-key global-map [menu-bar help-menu]
521 (cons "Help" menu-bar-help-menu)))
522 (t
523 (let ((contents (reverse (cdr menu-bar-help-menu))))
524 (setq menu-bar-help-menu
525 (append (list 'keymap) (cdr contents) (list "Info"))))
526 (define-key global-map [menu-bar help-menu]
527 (cons "Info" menu-bar-help-menu))))
528
edfda783
AR
529(if (not (eq system-type 'darwin))
530 ;; in OS X it's in the app menu already
531 (define-key menu-bar-help-menu [info-panel]
532 '("About Emacs..." . ns-do-emacs-info-panel)))
533
534
535;;;; File menu, replaces standard under ns-extended-platform-support
536(defvar menu-bar-ns-file-menu (make-sparse-keymap "File"))
537(define-key menu-bar-ns-file-menu [one-window]
538 '("Remove Splits" . delete-other-windows))
539(define-key menu-bar-ns-file-menu [split-window]
540 '("Split Window" . split-window-vertically))
541
542(define-key menu-bar-ns-file-menu [separator-print] '("--"))
543
544(defvar ns-ps-print-menu-map (make-sparse-keymap "Postscript Print"))
545(define-key ns-ps-print-menu-map [ps-print-region]
546 '("Region (B+W)" . ps-print-region))
547(define-key ns-ps-print-menu-map [ps-print-buffer]
548 '("Buffer (B+W)" . ps-print-buffer))
549(define-key ns-ps-print-menu-map [ps-print-region-faces]
550 '("Region" . ps-print-region-with-faces))
551(define-key ns-ps-print-menu-map [ps-print-buffer-faces]
c469837a 552 '("Buffer" . ps-print-buffer-with-faces))
edfda783
AR
553(define-key menu-bar-ns-file-menu [postscript-print]
554 (cons "Postscript Print" ns-ps-print-menu-map))
555
556(define-key menu-bar-ns-file-menu [print-region]
557 '("Print Region" . print-region))
558(define-key menu-bar-ns-file-menu [print-buffer]
559 '("Print Buffer" . ns-print-buffer))
560
561(define-key menu-bar-ns-file-menu [separator-save] '("--"))
562
563(define-key menu-bar-ns-file-menu [recover-session]
564 '("Recover Crashed Session" . recover-session))
565(define-key menu-bar-ns-file-menu [revert-buffer]
566 '("Revert Buffer" . revert-buffer))
567(define-key menu-bar-ns-file-menu [write-file]
568 '("Save Buffer As..." . ns-write-file-using-panel))
569(define-key menu-bar-ns-file-menu [save-buffer] '("Save Buffer" . save-buffer))
570
571(define-key menu-bar-ns-file-menu [kill-buffer]
572 '("Kill Current Buffer" . kill-this-buffer))
573(define-key menu-bar-ns-file-menu [delete-this-frame]
574 '("Close Frame" . delete-frame))
575
576(define-key menu-bar-ns-file-menu [separator-open] '("--"))
577
578(define-key menu-bar-ns-file-menu [insert-file]
579 '("Insert File..." . insert-file))
580(define-key menu-bar-ns-file-menu [dired]
581 '("Open Directory..." . ns-open-file-using-panel))
582(define-key menu-bar-ns-file-menu [open-file]
583 '("Open File..." . ns-open-file-using-panel))
584(define-key menu-bar-ns-file-menu [make-frame]
585 '("New Frame" . make-frame))
586
587
588;;;; Edit menu: Modify slightly
589
ebe68042 590;; Substitute a Copy function that works better under X (for GNUstep).
edfda783
AR
591(easy-menu-remove-item global-map '("menu-bar" "edit") 'copy)
592(define-key-after menu-bar-edit-menu [copy]
593 '(menu-item "Copy" ns-copy-including-secondary
ebe68042
SM
594 :enable mark-active
595 :help "Copy text in region between mark and current position")
edfda783
AR
596 'cut)
597
ebe68042
SM
598;; Change to same precondition as select-and-paste, as we don't have
599;; `x-selection-exists-p'.
edfda783
AR
600(easy-menu-remove-item global-map '("menu-bar" "edit") 'paste)
601(define-key-after menu-bar-edit-menu [paste]
602 '(menu-item "Paste" yank
ebe68042
SM
603 :enable (and (cdr yank-menu) (not buffer-read-only))
604 :help "Paste (yank) text most recently cut/copied")
edfda783
AR
605 'copy)
606
ebe68042 607;; Change text to be more consistent with surrounding menu items `paste', etc.
edfda783
AR
608(easy-menu-remove-item global-map '("menu-bar" "edit") 'paste-from-menu)
609(define-key-after menu-bar-edit-menu [select-paste]
610 '(menu-item "Select and Paste" yank-menu
ebe68042
SM
611 :enable (and (cdr yank-menu) (not buffer-read-only))
612 :help "Choose a string from the kill ring and paste it")
edfda783
AR
613 'paste)
614
ebe68042 615;; Separate undo from cut/paste section, add spell for platform consistency.
edfda783
AR
616(define-key-after menu-bar-edit-menu [separator-undo] '("--") 'undo)
617(define-key-after menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map) 'fill)
618
619
620;;;; Windows menu
d377ef4a 621(defun menu-bar-select-frame (&optional frame)
edfda783
AR
622 (interactive)
623 (make-frame-visible last-command-event)
624 (raise-frame last-command-event)
625 (select-frame last-command-event))
626
627(defun menu-bar-update-frames ()
628 ;; If user discards the Windows item, play along.
ebe68042
SM
629 (when (lookup-key (current-global-map) [menu-bar windows])
630 (let ((frames (frame-list))
631 (frames-menu (make-sparse-keymap "Select Frame")))
632 (setcdr frames-menu
633 (nconc
634 (mapcar (lambda (frame)
635 (list* frame
636 (cdr (assq 'name (frame-parameters frame)))
637 'menu-bar-select-frame))
638 frames)
639 (cdr frames-menu)))
640 (define-key frames-menu [separator-frames] '("--"))
641 (define-key frames-menu [popup-color-panel]
642 '("Colors..." . ns-popup-color-panel))
643 (define-key frames-menu [popup-font-panel]
644 '("Font Panel..." . ns-popup-font-panel))
645 (define-key frames-menu [separator-arrange] '("--"))
646 (define-key frames-menu [arrange-all-frames]
647 '("Arrange All Frames" . ns-arrange-all-frames))
648 (define-key frames-menu [arrange-visible-frames]
649 '("Arrange Visible Frames" . ns-arrange-visible-frames))
650 ;; Don't use delete-frame as event name
651 ;; because that is a special event.
652 (define-key (current-global-map) [menu-bar windows]
653 (cons "Windows" frames-menu)))))
edfda783
AR
654
655(defun force-menu-bar-update-buffers ()
656 ;; This is a hack to get around fact that we already checked
657 ;; frame-or-buffer-changed-p and reset it, so menu-bar-update-buffers
658 ;; does not pick up any change.
659 (menu-bar-update-buffers t))
660
661(add-hook 'menu-bar-update-fab-hook 'menu-bar-update-frames)
662(add-hook 'menu-bar-update-fab-hook 'force-menu-bar-update-buffers)
663
664(defun menu-bar-update-frames-and-buffers ()
665 (if (frame-or-buffer-changed-p)
666 (run-hooks 'menu-bar-update-fab-hook)))
667
668(setq menu-bar-update-hook
669 (delq 'menu-bar-update-buffers menu-bar-update-hook))
670(add-hook 'menu-bar-update-hook 'menu-bar-update-frames-and-buffers)
671
672(menu-bar-update-frames-and-buffers)
673
674
675;; ns-arrange functions contributed
676;; by Eberhard Mandler <mandler@dbag.ulm.DaimlerBenz.COM>
677(defun ns-arrange-all-frames ()
678 "Arranges all frames according to topline"
679 (interactive)
680 (ns-arrange-frames t))
681
682(defun ns-arrange-visible-frames ()
683 "Arranges all visible frames according to topline"
684 (interactive)
685 (ns-arrange-frames nil))
686
687(defun ns-arrange-frames ( vis)
688 (let ((frame (next-frame))
689 (end-frame (selected-frame))
690 (inc-x 20) ;relative position of frames
691 (inc-y 22)
692 (x-pos 100) ;start position
693 (y-pos 40)
694 (done nil))
695 (while (not done) ;cycle through all frames
696 (if (not (or vis (eq (frame-visible-p frame) t)))
ebe68042 697 (setq x-pos x-pos); do nothing; true case
edfda783
AR
698 (set-frame-position frame x-pos y-pos)
699 (setq x-pos (+ x-pos inc-x))
700 (setq y-pos (+ y-pos inc-y))
701 (raise-frame frame))
702 (select-frame frame)
703 (setq frame (next-frame))
704 (setq done (equal frame end-frame)))
705 (set-frame-position end-frame x-pos y-pos)
706 (raise-frame frame)
707 (select-frame frame)))
708
709
710;;;; Services
d377ef4a
GM
711(declare-function ns-perform-service "nsfns.m" (service send))
712
edfda783
AR
713(defun ns-define-service (path)
714 (let ((mapping [menu-bar services])
715 (service (mapconcat 'identity path "/"))
716 (name (intern
ebe68042
SM
717 (subst-char-in-string
718 ?\s ?-
719 (mapconcat 'identity (cons "ns-service" path) "-")))))
720 ;; This defines the function.
721 (defalias name
722 (lexical-let ((service service))
723 (lambda (arg)
724 (interactive "p")
725 (let* ((in-string
726 (cond ((stringp arg) arg)
727 (mark-active
728 (buffer-substring (region-beginning) (region-end)))))
729 (out-string (ns-perform-service service in-string)))
730 (cond
731 ((stringp arg) out-string)
732 ((and out-string (or (not in-string)
733 (not (string= in-string out-string))))
734 (if mark-active (delete-region (region-beginning) (region-end)))
735 (insert out-string)
736 (setq deactivate-mark nil)))))))
edfda783
AR
737 (cond
738 ((lookup-key global-map mapping)
739 (while (cdr path)
740 (setq mapping (vconcat mapping (list (intern (car path)))))
741 (if (not (keymapp (lookup-key global-map mapping)))
742 (define-key global-map mapping
743 (cons (car path) (make-sparse-keymap (car path)))))
744 (setq path (cdr path)))
745 (setq mapping (vconcat mapping (list (intern (car path)))))
746 (define-key global-map mapping (cons (car path) name))))
747 name))
748
c0642f6d
GM
749;; nsterm.m
750(defvar ns-input-spi-name)
751(defvar ns-input-spi-arg)
752
f2d9c15f
GM
753(declare-function dnd-open-file "dnd" (uri action))
754
edfda783 755(defun ns-spi-service-call ()
82a330df 756 "Respond to a service request."
edfda783
AR
757 (interactive)
758 (cond ((string-equal ns-input-spi-name "open-selection")
759 (switch-to-buffer (generate-new-buffer "*untitled*"))
760 (insert ns-input-spi-arg))
761 ((string-equal ns-input-spi-name "open-file")
762 (dnd-open-file ns-input-spi-arg nil))
763 ((string-equal ns-input-spi-name "mail-selection")
764 (compose-mail)
765 (rfc822-goto-eoh)
766 (forward-line 1)
767 (insert ns-input-spi-arg))
768 ((string-equal ns-input-spi-name "mail-to")
769 (compose-mail ns-input-spi-arg))
770 (t (error (concat "Service " ns-input-spi-name " not recognized")))))
771
772
773;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
774
775
776
a5a1b464
CY
777;;;; Composed key sequence handling for Nextstep system input methods.
778;;;; (On Nextstep systems, input methods are provided for CJK
779;;;; characters, etc. which require multiple keystrokes, and during
780;;;; entry a partial ("working") result is typically shown in the
781;;;; editing window.)
edfda783
AR
782
783(defface ns-working-text-face
784 '((t :underline t))
785 "Face used to highlight working text during compose sequence insert."
786 :group 'ns)
787
788(defvar ns-working-overlay nil
789 "Overlay used to highlight working text during compose sequence insert.")
790(make-variable-buffer-local 'ns-working-overlay)
791(defvar ns-working-overlay-len 0
792 "Length of working text during compose sequence insert.")
793(make-variable-buffer-local 'ns-working-overlay-len)
794
ebe68042
SM
795;; Based on mac-win.el 2007/08/26 unicode-2. This will fail if called
796;; from an "interactive" function.
edfda783
AR
797(defun ns-in-echo-area ()
798 "Whether, for purposes of inserting working composition text, the minibuffer
799is currently being used."
800 (or isearch-mode
801 (and cursor-in-echo-area (current-message))
802 ;; Overlay strings are not shown in some cases.
803 (get-char-property (point) 'invisible)
804 (and (not (bobp))
805 (or (and (get-char-property (point) 'display)
806 (eq (get-char-property (1- (point)) 'display)
807 (get-char-property (point) 'display)))
808 (and (get-char-property (point) 'composition)
809 (eq (get-char-property (1- (point)) 'composition)
810 (get-char-property (point) 'composition)))))))
811
ebe68042
SM
812;; Currently not used, doesn't work because the 'interactive' here stays
813;; for subinvocations.
edfda783
AR
814(defun ns-insert-working-text ()
815 (interactive)
816 (if (ns-in-echo-area) (ns-echo-working-text) (ns-put-working-text)))
817
c0642f6d
GM
818(defvar ns-working-text) ; nsterm.m
819
edfda783
AR
820(defun ns-put-working-text ()
821 "Insert contents of ns-working-text as UTF8 string and mark with
822ns-working-overlay. Any previously existing working text is cleared first.
823The overlay is assigned the face ns-working-text-face."
824 (interactive)
825 (if ns-working-overlay (ns-delete-working-text))
826 (let ((start (point)))
827 (insert ns-working-text)
828 (overlay-put (setq ns-working-overlay (make-overlay start (point)
829 (current-buffer) nil t))
830 'face 'ns-working-text-face)
831 (setq ns-working-overlay-len (+ ns-working-overlay-len (- (point) start)))))
832
833(defun ns-echo-working-text ()
834 "Echo contents of ns-working-text in message display area.
835See ns-insert-working-text."
836 (if ns-working-overlay (ns-unecho-working-text))
837 (let* ((msg (current-message))
838 (msglen (length msg))
839 message-log-max)
840 (setq ns-working-overlay-len (length ns-working-text))
841 (setq msg (concat msg ns-working-text))
842 (put-text-property msglen (+ msglen ns-working-overlay-len) 'face 'ns-working-text-face msg)
843 (message "%s" msg)
844 (setq ns-working-overlay t)))
845
846(defun ns-delete-working-text()
847 "Delete working text and clear ns-working-overlay."
848 (interactive)
849 (delete-backward-char ns-working-overlay-len)
850 (setq ns-working-overlay-len 0)
851 (delete-overlay ns-working-overlay))
852
853(defun ns-unecho-working-text()
854 "Delete working text from echo area and clear ns-working-overlay."
855 (let ((msg (current-message))
856 message-log-max)
857 (setq msg (substring msg 0 (- (length msg) ns-working-overlay-len)))
858 (setq ns-working-overlay-len 0)
859 (setq ns-working-overlay nil)))
860
861
c0642f6d
GM
862(declare-function ns-convert-utf8-nfd-to-nfc "nsfns.m" (str))
863
edfda783
AR
864;;;; OS X file system Unicode UTF-8 NFD (decomposed form) support
865;; Lisp code based on utf-8m.el, by Seiji Zenitani, Eiji Honjoh, and
866;; Carsten Bormann.
867(if (eq system-type 'darwin)
868 (progn
869
870 (defun ns-utf8-nfd-post-read-conversion (length)
871 "Calls ns-convert-utf8-nfd-to-nfc to compose char sequences."
872 (save-excursion
873 (save-restriction
874 (narrow-to-region (point) (+ (point) length))
875 (let ((str (buffer-string)))
876 (delete-region (point-min) (point-max))
877 (insert (ns-convert-utf8-nfd-to-nfc str))
878 (- (point-max) (point-min))
879 ))))
880
881 (define-coding-system 'utf-8-nfd
882 "UTF-8 NFD (decomposed) encoding."
883 :coding-type 'utf-8
884 :mnemonic ?U
885 :charset-list '(unicode)
886 :post-read-conversion 'ns-utf8-nfd-post-read-conversion)
887 (set-file-name-coding-system 'utf-8-nfd)))
888
889;; PENDING: disable composition-based display for Indic scripts as it
a5a1b464 890;; is not working well under Nextstep for some reason
edfda783 891(set-char-table-range composition-function-table
ebe68042 892 '(#x0900 . #x0DFF) nil)
edfda783
AR
893
894
895;;;; Inter-app communications support.
896
c0642f6d
GM
897(defvar ns-input-text) ; nsterm.m
898
edfda783
AR
899(defun ns-insert-text ()
900 "Insert contents of ns-input-text at point."
901 (interactive)
902 (insert ns-input-text)
903 (setq ns-input-text nil))
c0642f6d 904
edfda783
AR
905(defun ns-insert-file ()
906 "Insert contents of file ns-input-file like insert-file but with less
907prompting. If file is a directory perform a find-file on it."
908 (interactive)
909 (let ((f))
910 (setq f (car ns-input-file))
911 (setq ns-input-file (cdr ns-input-file))
912 (if (file-directory-p f)
913 (find-file f)
914 (push-mark (+ (point) (car (cdr (insert-file-contents f))))))))
915
916(defvar ns-select-overlay nil
a5a1b464 917 "Overlay used to highlight areas in files requested by Nextstep apps.")
edfda783
AR
918(make-variable-buffer-local 'ns-select-overlay)
919
c0642f6d
GM
920(defvar ns-input-line) ; nsterm.m
921
edfda783 922(defun ns-open-file-select-line ()
b90cc058
CY
923 "Open a buffer containing the file `ns-input-file'.
924Lines are highlighted according to `ns-input-line'."
edfda783
AR
925 (interactive)
926 (ns-find-file)
927 (cond
928 ((and ns-input-line (buffer-modified-p))
929 (if ns-select-overlay
930 (setq ns-select-overlay (delete-overlay ns-select-overlay)))
931 (deactivate-mark)
932 (goto-line (if (consp ns-input-line)
933 (min (car ns-input-line) (cdr ns-input-line))
934 ns-input-line)))
935 (ns-input-line
936 (if (not ns-select-overlay)
937 (overlay-put (setq ns-select-overlay (make-overlay (point-min) (point-min)))
938 'face 'highlight))
939 (let ((beg (save-excursion
940 (goto-line (if (consp ns-input-line)
941 (min (car ns-input-line) (cdr ns-input-line))
942 ns-input-line))
943 (point)))
944 (end (save-excursion
945 (goto-line (+ 1 (if (consp ns-input-line)
946 (max (car ns-input-line) (cdr ns-input-line))
947 ns-input-line)))
948 (point))))
949 (move-overlay ns-select-overlay beg end)
950 (deactivate-mark)
951 (goto-char beg)))
952 (t
953 (if ns-select-overlay
954 (setq ns-select-overlay (delete-overlay ns-select-overlay))))))
955
956(defun ns-unselect-line ()
a5a1b464 957 "Removes any Nextstep highlight a buffer may contain."
edfda783
AR
958 (if ns-select-overlay
959 (setq ns-select-overlay (delete-overlay ns-select-overlay))))
960
961(add-hook 'first-change-hook 'ns-unselect-line)
962
963
964
965;;;; Preferences handling.
c0642f6d 966(declare-function ns-get-resource "nsfns.m" (owner name))
edfda783
AR
967
968(defun get-lisp-resource (arg1 arg2)
969 (let ((res (ns-get-resource arg1 arg2)))
970 (cond
971 ((not res) 'unbound)
972 ((string-equal (upcase res) "YES") t)
973 ((string-equal (upcase res) "NO") nil)
974 (t (read res)))))
975
c0642f6d
GM
976;; nsterm.m
977(defvar ns-command-modifier)
978(defvar ns-control-modifier)
979(defvar ns-function-modifier)
980(defvar ns-antialias-text)
981(defvar ns-use-qd-smoothing)
982(defvar ns-use-system-highlight-color)
983
984(declare-function ns-set-resource "nsfns.m" (owner name value))
985(declare-function ns-font-name "nsfns.m" (name))
986(declare-function ns-read-file-name "nsfns.m"
987 (prompt &optional dir isLoad init))
988
edfda783
AR
989(defun ns-save-preferences ()
990 "Set all the defaults."
991 (interactive)
992 ;; Global preferences
993 (ns-set-resource nil "AlternateModifier" (symbol-name ns-alternate-modifier))
994 (ns-set-resource nil "CommandModifier" (symbol-name ns-command-modifier))
995 (ns-set-resource nil "ControlModifier" (symbol-name ns-control-modifier))
996 (ns-set-resource nil "FunctionModifier" (symbol-name ns-function-modifier))
edfda783 997 (ns-set-resource nil "ExpandSpace"
ebe68042
SM
998 (if ns-expand-space
999 (number-to-string ns-expand-space)
1000 "NO"))
edfda783
AR
1001 (ns-set-resource nil "GSFontAntiAlias" (if ns-antialias-text "YES" "NO"))
1002 (ns-set-resource nil "UseQuickdrawSmoothing"
1003 (if ns-use-qd-smoothing "YES" "NO"))
1004 (ns-set-resource nil "UseSystemHighlightColor"
1005 (if ns-use-system-highlight-color "YES" "NO"))
1006 ;; Default frame parameters
d377ef4a
GM
1007 (let ((p (frame-parameters))
1008 v)
1009 (if (setq v (assq 'font p))
1010 (ns-set-resource nil "Font" (ns-font-name (cdr v))))
1011 (if (setq v (assq 'fontsize p))
1012 (ns-set-resource nil "FontSize" (number-to-string (cdr v))))
1013 (if (setq v (assq 'foreground-color p))
1014 (ns-set-resource nil "Foreground" (cdr v)))
1015 (if (setq v (assq 'background-color p))
1016 (ns-set-resource nil "Background" (cdr v)))
1017 (if (setq v (assq 'cursor-color p))
1018 (ns-set-resource nil "CursorColor" (cdr v)))
1019 (if (setq v (assq 'cursor-type p))
1020 (ns-set-resource nil "CursorType" (if (symbolp (cdr v))
1021 (symbol-name (cdr v))
1022 (cdr v))))
1023 (if (setq v (assq 'underline p))
1024 (ns-set-resource nil "Underline"
1025 (case (cdr v)
1026 ((t) "YES")
1027 ((nil) "NO")
1028 (t (cdr v)))))
1029 (if (setq v (assq 'internal-border-width p))
1030 (ns-set-resource nil "InternalBorderWidth"
a5e1066d 1031 (number-to-string (cdr v))))
d377ef4a
GM
1032 (if (setq v (assq 'vertical-scroll-bars p))
1033 (ns-set-resource nil "VerticalScrollBars"
1034 (case (cdr v)
1035 ((t) "YES")
1036 ((nil) "NO")
1037 ((left) "left")
1038 ((right) "right")
1039 (t nil))))
1040 (if (setq v (assq 'height p))
1041 (ns-set-resource nil "Height" (number-to-string (cdr v))))
1042 (if (setq v (assq 'width p))
1043 (ns-set-resource nil "Width" (number-to-string (cdr v))))
1044 (if (setq v (assq 'top p))
1045 (ns-set-resource nil "Top" (number-to-string (cdr v))))
1046 (if (setq v (assq 'left p))
1047 (ns-set-resource nil "Left" (number-to-string (cdr v))))
edfda783 1048 ;; These not fully supported
d377ef4a
GM
1049 (if (setq v (assq 'auto-raise p))
1050 (ns-set-resource nil "AutoRaise" (if (cdr v) "YES" "NO")))
1051 (if (setq v (assq 'auto-lower p))
1052 (ns-set-resource nil "AutoLower" (if (cdr v) "YES" "NO")))
1053 (if (setq v (assq 'menu-bar-lines p))
1054 (ns-set-resource nil "Menus" (if (cdr v) "YES" "NO")))
edfda783
AR
1055 )
1056 (let ((fl (face-list)))
1057 (while (consp fl)
1058 (or (eq 'default (car fl))
1059 ;; dont save Default* since it causes all created faces to
1060 ;; inherit its values. The properties of the default face
1061 ;; have already been saved from the frame-parameters anyway.
1062 (let* ((name (symbol-name (car fl)))
1063 (font (face-font (car fl)))
ebe68042 1064 ;; (fontsize (face-fontsize (car fl)))
edfda783
AR
1065 (foreground (face-foreground (car fl)))
1066 (background (face-background (car fl)))
1067 (underline (face-underline-p (car fl)))
1068 (italic (face-italic-p (car fl)))
1069 (bold (face-bold-p (car fl)))
1070 (stipple (face-stipple (car fl))))
ebe68042
SM
1071 ;; (ns-set-resource nil (concat name ".attributeFont")
1072 ;; (if font font nil))
1073 ;; (ns-set-resource nil (concat name ".attributeFontSize")
1074 ;; (if fontsize (number-to-string fontsize) nil))
edfda783 1075 (ns-set-resource nil (concat name ".attributeForeground")
ebe68042 1076 (if foreground foreground nil))
edfda783 1077 (ns-set-resource nil (concat name ".attributeBackground")
ebe68042 1078 (if background background nil))
edfda783 1079 (ns-set-resource nil (concat name ".attributeUnderline")
ebe68042 1080 (if underline "YES" nil))
edfda783 1081 (ns-set-resource nil (concat name ".attributeItalic")
ebe68042 1082 (if italic "YES" nil))
edfda783 1083 (ns-set-resource nil (concat name ".attributeBold")
ebe68042 1084 (if bold "YES" nil))
edfda783
AR
1085 (and stipple
1086 (or (stringp stipple)
1087 (setq stipple (prin1-to-string stipple))))
1088 (ns-set-resource nil (concat name ".attributeStipple")
ebe68042 1089 (if stipple stipple nil))))
edfda783
AR
1090 (setq fl (cdr fl)))))
1091
c0642f6d
GM
1092(declare-function menu-bar-options-save-orig "ns-win" () t)
1093
edfda783
AR
1094;; call ns-save-preferences when menu-bar-options-save is called
1095(fset 'menu-bar-options-save-orig (symbol-function 'menu-bar-options-save))
1096(defun ns-save-options ()
1097 (interactive)
1098 (menu-bar-options-save-orig)
1099 (ns-save-preferences))
1100(fset 'menu-bar-options-save (symbol-function 'ns-save-options))
1101
1102
1103;;;; File handling.
1104
1105(defun ns-open-file-using-panel ()
1106 "Pop up open-file panel, and load the result in a buffer."
1107 (interactive)
ebe68042 1108 ;; Prompt dir defaultName isLoad initial.
edfda783
AR
1109 (setq ns-input-file (ns-read-file-name "Select File to Load" nil t nil))
1110 (if ns-input-file
1111 (and (setq ns-input-file (list ns-input-file)) (ns-find-file))))
1112
1113(defun ns-write-file-using-panel ()
1114 "Pop up save-file panel, and save buffer in resulting name."
1115 (interactive)
1116 (let (ns-output-file)
ebe68042 1117 ;; Prompt dir defaultName isLoad initial.
edfda783
AR
1118 (setq ns-output-file (ns-read-file-name "Save As" nil nil nil))
1119 (message ns-output-file)
1120 (if ns-output-file (write-file ns-output-file))))
1121
c0642f6d
GM
1122(defvar ns-pop-up-frames 'fresh
1123 "*Non-nil means open files upon request from the Workspace in a new frame.
1124If t, always do so. Any other non-nil value means open a new frame
1125unless the current buffer is a scratch buffer.")
1126
1127(declare-function ns-hide-emacs "nsfns.m" (on))
1128
edfda783
AR
1129(defun ns-find-file ()
1130 "Do a find-file with the ns-input-file as argument."
1131 (interactive)
1132 (let ((f) (file) (bufwin1) (bufwin2))
1133 (setq f (file-truename (car ns-input-file)))
1134 (setq ns-input-file (cdr ns-input-file))
1135 (setq file (find-file-noselect f))
1136 (setq bufwin1 (get-buffer-window file 'visible))
1137 (setq bufwin2 (get-buffer-window "*scratch*" 'visibile))
1138 (cond
1139 (bufwin1
1140 (select-frame (window-frame bufwin1))
1141 (raise-frame (window-frame bufwin1))
1142 (select-window bufwin1))
1143 ((and (eq ns-pop-up-frames 'fresh) bufwin2)
1144 (ns-hide-emacs 'activate)
1145 (select-frame (window-frame bufwin2))
1146 (raise-frame (window-frame bufwin2))
1147 (select-window bufwin2)
1148 (find-file f))
1149 (ns-pop-up-frames
1150 (ns-hide-emacs 'activate)
1151 (let ((pop-up-frames t)) (pop-to-buffer file nil)))
1152 (t
1153 (ns-hide-emacs 'activate)
1154 (find-file f)))))
1155
1156
1157
1158;;;; Frame-related functions.
1159
a5a1b464 1160;; Don't show the frame name; that's redundant with Nextstep.
edfda783
AR
1161(setq-default mode-line-frame-identification '(" "))
1162
edfda783
AR
1163;; You say tomAYto, I say tomAHto..
1164(defvaralias 'ns-option-modifier 'ns-alternate-modifier)
1165
1166(defun ns-do-hide-emacs ()
1167 (interactive)
1168 (ns-hide-emacs t))
1169
c0642f6d
GM
1170(declare-function ns-hide-others "nsfns.m" ())
1171
edfda783
AR
1172(defun ns-do-hide-others ()
1173 (interactive)
1174 (ns-hide-others))
1175
c0642f6d
GM
1176(declare-function ns-emacs-info-panel "nsfns.m" ())
1177
edfda783
AR
1178(defun ns-do-emacs-info-panel ()
1179 (interactive)
1180 (ns-emacs-info-panel))
1181
1182(defun ns-next-frame ()
1183 "Switch to next visible frame."
1184 (interactive)
1185 (other-frame 1))
1186(defun ns-prev-frame ()
1187 "Switch to previous visible frame."
1188 (interactive)
1189 (other-frame -1))
1190
ebe68042 1191;; If no position specified, make new frame offset by 25 from current.
e5744c66 1192(defvar parameters) ; dynamically bound in make-frame
edfda783 1193(add-hook 'before-make-frame-hook
ebe68042
SM
1194 (lambda ()
1195 (let ((left (cdr (assq 'left (frame-parameters))))
1196 (top (cdr (assq 'top (frame-parameters)))))
1197 (if (consp left) (setq left (cadr left)))
1198 (if (consp top) (setq top (cadr top)))
1199 (cond
1200 ((or (assq 'top parameters) (assq 'left parameters)))
1201 ((or (not left) (not top)))
1202 (t
1203 (setq parameters (cons (cons 'left (+ left 25))
1204 (cons (cons 'top (+ top 25))
1205 parameters))))))))
1206
1207;; frame will be focused anyway, so select it
55e8d9a5 1208;; (if this is not done, modeline is dimmed until first interaction)
edfda783
AR
1209(add-hook 'after-make-frame-functions 'select-frame)
1210
f2d9c15f
GM
1211(defvar tool-bar-mode)
1212(declare-function tool-bar-mode "tool-bar" (&optional arg))
1213
edfda783
AR
1214;; Based on a function by David Reitter <dreitter@inf.ed.ac.uk> ;
1215;; see http://lists.gnu.org/archive/html/emacs-devel/2005-09/msg00681.html .
1216(defun ns-toggle-toolbar (&optional frame)
1217 "Switches the tool bar on and off in frame FRAME.
1218 If FRAME is nil, the change applies to the selected frame."
1219 (interactive)
ebe68042
SM
1220 (modify-frame-parameters
1221 frame (list (cons 'tool-bar-lines
edfda783
AR
1222 (if (> (or (frame-parameter frame 'tool-bar-lines) 0) 0)
1223 0 1)) ))
1224 (if (not tool-bar-mode) (tool-bar-mode t)))
1225
edfda783
AR
1226
1227
1228;;;; Dialog-related functions.
1229
1230;; Ask user for confirm before printing. Due to Kevin Rodgers.
1231(defun ns-print-buffer ()
1232 "Interactive front-end to `print-buffer': asks for user confirmation first."
1233 (interactive)
1234 (if (and (interactive-p)
ebe68042
SM
1235 (or (listp last-nonmenu-event)
1236 (and (char-or-string-p (event-basic-type last-command-event))
1237 (memq 'super (event-modifiers last-command-event)))))
1238 (let ((last-nonmenu-event (if (listp last-nonmenu-event)
1239 last-nonmenu-event
1240 ;; Fake it:
1241 `(mouse-1 POSITION 1))))
1242 (if (y-or-n-p (format "Print buffer %s? " (buffer-name)))
1243 (print-buffer)
edfda783
AR
1244 (error "Cancelled")))
1245 (print-buffer)))
1246
edfda783
AR
1247
1248;;;; Font support.
1249
edfda783
AR
1250;; Needed for font listing functions under both backend and normal
1251(setq scalable-fonts-allowed t)
1252
1253;; Set to use font panel instead
1254(defalias 'generate-fontset-menu 'ns-popup-font-panel)
1255(defalias 'mouse-set-font 'ns-popup-font-panel)
1256
c0642f6d
GM
1257;; nsterm.m
1258(defvar ns-input-font)
1259(defvar ns-input-fontsize)
1260
edfda783
AR
1261(defun ns-respond-to-change-font ()
1262 "Respond to changeFont: event, expecting ns-input-font and\n\
1263ns-input-fontsize of new font."
1264 (interactive)
1265 (modify-frame-parameters (selected-frame)
1266 (list (cons 'font ns-input-font)
1267 (cons 'fontsize ns-input-fontsize)))
1268 (set-frame-font ns-input-font))
1269
1270
1271;; Default fontset for Mac OS X. This is mainly here to show how a fontset
1272;; can be set up manually. Ordinarily, fontsets are auto-created whenever
1273;; a font is chosen by
1274(defvar ns-standard-fontset-spec
ebe68042
SM
1275 ;; Only some code supports this so far, so use uglier XLFD version
1276 ;; "-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard,latin:Courier,han:Kai"
1277 (mapconcat 'identity
1278 '("-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard"
1279 "latin:-*-Courier-*-*-*-*-10-*-*-*-*-*-iso10646-1"
1280 "han:-*-Kai-*-*-*-*-10-*-*-*-*-*-iso10646-1"
1281 "cyrillic:-*-Trebuchet$MS-*-*-*-*-10-*-*-*-*-*-iso10646-1")
1282 ",")
1283 "String of fontset spec of the standard fontset.
edfda783
AR
1284This defines a fontset consisting of the Courier and other fonts that
1285come with OS X\".
1286See the documentation of `create-fontset-from-fontset-spec for the format.")
1287
ebe68042 1288;; Conditional on new-fontset so bootstrapping works on non-GUI compiles.
edfda783
AR
1289(if (fboundp 'new-fontset)
1290 (progn
1291 ;; Setup the default fontset.
1292 (setup-default-fontset)
1293 ;; Create the standard fontset.
ebe68042 1294 (create-fontset-from-fontset-spec ns-standard-fontset-spec t)))
edfda783 1295
ebe68042
SM
1296;;(push (cons 'font "-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard")
1297;; default-frame-alist)
edfda783 1298
ebe68042 1299;; Add some additional scripts to var we use for fontset generation.
edfda783
AR
1300(setq script-representative-chars
1301 (cons '(kana #xff8a)
1302 (cons '(symbol #x2295 #x2287 #x25a1)
ebe68042 1303 script-representative-chars)))
edfda783
AR
1304
1305
1306;;;; Pasteboard support.
1307
c0642f6d
GM
1308(declare-function ns-get-cut-buffer-internal "nsselect.m" (buffer))
1309
edfda783
AR
1310(defun ns-get-pasteboard ()
1311 "Returns the value of the pasteboard."
1312 (ns-get-cut-buffer-internal 'PRIMARY))
1313
c0642f6d
GM
1314(declare-function ns-store-cut-buffer-internal "nsselect.m" (buffer string))
1315
edfda783 1316(defun ns-set-pasteboard (string)
a5a1b464 1317 "Store STRING into the pasteboard of the Nextstep display server."
edfda783
AR
1318 ;; Check the data type of STRING.
1319 (if (not (stringp string)) (error "Nonstring given to pasteboard"))
1320 (ns-store-cut-buffer-internal 'PRIMARY string))
1321
ebe68042
SM
1322;; We keep track of the last text selected here, so we can check the
1323;; current selection against it, and avoid passing back our own text
9e50ff0c 1324;; from x-cut-buffer-or-selection-value.
edfda783
AR
1325(defvar ns-last-selected-text nil)
1326
9e50ff0c 1327(defun x-select-text (text &optional push)
ebe68042 1328 "Put TEXT, a string, on the pasteboard."
edfda783
AR
1329 ;; Don't send the pasteboard too much text.
1330 ;; It becomes slow, and if really big it causes errors.
1331 (ns-set-pasteboard text)
1332 (setq ns-last-selected-text text))
1333
a5a1b464
CY
1334;; Return the value of the current Nextstep selection. For
1335;; compatibility with older Nextstep applications, this checks cut
1336;; buffer 0 before retrieving the value of the primary selection.
9e50ff0c 1337(defun x-cut-buffer-or-selection-value ()
edfda783 1338 (let (text)
d377ef4a 1339
edfda783
AR
1340 ;; Consult the selection, then the cut buffer. Treat empty strings
1341 ;; as if they were unset.
1342 (or text (setq text (ns-get-pasteboard)))
1343 (if (string= text "") (setq text nil))
d377ef4a 1344
edfda783
AR
1345 (cond
1346 ((not text) nil)
1347 ((eq text ns-last-selected-text) nil)
1348 ((string= text ns-last-selected-text)
1349 ;; Record the newer string, so subsequent calls can use the `eq' test.
1350 (setq ns-last-selected-text text)
1351 nil)
1352 (t
1353 (setq ns-last-selected-text text)))))
1354
1355(defun ns-copy-including-secondary ()
1356 (interactive)
1357 (call-interactively 'kill-ring-save)
1358 (ns-store-cut-buffer-internal 'SECONDARY
1359 (buffer-substring (point) (mark t))))
1360(defun ns-paste-secondary ()
1361 (interactive)
1362 (insert (ns-get-cut-buffer-internal 'SECONDARY)))
1363
1364;; PENDING: not sure what to do here.. for now interprog- are set in
ebe68042 1365;; init-fn-keys, and unsure whether these x- settings have an effect.
9e50ff0c
DN
1366;;(setq interprogram-cut-function 'x-select-text
1367;; interprogram-paste-function 'x-cut-buffer-or-selection-value)
ebe68042 1368;; These only needed if above not working.
edfda783
AR
1369
1370(set-face-background 'region "ns_selection_color")
1371
1372
1373
1374;;;; Scrollbar handling.
1375
1376(global-set-key [vertical-scroll-bar down-mouse-1] 'ns-handle-scroll-bar-event)
1377(global-unset-key [vertical-scroll-bar mouse-1])
1378(global-unset-key [vertical-scroll-bar drag-mouse-1])
1379
f2d9c15f
GM
1380(declare-function scroll-bar-scale "scroll-bar" (num-denom whole))
1381
edfda783 1382(defun ns-scroll-bar-move (event)
55e8d9a5 1383 "Scroll the frame according to a Nextstep scroller event."
edfda783
AR
1384 (interactive "e")
1385 (let* ((pos (event-end event))
1386 (window (nth 0 pos))
1387 (scale (nth 2 pos)))
1388 (save-excursion
1389 (set-buffer (window-buffer window))
1390 (cond
1391 ((eq (car scale) (cdr scale))
1392 (goto-char (point-max)))
1393 ((= (car scale) 0)
1394 (goto-char (point-min)))
1395 (t
1396 (goto-char (+ (point-min) 1
1397 (scroll-bar-scale scale (- (point-max) (point-min)))))))
1398 (beginning-of-line)
1399 (set-window-start window (point))
1400 (vertical-motion (/ (window-height window) 2) window))))
1401
1402(defun ns-handle-scroll-bar-event (event)
55e8d9a5 1403 "Handle scroll bar EVENT to emulate Nextstep style scrolling."
edfda783
AR
1404 (interactive "e")
1405 (let* ((position (event-start event))
1406 (bar-part (nth 4 position))
1407 (window (nth 0 position))
1408 (old-window (selected-window)))
1409 (cond
1410 ((eq bar-part 'ratio)
1411 (ns-scroll-bar-move event))
1412 ((eq bar-part 'handle)
1413 (if (eq window (selected-window))
1414 (track-mouse (ns-scroll-bar-move event))
ebe68042 1415 ;; track-mouse faster for selected window, slower for unselected.
edfda783
AR
1416 (ns-scroll-bar-move event)))
1417 (t
1418 (select-window window)
1419 (cond
1420 ((eq bar-part 'up)
1421 (goto-char (window-start window))
1422 (scroll-down 1))
1423 ((eq bar-part 'above-handle)
1424 (scroll-down))
1425 ((eq bar-part 'below-handle)
1426 (scroll-up))
1427 ((eq bar-part 'down)
1428 (goto-char (window-start window))
1429 (scroll-up 1)))
1430 (select-window old-window)))))
1431
1432
1433;;;; Color support.
1434
c0642f6d
GM
1435(declare-function ns-list-colors "nsfns.m" (&optional frame))
1436
edfda783
AR
1437(defvar x-colors (ns-list-colors)
1438 "The list of colors defined in non-PANTONE color files.")
edfda783 1439
9e50ff0c 1440(defun xw-defined-colors (&optional frame)
edfda783
AR
1441 "Return a list of colors supported for a particular frame.
1442The argument FRAME specifies which frame to try.
a5a1b464 1443The value may be different for frames on different Nextstep displays."
edfda783
AR
1444 (or frame (setq frame (selected-frame)))
1445 (let ((all-colors x-colors)
1446 (this-color nil)
1447 (defined-colors nil))
1448 (while all-colors
1449 (setq this-color (car all-colors)
1450 all-colors (cdr all-colors))
ebe68042
SM
1451 ;; (and (face-color-supported-p frame this-color t)
1452 (setq defined-colors (cons this-color defined-colors))) ;;)
edfda783 1453 defined-colors))
edfda783 1454
c0642f6d
GM
1455(declare-function ns-set-alpha "nsfns.m" (color alpha))
1456
edfda783
AR
1457;; Convenience and work-around for fact that set color fns now require named.
1458(defun ns-set-background-alpha (alpha)
1459 "Sets alpha (opacity) of background.
1460Set from 0.0 (fully transparent) to 1.0 (fully opaque; default).
1461Note, tranparency works better on Tiger (10.4) and higher."
1462 (interactive "nSet background alpha to: ")
1463 (let ((bgcolor (cdr (assq 'background-color (frame-parameters)))))
1464 (set-frame-parameter (selected-frame)
1465 'background-color (ns-set-alpha bgcolor alpha))))
1466
1467;; Functions for color panel + drag
1468(defun ns-face-at-pos (pos)
1469 (let* ((frame (car pos))
1470 (frame-pos (cons (cadr pos) (cddr pos)))
1471 (window (window-at (car frame-pos) (cdr frame-pos) frame))
1472 (window-pos (coordinates-in-window-p frame-pos window))
1473 (buffer (window-buffer window))
1474 (edges (window-edges window)))
1475 (cond
1476 ((not window-pos)
1477 nil)
1478 ((eq window-pos 'mode-line)
1479 'modeline)
1480 ((eq window-pos 'vertical-line)
1481 'default)
1482 ((consp window-pos)
1483 (save-excursion
1484 (set-buffer buffer)
1485 (let ((p (car (compute-motion (window-start window)
1486 (cons (nth 0 edges) (nth 1 edges))
1487 (window-end window)
1488 frame-pos
1489 (- (window-width window) 1)
1490 nil
1491 window))))
1492 (cond
1493 ((eq p (window-point window))
1494 'cursor)
1495 ((and mark-active (< (region-beginning) p) (< p (region-end)))
1496 'region)
1497 (t
1498 (let ((faces (get-char-property p 'face window)))
1499 (if (consp faces) (car faces) faces)))))))
1500 (t
1501 nil))))
1502
c0642f6d
GM
1503(defvar ns-input-color) ; nsterm.m
1504
edfda783
AR
1505(defun ns-set-foreground-at-mouse ()
1506 "Set the foreground color at the mouse location to ns-input-color."
1507 (interactive)
1508 (let* ((pos (mouse-position))
1509 (frame (car pos))
1510 (face (ns-face-at-pos pos)))
1511 (cond
1512 ((eq face 'cursor)
c0642f6d 1513 (modify-frame-parameters frame (list (cons 'cursor-color
edfda783
AR
1514 ns-input-color))))
1515 ((not face)
1516 (modify-frame-parameters frame (list (cons 'foreground-color
1517 ns-input-color))))
1518 (t
1519 (set-face-foreground face ns-input-color frame)))))
1520
1521(defun ns-set-background-at-mouse ()
1522 "Set the background color at the mouse location to ns-input-color."
1523 (interactive)
1524 (let* ((pos (mouse-position))
1525 (frame (car pos))
1526 (face (ns-face-at-pos pos)))
1527 (cond
1528 ((eq face 'cursor)
1529 (modify-frame-parameters frame (list (cons 'cursor-color
1530 ns-input-color))))
1531 ((not face)
1532 (modify-frame-parameters frame (list (cons 'background-color
1533 ns-input-color))))
1534 (t
1535 (set-face-background face ns-input-color frame)))))
1536
a5a1b464 1537;; Set some options to be as Nextstep-like as possible.
edfda783
AR
1538(setq frame-title-format t
1539 icon-title-format t)
1540
edfda783
AR
1541
1542(defvar ns-initialized nil
a5a1b464 1543 "Non-nil if Nextstep windowing has been initialized.")
edfda783 1544
c0642f6d 1545(declare-function ns-list-services "nsfns.m" ())
f2d9c15f
GM
1546(declare-function x-open-connection "xfns.c"
1547 (display &optional xrm-string must-succeed))
c0642f6d 1548
a5a1b464
CY
1549;; Do the actual Nextstep Windows setup here; the above code just
1550;; defines functions and variables that we use now.
edfda783 1551(defun ns-initialize-window-system ()
a5a1b464 1552 "Initialize Emacs for Nextstep (Cocoa / GNUstep) windowing."
edfda783 1553
ebe68042 1554 ;; PENDING: not needed?
edfda783
AR
1555 (setq command-line-args (ns-handle-args command-line-args))
1556
9e50ff0c 1557 (x-open-connection (system-name) nil t)
edfda783 1558
ebe68042
SM
1559 (dolist (service (ns-list-services))
1560 (if (eq (car service) 'undefined)
1561 (ns-define-service (cdr service))
1562 (define-key global-map (vector (car service))
1563 (ns-define-service (cdr service)))))
edfda783
AR
1564
1565 (if (and (eq (get-lisp-resource nil "NXAutoLaunch") t)
1566 (eq (get-lisp-resource nil "HideOnAutoLaunch") t))
1567 (add-hook 'after-init-hook 'ns-do-hide-emacs))
1568
ebe68042 1569 ;; FIXME: This will surely lead to "MODIFIED OUTSIDE CUSTOM" warnings.
edfda783
AR
1570 (menu-bar-mode (if (get-lisp-resource nil "Menus") 1 -1))
1571 (mouse-wheel-mode 1)
1572
1573 (setq ns-initialized t))
1574
1575(add-to-list 'handle-args-function-alist '(ns . ns-handle-args))
1576(add-to-list 'frame-creation-function-alist '(ns . x-create-frame-with-faces))
1577(add-to-list 'window-system-initialization-alist '(ns . ns-initialize-window-system))
1578
1579
1580(provide 'ns-win)
1581
0ae1e5e5 1582;; arch-tag: eb138a45-4e2e-4d68-b1c9-a39665731644
edfda783 1583;;; ns-win.el ends here