:type '(choice number (const :tag "No preview unless requested" nil))
:group 'register)
+(defcustom register-confirm-overwrite t
+ "Whether to ask for confirmation before overwriting register contents."
+ :version "30.1"
+ :type 'boolean
+ :group 'register)
+
(defun get-register (register)
"Return contents of Emacs register named REGISTER, or nil if none."
(alist-get register register-alist))
Called with one argument, a cons (NAME . CONTENTS) as found in `register-alist'.
The function should return a string, the description of the argument.")
-(defun register-preview (buffer &optional show-empty)
+(defun register-preview (buffer &optional show-empty highlight)
"Pop up a window showing the registers preview in BUFFER.
If SHOW-EMPTY is non-nil, show the window even if no registers.
-Format of each entry is controlled by the variable `register-preview-function'."
+Optional argument HIGHLIGHT says to highlight the description of
+a register with that name. Format of each entry is controlled by
+the variable `register-preview-function'."
(when (or show-empty (consp register-alist))
(with-current-buffer-window
buffer
(preserve-size . (nil . t))))
nil
(with-current-buffer standard-output
+ (delete-region (point-min) (point-max))
(setq cursor-in-non-selected-windows nil)
(mapc (lambda (elem)
- (when (get-register (car elem))
- (insert (funcall register-preview-function elem))))
+ (when-let ((name (car elem))
+ (reg (get-register name))
+ (desc (funcall register-preview-function elem)))
+ (when (equal highlight name)
+ (add-face-text-property 0 (length desc) 'match nil desc))
+ (insert desc)))
register-alist)))))
-(defun register-read-with-preview (prompt)
+(defun register-read-with-preview (prompt &optional confirm)
"Read and return a register name, possibly showing existing registers.
-Prompt with the string PROMPT. If `register-alist' and
+Prompt with the string PROMPT. Optional argument CONFIRM says to
+ask for confirmation if the register is already in use and
+`register-confirm-overwrite' is non-nil. If `register-alist' and
`register-preview-delay' are both non-nil, display a window
-listing existing registers after `register-preview-delay' seconds.
-If `help-char' (or a member of `help-event-list') is pressed,
-display such a window regardless."
+listing existing registers after `register-preview-delay'
+seconds. If `help-char' (or a member of `help-event-list') is
+pressed, display such a window regardless."
(let* ((buffer "*Register Preview*")
(timer (when (numberp register-preview-delay)
(run-with-timer register-preview-delay nil
help-chars)
(unless (get-buffer-window buffer)
(register-preview buffer 'show-empty)))
- (when (or (eq ?\C-g last-input-event)
- (eq 'escape last-input-event)
- (eq ?\C-\[ last-input-event))
+ (cond
+ ((or (eq ?\C-g last-input-event)
+ (eq 'escape last-input-event)
+ (eq ?\C-\[ last-input-event))
(keyboard-quit))
+ ((and (get-register last-input-event)
+ confirm register-confirm-overwrite
+ (not (progn
+ (register-preview buffer nil last-input-event)
+ (y-or-n-p (substitute-quotes
+ (format "Overwrite register `%s'?"
+ (single-key-description
+ last-input-event))))))
+ (user-error "Register already in use"))))
(if (characterp last-input-event) last-input-event
(error "Non-character input-event")))
(and (timerp timer) (cancel-timer timer))
(interactive (list (register-read-with-preview
(if current-prefix-arg
"Frame configuration to register: "
- "Point to register: "))
+ "Point to register: ")
+ t)
current-prefix-arg))
;; Turn the marker into a file-ref if the buffer is killed.
(add-hook 'kill-buffer-hook 'register-swap-out nil t)
Interactively, prompt for REGISTER using `register-read-with-preview'."
(interactive (list (register-read-with-preview
- "Window configuration to register: ")
+ "Window configuration to register: " t)
current-prefix-arg))
;; current-window-configuration does not include the value
;; of point in the current buffer, so record that separately.
Interactively, prompt for REGISTER using `register-read-with-preview'."
(interactive (list (register-read-with-preview
- "Frame configuration to register: ")
+ "Frame configuration to register: " t)
current-prefix-arg))
;; current-frame-configuration does not include the value
;; of point in the current buffer, so record that separately.
Interactively, prompt for REGISTER using `register-read-with-preview'."
(interactive (list current-prefix-arg
- (register-read-with-preview "Number to register: ")))
+ (register-read-with-preview "Number to register: " t)))
(set-register register
(if number
(prefix-numeric-value number)
Interactively, prompt for REGISTER using `register-read-with-preview'
and use mark and point as START and END; REGION is always non-nil in
this case."
- (interactive (list (register-read-with-preview "Copy to register: ")
+ (interactive (list (register-read-with-preview "Copy to register: " t)
(region-beginning)
(region-end)
current-prefix-arg
Interactively, prompt for REGISTER using `register-read-with-preview',
and use mark and point as START and END."
(interactive (list (register-read-with-preview
- "Copy rectangle to register: ")
+ "Copy rectangle to register: " t)
(region-beginning)
(region-end)
current-prefix-arg))