Skip to content

Instantly share code, notes, and snippets.

@markhepburn
Created July 28, 2009 23:51
Show Gist options
  • Save markhepburn/157761 to your computer and use it in GitHub Desktop.
Save markhepburn/157761 to your computer and use it in GitHub Desktop.
;;; Problem solved, by subterfuge.
(defadvice rgrep (around rgrep-rename-previous-buffer activate compile)
"If a previous *grep* buffer exists, offer to rename it before
running the new process."
(interactive
(let ((old-grep-buf (get-buffer "*grep*")))
(if (and old-grep-buf
(y-or-n-p "Grep results buffer already exists; rename it first? "))
(let ((new-buf-name (read-string "New name: ")))
(with-current-buffer old-grep-buf
(rename-buffer new-buf-name))))
;; Sneaky: we need to match the interactive spec for the original
;; rgrep, but we also just want to use the original
;; interactivity. Hence, this dummy list followed by
;; 'call-interactively (and no reference to ad-do-it)
(list 'ignored-re 'ignored-files 'ignored-dirs)))
(call-interactively 'ad-Orig-rgrep))
;;; This doesn't work: I want to advise rgrep so that if an existing
;;; set of results exists, offer the user the opportunity to rename
;;; them before they get clobbered. Currently it errors because the
;;; argument lists don't match (due to the interactive form), but I
;;; also want to continue using the original definition's interaction,
;;; just tack some extra before it. Can I do this?
;; (defadvice rgrep (around rgrep-rename-previous-buffer activate compile)
;; "If a previous *grep* buffer exists, offer to rename it before
;; running the new process."
;; (interactive)
;; (let ((old-grep-buf (get-buffer "*grep*")))
;; (if (and old-grep-buf
;; (y-or-n-p "Grep results buffer already exists; rename it first? "))
;; (let ((new-buf-name (read-string "New name: ")))
;; (with-current-buffer old-grep-buf
;; (rename-buffer new-buf-name)))))
;; (call-interactively 'ad-Orig-rgrep))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment