PlanDuSite ModificationsRécentes Nouvelles SectionElisp CommentFaire

BrowseKillRing

This page is about ways to browse the kill ring, choosing a kill to yank.

Browsing the Kill Ring

In both cases you can clean up the ring selectively during completion, deleting entries on the fly. And you can sort completions in various ways. All Icicles completion features are available, including progressive completion.
       (global-set-key "\C-cy" '(lambda ()
                                 (interactive)
                                 (popup-menu 'yank-menu)))
 (defun konix/kill-ring-insert ()
   (interactive)
   (let (
        (to_insert (completing-read "Yank : "
                                    (delete-duplicates kill-ring :test #'equal)
                                    )
                   )
        )
    (when (and to_insert
               (region-active-p)
               )
      ;; the currently highlighted section is to be replaced by the yank
      (delete-region (region-beginning) (region-end))
      )
    (insert to_insert)
    )
   )

Discussion

I can’t praise browse-kill-ring enough – I don’t use it all that much, but when I need it there’s no replacing it. – Anonymous

Someone once said “The easiest way to invoke kill-ring browsing is just to use ‘M-y’ without first using ‘C-y’”. But when I try that (with Emacs 22) I just get “Previous command was not a yank”.

To get the “M-y immediately pulls up the kill ring” behavior, you need to run M-x browse-kill-ring-default-keybindings. Or, to put this in .emacs

 (require 'browse-kill-ring)
 (browse-kill-ring-default-keybindings)

Not that anyone here will believe me, but I am the NickHurley? referenced above. I can safely say that I have abandoned browse-kill-ring.el (which is now hosted on GitHub: https://github.com/todesschaf/browse-kill-ring for those who are interested). Enjoy.

Here’s a “poor man’s kill-ring-browser”:

    (defun yank-browse (string)
      "Browse the `kill-ring' to choose which entry to yank."
      (interactive
       (minibuffer-with-setup-hook #'minibuffer-completion-help
         (let* ((kills (delete-dups (append kill-ring-yank-pointer kill-ring nil)))
                (entries
                 (mapcar (lambda (string)
                           (let ((pos 0))
                             ;; FIXME: Maybe we should start by removing
                             ;; all properties.
                             (setq string (copy-sequence string))
                             (while (string-match "\n" string pos)
                               ;; FIXME: Maybe completion--insert-strings should
                               ;; do that for us.
                               (put-text-property
                                (match-beginning 0) (match-end 0)
                                'display (eval-when-compile
                                           (propertize "\\n" 'face 'escape-glyph))
                                string)
                               (setq pos (match-end 0)))
                             ;; FIXME: We may use the window-width of the
                             ;; wrong window.
                             (when (>= (* 3 (string-width string))
                                       (* 2 (window-width)))
                               (let ((half (- (/ (window-width) 3) 1)))
                                 ;; FIXME: We're using char-counts rather than
                                 ;; width-count.
                                 (put-text-property
                                  half (- (length string) half)
                                  'display (eval-when-compile
                                             (propertize "……" 'face 'escape-glyph))
                                  string)))
                             string))
                         kills))
                (table (lambda (string pred action)
                         (cond
                          ((eq action 'metadata)
                           '(metadata (category . kill-ring)))
                          (t
                           (complete-with-action action entries string pred))))))
           ;; FIXME: We should return the entry from the kill-ring rather than
           ;; the entry from the completion-table.
           ;; FIXME: substring completion doesn't work well because it only matches
           ;; subtrings before the first \n.
           ;; FIXME: completion--insert-strings assumes that boundaries of
           ;; candidates are obvious enough, but with kill-ring entries this is not
           ;; true, so we'd probably want to display them with «...» around them.
           (list (completing-read "Yank: " table nil t)))))
      (setq this-command 'yank)
      (insert-for-yank string))

I adapted popup-kill-ring to replace a potential previous yank like the default M-y does (yank-pop).

diff --git a/libs/popup-kill-ring.el b/libs/popup-kill-ring.el
--- a/libs/popup-kill-ring.el
+++ b/libs/popup-kill-ring.el
@@ -280,6 +280,8 @@ and `pos-tip.el'"
                                            :scroll-bar t
                                            :isearch popup-kill-ring-isearch))
                    (when item
+                     (when (or (eq last-command 'cua--paste-rectangle) (eq last-command 'yank))
+                       (undo))
                      (setq num (popup-kill-ring-get-index item))
                      (when num
                        (let ((kill-ring-item (nth num kill-ring)))

ArneBab


CategoryEditing