aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Pluim2019-10-09 15:12:18 +0200
committerRobert Pluim2019-10-09 15:14:17 +0200
commitb5fbefdac66613e9dedc168d5a356a384a6d4f3f (patch)
tree6ba695bd2a2e878d9e996049a0f5af8cf1137c2e
parente26c19b88906663fb66ca58d84b85ad336d84f49 (diff)
downloademacs-b5fbefdac66613e9dedc168d5a356a384a6d4f3f.tar.gz
emacs-b5fbefdac66613e9dedc168d5a356a384a6d4f3f.zip
Expand documentation on Lisp variables defined in C sources
* doc/lispref/internals.texi (Writing Emacs Primitives): Add description of DEFVAR_* arguments. Describe variable naming conventions. Explain how to express quoting of symbols in C, plus 'specbind' and how to create buffer-local variables.
-rw-r--r--doc/lispref/internals.texi73
1 files changed, 68 insertions, 5 deletions
diff --git a/doc/lispref/internals.texi b/doc/lispref/internals.texi
index 9fb826c93bb..ab385168af2 100644
--- a/doc/lispref/internals.texi
+++ b/doc/lispref/internals.texi
@@ -945,7 +945,7 @@ of these functions are called, and add a call to
945@anchor{Defining Lisp variables in C} 945@anchor{Defining Lisp variables in C}
946@vindex byte-boolean-vars 946@vindex byte-boolean-vars
947@cindex defining Lisp variables in C 947@cindex defining Lisp variables in C
948@cindex @code{DEFVAR_INT}, @code{DEFVAR_LISP}, @code{DEFVAR_BOOL} 948@cindex @code{DEFVAR_INT}, @code{DEFVAR_LISP}, @code{DEFVAR_BOOL}, @code{DEFSYM}
949 The function @code{syms_of_@var{filename}} is also the place to define 949 The function @code{syms_of_@var{filename}} is also the place to define
950any C variables that are to be visible as Lisp variables. 950any C variables that are to be visible as Lisp variables.
951@code{DEFVAR_LISP} makes a C variable of type @code{Lisp_Object} visible 951@code{DEFVAR_LISP} makes a C variable of type @code{Lisp_Object} visible
@@ -956,15 +956,78 @@ with a value that is either @code{t} or @code{nil}. Note that variables
956defined with @code{DEFVAR_BOOL} are automatically added to the list 956defined with @code{DEFVAR_BOOL} are automatically added to the list
957@code{byte-boolean-vars} used by the byte compiler. 957@code{byte-boolean-vars} used by the byte compiler.
958 958
959 These macros all expect three arguments:
960
961@table @code
962@item lname
963The name of the variable to be used by Lisp programs.
964@item vname
965The name of the variable in the C sources.
966@item doc
967The documentation for the variable, as a C
968comment. @xref{Documentation Basics} for more details.
969@end table
970
971 By convention, when defining variables of a ``native'' type
972(@code{int} and @code{bool}), the name of the C variable is the name
973of the Lisp variable with @code{-} replaced by @code{_}. When the
974variable has type @code{Lisp_Object}, the convention is to also prefix
975the C variable name with @code{V}. i.e.
976
977@smallexample
978DEFVAR_INT ("my-int-variable", my_int_variable,
979 doc: /* An integer variable. */);
980
981DEFVAR_LISP ("my-lisp-variable", Vmy_lisp_variable,
982 doc: /* A Lisp variable. */);
983@end smallexample
984
985 There are situations in Lisp where you need to refer to the symbol
986itself rather than the value of that symbol. One such case is when
987temporarily overriding the value of a variable, which in Lisp is done
988with @code{let}. In C sources, this is done by defining a
989corresponding, constant symbol, and using @code{specbind}. By
990convention, @code{Qmy_lisp_variable} corresponds to
991@code{Vmy_lisp_variable}; to define it, use the @code{DEFSYM} macro.
992i.e.
993
994@smallexample
995DEFSYM (Qmy_lisp_variable, "my-lisp-variable");
996@end smallexample
997
998 To perform the actual binding:
999
1000@smallexample
1001specbind (Qmy_lisp_variable, Qt);
1002@end smallexample
1003
1004 In Lisp symbols sometimes need to be quoted, to achieve the same
1005effect in C you again use the corresponding constant symbol
1006@code{Qmy_lisp_variable}. For example, when creating a buffer-local
1007variable (@pxref{Buffer-Local Variables}) in Lisp you would write:
1008
1009@smallexample
1010(make-variable-buffer-local 'my-lisp-variable)
1011@end smallexample
1012
1013In C the corresponding code uses @code{Fmake_variable_buffer_local} in
1014combination with @code{DEFSYM}, i.e.
1015
1016@smallexample
1017DEFSYM (Qmy_lisp_variable, "my-lisp-variable");
1018Fmake_variable_buffer_local (Qmy_lisp_variable);
1019@end smallexample
1020
959@cindex defining customization variables in C 1021@cindex defining customization variables in C
960 If you want to make a Lisp variable that is defined in C behave 1022 If you want to make a Lisp variable that is defined in C behave
961like one declared with @code{defcustom}, add an appropriate entry to 1023like one declared with @code{defcustom}, add an appropriate entry to
962@file{cus-start.el}. 1024@file{cus-start.el}. @xref{Variable Definitions}, for a description of
1025the format to use.
963 1026
964@cindex @code{staticpro}, protection from GC 1027@cindex @code{staticpro}, protection from GC
965 If you define a file-scope C variable of type @code{Lisp_Object}, 1028 If you directly define a file-scope C variable of type
966you must protect it from garbage-collection by calling @code{staticpro} 1029@code{Lisp_Object}, you must protect it from garbage-collection by
967in @code{syms_of_@var{filename}}, like this: 1030calling @code{staticpro} in @code{syms_of_@var{filename}}, like this:
968 1031
969@example 1032@example
970staticpro (&@var{variable}); 1033staticpro (&@var{variable});