1 /* Record indices of function doc strings stored in a file.
2 Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <sys/types.h>
24 #include <sys/file.h> /* Must be after sys/types.h for USG and BSD4_1*/
37 Lisp_Object Vdoc_file_name
;
40 get_doc_string (filepos
)
43 char buf
[512 * 32 + 1];
46 register char *p
, *p1
;
48 extern char *index ();
50 if (XTYPE (Vdata_directory
) != Lisp_String
51 || XTYPE (Vdoc_file_name
) != Lisp_String
)
54 name
= (char *) alloca (XSTRING (Vdata_directory
)->size
55 + XSTRING (Vdoc_file_name
)->size
+ 8);
56 strcpy (name
, XSTRING (Vdata_directory
)->data
);
57 strcat (name
, XSTRING (Vdoc_file_name
)->data
);
60 /* For VMS versions with limited file name syntax,
61 convert the name to something VMS will allow. */
69 #endif /* not VMS4_4 */
71 strcpy (name
, sys_translate_unix (name
));
75 fd
= open (name
, O_RDONLY
, 0);
77 error ("Cannot open doc string file \"%s\"", name
);
78 if (0 > lseek (fd
, filepos
, 0))
81 error ("Position %ld out of range in doc string file \"%s\"",
85 while (p
!= buf
+ sizeof buf
- 1)
87 count
= read (fd
, p
, 512);
91 p1
= index (p
, '\037');
101 return make_string (buf
, p
- buf
);
104 DEFUN ("documentation", Fdocumentation
, Sdocumentation
, 1, 2, 0,
105 "Return the documentation string of FUNCTION.\n\
106 Unless a non-nil second argument is given, the\n\
107 string is passed through `substitute-command-keys'.")
109 Lisp_Object fun1
, raw
;
113 Lisp_Object tem
, doc
;
116 while (XTYPE (fun
) == Lisp_Symbol
)
119 fun
= Fsymbol_function (fun
);
125 if (XSUBR (fun
)->doc
== 0) return Qnil
;
126 if ((int) XSUBR (fun
)->doc
>= 0)
127 doc
= build_string (XSUBR (fun
)->doc
);
129 doc
= get_doc_string (- (int) XSUBR (fun
)->doc
);
133 if (XVECTOR (fun
)->size
<= COMPILED_DOC_STRING
)
135 tem
= XVECTOR (fun
)->contents
[COMPILED_DOC_STRING
];
136 if (XTYPE (tem
) == Lisp_String
)
138 else if (XTYPE (tem
) == Lisp_Int
&& XINT (tem
) >= 0)
139 doc
= get_doc_string (XFASTINT (tem
));
146 return build_string ("Keyboard macro.");
150 if (XTYPE (funcar
) != Lisp_Symbol
)
151 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
152 if (XSYMBOL (funcar
) == XSYMBOL (Qkeymap
))
153 return build_string ("Prefix command (definition is a keymap associating keystrokes with\n\
155 if (XSYMBOL (funcar
) == XSYMBOL (Qlambda
)
156 || XSYMBOL (funcar
) == XSYMBOL (Qautoload
))
158 tem
= Fcar (Fcdr (Fcdr (fun
)));
159 if (XTYPE (tem
) == Lisp_String
)
161 else if (XTYPE (tem
) == Lisp_Int
&& XINT (tem
) >= 0)
162 doc
= get_doc_string (XFASTINT (tem
));
166 if (XSYMBOL (funcar
) == XSYMBOL (Qmocklisp
))
168 if (XSYMBOL (funcar
) == XSYMBOL (Qmacro
))
169 return Fdocumentation (Fcdr (fun
), raw
);
171 /* Fall through to the default to report an error. */
174 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
178 doc
= Fsubstitute_command_keys (doc
);
182 DEFUN ("documentation-property", Fdocumentation_property
, Sdocumentation_property
, 2, 2, 0,
183 "Return the documentation string that is SYMBOL's PROP property.\n\
184 This is like `get', but it can refer to strings stored in the\n\
185 `etc/DOC' file; and if the value is a string, it is passed through\n\
186 `substitute-command-keys'. A non-nil third argument avoids this\n\
189 Lisp_Object sym
, prop
, raw
;
191 register Lisp_Object tem
;
193 tem
= Fget (sym
, prop
);
194 if (XTYPE (tem
) == Lisp_Int
)
195 tem
= get_doc_string (XINT (tem
) > 0 ? XINT (tem
) : - XINT (tem
));
196 if (NILP (raw
) && XTYPE (tem
) == Lisp_String
)
197 return Fsubstitute_command_keys (tem
);
201 DEFUN ("Snarf-documentation", Fsnarf_documentation
, Ssnarf_documentation
,
203 "Used during Emacs initialization, before dumping runnable Emacs,\n\
204 to find pointers to doc strings stored in `etc/DOC...' and\n\
205 record them in function definitions.\n\
206 One arg, FILENAME, a string which does not include a directory.\n\
207 The file is found in `../etc' now; found in the `data-directory'\n\
208 when doc strings are referred to later in the dumped Emacs.")
210 Lisp_Object filename
;
216 register char *p
, *end
;
217 Lisp_Object sym
, fun
, tem
;
219 extern char *index ();
221 CHECK_STRING (filename
, 0);
224 name
= (char *) alloca (XSTRING (filename
)->size
+ 14);
225 strcpy (name
, "../etc/");
226 #else /* CANNOT_DUMP */
227 CHECK_STRING (Vdata_directory
, 0);
228 name
= (char *) alloca (XSTRING (filename
)->size
+
229 XSTRING (Vdata_directory
)->size
+ 1);
230 strcpy (name
, XSTRING (Vdata_directory
)->data
);
231 #endif /* CANNOT_DUMP */
232 strcat (name
, XSTRING (filename
)->data
); /*** Add this line ***/
235 /* For VMS versions with limited file name syntax,
236 convert the name to something VMS will allow. */
244 #endif /* not VMS4_4 */
246 strcpy (name
, sys_translate_unix (name
));
250 fd
= open (name
, O_RDONLY
, 0);
252 report_file_error ("Opening doc string file",
253 Fcons (build_string (name
), Qnil
));
254 Vdoc_file_name
= filename
;
260 filled
+= read (fd
, &buf
[filled
], sizeof buf
- 1 - filled
);
266 end
= buf
+ (filled
< 512 ? filled
: filled
- 128);
267 while (p
!= end
&& *p
!= '\037') p
++;
268 /* p points to ^_Ffunctionname\n or ^_Vvarname\n. */
271 end
= index (p
, '\n');
272 sym
= oblookup (Vobarray
, p
+ 2, end
- p
- 2);
273 if (XTYPE (sym
) == Lisp_Symbol
)
275 /* Attach a docstring to a variable? */
278 /* Install file-position as variable-documentation property
279 and make it negative for a user-variable
280 (doc starts with a `*'). */
281 Fput (sym
, Qvariable_documentation
,
282 make_number ((pos
+ end
+ 1 - buf
)
283 * (end
[1] == '*' ? -1 : 1)));
286 /* Attach a docstring to a function? The type determines where
287 the docstring is stored. */
288 else if (p
[1] == 'F')
290 fun
= XSYMBOL (sym
)->function
;
292 /* Lisp_Subrs have a slot for it. */
293 if (XTYPE (fun
) == Lisp_Subr
)
294 XSUBR (fun
)->doc
= (char *) - (pos
+ end
+ 1 - buf
);
296 /* If it's a lisp form, stick it in the form. */
297 else if (CONSP (fun
))
299 tem
= XCONS (fun
)->car
;
300 if (EQ (tem
, Qlambda
) || EQ (tem
, Qautoload
))
302 tem
= Fcdr (Fcdr (fun
));
304 XTYPE (XCONS (tem
)->car
) == Lisp_Int
)
305 XFASTINT (XCONS (tem
)->car
) = (pos
+ end
+ 1 - buf
);
309 /* Bytecode objects sometimes have slots for it. */
310 else if (XTYPE (fun
) == Lisp_Compiled
)
312 /* This bytecode object must have a slot for the
313 docstring, since we've found a docstring for it. */
314 if (XVECTOR (fun
)->size
<= COMPILED_DOC_STRING
)
317 XFASTINT (XVECTOR (fun
)->contents
[COMPILED_DOC_STRING
])
318 = pos
+ end
+ 1 - buf
;
321 else error ("DOC file invalid at position %d", pos
);
326 bcopy (end
, buf
, filled
);
332 DEFUN ("substitute-command-keys", Fsubstitute_command_keys
,
333 Ssubstitute_command_keys
, 1, 1, 0,
334 "Substitute key descriptions for command names in STRING.\n\
335 Return a new string which is STRING with substrings of the form \\=\\[COMMAND]\n\
336 replaced by either: a keystroke sequence that will invoke COMMAND,\n\
337 or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
338 Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
339 \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
340 Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
341 as the keymap for future \\=\\[COMMAND] substrings.\n\
342 \\=\\= quotes the following character and is discarded;\n\
343 thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
349 register unsigned char *strp
;
350 register unsigned char *bufp
;
354 register Lisp_Object tem
;
356 unsigned char *start
;
363 CHECK_STRING (str
, 0);
366 keymap
= current_buffer
->keymap
;
368 bsize
= XSTRING (str
)->size
;
369 bufp
= buf
= (unsigned char *) xmalloc (bsize
);
371 strp
= (unsigned char *) XSTRING (str
)->data
;
372 while (strp
< (unsigned char *) XSTRING (str
)->data
+ XSTRING (str
)->size
)
374 if (strp
[0] == '\\' && strp
[1] == '=')
376 /* \= quotes the next character;
377 thus, to put in \[ without its special meaning, use \=\[. */
382 else if (strp
[0] == '\\' && strp
[1] == '[')
385 strp
+= 2; /* skip \[ */
388 while ((strp
- (unsigned char *) XSTRING (str
)->data
389 < XSTRING (str
)->size
)
392 length
= strp
- start
;
395 /* Save STRP in IDX. */
396 idx
= strp
- (unsigned char *) XSTRING (str
)->data
;
397 tem
= Fintern (make_string (start
, length
), Qnil
);
398 tem
= Fwhere_is_internal (tem
, keymap
, Qnil
, Qt
, Qnil
);
400 if (NILP (tem
)) /* but not on any keys */
402 new = (unsigned char *) xrealloc (buf
, bsize
+= 4);
405 bcopy ("M-x ", bufp
, 4);
410 { /* function is on a key */
411 tem
= Fkey_description (tem
);
415 /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
416 \<foo> just sets the keymap used for \[cmd]. */
417 else if (strp
[0] == '\\' && (strp
[1] == '{' || strp
[1] == '<'))
419 struct buffer
*oldbuf
;
423 strp
+= 2; /* skip \{ or \< */
426 while ((strp
- (unsigned char *) XSTRING (str
)->data
427 < XSTRING (str
)->size
)
428 && *strp
!= '}' && *strp
!= '>')
430 length
= strp
- start
;
431 strp
++; /* skip } or > */
433 /* Save STRP in IDX. */
434 idx
= strp
- (unsigned char *) XSTRING (str
)->data
;
436 /* Get the value of the keymap in TEM, or nil if undefined.
437 Do this while still in the user's current buffer
438 in case it is a local variable. */
439 name
= Fintern (make_string (start
, length
), Qnil
);
440 tem
= Fboundp (name
);
443 tem
= Fsymbol_value (name
);
445 tem
= get_keymap_1 (tem
, 0);
448 /* Now switch to a temp buffer. */
449 oldbuf
= current_buffer
;
450 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
454 name
= Fsymbol_name (name
);
455 insert_string ("\nUses keymap \"");
456 insert_from_string (name
, 0, XSTRING (name
)->size
);
457 insert_string ("\", which is not currently defined.\n");
458 if (start
[-1] == '<') keymap
= Qnil
;
460 else if (start
[-1] == '<')
463 describe_map_tree (tem
, 1, Qnil
);
464 tem
= Fbuffer_string ();
466 set_buffer_internal (oldbuf
);
469 start
= XSTRING (tem
)->data
;
470 length
= XSTRING (tem
)->size
;
472 new = (unsigned char *) xrealloc (buf
, bsize
+= length
);
475 bcopy (start
, bufp
, length
);
477 /* Check STR again in case gc relocated it. */
478 strp
= (unsigned char *) XSTRING (str
)->data
+ idx
;
480 else /* just copy other chars */
484 if (changed
) /* don't bother if nothing substituted */
485 tem
= make_string (buf
, bufp
- buf
);
495 DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name
,
496 "Name of file containing documentation strings of built-in symbols.");
497 Vdoc_file_name
= Qnil
;
499 defsubr (&Sdocumentation
);
500 defsubr (&Sdocumentation_property
);
501 defsubr (&Ssnarf_documentation
);
502 defsubr (&Ssubstitute_command_keys
);