; doc/emacs/misc.texi (Network Security): Fix typo.
[emacs.git] / lisp / term / ns-win.el
blob8b23cab0100daa202783be3d6c79ffb593581a94
1 ;;; ns-win.el --- lisp side of interface with NeXT/Open/GNUstep/macOS window system -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1994, 2005-2018 Free Software Foundation, Inc.
5 ;; Authors: Carl Edman
6 ;; Christian Limpach
7 ;; Scott Bender
8 ;; Christophe de Dinechin
9 ;; Adrian Robert
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 <https://www.gnu.org/licenses/>.
27 ;;; Commentary:
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.
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))
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 (require 'dnd)
54 (require 'ucs-normalize)
56 (defgroup ns nil
57 "GNUstep/macOS specific features."
58 :group 'environment)
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)
66 ;; nsterm.m.
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)
72 '(ns-open-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]+\\) ?\\)?\\)?\\)?"
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)))))))))
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-current-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 [?\M-\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-current-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-+] 'text-scale-adjust)
145 (define-key global-map [?\s-=] 'text-scale-adjust)
146 (define-key global-map [?\s--] 'text-scale-adjust)
147 (define-key global-map [?\s-0] 'text-scale-adjust)
148 (define-key global-map [?\s-|] 'shell-command-on-region)
149 (define-key global-map [s-kp-bar] 'shell-command-on-region)
150 (define-key global-map [?\C-\s- ] 'ns-do-show-character-palette)
151 ;; (as in Terminal.app)
152 (define-key global-map [s-right] 'ns-next-frame)
153 (define-key global-map [s-left] 'ns-prev-frame)
155 (define-key global-map [home] 'beginning-of-buffer)
156 (define-key global-map [end] 'end-of-buffer)
157 (define-key global-map [kp-home] 'beginning-of-buffer)
158 (define-key global-map [kp-end] 'end-of-buffer)
159 (define-key global-map [kp-prior] 'scroll-down-command)
160 (define-key global-map [kp-next] 'scroll-up-command)
162 ;; Allow shift-clicks to work similarly to under Nextstep.
163 (define-key global-map [S-mouse-1] 'mouse-save-then-kill)
164 (global-unset-key [S-down-mouse-1])
166 ;; Special Nextstep-generated events are converted to function keys. Here
167 ;; are the bindings for them. Note, these keys are actually declared in
168 ;; x-setup-function-keys in common-win.
169 (define-key global-map [ns-power-off] 'save-buffers-kill-emacs)
170 (define-key global-map [ns-open-file] 'ns-find-file)
171 (define-key global-map [ns-open-temp-file] [ns-open-file])
172 (define-key global-map [ns-change-font] 'ns-respond-to-change-font)
173 (define-key global-map [ns-open-file-line] 'ns-open-file-select-line)
174 (define-key global-map [ns-spi-service-call] 'ns-spi-service-call)
175 (define-key global-map [ns-new-frame] 'make-frame)
176 (define-key global-map [ns-toggle-toolbar] 'ns-toggle-toolbar)
177 (define-key global-map [ns-show-prefs] 'customize)
180 ;; Set up a number of aliases and other layers to pretend we're using
181 ;; the Choi/Mitsuharu Carbon port.
183 (defvaralias 'mac-allow-anti-aliasing 'ns-antialias-text)
184 (defvaralias 'mac-command-modifier 'ns-command-modifier)
185 (defvaralias 'mac-right-command-modifier 'ns-right-command-modifier)
186 (defvaralias 'mac-control-modifier 'ns-control-modifier)
187 (defvaralias 'mac-right-control-modifier 'ns-right-control-modifier)
188 (defvaralias 'mac-option-modifier 'ns-option-modifier)
189 (defvaralias 'mac-right-option-modifier 'ns-right-option-modifier)
190 (defvaralias 'mac-function-modifier 'ns-function-modifier)
191 (declare-function ns-do-applescript "nsfns.m" (script))
192 (defalias 'do-applescript 'ns-do-applescript)
194 ;;;; Services
195 (declare-function ns-perform-service "nsfns.m" (service send))
197 (defun ns-define-service (path)
198 (let ((mapping [menu-bar services])
199 (service (mapconcat 'identity path "/"))
200 (name (intern
201 (subst-char-in-string
202 ?\s ?-
203 (mapconcat 'identity (cons "ns-service" path) "-")))))
204 ;; This defines the function.
205 (defalias name
206 (lambda (arg)
207 (interactive "p")
208 (let* ((in-string
209 (cond ((stringp arg) arg)
210 (mark-active
211 (buffer-substring (region-beginning) (region-end)))))
212 (out-string (ns-perform-service service in-string)))
213 (cond
214 ((stringp arg) out-string)
215 ((and out-string (or (not in-string)
216 (not (string= in-string out-string))))
217 (if mark-active (delete-region (region-beginning) (region-end)))
218 (insert out-string)
219 (setq deactivate-mark nil))))))
220 (cond
221 ((lookup-key global-map mapping)
222 (while (cdr path)
223 (setq mapping (vconcat mapping (list (intern (car path)))))
224 (if (not (keymapp (lookup-key global-map mapping)))
225 (define-key global-map mapping
226 (cons (car path) (make-sparse-keymap (car path)))))
227 (setq path (cdr path)))
228 (setq mapping (vconcat mapping (list (intern (car path)))))
229 (define-key global-map mapping (cons (car path) name))))
230 name))
232 ;; nsterm.m
233 (defvar ns-input-spi-name)
234 (defvar ns-input-spi-arg)
236 (declare-function dnd-open-file "dnd" (uri action))
238 ;; Handles multiline strings that are passed to the "open-file" service.
239 (defun ns-open-file-service (filenames)
240 "Open multiple files when selecting a multiline string FILENAMES."
241 (let ((filelist (split-string filenames "[\n\r]+" t "[ \u00A0\t]+")))
242 ;; The path strings are trimmed for spaces, nbsp and tabs.
243 (dolist (filestring filelist)
244 (dnd-open-file filestring nil))))
247 (defun ns-spi-service-call ()
248 "Respond to a service request."
249 (interactive)
250 (cond ((string-equal ns-input-spi-name "open-selection")
251 (switch-to-buffer (generate-new-buffer "*untitled*"))
252 (insert ns-input-spi-arg))
253 ((string-equal ns-input-spi-name "open-file")
254 (ns-open-file-service ns-input-spi-arg))
255 ((string-equal ns-input-spi-name "mail-selection")
256 (compose-mail)
257 (rfc822-goto-eoh)
258 (forward-line 1)
259 (insert ns-input-spi-arg))
260 ((string-equal ns-input-spi-name "mail-to")
261 (compose-mail ns-input-spi-arg))
262 (t (error "Service %s not recognized" ns-input-spi-name))))
265 ;; Composed key sequence handling for Nextstep system input methods.
266 ;; (On Nextstep systems, input methods are provided for CJK
267 ;; characters, etc. which require multiple keystrokes, and during
268 ;; entry a partial ("working") result is typically shown in the
269 ;; editing window.)
271 (defface ns-working-text-face
272 '((t :underline t))
273 "Face used to highlight working text during compose sequence insert."
274 :group 'ns)
276 (defvar ns-working-overlay nil
277 "Overlay used to highlight working text during compose sequence insert.
278 When text is in th echo area, this just stores the length of the working text.")
280 (defvar ns-working-text) ; nsterm.m
282 ;; Test if in echo area, based on mac-win.el 2007/08/26 unicode-2.
283 ;; This will fail if called from a NONASCII_KEYSTROKE event on the global map.
284 (defun ns-in-echo-area ()
285 "Whether, for purposes of inserting working composition text, the minibuffer
286 is currently being used."
287 (or isearch-mode
288 (and cursor-in-echo-area (current-message))
289 ;; Overlay strings are not shown in some cases.
290 (get-char-property (point) 'invisible)
291 (and (not (bobp))
292 (or (and (get-char-property (point) 'display)
293 (eq (get-char-property (1- (point)) 'display)
294 (get-char-property (point) 'display)))
295 (and (get-char-property (point) 'composition)
296 (eq (get-char-property (1- (point)) 'composition)
297 (get-char-property (point) 'composition)))))))
299 ;; The 'interactive' here stays for subinvocations, so the ns-in-echo-area
300 ;; always returns nil for some reason. If this WASN'T the case, we could
301 ;; map this to [ns-insert-working-text] and eliminate Fevals in nsterm.m.
302 ;; These functions test whether in echo area and delegate accordingly.
303 (defun ns-put-working-text ()
304 (interactive)
305 (if (ns-in-echo-area) (ns-echo-working-text) (ns-insert-working-text)))
306 (defun ns-unput-working-text ()
307 (interactive)
308 (ns-delete-working-text))
310 (defun ns-insert-working-text ()
311 "Insert contents of `ns-working-text' as UTF-8 string and mark with
312 `ns-working-overlay'. Any previously existing working text is cleared first.
313 The overlay is assigned the face `ns-working-text-face'."
314 ;; FIXME: if buffer is read-only, don't try to insert anything, and
315 ;; if text is bound to a command, execute that instead (Bug#1453).
316 (interactive)
317 (ns-delete-working-text)
318 (let ((start (point)))
319 (insert ns-working-text)
320 (overlay-put (setq ns-working-overlay (make-overlay start (point)
321 (current-buffer) nil t))
322 'face 'ns-working-text-face)))
324 (defun ns-echo-working-text ()
325 "Echo contents of `ns-working-text' in message display area.
326 See `ns-insert-working-text'."
327 (ns-delete-working-text)
328 (let* ((msg (current-message))
329 (msglen (length msg))
330 message-log-max)
331 (setq ns-working-overlay (length ns-working-text))
332 (setq msg (concat msg ns-working-text))
333 (put-text-property msglen (+ msglen ns-working-overlay)
334 'face 'ns-working-text-face msg)
335 (message "%s" msg)))
337 (defun ns-delete-working-text()
338 "Delete working text and clear `ns-working-overlay'."
339 (interactive)
340 (cond
341 ((and (overlayp ns-working-overlay)
342 ;; Still alive?
343 (overlay-buffer ns-working-overlay))
344 (with-current-buffer (overlay-buffer ns-working-overlay)
345 (delete-region (overlay-start ns-working-overlay)
346 (overlay-end ns-working-overlay))
347 (delete-overlay ns-working-overlay)))
348 ((integerp ns-working-overlay)
349 (let ((msg (current-message))
350 message-log-max)
351 (setq msg (substring msg 0 (- (length msg) ns-working-overlay)))
352 (message "%s" msg))))
353 (setq ns-working-overlay nil))
356 ;; macOS file system Unicode UTF-8 NFD (decomposed form) support.
357 (when (eq system-type 'darwin)
358 ;; Used prior to Emacs 25.
359 (define-coding-system-alias 'utf-8-nfd 'utf-8-hfs)
361 (set-file-name-coding-system 'utf-8-hfs-unix))
363 ;;;; Inter-app communications support.
365 (defun ns-insert-file ()
366 "Insert contents of file `ns-input-file' like insert-file but with less
367 prompting. If file is a directory perform a `find-file' on it."
368 (interactive)
369 (let ((f (pop ns-input-file)))
370 (if (file-directory-p f)
371 (find-file f)
372 (push-mark (+ (point) (cadr (insert-file-contents f)))))))
374 (defvar ns-select-overlay nil
375 "Overlay used to highlight areas in files requested by Nextstep apps.")
376 (make-variable-buffer-local 'ns-select-overlay)
378 (defvar ns-input-line) ; nsterm.m
380 (defun ns-open-file-select-line ()
381 "Open a buffer containing the file `ns-input-file'.
382 Lines are highlighted according to `ns-input-line'."
383 (interactive)
384 (ns-find-file)
385 (cond
386 ((and ns-input-line (buffer-modified-p))
387 (if ns-select-overlay
388 (setq ns-select-overlay (delete-overlay ns-select-overlay)))
389 (deactivate-mark)
390 (goto-char (point-min))
391 (forward-line (1- (if (consp ns-input-line)
392 (min (car ns-input-line) (cdr ns-input-line))
393 ns-input-line))))
394 (ns-input-line
395 (if (not ns-select-overlay)
396 (overlay-put (setq ns-select-overlay (make-overlay (point-min)
397 (point-min)))
398 'face 'highlight))
399 (let ((beg (save-excursion
400 (goto-char (point-min))
401 (line-beginning-position
402 (if (consp ns-input-line)
403 (min (car ns-input-line) (cdr ns-input-line))
404 ns-input-line))))
405 (end (save-excursion
406 (goto-char (point-min))
407 (line-beginning-position
408 (1+ (if (consp ns-input-line)
409 (max (car ns-input-line) (cdr ns-input-line))
410 ns-input-line))))))
411 (move-overlay ns-select-overlay beg end)
412 (deactivate-mark)
413 (goto-char beg)))
415 (if ns-select-overlay
416 (setq ns-select-overlay (delete-overlay ns-select-overlay))))))
418 (defun ns-unselect-line ()
419 "Removes any Nextstep highlight a buffer may contain."
420 (if ns-select-overlay
421 (setq ns-select-overlay (delete-overlay ns-select-overlay))))
423 (add-hook 'first-change-hook 'ns-unselect-line)
425 ;;;; Preferences handling.
426 (declare-function ns-get-resource "nsfns.m" (owner name))
428 (defun get-lisp-resource (arg1 arg2)
429 (let ((res (ns-get-resource arg1 arg2)))
430 (cond
431 ((not res) 'unbound)
432 ((string-equal (upcase res) "YES") t)
433 ((string-equal (upcase res) "NO") nil)
434 (t (read res)))))
436 ;; nsterm.m
438 (declare-function ns-read-file-name "nsfns.m"
439 (prompt &optional dir mustmatch init dir_only_p))
441 ;;;; File handling.
443 (defun x-file-dialog (prompt dir default_filename mustmatch only_dir_p)
444 "SKIP: real doc in xfns.c."
445 (ns-read-file-name prompt dir mustmatch default_filename only_dir_p))
447 (defun ns-open-file-using-panel ()
448 "Pop up open-file panel, and load the result in a buffer."
449 (interactive)
450 ;; Prompt dir defaultName isLoad initial.
451 (setq ns-input-file (ns-read-file-name "Select File to Load" nil t nil))
452 (if ns-input-file
453 (and (setq ns-input-file (list ns-input-file)) (ns-find-file))))
455 (defun ns-write-file-using-panel ()
456 "Pop up save-file panel, and save buffer in resulting name."
457 (interactive)
458 (let (ns-output-file)
459 ;; Prompt dir defaultName isLoad initial.
460 (setq ns-output-file (ns-read-file-name "Save As" nil nil nil))
461 (message ns-output-file)
462 (if ns-output-file (write-file ns-output-file))))
464 (defcustom ns-pop-up-frames 'fresh
465 "Non-nil means open files upon request from the Workspace in a new frame.
466 If t, always do so. Any other non-nil value means open a new frame
467 unless the current buffer is a scratch buffer."
468 :type '(choice (const :tag "Never" nil)
469 (const :tag "Always" t)
470 (other :tag "Except for scratch buffer" fresh))
471 :version "23.1"
472 :group 'ns)
474 (declare-function ns-hide-emacs "nsfns.m" (on))
476 (defun ns-find-file ()
477 "Do a `find-file' with the `ns-input-file' as argument."
478 (interactive)
479 (let* ((f (file-truename
480 (expand-file-name (pop ns-input-file)
481 command-line-default-directory)))
482 (file (find-file-noselect f))
483 (bufwin1 (get-buffer-window file 'visible))
484 (bufwin2 (get-buffer-window "*scratch*" 'visible)))
485 (cond
486 (bufwin1
487 (select-frame (window-frame bufwin1))
488 (raise-frame (window-frame bufwin1))
489 (select-window bufwin1))
490 ((and (eq ns-pop-up-frames 'fresh) bufwin2)
491 (ns-hide-emacs 'activate)
492 (select-frame (window-frame bufwin2))
493 (raise-frame (window-frame bufwin2))
494 (select-window bufwin2)
495 (find-file f))
496 (ns-pop-up-frames
497 (ns-hide-emacs 'activate)
498 (let ((pop-up-frames t)) (pop-to-buffer file nil)))
500 (ns-hide-emacs 'activate)
501 (find-file f)))))
504 (defun ns-drag-n-drop (event &optional new-frame force-text)
505 "Edit the files listed in the drag-n-drop EVENT.
506 Switch to a buffer editing the last file dropped."
507 (interactive "e")
508 (let* ((window (posn-window (event-start event)))
509 (arg (car (cdr (cdr event))))
510 (type (car arg))
511 (data (car (cdr arg)))
512 (url-or-string (cond ((eq type 'file)
513 (concat "file:" data))
514 (t data))))
515 (set-frame-selected-window nil window)
516 (when new-frame
517 (select-frame (make-frame)))
518 (raise-frame)
519 (setq window (selected-window))
520 (if force-text
521 (dnd-insert-text window 'private data)
522 (dnd-handle-one-url window 'private url-or-string))))
525 (defun ns-drag-n-drop-other-frame (event)
526 "Edit the files listed in the drag-n-drop EVENT, in other frames.
527 May create new frames, or reuse existing ones. The frame editing
528 the last file dropped is selected."
529 (interactive "e")
530 (ns-drag-n-drop event t))
532 (defun ns-drag-n-drop-as-text (event)
533 "Drop the data in EVENT as text."
534 (interactive "e")
535 (ns-drag-n-drop event nil t))
537 (defun ns-drag-n-drop-as-text-other-frame (event)
538 "Drop the data in EVENT as text in a new frame."
539 (interactive "e")
540 (ns-drag-n-drop event t t))
542 (global-set-key [drag-n-drop] 'ns-drag-n-drop)
543 (global-set-key [C-drag-n-drop] 'ns-drag-n-drop-other-frame)
544 (global-set-key [M-drag-n-drop] 'ns-drag-n-drop-as-text)
545 (global-set-key [C-M-drag-n-drop] 'ns-drag-n-drop-as-text-other-frame)
547 ;;;; Frame-related functions.
549 ;; nsterm.m
550 (defvar ns-alternate-modifier)
551 (defvar ns-right-alternate-modifier)
552 (defvar ns-right-command-modifier)
553 (defvar ns-right-control-modifier)
555 ;; You say tomAYto, I say tomAHto..
556 (with-no-warnings
557 (defvaralias 'ns-option-modifier 'ns-alternate-modifier)
558 (defvaralias 'ns-right-option-modifier 'ns-right-alternate-modifier))
560 (defun ns-do-hide-emacs ()
561 (interactive)
562 (ns-hide-emacs t))
564 (declare-function ns-hide-others "nsfns.m" ())
566 (defun ns-do-hide-others ()
567 (interactive)
568 (ns-hide-others))
570 (declare-function ns-emacs-info-panel "nsfns.m" ())
572 (defun ns-do-emacs-info-panel ()
573 (interactive)
574 (ns-emacs-info-panel))
576 (declare-function ns-show-character-palette "nsfns.m" ())
578 (defun ns-do-show-character-palette ()
579 (interactive)
580 (ns-show-character-palette))
582 (defun ns-next-frame ()
583 "Switch to next visible frame."
584 (interactive)
585 (other-frame 1))
587 (defun ns-prev-frame ()
588 "Switch to previous visible frame."
589 (interactive)
590 (other-frame -1))
592 ;; Frame will be focused anyway, so select it
593 ;; (if this is not done, mode line is dimmed until first interaction)
594 ;; FIXME: Sounds like we're working around a bug in the underlying code.
595 (add-hook 'after-make-frame-functions 'select-frame)
597 (defvar tool-bar-mode)
598 (declare-function tool-bar-mode "tool-bar" (&optional arg))
600 ;; Based on a function by David Reitter <dreitter@inf.ed.ac.uk> ;
601 ;; see https://lists.gnu.org/r/emacs-devel/2005-09/msg00681.html .
602 (defun ns-toggle-toolbar (&optional frame)
603 "Switches the tool bar on and off in frame FRAME.
604 If FRAME is nil, the change applies to the selected frame."
605 (interactive)
606 (modify-frame-parameters
607 frame (list (cons 'tool-bar-lines
608 (if (> (or (frame-parameter frame 'tool-bar-lines) 0) 0)
609 0 1)) ))
610 (if (not tool-bar-mode) (tool-bar-mode t)))
613 ;;;; Dialog-related functions.
615 ;; Ask user for confirm before printing. Due to Kevin Rodgers.
616 (defun ns-print-buffer ()
617 "Interactive front-end to `print-buffer': asks for user confirmation first."
618 (interactive)
619 (if (and (called-interactively-p 'interactive)
620 (or (listp last-nonmenu-event)
621 (and (char-or-string-p (event-basic-type last-command-event))
622 (memq 'super (event-modifiers last-command-event)))))
623 (let ((last-nonmenu-event (if (listp last-nonmenu-event)
624 last-nonmenu-event
625 ;; Fake it:
626 `(mouse-1 POSITION 1))))
627 (if (y-or-n-p (format "Print buffer %s? " (buffer-name)))
628 (print-buffer)
629 (error "Canceled")))
630 (print-buffer)))
632 ;;;; Font support.
634 ;; Needed for font listing functions under both backend and normal
635 (setq scalable-fonts-allowed t)
637 ;; Set to use font panel instead
638 (declare-function ns-popup-font-panel "nsfns.m" (&optional frame))
639 (defalias 'x-select-font 'ns-popup-font-panel "Pop up the font panel.
640 This function has been overloaded in Nextstep.")
641 (defalias 'mouse-set-font 'ns-popup-font-panel "Pop up the font panel.
642 This function has been overloaded in Nextstep.")
644 ;; nsterm.m
645 (defvar ns-input-font)
646 (defvar ns-input-fontsize)
648 (defun ns-respond-to-change-font ()
649 "Respond to changeFont: event, expecting `ns-input-font' and\n\
650 `ns-input-fontsize' of new font."
651 (interactive)
652 (modify-frame-parameters (selected-frame)
653 (list (cons 'fontsize ns-input-fontsize)))
654 (modify-frame-parameters (selected-frame)
655 (list (cons 'font ns-input-font)))
656 (set-frame-font ns-input-font))
659 ;; Default fontset for macOS. This is mainly here to show how a fontset
660 ;; can be set up manually. Ordinarily, fontsets are auto-created whenever
661 ;; a font is chosen by
662 (defvar ns-standard-fontset-spec
663 ;; Only some code supports this so far, so use uglier XLFD version
664 ;; "-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard,latin:Courier,han:Kai"
665 (mapconcat 'identity
666 '("-ns-*-*-*-*-*-10-*-*-*-*-*-fontset-standard"
667 "latin:-*-Courier-*-*-*-*-10-*-*-*-*-*-iso10646-1"
668 "han:-*-Kai-*-*-*-*-10-*-*-*-*-*-iso10646-1"
669 "cyrillic:-*-Trebuchet$MS-*-*-*-*-10-*-*-*-*-*-iso10646-1")
670 ",")
671 "String of fontset spec of the standard fontset.
672 This defines a fontset consisting of the Courier and other fonts that
673 come with macOS.
674 See the documentation of `create-fontset-from-fontset-spec' for the format.")
676 (defvar ns-reg-to-script) ; nsfont.m
678 ;; This maps font registries (not exposed by NS APIs for font selection) to
679 ;; Unicode scripts (which can be mapped to Unicode character ranges which are).
680 ;; See ../international/fontset.el
681 (setq ns-reg-to-script
682 '(("iso8859-1" . latin)
683 ("iso8859-2" . latin)
684 ("iso8859-3" . latin)
685 ("iso8859-4" . latin)
686 ("iso8859-5" . cyrillic)
687 ("microsoft-cp1251" . cyrillic)
688 ("koi8-r" . cyrillic)
689 ("iso8859-6" . arabic)
690 ("iso8859-7" . greek)
691 ("iso8859-8" . hebrew)
692 ("iso8859-9" . latin)
693 ("iso8859-10" . latin)
694 ("iso8859-11" . thai)
695 ("tis620" . thai)
696 ("iso8859-13" . latin)
697 ("iso8859-14" . latin)
698 ("iso8859-15" . latin)
699 ("iso8859-16" . latin)
700 ("viscii1.1-1" . latin)
701 ("jisx0201" . kana)
702 ("jisx0208" . han)
703 ("jisx0212" . han)
704 ("jisx0213" . han)
705 ("gb2312.1980" . han)
706 ("gb18030" . han)
707 ("gbk-0" . han)
708 ("big5" . han)
709 ("cns11643" . han)
710 ("sisheng_cwnn" . bopomofo)
711 ("ksc5601.1987" . hangul)
712 ("ethiopic-unicode" . ethiopic)
713 ("is13194-devanagari" . indian-is13194)
714 ("iso10646.indian-1" . devanagari)))
717 ;;;; Pasteboard support.
719 (define-obsolete-function-alias 'ns-store-cut-buffer-internal
720 'gui-set-selection "24.1")
723 (defun ns-copy-including-secondary ()
724 (interactive)
725 (call-interactively 'kill-ring-save)
726 (gui-set-selection 'SECONDARY (buffer-substring (point) (mark t))))
728 (defun ns-paste-secondary ()
729 (interactive)
730 (insert (gui-get-selection 'SECONDARY)))
733 ;;;; Scrollbar handling.
735 (global-set-key [vertical-scroll-bar down-mouse-1] 'scroll-bar-toolkit-scroll)
736 (global-set-key [horizontal-scroll-bar down-mouse-1] 'scroll-bar-toolkit-horizontal-scroll)
737 (global-unset-key [vertical-scroll-bar mouse-1])
738 (global-unset-key [vertical-scroll-bar drag-mouse-1])
739 (global-unset-key [horizontal-scroll-bar mouse-1])
740 (global-unset-key [horizontal-scroll-bar drag-mouse-1])
743 ;;;; macOS-like defaults for trackpad and mouse wheel scrolling on
744 ;;;; macOS 10.7+.
746 (defvar ns-version-string)
747 (defvar mouse-wheel-scroll-amount)
748 (defvar mouse-wheel-progressive-speed)
750 ;; FIXME: This doesn't look right. Is there a better way to do this
751 ;; that keeps customize happy?
752 (when (featurep 'cocoa)
753 (let ((appkit-version
754 (progn (string-match "^appkit-\\([^\s-]*\\)" ns-version-string)
755 (string-to-number (match-string 1 ns-version-string)))))
756 ;; Appkit 1138 ~= macOS 10.7.
757 (when (>= appkit-version 1138)
758 (setq mouse-wheel-scroll-amount '(1 ((shift) . 5) ((control))))
759 (put 'mouse-wheel-scroll-amount 'customized-value
760 (list (custom-quote (symbol-value 'mouse-wheel-scroll-amount))))
762 (setq mouse-wheel-progressive-speed nil)
763 (put 'mouse-wheel-progressive-speed 'customized-value
764 (list (custom-quote
765 (symbol-value 'mouse-wheel-progressive-speed)))))))
768 ;;;; Color support.
770 ;; Functions for color panel + drag
771 (defun ns-face-at-pos (pos)
772 (let* ((frame (car pos))
773 (frame-pos (cons (cadr pos) (cddr pos)))
774 (window (window-at (car frame-pos) (cdr frame-pos) frame))
775 (window-pos (coordinates-in-window-p frame-pos window))
776 (buffer (window-buffer window))
777 (edges (window-edges window)))
778 (cond
779 ((not window-pos)
780 nil)
781 ((eq window-pos 'mode-line)
782 'mode-line)
783 ((eq window-pos 'vertical-line)
784 'default)
785 ((consp window-pos)
786 (with-current-buffer buffer
787 (let ((p (car (compute-motion (window-start window)
788 (cons (nth 0 edges) (nth 1 edges))
789 (window-end window)
790 frame-pos
791 (- (window-width window) 1)
793 window))))
794 (cond
795 ((eq p (window-point window))
796 'cursor)
797 ((and mark-active (< (region-beginning) p) (< p (region-end)))
798 'region)
800 (let ((faces (get-char-property p 'face window)))
801 (if (consp faces) (car faces) faces)))))))
803 nil))))
805 (defun ns-suspend-error ()
806 ;; Don't allow suspending if any of the frames are NS frames.
807 (if (memq 'ns (mapcar 'window-system (frame-list)))
808 (error "Cannot suspend Emacs while an NS GUI frame exists")))
811 ;; Set some options to be as Nextstep-like as possible.
812 (setq frame-title-format "%b"
813 icon-title-format "%b")
816 (defvar ns-initialized nil
817 "Non-nil if Nextstep windowing has been initialized.")
819 (declare-function x-handle-args "common-win" (args))
820 (declare-function ns-list-services "nsfns.m" ())
821 (declare-function x-open-connection "nsfns.m"
822 (display &optional xrm-string must-succeed))
823 (declare-function ns-set-resource "nsfns.m" (owner name value))
825 ;; Do the actual Nextstep Windows setup here; the above code just
826 ;; defines functions and variables that we use now.
827 (cl-defmethod window-system-initialization (&context (window-system ns)
828 &optional _display)
829 "Initialize Emacs for Nextstep (Cocoa / GNUstep) windowing."
830 (cl-assert (not ns-initialized))
832 ;; PENDING: not needed?
833 (setq command-line-args (x-handle-args command-line-args))
835 ;; Setup the default fontset.
836 (create-default-fontset)
837 ;; Create the standard fontset.
838 (condition-case err
839 (create-fontset-from-fontset-spec ns-standard-fontset-spec t)
840 (error (display-warning
841 'initialization
842 (format "Creation of the standard fontset failed: %s" err)
843 :error)))
845 (x-open-connection (or (system-name) "") x-command-line-resources t)
847 ;; Add GNUstep menu items Services, Hide and Quit. Rename Help to Info
848 ;; and put it first (i.e. omit from menu-bar-final-items.
849 (if (featurep 'gnustep)
850 (progn
851 (setq menu-bar-final-items '(buffer services hide-app quit))
853 ;; If running under GNUstep, "Help" is moved and renamed "Info".
854 (bindings--define-key global-map [menu-bar help-menu]
855 (cons "Info" menu-bar-help-menu))
856 (bindings--define-key global-map [menu-bar quit]
857 '(menu-item "Quit" save-buffers-kill-emacs
858 :help "Save unsaved buffers, then exit"))
859 (bindings--define-key global-map [menu-bar hide-app]
860 '(menu-item "Hide" ns-do-hide-emacs
861 :help "Hide Emacs"))
862 (bindings--define-key global-map [menu-bar services]
863 (cons "Services" (make-sparse-keymap "Services")))))
866 (dolist (service (ns-list-services))
867 (if (eq (car service) 'undefined)
868 (ns-define-service (cdr service))
869 (define-key global-map (vector (car service))
870 (ns-define-service (cdr service)))))
872 (if (and (eq (get-lisp-resource nil "NXAutoLaunch") t)
873 (eq (get-lisp-resource nil "HideOnAutoLaunch") t))
874 (add-hook 'after-init-hook 'ns-do-hide-emacs))
876 ;; FIXME: This will surely lead to "MODIFIED OUTSIDE CUSTOM" warnings.
877 (menu-bar-mode (if (get-lisp-resource nil "Menus") 1 -1))
879 ;; For Darwin nothing except UTF-8 makes sense.
880 (when (eq system-type 'darwin)
881 (add-hook 'before-init-hook
882 #'(lambda ()
883 (setq locale-coding-system 'utf-8-unix)
884 (setq default-process-coding-system
885 '(utf-8-unix . utf-8-unix)))))
887 ;; Mac OS X Lion introduces PressAndHold, which is unsupported by this port.
888 ;; See this thread for more details:
889 ;; https://lists.gnu.org/r/emacs-devel/2011-06/msg00505.html
890 (ns-set-resource nil "ApplePressAndHoldEnabled" "NO")
892 (x-apply-session-resources)
894 ;; Don't let Emacs suspend under NS.
895 (add-hook 'suspend-hook 'ns-suspend-error)
897 (setq ns-initialized t))
899 ;; Any display name is OK.
900 (add-to-list 'display-format-alist '(".*" . ns))
901 (cl-defmethod handle-args-function (args &context (window-system ns))
902 (x-handle-args args))
904 (cl-defmethod frame-creation-function (params &context (window-system ns))
905 (x-create-frame-with-faces params))
907 (declare-function ns-own-selection-internal "nsselect.m" (selection value))
908 (declare-function ns-disown-selection-internal "nsselect.m" (selection))
909 (declare-function ns-selection-owner-p "nsselect.m" (&optional selection))
910 (declare-function ns-selection-exists-p "nsselect.m" (&optional selection))
911 (declare-function ns-get-selection "nsselect.m" (selection-symbol target-type))
913 (cl-defmethod gui-backend-set-selection (selection value
914 &context (window-system ns))
915 (if value (ns-own-selection-internal selection value)
916 (ns-disown-selection-internal selection)))
918 (cl-defmethod gui-backend-selection-owner-p (selection
919 &context (window-system ns))
920 (ns-selection-owner-p selection))
922 (cl-defmethod gui-backend-selection-exists-p (selection
923 &context (window-system ns))
924 (ns-selection-exists-p selection))
926 (cl-defmethod gui-backend-get-selection (selection-symbol target-type
927 &context (window-system ns))
928 (ns-get-selection selection-symbol target-type))
930 (provide 'ns-win)
931 (provide 'term/ns-win)
933 ;;; ns-win.el ends here