Merge changes from emacs-23 branch
[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 (or (featurep 'ns)
45 (error "%s: Loading ns-win.el but not compiled for GNUstep/MacOS"
46 (invocation-name)))
47
48 (eval-when-compile (require 'cl)) ; lexical-let
49
50 ;; Documentation-purposes only: actually loaded in loadup.el.
51 (require 'frame)
52 (require 'mouse)
53 (require 'faces)
54 (require 'menu-bar)
55 (require 'fontset)
56
57 (defgroup ns nil
58 "GNUstep/Mac OS X specific features."
59 :group 'environment)
60
61 ;;;; Command line argument handling.
62
63 (defvar x-invocation-args)
64 (defvar ns-command-line-resources nil) ; FIXME unused?
65
66 ;; nsterm.m.
67 (defvar ns-input-file)
68
69 (defun ns-handle-nxopen (switch &optional temp)
70 (setq unread-command-events (append unread-command-events
71 (if temp '(ns-open-temp-file)
72 '(ns-open-file)))
73 ns-input-file (append ns-input-file (list (pop x-invocation-args)))))
74
75 (defun ns-handle-nxopentemp (switch)
76 (ns-handle-nxopen switch t))
77
78 (defun ns-ignore-1-arg (switch)
79 (setq x-invocation-args (cdr x-invocation-args)))
80
81 (defun ns-parse-geometry (geom)
82 "Parse a Nextstep-style geometry string GEOM.
83 Returns an alist of the form ((top . TOP), (left . LEFT) ... ).
84 The properties returned may include `top', `left', `height', and `width'."
85 (when (string-match "\\([0-9]+\\)\\( \\([0-9]+\\)\\( \\([0-9]+\\)\
86 \\( \\([0-9]+\\) ?\\)?\\)?\\)?"
87 geom)
88 (apply
89 'append
90 (list
91 (list (cons 'top (string-to-number (match-string 1 geom))))
92 (if (match-string 3 geom)
93 (list (cons 'left (string-to-number (match-string 3 geom)))))
94 (if (match-string 5 geom)
95 (list (cons 'height (string-to-number (match-string 5 geom)))))
96 (if (match-string 7 geom)
97 (list (cons 'width (string-to-number (match-string 7 geom)))))))))
98
99 ;;;; Keyboard mapping.
100
101 (define-obsolete-variable-alias 'ns-alternatives-map 'x-alternatives-map "24.1")
102
103 ;; Here are some Nextstep-like bindings for command key sequences.
104 (define-key global-map [?\s-,] 'customize)
105 (define-key global-map [?\s-'] 'next-multiframe-window)
106 (define-key global-map [?\s-`] 'other-frame)
107 (define-key global-map [?\s-~] 'ns-prev-frame)
108 (define-key global-map [?\s--] 'center-line)
109 (define-key global-map [?\s-:] 'ispell)
110 (define-key global-map [?\s-\;] 'ispell-next)
111 (define-key global-map [?\s-?] 'info)
112 (define-key global-map [?\s-^] 'kill-some-buffers)
113 (define-key global-map [?\s-&] 'kill-this-buffer)
114 (define-key global-map [?\s-C] 'ns-popup-color-panel)
115 (define-key global-map [?\s-D] 'dired)
116 (define-key global-map [?\s-E] 'edit-abbrevs)
117 (define-key global-map [?\s-L] 'shell-command)
118 (define-key global-map [?\s-M] 'manual-entry)
119 (define-key global-map [?\s-S] 'ns-write-file-using-panel)
120 (define-key global-map [?\s-a] 'mark-whole-buffer)
121 (define-key global-map [?\s-c] 'ns-copy-including-secondary)
122 (define-key global-map [?\s-d] 'isearch-repeat-backward)
123 (define-key global-map [?\s-e] 'isearch-yank-kill)
124 (define-key global-map [?\s-f] 'isearch-forward)
125 (define-key global-map [?\s-g] 'isearch-repeat-forward)
126 (define-key global-map [?\s-h] 'ns-do-hide-emacs)
127 (define-key global-map [?\s-H] 'ns-do-hide-others)
128 (define-key global-map [?\s-j] 'exchange-point-and-mark)
129 (define-key global-map [?\s-k] 'kill-this-buffer)
130 (define-key global-map [?\s-l] 'goto-line)
131 (define-key global-map [?\s-m] 'iconify-frame)
132 (define-key global-map [?\s-n] 'make-frame)
133 (define-key global-map [?\s-o] 'ns-open-file-using-panel)
134 (define-key global-map [?\s-p] 'ns-print-buffer)
135 (define-key global-map [?\s-q] 'save-buffers-kill-emacs)
136 (define-key global-map [?\s-s] 'save-buffer)
137 (define-key global-map [?\s-t] 'ns-popup-font-panel)
138 (define-key global-map [?\s-u] 'revert-buffer)
139 (define-key global-map [?\s-v] 'yank)
140 (define-key global-map [?\s-w] 'delete-frame)
141 (define-key global-map [?\s-x] 'kill-region)
142 (define-key global-map [?\s-y] 'ns-paste-secondary)
143 (define-key global-map [?\s-z] 'undo)
144 (define-key global-map [?\s-|] 'shell-command-on-region)
145 (define-key global-map [s-kp-bar] 'shell-command-on-region)
146 ;; (as in Terminal.app)
147 (define-key global-map [s-right] 'ns-next-frame)
148 (define-key global-map [s-left] 'ns-prev-frame)
149
150 (define-key global-map [home] 'beginning-of-buffer)
151 (define-key global-map [end] 'end-of-buffer)
152 (define-key global-map [kp-home] 'beginning-of-buffer)
153 (define-key global-map [kp-end] 'end-of-buffer)
154 (define-key global-map [kp-prior] 'scroll-down)
155 (define-key global-map [kp-next] 'scroll-up)
156
157 ;; Allow shift-clicks to work similarly to under Nextstep.
158 (define-key global-map [S-mouse-1] 'mouse-save-then-kill)
159 (global-unset-key [S-down-mouse-1])
160
161 ;; Special Nextstep-generated events are converted to function keys. Here
162 ;; are the bindings for them. Note, these keys are actually declared in
163 ;; x-setup-function-keys in common-win.
164 (define-key global-map [ns-power-off] 'save-buffers-kill-emacs)
165 (define-key global-map [ns-open-file] 'ns-find-file)
166 (define-key global-map [ns-open-temp-file] [ns-open-file])
167 (define-key global-map [ns-drag-file] 'ns-insert-file)
168 (define-key global-map [ns-drag-color] 'ns-set-foreground-at-mouse)
169 (define-key global-map [S-ns-drag-color] 'ns-set-background-at-mouse)
170 (define-key global-map [ns-drag-text] 'ns-insert-text)
171 (define-key global-map [ns-change-font] 'ns-respond-to-change-font)
172 (define-key global-map [ns-open-file-line] 'ns-open-file-select-line)
173 (define-key global-map [ns-spi-service-call] 'ns-spi-service-call)
174 (define-key global-map [ns-toggle-toolbar] 'ns-toggle-toolbar)
175
176
177 ;; Set up a number of aliases and other layers to pretend we're using
178 ;; the Choi/Mitsuharu Carbon port.
179
180 (defvaralias 'mac-allow-anti-aliasing 'ns-antialias-text)
181 (defvaralias 'mac-command-modifier 'ns-command-modifier)
182 (defvaralias 'mac-right-command-modifier 'ns-right-command-modifier)
183 (defvaralias 'mac-control-modifier 'ns-control-modifier)
184 (defvaralias 'mac-right-control-modifier 'ns-right-control-modifier)
185 (defvaralias 'mac-option-modifier 'ns-option-modifier)
186 (defvaralias 'mac-right-option-modifier 'ns-right-option-modifier)
187 (defvaralias 'mac-function-modifier 'ns-function-modifier)
188 (declare-function ns-do-applescript "nsfns.m" (script))
189 (defalias 'do-applescript 'ns-do-applescript)
190
191 ;;;; Services
192 (declare-function ns-perform-service "nsfns.m" (service send))
193
194 (defun ns-define-service (path)
195 (let ((mapping [menu-bar services])
196 (service (mapconcat 'identity path "/"))
197 (name (intern
198 (subst-char-in-string
199 ?\s ?-
200 (mapconcat 'identity (cons "ns-service" path) "-")))))
201 ;; This defines the function.
202 (defalias name
203 (lexical-let ((service service))
204 (lambda (arg)
205 (interactive "p")
206 (let* ((in-string
207 (cond ((stringp arg) arg)
208 (mark-active
209 (buffer-substring (region-beginning) (region-end)))))
210 (out-string (ns-perform-service service in-string)))
211 (cond
212 ((stringp arg) out-string)
213 ((and out-string (or (not in-string)
214 (not (string= in-string out-string))))
215 (if mark-active (delete-region (region-beginning) (region-end)))
216 (insert out-string)
217 (setq deactivate-mark nil)))))))
218 (cond
219 ((lookup-key global-map mapping)
220 (while (cdr path)
221 (setq mapping (vconcat mapping (list (intern (car path)))))
222 (if (not (keymapp (lookup-key global-map mapping)))
223 (define-key global-map mapping
224 (cons (car path) (make-sparse-keymap (car path)))))
225 (setq path (cdr path)))
226 (setq mapping (vconcat mapping (list (intern (car path)))))
227 (define-key global-map mapping (cons (car path) name))))
228 name))
229
230 ;; nsterm.m
231 (defvar ns-input-spi-name)
232 (defvar ns-input-spi-arg)
233
234 (declare-function dnd-open-file "dnd" (uri action))
235
236 (defun ns-spi-service-call ()
237 "Respond to a service request."
238 (interactive)
239 (cond ((string-equal ns-input-spi-name "open-selection")
240 (switch-to-buffer (generate-new-buffer "*untitled*"))
241 (insert ns-input-spi-arg))
242 ((string-equal ns-input-spi-name "open-file")
243 (dnd-open-file ns-input-spi-arg nil))
244 ((string-equal ns-input-spi-name "mail-selection")
245 (compose-mail)
246 (rfc822-goto-eoh)
247 (forward-line 1)
248 (insert ns-input-spi-arg))
249 ((string-equal ns-input-spi-name "mail-to")
250 (compose-mail ns-input-spi-arg))
251 (t (error (concat "Service " ns-input-spi-name " not recognized")))))
252
253
254 ;; Composed key sequence handling for Nextstep system input methods.
255 ;; (On Nextstep systems, input methods are provided for CJK
256 ;; characters, etc. which require multiple keystrokes, and during
257 ;; entry a partial ("working") result is typically shown in the
258 ;; editing window.)
259
260 (defface ns-working-text-face
261 '((t :underline t))
262 "Face used to highlight working text during compose sequence insert."
263 :group 'ns)
264
265 (defvar ns-working-overlay nil
266 "Overlay used to highlight working text during compose sequence insert.
267 When text is in th echo area, this just stores the length of the working text.")
268
269 (defvar ns-working-text) ; nsterm.m
270
271 ;; Test if in echo area, based on mac-win.el 2007/08/26 unicode-2.
272 ;; This will fail if called from a NONASCII_KEYSTROKE event on the global map.
273 (defun ns-in-echo-area ()
274 "Whether, for purposes of inserting working composition text, the minibuffer
275 is currently being used."
276 (or isearch-mode
277 (and cursor-in-echo-area (current-message))
278 ;; Overlay strings are not shown in some cases.
279 (get-char-property (point) 'invisible)
280 (and (not (bobp))
281 (or (and (get-char-property (point) 'display)
282 (eq (get-char-property (1- (point)) 'display)
283 (get-char-property (point) 'display)))
284 (and (get-char-property (point) 'composition)
285 (eq (get-char-property (1- (point)) 'composition)
286 (get-char-property (point) 'composition)))))))
287
288 ;; The 'interactive' here stays for subinvocations, so the ns-in-echo-area
289 ;; always returns nil for some reason. If this WASN'T the case, we could
290 ;; map this to [ns-insert-working-text] and eliminate Fevals in nsterm.m.
291 ;; These functions test whether in echo area and delegate accordingly.
292 (defun ns-put-working-text ()
293 (interactive)
294 (if (ns-in-echo-area) (ns-echo-working-text) (ns-insert-working-text)))
295 (defun ns-unput-working-text ()
296 (interactive)
297 (ns-delete-working-text))
298
299 (defun ns-insert-working-text ()
300 "Insert contents of `ns-working-text' as UTF-8 string and mark with
301 `ns-working-overlay'. Any previously existing working text is cleared first.
302 The overlay is assigned the face `ns-working-text-face'."
303 ;; FIXME: if buffer is read-only, don't try to insert anything
304 ;; and if text is bound to a command, execute that instead (Bug#1453)
305 (interactive)
306 (ns-delete-working-text)
307 (let ((start (point)))
308 (insert ns-working-text)
309 (overlay-put (setq ns-working-overlay (make-overlay start (point)
310 (current-buffer) nil t))
311 'face 'ns-working-text-face)))
312
313 (defun ns-echo-working-text ()
314 "Echo contents of `ns-working-text' in message display area.
315 See `ns-insert-working-text'."
316 (ns-delete-working-text)
317 (let* ((msg (current-message))
318 (msglen (length msg))
319 message-log-max)
320 (setq ns-working-overlay (length ns-working-text))
321 (setq msg (concat msg ns-working-text))
322 (put-text-property msglen (+ msglen ns-working-overlay)
323 'face 'ns-working-text-face msg)
324 (message "%s" msg)))
325
326 (defun ns-delete-working-text()
327 "Delete working text and clear `ns-working-overlay'."
328 (interactive)
329 (cond
330 ((and (overlayp ns-working-overlay)
331 ;; Still alive?
332 (overlay-buffer ns-working-overlay))
333 (with-current-buffer (overlay-buffer ns-working-overlay)
334 (delete-region (overlay-start ns-working-overlay)
335 (overlay-end ns-working-overlay))
336 (delete-overlay ns-working-overlay)))
337 ((integerp ns-working-overlay)
338 (let ((msg (current-message))
339 message-log-max)
340 (setq msg (substring msg 0 (- (length msg) ns-working-overlay)))
341 (message "%s" msg))))
342 (setq ns-working-overlay nil))
343
344
345 (declare-function ns-convert-utf8-nfd-to-nfc "nsfns.m" (str))
346
347 ;;;; OS X file system Unicode UTF-8 NFD (decomposed form) support
348 ;; Lisp code based on utf-8m.el, by Seiji Zenitani, Eiji Honjoh, and
349 ;; Carsten Bormann.
350 (when (eq system-type 'darwin)
351 (defun ns-utf8-nfd-post-read-conversion (length)
352 "Calls `ns-convert-utf8-nfd-to-nfc' to compose char sequences."
353 (save-excursion
354 (save-restriction
355 (narrow-to-region (point) (+ (point) length))
356 (let ((str (buffer-string)))
357 (delete-region (point-min) (point-max))
358 (insert (ns-convert-utf8-nfd-to-nfc str))
359 (- (point-max) (point-min))))))
360
361 (define-coding-system 'utf-8-nfd
362 "UTF-8 NFD (decomposed) encoding."
363 :coding-type 'utf-8
364 :mnemonic ?U
365 :charset-list '(unicode)
366 :post-read-conversion 'ns-utf8-nfd-post-read-conversion)
367 (set-file-name-coding-system 'utf-8-nfd))
368
369 ;;;; Inter-app communications support.
370
371 (defvar ns-input-text) ; nsterm.m
372
373 (defun ns-insert-text ()
374 "Insert contents of `ns-input-text' at point."
375 (interactive)
376 (insert ns-input-text)
377 (setq ns-input-text nil))
378
379 (defun ns-insert-file ()
380 "Insert contents of file `ns-input-file' like insert-file but with less
381 prompting. If file is a directory perform a `find-file' on it."
382 (interactive)
383 (let ((f (pop ns-input-file)))
384 (if (file-directory-p f)
385 (find-file f)
386 (push-mark (+ (point) (cadr (insert-file-contents f)))))))
387
388 (defvar ns-select-overlay nil
389 "Overlay used to highlight areas in files requested by Nextstep apps.")
390 (make-variable-buffer-local 'ns-select-overlay)
391
392 (defvar ns-input-line) ; nsterm.m
393
394 (defun ns-open-file-select-line ()
395 "Open a buffer containing the file `ns-input-file'.
396 Lines are highlighted according to `ns-input-line'."
397 (interactive)
398 (ns-find-file)
399 (cond
400 ((and ns-input-line (buffer-modified-p))
401 (if ns-select-overlay
402 (setq ns-select-overlay (delete-overlay ns-select-overlay)))
403 (deactivate-mark)
404 (goto-char (point-min))
405 (forward-line (1- (if (consp ns-input-line)
406 (min (car ns-input-line) (cdr ns-input-line))
407 ns-input-line))))
408 (ns-input-line
409 (if (not ns-select-overlay)
410 (overlay-put (setq ns-select-overlay (make-overlay (point-min)
411 (point-min)))
412 'face 'highlight))
413 (let ((beg (save-excursion
414 (goto-char (point-min))
415 (line-beginning-position
416 (if (consp ns-input-line)
417 (min (car ns-input-line) (cdr ns-input-line))
418 ns-input-line))))
419 (end (save-excursion
420 (goto-char (point-min))
421 (line-beginning-position
422 (1+ (if (consp ns-input-line)
423 (max (car ns-input-line) (cdr ns-input-line))
424 ns-input-line))))))
425 (move-overlay ns-select-overlay beg end)
426 (deactivate-mark)
427 (goto-char beg)))
428 (t
429 (if ns-select-overlay
430 (setq ns-select-overlay (delete-overlay ns-select-overlay))))))
431
432 (defun ns-unselect-line ()
433 "Removes any Nextstep highlight a buffer may contain."
434 (if ns-select-overlay
435 (setq ns-select-overlay (delete-overlay ns-select-overlay))))
436
437 (add-hook 'first-change-hook 'ns-unselect-line)
438
439 ;;;; Preferences handling.
440 (declare-function ns-get-resource "nsfns.m" (owner name))
441
442 (defun get-lisp-resource (arg1 arg2)
443 (let ((res (ns-get-resource arg1 arg2)))
444 (cond
445 ((not res) 'unbound)
446 ((string-equal (upcase res) "YES") t)
447 ((string-equal (upcase res) "NO") nil)
448 (t (read res)))))
449
450 ;; nsterm.m
451
452 (declare-function ns-read-file-name "nsfns.m"
453 (prompt &optional dir isLoad init))
454
455 ;;;; File handling.
456
457 (defun ns-open-file-using-panel ()
458 "Pop up open-file panel, and load the result in a buffer."
459 (interactive)
460 ;; Prompt dir defaultName isLoad initial.
461 (setq ns-input-file (ns-read-file-name "Select File to Load" nil t nil))
462 (if ns-input-file
463 (and (setq ns-input-file (list ns-input-file)) (ns-find-file))))
464
465 (defun ns-write-file-using-panel ()
466 "Pop up save-file panel, and save buffer in resulting name."
467 (interactive)
468 (let (ns-output-file)
469 ;; Prompt dir defaultName isLoad initial.
470 (setq ns-output-file (ns-read-file-name "Save As" nil nil nil))
471 (message ns-output-file)
472 (if ns-output-file (write-file ns-output-file))))
473
474 (defcustom ns-pop-up-frames 'fresh
475 "Non-nil means open files upon request from the Workspace in a new frame.
476 If t, always do so. Any other non-nil value means open a new frame
477 unless the current buffer is a scratch buffer."
478 :type '(choice (const :tag "Never" nil)
479 (const :tag "Always" t)
480 (other :tag "Except for scratch buffer" fresh))
481 :version "23.1"
482 :group 'ns)
483
484 (declare-function ns-hide-emacs "nsfns.m" (on))
485
486 (defun ns-find-file ()
487 "Do a `find-file' with the `ns-input-file' as argument."
488 (interactive)
489 (let* ((f (file-truename (pop ns-input-file)))
490 (file (find-file-noselect f))
491 (bufwin1 (get-buffer-window file 'visible))
492 (bufwin2 (get-buffer-window "*scratch*" 'visibile)))
493 (cond
494 (bufwin1
495 (select-frame (window-frame bufwin1))
496 (raise-frame (window-frame bufwin1))
497 (select-window bufwin1))
498 ((and (eq ns-pop-up-frames 'fresh) bufwin2)
499 (ns-hide-emacs 'activate)
500 (select-frame (window-frame bufwin2))
501 (raise-frame (window-frame bufwin2))
502 (select-window bufwin2)
503 (find-file f))
504 (ns-pop-up-frames
505 (ns-hide-emacs 'activate)
506 (let ((pop-up-frames t)) (pop-to-buffer file nil)))
507 (t
508 (ns-hide-emacs 'activate)
509 (find-file f)))))
510
511 ;;;; Frame-related functions.
512
513 ;; Don't show the frame name; that's redundant with Nextstep.
514 (setq-default mode-line-frame-identification '(" "))
515
516 ;; nsterm.m
517 (defvar ns-alternate-modifier)
518 (defvar ns-right-alternate-modifier)
519 (defvar ns-right-command-modifier)
520 (defvar ns-right-control-modifier)
521
522 ;; You say tomAYto, I say tomAHto..
523 (defvaralias 'ns-option-modifier 'ns-alternate-modifier)
524 (defvaralias 'ns-right-option-modifier 'ns-right-alternate-modifier)
525
526 (defun ns-do-hide-emacs ()
527 (interactive)
528 (ns-hide-emacs t))
529
530 (declare-function ns-hide-others "nsfns.m" ())
531
532 (defun ns-do-hide-others ()
533 (interactive)
534 (ns-hide-others))
535
536 (declare-function ns-emacs-info-panel "nsfns.m" ())
537
538 (defun ns-do-emacs-info-panel ()
539 (interactive)
540 (ns-emacs-info-panel))
541
542 (defun ns-next-frame ()
543 "Switch to next visible frame."
544 (interactive)
545 (other-frame 1))
546
547 (defun ns-prev-frame ()
548 "Switch to previous visible frame."
549 (interactive)
550 (other-frame -1))
551
552 ;; If no position specified, make new frame offset by 25 from current.
553 (defvar parameters) ; dynamically bound in make-frame
554 (add-hook 'before-make-frame-hook
555 (lambda ()
556 (let ((left (cdr (assq 'left (frame-parameters))))
557 (top (cdr (assq 'top (frame-parameters)))))
558 (if (consp left) (setq left (cadr left)))
559 (if (consp top) (setq top (cadr top)))
560 (cond
561 ((or (assq 'top parameters) (assq 'left parameters)))
562 ((or (not left) (not top)))
563 (t
564 (setq parameters (cons (cons 'left (+ left 25))
565 (cons (cons 'top (+ top 25))
566 parameters))))))))
567
568 ;; frame will be focused anyway, so select it
569 ;; (if this is not done, modeline is dimmed until first interaction)
570 (add-hook 'after-make-frame-functions 'select-frame)
571
572 (defvar tool-bar-mode)
573 (declare-function tool-bar-mode "tool-bar" (&optional arg))
574
575 ;; Based on a function by David Reitter <dreitter@inf.ed.ac.uk> ;
576 ;; see http://lists.gnu.org/archive/html/emacs-devel/2005-09/msg00681.html .
577 (defun ns-toggle-toolbar (&optional frame)
578 "Switches the tool bar on and off in frame FRAME.
579 If FRAME is nil, the change applies to the selected frame."
580 (interactive)
581 (modify-frame-parameters
582 frame (list (cons 'tool-bar-lines
583 (if (> (or (frame-parameter frame 'tool-bar-lines) 0) 0)
584 0 1)) ))
585 (if (not tool-bar-mode) (tool-bar-mode t)))
586
587
588 ;;;; Dialog-related functions.
589
590 ;; Ask user for confirm before printing. Due to Kevin Rodgers.
591 (defun ns-print-buffer ()
592 "Interactive front-end to `print-buffer': asks for user confirmation first."
593 (interactive)
594 (if (and (called-interactively-p 'interactive)
595 (or (listp last-nonmenu-event)
596 (and (char-or-string-p (event-basic-type last-command-event))
597 (memq 'super (event-modifiers last-command-event)))))
598 (let ((last-nonmenu-event (if (listp last-nonmenu-event)
599 last-nonmenu-event
600 ;; Fake it:
601 `(mouse-1 POSITION 1))))
602 (if (y-or-n-p (format "Print buffer %s? " (buffer-name)))
603 (print-buffer)
604 (error "Cancelled")))
605 (print-buffer)))
606
607 ;;;; Font support.
608
609 ;; Needed for font listing functions under both backend and normal
610 (setq scalable-fonts-allowed t)
611
612 ;; Set to use font panel instead
613 (declare-function ns-popup-font-panel "nsfns.m" (&optional frame))
614 (defalias 'x-select-font 'ns-popup-font-panel "Pop up the font panel.
615 This function has been overloaded in Nextstep.")
616 (defalias 'mouse-set-font 'ns-popup-font-panel "Pop up the font panel.
617 This function has been overloaded in Nextstep.")
618
619 ;; nsterm.m
620 (defvar ns-input-font)
621 (defvar ns-input-fontsize)
622
623 (defun ns-respond-to-change-font ()
624 "Respond to changeFont: event, expecting `ns-input-font' and\n\
625 `ns-input-fontsize' of new font."
626 (interactive)
627 (modify-frame-parameters (selected-frame)
628 (list (cons 'font ns-input-font)
629 (cons 'fontsize ns-input-fontsize)))
630 (set-frame-font ns-input-font))
631
632
633 ;; Default fontset for Mac OS X. This is mainly here to show how a fontset
634 ;; can be set up manually. Ordinarily, fontsets are auto-created whenever
635 ;; a font is chosen by
636 (defvar ns-standard-fontset-spec
637 ;; Only some code supports this so far, so use uglier XLFD version
638 ;; "-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard,latin:Courier,han:Kai"
639 (mapconcat 'identity
640 '("-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard"
641 "latin:-*-Courier-*-*-*-*-10-*-*-*-*-*-iso10646-1"
642 "han:-*-Kai-*-*-*-*-10-*-*-*-*-*-iso10646-1"
643 "cyrillic:-*-Trebuchet$MS-*-*-*-*-10-*-*-*-*-*-iso10646-1")
644 ",")
645 "String of fontset spec of the standard fontset.
646 This defines a fontset consisting of the Courier and other fonts that
647 come with OS X.
648 See the documentation of `create-fontset-from-fontset-spec' for the format.")
649
650 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles.
651 (when (fboundp 'new-fontset)
652 ;; Setup the default fontset.
653 (create-default-fontset)
654 ;; Create the standard fontset.
655 (condition-case err
656 (create-fontset-from-fontset-spec ns-standard-fontset-spec t)
657 (error (display-warning
658 'initialization
659 (format "Creation of the standard fontset failed: %s" err)
660 :error))))
661
662 (defvar ns-reg-to-script) ; nsfont.m
663
664 ;; This maps font registries (not exposed by NS APIs for font selection) to
665 ;; unicode scripts (which can be mapped to unicode character ranges which are).
666 ;; See ../international/fontset.el
667 (setq ns-reg-to-script
668 '(("iso8859-1" . latin)
669 ("iso8859-2" . latin)
670 ("iso8859-3" . latin)
671 ("iso8859-4" . latin)
672 ("iso8859-5" . cyrillic)
673 ("microsoft-cp1251" . cyrillic)
674 ("koi8-r" . cyrillic)
675 ("iso8859-6" . arabic)
676 ("iso8859-7" . greek)
677 ("iso8859-8" . hebrew)
678 ("iso8859-9" . latin)
679 ("iso8859-10" . latin)
680 ("iso8859-11" . thai)
681 ("tis620" . thai)
682 ("iso8859-13" . latin)
683 ("iso8859-14" . latin)
684 ("iso8859-15" . latin)
685 ("iso8859-16" . latin)
686 ("viscii1.1-1" . latin)
687 ("jisx0201" . kana)
688 ("jisx0208" . han)
689 ("jisx0212" . han)
690 ("jisx0213" . han)
691 ("gb2312.1980" . han)
692 ("gb18030" . han)
693 ("gbk-0" . han)
694 ("big5" . han)
695 ("cns11643" . han)
696 ("sisheng_cwnn" . bopomofo)
697 ("ksc5601.1987" . hangul)
698 ("ethiopic-unicode" . ethiopic)
699 ("is13194-devanagari" . indian-is13194)
700 ("iso10646.indian-1" . devanagari)))
701
702
703 ;;;; Pasteboard support.
704
705 (declare-function ns-get-cut-buffer-internal "nsselect.m" (buffer))
706
707 (defun ns-get-pasteboard ()
708 "Returns the value of the pasteboard."
709 (ns-get-cut-buffer-internal 'CLIPBOARD))
710
711 (declare-function ns-store-cut-buffer-internal "nsselect.m" (buffer string))
712
713 (defun ns-set-pasteboard (string)
714 "Store STRING into the pasteboard of the Nextstep display server."
715 ;; Check the data type of STRING.
716 (if (not (stringp string)) (error "Nonstring given to pasteboard"))
717 (ns-store-cut-buffer-internal 'CLIPBOARD string))
718
719 ;; We keep track of the last text selected here, so we can check the
720 ;; current selection against it, and avoid passing back our own text
721 ;; from x-selection-value.
722 (defvar ns-last-selected-text nil)
723
724 ;; Return the value of the current Nextstep selection. For
725 ;; compatibility with older Nextstep applications, this checks cut
726 ;; buffer 0 before retrieving the value of the primary selection.
727 (defun x-selection-value ()
728 (let (text)
729 ;; Consult the selection. Treat empty strings as if they were unset.
730 (or text (setq text (ns-get-pasteboard)))
731 (if (string= text "") (setq text nil))
732 (cond
733 ((not text) nil)
734 ((eq text ns-last-selected-text) nil)
735 ((string= text ns-last-selected-text)
736 ;; Record the newer string, so subsequent calls can use the `eq' test.
737 (setq ns-last-selected-text text)
738 nil)
739 (t
740 (setq ns-last-selected-text text)))))
741
742 (defun ns-copy-including-secondary ()
743 (interactive)
744 (call-interactively 'kill-ring-save)
745 (ns-store-cut-buffer-internal 'SECONDARY
746 (buffer-substring (point) (mark t))))
747 (defun ns-paste-secondary ()
748 (interactive)
749 (insert (ns-get-cut-buffer-internal 'SECONDARY)))
750
751
752 ;;;; Scrollbar handling.
753
754 (global-set-key [vertical-scroll-bar down-mouse-1] 'ns-handle-scroll-bar-event)
755 (global-unset-key [vertical-scroll-bar mouse-1])
756 (global-unset-key [vertical-scroll-bar drag-mouse-1])
757
758 (declare-function scroll-bar-scale "scroll-bar" (num-denom whole))
759
760 (defun ns-scroll-bar-move (event)
761 "Scroll the frame according to a Nextstep scroller event."
762 (interactive "e")
763 (let* ((pos (event-end event))
764 (window (nth 0 pos))
765 (scale (nth 2 pos)))
766 (with-current-buffer (window-buffer window)
767 (cond
768 ((eq (car scale) (cdr scale))
769 (goto-char (point-max)))
770 ((= (car scale) 0)
771 (goto-char (point-min)))
772 (t
773 (goto-char (+ (point-min) 1
774 (scroll-bar-scale scale (- (point-max) (point-min)))))))
775 (beginning-of-line)
776 (set-window-start window (point))
777 (vertical-motion (/ (window-height window) 2) window))))
778
779 (defun ns-handle-scroll-bar-event (event)
780 "Handle scroll bar EVENT to emulate Nextstep style scrolling."
781 (interactive "e")
782 (let* ((position (event-start event))
783 (bar-part (nth 4 position))
784 (window (nth 0 position))
785 (old-window (selected-window)))
786 (cond
787 ((eq bar-part 'ratio)
788 (ns-scroll-bar-move event))
789 ((eq bar-part 'handle)
790 (if (eq window (selected-window))
791 (track-mouse (ns-scroll-bar-move event))
792 ;; track-mouse faster for selected window, slower for unselected.
793 (ns-scroll-bar-move event)))
794 (t
795 (select-window window)
796 (cond
797 ((eq bar-part 'up)
798 (goto-char (window-start window))
799 (scroll-down 1))
800 ((eq bar-part 'above-handle)
801 (scroll-down))
802 ((eq bar-part 'below-handle)
803 (scroll-up))
804 ((eq bar-part 'down)
805 (goto-char (window-start window))
806 (scroll-up 1)))
807 (select-window old-window)))))
808
809
810 ;;;; Color support.
811
812 ;; Functions for color panel + drag
813 (defun ns-face-at-pos (pos)
814 (let* ((frame (car pos))
815 (frame-pos (cons (cadr pos) (cddr pos)))
816 (window (window-at (car frame-pos) (cdr frame-pos) frame))
817 (window-pos (coordinates-in-window-p frame-pos window))
818 (buffer (window-buffer window))
819 (edges (window-edges window)))
820 (cond
821 ((not window-pos)
822 nil)
823 ((eq window-pos 'mode-line)
824 'modeline)
825 ((eq window-pos 'vertical-line)
826 'default)
827 ((consp window-pos)
828 (with-current-buffer buffer
829 (let ((p (car (compute-motion (window-start window)
830 (cons (nth 0 edges) (nth 1 edges))
831 (window-end window)
832 frame-pos
833 (- (window-width window) 1)
834 nil
835 window))))
836 (cond
837 ((eq p (window-point window))
838 'cursor)
839 ((and mark-active (< (region-beginning) p) (< p (region-end)))
840 'region)
841 (t
842 (let ((faces (get-char-property p 'face window)))
843 (if (consp faces) (car faces) faces)))))))
844 (t
845 nil))))
846
847 (defvar ns-input-color) ; nsterm.m
848
849 (defun ns-set-foreground-at-mouse ()
850 "Set the foreground color at the mouse location to `ns-input-color'."
851 (interactive)
852 (let* ((pos (mouse-position))
853 (frame (car pos))
854 (face (ns-face-at-pos pos)))
855 (cond
856 ((eq face 'cursor)
857 (modify-frame-parameters frame (list (cons 'cursor-color
858 ns-input-color))))
859 ((not face)
860 (modify-frame-parameters frame (list (cons 'foreground-color
861 ns-input-color))))
862 (t
863 (set-face-foreground face ns-input-color frame)))))
864
865 (defun ns-set-background-at-mouse ()
866 "Set the background color at the mouse location to `ns-input-color'."
867 (interactive)
868 (let* ((pos (mouse-position))
869 (frame (car pos))
870 (face (ns-face-at-pos pos)))
871 (cond
872 ((eq face 'cursor)
873 (modify-frame-parameters frame (list (cons 'cursor-color
874 ns-input-color))))
875 ((not face)
876 (modify-frame-parameters frame (list (cons 'background-color
877 ns-input-color))))
878 (t
879 (set-face-background face ns-input-color frame)))))
880
881 ;; Set some options to be as Nextstep-like as possible.
882 (setq frame-title-format t
883 icon-title-format t)
884
885
886 (defvar ns-initialized nil
887 "Non-nil if Nextstep windowing has been initialized.")
888
889 (declare-function ns-list-services "nsfns.m" ())
890 (declare-function x-open-connection "nsfns.m"
891 (display &optional xrm-string must-succeed))
892
893 ;; Do the actual Nextstep Windows setup here; the above code just
894 ;; defines functions and variables that we use now.
895 (defun ns-initialize-window-system ()
896 "Initialize Emacs for Nextstep (Cocoa / GNUstep) windowing."
897
898 ;; PENDING: not needed?
899 (setq command-line-args (x-handle-args command-line-args))
900
901 (x-open-connection (system-name) nil t)
902
903 (dolist (service (ns-list-services))
904 (if (eq (car service) 'undefined)
905 (ns-define-service (cdr service))
906 (define-key global-map (vector (car service))
907 (ns-define-service (cdr service)))))
908
909 (if (and (eq (get-lisp-resource nil "NXAutoLaunch") t)
910 (eq (get-lisp-resource nil "HideOnAutoLaunch") t))
911 (add-hook 'after-init-hook 'ns-do-hide-emacs))
912
913 ;; FIXME: This will surely lead to "MODIFIED OUTSIDE CUSTOM" warnings.
914 (menu-bar-mode (if (get-lisp-resource nil "Menus") 1 -1))
915
916 (setq ns-initialized t))
917
918 (add-to-list 'handle-args-function-alist '(ns . x-handle-args))
919 (add-to-list 'frame-creation-function-alist '(ns . x-create-frame-with-faces))
920 (add-to-list 'window-system-initialization-alist '(ns . ns-initialize-window-system))
921
922
923 (provide 'ns-win)
924
925 ;;; ns-win.el ends here