1 ;;; cua-gmrk.el --- CUA unified global mark support
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
35 ;; Non-nil when global marker is active.
36 (defvar cua--global-mark-active nil
)
38 ;; Global mark position marker.
39 (defvar cua--global-mark-marker nil
)
41 ;; Overlay for global mark position.
42 (defvar cua--global-mark-overlay nil
)
44 ;; Initialize global mark things once...
45 (defvar cua--global-mark-initialized nil
)
47 ;; Saved configured blink-cursor-interval
48 (defvar cua--orig-blink-cursor-interval nil
)
50 (defun cua--deactivate-global-mark (&optional msg
)
51 (when cua--global-mark-overlay
52 (delete-overlay cua--global-mark-overlay
)
53 (setq cua--global-mark-overlay nil
))
54 (if (markerp cua--global-mark-marker
)
55 (move-marker cua--global-mark-marker nil
))
56 (if cua--orig-blink-cursor-interval
57 (setq blink-cursor-interval cua--orig-blink-cursor-interval
58 cua--orig-blink-cursor-interval nil
))
59 (setq cua--global-mark-active nil
)
61 (message "Global Mark Cleared")))
63 (defun cua--activate-global-mark (&optional msg
)
64 (if (not (markerp cua--global-mark-marker
))
65 (setq cua--global-mark-marker
(make-marker)))
69 (move-marker cua--global-mark-marker
(point))
70 (if (overlayp cua--global-mark-overlay
)
71 (move-overlay cua--global-mark-overlay
(point) (1+ (point)))
72 (setq cua--global-mark-overlay
73 (make-overlay (point) (1+ (point))))
74 (overlay-put cua--global-mark-overlay
'face
'cua-global-mark
))
75 (if (and cua-global-mark-blink-cursor-interval
76 (not cua--orig-blink-cursor-interval
))
77 (setq cua--orig-blink-cursor-interval blink-cursor-interval
78 blink-cursor-interval cua-global-mark-blink-cursor-interval
))
79 (setq cua--global-mark-active t
)
81 (message "Global Mark Set")))
83 (defun cua--global-mark-active ()
84 (if cua--global-mark-active
85 (or (and (markerp cua--global-mark-marker
)
86 (marker-buffer cua--global-mark-marker
))
87 (and (cua--deactivate-global-mark nil
)
90 (defun cua-toggle-global-mark (stay)
91 "Set or cancel the global marker.
92 When the global marker is set, CUA cut and copy commands will automatically
93 insert the deleted or copied text before the global marker, even when the
94 global marker is in another buffer.
95 If the global marker isn't set, set the global marker at point in the current
96 buffer. Otherwise jump to the global marker position and cancel it.
97 With prefix argument, don't jump to global mark when cancelling it."
99 (unless cua--global-mark-initialized
100 (cua--init-global-mark))
101 (if (not (cua--global-mark-active))
102 (if (not buffer-read-only
)
103 (cua--activate-global-mark t
)
105 (message "Cannot set global mark in read-only buffer"))
107 (pop-to-buffer (marker-buffer cua--global-mark-marker
))
108 (goto-char cua--global-mark-marker
))
109 (cua--deactivate-global-mark t
)))
111 (defun cua--insert-at-global-mark (str &optional msg
)
112 ;; Insert string at global marker and move marker
114 (set-buffer (marker-buffer cua--global-mark-marker
))
115 (goto-char (marker-position cua--global-mark-marker
))
116 (insert-for-yank str
)
117 (cua--activate-global-mark))
119 (message "%s %d to global mark in %s:%d" msg
121 (buffer-name (marker-buffer cua--global-mark-marker
))
122 (marker-position cua--global-mark-marker
))))
124 (defun cua--delete-at-global-mark (arg &optional msg
)
125 ;; Delete chars at global marker
127 (set-buffer (marker-buffer cua--global-mark-marker
))
128 (goto-char (marker-position cua--global-mark-marker
))
131 (message "%s %d chars at global mark in %s:%d" msg arg
132 (buffer-name (marker-buffer cua--global-mark-marker
))
133 (marker-position cua--global-mark-marker
))))
135 (defun cua-copy-region-to-global-mark (start end
)
136 "Copy region to global mark buffer/position."
138 (if (cua--global-mark-active)
139 (let ((src-buf (current-buffer)))
141 (if (equal (marker-buffer cua--global-mark-marker
) src-buf
)
142 (let ((text (filter-buffer-substring start end nil t
)))
143 (goto-char (marker-position cua--global-mark-marker
))
145 (set-buffer (marker-buffer cua--global-mark-marker
))
146 (goto-char (marker-position cua--global-mark-marker
))
147 (insert-buffer-substring-as-yank src-buf start end
))
148 (cua--activate-global-mark)
149 (message "Copied %d to global mark in %s:%d"
151 (buffer-name (marker-buffer cua--global-mark-marker
))
152 (marker-position cua--global-mark-marker
))))
153 (cua--deactivate-global-mark)
154 (message "No Global Mark")))
156 (defun cua-cut-region-to-global-mark (start end
)
157 "Move region to global buffer/position."
159 (if (cua--global-mark-active)
160 (let ((src-buf (current-buffer)))
162 (if (equal (marker-buffer cua--global-mark-marker
) src-buf
)
163 (if (and (< start
(marker-position cua--global-mark-marker
))
164 (< (marker-position cua--global-mark-marker
) end
))
165 (message "Can't move region into itself")
166 (let ((text (filter-buffer-substring start end nil t
))
167 (p1 (copy-marker start
))
168 (p2 (copy-marker end
)))
169 (goto-char (marker-position cua--global-mark-marker
))
171 (cua--activate-global-mark)
172 (delete-region (marker-position p1
) (marker-position p2
))
174 (move-marker p2 nil
)))
175 (set-buffer (marker-buffer cua--global-mark-marker
))
176 (goto-char (marker-position cua--global-mark-marker
))
177 (insert-buffer-substring src-buf start end
)
178 (message "Moved %d to global mark in %s:%d"
180 (buffer-name (marker-buffer cua--global-mark-marker
))
181 (marker-position cua--global-mark-marker
))
182 (cua--activate-global-mark)
184 (delete-region start end
))))
185 (cua--deactivate-global-mark)
186 (message "No Global Mark")))
188 (defun cua--copy-rectangle-to-global-mark (as-text)
189 ;; Copy rectangle to global mark buffer/position.
190 (if (cua--global-mark-active)
191 (let ((src-buf (current-buffer))
192 (text (cua--extract-rectangle)))
194 (set-buffer (marker-buffer cua--global-mark-marker
))
195 (goto-char (marker-position cua--global-mark-marker
))
198 (insert-for-yank (car text
))
199 (if (setq text
(cdr text
))
201 (cua--insert-rectangle text
'auto
))
202 (cua--activate-global-mark)
203 (message "Copied rectangle to global mark in %s:%d"
204 (buffer-name (marker-buffer cua--global-mark-marker
))
205 (marker-position cua--global-mark-marker
))))
206 (cua--deactivate-global-mark)
207 (message "No Global Mark")))
209 (defun cua--cut-rectangle-to-global-mark (as-text)
210 ;; Move rectangle to global buffer/position.
211 (if (cua--global-mark-active)
212 (let ((src-buf (current-buffer)))
214 (if (equal (marker-buffer cua--global-mark-marker
) src-buf
)
215 (let ((olist (overlays-at (marker-position cua--global-mark-marker
)))
218 (if (eq (overlay-get (car olist
) 'face
) 'cua-rectangle
)
219 (setq in-rect t olist nil
)
220 (setq olist
(cdr olist
))))
222 (message "Can't move rectangle into itself")
223 (let ((text (cua--extract-rectangle)))
224 (cua--delete-rectangle)
225 (goto-char (marker-position cua--global-mark-marker
))
228 (insert-for-yank (car text
))
229 (if (setq text
(cdr text
))
231 (cua--insert-rectangle text
'auto
))
232 (cua--activate-global-mark))))
233 (let ((text (cua--extract-rectangle)))
234 (cua--delete-rectangle)
235 (set-buffer (marker-buffer cua--global-mark-marker
))
236 (goto-char (marker-position cua--global-mark-marker
))
237 (cua--insert-rectangle text
'auto
))
238 (message "Moved rectangle to global mark in %s:%d"
239 (buffer-name (marker-buffer cua--global-mark-marker
))
240 (marker-position cua--global-mark-marker
))
241 (cua--activate-global-mark))))
242 (cua--deactivate-global-mark)
243 (message "No Global Mark")))
245 (defun cua-copy-to-global-mark ()
246 "Copy active region/rectangle to global mark buffer/position."
248 (setq cua--last-killed-rectangle nil
)
250 (cua--copy-rectangle-to-global-mark nil
)
251 (let ((start (mark)) (end (point)))
253 (setq start
(prog1 end
(setq end start
))))
254 (cua-copy-region-to-global-mark start end
))))
256 (defun cua-copy-next-to-global-mark (n)
257 "Copy the following N characters in buffer to global mark buffer/position."
259 (setq cua--last-killed-rectangle nil
)
263 (cua-copy-region-to-global-mark p
(point)))))
265 (defun cua-cut-to-global-mark ()
266 "Move active region/rectangle to global mark buffer/position."
269 (cua-copy-to-global-mark)
270 (setq cua--last-killed-rectangle nil
)
272 (cua--cut-rectangle-to-global-mark nil
)
273 (let ((start (mark)) (end (point)))
275 (setq start
(prog1 end
(setq end start
))))
276 (cua-cut-region-to-global-mark start end
)))))
278 (defun cua-cut-next-to-global-mark (n)
279 "Move the following N characters in buffer to global mark buffer/position."
281 (setq cua--last-killed-rectangle nil
)
285 (cua-cut-region-to-global-mark p
(point)))))
287 (defun cua-delete-char-at-global-mark (arg)
288 "Delete character following the global mark position."
290 (cua--delete-at-global-mark arg
"Deleted"))
292 (defun cua-delete-backward-char-at-global-mark (arg)
293 "Delete character before the global mark position."
295 (cua--delete-at-global-mark (- arg
) "Deleted backward"))
297 (defun cua-insert-char-at-global-mark ()
298 "Insert the character you type at the global mark position."
300 (cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted"))
302 (defun cua-insert-newline-at-global-mark ()
303 "Insert a newline at the global mark position."
305 (cua--insert-at-global-mark "\n"))
307 (defun cua-indent-to-global-mark-column ()
308 "Indent current line or rectangle to global mark column."
310 (if (cua--global-mark-active)
313 (set-buffer (marker-buffer cua--global-mark-marker
))
314 (goto-char (marker-position cua--global-mark-marker
))
315 (setq col
(current-column)))
317 (cua--indent-rectangle nil col t
)
319 (if (eq (current-buffer) (marker-buffer cua--global-mark-marker
))
321 (goto-char (marker-position cua--global-mark-marker
))
323 (move-marker cua--global-mark-marker
(point))
324 (move-overlay cua--global-mark-overlay
(point) (1+ (point))))))))
327 (defun cua-cancel-global-mark ()
328 "Cancel the global mark."
332 (if (cua--global-mark-active)
333 (cua--deactivate-global-mark t
)))
336 ;;; Post-command hook for global mark.
338 (defun cua--global-mark-post-command ()
339 (when (and (cua--global-mark-active) ;; Updates cua--global-mark-active variable
340 cua-global-mark-keep-visible
)
341 ;; keep global mark position visible
343 (if (or (not (eq (current-buffer) (marker-buffer cua--global-mark-marker
)))
344 (not (pos-visible-in-window-p (marker-position cua--global-mark-marker
))))
345 (let ((w (selected-window)) (p (point)) h
)
346 ;; The following code is an attempt to keep the global mark visible in
347 ;; other window -- but it doesn't work.
348 (switch-to-buffer-other-window (marker-buffer cua--global-mark-marker
) t
)
349 (goto-char (marker-position cua--global-mark-marker
))
350 (if (not (pos-visible-in-window-p (marker-position cua--global-mark-marker
)))
351 (recenter (if (> (setq h
(- (window-height) 4)) 1) h
'(4))))
357 (defun cua--init-global-mark ()
358 (define-key cua--global-mark-keymap
[remap copy-region-as-kill
] 'cua-copy-to-global-mark
)
359 (define-key cua--global-mark-keymap
[remap kill-ring-save
] 'cua-copy-to-global-mark
)
360 (define-key cua--global-mark-keymap
[remap kill-region
] 'cua-cut-to-global-mark
)
361 (define-key cua--global-mark-keymap
[remap yank
] 'cua-copy-next-to-global-mark
)
363 (define-key cua--global-mark-keymap
[remap keyboard-escape-quit
] 'cua-cancel-global-mark
)
364 (define-key cua--global-mark-keymap
[remap keyboard-quit
] 'cua-cancel-global-mark
)
366 (define-key cua--global-mark-keymap
[(control ?d
)] 'cua-cut-next-to-global-mark
)
367 (define-key cua--global-mark-keymap
[remap delete-backward-char
] 'cua-delete-backward-char-at-global-mark
)
368 (define-key cua--global-mark-keymap
[remap backward-delete-char
] 'cua-delete-backward-char-at-global-mark
)
369 (define-key cua--global-mark-keymap
[remap backward-delete-char-untabify
] 'cua-delete-backward-char-at-global-mark
)
370 (define-key cua--global-mark-keymap
[remap self-insert-command
] 'cua-insert-char-at-global-mark
)
371 (define-key cua--global-mark-keymap
[remap self-insert-iso
] 'cua-insert-char-at-global-mark
)
373 ;; Catch self-inserting characters which are "stolen" by other modes
374 (define-key cua--global-mark-keymap
[t]
375 '(menu-item "sic" cua-insert-char-at-global-mark :filter cua--self-insert-char-p))
377 (define-key cua--global-mark-keymap [remap newline] 'cua-insert-newline-at-global-mark)
378 (define-key cua--global-mark-keymap [remap newline-and-indent] 'cua-insert-newline-at-global-mark)
379 (define-key cua--global-mark-keymap "\r" 'cua-insert-newline-at-global-mark)
381 (define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column)
383 (setq cua--global-mark-initialized t))
387 ;; arch-tag: 553d8076-a91d-48ae-825d-6cb962a5f67f
388 ;;; cua-gmrk.el ends here