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

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

This file is part of GNU Emacs.

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

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
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Roland McGrath's avatar
Roland McGrath committed
19

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

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

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

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

44
struct buffer *current_buffer;		/* The current buffer.  */
Roland McGrath's avatar
Roland McGrath committed
45 46

/* First buffer in chain of all buffers (in reverse order of creation).
47
   Threaded through ->header.next.buffer.  */
Roland McGrath's avatar
Roland McGrath committed
48 49 50 51 52 53 54 55 56 57

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

58
struct buffer alignas (GCALIGNMENT) buffer_defaults;
Roland McGrath's avatar
Roland McGrath committed
59 60 61 62 63 64 65

/* 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,
66 67
   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
68 69 70 71 72 73

   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
74
   zero, that is a bug.  */
Roland McGrath's avatar
Roland McGrath committed
75 76 77 78

struct buffer buffer_local_flags;

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

81
struct buffer alignas (GCALIGNMENT) buffer_local_symbols;
Kenichi Handa's avatar
Kenichi Handa committed
82

83 84 85 86 87 88
/* 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))

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

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

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

100
int last_per_buffer_idx;
101

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

108 109 110
/* 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
111 112
Lisp_Object Vbuffer_alist;

113
static Lisp_Object Qkill_buffer_query_functions;
114

115
/* Hook run before changing a major mode.  */
116
static Lisp_Object Qchange_major_mode_hook;
117

Jim Blandy's avatar
Jim Blandy committed
118
Lisp_Object Qfirst_change_hook;
119 120
Lisp_Object Qbefore_change_functions;
Lisp_Object Qafter_change_functions;
Roland McGrath's avatar
Roland McGrath committed
121

122 123
static Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
static Lisp_Object Qpermanent_local_hook;
Roland McGrath's avatar
Roland McGrath committed
124

125
static Lisp_Object Qprotected_field;
Roland McGrath's avatar
Roland McGrath committed
126

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

129
static Lisp_Object Qkill_buffer_hook;
130
static Lisp_Object Qbuffer_list_update_hook;
Roland McGrath's avatar
Roland McGrath committed
131

132
static Lisp_Object Qget_file_buffer;
Richard M. Stallman's avatar
Richard M. Stallman committed
133

134
static Lisp_Object Qoverlayp;
Jim Blandy's avatar
Jim Blandy committed
135

136
Lisp_Object Qpriority, Qbefore_string, Qafter_string;
137

138
static Lisp_Object Qevaporate;
139

140 141 142 143
Lisp_Object Qmodification_hooks;
Lisp_Object Qinsert_in_front_hooks;
Lisp_Object Qinsert_behind_hooks;

144
static void alloc_buffer_text (struct buffer *, ptrdiff_t);
145 146
static void free_buffer_text (struct buffer *b);
static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
147
static void modify_overlay (struct buffer *, ptrdiff_t, ptrdiff_t);
148
static Lisp_Object buffer_lisp_local_variables (struct buffer *, bool);
149

150 151 152 153 154 155
static void
CHECK_OVERLAY (Lisp_Object x)
{
  CHECK_TYPE (OVERLAYP (x), Qoverlayp, x);
}

156 157
/* These setters are used only in this file, so they can be private.
   The public setters are inline functions defined in buffer.h.  */
158
static void
Paul Eggert's avatar
Paul Eggert committed
159 160 161 162
bset_abbrev_mode (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (abbrev_mode) = val;
}
163
static void
Paul Eggert's avatar
Paul Eggert committed
164 165 166 167
bset_abbrev_table (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (abbrev_table) = val;
}
168
static void
Paul Eggert's avatar
Paul Eggert committed
169 170 171 172
bset_auto_fill_function (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (auto_fill_function) = val;
}
173
static void
Paul Eggert's avatar
Paul Eggert committed
174 175 176 177
bset_auto_save_file_format (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (auto_save_file_format) = val;
}
178
static void
Paul Eggert's avatar
Paul Eggert committed
179 180 181 182
bset_auto_save_file_name (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (auto_save_file_name) = val;
}
183
static void
Paul Eggert's avatar
Paul Eggert committed
184 185 186 187
bset_backed_up (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (backed_up) = val;
}
188
static void
Paul Eggert's avatar
Paul Eggert committed
189 190 191 192
bset_begv_marker (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (begv_marker) = val;
}
193
static void
Paul Eggert's avatar
Paul Eggert committed
194 195 196 197
bset_bidi_display_reordering (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (bidi_display_reordering) = val;
}
198
static void
Paul Eggert's avatar
Paul Eggert committed
199 200 201 202
bset_buffer_file_coding_system (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (buffer_file_coding_system) = val;
}
203
static void
Paul Eggert's avatar
Paul Eggert committed
204 205 206 207
bset_case_fold_search (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (case_fold_search) = val;
}
208
static void
Paul Eggert's avatar
Paul Eggert committed
209 210 211 212
bset_ctl_arrow (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (ctl_arrow) = val;
}
213
static void
Paul Eggert's avatar
Paul Eggert committed
214 215 216 217
bset_cursor_in_non_selected_windows (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (cursor_in_non_selected_windows) = val;
}
218
static void
Paul Eggert's avatar
Paul Eggert committed
219 220 221 222
bset_cursor_type (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (cursor_type) = val;
}
223
static void
Paul Eggert's avatar
Paul Eggert committed
224 225 226 227
bset_display_table (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (display_table) = val;
}
228
static void
Paul Eggert's avatar
Paul Eggert committed
229 230 231 232
bset_extra_line_spacing (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (extra_line_spacing) = val;
}
233
static void
Paul Eggert's avatar
Paul Eggert committed
234 235 236 237
bset_file_format (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (file_format) = val;
}
238
static void
Paul Eggert's avatar
Paul Eggert committed
239 240 241 242
bset_file_truename (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (file_truename) = val;
}
243
static void
Paul Eggert's avatar
Paul Eggert committed
244 245 246 247
bset_fringe_cursor_alist (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (fringe_cursor_alist) = val;
}
248
static void
Paul Eggert's avatar
Paul Eggert committed
249 250 251 252
bset_fringe_indicator_alist (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (fringe_indicator_alist) = val;
}
253
static void
Paul Eggert's avatar
Paul Eggert committed
254 255 256 257
bset_fringes_outside_margins (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (fringes_outside_margins) = val;
}
258
static void
Paul Eggert's avatar
Paul Eggert committed
259 260 261 262
bset_header_line_format (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (header_line_format) = val;
}
263
static void
Paul Eggert's avatar
Paul Eggert committed
264 265 266 267
bset_indicate_buffer_boundaries (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (indicate_buffer_boundaries) = val;
}
268
static void
Paul Eggert's avatar
Paul Eggert committed
269 270 271 272
bset_indicate_empty_lines (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (indicate_empty_lines) = val;
}
273
static void
Paul Eggert's avatar
Paul Eggert committed
274 275 276 277
bset_invisibility_spec (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (invisibility_spec) = val;
}
278
static void
Paul Eggert's avatar
Paul Eggert committed
279 280 281 282
bset_left_fringe_width (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (left_fringe_width) = val;
}
283
static void
Paul Eggert's avatar
Paul Eggert committed
284 285 286 287
bset_major_mode (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (major_mode) = val;
}
288
static void
Paul Eggert's avatar
Paul Eggert committed
289 290 291 292
bset_mark (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (mark) = val;
}
293
static void
Paul Eggert's avatar
Paul Eggert committed
294 295 296 297
bset_minor_modes (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (minor_modes) = val;
}
298
static void
Paul Eggert's avatar
Paul Eggert committed
299 300 301 302
bset_mode_line_format (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (mode_line_format) = val;
}
303
static void
Paul Eggert's avatar
Paul Eggert committed
304 305 306 307
bset_mode_name (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (mode_name) = val;
}
308
static void
Paul Eggert's avatar
Paul Eggert committed
309 310 311 312
bset_name (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (name) = val;
}
313
static void
Paul Eggert's avatar
Paul Eggert committed
314 315 316 317
bset_overwrite_mode (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (overwrite_mode) = val;
}
318
static void
Paul Eggert's avatar
Paul Eggert committed
319 320 321 322
bset_pt_marker (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (pt_marker) = val;
}
323
static void
Paul Eggert's avatar
Paul Eggert committed
324 325 326 327
bset_right_fringe_width (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (right_fringe_width) = val;
}
328
static void
Paul Eggert's avatar
Paul Eggert committed
329 330 331 332
bset_save_length (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (save_length) = val;
}
333
static void
Paul Eggert's avatar
Paul Eggert committed
334 335 336 337
bset_scroll_bar_width (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (scroll_bar_width) = val;
}
338
static void
Paul Eggert's avatar
Paul Eggert committed
339 340 341 342
bset_scroll_down_aggressively (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (scroll_down_aggressively) = val;
}
343
static void
Paul Eggert's avatar
Paul Eggert committed
344 345 346 347
bset_scroll_up_aggressively (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (scroll_up_aggressively) = val;
}
348
static void
Paul Eggert's avatar
Paul Eggert committed
349 350 351 352
bset_selective_display (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (selective_display) = val;
}
353
static void
Paul Eggert's avatar
Paul Eggert committed
354 355 356 357
bset_selective_display_ellipses (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (selective_display_ellipses) = val;
}
358
static void
Paul Eggert's avatar
Paul Eggert committed
359 360 361 362
bset_vertical_scroll_bar_type (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (vertical_scroll_bar_type) = val;
}
363
static void
Paul Eggert's avatar
Paul Eggert committed
364 365 366 367
bset_word_wrap (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (word_wrap) = val;
}
368
static void
Paul Eggert's avatar
Paul Eggert committed
369 370 371 372 373
bset_zv_marker (struct buffer *b, Lisp_Object val)
{
  b->INTERNAL_FIELD (zv_marker) = val;
}

374
void
375
nsberror (Lisp_Object spec)
Roland McGrath's avatar
Roland McGrath committed
376
{
377
  if (STRINGP (spec))
378
    error ("No buffer named %s", SDATA (spec));
Roland McGrath's avatar
Roland McGrath committed
379 380 381
  error ("Invalid buffer argument");
}

Paul Eggert's avatar
Paul Eggert committed
382
DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0,
383 384
       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
385
  (Lisp_Object object)
386
{
387
  return ((BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object)))
388 389 390
	  ? Qt : Qnil);
}

391
DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
392
       doc: /* Return a list of all existing live buffers.
393 394 395
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
396
  (Lisp_Object frame)
Roland McGrath's avatar
Roland McGrath committed
397
{
398
  Lisp_Object general;
399 400 401 402
  general = Fmapcar (Qcdr, Vbuffer_alist);

  if (FRAMEP (frame))
    {
403 404
      Lisp_Object framelist, prevlist, tail;
      Lisp_Object args[3];
405

406
      framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
407
      prevlist = Fnreverse (Fcopy_sequence
408
			    (XFRAME (frame)->buried_buffer_list));
409

410 411
      /* Remove from GENERAL any buffer that duplicates one in
         FRAMELIST or PREVLIST.  */
412
      tail = framelist;
413
      while (CONSP (tail))
414
	{
415 416
	  general = Fdelq (XCAR (tail), general);
	  tail = XCDR (tail);
417
	}
418 419 420 421 422 423 424 425 426 427 428
      tail = prevlist;
      while (CONSP (tail))
	{
	  general = Fdelq (XCAR (tail), general);
	  tail = XCDR (tail);
	}

      args[0] = framelist;
      args[1] = general;
      args[2] = prevlist;
      return Fnconc (3, args);
429
    }
430 431
  else
    return general;
Roland McGrath's avatar
Roland McGrath committed
432 433
}

434 435 436 437 438
/* Like Fassoc, but use Fstring_equal to compare
   (which ignores text properties),
   and don't ever QUIT.  */

static Lisp_Object
439
assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list)
440 441
{
  register Lisp_Object tail;
442
  for (tail = list; CONSP (tail); tail = XCDR (tail))
443 444
    {
      register Lisp_Object elt, tem;
445
      elt = XCAR (tail);
446 447 448 449 450 451 452
      tem = Fstring_equal (Fcar (elt), key);
      if (!NILP (tem))
	return elt;
    }
  return Qnil;
}

Paul Eggert's avatar
Paul Eggert committed
453
DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
454 455 456 457
       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
458
  (register Lisp_Object buffer_or_name)
Roland McGrath's avatar
Roland McGrath committed
459
{
460 461 462
  if (BUFFERP (buffer_or_name))
    return buffer_or_name;
  CHECK_STRING (buffer_or_name);
Roland McGrath's avatar
Roland McGrath committed
463

464
  return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
Roland McGrath's avatar
Roland McGrath committed
465 466 467
}

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

476
  CHECK_STRING (filename);
Roland McGrath's avatar
Roland McGrath committed
477 478
  filename = Fexpand_file_name (filename, Qnil);

Richard M. Stallman's avatar
Richard M. Stallman committed
479 480
  /* If the file name has special constructs in it,
     call the corresponding file handler.  */
481
  handler = Ffind_file_name_handler (filename, Qget_file_buffer);
Richard M. Stallman's avatar
Richard M. Stallman committed
482
  if (!NILP (handler))
483 484 485 486 487
    {
      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
488

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

498
Lisp_Object
499
get_truename_buffer (register Lisp_Object filename)
500
{
501
  register Lisp_Object tail, buf;
502

503
  FOR_EACH_LIVE_BUFFER (tail, buf)
504
    {
Tom Tromey's avatar
Tom Tromey committed
505
      if (!STRINGP (BVAR (XBUFFER (buf), file_truename))) continue;
506
      if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), file_truename), filename)))
507 508 509 510 511
	return buf;
    }
  return Qnil;
}

Paul Eggert's avatar
Paul Eggert committed
512
DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
513 514 515 516 517 518 519 520
       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
521
  (register Lisp_Object buffer_or_name)
Roland McGrath's avatar
Roland McGrath committed
522
{
523
  register Lisp_Object buffer, name;
Roland McGrath's avatar
Roland McGrath committed
524 525
  register struct buffer *b;

526 527 528
  buffer = Fget_buffer (buffer_or_name);
  if (!NILP (buffer))
    return buffer;
Roland McGrath's avatar
Roland McGrath committed
529

530
  if (SCHARS (buffer_or_name) == 0)
531 532
    error ("Empty string for buffer name is not allowed");

533
  b = allocate_buffer ();
Roland McGrath's avatar
Roland McGrath committed
534

535 536
  /* An ordinary buffer uses its own struct buffer_text.  */
  b->text = &b->own_text;
537 538 539
  b->base_buffer = NULL;
  /* No one shares the text with us now.  */
  b->indirections = 0;
Dmitry Antipov's avatar
Dmitry Antipov committed
540 541
  /* No one shows us now.  */
  b->window_count = 0;
542

Roland McGrath's avatar
Roland McGrath committed
543
  BUF_GAP_SIZE (b) = 20;
544
  block_input ();
Karl Heuer's avatar
Karl Heuer committed
545 546
  /* We allocate extra 1-byte at the tail and keep it always '\0' for
     anchoring a search.  */
547
  alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1);
548
  unblock_input ();
Roland McGrath's avatar
Roland McGrath committed
549
  if (! BUF_BEG_ADDR (b))
Paul Eggert's avatar
Paul Eggert committed
550
    buffer_memory_full (BUF_GAP_SIZE (b) + 1);
Roland McGrath's avatar
Roland McGrath committed
551

552 553 554 555 556 557 558
  b->pt = BEG;
  b->begv = BEG;
  b->zv = BEG;
  b->pt_byte = BEG_BYTE;
  b->begv_byte = BEG_BYTE;
  b->zv_byte = BEG_BYTE;

559 560
  BUF_GPT (b) = BEG;
  BUF_GPT_BYTE (b) = BEG_BYTE;
561 562

  BUF_Z (b) = BEG;
563
  BUF_Z_BYTE (b) = BEG_BYTE;
Roland McGrath's avatar
Roland McGrath committed
564
  BUF_MODIFF (b) = 1;
565
  BUF_CHARS_MODIFF (b) = 1;
566
  BUF_OVERLAY_MODIFF (b) = 1;
567
  BUF_SAVE_MODIFF (b) = 1;
568
  BUF_COMPACT (b) = 1;
569
  set_buffer_intervals (b, NULL);
570 571 572 573
  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
574
  *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
575
  b->text->inhibit_shrinking = false;
576
  b->text->redisplay = false;
Roland McGrath's avatar
Roland McGrath committed
577

578 579
  b->newline_cache = 0;
  b->width_run_cache = 0;
580
  b->bidi_paragraph_cache = 0;
Paul Eggert's avatar
Paul Eggert committed
581
  bset_width_table (b, Qnil);
582
  b->prevent_redisplay_optimizations_p = 1;
583

584 585
  /* An ordinary buffer normally doesn't need markers
     to handle BEGV and ZV.  */
Paul Eggert's avatar
Paul Eggert committed
586 587 588
  bset_pt_marker (b, Qnil);
  bset_begv_marker (b, Qnil);
  bset_zv_marker (b, Qnil);
589

590
  name = Fcopy_sequence (buffer_or_name);
591
  set_string_intervals (name, NULL);
Paul Eggert's avatar
Paul Eggert committed
592
  bset_name (b, name);
593

Paul Eggert's avatar
Paul Eggert committed
594
  bset_undo_list (b, SREF (name, 0) != ' ' ? Qnil : Qt);
Roland McGrath's avatar
Roland McGrath committed
595 596

  reset_buffer (b);
597
  reset_buffer_local_variables (b, 1);
Roland McGrath's avatar
Roland McGrath committed
598

Paul Eggert's avatar
Paul Eggert committed
599
  bset_mark (b, Fmake_marker ());
600
  BUF_MARKERS (b) = NULL;
601

Roland McGrath's avatar
Roland McGrath committed
602
  /* Put this in the alist of all live buffers.  */
603
  XSETBUFFER (buffer, b);
604
  Vbuffer_alist = nconc2 (Vbuffer_alist, list1 (Fcons (name, buffer)));
605 606 607
  /* And run buffer-list-update-hook.  */
  if (!NILP (Vrun_hooks))
    call1 (Vrun_hooks, Qbuffer_list_update_hook);
Roland McGrath's avatar
Roland McGrath committed
608

609
  return buffer;
610 611
}

612

613 614 615
/* Return a list of overlays which is a copy of the overlay list
   LIST, but for buffer B.  */

616
static struct Lisp_Overlay *
617
copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
618
{
619
  struct Lisp_Overlay *result = NULL, *tail = NULL;
620

621
  for (; list; list = list->next)
622
    {
Dmitry Antipov's avatar
Dmitry Antipov committed
623 624
      Lisp_Object overlay, start, end;
      struct Lisp_Marker *m;
625

626 627
      eassert (MARKERP (list->start));
      m = XMARKER (list->start);
Dmitry Antipov's avatar
Dmitry Antipov committed
628 629
      start = build_marker (b, m->charpos, m->bytepos);
      XMARKER (start)->insertion_type = m->insertion_type;
630

631 632
      eassert (MARKERP (list->end));
      m = XMARKER (list->end);
Dmitry Antipov's avatar
Dmitry Antipov committed
633 634
      end = build_marker (b, m->charpos, m->bytepos);
      XMARKER (end)->insertion_type = m->insertion_type;
635

636
      overlay = build_overlay (start, end, Fcopy_sequence (list->plist));
637 638 639 640
      if (tail)
	tail = tail->next = XOVERLAY (overlay);
      else
	result = tail = XOVERLAY (overlay);
641 642
    }

643
  return result;
644
}
645

646 647
/* Set an appropriate overlay of B.  */

648
static void
649 650 651 652 653
set_buffer_overlays_before (struct buffer *b, struct Lisp_Overlay *o)
{
  b->overlays_before = o;
}

654
static void
655 656 657 658
set_buffer_overlays_after (struct buffer *b, struct Lisp_Overlay *o)
{
  b->overlays_after = o;
}
659

660 661 662 663 664 665 666 667
/* 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
668
clone_per_buffer_values (struct buffer *from, struct buffer *to)
669 670 671
{
  int offset;

672
  FOR_EACH_PER_BUFFER_OBJECT_AT (offset)
673 674 675
    {
      Lisp_Object obj;

676 677 678 679
      /* Don't touch the `name' which should be unique for every buffer.  */
      if (offset == PER_BUFFER_VAR_OFFSET (name))
	continue;

680
      obj = per_buffer_value (from, offset);
681
      if (MARKERP (obj) && XMARKER (obj)->buffer == from)
682 683
	{
	  struct Lisp_Marker *m = XMARKER (obj);
684 685

	  obj = build_marker (to, m->charpos, m->bytepos);
686 687 688
	  XMARKER (obj)->insertion_type = m->insertion_type;
	}

689
      set_per_buffer_value (to, offset, obj);
690 691
    }

692
  memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
693

694 695
  set_buffer_overlays_before (to, copy_overlays (to, from->overlays_before));
  set_buffer_overlays_after (to, copy_overlays (to, from->overlays_after));
696

697 698
  /* 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
699
  bset_local_var_alist (to, buffer_lisp_local_variables (from, 1));
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 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

/* 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));
    }
}


749 750
DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
       2, 3,
751
       "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
752
       doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.
Kenichi Handa's avatar
Kenichi Handa committed
753
BASE-BUFFER should be a live buffer, or the name of an existing buffer.
Pavel Janík's avatar
Pavel Janík committed
754 755 756
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.
757
CLONE nil means the indirect buffer's state is reset to default values.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
758
  (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone)
759
{
Kenichi Handa's avatar
Kenichi Handa committed
760
  Lisp_Object buf, tem;
761
  struct buffer *b;
762

Kenichi Handa's avatar
Kenichi Handa committed
763
  CHECK_STRING (name);
764 765
  buf = Fget_buffer (name);
  if (!NILP (buf))
766
    error ("Buffer name `%s' is in use", SDATA (name));
767

Kenichi Handa's avatar
Kenichi Handa committed
768
  tem = base_buffer;
769 770
  base_buffer = Fget_buffer (base_buffer);
  if (NILP (base_buffer))
Kenichi Handa's avatar
Kenichi Handa committed
771
    error ("No such buffer: `%s'", SDATA (tem));
772
  if (!BUFFER_LIVE_P (XBUFFER (base_buffer)))
Kenichi Handa's avatar
Kenichi Handa committed
773
    error ("Base buffer has been killed");
774

775
  if (SCHARS (name) == 0)
776 777
    error ("Empty string for buffer name is not allowed");

778
  b = allocate_buffer ();
779

780 781
  /* No double indirection - if base buffer is indirect,
     new buffer becomes an indirect to base's base.  */
782 783 784
  b->base_buffer = (XBUFFER (base_buffer)->base_buffer
		    ? XBUFFER (base_buffer)->base_buffer
		    : XBUFFER (base_buffer));
785 786 787

  /* Use the base buffer's text object.  */
  b->text = b->base_buffer->text;
788 789 790 791
  /* 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
792 793
  /* Always -1 for an indirect buffer.  */
  b->window_count = -1;
794

795 796 797 798 799 800
  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;
801 802 803

  b->newline_cache = 0;
  b->width_run_cache = 0;
804
  b->bidi_paragraph_cache = 0;
Paul Eggert's avatar
Paul Eggert committed
805
  bset_width_table (b, Qnil);
806 807

  name = Fcopy_sequence (name);
808
  set_string_intervals (name, NULL);
Paul Eggert's avatar
Paul Eggert committed
809
  bset_name (b, name);
810 811

  reset_buffer (b);
812
  reset_buffer_local_variables (b, 1);
813 814 815

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