1 ;;; ns-win.el --- lisp side of interface with NeXT/Open/GNUstep/MacOS X window system -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1994, 2005-2016 Free Software Foundation, Inc.
8 ;; Christophe de Dinechin
10 ;; Keywords: terminals
12 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
35 ;; startup.el will then examine startup files, and eventually call the hooks
36 ;; which create the first window (s).
38 ;; A number of other Nextstep convenience functions are defined in
39 ;; this file, which works in close coordination with src/nsfns.m.
42 (eval-when-compile (require 'cl-lib
))
44 (error "%s: Loading ns-win.el but not compiled for GNUstep/MacOS"
47 ;; Documentation-purposes only: actually loaded in loadup.el.
54 (require 'ucs-normalize
)
57 "GNUstep/Mac OS X specific features."
60 ;;;; Command line argument handling.
62 (defvar x-invocation-args
)
63 ;; Set in term/common-win.el; currently unused by Nextstep's x-open-connection.
64 (defvar x-command-line-resources
)
67 (defvar ns-input-file
)
69 (defun ns-handle-nxopen (_switch &optional temp
)
70 (setq unread-command-events
(append unread-command-events
71 (if temp
'(ns-open-temp-file)
73 ns-input-file
(append ns-input-file
(list (pop x-invocation-args
)))))
75 (defun ns-handle-nxopentemp (switch)
76 (ns-handle-nxopen switch t
))
78 (defun ns-ignore-1-arg (_switch)
79 (setq x-invocation-args
(cdr x-invocation-args
)))
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]+\\) ?\\)?\\)?\\)?"
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
)))))))))
99 ;;;; Keyboard mapping.
101 (define-obsolete-variable-alias 'ns-alternatives-map
'x-alternatives-map
"24.1")
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-?
] 'info
)
111 (define-key global-map
[?\s-^
] 'kill-some-buffers
)
112 (define-key global-map
[?\s-
&] 'kill-this-buffer
)
113 (define-key global-map
[?\s-C
] 'ns-popup-color-panel
)
114 (define-key global-map
[?\s-D
] 'dired
)
115 (define-key global-map
[?\s-E
] 'edit-abbrevs
)
116 (define-key global-map
[?\s-L
] 'shell-command
)
117 (define-key global-map
[?\s-M
] 'manual-entry
)
118 (define-key global-map
[?\s-S
] 'ns-write-file-using-panel
)
119 (define-key global-map
[?\s-a
] 'mark-whole-buffer
)
120 (define-key global-map
[?\s-c
] 'ns-copy-including-secondary
)
121 (define-key global-map
[?\s-d
] 'isearch-repeat-backward
)
122 (define-key global-map
[?\s-e
] 'isearch-yank-kill
)
123 (define-key global-map
[?\s-f
] 'isearch-forward
)
124 (define-key global-map
[?\s-g
] 'isearch-repeat-forward
)
125 (define-key global-map
[?\s-h
] 'ns-do-hide-emacs
)
126 (define-key global-map
[?\s-H
] 'ns-do-hide-others
)
127 (define-key global-map
[?\s-j
] 'exchange-point-and-mark
)
128 (define-key global-map
[?\s-k
] 'kill-this-buffer
)
129 (define-key global-map
[?\s-l
] 'goto-line
)
130 (define-key global-map
[?\s-m
] 'iconify-frame
)
131 (define-key global-map
[?\s-n
] 'make-frame
)
132 (define-key global-map
[?\s-o
] 'ns-open-file-using-panel
)
133 (define-key global-map
[?\s-p
] 'ns-print-buffer
)
134 (define-key global-map
[?\s-q
] 'save-buffers-kill-emacs
)
135 (define-key global-map
[?\s-s
] 'save-buffer
)
136 (define-key global-map
[?\s-t
] 'ns-popup-font-panel
)
137 (define-key global-map
[?\s-u
] 'revert-buffer
)
138 (define-key global-map
[?\s-v
] 'yank
)
139 (define-key global-map
[?\s-w
] 'delete-frame
)
140 (define-key global-map
[?\s-x
] 'kill-region
)
141 (define-key global-map
[?\s-y
] 'ns-paste-secondary
)
142 (define-key global-map
[?\s-z
] 'undo
)
143 (define-key global-map
[?\s-|
] 'shell-command-on-region
)
144 (define-key global-map
[s-kp-bar
] 'shell-command-on-region
)
145 ;; (as in Terminal.app)
146 (define-key global-map
[s-right
] 'ns-next-frame
)
147 (define-key global-map
[s-left
] 'ns-prev-frame
)
149 (define-key global-map
[home] 'beginning-of-buffer)
150 (define-key global-map [end] 'end-of-buffer)
151 (define-key global-map [kp-home] 'beginning-of-buffer)
152 (define-key global-map [kp-end] 'end-of-buffer)
153 (define-key global-map [kp-prior] 'scroll-down-command)
154 (define-key global-map [kp-next] 'scroll-up-command)
156 ;; Allow shift-clicks to work similarly to under Nextstep.
157 (define-key global-map [S-mouse-1] 'mouse-save-then-kill)
158 (global-unset-key [S-down-mouse-1])
160 ;; Special Nextstep-generated events are converted to function keys. Here
161 ;; are the bindings for them. Note, these keys are actually declared in
162 ;; x-setup-function-keys in common-win.
163 (define-key global-map [ns-power-off] 'save-buffers-kill-emacs)
164 (define-key global-map [ns-open-file] 'ns-find-file)
165 (define-key global-map [ns-open-temp-file] [ns-open-file])
166 (define-key global-map [ns-change-font] 'ns-respond-to-change-font)
167 (define-key global-map [ns-open-file-line] 'ns-open-file-select-line)
168 (define-key global-map [ns-spi-service-call] 'ns-spi-service-call)
169 (define-key global-map [ns-new-frame] 'make-frame)
170 (define-key global-map [ns-toggle-toolbar] 'ns-toggle-toolbar)
171 (define-key global-map [ns-show-prefs] 'customize)
174 ;; Set up a number of aliases and other layers to pretend we're using
175 ;; the Choi/Mitsuharu Carbon port.
177 (defvaralias 'mac-allow-anti-aliasing 'ns-antialias-text)
178 (defvaralias 'mac-command-modifier 'ns-command-modifier)
179 (defvaralias 'mac-right-command-modifier 'ns-right-command-modifier)
180 (defvaralias 'mac-control-modifier 'ns-control-modifier)
181 (defvaralias 'mac-right-control-modifier 'ns-right-control-modifier)
182 (defvaralias 'mac-option-modifier 'ns-option-modifier)
183 (defvaralias 'mac-right-option-modifier 'ns-right-option-modifier)
184 (defvaralias 'mac-function-modifier 'ns-function-modifier)
185 (declare-function ns-do-applescript "nsfns.m" (script))
186 (defalias 'do-applescript 'ns-do-applescript)
189 (declare-function ns-perform-service "nsfns.m" (service send))
191 (defun ns-define-service (path)
192 (let ((mapping [menu-bar services])
193 (service (mapconcat 'identity path "/"))
195 (subst-char-in-string
197 (mapconcat 'identity (cons "ns-service" path) "-")))))
198 ;; This defines the function.
203 (cond ((stringp arg) arg)
205 (buffer-substring (region-beginning) (region-end)))))
206 (out-string (ns-perform-service service in-string)))
208 ((stringp arg) out-string)
209 ((and out-string (or (not in-string)
210 (not (string= in-string out-string))))
211 (if mark-active (delete-region (region-beginning) (region-end)))
213 (setq deactivate-mark nil))))))
215 ((lookup-key global-map mapping)
217 (setq mapping (vconcat mapping (list (intern (car path)))))
218 (if (not (keymapp (lookup-key global-map mapping)))
219 (define-key global-map mapping
220 (cons (car path) (make-sparse-keymap (car path)))))
221 (setq path (cdr path)))
222 (setq mapping (vconcat mapping (list (intern (car path)))))
223 (define-key global-map mapping (cons (car path) name))))
227 (defvar ns-input-spi-name)
228 (defvar ns-input-spi-arg)
230 (declare-function dnd-open-file "dnd" (uri action))
232 (defun ns-spi-service-call ()
233 "Respond to a service request."
235 (cond ((string-equal ns-input-spi-name "open-selection")
236 (switch-to-buffer (generate-new-buffer "*untitled*"))
237 (insert ns-input-spi-arg))
238 ((string-equal ns-input-spi-name "open-file")
239 (dnd-open-file ns-input-spi-arg nil))
240 ((string-equal ns-input-spi-name "mail-selection")
244 (insert ns-input-spi-arg))
245 ((string-equal ns-input-spi-name "mail-to")
246 (compose-mail ns-input-spi-arg))
247 (t (error "Service %s not recognized" ns-input-spi-name))))
250 ;; Composed key sequence handling for Nextstep system input methods.
251 ;; (On Nextstep systems, input methods are provided for CJK
252 ;; characters, etc. which require multiple keystrokes, and during
253 ;; entry a partial ("working") result is typically shown in the
256 (defface ns-working-text-face
258 "Face used to highlight working text during compose sequence insert."
261 (defvar ns-working-overlay nil
262 "Overlay used to highlight working text during compose sequence insert.
263 When text is in th echo area, this just stores the length of the working text.")
265 (defvar ns-working-text) ; nsterm.m
267 ;; Test if in echo area, based on mac-win.el 2007/08/26 unicode-2.
268 ;; This will fail if called from a NONASCII_KEYSTROKE event on the global map.
269 (defun ns-in-echo-area ()
270 "Whether, for purposes of inserting working composition text, the minibuffer
271 is currently being used."
273 (and cursor-in-echo-area (current-message))
274 ;; Overlay strings are not shown in some cases.
275 (get-char-property (point) 'invisible)
277 (or (and (get-char-property (point) 'display)
278 (eq (get-char-property (1- (point)) 'display)
279 (get-char-property (point) 'display)))
280 (and (get-char-property (point) 'composition)
281 (eq (get-char-property (1- (point)) 'composition)
282 (get-char-property (point) 'composition)))))))
284 ;; The 'interactive' here stays for subinvocations, so the ns-in-echo-area
285 ;; always returns nil for some reason. If this WASN'T the case, we could
286 ;; map this to [ns-insert-working-text] and eliminate Fevals in nsterm.m.
287 ;; These functions test whether in echo area and delegate accordingly.
288 (defun ns-put-working-text ()
290 (if (ns-in-echo-area) (ns-echo-working-text) (ns-insert-working-text)))
291 (defun ns-unput-working-text ()
293 (ns-delete-working-text))
295 (defun ns-insert-working-text ()
296 "Insert contents of `ns-working-text' as UTF-8 string and mark with
297 `ns-working-overlay'. Any previously existing working text is cleared first.
298 The overlay is assigned the face `ns-working-text-face'."
299 ;; FIXME: if buffer is read-only, don't try to insert anything
300 ;; and if text is bound to a command, execute that instead (Bug#1453)
302 (ns-delete-working-text)
303 (let ((start (point)))
304 (insert ns-working-text)
305 (overlay-put (setq ns-working-overlay (make-overlay start (point)
306 (current-buffer) nil t))
307 'face 'ns-working-text-face)))
309 (defun ns-echo-working-text ()
310 "Echo contents of `ns-working-text' in message display area.
311 See `ns-insert-working-text'."
312 (ns-delete-working-text)
313 (let* ((msg (current-message))
314 (msglen (length msg))
316 (setq ns-working-overlay (length ns-working-text))
317 (setq msg (concat msg ns-working-text))
318 (put-text-property msglen (+ msglen ns-working-overlay)
319 'face 'ns-working-text-face msg)
322 (defun ns-delete-working-text()
323 "Delete working text and clear `ns-working-overlay'."
326 ((and (overlayp ns-working-overlay)
328 (overlay-buffer ns-working-overlay))
329 (with-current-buffer (overlay-buffer ns-working-overlay)
330 (delete-region (overlay-start ns-working-overlay)
331 (overlay-end ns-working-overlay))
332 (delete-overlay ns-working-overlay)))
333 ((integerp ns-working-overlay)
334 (let ((msg (current-message))
336 (setq msg (substring msg 0 (- (length msg) ns-working-overlay)))
337 (message "%s" msg))))
338 (setq ns-working-overlay nil))
341 ;; OS X file system Unicode UTF-8 NFD (decomposed form) support.
342 (when (eq system-type 'darwin)
343 ;; Used prior to Emacs 25.
344 (define-coding-system-alias 'utf-8-nfd 'utf-8-hfs)
346 (set-file-name-coding-system 'utf-8-hfs))
348 ;;;; Inter-app communications support.
350 (defun ns-insert-file ()
351 "Insert contents of file `ns-input-file' like insert-file but with less
352 prompting. If file is a directory perform a `find-file' on it."
354 (let ((f (pop ns-input-file)))
355 (if (file-directory-p f)
357 (push-mark (+ (point) (cadr (insert-file-contents f)))))))
359 (defvar ns-select-overlay nil
360 "Overlay used to highlight areas in files requested by Nextstep apps.")
361 (make-variable-buffer-local 'ns-select-overlay)
363 (defvar ns-input-line) ; nsterm.m
365 (defun ns-open-file-select-line ()
366 "Open a buffer containing the file `ns-input-file'.
367 Lines are highlighted according to `ns-input-line'."
371 ((and ns-input-line (buffer-modified-p))
372 (if ns-select-overlay
373 (setq ns-select-overlay (delete-overlay ns-select-overlay)))
375 (goto-char (point-min))
376 (forward-line (1- (if (consp ns-input-line)
377 (min (car ns-input-line) (cdr ns-input-line))
380 (if (not ns-select-overlay)
381 (overlay-put (setq ns-select-overlay (make-overlay (point-min)
384 (let ((beg (save-excursion
385 (goto-char (point-min))
386 (line-beginning-position
387 (if (consp ns-input-line)
388 (min (car ns-input-line) (cdr ns-input-line))
391 (goto-char (point-min))
392 (line-beginning-position
393 (1+ (if (consp ns-input-line)
394 (max (car ns-input-line) (cdr ns-input-line))
396 (move-overlay ns-select-overlay beg end)
400 (if ns-select-overlay
401 (setq ns-select-overlay (delete-overlay ns-select-overlay))))))
403 (defun ns-unselect-line ()
404 "Removes any Nextstep highlight a buffer may contain."
405 (if ns-select-overlay
406 (setq ns-select-overlay (delete-overlay ns-select-overlay))))
408 (add-hook 'first-change-hook 'ns-unselect-line)
410 ;;;; Preferences handling.
411 (declare-function ns-get-resource "nsfns.m" (owner name))
413 (defun get-lisp-resource (arg1 arg2)
414 (let ((res (ns-get-resource arg1 arg2)))
417 ((string-equal (upcase res) "YES") t)
418 ((string-equal (upcase res) "NO") nil)
423 (declare-function ns-read-file-name "nsfns.m"
424 (prompt &optional dir mustmatch init dir_only_p))
428 (defun x-file-dialog (prompt dir default_filename mustmatch only_dir_p)
429 "Read file name, prompting with PROMPT in directory DIR.
430 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
431 selection box, if specified. If MUSTMATCH is non-nil, the returned file
432 or directory must exist.
434 This function is only defined on NS, MS Windows, and X Windows with the
435 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
436 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories."
437 (ns-read-file-name prompt dir mustmatch default_filename only_dir_p))
439 (defun ns-open-file-using-panel ()
440 "Pop up open-file panel, and load the result in a buffer."
442 ;; Prompt dir defaultName isLoad initial.
443 (setq ns-input-file (ns-read-file-name "Select File to Load" nil t nil))
445 (and (setq ns-input-file (list ns-input-file)) (ns-find-file))))
447 (defun ns-write-file-using-panel ()
448 "Pop up save-file panel, and save buffer in resulting name."
450 (let (ns-output-file)
451 ;; Prompt dir defaultName isLoad initial.
452 (setq ns-output-file (ns-read-file-name "Save As" nil nil nil))
453 (message ns-output-file)
454 (if ns-output-file (write-file ns-output-file))))
456 (defcustom ns-pop-up-frames 'fresh
457 "Non-nil means open files upon request from the Workspace in a new frame.
458 If t, always do so. Any other non-nil value means open a new frame
459 unless the current buffer is a scratch buffer."
460 :type '(choice (const :tag "Never" nil)
461 (const :tag "Always" t)
462 (other :tag "Except for scratch buffer" fresh))
466 (declare-function ns-hide-emacs "nsfns.m" (on))
468 (defun ns-find-file ()
469 "Do a `find-file' with the `ns-input-file' as argument."
471 (let* ((f (file-truename
472 (expand-file-name (pop ns-input-file)
473 command-line-default-directory)))
474 (file (find-file-noselect f))
475 (bufwin1 (get-buffer-window file 'visible))
476 (bufwin2 (get-buffer-window "*scratch*" 'visible)))
479 (select-frame (window-frame bufwin1))
480 (raise-frame (window-frame bufwin1))
481 (select-window bufwin1))
482 ((and (eq ns-pop-up-frames 'fresh) bufwin2)
483 (ns-hide-emacs 'activate)
484 (select-frame (window-frame bufwin2))
485 (raise-frame (window-frame bufwin2))
486 (select-window bufwin2)
489 (ns-hide-emacs 'activate)
490 (let ((pop-up-frames t)) (pop-to-buffer file nil)))
492 (ns-hide-emacs 'activate)
496 (defun ns-drag-n-drop (event &optional new-frame force-text)
497 "Edit the files listed in the drag-n-drop EVENT.
498 Switch to a buffer editing the last file dropped."
500 (let* ((window (posn-window (event-start event)))
501 (arg (car (cdr (cdr event))))
503 (data (car (cdr arg)))
504 (url-or-string (cond ((eq type 'file)
505 (concat "file:" data))
507 (set-frame-selected-window nil window)
509 (select-frame (make-frame)))
511 (setq window (selected-window))
513 (dnd-insert-text window 'private data)
514 (dnd-handle-one-url window 'private url-or-string))))
517 (defun ns-drag-n-drop-other-frame (event)
518 "Edit the files listed in the drag-n-drop EVENT, in other frames.
519 May create new frames, or reuse existing ones. The frame editing
520 the last file dropped is selected."
522 (ns-drag-n-drop event t))
524 (defun ns-drag-n-drop-as-text (event)
525 "Drop the data in EVENT as text."
527 (ns-drag-n-drop event nil t))
529 (defun ns-drag-n-drop-as-text-other-frame (event)
530 "Drop the data in EVENT as text in a new frame."
532 (ns-drag-n-drop event t t))
534 (global-set-key [drag-n-drop] 'ns-drag-n-drop)
535 (global-set-key [C-drag-n-drop] 'ns-drag-n-drop-other-frame)
536 (global-set-key [M-drag-n-drop] 'ns-drag-n-drop-as-text)
537 (global-set-key [C-M-drag-n-drop] 'ns-drag-n-drop-as-text-other-frame)
539 ;;;; Frame-related functions.
542 (defvar ns-alternate-modifier)
543 (defvar ns-right-alternate-modifier)
544 (defvar ns-right-command-modifier)
545 (defvar ns-right-control-modifier)
547 ;; You say tomAYto, I say tomAHto..
548 (defvaralias 'ns-option-modifier 'ns-alternate-modifier)
549 (defvaralias 'ns-right-option-modifier 'ns-right-alternate-modifier)
551 (defun ns-do-hide-emacs ()
555 (declare-function ns-hide-others "nsfns.m" ())
557 (defun ns-do-hide-others ()
561 (declare-function ns-emacs-info-panel "nsfns.m" ())
563 (defun ns-do-emacs-info-panel ()
565 (ns-emacs-info-panel))
567 (defun ns-next-frame ()
568 "Switch to next visible frame."
572 (defun ns-prev-frame ()
573 "Switch to previous visible frame."
577 ;; Frame will be focused anyway, so select it
578 ;; (if this is not done, mode line is dimmed until first interaction)
579 ;; FIXME: Sounds like we're working around a bug in the underlying code.
580 (add-hook 'after-make-frame-functions 'select-frame)
582 (defvar tool-bar-mode)
583 (declare-function tool-bar-mode "tool-bar" (&optional arg))
585 ;; Based on a function by David Reitter <dreitter@inf.ed.ac.uk> ;
586 ;; see http://lists.gnu.org/archive/html/emacs-devel/2005-09/msg00681.html .
587 (defun ns-toggle-toolbar (&optional frame)
588 "Switches the tool bar on and off in frame FRAME.
589 If FRAME is nil, the change applies to the selected frame."
591 (modify-frame-parameters
592 frame (list (cons 'tool-bar-lines
593 (if (> (or (frame-parameter frame 'tool-bar-lines) 0) 0)
595 (if (not tool-bar-mode) (tool-bar-mode t)))
598 ;;;; Dialog-related functions.
600 ;; Ask user for confirm before printing. Due to Kevin Rodgers.
601 (defun ns-print-buffer ()
602 "Interactive front-end to `print-buffer': asks for user confirmation first."
604 (if (and (called-interactively-p 'interactive)
605 (or (listp last-nonmenu-event)
606 (and (char-or-string-p (event-basic-type last-command-event))
607 (memq 'super (event-modifiers last-command-event)))))
608 (let ((last-nonmenu-event (if (listp last-nonmenu-event)
611 `(mouse-1 POSITION 1))))
612 (if (y-or-n-p (format "Print buffer %s? " (buffer-name)))
619 ;; Needed for font listing functions under both backend and normal
620 (setq scalable-fonts-allowed t)
622 ;; Set to use font panel instead
623 (declare-function ns-popup-font-panel "nsfns.m" (&optional frame))
624 (defalias 'x-select-font 'ns-popup-font-panel "Pop up the font panel.
625 This function has been overloaded in Nextstep.")
626 (defalias 'mouse-set-font 'ns-popup-font-panel "Pop up the font panel.
627 This function has been overloaded in Nextstep.")
630 (defvar ns-input-font)
631 (defvar ns-input-fontsize)
633 (defun ns-respond-to-change-font ()
634 "Respond to changeFont: event, expecting `ns-input-font' and\n\
635 `ns-input-fontsize' of new font."
637 (modify-frame-parameters (selected-frame)
638 (list (cons 'fontsize ns-input-fontsize)))
639 (modify-frame-parameters (selected-frame)
640 (list (cons 'font ns-input-font)))
641 (set-frame-font ns-input-font))
644 ;; Default fontset for Mac OS X. This is mainly here to show how a fontset
645 ;; can be set up manually. Ordinarily, fontsets are auto-created whenever
646 ;; a font is chosen by
647 (defvar ns-standard-fontset-spec
648 ;; Only some code supports this so far, so use uglier XLFD version
649 ;; "-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard,latin:Courier,han:Kai"
651 '("-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard"
652 "latin:-*-Courier-*-*-*-*-10-*-*-*-*-*-iso10646-1"
653 "han:-*-Kai-*-*-*-*-10-*-*-*-*-*-iso10646-1"
654 "cyrillic:-*-Trebuchet$MS-*-*-*-*-10-*-*-*-*-*-iso10646-1")
656 "String of fontset spec of the standard fontset.
657 This defines a fontset consisting of the Courier and other fonts that
659 See the documentation of `create-fontset-from-fontset-spec' for the format.")
661 (defvar ns-reg-to-script) ; nsfont.m
663 ;; This maps font registries (not exposed by NS APIs for font selection) to
664 ;; Unicode scripts (which can be mapped to Unicode character ranges which are).
665 ;; See ../international/fontset.el
666 (setq ns-reg-to-script
667 '(("iso8859-1" . latin)
668 ("iso8859-2" . latin)
669 ("iso8859-3" . latin)
670 ("iso8859-4" . latin)
671 ("iso8859-5" . cyrillic)
672 ("microsoft-cp1251" . cyrillic)
673 ("koi8-r" . cyrillic)
674 ("iso8859-6" . arabic)
675 ("iso8859-7" . greek)
676 ("iso8859-8" . hebrew)
677 ("iso8859-9" . latin)
678 ("iso8859-10" . latin)
679 ("iso8859-11" . thai)
681 ("iso8859-13" . latin)
682 ("iso8859-14" . latin)
683 ("iso8859-15" . latin)
684 ("iso8859-16" . latin)
685 ("viscii1.1-1" . latin)
690 ("gb2312.1980" . han)
695 ("sisheng_cwnn" . bopomofo)
696 ("ksc5601.1987" . hangul)
697 ("ethiopic-unicode" . ethiopic)
698 ("is13194-devanagari" . indian-is13194)
699 ("iso10646.indian-1" . devanagari)))
702 ;;;; Pasteboard support.
704 (define-obsolete-function-alias 'ns-store-cut-buffer-internal
705 'gui-set-selection "24.1")
708 (defun ns-copy-including-secondary ()
710 (call-interactively 'kill-ring-save)
711 (gui-set-selection 'SECONDARY (buffer-substring (point) (mark t))))
713 (defun ns-paste-secondary ()
715 (insert (gui-get-selection 'SECONDARY)))
718 ;;;; Scrollbar handling.
720 (global-set-key [vertical-scroll-bar down-mouse-1] 'ns-handle-scroll-bar-event)
721 (global-unset-key [vertical-scroll-bar mouse-1])
722 (global-unset-key [vertical-scroll-bar drag-mouse-1])
724 (declare-function scroll-bar-scale "scroll-bar" (num-denom whole))
726 (defun ns-scroll-bar-move (event)
727 "Scroll the frame according to a Nextstep scroller event."
729 (let* ((pos (event-end event))
732 (with-current-buffer (window-buffer window)
734 ((eq (car scale) (cdr scale))
735 (goto-char (point-max)))
737 (goto-char (point-min)))
739 (goto-char (+ (point-min) 1
740 (scroll-bar-scale scale (- (point-max) (point-min)))))))
742 (set-window-start window (point))
743 (vertical-motion (/ (window-height window) 2) window))))
745 (defun ns-handle-scroll-bar-event (event)
746 "Handle scroll bar EVENT to emulate Nextstep style scrolling."
748 (let* ((position (event-start event))
749 (bar-part (nth 4 position))
750 (window (nth 0 position))
751 (old-window (selected-window)))
753 ((eq bar-part 'ratio)
754 (ns-scroll-bar-move event))
755 ((eq bar-part 'handle)
756 (if (eq window (selected-window))
757 (track-mouse (ns-scroll-bar-move event))
758 ;; track-mouse faster for selected window, slower for unselected.
759 (ns-scroll-bar-move event)))
761 (select-window window)
764 (goto-char (window-start window))
766 ((eq bar-part 'above-handle)
768 ((eq bar-part 'below-handle)
771 (goto-char (window-start window))
773 (select-window old-window)))))
778 ;; Functions for color panel + drag
779 (defun ns-face-at-pos (pos)
780 (let* ((frame (car pos))
781 (frame-pos (cons (cadr pos) (cddr pos)))
782 (window (window-at (car frame-pos) (cdr frame-pos) frame))
783 (window-pos (coordinates-in-window-p frame-pos window))
784 (buffer (window-buffer window))
785 (edges (window-edges window)))
789 ((eq window-pos 'mode-line)
791 ((eq window-pos 'vertical-line)
794 (with-current-buffer buffer
795 (let ((p (car (compute-motion (window-start window)
796 (cons (nth 0 edges) (nth 1 edges))
799 (- (window-width window) 1)
803 ((eq p (window-point window))
805 ((and mark-active (< (region-beginning) p) (< p (region-end)))
808 (let ((faces (get-char-property p 'face window)))
809 (if (consp faces) (car faces) faces)))))))
813 (defun ns-suspend-error ()
814 ;; Don't allow suspending if any of the frames are NS frames.
815 (if (memq 'ns (mapcar 'window-system (frame-list)))
816 (error "Cannot suspend Emacs while running under NS")))
819 ;; Set some options to be as Nextstep-like as possible.
820 (setq frame-title-format t
824 (defvar ns-initialized nil
825 "Non-nil if Nextstep windowing has been initialized.")
827 (declare-function x-handle-args "common-win" (args))
828 (declare-function ns-list-services "nsfns.m" ())
829 (declare-function x-open-connection "nsfns.m"
830 (display &optional xrm-string must-succeed))
831 (declare-function ns-set-resource "nsfns.m" (owner name value))
833 ;; Do the actual Nextstep Windows setup here; the above code just
834 ;; defines functions and variables that we use now.
835 (cl-defmethod window-system-initialization (&context (window-system ns)
837 "Initialize Emacs for Nextstep (Cocoa / GNUstep) windowing."
838 (cl-assert (not ns-initialized))
840 ;; PENDING: not needed?
841 (setq command-line-args (x-handle-args command-line-args))
843 ;; Setup the default fontset.
844 (create-default-fontset)
845 ;; Create the standard fontset.
847 (create-fontset-from-fontset-spec ns-standard-fontset-spec t)
848 (error (display-warning
850 (format "Creation of the standard fontset failed: %s" err)
853 (x-open-connection (system-name) x-command-line-resources t)
855 ;; Add GNUstep menu items Services, Hide and Quit. Rename Help to Info
856 ;; and put it first (i.e. omit from menu-bar-final-items.
857 (if (featurep 'gnustep)
859 (setq menu-bar-final-items '(buffer services hide-app quit))
861 ;; If running under GNUstep, "Help" is moved and renamed "Info".
862 (bindings--define-key global-map [menu-bar help-menu]
863 (cons "Info" menu-bar-help-menu))
864 (bindings--define-key global-map [menu-bar quit]
865 '(menu-item "Quit" save-buffers-kill-emacs
866 :help "Save unsaved buffers, then exit"))
867 (bindings--define-key global-map [menu-bar hide-app]
868 '(menu-item "Hide" ns-do-hide-emacs
870 (bindings--define-key global-map [menu-bar services]
871 (cons "Services" (make-sparse-keymap "Services")))))
874 (dolist (service (ns-list-services))
875 (if (eq (car service) 'undefined)
876 (ns-define-service (cdr service))
877 (define-key global-map (vector (car service))
878 (ns-define-service (cdr service)))))
880 (if (and (eq (get-lisp-resource nil "NXAutoLaunch") t)
881 (eq (get-lisp-resource nil "HideOnAutoLaunch") t))
882 (add-hook 'after-init-hook 'ns-do-hide-emacs))
884 ;; FIXME: This will surely lead to "MODIFIED OUTSIDE CUSTOM" warnings.
885 (menu-bar-mode (if (get-lisp-resource nil "Menus") 1 -1))
887 ;; For Darwin nothing except UTF-8 makes sense.
888 (when (eq system-type 'darwin)
889 (add-hook 'before-init-hook
891 (setq locale-coding-system 'utf-8-unix)
892 (setq default-process-coding-system
893 '(utf-8-unix . utf-8-unix)))))
895 ;; OS X Lion introduces PressAndHold, which is unsupported by this port.
896 ;; See this thread for more details:
897 ;; http://lists.gnu.org/archive/html/emacs-devel/2011-06/msg00505.html
898 (ns-set-resource nil "ApplePressAndHoldEnabled" "NO")
900 (x-apply-session-resources)
902 ;; Don't let Emacs suspend under NS.
903 (add-hook 'suspend-hook 'ns-suspend-error)
905 (setq ns-initialized t))
907 ;; Any display name is OK.
908 (add-to-list 'display-format-alist '(".*" . ns))
909 (cl-defmethod handle-args-function (args &context (window-system ns))
910 (x-handle-args args))
912 (cl-defmethod frame-creation-function (params &context (window-system ns))
913 (x-create-frame-with-faces params))
915 (declare-function ns-own-selection-internal "nsselect.m" (selection value))
916 (declare-function ns-disown-selection-internal "nsselect.m" (selection))
917 (declare-function ns-selection-owner-p "nsselect.m" (&optional selection))
918 (declare-function ns-selection-exists-p "nsselect.m" (&optional selection))
919 (declare-function ns-get-selection "nsselect.m" (selection-symbol target-type))
921 (cl-defmethod gui-backend-set-selection (selection value
922 &context (window-system ns))
923 (if value (ns-own-selection-internal selection value)
924 (ns-disown-selection-internal selection)))
926 (cl-defmethod gui-backend-selection-owner-p (selection
927 &context (window-system ns))
928 (ns-selection-owner-p selection))
930 (cl-defmethod gui-backend-selection-exists-p (selection
931 &context (window-system ns))
932 (ns-selection-exists-p selection))
934 (cl-defmethod gui-backend-get-selection (selection-symbol target-type
935 &context (window-system ns))
936 (ns-get-selection selection-symbol target-type))
939 (provide 'term/ns-win)
941 ;;; ns-win.el ends here