Fix display when left-fringe/right-fringe display spec is invalid.
[emacs.git] / lisp / emulation / cua-gmrk.el
blob408d90618aa0238b2f81ba716f18b422af28f810
1 ;;; cua-gmrk.el --- CUA unified global mark support
3 ;; Copyright (C) 1997-2012 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 (eval-when-compile
29 (require 'cua-base)
30 (require 'cua-rect)
33 ;;; Global Marker
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)
60 (if msg
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)))
66 (when (eobp)
67 (insert " ")
68 (backward-char 1))
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)
80 (if msg
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)
88 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 canceling it."
98 (interactive "P")
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)
104 (ding)
105 (message "Cannot set global mark in read-only buffer"))
106 (when (not stay)
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
113 (with-current-buffer (marker-buffer cua--global-mark-marker)
114 (goto-char (marker-position cua--global-mark-marker))
115 (insert-for-yank str)
116 (cua--activate-global-mark))
117 (if msg
118 (message "%s %d to global mark in %s:%d" msg
119 (length str)
120 (buffer-name (marker-buffer cua--global-mark-marker))
121 (marker-position cua--global-mark-marker))))
123 (defun cua--delete-at-global-mark (arg &optional msg)
124 ;; Delete chars at global marker
125 (with-current-buffer (marker-buffer cua--global-mark-marker)
126 (goto-char (marker-position cua--global-mark-marker))
127 (delete-char arg))
128 (if msg
129 (message "%s %d chars at global mark in %s:%d" msg arg
130 (buffer-name (marker-buffer cua--global-mark-marker))
131 (marker-position cua--global-mark-marker))))
133 (defun cua-copy-region-to-global-mark (start end)
134 "Copy region to global mark buffer/position."
135 (interactive "r")
136 (if (cua--global-mark-active)
137 (let ((src-buf (current-buffer)))
138 (save-excursion
139 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
140 (let ((text (cua--filter-buffer-noprops start end)))
141 (goto-char (marker-position cua--global-mark-marker))
142 (insert text))
143 (set-buffer (marker-buffer cua--global-mark-marker))
144 (goto-char (marker-position cua--global-mark-marker))
145 (insert-buffer-substring-as-yank src-buf start end))
146 (cua--activate-global-mark)
147 (message "Copied %d to global mark in %s:%d"
148 (abs (- end start))
149 (buffer-name (marker-buffer cua--global-mark-marker))
150 (marker-position cua--global-mark-marker))))
151 (cua--deactivate-global-mark)
152 (message "No Global Mark")))
154 (defun cua-cut-region-to-global-mark (start end)
155 "Move region to global buffer/position."
156 (interactive "r")
157 (if (cua--global-mark-active)
158 (let ((src-buf (current-buffer)))
159 (save-excursion
160 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
161 (if (and (< start (marker-position cua--global-mark-marker))
162 (< (marker-position cua--global-mark-marker) end))
163 (message "Can't move region into itself")
164 (let ((text (cua--filter-buffer-noprops start end))
165 (p1 (copy-marker start))
166 (p2 (copy-marker end)))
167 (goto-char (marker-position cua--global-mark-marker))
168 (insert text)
169 (cua--activate-global-mark)
170 (delete-region (marker-position p1) (marker-position p2))
171 (move-marker p1 nil)
172 (move-marker p2 nil)))
173 (set-buffer (marker-buffer cua--global-mark-marker))
174 (goto-char (marker-position cua--global-mark-marker))
175 (insert-buffer-substring src-buf start end)
176 (message "Moved %d to global mark in %s:%d"
177 (abs (- end start))
178 (buffer-name (marker-buffer cua--global-mark-marker))
179 (marker-position cua--global-mark-marker))
180 (cua--activate-global-mark)
181 (set-buffer src-buf)
182 (delete-region start end))))
183 (cua--deactivate-global-mark)
184 (message "No Global Mark")))
186 (defun cua--copy-rectangle-to-global-mark (as-text)
187 ;; Copy rectangle to global mark buffer/position.
188 (if (cua--global-mark-active)
189 (let ((src-buf (current-buffer))
190 (text (cua--extract-rectangle)))
191 (with-current-buffer (marker-buffer cua--global-mark-marker)
192 (goto-char (marker-position cua--global-mark-marker))
193 (if as-text
194 (while text
195 (insert-for-yank (car text))
196 (if (setq text (cdr text))
197 (insert "\n")))
198 (cua--insert-rectangle text 'auto))
199 (cua--activate-global-mark)
200 (message "Copied rectangle to global mark in %s:%d"
201 (buffer-name (marker-buffer cua--global-mark-marker))
202 (marker-position cua--global-mark-marker))))
203 (cua--deactivate-global-mark)
204 (message "No Global Mark")))
206 (defun cua--cut-rectangle-to-global-mark (as-text)
207 ;; Move rectangle to global buffer/position.
208 (if (cua--global-mark-active)
209 (let ((src-buf (current-buffer)))
210 (save-excursion
211 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
212 (let ((olist (overlays-at (marker-position cua--global-mark-marker)))
213 in-rect)
214 (while olist
215 (if (eq (overlay-get (car olist) 'face) 'cua-rectangle)
216 (setq in-rect t olist nil)
217 (setq olist (cdr olist))))
218 (if in-rect
219 (message "Can't move rectangle into itself")
220 (let ((text (cua--extract-rectangle)))
221 (cua--delete-rectangle)
222 (goto-char (marker-position cua--global-mark-marker))
223 (if as-text
224 (while text
225 (insert-for-yank (car text))
226 (if (setq text (cdr text))
227 (insert "\n")))
228 (cua--insert-rectangle text 'auto))
229 (cua--activate-global-mark))))
230 (let ((text (cua--extract-rectangle)))
231 (cua--delete-rectangle)
232 (set-buffer (marker-buffer cua--global-mark-marker))
233 (goto-char (marker-position cua--global-mark-marker))
234 (cua--insert-rectangle text 'auto))
235 (message "Moved rectangle to global mark in %s:%d"
236 (buffer-name (marker-buffer cua--global-mark-marker))
237 (marker-position cua--global-mark-marker))
238 (cua--activate-global-mark))))
239 (cua--deactivate-global-mark)
240 (message "No Global Mark")))
242 (defun cua-copy-to-global-mark ()
243 "Copy active region/rectangle to global mark buffer/position."
244 (interactive)
245 (setq cua--last-killed-rectangle nil)
246 (if cua--rectangle
247 (cua--copy-rectangle-to-global-mark nil)
248 (let ((start (mark)) (end (point)))
249 (or (<= start end)
250 (setq start (prog1 end (setq end start))))
251 (cua-copy-region-to-global-mark start end))))
253 (defun cua-copy-next-to-global-mark (n)
254 "Copy the following N characters in buffer to global mark buffer/position."
255 (interactive "p")
256 (setq cua--last-killed-rectangle nil)
257 (or (eobp)
258 (let ((p (point)))
259 (goto-char (+ p n))
260 (cua-copy-region-to-global-mark p (point)))))
262 (defun cua-cut-to-global-mark ()
263 "Move active region/rectangle to global mark buffer/position."
264 (interactive)
265 (if buffer-read-only
266 (cua-copy-to-global-mark)
267 (setq cua--last-killed-rectangle nil)
268 (if cua--rectangle
269 (cua--cut-rectangle-to-global-mark nil)
270 (let ((start (mark)) (end (point)))
271 (or (<= start end)
272 (setq start (prog1 end (setq end start))))
273 (cua-cut-region-to-global-mark start end)))))
275 (defun cua-cut-next-to-global-mark (n)
276 "Move the following N characters in buffer to global mark buffer/position."
277 (interactive "p")
278 (setq cua--last-killed-rectangle nil)
279 (or (eobp)
280 (let ((p (point)))
281 (goto-char (+ p n))
282 (cua-cut-region-to-global-mark p (point)))))
284 (defun cua-delete-char-at-global-mark (arg)
285 "Delete character following the global mark position."
286 (interactive "p")
287 (cua--delete-at-global-mark arg "Deleted"))
289 (defun cua-delete-backward-char-at-global-mark (arg)
290 "Delete character before the global mark position."
291 (interactive "p")
292 (cua--delete-at-global-mark (- arg) "Deleted backward"))
294 (defun cua-insert-char-at-global-mark ()
295 "Insert the character you type at the global mark position."
296 (interactive)
297 (cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted"))
299 (defun cua-insert-newline-at-global-mark ()
300 "Insert a newline at the global mark position."
301 (interactive)
302 (cua--insert-at-global-mark "\n"))
304 (defun cua-indent-to-global-mark-column ()
305 "Indent current line or rectangle to global mark column."
306 (interactive "*")
307 (if (cua--global-mark-active)
308 (let (col)
309 (with-current-buffer (marker-buffer cua--global-mark-marker)
310 (goto-char (marker-position cua--global-mark-marker))
311 (setq col (current-column)))
312 (if cua--rectangle
313 (cua--indent-rectangle nil col t)
314 (indent-to col))
315 (if (eq (current-buffer) (marker-buffer cua--global-mark-marker))
316 (save-excursion
317 (goto-char (marker-position cua--global-mark-marker))
318 (move-to-column col)
319 (move-marker cua--global-mark-marker (point))
320 (move-overlay cua--global-mark-overlay (point) (1+ (point))))))))
323 (defun cua-cancel-global-mark ()
324 "Cancel the global mark."
325 (interactive)
326 (if mark-active
327 (cua-cancel)
328 (if (cua--global-mark-active)
329 (cua--deactivate-global-mark t)))
330 (cua--fallback))
332 ;;; Post-command hook for global mark.
334 (defun cua--global-mark-post-command ()
335 (when (and (cua--global-mark-active) ;; Updates cua--global-mark-active variable
336 cua-global-mark-keep-visible)
337 ;; keep global mark position visible
338 (sit-for 0)
339 (if (or (not (eq (current-buffer) (marker-buffer cua--global-mark-marker)))
340 (not (pos-visible-in-window-p (marker-position cua--global-mark-marker))))
341 (let ((w (selected-window)) (p (point)) h)
342 ;; The following code is an attempt to keep the global mark visible in
343 ;; other window -- but it doesn't work.
344 (switch-to-buffer-other-window (marker-buffer cua--global-mark-marker) t)
345 (goto-char (marker-position cua--global-mark-marker))
346 (if (not (pos-visible-in-window-p (marker-position cua--global-mark-marker)))
347 (recenter (if (> (setq h (- (window-height) 4)) 1) h '(4))))
348 (select-window w)
349 (goto-char p)))))
351 ;;; Initialization
353 (defun cua--init-global-mark ()
354 (define-key cua--global-mark-keymap [remap copy-region-as-kill] 'cua-copy-to-global-mark)
355 (define-key cua--global-mark-keymap [remap kill-ring-save] 'cua-copy-to-global-mark)
356 (define-key cua--global-mark-keymap [remap kill-region] 'cua-cut-to-global-mark)
357 (define-key cua--global-mark-keymap [remap yank] 'cua-copy-next-to-global-mark)
359 (define-key cua--global-mark-keymap [remap keyboard-escape-quit] 'cua-cancel-global-mark)
360 (define-key cua--global-mark-keymap [remap keyboard-quit] 'cua-cancel-global-mark)
362 (define-key cua--global-mark-keymap [(control ?d)] 'cua-cut-next-to-global-mark)
363 (define-key cua--global-mark-keymap [remap delete-backward-char] 'cua-delete-backward-char-at-global-mark)
364 (define-key cua--global-mark-keymap [remap backward-delete-char] 'cua-delete-backward-char-at-global-mark)
365 (define-key cua--global-mark-keymap [remap backward-delete-char-untabify] 'cua-delete-backward-char-at-global-mark)
366 (define-key cua--global-mark-keymap [remap self-insert-command] 'cua-insert-char-at-global-mark)
367 (define-key cua--global-mark-keymap [remap self-insert-iso] 'cua-insert-char-at-global-mark)
369 ;; Catch self-inserting characters which are "stolen" by other modes
370 (define-key cua--global-mark-keymap [t]
371 '(menu-item "sic" cua-insert-char-at-global-mark :filter cua--self-insert-char-p))
373 (define-key cua--global-mark-keymap [remap newline] 'cua-insert-newline-at-global-mark)
374 (define-key cua--global-mark-keymap [remap newline-and-indent] 'cua-insert-newline-at-global-mark)
375 (define-key cua--global-mark-keymap "\r" 'cua-insert-newline-at-global-mark)
377 (define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column)
379 (setq cua--global-mark-initialized t))
381 (provide 'cua-gmrk)
383 ;;; cua-gmrk.el ends here