buffer.c 210 KB
Newer Older
Roland McGrath's avatar
Roland McGrath committed
1
/* Buffer manipulation primitives for GNU Emacs.
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1985-1989, 1993-1995, 1997-2020 Free Software Foundation,
Paul Eggert's avatar
Paul Eggert committed
4
Inc.
Roland McGrath's avatar
Roland McGrath committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Roland McGrath's avatar
Roland McGrath 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.
Roland McGrath's avatar
Roland McGrath 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 <https://www.gnu.org/licenses/>.  */
Roland McGrath's avatar
Roland McGrath committed
20

21
#include <config.h>
Roland McGrath's avatar
Roland McGrath committed
22

23 24
#include <sys/types.h>
#include <sys/stat.h>
Roland McGrath's avatar
Roland McGrath committed
25
#include <sys/param.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
26
#include <errno.h>
Dave Love's avatar
Dave Love committed
27
#include <stdio.h>
Paul Eggert's avatar
Paul Eggert committed
28
#include <stdlib.h>
Andreas Schwab's avatar
Andreas Schwab committed
29
#include <unistd.h>
30

Paul Eggert's avatar
Paul Eggert committed
31 32
#include <verify.h>

Roland McGrath's avatar
Roland McGrath committed
33
#include "lisp.h"
34
#include "intervals.h"
35
#include "process.h"
36
#include "systime.h"
Roland McGrath's avatar
Roland McGrath committed
37 38
#include "window.h"
#include "commands.h"
39
#include "character.h"
40
#include "coding.h"
41
#include "buffer.h"
42
#include "region-cache.h"
Roland McGrath's avatar
Roland McGrath committed
43
#include "indent.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
44
#include "blockinput.h"
Stefan Monnier's avatar
Stefan Monnier committed
45
#include "keymap.h"
46
#include "frame.h"
Paul Eggert's avatar
Paul Eggert committed
47
#include "xwidget.h"
Daniel Colascione's avatar
Daniel Colascione committed
48
#include "pdumper.h"
Roland McGrath's avatar
Roland McGrath committed
49

50 51 52
#ifdef WINDOWSNT
#include "w32heap.h"		/* for mmap_* */
#endif
53

Roland McGrath's avatar
Roland McGrath committed
54 55 56 57 58 59 60
/* This structure holds the default values of the buffer-local variables
   defined with DEFVAR_PER_BUFFER, that have special slots in each buffer.
   The default value occupies the same slot in this structure
   as an individual buffer's value occupies in that buffer.
   Setting the default value also goes through the alist of buffers
   and stores into each buffer that does not say it has a local value.  */

61
struct buffer buffer_defaults;
Roland McGrath's avatar
Roland McGrath committed
62 63 64 65 66 67 68

/* This structure marks which slots in a buffer have corresponding
   default values in buffer_defaults.
   Each such slot has a nonzero value in this structure.
   The value has only one nonzero bit.

   When a buffer has its own local value for a slot,
69 70
   the entry for that slot (found in the same slot in this structure)
   is turned on in the buffer's local_flags array.
Roland McGrath's avatar
Roland McGrath committed
71 72 73 74 75 76

   If a slot in this structure is -1, then even though there may
   be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
   and the corresponding slot in buffer_defaults is not used.

   If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is
77
   zero, that is a bug.  */
Roland McGrath's avatar
Roland McGrath committed
78 79 80 81

struct buffer buffer_local_flags;

/* This structure holds the names of symbols whose values may be
82
   buffer-local.  It is indexed and accessed in the same way as the above.  */
Roland McGrath's avatar
Roland McGrath committed
83

84
struct buffer buffer_local_symbols;
Kenichi Handa's avatar
Kenichi Handa committed
85

86 87 88 89 90 91
/* Return the symbol of the per-buffer variable at offset OFFSET in
   the buffer structure.  */

#define PER_BUFFER_SYMBOL(OFFSET) \
      (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_symbols))

92 93 94
/* Maximum length of an overlay vector.  */
#define OVERLAY_COUNT_MAX						\
  ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM,				\
95
		    min (PTRDIFF_MAX, SIZE_MAX) / word_size))
96

97 98
/* Flags indicating which built-in buffer-local variables
   are permanent locals.  */
99
static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
100 101 102

/* Number of per-buffer variables used.  */

Paul Eggert's avatar
Paul Eggert committed
103
static int last_per_buffer_idx;
104

105
static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
106
                                    bool after, Lisp_Object arg1,
107
                                    Lisp_Object arg2, Lisp_Object arg3);
108
static void reset_buffer_local_variables (struct buffer *, bool);
Roland McGrath's avatar
Roland McGrath committed
109

110 111 112
/* Alist of all buffer names vs the buffers.  This used to be
   a Lisp-visible variable, but is no longer, to prevent lossage
   due to user rplac'ing this alist or its elements.  */
Roland McGrath's avatar
Roland McGrath committed
113 114
Lisp_Object Vbuffer_alist;

115
static Lisp_Object QSFundamental;	/* A string "Fundamental".  */
Roland McGrath's avatar
Roland McGrath committed
116

117
static void alloc_buffer_text (struct buffer *, ptrdiff_t);
118 119
static void free_buffer_text (struct buffer *b);
static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
120
static void modify_overlay (struct buffer *, ptrdiff_t, ptrdiff_t);
121
static Lisp_Object buffer_lisp_local_variables (struct buffer *, bool);
122
static Lisp_Object buffer_local_variables_1 (struct buffer *buf, int offset, Lisp_Object sym);
123

124 125 126 127 128 129
static void
CHECK_OVERLAY (Lisp_Object x)
{
  CHECK_TYPE (OVERLAYP (x), Qoverlayp, x);
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* Convert the position POS to an EMACS_INT that fits in a fixnum.
   Yield POS's value if POS is already a fixnum, POS's marker position
   if POS is a marker, and MOST_NEGATIVE_FIXNUM or
   MOST_POSITIVE_FIXNUM if POS is a negative or positive bignum.
   Signal an error if POS is not of the proper form.  */

EMACS_INT
fix_position (Lisp_Object pos)
{
  if (FIXNUMP (pos))
    return XFIXNUM (pos);
  if (MARKERP (pos))
    return marker_position (pos);
  CHECK_TYPE (BIGNUMP (pos), Qinteger_or_marker_p, pos);
  return !NILP (Fnatnump (pos)) ? MOST_POSITIVE_FIXNUM : MOST_NEGATIVE_FIXNUM;
}

147 148
/* These setters are used only in this file, so they can be private.
   The public setters are inline functions defined in buffer.h.  */
149
static void
Paul Eggert's avatar
Paul Eggert committed
150 151
bset_abbrev_mode (struct buffer *b, Lisp_Object val)
{
152
  b->abbrev_mode_ = val;
Paul Eggert's avatar
Paul Eggert committed
153
}
154
static void
Paul Eggert's avatar
Paul Eggert committed
155 156
bset_abbrev_table (struct buffer *b, Lisp_Object val)
{
157
  b->abbrev_table_ = val;
Paul Eggert's avatar
Paul Eggert committed
158
}
159
static void
Paul Eggert's avatar
Paul Eggert committed
160 161
bset_auto_fill_function (struct buffer *b, Lisp_Object val)
{
162
  b->auto_fill_function_ = val;
Paul Eggert's avatar
Paul Eggert committed
163
}
164
static void
Paul Eggert's avatar
Paul Eggert committed
165 166
bset_auto_save_file_format (struct buffer *b, Lisp_Object val)
{
167
  b->auto_save_file_format_ = val;
Paul Eggert's avatar
Paul Eggert committed
168
}
169
static void
Paul Eggert's avatar
Paul Eggert committed
170 171
bset_auto_save_file_name (struct buffer *b, Lisp_Object val)
{
172
  b->auto_save_file_name_ = val;
Paul Eggert's avatar
Paul Eggert committed
173
}
174
static void
Paul Eggert's avatar
Paul Eggert committed
175 176
bset_backed_up (struct buffer *b, Lisp_Object val)
{
177
  b->backed_up_ = val;
Paul Eggert's avatar
Paul Eggert committed
178
}
179
static void
Paul Eggert's avatar
Paul Eggert committed
180 181
bset_begv_marker (struct buffer *b, Lisp_Object val)
{
182
  b->begv_marker_ = val;
Paul Eggert's avatar
Paul Eggert committed
183
}
184
static void
Paul Eggert's avatar
Paul Eggert committed
185 186
bset_bidi_display_reordering (struct buffer *b, Lisp_Object val)
{
187
  b->bidi_display_reordering_ = val;
Paul Eggert's avatar
Paul Eggert committed
188
}
189
static void
190 191 192 193 194 195 196 197 198 199
bset_bidi_paragraph_start_re (struct buffer *b, Lisp_Object val)
{
  b->bidi_paragraph_start_re_ = val;
}
static void
bset_bidi_paragraph_separate_re (struct buffer *b, Lisp_Object val)
{
  b->bidi_paragraph_separate_re_ = val;
}
static void
Paul Eggert's avatar
Paul Eggert committed
200 201
bset_buffer_file_coding_system (struct buffer *b, Lisp_Object val)
{
202
  b->buffer_file_coding_system_ = val;
Paul Eggert's avatar
Paul Eggert committed
203
}
204
static void
Paul Eggert's avatar
Paul Eggert committed
205 206
bset_case_fold_search (struct buffer *b, Lisp_Object val)
{
207
  b->case_fold_search_ = val;
Paul Eggert's avatar
Paul Eggert committed
208
}
209
static void
Paul Eggert's avatar
Paul Eggert committed
210 211
bset_ctl_arrow (struct buffer *b, Lisp_Object val)
{
212
  b->ctl_arrow_ = val;
Paul Eggert's avatar
Paul Eggert committed
213
}
214
static void
Paul Eggert's avatar
Paul Eggert committed
215 216
bset_cursor_in_non_selected_windows (struct buffer *b, Lisp_Object val)
{
217
  b->cursor_in_non_selected_windows_ = val;
Paul Eggert's avatar
Paul Eggert committed
218
}
219
static void
Paul Eggert's avatar
Paul Eggert committed
220 221
bset_cursor_type (struct buffer *b, Lisp_Object val)
{
222
  b->cursor_type_ = val;
Paul Eggert's avatar
Paul Eggert committed
223
}
224
static void
Paul Eggert's avatar
Paul Eggert committed
225 226
bset_display_table (struct buffer *b, Lisp_Object val)
{
227
  b->display_table_ = val;
Paul Eggert's avatar
Paul Eggert committed
228
}
229
static void
Paul Eggert's avatar
Paul Eggert committed
230 231
bset_extra_line_spacing (struct buffer *b, Lisp_Object val)
{
232
  b->extra_line_spacing_ = val;
Paul Eggert's avatar
Paul Eggert committed
233
}
234
static void
Paul Eggert's avatar
Paul Eggert committed
235 236
bset_file_format (struct buffer *b, Lisp_Object val)
{
237
  b->file_format_ = val;
Paul Eggert's avatar
Paul Eggert committed
238
}
239
static void
Paul Eggert's avatar
Paul Eggert committed
240 241
bset_file_truename (struct buffer *b, Lisp_Object val)
{
242
  b->file_truename_ = val;
Paul Eggert's avatar
Paul Eggert committed
243
}
244
static void
Paul Eggert's avatar
Paul Eggert committed
245 246
bset_fringe_cursor_alist (struct buffer *b, Lisp_Object val)
{
247
  b->fringe_cursor_alist_ = val;
Paul Eggert's avatar
Paul Eggert committed
248
}
249
static void
Paul Eggert's avatar
Paul Eggert committed
250 251
bset_fringe_indicator_alist (struct buffer *b, Lisp_Object val)
{
252
  b->fringe_indicator_alist_ = val;
Paul Eggert's avatar
Paul Eggert committed
253
}
254
static void
Paul Eggert's avatar
Paul Eggert committed
255 256
bset_fringes_outside_margins (struct buffer *b, Lisp_Object val)
{
257
  b->fringes_outside_margins_ = val;
Paul Eggert's avatar
Paul Eggert committed
258
}
259
static void
Paul Eggert's avatar
Paul Eggert committed
260 261
bset_header_line_format (struct buffer *b, Lisp_Object val)
{
262
  b->header_line_format_ = val;
Paul Eggert's avatar
Paul Eggert committed
263
}
264
static void
265 266 267 268 269
bset_tab_line_format (struct buffer *b, Lisp_Object val)
{
  b->tab_line_format_ = val;
}
static void
Paul Eggert's avatar
Paul Eggert committed
270 271
bset_indicate_buffer_boundaries (struct buffer *b, Lisp_Object val)
{
272
  b->indicate_buffer_boundaries_ = val;
Paul Eggert's avatar
Paul Eggert committed
273
}
274
static void
Paul Eggert's avatar
Paul Eggert committed
275 276
bset_indicate_empty_lines (struct buffer *b, Lisp_Object val)
{
277
  b->indicate_empty_lines_ = val;
Paul Eggert's avatar
Paul Eggert committed
278
}
279
static void
Paul Eggert's avatar
Paul Eggert committed
280 281
bset_invisibility_spec (struct buffer *b, Lisp_Object val)
{
282
  b->invisibility_spec_ = val;
Paul Eggert's avatar
Paul Eggert committed
283
}
284
static void
Paul Eggert's avatar
Paul Eggert committed
285 286
bset_left_fringe_width (struct buffer *b, Lisp_Object val)
{
287
  b->left_fringe_width_ = val;
Paul Eggert's avatar
Paul Eggert committed
288
}
289
static void
Paul Eggert's avatar
Paul Eggert committed
290 291
bset_major_mode (struct buffer *b, Lisp_Object val)
{
292
  b->major_mode_ = val;
Paul Eggert's avatar
Paul Eggert committed
293
}
294
static void
Paul Eggert's avatar
Paul Eggert committed
295 296
bset_mark (struct buffer *b, Lisp_Object val)
{
297
  b->mark_ = val;
Paul Eggert's avatar
Paul Eggert committed
298
}
299
static void
Paul Eggert's avatar
Paul Eggert committed
300 301
bset_minor_modes (struct buffer *b, Lisp_Object val)
{
302
  b->minor_modes_ = val;
Paul Eggert's avatar
Paul Eggert committed
303
}
304
static void
Paul Eggert's avatar
Paul Eggert committed
305 306
bset_mode_line_format (struct buffer *b, Lisp_Object val)
{
307
  b->mode_line_format_ = val;
Paul Eggert's avatar
Paul Eggert committed
308
}
309
static void
Paul Eggert's avatar
Paul Eggert committed
310 311
bset_mode_name (struct buffer *b, Lisp_Object val)
{
312
  b->mode_name_ = val;
Paul Eggert's avatar
Paul Eggert committed
313
}
314
static void
Paul Eggert's avatar
Paul Eggert committed
315 316
bset_name (struct buffer *b, Lisp_Object val)
{
317
  b->name_ = val;
Paul Eggert's avatar
Paul Eggert committed
318
}
319
static void
Paul Eggert's avatar
Paul Eggert committed
320 321
bset_overwrite_mode (struct buffer *b, Lisp_Object val)
{
322
  b->overwrite_mode_ = val;
Paul Eggert's avatar
Paul Eggert committed
323
}
324
static void
Paul Eggert's avatar
Paul Eggert committed
325 326
bset_pt_marker (struct buffer *b, Lisp_Object val)
{
327
  b->pt_marker_ = val;
Paul Eggert's avatar
Paul Eggert committed
328
}
329
static void
Paul Eggert's avatar
Paul Eggert committed
330 331
bset_right_fringe_width (struct buffer *b, Lisp_Object val)
{
332
  b->right_fringe_width_ = val;
Paul Eggert's avatar
Paul Eggert committed
333
}
334
static void
Paul Eggert's avatar
Paul Eggert committed
335 336
bset_save_length (struct buffer *b, Lisp_Object val)
{
337
  b->save_length_ = val;
Paul Eggert's avatar
Paul Eggert committed
338
}
339
static void
Paul Eggert's avatar
Paul Eggert committed
340 341
bset_scroll_bar_width (struct buffer *b, Lisp_Object val)
{
342
  b->scroll_bar_width_ = val;
Paul Eggert's avatar
Paul Eggert committed
343
}
344
static void
345 346
bset_scroll_bar_height (struct buffer *b, Lisp_Object val)
{
347
  b->scroll_bar_height_ = val;
348 349
}
static void
Paul Eggert's avatar
Paul Eggert committed
350 351
bset_scroll_down_aggressively (struct buffer *b, Lisp_Object val)
{
352
  b->scroll_down_aggressively_ = val;
Paul Eggert's avatar
Paul Eggert committed
353
}
354
static void
Paul Eggert's avatar
Paul Eggert committed
355 356
bset_scroll_up_aggressively (struct buffer *b, Lisp_Object val)
{
357
  b->scroll_up_aggressively_ = val;
Paul Eggert's avatar
Paul Eggert committed
358
}
359
static void
Paul Eggert's avatar
Paul Eggert committed
360 361
bset_selective_display (struct buffer *b, Lisp_Object val)
{
362
  b->selective_display_ = val;
Paul Eggert's avatar
Paul Eggert committed
363
}
364
static void
Paul Eggert's avatar
Paul Eggert committed
365 366
bset_selective_display_ellipses (struct buffer *b, Lisp_Object val)
{
367
  b->selective_display_ellipses_ = val;
Paul Eggert's avatar
Paul Eggert committed
368
}
369
static void
Paul Eggert's avatar
Paul Eggert committed
370 371
bset_vertical_scroll_bar_type (struct buffer *b, Lisp_Object val)
{
372
  b->vertical_scroll_bar_type_ = val;
Paul Eggert's avatar
Paul Eggert committed
373
}
374
static void
375 376
bset_horizontal_scroll_bar_type (struct buffer *b, Lisp_Object val)
{
377
  b->horizontal_scroll_bar_type_ = val;
378 379
}
static void
Paul Eggert's avatar
Paul Eggert committed
380 381
bset_word_wrap (struct buffer *b, Lisp_Object val)
{
382
  b->word_wrap_ = val;
Paul Eggert's avatar
Paul Eggert committed
383
}
384
static void
Paul Eggert's avatar
Paul Eggert committed
385 386
bset_zv_marker (struct buffer *b, Lisp_Object val)
{
387
  b->zv_marker_ = val;
Paul Eggert's avatar
Paul Eggert committed
388 389
}

390
void
391
nsberror (Lisp_Object spec)
Roland McGrath's avatar
Roland McGrath committed
392
{
393
  if (STRINGP (spec))
394
    error ("No buffer named %s", SDATA (spec));
Roland McGrath's avatar
Roland McGrath committed
395 396 397
  error ("Invalid buffer argument");
}

Paul Eggert's avatar
Paul Eggert committed
398
DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
399
       doc: /* Return t if OBJECT is a buffer which has not been killed.
400
Value is nil if OBJECT is not a buffer or if it has been killed.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
401
  (Lisp_Object object)
402
{
403
  return ((BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object)))
404 405 406
	  ? Qt : Qnil);
}

407
DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
408 409 410
       doc: /* Return a list of all live buffers.
If the optional arg FRAME is a frame, return the buffer list in the
proper order for that frame: the buffers shown in FRAME come first,
411
followed by the rest of the buffers.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
412
  (Lisp_Object frame)
Roland McGrath's avatar
Roland McGrath committed
413
{
414
  Lisp_Object general;
415 416 417 418
  general = Fmapcar (Qcdr, Vbuffer_alist);

  if (FRAMEP (frame))
    {
419
      Lisp_Object framelist, prevlist, tail;
420

421
      framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
422
      prevlist = Fnreverse (Fcopy_sequence
423
			    (XFRAME (frame)->buried_buffer_list));
424

425 426
      /* Remove from GENERAL any buffer that duplicates one in
         FRAMELIST or PREVLIST.  */
427
      tail = framelist;
428
      while (CONSP (tail))
429
	{
430 431
	  general = Fdelq (XCAR (tail), general);
	  tail = XCDR (tail);
432
	}
433 434 435 436 437 438 439
      tail = prevlist;
      while (CONSP (tail))
	{
	  general = Fdelq (XCAR (tail), general);
	  tail = XCDR (tail);
	}

440
      return CALLN (Fnconc, framelist, general, prevlist);
441
    }
442 443
  else
    return general;
Roland McGrath's avatar
Roland McGrath committed
444 445
}

446
/* Like Fassoc, but use Fstring_equal to compare
Paul Eggert's avatar
Paul Eggert committed
447
   (which ignores text properties), and don't ever quit.  */
448 449

static Lisp_Object
Paul Eggert's avatar
Paul Eggert committed
450
assoc_ignore_text_properties (Lisp_Object key, Lisp_Object list)
451
{
Paul Eggert's avatar
Paul Eggert committed
452
  Lisp_Object tail;
453
  for (tail = list; CONSP (tail); tail = XCDR (tail))
454
    {
Paul Eggert's avatar
Paul Eggert committed
455 456
      Lisp_Object elt = XCAR (tail);
      if (!NILP (Fstring_equal (Fcar (elt), key)))
457 458 459 460 461
	return elt;
    }
  return Qnil;
}

Paul Eggert's avatar
Paul Eggert committed
462
DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
463 464 465 466
       doc: /* Return the buffer named BUFFER-OR-NAME.
BUFFER-OR-NAME must be either a string or a buffer.  If BUFFER-OR-NAME
is a string and there is no buffer with that name, return nil.  If
BUFFER-OR-NAME is a buffer, return it as given.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
467
  (register Lisp_Object buffer_or_name)
Roland McGrath's avatar
Roland McGrath committed
468
{
469 470 471
  if (BUFFERP (buffer_or_name))
    return buffer_or_name;
  CHECK_STRING (buffer_or_name);
Roland McGrath's avatar
Roland McGrath committed
472

473
  return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
Roland McGrath's avatar
Roland McGrath committed
474 475 476
}

DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
477
       doc: /* Return the buffer visiting file FILENAME (a string).
Pavel Janík's avatar
Pavel Janík committed
478 479
The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
If there is no such live buffer, return nil.
480
See also `find-buffer-visiting'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
481
  (register Lisp_Object filename)
Roland McGrath's avatar
Roland McGrath committed
482
{
483
  register Lisp_Object tail, buf, handler;
Richard M. Stallman's avatar
Richard M. Stallman committed
484

485
  CHECK_STRING (filename);
Roland McGrath's avatar
Roland McGrath committed
486 487
  filename = Fexpand_file_name (filename, Qnil);

Richard M. Stallman's avatar
Richard M. Stallman committed
488
  /* If the file name has special constructs in it,
489
     call the corresponding file name handler.  */
490
  handler = Ffind_file_name_handler (filename, Qget_file_buffer);
Richard M. Stallman's avatar
Richard M. Stallman committed
491
  if (!NILP (handler))
492 493 494 495 496
    {
      Lisp_Object handled_buf = call2 (handler, Qget_file_buffer,
				       filename);
      return BUFFERP (handled_buf) ? handled_buf : Qnil;
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
497

498
  FOR_EACH_LIVE_BUFFER (tail, buf)
Roland McGrath's avatar
Roland McGrath committed
499
    {
Tom Tromey's avatar
Tom Tromey committed
500
      if (!STRINGP (BVAR (XBUFFER (buf), filename))) continue;
501
      if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), filename), filename)))
Roland McGrath's avatar
Roland McGrath committed
502 503 504 505 506
	return buf;
    }
  return Qnil;
}

507
Lisp_Object
508
get_truename_buffer (register Lisp_Object filename)
509
{
510
  register Lisp_Object tail, buf;
511

512
  FOR_EACH_LIVE_BUFFER (tail, buf)
513
    {
Tom Tromey's avatar
Tom Tromey committed
514
      if (!STRINGP (BVAR (XBUFFER (buf), file_truename))) continue;
515
      if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), file_truename), filename)))
516 517 518 519 520
	return buf;
    }
  return Qnil;
}

Paul Eggert's avatar
Paul Eggert committed
521
DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
522 523 524 525 526 527 528 529
       doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
If BUFFER-OR-NAME is a string and a live buffer with that name exists,
return that buffer.  If no such buffer exists, create a new buffer with
that name and return it.  If BUFFER-OR-NAME starts with a space, the new
buffer does not keep undo information.

If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
even if it is dead.  The return value is never nil.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
530
  (register Lisp_Object buffer_or_name)
Roland McGrath's avatar
Roland McGrath committed
531
{
532
  register Lisp_Object buffer, name;
Roland McGrath's avatar
Roland McGrath committed
533 534
  register struct buffer *b;

535 536 537
  buffer = Fget_buffer (buffer_or_name);
  if (!NILP (buffer))
    return buffer;
Roland McGrath's avatar
Roland McGrath committed
538

539
  if (SCHARS (buffer_or_name) == 0)
540 541
    error ("Empty string for buffer name is not allowed");

542
  b = allocate_buffer ();
Roland McGrath's avatar
Roland McGrath committed
543

544 545
  /* An ordinary buffer uses its own struct buffer_text.  */
  b->text = &b->own_text;
546 547 548
  b->base_buffer = NULL;
  /* No one shares the text with us now.  */
  b->indirections = 0;
Dmitry Antipov's avatar
Dmitry Antipov committed
549 550
  /* No one shows us now.  */
  b->window_count = 0;
551

Daniel Colascione's avatar
Daniel Colascione committed
552 553
  memset (&b->local_flags, 0, sizeof (b->local_flags));

Roland McGrath's avatar
Roland McGrath committed
554
  BUF_GAP_SIZE (b) = 20;
555
  block_input ();
Karl Heuer's avatar
Karl Heuer committed
556 557
  /* We allocate extra 1-byte at the tail and keep it always '\0' for
     anchoring a search.  */
558
  alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
559
  unblock_input ();
Roland McGrath's avatar
Roland McGrath committed
560
  if (! BUF_BEG_ADDR (b))
Paul Eggert's avatar
Paul Eggert committed
561
    buffer_memory_full (BUF_GAP_SIZE (b) + 1);
Roland McGrath's avatar
Roland McGrath committed
562

563 564 565 566 567 568 569
  b->pt = BEG;
  b->begv = BEG;
  b->zv = BEG;
  b->pt_byte = BEG_BYTE;
  b->begv_byte = BEG_BYTE;
  b->zv_byte = BEG_BYTE;

570 571
  BUF_GPT (b) = BEG;
  BUF_GPT_BYTE (b) = BEG_BYTE;
572 573

  BUF_Z (b) = BEG;
574
  BUF_Z_BYTE (b) = BEG_BYTE;
Roland McGrath's avatar
Roland McGrath committed
575
  BUF_MODIFF (b) = 1;
576
  BUF_CHARS_MODIFF (b) = 1;
577
  BUF_OVERLAY_MODIFF (b) = 1;
578
  BUF_SAVE_MODIFF (b) = 1;
579
  BUF_COMPACT (b) = 1;
580
  set_buffer_intervals (b, NULL);
581 582 583 584
  BUF_UNCHANGED_MODIFIED (b) = 1;
  BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
  BUF_END_UNCHANGED (b) = 0;
  BUF_BEG_UNCHANGED (b) = 0;
Karl Heuer's avatar
Karl Heuer committed
585
  *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
586
  b->text->inhibit_shrinking = false;
587
  b->text->redisplay = false;
Roland McGrath's avatar
Roland McGrath committed
588

589 590
  b->newline_cache = 0;
  b->width_run_cache = 0;
591
  b->bidi_paragraph_cache = 0;
Paul Eggert's avatar
Paul Eggert committed
592
  bset_width_table (b, Qnil);
593
  b->prevent_redisplay_optimizations_p = 1;
594

595 596
  /* An ordinary buffer normally doesn't need markers
     to handle BEGV and ZV.  */
Paul Eggert's avatar
Paul Eggert committed
597 598 599
  bset_pt_marker (b, Qnil);
  bset_begv_marker (b, Qnil);
  bset_zv_marker (b, Qnil);
600

601
  name = Fcopy_sequence (buffer_or_name);
602
  set_string_intervals (name, NULL);
Paul Eggert's avatar
Paul Eggert committed
603
  bset_name (b, name);
604

605 606 607 608
  b->inhibit_buffer_hooks
    = (STRINGP (Vcode_conversion_workbuf_name)
       && strncmp (SSDATA (name), SSDATA (Vcode_conversion_workbuf_name),
		   SBYTES (Vcode_conversion_workbuf_name)) == 0);
609

Paul Eggert's avatar
Paul Eggert committed
610
  bset_undo_list (b, SREF (name, 0) != ' ' ? Qnil : Qt);
Roland McGrath's avatar
Roland McGrath committed
611 612

  reset_buffer (b);
613
  reset_buffer_local_variables (b, 1);
Roland McGrath's avatar
Roland McGrath committed
614

Paul Eggert's avatar
Paul Eggert committed
615
  bset_mark (b, Fmake_marker ());
616
  BUF_MARKERS (b) = NULL;
617

Roland McGrath's avatar
Roland McGrath committed
618
  /* Put this in the alist of all live buffers.  */
619
  XSETBUFFER (buffer, b);
620
  Vbuffer_alist = nconc2 (Vbuffer_alist, list1 (Fcons (name, buffer)));
621
  /* And run buffer-list-update-hook.  */
622
  if (!NILP (Vrun_hooks) && !b->inhibit_buffer_hooks)
623
    call1 (Vrun_hooks, Qbuffer_list_update_hook);
Roland McGrath's avatar
Roland McGrath committed
624

625
  return buffer;
626 627
}

628

629 630 631
/* Return a list of overlays which is a copy of the overlay list
   LIST, but for buffer B.  */

632
static struct Lisp_Overlay *
633
copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
634
{
635
  struct Lisp_Overlay *result = NULL, *tail = NULL;
636

637
  for (; list; list = list->next)
638
    {
Dmitry Antipov's avatar
Dmitry Antipov committed
639 640
      Lisp_Object overlay, start, end;
      struct Lisp_Marker *m;
641

642 643
      eassert (MARKERP (list->start));
      m = XMARKER (list->start);
Dmitry Antipov's avatar
Dmitry Antipov committed
644 645
      start = build_marker (b, m->charpos, m->bytepos);
      XMARKER (start)->insertion_type = m->insertion_type;
646

647 648
      eassert (MARKERP (list->end));
      m = XMARKER (list->end);
Dmitry Antipov's avatar
Dmitry Antipov committed
649 650
      end = build_marker (b, m->charpos, m->bytepos);
      XMARKER (end)->insertion_type = m->insertion_type;
651

652
      overlay = build_overlay (start, end, Fcopy_sequence (list->plist));
653 654 655 656
      if (tail)
	tail = tail->next = XOVERLAY (overlay);
      else
	result = tail = XOVERLAY (overlay);
657 658
    }

659
  return result;
660
}
661

662 663
/* Set an appropriate overlay of B.  */

664
static void
665 666 667 668 669
set_buffer_overlays_before (struct buffer *b, struct Lisp_Overlay *o)
{
  b->overlays_before = o;
}

670
static void
671 672 673 674
set_buffer_overlays_after (struct buffer *b, struct Lisp_Overlay *o)
{
  b->overlays_after = o;
}
675

Paul Eggert's avatar
Paul Eggert committed
676 677 678 679 680 681
bool
valid_per_buffer_idx (int idx)
{
  return 0 <= idx && idx < last_per_buffer_idx;
}

682 683 684 685 686 687 688 689
/* Clone per-buffer values of buffer FROM.

   Buffer TO gets the same per-buffer values as FROM, with the
   following exceptions: (1) TO's name is left untouched, (2) markers
   are copied and made to refer to TO, and (3) overlay lists are
   copied.  */

static void
690
clone_per_buffer_values (struct buffer *from, struct buffer *to)
691 692 693
{
  int offset;

694
  FOR_EACH_PER_BUFFER_OBJECT_AT (offset)
695 696 697
    {
      Lisp_Object obj;

698 699 700 701
      /* Don't touch the `name' which should be unique for every buffer.  */
      if (offset == PER_BUFFER_VAR_OFFSET (name))
	continue;

702
      obj = per_buffer_value (from, offset);
703
      if (MARKERP (obj) && XMARKER (obj)->buffer == from)
704 705
	{
	  struct Lisp_Marker *m = XMARKER (obj);
706 707

	  obj = build_marker (to, m->charpos, m->bytepos);
708 709 710
	  XMARKER (obj)->insertion_type = m->insertion_type;
	}

711
      set_per_buffer_value (to, offset, obj);
712 713
    }

714
  memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
715

716 717
  set_buffer_overlays_before (to, copy_overlays (to, from->overlays_before));
  set_buffer_overlays_after (to, copy_overlays (to, from->overlays_after));
718

719 720
  /* Get (a copy of) the alist of Lisp-level local variables of FROM
     and install that in TO.  */
Paul Eggert's avatar
Paul Eggert committed
721
  bset_local_var_alist (to, buffer_lisp_local_variables (from, 1));
722 723
}

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

/* If buffer B has markers to record PT, BEGV and ZV when it is not
   current, update these markers.  */

static void
record_buffer_markers (struct buffer *b)
{
  if (! NILP (BVAR (b, pt_marker)))
    {
      Lisp_Object buffer;

      eassert (!NILP (BVAR (b, begv_marker)));
      eassert (!NILP (BVAR (b, zv_marker)));

      XSETBUFFER (buffer, b);
      set_marker_both (BVAR (b, pt_marker), buffer, b->pt, b->pt_byte);
      set_marker_both (BVAR (b, begv_marker), buffer, b->begv, b->begv_byte);
      set_marker_both (BVAR (b, zv_marker), buffer, b->zv, b->zv_byte);
    }
}


/* If buffer B has markers to record PT, BEGV and ZV when it is not
   current, fetch these values into B->begv etc.  */

static void
fetch_buffer_markers (struct buffer *b)
{
  if (! NILP (BVAR (b, pt_marker)))
    {
      Lisp_Object m;

      eassert (!NILP (BVAR (b, begv_marker)));
      eassert (!NILP (BVAR (b, zv_marker)));

      m = BVAR (b, pt_marker);
      SET_BUF_PT_BOTH (b, marker_position (m), marker_byte_position (m));

      m = BVAR (b, begv_marker);
      SET_BUF_BEGV_BOTH (b, marker_position (m), marker_byte_position (m));

      m = BVAR (b, zv_marker);
      SET_BUF_ZV_BOTH (b, marker_position (m), marker_byte_position (m));
    }
}


771 772
DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
       2, 3,
773
       "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
774
       doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
Kenichi Handa's avatar
Kenichi Handa committed
775
BASE-BUFFER should be a live buffer, or the name of an existing buffer.
Pavel Janík's avatar
Pavel Janík committed
776 777 778
NAME should be a string which is not the name of an existing buffer.
Optional argument CLONE non-nil means preserve BASE-BUFFER's state,
such as major and minor modes, in the indirect buffer.
779
CLONE nil means the indirect buffer's state is reset to default values.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
780
  (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone)
781
{
Kenichi Handa's avatar
Kenichi Handa committed
782
  Lisp_Object buf, tem;
783
  struct buffer *b;
784

Kenichi Handa's avatar
Kenichi Handa committed
785
  CHECK_STRING (name);
786 787
  buf = Fget_buffer (name);
  if (!NILP (buf))
788
    error ("Buffer name `%s' is in use", SDATA (name));
789

Kenichi Handa's avatar
Kenichi Handa committed
790
  tem = base_buffer;
791 792
  base_buffer = Fget_buffer (base_buffer);
  if (NILP (base_buffer))
Kenichi Handa's avatar
Kenichi Handa committed
793
    error ("No such buffer: `%s'", SDATA (tem));
794
  if (!BUFFER_LIVE_P (XBUFFER (base_buffer)))
Kenichi Handa's avatar
Kenichi Handa committed
795
    error ("Base buffer has been killed");
796

797
  if (SCHARS (name) == 0)
798 799
    error ("Empty string for buffer name is not allowed");

800
  b = allocate_buffer ();
801

802 803
  /* No double indirection - if base buffer is indirect,
     new buffer becomes an indirect to base's base.  */
804 805 806
  b->base_buffer = (XBUFFER (base_buffer)->base_buffer
		    ? XBUFFER (base_buffer)->base_buffer
		    : XBUFFER (base_buffer));
807 808 809

  /* Use the base buffer's text object.  */
  b->text = b->base_buffer->text;
810 811 812 813
  /* We have no own text.  */
  b->indirections = -1;
  /* Notify base buffer that we share the text now.  */
  b->base_buffer->indirections++;
Dmitry Antipov's avatar
Dmitry Antipov committed
814 815
  /* Always -1 for an indirect buffer.  */
  b->window_count = -1;
816

Daniel Colascione's avatar
Daniel Colascione committed
817 818
  memset (&b->local_flags, 0, sizeof (b->local_flags));

819 820 821 822 823 824
  b->pt = b->base_buffer->pt;
  b->begv = b->base_buffer->begv;
  b->zv = b->base_buffer->zv;
  b->pt_byte = b->base_buffer->pt_byte;
  b->begv_byte = b->base_buffer->begv_byte;
  b->zv_byte = b->base_buffer->zv_byte;
825 826 827

  b->newline_cache = 0;
  b->width_run_cache = 0;
828
  b->bidi_paragraph_cache = 0;
Paul Eggert's avatar
Paul Eggert committed
829
  bset_width_table (b, Qnil);
830 831

  name = Fcopy_sequence (name);
832
  set_string_intervals (name, NULL);
Paul Eggert's avatar
Paul Eggert committed
833
  bset_name (b, name);
834

835 836 837
  /* An indirect buffer shares undo list of its base (Bug#18180).  */
  bset_undo_list (b, BVAR (b->base_buffer, undo_list));

838
  reset_buffer (b);
839
  reset_buffer_local_variables (b, 1);
840 841 842

  /* Put this in the alist of all live buffers.  */
  XSETBUFFER (buf, b);
843
  Vbuffer_alist = nconc2 (Vbuffer_alist, list1 (Fcons (name, buf)));
844

Paul Eggert's avatar
Paul Eggert committed
845
  bset_mark (b, Fmake_marker ());
846

847
  /* The multibyte status belongs to the base buffer.  */
Paul Eggert's avatar
Paul Eggert committed
848 849
  bset_enable_multibyte_characters
    (b, BVAR (b->base_buffer, enable_multibyte_characters));
850

851
  /* Make sure the base buffer has markers for its narrowing.  */
Tom Tromey's avatar
Tom Tromey committed
852
  if (NILP (BVAR (b->base_buffer, pt_marker)))
853
    {
854 855
      eassert (NILP (BVAR (b->base_buffer, begv_marker)));
      eassert (NILP (BVAR (b->base_buffer, zv_marker)));
856

Paul Eggert's avatar
Paul Eggert committed
857 858 859
      bset_pt_marker (b->base_buffer,
		      build_marker (b->base_buffer, b->base_buffer->pt,
				    b->base_buffer->pt_byte));
860

Paul Eggert's avatar
Paul Eggert committed
861 862 863
      bset_begv_marker (b->base_buffer,
			build_marker (b->base_buffer, b->base_buffer->begv,
				      b->base_buffer->begv_byte));