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)
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. */
25 #include <sys/types.h>
26 #include <sys/file.h> /* Must be after sys/types.h for USG and BSD4_1*/
47 extern char *index
P_ ((const char *, int));
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. */
59 munge_doc_file_name (name
)
64 /* For VMS versions with limited file name syntax,
65 convert the name to something VMS will allow. */
73 #endif /* not VMS4_4 */
75 strcpy (name
, sys_translate_unix (name
));
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. */
91 read_bytecode_char (unreadflag
)
96 read_bytecode_pointer
--;
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),
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. */
122 get_doc_string (filepos
, unibyte
, definition
)
124 int unibyte
, definition
;
129 register char *p
, *p1
;
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
));
148 position
= - position
;
150 if (!STRINGP (Vdoc_directory
))
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
);
162 minsize
= SCHARS (Vdoc_directory
);
163 /* sizeof ("../etc/") == 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
);
173 name
= (char *) SDATA (file
);
176 fd
= emacs_open (name
, O_RDONLY
, 0);
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);
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))
202 error ("Position %ld out of range in doc string file \"%s\"",
206 /* Read the doc string into get_doc_string_buffer.
207 P points beyond the data just read. */
209 p
= get_doc_string_buffer
;
212 int space_left
= (get_doc_string_buffer_size
213 - (p
- get_doc_string_buffer
));
216 /* Allocate or grow the buffer if we need to. */
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
);
237 error ("Read error on documentation file");
242 if (p
== get_doc_string_buffer
)
243 p1
= (char *) index (p
+ offset
, '\037');
245 p1
= (char *) index (p
, '\037');
256 /* Sanity checking. */
260 if (get_doc_string_buffer
[offset
- test
++] != ' ')
262 while (get_doc_string_buffer
[offset
- test
] >= '0'
263 && get_doc_string_buffer
[offset
- test
] <= '9')
265 if (get_doc_string_buffer
[offset
- test
++] != '@'
266 || get_doc_string_buffer
[offset
- test
] != '#')
272 if (get_doc_string_buffer
[offset
- test
++] != '\n')
274 while (get_doc_string_buffer
[offset
- test
] > ' ')
276 if (get_doc_string_buffer
[offset
- test
] != '\037')
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
;
299 error ("Invalid data in documentation file -- ^A followed by code 0%o", c
);
305 /* If DEFINITION, read from this buffer
306 the same way we would read bytes from a file. */
309 read_bytecode_pointer
= get_doc_string_buffer
+ offset
;
310 return Fread (Qlambda
);
314 return make_unibyte_string (get_doc_string_buffer
+ offset
,
315 to
- (get_doc_string_buffer
+ offset
));
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
,
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. */
333 read_doc_string (filepos
)
336 return get_doc_string (filepos
, 0, 1);
340 reread_doc_file (file
)
344 Lisp_Object reply
, prompt
[3];
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
));
357 Fsnarf_documentation (Vdoc_file_name
);
359 Fload (file
, Qt
, Qt
, Qt
, Qnil
);
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'. */)
369 Lisp_Object function
, raw
;
373 Lisp_Object tem
, doc
;
380 if (SYMBOLP (function
)
381 && (tem
= Fget (function
, Qfunction_documentation
),
383 return Fdocumentation_property (function
, Qfunction_documentation
, raw
);
385 fun
= Findirect_function (function
);
388 if (XSUBR (fun
)->doc
== 0)
390 else if ((EMACS_INT
) XSUBR (fun
)->doc
>= 0)
391 doc
= build_string (XSUBR (fun
)->doc
);
393 doc
= make_number ((EMACS_INT
) XSUBR (fun
)->doc
);
395 else if (COMPILEDP (fun
))
397 if ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) <= COMPILED_DOC_STRING
)
399 tem
= AREF (fun
, COMPILED_DOC_STRING
);
402 else if (NATNUMP (tem
) || CONSP (tem
))
407 else if (STRINGP (fun
) || VECTORP (fun
))
409 return build_string ("Keyboard macro.");
411 else if (CONSP (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
))
422 tem1
= Fcdr (Fcdr (fun
));
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
)))
434 else if (EQ (funcar
, Qmacro
))
435 return Fdocumentation (Fcdr (fun
), raw
);
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)))
449 if (INTEGERP (doc
) || CONSP (doc
))
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
));
471 doc
= Fsubstitute_command_keys (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
485 Lisp_Object symbol
, prop
, raw
;
490 documentation_property
:
492 tem
= Fget (symbol
, prop
);
493 if (EQ (tem
, make_number (0)))
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
));
509 goto documentation_property
;
513 else if (!STRINGP (tem
))
514 /* Feval protects its argument. */
517 if (NILP (raw
) && STRINGP (tem
))
518 tem
= Fsubstitute_command_keys (tem
);
522 /* Scanning the DOC files and placing docstring offsets into functions. */
525 store_function_docstring (fun
, offset
)
527 /* Use EMACS_INT because we get this from pointer subtraction. */
530 fun
= indirect_function (fun
);
532 /* The type determines where the docstring is stored. */
534 /* Lisp_Subrs have a slot for it. */
536 XSUBR (fun
)->doc
= (char *) - offset
;
538 /* If it's a lisp form, stick it in the form. */
539 else if (CONSP (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
,
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'. */)
575 Lisp_Object filename
;
581 register char *p
, *end
;
585 CHECK_STRING (filename
);
589 (!NILP (Vpurify_flag
))
590 #else /* CANNOT_DUMP */
592 #endif /* CANNOT_DUMP */
594 name
= (char *) alloca (SCHARS (filename
) + 14);
595 strcpy (name
, "../etc/");
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 ***/
607 /* For VMS versions with limited file name syntax,
608 convert the name to something VMS will allow. */
617 strcpy (name
, sys_translate_unix (name
));
621 fd
= emacs_open (name
, O_RDONLY
, 0);
623 report_file_error ("Opening doc string file",
624 Fcons (build_string (name
), Qnil
));
625 Vdoc_file_name
= filename
;
631 filled
+= emacs_read (fd
, &buf
[filled
], sizeof buf
- 1 - filled
);
637 end
= buf
+ (filled
< 512 ? filled
: filled
- 128);
638 while (p
!= end
&& *p
!= '\037') p
++;
639 /* p points to ^_Ffunctionname\n or ^_Vvarname\n. */
642 end
= (char *) index (p
, '\n');
643 sym
= oblookup (Vobarray
, p
+ 2,
644 multibyte_chars_in_text (p
+ 2, end
- p
- 2),
648 /* Attach a docstring to a variable? */
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. */
667 error ("DOC file invalid at position %d", pos
);
672 bcopy (end
, buf
, filled
);
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. */)
695 register unsigned char *strp
;
696 register unsigned char *bufp
;
701 unsigned char *start
;
702 int length
, length_byte
;
704 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
711 CHECK_STRING (string
);
715 GCPRO4 (string
, tem
, keymap
, name
);
717 multibyte
= STRING_MULTIBYTE (string
);
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
;
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 \=\[. */
743 int maxlen
= SDATA (string
) + SBYTES (string
) - strp
;
745 STRING_CHAR_AND_LENGTH (strp
, maxlen
, len
);
749 bcopy (strp
, bufp
, len
);
755 *bufp
++ = *strp
++, nchars
++;
757 else if (strp
[0] == '\\' && strp
[1] == '[')
759 Lisp_Object firstkey
;
763 strp
+= 2; /* skip \[ */
765 start_idx
= start
- SDATA (string
);
767 while ((strp
- SDATA (string
)
771 length_byte
= strp
- start
;
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
);
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);
794 bcopy ("M-x ", bufp
, 4);
798 length
= multibyte_chars_in_text (start
, length_byte
);
800 length
= length_byte
;
804 { /* function is on a key */
805 tem
= Fkey_description (tem
, Qnil
);
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
;
817 strp
+= 2; /* skip \{ or \< */
819 start_idx
= start
- SDATA (string
);
821 while ((strp
- SDATA (string
) < SBYTES (string
))
822 && *strp
!= '}' && *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
);
838 tem
= Fsymbol_value (name
);
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
));
854 name
= Fsymbol_name (name
);
855 insert_string ("\nUses keymap \"");
856 insert_from_string (name
, 0, 0,
859 insert_string ("\", which is not currently defined.\n");
860 if (start
[-1] == '<') keymap
= Qnil
;
862 else if (start
[-1] == '<')
865 describe_map_tree (tem
, 1, Qnil
, Qnil
, (char *)0, 1, 0, 0);
866 tem
= Fbuffer_string ();
868 set_buffer_internal (oldbuf
);
872 length
= SCHARS (tem
);
873 length_byte
= SBYTES (tem
);
876 int offset
= bufp
- buf
;
877 buf
= (unsigned char *) xrealloc (buf
, bsize
+= length_byte
);
879 bcopy (start
, bufp
, length_byte
);
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
++;
891 int maxlen
= SDATA (string
) + SBYTES (string
) - strp
;
893 STRING_CHAR_AND_LENGTH (strp
, maxlen
, len
);
897 bcopy (strp
, bufp
, len
);
904 if (changed
) /* don't bother if nothing substituted */
905 tem
= make_string_from_bytes (buf
, nchars
, bufp
- buf
);
909 RETURN_UNGCPRO (tem
);
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) */