buffer.c 204 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-2017 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 "buffer.h"
41
#include "region-cache.h"
Roland McGrath's avatar
Roland McGrath committed
42
#include "indent.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
43
#include "blockinput.h"
Stefan Monnier's avatar
Stefan Monnier committed
44
#include "keymap.h"
45
#include "frame.h"
Paul Eggert's avatar
Paul Eggert committed
46
#include "xwidget.h"
Roland McGrath's avatar
Roland McGrath committed
47

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

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

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

64
struct buffer buffer_defaults;
Roland McGrath's avatar
Roland McGrath committed
65 66 67 68 69 70 71

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

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

struct buffer buffer_local_flags;

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

87
struct buffer buffer_local_symbols;
Kenichi Handa's avatar
Kenichi Handa committed
88

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

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

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

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

106
int last_per_buffer_idx;
107

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

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

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

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

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

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

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

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

  if (FRAMEP (frame))
    {
400
      Lisp_Object framelist, prevlist, tail;
401

402
      framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
403
      prevlist = Fnreverse (Fcopy_sequence
404
			    (XFRAME (frame)->buried_buffer_list));
405

406 407
      /* Remove from GENERAL any buffer that duplicates one in
         FRAMELIST or PREVLIST.  */
408
      tail = framelist;
409
      while (CONSP (tail))
410
	{
411 412
	  general = Fdelq (XCAR (tail), general);
	  tail = XCDR (tail);
413
	}
414 415 416 417 418 419 420
      tail = prevlist;
      while (CONSP (tail))
	{
	  general = Fdelq (XCAR (tail), general);
	  tail = XCDR (tail);
	}

421
      return CALLN (Fnconc, framelist, general, prevlist);
422
    }
423 424
  else
    return general;
Roland McGrath's avatar
Roland McGrath committed
425 426
}

427
/* Like Fassoc, but use Fstring_equal to compare
Paul Eggert's avatar
Paul Eggert committed
428
   (which ignores text properties), and don't ever quit.  */
429 430

static Lisp_Object
Paul Eggert's avatar
Paul Eggert committed
431
assoc_ignore_text_properties (Lisp_Object key, Lisp_Object list)
432
{
Paul Eggert's avatar
Paul Eggert committed
433
  Lisp_Object tail;
434
  for (tail = list; CONSP (tail); tail = XCDR (tail))
435
    {
Paul Eggert's avatar
Paul Eggert committed
436 437
      Lisp_Object elt = XCAR (tail);
      if (!NILP (Fstring_equal (Fcar (elt), key)))
438 439 440 441 442
	return elt;
    }
  return Qnil;
}

Paul Eggert's avatar
Paul Eggert committed
443
DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
444 445 446 447
       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
448
  (register Lisp_Object buffer_or_name)
Roland McGrath's avatar
Roland McGrath committed
449
{
450 451 452
  if (BUFFERP (buffer_or_name))
    return buffer_or_name;
  CHECK_STRING (buffer_or_name);
Roland McGrath's avatar
Roland McGrath committed
453

454
  return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
Roland McGrath's avatar
Roland McGrath committed
455 456 457
}

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

466
  CHECK_STRING (filename);
Roland McGrath's avatar
Roland McGrath committed
467 468
  filename = Fexpand_file_name (filename, Qnil);

Richard M. Stallman's avatar
Richard M. Stallman committed
469 470
  /* If the file name has special constructs in it,
     call the corresponding file handler.  */
471
  handler = Ffind_file_name_handler (filename, Qget_file_buffer);
Richard M. Stallman's avatar
Richard M. Stallman committed
472
  if (!NILP (handler))
473 474 475 476 477
    {
      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
478

479
  FOR_EACH_LIVE_BUFFER (tail, buf)
Roland McGrath's avatar
Roland McGrath committed
480
    {
Tom Tromey's avatar
Tom Tromey committed
481
      if (!STRINGP (BVAR (XBUFFER (buf), filename))) continue;
482
      if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), filename), filename)))
Roland McGrath's avatar
Roland McGrath committed
483 484 485 486 487
	return buf;
    }
  return Qnil;
}

488
Lisp_Object
489
get_truename_buffer (register Lisp_Object filename)
490
{
491
  register Lisp_Object tail, buf;
492

493
  FOR_EACH_LIVE_BUFFER (tail, buf)
494
    {
Tom Tromey's avatar
Tom Tromey committed
495
      if (!STRINGP (BVAR (XBUFFER (buf), file_truename))) continue;
496
      if (!NILP (Fstring_equal (BVAR (XBUFFER (buf), file_truename), filename)))
497 498 499 500 501
	return buf;
    }
  return Qnil;
}

Paul Eggert's avatar
Paul Eggert committed
502
DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
503 504 505 506 507 508 509 510
       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
511
  (register Lisp_Object buffer_or_name)
Roland McGrath's avatar
Roland McGrath committed
512
{
513
  register Lisp_Object buffer, name;
Roland McGrath's avatar
Roland McGrath committed
514 515
  register struct buffer *b;

516 517 518
  buffer = Fget_buffer (buffer_or_name);
  if (!NILP (buffer))
    return buffer;
Roland McGrath's avatar
Roland McGrath committed
519

520
  if (SCHARS (buffer_or_name) == 0)
521 522
    error ("Empty string for buffer name is not allowed");

523
  b = allocate_buffer ();
Roland McGrath's avatar
Roland McGrath committed
524

525 526
  /* An ordinary buffer uses its own struct buffer_text.  */
  b->text = &b->own_text;
527 528 529
  b->base_buffer = NULL;
  /* No one shares the text with us now.  */
  b->indirections = 0;
Dmitry Antipov's avatar
Dmitry Antipov committed
530 531
  /* No one shows us now.  */
  b->window_count = 0;
532

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

542 543 544 545 546 547 548
  b->pt = BEG;
  b->begv = BEG;
  b->zv = BEG;
  b->pt_byte = BEG_BYTE;
  b->begv_byte = BEG_BYTE;
  b->zv_byte = BEG_BYTE;

549 550
  BUF_GPT (b) = BEG;
  BUF_GPT_BYTE (b) = BEG_BYTE;
551 552

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

568 569
  b->newline_cache = 0;
  b->width_run_cache = 0;
570
  b->bidi_paragraph_cache = 0;
Paul Eggert's avatar
Paul Eggert committed
571
  bset_width_table (b, Qnil);
572
  b->prevent_redisplay_optimizations_p = 1;
573

574 575
  /* An ordinary buffer normally doesn't need markers
     to handle BEGV and ZV.  */
Paul Eggert's avatar
Paul Eggert committed
576 577 578
  bset_pt_marker (b, Qnil);
  bset_begv_marker (b, Qnil);
  bset_zv_marker (b, Qnil);
579

580
  name = Fcopy_sequence (buffer_or_name);
581
  set_string_intervals (name, NULL);
Paul Eggert's avatar
Paul Eggert committed
582
  bset_name (b, name);
583

Paul Eggert's avatar
Paul Eggert committed
584
  bset_undo_list (b, SREF (name, 0) != ' ' ? Qnil : Qt);
Roland McGrath's avatar
Roland McGrath committed
585 586

  reset_buffer (b);
587
  reset_buffer_local_variables (b, 1);
Roland McGrath's avatar
Roland McGrath committed
588

Paul Eggert's avatar
Paul Eggert committed
589
  bset_mark (b, Fmake_marker ());
590
  BUF_MARKERS (b) = NULL;
591

Roland McGrath's avatar
Roland McGrath committed
592
  /* Put this in the alist of all live buffers.  */
593
  XSETBUFFER (buffer, b);
594
  Vbuffer_alist = nconc2 (Vbuffer_alist, list1 (Fcons (name, buffer)));
595 596 597
  /* And run buffer-list-update-hook.  */
  if (!NILP (Vrun_hooks))
    call1 (Vrun_hooks, Qbuffer_list_update_hook);
Roland McGrath's avatar
Roland McGrath committed
598

599
  return buffer;
600 601
}

602

603 604 605
/* Return a list of overlays which is a copy of the overlay list
   LIST, but for buffer B.  */

606
static struct Lisp_Overlay *
607
copy_overlays (struct buffer *b, struct Lisp_Overlay *list)
608
{
609
  struct Lisp_Overlay *result = NULL, *tail = NULL;
610

611
  for (; list; list = list->next)
612
    {
Dmitry Antipov's avatar
Dmitry Antipov committed
613 614
      Lisp_Object overlay, start, end;
      struct Lisp_Marker *m;
615

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

621 622
      eassert (MARKERP (list->end));
      m = XMARKER (list->end);
Dmitry Antipov's avatar
Dmitry Antipov committed
623 624
      end = build_marker (b, m->charpos, m->bytepos);
      XMARKER (end)->insertion_type = m->insertion_type;
625

626
      overlay = build_overlay (start, end, Fcopy_sequence (list->plist));
627 628 629 630
      if (tail)
	tail = tail->next = XOVERLAY (overlay);
      else
	result = tail = XOVERLAY (overlay);
631 632
    }

633
  return result;
634
}
635

636 637
/* Set an appropriate overlay of B.  */

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

644
static void
645 646 647 648
set_buffer_overlays_after (struct buffer *b, struct Lisp_Overlay *o)
{
  b->overlays_after = o;
}
649

650 651 652 653 654 655 656 657
/* 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
658
clone_per_buffer_values (struct buffer *from, struct buffer *to)
659 660 661
{
  int offset;

662
  FOR_EACH_PER_BUFFER_OBJECT_AT (offset)
663 664 665
    {
      Lisp_Object obj;

666 667 668 669
      /* Don't touch the `name' which should be unique for every buffer.  */
      if (offset == PER_BUFFER_VAR_OFFSET (name))
	continue;

670
      obj = per_buffer_value (from, offset);
671
      if (MARKERP (obj) && XMARKER (obj)->buffer == from)
672 673
	{
	  struct Lisp_Marker *m = XMARKER (obj);
674 675

	  obj = build_marker (to, m->charpos, m->bytepos);
676 677 678
	  XMARKER (obj)->insertion_type = m->insertion_type;
	}

679
      set_per_buffer_value (to, offset, obj);
680 681
    }

682
  memcpy (to->local_flags, from->local_flags, sizeof to->local_flags);
683

684 685
  set_buffer_overlays_before (to, copy_overlays (to, from->overlays_before));
  set_buffer_overlays_after (to, copy_overlays (to, from->overlays_after));
686

687 688
  /* 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
689
  bset_local_var_alist (to, buffer_lisp_local_variables (from, 1));
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 733 734 735 736 737 738

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


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

Kenichi Handa's avatar
Kenichi Handa committed
753
  CHECK_STRING (name);
754 755
  buf = Fget_buffer (name);
  if (!NILP (buf))
756
    error ("Buffer name `%s' is in use", SDATA (name));
757

Kenichi Handa's avatar
Kenichi Handa committed
758
  tem = base_buffer;
759 760
  base_buffer = Fget_buffer (base_buffer);
  if (NILP (base_buffer))
Kenichi Handa's avatar
Kenichi Handa committed
761
    error ("No such buffer: `%s'", SDATA (tem));
762
  if (!BUFFER_LIVE_P (XBUFFER (base_buffer)))
Kenichi Handa's avatar
Kenichi Handa committed
763
    error ("Base buffer has been killed");
764

765
  if (SCHARS (name) == 0)
766 767
    error ("Empty string for buffer name is not allowed");

768
  b = allocate_buffer ();
769

770 771
  /* No double indirection - if base buffer is indirect,
     new buffer becomes an indirect to base's base.  */
772 773 774
  b->base_buffer = (XBUFFER (base_buffer)->base_buffer
		    ? XBUFFER (base_buffer)<