buffer.c 205 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-2016 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 <http://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>
Andreas Schwab's avatar
Andreas Schwab committed
28
#include <unistd.h>
29

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

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

47 48 49
#ifdef WINDOWSNT
#include "w32heap.h"		/* for mmap_* */
#endif
50

51
struct buffer *current_buffer;		/* The current buffer.  */
Roland McGrath's avatar
Roland McGrath committed
52 53

/* First buffer in chain of all buffers (in reverse order of creation).
54
   Threaded through ->header.next.buffer.  */
Roland McGrath's avatar
Roland McGrath committed
55 56 57 58 59 60 61 62 63 64

struct buffer *all_buffers;

/* 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.  */

65
struct buffer alignas (GCALIGNMENT) buffer_defaults;
Roland McGrath's avatar
Roland McGrath committed
66 67 68 69 70 71 72

/* 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,
73 74
   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
75 76 77 78 79 80

   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
81
   zero, that is a bug.  */
Roland McGrath's avatar
Roland McGrath committed
82 83 84 85

struct buffer buffer_local_flags;

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

88
struct buffer alignas (GCALIGNMENT) buffer_local_symbols;
Kenichi Handa's avatar
Kenichi Handa committed
89

90 91 92 93 94 95
/* 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))

96 97 98
/* Maximum length of an overlay vector.  */
#define OVERLAY_COUNT_MAX						\
  ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM,				\
99
		    min (PTRDIFF_MAX, SIZE_MAX) / word_size))
100

101 102
/* Flags indicating which built-in buffer-local variables
   are permanent locals.  */
103
static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
104 105 106

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

107
int last_per_buffer_idx;
108

109
static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay,
110
                                    bool after, Lisp_Object arg1,
111 112
                                    Lisp_Object arg2, Lisp_Object arg3);
static void swap_out_buffer_local_variables (struct buffer *b);
113
static void reset_buffer_local_variables (struct buffer *, bool);
Roland McGrath's avatar
Roland McGrath committed
114

115 116 117
/* 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
118 119
Lisp_Object Vbuffer_alist;

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

122
static void alloc_buffer_text (struct buffer *, ptrdiff_t);
123 124
static void free_buffer_text (struct buffer *b);
static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
125
static void modify_overlay (struct buffer *, ptrdiff_t, ptrdiff_t);
126
static Lisp_Object buffer_lisp_local_variables (struct buffer *, bool);
127

128 129 130 131 132 133
static void
CHECK_OVERLAY (Lisp_Object x)
{
  CHECK_TYPE (OVERLAYP (x), Qoverlayp, x);
}

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

362
void
363
nsberror (Lisp_Object spec)
Roland McGrath's avatar
Roland McGrath committed
364
{
365
  if (STRINGP (spec))
366
    error ("No buffer named %s", SDATA (spec));
Roland McGrath's avatar
Roland McGrath committed
367 368 369
  error ("Invalid buffer argument");
}

Paul Eggert's avatar
Paul Eggert committed
370
DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
371 372
       doc: /* Return non-nil if OBJECT is a buffer which has not been killed.
Value is nil if OBJECT is not a buffer or if it has been killed.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
373
  (Lisp_Object object)
374
{
375
  return ((BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object)))
376 377 378
	  ? Qt : Qnil);
}

379
DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
380
       doc: /* Return a list of all existing live buffers.
381 382 383
If the optional arg FRAME is a frame, we return the buffer list in the
proper order for that frame: the buffers show in FRAME come first,
followed by the rest of the buffers.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
384
  (Lisp_Object frame)
Roland McGrath's avatar
Roland McGrath committed
385
{
386
  Lisp_Object general;
387 388 389 390
  general = Fmapcar (Qcdr, Vbuffer_alist);

  if (FRAMEP (frame))
    {
391
      Lisp_Object framelist, prevlist, tail;
392

393
      framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
394
      prevlist = Fnreverse (Fcopy_sequence
395
			    (XFRAME (frame)->buried_buffer_list));
396

397 398
      /* Remove from GENERAL any buffer that duplicates one in
         FRAMELIST or PREVLIST.  */
399
      tail = framelist;
400
      while (CONSP (tail))
401
	{
402 403
	  general = Fdelq (XCAR (tail), general);
	  tail = XCDR (tail);
404
	}
405 406 407 408 409 410 411
      tail = prevlist;
      while (CONSP (tail))
	{
	  general = Fdelq (XCAR (tail), general);
	  tail = XCDR (tail);
	}

412
      return CALLN (Fnconc, framelist, general, prevlist);
413
    }
414 415
  else
    return general;
Roland McGrath's avatar
Roland McGrath committed
416 417
}

418 419 420 421 422
/* Like Fassoc, but use Fstring_equal to compare
   (which ignores text properties),
   and don't ever QUIT.  */

static Lisp_Object
423
assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list)
424 425
{
  register Lisp_Object tail;
426
  for (tail = list; CONSP (tail); tail = XCDR (tail))
427 428
    {
      register Lisp_Object elt, tem;
429
      elt = XCAR (tail);
430 431 432 433 434 435 436
      tem = Fstring_equal (Fcar (elt), key);
      if (!NILP (tem))
	return elt;
    }
  return Qnil;
}

Paul Eggert's avatar
Paul Eggert committed
437
DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
438 439 440 441
       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
442
  (register Lisp_Object buffer_or_name)
Roland McGrath's avatar
Roland McGrath committed
443
{
444 445 446
  if (BUFFERP (buffer_or_name))
    return buffer_or_name;
  CHECK_STRING (buffer_or_name);
Roland McGrath's avatar
Roland McGrath committed
447

448
  return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
Roland McGrath's avatar
Roland McGrath committed
449 450 451
}

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

460
  CHECK_STRING (filename);
Roland McGrath's avatar
Roland McGrath committed
461 462
  filename = Fexpand_file_name (filename, Qnil);

Richard M. Stallman's avatar
Richard M. Stallman committed
463 464
  /* If the file name has special constructs in it,
     call the corresponding file handler.  */
465
  handler = Ffind_file_name_handler (filename, Qget_file_buffer);
Richard M. Stallman's avatar
Richard M. Stallman committed
466
  if (!NILP (handler))
467 468 469 470 471
    {
      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
472

473
  FOR_EACH_LIVE_BUFFER (tail, buf)
Roland McGrath's avatar
Roland McGrath committed
474
    {
Tom Tromey's avatar
Tom Tromey committed
475
      if (!STRINGP (BVAR (XBUFFER (buf), filename))) continue;
476
      if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), filename), filename)))
Roland McGrath's avatar
Roland McGrath committed
477 478 479 480 481
	return buf;
    }
  return Qnil;
}

482
Lisp_Object
483
get_truename_buffer (register Lisp_Object filename)
484
{
485
  register Lisp_Object tail, buf;
486

487
  FOR_EACH_LIVE_BUFFER (tail, buf)
488
    {
Tom Tromey's avatar
Tom Tromey committed
489
      if (!STRINGP (BVAR (XBUFFER (buf), file_truename))) continue;
490
      if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), file_truename), filename)))
491 492 493 494 495
	return buf;
    }
  return Qnil;
}

Paul Eggert's avatar
Paul Eggert committed
496
DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
497 498 499 500 501 502 503 504
       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
505
  (register Lisp_Object buffer_or_name)
Roland McGrath's avatar
Roland McGrath committed
506
{
507
  register Lisp_Object buffer, name;
Roland McGrath's avatar
Roland McGrath committed
508 509
  register struct buffer *b;

510 511 512
  buffer = Fget_buffer (buffer_or_name);
  if (!NILP (buffer))
    return buffer;
Roland McGrath's avatar
Roland McGrath committed
513

514
  if (SCHARS (buffer_or_name) == 0)
515 516
    error ("Empty string for buffer name is not allowed");

517
  b = allocate_buffer ();
Roland McGrath's avatar
Roland McGrath committed
518

519 520
  /* An ordinary buffer uses its own struct buffer_text.  */
  b->text = &b->own_text;
521 522 523
  b->base_buffer = NULL;
  /* No one shares the text with us now.  */
  b->indirections = 0;
Dmitry Antipov's avatar
Dmitry Antipov committed
524 525
  /* No one shows us now.  */
  b->window_count = 0;
526

Roland McGrath's avatar
Roland McGrath committed
527
  BUF_GAP_SIZE (b) = 20;
528
  block_input ();
Karl Heuer's avatar
Karl Heuer committed
529 530
  /* We allocate extra 1-byte at the tail and keep it always '\0' for
     anchoring a search.  */
531
  alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
532
  unblock_input ();
Roland McGrath's avatar
Roland McGrath committed
533
  if (! BUF_BEG_ADDR (b))
Paul Eggert's avatar
Paul Eggert committed
534
    buffer_memory_full (BUF_GAP_SIZE (b) + 1);
Roland McGrath's avatar
Roland McGrath committed
535

536 537 538 539 540 541 542
  b->pt = BEG;
  b->begv = BEG;
  b->zv = BEG;
  b->pt_byte = BEG_BYTE;
  b->begv_byte = BEG_BYTE;
  b->zv_byte = BEG_BYTE;

543 544
  BUF_GPT (b) = BEG;
  BUF_GPT_BYTE (b) = BEG_BYTE;
545 546

  BUF_Z (b) = BEG;
547
  BUF_Z_BYTE (b) = BEG_BYTE;
Roland McGrath's avatar
Roland McGrath committed
548
  BUF_MODIFF (b) = 1;
549
  BUF_CHARS_MODIFF (b) = 1;
550
  BUF_OVERLAY_MODIFF (b) = 1;
551
  BUF_SAVE_MODIFF (b) = 1;
552
  BUF_COMPACT (b) = 1;
553
  set_buffer_intervals (b, NULL);
554 555 556 557
  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
558
  *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
559
  b->text->inhibit_shrinking = false;
560
  b->text->redisplay = false;
Roland McGrath's avatar
Roland McGrath committed
561

562 563
  b->newline_cache = 0;
  b->width_run_cache = 0;
564
  b->bidi_paragraph_cache = 0;
Paul Eggert's avatar
Paul Eggert committed
565
  bset_width_table (b, Qnil);
566
  b->prevent_redisplay_optimizations_p = 1;
567

568 569
  /* An ordinary buffer normally doesn't need markers
     to handle BEGV and ZV.  */
Paul Eggert's avatar
Paul Eggert committed
570 571 572
  bset_pt_marker (b, Qnil);
  bset_begv_marker (b, Qnil);
  bset_zv_marker (b, Qnil);
573

574
  name = Fcopy_sequence (buffer_or_name);
575
  set_string_intervals (name, NULL);
Paul Eggert's avatar
Paul Eggert committed
576
  bset_name (b, name);
577

Paul Eggert's avatar
Paul Eggert committed
578
  bset_undo_list (b, SREF (name, 0) != ' ' ? Qnil : Qt);
Roland McGrath's avatar
Roland McGrath committed
579 580

  reset_buffer (b);
581
  reset_buffer_local_variables (b, 1);
Roland McGrath's avatar
Roland McGrath committed
582

Paul Eggert's avatar
Paul Eggert committed
583
  bset_mark (b, Fmake_marker ());
584
  BUF_MARKERS (b) = NULL;
585

Roland McGrath's avatar
Roland McGrath committed
586
  /* Put this in the alist of all live buffers.  */
587
  XSETBUFFER (buffer, b);
588
  Vbuffer_alist = nconc2 (Vbuffer_alist, list1 (Fcons (name, buffer)));
589 590 591
  /* And run buffer-list-update-hook.  */
  if (!NILP (Vrun_hooks))
    call1 (Vrun_hooks, Qbuffer_list_update_hook);
Roland McGrath's avatar
Roland McGrath committed
592

593
  return buffer;
594 595
}

596

597 598 599
/* Return a list of overlays which is a copy of the overlay list
   LIST, but for buffer B.  */

600
static struct Lisp_Overlay *
601
copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
602
{
603
  struct Lisp_Overlay *result = NULL, *tail = NULL;
604

605
  for (; list; list = list->next)
606
    {
Dmitry Antipov's avatar
Dmitry Antipov committed
607 608
      Lisp_Object overlay, start, end;
      struct Lisp_Marker *m;
609

610 611
      eassert (MARKERP (list->start));
      m = XMARKER (list->start);
Dmitry Antipov's avatar
Dmitry Antipov committed
612 613
      start = build_marker (b, m->charpos, m->bytepos);
      XMARKER (start)->insertion_type = m->insertion_type;
614

615 616
      eassert (MARKERP (list->end));
      m = XMARKER (list->end);
Dmitry Antipov's avatar
Dmitry Antipov committed
617 618
      end = build_marker (b, m->charpos, m->bytepos);
      XMARKER (end)->insertion_type = m->insertion_type;
619

620
      overlay = build_overlay (start, end, Fcopy_sequence (list->plist));
621 622 623 624
      if (tail)
	tail = tail->next = XOVERLAY (overlay);
      else
	result = tail = XOVERLAY (overlay);
625 626
    }

627
  return result;
628
}
629

630 631
/* Set an appropriate overlay of B.  */

632
static void
633 634 635 636 637
set_buffer_overlays_before (struct buffer *b, struct Lisp_Overlay *o)
{
  b->overlays_before = o;
}

638
static void
639 640 641 642
set_buffer_overlays_after (struct buffer *b, struct Lisp_Overlay *o)
{
  b->overlays_after = o;
}
643

644 645 646 647 648 649 650 651
/* 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
652
clone_per_buffer_values (struct buffer *from, struct buffer *to)
653 654 655
{
  int offset;

656
  FOR_EACH_PER_BUFFER_OBJECT_AT (offset)
657 658 659
    {
      Lisp_Object obj;

660 661 662 663
      /* Don't touch the `name' which should be unique for every buffer.  */
      if (offset == PER_BUFFER_VAR_OFFSET (name))
	continue;

664
      obj = per_buffer_value (from, offset);
665
      if (MARKERP (obj) && XMARKER (obj)->buffer == from)
666 667
	{
	  struct Lisp_Marker *m = XMARKER (obj);
668 669

	  obj = build_marker (to, m->charpos, m->bytepos);
670 671 672
	  XMARKER (obj)->insertion_type = m->insertion_type;
	}

673
      set_per_buffer_value (to, offset, obj);
674 675
    }

676
  memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
677

678 679
  set_buffer_overlays_before (to, copy_overlays (to, from->overlays_before));
  set_buffer_overlays_after (to, copy_overlays (to, from->overlays_after));
680

681 682
  /* 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
683
  bset_local_var_alist (to, buffer_lisp_local_variables (from, 1));
684 685
}

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

/* 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)))
    {