diff options
| author | Robert Pluim | 2019-10-09 15:12:18 +0200 |
|---|---|---|
| committer | Robert Pluim | 2019-10-09 15:14:17 +0200 |
| commit | b5fbefdac66613e9dedc168d5a356a384a6d4f3f (patch) | |
| tree | 6ba695bd2a2e878d9e996049a0f5af8cf1137c2e | |
| parent | e26c19b88906663fb66ca58d84b85ad336d84f49 (diff) | |
| download | emacs-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.texi | 73 |
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 |
| 950 | any C variables that are to be visible as Lisp variables. | 950 | any 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 | |||
| 956 | defined with @code{DEFVAR_BOOL} are automatically added to the list | 956 | defined 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 | ||
| 963 | The name of the variable to be used by Lisp programs. | ||
| 964 | @item vname | ||
| 965 | The name of the variable in the C sources. | ||
| 966 | @item doc | ||
| 967 | The documentation for the variable, as a C | ||
| 968 | comment. @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 | ||
| 973 | of the Lisp variable with @code{-} replaced by @code{_}. When the | ||
| 974 | variable has type @code{Lisp_Object}, the convention is to also prefix | ||
| 975 | the C variable name with @code{V}. i.e. | ||
| 976 | |||
| 977 | @smallexample | ||
| 978 | DEFVAR_INT ("my-int-variable", my_int_variable, | ||
| 979 | doc: /* An integer variable. */); | ||
| 980 | |||
| 981 | DEFVAR_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 | ||
| 986 | itself rather than the value of that symbol. One such case is when | ||
| 987 | temporarily overriding the value of a variable, which in Lisp is done | ||
| 988 | with @code{let}. In C sources, this is done by defining a | ||
| 989 | corresponding, constant symbol, and using @code{specbind}. By | ||
| 990 | convention, @code{Qmy_lisp_variable} corresponds to | ||
| 991 | @code{Vmy_lisp_variable}; to define it, use the @code{DEFSYM} macro. | ||
| 992 | i.e. | ||
| 993 | |||
| 994 | @smallexample | ||
| 995 | DEFSYM (Qmy_lisp_variable, "my-lisp-variable"); | ||
| 996 | @end smallexample | ||
| 997 | |||
| 998 | To perform the actual binding: | ||
| 999 | |||
| 1000 | @smallexample | ||
| 1001 | specbind (Qmy_lisp_variable, Qt); | ||
| 1002 | @end smallexample | ||
| 1003 | |||
| 1004 | In Lisp symbols sometimes need to be quoted, to achieve the same | ||
| 1005 | effect in C you again use the corresponding constant symbol | ||
| 1006 | @code{Qmy_lisp_variable}. For example, when creating a buffer-local | ||
| 1007 | variable (@pxref{Buffer-Local Variables}) in Lisp you would write: | ||
| 1008 | |||
| 1009 | @smallexample | ||
| 1010 | (make-variable-buffer-local 'my-lisp-variable) | ||
| 1011 | @end smallexample | ||
| 1012 | |||
| 1013 | In C the corresponding code uses @code{Fmake_variable_buffer_local} in | ||
| 1014 | combination with @code{DEFSYM}, i.e. | ||
| 1015 | |||
| 1016 | @smallexample | ||
| 1017 | DEFSYM (Qmy_lisp_variable, "my-lisp-variable"); | ||
| 1018 | Fmake_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 |
| 961 | like one declared with @code{defcustom}, add an appropriate entry to | 1023 | like 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 |
| 1025 | the 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 |
| 966 | you must protect it from garbage-collection by calling @code{staticpro} | 1029 | @code{Lisp_Object}, you must protect it from garbage-collection by |
| 967 | in @code{syms_of_@var{filename}}, like this: | 1030 | calling @code{staticpro} in @code{syms_of_@var{filename}}, like this: |
| 968 | 1031 | ||
| 969 | @example | 1032 | @example |
| 970 | staticpro (&@var{variable}); | 1033 | staticpro (&@var{variable}); |