5

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

Community
  • 1
  • 1
Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74

1 Answers1

4

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

Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74
  • You should be able to edit your question and add this information to your question (as opposed to proving an "answer" - which it really isn't). – Trey Jackson Jun 06 '12 at 20:02
  • @Trey didn't I answer the question? The answer explains what `ad-activate` does. Or maybe it doesn't. Let's add that... – Michel de Ruiter Jun 06 '12 at 20:05
  • Ah, perhaps it's that the original question wasn't very clear (to me), and what I assumed you were asking wasn't really what you wanted to know... – Trey Jackson Jun 06 '12 at 20:08
  • 3
    you can define a function around align-regexp and then use that function. however, any _other_ functions which use align-regexp directly will still use the old version. when you advise a function, you essentially _replace_ it, so any other code which uses align-regexp will now use your modified version. final note: advising is very powerful and very dangerous, use sparingly (it's sometimes hard to predict how your advice might operate when used by other code in other contexts). – jtahlborn Jun 06 '12 at 20:43
  • another note, if you define the advice with `... (around align-regexp-with-spaces activate compile preactivate) ...`, then you don't need the separate `(ad-activate...` line (there are subtleties to using each of those modifiers but they've always worked fine for me in practice). – jtahlborn Jun 06 '12 at 20:45
  • 2
    Personally I like having the explicit call to `ad-activate` just because it's convenient to change it to the corresponding `ad-deactivate` to evaluate that (or re-evaluate as-is after an `ad-disable-*` call). – phils Jun 06 '12 at 23:01
  • 1
    Michel de Ruiter: More precisely, for the function specified, `ad-activate` activates *all* currently-enabled advice (not necessarily just one defined in the previous expression), and will also *deactivate* any advice which was previously active, but has since been disabled (see http://www.gnu.org/software/emacs/manual/html_node/elisp/Enabling-Advice.html). – phils Jun 07 '12 at 04:50