Rorschach
Rorschach

Reputation: 32426

Change expression in body of elisp function with advice

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.

Upvotes: 1

Views: 366

Answers (1)

Barmar
Barmar

Reputation: 781068

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))

Upvotes: 3

Related Questions