1 ;;; cua-gmrk.el --- CUA unified global mark support
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Kim F. Storm <storm@cua.dk>
7 ;; Keywords: keyboard emulations convenience cua mark
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
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.
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
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
39 ;; Non-nil when global marker is active.
40 (defvar cua--global-mark-active nil
)
42 ;; Global mark position marker.
43 (defvar cua--global-mark-marker nil
)
45 ;; Overlay for global mark position.
46 (defvar cua--global-mark-overlay nil
)
48 ;; Initialize global mark things once...
49 (defvar cua--global-mark-initialized nil
)
51 ;; Saved configured blink-cursor-interval
52 (defvar cua--orig-blink-cursor-interval nil
)
54 (defun cua--deactivate-global-mark (&optional msg
)
55 (when cua--global-mark-overlay
56 (delete-overlay cua--global-mark-overlay
)
57 (setq cua--global-mark-overlay nil
))
58 (if (markerp cua--global-mark-marker
)
59 (move-marker cua--global-mark-marker nil
))
60 (if cua--orig-blink-cursor-interval
61 (setq blink-cursor-interval cua--orig-blink-cursor-interval
62 cua--orig-blink-cursor-interval nil
))
63 (setq cua--global-mark-active nil
)
65 (message "Global Mark Cleared")))
67 (defun cua--activate-global-mark (&optional msg
)
68 (if (not (markerp cua--global-mark-marker
))
69 (setq cua--global-mark-marker
(make-marker)))
73 (move-marker cua--global-mark-marker
(point))
74 (if (overlayp cua--global-mark-overlay
)
75 (move-overlay cua--global-mark-overlay
(point) (1+ (point)))
76 (setq cua--global-mark-overlay
77 (make-overlay (point) (1+ (point))))
78 (overlay-put cua--global-mark-overlay
'face
'cua-global-mark
))
79 (if (and cua-global-mark-blink-cursor-interval
80 (not cua--orig-blink-cursor-interval
))
81 (setq cua--orig-blink-cursor-interval blink-cursor-interval
82 blink-cursor-interval cua-global-mark-blink-cursor-interval
))
83 (setq cua--global-mark-active t
)
85 (message "Global Mark Set")))
87 (defun cua--global-mark-active ()
88 (if cua--global-mark-active
89 (or (and (markerp cua--global-mark-marker
)
90 (marker-buffer cua--global-mark-marker
))
91 (and (cua--deactivate-global-mark nil
)
94 (defun cua-toggle-global-mark (stay)
95 "Set or cancel the global marker.
96 When the global marker is set, CUA cut and copy commands will automatically
97 insert the deleted or copied text before the global marker, even when the
98 global marker is in another buffer.
99 If the global marker isn't set, set the global marker at point in the current
100 buffer. Otherwise jump to the global marker position and cancel it.
101 With prefix argument, don't jump to global mark when cancelling it."
103 (unless cua--global-mark-initialized
104 (cua--init-global-mark))
105 (if (not (cua--global-mark-active))
106 (if (not buffer-read-only
)
107 (cua--activate-global-mark t
)
109 (message "Cannot set global mark in read-only buffer"))
111 (pop-to-buffer (marker-buffer cua--global-mark-marker
))
112 (goto-char cua--global-mark-marker
))
113 (cua--deactivate-global-mark t
)))
115 (defun cua--insert-at-global-mark (str &optional msg
)
116 ;; Insert string at global marker and move marker
118 (set-buffer (marker-buffer cua--global-mark-marker
))
119 (goto-char (marker-position cua--global-mark-marker
))
120 (insert-for-yank str
)
121 (cua--activate-global-mark))
123 (message "%s %d to global mark in %s:%d" msg
125 (buffer-name (marker-buffer cua--global-mark-marker
))
126 (marker-position cua--global-mark-marker
))))
128 (defun cua--delete-at-global-mark (arg &optional msg
)
129 ;; Delete chars at global marker
131 (set-buffer (marker-buffer cua--global-mark-marker
))
132 (goto-char (marker-position cua--global-mark-marker
))
135 (message "%s %d chars at global mark in %s:%d" msg arg
136 (buffer-name (marker-buffer cua--global-mark-marker
))
137 (marker-position cua--global-mark-marker
))))
139 (defun cua-copy-region-to-global-mark (start end
)
140 "Copy region to global mark buffer/position."
142 (if (cua--global-mark-active)
143 (let ((src-buf (current-buffer)))
145 (if (equal (marker-buffer cua--global-mark-marker
) src-buf
)
146 (let ((text (filter-buffer-substring start end nil t
)))
147 (goto-char (marker-position cua--global-mark-marker
))
149 (set-buffer (marker-buffer cua--global-mark-marker
))
150 (goto-char (marker-position cua--global-mark-marker
))
151 (insert-buffer-substring-as-yank src-buf start end
))
152 (cua--activate-global-mark)
153 (message "Copied %d to global mark in %s:%d"
155 (buffer-name (marker-buffer cua--global-mark-marker
))
156 (marker-position cua--global-mark-marker
))))
157 (cua--deactivate-global-mark)
158 (message "No Global Mark")))
160 (defun cua-cut-region-to-global-mark (start end
)
161 "Move region to global buffer/position."
163 (if (cua--global-mark-active)
164 (let ((src-buf (current-buffer)))
166 (if (equal (marker-buffer cua--global-mark-marker
) src-buf
)
167 (if (and (< start
(marker-position cua--global-mark-marker
))
168 (< (marker-position cua--global-mark-marker
) end
))
169 (message "Can't move region into itself")
170 (let ((text (filter-buffer-substring start end nil t
))
171 (p1 (copy-marker start
))
172 (p2 (copy-marker end
)))
173 (goto-char (marker-position cua--global-mark-marker
))
175 (cua--activate-global-mark)
176 (delete-region (marker-position p1
) (marker-position p2
))
178 (move-marker p2 nil
)))
179 (set-buffer (marker-buffer cua--global-mark-marker
))
180 (goto-char (marker-position cua--global-mark-marker
))
181 (insert-buffer-substring src-buf start end
)
182 (message "Moved %d to global mark in %s:%d"
184 (buffer-name (marker-buffer cua--global-mark-marker
))
185 (marker-position cua--global-mark-marker
))
186 (cua--activate-global-mark)
188 (delete-region start end
))))
189 (cua--deactivate-global-mark)
190 (message "No Global Mark")))
192 (defun cua--copy-rectangle-to-global-mark (as-text)
193 ;; Copy rectangle to global mark buffer/position.
194 (if (cua--global-mark-active)
195 (let ((src-buf (current-buffer))
196 (text (cua--extract-rectangle)))
198 (set-buffer (marker-buffer cua--global-mark-marker
))
199 (goto-char (marker-position cua--global-mark-marker
))
202 (insert-for-yank (car text
))
203 (if (setq text
(cdr text
))
205 (cua--insert-rectangle text
'auto
))
206 (cua--activate-global-mark)
207 (message "Copied rectangle to global mark in %s:%d"
208 (buffer-name (marker-buffer cua--global-mark-marker
))
209 (marker-position cua--global-mark-marker
))))
210 (cua--deactivate-global-mark)
211 (message "No Global Mark")))
213 (defun cua--cut-rectangle-to-global-mark (as-text)
214 ;; Move rectangle to global buffer/position.
215 (if (cua--global-mark-active)
216 (let ((src-buf (current-buffer)))
218 (if (equal (marker-buffer cua--global-mark-marker
) src-buf
)
219 (let ((olist (overlays-at (marker-position cua--global-mark-marker
)))
222 (if (eq (overlay-get (car olist
) 'face
) 'cua-rectangle
)
223 (setq in-rect t olist nil
)
224 (setq olist
(cdr olist
))))
226 (message "Can't move rectangle into itself")
227 (let ((text (cua--extract-rectangle)))
228 (cua--delete-rectangle)
229 (goto-char (marker-position cua--global-mark-marker
))
232 (insert-for-yank (car text
))
233 (if (setq text
(cdr text
))
235 (cua--insert-rectangle text
'auto
))
236 (cua--activate-global-mark))))
237 (let ((text (cua--extract-rectangle)))
238 (cua--delete-rectangle)
239 (set-buffer (marker-buffer cua--global-mark-marker
))
240 (goto-char (marker-position cua--global-mark-marker
))
241 (cua--insert-rectangle text
'auto
))
242 (message "Moved rectangle to global mark in %s:%d"
243 (buffer-name (marker-buffer cua--global-mark-marker
))
244 (marker-position cua--global-mark-marker
))
245 (cua--activate-global-mark))))
246 (cua--deactivate-global-mark)
247 (message "No Global Mark")))
249 (defun cua-copy-to-global-mark ()
250 "Copy active region/rectangle to global mark buffer/position."
252 (setq cua--last-killed-rectangle nil
)
254 (cua--copy-rectangle-to-global-mark nil
)
255 (let ((start (mark)) (end (point)))
257 (setq start
(prog1 end
(setq end start
))))
258 (cua-copy-region-to-global-mark start end
))))
260 (defun cua-copy-next-to-global-mark (n)
261 "Copy the following N characters in buffer to global mark buffer/position."
263 (setq cua--last-killed-rectangle nil
)
267 (cua-copy-region-to-global-mark p
(point)))))
269 (defun cua-cut-to-global-mark ()
270 "Move active region/rectangle to global mark buffer/position."
273 (cua-copy-to-global-mark)
274 (setq cua--last-killed-rectangle nil
)
276 (cua--cut-rectangle-to-global-mark nil
)
277 (let ((start (mark)) (end (point)))
279 (setq start
(prog1 end
(setq end start
))))
280 (cua-cut-region-to-global-mark start end
)))))
282 (defun cua-cut-next-to-global-mark (n)
283 "Move the following N characters in buffer to global mark buffer/position."
285 (setq cua--last-killed-rectangle nil
)
289 (cua-cut-region-to-global-mark p
(point)))))
291 (defun cua-delete-char-at-global-mark (arg)
292 "Delete character following the global mark position."
294 (cua--delete-at-global-mark arg
"Deleted"))
296 (defun cua-delete-backward-char-at-global-mark (arg)
297 "Delete character before the global mark position."
299 (cua--delete-at-global-mark (- arg
) "Deleted backward"))
301 (defun cua-insert-char-at-global-mark ()
302 "Insert the character you type at the global mark position."
304 (cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted"))
306 (defun cua-insert-newline-at-global-mark ()
307 "Insert a newline at the global mark position."
309 (cua--insert-at-global-mark "\n"))
311 (defun cua-indent-to-global-mark-column ()
312 "Indent current line or rectangle to global mark column."
314 (if (cua--global-mark-active)
317 (set-buffer (marker-buffer cua--global-mark-marker
))
318 (goto-char (marker-position cua--global-mark-marker
))
319 (setq col
(current-column)))
321 (cua--indent-rectangle nil col t
)
323 (if (eq (current-buffer) (marker-buffer cua--global-mark-marker
))
325 (goto-char (marker-position cua--global-mark-marker
))
327 (move-marker cua--global-mark-marker
(point))
328 (move-overlay cua--global-mark-overlay
(point) (1+ (point))))))))
331 (defun cua-cancel-global-mark ()
332 "Cancel the global mark."
336 (if (cua--global-mark-active)
337 (cua--deactivate-global-mark t
)))
340 ;;; Post-command hook for global mark.
342 (defun cua--global-mark-post-command ()
343 (when (and (cua--global-mark-active) ;; Updates cua--global-mark-active variable
344 cua-global-mark-keep-visible
)
345 ;; keep global mark position visible
347 (if (or (not (eq (current-buffer) (marker-buffer cua--global-mark-marker
)))
348 (not (pos-visible-in-window-p (marker-position cua--global-mark-marker
))))
349 (let ((w (selected-window)) (p (point)) h
)
350 ;; The following code is an attempt to keep the global mark visible in
351 ;; other window -- but it doesn't work.
352 (switch-to-buffer-other-window (marker-buffer cua--global-mark-marker
) t
)
353 (goto-char (marker-position cua--global-mark-marker
))
354 (if (not (pos-visible-in-window-p (marker-position cua--global-mark-marker
)))
355 (recenter (if (> (setq h
(- (window-height) 4)) 1) h
'(4))))
361 (defun cua--init-global-mark ()
362 (define-key cua--global-mark-keymap
[remap copy-region-as-kill
] 'cua-copy-to-global-mark
)
363 (define-key cua--global-mark-keymap
[remap kill-ring-save
] 'cua-copy-to-global-mark
)
364 (define-key cua--global-mark-keymap
[remap kill-region
] 'cua-cut-to-global-mark
)
365 (define-key cua--global-mark-keymap
[remap yank
] 'cua-copy-next-to-global-mark
)
367 (define-key cua--global-mark-keymap
[remap keyboard-escape-quit
] 'cua-cancel-global-mark
)
368 (define-key cua--global-mark-keymap
[remap keyboard-quit
] 'cua-cancel-global-mark
)
370 (define-key cua--global-mark-keymap
[(control ?d
)] 'cua-cut-next-to-global-mark
)
371 (define-key cua--global-mark-keymap
[remap delete-backward-char
] 'cua-delete-backward-char-at-global-mark
)
372 (define-key cua--global-mark-keymap
[remap backward-delete-char
] 'cua-delete-backward-char-at-global-mark
)
373 (define-key cua--global-mark-keymap
[remap backward-delete-char-untabify
] 'cua-delete-backward-char-at-global-mark
)
374 (define-key cua--global-mark-keymap
[remap self-insert-command
] 'cua-insert-char-at-global-mark
)
375 (define-key cua--global-mark-keymap
[remap self-insert-iso
] 'cua-insert-char-at-global-mark
)
377 ;; Catch self-inserting characters which are "stolen" by other modes
378 (define-key cua--global-mark-keymap
[t]
379 '(menu-item "sic" cua-insert-char-at-global-mark :filter cua--self-insert-char-p))
381 (define-key cua--global-mark-keymap [remap newline] 'cua-insert-newline-at-global-mark)
382 (define-key cua--global-mark-keymap [remap newline-and-indent] 'cua-insert-newline-at-global-mark)
383 (define-key cua--global-mark-keymap "\r" 'cua-insert-newline-at-global-mark)
385 (define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column)
387 (setq cua--global-mark-initialized t))
389 ;;; arch-tag: 553d8076-a91d-48ae-825d-6cb962a5f67f
390 ;;; cua-gmrk.el ends here