diff options
| author | Glenn Morris | 2012-11-05 20:42:17 -0500 |
|---|---|---|
| committer | Glenn Morris | 2012-11-05 20:42:17 -0500 |
| commit | ebdbfb953591230bf217151535211af5a300ee3c (patch) | |
| tree | e750f2a872919270c65adb7f23a7ffda59ea8e41 | |
| parent | 6b3770fb9dd180abbda32383eba5e961e76d7504 (diff) | |
| download | emacs-ebdbfb953591230bf217151535211af5a300ee3c.tar.gz emacs-ebdbfb953591230bf217151535211af5a300ee3c.zip | |
Add some documentation on defining new generalized variables
* doc/lispref/variables.texi (Setting Generalized Variables):
Split most of previous contents into this subsection.
(Adding Generalized Variables): New subsection.
* doc/lispref/elisp.texi:
Add Generalized Variables subsections to detailed menu.
* etc/NEWS: Mention some gv.el macros by name.
| -rw-r--r-- | doc/lispref/ChangeLog | 7 | ||||
| -rw-r--r-- | doc/lispref/elisp.texi | 5 | ||||
| -rw-r--r-- | doc/lispref/variables.texi | 70 | ||||
| -rw-r--r-- | etc/NEWS | 2 |
4 files changed, 84 insertions, 0 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 8057089cf68..d97a258da34 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | 2012-11-06 Glenn Morris <rgm@gnu.org> | ||
| 2 | |||
| 3 | * variables.texi (Setting Generalized Variables): | ||
| 4 | Split most of previous contents into this subsection. | ||
| 5 | (Adding Generalized Variables): New subsection. | ||
| 6 | * elisp.texi: Add Generalized Variables subsections to detailed menu. | ||
| 7 | |||
| 1 | 2012-11-05 Chong Yidong <cyd@gnu.org> | 8 | 2012-11-05 Chong Yidong <cyd@gnu.org> |
| 2 | 9 | ||
| 3 | * frames.texi (Initial Parameters): Doc fix (Bug#12144). | 10 | * frames.texi (Initial Parameters): Doc fix (Bug#12144). |
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi index 06a2ebfcaf8..5f10b450e84 100644 --- a/doc/lispref/elisp.texi +++ b/doc/lispref/elisp.texi | |||
| @@ -502,6 +502,11 @@ Buffer-Local Variables | |||
| 502 | * Default Value:: The default value is seen in buffers | 502 | * Default Value:: The default value is seen in buffers |
| 503 | that don't have their own buffer-local values. | 503 | that don't have their own buffer-local values. |
| 504 | 504 | ||
| 505 | Generalized Variables | ||
| 506 | |||
| 507 | * Setting Generalized Variables:: The @code{setf} macro. | ||
| 508 | * Adding Generalized Variables:: Defining new @code{setf} forms. | ||
| 509 | |||
| 505 | Functions | 510 | Functions |
| 506 | 511 | ||
| 507 | * What Is a Function:: Lisp functions vs. primitives; terminology. | 512 | * What Is a Function:: Lisp functions vs. primitives; terminology. |
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi index 88b7909126e..f9f69850ef8 100644 --- a/doc/lispref/variables.texi +++ b/doc/lispref/variables.texi | |||
| @@ -1965,6 +1965,14 @@ and @samp{a[i] = x} stores an element using the same notation. | |||
| 1965 | Just as certain forms like @code{a[i]} can be lvalues in C, there | 1965 | Just as certain forms like @code{a[i]} can be lvalues in C, there |
| 1966 | is a set of forms that can be generalized variables in Lisp. | 1966 | is a set of forms that can be generalized variables in Lisp. |
| 1967 | 1967 | ||
| 1968 | @menu | ||
| 1969 | * Setting Generalized Variables:: The @code{setf} macro. | ||
| 1970 | * Adding Generalized Variables:: Defining new @code{setf} forms. | ||
| 1971 | @end menu | ||
| 1972 | |||
| 1973 | @node Setting Generalized Variables | ||
| 1974 | @subsection The @code{setf} Macro | ||
| 1975 | |||
| 1968 | The @code{setf} macro is the most basic way to operate on generalized | 1976 | The @code{setf} macro is the most basic way to operate on generalized |
| 1969 | variables. The @code{setf} form is like @code{setq}, except that it | 1977 | variables. The @code{setf} form is like @code{setq}, except that it |
| 1970 | accepts arbitrary place forms on the left side rather than just | 1978 | accepts arbitrary place forms on the left side rather than just |
| @@ -2049,3 +2057,65 @@ place can be used to insert or delete at any position in a list. | |||
| 2049 | The @file{cl-lib} library defines various extensions for generalized | 2057 | The @file{cl-lib} library defines various extensions for generalized |
| 2050 | variables, including additional @code{setf} places. | 2058 | variables, including additional @code{setf} places. |
| 2051 | @xref{Generalized Variables,,, cl, Common Lisp Extensions}. | 2059 | @xref{Generalized Variables,,, cl, Common Lisp Extensions}. |
| 2060 | |||
| 2061 | |||
| 2062 | @node Adding Generalized Variables | ||
| 2063 | @subsection Defining new @code{setf} forms | ||
| 2064 | |||
| 2065 | This section describes how to define new forms that @code{setf} can | ||
| 2066 | operate on. | ||
| 2067 | |||
| 2068 | @defmac gv-define-simple-setter name setter &optional fix-return | ||
| 2069 | This macro enables you to easily define @code{setf} methods for simple | ||
| 2070 | cases. @var{name} is the name of a function, macro, or special form. | ||
| 2071 | You can use this macro whenever @var{name} has a directly | ||
| 2072 | corresponding @var{setter} function that updates it, e.g., | ||
| 2073 | @code{(gv-define-simple-setter car setcar)}. | ||
| 2074 | |||
| 2075 | This macro translates a call of the form | ||
| 2076 | |||
| 2077 | @example | ||
| 2078 | (setf (@var{name} @var{args}@dots{}) @var{value}) | ||
| 2079 | @end example | ||
| 2080 | |||
| 2081 | into | ||
| 2082 | @example | ||
| 2083 | (@var{setter} @var{args}@dots{} @var{value}) | ||
| 2084 | @end example | ||
| 2085 | |||
| 2086 | @noindent | ||
| 2087 | Such a @code{setf} call is documented to return @var{value}. This is | ||
| 2088 | no problem with, e.g., @code{car} and @code{setcar}, because | ||
| 2089 | @code{setcar} returns the value that it set. If your @var{setter} | ||
| 2090 | function does not return @var{value}, use a non-@code{nil} value for | ||
| 2091 | the @var{fix-return} argument of @code{gv-define-simple-setter}. This | ||
| 2092 | wraps the @code{setf} expansion in @code{(prog1 @var{value} @dots{})} | ||
| 2093 | so that it returns the correct result. | ||
| 2094 | @end defmac | ||
| 2095 | |||
| 2096 | |||
| 2097 | @defmac gv-define-setter name arglist &rest body | ||
| 2098 | This macro allows for more complex @code{setf} expansions than the | ||
| 2099 | previous form. You may need to use this form, for example, if there | ||
| 2100 | is no simple setter function to call, or if there is one but it | ||
| 2101 | requires different arguments to the place form. | ||
| 2102 | |||
| 2103 | This macro expands the form | ||
| 2104 | @code{(setf (@var{name} @var{args}@dots{}) @var{value})} by | ||
| 2105 | first binding the @code{setf} argument forms | ||
| 2106 | @code{(@var{value} @var{args}@dots{})} according to @var{arglist}, | ||
| 2107 | and then executing @var{body}. @var{body} should return a Lisp | ||
| 2108 | form that does the assignment. Remember that it should return the | ||
| 2109 | value that was set. An example of using this macro is: | ||
| 2110 | |||
| 2111 | @example | ||
| 2112 | (gv-define-setter caar (val x) `(setcar (car ,x) ,val)) | ||
| 2113 | @end example | ||
| 2114 | @end defmac | ||
| 2115 | |||
| 2116 | @c FIXME? Not sure what, if anything, to say about this. | ||
| 2117 | @ignore | ||
| 2118 | @defmac gv-define-expander name handler | ||
| 2119 | This is the most general way to define a new @code{setf} expansion. | ||
| 2120 | @end defmac | ||
| 2121 | @end ignore | ||
| @@ -783,6 +783,8 @@ systems), or based on memory allocations. | |||
| 783 | 783 | ||
| 784 | ** CL-style generalized variables are now in core Elisp. | 784 | ** CL-style generalized variables are now in core Elisp. |
| 785 | `setf' is autoloaded; `push' and `pop' accept generalized variables. | 785 | `setf' is autoloaded; `push' and `pop' accept generalized variables. |
| 786 | You can define your own generalized variables using `gv-define-simple-setter', | ||
| 787 | `gv-define-setter', etc. | ||
| 786 | 788 | ||
| 787 | +++ | 789 | +++ |
| 788 | ** `defun' also accepts a (declare DECLS) form, like `defmacro'. | 790 | ** `defun' also accepts a (declare DECLS) form, like `defmacro'. |