Sync to HEAD
[bpt/emacs.git] / lisp / emulation / cua-gmrk.el
CommitLineData
72cc582e
KS
1;;; cua-gmrk.el --- CUA unified global mark support
2
3;; Copyright (C) 1997-2002 Free Software Foundation, Inc.
4
5;; Author: Kim F. Storm <storm@cua.dk>
6;; Keywords: keyboard emulations convenience cua mark
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
30764597
PJ
25;;; Commentary:
26
27;;; Code:
72cc582e
KS
28
29(provide 'cua-gmrk)
30
31(eval-when-compile
32 (require 'cua-base)
33 (require 'cua-rect)
34 )
35
a1506d29 36;;; Global Marker
72cc582e
KS
37
38;; Non-nil when global marker is active.
39(defvar cua--global-mark-active nil)
40
41;; Global mark position marker.
42(defvar cua--global-mark-marker nil)
43
44;; Overlay for global mark position.
45(defvar cua--global-mark-overlay nil)
46
47;; Initialize global mark things once...
48(defvar cua--global-mark-initialized nil)
49
50;; Saved configured blink-cursor-interval
51(defvar cua--orig-blink-cursor-interval nil)
52
53(defun cua--deactivate-global-mark (&optional msg)
54 (when cua--global-mark-overlay
55 (delete-overlay cua--global-mark-overlay)
56 (setq cua--global-mark-overlay nil))
57 (if (markerp cua--global-mark-marker)
58 (move-marker cua--global-mark-marker nil))
59 (if cua--orig-blink-cursor-interval
60 (setq blink-cursor-interval cua--orig-blink-cursor-interval
61 cua--orig-blink-cursor-interval nil))
62 (setq cua--global-mark-active nil)
63 (if msg
64 (message "Global Mark Cleared")))
65
66(defun cua--activate-global-mark (&optional msg)
67 (if (not (markerp cua--global-mark-marker))
68 (setq cua--global-mark-marker (make-marker)))
69 (when (eobp)
70 (insert " ")
71 (backward-char 1))
72 (move-marker cua--global-mark-marker (point))
73 (if (overlayp cua--global-mark-overlay)
74 (move-overlay cua--global-mark-overlay (point) (1+ (point)))
a1506d29 75 (setq cua--global-mark-overlay
72cc582e
KS
76 (make-overlay (point) (1+ (point))))
77 (overlay-put cua--global-mark-overlay 'face 'cua-global-mark-face))
78 (if (and cua-global-mark-blink-cursor-interval
79 (not cua--orig-blink-cursor-interval))
a1506d29 80 (setq cua--orig-blink-cursor-interval blink-cursor-interval
72cc582e
KS
81 blink-cursor-interval cua-global-mark-blink-cursor-interval))
82 (setq cua--global-mark-active t)
83 (if msg
84 (message "Global Mark Set")))
85
86(defun cua--global-mark-active ()
87 (if cua--global-mark-active
88 (or (and (markerp cua--global-mark-marker)
89 (marker-buffer cua--global-mark-marker))
90 (and (cua--deactivate-global-mark nil)
91 nil))))
92
93(defun cua-toggle-global-mark (stay)
94 "Set or cancel the global marker.
95When the global marker is set, CUA cut and copy commands will automatically
96insert the deleted or copied text before the global marker, even when the
97global marker is in another buffer.
98If the global marker isn't set, set the global marker at point in the current
99buffer. Otherwise jump to the global marker position and cancel it.
100With prefix argument, don't jump to global mark when cancelling it."
101 (interactive "P")
102 (unless cua--global-mark-initialized
103 (cua--init-global-mark))
104 (if (not (cua--global-mark-active))
105 (if (not buffer-read-only)
106 (cua--activate-global-mark t)
107 (ding)
108 (message "Cannot set global mark in read-only buffer."))
109 (when (not stay)
110 (pop-to-buffer (marker-buffer cua--global-mark-marker))
111 (goto-char cua--global-mark-marker))
112 (cua--deactivate-global-mark t)))
113
114(defun cua--insert-at-global-mark (str &optional msg)
115 ;; Insert string at global marker and move marker
116 (save-excursion
117 (set-buffer (marker-buffer cua--global-mark-marker))
118 (goto-char (marker-position cua--global-mark-marker))
119 (insert-for-yank str)
120 (cua--activate-global-mark))
121 (if msg
122 (message "%s %d to global mark in %s:%d" msg
123 (length str)
124 (buffer-name (marker-buffer cua--global-mark-marker))
125 (marker-position cua--global-mark-marker))))
126
127(defun cua--delete-at-global-mark (arg &optional msg)
128 ;; Delete chars at global marker
129 (save-excursion
130 (set-buffer (marker-buffer cua--global-mark-marker))
131 (goto-char (marker-position cua--global-mark-marker))
132 (delete-char arg))
133 (if msg
134 (message "%s %d chars at global mark in %s:%d" msg arg
135 (buffer-name (marker-buffer cua--global-mark-marker))
136 (marker-position cua--global-mark-marker))))
137
138(defun cua-copy-region-to-global-mark (start end)
139 "Copy region to global mark buffer/position."
140 (interactive "r")
141 (if (cua--global-mark-active)
142 (let ((src-buf (current-buffer)))
143 (save-excursion
144 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
145 (let ((text (buffer-substring-no-properties start end)))
146 (goto-char (marker-position cua--global-mark-marker))
147 (insert text))
148 (set-buffer (marker-buffer cua--global-mark-marker))
149 (goto-char (marker-position cua--global-mark-marker))
150 (insert-buffer-substring-as-yank src-buf start end))
151 (cua--activate-global-mark)
152 (message "Copied %d to global mark in %s:%d"
153 (abs (- end start))
154 (buffer-name (marker-buffer cua--global-mark-marker))
155 (marker-position cua--global-mark-marker))))
156 (cua--deactivate-global-mark)
157 (message "No Global Mark")))
158
159(defun cua-cut-region-to-global-mark (start end)
160 "Move region to global buffer/position."
161 (interactive "r")
162 (if (cua--global-mark-active)
163 (let ((src-buf (current-buffer)))
164 (save-excursion
165 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
166 (if (and (< start (marker-position cua--global-mark-marker))
167 (< (marker-position cua--global-mark-marker) end))
168 (message "Can't move region into itself.")
169 (let ((text (buffer-substring-no-properties start end))
170 (p1 (copy-marker start))
171 (p2 (copy-marker end)))
172 (goto-char (marker-position cua--global-mark-marker))
173 (insert text)
174 (cua--activate-global-mark)
175 (delete-region (marker-position p1) (marker-position p2))
176 (move-marker p1 nil)
177 (move-marker p2 nil)))
178 (set-buffer (marker-buffer cua--global-mark-marker))
179 (goto-char (marker-position cua--global-mark-marker))
180 (insert-buffer-substring src-buf start end)
181 (message "Moved %d to global mark in %s:%d"
182 (abs (- end start))
183 (buffer-name (marker-buffer cua--global-mark-marker))
184 (marker-position cua--global-mark-marker))
185 (cua--activate-global-mark)
186 (set-buffer src-buf)
187 (delete-region start end))))
188 (cua--deactivate-global-mark)
189 (message "No Global Mark")))
190
191(defun cua--copy-rectangle-to-global-mark (as-text)
192 ;; Copy rectangle to global mark buffer/position.
193 (if (cua--global-mark-active)
194 (let ((src-buf (current-buffer))
195 (text (cua--extract-rectangle)))
196 (save-excursion
197 (set-buffer (marker-buffer cua--global-mark-marker))
198 (goto-char (marker-position cua--global-mark-marker))
199 (if as-text
200 (while text
201 (insert-for-yank (car text))
202 (if (setq text (cdr text))
203 (insert "\n")))
204 (cua--insert-rectangle text 'auto))
205 (cua--activate-global-mark)
206 (message "Copied rectangle to global mark in %s:%d"
207 (buffer-name (marker-buffer cua--global-mark-marker))
208 (marker-position cua--global-mark-marker))))
209 (cua--deactivate-global-mark)
210 (message "No Global Mark")))
211
212(defun cua--cut-rectangle-to-global-mark (as-text)
213 ;; Move rectangle to global buffer/position.
214 (if (cua--global-mark-active)
215 (let ((src-buf (current-buffer)))
216 (save-excursion
217 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
218 (let ((olist (overlays-at (marker-position cua--global-mark-marker)))
219 in-rect)
220 (while olist
221 (if (eq (overlay-get (car olist) 'face) 'cua-rectangle-face)
222 (setq in-rect t olist nil)
223 (setq olist (cdr olist))))
224 (if in-rect
225 (message "Can't move rectangle into itself.")
226 (let ((text (cua--extract-rectangle)))
227 (cua--delete-rectangle)
228 (goto-char (marker-position cua--global-mark-marker))
229 (if as-text
230 (while text
231 (insert-for-yank (car text))
232 (if (setq text (cdr text))
233 (insert "\n")))
234 (cua--insert-rectangle text 'auto))
235 (cua--activate-global-mark))))
236 (let ((text (cua--extract-rectangle)))
237 (cua--delete-rectangle)
238 (set-buffer (marker-buffer cua--global-mark-marker))
239 (goto-char (marker-position cua--global-mark-marker))
240 (cua--insert-rectangle text 'auto))
241 (message "Moved rectangle to global mark in %s:%d"
242 (buffer-name (marker-buffer cua--global-mark-marker))
243 (marker-position cua--global-mark-marker))
244 (cua--activate-global-mark))))
245 (cua--deactivate-global-mark)
246 (message "No Global Mark")))
247
248(defun cua-copy-to-global-mark ()
249 "Copy active region/rectangle to global mark buffer/position."
250 (interactive)
251 (setq cua--last-killed-rectangle nil)
252 (if cua--rectangle
253 (cua--copy-rectangle-to-global-mark nil)
254 (let ((start (mark)) (end (point)))
255 (or (<= start end)
256 (setq start (prog1 end (setq end start))))
257 (cua-copy-region-to-global-mark start end))))
258
259(defun cua-copy-next-to-global-mark (n)
260 "Copy the following N characters in buffer to global mark buffer/position."
261 (interactive "p")
262 (setq cua--last-killed-rectangle nil)
263 (or (eobp)
264 (let ((p (point)))
265 (goto-char (+ p n))
266 (cua-copy-region-to-global-mark p (point)))))
267
268(defun cua-cut-to-global-mark ()
269 "Move active region/rectangle to global mark buffer/position."
270 (interactive)
271 (if buffer-read-only
272 (cua-copy-to-global-mark)
273 (setq cua--last-killed-rectangle nil)
274 (if cua--rectangle
275 (cua--cut-rectangle-to-global-mark nil)
276 (let ((start (mark)) (end (point)))
277 (or (<= start end)
278 (setq start (prog1 end (setq end start))))
279 (cua-cut-region-to-global-mark start end)))))
280
281(defun cua-cut-next-to-global-mark (n)
282 "Move the following N characters in buffer to global mark buffer/position."
283 (interactive "p")
284 (setq cua--last-killed-rectangle nil)
285 (or (eobp)
286 (let ((p (point)))
287 (goto-char (+ p n))
288 (cua-cut-region-to-global-mark p (point)))))
289
290(defun cua-delete-char-at-global-mark (arg)
291 "Delete character following the global mark position."
292 (interactive "p")
293 (cua--delete-at-global-mark arg "Deleted"))
294
295(defun cua-delete-backward-char-at-global-mark (arg)
296 "Delete character before the global mark position."
297 (interactive "p")
298 (cua--delete-at-global-mark (- arg) "Deleted backward"))
299
300(defun cua-insert-char-at-global-mark ()
301 "Insert the character you type at the global mark position."
302 (interactive)
303 (cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted"))
304
305(defun cua-insert-newline-at-global-mark ()
306 "Insert a newline at the global mark position."
307 (interactive)
308 (cua--insert-at-global-mark "\n"))
309
310(defun cua-indent-to-global-mark-column ()
311 "Indent current line or rectangle to global mark column."
312 (interactive "*")
313 (if (cua--global-mark-active)
314 (let (col)
315 (save-excursion
316 (set-buffer (marker-buffer cua--global-mark-marker))
317 (goto-char (marker-position cua--global-mark-marker))
318 (setq col (current-column)))
319 (if cua--rectangle
320 (cua--indent-rectangle nil col t)
321 (indent-to col))
322 (if (eq (current-buffer) (marker-buffer cua--global-mark-marker))
323 (save-excursion
324 (goto-char (marker-position cua--global-mark-marker))
325 (move-to-column col)
326 (move-marker cua--global-mark-marker (point))
327 (move-overlay cua--global-mark-overlay (point) (1+ (point))))))))
a1506d29 328
72cc582e
KS
329
330(defun cua-cancel-global-mark ()
331 "Cancel the global mark."
332 (interactive)
333 (if mark-active
334 (cua-cancel)
335 (if (cua--global-mark-active)
336 (cua--deactivate-global-mark t)))
337 (cua--fallback))
338
339;;; Post-command hook for global mark.
340
341(defun cua--global-mark-post-command ()
342 (when (and (cua--global-mark-active) ;; Updates cua--global-mark-active variable
343 cua-global-mark-keep-visible)
344 ;; keep global mark position visible
345 (sit-for 0)
346 (if (or (not (eq (current-buffer) (marker-buffer cua--global-mark-marker)))
347 (not (pos-visible-in-window-p (marker-position cua--global-mark-marker))))
348 (let ((w (selected-window)) (p (point)) h)
a1506d29 349 ;; The following code is an attempt to keep the global mark visible in
72cc582e
KS
350 ;; other window -- but it doesn't work.
351 (switch-to-buffer-other-window (marker-buffer cua--global-mark-marker) t)
352 (goto-char (marker-position cua--global-mark-marker))
353 (if (not (pos-visible-in-window-p (marker-position cua--global-mark-marker)))
354 (recenter (if (> (setq h (- (window-height) 4)) 1) h '(4))))
355 (select-window w)
356 (goto-char p)))))
357
358;;; Initialization
359
360(defun cua--init-global-mark ()
361 (unless (face-background 'cua-global-mark-face)
362 (copy-face 'region 'cua-global-mark-face)
363 (set-face-foreground 'cua-global-mark-face "black")
364 (set-face-background 'cua-global-mark-face "cyan"))
365
366 (define-key cua--global-mark-keymap [remap copy-region-as-kill] 'cua-copy-to-global-mark)
367 (define-key cua--global-mark-keymap [remap kill-ring-save] 'cua-copy-to-global-mark)
368 (define-key cua--global-mark-keymap [remap kill-region] 'cua-cut-to-global-mark)
369 (define-key cua--global-mark-keymap [remap yank] 'cua-copy-next-to-global-mark)
370
371 (define-key cua--global-mark-keymap [remap keyboard-escape-quit] 'cua-cancel-global-mark)
372 (define-key cua--global-mark-keymap [remap keyboard-quit] 'cua-cancel-global-mark)
373
374 (define-key cua--global-mark-keymap [(control ?d)] 'cua-cut-next-to-global-mark)
375 (define-key cua--global-mark-keymap [remap delete-backward-char] 'cua-delete-backward-char-at-global-mark)
376 (define-key cua--global-mark-keymap [remap backward-delete-char] 'cua-delete-backward-char-at-global-mark)
377 (define-key cua--global-mark-keymap [remap backward-delete-char-untabify] 'cua-delete-backward-char-at-global-mark)
378 (define-key cua--global-mark-keymap [remap self-insert-command] 'cua-insert-char-at-global-mark)
379 (define-key cua--global-mark-keymap [remap self-insert-iso] 'cua-insert-char-at-global-mark)
f8b38495
KS
380
381 ;; Catch self-inserting characters which are "stolen" by other modes
382 (define-key cua--global-mark-keymap [t]
383 '(menu-item "sic" cua-insert-char-at-global-mark :filter cua--self-insert-char-p))
384
72cc582e
KS
385 (define-key cua--global-mark-keymap [remap newline] 'cua-insert-newline-at-global-mark)
386 (define-key cua--global-mark-keymap [remap newline-and-indent] 'cua-insert-newline-at-global-mark)
387 (define-key cua--global-mark-keymap "\r" 'cua-insert-newline-at-global-mark)
388
389 (define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column)
390
391 (setq cua--global-mark-initialized t))
392
6b61353c 393;;; arch-tag: 553d8076-a91d-48ae-825d-6cb962a5f67f
72cc582e 394;;; cua-gmrk.el ends here