From a8e1ec1aa5271c54fbcdd8d52ca5a8d373da1f9c Mon Sep 17 00:00:00 2001 From: JSDurand Date: Wed, 3 Mar 2021 11:07:10 +0800 Subject: Completion for yank * basic.el (global-map): Bind it. * common.el (yank-complete-rotated-kill-ring): Rotating the kill ring so that the last kill is at the top. (yank-complete-history): History variable. (yank-complete): Use `completing-read' for choosing some text that was killed before. --- common.el | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'common.el') diff --git a/common.el b/common.el index 3271da1..ba2c534 100644 --- a/common.el +++ b/common.el @@ -153,6 +153,53 @@ ARG means do this command ARG times." (interactive "p") (durand-completion-scroll-down-or-go-to-minibuffer (- arg))) +;;; Use completion to select text that was killed before. + +;;;###autoload +(defun yank-complete-rotated-kill-ring () + "Return the rotated kill ring so that the current kill is at the top." + (let* ((tail kill-ring-yank-pointer) + (len-tail (length tail)) + (len (length kill-ring)) + (copied (copy-tree kill-ring)) + (head (progn (setcdr (nthcdr (mod (- -1 len-tail) + len) + copied) + nil) + copied)) + ;; Judged from `current-kill', this can be a list for some + ;; reason. + (interprogram-paste (and interprogram-paste-function + (funcall interprogram-paste-function)))) + (append (cond ((listp interprogram-paste) interprogram-paste) + ((stringp interprogram-paste) (list interprogram-paste))) + tail head))) + +;;;###autoload +(defvar yank-complete-history nil + "The history of `yank-complete'.") + +;;;###autoload +(defun yank-complete (&optional arg) + "Use completion to select text that was killed before. +If ARG is a cons cell, then put the point at the beginning, the +mark at the end for the first yank." + ;; This is largely copied from `yank'. + (interactive) + (let* ((inhibit-read-only t) + (kills (yank-complete-rotated-kill-ring)) + (choice (completing-read "Yank: " kills nil + t nil 'yank-complete-history))) + (setq yank-window-start (window-start)) + (setq this-command t) + (push-mark) + (insert-for-yank choice) + (cond ((consp arg) + (goto-char (prog1 (mark t) + (set-marker (mark-marker) (point) (current-buffer))))))) + (cond ((eq this-command t) + (setq this-command 'yank)))) + ;;; Don't ask me if I want to visit the file again. (require 'register) @@ -206,3 +253,49 @@ completely hiding it." +;;; Archives + +;; ;;;###autoload +;; (defvar yank-pop-completion-index 0 +;; "The index of the current kill.") + +;; ;;;###autoload +;; (defun yank-pop-cycle-candidate (&optional arg) +;; "Cycle the candidates by the offset ARG. +;; This is intended to be bound in the minibuffer-local-completion-map." +;; (interactive "p") +;; (cond ((minibufferp)) +;; ((listp minibuffer-completion-table)) +;; ((error "Function `yank-pop-cycle-candidate' called in a weird place"))) +;; (delete-minibuffer-contents) +;; (let ((len (length minibuffer-completion-table))) +;; (setq yank-pop-completion-index (+ arg yank-pop-completion-index)) +;; (insert (nth (mod yank-pop-completion-index len) +;; minibuffer-completion-table)))) + +;; ;;;###autoload +;; (defun yank-pop-completion-previous-candidate (&optional arg) +;; "Go to the previous ARG candidate. +;; This is intended to be bound in the minibuffer-local-completion-map." +;; (interactive "p") +;; (yank-pop-cycle-candidate (- arg))) + +;; ;;;###autoload +;; (defun yank-pop-completion-set-minibuffer () +;; "Set up minibuffer for use of `yank-pop-complete'." +;; (setq yank-pop-completion-index 0) +;; (define-key minibuffer-local-completion-map +;; (vector ?\M-p) #'yank-pop-completion-previous-candidate) +;; (define-key minibuffer-local-completion-map +;; (vector ?\M-n) #'yank-pop-cycle-candidate) +;; (add-hook 'minibuffer-exit-hook #'yank-pop-completion-clear)) + +;; ;;;###autoload +;; (defun yank-pop-completion-clear () +;; "Clear the traces of `yank-pop-complete'." +;; (setq yank-pop-completion-index 0) +;; (define-key minibuffer-local-completion-map +;; (vector ?\M-p) #'previous-history-element) +;; (define-key minibuffer-local-completion-map +;; (vector ?\M-n) #'next-history-element) +;; (remove-hook 'minibuffer-exit-hook #'yank-pop-completion-clear)) -- cgit v1.2.3-18-g5258