diff options
| author | Stefan Monnier | 2014-03-21 17:47:52 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2014-03-21 17:47:52 -0400 |
| commit | 0c0ec041630ca71b5fd031144451e949ce0fec01 (patch) | |
| tree | 97a1517239ecb664a7d5d05b40be5df29d0dc199 | |
| parent | 1e92a8a3aa7958ba699cd0430be4f23aff6c4c01 (diff) | |
| download | emacs-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/ChangeLog | 8 | ||||
| -rw-r--r-- | doc/lispref/functions.texi | 248 |
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 @@ | |||
| 1 | 2014-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 | |||
| 1 | 2014-03-21 Martin Rudalics <rudalics@gmx.at> | 9 | 2014-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 | ||
| 1173 | This will cause the process's output to be passed first to | 1173 | This 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. | 1174 | before being passed to the original process filter. @code{my-tracing-function} |
| 1175 | When you're done with it, you can revert to the untraced behavior with: | 1175 | receives the same arguments as the original function. When you're done with |
| 1176 | it, 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 | ||
| 1194 | and when you're tired of seeing this output, you can revert to the untraced | 1195 | Here, @code{his-tracing-function} is called instead of the original function |
| 1196 | and receives the original function (additionally to that function's arguments) | ||
| 1197 | as argument, so it can call it if and when it needs to. | ||
| 1198 | When you're tired of seeing this output, you can revert to the untraced | ||
| 1195 | behavior with: | 1199 | behavior 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 | ||
| 1201 | The arguments @code{:before} and @code{:above} used in the above examples | 1205 | The arguments @code{:before} and @code{:around} used in the above examples |
| 1202 | specify how the two functions are composed, since there are many different | 1206 | specify how the two functions are composed, since there are many different |
| 1203 | ways to do it. The added function is also called an @emph{advice}. | 1207 | ways 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 | |||
| 1225 | received as argument, use @code{advice-eval-interactive-spec}. | 1230 | received 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 |
| 1228 | existing function. It can be one of the following: | 1233 | existing function, e.g. whether @var{function} should be called before, or |
| 1229 | 1234 | after the original function. See @xref{Advice combinators} for the list of | |
| 1230 | @table @code | 1235 | available ways to compose the two functions. |
| 1231 | @item :before | ||
| 1232 | Call @var{function} before the old function. Both functions receive the | ||
| 1233 | same arguments, and the return value of the composition is the return value of | ||
| 1234 | the old function. More specifically, the composition of the two functions | ||
| 1235 | behaves like: | ||
| 1236 | @example | ||
| 1237 | (lambda (&rest r) (apply @var{function} r) (apply @var{oldfun} r)) | ||
| 1238 | @end example | ||
| 1239 | This is similar to @code{(add-hook @var{hook} @var{function})}, except that it | ||
| 1240 | applies to single-function hooks rather than normal hooks. | ||
| 1241 | |||
| 1242 | @item :after | ||
| 1243 | Call @var{function} after the old function. Both functions receive the | ||
| 1244 | same arguments, and the return value of the composition is the return value of | ||
| 1245 | the old function. More specifically, the composition of the two functions | ||
| 1246 | behaves like: | ||
| 1247 | @example | ||
| 1248 | (lambda (&rest r) (prog1 (apply @var{oldfun} r) (apply @var{function} r))) | ||
| 1249 | @end example | ||
| 1250 | This is similar to @code{(add-hook @var{hook} @var{function} nil 'append)}, | ||
| 1251 | except that it applies to single-function hooks rather than normal hooks. | ||
| 1252 | |||
| 1253 | @item :override | ||
| 1254 | This completely replaces the old function with the new one. The old function | ||
| 1255 | can of course be recovered if you later call @code{remove-function}. | ||
| 1256 | |||
| 1257 | @item :around | ||
| 1258 | Call @var{function} instead of the old function, but provide the old function | ||
| 1259 | as an extra argument to @var{function}. This is the most flexible composition. | ||
| 1260 | For example, it lets you call the old function with different arguments, or | ||
| 1261 | within a let-binding, or you can sometimes delegate the work to the old | ||
| 1262 | function and sometimes override it completely. More specifically, the | ||
| 1263 | composition 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 | ||
| 1269 | Call @var{function} before the old function and don't call the old | ||
| 1270 | function if @var{function} returns @code{nil}. Both functions receive the | ||
| 1271 | same arguments, and the return value of the composition is the return value of | ||
| 1272 | the old function. More specifically, the composition of the two functions | ||
| 1273 | behaves like: | ||
| 1274 | @example | ||
| 1275 | (lambda (&rest r) (and (apply @var{function} r) (apply @var{oldfun} r))) | ||
| 1276 | @end example | ||
| 1277 | This 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 | ||
| 1281 | Call @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 | ||
| 1283 | two functions behaves like: | ||
| 1284 | @example | ||
| 1285 | (lambda (&rest r) (or (apply @var{function} r) (apply @var{oldfun} r))) | ||
| 1286 | @end example | ||
| 1287 | This 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 | ||
| 1291 | Call @var{function} after the old function and only if the old function | ||
| 1292 | returned non-@code{nil}. Both functions receive the same arguments, and the | ||
| 1293 | return value of the composition is the return value of @var{function}. | ||
| 1294 | More 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 | ||
| 1298 | This is reminiscent of @code{(add-hook @var{hook} @var{function} nil 'append)}, | ||
| 1299 | when @var{hook} is run via @code{run-hook-with-args-until-failure}. | ||
| 1300 | |||
| 1301 | @item :after-until | ||
| 1302 | Call @var{function} after the old function and only if the old function | ||
| 1303 | returned @code{nil}. More specifically, the composition of the two functions | ||
| 1304 | behaves like: | ||
| 1305 | @example | ||
| 1306 | (lambda (&rest r) (or (apply @var{oldfun} r) (apply @var{function} r))) | ||
| 1307 | @end example | ||
| 1308 | This is reminiscent of @code{(add-hook @var{hook} @var{function} nil 'append)}, | ||
| 1309 | when @var{hook} is run via @code{run-hook-with-args-until-success}. | ||
| 1310 | |||
| 1311 | @item :filter-args | ||
| 1312 | Call @var{function} first and use the result (which should be a list) as the | ||
| 1313 | new arguments to pass to the old function. More specifically, the composition | ||
| 1314 | of 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 | ||
| 1320 | Call the old function first and pass the result to @var{function}. | ||
| 1321 | More 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 | ||
| 1327 | When modifying a variable (whose name will usually end with @code{-function}), | 1237 | When modifying a variable (whose name will usually end with @code{-function}), |
| 1328 | you can choose whether @var{function} is used globally or only in the current | 1238 | you 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 | |||
| 1343 | anonymous function. | 1253 | anonymous function. |
| 1344 | 1254 | ||
| 1345 | @item depth | 1255 | @item depth |
| 1346 | This specifies where to place the advice, in case several advices are present. | 1256 | This specifies how to order the advices, in case several advices are present. |
| 1347 | By default, the depth is 0. A depth of 100 indicates that this advice should | 1257 | By default, the depth is 0. A depth of 100 indicates that this advice should |
| 1348 | be kept as deep as possible, whereas a depth of -100 indicates that it | 1258 | be kept as deep as possible, whereas a depth of -100 indicates that it |
| 1349 | should stay as the outermost advice. When two advices specify the same depth, | 1259 | should stay as the outermost advice. When two advices specify the same depth, |
| 1350 | the most recently added advice will be outermost. | 1260 | the most recently added advice will be outermost. |
| 1261 | |||
| 1262 | For a @code{:before} advice, being outermost means that this advice will be run | ||
| 1263 | first, before any other advice, whereas being innermost means that it will run | ||
| 1264 | right before the original function, with no other advice run between itself and | ||
| 1265 | the original function. Similarly, for an @code{:after} advice innermost means | ||
| 1266 | that it will run right after the original function, with no other advice run in | ||
| 1267 | between, whereas outermost means that it will be run very last after all | ||
| 1268 | other advices. An innermost @code{:override} advice will only override the | ||
| 1269 | original function and other advices will apply to it, whereas an outermost | ||
| 1270 | @code{:override} advice will override not only the original function but all | ||
| 1271 | other 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 | |||
| 1419 | functions in Emacs. (There are currently a few exceptions to this | 1340 | functions in Emacs. (There are currently a few exceptions to this |
| 1420 | convention, but we aim to correct them.) | 1341 | convention, 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 |
| 1423 | However, special forms (@pxref{Special Forms}) cannot be advised. | 1344 | be advised, in much the same way as functions. Of course, this will not affect |
| 1345 | code that has already been macro-expanded, so you need to make sure the advice | ||
| 1346 | is 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}), |
| 1426 | but one should typically @emph{not} do so, for two reasons. Firstly, | 1349 | but 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 | |||
| 1453 | and its properties. | 1376 | and its properties. |
| 1454 | @end defun | 1377 | @end defun |
| 1455 | 1378 | ||
| 1379 | @node Advice combinators | ||
| 1380 | @subsection Ways to compose advices | ||
| 1381 | |||
| 1382 | Here 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 | ||
| 1388 | Call @var{function} before the old function. Both functions receive the | ||
| 1389 | same arguments, and the return value of the composition is the return value of | ||
| 1390 | the old function. More specifically, the composition of the two functions | ||
| 1391 | behaves 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 | ||
| 1396 | single-function hooks to @code{(add-hook '@var{hookvar} @var{function})} for | ||
| 1397 | normal hooks. | ||
| 1398 | |||
| 1399 | @item :after | ||
| 1400 | Call @var{function} after the old function. Both functions receive the | ||
| 1401 | same arguments, and the return value of the composition is the return value of | ||
| 1402 | the old function. More specifically, the composition of the two functions | ||
| 1403 | behaves 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 | ||
| 1408 | single-function hooks to @code{(add-hook '@var{hookvar} @var{function} | ||
| 1409 | 'append)} for normal hooks. | ||
| 1410 | |||
| 1411 | @item :override | ||
| 1412 | This completely replaces the old function with the new one. The old function | ||
| 1413 | can of course be recovered if you later call @code{remove-function}. | ||
| 1414 | |||
| 1415 | @item :around | ||
| 1416 | Call @var{function} instead of the old function, but provide the old function | ||
| 1417 | as an extra argument to @var{function}. This is the most flexible composition. | ||
| 1418 | For example, it lets you call the old function with different arguments, or | ||
| 1419 | many times, or within a let-binding, or you can sometimes delegate the work to | ||
| 1420 | the old function and sometimes override it completely. More specifically, the | ||
| 1421 | composition 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 | ||
| 1427 | Call @var{function} before the old function and don't call the old | ||
| 1428 | function if @var{function} returns @code{nil}. Both functions receive the | ||
| 1429 | same arguments, and the return value of the composition is the return value of | ||
| 1430 | the old function. More specifically, the composition of the two functions | ||
| 1431 | behaves 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 | ||
| 1436 | for single-function hooks to @code{(add-hook '@var{hookvar} @var{function})} | ||
| 1437 | when @var{hookvar} is run via @code{run-hook-with-args-until-failure}. | ||
| 1438 | |||
| 1439 | @item :before-until | ||
| 1440 | Call @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 | ||
| 1442 | two 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 | ||
| 1447 | for single-function hooks to @code{(add-hook '@var{hookvar} @var{function})} | ||
| 1448 | when @var{hookvar} is run via @code{run-hook-with-args-until-success}. | ||
| 1449 | |||
| 1450 | @item :after-while | ||
| 1451 | Call @var{function} after the old function and only if the old function | ||
| 1452 | returned non-@code{nil}. Both functions receive the same arguments, and the | ||
| 1453 | return value of the composition is the return value of @var{function}. | ||
| 1454 | More 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 | ||
| 1459 | for 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 | ||
| 1464 | Call @var{function} after the old function and only if the old function | ||
| 1465 | returned @code{nil}. More specifically, the composition of the two functions | ||
| 1466 | behaves 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 | ||
| 1471 | for 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 | ||
| 1476 | Call @var{function} first and use the result (which should be a list) as the | ||
| 1477 | new arguments to pass to the old function. More specifically, the composition | ||
| 1478 | of 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 | ||
| 1484 | Call the old function first and pass the result to @var{function}. | ||
| 1485 | More 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 | ||