comparison lisp/emulation/cua-gmrk.el @ 44938:358d42530d42

Added cua-mode based files [split from original cua.el]: cua-base.el, cua-rect.el, cua-gmrk.el, and keypad.el
author Kim F. Storm <storm@cua.dk>
date Sun, 28 Apr 2002 21:48:39 +0000
parents
children 829beb9a6a4b
comparison
equal deleted inserted replaced
44937:75c89848438b 44938:358d42530d42
1 ;;; cua-gmrk.el --- CUA unified global mark support
2
3 ;; Copyright (C) 1997-2002 Free Software Foundation, Inc.
4
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard emulations convenience cua mark
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25
26 (provide 'cua-gmrk)
27
28 (eval-when-compile
29 (require 'cua-base)
30 (require 'cua-rect)
31 )
32
33 ;;; Global Marker
34
35 ;; Non-nil when global marker is active.
36 (defvar cua--global-mark-active nil)
37
38 ;; Global mark position marker.
39 (defvar cua--global-mark-marker nil)
40
41 ;; Overlay for global mark position.
42 (defvar cua--global-mark-overlay nil)
43
44 ;; Initialize global mark things once...
45 (defvar cua--global-mark-initialized nil)
46
47 ;; Saved configured blink-cursor-interval
48 (defvar cua--orig-blink-cursor-interval nil)
49
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")))
62
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-face))
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")))
82
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))))
89
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."
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)))
110
111 (defun cua--insert-at-global-mark (str &optional msg)
112 ;; Insert string at global marker and move marker
113 (save-excursion
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))
118 (if msg
119 (message "%s %d to global mark in %s:%d" msg
120 (length str)
121 (buffer-name (marker-buffer cua--global-mark-marker))
122 (marker-position cua--global-mark-marker))))
123
124 (defun cua--delete-at-global-mark (arg &optional msg)
125 ;; Delete chars at global marker
126 (save-excursion
127 (set-buffer (marker-buffer cua--global-mark-marker))
128 (goto-char (marker-position cua--global-mark-marker))
129 (delete-char arg))
130 (if msg
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))))
134
135 (defun cua-copy-region-to-global-mark (start end)
136 "Copy region to global mark buffer/position."
137 (interactive "r")
138 (if (cua--global-mark-active)
139 (let ((src-buf (current-buffer)))
140 (save-excursion
141 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
142 (let ((text (buffer-substring-no-properties start end)))
143 (goto-char (marker-position cua--global-mark-marker))
144 (insert text))
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"
150 (abs (- end start))
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")))
155
156 (defun cua-cut-region-to-global-mark (start end)
157 "Move region to global buffer/position."
158 (interactive "r")
159 (if (cua--global-mark-active)
160 (let ((src-buf (current-buffer)))
161 (save-excursion
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 (buffer-substring-no-properties start end))
167 (p1 (copy-marker start))
168 (p2 (copy-marker end)))
169 (goto-char (marker-position cua--global-mark-marker))
170 (insert text)
171 (cua--activate-global-mark)
172 (delete-region (marker-position p1) (marker-position p2))
173 (move-marker p1 nil)
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"
179 (abs (- end start))
180 (buffer-name (marker-buffer cua--global-mark-marker))
181 (marker-position cua--global-mark-marker))
182 (cua--activate-global-mark)
183 (set-buffer src-buf)
184 (delete-region start end))))
185 (cua--deactivate-global-mark)
186 (message "No Global Mark")))
187
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)))
193 (save-excursion
194 (set-buffer (marker-buffer cua--global-mark-marker))
195 (goto-char (marker-position cua--global-mark-marker))
196 (if as-text
197 (while text
198 (insert-for-yank (car text))
199 (if (setq text (cdr text))
200 (insert "\n")))
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")))
208
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)))
213 (save-excursion
214 (if (equal (marker-buffer cua--global-mark-marker) src-buf)
215 (let ((olist (overlays-at (marker-position cua--global-mark-marker)))
216 in-rect)
217 (while olist
218 (if (eq (overlay-get (car olist) 'face) 'cua-rectangle-face)
219 (setq in-rect t olist nil)
220 (setq olist (cdr olist))))
221 (if in-rect
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))
226 (if as-text
227 (while text
228 (insert-for-yank (car text))
229 (if (setq text (cdr text))
230 (insert "\n")))
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")))
244
245 (defun cua-copy-to-global-mark ()
246 "Copy active region/rectangle to global mark buffer/position."
247 (interactive)
248 (setq cua--last-killed-rectangle nil)
249 (if cua--rectangle
250 (cua--copy-rectangle-to-global-mark nil)
251 (let ((start (mark)) (end (point)))
252 (or (<= start end)
253 (setq start (prog1 end (setq end start))))
254 (cua-copy-region-to-global-mark start end))))
255
256 (defun cua-copy-next-to-global-mark (n)
257 "Copy the following N characters in buffer to global mark buffer/position."
258 (interactive "p")
259 (setq cua--last-killed-rectangle nil)
260 (or (eobp)
261 (let ((p (point)))
262 (goto-char (+ p n))
263 (cua-copy-region-to-global-mark p (point)))))
264
265 (defun cua-cut-to-global-mark ()
266 "Move active region/rectangle to global mark buffer/position."
267 (interactive)
268 (if buffer-read-only
269 (cua-copy-to-global-mark)
270 (setq cua--last-killed-rectangle nil)
271 (if cua--rectangle
272 (cua--cut-rectangle-to-global-mark nil)
273 (let ((start (mark)) (end (point)))
274 (or (<= start end)
275 (setq start (prog1 end (setq end start))))
276 (cua-cut-region-to-global-mark start end)))))
277
278 (defun cua-cut-next-to-global-mark (n)
279 "Move the following N characters in buffer to global mark buffer/position."
280 (interactive "p")
281 (setq cua--last-killed-rectangle nil)
282 (or (eobp)
283 (let ((p (point)))
284 (goto-char (+ p n))
285 (cua-cut-region-to-global-mark p (point)))))
286
287 (defun cua-delete-char-at-global-mark (arg)
288 "Delete character following the global mark position."
289 (interactive "p")
290 (cua--delete-at-global-mark arg "Deleted"))
291
292 (defun cua-delete-backward-char-at-global-mark (arg)
293 "Delete character before the global mark position."
294 (interactive "p")
295 (cua--delete-at-global-mark (- arg) "Deleted backward"))
296
297 (defun cua-insert-char-at-global-mark ()
298 "Insert the character you type at the global mark position."
299 (interactive)
300 (cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted"))
301
302 (defun cua-insert-newline-at-global-mark ()
303 "Insert a newline at the global mark position."
304 (interactive)
305 (cua--insert-at-global-mark "\n"))
306
307 (defun cua-indent-to-global-mark-column ()
308 "Indent current line or rectangle to global mark column."
309 (interactive "*")
310 (if (cua--global-mark-active)
311 (let (col)
312 (save-excursion
313 (set-buffer (marker-buffer cua--global-mark-marker))
314 (goto-char (marker-position cua--global-mark-marker))
315 (setq col (current-column)))
316 (if cua--rectangle
317 (cua--indent-rectangle nil col t)
318 (indent-to col))
319 (if (eq (current-buffer) (marker-buffer cua--global-mark-marker))
320 (save-excursion
321 (goto-char (marker-position cua--global-mark-marker))
322 (move-to-column col)
323 (move-marker cua--global-mark-marker (point))
324 (move-overlay cua--global-mark-overlay (point) (1+ (point))))))))
325
326
327 (defun cua-cancel-global-mark ()
328 "Cancel the global mark."
329 (interactive)
330 (if mark-active
331 (cua-cancel)
332 (if (cua--global-mark-active)
333 (cua--deactivate-global-mark t)))
334 (cua--fallback))
335
336 ;;; Post-command hook for global mark.
337
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
342 (sit-for 0)
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))))
352 (select-window w)
353 (goto-char p)))))
354
355 ;;; Initialization
356
357 (defun cua--init-global-mark ()
358 (unless (face-background 'cua-global-mark-face)
359 (copy-face 'region 'cua-global-mark-face)
360 (set-face-foreground 'cua-global-mark-face "black")
361 (set-face-background 'cua-global-mark-face "cyan"))
362
363 (define-key cua--global-mark-keymap [remap copy-region-as-kill] 'cua-copy-to-global-mark)
364 (define-key cua--global-mark-keymap [remap kill-ring-save] 'cua-copy-to-global-mark)
365 (define-key cua--global-mark-keymap [remap kill-region] 'cua-cut-to-global-mark)
366 (define-key cua--global-mark-keymap [remap yank] 'cua-copy-next-to-global-mark)
367
368 (define-key cua--global-mark-keymap [remap keyboard-escape-quit] 'cua-cancel-global-mark)
369 (define-key cua--global-mark-keymap [remap keyboard-quit] 'cua-cancel-global-mark)
370
371 (define-key cua--global-mark-keymap [(control ?d)] 'cua-cut-next-to-global-mark)
372 (define-key cua--global-mark-keymap [remap delete-backward-char] 'cua-delete-backward-char-at-global-mark)
373 (define-key cua--global-mark-keymap [remap backward-delete-char] 'cua-delete-backward-char-at-global-mark)
374 (define-key cua--global-mark-keymap [remap backward-delete-char-untabify] 'cua-delete-backward-char-at-global-mark)
375 (define-key cua--global-mark-keymap [remap self-insert-command] 'cua-insert-char-at-global-mark)
376 (define-key cua--global-mark-keymap [remap self-insert-iso] 'cua-insert-char-at-global-mark)
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)
380
381 (define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column)
382
383 (setq cua--global-mark-initialized t))
384
385 ;;; cua-gmrk.el ends here