Fix (mapcar F S) crash when F alters S’s length
[emacs.git] / lisp / emulation / cua-gmrk.el
blob8e023783f3180e108213627d49e0701bfd8c1efd
1 ;;; cua-gmrk.el --- CUA unified global mark support
3 ;; Copyright (C) 1997-2016 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard emulations convenience cua mark
7 ;; Package: cua-base
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/>.
24 ;;; Commentary:
26 ;;; Code:
28 (require 'cua-base)
29 (require 'cua-rect)
31 ;;; Global Marker
33 ;; Non-nil when global marker is active.
34 (defvar cua--global-mark-active nil)
36 ;; Global mark position marker.
37 (defvar cua--global-mark-marker nil)
39 ;; Overlay for global mark position.
40 (defvar cua--global-mark-overlay nil)
42 ;; Initialize global mark things once...
43 (defvar cua--global-mark-initialized nil)
45 ;; Saved configured blink-cursor-interval
46 (defvar cua--orig-blink-cursor-interval nil)
48 (defun cua--deactivate-global-mark (&optional msg)
49 (when cua--global-mark-overlay
50 (delete-overlay cua--global-mark-overlay)
51 (setq cua--global-mark-overlay nil))
52 (if (markerp cua--global-mark-marker)
53 (move-marker cua--global-mark-marker nil))
54 (if cua--orig-blink-cursor-interval
55 (setq blink-cursor-interval cua--orig-blink-cursor-interval
56 cua--orig-blink-cursor-interval nil))
57 (setq cua--global-mark-active nil)
58 (if msg
59 (message "Global Mark Cleared")))
61 (defun cua--activate-global-mark (&optional msg)
62 (if (not (markerp cua--global-mark-marker))
63 (setq cua--global-mark-marker (make-marker)))
64 (when (eobp)
65 (insert " ")
66 (backward-char 1))
67 (move-marker cua--global-mark-marker (point))
68 (if (overlayp cua--global-mark-overlay)
69 (move-overlay cua--global-mark-overlay (point) (1+ (point)))
70 (setq cua--global-mark-overlay
71 (make-overlay (point) (1+ (point))))
72 (overlay-put cua--global-mark-overlay 'face 'cua-global-mark))
73 (if (and cua-global-mark-blink-cursor-interval
74 (not cua--orig-blink-cursor-interval))
75 (setq cua--orig-blink-cursor-interval blink-cursor-interval
76 blink-cursor-interval cua-global-mark-blink-cursor-interval))
77 (setq cua--global-mark-active t)
78 (if msg
79 (message "Global Mark Set")))
81 (defun cua--global-mark-active ()
82 (if cua--global-mark-active
83 (or (and (markerp cua--global-mark-marker)
84 (marker-buffer cua--global-mark-marker))
85 (and (cua--deactivate-global-mark nil)
86 nil))))
88 (defun cua-toggle-global-mark (stay)
89 "Set or cancel the global marker.
90 When the global marker is set, CUA cut and copy commands will automatically
91 insert the deleted or copied text before the global marker, even when the
92 global marker is in another buffer.
93 If the global marker isn't set, set the global marker at point in the current
94 buffer. Otherwise jump to the global marker position and cancel it.
95 With prefix argument, don't jump to global mark when canceling it."
96 (interactive "P")
97 (unless cua--global-mark-initialized
98 (cua--init-global-mark))
99 (if (not (cua--global-mark-active))
100 (if (not buffer-read-only)
101 (cua--activate-global-mark t)
102 (ding)
103 (message "Cannot set global mark in read-only buffer"))
104 (when (not stay)
105 (pop-to-buffer (marker-buffer cua--global-mark-marker))
106 (goto-char cua--global-mark-marker))
107 (cua--deactivate-global-mark t)))
109 (defun cua--insert-at-global-mark (str &optional msg)
110 ;; Insert string at global marker and move marker
111 (with-current-buffer (marker-buffer cua--global-mark-marker)
112 (goto-char (marker-position cua--global-mark-marker))
113 (insert-for-yank str)
114 (cua--activate-global-mark))
115 (if msg
116 (message "%s %d to global mark in %s:%d" msg
117 (length str)
118 (buffer-name (marker-buffer cua--global-mark-marker))
119 (marker-position cua--global-mark-marker))))
121 (defun cua--delete-at-global-mark (arg &optional msg)
122 ;; Delete chars at global marker
123 (with-current-buffer (marker-buffer cua--global-mark-marker)
124 (goto-char (marker-position cua--global-mark-marker))
125 (delete-char arg))
126 (if msg
127 (message "%s %d chars at global mark in %s:%d" msg arg
128 (buffer-name (marker-buffer cua--global-mark-marker))
129 (marker-position cua--global-mark-marker))))
131 (defun cua-copy-region-to-global-mark (start end)
132 "Copy region to global mark buffer/position."
133 (interactive "r")
134 (if (cua--global-mark-active)
135 (let ((src-buf (current-buffer)))
136 (save-excursion
137 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
138 (let ((text (cua--filter-buffer-noprops start end)))
139 (goto-char (marker-position cua--global-mark-marker))
140 (insert text))
141 (set-buffer (marker-buffer cua--global-mark-marker))
142 (goto-char (marker-position cua--global-mark-marker))
143 (insert-buffer-substring-as-yank src-buf start end))
144 (cua--activate-global-mark)
145 (message "Copied %d to global mark in %s:%d"
146 (abs (- end start))
147 (buffer-name (marker-buffer cua--global-mark-marker))
148 (marker-position cua--global-mark-marker))))
149 (cua--deactivate-global-mark)
150 (message "No Global Mark")))
152 (defun cua-cut-region-to-global-mark (start end)
153 "Move region to global buffer/position."
154 (interactive "r")
155 (if (cua--global-mark-active)
156 (let ((src-buf (current-buffer)))
157 (save-excursion
158 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
159 (if (and (< start (marker-position cua--global-mark-marker))
160 (< (marker-position cua--global-mark-marker) end))
161 (message "Can't move region into itself")
162 (let ((text (cua--filter-buffer-noprops start end))
163 (p1 (copy-marker start))
164 (p2 (copy-marker end)))
165 (goto-char (marker-position cua--global-mark-marker))
166 (insert text)
167 (cua--activate-global-mark)
168 (delete-region (marker-position p1) (marker-position p2))
169 (move-marker p1 nil)
170 (move-marker p2 nil)))
171 (set-buffer (marker-buffer cua--global-mark-marker))
172 (goto-char (marker-position cua--global-mark-marker))
173 (insert-buffer-substring src-buf start end)
174 (message "Moved %d to global mark in %s:%d"
175 (abs (- end start))
176 (buffer-name (marker-buffer cua--global-mark-marker))
177 (marker-position cua--global-mark-marker))
178 (cua--activate-global-mark)
179 (set-buffer src-buf)
180 (delete-region start end))))
181 (cua--deactivate-global-mark)
182 (message "No Global Mark")))
184 (defun cua--copy-rectangle-to-global-mark (as-text)
185 ;; Copy rectangle to global mark buffer/position.
186 (if (cua--global-mark-active)
187 (let ((src-buf (current-buffer))
188 (text (cua--extract-rectangle)))
189 (with-current-buffer (marker-buffer cua--global-mark-marker)
190 (goto-char (marker-position cua--global-mark-marker))
191 (if as-text
192 (while text
193 (insert-for-yank (car text))
194 (if (setq text (cdr text))
195 (insert "\n")))
196 (cua--insert-rectangle text 'auto))
197 (cua--activate-global-mark)
198 (message "Copied rectangle to global mark in %s:%d"
199 (buffer-name (marker-buffer cua--global-mark-marker))
200 (marker-position cua--global-mark-marker))))
201 (cua--deactivate-global-mark)
202 (message "No Global Mark")))
204 (defun cua--cut-rectangle-to-global-mark (as-text)
205 ;; Move rectangle to global buffer/position.
206 (if (cua--global-mark-active)
207 (let ((src-buf (current-buffer)))
208 (save-excursion
209 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
210 (let ((olist (overlays-at (marker-position cua--global-mark-marker)))
211 in-rect)
212 (while olist
213 (if (eq (overlay-get (car olist) 'face) 'cua-rectangle)
214 (setq in-rect t olist nil)
215 (setq olist (cdr olist))))
216 (if in-rect
217 (message "Can't move rectangle into itself")
218 (let ((text (cua--extract-rectangle)))
219 (cua--delete-rectangle)
220 (goto-char (marker-position cua--global-mark-marker))
221 (if as-text
222 (while text
223 (insert-for-yank (car text))
224 (if (setq text (cdr text))
225 (insert "\n")))
226 (cua--insert-rectangle text 'auto))
227 (cua--activate-global-mark))))
228 (let ((text (cua--extract-rectangle)))
229 (cua--delete-rectangle)
230 (set-buffer (marker-buffer cua--global-mark-marker))
231 (goto-char (marker-position cua--global-mark-marker))
232 (cua--insert-rectangle text 'auto))
233 (message "Moved rectangle to global mark in %s:%d"
234 (buffer-name (marker-buffer cua--global-mark-marker))
235 (marker-position cua--global-mark-marker))
236 (cua--activate-global-mark))))
237 (cua--deactivate-global-mark)
238 (message "No Global Mark")))
240 (defun cua-copy-to-global-mark ()
241 "Copy active region/rectangle to global mark buffer/position."
242 (interactive)
243 (setq cua--last-killed-rectangle nil)
244 (if cua--rectangle
245 (cua--copy-rectangle-to-global-mark nil)
246 (let ((start (mark)) (end (point)))
247 (or (<= start end)
248 (setq start (prog1 end (setq end start))))
249 (cua-copy-region-to-global-mark start end))))
251 (defun cua-copy-next-to-global-mark (n)
252 "Copy the following N characters in buffer to global mark buffer/position."
253 (interactive "p")
254 (setq cua--last-killed-rectangle nil)
255 (or (eobp)
256 (let ((p (point)))
257 (goto-char (+ p n))
258 (cua-copy-region-to-global-mark p (point)))))
260 (defun cua-cut-to-global-mark ()
261 "Move active region/rectangle to global mark buffer/position."
262 (interactive)
263 (if buffer-read-only
264 (cua-copy-to-global-mark)
265 (setq cua--last-killed-rectangle nil)
266 (if cua--rectangle
267 (cua--cut-rectangle-to-global-mark nil)
268 (let ((start (mark)) (end (point)))
269 (or (<= start end)
270 (setq start (prog1 end (setq end start))))
271 (cua-cut-region-to-global-mark start end)))))
273 (defun cua-cut-next-to-global-mark (n)
274 "Move the following N characters in buffer to global mark buffer/position."
275 (interactive "p")
276 (setq cua--last-killed-rectangle nil)
277 (or (eobp)
278 (let ((p (point)))
279 (goto-char (+ p n))
280 (cua-cut-region-to-global-mark p (point)))))
282 (defun cua-delete-char-at-global-mark (arg)
283 "Delete character following the global mark position."
284 (interactive "p")
285 (cua--delete-at-global-mark arg "Deleted"))
287 (defun cua-delete-backward-char-at-global-mark (arg)
288 "Delete character before the global mark position."
289 (interactive "p")
290 (cua--delete-at-global-mark (- arg) "Deleted backward"))
292 (defun cua-insert-char-at-global-mark ()
293 "Insert the character you type at the global mark position."
294 (interactive)
295 (cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted"))
297 (defun cua-insert-newline-at-global-mark ()
298 "Insert a newline at the global mark position."
299 (interactive)
300 (cua--insert-at-global-mark "\n"))
302 (defun cua-indent-to-global-mark-column ()
303 "Indent current line or rectangle to global mark column."
304 (interactive "*")
305 (if (cua--global-mark-active)
306 (let (col)
307 (with-current-buffer (marker-buffer cua--global-mark-marker)
308 (goto-char (marker-position cua--global-mark-marker))
309 (setq col (current-column)))
310 (if cua--rectangle
311 (cua--indent-rectangle nil col t)
312 (indent-to col))
313 (if (eq (current-buffer) (marker-buffer cua--global-mark-marker))
314 (save-excursion
315 (goto-char (marker-position cua--global-mark-marker))
316 (move-to-column col)
317 (move-marker cua--global-mark-marker (point))
318 (move-overlay cua--global-mark-overlay (point) (1+ (point))))))))
321 (defun cua-cancel-global-mark ()
322 "Cancel the global mark."
323 (interactive)
324 (if (region-active-p)
325 (cua-cancel)
326 (if (cua--global-mark-active)
327 (cua--deactivate-global-mark t)))
328 (cua--fallback))
330 ;;; Post-command hook for global mark.
332 (defun cua--global-mark-post-command ()
333 (when (and (cua--global-mark-active) ;; Updates cua--global-mark-active variable
334 cua-global-mark-keep-visible)
335 ;; keep global mark position visible
336 (sit-for 0)
337 (if (or (not (eq (current-buffer) (marker-buffer cua--global-mark-marker)))
338 (not (pos-visible-in-window-p (marker-position cua--global-mark-marker))))
339 (let ((w (selected-window)) (p (point)) h)
340 ;; The following code is an attempt to keep the global mark visible in
341 ;; other window -- but it doesn't work.
342 (switch-to-buffer-other-window (marker-buffer cua--global-mark-marker) t)
343 (goto-char (marker-position cua--global-mark-marker))
344 (if (not (pos-visible-in-window-p (marker-position cua--global-mark-marker)))
345 (recenter (if (> (setq h (- (window-height) 4)) 1) h '(4))))
346 (select-window w)
347 (goto-char p)))))
349 ;;; Initialization
351 (defun cua--init-global-mark ()
352 (define-key cua--global-mark-keymap [remap copy-region-as-kill] 'cua-copy-to-global-mark)
353 (define-key cua--global-mark-keymap [remap kill-ring-save] 'cua-copy-to-global-mark)
354 (define-key cua--global-mark-keymap [remap kill-region] 'cua-cut-to-global-mark)
355 (define-key cua--global-mark-keymap [remap yank] 'cua-copy-next-to-global-mark)
357 (define-key cua--global-mark-keymap [remap keyboard-escape-quit] 'cua-cancel-global-mark)
358 (define-key cua--global-mark-keymap [remap keyboard-quit] 'cua-cancel-global-mark)
360 (define-key cua--global-mark-keymap [(control ?d)] 'cua-cut-next-to-global-mark)
361 (define-key cua--global-mark-keymap [remap delete-backward-char] 'cua-delete-backward-char-at-global-mark)
362 (define-key cua--global-mark-keymap [remap backward-delete-char] 'cua-delete-backward-char-at-global-mark)
363 (define-key cua--global-mark-keymap [remap backward-delete-char-untabify] 'cua-delete-backward-char-at-global-mark)
364 (define-key cua--global-mark-keymap [remap self-insert-command] 'cua-insert-char-at-global-mark)
366 ;; Catch self-inserting characters which are "stolen" by other modes
367 (define-key cua--global-mark-keymap [t]
368 '(menu-item "sic" cua-insert-char-at-global-mark :filter cua--self-insert-char-p))
370 (define-key cua--global-mark-keymap [remap newline] 'cua-insert-newline-at-global-mark)
371 (define-key cua--global-mark-keymap [remap newline-and-indent] 'cua-insert-newline-at-global-mark)
372 (define-key cua--global-mark-keymap "\r" 'cua-insert-newline-at-global-mark)
374 (define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column)
376 (setq cua--global-mark-initialized t))
378 (provide 'cua-gmrk)
380 ;;; cua-gmrk.el ends here