aboutsummaryrefslogtreecommitdiffstats
path: root/doc/misc
diff options
context:
space:
mode:
authorStefan Kangas2025-02-22 17:32:31 +0100
committerStefan Kangas2025-02-23 00:38:22 +0100
commit95fee880e45184f4820e9704b75887ef2d91bd01 (patch)
tree09afd4a8d6f9ccf0f2cec36e4f604deaf040c728 /doc/misc
parent44a1c4a9aea54d6542bcf0c231b080f0ed023229 (diff)
downloademacs-95fee880e45184f4820e9704b75887ef2d91bd01.tar.gz
emacs-95fee880e45184f4820e9704b75887ef2d91bd01.zip
New macros incf and decf
* lisp/emacs-lisp/cl-lib.el (cl-incf, cl-decf): Move macros from here... * lisp/emacs-lisp/gv.el (incf, decf): ...to here. Make old names into aliases, documented as deprecated. * lisp/obsolete/cl.el: Don't alias incf and decf. * test/lisp/emacs-lisp/cl-lib-tests.el (cl-lib-test-incf) (cl-lib-test-decf): Move tests from here... * test/lisp/emacs-lisp/gv-tests.el (gv-incf, gv-decf): ...to here. * doc/lispref/numbers.texi (Arithmetic Operations): * lisp/emacs-lisp/shortdoc.el (number): Document incf and decf. * doc/lispref/variables.texi (Multisession Variables): * doc/misc/cl.texi (Organization, Modify Macros, Modify Macros) (Modify Macros, Macro Bindings, For Clauses, Property Lists) (Structures, Efficiency Concerns, Obsolete Setf Customization): Delete cl-incf and cl-decf documentation, moving any relevant parts to lispref. Delete some parts that seem to primarily regard implementation details that do not warrant inclusion in lispref. Update all examples to use incf/decf.
Diffstat (limited to 'doc/misc')
-rw-r--r--doc/misc/cl.texi112
1 files changed, 22 insertions, 90 deletions
diff --git a/doc/misc/cl.texi b/doc/misc/cl.texi
index f95900a085e..8fb308e64a5 100644
--- a/doc/misc/cl.texi
+++ b/doc/misc/cl.texi
@@ -168,9 +168,6 @@ and information about the package. This file is relatively compact.
168 168
169@item cl-extra.el 169@item cl-extra.el
170This file contains the larger, more complex or unusual functions. 170This file contains the larger, more complex or unusual functions.
171It is kept separate so that packages which only want to use Common
172Lisp fundamentals like the @code{cl-incf} function won't need to pay
173the overhead of loading the more advanced functions.
174 171
175@item cl-seq.el 172@item cl-seq.el
176This file contains most of the advanced functions for operating 173This file contains most of the advanced functions for operating
@@ -197,8 +194,8 @@ this package prior to Emacs 24.3. Nowadays, it is replaced by
197but use different function names (in fact, @file{cl.el} mainly just 194but use different function names (in fact, @file{cl.el} mainly just
198defines aliases to the @file{cl-lib.el} definitions). Where 195defines aliases to the @file{cl-lib.el} definitions). Where
199@file{cl-lib.el} defines a function called, for example, 196@file{cl-lib.el} defines a function called, for example,
200@code{cl-incf}, @file{cl.el} uses the same name but without the 197@code{cl-first}, @file{cl.el} uses the same name but without the
201@samp{cl-} prefix, e.g., @code{incf} in this example. There are a few 198@samp{cl-} prefix, e.g., @code{first} in this example. There are a few
202exceptions to this. First, functions such as @code{cl-defun} where 199exceptions to this. First, functions such as @code{cl-defun} where
203the unprefixed version was already used for a standard Emacs Lisp 200the unprefixed version was already used for a standard Emacs Lisp
204function. In such cases, the @file{cl.el} version adds a @samp{*} 201function. In such cases, the @file{cl.el} version adds a @samp{*}
@@ -1028,7 +1025,7 @@ generalized variables.
1028 1025
1029@menu 1026@menu
1030* Setf Extensions:: Additional @code{setf} places. 1027* Setf Extensions:: Additional @code{setf} places.
1031* Modify Macros:: @code{cl-incf}, @code{cl-rotatef}, @code{cl-letf}, @code{cl-callf}, etc. 1028* Modify Macros:: @code{cl-rotatef}, @code{cl-letf}, @code{cl-callf}, etc.
1032@end menu 1029@end menu
1033 1030
1034@node Setf Extensions 1031@node Setf Extensions
@@ -1085,52 +1082,6 @@ Specifically, all subforms are evaluated from left to right, then
1085all the assignments are done (in an undefined order). 1082all the assignments are done (in an undefined order).
1086@end defmac 1083@end defmac
1087 1084
1088@defmac cl-incf place &optional x
1089This macro increments the number stored in @var{place} by one, or
1090by @var{x} if specified. The incremented value is returned. For
1091example, @code{(cl-incf i)} is equivalent to @code{(setq i (1+ i))}, and
1092@code{(cl-incf (car x) 2)} is equivalent to @code{(setcar x (+ (car x) 2))}.
1093
1094As with @code{setf}, care is taken to preserve the ``apparent'' order
1095of evaluation. For example,
1096
1097@example
1098(cl-incf (aref vec (cl-incf i)))
1099@end example
1100
1101@noindent
1102appears to increment @code{i} once, then increment the element of
1103@code{vec} addressed by @code{i}; this is indeed exactly what it
1104does, which means the above form is @emph{not} equivalent to the
1105``obvious'' expansion,
1106
1107@example
1108(setf (aref vec (cl-incf i))
1109 (1+ (aref vec (cl-incf i)))) ; wrong!
1110@end example
1111
1112@noindent
1113but rather to something more like
1114
1115@example
1116(let ((temp (cl-incf i)))
1117 (setf (aref vec temp) (1+ (aref vec temp))))
1118@end example
1119
1120@noindent
1121Again, all of this is taken care of automatically by @code{cl-incf} and
1122the other generalized-variable macros.
1123
1124As a more Emacs-specific example of @code{cl-incf}, the expression
1125@code{(cl-incf (point) @var{n})} is essentially equivalent to
1126@code{(forward-char @var{n})}.
1127@end defmac
1128
1129@defmac cl-decf place &optional x
1130This macro decrements the number stored in @var{place} by one, or
1131by @var{x} if specified.
1132@end defmac
1133
1134@defmac cl-pushnew x place @t{&key :test :test-not :key} 1085@defmac cl-pushnew x place @t{&key :test :test-not :key}
1135This macro inserts @var{x} at the front of the list stored in 1086This macro inserts @var{x} at the front of the list stored in
1136@var{place}, but only if @var{x} isn't present in the list already. 1087@var{place}, but only if @var{x} isn't present in the list already.
@@ -1243,8 +1194,8 @@ It does the bindings in sequential rather than parallel order.
1243This is the ``generic'' modify macro. It calls @var{function}, 1194This is the ``generic'' modify macro. It calls @var{function},
1244which should be an unquoted function name, macro name, or lambda. 1195which should be an unquoted function name, macro name, or lambda.
1245It passes @var{place} and @var{args} as arguments, and assigns the 1196It passes @var{place} and @var{args} as arguments, and assigns the
1246result back to @var{place}. For example, @code{(cl-incf @var{place} 1197result back to @var{place}. For example, @code{(incf @var{place}
1247@var{n})} is the same as @code{(cl-callf + @var{place} @var{n})}. 1198@var{n})} could be implemented as @code{(cl-callf + @var{place} @var{n})}.
1248Some more examples: 1199Some more examples:
1249 1200
1250@example 1201@example
@@ -1264,7 +1215,7 @@ equivalent to @code{(cl-callf2 cons @var{x} @var{place})}.
1264@end defmac 1215@end defmac
1265 1216
1266The @code{cl-callf} and @code{cl-callf2} macros serve as building 1217The @code{cl-callf} and @code{cl-callf2} macros serve as building
1267blocks for other macros like @code{cl-incf}, and @code{cl-pushnew}. 1218blocks for other macros like @code{cl-pushnew}.
1268The @code{cl-letf} and @code{cl-letf*} macros are used in the processing 1219The @code{cl-letf} and @code{cl-letf*} macros are used in the processing
1269of symbol macros; @pxref{Macro Bindings}. 1220of symbol macros; @pxref{Macro Bindings}.
1270 1221
@@ -1401,7 +1352,7 @@ replaced by @var{expansion}.
1401@example 1352@example
1402(setq bar '(5 . 9)) 1353(setq bar '(5 . 9))
1403(cl-symbol-macrolet ((foo (car bar))) 1354(cl-symbol-macrolet ((foo (car bar)))
1404 (cl-incf foo)) 1355 (incf foo))
1405bar 1356bar
1406 @result{} (6 . 9) 1357 @result{} (6 . 9)
1407@end example 1358@end example
@@ -1426,7 +1377,7 @@ expansion of another macro:
1426 body)))) 1377 body))))
1427 1378
1428(setq mylist '(1 2 3 4)) 1379(setq mylist '(1 2 3 4))
1429(my-dolist (x mylist) (cl-incf x)) 1380(my-dolist (x mylist) (incf x))
1430mylist 1381mylist
1431 @result{} (2 3 4 5) 1382 @result{} (2 3 4 5)
1432@end example 1383@end example
@@ -1440,14 +1391,14 @@ shown here expands to
1440@example 1391@example
1441(cl-loop for G1234 on mylist do 1392(cl-loop for G1234 on mylist do
1442 (cl-symbol-macrolet ((x (car G1234))) 1393 (cl-symbol-macrolet ((x (car G1234)))
1443 (cl-incf x))) 1394 (incf x)))
1444@end example 1395@end example
1445 1396
1446@noindent 1397@noindent
1447which in turn expands to 1398which in turn expands to
1448 1399
1449@example 1400@example
1450(cl-loop for G1234 on mylist do (cl-incf (car G1234))) 1401(cl-loop for G1234 on mylist do (incf (car G1234)))
1451@end example 1402@end example
1452 1403
1453@xref{Loop Facility}, for a description of the @code{cl-loop} macro. 1404@xref{Loop Facility}, for a description of the @code{cl-loop} macro.
@@ -1999,7 +1950,7 @@ a @code{setf}-able ``reference'' onto the elements of the list
1999rather than just a temporary variable. For example, 1950rather than just a temporary variable. For example,
2000 1951
2001@example 1952@example
2002(cl-loop for x in-ref my-list do (cl-incf x)) 1953(cl-loop for x in-ref my-list do (incf x))
2003@end example 1954@end example
2004 1955
2005@noindent 1956@noindent
@@ -2940,7 +2891,7 @@ The @code{get} and @code{cl-get} functions are also @code{setf}-able.
2940The fact that @code{default} is ignored can sometimes be useful: 2891The fact that @code{default} is ignored can sometimes be useful:
2941 2892
2942@example 2893@example
2943(cl-incf (cl-get 'foo 'usage-count 0)) 2894(incf (cl-get 'foo 'usage-count 0))
2944@end example 2895@end example
2945 2896
2946Here, symbol @code{foo}'s @code{usage-count} property is incremented 2897Here, symbol @code{foo}'s @code{usage-count} property is incremented
@@ -4051,7 +4002,7 @@ calling @code{(person-first-name @var{p})}, @code{(person-age
4051slots by using @code{setf} on any of these place forms, for example: 4002slots by using @code{setf} on any of these place forms, for example:
4052 4003
4053@example 4004@example
4054(cl-incf (person-age birthday-boy)) 4005(incf (person-age birthday-boy))
4055@end example 4006@end example
4056 4007
4057You can create a new @code{person} by calling @code{make-person}, 4008You can create a new @code{person} by calling @code{make-person},
@@ -4459,29 +4410,14 @@ user to modify @var{place}.
4459Many of the advanced features of this package, such as @code{cl-defun}, 4410Many of the advanced features of this package, such as @code{cl-defun},
4460@code{cl-loop}, etc., are implemented as Lisp macros. In 4411@code{cl-loop}, etc., are implemented as Lisp macros. In
4461byte-compiled code, these complex notations will be expanded into 4412byte-compiled code, these complex notations will be expanded into
4462equivalent Lisp code which is simple and efficient. For example, 4413equivalent Lisp code which is simple and efficient. Thus, there is no
4463the form 4414performance penalty for using the more readable form in your compiled
4464 4415code.
4465@example
4466(cl-incf i n)
4467@end example
4468
4469@noindent
4470is expanded at compile-time to the Lisp form
4471
4472@example
4473(setq i (+ i n))
4474@end example
4475
4476@noindent
4477which is the most efficient way of doing this operation
4478in Lisp. Thus, there is no performance penalty for using the more
4479readable @code{cl-incf} form in your compiled code.
4480 4416
4481@emph{Interpreted} code, on the other hand, must expand these macros 4417@emph{Interpreted} code, on the other hand, must expand these macros
4482every time they are executed. For this reason it is strongly 4418every time they are executed. For this reason it is strongly
4483recommended that code making heavy use of macros be compiled. 4419recommended that code making heavy use of macros be compiled.
4484A loop using @code{cl-incf} a hundred times will execute considerably 4420A loop using @code{cl-first} a hundred times will execute considerably
4485faster if compiled, and will also garbage-collect less because the 4421faster if compiled, and will also garbage-collect less because the
4486macro expansion will not have to be generated, used, and thrown away a 4422macro expansion will not have to be generated, used, and thrown away a
4487hundred times. 4423hundred times.
@@ -4907,7 +4843,7 @@ call to @code{make-adder} itself.
4907@example 4843@example
4908(defun make-counter () 4844(defun make-counter ()
4909 (lexical-let ((n 0)) 4845 (lexical-let ((n 0))
4910 (cl-function (lambda (&optional (m 1)) (cl-incf n m))))) 4846 (cl-function (lambda (&optional (m 1)) (incf n m)))))
4911(setq count-1 (make-counter)) 4847(setq count-1 (make-counter))
4912(funcall count-1 3) 4848(funcall count-1 3)
4913 @result{} 3 4849 @result{} 3
@@ -5052,7 +4988,7 @@ In Emacs, these are obsolete, replaced by various features of
5052 4988
5053@defmac define-modify-macro name arglist function [doc-string] 4989@defmac define-modify-macro name arglist function [doc-string]
5054This macro defines a ``read-modify-write'' macro similar to 4990This macro defines a ``read-modify-write'' macro similar to
5055@code{cl-incf} and @code{cl-decf}. You can replace this macro 4991@code{incf} and @code{decf}. You can replace this macro
5056with @code{gv-letplace}. 4992with @code{gv-letplace}.
5057 4993
5058The macro @var{name} is defined to take a @var{place} argument 4994The macro @var{name} is defined to take a @var{place} argument
@@ -5180,8 +5116,8 @@ For example, the simple form of @code{defsetf} is shorthand for
5180 5116
5181The Lisp form that is returned can access the arguments from 5117The Lisp form that is returned can access the arguments from
5182@var{arglist} and @var{store-var} in an unrestricted fashion; 5118@var{arglist} and @var{store-var} in an unrestricted fashion;
5183macros like @code{cl-incf} that invoke this 5119macros that invoke this
5184setf-method will insert temporary variables as needed to make 5120setf-method should insert temporary variables as needed to make
5185sure the apparent order of evaluation is preserved. 5121sure the apparent order of evaluation is preserved.
5186 5122
5187Another standard example: 5123Another standard example:
@@ -5245,11 +5181,7 @@ temporary variables. In the setf-methods generated by
5245@code{defsetf}, the second return value is simply the list of 5181@code{defsetf}, the second return value is simply the list of
5246arguments in the place form, and the first return value is a 5182arguments in the place form, and the first return value is a
5247list of a corresponding number of temporary variables generated 5183list of a corresponding number of temporary variables generated
5248@c FIXME I don't think this is true anymore. 5184by @code{cl-gensym}.
5249by @code{cl-gensym}. Macros like @code{cl-incf} that
5250use this setf-method will optimize away most temporaries that
5251turn out to be unnecessary, so there is little reason for the
5252setf-method itself to optimize.
5253@end defmac 5185@end defmac
5254 5186
5255@node GNU Free Documentation License 5187@node GNU Free Documentation License