1

How do you match a specific expression in the body on elisp function when adding advice? Specifically, in the following example, I would like to advise the function to use find-file-noselect in place of find-file, ie. the line (find-file path) would be in effect (find-file-noselect path).

(defun tst-fun (path line column)
  (find-file path)
  (goto-char (point-min))
  (forward-line (1- line))
  (forward-char column))

;; not sure how to structure this
(defadvice tst-fun (around noselect activate)
  (find-file-noselect (ad-get-arg 0))
  ad-do-it)

I would rather have it as ad-add-function, but am trying to just get this working first.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Rorschach
  • 31,301
  • 5
  • 78
  • 129

1 Answers1

3

You could temporarily redefine find-file as find-file-noselect in the advice.

(require 'cl)
(defadvice tst-fun (around noselect activate)
  (flet ((find-file (&rest args)
           (apply 'find-file-noselect args)))
    ad-do-it))
Stefan
  • 27,908
  • 4
  • 53
  • 82
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I'm pretty sure there's no way to do that. Some other Lisp dialects had `:INSIDE` advice, where you could advise `find-file` only inside `tst-fun`, but Elisp doesn't have this. – Barmar Oct 27 '16 at 23:07
  • 2
    In "modern" Elisp, this would look like: `(require 'cl-lib) (advice-add 'tst-fun :around (lambda (orig &rest args) (cl-letf (((symbol-function 'find-file) #'find-file-noselect)) (apply orig args))`. – Stefan Oct 28 '16 at 14:05