Function: read-from-kill-ring

read-from-kill-ring is a byte-compiled function defined in simple.el.gz.

Signature

(read-from-kill-ring PROMPT)

Documentation

Read a kill-ring entry using completion and minibuffer history.

PROMPT is a string to prompt with. Return the entry as a string.

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun read-from-kill-ring (prompt)
  "Read a `kill-ring' entry using completion and minibuffer history.
PROMPT is a string to prompt with.
Return the entry as a string."
  ;; `current-kill' updates `kill-ring' with a possible interprogram-paste
  (current-kill 0)
  (let* ((history-add-new-input nil)
         (history-pos (when yank-from-kill-ring-rotate
                        (- (length kill-ring)
                           (length kill-ring-yank-pointer))))
         (ellipsis (if (char-displayable-p ?…) "…" "..."))
         ;; Remove keymaps from text properties of copied string,
         ;; because typing RET in the minibuffer might call
         ;; an irrelevant command from the map of copied string.
         (read-from-kill-ring-history
          (mapcar (lambda (s)
                    (remove-list-of-text-properties
                     0 (length s)
                     '(
                       keymap local-map action mouse-action
                       read-only button category help-args)
                     s)
                    s)
                  kill-ring))
         (completions
          (mapcar (lambda (s)
                    (let* ((s (query-replace-descr s))
                           (b 0)
                           (limit (frame-text-cols)))
                      ;; Add ellipsis on leading whitespace
                      (when (string-match "\\`[[:space:]]+" s)
                        (setq b (match-end 0))
                        (add-text-properties 0 b `(display ,ellipsis) s))
                      ;; Add ellipsis at the end of a long string
                      (when (> (length s) (+ limit b))
                        (add-text-properties
                         (min (+ limit b) (length s)) (length s)
                         `(display ,ellipsis) s))
                      s))
                  read-from-kill-ring-history)))
    (minibuffer-with-setup-hook
        (lambda ()
          ;; Allow ‘SPC’ to be self-inserting
          (use-local-map
           (let ((map (make-sparse-keymap)))
             (set-keymap-parent map (current-local-map))
             (define-key map " " nil)
             (define-key map "?" nil)
             map)))
      (completing-read
       prompt
       (lambda (string pred action)
         (if (eq action 'metadata)
             ;; Keep sorted by recency
             '(metadata (display-sort-function . identity))
           (complete-with-action action completions string pred)))
       nil nil nil
       (if history-pos
           (cons 'read-from-kill-ring-history
                 (if (zerop history-pos) history-pos (1+ history-pos)))
         'read-from-kill-ring-history)))))