(cperl-switch-to-doc-buffer): Don't use interactive-p.
[emacs.git] / src / doc.c
blob82df9134f61197ed0f7855826506ec9f79bc075d
1 /* Record indices of function doc strings stored in a file.
2 Copyright (C) 1985, 86,93,94,95,97,98,99,2000,04
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
25 #include <sys/types.h>
26 #include <sys/file.h> /* Must be after sys/types.h for USG and BSD4_1*/
28 #ifdef HAVE_FCNTL_H
29 #include <fcntl.h>
30 #endif
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
36 #ifndef O_RDONLY
37 #define O_RDONLY 0
38 #endif
40 #include "lisp.h"
41 #include "buffer.h"
42 #include "keyboard.h"
43 #include "charset.h"
44 #include "keymap.h"
46 #ifdef HAVE_INDEX
47 extern char *index P_ ((const char *, int));
48 #endif
50 Lisp_Object Vdoc_file_name;
52 Lisp_Object Qfunction_documentation;
54 extern Lisp_Object Voverriding_local_map;
56 /* For VMS versions with limited file name syntax,
57 convert the name to something VMS will allow. */
58 static void
59 munge_doc_file_name (name)
60 char *name;
62 #ifdef VMS
63 #ifndef VMS4_4
64 /* For VMS versions with limited file name syntax,
65 convert the name to something VMS will allow. */
66 p = name;
67 while (*p)
69 if (*p == '-')
70 *p = '_';
71 p++;
73 #endif /* not VMS4_4 */
74 #ifdef VMS4_4
75 strcpy (name, sys_translate_unix (name));
76 #endif /* VMS4_4 */
77 #endif /* VMS */
80 /* Buffer used for reading from documentation file. */
81 static char *get_doc_string_buffer;
82 static int get_doc_string_buffer_size;
84 static unsigned char *read_bytecode_pointer;
85 Lisp_Object Fsnarf_documentation P_ ((Lisp_Object));
87 /* readchar in lread.c calls back here to fetch the next byte.
88 If UNREADFLAG is 1, we unread a byte. */
90 int
91 read_bytecode_char (unreadflag)
92 int unreadflag;
94 if (unreadflag)
96 read_bytecode_pointer--;
97 return 0;
99 return *read_bytecode_pointer++;
102 /* Extract a doc string from a file. FILEPOS says where to get it.
103 If it is an integer, use that position in the standard DOC-... file.
104 If it is (FILE . INTEGER), use FILE as the file name
105 and INTEGER as the position in that file.
106 But if INTEGER is negative, make it positive.
107 (A negative integer is used for user variables, so we can distinguish
108 them without actually fetching the doc string.)
110 If the location does not point to the beginning of a docstring
111 (e.g. because the file has been modified and the location is stale),
112 return nil.
114 If UNIBYTE is nonzero, always make a unibyte string.
116 If DEFINITION is nonzero, assume this is for reading
117 a dynamic function definition; convert the bytestring
118 and the constants vector with appropriate byte handling,
119 and return a cons cell. */
121 Lisp_Object
122 get_doc_string (filepos, unibyte, definition)
123 Lisp_Object filepos;
124 int unibyte, definition;
126 char *from, *to;
127 register int fd;
128 register char *name;
129 register char *p, *p1;
130 int minsize;
131 int offset, position;
132 Lisp_Object file, tem;
134 if (INTEGERP (filepos))
136 file = Vdoc_file_name;
137 position = XINT (filepos);
139 else if (CONSP (filepos))
141 file = XCAR (filepos);
142 position = XINT (XCDR (filepos));
144 else
145 return Qnil;
147 if (position < 0)
148 position = - position;
150 if (!STRINGP (Vdoc_directory))
151 return Qnil;
153 if (!STRINGP (file))
154 return Qnil;
156 /* Put the file name in NAME as a C string.
157 If it is relative, combine it with Vdoc_directory. */
159 tem = Ffile_name_absolute_p (file);
160 if (NILP (tem))
162 minsize = SCHARS (Vdoc_directory);
163 /* sizeof ("../etc/") == 8 */
164 if (minsize < 8)
165 minsize = 8;
166 name = (char *) alloca (minsize + SCHARS (file) + 8);
167 strcpy (name, SDATA (Vdoc_directory));
168 strcat (name, SDATA (file));
169 munge_doc_file_name (name);
171 else
173 name = (char *) SDATA (file);
176 fd = emacs_open (name, O_RDONLY, 0);
177 if (fd < 0)
179 #ifndef CANNOT_DUMP
180 if (!NILP (Vpurify_flag))
182 /* Preparing to dump; DOC file is probably not installed.
183 So check in ../etc. */
184 strcpy (name, "../etc/");
185 strcat (name, SDATA (file));
186 munge_doc_file_name (name);
188 fd = emacs_open (name, O_RDONLY, 0);
190 #endif
191 if (fd < 0)
192 error ("Cannot open doc string file \"%s\"", name);
195 /* Seek only to beginning of disk block. */
196 /* Make sure we read at least 1024 bytes before `position'
197 so we can check the leading text for consistency. */
198 offset = min (position, max (1024, position % (8 * 1024)));
199 if (0 > lseek (fd, position - offset, 0))
201 emacs_close (fd);
202 error ("Position %ld out of range in doc string file \"%s\"",
203 position, name);
206 /* Read the doc string into get_doc_string_buffer.
207 P points beyond the data just read. */
209 p = get_doc_string_buffer;
210 while (1)
212 int space_left = (get_doc_string_buffer_size
213 - (p - get_doc_string_buffer));
214 int nread;
216 /* Allocate or grow the buffer if we need to. */
217 if (space_left == 0)
219 int in_buffer = p - get_doc_string_buffer;
220 get_doc_string_buffer_size += 16 * 1024;
221 get_doc_string_buffer
222 = (char *) xrealloc (get_doc_string_buffer,
223 get_doc_string_buffer_size + 1);
224 p = get_doc_string_buffer + in_buffer;
225 space_left = (get_doc_string_buffer_size
226 - (p - get_doc_string_buffer));
229 /* Read a disk block at a time.
230 If we read the same block last time, maybe skip this? */
231 if (space_left > 1024 * 8)
232 space_left = 1024 * 8;
233 nread = emacs_read (fd, p, space_left);
234 if (nread < 0)
236 emacs_close (fd);
237 error ("Read error on documentation file");
239 p[nread] = 0;
240 if (!nread)
241 break;
242 if (p == get_doc_string_buffer)
243 p1 = (char *) index (p + offset, '\037');
244 else
245 p1 = (char *) index (p, '\037');
246 if (p1)
248 *p1 = 0;
249 p = p1;
250 break;
252 p += nread;
254 emacs_close (fd);
256 /* Sanity checking. */
257 if (CONSP (filepos))
259 int test = 1;
260 if (get_doc_string_buffer[offset - test++] != ' ')
261 return Qnil;
262 while (get_doc_string_buffer[offset - test] >= '0'
263 && get_doc_string_buffer[offset - test] <= '9')
264 test++;
265 if (get_doc_string_buffer[offset - test++] != '@'
266 || get_doc_string_buffer[offset - test] != '#')
267 return Qnil;
269 else
271 int test = 1;
272 if (get_doc_string_buffer[offset - test++] != '\n')
273 return Qnil;
274 while (get_doc_string_buffer[offset - test] > ' ')
275 test++;
276 if (get_doc_string_buffer[offset - test] != '\037')
277 return Qnil;
280 /* Scan the text and perform quoting with ^A (char code 1).
281 ^A^A becomes ^A, ^A0 becomes a null char, and ^A_ becomes a ^_. */
282 from = get_doc_string_buffer + offset;
283 to = get_doc_string_buffer + offset;
284 while (from != p)
286 if (*from == 1)
288 int c;
290 from++;
291 c = *from++;
292 if (c == 1)
293 *to++ = c;
294 else if (c == '0')
295 *to++ = 0;
296 else if (c == '_')
297 *to++ = 037;
298 else
299 error ("Invalid data in documentation file -- ^A followed by code 0%o", c);
301 else
302 *to++ = *from++;
305 /* If DEFINITION, read from this buffer
306 the same way we would read bytes from a file. */
307 if (definition)
309 read_bytecode_pointer = get_doc_string_buffer + offset;
310 return Fread (Qlambda);
313 if (unibyte)
314 return make_unibyte_string (get_doc_string_buffer + offset,
315 to - (get_doc_string_buffer + offset));
316 else
318 /* Let the data determine whether the string is multibyte,
319 even if Emacs is running in --unibyte mode. */
320 int nchars = multibyte_chars_in_text (get_doc_string_buffer + offset,
321 to - (get_doc_string_buffer + offset));
322 return make_string_from_bytes (get_doc_string_buffer + offset,
323 nchars,
324 to - (get_doc_string_buffer + offset));
328 /* Get a string from position FILEPOS and pass it through the Lisp reader.
329 We use this for fetching the bytecode string and constants vector
330 of a compiled function from the .elc file. */
332 Lisp_Object
333 read_doc_string (filepos)
334 Lisp_Object filepos;
336 return get_doc_string (filepos, 0, 1);
339 static int
340 reread_doc_file (file)
341 Lisp_Object file;
343 #if 0
344 Lisp_Object reply, prompt[3];
345 struct gcpro gcpro1;
346 GCPRO1 (file);
347 prompt[0] = build_string ("File ");
348 prompt[1] = NILP (file) ? Vdoc_file_name : file;
349 prompt[2] = build_string (" is out of sync. Reload? ");
350 reply = Fy_or_n_p (Fconcat (3, prompt));
351 UNGCPRO;
352 if (NILP (reply))
353 return 0;
354 #endif
356 if (NILP (file))
357 Fsnarf_documentation (Vdoc_file_name);
358 else
359 Fload (file, Qt, Qt, Qt, Qnil);
361 return 1;
364 DEFUN ("documentation", Fdocumentation, Sdocumentation, 1, 2, 0,
365 doc: /* Return the documentation string of FUNCTION.
366 Unless a non-nil second argument RAW is given, the
367 string is passed through `substitute-command-keys'. */)
368 (function, raw)
369 Lisp_Object function, raw;
371 Lisp_Object fun;
372 Lisp_Object funcar;
373 Lisp_Object tem, doc;
374 int try_reload = 1;
376 documentation:
378 doc = Qnil;
380 if (SYMBOLP (function)
381 && (tem = Fget (function, Qfunction_documentation),
382 !NILP (tem)))
383 return Fdocumentation_property (function, Qfunction_documentation, raw);
385 fun = Findirect_function (function);
386 if (SUBRP (fun))
388 if (XSUBR (fun)->doc == 0)
389 return Qnil;
390 else if ((EMACS_INT) XSUBR (fun)->doc >= 0)
391 doc = build_string (XSUBR (fun)->doc);
392 else
393 doc = make_number ((EMACS_INT) XSUBR (fun)->doc);
395 else if (COMPILEDP (fun))
397 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) <= COMPILED_DOC_STRING)
398 return Qnil;
399 tem = AREF (fun, COMPILED_DOC_STRING);
400 if (STRINGP (tem))
401 doc = tem;
402 else if (NATNUMP (tem) || CONSP (tem))
403 doc = tem;
404 else
405 return Qnil;
407 else if (STRINGP (fun) || VECTORP (fun))
409 return build_string ("Keyboard macro.");
411 else if (CONSP (fun))
413 funcar = Fcar (fun);
414 if (!SYMBOLP (funcar))
415 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
416 else if (EQ (funcar, Qkeymap))
417 return build_string ("Prefix command (definition is a keymap associating keystrokes with commands).");
418 else if (EQ (funcar, Qlambda)
419 || EQ (funcar, Qautoload))
421 Lisp_Object tem1;
422 tem1 = Fcdr (Fcdr (fun));
423 tem = Fcar (tem1);
424 if (STRINGP (tem))
425 doc = tem;
426 /* Handle a doc reference--but these never come last
427 in the function body, so reject them if they are last. */
428 else if ((NATNUMP (tem) || (CONSP (tem) && INTEGERP (XCDR (tem))))
429 && !NILP (XCDR (tem1)))
430 doc = tem;
431 else
432 return Qnil;
434 else if (EQ (funcar, Qmacro))
435 return Fdocumentation (Fcdr (fun), raw);
436 else
437 goto oops;
439 else
441 oops:
442 Fsignal (Qinvalid_function, Fcons (fun, Qnil));
445 /* If DOC is 0, it's typically because of a dumped file missing
446 from the DOC file (bug in src/Makefile.in). */
447 if (EQ (doc, make_number (0)))
448 doc = Qnil;
449 if (INTEGERP (doc) || CONSP (doc))
451 Lisp_Object tem;
452 tem = get_doc_string (doc, 0, 0);
453 if (NILP (tem) && try_reload)
455 /* The file is newer, we need to reset the pointers. */
456 struct gcpro gcpro1, gcpro2;
457 GCPRO2 (function, raw);
458 try_reload = reread_doc_file (Fcar_safe (doc));
459 UNGCPRO;
460 if (try_reload)
462 try_reload = 0;
463 goto documentation;
466 else
467 doc = tem;
470 if (NILP (raw))
471 doc = Fsubstitute_command_keys (doc);
472 return doc;
475 DEFUN ("documentation-property", Fdocumentation_property,
476 Sdocumentation_property, 2, 3, 0,
477 doc: /* Return the documentation string that is SYMBOL's PROP property.
478 Third argument RAW omitted or nil means pass the result through
479 `substitute-command-keys' if it is a string.
481 This differs from `get' in that it can refer to strings stored in the
482 `etc/DOC' file; and that it evaluates documentation properties that
483 aren't strings. */)
484 (symbol, prop, raw)
485 Lisp_Object symbol, prop, raw;
487 int try_reload = 1;
488 Lisp_Object tem;
490 documentation_property:
492 tem = Fget (symbol, prop);
493 if (EQ (tem, make_number (0)))
494 tem = Qnil;
495 if (INTEGERP (tem) || (CONSP (tem) && INTEGERP (XCDR (tem))))
497 Lisp_Object doc = tem;
498 tem = get_doc_string (tem, 0, 0);
499 if (NILP (tem) && try_reload)
501 /* The file is newer, we need to reset the pointers. */
502 struct gcpro gcpro1, gcpro2, gcpro3;
503 GCPRO3 (symbol, prop, raw);
504 try_reload = reread_doc_file (Fcar_safe (doc));
505 UNGCPRO;
506 if (try_reload)
508 try_reload = 0;
509 goto documentation_property;
513 else if (!STRINGP (tem))
514 /* Feval protects its argument. */
515 tem = Feval (tem);
517 if (NILP (raw) && STRINGP (tem))
518 tem = Fsubstitute_command_keys (tem);
519 return tem;
522 /* Scanning the DOC files and placing docstring offsets into functions. */
524 static void
525 store_function_docstring (fun, offset)
526 Lisp_Object fun;
527 /* Use EMACS_INT because we get this from pointer subtraction. */
528 EMACS_INT offset;
530 fun = indirect_function (fun);
532 /* The type determines where the docstring is stored. */
534 /* Lisp_Subrs have a slot for it. */
535 if (SUBRP (fun))
536 XSUBR (fun)->doc = (char *) - offset;
538 /* If it's a lisp form, stick it in the form. */
539 else if (CONSP (fun))
541 Lisp_Object tem;
543 tem = XCAR (fun);
544 if (EQ (tem, Qlambda) || EQ (tem, Qautoload))
546 tem = Fcdr (Fcdr (fun));
547 if (CONSP (tem) && INTEGERP (XCAR (tem)))
548 XSETCARFASTINT (tem, offset);
550 else if (EQ (tem, Qmacro))
551 store_function_docstring (XCDR (fun), offset);
554 /* Bytecode objects sometimes have slots for it. */
555 else if (COMPILEDP (fun))
557 /* This bytecode object must have a slot for the
558 docstring, since we've found a docstring for it. */
559 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_DOC_STRING)
560 XSETFASTINT (AREF (fun, COMPILED_DOC_STRING), offset);
565 DEFUN ("Snarf-documentation", Fsnarf_documentation, Ssnarf_documentation,
566 1, 1, 0,
567 doc: /* Used during Emacs initialization to scan the `etc/DOC...' file.
568 This searches the `etc/DOC...' file for doc strings and
569 records them in function and variable definitions.
570 The function takes one argument, FILENAME, a string;
571 it specifies the file name (without a directory) of the DOC file.
572 That file is found in `../etc' now; later, when the dumped Emacs is run,
573 the same file name is found in the `doc-directory'. */)
574 (filename)
575 Lisp_Object filename;
577 int fd;
578 char buf[1024 + 1];
579 register int filled;
580 register int pos;
581 register char *p, *end;
582 Lisp_Object sym;
583 char *name;
585 CHECK_STRING (filename);
588 #ifndef CANNOT_DUMP
589 (!NILP (Vpurify_flag))
590 #else /* CANNOT_DUMP */
592 #endif /* CANNOT_DUMP */
594 name = (char *) alloca (SCHARS (filename) + 14);
595 strcpy (name, "../etc/");
597 else
599 CHECK_STRING (Vdoc_directory);
600 name = (char *) alloca (SCHARS (filename)
601 + SCHARS (Vdoc_directory) + 1);
602 strcpy (name, SDATA (Vdoc_directory));
604 strcat (name, SDATA (filename)); /*** Add this line ***/
605 #ifdef VMS
606 #ifndef VMS4_4
607 /* For VMS versions with limited file name syntax,
608 convert the name to something VMS will allow. */
609 p = name;
610 while (*p)
612 if (*p == '-')
613 *p = '_';
614 p++;
616 #else /* VMS4_4 */
617 strcpy (name, sys_translate_unix (name));
618 #endif /* VMS4_4 */
619 #endif /* VMS */
621 fd = emacs_open (name, O_RDONLY, 0);
622 if (fd < 0)
623 report_file_error ("Opening doc string file",
624 Fcons (build_string (name), Qnil));
625 Vdoc_file_name = filename;
626 filled = 0;
627 pos = 0;
628 while (1)
630 if (filled < 512)
631 filled += emacs_read (fd, &buf[filled], sizeof buf - 1 - filled);
632 if (!filled)
633 break;
635 buf[filled] = 0;
636 p = buf;
637 end = buf + (filled < 512 ? filled : filled - 128);
638 while (p != end && *p != '\037') p++;
639 /* p points to ^_Ffunctionname\n or ^_Vvarname\n. */
640 if (p != end)
642 end = (char *) index (p, '\n');
643 sym = oblookup (Vobarray, p + 2,
644 multibyte_chars_in_text (p + 2, end - p - 2),
645 end - p - 2);
646 if (SYMBOLP (sym))
648 /* Attach a docstring to a variable? */
649 if (p[1] == 'V')
651 /* Install file-position as variable-documentation property
652 and make it negative for a user-variable
653 (doc starts with a `*'). */
654 Fput (sym, Qvariable_documentation,
655 make_number ((pos + end + 1 - buf)
656 * (end[1] == '*' ? -1 : 1)));
659 /* Attach a docstring to a function? */
660 else if (p[1] == 'F')
661 store_function_docstring (sym, pos + end + 1 - buf);
663 else if (p[1] == 'S')
664 ; /* Just a source file name boundary marker. Ignore it. */
666 else
667 error ("DOC file invalid at position %d", pos);
670 pos += end - buf;
671 filled -= end - buf;
672 bcopy (end, buf, filled);
674 emacs_close (fd);
675 return Qnil;
678 DEFUN ("substitute-command-keys", Fsubstitute_command_keys,
679 Ssubstitute_command_keys, 1, 1, 0,
680 doc: /* Substitute key descriptions for command names in STRING.
681 Return a new string which is STRING with substrings of the form \\=\\[COMMAND]
682 replaced by either: a keystroke sequence that will invoke COMMAND,
683 or "M-x COMMAND" if COMMAND is not on any keys.
684 Substrings of the form \\=\\{MAPVAR} are replaced by summaries
685 \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.
686 Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR
687 as the keymap for future \\=\\[COMMAND] substrings.
688 \\=\\= quotes the following character and is discarded;
689 thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output. */)
690 (string)
691 Lisp_Object string;
693 unsigned char *buf;
694 int changed = 0;
695 register unsigned char *strp;
696 register unsigned char *bufp;
697 int idx;
698 int bsize;
699 Lisp_Object tem;
700 Lisp_Object keymap;
701 unsigned char *start;
702 int length, length_byte;
703 Lisp_Object name;
704 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
705 int multibyte;
706 int nchars;
708 if (NILP (string))
709 return Qnil;
711 CHECK_STRING (string);
712 tem = Qnil;
713 keymap = Qnil;
714 name = Qnil;
715 GCPRO4 (string, tem, keymap, name);
717 multibyte = STRING_MULTIBYTE (string);
718 nchars = 0;
720 /* KEYMAP is either nil (which means search all the active keymaps)
721 or a specified local map (which means search just that and the
722 global map). If non-nil, it might come from Voverriding_local_map,
723 or from a \\<mapname> construct in STRING itself.. */
724 keymap = current_kboard->Voverriding_terminal_local_map;
725 if (NILP (keymap))
726 keymap = Voverriding_local_map;
728 bsize = SBYTES (string);
729 bufp = buf = (unsigned char *) xmalloc (bsize);
731 strp = SDATA (string);
732 while (strp < SDATA (string) + SBYTES (string))
734 if (strp[0] == '\\' && strp[1] == '=')
736 /* \= quotes the next character;
737 thus, to put in \[ without its special meaning, use \=\[. */
738 changed = 1;
739 strp += 2;
740 if (multibyte)
742 int len;
743 int maxlen = SDATA (string) + SBYTES (string) - strp;
745 STRING_CHAR_AND_LENGTH (strp, maxlen, len);
746 if (len == 1)
747 *bufp = *strp;
748 else
749 bcopy (strp, bufp, len);
750 strp += len;
751 bufp += len;
752 nchars++;
754 else
755 *bufp++ = *strp++, nchars++;
757 else if (strp[0] == '\\' && strp[1] == '[')
759 Lisp_Object firstkey;
760 int start_idx;
762 changed = 1;
763 strp += 2; /* skip \[ */
764 start = strp;
765 start_idx = start - SDATA (string);
767 while ((strp - SDATA (string)
768 < SBYTES (string))
769 && *strp != ']')
770 strp++;
771 length_byte = strp - start;
773 strp++; /* skip ] */
775 /* Save STRP in IDX. */
776 idx = strp - SDATA (string);
777 name = Fintern (make_string (start, length_byte), Qnil);
779 /* Ignore remappings unless there are no ordinary bindings. */
780 tem = Fwhere_is_internal (name, keymap, Qt, Qnil, Qt);
781 if (NILP (tem))
782 tem = Fwhere_is_internal (name, keymap, Qt, Qnil, Qnil);
784 /* Note the Fwhere_is_internal can GC, so we have to take
785 relocation of string contents into account. */
786 strp = SDATA (string) + idx;
787 start = SDATA (string) + start_idx;
789 if (NILP (tem)) /* but not on any keys */
791 int offset = bufp - buf;
792 buf = (unsigned char *) xrealloc (buf, bsize += 4);
793 bufp = buf + offset;
794 bcopy ("M-x ", bufp, 4);
795 bufp += 4;
796 nchars += 4;
797 if (multibyte)
798 length = multibyte_chars_in_text (start, length_byte);
799 else
800 length = length_byte;
801 goto subst;
803 else
804 { /* function is on a key */
805 tem = Fkey_description (tem, Qnil);
806 goto subst_string;
809 /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
810 \<foo> just sets the keymap used for \[cmd]. */
811 else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
813 struct buffer *oldbuf;
814 int start_idx;
816 changed = 1;
817 strp += 2; /* skip \{ or \< */
818 start = strp;
819 start_idx = start - SDATA (string);
821 while ((strp - SDATA (string) < SBYTES (string))
822 && *strp != '}' && *strp != '>')
823 strp++;
825 length_byte = strp - start;
826 strp++; /* skip } or > */
828 /* Save STRP in IDX. */
829 idx = strp - SDATA (string);
831 /* Get the value of the keymap in TEM, or nil if undefined.
832 Do this while still in the user's current buffer
833 in case it is a local variable. */
834 name = Fintern (make_string (start, length_byte), Qnil);
835 tem = Fboundp (name);
836 if (! NILP (tem))
838 tem = Fsymbol_value (name);
839 if (! NILP (tem))
841 tem = get_keymap (tem, 0, 1);
842 /* Note that get_keymap can GC. */
843 strp = SDATA (string) + idx;
844 start = SDATA (string) + start_idx;
848 /* Now switch to a temp buffer. */
849 oldbuf = current_buffer;
850 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
852 if (NILP (tem))
854 name = Fsymbol_name (name);
855 insert_string ("\nUses keymap \"");
856 insert_from_string (name, 0, 0,
857 SCHARS (name),
858 SBYTES (name), 1);
859 insert_string ("\", which is not currently defined.\n");
860 if (start[-1] == '<') keymap = Qnil;
862 else if (start[-1] == '<')
863 keymap = tem;
864 else
865 describe_map_tree (tem, 1, Qnil, Qnil, (char *)0, 1, 0, 0);
866 tem = Fbuffer_string ();
867 Ferase_buffer ();
868 set_buffer_internal (oldbuf);
870 subst_string:
871 start = SDATA (tem);
872 length = SCHARS (tem);
873 length_byte = SBYTES (tem);
874 subst:
876 int offset = bufp - buf;
877 buf = (unsigned char *) xrealloc (buf, bsize += length_byte);
878 bufp = buf + offset;
879 bcopy (start, bufp, length_byte);
880 bufp += length_byte;
881 nchars += length;
882 /* Check STRING again in case gc relocated it. */
883 strp = (unsigned char *) SDATA (string) + idx;
886 else if (! multibyte) /* just copy other chars */
887 *bufp++ = *strp++, nchars++;
888 else
890 int len;
891 int maxlen = SDATA (string) + SBYTES (string) - strp;
893 STRING_CHAR_AND_LENGTH (strp, maxlen, len);
894 if (len == 1)
895 *bufp = *strp;
896 else
897 bcopy (strp, bufp, len);
898 strp += len;
899 bufp += len;
900 nchars++;
904 if (changed) /* don't bother if nothing substituted */
905 tem = make_string_from_bytes (buf, nchars, bufp - buf);
906 else
907 tem = string;
908 xfree (buf);
909 RETURN_UNGCPRO (tem);
912 void
913 syms_of_doc ()
915 Qfunction_documentation = intern ("function-documentation");
916 staticpro (&Qfunction_documentation);
918 DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name,
919 doc: /* Name of file containing documentation strings of built-in symbols. */);
920 Vdoc_file_name = Qnil;
922 defsubr (&Sdocumentation);
923 defsubr (&Sdocumentation_property);
924 defsubr (&Ssnarf_documentation);
925 defsubr (&Ssubstitute_command_keys);
928 /* arch-tag: 56281d4d-6949-43e2-be2e-f6517de744ba
929 (do not change this comment) */