1 ;;; cua-gmrk.el --- CUA unified global mark support
3 ;; Copyright (C) 1997-2002 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard emulations convenience cua mark
8 ;; This file is part of GNU Emacs.
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)
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.
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.
38 ;; Non-nil when global marker is active.
39 (defvar cua--global-mark-active nil
)
41 ;; Global mark position marker.
42 (defvar cua--global-mark-marker nil
)
44 ;; Overlay for global mark position.
45 (defvar cua--global-mark-overlay nil
)
47 ;; Initialize global mark things once...
48 (defvar cua--global-mark-initialized nil
)
50 ;; Saved configured blink-cursor-interval
51 (defvar cua--orig-blink-cursor-interval nil
)
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
)
64 (message "Global Mark Cleared")))
66 (defun cua--activate-global-mark (&optional msg
)
67 (if (not (markerp cua--global-mark-marker
))
68 (setq cua--global-mark-marker
(make-marker)))
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)))
75 (setq cua--global-mark-overlay
76 (make-overlay (point) (1+ (point))))
77 (overlay-put cua--global-mark-overlay
'face
'cua-global-mark
))
78 (if (and cua-global-mark-blink-cursor-interval
79 (not cua--orig-blink-cursor-interval
))
80 (setq cua--orig-blink-cursor-interval blink-cursor-interval
81 blink-cursor-interval cua-global-mark-blink-cursor-interval
))
82 (setq cua--global-mark-active t
)
84 (message "Global Mark Set")))
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
)
93 (defun cua-toggle-global-mark (stay)
94 "Set or cancel the global marker.
95 When the global marker is set, CUA cut and copy commands will automatically
96 insert the deleted or copied text before the global marker, even when the
97 global marker is in another buffer.
98 If the global marker isn't set, set the global marker at point in the current
99 buffer. Otherwise jump to the global marker position and cancel it.
100 With prefix argument, don't jump to global mark when cancelling it."
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
)
108 (message "Cannot set global mark in read-only buffer"))
110 (pop-to-buffer (marker-buffer cua--global-mark-marker
))
111 (goto-char cua--global-mark-marker
))
112 (cua--deactivate-global-mark t
)))
114 (defun cua--insert-at-global-mark (str &optional msg
)
115 ;; Insert string at global marker and move marker
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))
122 (message "%s %d to global mark in %s:%d" msg
124 (buffer-name (marker-buffer cua--global-mark-marker
))
125 (marker-position cua--global-mark-marker
))))
127 (defun cua--delete-at-global-mark (arg &optional msg
)
128 ;; Delete chars at global marker
130 (set-buffer (marker-buffer cua--global-mark-marker
))
131 (goto-char (marker-position cua--global-mark-marker
))
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
))))
138 (defun cua-copy-region-to-global-mark (start end
)
139 "Copy region to global mark buffer/position."
141 (if (cua--global-mark-active)
142 (let ((src-buf (current-buffer)))
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
))
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"
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")))
159 (defun cua-cut-region-to-global-mark (start end
)
160 "Move region to global buffer/position."
162 (if (cua--global-mark-active)
163 (let ((src-buf (current-buffer)))
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
))
174 (cua--activate-global-mark)
175 (delete-region (marker-position p1
) (marker-position p2
))
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"
183 (buffer-name (marker-buffer cua--global-mark-marker
))
184 (marker-position cua--global-mark-marker
))
185 (cua--activate-global-mark)
187 (delete-region start end
))))
188 (cua--deactivate-global-mark)
189 (message "No Global Mark")))
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)))
197 (set-buffer (marker-buffer cua--global-mark-marker
))
198 (goto-char (marker-position cua--global-mark-marker
))
201 (insert-for-yank (car text
))
202 (if (setq text
(cdr text
))
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")))
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)))
217 (if (equal (marker-buffer cua--global-mark-marker
) src-buf
)
218 (let ((olist (overlays-at (marker-position cua--global-mark-marker
)))
221 (if (eq (overlay-get (car olist
) 'face
) 'cua-rectangle
)
222 (setq in-rect t olist nil
)
223 (setq olist
(cdr olist
))))
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
))
231 (insert-for-yank (car text
))
232 (if (setq text
(cdr text
))
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")))
248 (defun cua-copy-to-global-mark ()
249 "Copy active region/rectangle to global mark buffer/position."
251 (setq cua--last-killed-rectangle nil
)
253 (cua--copy-rectangle-to-global-mark nil
)
254 (let ((start (mark)) (end (point)))
256 (setq start
(prog1 end
(setq end start
))))
257 (cua-copy-region-to-global-mark start end
))))
259 (defun cua-copy-next-to-global-mark (n)
260 "Copy the following N characters in buffer to global mark buffer/position."
262 (setq cua--last-killed-rectangle nil
)
266 (cua-copy-region-to-global-mark p
(point)))))
268 (defun cua-cut-to-global-mark ()
269 "Move active region/rectangle to global mark buffer/position."
272 (cua-copy-to-global-mark)
273 (setq cua--last-killed-rectangle nil
)
275 (cua--cut-rectangle-to-global-mark nil
)
276 (let ((start (mark)) (end (point)))
278 (setq start
(prog1 end
(setq end start
))))
279 (cua-cut-region-to-global-mark start end
)))))
281 (defun cua-cut-next-to-global-mark (n)
282 "Move the following N characters in buffer to global mark buffer/position."
284 (setq cua--last-killed-rectangle nil
)
288 (cua-cut-region-to-global-mark p
(point)))))
290 (defun cua-delete-char-at-global-mark (arg)
291 "Delete character following the global mark position."
293 (cua--delete-at-global-mark arg
"Deleted"))
295 (defun cua-delete-backward-char-at-global-mark (arg)
296 "Delete character before the global mark position."
298 (cua--delete-at-global-mark (- arg
) "Deleted backward"))
300 (defun cua-insert-char-at-global-mark ()
301 "Insert the character you type at the global mark position."
303 (cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted"))
305 (defun cua-insert-newline-at-global-mark ()
306 "Insert a newline at the global mark position."
308 (cua--insert-at-global-mark "\n"))
310 (defun cua-indent-to-global-mark-column ()
311 "Indent current line or rectangle to global mark column."
313 (if (cua--global-mark-active)
316 (set-buffer (marker-buffer cua--global-mark-marker
))
317 (goto-char (marker-position cua--global-mark-marker
))
318 (setq col
(current-column)))
320 (cua--indent-rectangle nil col t
)
322 (if (eq (current-buffer) (marker-buffer cua--global-mark-marker
))
324 (goto-char (marker-position cua--global-mark-marker
))
326 (move-marker cua--global-mark-marker
(point))
327 (move-overlay cua--global-mark-overlay
(point) (1+ (point))))))))
330 (defun cua-cancel-global-mark ()
331 "Cancel the global mark."
335 (if (cua--global-mark-active)
336 (cua--deactivate-global-mark t
)))
339 ;;; Post-command hook for global mark.
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
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
)
349 ;; The following code is an attempt to keep the global mark visible in
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))))
360 (defun cua--init-global-mark ()
361 (define-key cua--global-mark-keymap
[remap copy-region-as-kill
] 'cua-copy-to-global-mark
)
362 (define-key cua--global-mark-keymap
[remap kill-ring-save
] 'cua-copy-to-global-mark
)
363 (define-key cua--global-mark-keymap
[remap kill-region
] 'cua-cut-to-global-mark
)
364 (define-key cua--global-mark-keymap
[remap yank
] 'cua-copy-next-to-global-mark
)
366 (define-key cua--global-mark-keymap
[remap keyboard-escape-quit
] 'cua-cancel-global-mark
)
367 (define-key cua--global-mark-keymap
[remap keyboard-quit
] 'cua-cancel-global-mark
)
369 (define-key cua--global-mark-keymap
[(control ?d
)] 'cua-cut-next-to-global-mark
)
370 (define-key cua--global-mark-keymap
[remap delete-backward-char
] 'cua-delete-backward-char-at-global-mark
)
371 (define-key cua--global-mark-keymap
[remap backward-delete-char
] 'cua-delete-backward-char-at-global-mark
)
372 (define-key cua--global-mark-keymap
[remap backward-delete-char-untabify
] 'cua-delete-backward-char-at-global-mark
)
373 (define-key cua--global-mark-keymap
[remap self-insert-command
] 'cua-insert-char-at-global-mark
)
374 (define-key cua--global-mark-keymap
[remap self-insert-iso
] 'cua-insert-char-at-global-mark
)
376 ;; Catch self-inserting characters which are "stolen" by other modes
377 (define-key cua--global-mark-keymap
[t]
378 '(menu-item "sic" cua-insert-char-at-global-mark :filter cua--self-insert-char-p))
380 (define-key cua--global-mark-keymap [remap newline] 'cua-insert-newline-at-global-mark)
381 (define-key cua--global-mark-keymap [remap newline-and-indent] 'cua-insert-newline-at-global-mark)
382 (define-key cua--global-mark-keymap "\r" 'cua-insert-newline-at-global-mark)
384 (define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column)
386 (setq cua--global-mark-initialized t))
388 ;;; arch-tag: 553d8076-a91d-48ae-825d-6cb962a5f67f
389 ;;; cua-gmrk.el ends here