What does ad-activate do?

2019-06-21 19:33发布

问题:

In an answer, I noticed:

;; Align with spaces only
(defadvice align-regexp (around align-regexp-with-spaces)
  "Never use tabs for alignment."
  (let ((indent-tabs-mode nil))
    ad-do-it))
(ad-activate 'align-regexp)

This sounds promising, but... what does it do?!

I tried eval-region on the block of code. But for me, all it does is adding the following to the align-regexp docs:

This function is advised.

Around-advice `align-regexp-with-spaces':
Never use tabs for alignment.

I don't seem to be able to actually use align-regexp-with-spaces, if that's what should be the effect... What am I missing?

I used GNU Emacs version 24.0.96.1 (i386-mingw-nt6.1.7601).

回答1:

While asking this question, I realized that I just didn't get the idea of advising functions.

It became clear to me that:

  • align-regexp-with-spaces isn't a function nor a variable but only a name (to enable/disable single pieces of advice)
  • ever since (ad-activate 'align-regexp), align-regexp just does what I 'advised' it to: not to use tabs

So: ad-activate activates the advice, effectively changing the original function's behavior. Great!

I don't get why this is 'better' than defining a function around align-regexp though. But then again I don't know much about Emacs Lisp.

I'm afraid the extra lines of documentation only added to the confusion...