aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorGlenn Morris2012-10-28 18:29:22 -0700
committerGlenn Morris2012-10-28 18:29:22 -0700
commitd571e9c385d648a90581e2c5cd1a883b8c0ea0e4 (patch)
treef99e49b26363c7d865c56a1b0169f565109c4c7b /doc
parenta3c5b619fc4c1334c4d295def008318ae0e9bca7 (diff)
downloademacs-d571e9c385d648a90581e2c5cd1a883b8c0ea0e4.tar.gz
emacs-d571e9c385d648a90581e2c5cd1a883b8c0ea0e4.zip
Move some cl.texi setf extensions to the Obsolete appendix
* doc/misc/cl.texi (Customizing Setf): Move contents to "Obsolete Setf Customization". (Modify Macros, Multiple Values): Remove mentions of obsolete feature.
Diffstat (limited to 'doc')
-rw-r--r--doc/misc/ChangeLog2
-rw-r--r--doc/misc/cl.texi425
2 files changed, 205 insertions, 222 deletions
diff --git a/doc/misc/ChangeLog b/doc/misc/ChangeLog
index 9aeaf87426e..e85e18ff5c7 100644
--- a/doc/misc/ChangeLog
+++ b/doc/misc/ChangeLog
@@ -2,6 +2,8 @@
2 2
3 * cl.texi (Organization): More details on cl-lib.el versus cl.el. 3 * cl.texi (Organization): More details on cl-lib.el versus cl.el.
4 (Setf Extensions): Remove `apply' setf since it seems to be disabled. 4 (Setf Extensions): Remove `apply' setf since it seems to be disabled.
5 (Customizing Setf): Move contents to "Obsolete Setf Customization".
6 (Modify Macros, Multiple Values): Remove mentions of obsolete feature.
5 7
62012-10-28 Glenn Morris <rgm@gnu.org> 82012-10-28 Glenn Morris <rgm@gnu.org>
7 9
diff --git a/doc/misc/cl.texi b/doc/misc/cl.texi
index b77dfcd94be..086cc25fbcc 100644
--- a/doc/misc/cl.texi
+++ b/doc/misc/cl.texi
@@ -883,7 +883,6 @@ generalized variables.
883@menu 883@menu
884* Setf Extensions:: Additional @code{setf} places. 884* Setf Extensions:: Additional @code{setf} places.
885* Modify Macros:: @code{cl-incf}, @code{cl-rotatef}, @code{letf}, @code{cl-callf}, etc. 885* Modify Macros:: @code{cl-incf}, @code{cl-rotatef}, @code{letf}, @code{cl-callf}, etc.
886* Customizing Setf:: @code{define-modify-macro}, @code{defsetf}, @code{define-setf-method}.
887@end menu 886@end menu
888 887
889@node Setf Extensions 888@node Setf Extensions
@@ -1198,9 +1197,7 @@ Some more examples:
1198(cl-callf cl-union happy-people (list joe bob) :test 'same-person) 1197(cl-callf cl-union happy-people (list joe bob) :test 'same-person)
1199@end example 1198@end example
1200 1199
1201@xref{Customizing Setf}, for @code{define-modify-macro}, a way 1200Note again that @code{cl-callf} is an extension to standard Common Lisp.
1202to create even more concise notations for modify macros. Note
1203again that @code{cl-callf} is an extension to standard Common Lisp.
1204@end defmac 1201@end defmac
1205 1202
1206@defmac cl-callf2 @var{function} @var{arg1} @var{place} @var{args}@dots{} 1203@defmac cl-callf2 @var{function} @var{arg1} @var{place} @var{args}@dots{}
@@ -1216,214 +1213,6 @@ blocks for other macros like @code{cl-incf}, @code{cl-pushnew}, and
1216macros are used in the processing of symbol macros; 1213macros are used in the processing of symbol macros;
1217@pxref{Macro Bindings}. 1214@pxref{Macro Bindings}.
1218 1215
1219@node Customizing Setf
1220@subsection Customizing Setf
1221
1222@noindent
1223Common Lisp defines three macros, @code{define-modify-macro},
1224@code{defsetf}, and @code{define-setf-method}, that allow the
1225user to extend generalized variables in various ways.
1226
1227@defmac define-modify-macro name arglist function [doc-string]
1228This macro defines a ``read-modify-write'' macro similar to
1229@code{cl-incf} and @code{cl-decf}. The macro @var{name} is defined
1230to take a @var{place} argument followed by additional arguments
1231described by @var{arglist}. The call
1232
1233@example
1234(@var{name} @var{place} @var{args}...)
1235@end example
1236
1237@noindent
1238will be expanded to
1239
1240@example
1241(cl-callf @var{func} @var{place} @var{args}...)
1242@end example
1243
1244@noindent
1245which in turn is roughly equivalent to
1246
1247@example
1248(setf @var{place} (@var{func} @var{place} @var{args}...))
1249@end example
1250
1251For example:
1252
1253@example
1254(define-modify-macro cl-incf (&optional (n 1)) +)
1255(define-modify-macro cl-concatf (&rest args) concat)
1256@end example
1257
1258Note that @code{&key} is not allowed in @var{arglist}, but
1259@code{&rest} is sufficient to pass keywords on to the function.
1260
1261Most of the modify macros defined by Common Lisp do not exactly
1262follow the pattern of @code{define-modify-macro}. For example,
1263@code{push} takes its arguments in the wrong order, and @code{pop}
1264is completely irregular. You can define these macros ``by hand''
1265using @code{get-setf-method}, or consult the source
1266to see how to use the internal @code{setf} building blocks.
1267@end defmac
1268
1269@defmac defsetf access-fn update-fn
1270This is the simpler of two @code{defsetf} forms. Where
1271@var{access-fn} is the name of a function which accesses a place,
1272this declares @var{update-fn} to be the corresponding store
1273function. From now on,
1274
1275@example
1276(setf (@var{access-fn} @var{arg1} @var{arg2} @var{arg3}) @var{value})
1277@end example
1278
1279@noindent
1280will be expanded to
1281
1282@example
1283(@var{update-fn} @var{arg1} @var{arg2} @var{arg3} @var{value})
1284@end example
1285
1286@noindent
1287The @var{update-fn} is required to be either a true function, or
1288a macro which evaluates its arguments in a function-like way. Also,
1289the @var{update-fn} is expected to return @var{value} as its result.
1290Otherwise, the above expansion would not obey the rules for the way
1291@code{setf} is supposed to behave.
1292
1293As a special (non-Common-Lisp) extension, a third argument of @code{t}
1294to @code{defsetf} says that the @code{update-fn}'s return value is
1295not suitable, so that the above @code{setf} should be expanded to
1296something more like
1297
1298@example
1299(let ((temp @var{value}))
1300 (@var{update-fn} @var{arg1} @var{arg2} @var{arg3} temp)
1301 temp)
1302@end example
1303
1304Some examples of the use of @code{defsetf}, drawn from the standard
1305suite of setf methods, are:
1306
1307@example
1308(defsetf car setcar)
1309(defsetf symbol-value set)
1310(defsetf buffer-name rename-buffer t)
1311@end example
1312@end defmac
1313
1314@defmac defsetf access-fn arglist (store-var) forms@dots{}
1315This is the second, more complex, form of @code{defsetf}. It is
1316rather like @code{defmacro} except for the additional @var{store-var}
1317argument. The @var{forms} should return a Lisp form which stores
1318the value of @var{store-var} into the generalized variable formed
1319by a call to @var{access-fn} with arguments described by @var{arglist}.
1320The @var{forms} may begin with a string which documents the @code{setf}
1321method (analogous to the doc string that appears at the front of a
1322function).
1323
1324For example, the simple form of @code{defsetf} is shorthand for
1325
1326@example
1327(defsetf @var{access-fn} (&rest args) (store)
1328 (append '(@var{update-fn}) args (list store)))
1329@end example
1330
1331The Lisp form that is returned can access the arguments from
1332@var{arglist} and @var{store-var} in an unrestricted fashion;
1333macros like @code{setf} and @code{cl-incf} which invoke this
1334setf-method will insert temporary variables as needed to make
1335sure the apparent order of evaluation is preserved.
1336
1337Another example drawn from the standard package:
1338
1339@example
1340(defsetf nth (n x) (store)
1341 (list 'setcar (list 'nthcdr n x) store))
1342@end example
1343@end defmac
1344
1345@defmac define-setf-method access-fn arglist forms@dots{}
1346This is the most general way to create new place forms. When
1347a @code{setf} to @var{access-fn} with arguments described by
1348@var{arglist} is expanded, the @var{forms} are evaluated and
1349must return a list of five items:
1350
1351@enumerate
1352@item
1353A list of @dfn{temporary variables}.
1354
1355@item
1356A list of @dfn{value forms} corresponding to the temporary variables
1357above. The temporary variables will be bound to these value forms
1358as the first step of any operation on the generalized variable.
1359
1360@item
1361A list of exactly one @dfn{store variable} (generally obtained
1362from a call to @code{gensym}).
1363
1364@item
1365A Lisp form which stores the contents of the store variable into
1366the generalized variable, assuming the temporaries have been
1367bound as described above.
1368
1369@item
1370A Lisp form which accesses the contents of the generalized variable,
1371assuming the temporaries have been bound.
1372@end enumerate
1373
1374This is exactly like the Common Lisp macro of the same name,
1375except that the method returns a list of five values rather
1376than the five values themselves, since Emacs Lisp does not
1377support Common Lisp's notion of multiple return values.
1378
1379Once again, the @var{forms} may begin with a documentation string.
1380
1381A setf-method should be maximally conservative with regard to
1382temporary variables. In the setf-methods generated by
1383@code{defsetf}, the second return value is simply the list of
1384arguments in the place form, and the first return value is a
1385list of a corresponding number of temporary variables generated
1386by @code{cl-gensym}. Macros like @code{setf} and @code{cl-incf} which
1387use this setf-method will optimize away most temporaries that
1388turn out to be unnecessary, so there is little reason for the
1389setf-method itself to optimize.
1390@end defmac
1391
1392@defun get-setf-method place &optional env
1393This function returns the setf-method for @var{place}, by
1394invoking the definition previously recorded by @code{defsetf}
1395or @code{define-setf-method}. The result is a list of five
1396values as described above. You can use this function to build
1397your own @code{cl-incf}-like modify macros. (Actually, it is
1398@c FIXME?
1399better to use the internal functions @code{cl-setf-do-modify}
1400and @code{cl-setf-do-store}, which are a bit easier to use and
1401which also do a number of optimizations; consult the source
1402code for the @code{cl-incf} function for a simple example.)
1403
1404The argument @var{env} specifies the ``environment'' to be
1405passed on to @code{macroexpand} if @code{get-setf-method} should
1406need to expand a macro in @var{place}. It should come from
1407an @code{&environment} argument to the macro or setf-method
1408that called @code{get-setf-method}.
1409
1410See also the source code for the setf-methods for @code{apply}
1411and @code{substring}, each of which works by calling
1412@code{get-setf-method} on a simpler case, then massaging
1413the result in various ways.
1414@end defun
1415
1416Modern Common Lisp defines a second, independent way to specify
1417the @code{setf} behavior of a function, namely ``@code{setf}
1418functions'' whose names are lists @code{(setf @var{name})}
1419rather than symbols. For example, @code{(defun (setf foo) @dots{})}
1420defines the function that is used when @code{setf} is applied to
1421@code{foo}. This package does not currently support @code{setf}
1422functions. In particular, it is a compile-time error to use
1423@code{setf} on a form which has not already been @code{defsetf}'d
1424or otherwise declared; in newer Common Lisps, this would not be
1425an error since the function @code{(setf @var{func})} might be
1426defined later.
1427 1216
1428@node Variable Bindings 1217@node Variable Bindings
1429@section Variable Bindings 1218@section Variable Bindings
@@ -2672,8 +2461,8 @@ functions, by contrast, always return exactly one result. This
2672package makes no attempt to emulate Common Lisp multiple return 2461package makes no attempt to emulate Common Lisp multiple return
2673values; Emacs versions of Common Lisp functions that return more 2462values; Emacs versions of Common Lisp functions that return more
2674than one value either return just the first value (as in 2463than one value either return just the first value (as in
2675@code{cl-compiler-macroexpand}) or return a list of values (as in 2464@code{cl-compiler-macroexpand}) or return a list of values.
2676@code{get-setf-method}). This package @emph{does} define placeholders 2465This package @emph{does} define placeholders
2677for the Common Lisp functions that work with multiple values, but 2466for the Common Lisp functions that work with multiple values, but
2678in Emacs Lisp these functions simply operate on lists instead. 2467in Emacs Lisp these functions simply operate on lists instead.
2679The @code{cl-values} form, for example, is a synonym for @code{list} 2468The @code{cl-values} form, for example, is a synonym for @code{list}
@@ -5088,22 +4877,214 @@ Replaced by @code{cl-letf} (@pxref{Modify Macros}).
5088@node Obsolete Setf Customization 4877@node Obsolete Setf Customization
5089@appendixsec Obsolete Ways to Customize Setf 4878@appendixsec Obsolete Ways to Customize Setf
5090 4879
5091This section describes some obsolete ways to extend @code{setf}. 4880Common Lisp defines three macros, @code{define-modify-macro},
5092They are replaced by @file{gv.el} in Emacs 24.3. 4881@code{defsetf}, and @code{define-setf-method}, that allow the
4882user to extend generalized variables in various ways.
4883In Emacs, these are obsolete, replaced by various features of
4884@file{gv.el} in Emacs 24.3.
4885@c FIXME details.
4886
4887@defmac define-modify-macro name arglist function [doc-string]
4888This macro defines a ``read-modify-write'' macro similar to
4889@code{cl-incf} and @code{cl-decf}. The macro @var{name} is defined
4890to take a @var{place} argument followed by additional arguments
4891described by @var{arglist}. The call
4892
4893@example
4894(@var{name} @var{place} @var{args}...)
4895@end example
4896
4897@noindent
4898will be expanded to
5093 4899
5094@c FIXME. 4900@example
5095@defmac define-setf-method name arglist &rest body 4901(cl-callf @var{func} @var{place} @var{args}...)
5096Use @file{gv.el}. 4902@end example
4903
4904@noindent
4905which in turn is roughly equivalent to
4906
4907@example
4908(setf @var{place} (@var{func} @var{place} @var{args}...))
4909@end example
4910
4911For example:
4912
4913@example
4914(define-modify-macro cl-incf (&optional (n 1)) +)
4915(define-modify-macro cl-concatf (&rest args) concat)
4916@end example
4917
4918Note that @code{&key} is not allowed in @var{arglist}, but
4919@code{&rest} is sufficient to pass keywords on to the function.
4920
4921Most of the modify macros defined by Common Lisp do not exactly
4922follow the pattern of @code{define-modify-macro}. For example,
4923@code{push} takes its arguments in the wrong order, and @code{pop}
4924is completely irregular. You can define these macros ``by hand''
4925using @code{get-setf-method}, or consult the source
4926to see how to use the internal @code{setf} building blocks.
5097@end defmac 4927@end defmac
5098 4928
5099@defmac defsetf access-fn update-fn 4929@defmac defsetf access-fn update-fn
5100Use @file{gv.el}. 4930This is the simpler of two @code{defsetf} forms. Where
4931@var{access-fn} is the name of a function which accesses a place,
4932this declares @var{update-fn} to be the corresponding store
4933function. From now on,
4934
4935@example
4936(setf (@var{access-fn} @var{arg1} @var{arg2} @var{arg3}) @var{value})
4937@end example
4938
4939@noindent
4940will be expanded to
4941
4942@example
4943(@var{update-fn} @var{arg1} @var{arg2} @var{arg3} @var{value})
4944@end example
4945
4946@noindent
4947The @var{update-fn} is required to be either a true function, or
4948a macro which evaluates its arguments in a function-like way. Also,
4949the @var{update-fn} is expected to return @var{value} as its result.
4950Otherwise, the above expansion would not obey the rules for the way
4951@code{setf} is supposed to behave.
4952
4953As a special (non-Common-Lisp) extension, a third argument of @code{t}
4954to @code{defsetf} says that the @code{update-fn}'s return value is
4955not suitable, so that the above @code{setf} should be expanded to
4956something more like
4957
4958@example
4959(let ((temp @var{value}))
4960 (@var{update-fn} @var{arg1} @var{arg2} @var{arg3} temp)
4961 temp)
4962@end example
4963
4964Some examples of the use of @code{defsetf}, drawn from the standard
4965suite of setf methods, are:
4966
4967@example
4968(defsetf car setcar)
4969(defsetf symbol-value set)
4970(defsetf buffer-name rename-buffer t)
4971@end example
5101@end defmac 4972@end defmac
5102 4973
5103@defmac define-modify-macro name arglist function [doc-string] 4974@defmac defsetf access-fn arglist (store-var) forms@dots{}
5104Use @file{gv.el}. 4975This is the second, more complex, form of @code{defsetf}. It is
4976rather like @code{defmacro} except for the additional @var{store-var}
4977argument. The @var{forms} should return a Lisp form which stores
4978the value of @var{store-var} into the generalized variable formed
4979by a call to @var{access-fn} with arguments described by @var{arglist}.
4980The @var{forms} may begin with a string which documents the @code{setf}
4981method (analogous to the doc string that appears at the front of a
4982function).
4983
4984For example, the simple form of @code{defsetf} is shorthand for
4985
4986@example
4987(defsetf @var{access-fn} (&rest args) (store)
4988 (append '(@var{update-fn}) args (list store)))
4989@end example
4990
4991The Lisp form that is returned can access the arguments from
4992@var{arglist} and @var{store-var} in an unrestricted fashion;
4993macros like @code{setf} and @code{cl-incf} which invoke this
4994setf-method will insert temporary variables as needed to make
4995sure the apparent order of evaluation is preserved.
4996
4997Another example drawn from the standard package:
4998
4999@example
5000(defsetf nth (n x) (store)
5001 (list 'setcar (list 'nthcdr n x) store))
5002@end example
5105@end defmac 5003@end defmac
5106 5004
5005@defmac define-setf-method access-fn arglist forms@dots{}
5006This is the most general way to create new place forms. When
5007a @code{setf} to @var{access-fn} with arguments described by
5008@var{arglist} is expanded, the @var{forms} are evaluated and
5009must return a list of five items:
5010
5011@enumerate
5012@item
5013A list of @dfn{temporary variables}.
5014
5015@item
5016A list of @dfn{value forms} corresponding to the temporary variables
5017above. The temporary variables will be bound to these value forms
5018as the first step of any operation on the generalized variable.
5019
5020@item
5021A list of exactly one @dfn{store variable} (generally obtained
5022from a call to @code{gensym}).
5023
5024@item
5025A Lisp form which stores the contents of the store variable into
5026the generalized variable, assuming the temporaries have been
5027bound as described above.
5028
5029@item
5030A Lisp form which accesses the contents of the generalized variable,
5031assuming the temporaries have been bound.
5032@end enumerate
5033
5034This is exactly like the Common Lisp macro of the same name,
5035except that the method returns a list of five values rather
5036than the five values themselves, since Emacs Lisp does not
5037support Common Lisp's notion of multiple return values.
5038
5039Once again, the @var{forms} may begin with a documentation string.
5040
5041A setf-method should be maximally conservative with regard to
5042temporary variables. In the setf-methods generated by
5043@code{defsetf}, the second return value is simply the list of
5044arguments in the place form, and the first return value is a
5045list of a corresponding number of temporary variables generated
5046by @code{cl-gensym}. Macros like @code{setf} and @code{cl-incf} which
5047use this setf-method will optimize away most temporaries that
5048turn out to be unnecessary, so there is little reason for the
5049setf-method itself to optimize.
5050@end defmac
5051
5052@defun get-setf-method place &optional env
5053This function returns the setf-method for @var{place}, by
5054invoking the definition previously recorded by @code{defsetf}
5055or @code{define-setf-method}. The result is a list of five
5056values as described above. You can use this function to build
5057your own @code{cl-incf}-like modify macros. (Actually, it is
5058@c FIXME?
5059better to use the internal functions @code{cl-setf-do-modify}
5060and @code{cl-setf-do-store}, which are a bit easier to use and
5061which also do a number of optimizations; consult the source
5062code for the @code{cl-incf} function for a simple example.)
5063
5064The argument @var{env} specifies the ``environment'' to be
5065passed on to @code{macroexpand} if @code{get-setf-method} should
5066need to expand a macro in @var{place}. It should come from
5067an @code{&environment} argument to the macro or setf-method
5068that called @code{get-setf-method}.
5069
5070See also the source code for the setf-methods for @code{apply}
5071and @code{substring}, each of which works by calling
5072@code{get-setf-method} on a simpler case, then massaging
5073the result in various ways.
5074@end defun
5075
5076Modern Common Lisp defines a second, independent way to specify
5077the @code{setf} behavior of a function, namely ``@code{setf}
5078functions'' whose names are lists @code{(setf @var{name})}
5079rather than symbols. For example, @code{(defun (setf foo) @dots{})}
5080defines the function that is used when @code{setf} is applied to
5081@code{foo}. This package does not currently support @code{setf}
5082functions. In particular, it is a compile-time error to use
5083@code{setf} on a form which has not already been @code{defsetf}'d
5084or otherwise declared; in newer Common Lisps, this would not be
5085an error since the function @code{(setf @var{func})} might be
5086defined later.
5087
5107 5088
5108@node GNU Free Documentation License 5089@node GNU Free Documentation License
5109@appendix GNU Free Documentation License 5090@appendix GNU Free Documentation License