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