1;;; register.el --- register commands for Emacs 2 3;; Copyright (C) 1985, 1993, 1994, 2001, 2002, 2003, 2004, 4;; 2005, 2006, 2007 Free Software Foundation, Inc. 5 6;; Maintainer: FSF 7;; Keywords: internal 8 9;; This file is part of GNU Emacs. 10 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 2, or (at your option) 14;; any later version. 15 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. 20 21;; You should have received a copy of the GNU General Public License 22;; along with GNU Emacs; see the file COPYING. If not, write to the 23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 24;; Boston, MA 02110-1301, USA. 25 26;;; Commentary: 27 28;; This package of functions emulates and somewhat extends the venerable 29;; TECO's `register' feature, which permits you to save various useful 30;; pieces of buffer state to named variables. The entry points are 31;; documented in the Emacs user's manual. 32 33;;; Code: 34 35(defvar register-alist nil 36 "Alist of elements (NAME . CONTENTS), one for each Emacs register. 37NAME is a character (a number). CONTENTS is a string, number, marker or list. 38A list of strings represents a rectangle. 39A list of the form (file . NAME) represents the file named NAME. 40A list of the form (file-query NAME POSITION) represents position POSITION 41 in the file named NAME, but query before visiting it. 42A list of the form (WINDOW-CONFIGURATION POSITION) 43 represents a saved window configuration plus a saved value of point. 44A list of the form (FRAME-CONFIGURATION POSITION) 45 represents a saved frame configuration plus a saved value of point.") 46 47(defun get-register (reg) 48 "Return contents of Emacs register named REG, or nil if none." 49 (cdr (assq reg register-alist))) 50 51(defun set-register (register value) 52 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE. 53See the documentation of the variable `register-alist' for possible VALUE." 54 (let ((aelt (assq register register-alist))) 55 (if aelt 56 (setcdr aelt value) 57 (push (cons register value) register-alist)) 58 value)) 59 60(defun point-to-register (register &optional arg) 61 "Store current location of point in register REGISTER. 62With prefix argument, store current frame configuration. 63Use \\[jump-to-register] to go to that location or restore that configuration. 64Argument is a character, naming the register." 65 (interactive "cPoint to register: \nP") 66 ;; Turn the marker into a file-ref if the buffer is killed. 67 (add-hook 'kill-buffer-hook 'register-swap-out nil t) 68 (set-register register 69 (if arg (list (current-frame-configuration) (point-marker)) 70 (point-marker)))) 71 72(defun window-configuration-to-register (register &optional arg) 73 "Store the window configuration of the selected frame in register REGISTER. 74Use \\[jump-to-register] to restore the configuration. 75Argument is a character, naming the register." 76 (interactive "cWindow configuration to register: \nP") 77 ;; current-window-configuration does not include the value 78 ;; of point in the current buffer, so record that separately. 79 (set-register register (list (current-window-configuration) (point-marker)))) 80 81(defun frame-configuration-to-register (register &optional arg) 82 "Store the window configuration of all frames in register REGISTER. 83Use \\[jump-to-register] to restore the configuration. 84Argument is a character, naming the register." 85 (interactive "cFrame configuration to register: \nP") 86 ;; current-frame-configuration does not include the value 87 ;; of point in the current buffer, so record that separately. 88 (set-register register (list (current-frame-configuration) (point-marker)))) 89 90(defalias 'register-to-point 'jump-to-register) 91(defun jump-to-register (register &optional delete) 92 "Move point to location stored in a register. 93If the register contains a file name, find that file. 94 \(To put a file name in a register, you must use `set-register'.) 95If the register contains a window configuration (one frame) or a frame 96configuration (all frames), restore that frame or all frames accordingly. 97First argument is a character, naming the register. 98Optional second arg non-nil (interactively, prefix argument) says to 99delete any existing frames that the frame configuration doesn't mention. 100\(Otherwise, these frames are iconified.)" 101 (interactive "cJump to register: \nP") 102 (let ((val (get-register register))) 103 (cond 104 ((and (consp val) (frame-configuration-p (car val))) 105 (set-frame-configuration (car val) (not delete)) 106 (goto-char (cadr val))) 107 ((and (consp val) (window-configuration-p (car val))) 108 (set-window-configuration (car val)) 109 (goto-char (cadr val))) 110 ((markerp val) 111 (or (marker-buffer val) 112 (error "That register's buffer no longer exists")) 113 (switch-to-buffer (marker-buffer val)) 114 (goto-char val)) 115 ((and (consp val) (eq (car val) 'file)) 116 (find-file (cdr val))) 117 ((and (consp val) (eq (car val) 'file-query)) 118 (or (find-buffer-visiting (nth 1 val)) 119 (y-or-n-p (format "Visit file %s again? " (nth 1 val))) 120 (error "Register access aborted")) 121 (find-file (nth 1 val)) 122 (goto-char (nth 2 val))) 123 (t 124 (error "Register doesn't contain a buffer position or configuration"))))) 125 126(defun register-swap-out () 127 "Turn markers into file-query references when a buffer is killed." 128 (and buffer-file-name 129 (dolist (elem register-alist) 130 (and (markerp (cdr elem)) 131 (eq (marker-buffer (cdr elem)) (current-buffer)) 132 (setcdr elem 133 (list 'file-query 134 buffer-file-name 135 (marker-position (cdr elem)))))))) 136 137(defun number-to-register (number register) 138 "Store a number in a register. 139Two args, NUMBER and REGISTER (a character, naming the register). 140If NUMBER is nil, a decimal number is read from the buffer starting 141at point, and point moves to the end of that number. 142Interactively, NUMBER is the prefix arg (none means nil)." 143 (interactive "P\ncNumber to register: ") 144 (set-register register 145 (if number 146 (prefix-numeric-value number) 147 (if (looking-at "\\s-*-?[0-9]+") 148 (progn 149 (goto-char (match-end 0)) 150 (string-to-number (match-string 0))) 151 0)))) 152 153(defun increment-register (number register) 154 "Add NUMBER to the contents of register REGISTER. 155Interactively, NUMBER is the prefix arg." 156 (interactive "p\ncIncrement register: ") 157 (or (numberp (get-register register)) 158 (error "Register does not contain a number")) 159 (set-register register (+ number (get-register register)))) 160 161(defun view-register (register) 162 "Display what is contained in register named REGISTER. 163The Lisp value REGISTER is a character." 164 (interactive "cView register: ") 165 (let ((val (get-register register))) 166 (if (null val) 167 (message "Register %s is empty" (single-key-description register)) 168 (with-output-to-temp-buffer "*Output*" 169 (describe-register-1 register t))))) 170 171(defun list-registers () 172 "Display a list of nonempty registers saying briefly what they contain." 173 (interactive) 174 (let ((list (copy-sequence register-alist))) 175 (setq list (sort list (lambda (a b) (< (car a) (car b))))) 176 (with-output-to-temp-buffer "*Output*" 177 (dolist (elt list) 178 (when (get-register (car elt)) 179 (describe-register-1 (car elt)) 180 (terpri)))))) 181 182(defun describe-register-1 (register &optional verbose) 183 (princ "Register ") 184 (princ (single-key-description register)) 185 (princ " contains ") 186 (let ((val (get-register register))) 187 (cond 188 ((numberp val) 189 (princ val)) 190 191 ((markerp val) 192 (let ((buf (marker-buffer val))) 193 (if (null buf) 194 (princ "a marker in no buffer") 195 (princ "a buffer position:\n buffer ") 196 (princ (buffer-name buf)) 197 (princ ", position ") 198 (princ (marker-position val))))) 199 200 ((and (consp val) (window-configuration-p (car val))) 201 (princ "a window configuration.")) 202 203 ((and (consp val) (frame-configuration-p (car val))) 204 (princ "a frame configuration.")) 205 206 ((and (consp val) (eq (car val) 'file)) 207 (princ "the file ") 208 (prin1 (cdr val)) 209 (princ ".")) 210 211 ((and (consp val) (eq (car val) 'file-query)) 212 (princ "a file-query reference:\n file ") 213 (prin1 (car (cdr val))) 214 (princ ",\n position ") 215 (princ (car (cdr (cdr val)))) 216 (princ ".")) 217 218 ((consp val) 219 (if verbose 220 (progn 221 (princ "the rectangle:\n") 222 (while val 223 (princ " ") 224 (princ (car val)) 225 (terpri) 226 (setq val (cdr val)))) 227 (princ "a rectangle starting with ") 228 (princ (car val)))) 229 230 ((stringp val) 231 (if (eq yank-excluded-properties t) 232 (set-text-properties 0 (length val) nil val) 233 (remove-list-of-text-properties 0 (length val) 234 yank-excluded-properties val)) 235 (if verbose 236 (progn 237 (princ "the text:\n") 238 (princ val)) 239 (cond 240 ;; Extract first N characters starting with first non-whitespace. 241 ((string-match (format "[^ \t\n].\\{,%d\\}" 242 ;; Deduct 6 for the spaces inserted below. 243 (min 20 (max 0 (- (window-width) 6)))) 244 val) 245 (princ "text starting with\n ") 246 (princ (match-string 0 val))) 247 ((string-match "^[ \t\n]+$" val) 248 (princ "whitespace")) 249 (t 250 (princ "the empty string"))))) 251 (t 252 (princ "Garbage:\n") 253 (if verbose (prin1 val)))))) 254 255(defun insert-register (register &optional arg) 256 "Insert contents of register REGISTER. (REGISTER is a character.) 257Normally puts point before and mark after the inserted text. 258If optional second arg is non-nil, puts mark before and point after. 259Interactively, second arg is non-nil if prefix arg is supplied." 260 (interactive "*cInsert register: \nP") 261 (push-mark) 262 (let ((val (get-register register))) 263 (cond 264 ((consp val) 265 (insert-rectangle val)) 266 ((stringp val) 267 (insert-for-yank val)) 268 ((numberp val) 269 (princ val (current-buffer))) 270 ((and (markerp val) (marker-position val)) 271 (princ (marker-position val) (current-buffer))) 272 (t 273 (error "Register does not contain text")))) 274 (if (not arg) (exchange-point-and-mark))) 275 276(defun copy-to-register (register start end &optional delete-flag) 277 "Copy region into register REGISTER. With prefix arg, delete as well. 278Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. 279START and END are buffer positions indicating what to copy." 280 (interactive "cCopy to register: \nr\nP") 281 (set-register register (filter-buffer-substring start end)) 282 (if delete-flag (delete-region start end))) 283 284(defun append-to-register (register start end &optional delete-flag) 285 "Append region to text in register REGISTER. 286With prefix arg, delete as well. 287Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. 288START and END are buffer positions indicating what to append." 289 (interactive "cAppend to register: \nr\nP") 290 (or (stringp (get-register register)) 291 (error "Register does not contain text")) 292 (set-register register (concat (get-register register) 293 (filter-buffer-substring start end))) 294 (if delete-flag (delete-region start end))) 295 296(defun prepend-to-register (register start end &optional delete-flag) 297 "Prepend region to text in register REGISTER. 298With prefix arg, delete as well. 299Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. 300START and END are buffer positions indicating what to prepend." 301 (interactive "cPrepend to register: \nr\nP") 302 (or (stringp (get-register register)) 303 (error "Register does not contain text")) 304 (set-register register (concat (filter-buffer-substring start end) 305 (get-register register))) 306 (if delete-flag (delete-region start end))) 307 308(defun copy-rectangle-to-register (register start end &optional delete-flag) 309 "Copy rectangular region into register REGISTER. 310With prefix arg, delete as well. To insert this register 311in the buffer, use \\[insert-register]. 312 313Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG. 314START and END are buffer positions giving two corners of rectangle." 315 (interactive "cCopy rectangle to register: \nr\nP") 316 (set-register register 317 (if delete-flag 318 (delete-extract-rectangle start end) 319 (extract-rectangle start end)))) 320 321(provide 'register) 322;;; arch-tag: ce14dd68-8265-475f-9341-5d4ec5a53035 323;;; register.el ends here 324