| 1 | ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*- |
| 2 | |
| 3 | ;; Copyright (C) 1985, 1993-1994, 2001-2013 Free Software Foundation, |
| 4 | ;; Inc. |
| 5 | |
| 6 | ;; Maintainer: FSF |
| 7 | ;; Keywords: internal |
| 8 | ;; Package: emacs |
| 9 | |
| 10 | ;; This file is part of GNU Emacs. |
| 11 | |
| 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 13 | ;; it under the terms of the GNU General Public License as published by |
| 14 | ;; the Free Software Foundation, either version 3 of the License, or |
| 15 | ;; (at your option) any later version. |
| 16 | |
| 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ;; GNU General Public License for more details. |
| 21 | |
| 22 | ;; You should have received a copy of the GNU General Public License |
| 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | |
| 27 | ;; This package of functions emulates and somewhat extends the venerable |
| 28 | ;; TECO's `register' feature, which permits you to save various useful |
| 29 | ;; pieces of buffer state to named variables. The entry points are |
| 30 | ;; documented in the Emacs user's manual. |
| 31 | |
| 32 | (eval-when-compile (require 'cl-lib)) |
| 33 | |
| 34 | ;;; Code: |
| 35 | |
| 36 | (cl-defstruct |
| 37 | (registerv (:constructor nil) |
| 38 | (:constructor registerv--make (&optional data print-func |
| 39 | jump-func insert-func)) |
| 40 | (:copier nil) |
| 41 | (:type vector) |
| 42 | :named) |
| 43 | (data nil :read-only t) |
| 44 | (print-func nil :read-only t) |
| 45 | (jump-func nil :read-only t) |
| 46 | (insert-func nil :read-only t)) |
| 47 | |
| 48 | (cl-defun registerv-make (data &key print-func jump-func insert-func) |
| 49 | "Create a register value object. |
| 50 | |
| 51 | DATA can be any value. |
| 52 | PRINT-FUNC if provided controls how `list-registers' and |
| 53 | `view-register' print the register. It should be a function |
| 54 | receiving one argument DATA and print text that completes |
| 55 | this sentence: |
| 56 | Register X contains [TEXT PRINTED BY PRINT-FUNC] |
| 57 | JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register. |
| 58 | INSERT-FUNC if provided, controls how `insert-register' insert the register. |
| 59 | They both receive DATA as argument." |
| 60 | (registerv--make data print-func jump-func insert-func)) |
| 61 | |
| 62 | (defvar register-alist nil |
| 63 | "Alist of elements (NAME . CONTENTS), one for each Emacs register. |
| 64 | NAME is a character (a number). CONTENTS is a string, number, marker, list |
| 65 | or a struct returned by `registerv-make'. |
| 66 | A list of strings represents a rectangle. |
| 67 | A list of the form (file . FILE-NAME) represents the file named FILE-NAME. |
| 68 | A list of the form (file-query FILE-NAME POSITION) represents |
| 69 | position POSITION in the file named FILE-NAME, but query before |
| 70 | visiting it. |
| 71 | A list of the form (WINDOW-CONFIGURATION POSITION) |
| 72 | represents a saved window configuration plus a saved value of point. |
| 73 | A list of the form (FRAME-CONFIGURATION POSITION) |
| 74 | represents a saved frame configuration plus a saved value of point.") |
| 75 | |
| 76 | (defgroup register nil |
| 77 | "Register commands." |
| 78 | :group 'convenience |
| 79 | :version "24.3") |
| 80 | |
| 81 | (defcustom register-separator nil |
| 82 | "Register containing the text to put between collected texts, or nil if none. |
| 83 | |
| 84 | When collecting text with |
| 85 | `append-to-register' (resp. `prepend-to-register') contents of |
| 86 | this register is added to the beginning (resp. end) of the marked |
| 87 | text." |
| 88 | :group 'register |
| 89 | :type '(choice (const :tag "None" nil) |
| 90 | (character :tag "Use register" :value ?+))) |
| 91 | |
| 92 | (defcustom register-preview-delay 1 |
| 93 | "If non-nil delay in seconds to pop up the preview window." |
| 94 | :type '(choice number (const :tag "Indefinitely" nil)) |
| 95 | :group 'register) |
| 96 | |
| 97 | (defun get-register (register) |
| 98 | "Return contents of Emacs register named REGISTER, or nil if none." |
| 99 | (cdr (assq register register-alist))) |
| 100 | |
| 101 | (defun set-register (register value) |
| 102 | "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE. |
| 103 | See the documentation of the variable `register-alist' for possible VALUEs." |
| 104 | (let ((aelt (assq register register-alist))) |
| 105 | (if aelt |
| 106 | (setcdr aelt value) |
| 107 | (push (cons register value) register-alist)) |
| 108 | value)) |
| 109 | |
| 110 | (defun register-describe-oneline (c) |
| 111 | "One-line description of register C." |
| 112 | (let ((d (replace-regexp-in-string |
| 113 | "\n[ \t]*" " " |
| 114 | (with-output-to-string (describe-register-1 c))))) |
| 115 | (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d) |
| 116 | (substring d (match-end 0)) |
| 117 | d))) |
| 118 | |
| 119 | (defvar register-preview-functions nil) |
| 120 | |
| 121 | (defun register-preview (buffer &optional show-empty) |
| 122 | "Pop up a window to show register preview in BUFFER. |
| 123 | If SHOW-EMPTY is non-nil show the window even if no registers." |
| 124 | (when (or show-empty (consp register-alist)) |
| 125 | (let ((split-height-threshold 0)) |
| 126 | (with-temp-buffer-window |
| 127 | buffer |
| 128 | (cons 'display-buffer-below-selected |
| 129 | '((window-height . fit-window-to-buffer))) |
| 130 | nil |
| 131 | (with-current-buffer standard-output |
| 132 | (setq cursor-in-non-selected-windows nil) |
| 133 | (mapc |
| 134 | (lambda (r) |
| 135 | (insert (or (run-hook-with-args-until-success |
| 136 | 'register-preview-functions r) |
| 137 | (format "%s %s\n" |
| 138 | (concat (single-key-description (car r)) ":") |
| 139 | (register-describe-oneline (car r)))))) |
| 140 | register-alist)))))) |
| 141 | |
| 142 | (defun register-read-with-preview (prompt) |
| 143 | "Read an event with register preview using PROMPT. |
| 144 | Pop up a register preview window if the input is a help char but |
| 145 | is not a register. Alternatively if `register-preview-delay' is a |
| 146 | number the preview window is popped up after some delay." |
| 147 | (let* ((buffer "*Register Preview*") |
| 148 | (timer (when (numberp register-preview-delay) |
| 149 | (run-with-timer register-preview-delay nil |
| 150 | (lambda () |
| 151 | (unless (get-buffer-window buffer) |
| 152 | (register-preview buffer)))))) |
| 153 | (help-chars (cl-loop for c in (cons help-char help-event-list) |
| 154 | when (not (get-register c)) |
| 155 | collect c))) |
| 156 | (unwind-protect |
| 157 | (progn |
| 158 | (while (memq (read-event (propertize prompt 'face 'minibuffer-prompt)) |
| 159 | help-chars) |
| 160 | (unless (get-buffer-window buffer) |
| 161 | (register-preview buffer 'show-empty))) |
| 162 | last-input-event) |
| 163 | (and (timerp timer) (cancel-timer timer)) |
| 164 | (let ((w (get-buffer-window buffer))) |
| 165 | (and (window-live-p w) (delete-window w))) |
| 166 | (and (get-buffer buffer) (kill-buffer buffer))))) |
| 167 | |
| 168 | (defun point-to-register (register &optional arg) |
| 169 | "Store current location of point in register REGISTER. |
| 170 | With prefix argument, store current frame configuration. |
| 171 | Use \\[jump-to-register] to go to that location or restore that configuration. |
| 172 | Argument is a character, naming the register." |
| 173 | (interactive (list (register-read-with-preview "Point to register: ") |
| 174 | current-prefix-arg)) |
| 175 | ;; Turn the marker into a file-ref if the buffer is killed. |
| 176 | (add-hook 'kill-buffer-hook 'register-swap-out nil t) |
| 177 | (set-register register |
| 178 | (if arg (list (current-frame-configuration) (point-marker)) |
| 179 | (point-marker)))) |
| 180 | |
| 181 | (defun window-configuration-to-register (register &optional _arg) |
| 182 | "Store the window configuration of the selected frame in register REGISTER. |
| 183 | Use \\[jump-to-register] to restore the configuration. |
| 184 | Argument is a character, naming the register." |
| 185 | (interactive (list (register-read-with-preview |
| 186 | "Window configuration to register: ") |
| 187 | current-prefix-arg)) |
| 188 | ;; current-window-configuration does not include the value |
| 189 | ;; of point in the current buffer, so record that separately. |
| 190 | (set-register register (list (current-window-configuration) (point-marker)))) |
| 191 | |
| 192 | (defun frame-configuration-to-register (register &optional _arg) |
| 193 | "Store the window configuration of all frames in register REGISTER. |
| 194 | Use \\[jump-to-register] to restore the configuration. |
| 195 | Argument is a character, naming the register." |
| 196 | (interactive (list (register-read-with-preview |
| 197 | "Frame configuration to register: ") |
| 198 | current-prefix-arg)) |
| 199 | ;; current-frame-configuration does not include the value |
| 200 | ;; of point in the current buffer, so record that separately. |
| 201 | (set-register register (list (current-frame-configuration) (point-marker)))) |
| 202 | |
| 203 | (defalias 'register-to-point 'jump-to-register) |
| 204 | (defun jump-to-register (register &optional delete) |
| 205 | "Move point to location stored in a register. |
| 206 | If the register contains a file name, find that file. |
| 207 | \(To put a file name in a register, you must use `set-register'.) |
| 208 | If the register contains a window configuration (one frame) or a frameset |
| 209 | \(all frames), restore that frame or all frames accordingly. |
| 210 | First argument is a character, naming the register. |
| 211 | Optional second arg non-nil (interactively, prefix argument) says to |
| 212 | delete any existing frames that the frameset doesn't mention. |
| 213 | \(Otherwise, these frames are iconified.)" |
| 214 | (interactive (list (register-read-with-preview "Jump to register: ") |
| 215 | current-prefix-arg)) |
| 216 | (let ((val (get-register register))) |
| 217 | (cond |
| 218 | ((registerv-p val) |
| 219 | (cl-assert (registerv-jump-func val) nil |
| 220 | "Don't know how to jump to register %s" |
| 221 | (single-key-description register)) |
| 222 | (funcall (registerv-jump-func val) (registerv-data val))) |
| 223 | ((and (consp val) (frame-configuration-p (car val))) |
| 224 | (set-frame-configuration (car val) (not delete)) |
| 225 | (goto-char (cadr val))) |
| 226 | ((and (consp val) (window-configuration-p (car val))) |
| 227 | (set-window-configuration (car val)) |
| 228 | (goto-char (cadr val))) |
| 229 | ((markerp val) |
| 230 | (or (marker-buffer val) |
| 231 | (error "That register's buffer no longer exists")) |
| 232 | (switch-to-buffer (marker-buffer val)) |
| 233 | (goto-char val)) |
| 234 | ((and (consp val) (eq (car val) 'file)) |
| 235 | (find-file (cdr val))) |
| 236 | ((and (consp val) (eq (car val) 'file-query)) |
| 237 | (or (find-buffer-visiting (nth 1 val)) |
| 238 | (y-or-n-p (format "Visit file %s again? " (nth 1 val))) |
| 239 | (error "Register access aborted")) |
| 240 | (find-file (nth 1 val)) |
| 241 | (goto-char (nth 2 val))) |
| 242 | (t |
| 243 | (error "Register doesn't contain a buffer position or configuration"))))) |
| 244 | |
| 245 | (defun register-swap-out () |
| 246 | "Turn markers into file-query references when a buffer is killed." |
| 247 | (and buffer-file-name |
| 248 | (dolist (elem register-alist) |
| 249 | (and (markerp (cdr elem)) |
| 250 | (eq (marker-buffer (cdr elem)) (current-buffer)) |
| 251 | (setcdr elem |
| 252 | (list 'file-query |
| 253 | buffer-file-name |
| 254 | (marker-position (cdr elem)))))))) |
| 255 | |
| 256 | (defun number-to-register (number register) |
| 257 | "Store a number in a register. |
| 258 | Two args, NUMBER and REGISTER (a character, naming the register). |
| 259 | If NUMBER is nil, a decimal number is read from the buffer starting |
| 260 | at point, and point moves to the end of that number. |
| 261 | Interactively, NUMBER is the prefix arg (none means nil)." |
| 262 | (interactive (list current-prefix-arg |
| 263 | (register-read-with-preview "Number to register: "))) |
| 264 | (set-register register |
| 265 | (if number |
| 266 | (prefix-numeric-value number) |
| 267 | (if (looking-at "\\s-*-?[0-9]+") |
| 268 | (progn |
| 269 | (goto-char (match-end 0)) |
| 270 | (string-to-number (match-string 0))) |
| 271 | 0)))) |
| 272 | |
| 273 | (defun increment-register (prefix register) |
| 274 | "Augment contents of REGISTER. |
| 275 | Interactively, PREFIX is in raw form. |
| 276 | |
| 277 | If REGISTER contains a number, add `prefix-numeric-value' of |
| 278 | PREFIX to it. |
| 279 | |
| 280 | If REGISTER is empty or if it contains text, call |
| 281 | `append-to-register' with `delete-flag' set to PREFIX." |
| 282 | (interactive "P\ncIncrement register: ") |
| 283 | (let ((register-val (get-register register))) |
| 284 | (cond |
| 285 | ((numberp register-val) |
| 286 | (let ((number (prefix-numeric-value prefix))) |
| 287 | (set-register register (+ number register-val)))) |
| 288 | ((or (not register-val) (stringp register-val)) |
| 289 | (append-to-register register (region-beginning) (region-end) prefix)) |
| 290 | (t (error "Register does not contain a number or text"))))) |
| 291 | |
| 292 | (defun view-register (register) |
| 293 | "Display what is contained in register named REGISTER. |
| 294 | The Lisp value REGISTER is a character." |
| 295 | (interactive (list (register-read-with-preview "View register: "))) |
| 296 | (let ((val (get-register register))) |
| 297 | (if (null val) |
| 298 | (message "Register %s is empty" (single-key-description register)) |
| 299 | (with-output-to-temp-buffer "*Output*" |
| 300 | (describe-register-1 register t))))) |
| 301 | |
| 302 | (defun list-registers () |
| 303 | "Display a list of nonempty registers saying briefly what they contain." |
| 304 | (interactive) |
| 305 | (let ((list (copy-sequence register-alist))) |
| 306 | (setq list (sort list (lambda (a b) (< (car a) (car b))))) |
| 307 | (with-output-to-temp-buffer "*Output*" |
| 308 | (dolist (elt list) |
| 309 | (when (get-register (car elt)) |
| 310 | (describe-register-1 (car elt)) |
| 311 | (terpri)))))) |
| 312 | |
| 313 | (defun describe-register-1 (register &optional verbose) |
| 314 | (princ "Register ") |
| 315 | (princ (single-key-description register)) |
| 316 | (princ " contains ") |
| 317 | (let ((val (get-register register))) |
| 318 | (cond |
| 319 | ((registerv-p val) |
| 320 | (if (registerv-print-func val) |
| 321 | (funcall (registerv-print-func val) (registerv-data val)) |
| 322 | (princ "[UNPRINTABLE CONTENTS]."))) |
| 323 | |
| 324 | ((numberp val) |
| 325 | (princ val)) |
| 326 | |
| 327 | ((markerp val) |
| 328 | (let ((buf (marker-buffer val))) |
| 329 | (if (null buf) |
| 330 | (princ "a marker in no buffer") |
| 331 | (princ "a buffer position:\n buffer ") |
| 332 | (princ (buffer-name buf)) |
| 333 | (princ ", position ") |
| 334 | (princ (marker-position val))))) |
| 335 | |
| 336 | ((and (consp val) (window-configuration-p (car val))) |
| 337 | (princ "a window configuration.")) |
| 338 | |
| 339 | ((and (consp val) (frame-configuration-p (car val))) |
| 340 | (princ "a frame configuration.")) |
| 341 | |
| 342 | ((and (consp val) (eq (car val) 'file)) |
| 343 | (princ "the file ") |
| 344 | (prin1 (cdr val)) |
| 345 | (princ ".")) |
| 346 | |
| 347 | ((and (consp val) (eq (car val) 'file-query)) |
| 348 | (princ "a file-query reference:\n file ") |
| 349 | (prin1 (car (cdr val))) |
| 350 | (princ ",\n position ") |
| 351 | (princ (car (cdr (cdr val)))) |
| 352 | (princ ".")) |
| 353 | |
| 354 | ((consp val) |
| 355 | (if verbose |
| 356 | (progn |
| 357 | (princ "the rectangle:\n") |
| 358 | (while val |
| 359 | (princ " ") |
| 360 | (princ (car val)) |
| 361 | (terpri) |
| 362 | (setq val (cdr val)))) |
| 363 | (princ "a rectangle starting with ") |
| 364 | (princ (car val)))) |
| 365 | |
| 366 | ((stringp val) |
| 367 | (if (eq yank-excluded-properties t) |
| 368 | (set-text-properties 0 (length val) nil val) |
| 369 | (remove-list-of-text-properties 0 (length val) |
| 370 | yank-excluded-properties val)) |
| 371 | (if verbose |
| 372 | (progn |
| 373 | (princ "the text:\n") |
| 374 | (princ val)) |
| 375 | (cond |
| 376 | ;; Extract first N characters starting with first non-whitespace. |
| 377 | ((string-match (format "[^ \t\n].\\{,%d\\}" |
| 378 | ;; Deduct 6 for the spaces inserted below. |
| 379 | (min 20 (max 0 (- (window-width) 6)))) |
| 380 | val) |
| 381 | (princ "text starting with\n ") |
| 382 | (princ (match-string 0 val))) |
| 383 | ((string-match "^[ \t\n]+$" val) |
| 384 | (princ "whitespace")) |
| 385 | (t |
| 386 | (princ "the empty string"))))) |
| 387 | (t |
| 388 | (princ "Garbage:\n") |
| 389 | (if verbose (prin1 val)))))) |
| 390 | |
| 391 | (defun insert-register (register &optional arg) |
| 392 | "Insert contents of register REGISTER. (REGISTER is a character.) |
| 393 | Normally puts point before and mark after the inserted text. |
| 394 | If optional second arg is non-nil, puts mark before and point after. |
| 395 | Interactively, second arg is non-nil if prefix arg is supplied." |
| 396 | (interactive (progn |
| 397 | (barf-if-buffer-read-only) |
| 398 | (register-read-with-preview "Insert register: ") |
| 399 | current-prefix-arg)) |
| 400 | (push-mark) |
| 401 | (let ((val (get-register register))) |
| 402 | (cond |
| 403 | ((registerv-p val) |
| 404 | (cl-assert (registerv-insert-func val) nil |
| 405 | "Don't know how to insert register %s" |
| 406 | (single-key-description register)) |
| 407 | (funcall (registerv-insert-func val) (registerv-data val))) |
| 408 | ((consp val) |
| 409 | (insert-rectangle val)) |
| 410 | ((stringp val) |
| 411 | (insert-for-yank val)) |
| 412 | ((numberp val) |
| 413 | (princ val (current-buffer))) |
| 414 | ((and (markerp val) (marker-position val)) |
| 415 | (princ (marker-position val) (current-buffer))) |
| 416 | (t |
| 417 | (error "Register does not contain text")))) |
| 418 | (if (not arg) (exchange-point-and-mark))) |
| 419 | |
| 420 | (defun copy-to-register (register start end &optional delete-flag) |
| 421 | "Copy region into register REGISTER. |
| 422 | With prefix arg, delete as well. |
| 423 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. |
| 424 | START and END are buffer positions indicating what to copy." |
| 425 | (interactive (list (register-read-with-preview "Copy to register: ") |
| 426 | (region-beginning) |
| 427 | (region-end) |
| 428 | current-prefix-arg)) |
| 429 | (set-register register (filter-buffer-substring start end)) |
| 430 | (setq deactivate-mark t) |
| 431 | (cond (delete-flag |
| 432 | (delete-region start end)) |
| 433 | ((called-interactively-p 'interactive) |
| 434 | (indicate-copied-region)))) |
| 435 | |
| 436 | (defun append-to-register (register start end &optional delete-flag) |
| 437 | "Append region to text in register REGISTER. |
| 438 | With prefix arg, delete as well. |
| 439 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. |
| 440 | START and END are buffer positions indicating what to append." |
| 441 | (interactive (list (register-read-with-preview "Append to register: ") |
| 442 | (region-beginning) |
| 443 | (region-end) |
| 444 | current-prefix-arg)) |
| 445 | (let ((reg (get-register register)) |
| 446 | (text (filter-buffer-substring start end)) |
| 447 | (separator (and register-separator (get-register register-separator)))) |
| 448 | (set-register |
| 449 | register (cond ((not reg) text) |
| 450 | ((stringp reg) (concat reg separator text)) |
| 451 | (t (error "Register does not contain text"))))) |
| 452 | (setq deactivate-mark t) |
| 453 | (cond (delete-flag |
| 454 | (delete-region start end)) |
| 455 | ((called-interactively-p 'interactive) |
| 456 | (indicate-copied-region)))) |
| 457 | |
| 458 | (defun prepend-to-register (register start end &optional delete-flag) |
| 459 | "Prepend region to text in register REGISTER. |
| 460 | With prefix arg, delete as well. |
| 461 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. |
| 462 | START and END are buffer positions indicating what to prepend." |
| 463 | (interactive (list (register-read-with-preview "Prepend to register: ") |
| 464 | (region-beginning) |
| 465 | (region-end) |
| 466 | current-prefix-arg)) |
| 467 | (let ((reg (get-register register)) |
| 468 | (text (filter-buffer-substring start end)) |
| 469 | (separator (and register-separator (get-register register-separator)))) |
| 470 | (set-register |
| 471 | register (cond ((not reg) text) |
| 472 | ((stringp reg) (concat text separator reg)) |
| 473 | (t (error "Register does not contain text"))))) |
| 474 | (setq deactivate-mark t) |
| 475 | (cond (delete-flag |
| 476 | (delete-region start end)) |
| 477 | ((called-interactively-p 'interactive) |
| 478 | (indicate-copied-region)))) |
| 479 | |
| 480 | (defun copy-rectangle-to-register (register start end &optional delete-flag) |
| 481 | "Copy rectangular region into register REGISTER. |
| 482 | With prefix arg, delete as well. |
| 483 | To insert this register in the buffer, use \\[insert-register]. |
| 484 | |
| 485 | Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG. |
| 486 | START and END are buffer positions giving two corners of rectangle." |
| 487 | (interactive (list (register-read-with-preview |
| 488 | "Copy rectangle to register: ") |
| 489 | (region-beginning) |
| 490 | (region-end) |
| 491 | current-prefix-arg)) |
| 492 | (let ((rectangle (if delete-flag |
| 493 | (delete-extract-rectangle start end) |
| 494 | (extract-rectangle start end)))) |
| 495 | (set-register register rectangle) |
| 496 | (when (and (null delete-flag) |
| 497 | (called-interactively-p 'interactive)) |
| 498 | (setq deactivate-mark t) |
| 499 | (indicate-copied-region (length (car rectangle)))))) |
| 500 | |
| 501 | (provide 'register) |
| 502 | ;;; register.el ends here |