Advice (programming): Difference between revisions

Content deleted Content added
m Update syntaxhighlight tags - remove use of deprecated <source> tags - BOT in trial - BRFA
m Fixed typos found with Wikipedia:Typo_Team/moss.
Line 20:
The practical use of advice functions is generally to modify or otherwise extend the behavior of functions which cannot be easily modified or extended. The [[Emacspeak]] [[Emacs]]-addon makes extensive use of advice: it must modify thousands of existing Emacs modules and functions such that it can produce audio output for the blind corresponding to the visual presentation, but it would be infeasible to copy all of them and redefine them to produce audio output in addition to their normal outputs; so, the Emacspeak programmers define advice functions which run before and after.
 
Another Emacs example; suppose after one corrected a misspelled word through [[ispell]], one wanted to re-spellcheck the entire buffer. <code>ispell-word</code> offers no such functionality, even if the spellchecked word is used a thousand times. One ''could'' track down the definition of <code>ispell-word</code>, copy it into one's .emacsEmac, and write the additional functionality, but this is tedious, prone to broken-ness (the .emacsEmac version will get out of sync with the actual Ispell Elisp module, if it even works out of its home). What one wants is fairly simple: just to run another command after <code>ispell-word</code> runs. Using advice functions, it can be done as simply as this:
 
<syntaxhighlight lang="elisp">