1 /* Record indices of function doc strings stored in a file.
2 Copyright (C) 1985, 1986 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*/
38 Lisp_Object Vdoc_file_name
;
41 get_doc_string (filepos
)
44 char buf
[512 * 32 + 1];
47 register char *p
, *p1
;
49 extern char *index ();
51 if (XTYPE (Vexec_directory
) != Lisp_String
52 || XTYPE (Vdoc_file_name
) != Lisp_String
)
55 name
= (char *) alloca (XSTRING (Vexec_directory
)->size
56 + XSTRING (Vdoc_file_name
)->size
+ 8);
57 strcpy (name
, XSTRING (Vexec_directory
)->data
);
58 strcat (name
, XSTRING (Vdoc_file_name
)->data
);
61 /* For VMS versions with limited file name syntax,
62 convert the name to something VMS will allow. */
70 #endif /* not VMS4_4 */
72 strcpy (name
, sys_translate_unix (name
));
76 fd
= open (name
, O_RDONLY
, 0);
78 error ("Cannot open doc string file \"%s\"", name
);
79 if (0 > lseek (fd
, filepos
, 0))
82 error ("Position %ld out of range in doc string file \"%s\"",
86 while (p
!= buf
+ sizeof buf
- 1)
88 count
= read (fd
, p
, 512);
92 p1
= index (p
, '\037');
102 return make_string (buf
, p
- buf
);
105 DEFUN ("documentation", Fdocumentation
, Sdocumentation
, 1, 1, 0,
106 "Return the documentation string of FUNCTION.")
115 while (XTYPE (fun
) == Lisp_Symbol
)
118 fun
= Fsymbol_function (fun
);
124 if (XSUBR (fun
)->doc
== 0) return Qnil
;
125 if ((int) XSUBR (fun
)->doc
>= 0)
126 return Fsubstitute_command_keys (build_string (XSUBR (fun
)->doc
));
129 Fsubstitute_command_keys (get_doc_string (- (int) XSUBR (fun
)->doc
));
132 if (XVECTOR (fun
)->size
<= COMPILED_DOC_STRING
)
134 tem
= XVECTOR (fun
)->contents
[COMPILED_DOC_STRING
];
135 if (XTYPE (tem
) == Lisp_String
)
136 return Fsubstitute_command_keys (tem
);
137 if (XTYPE (tem
) == Lisp_Int
&& XINT (tem
) >= 0)
138 return Fsubstitute_command_keys (get_doc_string (XFASTINT (tem
)));
143 return build_string ("Keyboard macro.");
147 if (XTYPE (funcar
) != Lisp_Symbol
)
148 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
149 if (XSYMBOL (funcar
) == XSYMBOL (Qkeymap
))
150 return build_string ("Prefix command (definition is a keymap associating keystrokes with\n\
152 if (XSYMBOL (funcar
) == XSYMBOL (Qlambda
)
153 || XSYMBOL (funcar
) == XSYMBOL (Qautoload
))
155 tem
= Fcar (Fcdr (Fcdr (fun
)));
156 if (XTYPE (tem
) == Lisp_String
)
157 return Fsubstitute_command_keys (tem
);
158 if (XTYPE (tem
) == Lisp_Int
&& XINT (tem
) >= 0)
159 return Fsubstitute_command_keys (get_doc_string (XFASTINT (tem
)));
162 if (XSYMBOL (funcar
) == XSYMBOL (Qmocklisp
))
164 if (XSYMBOL (funcar
) == XSYMBOL (Qmacro
))
165 return Fdocumentation (Fcdr (fun
));
167 /* Fall through to the default to report an error. */
170 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
174 DEFUN ("documentation-property", Fdocumentation_property
,
175 Sdocumentation_property
, 2, 2, 0,
176 "Return the documentation string that is SYMBOL's PROP property.\n\
177 This differs from using `get' only in that it can refer to strings\n\
178 stored in the `etc/DOC' file.")
180 Lisp_Object sym
, prop
;
182 register Lisp_Object tem
;
184 tem
= Fget (sym
, prop
);
185 if (XTYPE (tem
) == Lisp_Int
)
186 tem
= get_doc_string (XINT (tem
) > 0 ? XINT (tem
) : - XINT (tem
));
187 return Fsubstitute_command_keys (tem
);
190 DEFUN ("Snarf-documentation", Fsnarf_documentation
, Ssnarf_documentation
,
192 "Used during Emacs initialization, before dumping runnable Emacs,\n\
193 to find pointers to doc strings stored in `etc/DOC...' and\n\
194 record them in function definitions.\n\
195 One arg, FILENAME, a string which does not include a directory.\n\
196 The file is found in `../etc' now; found in the `exec-directory'\n\
197 when doc strings are referred to later in the dumped Emacs.")
199 Lisp_Object filename
;
205 register char *p
, *end
;
206 Lisp_Object sym
, fun
, tem
;
208 extern char *index ();
210 CHECK_STRING (filename
, 0);
213 name
= (char *) alloca (XSTRING (filename
)->size
+ 8);
214 strcpy (name
, "../etc/");
215 #else /* CANNOT_DUMP */
216 CHECK_STRING (Vexec_directory
, 0);
217 name
= (char *) alloca (XSTRING (filename
)->size
+
218 XSTRING (Vexec_directory
)->size
+ 1);
219 strcpy (name
, XSTRING (Vexec_directory
)->data
);
220 #endif /* CANNOT_DUMP */
221 strcat (name
, XSTRING (filename
)->data
); /*** Add this line ***/
224 /* For VMS versions with limited file name syntax,
225 convert the name to something VMS will allow. */
233 #endif /* not VMS4_4 */
235 strcpy (name
, sys_translate_unix (name
));
239 fd
= open (name
, O_RDONLY
, 0);
241 report_file_error ("Opening doc string file",
242 Fcons (build_string (name
), Qnil
));
243 Vdoc_file_name
= filename
;
249 filled
+= read (fd
, &buf
[filled
], sizeof buf
- 1 - filled
);
255 end
= buf
+ (filled
< 512 ? filled
: filled
- 128);
256 while (p
!= end
&& *p
!= '\037') p
++;
257 /* p points to ^_Ffunctionname\n or ^_Vvarname\n. */
260 end
= index (p
, '\n');
261 sym
= oblookup (Vobarray
, p
+ 2, end
- p
- 2);
262 if (XTYPE (sym
) == Lisp_Symbol
)
264 /* Attach a docstring to a variable? */
267 /* Install file-position as variable-documentation property
268 and make it negative for a user-variable
269 (doc starts with a `*'). */
270 Fput (sym
, Qvariable_documentation
,
271 make_number ((pos
+ end
+ 1 - buf
)
272 * (end
[1] == '*' ? -1 : 1)));
275 /* Attach a docstring to a function? The type determines where
276 the docstring is stored. */
277 else if (p
[1] == 'F')
279 fun
= XSYMBOL (sym
)->function
;
281 /* Lisp_Subrs have a slot for it. */
282 if (XTYPE (fun
) == Lisp_Subr
)
283 XSUBR (fun
)->doc
= (char *) - (pos
+ end
+ 1 - buf
);
285 /* If it's a lisp form, stick it in the form. */
286 else if (CONSP (fun
))
288 tem
= XCONS (fun
)->car
;
289 if (EQ (tem
, Qlambda
) || EQ (tem
, Qautoload
))
291 tem
= Fcdr (Fcdr (fun
));
293 XTYPE (XCONS (tem
)->car
) == Lisp_Int
)
294 XFASTINT (XCONS (tem
)->car
) = (pos
+ end
+ 1 - buf
);
298 /* Bytecode objects sometimes have slots for it. */
299 else if (XTYPE (fun
) == Lisp_Compiled
)
301 /* This bytecode object must have a slot for the
302 docstring, since we've found a docstring for it. */
303 if (XVECTOR (fun
)->size
<= COMPILED_DOC_STRING
)
306 XFASTINT (XVECTOR (fun
)->contents
[COMPILED_DOC_STRING
])
307 = pos
+ end
+ 1 - buf
;
310 else error ("DOC file invalid at position %d", pos
);
315 bcopy (end
, buf
, filled
);
321 DEFUN ("substitute-command-keys", Fsubstitute_command_keys
,
322 Ssubstitute_command_keys
, 1, 1, 0,
323 "Substitute key descriptions for command names in STRING.\n\
324 Return a new string which is STRING with substrings of the form \\=\\[COMMAND]\n\
325 replaced by either: a keystroke sequence that will invoke COMMAND,\n\
326 or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
327 Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
328 \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
329 Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
330 as the keymap for future \\=\\[COMMAND] substrings.\n\
331 \\=\\= quotes the following character and is discarded;\n\
332 thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
338 register unsigned char *strp
;
339 register unsigned char *bufp
;
343 register Lisp_Object tem
;
345 unsigned char *start
;
352 CHECK_STRING (str
, 0);
355 keymap
= current_buffer
->keymap
;
357 bsize
= XSTRING (str
)->size
;
358 bufp
= buf
= (unsigned char *) xmalloc (bsize
);
360 strp
= (unsigned char *) XSTRING (str
)->data
;
361 while (strp
< (unsigned char *) XSTRING (str
)->data
+ XSTRING (str
)->size
)
363 if (strp
[0] == '\\' && strp
[1] == '=')
365 /* \= quotes the next character;
366 thus, to put in \[ without its special meaning, use \=\[. */
371 else if (strp
[0] == '\\' && strp
[1] == '[')
374 strp
+= 2; /* skip \[ */
377 while ((strp
- (unsigned char *) XSTRING (str
)->data
378 < XSTRING (str
)->size
)
381 length
= strp
- start
;
384 /* Save STRP in IDX. */
385 idx
= strp
- (unsigned char *) XSTRING (str
)->data
;
386 tem
= Fintern (make_string (start
, length
), Qnil
);
387 tem
= Fwhere_is_internal (tem
, keymap
, Qnil
, Qt
, Qnil
);
389 if (NULL (tem
)) /* but not on any keys */
391 new = (unsigned char *) xrealloc (buf
, bsize
+= 4);
394 bcopy ("M-x ", bufp
, 4);
399 { /* function is on a key */
400 tem
= Fkey_description (tem
);
404 /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
405 \<foo> just sets the keymap used for \[cmd]. */
406 else if (strp
[0] == '\\' && (strp
[1] == '{' || strp
[1] == '<'))
408 struct buffer
*oldbuf
;
412 strp
+= 2; /* skip \{ or \< */
415 while ((strp
- (unsigned char *) XSTRING (str
)->data
416 < XSTRING (str
)->size
)
417 && *strp
!= '}' && *strp
!= '>')
419 length
= strp
- start
;
420 strp
++; /* skip } or > */
422 /* Save STRP in IDX. */
423 idx
= strp
- (unsigned char *) XSTRING (str
)->data
;
425 /* Get the value of the keymap in TEM, or nil if undefined.
426 Do this while still in the user's current buffer
427 in case it is a local variable. */
428 name
= Fintern (make_string (start
, length
), Qnil
);
429 tem
= Fboundp (name
);
432 tem
= Fsymbol_value (name
);
434 tem
= get_keymap_1 (tem
, 0);
437 /* Now switch to a temp buffer. */
438 oldbuf
= current_buffer
;
439 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
443 name
= Fsymbol_name (name
);
444 insert_string ("\nUses keymap \"");
445 insert_from_string (name
, 0, XSTRING (name
)->size
);
446 insert_string ("\", which is not currently defined.\n");
447 if (start
[-1] == '<') keymap
= Qnil
;
449 else if (start
[-1] == '<')
452 describe_map_tree (tem
, 1, Qnil
);
453 tem
= Fbuffer_string ();
455 set_buffer_internal (oldbuf
);
458 start
= XSTRING (tem
)->data
;
459 length
= XSTRING (tem
)->size
;
461 new = (unsigned char *) xrealloc (buf
, bsize
+= length
);
464 bcopy (start
, bufp
, length
);
466 /* Check STR again in case gc relocated it. */
467 strp
= (unsigned char *) XSTRING (str
)->data
+ idx
;
469 else /* just copy other chars */
473 if (changed
) /* don't bother if nothing substituted */
474 tem
= make_string (buf
, bufp
- buf
);
484 DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name
,
485 "Name of file containing documentation strings of built-in symbols.");
486 Vdoc_file_name
= Qnil
;
488 defsubr (&Sdocumentation
);
489 defsubr (&Sdocumentation_property
);
490 defsubr (&Ssnarf_documentation
);
491 defsubr (&Ssubstitute_command_keys
);