[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: Fri, 17 Jan 2014 17:19:50 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Andreas Röhler <andreas.roehler <at> online.de> writes:
> In cases where a plenty of occur-calls is done, 
> maybe the discussion WRT to memory pertains?

Here's an other experimental code which does it the other way:
it does not store buffer contents, it reruns the command

This way memory is not an issue, because it only stores
a short description and a restore function, but in this case
it requires explicit support from the involved commands,
they need to supply the function for restoring the results.

(The code again is simple, so it stores the live buffer
pointers of the occur target buffers, it does not reopen 
the target buffer if it has been closed after running occur.)

(setq buffer-history nil)

(defun buffer-history-save (description restore-function)
  (let ((entry (assoc major-mode buffer-history)))
    (unless entry
      (setq entry (cons major-mode '()))
      (push entry buffer-history))
    (push (list 'description description
                'restore-function restore-function)
          (cdr entry))))

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

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

    (dolist (item (cdr entry))
      (let ((start (point)))
        (insert (plist-get item 'description) "\n")
         start (1+ start)
         (plist-get item 'restore-function))))

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

(defun buffer-history-restore ()
  (funcall (get-text-property (line-beginning-position)

(defun buffer-history-save-occur ()
  (let* ((regexp (car occur-revert-arguments))
         (buffers (car (cddr occur-revert-arguments)))
          (cond ((eq this-command 'occur)
                 (eval `(let ((buffer (car buffers))
                              (regexp regexp))
                          (lambda ()
                            (with-current-buffer buffer
                              (occur regexp))))

                ((eq this-command 'multi-occur)
                 (eval `(let ((buffers buffers)
                              (regexp regexp))
                          (lambda ()
                            (multi-occur buffers regexp)))

    (if (not restore-fun)
        (unless (eq this-command 'buffer-history-restore)
          (message "buffer-history: unsupported occur command '%s'"

      (set-text-properties 0 (length regexp) nil regexp)

       (format "'%s' in %s"
               (mapconcat (lambda (buffer)
                            (buffer-name buffer))
                          ", "))

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

reply via email to

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