* doc.c: #include keyboard.h.
[bpt/emacs.git] / src / doc.c
1 /* Record indices of function doc strings stored in a file.
2 Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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)
9 any later version.
10
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.
15
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. */
19
20
21 #include "config.h"
22
23 #include <sys/types.h>
24 #include <sys/file.h> /* Must be after sys/types.h for USG and BSD4_1*/
25
26 #ifdef USG5
27 #include <fcntl.h>
28 #endif
29
30 #ifndef O_RDONLY
31 #define O_RDONLY 0
32 #endif
33
34 #include "lisp.h"
35 #include "buffer.h"
36 #include "keyboard.h"
37
38 Lisp_Object Vdoc_file_name;
39
40 Lisp_Object
41 get_doc_string (filepos)
42 long filepos;
43 {
44 char buf[512 * 32 + 1];
45 register int fd;
46 register char *name;
47 register char *p, *p1;
48 register int count;
49 extern char *index ();
50
51 if (XTYPE (Vdata_directory) != Lisp_String
52 || XTYPE (Vdoc_file_name) != Lisp_String)
53 return Qnil;
54
55 name = (char *) alloca (XSTRING (Vdata_directory)->size
56 + XSTRING (Vdoc_file_name)->size + 8);
57 strcpy (name, XSTRING (Vdata_directory)->data);
58 strcat (name, XSTRING (Vdoc_file_name)->data);
59 #ifdef VMS
60 #ifndef VMS4_4
61 /* For VMS versions with limited file name syntax,
62 convert the name to something VMS will allow. */
63 p = name;
64 while (*p)
65 {
66 if (*p == '-')
67 *p = '_';
68 p++;
69 }
70 #endif /* not VMS4_4 */
71 #ifdef VMS4_4
72 strcpy (name, sys_translate_unix (name));
73 #endif /* VMS4_4 */
74 #endif /* VMS */
75
76 fd = open (name, O_RDONLY, 0);
77 if (fd < 0)
78 error ("Cannot open doc string file \"%s\"", name);
79 if (0 > lseek (fd, filepos, 0))
80 {
81 close (fd);
82 error ("Position %ld out of range in doc string file \"%s\"",
83 filepos, name);
84 }
85 p = buf;
86 while (p != buf + sizeof buf - 1)
87 {
88 count = read (fd, p, 512);
89 p[count] = 0;
90 if (!count)
91 break;
92 p1 = index (p, '\037');
93 if (p1)
94 {
95 *p1 = 0;
96 p = p1;
97 break;
98 }
99 p += count;
100 }
101 close (fd);
102 return make_string (buf, p - buf);
103 }
104
105 DEFUN ("documentation", Fdocumentation, Sdocumentation, 1, 2, 0,
106 "Return the documentation string of FUNCTION.\n\
107 Unless a non-nil second argument is given, the\n\
108 string is passed through `substitute-command-keys'.")
109 (function, raw)
110 Lisp_Object function, raw;
111 {
112 Lisp_Object fun;
113 Lisp_Object funcar;
114 Lisp_Object tem, doc;
115
116 fun = Findirect_function (function);
117
118 switch (XTYPE (fun))
119 {
120 case Lisp_Subr:
121 if (XSUBR (fun)->doc == 0) return Qnil;
122 if ((int) XSUBR (fun)->doc >= 0)
123 doc = build_string (XSUBR (fun)->doc);
124 else
125 doc = get_doc_string (- (int) XSUBR (fun)->doc);
126 break;
127
128 case Lisp_Compiled:
129 if (XVECTOR (fun)->size <= COMPILED_DOC_STRING)
130 return Qnil;
131 tem = XVECTOR (fun)->contents[COMPILED_DOC_STRING];
132 if (XTYPE (tem) == Lisp_String)
133 doc = tem;
134 else if (XTYPE (tem) == Lisp_Int && XINT (tem) >= 0)
135 doc = get_doc_string (XFASTINT (tem));
136 else
137 return Qnil;
138 break;
139
140 case Lisp_String:
141 case Lisp_Vector:
142 return build_string ("Keyboard macro.");
143
144 case Lisp_Cons:
145 funcar = Fcar (fun);
146 if (XTYPE (funcar) != Lisp_Symbol)
147 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
148 else if (EQ (funcar, Qkeymap))
149 return build_string ("Prefix command (definition is a keymap associating keystrokes with\n\
150 subcommands.)");
151 else if (EQ (funcar, Qlambda)
152 || EQ (funcar, Qautoload))
153 {
154 tem = Fcar (Fcdr (Fcdr (fun)));
155 if (XTYPE (tem) == Lisp_String)
156 doc = tem;
157 else if (XTYPE (tem) == Lisp_Int && XINT (tem) >= 0)
158 doc = get_doc_string (XFASTINT (tem));
159 else
160 return Qnil;
161
162 break;
163 }
164 else if (EQ (funcar, Qmocklisp))
165 return Qnil;
166 else if (EQ (funcar, Qmacro))
167 return Fdocumentation (Fcdr (fun), raw);
168
169 /* Fall through to the default to report an error. */
170
171 default:
172 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
173 }
174
175 if (NILP (raw))
176 {
177 struct gcpro gcpro1;
178
179 GCPRO1 (doc);
180 doc = Fsubstitute_command_keys (doc);
181 UNGCPRO;
182 }
183 return doc;
184 }
185
186 DEFUN ("documentation-property", Fdocumentation_property, Sdocumentation_property, 2, 2, 0,
187 "Return the documentation string that is SYMBOL's PROP property.\n\
188 This is like `get', but it can refer to strings stored in the\n\
189 `etc/DOC' file; and if the value is a string, it is passed through\n\
190 `substitute-command-keys'. A non-nil third argument avoids this\n\
191 translation.")
192 (sym, prop, raw)
193 Lisp_Object sym, prop, raw;
194 {
195 register Lisp_Object tem;
196
197 tem = Fget (sym, prop);
198 if (XTYPE (tem) == Lisp_Int)
199 tem = get_doc_string (XINT (tem) > 0 ? XINT (tem) : - XINT (tem));
200 if (NILP (raw) && XTYPE (tem) == Lisp_String)
201 return Fsubstitute_command_keys (tem);
202 return tem;
203 }
204 \f
205 DEFUN ("Snarf-documentation", Fsnarf_documentation, Ssnarf_documentation,
206 1, 1, 0,
207 "Used during Emacs initialization, before dumping runnable Emacs,\n\
208 to find pointers to doc strings stored in `etc/DOC...' and\n\
209 record them in function definitions.\n\
210 One arg, FILENAME, a string which does not include a directory.\n\
211 The file is found in `../etc' now; found in the `data-directory'\n\
212 when doc strings are referred to later in the dumped Emacs.")
213 (filename)
214 Lisp_Object filename;
215 {
216 int fd;
217 char buf[1024 + 1];
218 register int filled;
219 register int pos;
220 register char *p, *end;
221 Lisp_Object sym, fun, tem;
222 char *name;
223 extern char *index ();
224
225 #ifndef CANNOT_DUMP
226 if (NILP (Vpurify_flag))
227 error ("Snarf-documentation can only be called in an undumped Emacs");
228 #endif
229
230 CHECK_STRING (filename, 0);
231
232 #ifndef CANNOT_DUMP
233 name = (char *) alloca (XSTRING (filename)->size + 14);
234 strcpy (name, "../etc/");
235 #else /* CANNOT_DUMP */
236 CHECK_STRING (Vdata_directory, 0);
237 name = (char *) alloca (XSTRING (filename)->size +
238 XSTRING (Vdata_directory)->size + 1);
239 strcpy (name, XSTRING (Vdata_directory)->data);
240 #endif /* CANNOT_DUMP */
241 strcat (name, XSTRING (filename)->data); /*** Add this line ***/
242 #ifdef VMS
243 #ifndef VMS4_4
244 /* For VMS versions with limited file name syntax,
245 convert the name to something VMS will allow. */
246 p = name;
247 while (*p)
248 {
249 if (*p == '-')
250 *p = '_';
251 p++;
252 }
253 #endif /* not VMS4_4 */
254 #ifdef VMS4_4
255 strcpy (name, sys_translate_unix (name));
256 #endif /* VMS4_4 */
257 #endif /* VMS */
258
259 fd = open (name, O_RDONLY, 0);
260 if (fd < 0)
261 report_file_error ("Opening doc string file",
262 Fcons (build_string (name), Qnil));
263 Vdoc_file_name = filename;
264 filled = 0;
265 pos = 0;
266 while (1)
267 {
268 if (filled < 512)
269 filled += read (fd, &buf[filled], sizeof buf - 1 - filled);
270 if (!filled)
271 break;
272
273 buf[filled] = 0;
274 p = buf;
275 end = buf + (filled < 512 ? filled : filled - 128);
276 while (p != end && *p != '\037') p++;
277 /* p points to ^_Ffunctionname\n or ^_Vvarname\n. */
278 if (p != end)
279 {
280 end = index (p, '\n');
281 sym = oblookup (Vobarray, p + 2, end - p - 2);
282 if (XTYPE (sym) == Lisp_Symbol)
283 {
284 /* Attach a docstring to a variable? */
285 if (p[1] == 'V')
286 {
287 /* Install file-position as variable-documentation property
288 and make it negative for a user-variable
289 (doc starts with a `*'). */
290 Fput (sym, Qvariable_documentation,
291 make_number ((pos + end + 1 - buf)
292 * (end[1] == '*' ? -1 : 1)));
293 }
294
295 /* Attach a docstring to a function? The type determines where
296 the docstring is stored. */
297 else if (p[1] == 'F')
298 {
299 fun = XSYMBOL (sym)->function;
300
301 /* Lisp_Subrs have a slot for it. */
302 if (XTYPE (fun) == Lisp_Subr)
303 XSUBR (fun)->doc = (char *) - (pos + end + 1 - buf);
304
305 /* If it's a lisp form, stick it in the form. */
306 else if (CONSP (fun))
307 {
308 tem = XCONS (fun)->car;
309 if (EQ (tem, Qlambda) || EQ (tem, Qautoload))
310 {
311 tem = Fcdr (Fcdr (fun));
312 if (CONSP (tem) &&
313 XTYPE (XCONS (tem)->car) == Lisp_Int)
314 XFASTINT (XCONS (tem)->car) = (pos + end + 1 - buf);
315 }
316 }
317
318 /* Bytecode objects sometimes have slots for it. */
319 else if (XTYPE (fun) == Lisp_Compiled)
320 {
321 /* This bytecode object must have a slot for the
322 docstring, since we've found a docstring for it. */
323 if (XVECTOR (fun)->size <= COMPILED_DOC_STRING)
324 abort ();
325
326 XFASTINT (XVECTOR (fun)->contents[COMPILED_DOC_STRING])
327 = pos + end + 1 - buf;
328 }
329 }
330 else error ("DOC file invalid at position %d", pos);
331 }
332 }
333 pos += end - buf;
334 filled -= end - buf;
335 bcopy (end, buf, filled);
336 }
337 close (fd);
338 return Qnil;
339 }
340 \f
341 DEFUN ("substitute-command-keys", Fsubstitute_command_keys,
342 Ssubstitute_command_keys, 1, 1, 0,
343 "Substitute key descriptions for command names in STRING.\n\
344 Return a new string which is STRING with substrings of the form \\=\\[COMMAND]\n\
345 replaced by either: a keystroke sequence that will invoke COMMAND,\n\
346 or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
347 Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
348 \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
349 Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
350 as the keymap for future \\=\\[COMMAND] substrings.\n\
351 \\=\\= quotes the following character and is discarded;\n\
352 thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
353 (str)
354 Lisp_Object str;
355 {
356 unsigned char *buf;
357 int changed = 0;
358 register unsigned char *strp;
359 register unsigned char *bufp;
360 int idx;
361 int bsize;
362 unsigned char *new;
363 Lisp_Object tem;
364 Lisp_Object keymap;
365 unsigned char *start;
366 int length;
367 Lisp_Object name;
368 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
369
370 if (NILP (str))
371 return Qnil;
372
373 CHECK_STRING (str, 0);
374 tem = Qnil;
375 keymap = Qnil;
376 name = Qnil;
377 GCPRO4 (str, tem, keymap, name);
378
379 keymap = current_buffer->keymap;
380
381 bsize = XSTRING (str)->size;
382 bufp = buf = (unsigned char *) xmalloc (bsize);
383
384 strp = (unsigned char *) XSTRING (str)->data;
385 while (strp < (unsigned char *) XSTRING (str)->data + XSTRING (str)->size)
386 {
387 if (strp[0] == '\\' && strp[1] == '=')
388 {
389 /* \= quotes the next character;
390 thus, to put in \[ without its special meaning, use \=\[. */
391 changed = 1;
392 *bufp++ = strp[2];
393 strp += 3;
394 }
395 else if (strp[0] == '\\' && strp[1] == '[')
396 {
397 changed = 1;
398 strp += 2; /* skip \[ */
399 start = strp;
400
401 while ((strp - (unsigned char *) XSTRING (str)->data
402 < XSTRING (str)->size)
403 && *strp != ']')
404 strp++;
405 length = strp - start;
406 strp++; /* skip ] */
407
408 /* Save STRP in IDX. */
409 idx = strp - (unsigned char *) XSTRING (str)->data;
410 tem = Fintern (make_string (start, length), Qnil);
411 tem = Fwhere_is_internal (tem, keymap, Qnil, Qt, Qnil);
412
413 if (NILP (tem)) /* but not on any keys */
414 {
415 new = (unsigned char *) xrealloc (buf, bsize += 4);
416 bufp += new - buf;
417 buf = new;
418 bcopy ("M-x ", bufp, 4);
419 bufp += 4;
420 goto subst;
421 }
422 else
423 { /* function is on a key */
424 tem = Fkey_description (tem);
425 goto subst_string;
426 }
427 }
428 /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
429 \<foo> just sets the keymap used for \[cmd]. */
430 else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
431 {
432 struct buffer *oldbuf;
433
434 changed = 1;
435 strp += 2; /* skip \{ or \< */
436 start = strp;
437
438 while ((strp - (unsigned char *) XSTRING (str)->data
439 < XSTRING (str)->size)
440 && *strp != '}' && *strp != '>')
441 strp++;
442 length = strp - start;
443 strp++; /* skip } or > */
444
445 /* Save STRP in IDX. */
446 idx = strp - (unsigned char *) XSTRING (str)->data;
447
448 /* Get the value of the keymap in TEM, or nil if undefined.
449 Do this while still in the user's current buffer
450 in case it is a local variable. */
451 name = Fintern (make_string (start, length), Qnil);
452 tem = Fboundp (name);
453 if (! NILP (tem))
454 {
455 tem = Fsymbol_value (name);
456 if (! NILP (tem))
457 tem = get_keymap_1 (tem, 0, 1);
458 }
459
460 /* Now switch to a temp buffer. */
461 oldbuf = current_buffer;
462 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
463
464 if (NILP (tem))
465 {
466 name = Fsymbol_name (name);
467 insert_string ("\nUses keymap \"");
468 insert_from_string (name, 0, XSTRING (name)->size);
469 insert_string ("\", which is not currently defined.\n");
470 if (start[-1] == '<') keymap = Qnil;
471 }
472 else if (start[-1] == '<')
473 keymap = tem;
474 else
475 describe_map_tree (tem, 1, Qnil);
476 tem = Fbuffer_string ();
477 Ferase_buffer ();
478 set_buffer_internal (oldbuf);
479
480 subst_string:
481 start = XSTRING (tem)->data;
482 length = XSTRING (tem)->size;
483 subst:
484 new = (unsigned char *) xrealloc (buf, bsize += length);
485 bufp += new - buf;
486 buf = new;
487 bcopy (start, bufp, length);
488 bufp += length;
489 /* Check STR again in case gc relocated it. */
490 strp = (unsigned char *) XSTRING (str)->data + idx;
491 }
492 else /* just copy other chars */
493 *bufp++ = *strp++;
494 }
495
496 if (changed) /* don't bother if nothing substituted */
497 tem = make_string (buf, bufp - buf);
498 else
499 tem = str;
500 free (buf);
501 RETURN_UNGCPRO (tem);
502 }
503 \f
504 syms_of_doc ()
505 {
506 DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name,
507 "Name of file containing documentation strings of built-in symbols.");
508 Vdoc_file_name = Qnil;
509
510 defsubr (&Sdocumentation);
511 defsubr (&Sdocumentation_property);
512 defsubr (&Ssnarf_documentation);
513 defsubr (&Ssubstitute_command_keys);
514 }