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