Merge latest fix to xg_event_is_for_menubar.
[bpt/emacs.git] / lisp / term / ns-win.el
1 ;;; ns-win.el --- lisp side of interface with NeXT/Open/GNUstep/MacOS X window system
2
3 ;; Copyright (C) 1993, 1994, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5
6 ;; Authors: Carl Edman
7 ;; Christian Limpach
8 ;; Scott Bender
9 ;; Christophe de Dinechin
10 ;; Adrian Robert
11 ;; Keywords: terminals
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29
30 ;; ns-win.el: this file is loaded from ../lisp/startup.el when it
31 ;; recognizes that Nextstep windows are to be used. Command line
32 ;; switches are parsed and those pertaining to Nextstep are processed
33 ;; and removed from the command line. The Nextstep display is opened
34 ;; and hooks are set for popping up the initial window.
35
36 ;; startup.el will then examine startup files, and eventually call the hooks
37 ;; which create the first window (s).
38
39 ;; A number of other Nextstep convenience functions are defined in
40 ;; this file, which works in close coordination with src/nsfns.m.
41
42 ;;; Code:
43
44
45 (if (not (featurep 'ns))
46 (error "%s: Loading ns-win.el but not compiled for GNUstep/MacOS"
47 (invocation-name)))
48
49 (eval-when-compile (require 'cl))
50
51 ;; Documentation-purposes only: actually loaded in loadup.el
52 (require 'frame)
53 (require 'mouse)
54 (require 'faces)
55 (require 'easymenu)
56 (require 'menu-bar)
57 (require 'fontset)
58
59 ;; Not needed?
60 ;;(require 'ispell)
61
62 (defgroup ns nil
63 "GNUstep/Mac OS X specific features."
64 :group 'environment)
65
66 ;; nsterm.m
67 (defvar ns-version-string)
68 (defvar ns-alternate-modifier)
69
70 ;;;; Command line argument handling.
71
72 (defvar ns-invocation-args nil)
73 (defvar ns-command-line-resources nil)
74
75 ;; Handler for switches of the form "-switch value" or "-switch".
76 (defun ns-handle-switch (switch &optional numeric)
77 (let ((aelt (assoc switch command-line-ns-option-alist)))
78 (if aelt
79 (setq default-frame-alist
80 (cons (cons (nth 3 aelt)
81 (if numeric
82 (string-to-number (pop ns-invocation-args))
83 (or (nth 4 aelt) (pop ns-invocation-args))))
84 default-frame-alist)))))
85
86 ;; Handler for switches of the form "-switch n"
87 (defun ns-handle-numeric-switch (switch)
88 (ns-handle-switch switch t))
89
90 ;; Make -iconic apply only to the initial frame!
91 (defun ns-handle-iconic (switch)
92 (setq initial-frame-alist
93 (cons '(visibility . icon) initial-frame-alist)))
94
95 ;; Handle the -name option, set the name of the initial frame.
96 (defun ns-handle-name-switch (switch)
97 (or (consp ns-invocation-args)
98 (error "%s: missing argument to `%s' option" (invocation-name) switch))
99 (setq initial-frame-alist (cons (cons 'name (pop ns-invocation-args))
100 initial-frame-alist)))
101
102 ;; Set (but not used?) in frame.el.
103 (defvar x-display-name nil
104 "The name of the window display on which Emacs was started.
105 On X, the display name of individual X frames is recorded in the
106 `display' frame parameter.")
107
108 ;; nsterm.m.
109 (defvar ns-input-file)
110
111 (defun ns-handle-nxopen (switch)
112 (setq unread-command-events (append unread-command-events '(ns-open-file))
113 ns-input-file (append ns-input-file (list (pop ns-invocation-args)))))
114
115 (defun ns-handle-nxopentemp (switch)
116 (setq unread-command-events (append unread-command-events
117 '(ns-open-temp-file))
118 ns-input-file (append ns-input-file (list (pop ns-invocation-args)))))
119
120 (defun ns-ignore-1-arg (switch)
121 (setq ns-invocation-args (cdr ns-invocation-args)))
122 (defun ns-ignore-2-arg (switch)
123 (setq ns-invocation-args (cddr ns-invocation-args)))
124
125 (defun ns-handle-args (args)
126 "Process Nextstep-related command line options.
127 This is run before the user's startup file is loaded.
128 The options in ARGS are copied to `ns-invocation-args'.
129 The Nextstep-related settings are then applied using the handlers
130 defined in `command-line-ns-option-alist'.
131 The return value is ARGS minus the number of arguments processed."
132 ;; We use ARGS to accumulate the args that we don't handle here, to return.
133 (setq ns-invocation-args args
134 args nil)
135 (while ns-invocation-args
136 (let* ((this-switch (pop ns-invocation-args))
137 (orig-this-switch this-switch)
138 completion argval aelt handler)
139 ;; Check for long options with attached arguments
140 ;; and separate out the attached option argument into argval.
141 (if (string-match "^--[^=]*=" this-switch)
142 (setq argval (substring this-switch (match-end 0))
143 this-switch (substring this-switch 0 (1- (match-end 0)))))
144 ;; Complete names of long options.
145 (if (string-match "^--" this-switch)
146 (progn
147 (setq completion (try-completion this-switch
148 command-line-ns-option-alist))
149 (if (eq completion t)
150 ;; Exact match for long option.
151 nil
152 (if (stringp completion)
153 (let ((elt (assoc completion command-line-ns-option-alist)))
154 ;; Check for abbreviated long option.
155 (or elt
156 (error "Option `%s' is ambiguous" this-switch))
157 (setq this-switch completion))))))
158 (setq aelt (assoc this-switch command-line-ns-option-alist))
159 (if aelt (setq handler (nth 2 aelt)))
160 (if handler
161 (if argval
162 (let ((ns-invocation-args
163 (cons argval ns-invocation-args)))
164 (funcall handler this-switch))
165 (funcall handler this-switch))
166 (setq args (cons orig-this-switch args)))))
167 (nreverse args))
168
169 (defun ns-parse-geometry (geom)
170 "Parse a Nextstep-style geometry string GEOM.
171 Returns an alist of the form ((top . TOP), (left . LEFT) ... ).
172 The properties returned may include `top', `left', `height', and `width'."
173 (when (string-match "\\([0-9]+\\)\\( \\([0-9]+\\)\\( \\([0-9]+\\)\
174 \\( \\([0-9]+\\) ?\\)?\\)?\\)?"
175 geom)
176 (apply
177 'append
178 (list
179 (list (cons 'top (string-to-number (match-string 1 geom))))
180 (if (match-string 3 geom)
181 (list (cons 'left (string-to-number (match-string 3 geom)))))
182 (if (match-string 5 geom)
183 (list (cons 'height (string-to-number (match-string 5 geom)))))
184 (if (match-string 7 geom)
185 (list (cons 'width (string-to-number (match-string 7 geom)))))))))
186
187 ;;;; Keyboard mapping.
188
189 (defvar ns-alternatives-map
190 (let ((map (make-sparse-keymap)))
191 ;; Map certain keypad keys into ASCII characters
192 ;; that people usually expect.
193 (define-key map [S-tab] [backtab])
194 (define-key map [M-backspace] [?\M-\d])
195 (define-key map [M-delete] [?\M-\d])
196 (define-key map [M-tab] [?\M-\t])
197 (define-key map [M-linefeed] [?\M-\n])
198 (define-key map [M-clear] [?\M-\C-l])
199 (define-key map [M-return] [?\M-\C-m])
200 (define-key map [M-escape] [?\M-\e])
201 map)
202 "Keymap of alternative meanings for some keys under Nextstep.")
203
204 ;; Here are some Nextstep-like bindings for command key sequences.
205 (define-key global-map [?\s-,] 'customize)
206 (define-key global-map [?\s-'] 'next-multiframe-window)
207 (define-key global-map [?\s-`] 'other-frame)
208 (define-key global-map [?\s-~] 'ns-prev-frame)
209 (define-key global-map [?\s--] 'center-line)
210 (define-key global-map [?\s-:] 'ispell)
211 (define-key global-map [?\s-\;] 'ispell-next)
212 (define-key global-map [?\s-?] 'info)
213 (define-key global-map [?\s-^] 'kill-some-buffers)
214 (define-key global-map [?\s-&] 'kill-this-buffer)
215 (define-key global-map [?\s-C] 'ns-popup-color-panel)
216 (define-key global-map [?\s-D] 'dired)
217 (define-key global-map [?\s-E] 'edit-abbrevs)
218 (define-key global-map [?\s-L] 'shell-command)
219 (define-key global-map [?\s-M] 'manual-entry)
220 (define-key global-map [?\s-S] 'ns-write-file-using-panel)
221 (define-key global-map [?\s-a] 'mark-whole-buffer)
222 (define-key global-map [?\s-c] 'ns-copy-including-secondary)
223 (define-key global-map [?\s-d] 'isearch-repeat-backward)
224 (define-key global-map [?\s-e] 'isearch-yank-kill)
225 (define-key global-map [?\s-f] 'isearch-forward)
226 (define-key global-map [?\s-g] 'isearch-repeat-forward)
227 (define-key global-map [?\s-h] 'ns-do-hide-emacs)
228 (define-key global-map [?\s-H] 'ns-do-hide-others)
229 (define-key global-map [?\s-j] 'exchange-point-and-mark)
230 (define-key global-map [?\s-k] 'kill-this-buffer)
231 (define-key global-map [?\s-l] 'goto-line)
232 (define-key global-map [?\s-m] 'iconify-frame)
233 (define-key global-map [?\s-n] 'make-frame)
234 (define-key global-map [?\s-o] 'ns-open-file-using-panel)
235 (define-key global-map [?\s-p] 'ns-print-buffer)
236 (define-key global-map [?\s-q] 'save-buffers-kill-emacs)
237 (define-key global-map [?\s-s] 'save-buffer)
238 (define-key global-map [?\s-t] 'ns-popup-font-panel)
239 (define-key global-map [?\s-u] 'revert-buffer)
240 (define-key global-map [?\s-v] 'yank)
241 (define-key global-map [?\s-w] 'delete-frame)
242 (define-key global-map [?\s-x] 'kill-region)
243 (define-key global-map [?\s-y] 'ns-paste-secondary)
244 (define-key global-map [?\s-z] 'undo)
245 (define-key global-map [?\s-|] 'shell-command-on-region)
246 (define-key global-map [s-kp-bar] 'shell-command-on-region)
247 ;; (as in Terminal.app)
248 (define-key global-map [s-right] 'ns-next-frame)
249 (define-key global-map [s-left] 'ns-prev-frame)
250
251 (define-key global-map [home] 'beginning-of-buffer)
252 (define-key global-map [end] 'end-of-buffer)
253 (define-key global-map [kp-home] 'beginning-of-buffer)
254 (define-key global-map [kp-end] 'end-of-buffer)
255 (define-key global-map [kp-prior] 'scroll-down)
256 (define-key global-map [kp-next] 'scroll-up)
257
258 ;;; Allow shift-clicks to work similarly to under Nextstep
259 (define-key global-map [S-mouse-1] 'mouse-save-then-kill)
260 (global-unset-key [S-down-mouse-1])
261
262
263 ;; Special Nextstep-generated events are converted to function keys. Here
264 ;; are the bindings for them.
265 (define-key global-map [ns-power-off] 'save-buffers-kill-emacs)
266 (define-key global-map [ns-open-file] 'ns-find-file)
267 (define-key global-map [ns-open-temp-file] [ns-open-file])
268 (define-key global-map [ns-drag-file] 'ns-insert-file)
269 (define-key global-map [ns-drag-color] 'ns-set-foreground-at-mouse)
270 (define-key global-map [S-ns-drag-color] 'ns-set-background-at-mouse)
271 (define-key global-map [ns-drag-text] 'ns-insert-text)
272 (define-key global-map [ns-change-font] 'ns-respond-to-change-font)
273 (define-key global-map [ns-open-file-line] 'ns-open-file-select-line)
274 (define-key global-map [ns-spi-service-call] 'ns-spi-service-call)
275 (define-key global-map [ns-new-frame] 'make-frame)
276 (define-key global-map [ns-toggle-toolbar] 'ns-toggle-toolbar)
277 (define-key global-map [ns-show-prefs] 'customize)
278
279
280 ;; Set up a number of aliases and other layers to pretend we're using
281 ;; the Choi/Mitsuharu Carbon port.
282
283 (defvaralias 'mac-allow-anti-aliasing 'ns-antialias-text)
284 (defvaralias 'mac-command-modifier 'ns-command-modifier)
285 (defvaralias 'mac-control-modifier 'ns-control-modifier)
286 (defvaralias 'mac-option-modifier 'ns-option-modifier)
287 (defvaralias 'mac-function-modifier 'ns-function-modifier)
288 (declare-function ns-do-applescript "nsfns.m" (script))
289 (defalias 'do-applescript 'ns-do-applescript)
290
291 (defun x-setup-function-keys (frame)
292 "Set up `function-key-map' on the graphical frame FRAME."
293 (unless (terminal-parameter frame 'x-setup-function-keys)
294 (with-selected-frame frame
295 (setq interprogram-cut-function 'x-select-text
296 interprogram-paste-function 'x-cut-buffer-or-selection-value)
297 (let ((map (copy-keymap ns-alternatives-map)))
298 (set-keymap-parent map (keymap-parent local-function-key-map))
299 (set-keymap-parent local-function-key-map map))
300 (setq system-key-alist
301 (list
302 (cons (logior (lsh 0 16) 1) 'ns-power-off)
303 (cons (logior (lsh 0 16) 2) 'ns-open-file)
304 (cons (logior (lsh 0 16) 3) 'ns-open-temp-file)
305 (cons (logior (lsh 0 16) 4) 'ns-drag-file)
306 (cons (logior (lsh 0 16) 5) 'ns-drag-color)
307 (cons (logior (lsh 0 16) 6) 'ns-drag-text)
308 (cons (logior (lsh 0 16) 7) 'ns-change-font)
309 (cons (logior (lsh 0 16) 8) 'ns-open-file-line)
310 ; (cons (logior (lsh 0 16) 9) 'ns-insert-working-text)
311 ; (cons (logior (lsh 0 16) 10) 'ns-delete-working-text)
312 (cons (logior (lsh 0 16) 11) 'ns-spi-service-call)
313 (cons (logior (lsh 0 16) 12) 'ns-new-frame)
314 (cons (logior (lsh 0 16) 13) 'ns-toggle-toolbar)
315 (cons (logior (lsh 0 16) 14) 'ns-show-prefs)
316 (cons (logior (lsh 1 16) 32) 'f1)
317 (cons (logior (lsh 1 16) 33) 'f2)
318 (cons (logior (lsh 1 16) 34) 'f3)
319 (cons (logior (lsh 1 16) 35) 'f4)
320 (cons (logior (lsh 1 16) 36) 'f5)
321 (cons (logior (lsh 1 16) 37) 'f6)
322 (cons (logior (lsh 1 16) 38) 'f7)
323 (cons (logior (lsh 1 16) 39) 'f8)
324 (cons (logior (lsh 1 16) 40) 'f9)
325 (cons (logior (lsh 1 16) 41) 'f10)
326 (cons (logior (lsh 1 16) 42) 'f11)
327 (cons (logior (lsh 1 16) 43) 'f12)
328 (cons (logior (lsh 1 16) 44) 'kp-insert)
329 (cons (logior (lsh 1 16) 45) 'kp-delete)
330 (cons (logior (lsh 1 16) 46) 'kp-home)
331 (cons (logior (lsh 1 16) 47) 'kp-end)
332 (cons (logior (lsh 1 16) 48) 'kp-prior)
333 (cons (logior (lsh 1 16) 49) 'kp-next)
334 (cons (logior (lsh 1 16) 50) 'print-screen)
335 (cons (logior (lsh 1 16) 51) 'scroll-lock)
336 (cons (logior (lsh 1 16) 52) 'pause)
337 (cons (logior (lsh 1 16) 53) 'system)
338 (cons (logior (lsh 1 16) 54) 'break)
339 (cons (logior (lsh 1 16) 56) 'please-tell-carl-what-this-key-is-called-56)
340 (cons (logior (lsh 1 16) 61) 'please-tell-carl-what-this-key-is-called-61)
341 (cons (logior (lsh 1 16) 62) 'please-tell-carl-what-this-key-is-called-62)
342 (cons (logior (lsh 1 16) 63) 'please-tell-carl-what-this-key-is-called-63)
343 (cons (logior (lsh 1 16) 64) 'please-tell-carl-what-this-key-is-called-64)
344 (cons (logior (lsh 1 16) 69) 'please-tell-carl-what-this-key-is-called-69)
345 (cons (logior (lsh 1 16) 70) 'please-tell-carl-what-this-key-is-called-70)
346 (cons (logior (lsh 1 16) 71) 'please-tell-carl-what-this-key-is-called-71)
347 (cons (logior (lsh 1 16) 72) 'please-tell-carl-what-this-key-is-called-72)
348 (cons (logior (lsh 1 16) 73) 'please-tell-carl-what-this-key-is-called-73)
349 (cons (logior (lsh 2 16) 3) 'kp-enter)
350 (cons (logior (lsh 2 16) 9) 'kp-tab)
351 (cons (logior (lsh 2 16) 28) 'kp-quit)
352 (cons (logior (lsh 2 16) 35) 'kp-hash)
353 (cons (logior (lsh 2 16) 42) 'kp-multiply)
354 (cons (logior (lsh 2 16) 43) 'kp-add)
355 (cons (logior (lsh 2 16) 44) 'kp-separator)
356 (cons (logior (lsh 2 16) 45) 'kp-subtract)
357 (cons (logior (lsh 2 16) 46) 'kp-decimal)
358 (cons (logior (lsh 2 16) 47) 'kp-divide)
359 (cons (logior (lsh 2 16) 48) 'kp-0)
360 (cons (logior (lsh 2 16) 49) 'kp-1)
361 (cons (logior (lsh 2 16) 50) 'kp-2)
362 (cons (logior (lsh 2 16) 51) 'kp-3)
363 (cons (logior (lsh 2 16) 52) 'kp-4)
364 (cons (logior (lsh 2 16) 53) 'kp-5)
365 (cons (logior (lsh 2 16) 54) 'kp-6)
366 (cons (logior (lsh 2 16) 55) 'kp-7)
367 (cons (logior (lsh 2 16) 56) 'kp-8)
368 (cons (logior (lsh 2 16) 57) 'kp-9)
369 (cons (logior (lsh 2 16) 60) 'kp-less)
370 (cons (logior (lsh 2 16) 61) 'kp-equal)
371 (cons (logior (lsh 2 16) 62) 'kp-more)
372 (cons (logior (lsh 2 16) 64) 'kp-at)
373 (cons (logior (lsh 2 16) 92) 'kp-backslash)
374 (cons (logior (lsh 2 16) 96) 'kp-backtick)
375 (cons (logior (lsh 2 16) 124) 'kp-bar)
376 (cons (logior (lsh 2 16) 126) 'kp-tilde)
377 (cons (logior (lsh 2 16) 157) 'kp-mu)
378 (cons (logior (lsh 2 16) 165) 'kp-yen)
379 (cons (logior (lsh 2 16) 167) 'kp-paragraph)
380 (cons (logior (lsh 2 16) 172) 'left)
381 (cons (logior (lsh 2 16) 173) 'up)
382 (cons (logior (lsh 2 16) 174) 'right)
383 (cons (logior (lsh 2 16) 175) 'down)
384 (cons (logior (lsh 2 16) 176) 'kp-ring)
385 (cons (logior (lsh 2 16) 201) 'kp-square)
386 (cons (logior (lsh 2 16) 204) 'kp-cube)
387 (cons (logior (lsh 3 16) 8) 'backspace)
388 (cons (logior (lsh 3 16) 9) 'tab)
389 (cons (logior (lsh 3 16) 10) 'linefeed)
390 (cons (logior (lsh 3 16) 11) 'clear)
391 (cons (logior (lsh 3 16) 13) 'return)
392 (cons (logior (lsh 3 16) 18) 'pause)
393 (cons (logior (lsh 3 16) 25) 'S-tab)
394 (cons (logior (lsh 3 16) 27) 'escape)
395 (cons (logior (lsh 3 16) 127) 'delete)
396 )))
397 (set-terminal-parameter frame 'x-setup-function-keys t)))
398
399
400 ;; Add a couple of menus and rearrange some others; easiest just to redo toplvl
401 ;; Note keymap defns must be given last-to-first
402 (define-key global-map [menu-bar] (make-sparse-keymap "menu-bar"))
403
404 (setq menu-bar-final-items
405 (cond ((eq system-type 'darwin)
406 '(buffer windows services help-menu))
407 ;; Otherwise, GNUstep.
408 (t
409 '(buffer windows services hide-app quit))))
410
411 ;; Add standard top-level items to GNUstep menu.
412 (unless (eq system-type 'darwin)
413 (define-key global-map [menu-bar quit] '("Quit" . save-buffers-kill-emacs))
414 (define-key global-map [menu-bar hide-app] '("Hide" . ns-do-hide-emacs)))
415
416 (define-key global-map [menu-bar services]
417 (cons "Services" (make-sparse-keymap "Services")))
418 (define-key global-map [menu-bar buffer]
419 (cons "Buffers" global-buffers-menu-map))
420 ;; (cons "Buffers" (make-sparse-keymap "Buffers")))
421 (define-key global-map [menu-bar tools] (cons "Tools" menu-bar-tools-menu))
422 (define-key global-map [menu-bar options] (cons "Options" menu-bar-options-menu))
423 (define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
424 (define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu))
425
426 ;; If running under GNUstep, rename "Help" to "Info"
427 (cond ((eq system-type 'darwin)
428 (define-key global-map [menu-bar help-menu]
429 (cons "Help" menu-bar-help-menu)))
430 (t
431 (let ((contents (reverse (cdr menu-bar-help-menu))))
432 (setq menu-bar-help-menu
433 (append (list 'keymap) (cdr contents) (list "Info"))))
434 (define-key global-map [menu-bar help-menu]
435 (cons "Info" menu-bar-help-menu))))
436
437 (if (not (eq system-type 'darwin))
438 ;; in OS X it's in the app menu already
439 (define-key menu-bar-help-menu [info-panel]
440 '("About Emacs..." . ns-do-emacs-info-panel)))
441
442 ;;;; Edit menu: Modify slightly
443
444 ;; Substitute a Copy function that works better under X (for GNUstep).
445 (easy-menu-remove-item global-map '("menu-bar" "edit") 'copy)
446 (define-key-after menu-bar-edit-menu [copy]
447 '(menu-item "Copy" ns-copy-including-secondary
448 :enable mark-active
449 :help "Copy text in region between mark and current position")
450 'cut)
451
452 ;; Change to same precondition as select-and-paste, as we don't have
453 ;; `x-selection-exists-p'.
454 (easy-menu-remove-item global-map '("menu-bar" "edit") 'paste)
455 (define-key-after menu-bar-edit-menu [paste]
456 '(menu-item "Paste" yank
457 :enable (and (cdr yank-menu) (not buffer-read-only))
458 :help "Paste (yank) text most recently cut/copied")
459 'copy)
460
461 ;; Change text to be more consistent with surrounding menu items `paste', etc.
462 (easy-menu-remove-item global-map '("menu-bar" "edit") 'paste-from-menu)
463 (define-key-after menu-bar-edit-menu [select-paste]
464 '(menu-item "Select and Paste" yank-menu
465 :enable (and (cdr yank-menu) (not buffer-read-only))
466 :help "Choose a string from the kill ring and paste it")
467 'paste)
468
469 ;; Separate undo from cut/paste section, add spell for platform consistency.
470 (define-key-after menu-bar-edit-menu [separator-undo] '("--") 'undo)
471 (define-key-after menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map) 'fill)
472
473
474 ;;;; Services
475 (declare-function ns-perform-service "nsfns.m" (service send))
476
477 (defun ns-define-service (path)
478 (let ((mapping [menu-bar services])
479 (service (mapconcat 'identity path "/"))
480 (name (intern
481 (subst-char-in-string
482 ?\s ?-
483 (mapconcat 'identity (cons "ns-service" path) "-")))))
484 ;; This defines the function.
485 (defalias name
486 (lexical-let ((service service))
487 (lambda (arg)
488 (interactive "p")
489 (let* ((in-string
490 (cond ((stringp arg) arg)
491 (mark-active
492 (buffer-substring (region-beginning) (region-end)))))
493 (out-string (ns-perform-service service in-string)))
494 (cond
495 ((stringp arg) out-string)
496 ((and out-string (or (not in-string)
497 (not (string= in-string out-string))))
498 (if mark-active (delete-region (region-beginning) (region-end)))
499 (insert out-string)
500 (setq deactivate-mark nil)))))))
501 (cond
502 ((lookup-key global-map mapping)
503 (while (cdr path)
504 (setq mapping (vconcat mapping (list (intern (car path)))))
505 (if (not (keymapp (lookup-key global-map mapping)))
506 (define-key global-map mapping
507 (cons (car path) (make-sparse-keymap (car path)))))
508 (setq path (cdr path)))
509 (setq mapping (vconcat mapping (list (intern (car path)))))
510 (define-key global-map mapping (cons (car path) name))))
511 name))
512
513 ;; nsterm.m
514 (defvar ns-input-spi-name)
515 (defvar ns-input-spi-arg)
516
517 (declare-function dnd-open-file "dnd" (uri action))
518
519 (defun ns-spi-service-call ()
520 "Respond to a service request."
521 (interactive)
522 (cond ((string-equal ns-input-spi-name "open-selection")
523 (switch-to-buffer (generate-new-buffer "*untitled*"))
524 (insert ns-input-spi-arg))
525 ((string-equal ns-input-spi-name "open-file")
526 (dnd-open-file ns-input-spi-arg nil))
527 ((string-equal ns-input-spi-name "mail-selection")
528 (compose-mail)
529 (rfc822-goto-eoh)
530 (forward-line 1)
531 (insert ns-input-spi-arg))
532 ((string-equal ns-input-spi-name "mail-to")
533 (compose-mail ns-input-spi-arg))
534 (t (error (concat "Service " ns-input-spi-name " not recognized")))))
535
536
537 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
538
539
540
541 ;; Composed key sequence handling for Nextstep system input methods.
542 ;; (On Nextstep systems, input methods are provided for CJK
543 ;; characters, etc. which require multiple keystrokes, and during
544 ;; entry a partial ("working") result is typically shown in the
545 ;; editing window.)
546
547 (defface ns-working-text-face
548 '((t :underline t))
549 "Face used to highlight working text during compose sequence insert."
550 :group 'ns)
551
552 (defvar ns-working-overlay nil
553 "Overlay used to highlight working text during compose sequence insert.
554 When text is in th echo area, this just stores the length of the working text.")
555
556 (defvar ns-working-text) ; nsterm.m
557
558 ;; Test if in echo area, based on mac-win.el 2007/08/26 unicode-2.
559 ;; This will fail if called from a NONASCII_KEYSTROKE event on the global map.
560 (defun ns-in-echo-area ()
561 "Whether, for purposes of inserting working composition text, the minibuffer
562 is currently being used."
563 (or isearch-mode
564 (and cursor-in-echo-area (current-message))
565 ;; Overlay strings are not shown in some cases.
566 (get-char-property (point) 'invisible)
567 (and (not (bobp))
568 (or (and (get-char-property (point) 'display)
569 (eq (get-char-property (1- (point)) 'display)
570 (get-char-property (point) 'display)))
571 (and (get-char-property (point) 'composition)
572 (eq (get-char-property (1- (point)) 'composition)
573 (get-char-property (point) 'composition)))))))
574
575 ;; The 'interactive' here stays for subinvocations, so the ns-in-echo-area
576 ;; always returns nil for some reason. If this WASN'T the case, we could
577 ;; map this to [ns-insert-working-text] and eliminate Fevals in nsterm.m.
578 ;; These functions test whether in echo area and delegate accordingly.
579 (defun ns-put-working-text ()
580 (interactive)
581 (if (ns-in-echo-area) (ns-echo-working-text) (ns-insert-working-text)))
582 (defun ns-unput-working-text ()
583 (interactive)
584 (ns-delete-working-text))
585
586 (defun ns-insert-working-text ()
587 "Insert contents of `ns-working-text' as UTF-8 string and mark with
588 `ns-working-overlay'. Any previously existing working text is cleared first.
589 The overlay is assigned the face `ns-working-text-face'."
590 ;; FIXME: if buffer is read-only, don't try to insert anything
591 ;; and if text is bound to a command, execute that instead (Bug#1453)
592 (interactive)
593 (ns-delete-working-text)
594 (let ((start (point)))
595 (insert ns-working-text)
596 (overlay-put (setq ns-working-overlay (make-overlay start (point)
597 (current-buffer) nil t))
598 'face 'ns-working-text-face)))
599
600 (defun ns-echo-working-text ()
601 "Echo contents of `ns-working-text' in message display area.
602 See `ns-insert-working-text'."
603 (ns-delete-working-text)
604 (let* ((msg (current-message))
605 (msglen (length msg))
606 message-log-max)
607 (setq ns-working-overlay (length ns-working-text))
608 (setq msg (concat msg ns-working-text))
609 (put-text-property msglen (+ msglen ns-working-overlay)
610 'face 'ns-working-text-face msg)
611 (message "%s" msg)))
612
613 (defun ns-delete-working-text()
614 "Delete working text and clear `ns-working-overlay'."
615 (interactive)
616 (cond
617 ((and (overlayp ns-working-overlay)
618 ;; Still alive?
619 (overlay-buffer ns-working-overlay))
620 (with-current-buffer (overlay-buffer ns-working-overlay)
621 (delete-region (overlay-start ns-working-overlay)
622 (overlay-end ns-working-overlay))
623 (delete-overlay ns-working-overlay)))
624 ((integerp ns-working-overlay)
625 (let ((msg (current-message))
626 message-log-max)
627 (setq msg (substring msg 0 (- (length msg) ns-working-overlay)))
628 (message "%s" msg))))
629 (setq ns-working-overlay nil))
630
631
632 (declare-function ns-convert-utf8-nfd-to-nfc "nsfns.m" (str))
633
634 ;;;; OS X file system Unicode UTF-8 NFD (decomposed form) support
635 ;; Lisp code based on utf-8m.el, by Seiji Zenitani, Eiji Honjoh, and
636 ;; Carsten Bormann.
637 (if (eq system-type 'darwin)
638 (progn
639
640 (defun ns-utf8-nfd-post-read-conversion (length)
641 "Calls `ns-convert-utf8-nfd-to-nfc' to compose char sequences."
642 (save-excursion
643 (save-restriction
644 (narrow-to-region (point) (+ (point) length))
645 (let ((str (buffer-string)))
646 (delete-region (point-min) (point-max))
647 (insert (ns-convert-utf8-nfd-to-nfc str))
648 (- (point-max) (point-min))
649 ))))
650
651 (define-coding-system 'utf-8-nfd
652 "UTF-8 NFD (decomposed) encoding."
653 :coding-type 'utf-8
654 :mnemonic ?U
655 :charset-list '(unicode)
656 :post-read-conversion 'ns-utf8-nfd-post-read-conversion)
657 (set-file-name-coding-system 'utf-8-nfd)))
658
659
660
661 ;;;; Inter-app communications support.
662
663 (defvar ns-input-text) ; nsterm.m
664
665 (defun ns-insert-text ()
666 "Insert contents of `ns-input-text' at point."
667 (interactive)
668 (insert ns-input-text)
669 (setq ns-input-text nil))
670
671 (defun ns-insert-file ()
672 "Insert contents of file `ns-input-file' like insert-file but with less
673 prompting. If file is a directory perform a `find-file' on it."
674 (interactive)
675 (let ((f))
676 (setq f (car ns-input-file))
677 (setq ns-input-file (cdr ns-input-file))
678 (if (file-directory-p f)
679 (find-file f)
680 (push-mark (+ (point) (car (cdr (insert-file-contents f))))))))
681
682 (defvar ns-select-overlay nil
683 "Overlay used to highlight areas in files requested by Nextstep apps.")
684 (make-variable-buffer-local 'ns-select-overlay)
685
686 (defvar ns-input-line) ; nsterm.m
687
688 (defun ns-open-file-select-line ()
689 "Open a buffer containing the file `ns-input-file'.
690 Lines are highlighted according to `ns-input-line'."
691 (interactive)
692 (ns-find-file)
693 (cond
694 ((and ns-input-line (buffer-modified-p))
695 (if ns-select-overlay
696 (setq ns-select-overlay (delete-overlay ns-select-overlay)))
697 (deactivate-mark)
698 (goto-char (point-min))
699 (forward-line (1- (if (consp ns-input-line)
700 (min (car ns-input-line) (cdr ns-input-line))
701 ns-input-line))))
702 (ns-input-line
703 (if (not ns-select-overlay)
704 (overlay-put (setq ns-select-overlay (make-overlay (point-min)
705 (point-min)))
706 'face 'highlight))
707 (let ((beg (save-excursion
708 (goto-char (point-min))
709 (line-beginning-position
710 (if (consp ns-input-line)
711 (min (car ns-input-line) (cdr ns-input-line))
712 ns-input-line))))
713 (end (save-excursion
714 (goto-char (point-min))
715 (line-beginning-position
716 (1+ (if (consp ns-input-line)
717 (max (car ns-input-line) (cdr ns-input-line))
718 ns-input-line))))))
719 (move-overlay ns-select-overlay beg end)
720 (deactivate-mark)
721 (goto-char beg)))
722 (t
723 (if ns-select-overlay
724 (setq ns-select-overlay (delete-overlay ns-select-overlay))))))
725
726 (defun ns-unselect-line ()
727 "Removes any Nextstep highlight a buffer may contain."
728 (if ns-select-overlay
729 (setq ns-select-overlay (delete-overlay ns-select-overlay))))
730
731 (add-hook 'first-change-hook 'ns-unselect-line)
732
733
734
735 ;;;; Preferences handling.
736 (declare-function ns-get-resource "nsfns.m" (owner name))
737
738 (defun get-lisp-resource (arg1 arg2)
739 (let ((res (ns-get-resource arg1 arg2)))
740 (cond
741 ((not res) 'unbound)
742 ((string-equal (upcase res) "YES") t)
743 ((string-equal (upcase res) "NO") nil)
744 (t (read res)))))
745
746 ;; nsterm.m
747
748 (declare-function ns-read-file-name "nsfns.m"
749 (prompt &optional dir isLoad init))
750
751 ;;;; File handling.
752
753 (defun ns-open-file-using-panel ()
754 "Pop up open-file panel, and load the result in a buffer."
755 (interactive)
756 ;; Prompt dir defaultName isLoad initial.
757 (setq ns-input-file (ns-read-file-name "Select File to Load" nil t nil))
758 (if ns-input-file
759 (and (setq ns-input-file (list ns-input-file)) (ns-find-file))))
760
761 (defun ns-write-file-using-panel ()
762 "Pop up save-file panel, and save buffer in resulting name."
763 (interactive)
764 (let (ns-output-file)
765 ;; Prompt dir defaultName isLoad initial.
766 (setq ns-output-file (ns-read-file-name "Save As" nil nil nil))
767 (message ns-output-file)
768 (if ns-output-file (write-file ns-output-file))))
769
770 (defcustom ns-pop-up-frames 'fresh
771 "Non-nil means open files upon request from the Workspace in a new frame.
772 If t, always do so. Any other non-nil value means open a new frame
773 unless the current buffer is a scratch buffer."
774 :type '(choice (const :tag "Never" nil)
775 (const :tag "Always" t)
776 (other :tag "Except for scratch buffer" fresh))
777 :version "23.1"
778 :group 'ns)
779
780 (declare-function ns-hide-emacs "nsfns.m" (on))
781
782 (defun ns-find-file ()
783 "Do a `find-file' with the `ns-input-file' as argument."
784 (interactive)
785 (let ((f) (file) (bufwin1) (bufwin2))
786 (setq f (file-truename (car ns-input-file)))
787 (setq ns-input-file (cdr ns-input-file))
788 (setq file (find-file-noselect f))
789 (setq bufwin1 (get-buffer-window file 'visible))
790 (setq bufwin2 (get-buffer-window "*scratch*" 'visibile))
791 (cond
792 (bufwin1
793 (select-frame (window-frame bufwin1))
794 (raise-frame (window-frame bufwin1))
795 (select-window bufwin1))
796 ((and (eq ns-pop-up-frames 'fresh) bufwin2)
797 (ns-hide-emacs 'activate)
798 (select-frame (window-frame bufwin2))
799 (raise-frame (window-frame bufwin2))
800 (select-window bufwin2)
801 (find-file f))
802 (ns-pop-up-frames
803 (ns-hide-emacs 'activate)
804 (let ((pop-up-frames t)) (pop-to-buffer file nil)))
805 (t
806 (ns-hide-emacs 'activate)
807 (find-file f)))))
808
809
810
811 ;;;; Frame-related functions.
812
813 ;; Don't show the frame name; that's redundant with Nextstep.
814 (setq-default mode-line-frame-identification '(" "))
815
816 ;; You say tomAYto, I say tomAHto..
817 (defvaralias 'ns-option-modifier 'ns-alternate-modifier)
818
819 (defun ns-do-hide-emacs ()
820 (interactive)
821 (ns-hide-emacs t))
822
823 (declare-function ns-hide-others "nsfns.m" ())
824
825 (defun ns-do-hide-others ()
826 (interactive)
827 (ns-hide-others))
828
829 (declare-function ns-emacs-info-panel "nsfns.m" ())
830
831 (defun ns-do-emacs-info-panel ()
832 (interactive)
833 (ns-emacs-info-panel))
834
835 (defun ns-next-frame ()
836 "Switch to next visible frame."
837 (interactive)
838 (other-frame 1))
839
840 (defun ns-prev-frame ()
841 "Switch to previous visible frame."
842 (interactive)
843 (other-frame -1))
844
845 ;; If no position specified, make new frame offset by 25 from current.
846 (defvar parameters) ; dynamically bound in make-frame
847 (add-hook 'before-make-frame-hook
848 (lambda ()
849 (let ((left (cdr (assq 'left (frame-parameters))))
850 (top (cdr (assq 'top (frame-parameters)))))
851 (if (consp left) (setq left (cadr left)))
852 (if (consp top) (setq top (cadr top)))
853 (cond
854 ((or (assq 'top parameters) (assq 'left parameters)))
855 ((or (not left) (not top)))
856 (t
857 (setq parameters (cons (cons 'left (+ left 25))
858 (cons (cons 'top (+ top 25))
859 parameters))))))))
860
861 ;; frame will be focused anyway, so select it
862 ;; (if this is not done, modeline is dimmed until first interaction)
863 (add-hook 'after-make-frame-functions 'select-frame)
864
865 (defvar tool-bar-mode)
866 (declare-function tool-bar-mode "tool-bar" (&optional arg))
867
868 ;; Based on a function by David Reitter <dreitter@inf.ed.ac.uk> ;
869 ;; see http://lists.gnu.org/archive/html/emacs-devel/2005-09/msg00681.html .
870 (defun ns-toggle-toolbar (&optional frame)
871 "Switches the tool bar on and off in frame FRAME.
872 If FRAME is nil, the change applies to the selected frame."
873 (interactive)
874 (modify-frame-parameters
875 frame (list (cons 'tool-bar-lines
876 (if (> (or (frame-parameter frame 'tool-bar-lines) 0) 0)
877 0 1)) ))
878 (if (not tool-bar-mode) (tool-bar-mode t)))
879
880
881
882 ;;;; Dialog-related functions.
883
884
885 ;; Ask user for confirm before printing. Due to Kevin Rodgers.
886 (defun ns-print-buffer ()
887 "Interactive front-end to `print-buffer': asks for user confirmation first."
888 (interactive)
889 (if (and (called-interactively-p 'interactive)
890 (or (listp last-nonmenu-event)
891 (and (char-or-string-p (event-basic-type last-command-event))
892 (memq 'super (event-modifiers last-command-event)))))
893 (let ((last-nonmenu-event (if (listp last-nonmenu-event)
894 last-nonmenu-event
895 ;; Fake it:
896 `(mouse-1 POSITION 1))))
897 (if (y-or-n-p (format "Print buffer %s? " (buffer-name)))
898 (print-buffer)
899 (error "Cancelled")))
900 (print-buffer)))
901
902
903 ;;;; Font support.
904
905 ;; Needed for font listing functions under both backend and normal
906 (setq scalable-fonts-allowed t)
907
908 ;; Set to use font panel instead
909 (declare-function ns-popup-font-panel "nsfns.m" (&optional frame))
910 (defalias 'x-select-font 'ns-popup-font-panel "Pop up the font panel.
911 This function has been overloaded in Nextstep.")
912 (defalias 'mouse-set-font 'ns-popup-font-panel "Pop up the font panel.
913 This function has been overloaded in Nextstep.")
914
915 ;; nsterm.m
916 (defvar ns-input-font)
917 (defvar ns-input-fontsize)
918
919 (defun ns-respond-to-change-font ()
920 "Respond to changeFont: event, expecting `ns-input-font' and\n\
921 `ns-input-fontsize' of new font."
922 (interactive)
923 (modify-frame-parameters (selected-frame)
924 (list (cons 'font ns-input-font)
925 (cons 'fontsize ns-input-fontsize)))
926 (set-frame-font ns-input-font))
927
928
929 ;; Default fontset for Mac OS X. This is mainly here to show how a fontset
930 ;; can be set up manually. Ordinarily, fontsets are auto-created whenever
931 ;; a font is chosen by
932 (defvar ns-standard-fontset-spec
933 ;; Only some code supports this so far, so use uglier XLFD version
934 ;; "-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard,latin:Courier,han:Kai"
935 (mapconcat 'identity
936 '("-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard"
937 "latin:-*-Courier-*-*-*-*-10-*-*-*-*-*-iso10646-1"
938 "han:-*-Kai-*-*-*-*-10-*-*-*-*-*-iso10646-1"
939 "cyrillic:-*-Trebuchet$MS-*-*-*-*-10-*-*-*-*-*-iso10646-1")
940 ",")
941 "String of fontset spec of the standard fontset.
942 This defines a fontset consisting of the Courier and other fonts that
943 come with OS X.
944 See the documentation of `create-fontset-from-fontset-spec' for the format.")
945
946 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles.
947 (if (fboundp 'new-fontset)
948 (progn
949 ;; Setup the default fontset.
950 (create-default-fontset)
951 ;; Create the standard fontset.
952 (condition-case err
953 (create-fontset-from-fontset-spec ns-standard-fontset-spec t)
954 (error (display-warning
955 'initialization
956 (format "Creation of the standard fontset failed: %s" err)
957 :error)))))
958
959 (defvar ns-reg-to-script) ; nsfont.m
960
961 ;; This maps font registries (not exposed by NS APIs for font selection) to
962 ;; unicode scripts (which can be mapped to unicode character ranges which are).
963 ;; See ../international/fontset.el
964 (setq ns-reg-to-script
965 '(("iso8859-1" . latin)
966 ("iso8859-2" . latin)
967 ("iso8859-3" . latin)
968 ("iso8859-4" . latin)
969 ("iso8859-5" . cyrillic)
970 ("microsoft-cp1251" . cyrillic)
971 ("koi8-r" . cyrillic)
972 ("iso8859-6" . arabic)
973 ("iso8859-7" . greek)
974 ("iso8859-8" . hebrew)
975 ("iso8859-9" . latin)
976 ("iso8859-10" . latin)
977 ("iso8859-11" . thai)
978 ("tis620" . thai)
979 ("iso8859-13" . latin)
980 ("iso8859-14" . latin)
981 ("iso8859-15" . latin)
982 ("iso8859-16" . latin)
983 ("viscii1.1-1" . latin)
984 ("jisx0201" . kana)
985 ("jisx0208" . han)
986 ("jisx0212" . han)
987 ("jisx0213" . han)
988 ("gb2312.1980" . han)
989 ("gb18030" . han)
990 ("gbk-0" . han)
991 ("big5" . han)
992 ("cns11643" . han)
993 ("sisheng_cwnn" . bopomofo)
994 ("ksc5601.1987" . hangul)
995 ("ethiopic-unicode" . ethiopic)
996 ("is13194-devanagari" . indian-is13194)
997 ("iso10646.indian-1" . devanagari)))
998
999
1000 ;;;; Pasteboard support.
1001
1002 (declare-function ns-get-cut-buffer-internal "nsselect.m" (buffer))
1003
1004 (defun ns-get-pasteboard ()
1005 "Returns the value of the pasteboard."
1006 (ns-get-cut-buffer-internal 'PRIMARY))
1007
1008 (declare-function ns-store-cut-buffer-internal "nsselect.m" (buffer string))
1009
1010 (defun ns-set-pasteboard (string)
1011 "Store STRING into the pasteboard of the Nextstep display server."
1012 ;; Check the data type of STRING.
1013 (if (not (stringp string)) (error "Nonstring given to pasteboard"))
1014 (ns-store-cut-buffer-internal 'PRIMARY string))
1015
1016 ;; We keep track of the last text selected here, so we can check the
1017 ;; current selection against it, and avoid passing back our own text
1018 ;; from x-cut-buffer-or-selection-value.
1019 (defvar ns-last-selected-text nil)
1020
1021 (defun x-select-text (text &optional push)
1022 "Select TEXT, a string, according to the window system.
1023
1024 On X, put TEXT in the primary X selection. For backward
1025 compatibility with older X applications, set the value of X cut
1026 buffer 0 as well, and if the optional argument PUSH is non-nil,
1027 rotate the cut buffers. If `x-select-enable-clipboard' is
1028 non-nil, copy the text to the X clipboard as well.
1029
1030 On Windows, make TEXT the current selection. If
1031 `x-select-enable-clipboard' is non-nil, copy the text to the
1032 clipboard as well. The argument PUSH is ignored.
1033
1034 On Nextstep, put TEXT in the pasteboard; PUSH is ignored."
1035 ;; Don't send the pasteboard too much text.
1036 ;; It becomes slow, and if really big it causes errors.
1037 (ns-set-pasteboard text)
1038 (setq ns-last-selected-text text))
1039
1040 ;; Return the value of the current Nextstep selection. For
1041 ;; compatibility with older Nextstep applications, this checks cut
1042 ;; buffer 0 before retrieving the value of the primary selection.
1043 (defun x-cut-buffer-or-selection-value ()
1044 (let (text)
1045
1046 ;; Consult the selection, then the cut buffer. Treat empty strings
1047 ;; as if they were unset.
1048 (or text (setq text (ns-get-pasteboard)))
1049 (if (string= text "") (setq text nil))
1050
1051 (cond
1052 ((not text) nil)
1053 ((eq text ns-last-selected-text) nil)
1054 ((string= text ns-last-selected-text)
1055 ;; Record the newer string, so subsequent calls can use the `eq' test.
1056 (setq ns-last-selected-text text)
1057 nil)
1058 (t
1059 (setq ns-last-selected-text text)))))
1060
1061 (defun ns-copy-including-secondary ()
1062 (interactive)
1063 (call-interactively 'kill-ring-save)
1064 (ns-store-cut-buffer-internal 'SECONDARY
1065 (buffer-substring (point) (mark t))))
1066 (defun ns-paste-secondary ()
1067 (interactive)
1068 (insert (ns-get-cut-buffer-internal 'SECONDARY)))
1069
1070
1071
1072 ;;;; Scrollbar handling.
1073
1074 (global-set-key [vertical-scroll-bar down-mouse-1] 'ns-handle-scroll-bar-event)
1075 (global-unset-key [vertical-scroll-bar mouse-1])
1076 (global-unset-key [vertical-scroll-bar drag-mouse-1])
1077
1078 (declare-function scroll-bar-scale "scroll-bar" (num-denom whole))
1079
1080 (defun ns-scroll-bar-move (event)
1081 "Scroll the frame according to a Nextstep scroller event."
1082 (interactive "e")
1083 (let* ((pos (event-end event))
1084 (window (nth 0 pos))
1085 (scale (nth 2 pos)))
1086 (with-current-buffer (window-buffer window)
1087 (cond
1088 ((eq (car scale) (cdr scale))
1089 (goto-char (point-max)))
1090 ((= (car scale) 0)
1091 (goto-char (point-min)))
1092 (t
1093 (goto-char (+ (point-min) 1
1094 (scroll-bar-scale scale (- (point-max) (point-min)))))))
1095 (beginning-of-line)
1096 (set-window-start window (point))
1097 (vertical-motion (/ (window-height window) 2) window))))
1098
1099 (defun ns-handle-scroll-bar-event (event)
1100 "Handle scroll bar EVENT to emulate Nextstep style scrolling."
1101 (interactive "e")
1102 (let* ((position (event-start event))
1103 (bar-part (nth 4 position))
1104 (window (nth 0 position))
1105 (old-window (selected-window)))
1106 (cond
1107 ((eq bar-part 'ratio)
1108 (ns-scroll-bar-move event))
1109 ((eq bar-part 'handle)
1110 (if (eq window (selected-window))
1111 (track-mouse (ns-scroll-bar-move event))
1112 ;; track-mouse faster for selected window, slower for unselected.
1113 (ns-scroll-bar-move event)))
1114 (t
1115 (select-window window)
1116 (cond
1117 ((eq bar-part 'up)
1118 (goto-char (window-start window))
1119 (scroll-down 1))
1120 ((eq bar-part 'above-handle)
1121 (scroll-down))
1122 ((eq bar-part 'below-handle)
1123 (scroll-up))
1124 ((eq bar-part 'down)
1125 (goto-char (window-start window))
1126 (scroll-up 1)))
1127 (select-window old-window)))))
1128
1129
1130 ;;;; Color support.
1131
1132 (declare-function ns-list-colors "nsfns.m" (&optional frame))
1133
1134 (defvar x-colors (ns-list-colors)
1135 "List of basic colors available on color displays.
1136 For X, the list comes from the `rgb.txt' file,v 10.41 94/02/20.
1137 For Nextstep, this is a list of non-PANTONE colors returned by
1138 the operating system.")
1139
1140 (defun xw-defined-colors (&optional frame)
1141 "Internal function called by `defined-colors'."
1142 (or frame (setq frame (selected-frame)))
1143 (let ((all-colors x-colors)
1144 (this-color nil)
1145 (defined-colors nil))
1146 (while all-colors
1147 (setq this-color (car all-colors)
1148 all-colors (cdr all-colors))
1149 ;; (and (face-color-supported-p frame this-color t)
1150 (setq defined-colors (cons this-color defined-colors))) ;;)
1151 defined-colors))
1152
1153 ;; Functions for color panel + drag
1154 (defun ns-face-at-pos (pos)
1155 (let* ((frame (car pos))
1156 (frame-pos (cons (cadr pos) (cddr pos)))
1157 (window (window-at (car frame-pos) (cdr frame-pos) frame))
1158 (window-pos (coordinates-in-window-p frame-pos window))
1159 (buffer (window-buffer window))
1160 (edges (window-edges window)))
1161 (cond
1162 ((not window-pos)
1163 nil)
1164 ((eq window-pos 'mode-line)
1165 'modeline)
1166 ((eq window-pos 'vertical-line)
1167 'default)
1168 ((consp window-pos)
1169 (with-current-buffer buffer
1170 (let ((p (car (compute-motion (window-start window)
1171 (cons (nth 0 edges) (nth 1 edges))
1172 (window-end window)
1173 frame-pos
1174 (- (window-width window) 1)
1175 nil
1176 window))))
1177 (cond
1178 ((eq p (window-point window))
1179 'cursor)
1180 ((and mark-active (< (region-beginning) p) (< p (region-end)))
1181 'region)
1182 (t
1183 (let ((faces (get-char-property p 'face window)))
1184 (if (consp faces) (car faces) faces)))))))
1185 (t
1186 nil))))
1187
1188 (defvar ns-input-color) ; nsterm.m
1189
1190 (defun ns-set-foreground-at-mouse ()
1191 "Set the foreground color at the mouse location to `ns-input-color'."
1192 (interactive)
1193 (let* ((pos (mouse-position))
1194 (frame (car pos))
1195 (face (ns-face-at-pos pos)))
1196 (cond
1197 ((eq face 'cursor)
1198 (modify-frame-parameters frame (list (cons 'cursor-color
1199 ns-input-color))))
1200 ((not face)
1201 (modify-frame-parameters frame (list (cons 'foreground-color
1202 ns-input-color))))
1203 (t
1204 (set-face-foreground face ns-input-color frame)))))
1205
1206 (defun ns-set-background-at-mouse ()
1207 "Set the background color at the mouse location to `ns-input-color'."
1208 (interactive)
1209 (let* ((pos (mouse-position))
1210 (frame (car pos))
1211 (face (ns-face-at-pos pos)))
1212 (cond
1213 ((eq face 'cursor)
1214 (modify-frame-parameters frame (list (cons 'cursor-color
1215 ns-input-color))))
1216 ((not face)
1217 (modify-frame-parameters frame (list (cons 'background-color
1218 ns-input-color))))
1219 (t
1220 (set-face-background face ns-input-color frame)))))
1221
1222 ;; Set some options to be as Nextstep-like as possible.
1223 (setq frame-title-format t
1224 icon-title-format t)
1225
1226
1227 (defvar ns-initialized nil
1228 "Non-nil if Nextstep windowing has been initialized.")
1229
1230 (declare-function ns-list-services "nsfns.m" ())
1231 (declare-function x-open-connection "nsfns.m"
1232 (display &optional xrm-string must-succeed))
1233
1234 ;; Do the actual Nextstep Windows setup here; the above code just
1235 ;; defines functions and variables that we use now.
1236 (defun ns-initialize-window-system ()
1237 "Initialize Emacs for Nextstep (Cocoa / GNUstep) windowing."
1238
1239 ;; PENDING: not needed?
1240 (setq command-line-args (ns-handle-args command-line-args))
1241
1242 (x-open-connection (system-name) nil t)
1243
1244 (dolist (service (ns-list-services))
1245 (if (eq (car service) 'undefined)
1246 (ns-define-service (cdr service))
1247 (define-key global-map (vector (car service))
1248 (ns-define-service (cdr service)))))
1249
1250 (if (and (eq (get-lisp-resource nil "NXAutoLaunch") t)
1251 (eq (get-lisp-resource nil "HideOnAutoLaunch") t))
1252 (add-hook 'after-init-hook 'ns-do-hide-emacs))
1253
1254 ;; FIXME: This will surely lead to "MODIFIED OUTSIDE CUSTOM" warnings.
1255 (menu-bar-mode (if (get-lisp-resource nil "Menus") 1 -1))
1256
1257 (setq ns-initialized t))
1258
1259 (add-to-list 'handle-args-function-alist '(ns . ns-handle-args))
1260 (add-to-list 'frame-creation-function-alist '(ns . x-create-frame-with-faces))
1261 (add-to-list 'window-system-initialization-alist '(ns . ns-initialize-window-system))
1262
1263
1264 (provide 'ns-win)
1265
1266 ;; arch-tag: eb138a45-4e2e-4d68-b1c9-a39665731644
1267 ;;; ns-win.el ends here