1

How do you prevent the advised function from running when the advice returns nil?

(defadvice beginning-of-line (before test activate)
 nil)

-> Not running beginning-of-line at all.

EDIT: Just to take away your worries, I do not intend to use it on beginning-of-line.

Drew
  • 29,895
  • 7
  • 74
  • 104
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160

1 Answers1

1

You can use an around advice, as is:

(defadvice foo (around test activate)
  (if my-test
      ad-do-it))

The construct ad-do-it corresponds to running the original function.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • This does not work for a `cond`. Actually, the around seems to cause the trouble. In this case it runs the advise, finds out it should run the function, but then it does the advise again! – PascalVKooten Feb 19 '13 at 12:17
  • This doesn't sound right! Are you sure your old advice isn't still active as well? – Lindydancer Feb 19 '13 at 12:19
  • I am absolutely certain: closed and reopened emacs. – PascalVKooten Feb 19 '13 at 12:20
  • To elaborate, the function deletes several lines, and the advice has a cond which has (looking-at "(") in there. The last one of the `cond` is ad-do-it. Here's the thing: first of all it deletes several lines (because it is not looking at "("), but then after deleting those lines it is (looking-at "(") and then it also deletes that "(" – PascalVKooten Feb 19 '13 at 12:22
  • You should be able to run the function from here (all included): http://pastebin.com/AUenTCfG – PascalVKooten Feb 19 '13 at 12:35
  • I just tested it. It seems to work as intended -- whenever the point is at a parenthesis it, and its mate, are deleted. Otherwise things like the region is deleted. Try adding "message":s to convince yourself that things are not executed more than you expect. – Lindydancer Feb 19 '13 at 14:55
  • The thing is, when you have some lines above a (defun) in empty space, it deletes the return characters as intended, but it also deletes the parentheses. It should only delete the enters. – PascalVKooten Feb 19 '13 at 15:20
  • Again, it works as intended here, using the code you posted... The `(defun)` is left intact and the empty lines are deleted. – Lindydancer Feb 19 '13 at 16:02
  • Yea, when renaming the functions, this code does work! Thanks, I'll have to look again in my code to see what is going on. – PascalVKooten Feb 19 '13 at 22:37