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