aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2014-03-21 17:47:52 -0400
committerStefan Monnier2014-03-21 17:47:52 -0400
commit0c0ec041630ca71b5fd031144451e949ce0fec01 (patch)
tree97a1517239ecb664a7d5d05b40be5df29d0dc199
parent1e92a8a3aa7958ba699cd0430be4f23aff6c4c01 (diff)
downloademacs-0c0ec041630ca71b5fd031144451e949ce0fec01.tar.gz
emacs-0c0ec041630ca71b5fd031144451e949ce0fec01.zip
* doc/lispref/functions.texi (Advising Functions): Explain a bit more how
arguments work. (Advice combinators): New node. (Core Advising Primitives): Use it. Expand description of "depth". (Advising Named Functions): Document limitation of advices on macros.
-rw-r--r--doc/lispref/ChangeLog8
-rw-r--r--doc/lispref/functions.texi248
2 files changed, 150 insertions, 106 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog
index 3bbcee76884..ea5e50554fa 100644
--- a/doc/lispref/ChangeLog
+++ b/doc/lispref/ChangeLog
@@ -1,3 +1,11 @@
12014-03-21 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * functions.texi (Advising Functions): Explain a bit more how
4 arguments work.
5 (Advice combinators): New node.
6 (Core Advising Primitives): Use it. Expand description of "depth".
7 (Advising Named Functions): Document limitation of advices on macros.
8
12014-03-21 Martin Rudalics <rudalics@gmx.at> 92014-03-21 Martin Rudalics <rudalics@gmx.at>
2 10
3 * frames.texi (Size and Position): In `frame-resize-pixelwise' 11 * frames.texi (Size and Position): In `frame-resize-pixelwise'
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index 3e1db68f70d..c791e82bd40 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -1170,9 +1170,10 @@ For example, in order to trace the calls to the process filter of a process
1170(add-function :before (process-filter @var{proc}) #'my-tracing-function) 1170(add-function :before (process-filter @var{proc}) #'my-tracing-function)
1171@end example 1171@end example
1172 1172
1173This will cause the process's output to be passed first to 1173This will cause the process's output to be passed to @code{my-tracing-function}
1174@code{my-tracing-function} and then to the original process filter. 1174before being passed to the original process filter. @code{my-tracing-function}
1175When you're done with it, you can revert to the untraced behavior with: 1175receives the same arguments as the original function. When you're done with
1176it, you can revert to the untraced behavior with:
1176 1177
1177@example 1178@example
1178(remove-function (process-filter @var{proc}) #'my-tracing-function) 1179(remove-function (process-filter @var{proc}) #'my-tracing-function)
@@ -1191,20 +1192,24 @@ Similarly, if you want to trace the execution of the function named
1191(advice-add 'display-buffer :around #'his-tracing-function) 1192(advice-add 'display-buffer :around #'his-tracing-function)
1192@end example 1193@end example
1193 1194
1194and when you're tired of seeing this output, you can revert to the untraced 1195Here, @code{his-tracing-function} is called instead of the original function
1196and receives the original function (additionally to that function's arguments)
1197as argument, so it can call it if and when it needs to.
1198When you're tired of seeing this output, you can revert to the untraced
1195behavior with: 1199behavior with:
1196 1200
1197@example 1201@example
1198(advice-remove 'display-buffer #'his-tracing-function) 1202(advice-remove 'display-buffer #'his-tracing-function)
1199@end example 1203@end example
1200 1204
1201The arguments @code{:before} and @code{:above} used in the above examples 1205The arguments @code{:before} and @code{:around} used in the above examples
1202specify how the two functions are composed, since there are many different 1206specify how the two functions are composed, since there are many different
1203ways to do it. The added function is also called an @emph{advice}. 1207ways to do it. The added function is also called an @emph{advice}.
1204 1208
1205@menu 1209@menu
1206* Core Advising Primitives:: Primitives to Manipulate Advices 1210* Core Advising Primitives:: Primitives to Manipulate Advices
1207* Advising Named Functions:: Advising Named Functions 1211* Advising Named Functions:: Advising Named Functions
1212* Advice combinators:: Ways to compose advices
1208* Porting old advices:: Adapting code using the old defadvice 1213* Porting old advices:: Adapting code using the old defadvice
1209@end menu 1214@end menu
1210 1215
@@ -1225,104 +1230,9 @@ argument the interactive spec of the original function. To interpret the spec
1225received as argument, use @code{advice-eval-interactive-spec}. 1230received as argument, use @code{advice-eval-interactive-spec}.
1226 1231
1227@var{where} determines how @var{function} is composed with the 1232@var{where} determines how @var{function} is composed with the
1228existing function. It can be one of the following: 1233existing function, e.g. whether @var{function} should be called before, or
1229 1234after the original function. See @xref{Advice combinators} for the list of
1230@table @code 1235available ways to compose the two functions.
1231@item :before
1232Call @var{function} before the old function. Both functions receive the
1233same arguments, and the return value of the composition is the return value of
1234the old function. More specifically, the composition of the two functions
1235behaves like:
1236@example
1237(lambda (&rest r) (apply @var{function} r) (apply @var{oldfun} r))
1238@end example
1239This is similar to @code{(add-hook @var{hook} @var{function})}, except that it
1240applies to single-function hooks rather than normal hooks.
1241
1242@item :after
1243Call @var{function} after the old function. Both functions receive the
1244same arguments, and the return value of the composition is the return value of
1245the old function. More specifically, the composition of the two functions
1246behaves like:
1247@example
1248(lambda (&rest r) (prog1 (apply @var{oldfun} r) (apply @var{function} r)))
1249@end example
1250This is similar to @code{(add-hook @var{hook} @var{function} nil 'append)},
1251except that it applies to single-function hooks rather than normal hooks.
1252
1253@item :override
1254This completely replaces the old function with the new one. The old function
1255can of course be recovered if you later call @code{remove-function}.
1256
1257@item :around
1258Call @var{function} instead of the old function, but provide the old function
1259as an extra argument to @var{function}. This is the most flexible composition.
1260For example, it lets you call the old function with different arguments, or
1261within a let-binding, or you can sometimes delegate the work to the old
1262function and sometimes override it completely. More specifically, the
1263composition of the two functions behaves like:
1264@example
1265(lambda (&rest r) (apply @var{function} @var{oldfun} r))
1266@end example
1267
1268@item :before-while
1269Call @var{function} before the old function and don't call the old
1270function if @var{function} returns @code{nil}. Both functions receive the
1271same arguments, and the return value of the composition is the return value of
1272the old function. More specifically, the composition of the two functions
1273behaves like:
1274@example
1275(lambda (&rest r) (and (apply @var{function} r) (apply @var{oldfun} r)))
1276@end example
1277This is reminiscent of @code{(add-hook @var{hook} @var{function})}, when
1278@var{hook} is run via @code{run-hook-with-args-until-failure}.
1279
1280@item :before-until
1281Call @var{function} before the old function and only call the old function if
1282@var{function} returns @code{nil}. More specifically, the composition of the
1283two functions behaves like:
1284@example
1285(lambda (&rest r) (or (apply @var{function} r) (apply @var{oldfun} r)))
1286@end example
1287This is reminiscent of @code{(add-hook @var{hook} @var{function})}, when
1288@var{hook} is run via @code{run-hook-with-args-until-success}.
1289
1290@item :after-while
1291Call @var{function} after the old function and only if the old function
1292returned non-@code{nil}. Both functions receive the same arguments, and the
1293return value of the composition is the return value of @var{function}.
1294More specifically, the composition of the two functions behaves like:
1295@example
1296(lambda (&rest r) (and (apply @var{oldfun} r) (apply @var{function} r)))
1297@end example
1298This is reminiscent of @code{(add-hook @var{hook} @var{function} nil 'append)},
1299when @var{hook} is run via @code{run-hook-with-args-until-failure}.
1300
1301@item :after-until
1302Call @var{function} after the old function and only if the old function
1303returned @code{nil}. More specifically, the composition of the two functions
1304behaves like:
1305@example
1306(lambda (&rest r) (or (apply @var{oldfun} r) (apply @var{function} r)))
1307@end example
1308This is reminiscent of @code{(add-hook @var{hook} @var{function} nil 'append)},
1309when @var{hook} is run via @code{run-hook-with-args-until-success}.
1310
1311@item :filter-args
1312Call @var{function} first and use the result (which should be a list) as the
1313new arguments to pass to the old function. More specifically, the composition
1314of the two functions behaves like:
1315@example
1316(lambda (&rest r) (apply @var{oldfun} (funcall @var{function} r)))
1317@end example
1318
1319@item :filter-return
1320Call the old function first and pass the result to @var{function}.
1321More specifically, the composition of the two functions behaves like:
1322@example
1323(lambda (&rest r) (funcall @var{function} (apply @var{oldfun} r)))
1324@end example
1325@end table
1326 1236
1327When modifying a variable (whose name will usually end with @code{-function}), 1237When modifying a variable (whose name will usually end with @code{-function}),
1328you can choose whether @var{function} is used globally or only in the current 1238you can choose whether @var{function} is used globally or only in the current
@@ -1343,11 +1253,22 @@ identify which function to remove. Typically used when @var{function} is an
1343anonymous function. 1253anonymous function.
1344 1254
1345@item depth 1255@item depth
1346This specifies where to place the advice, in case several advices are present. 1256This specifies how to order the advices, in case several advices are present.
1347By default, the depth is 0. A depth of 100 indicates that this advice should 1257By default, the depth is 0. A depth of 100 indicates that this advice should
1348be kept as deep as possible, whereas a depth of -100 indicates that it 1258be kept as deep as possible, whereas a depth of -100 indicates that it
1349should stay as the outermost advice. When two advices specify the same depth, 1259should stay as the outermost advice. When two advices specify the same depth,
1350the most recently added advice will be outermost. 1260the most recently added advice will be outermost.
1261
1262For a @code{:before} advice, being outermost means that this advice will be run
1263first, before any other advice, whereas being innermost means that it will run
1264right before the original function, with no other advice run between itself and
1265the original function. Similarly, for an @code{:after} advice innermost means
1266that it will run right after the original function, with no other advice run in
1267between, whereas outermost means that it will be run very last after all
1268other advices. An innermost @code{:override} advice will only override the
1269original function and other advices will apply to it, whereas an outermost
1270@code{:override} advice will override not only the original function but all
1271other advices applied to it as well.
1351@end table 1272@end table
1352@end defmac 1273@end defmac
1353 1274
@@ -1419,8 +1340,10 @@ In particular, Emacs's own source files should not put advice on
1419functions in Emacs. (There are currently a few exceptions to this 1340functions in Emacs. (There are currently a few exceptions to this
1420convention, but we aim to correct them.) 1341convention, but we aim to correct them.)
1421 1342
1422 Macros can also be advised, in much the same way as functions. 1343 Special forms (@pxref{Special Forms}) cannot be advised, however macros can
1423However, special forms (@pxref{Special Forms}) cannot be advised. 1344be advised, in much the same way as functions. Of course, this will not affect
1345code that has already been macro-expanded, so you need to make sure the advice
1346is installed before the macro is expanded.
1424 1347
1425 It is possible to advise a primitive (@pxref{What Is a Function}), 1348 It is possible to advise a primitive (@pxref{What Is a Function}),
1426but one should typically @emph{not} do so, for two reasons. Firstly, 1349but one should typically @emph{not} do so, for two reasons. Firstly,
@@ -1453,6 +1376,119 @@ Call @var{function} for every advice that was added to the named function
1453and its properties. 1376and its properties.
1454@end defun 1377@end defun
1455 1378
1379@node Advice combinators
1380@subsection Ways to compose advices
1381
1382Here are the different possible values for the @var{where} argument of
1383@code{add-function} and @code{advice-add}, specifying how the advice
1384@var{function} and the original function should be composed.
1385
1386@table @code
1387@item :before
1388Call @var{function} before the old function. Both functions receive the
1389same arguments, and the return value of the composition is the return value of
1390the old function. More specifically, the composition of the two functions
1391behaves like:
1392@example
1393(lambda (&rest r) (apply @var{function} r) (apply @var{oldfun} r))
1394@end example
1395@code{(add-function :before @var{funvar} @var{function})} is comparable for
1396single-function hooks to @code{(add-hook '@var{hookvar} @var{function})} for
1397normal hooks.
1398
1399@item :after
1400Call @var{function} after the old function. Both functions receive the
1401same arguments, and the return value of the composition is the return value of
1402the old function. More specifically, the composition of the two functions
1403behaves like:
1404@example
1405(lambda (&rest r) (prog1 (apply @var{oldfun} r) (apply @var{function} r)))
1406@end example
1407@code{(add-function :after @var{funvar} @var{function})} is comparable for
1408single-function hooks to @code{(add-hook '@var{hookvar} @var{function}
1409'append)} for normal hooks.
1410
1411@item :override
1412This completely replaces the old function with the new one. The old function
1413can of course be recovered if you later call @code{remove-function}.
1414
1415@item :around
1416Call @var{function} instead of the old function, but provide the old function
1417as an extra argument to @var{function}. This is the most flexible composition.
1418For example, it lets you call the old function with different arguments, or
1419many times, or within a let-binding, or you can sometimes delegate the work to
1420the old function and sometimes override it completely. More specifically, the
1421composition of the two functions behaves like:
1422@example
1423(lambda (&rest r) (apply @var{function} @var{oldfun} r))
1424@end example
1425
1426@item :before-while
1427Call @var{function} before the old function and don't call the old
1428function if @var{function} returns @code{nil}. Both functions receive the
1429same arguments, and the return value of the composition is the return value of
1430the old function. More specifically, the composition of the two functions
1431behaves like:
1432@example
1433(lambda (&rest r) (and (apply @var{function} r) (apply @var{oldfun} r)))
1434@end example
1435@code{(add-function :before-while @var{funvar} @var{function})} is comparable
1436for single-function hooks to @code{(add-hook '@var{hookvar} @var{function})}
1437when @var{hookvar} is run via @code{run-hook-with-args-until-failure}.
1438
1439@item :before-until
1440Call @var{function} before the old function and only call the old function if
1441@var{function} returns @code{nil}. More specifically, the composition of the
1442two functions behaves like:
1443@example
1444(lambda (&rest r) (or (apply @var{function} r) (apply @var{oldfun} r)))
1445@end example
1446@code{(add-function :before-until @var{funvar} @var{function})} is comparable
1447for single-function hooks to @code{(add-hook '@var{hookvar} @var{function})}
1448when @var{hookvar} is run via @code{run-hook-with-args-until-success}.
1449
1450@item :after-while
1451Call @var{function} after the old function and only if the old function
1452returned non-@code{nil}. Both functions receive the same arguments, and the
1453return value of the composition is the return value of @var{function}.
1454More specifically, the composition of the two functions behaves like:
1455@example
1456(lambda (&rest r) (and (apply @var{oldfun} r) (apply @var{function} r)))
1457@end example
1458@code{(add-function :after-while @var{funvar} @var{function})} is comparable
1459for single-function hooks to @code{(add-hook '@var{hookvar} @var{function}
1460'append)} when @var{hookvar} is run via
1461@code{run-hook-with-args-until-failure}.
1462
1463@item :after-until
1464Call @var{function} after the old function and only if the old function
1465returned @code{nil}. More specifically, the composition of the two functions
1466behaves like:
1467@example
1468(lambda (&rest r) (or (apply @var{oldfun} r) (apply @var{function} r)))
1469@end example
1470@code{(add-function :after-until @var{funvar} @var{function})} is comparable
1471for single-function hooks to @code{(add-hook '@var{hookvar} @var{function}
1472'append)} when @var{hookvar} is run via
1473@code{run-hook-with-args-until-success}.
1474
1475@item :filter-args
1476Call @var{function} first and use the result (which should be a list) as the
1477new arguments to pass to the old function. More specifically, the composition
1478of the two functions behaves like:
1479@example
1480(lambda (&rest r) (apply @var{oldfun} (funcall @var{function} r)))
1481@end example
1482
1483@item :filter-return
1484Call the old function first and pass the result to @var{function}.
1485More specifically, the composition of the two functions behaves like:
1486@example
1487(lambda (&rest r) (funcall @var{function} (apply @var{oldfun} r)))
1488@end example
1489@end table
1490
1491
1456@node Porting old advices 1492@node Porting old advices
1457@subsection Adapting code using the old defadvice 1493@subsection Adapting code using the old defadvice
1458 1494