| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
(provide 'cua-gmrk) |
|---|
| 31 |
|
|---|
| 32 |
(eval-when-compile |
|---|
| 33 |
(require 'cua-base) |
|---|
| 34 |
(require 'cua-rect) |
|---|
| 35 |
) |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
(defvar cua--global-mark-active nil) |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
(defvar cua--global-mark-marker nil) |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
(defvar cua--global-mark-overlay nil) |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
(defvar cua--global-mark-initialized nil) |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
(defvar cua--orig-blink-cursor-interval nil) |
|---|
| 53 |
|
|---|
| 54 |
(defun cua--deactivate-global-mark (&optional msg) |
|---|
| 55 |
(when cua--global-mark-overlay |
|---|
| 56 |
(delete-overlay cua--global-mark-overlay) |
|---|
| 57 |
(setq cua--global-mark-overlay nil)) |
|---|
| 58 |
(if (markerp cua--global-mark-marker) |
|---|
| 59 |
(move-marker cua--global-mark-marker nil)) |
|---|
| 60 |
(if cua--orig-blink-cursor-interval |
|---|
| 61 |
(setq blink-cursor-interval cua--orig-blink-cursor-interval |
|---|
| 62 |
cua--orig-blink-cursor-interval nil)) |
|---|
| 63 |
(setq cua--global-mark-active nil) |
|---|
| 64 |
(if msg |
|---|
| 65 |
(message "Global Mark Cleared"))) |
|---|
| 66 |
|
|---|
| 67 |
(defun cua--activate-global-mark (&optional msg) |
|---|
| 68 |
(if (not (markerp cua--global-mark-marker)) |
|---|
| 69 |
(setq cua--global-mark-marker (make-marker))) |
|---|
| 70 |
(when (eobp) |
|---|
| 71 |
(insert " ") |
|---|
| 72 |
(backward-char 1)) |
|---|
| 73 |
(move-marker cua--global-mark-marker (point)) |
|---|
| 74 |
(if (overlayp cua--global-mark-overlay) |
|---|
| 75 |
(move-overlay cua--global-mark-overlay (point) (1+ (point))) |
|---|
| 76 |
(setq cua--global-mark-overlay |
|---|
| 77 |
(make-overlay (point) (1+ (point)))) |
|---|
| 78 |
(overlay-put cua--global-mark-overlay 'face 'cua-global-mark)) |
|---|
| 79 |
(if (and cua-global-mark-blink-cursor-interval |
|---|
| 80 |
(not cua--orig-blink-cursor-interval)) |
|---|
| 81 |
(setq cua--orig-blink-cursor-interval blink-cursor-interval |
|---|
| 82 |
blink-cursor-interval cua-global-mark-blink-cursor-interval)) |
|---|
| 83 |
(setq cua--global-mark-active t) |
|---|
| 84 |
(if msg |
|---|
| 85 |
(message "Global Mark Set"))) |
|---|
| 86 |
|
|---|
| 87 |
(defun cua--global-mark-active () |
|---|
| 88 |
(if cua--global-mark-active |
|---|
| 89 |
(or (and (markerp cua--global-mark-marker) |
|---|
| 90 |
(marker-buffer cua--global-mark-marker)) |
|---|
| 91 |
(and (cua--deactivate-global-mark nil) |
|---|
| 92 |
nil)))) |
|---|
| 93 |
|
|---|
| 94 |
(defun cua-toggle-global-mark (stay) |
|---|
| 95 |
"Set or cancel the global marker. |
|---|
| 96 |
When the global marker is set, CUA cut and copy commands will automatically |
|---|
| 97 |
insert the deleted or copied text before the global marker, even when the |
|---|
| 98 |
global marker is in another buffer. |
|---|
| 99 |
If the global marker isn't set, set the global marker at point in the current |
|---|
| 100 |
buffer. Otherwise jump to the global marker position and cancel it. |
|---|
| 101 |
With prefix argument, don't jump to global mark when cancelling it." |
|---|
| 102 |
(interactive "P") |
|---|
| 103 |
(unless cua--global-mark-initialized |
|---|
| 104 |
(cua--init-global-mark)) |
|---|
| 105 |
(if (not (cua--global-mark-active)) |
|---|
| 106 |
(if (not buffer-read-only) |
|---|
| 107 |
(cua--activate-global-mark t) |
|---|
| 108 |
(ding) |
|---|
| 109 |
(message "Cannot set global mark in read-only buffer")) |
|---|
| 110 |
(when (not stay) |
|---|
| 111 |
(pop-to-buffer (marker-buffer cua--global-mark-marker)) |
|---|
| 112 |
(goto-char cua--global-mark-marker)) |
|---|
| 113 |
(cua--deactivate-global-mark t))) |
|---|
| 114 |
|
|---|
| 115 |
(defun cua--insert-at-global-mark (str &optional msg) |
|---|
| 116 |
|
|---|
| 117 |
(save-excursion |
|---|
| 118 |
(set-buffer (marker-buffer cua--global-mark-marker)) |
|---|
| 119 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 120 |
(insert-for-yank str) |
|---|
| 121 |
(cua--activate-global-mark)) |
|---|
| 122 |
(if msg |
|---|
| 123 |
(message "%s %d to global mark in %s:%d" msg |
|---|
| 124 |
(length str) |
|---|
| 125 |
(buffer-name (marker-buffer cua--global-mark-marker)) |
|---|
| 126 |
(marker-position cua--global-mark-marker)))) |
|---|
| 127 |
|
|---|
| 128 |
(defun cua--delete-at-global-mark (arg &optional msg) |
|---|
| 129 |
|
|---|
| 130 |
(save-excursion |
|---|
| 131 |
(set-buffer (marker-buffer cua--global-mark-marker)) |
|---|
| 132 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 133 |
(delete-char arg)) |
|---|
| 134 |
(if msg |
|---|
| 135 |
(message "%s %d chars at global mark in %s:%d" msg arg |
|---|
| 136 |
(buffer-name (marker-buffer cua--global-mark-marker)) |
|---|
| 137 |
(marker-position cua--global-mark-marker)))) |
|---|
| 138 |
|
|---|
| 139 |
(defun cua-copy-region-to-global-mark (start end) |
|---|
| 140 |
"Copy region to global mark buffer/position." |
|---|
| 141 |
(interactive "r") |
|---|
| 142 |
(if (cua--global-mark-active) |
|---|
| 143 |
(let ((src-buf (current-buffer))) |
|---|
| 144 |
(save-excursion |
|---|
| 145 |
(if (equal (marker-buffer cua--global-mark-marker) src-buf) |
|---|
| 146 |
(let ((text (filter-buffer-substring start end nil t))) |
|---|
| 147 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 148 |
(insert text)) |
|---|
| 149 |
(set-buffer (marker-buffer cua--global-mark-marker)) |
|---|
| 150 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 151 |
(insert-buffer-substring-as-yank src-buf start end)) |
|---|
| 152 |
(cua--activate-global-mark) |
|---|
| 153 |
(message "Copied %d to global mark in %s:%d" |
|---|
| 154 |
(abs (- end start)) |
|---|
| 155 |
(buffer-name (marker-buffer cua--global-mark-marker)) |
|---|
| 156 |
(marker-position cua--global-mark-marker)))) |
|---|
| 157 |
(cua--deactivate-global-mark) |
|---|
| 158 |
(message "No Global Mark"))) |
|---|
| 159 |
|
|---|
| 160 |
(defun cua-cut-region-to-global-mark (start end) |
|---|
| 161 |
"Move region to global buffer/position." |
|---|
| 162 |
(interactive "r") |
|---|
| 163 |
(if (cua--global-mark-active) |
|---|
| 164 |
(let ((src-buf (current-buffer))) |
|---|
| 165 |
(save-excursion |
|---|
| 166 |
(if (equal (marker-buffer cua--global-mark-marker) src-buf) |
|---|
| 167 |
(if (and (< start (marker-position cua--global-mark-marker)) |
|---|
| 168 |
(< (marker-position cua--global-mark-marker) end)) |
|---|
| 169 |
(message "Can't move region into itself") |
|---|
| 170 |
(let ((text (filter-buffer-substring start end nil t)) |
|---|
| 171 |
(p1 (copy-marker start)) |
|---|
| 172 |
(p2 (copy-marker end))) |
|---|
| 173 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 174 |
(insert text) |
|---|
| 175 |
(cua--activate-global-mark) |
|---|
| 176 |
(delete-region (marker-position p1) (marker-position p2)) |
|---|
| 177 |
(move-marker p1 nil) |
|---|
| 178 |
(move-marker p2 nil))) |
|---|
| 179 |
(set-buffer (marker-buffer cua--global-mark-marker)) |
|---|
| 180 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 181 |
(insert-buffer-substring src-buf start end) |
|---|
| 182 |
(message "Moved %d to global mark in %s:%d" |
|---|
| 183 |
(abs (- end start)) |
|---|
| 184 |
(buffer-name (marker-buffer cua--global-mark-marker)) |
|---|
| 185 |
(marker-position cua--global-mark-marker)) |
|---|
| 186 |
(cua--activate-global-mark) |
|---|
| 187 |
(set-buffer src-buf) |
|---|
| 188 |
(delete-region start end)))) |
|---|
| 189 |
(cua--deactivate-global-mark) |
|---|
| 190 |
(message "No Global Mark"))) |
|---|
| 191 |
|
|---|
| 192 |
(defun cua--copy-rectangle-to-global-mark (as-text) |
|---|
| 193 |
|
|---|
| 194 |
(if (cua--global-mark-active) |
|---|
| 195 |
(let ((src-buf (current-buffer)) |
|---|
| 196 |
(text (cua--extract-rectangle))) |
|---|
| 197 |
(save-excursion |
|---|
| 198 |
(set-buffer (marker-buffer cua--global-mark-marker)) |
|---|
| 199 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 200 |
(if as-text |
|---|
| 201 |
(while text |
|---|
| 202 |
(insert-for-yank (car text)) |
|---|
| 203 |
(if (setq text (cdr text)) |
|---|
| 204 |
(insert "\n"))) |
|---|
| 205 |
(cua--insert-rectangle text 'auto)) |
|---|
| 206 |
(cua--activate-global-mark) |
|---|
| 207 |
(message "Copied rectangle to global mark in %s:%d" |
|---|
| 208 |
(buffer-name (marker-buffer cua--global-mark-marker)) |
|---|
| 209 |
(marker-position cua--global-mark-marker)))) |
|---|
| 210 |
(cua--deactivate-global-mark) |
|---|
| 211 |
(message "No Global Mark"))) |
|---|
| 212 |
|
|---|
| 213 |
(defun cua--cut-rectangle-to-global-mark (as-text) |
|---|
| 214 |
|
|---|
| 215 |
(if (cua--global-mark-active) |
|---|
| 216 |
(let ((src-buf (current-buffer))) |
|---|
| 217 |
(save-excursion |
|---|
| 218 |
(if (equal (marker-buffer cua--global-mark-marker) src-buf) |
|---|
| 219 |
(let ((olist (overlays-at (marker-position cua--global-mark-marker))) |
|---|
| 220 |
in-rect) |
|---|
| 221 |
(while olist |
|---|
| 222 |
(if (eq (overlay-get (car olist) 'face) 'cua-rectangle) |
|---|
| 223 |
(setq in-rect t olist nil) |
|---|
| 224 |
(setq olist (cdr olist)))) |
|---|
| 225 |
(if in-rect |
|---|
| 226 |
(message "Can't move rectangle into itself") |
|---|
| 227 |
(let ((text (cua--extract-rectangle))) |
|---|
| 228 |
(cua--delete-rectangle) |
|---|
| 229 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 230 |
(if as-text |
|---|
| 231 |
(while text |
|---|
| 232 |
(insert-for-yank (car text)) |
|---|
| 233 |
(if (setq text (cdr text)) |
|---|
| 234 |
(insert "\n"))) |
|---|
| 235 |
(cua--insert-rectangle text 'auto)) |
|---|
| 236 |
(cua--activate-global-mark)))) |
|---|
| 237 |
(let ((text (cua--extract-rectangle))) |
|---|
| 238 |
(cua--delete-rectangle) |
|---|
| 239 |
(set-buffer (marker-buffer cua--global-mark-marker)) |
|---|
| 240 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 241 |
(cua--insert-rectangle text 'auto)) |
|---|
| 242 |
(message "Moved rectangle to global mark in %s:%d" |
|---|
| 243 |
(buffer-name (marker-buffer cua--global-mark-marker)) |
|---|
| 244 |
(marker-position cua--global-mark-marker)) |
|---|
| 245 |
(cua--activate-global-mark)))) |
|---|
| 246 |
(cua--deactivate-global-mark) |
|---|
| 247 |
(message "No Global Mark"))) |
|---|
| 248 |
|
|---|
| 249 |
(defun cua-copy-to-global-mark () |
|---|
| 250 |
"Copy active region/rectangle to global mark buffer/position." |
|---|
| 251 |
(interactive) |
|---|
| 252 |
(setq cua--last-killed-rectangle nil) |
|---|
| 253 |
(if cua--rectangle |
|---|
| 254 |
(cua--copy-rectangle-to-global-mark nil) |
|---|
| 255 |
(let ((start (mark)) (end (point))) |
|---|
| 256 |
(or (<= start end) |
|---|
| 257 |
(setq start (prog1 end (setq end start)))) |
|---|
| 258 |
(cua-copy-region-to-global-mark start end)))) |
|---|
| 259 |
|
|---|
| 260 |
(defun cua-copy-next-to-global-mark (n) |
|---|
| 261 |
"Copy the following N characters in buffer to global mark buffer/position." |
|---|
| 262 |
(interactive "p") |
|---|
| 263 |
(setq cua--last-killed-rectangle nil) |
|---|
| 264 |
(or (eobp) |
|---|
| 265 |
(let ((p (point))) |
|---|
| 266 |
(goto-char (+ p n)) |
|---|
| 267 |
(cua-copy-region-to-global-mark p (point))))) |
|---|
| 268 |
|
|---|
| 269 |
(defun cua-cut-to-global-mark () |
|---|
| 270 |
"Move active region/rectangle to global mark buffer/position." |
|---|
| 271 |
(interactive) |
|---|
| 272 |
(if buffer-read-only |
|---|
| 273 |
(cua-copy-to-global-mark) |
|---|
| 274 |
(setq cua--last-killed-rectangle nil) |
|---|
| 275 |
(if cua--rectangle |
|---|
| 276 |
(cua--cut-rectangle-to-global-mark nil) |
|---|
| 277 |
(let ((start (mark)) (end (point))) |
|---|
| 278 |
(or (<= start end) |
|---|
| 279 |
(setq start (prog1 end (setq end start)))) |
|---|
| 280 |
(cua-cut-region-to-global-mark start end))))) |
|---|
| 281 |
|
|---|
| 282 |
(defun cua-cut-next-to-global-mark (n) |
|---|
| 283 |
"Move the following N characters in buffer to global mark buffer/position." |
|---|
| 284 |
(interactive "p") |
|---|
| 285 |
(setq cua--last-killed-rectangle nil) |
|---|
| 286 |
(or (eobp) |
|---|
| 287 |
(let ((p (point))) |
|---|
| 288 |
(goto-char (+ p n)) |
|---|
| 289 |
(cua-cut-region-to-global-mark p (point))))) |
|---|
| 290 |
|
|---|
| 291 |
(defun cua-delete-char-at-global-mark (arg) |
|---|
| 292 |
"Delete character following the global mark position." |
|---|
| 293 |
(interactive "p") |
|---|
| 294 |
(cua--delete-at-global-mark arg "Deleted")) |
|---|
| 295 |
|
|---|
| 296 |
(defun cua-delete-backward-char-at-global-mark (arg) |
|---|
| 297 |
"Delete character before the global mark position." |
|---|
| 298 |
(interactive "p") |
|---|
| 299 |
(cua--delete-at-global-mark (- arg) "Deleted backward")) |
|---|
| 300 |
|
|---|
| 301 |
(defun cua-insert-char-at-global-mark () |
|---|
| 302 |
"Insert the character you type at the global mark position." |
|---|
| 303 |
(interactive) |
|---|
| 304 |
(cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted")) |
|---|
| 305 |
|
|---|
| 306 |
(defun cua-insert-newline-at-global-mark () |
|---|
| 307 |
"Insert a newline at the global mark position." |
|---|
| 308 |
(interactive) |
|---|
| 309 |
(cua--insert-at-global-mark "\n")) |
|---|
| 310 |
|
|---|
| 311 |
(defun cua-indent-to-global-mark-column () |
|---|
| 312 |
"Indent current line or rectangle to global mark column." |
|---|
| 313 |
(interactive "*") |
|---|
| 314 |
(if (cua--global-mark-active) |
|---|
| 315 |
(let (col) |
|---|
| 316 |
(save-excursion |
|---|
| 317 |
(set-buffer (marker-buffer cua--global-mark-marker)) |
|---|
| 318 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 319 |
(setq col (current-column))) |
|---|
| 320 |
(if cua--rectangle |
|---|
| 321 |
(cua--indent-rectangle nil col t) |
|---|
| 322 |
(indent-to col)) |
|---|
| 323 |
(if (eq (current-buffer) (marker-buffer cua--global-mark-marker)) |
|---|
| 324 |
(save-excursion |
|---|
| 325 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 326 |
(move-to-column col) |
|---|
| 327 |
(move-marker cua--global-mark-marker (point)) |
|---|
| 328 |
(move-overlay cua--global-mark-overlay (point) (1+ (point)))))))) |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
(defun cua-cancel-global-mark () |
|---|
| 332 |
"Cancel the global mark." |
|---|
| 333 |
(interactive) |
|---|
| 334 |
(if mark-active |
|---|
| 335 |
(cua-cancel) |
|---|
| 336 |
(if (cua--global-mark-active) |
|---|
| 337 |
(cua--deactivate-global-mark t))) |
|---|
| 338 |
(cua--fallback)) |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
(defun cua--global-mark-post-command () |
|---|
| 343 |
(when (and (cua--global-mark-active) |
|---|
| 344 |
cua-global-mark-keep-visible) |
|---|
| 345 |
|
|---|
| 346 |
(sit-for 0) |
|---|
| 347 |
(if (or (not (eq (current-buffer) (marker-buffer cua--global-mark-marker))) |
|---|
| 348 |
(not (pos-visible-in-window-p (marker-position cua--global-mark-marker)))) |
|---|
| 349 |
(let ((w (selected-window)) (p (point)) h) |
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
(switch-to-buffer-other-window (marker-buffer cua--global-mark-marker) t) |
|---|
| 353 |
(goto-char (marker-position cua--global-mark-marker)) |
|---|
| 354 |
(if (not (pos-visible-in-window-p (marker-position cua--global-mark-marker))) |
|---|
| 355 |
(recenter (if (> (setq h (- (window-height) 4)) 1) h '(4)))) |
|---|
| 356 |
(select-window w) |
|---|
| 357 |
(goto-char p))))) |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
(defun cua--init-global-mark () |
|---|
| 362 |
(define-key cua--global-mark-keymap [remap copy-region-as-kill] 'cua-copy-to-global-mark) |
|---|
| 363 |
(define-key cua--global-mark-keymap [remap kill-ring-save] 'cua-copy-to-global-mark) |
|---|
| 364 |
(define-key cua--global-mark-keymap [remap kill-region] 'cua-cut-to-global-mark) |
|---|
| 365 |
(define-key cua--global-mark-keymap [remap yank] 'cua-copy-next-to-global-mark) |
|---|
| 366 |
|
|---|
| 367 |
(define-key cua--global-mark-keymap [remap keyboard-escape-quit] 'cua-cancel-global-mark) |
|---|
| 368 |
(define-key cua--global-mark-keymap [remap keyboard-quit] 'cua-cancel-global-mark) |
|---|
| 369 |
|
|---|
| 370 |
(define-key cua--global-mark-keymap [(control ?d)] 'cua-cut-next-to-global-mark) |
|---|
| 371 |
(define-key cua--global-mark-keymap [remap delete-backward-char] 'cua-delete-backward-char-at-global-mark) |
|---|
| 372 |
(define-key cua--global-mark-keymap [remap backward-delete-char] 'cua-delete-backward-char-at-global-mark) |
|---|
| 373 |
(define-key cua--global-mark-keymap [remap backward-delete-char-untabify] 'cua-delete-backward-char-at-global-mark) |
|---|
| 374 |
(define-key cua--global-mark-keymap [remap self-insert-command] 'cua-insert-char-at-global-mark) |
|---|
| 375 |
(define-key cua--global-mark-keymap [remap self-insert-iso] 'cua-insert-char-at-global-mark) |
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
(define-key cua--global-mark-keymap [t] |
|---|
| 379 |
'(menu-item "sic" cua-insert-char-at-global-mark :filter cua--self-insert-char-p)) |
|---|
| 380 |
|
|---|
| 381 |
(define-key cua--global-mark-keymap [remap newline] 'cua-insert-newline-at-global-mark) |
|---|
| 382 |
(define-key cua--global-mark-keymap [remap newline-and-indent] 'cua-insert-newline-at-global-mark) |
|---|
| 383 |
(define-key cua--global-mark-keymap "\r" 'cua-insert-newline-at-global-mark) |
|---|
| 384 |
|
|---|
| 385 |
(define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column) |
|---|
| 386 |
|
|---|
| 387 |
(setq cua--global-mark-initialized t)) |
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|