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