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