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