What does ad-activate do?

扶醉桌前 提交于 2019-12-10 15:08:31

问题


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



来源:https://stackoverflow.com/questions/10921225/what-does-ad-activate-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!