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*/
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, 1, 0,
105 "Return the documentation string of FUNCTION.")
114 while (XTYPE (fun
) == Lisp_Symbol
)
117 fun
= Fsymbol_function (fun
);
123 if (XSUBR (fun
)->doc
== 0) return Qnil
;
124 if ((int) XSUBR (fun
)->doc
>= 0)
125 return Fsubstitute_command_keys (build_string (XSUBR (fun
)->doc
));
128 Fsubstitute_command_keys (get_doc_string (- (int) XSUBR (fun
)->doc
));
131 if (XVECTOR (fun
)->size
<= COMPILED_DOC_STRING
)
133 tem
= XVECTOR (fun
)->contents
[COMPILED_DOC_STRING
];
134 if (XTYPE (tem
) == Lisp_String
)
135 return Fsubstitute_command_keys (tem
);
136 if (XTYPE (tem
) == Lisp_Int
&& XINT (tem
) >= 0)
137 return Fsubstitute_command_keys (get_doc_string (XFASTINT (tem
)));
142 return build_string ("Keyboard macro.");
146 if (XTYPE (funcar
) != Lisp_Symbol
)
147 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
148 if (XSYMBOL (funcar
) == XSYMBOL (Qkeymap
))
149 return build_string ("Prefix command (definition is a keymap associating keystrokes with\n\
151 if (XSYMBOL (funcar
) == XSYMBOL (Qlambda
)
152 || XSYMBOL (funcar
) == XSYMBOL (Qautoload
))
154 tem
= Fcar (Fcdr (Fcdr (fun
)));
155 if (XTYPE (tem
) == Lisp_String
)
156 return Fsubstitute_command_keys (tem
);
157 if (XTYPE (tem
) == Lisp_Int
&& XINT (tem
) >= 0)
158 return Fsubstitute_command_keys (get_doc_string (XFASTINT (tem
)));
161 if (XSYMBOL (funcar
) == XSYMBOL (Qmocklisp
))
163 if (XSYMBOL (funcar
) == XSYMBOL (Qmacro
))
164 return Fdocumentation (Fcdr (fun
));
166 /* Fall through to the default to report an error. */
169 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
173 DEFUN ("documentation-property", Fdocumentation_property
,
174 Sdocumentation_property
, 2, 2, 0,
175 "Return the documentation string that is SYMBOL's PROP property.\n\
176 This differs from using `get' only in that it can refer to strings\n\
177 stored in the `share-lib/DOC' file.")
179 Lisp_Object sym
, prop
;
181 register Lisp_Object tem
;
183 tem
= Fget (sym
, prop
);
184 if (XTYPE (tem
) == Lisp_Int
)
185 tem
= get_doc_string (XINT (tem
) > 0 ? XINT (tem
) : - XINT (tem
));
186 if (XTYPE (tem
) == Lisp_String
)
187 return Fsubstitute_command_keys (tem
);
191 DEFUN ("Snarf-documentation", Fsnarf_documentation
, Ssnarf_documentation
,
193 "Used during Emacs initialization, before dumping runnable Emacs,\n\
194 to find pointers to doc strings stored in `share-lib/DOC...' and\n\
195 record them in function definitions.\n\
196 One arg, FILENAME, a string which does not include a directory.\n\
197 The file is found in `../share-lib' now; found in the `data-directory'\n\
198 when doc strings are referred to later in the dumped Emacs.")
200 Lisp_Object filename
;
206 register char *p
, *end
;
207 Lisp_Object sym
, fun
, tem
;
209 extern char *index ();
211 CHECK_STRING (filename
, 0);
214 name
= (char *) alloca (XSTRING (filename
)->size
+ 14);
215 strcpy (name
, "../share-lib/");
216 #else /* CANNOT_DUMP */
217 CHECK_STRING (Vdata_directory
, 0);
218 name
= (char *) alloca (XSTRING (filename
)->size
+
219 XSTRING (Vdata_directory
)->size
+ 1);
220 strcpy (name
, XSTRING (Vdata_directory
)->data
);
221 #endif /* CANNOT_DUMP */
222 strcat (name
, XSTRING (filename
)->data
); /*** Add this line ***/
225 /* For VMS versions with limited file name syntax,
226 convert the name to something VMS will allow. */
234 #endif /* not VMS4_4 */
236 strcpy (name
, sys_translate_unix (name
));
240 fd
= open (name
, O_RDONLY
, 0);
242 report_file_error ("Opening doc string file",
243 Fcons (build_string (name
), Qnil
));
244 Vdoc_file_name
= filename
;
250 filled
+= read (fd
, &buf
[filled
], sizeof buf
- 1 - filled
);
256 end
= buf
+ (filled
< 512 ? filled
: filled
- 128);
257 while (p
!= end
&& *p
!= '\037') p
++;
258 /* p points to ^_Ffunctionname\n or ^_Vvarname\n. */
261 end
= index (p
, '\n');
262 sym
= oblookup (Vobarray
, p
+ 2, end
- p
- 2);
263 if (XTYPE (sym
) == Lisp_Symbol
)
265 /* Attach a docstring to a variable? */
268 /* Install file-position as variable-documentation property
269 and make it negative for a user-variable
270 (doc starts with a `*'). */
271 Fput (sym
, Qvariable_documentation
,
272 make_number ((pos
+ end
+ 1 - buf
)
273 * (end
[1] == '*' ? -1 : 1)));
276 /* Attach a docstring to a function? The type determines where
277 the docstring is stored. */
278 else if (p
[1] == 'F')
280 fun
= XSYMBOL (sym
)->function
;
282 /* Lisp_Subrs have a slot for it. */
283 if (XTYPE (fun
) == Lisp_Subr
)
284 XSUBR (fun
)->doc
= (char *) - (pos
+ end
+ 1 - buf
);
286 /* If it's a lisp form, stick it in the form. */
287 else if (CONSP (fun
))
289 tem
= XCONS (fun
)->car
;
290 if (EQ (tem
, Qlambda
) || EQ (tem
, Qautoload
))
292 tem
= Fcdr (Fcdr (fun
));
294 XTYPE (XCONS (tem
)->car
) == Lisp_Int
)
295 XFASTINT (XCONS (tem
)->car
) = (pos
+ end
+ 1 - buf
);
299 /* Bytecode objects sometimes have slots for it. */
300 else if (XTYPE (fun
) == Lisp_Compiled
)
302 /* This bytecode object must have a slot for the
303 docstring, since we've found a docstring for it. */
304 if (XVECTOR (fun
)->size
<= COMPILED_DOC_STRING
)
307 XFASTINT (XVECTOR (fun
)->contents
[COMPILED_DOC_STRING
])
308 = pos
+ end
+ 1 - buf
;
311 else error ("DOC file invalid at position %d", pos
);
316 bcopy (end
, buf
, filled
);
322 DEFUN ("substitute-command-keys", Fsubstitute_command_keys
,
323 Ssubstitute_command_keys
, 1, 1, 0,
324 "Substitute key descriptions for command names in STRING.\n\
325 Return a new string which is STRING with substrings of the form \\=\\[COMMAND]\n\
326 replaced by either: a keystroke sequence that will invoke COMMAND,\n\
327 or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
328 Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
329 \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
330 Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
331 as the keymap for future \\=\\[COMMAND] substrings.\n\
332 \\=\\= quotes the following character and is discarded;\n\
333 thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
339 register unsigned char *strp
;
340 register unsigned char *bufp
;
344 register Lisp_Object tem
;
346 unsigned char *start
;
353 CHECK_STRING (str
, 0);
356 keymap
= current_buffer
->keymap
;
358 bsize
= XSTRING (str
)->size
;
359 bufp
= buf
= (unsigned char *) xmalloc (bsize
);
361 strp
= (unsigned char *) XSTRING (str
)->data
;
362 while (strp
< (unsigned char *) XSTRING (str
)->data
+ XSTRING (str
)->size
)
364 if (strp
[0] == '\\' && strp
[1] == '=')
366 /* \= quotes the next character;
367 thus, to put in \[ without its special meaning, use \=\[. */
372 else if (strp
[0] == '\\' && strp
[1] == '[')
375 strp
+= 2; /* skip \[ */
378 while ((strp
- (unsigned char *) XSTRING (str
)->data
379 < XSTRING (str
)->size
)
382 length
= strp
- start
;
385 /* Save STRP in IDX. */
386 idx
= strp
- (unsigned char *) XSTRING (str
)->data
;
387 tem
= Fintern (make_string (start
, length
), Qnil
);
388 tem
= Fwhere_is_internal (tem
, keymap
, Qnil
, Qt
, Qnil
);
390 if (NILP (tem
)) /* but not on any keys */
392 new = (unsigned char *) xrealloc (buf
, bsize
+= 4);
395 bcopy ("M-x ", bufp
, 4);
400 { /* function is on a key */
401 tem
= Fkey_description (tem
);
405 /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
406 \<foo> just sets the keymap used for \[cmd]. */
407 else if (strp
[0] == '\\' && (strp
[1] == '{' || strp
[1] == '<'))
409 struct buffer
*oldbuf
;
413 strp
+= 2; /* skip \{ or \< */
416 while ((strp
- (unsigned char *) XSTRING (str
)->data
417 < XSTRING (str
)->size
)
418 && *strp
!= '}' && *strp
!= '>')
420 length
= strp
- start
;
421 strp
++; /* skip } or > */
423 /* Save STRP in IDX. */
424 idx
= strp
- (unsigned char *) XSTRING (str
)->data
;
426 /* Get the value of the keymap in TEM, or nil if undefined.
427 Do this while still in the user's current buffer
428 in case it is a local variable. */
429 name
= Fintern (make_string (start
, length
), Qnil
);
430 tem
= Fboundp (name
);
433 tem
= Fsymbol_value (name
);
435 tem
= get_keymap_1 (tem
, 0);
438 /* Now switch to a temp buffer. */
439 oldbuf
= current_buffer
;
440 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer
));
444 name
= Fsymbol_name (name
);
445 insert_string ("\nUses keymap \"");
446 insert_from_string (name
, 0, XSTRING (name
)->size
);
447 insert_string ("\", which is not currently defined.\n");
448 if (start
[-1] == '<') keymap
= Qnil
;
450 else if (start
[-1] == '<')
453 describe_map_tree (tem
, 1, Qnil
);
454 tem
= Fbuffer_string ();
456 set_buffer_internal (oldbuf
);
459 start
= XSTRING (tem
)->data
;
460 length
= XSTRING (tem
)->size
;
462 new = (unsigned char *) xrealloc (buf
, bsize
+= length
);
465 bcopy (start
, bufp
, length
);
467 /* Check STR again in case gc relocated it. */
468 strp
= (unsigned char *) XSTRING (str
)->data
+ idx
;
470 else /* just copy other chars */
474 if (changed
) /* don't bother if nothing substituted */
475 tem
= make_string (buf
, bufp
- buf
);
485 DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name
,
486 "Name of file containing documentation strings of built-in symbols.");
487 Vdoc_file_name
= Qnil
;
489 defsubr (&Sdocumentation
);
490 defsubr (&Sdocumentation_property
);
491 defsubr (&Ssnarf_documentation
);
492 defsubr (&Ssubstitute_command_keys
);