Update years in copyright notice; nfc.
[bpt/emacs.git] / src / doc.c
1 /* Record indices of function doc strings stored in a file.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22
23 #include <config.h>
24
25 #include <sys/types.h>
26 #include <sys/file.h> /* Must be after sys/types.h for USG and BSD4_1*/
27 #include <ctype.h>
28
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #endif
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #ifndef O_RDONLY
38 #define O_RDONLY 0
39 #endif
40
41 #include "lisp.h"
42 #include "buffer.h"
43 #include "keyboard.h"
44 #include "charset.h"
45 #include "keymap.h"
46
47 #ifdef HAVE_INDEX
48 extern char *index P_ ((const char *, int));
49 #endif
50
51 Lisp_Object Vdoc_file_name;
52
53 Lisp_Object Qfunction_documentation;
54
55 /* A list of files used to build this Emacs binary. */
56 static Lisp_Object Vbuild_files;
57
58 extern Lisp_Object Voverriding_local_map;
59
60 /* For VMS versions with limited file name syntax,
61 convert the name to something VMS will allow. */
62 static void
63 munge_doc_file_name (name)
64 char *name;
65 {
66 #ifdef VMS
67 #ifndef NO_HYPHENS_IN_FILENAMES
68 extern char * sys_translate_unix (char *ufile);
69 strcpy (name, sys_translate_unix (name));
70 #else /* NO_HYPHENS_IN_FILENAMES */
71 char *p = name;
72 while (*p)
73 {
74 if (*p == '-')
75 *p = '_';
76 p++;
77 }
78 #endif /* NO_HYPHENS_IN_FILENAMES */
79 #endif /* VMS */
80 }
81
82 /* Buffer used for reading from documentation file. */
83 static char *get_doc_string_buffer;
84 static int get_doc_string_buffer_size;
85
86 static unsigned char *read_bytecode_pointer;
87 Lisp_Object Fsnarf_documentation P_ ((Lisp_Object));
88
89 /* readchar in lread.c calls back here to fetch the next byte.
90 If UNREADFLAG is 1, we unread a byte. */
91
92 int
93 read_bytecode_char (unreadflag)
94 int unreadflag;
95 {
96 if (unreadflag)
97 {
98 read_bytecode_pointer--;
99 return 0;
100 }
101 return *read_bytecode_pointer++;
102 }
103
104 /* Extract a doc string from a file. FILEPOS says where to get it.
105 If it is an integer, use that position in the standard DOC-... file.
106 If it is (FILE . INTEGER), use FILE as the file name
107 and INTEGER as the position in that file.
108 But if INTEGER is negative, make it positive.
109 (A negative integer is used for user variables, so we can distinguish
110 them without actually fetching the doc string.)
111
112 If the location does not point to the beginning of a docstring
113 (e.g. because the file has been modified and the location is stale),
114 return nil.
115
116 If UNIBYTE is nonzero, always make a unibyte string.
117
118 If DEFINITION is nonzero, assume this is for reading
119 a dynamic function definition; convert the bytestring
120 and the constants vector with appropriate byte handling,
121 and return a cons cell. */
122
123 Lisp_Object
124 get_doc_string (filepos, unibyte, definition)
125 Lisp_Object filepos;
126 int unibyte, definition;
127 {
128 char *from, *to;
129 register int fd;
130 register char *name;
131 register char *p, *p1;
132 int minsize;
133 int offset, position;
134 Lisp_Object file, tem;
135
136 if (INTEGERP (filepos))
137 {
138 file = Vdoc_file_name;
139 position = XINT (filepos);
140 }
141 else if (CONSP (filepos))
142 {
143 file = XCAR (filepos);
144 position = XINT (XCDR (filepos));
145 }
146 else
147 return Qnil;
148
149 if (position < 0)
150 position = - position;
151
152 if (!STRINGP (Vdoc_directory))
153 return Qnil;
154
155 if (!STRINGP (file))
156 return Qnil;
157
158 /* Put the file name in NAME as a C string.
159 If it is relative, combine it with Vdoc_directory. */
160
161 tem = Ffile_name_absolute_p (file);
162 if (NILP (tem))
163 {
164 minsize = SCHARS (Vdoc_directory);
165 /* sizeof ("../etc/") == 8 */
166 if (minsize < 8)
167 minsize = 8;
168 name = (char *) alloca (minsize + SCHARS (file) + 8);
169 strcpy (name, SDATA (Vdoc_directory));
170 strcat (name, SDATA (file));
171 munge_doc_file_name (name);
172 }
173 else
174 {
175 name = (char *) SDATA (file);
176 }
177
178 fd = emacs_open (name, O_RDONLY, 0);
179 if (fd < 0)
180 {
181 #ifndef CANNOT_DUMP
182 if (!NILP (Vpurify_flag))
183 {
184 /* Preparing to dump; DOC file is probably not installed.
185 So check in ../etc. */
186 strcpy (name, "../etc/");
187 strcat (name, SDATA (file));
188 munge_doc_file_name (name);
189
190 fd = emacs_open (name, O_RDONLY, 0);
191 }
192 #endif
193 if (fd < 0)
194 error ("Cannot open doc string file \"%s\"", name);
195 }
196
197 /* Seek only to beginning of disk block. */
198 /* Make sure we read at least 1024 bytes before `position'
199 so we can check the leading text for consistency. */
200 offset = min (position, max (1024, position % (8 * 1024)));
201 if (0 > lseek (fd, position - offset, 0))
202 {
203 emacs_close (fd);
204 error ("Position %ld out of range in doc string file \"%s\"",
205 position, name);
206 }
207
208 /* Read the doc string into get_doc_string_buffer.
209 P points beyond the data just read. */
210
211 p = get_doc_string_buffer;
212 while (1)
213 {
214 int space_left = (get_doc_string_buffer_size
215 - (p - get_doc_string_buffer));
216 int nread;
217
218 /* Allocate or grow the buffer if we need to. */
219 if (space_left == 0)
220 {
221 int in_buffer = p - get_doc_string_buffer;
222 get_doc_string_buffer_size += 16 * 1024;
223 get_doc_string_buffer
224 = (char *) xrealloc (get_doc_string_buffer,
225 get_doc_string_buffer_size + 1);
226 p = get_doc_string_buffer + in_buffer;
227 space_left = (get_doc_string_buffer_size
228 - (p - get_doc_string_buffer));
229 }
230
231 /* Read a disk block at a time.
232 If we read the same block last time, maybe skip this? */
233 if (space_left > 1024 * 8)
234 space_left = 1024 * 8;
235 nread = emacs_read (fd, p, space_left);
236 if (nread < 0)
237 {
238 emacs_close (fd);
239 error ("Read error on documentation file");
240 }
241 p[nread] = 0;
242 if (!nread)
243 break;
244 if (p == get_doc_string_buffer)
245 p1 = (char *) index (p + offset, '\037');
246 else
247 p1 = (char *) index (p, '\037');
248 if (p1)
249 {
250 *p1 = 0;
251 p = p1;
252 break;
253 }
254 p += nread;
255 }
256 emacs_close (fd);
257
258 /* Sanity checking. */
259 if (CONSP (filepos))
260 {
261 int test = 1;
262 if (get_doc_string_buffer[offset - test++] != ' ')
263 return Qnil;
264 while (get_doc_string_buffer[offset - test] >= '0'
265 && get_doc_string_buffer[offset - test] <= '9')
266 test++;
267 if (get_doc_string_buffer[offset - test++] != '@'
268 || get_doc_string_buffer[offset - test] != '#')
269 return Qnil;
270 }
271 else
272 {
273 int test = 1;
274 if (get_doc_string_buffer[offset - test++] != '\n')
275 return Qnil;
276 while (get_doc_string_buffer[offset - test] > ' ')
277 test++;
278 if (get_doc_string_buffer[offset - test] != '\037')
279 return Qnil;
280 }
281
282 /* Scan the text and perform quoting with ^A (char code 1).
283 ^A^A becomes ^A, ^A0 becomes a null char, and ^A_ becomes a ^_. */
284 from = get_doc_string_buffer + offset;
285 to = get_doc_string_buffer + offset;
286 while (from != p)
287 {
288 if (*from == 1)
289 {
290 int c;
291
292 from++;
293 c = *from++;
294 if (c == 1)
295 *to++ = c;
296 else if (c == '0')
297 *to++ = 0;
298 else if (c == '_')
299 *to++ = 037;
300 else
301 error ("Invalid data in documentation file -- ^A followed by code 0%o", c);
302 }
303 else
304 *to++ = *from++;
305 }
306
307 /* If DEFINITION, read from this buffer
308 the same way we would read bytes from a file. */
309 if (definition)
310 {
311 read_bytecode_pointer = get_doc_string_buffer + offset;
312 return Fread (Qlambda);
313 }
314
315 if (unibyte)
316 return make_unibyte_string (get_doc_string_buffer + offset,
317 to - (get_doc_string_buffer + offset));
318 else
319 {
320 /* Let the data determine whether the string is multibyte,
321 even if Emacs is running in --unibyte mode. */
322 int nchars = multibyte_chars_in_text (get_doc_string_buffer + offset,
323 to - (get_doc_string_buffer + offset));
324 return make_string_from_bytes (get_doc_string_buffer + offset,
325 nchars,
326 to - (get_doc_string_buffer + offset));
327 }
328 }
329
330 /* Get a string from position FILEPOS and pass it through the Lisp reader.
331 We use this for fetching the bytecode string and constants vector
332 of a compiled function from the .elc file. */
333
334 Lisp_Object
335 read_doc_string (filepos)
336 Lisp_Object filepos;
337 {
338 return get_doc_string (filepos, 0, 1);
339 }
340
341 static int
342 reread_doc_file (file)
343 Lisp_Object file;
344 {
345 #if 0
346 Lisp_Object reply, prompt[3];
347 struct gcpro gcpro1;
348 GCPRO1 (file);
349 prompt[0] = build_string ("File ");
350 prompt[1] = NILP (file) ? Vdoc_file_name : file;
351 prompt[2] = build_string (" is out of sync. Reload? ");
352 reply = Fy_or_n_p (Fconcat (3, prompt));
353 UNGCPRO;
354 if (NILP (reply))
355 return 0;
356 #endif
357
358 if (NILP (file))
359 Fsnarf_documentation (Vdoc_file_name);
360 else
361 Fload (file, Qt, Qt, Qt, Qnil);
362
363 return 1;
364 }
365
366 DEFUN ("documentation", Fdocumentation, Sdocumentation, 1, 2, 0,
367 doc: /* Return the documentation string of FUNCTION.
368 Unless a non-nil second argument RAW is given, the
369 string is passed through `substitute-command-keys'. */)
370 (function, raw)
371 Lisp_Object function, raw;
372 {
373 Lisp_Object fun;
374 Lisp_Object funcar;
375 Lisp_Object tem, doc;
376 int try_reload = 1;
377
378 documentation:
379
380 doc = Qnil;
381
382 if (SYMBOLP (function)
383 && (tem = Fget (function, Qfunction_documentation),
384 !NILP (tem)))
385 return Fdocumentation_property (function, Qfunction_documentation, raw);
386
387 fun = Findirect_function (function);
388 if (SUBRP (fun))
389 {
390 if (XSUBR (fun)->doc == 0)
391 return Qnil;
392 else if ((EMACS_INT) XSUBR (fun)->doc >= 0)
393 doc = build_string (XSUBR (fun)->doc);
394 else
395 doc = make_number ((EMACS_INT) XSUBR (fun)->doc);
396 }
397 else if (COMPILEDP (fun))
398 {
399 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) <= COMPILED_DOC_STRING)
400 return Qnil;
401 tem = AREF (fun, COMPILED_DOC_STRING);
402 if (STRINGP (tem))
403 doc = tem;
404 else if (NATNUMP (tem) || CONSP (tem))
405 doc = tem;
406 else
407 return Qnil;
408 }
409 else if (STRINGP (fun) || VECTORP (fun))
410 {
411 return build_string ("Keyboard macro.");
412 }
413 else if (CONSP (fun))
414 {
415 funcar = Fcar (fun);
416 if (!SYMBOLP (funcar))
417 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
418 else if (EQ (funcar, Qkeymap))
419 return build_string ("Prefix command (definition is a keymap associating keystrokes with commands).");
420 else if (EQ (funcar, Qlambda)
421 || EQ (funcar, Qautoload))
422 {
423 Lisp_Object tem1;
424 tem1 = Fcdr (Fcdr (fun));
425 tem = Fcar (tem1);
426 if (STRINGP (tem))
427 doc = tem;
428 /* Handle a doc reference--but these never come last
429 in the function body, so reject them if they are last. */
430 else if ((NATNUMP (tem) || (CONSP (tem) && INTEGERP (XCDR (tem))))
431 && !NILP (XCDR (tem1)))
432 doc = tem;
433 else
434 return Qnil;
435 }
436 else if (EQ (funcar, Qmacro))
437 return Fdocumentation (Fcdr (fun), raw);
438 else
439 goto oops;
440 }
441 else
442 {
443 oops:
444 Fsignal (Qinvalid_function, Fcons (fun, Qnil));
445 }
446
447 /* If DOC is 0, it's typically because of a dumped file missing
448 from the DOC file (bug in src/Makefile.in). */
449 if (EQ (doc, make_number (0)))
450 doc = Qnil;
451 if (INTEGERP (doc) || CONSP (doc))
452 {
453 Lisp_Object tem;
454 tem = get_doc_string (doc, 0, 0);
455 if (NILP (tem) && try_reload)
456 {
457 /* The file is newer, we need to reset the pointers. */
458 struct gcpro gcpro1, gcpro2;
459 GCPRO2 (function, raw);
460 try_reload = reread_doc_file (Fcar_safe (doc));
461 UNGCPRO;
462 if (try_reload)
463 {
464 try_reload = 0;
465 goto documentation;
466 }
467 }
468 else
469 doc = tem;
470 }
471
472 if (NILP (raw))
473 doc = Fsubstitute_command_keys (doc);
474 return doc;
475 }
476
477 DEFUN ("documentation-property", Fdocumentation_property,
478 Sdocumentation_property, 2, 3, 0,
479 doc: /* Return the documentation string that is SYMBOL's PROP property.
480 Third argument RAW omitted or nil means pass the result through
481 `substitute-command-keys' if it is a string.
482
483 This differs from `get' in that it can refer to strings stored in the
484 `etc/DOC' file; and that it evaluates documentation properties that
485 aren't strings. */)
486 (symbol, prop, raw)
487 Lisp_Object symbol, prop, raw;
488 {
489 int try_reload = 1;
490 Lisp_Object tem;
491
492 documentation_property:
493
494 tem = Fget (symbol, prop);
495 if (EQ (tem, make_number (0)))
496 tem = Qnil;
497 if (INTEGERP (tem) || (CONSP (tem) && INTEGERP (XCDR (tem))))
498 {
499 Lisp_Object doc = tem;
500 tem = get_doc_string (tem, 0, 0);
501 if (NILP (tem) && try_reload)
502 {
503 /* The file is newer, we need to reset the pointers. */
504 struct gcpro gcpro1, gcpro2, gcpro3;
505 GCPRO3 (symbol, prop, raw);
506 try_reload = reread_doc_file (Fcar_safe (doc));
507 UNGCPRO;
508 if (try_reload)
509 {
510 try_reload = 0;
511 goto documentation_property;
512 }
513 }
514 }
515 else if (!STRINGP (tem))
516 /* Feval protects its argument. */
517 tem = Feval (tem);
518
519 if (NILP (raw) && STRINGP (tem))
520 tem = Fsubstitute_command_keys (tem);
521 return tem;
522 }
523 \f
524 /* Scanning the DOC files and placing docstring offsets into functions. */
525
526 static void
527 store_function_docstring (fun, offset)
528 Lisp_Object fun;
529 /* Use EMACS_INT because we get this from pointer subtraction. */
530 EMACS_INT offset;
531 {
532 fun = indirect_function (fun);
533
534 /* The type determines where the docstring is stored. */
535
536 /* Lisp_Subrs have a slot for it. */
537 if (SUBRP (fun))
538 XSUBR (fun)->doc = (char *) - offset;
539
540 /* If it's a lisp form, stick it in the form. */
541 else if (CONSP (fun))
542 {
543 Lisp_Object tem;
544
545 tem = XCAR (fun);
546 if (EQ (tem, Qlambda) || EQ (tem, Qautoload))
547 {
548 tem = Fcdr (Fcdr (fun));
549 if (CONSP (tem) && INTEGERP (XCAR (tem)))
550 XSETCARFASTINT (tem, offset);
551 }
552 else if (EQ (tem, Qmacro))
553 store_function_docstring (XCDR (fun), offset);
554 }
555
556 /* Bytecode objects sometimes have slots for it. */
557 else if (COMPILEDP (fun))
558 {
559 /* This bytecode object must have a slot for the
560 docstring, since we've found a docstring for it. */
561 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_DOC_STRING)
562 XSETFASTINT (AREF (fun, COMPILED_DOC_STRING), offset);
563 }
564 }
565
566
567 DEFUN ("Snarf-documentation", Fsnarf_documentation, Ssnarf_documentation,
568 1, 1, 0,
569 doc: /* Used during Emacs initialization to scan the `etc/DOC...' file.
570 This searches the `etc/DOC...' file for doc strings and
571 records them in function and variable definitions.
572 The function takes one argument, FILENAME, a string;
573 it specifies the file name (without a directory) of the DOC file.
574 That file is found in `../etc' now; later, when the dumped Emacs is run,
575 the same file name is found in the `doc-directory'. */)
576 (filename)
577 Lisp_Object filename;
578 {
579 int fd;
580 char buf[1024 + 1];
581 register int filled;
582 register int pos;
583 register char *p, *end;
584 Lisp_Object sym;
585 char *name;
586 int skip_file = 0;
587
588 CHECK_STRING (filename);
589
590 if
591 #ifndef CANNOT_DUMP
592 (!NILP (Vpurify_flag))
593 #else /* CANNOT_DUMP */
594 (0)
595 #endif /* CANNOT_DUMP */
596 {
597 name = (char *) alloca (SCHARS (filename) + 14);
598 strcpy (name, "../etc/");
599 }
600 else
601 {
602 CHECK_STRING (Vdoc_directory);
603 name = (char *) alloca (SCHARS (filename)
604 + SCHARS (Vdoc_directory) + 1);
605 strcpy (name, SDATA (Vdoc_directory));
606 }
607 strcat (name, SDATA (filename)); /*** Add this line ***/
608 munge_doc_file_name (name);
609
610 /* Vbuild_files is nil when temacs is run, and non-nil after that. */
611 if (NILP (Vbuild_files))
612 {
613 size_t cp_size = 0;
614 size_t to_read;
615 int nr_read;
616 char *cp = NULL;
617 char *beg, *end;
618
619 fd = emacs_open ("buildobj.lst", O_RDONLY, 0);
620 if (fd < 0)
621 report_file_error ("Opening file buildobj.lst", Qnil);
622
623 filled = 0;
624 for (;;)
625 {
626 cp_size += 1024;
627 to_read = cp_size - 1 - filled;
628 cp = xrealloc (cp, cp_size);
629 nr_read = emacs_read (fd, &cp[filled], to_read);
630 filled += nr_read;
631 if (nr_read < to_read)
632 break;
633 }
634
635 emacs_close (fd);
636 cp[filled] = 0;
637
638 for (beg = cp; *beg; beg = end)
639 {
640 int len;
641
642 while (*beg && isspace (*beg)) ++beg;
643
644 for (end = beg; *end && ! isspace (*end); ++end)
645 if (*end == '/') beg = end+1; /* skip directory part */
646
647 len = end - beg;
648 if (len > 4 && end[-4] == '.' && end[-3] == 'o')
649 len -= 2; /* Just take .o if it ends in .obj */
650
651 if (len > 0)
652 Vbuild_files = Fcons (make_string (beg, len), Vbuild_files);
653 }
654
655 xfree (cp);
656 }
657
658 fd = emacs_open (name, O_RDONLY, 0);
659 if (fd < 0)
660 report_file_error ("Opening doc string file",
661 Fcons (build_string (name), Qnil));
662 Vdoc_file_name = filename;
663 filled = 0;
664 pos = 0;
665 while (1)
666 {
667 if (filled < 512)
668 filled += emacs_read (fd, &buf[filled], sizeof buf - 1 - filled);
669 if (!filled)
670 break;
671
672 buf[filled] = 0;
673 p = buf;
674 end = buf + (filled < 512 ? filled : filled - 128);
675 while (p != end && *p != '\037') p++;
676 /* p points to ^_Ffunctionname\n or ^_Vvarname\n. */
677 if (p != end)
678 {
679 end = (char *) index (p, '\n');
680
681 /* See if this is a file name, and if it is a file in build-files. */
682 if (p[1] == 'S' && end - p > 4 && end[-2] == '.'
683 && (end[-1] == 'o' || end[-1] == 'c'))
684 {
685 int len = end - p - 2;
686 char *fromfile = alloca (len + 1);
687 strncpy (fromfile, &p[2], len);
688 fromfile[len] = 0;
689 if (fromfile[len-1] == 'c')
690 fromfile[len-1] = 'o';
691
692 if (EQ (Fmember (build_string (fromfile), Vbuild_files), Qnil))
693 skip_file = 1;
694 else
695 skip_file = 0;
696 }
697
698 sym = oblookup (Vobarray, p + 2,
699 multibyte_chars_in_text (p + 2, end - p - 2),
700 end - p - 2);
701 if (! skip_file && SYMBOLP (sym))
702 {
703 /* Attach a docstring to a variable? */
704 if (p[1] == 'V')
705 {
706 /* Install file-position as variable-documentation property
707 and make it negative for a user-variable
708 (doc starts with a `*'). */
709 Fput (sym, Qvariable_documentation,
710 make_number ((pos + end + 1 - buf)
711 * (end[1] == '*' ? -1 : 1)));
712 }
713
714 /* Attach a docstring to a function? */
715 else if (p[1] == 'F')
716 store_function_docstring (sym, pos + end + 1 - buf);
717
718 else if (p[1] == 'S')
719 ; /* Just a source file name boundary marker. Ignore it. */
720
721 else
722 error ("DOC file invalid at position %d", pos);
723 }
724 }
725 pos += end - buf;
726 filled -= end - buf;
727 bcopy (end, buf, filled);
728 }
729 emacs_close (fd);
730 return Qnil;
731 }
732 \f
733 DEFUN ("substitute-command-keys", Fsubstitute_command_keys,
734 Ssubstitute_command_keys, 1, 1, 0,
735 doc: /* Substitute key descriptions for command names in STRING.
736 Return a new string which is STRING with substrings of the form \\=\\[COMMAND]
737 replaced by either: a keystroke sequence that will invoke COMMAND,
738 or "M-x COMMAND" if COMMAND is not on any keys.
739 Substrings of the form \\=\\{MAPVAR} are replaced by summaries
740 \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.
741 Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR
742 as the keymap for future \\=\\[COMMAND] substrings.
743 \\=\\= quotes the following character and is discarded;
744 thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output. */)
745 (string)
746 Lisp_Object string;
747 {
748 unsigned char *buf;
749 int changed = 0;
750 register unsigned char *strp;
751 register unsigned char *bufp;
752 int idx;
753 int bsize;
754 Lisp_Object tem;
755 Lisp_Object keymap;
756 unsigned char *start;
757 int length, length_byte;
758 Lisp_Object name;
759 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
760 int multibyte;
761 int nchars;
762
763 if (NILP (string))
764 return Qnil;
765
766 CHECK_STRING (string);
767 tem = Qnil;
768 keymap = Qnil;
769 name = Qnil;
770 GCPRO4 (string, tem, keymap, name);
771
772 multibyte = STRING_MULTIBYTE (string);
773 nchars = 0;
774
775 /* KEYMAP is either nil (which means search all the active keymaps)
776 or a specified local map (which means search just that and the
777 global map). If non-nil, it might come from Voverriding_local_map,
778 or from a \\<mapname> construct in STRING itself.. */
779 keymap = current_kboard->Voverriding_terminal_local_map;
780 if (NILP (keymap))
781 keymap = Voverriding_local_map;
782
783 bsize = SBYTES (string);
784 bufp = buf = (unsigned char *) xmalloc (bsize);
785
786 strp = SDATA (string);
787 while (strp < SDATA (string) + SBYTES (string))
788 {
789 if (strp[0] == '\\' && strp[1] == '=')
790 {
791 /* \= quotes the next character;
792 thus, to put in \[ without its special meaning, use \=\[. */
793 changed = 1;
794 strp += 2;
795 if (multibyte)
796 {
797 int len;
798 int maxlen = SDATA (string) + SBYTES (string) - strp;
799
800 STRING_CHAR_AND_LENGTH (strp, maxlen, len);
801 if (len == 1)
802 *bufp = *strp;
803 else
804 bcopy (strp, bufp, len);
805 strp += len;
806 bufp += len;
807 nchars++;
808 }
809 else
810 *bufp++ = *strp++, nchars++;
811 }
812 else if (strp[0] == '\\' && strp[1] == '[')
813 {
814 int start_idx;
815
816 changed = 1;
817 strp += 2; /* skip \[ */
818 start = strp;
819 start_idx = start - SDATA (string);
820
821 while ((strp - SDATA (string)
822 < SBYTES (string))
823 && *strp != ']')
824 strp++;
825 length_byte = strp - start;
826
827 strp++; /* skip ] */
828
829 /* Save STRP in IDX. */
830 idx = strp - SDATA (string);
831 name = Fintern (make_string (start, length_byte), Qnil);
832
833 /* Ignore remappings unless there are no ordinary bindings. */
834 tem = Fwhere_is_internal (name, keymap, Qt, Qnil, Qt);
835 if (NILP (tem))
836 tem = Fwhere_is_internal (name, keymap, Qt, Qnil, Qnil);
837
838 /* Note the Fwhere_is_internal can GC, so we have to take
839 relocation of string contents into account. */
840 strp = SDATA (string) + idx;
841 start = SDATA (string) + start_idx;
842
843 if (NILP (tem)) /* but not on any keys */
844 {
845 int offset = bufp - buf;
846 buf = (unsigned char *) xrealloc (buf, bsize += 4);
847 bufp = buf + offset;
848 bcopy ("M-x ", bufp, 4);
849 bufp += 4;
850 nchars += 4;
851 if (multibyte)
852 length = multibyte_chars_in_text (start, length_byte);
853 else
854 length = length_byte;
855 goto subst;
856 }
857 else
858 { /* function is on a key */
859 tem = Fkey_description (tem, Qnil);
860 goto subst_string;
861 }
862 }
863 /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
864 \<foo> just sets the keymap used for \[cmd]. */
865 else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
866 {
867 struct buffer *oldbuf;
868 int start_idx;
869 /* This is for computing the SHADOWS arg for describe_map_tree. */
870 Lisp_Object active_maps = Fcurrent_active_maps (Qnil);
871 Lisp_Object earlier_maps;
872
873 changed = 1;
874 strp += 2; /* skip \{ or \< */
875 start = strp;
876 start_idx = start - SDATA (string);
877
878 while ((strp - SDATA (string) < SBYTES (string))
879 && *strp != '}' && *strp != '>')
880 strp++;
881
882 length_byte = strp - start;
883 strp++; /* skip } or > */
884
885 /* Save STRP in IDX. */
886 idx = strp - SDATA (string);
887
888 /* Get the value of the keymap in TEM, or nil if undefined.
889 Do this while still in the user's current buffer
890 in case it is a local variable. */
891 name = Fintern (make_string (start, length_byte), Qnil);
892 tem = Fboundp (name);
893 if (! NILP (tem))
894 {
895 tem = Fsymbol_value (name);
896 if (! NILP (tem))
897 {
898 tem = get_keymap (tem, 0, 1);
899 /* Note that get_keymap can GC. */
900 strp = SDATA (string) + idx;
901 start = SDATA (string) + start_idx;
902 }
903 }
904
905 /* Now switch to a temp buffer. */
906 oldbuf = current_buffer;
907 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
908
909 if (NILP (tem))
910 {
911 name = Fsymbol_name (name);
912 insert_string ("\nUses keymap \"");
913 insert_from_string (name, 0, 0,
914 SCHARS (name),
915 SBYTES (name), 1);
916 insert_string ("\", which is not currently defined.\n");
917 if (start[-1] == '<') keymap = Qnil;
918 }
919 else if (start[-1] == '<')
920 keymap = tem;
921 else
922 {
923 /* Get the list of active keymaps that precede this one.
924 If this one's not active, get nil. */
925 earlier_maps = Fcdr (Fmemq (tem, Freverse (active_maps)));
926 describe_map_tree (tem, 1, Fnreverse (earlier_maps),
927 Qnil, (char *)0, 1, 0, 0, 1);
928 }
929 tem = Fbuffer_string ();
930 Ferase_buffer ();
931 set_buffer_internal (oldbuf);
932
933 subst_string:
934 start = SDATA (tem);
935 length = SCHARS (tem);
936 length_byte = SBYTES (tem);
937 subst:
938 {
939 int offset = bufp - buf;
940 buf = (unsigned char *) xrealloc (buf, bsize += length_byte);
941 bufp = buf + offset;
942 bcopy (start, bufp, length_byte);
943 bufp += length_byte;
944 nchars += length;
945 /* Check STRING again in case gc relocated it. */
946 strp = (unsigned char *) SDATA (string) + idx;
947 }
948 }
949 else if (! multibyte) /* just copy other chars */
950 *bufp++ = *strp++, nchars++;
951 else
952 {
953 int len;
954 int maxlen = SDATA (string) + SBYTES (string) - strp;
955
956 STRING_CHAR_AND_LENGTH (strp, maxlen, len);
957 if (len == 1)
958 *bufp = *strp;
959 else
960 bcopy (strp, bufp, len);
961 strp += len;
962 bufp += len;
963 nchars++;
964 }
965 }
966
967 if (changed) /* don't bother if nothing substituted */
968 tem = make_string_from_bytes (buf, nchars, bufp - buf);
969 else
970 tem = string;
971 xfree (buf);
972 RETURN_UNGCPRO (tem);
973 }
974 \f
975 void
976 syms_of_doc ()
977 {
978 Qfunction_documentation = intern ("function-documentation");
979 staticpro (&Qfunction_documentation);
980
981 DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name,
982 doc: /* Name of file containing documentation strings of built-in symbols. */);
983 Vdoc_file_name = Qnil;
984
985 DEFVAR_LISP ("build-files", &Vbuild_files,
986 doc: /* A list of files used to build this Emacs binary. */);
987 Vbuild_files = Qnil;
988
989 defsubr (&Sdocumentation);
990 defsubr (&Sdocumentation_property);
991 defsubr (&Ssnarf_documentation);
992 defsubr (&Ssubstitute_command_keys);
993 }
994
995 /* arch-tag: 56281d4d-6949-43e2-be2e-f6517de744ba
996 (do not change this comment) */