doc.c 26 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Record indices of function doc strings stored in a file.
Glenn Morris's avatar
Glenn Morris committed
2

Paul Eggert's avatar
Paul Eggert committed
3 4
Copyright (C) 1985-1986, 1993-1995, 1997-2015 Free Software Foundation,
Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
9
it under the terms of the GNU General Public License as published by
10 11
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
12 13 14 15 16 17 18

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
20 21


22
#include <config.h>
Jim Blandy's avatar
Jim Blandy committed
23

24
#include <errno.h>
Jim Blandy's avatar
Jim Blandy committed
25
#include <sys/types.h>
26
#include <sys/file.h>	/* Must be after sys/types.h for USG.  */
Jim Blandy's avatar
Jim Blandy committed
27
#include <fcntl.h>
28 29
#include <unistd.h>

30 31
#include <c-ctype.h>

Jim Blandy's avatar
Jim Blandy committed
32
#include "lisp.h"
33
#include "character.h"
Jim Blandy's avatar
Jim Blandy committed
34
#include "buffer.h"
Jim Blandy's avatar
Jim Blandy committed
35
#include "keyboard.h"
Stefan Monnier's avatar
Stefan Monnier committed
36
#include "keymap.h"
Jim Blandy's avatar
Jim Blandy committed
37

38 39
/* Buffer used for reading from documentation file.  */
static char *get_doc_string_buffer;
40
static ptrdiff_t get_doc_string_buffer_size;
41

42 43
static unsigned char *read_bytecode_pointer;

Paul Eggert's avatar
Paul Eggert committed
44 45
static char const sibling_etc[] = "../etc/";

46
/* `readchar' in lread.c calls back here to fetch the next byte.
47 48 49
   If UNREADFLAG is 1, we unread a byte.  */

int
50
read_bytecode_char (bool unreadflag)
51 52 53 54 55 56 57 58 59
{
  if (unreadflag)
    {
      read_bytecode_pointer--;
      return 0;
    }
  return *read_bytecode_pointer++;
}

60
/* Extract a doc string from a file.  FILEPOS says where to get it.
61
   If it is an integer, use that position in the standard DOC file.
62
   If it is (FILE . INTEGER), use FILE as the file name
63 64 65
   and INTEGER as the position in that file.
   But if INTEGER is negative, make it positive.
   (A negative integer is used for user variables, so we can distinguish
66 67
   them without actually fetching the doc string.)

68 69 70 71
   If the location does not point to the beginning of a docstring
   (e.g. because the file has been modified and the location is stale),
   return nil.

72
   If UNIBYTE, always make a unibyte string.
73

74
   If DEFINITION, assume this is for reading
75 76 77
   a dynamic function definition; convert the bytestring
   and the constants vector with appropriate byte handling,
   and return a cons cell.  */
78

79
Lisp_Object
80
get_doc_string (Lisp_Object filepos, bool unibyte, bool definition)
81
{
82 83
  char *from, *to, *name, *p, *p1;
  int fd;
84 85
  int offset;
  EMACS_INT position;
Paul Eggert's avatar
Paul Eggert committed
86
  Lisp_Object file, tem, pos;
87
  ptrdiff_t count;
88
  USE_SAFE_ALLOCA;
89

90 91 92
  if (INTEGERP (filepos))
    {
      file = Vdoc_file_name;
Paul Eggert's avatar
Paul Eggert committed
93
      pos = filepos;
94 95 96
    }
  else if (CONSP (filepos))
    {
97
      file = XCAR (filepos);
Paul Eggert's avatar
Paul Eggert committed
98
      pos = XCDR (filepos);
99 100
    }
  else
101 102
    return Qnil;

Paul Eggert's avatar
Paul Eggert committed
103
  position = eabs (XINT (pos));
104

105 106 107 108 109
  if (!STRINGP (Vdoc_directory))
    return Qnil;

  if (!STRINGP (file))
    return Qnil;
110

111 112 113 114
  /* Put the file name in NAME as a C string.
     If it is relative, combine it with Vdoc_directory.  */

  tem = Ffile_name_absolute_p (file);
115
  file = ENCODE_FILE (file);
Paul Eggert's avatar
Paul Eggert committed
116 117 118 119 120 121 122 123
  Lisp_Object docdir
    = NILP (tem) ? ENCODE_FILE (Vdoc_directory) : empty_unibyte_string;
  ptrdiff_t docdir_sizemax = SBYTES (docdir) + 1;
#ifndef CANNOT_DUMP
  docdir_sizemax = max (docdir_sizemax, sizeof sibling_etc);
#endif
  name = SAFE_ALLOCA (docdir_sizemax + SBYTES (file));
  lispstpcpy (lispstpcpy (name, docdir), file);
Jim Blandy's avatar
Jim Blandy committed
124

125
  fd = emacs_open (name, O_RDONLY, 0);
Jim Blandy's avatar
Jim Blandy committed
126
  if (fd < 0)
127 128 129 130 131
    {
#ifndef CANNOT_DUMP
      if (!NILP (Vpurify_flag))
	{
	  /* Preparing to dump; DOC file is probably not installed.
132
	     So check in ../etc.  */
Paul Eggert's avatar
Paul Eggert committed
133
	  lispstpcpy (stpcpy (name, sibling_etc), file);
134

135
	  fd = emacs_open (name, O_RDONLY, 0);
136 137 138
	}
#endif
      if (fd < 0)
139 140
	{
	  SAFE_FREE ();
141 142 143
	  AUTO_STRING (cannot_open, "Cannot open doc string file \"");
	  AUTO_STRING (quote_nl, "\"\n");
	  return concat3 (cannot_open, file, quote_nl);
144
	}
145
    }
146 147
  count = SPECPDL_INDEX ();
  record_unwind_protect_int (close_file_unwind, fd);
148

149
  /* Seek only to beginning of disk block.  */
150 151 152
  /* Make sure we read at least 1024 bytes before `position'
     so we can check the leading text for consistency.  */
  offset = min (position, max (1024, position % (8 * 1024)));
153 154
  if (TYPE_MAXIMUM (off_t) < position
      || lseek (fd, position - offset, 0) < 0)
155 156
    error ("Position %"pI"d out of range in doc string file \"%s\"",
	   position, name);
157

158 159
  /* Read the doc string into get_doc_string_buffer.
     P points beyond the data just read.  */
160

161
  p = get_doc_string_buffer;
162
  while (1)
Jim Blandy's avatar
Jim Blandy committed
163
    {
164
      ptrdiff_t space_left = (get_doc_string_buffer_size - 1
165
			      - (p - get_doc_string_buffer));
166 167
      int nread;

168
      /* Allocate or grow the buffer if we need to.  */
169
      if (space_left <= 0)
170
	{
171
	  ptrdiff_t in_buffer = p - get_doc_string_buffer;
172 173 174
	  get_doc_string_buffer
	    = xpalloc (get_doc_string_buffer, &get_doc_string_buffer_size,
		       16 * 1024, -1, 1);
175
	  p = get_doc_string_buffer + in_buffer;
176
	  space_left = (get_doc_string_buffer_size - 1
177
			- (p - get_doc_string_buffer));
178 179
	}

180 181
      /* Read a disk block at a time.
         If we read the same block last time, maybe skip this?  */
182 183
      if (space_left > 1024 * 8)
	space_left = 1024 * 8;
184
      nread = emacs_read (fd, p, space_left);
185
      if (nread < 0)
186
	report_file_error ("Read error on documentation file", file);
187 188
      p[nread] = 0;
      if (!nread)
Jim Blandy's avatar
Jim Blandy committed
189
	break;
190
      if (p == get_doc_string_buffer)
191
	p1 = strchr (p + offset, '\037');
192
      else
193
	p1 = strchr (p, '\037');
Jim Blandy's avatar
Jim Blandy committed
194 195 196 197 198 199
      if (p1)
	{
	  *p1 = 0;
	  p = p1;
	  break;
	}
200
      p += nread;
Jim Blandy's avatar
Jim Blandy committed
201
    }
202 203
  unbind_to (count, Qnil);
  SAFE_FREE ();
204

205 206 207 208
  /* Sanity checking.  */
  if (CONSP (filepos))
    {
      int test = 1;
209 210 211 212 213 214 215 216 217 218 219 220 221 222
      /* A dynamic docstring should be either at the very beginning of a "#@
	 comment" or right after a dynamic docstring delimiter (in case we
	 pack several such docstrings within the same comment).  */
      if (get_doc_string_buffer[offset - test] != '\037')
	{
	  if (get_doc_string_buffer[offset - test++] != ' ')
	    return Qnil;
	  while (get_doc_string_buffer[offset - test] >= '0'
		 && get_doc_string_buffer[offset - test] <= '9')
	    test++;
	  if (get_doc_string_buffer[offset - test++] != '@'
	      || get_doc_string_buffer[offset - test] != '#')
	    return Qnil;
	}
223 224 225 226 227 228 229 230 231 232 233 234
    }
  else
    {
      int test = 1;
      if (get_doc_string_buffer[offset - test++] != '\n')
	return Qnil;
      while (get_doc_string_buffer[offset - test] > ' ')
	test++;
      if (get_doc_string_buffer[offset - test] != '\037')
	return Qnil;
    }

235 236
  /* Scan the text and perform quoting with ^A (char code 1).
     ^A^A becomes ^A, ^A0 becomes a null char, and ^A_ becomes a ^_.  */
237 238
  from = get_doc_string_buffer + offset;
  to = get_doc_string_buffer + offset;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  while (from != p)
    {
      if (*from == 1)
	{
	  int c;

	  from++;
	  c = *from++;
	  if (c == 1)
	    *to++ = c;
	  else if (c == '0')
	    *to++ = 0;
	  else if (c == '_')
	    *to++ = 037;
	  else
254 255 256
	    {
	      unsigned char uc = c;
	      error ("\
257
Invalid data in documentation file -- %c followed by code %03o",
258 259
		     1, uc);
	    }
260 261 262 263 264
	}
      else
	*to++ = *from++;
    }

265 266
  /* If DEFINITION, read from this buffer
     the same way we would read bytes from a file.  */
267 268
  if (definition)
    {
269
      read_bytecode_pointer = (unsigned char *) get_doc_string_buffer + offset;
270
      return Fread (Qlambda);
271 272
    }

273 274 275 276
  if (unibyte)
    return make_unibyte_string (get_doc_string_buffer + offset,
				to - (get_doc_string_buffer + offset));
  else
277
    {
278
      /* The data determines whether the string is multibyte.  */
279 280 281 282
      ptrdiff_t nchars
	= multibyte_chars_in_text (((unsigned char *) get_doc_string_buffer
				    + offset),
				   to - (get_doc_string_buffer + offset));
283 284 285 286
      return make_string_from_bytes (get_doc_string_buffer + offset,
				     nchars,
				     to - (get_doc_string_buffer + offset));
    }
287 288 289 290 291 292 293
}

/* Get a string from position FILEPOS and pass it through the Lisp reader.
   We use this for fetching the bytecode string and constants vector
   of a compiled function from the .elc file.  */

Lisp_Object
294
read_doc_string (Lisp_Object filepos)
295
{
296
  return get_doc_string (filepos, 0, 1);
Jim Blandy's avatar
Jim Blandy committed
297 298
}

299
static bool
300
reread_doc_file (Lisp_Object file)
301 302 303 304 305
{
  if (NILP (file))
    Fsnarf_documentation (Vdoc_file_name);
  else
    Fload (file, Qt, Qt, Qt, Qnil);
306 307

  return 1;
308 309
}

Roland McGrath's avatar
Roland McGrath committed
310
DEFUN ("documentation", Fdocumentation, Sdocumentation, 1, 2, 0,
311 312 313
       doc: /* Return the documentation string of FUNCTION.
Unless a non-nil second argument RAW is given, the
string is passed through `substitute-command-keys'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
314
  (Lisp_Object function, Lisp_Object raw)
Jim Blandy's avatar
Jim Blandy committed
315 316 317
{
  Lisp_Object fun;
  Lisp_Object funcar;
318
  Lisp_Object doc;
319
  bool try_reload = 1;
320 321

 documentation:
Jim Blandy's avatar
Jim Blandy committed
322

323
  doc = Qnil;
324

Juanma Barranquero's avatar
Juanma Barranquero committed
325 326 327 328 329 330 331 332
  if (SYMBOLP (function))
    {
      Lisp_Object tem = Fget (function, Qfunction_documentation);
      if (!NILP (tem))
	return Fdocumentation_property (function, Qfunction_documentation,
					raw);
    }

333
  fun = Findirect_function (function, Qnil);
334 335
  if (CONSP (fun) && EQ (XCAR (fun), Qmacro))
    fun = XCDR (fun);
336
  if (SUBRP (fun))
Jim Blandy's avatar
Jim Blandy committed
337
    {
338 339
      if (XSUBR (fun)->doc == 0)
	return Qnil;
Paul Eggert's avatar
Paul Eggert committed
340 341
      /* FIXME: This is not portable, as it assumes that string
	 pointers have the top bit clear.  */
342
      else if ((intptr_t) XSUBR (fun)->doc >= 0)
Roland McGrath's avatar
Roland McGrath committed
343
	doc = build_string (XSUBR (fun)->doc);
Jim Blandy's avatar
Jim Blandy committed
344
      else
345
	doc = make_number ((intptr_t) XSUBR (fun)->doc);
346 347 348
    }
  else if (COMPILEDP (fun))
    {
349
      if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) <= COMPILED_DOC_STRING)
Jim Blandy's avatar
Jim Blandy committed
350
	return Qnil;
Roland McGrath's avatar
Roland McGrath committed
351
      else
352 353 354 355 356 357 358 359 360
	{
	  Lisp_Object tem = AREF (fun, COMPILED_DOC_STRING);
	  if (STRINGP (tem))
	    doc = tem;
	  else if (NATNUMP (tem) || CONSP (tem))
	    doc = tem;
	  else
	    return Qnil;
	}
361 362 363
    }
  else if (STRINGP (fun) || VECTORP (fun))
    {
Jim Blandy's avatar
Jim Blandy committed
364
      return build_string ("Keyboard macro.");
365 366 367
    }
  else if (CONSP (fun))
    {
368
      funcar = XCAR (fun);
369
      if (!SYMBOLP (funcar))
Kim F. Storm's avatar
Kim F. Storm committed
370
	xsignal1 (Qinvalid_function, fun);
Jim Blandy's avatar
Jim Blandy committed
371
      else if (EQ (funcar, Qkeymap))
372
	return build_string ("Prefix command (definition is a keymap associating keystrokes with commands).");
Jim Blandy's avatar
Jim Blandy committed
373
      else if (EQ (funcar, Qlambda)
374
	       || (EQ (funcar, Qclosure) && (fun = XCDR (fun), 1))
Jim Blandy's avatar
Jim Blandy committed
375
	       || EQ (funcar, Qautoload))
Jim Blandy's avatar
Jim Blandy committed
376
	{
377 378
	  Lisp_Object tem1 = Fcdr (Fcdr (fun));
	  Lisp_Object tem = Fcar (tem1);
379
	  if (STRINGP (tem))
Roland McGrath's avatar
Roland McGrath committed
380
	    doc = tem;
381 382
	  /* Handle a doc reference--but these never come last
	     in the function body, so reject them if they are last.  */
383 384 385
	  else if ((NATNUMP (tem) || (CONSP (tem) && INTEGERP (XCDR (tem))))
		   && !NILP (XCDR (tem1)))
	    doc = tem;
Roland McGrath's avatar
Roland McGrath committed
386 387
	  else
	    return Qnil;
Jim Blandy's avatar
Jim Blandy committed
388
	}
389 390 391 392 393 394
      else
	goto oops;
    }
  else
    {
    oops:
Kim F. Storm's avatar
Kim F. Storm committed
395
      xsignal1 (Qinvalid_function, fun);
Jim Blandy's avatar
Jim Blandy committed
396
    }
Roland McGrath's avatar
Roland McGrath committed
397

398 399
  /* If DOC is 0, it's typically because of a dumped file missing
     from the DOC file (bug in src/Makefile.in).  */
400 401
  if (EQ (doc, make_number (0)))
    doc = Qnil;
402
  if (INTEGERP (doc) || CONSP (doc))
403 404 405
    {
      Lisp_Object tem;
      tem = get_doc_string (doc, 0, 0);
406
      if (NILP (tem) && try_reload)
407 408 409 410
	{
	  /* The file is newer, we need to reset the pointers.  */
	  struct gcpro gcpro1, gcpro2;
	  GCPRO2 (function, raw);
411
	  try_reload = reread_doc_file (Fcar_safe (doc));
412
	  UNGCPRO;
413 414 415 416 417
	  if (try_reload)
	    {
	      try_reload = 0;
	      goto documentation;
	    }
418 419 420 421
	}
      else
	doc = tem;
    }
422

Jim Blandy's avatar
Jim Blandy committed
423
  if (NILP (raw))
424
    doc = Fsubstitute_command_keys (doc);
Roland McGrath's avatar
Roland McGrath committed
425
  return doc;
Jim Blandy's avatar
Jim Blandy committed
426 427
}

428 429
DEFUN ("documentation-property", Fdocumentation_property,
       Sdocumentation_property, 2, 3, 0,
430 431 432 433 434 435 436
       doc: /* Return the documentation string that is SYMBOL's PROP property.
Third argument RAW omitted or nil means pass the result through
`substitute-command-keys' if it is a string.

This differs from `get' in that it can refer to strings stored in the
`etc/DOC' file; and that it evaluates documentation properties that
aren't strings.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
437
  (Lisp_Object symbol, Lisp_Object prop, Lisp_Object raw)
Jim Blandy's avatar
Jim Blandy committed
438
{
439
  bool try_reload = 1;
440
  Lisp_Object tem;
Jim Blandy's avatar
Jim Blandy committed
441

442
 documentation_property:
443

444
  tem = Fget (symbol, prop);
445
  if (EQ (tem, make_number (0)))
446
    tem = Qnil;
447
  if (INTEGERP (tem) || (CONSP (tem) && INTEGERP (XCDR (tem))))
448 449 450
    {
      Lisp_Object doc = tem;
      tem = get_doc_string (tem, 0, 0);
451
      if (NILP (tem) && try_reload)
452 453 454 455
	{
	  /* The file is newer, we need to reset the pointers.  */
	  struct gcpro gcpro1, gcpro2, gcpro3;
	  GCPRO3 (symbol, prop, raw);
456
	  try_reload = reread_doc_file (Fcar_safe (doc));
457
	  UNGCPRO;
458 459 460 461 462
	  if (try_reload)
	    {
	      try_reload = 0;
	      goto documentation_property;
	    }
463 464
	}
    }
465 466
  else if (!STRINGP (tem))
    /* Feval protects its argument.  */
467
    tem = Feval (tem, Qnil);
468

469
  if (NILP (raw) && STRINGP (tem))
470
    tem = Fsubstitute_command_keys (tem);
471
  return tem;
Jim Blandy's avatar
Jim Blandy committed
472 473
}

474 475 476
/* Scanning the DOC files and placing docstring offsets into functions.  */

static void
Paul Eggert's avatar
Paul Eggert committed
477
store_function_docstring (Lisp_Object obj, ptrdiff_t offset)
478
{
479 480
  /* Don't use indirect_function here, or defaliases will apply their
     docstrings to the base functions (Bug#2603).  */
481
  Lisp_Object fun = SYMBOLP (obj) ? XSYMBOL (obj)->function : obj;
482 483 484 485

  /* The type determines where the docstring is stored.  */

  /* Lisp_Subrs have a slot for it.  */
486
  if (SUBRP (fun))
487 488 489 490
    {
      intptr_t negative_offset = - offset;
      XSUBR (fun)->doc = (char *) negative_offset;
    }
491 492 493 494 495 496

  /* If it's a lisp form, stick it in the form.  */
  else if (CONSP (fun))
    {
      Lisp_Object tem;

497
      tem = XCAR (fun);
498 499
      if (EQ (tem, Qlambda) || EQ (tem, Qautoload)
	  || (EQ (tem, Qclosure) && (fun = XCDR (fun), 1)))
500 501
	{
	  tem = Fcdr (Fcdr (fun));
502
	  if (CONSP (tem) && INTEGERP (XCAR (tem)))
503 504
	    /* FIXME: This modifies typically pure hash-cons'd data, so its
	       correctness is quite delicate.  */
505
	    XSETCAR (tem, make_number (offset));
506 507
	}
      else if (EQ (tem, Qmacro))
508
	store_function_docstring (XCDR (fun), offset);
509 510 511
    }

  /* Bytecode objects sometimes have slots for it.  */
512
  else if (COMPILEDP (fun))
513 514 515
    {
      /* This bytecode object must have a slot for the
	 docstring, since we've found a docstring for it.  */
516
      if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_DOC_STRING)
517
	ASET (fun, COMPILED_DOC_STRING, make_number (offset));
518 519
      else
	message ("No docstring slot for %s",
520
		 SYMBOLP (obj) ? SSDATA (SYMBOL_NAME (obj)) : "<anonymous>");
521 522 523 524
    }
}


Jim Blandy's avatar
Jim Blandy committed
525
DEFUN ("Snarf-documentation", Fsnarf_documentation, Ssnarf_documentation,
526
       1, 1, 0,
527 528 529 530 531 532
       doc: /* Used during Emacs initialization to scan the `etc/DOC...' file.
This searches the `etc/DOC...' file for doc strings and
records them in function and variable definitions.
The function takes one argument, FILENAME, a string;
it specifies the file name (without a directory) of the DOC file.
That file is found in `../etc' now; later, when the dumped Emacs is run,
533
the same file name is found in the `doc-directory'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
534
  (Lisp_Object filename)
Jim Blandy's avatar
Jim Blandy committed
535 536 537
{
  int fd;
  char buf[1024 + 1];
538 539
  int filled;
  EMACS_INT pos;
540
  Lisp_Object sym;
541 542
  char *p, *name;
  bool skip_file = 0;
543
  ptrdiff_t count;
544 545
  char const *dirname;
  ptrdiff_t dirlen;
546 547 548 549 550 551
  /* Preloaded defcustoms using custom-initialize-delay are added to
     this list, but kept unbound.  See http://debbugs.gnu.org/11565  */
  Lisp_Object delayed_init =
    find_symbol_value (intern ("custom-delayed-init-variables"));

  if (EQ (delayed_init, Qunbound)) delayed_init = Qnil;
Jim Blandy's avatar
Jim Blandy committed
552

553
  CHECK_STRING (filename);
Jim Blandy's avatar
Jim Blandy committed
554

555
  if
Jim Blandy's avatar
Jim Blandy committed
556
#ifndef CANNOT_DUMP
557
    (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
558
#else /* CANNOT_DUMP */
559
      (0)
Jim Blandy's avatar
Jim Blandy committed
560
#endif /* CANNOT_DUMP */
561
    {
562 563
      dirname = sibling_etc;
      dirlen = sizeof sibling_etc - 1;
564 565 566 567
    }
  else
    {
      CHECK_STRING (Vdoc_directory);
568 569
      dirname = SSDATA (Vdoc_directory);
      dirlen = SBYTES (Vdoc_directory);
570
    }
571 572 573 574

  count = SPECPDL_INDEX ();
  USE_SAFE_ALLOCA;
  name = SAFE_ALLOCA (dirlen + SBYTES (filename) + 1);
Paul Eggert's avatar
Paul Eggert committed
575
  lispstpcpy (stpcpy (name, dirname), filename); 	/*** Add this line ***/
Jim Blandy's avatar
Jim Blandy committed
576

577 578
  /* Vbuild_files is nil when temacs is run, and non-nil after that.  */
  if (NILP (Vbuild_files))
Paul Eggert's avatar
Paul Eggert committed
579 580 581 582 583
    {
      static char const *const buildobj[] =
	{
	  #include "buildobj.h"
	};
584
      int i = ARRAYELTS (buildobj);
Paul Eggert's avatar
Paul Eggert committed
585 586 587 588
      while (0 <= --i)
	Vbuild_files = Fcons (build_string (buildobj[i]), Vbuild_files);
      Vbuild_files = Fpurecopy (Vbuild_files);
    }
589

590
  fd = emacs_open (name, O_RDONLY, 0);
Jim Blandy's avatar
Jim Blandy committed
591
  if (fd < 0)
592 593 594 595 596
    {
      int open_errno = errno;
      report_file_errno ("Opening doc string file", build_string (name),
			 open_errno);
    }
597
  record_unwind_protect_int (close_file_unwind, fd);
Jim Blandy's avatar
Jim Blandy committed
598 599 600 601 602
  Vdoc_file_name = filename;
  filled = 0;
  pos = 0;
  while (1)
    {
603
      register char *end;
Jim Blandy's avatar
Jim Blandy committed
604
      if (filled < 512)
605
	filled += emacs_read (fd, &buf[filled], sizeof buf - 1 - filled);
Jim Blandy's avatar
Jim Blandy committed
606 607 608 609 610
      if (!filled)
	break;

      buf[filled] = 0;
      end = buf + (filled < 512 ? filled : filled - 128);
611
      p = memchr (buf, '\037', end - buf);
612
      /* p points to ^_Ffunctionname\n or ^_Vvarname\n or ^_Sfilename\n.  */
613
      if (p)
Jim Blandy's avatar
Jim Blandy committed
614
	{
615
	  end = strchr (p, '\n');
616 617

          /* See if this is a file name, and if it is a file in build-files.  */
618
          if (p[1] == 'S')
619
            {
620 621 622 623
              skip_file = 0;
              if (end - p > 4 && end[-2] == '.'
                  && (end[-1] == 'o' || end[-1] == 'c'))
                {
624
                  ptrdiff_t len = end - p - 2;
625
                  char *fromfile = SAFE_ALLOCA (len + 1);
626
                  memcpy (fromfile, &p[2], len);
627 628 629 630 631 632 633
                  fromfile[len] = 0;
                  if (fromfile[len-1] == 'c')
                    fromfile[len-1] = 'o';

                  skip_file = NILP (Fmember (build_string (fromfile),
                                             Vbuild_files));
                }
634 635
            }

Richard M. Stallman's avatar
Richard M. Stallman committed
636
	  sym = oblookup (Vobarray, p + 2,
637 638
			  multibyte_chars_in_text ((unsigned char *) p + 2,
						   end - p - 2),
Richard M. Stallman's avatar
Richard M. Stallman committed
639
			  end - p - 2);
640 641 642 643
	  /* Check skip_file so that when a function is defined several
	     times in different files (typically, once in xterm, once in
	     w32term, ...), we only pay attention to the one that
	     matters.  */
644
	  if (! skip_file && SYMBOLP (sym))
Jim Blandy's avatar
Jim Blandy committed
645 646 647 648 649 650 651
	    {
	      /* Attach a docstring to a variable?  */
	      if (p[1] == 'V')
		{
		  /* Install file-position as variable-documentation property
		     and make it negative for a user-variable
		     (doc starts with a `*').  */
652 653
                  if (!NILP (Fboundp (sym))
                      || !NILP (Fmemq (sym, delayed_init)))
Glenn Morris's avatar
Glenn Morris committed
654 655 656
                    Fput (sym, Qvariable_documentation,
                          make_number ((pos + end + 1 - buf)
                                       * (end[1] == '*' ? -1 : 1)));
Jim Blandy's avatar
Jim Blandy committed
657 658
		}

659
	      /* Attach a docstring to a function?  */
Jim Blandy's avatar
Jim Blandy committed
660
	      else if (p[1] == 'F')
Glenn Morris's avatar
Glenn Morris committed
661 662 663 664
                {
                  if (!NILP (Ffboundp (sym)))
                    store_function_docstring (sym, pos + end + 1 - buf);
                }
Kenichi Handa's avatar
Kenichi Handa committed
665 666 667
	      else if (p[1] == 'S')
		; /* Just a source file name boundary marker.  Ignore it.  */

668
	      else
669
		error ("DOC file invalid at position %"pI"d", pos);
Jim Blandy's avatar
Jim Blandy committed
670 671 672 673
	    }
	}
      pos += end - buf;
      filled -= end - buf;
674
      memmove (buf, end, filled);
Jim Blandy's avatar
Jim Blandy committed
675
    }
676 677

  SAFE_FREE ();
678
  return unbind_to (count, Qnil);
Jim Blandy's avatar
Jim Blandy committed
679 680
}

Paul Eggert's avatar
Paul Eggert committed
681
DEFUN ("substitute-command-keys", Fsubstitute_command_keys,
682 683
       Ssubstitute_command_keys, 1, 1, 0,
       doc: /* Substitute key descriptions for command names in STRING.
684 685 686 687 688 689 690 691 692 693 694
Each substring of the form \\=\\[COMMAND] is replaced by either a
keystroke sequence that invokes COMMAND, or "M-x COMMAND" if COMMAND
is not on any keys.

Each substring of the form \\=\\{MAPVAR} is replaced by a summary of
the value of MAPVAR as a keymap.  This summary is similar to the one
produced by `describe-bindings'.  The summary ends in two newlines
\(used by the helper function `help-make-xrefs' to find the end of the
summary).

Each substring of the form \\=\\<MAPVAR> specifies the use of MAPVAR
695 696
as the keymap for future \\=\\[COMMAND] substrings.
\\=\\= quotes the following character and is discarded;
697 698
thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.

699 700 701 702
Replacement characters that should not be further transformed have the
text property `escaped'.  For example, when \\=\\=\\=` is replaced by \\=` in
the output, the replacement character has the text property `escaped'.

703
Return the original STRING if no substitutions are made.
704
Otherwise, return a new string.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
705
  (Lisp_Object string)
Jim Blandy's avatar
Jim Blandy committed
706
{
707
  if (NILP (string))
Jim Blandy's avatar
Jim Blandy committed
708
    return Qnil;
709
  CHECK_STRING (string);
Jim Blandy's avatar
Jim Blandy committed
710

Paul Eggert's avatar
Paul Eggert committed
711 712 713
  bool changed = false;
  Lisp_Object tem = Qnil;
  Lisp_Object name = Qnil;
Richard M. Stallman's avatar
Richard M. Stallman committed
714

715 716 717
  /* KEYMAP is either nil (which means search all the active keymaps)
     or a specified local map (which means search just that and the
     global map).  If non-nil, it might come from Voverriding_local_map,
718
     or from a \\<mapname> construct in STRING itself..  */
Paul Eggert's avatar
Paul Eggert committed
719
  Lisp_Object keymap = Voverriding_local_map;
Jim Blandy's avatar
Jim Blandy committed
720

Paul Eggert's avatar
Paul Eggert committed
721 722 723 724 725
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  GCPRO4 (string, tem, keymap, name);

  bool multibyte = STRING_MULTIBYTE (string);
  ptrdiff_t nchars = 0;
Jim Blandy's avatar
Jim Blandy committed
726

727 728 729 730 731 732 733 734 735
  ptrdiff_t count = SPECPDL_INDEX ();

  /* Switch to a temp buffer.  */
  struct buffer *oldbuf = current_buffer;
  set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
  /* This is for an unusual case where some after-change
     function uses 'format' or 'prin1' or something else that
     will thrash Vprin1_to_string_buffer we are using.  */
  specbind (Qinhibit_modification_hooks, Qt);
Paul Eggert's avatar
Paul Eggert committed
736 737

  unsigned char *strp = SDATA (string);
738
  while (strp < SDATA (string) + SBYTES (string))
Jim Blandy's avatar
Jim Blandy committed
739
    {
740
      ptrdiff_t opoint = PT;
Paul Eggert's avatar
Paul Eggert committed
741

Jim Blandy's avatar
Jim Blandy committed
742 743 744 745
      if (strp[0] == '\\' && strp[1] == '=')
	{
	  /* \= quotes the next character;
	     thus, to put in \[ without its special meaning, use \=\[.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
746
	  strp += 2;
747 748 749 750 751 752 753 754
	  int len = 1;
	  if (multibyte)
	    STRING_CHAR_AND_LENGTH (strp, len);
	  insert_1_both ((char *) strp, 1, len, false, false, false);
	  strp += len;
	  nchars++;
	  Fput_text_property (make_number (opoint), make_number (opoint + 1),
			      Qescaped, Qt, Qnil);
Paul Eggert's avatar
Paul Eggert committed
755
	  changed = true;
Jim Blandy's avatar
Jim Blandy committed
756 757 758 759
	}
      else if (strp[0] == '\\' && strp[1] == '[')
	{
	  strp += 2;		/* skip \[ */
760
	  unsigned char *start = strp;
Paul Eggert's avatar
Paul Eggert committed
761
	  ptrdiff_t start_idx = start - SDATA (string);
Jim Blandy's avatar
Jim Blandy committed
762

763
	  while ((strp - SDATA (string)
764
		  < SBYTES (string))
Jim Blandy's avatar
Jim Blandy committed
765 766
		 && *strp != ']')
	    strp++;
767
	  ptrdiff_t length_byte = strp - start;
Richard M. Stallman's avatar
Richard M. Stallman committed
768

Jim Blandy's avatar
Jim Blandy committed
769 770 771
	  strp++;		/* skip ] */

	  /* Save STRP in IDX.  */
772
	  ptrdiff_t idx = strp - SDATA (string);
773
	  name = Fintern (make_string ((char *) start, length_byte), Qnil);
774

775
	  tem = Fwhere_is_internal (name, keymap, Qt, Qnil, Qnil);
776
	  if (VECTORP (tem) && ASIZE (tem) > 1
Paul Eggert's avatar
Paul Eggert committed
777
	      && EQ (AREF (tem, 0), Qremap) && SYMBOLP (AREF (tem, 1)))
778 779
	    {
	      name = AREF (tem, 1);
Paul Eggert's avatar
Paul Eggert committed
780
	      tem = Fwhere_is_internal (name, keymap, Qt, Qnil, Qnil);
781 782
	      /* Fwhere_is_internal can GC, so we have to take
		 relocation of string contents into account.  */
783 784
	    }

785
	  ptrdiff_t length;
Jim Blandy's avatar
Jim Blandy committed
786
	  if (NILP (tem))	/* but not on any keys */
Jim Blandy's avatar
Jim Blandy committed
787
	    {
788 789
	      insert_1_both ("M-x ", 4, 4, false, false, false);
	      strp = SDATA (string) + start_idx;
Richard M. Stallman's avatar
Richard M. Stallman committed
790
	      if (multibyte)
791
		length = multibyte_chars_in_text (strp, length_byte);
Richard M. Stallman's avatar
Richard M. Stallman committed
792 793
	      else
		length = length_byte;
794 795 796 797
	      insert_1_both ((char *) strp, length, length_byte,
			     false, false, false);
	      Fput_text_property (make_number (opoint), make_number (PT),
				  Qescaped, Qt, Qnil);
Jim Blandy's avatar
Jim Blandy committed
798 799
	    }
	  else
800 801 802
	    {
	      /* FIXME: Fkey_description doesn't properly mark quote
		 characters that should be escaped.  */
803
	      tem = Fkey_description (tem, Qnil);
804
	      insert_from_string (tem, 0, 0, SCHARS (tem), SBYTES (tem), true);
Jim Blandy's avatar
Jim Blandy committed
805
	    }
806 807 808

	  strp = SDATA (string) + idx;
	  changed = true;
Jim Blandy's avatar
Jim Blandy committed
809 810 811 812 813
	}
      /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
	 \<foo> just sets the keymap used for \[cmd].  */
      else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
	{
814
	  /* This is for computing the SHADOWS arg for describe_map_tree.  */
815
	  Lisp_Object active_maps = Fcurrent_active_maps (Qnil, Qnil);
Jim Blandy's avatar
Jim Blandy committed
816 817

	  strp += 2;		/* skip \{ or \< */
818
	  unsigned char *start = strp;
Paul Eggert's avatar
Paul Eggert committed
819
	  ptrdiff_t start_idx = start - SDATA (string);
Jim Blandy's avatar
Jim Blandy committed
820

Kenichi Handa's avatar
Kenichi Handa committed
821
	  while ((strp - SDATA (string) < SBYTES (string))
Jim Blandy's avatar
Jim Blandy committed
822 823
		 && *strp != '}' && *strp != '>')
	    strp++;
Richard M. Stallman's avatar
Richard M. Stallman committed
824

825
	  ptrdiff_t length_byte = strp - start;
Jim Blandy's avatar
Jim Blandy committed
826 827 828
	  strp++;			/* skip } or > */

	  /* Save STRP in IDX.  */
829
	  ptrdiff_t idx = strp - SDATA (string);
Jim Blandy's avatar
Jim Blandy committed
830 831 832 833

	  /* Get the value of the keymap in TEM, or nil if undefined.
	     Do this while still in the user's current buffer
	     in case it is a local variable.  */
834
	  name = Fintern (make_string ((char *) start, length_byte), Qnil);
Jim Blandy's avatar
Jim Blandy committed
835
	  tem = Fboundp (name);
Jim Blandy's avatar
Jim Blandy committed
836
	  if (! NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
837 838
	    {
	      tem = Fsymbol_value (name);
Jim Blandy's avatar
Jim Blandy committed
839
	      if (! NILP (tem))
840
		{
841 842
		  tem = get_keymap (tem, 0, 1);
		  /* Note that get_keymap can GC.  */
843 844
		  strp = SDATA (string) + idx;
		  start = SDATA (string) + start_idx;
845
		}
Jim Blandy's avatar
Jim Blandy committed
846 847
	    }

Jim Blandy's avatar
Jim Blandy committed
848
	  if (NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
849 850
	    {
	      name = Fsymbol_name (name);
851
	      insert_string ("\nUses keymap `");
852
	      ptrdiff_t opoint = PT;
Richard M. Stallman's avatar
Richard M. Stallman committed
853
	      insert_from_string (name, 0, 0,
854 855
				  SCHARS (name),
				  SBYTES (name), 1);
856 857
	      Fput_text_property (make_number (opoint), make_number (PT),
				  Qescaped, Qt, Qnil);
858
	      insert_string ("', which is not currently defined.\n");
Jim Blandy's avatar
Jim Blandy committed
859 860 861 862 863
	      if (start[-1] == '<') keymap = Qnil;
	    }
	  else if (start[-1] == '<')
	    keymap = tem;
	  else
864 865 866
	    {
	      /* Get the list of active keymaps that precede this one.
		 If this one's not active, get nil.  */
Paul Eggert's avatar
Paul Eggert committed
867 868
	      Lisp_Object earlier_maps = Fcdr (Fmemq (tem,
						      Freverse (active_maps)));
869
	      describe_map_tree (tem, 1, Fnreverse (earlier_maps),
Paul Eggert's avatar
Paul Eggert committed
870
				 Qnil, 0, 1, 0, 0, 1);
871
	    }