[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Occur stack

From: Tom
Subject: Re: Occur stack
Date: Wed, 15 Jan 2014 20:16:22 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Andreas Röhler <andreas.roehler <at> online.de> writes:
> Being able to jump back or read occur-history like 
> M-x browse-kill-ring would be great.

I created a simple proof of concept implementation, so it can
be tested in practice.

It can be activated by adding this to the occur hook and then
it saves the last 10 occur results:

(add-hook 'occur-hook 'buffer-history-save)

If you do some occuring and then run M-x buffer-history-list
in the occur buffer then it lists the stored occur sessions
and you can press RET on any of them to restore it in 
the occur buffer.

The code is generic, so in theory it can work for other
commands too, but I only tested it with occur.

Note that it's a very simple implementation, it only restores
the stored contents in the occur buffer, so it requires 
a live occur buffer. For this reason it begins with an occur
advice which prevents occur from killing the occur buffer
if there are no matches.

Here's the code: 

;; this is needed to prevent occur from killing the buffer
;; when there are no matches
(defadvice occur (around my-occur-advice activate)
  (flet ((kill-buffer (buf)
                      (bury-buffer buf)))

(setq buffer-history nil)

(defun buffer-history-save ()
  (let ((entry (assoc major-mode buffer-history)))
    (unless entry
      (setq entry (cons major-mode '()))
      (push entry buffer-history))
    (push (buffer-substring (point-min) (point-max))
          (cdr entry))
    (if (> (length (cdr entry)) 10)
        (setcdr entry (butlast (cdr entry))))))

(setq buffer-history-target-buffer nil)

(defun buffer-history-list ()
  (let ((entry (assoc major-mode buffer-history)))
    (unless entry
      (error "No buffer history here."))

    (setq buffer-history-target-buffer (current-buffer))

    (pop-to-buffer "*buffer history*")
    (let ((inhibit-read-only t))

    (dolist (item (cdr entry))
      ;; assuming the first line describes the contents
      (assert (string-match ".*?\n" item))
      (let ((start (point)))
        (let ((line (match-string 0 item)))
          (set-text-properties 0 (length line) nil line)
          (insert line))
         start (1+ start)
         'buffer-history-contents item)))

    (goto-char (point-min))
    (local-set-key (kbd "RET") 'buffer-history-restore)))

(defun buffer-history-restore ()
  (let ((contents (get-text-property (line-beginning-position)
    (pop-to-buffer buffer-history-target-buffer)
    (let ((inhibit-read-only t))
      (insert contents))
    (goto-char (point-min))))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]