1 /* Record indices of function doc strings stored in a file.
2 Copyright (C) 1985, 1986, 1993 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*/
42 Lisp_Object Vdoc_file_name
;
44 extern Lisp_Object Voverriding_local_map
;
47 get_doc_string (filepos
)
50 char buf
[512 * 32 + 1];
53 register char *p
, *p1
;
55 extern char *index ();
57 if (XTYPE (Vdoc_directory
) != Lisp_String
58 || XTYPE (Vdoc_file_name
) != Lisp_String
)
61 name
= (char *) alloca (XSTRING (Vdoc_directory
)->size
62 + XSTRING (Vdoc_file_name
)->size
+ 8);
63 strcpy (name
, XSTRING (Vdoc_directory
)->data
);
64 strcat (name
, XSTRING (Vdoc_file_name
)->data
);
67 /* For VMS versions with limited file name syntax,
68 convert the name to something VMS will allow. */
76 #endif /* not VMS4_4 */
78 strcpy (name
, sys_translate_unix (name
));
82 fd
= open (name
, O_RDONLY
, 0);
84 error ("Cannot open doc string file \"%s\"", name
);
85 if (0 > lseek (fd
, filepos
, 0))
88 error ("Position %ld out of range in doc string file \"%s\"",
92 while (p
!= buf
+ sizeof buf
- 1)
94 count
= read (fd
, p
, 512);
98 p1
= index (p
, '\037');
108 return make_string (buf
, p
- buf
);
111 DEFUN ("documentation", Fdocumentation
, Sdocumentation
, 1, 2, 0,
112 "Return the documentation string of FUNCTION.\n\
113 Unless a non-nil second argument is given, the\n\
114 string is passed through `substitute-command-keys'.")
116 Lisp_Object function
, raw
;
120 Lisp_Object tem
, doc
;
122 fun
= Findirect_function (function
);
127 if (XSUBR (fun
)->doc
== 0) return Qnil
;
128 if ((int) XSUBR (fun
)->doc
>= 0)
129 doc
= build_string (XSUBR (fun
)->doc
);
131 doc
= get_doc_string (- (int) XSUBR (fun
)->doc
);
135 if (XVECTOR (fun
)->size
<= COMPILED_DOC_STRING
)
137 tem
= XVECTOR (fun
)->contents
[COMPILED_DOC_STRING
];
138 if (XTYPE (tem
) == Lisp_String
)
140 else if (XTYPE (tem
) == Lisp_Int
&& XINT (tem
) >= 0)
141 doc
= get_doc_string (XFASTINT (tem
));
148 return build_string ("Keyboard macro.");
152 if (XTYPE (funcar
) != Lisp_Symbol
)
153 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
154 else if (EQ (funcar
, Qkeymap
))
155 return build_string ("Prefix command (definition is a keymap associating keystrokes with\n\
157 else if (EQ (funcar
, Qlambda
)
158 || EQ (funcar
, Qautoload
))
160 tem
= Fcar (Fcdr (Fcdr (fun
)));
161 if (XTYPE (tem
) == Lisp_String
)
163 else if (XTYPE (tem
) == Lisp_Int
&& XINT (tem
) >= 0)
164 doc
= get_doc_string (XFASTINT (tem
));
170 else if (EQ (funcar
, Qmocklisp
))
172 else if (EQ (funcar
, Qmacro
))
173 return Fdocumentation (Fcdr (fun
), raw
);
175 /* Fall through to the default to report an error. */
178 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
186 doc
= Fsubstitute_command_keys (doc
);
192 DEFUN ("documentation-property", Fdocumentation_property
, Sdocumentation_property
, 2, 3, 0,
193 "Return the documentation string that is SYMBOL's PROP property.\n\
194 This is like `get', but it can refer to strings stored in the\n\
195 `etc/DOC' file; and if the value is a string, it is passed through\n\
196 `substitute-command-keys'. A non-nil third argument avoids this\n\
199 Lisp_Object sym
, prop
, raw
;
201 register Lisp_Object tem
;
203 tem
= Fget (sym
, prop
);
204 if (XTYPE (tem
) == Lisp_Int
)
205 tem
= get_doc_string (XINT (tem
) > 0 ? XINT (tem
) : - XINT (tem
));
206 if (NILP (raw
) && XTYPE (tem
) == Lisp_String
)
207 return Fsubstitute_command_keys (tem
);
211 /* Scanning the DOC files and placing docstring offsets into functions. */
214 store_function_docstring (fun
, offset
)
218 fun
= indirect_function (fun
);
220 /* The type determines where the docstring is stored. */
222 /* Lisp_Subrs have a slot for it. */
223 if (XTYPE (fun
) == Lisp_Subr
)
224 XSUBR (fun
)->doc
= (char *) - offset
;
226 /* If it's a lisp form, stick it in the form. */
227 else if (CONSP (fun
))
231 tem
= XCONS (fun
)->car
;
232 if (EQ (tem
, Qlambda
) || EQ (tem
, Qautoload
))
234 tem
= Fcdr (Fcdr (fun
));
236 XTYPE (XCONS (tem
)->car
) == Lisp_Int
)
237 XFASTINT (XCONS (tem
)->car
) = offset
;
239 else if (EQ (tem
, Qmacro
))
240 store_function_docstring (XCONS (fun
)->cdr
, offset
);
243 /* Bytecode objects sometimes have slots for it. */
244 else if (XTYPE (fun
) == Lisp_Compiled
)
246 /* This bytecode object must have a slot for the
247 docstring, since we've found a docstring for it. */
248 if (XVECTOR (fun
)->size
<= COMPILED_DOC_STRING
)
251 XFASTINT (XVECTOR (fun
)->contents
[COMPILED_DOC_STRING
]) = offset
;
256 DEFUN ("Snarf-documentation", Fsnarf_documentation
, Ssnarf_documentation
,
258 "Used during Emacs initialization, before dumping runnable Emacs,\n\
259 to find pointers to doc strings stored in `etc/DOC...' and\n\
260 record them in function definitions.\n\
261 One arg, FILENAME, a string which does not include a directory.\n\
262 The file is found in `../etc' now; found in the `data-directory'\n\
263 when doc strings are referred to later in the dumped Emacs.")
265 Lisp_Object filename
;
271 register char *p
, *end
;
272 Lisp_Object sym
, fun
, tem
;
274 extern char *index ();
277 if (NILP (Vpurify_flag
))
278 error ("Snarf-documentation can only be called in an undumped Emacs");
281 CHECK_STRING (filename
, 0);
284 name
= (char *) alloca (XSTRING (filename
)->size
+ 14);
285 strcpy (name
, "../etc/");
286 #else /* CANNOT_DUMP */
287 CHECK_STRING (Vdoc_directory
, 0);
288 name
= (char *) alloca (XSTRING (filename
)->size
+
289 XSTRING (Vdoc_directory
)->size
+ 1);
290 strcpy (name
, XSTRING (Vdoc_directory
)->data
);
291 #endif /* CANNOT_DUMP */
292 strcat (name
, XSTRING (filename
)->data
); /*** Add this line ***/
295 /* For VMS versions with limited file name syntax,
296 convert the name to something VMS will allow. */
304 #endif /* not VMS4_4 */
306 strcpy (name
, sys_translate_unix (name
));
310 fd
= open (name
, O_RDONLY
, 0);
312 report_file_error ("Opening doc string file",
313 Fcons (build_string (name
), Qnil
));
314 Vdoc_file_name
= filename
;
320 filled
+= read (fd
, &buf
[filled
], sizeof buf
- 1 - filled
);
326 end
= buf
+ (filled
< 512 ? filled
: filled
- 128);
327 while (p
!= end
&& *p
!= '\037') p
++;
328 /* p points to ^_Ffunctionname\n or ^_Vvarname\n. */
331 end
= index (p
, '\n');
332 sym
= oblookup (Vobarray
, p
+ 2, end
- p
- 2);
333 if (XTYPE (sym
) == Lisp_Symbol
)
335 /* Attach a docstring to a variable? */
338 /* Install file-position as variable-documentation property
339 and make it negative for a user-variable
340 (doc starts with a `*'). */
341 Fput (sym
, Qvariable_documentation
,
342 make_number ((pos
+ end
+ 1 - buf
)
343 * (end
[1] == '*' ? -1 : 1)));
346 /* Attach a docstring to a function? */
347 else if (p
[1] == 'F')
348 store_function_docstring (sym
, pos
+ end
+ 1 - buf
);
351 error ("DOC file invalid at position %d", pos
);
356 bcopy (end
, buf
, filled
);
362 DEFUN ("substitute-command-keys", Fsubstitute_command_keys
,
363 Ssubstitute_command_keys
, 1, 1, 0,
364 "Substitute key descriptions for command names in STRING.\n\
365 Return a new string which is STRING with substrings of the form \\=\\[COMMAND]\n\
366 replaced by either: a keystroke sequence that will invoke COMMAND,\n\
367 or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
368 Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
369 \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
370 Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
371 as the keymap for future \\=\\[COMMAND] substrings.\n\
372 \\=\\= quotes the following character and is discarded;\n\
373 thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
379 register unsigned char *strp
;
380 register unsigned char *bufp
;
386 unsigned char *start
;
389 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
394 CHECK_STRING (str
, 0);
398 GCPRO4 (str
, tem
, keymap
, name
);
400 /* KEYMAP is either nil (which means search all the active keymaps)
401 or a specified local map (which means search just that and the
402 global map). If non-nil, it might come from Voverriding_local_map,
403 or from a \\<mapname> construct in STR itself.. */
404 keymap
= Voverriding_local_map
;
406 bsize
= XSTRING (str
)->size
;
407 bufp
= buf
= (unsigned char *) xmalloc (bsize
);
409 strp
= (unsigned char *) XSTRING (str
)->data
;
410 while (strp
< (unsigned char *) XSTRING (str
)->data
+ XSTRING (str
)->size
)
412 if (strp
[0] == '\\' && strp
[1] == '=')
414 /* \= quotes the next character;
415 thus, to put in \[ without its special meaning, use \=\[. */
420 else if (strp
[0] == '\\' && strp
[1] == '[')
422 Lisp_Object firstkey
;
425 strp
+= 2; /* skip \[ */
428 while ((strp
- (unsigned char *) XSTRING (str
)->data
429 < XSTRING (str
)->size
)
432 length
= strp
- start
;
435 /* Save STRP in IDX. */
436 idx
= strp
- (unsigned char *) XSTRING (str
)->data
;
437 tem
= Fintern (make_string (start
, length
), Qnil
);
438 tem
= Fwhere_is_internal (tem
, keymap
, Qt
, Qnil
);
440 /* Disregard menu bar bindings; it is positively annoying to
441 mention them when there's no menu bar, and it isn't terribly
442 useful even when there is a menu bar. */
445 firstkey
= Faref (tem
, make_number (0));
446 if (EQ (firstkey
, Qmenu_bar
))
450 if (NILP (tem
)) /* but not on any keys */
452 new = (unsigned char *) xrealloc (buf
, bsize
+= 4);
455 bcopy ("M-x ", bufp
, 4);
460 { /* function is on a key */
461 tem
= Fkey_description (tem
);
465 /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
466 \<foo> just sets the keymap used for \[cmd]. */
467 else if (strp
[0] == '\\' && (strp
[1] == '{' || strp
[1] == '<'))
469 struct buffer
*oldbuf
;
472 strp
+= 2; /* skip \{ or \< */
475 while ((strp
- (unsigned char *) XSTRING (str
)->data
476 < XSTRING (str
)->size
)
477 && *strp
!= '}' && *strp
!= '>')
479 length
= strp
- start
;
480 strp
++; /* skip } or > */
482 /* Save STRP in IDX. */
483 idx
= strp
- (unsigned char *) XSTRING (str
)->data
;
485 /* Get the value of the keymap in TEM, or nil if undefined.
486 Do this while still in the user's current buffer
487 in case it is a local variable. */
488 name
= Fintern (make_string (start
, length
), Qnil
);
489 tem
= Fboundp (name
);
492 tem
= Fsymbol_value (name
);
494 tem
= get_keymap_1 (tem
, 0, 1);
497 /* Now switch to a temp buffer. */
498 oldbuf
= current_buffer
;
499 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
503 name
= Fsymbol_name (name
);
504 insert_string ("\nUses keymap \"");
505 insert_from_string (name
, 0, XSTRING (name
)->size
, 1);
506 insert_string ("\", which is not currently defined.\n");
507 if (start
[-1] == '<') keymap
= Qnil
;
509 else if (start
[-1] == '<')
512 describe_map_tree (tem
, 1, Qnil
, Qnil
, 0, 1);
513 tem
= Fbuffer_string ();
515 set_buffer_internal (oldbuf
);
518 start
= XSTRING (tem
)->data
;
519 length
= XSTRING (tem
)->size
;
521 new = (unsigned char *) xrealloc (buf
, bsize
+= length
);
524 bcopy (start
, bufp
, length
);
526 /* Check STR again in case gc relocated it. */
527 strp
= (unsigned char *) XSTRING (str
)->data
+ idx
;
529 else /* just copy other chars */
533 if (changed
) /* don't bother if nothing substituted */
534 tem
= make_string (buf
, bufp
- buf
);
538 RETURN_UNGCPRO (tem
);
543 DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name
,
544 "Name of file containing documentation strings of built-in symbols.");
545 Vdoc_file_name
= Qnil
;
547 defsubr (&Sdocumentation
);
548 defsubr (&Sdocumentation_property
);
549 defsubr (&Ssnarf_documentation
);
550 defsubr (&Ssubstitute_command_keys
);