Commit | Line | Data |
---|---|---|
1ab256cb | 1 | /* Buffer manipulation primitives for GNU Emacs. |
66c6abf0 | 2 | |
96a72ee9 | 3 | Copyright (C) 1985-1989, 1993-1995, 1997-2012 Free Software Foundation, Inc. |
1ab256cb RM |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
1ab256cb | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
1ab256cb RM |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
1ab256cb | 19 | |
68c45bf0 | 20 | #include <config.h> |
1ab256cb | 21 | |
f162bcc3 PE |
22 | #define BUFFER_INLINE EXTERN_INLINE |
23 | ||
2381d133 JB |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> | |
1ab256cb | 26 | #include <sys/param.h> |
9dde47f5 | 27 | #include <errno.h> |
15e7873a | 28 | #include <stdio.h> |
d7306fe6 | 29 | #include <setjmp.h> |
dfcf069d | 30 | #include <unistd.h> |
7ee72033 | 31 | |
d6202519 PE |
32 | #include <verify.h> |
33 | ||
1ab256cb | 34 | #include "lisp.h" |
21cf4cf8 | 35 | #include "intervals.h" |
1ab256cb RM |
36 | #include "window.h" |
37 | #include "commands.h" | |
8f348ed5 | 38 | #include "character.h" |
e5560ff7 | 39 | #include "buffer.h" |
28e969dd | 40 | #include "region-cache.h" |
1ab256cb | 41 | #include "indent.h" |
d014bf88 | 42 | #include "blockinput.h" |
2538fae4 | 43 | #include "keyboard.h" |
e35f6ff7 | 44 | #include "keymap.h" |
08460cd4 | 45 | #include "frame.h" |
1ab256cb RM |
46 | |
47 | struct buffer *current_buffer; /* the current buffer */ | |
48 | ||
49 | /* First buffer in chain of all buffers (in reverse order of creation). | |
179dade4 | 50 | Threaded through ->header.next.buffer. */ |
1ab256cb RM |
51 | |
52 | struct buffer *all_buffers; | |
53 | ||
54 | /* This structure holds the default values of the buffer-local variables | |
55 | defined with DEFVAR_PER_BUFFER, that have special slots in each buffer. | |
56 | The default value occupies the same slot in this structure | |
57 | as an individual buffer's value occupies in that buffer. | |
58 | Setting the default value also goes through the alist of buffers | |
59 | and stores into each buffer that does not say it has a local value. */ | |
60 | ||
e32a5799 | 61 | struct buffer alignas (GCALIGNMENT) buffer_defaults; |
1ab256cb RM |
62 | |
63 | /* A Lisp_Object pointer to the above, used for staticpro */ | |
64 | ||
65 | static Lisp_Object Vbuffer_defaults; | |
66 | ||
67 | /* This structure marks which slots in a buffer have corresponding | |
68 | default values in buffer_defaults. | |
69 | Each such slot has a nonzero value in this structure. | |
70 | The value has only one nonzero bit. | |
71 | ||
72 | When a buffer has its own local value for a slot, | |
7c02e886 GM |
73 | the entry for that slot (found in the same slot in this structure) |
74 | is turned on in the buffer's local_flags array. | |
1ab256cb RM |
75 | |
76 | If a slot in this structure is -1, then even though there may | |
77 | be a DEFVAR_PER_BUFFER for the slot, there is no default value for it; | |
78 | and the corresponding slot in buffer_defaults is not used. | |
79 | ||
1ab256cb RM |
80 | If a slot in this structure corresponding to a DEFVAR_PER_BUFFER is |
81 | zero, that is a bug */ | |
82 | ||
83 | struct buffer buffer_local_flags; | |
84 | ||
85 | /* 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. */ | |
87 | ||
e32a5799 | 88 | struct buffer alignas (GCALIGNMENT) buffer_local_symbols; |
6b61353c | 89 | |
1ab256cb RM |
90 | /* A Lisp_Object pointer to the above, used for staticpro */ |
91 | static Lisp_Object Vbuffer_local_symbols; | |
92 | ||
ce5b453a SM |
93 | /* Return the symbol of the per-buffer variable at offset OFFSET in |
94 | the buffer structure. */ | |
95 | ||
96 | #define PER_BUFFER_SYMBOL(OFFSET) \ | |
97 | (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_symbols)) | |
98 | ||
21514da7 PE |
99 | /* Maximum length of an overlay vector. */ |
100 | #define OVERLAY_COUNT_MAX \ | |
101 | ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM, \ | |
663e2b3f | 102 | min (PTRDIFF_MAX, SIZE_MAX) / word_size)) |
21514da7 | 103 | |
13de9290 RS |
104 | /* Flags indicating which built-in buffer-local variables |
105 | are permanent locals. */ | |
7313acd0 | 106 | static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS]; |
7c02e886 GM |
107 | |
108 | /* Number of per-buffer variables used. */ | |
109 | ||
7313acd0 | 110 | int last_per_buffer_idx; |
13de9290 | 111 | |
f57e2426 | 112 | static void call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, |
37ef52bb | 113 | bool after, Lisp_Object arg1, |
f57e2426 J |
114 | Lisp_Object arg2, Lisp_Object arg3); |
115 | static void swap_out_buffer_local_variables (struct buffer *b); | |
37ef52bb | 116 | static void reset_buffer_local_variables (struct buffer *, bool); |
1ab256cb RM |
117 | |
118 | /* Alist of all buffer names vs the buffers. */ | |
119 | /* This used to be a variable, but is no longer, | |
120 | to prevent lossage due to user rplac'ing this alist or its elements. */ | |
121 | Lisp_Object Vbuffer_alist; | |
122 | ||
955cbe7b | 123 | static Lisp_Object Qkill_buffer_query_functions; |
dcdffbf6 | 124 | |
43ed3b8d | 125 | /* Hook run before changing a major mode. */ |
955cbe7b | 126 | static Lisp_Object Qchange_major_mode_hook; |
22378665 | 127 | |
dbc4e1c1 | 128 | Lisp_Object Qfirst_change_hook; |
22378665 RS |
129 | Lisp_Object Qbefore_change_functions; |
130 | Lisp_Object Qafter_change_functions; | |
1ab256cb | 131 | |
955cbe7b PE |
132 | static Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local; |
133 | static Lisp_Object Qpermanent_local_hook; | |
1ab256cb | 134 | |
955cbe7b | 135 | static Lisp_Object Qprotected_field; |
1ab256cb | 136 | |
955cbe7b | 137 | static Lisp_Object QSFundamental; /* A string "Fundamental" */ |
1ab256cb | 138 | |
955cbe7b | 139 | static Lisp_Object Qkill_buffer_hook; |
9397e56f | 140 | static Lisp_Object Qbuffer_list_update_hook; |
1ab256cb | 141 | |
955cbe7b | 142 | static Lisp_Object Qget_file_buffer; |
5fe0b67e | 143 | |
955cbe7b | 144 | static Lisp_Object Qoverlayp; |
52f8ec73 | 145 | |
955cbe7b | 146 | Lisp_Object Qpriority, Qbefore_string, Qafter_string; |
9397e56f | 147 | |
28545e04 | 148 | static Lisp_Object Qevaporate; |
5985d248 | 149 | |
294d215f RS |
150 | Lisp_Object Qmodification_hooks; |
151 | Lisp_Object Qinsert_in_front_hooks; | |
152 | Lisp_Object Qinsert_behind_hooks; | |
153 | ||
fd05c7e9 | 154 | static void alloc_buffer_text (struct buffer *, ptrdiff_t); |
f57e2426 J |
155 | static void free_buffer_text (struct buffer *b); |
156 | static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *); | |
d311d28c | 157 | static void modify_overlay (struct buffer *, ptrdiff_t, ptrdiff_t); |
37ef52bb | 158 | static Lisp_Object buffer_lisp_local_variables (struct buffer *, bool); |
b86af064 | 159 | |
39eb03f1 PE |
160 | /* These setters are used only in this file, so they can be private. */ |
161 | static inline void | |
162 | bset_abbrev_mode (struct buffer *b, Lisp_Object val) | |
163 | { | |
164 | b->INTERNAL_FIELD (abbrev_mode) = val; | |
165 | } | |
166 | static inline void | |
167 | bset_abbrev_table (struct buffer *b, Lisp_Object val) | |
168 | { | |
169 | b->INTERNAL_FIELD (abbrev_table) = val; | |
170 | } | |
171 | static inline void | |
172 | bset_auto_fill_function (struct buffer *b, Lisp_Object val) | |
173 | { | |
174 | b->INTERNAL_FIELD (auto_fill_function) = val; | |
175 | } | |
176 | static inline void | |
177 | bset_auto_save_file_format (struct buffer *b, Lisp_Object val) | |
178 | { | |
179 | b->INTERNAL_FIELD (auto_save_file_format) = val; | |
180 | } | |
181 | static inline void | |
182 | bset_auto_save_file_name (struct buffer *b, Lisp_Object val) | |
183 | { | |
184 | b->INTERNAL_FIELD (auto_save_file_name) = val; | |
185 | } | |
186 | static inline void | |
187 | bset_backed_up (struct buffer *b, Lisp_Object val) | |
188 | { | |
189 | b->INTERNAL_FIELD (backed_up) = val; | |
190 | } | |
191 | static inline void | |
192 | bset_begv_marker (struct buffer *b, Lisp_Object val) | |
193 | { | |
194 | b->INTERNAL_FIELD (begv_marker) = val; | |
195 | } | |
196 | static inline void | |
197 | bset_bidi_display_reordering (struct buffer *b, Lisp_Object val) | |
198 | { | |
199 | b->INTERNAL_FIELD (bidi_display_reordering) = val; | |
200 | } | |
201 | static inline void | |
202 | bset_buffer_file_coding_system (struct buffer *b, Lisp_Object val) | |
203 | { | |
204 | b->INTERNAL_FIELD (buffer_file_coding_system) = val; | |
205 | } | |
206 | static inline void | |
207 | bset_cache_long_line_scans (struct buffer *b, Lisp_Object val) | |
208 | { | |
209 | b->INTERNAL_FIELD (cache_long_line_scans) = val; | |
210 | } | |
211 | static inline void | |
212 | bset_case_fold_search (struct buffer *b, Lisp_Object val) | |
213 | { | |
214 | b->INTERNAL_FIELD (case_fold_search) = val; | |
215 | } | |
216 | static inline void | |
217 | bset_ctl_arrow (struct buffer *b, Lisp_Object val) | |
218 | { | |
219 | b->INTERNAL_FIELD (ctl_arrow) = val; | |
220 | } | |
221 | static inline void | |
222 | bset_cursor_in_non_selected_windows (struct buffer *b, Lisp_Object val) | |
223 | { | |
224 | b->INTERNAL_FIELD (cursor_in_non_selected_windows) = val; | |
225 | } | |
226 | static inline void | |
227 | bset_cursor_type (struct buffer *b, Lisp_Object val) | |
228 | { | |
229 | b->INTERNAL_FIELD (cursor_type) = val; | |
230 | } | |
231 | static inline void | |
232 | bset_display_table (struct buffer *b, Lisp_Object val) | |
233 | { | |
234 | b->INTERNAL_FIELD (display_table) = val; | |
235 | } | |
236 | static inline void | |
237 | bset_extra_line_spacing (struct buffer *b, Lisp_Object val) | |
238 | { | |
239 | b->INTERNAL_FIELD (extra_line_spacing) = val; | |
240 | } | |
241 | static inline void | |
242 | bset_file_format (struct buffer *b, Lisp_Object val) | |
243 | { | |
244 | b->INTERNAL_FIELD (file_format) = val; | |
245 | } | |
246 | static inline void | |
247 | bset_file_truename (struct buffer *b, Lisp_Object val) | |
248 | { | |
249 | b->INTERNAL_FIELD (file_truename) = val; | |
250 | } | |
251 | static inline void | |
252 | bset_fringe_cursor_alist (struct buffer *b, Lisp_Object val) | |
253 | { | |
254 | b->INTERNAL_FIELD (fringe_cursor_alist) = val; | |
255 | } | |
256 | static inline void | |
257 | bset_fringe_indicator_alist (struct buffer *b, Lisp_Object val) | |
258 | { | |
259 | b->INTERNAL_FIELD (fringe_indicator_alist) = val; | |
260 | } | |
261 | static inline void | |
262 | bset_fringes_outside_margins (struct buffer *b, Lisp_Object val) | |
263 | { | |
264 | b->INTERNAL_FIELD (fringes_outside_margins) = val; | |
265 | } | |
266 | static inline void | |
267 | bset_header_line_format (struct buffer *b, Lisp_Object val) | |
268 | { | |
269 | b->INTERNAL_FIELD (header_line_format) = val; | |
270 | } | |
271 | static inline void | |
272 | bset_indicate_buffer_boundaries (struct buffer *b, Lisp_Object val) | |
273 | { | |
274 | b->INTERNAL_FIELD (indicate_buffer_boundaries) = val; | |
275 | } | |
276 | static inline void | |
277 | bset_indicate_empty_lines (struct buffer *b, Lisp_Object val) | |
278 | { | |
279 | b->INTERNAL_FIELD (indicate_empty_lines) = val; | |
280 | } | |
281 | static inline void | |
282 | bset_invisibility_spec (struct buffer *b, Lisp_Object val) | |
283 | { | |
284 | b->INTERNAL_FIELD (invisibility_spec) = val; | |
285 | } | |
286 | static inline void | |
287 | bset_left_fringe_width (struct buffer *b, Lisp_Object val) | |
288 | { | |
289 | b->INTERNAL_FIELD (left_fringe_width) = val; | |
290 | } | |
291 | static inline void | |
292 | bset_major_mode (struct buffer *b, Lisp_Object val) | |
293 | { | |
294 | b->INTERNAL_FIELD (major_mode) = val; | |
295 | } | |
296 | static inline void | |
297 | bset_mark (struct buffer *b, Lisp_Object val) | |
298 | { | |
299 | b->INTERNAL_FIELD (mark) = val; | |
300 | } | |
301 | static inline void | |
302 | bset_minor_modes (struct buffer *b, Lisp_Object val) | |
303 | { | |
304 | b->INTERNAL_FIELD (minor_modes) = val; | |
305 | } | |
306 | static inline void | |
307 | bset_mode_line_format (struct buffer *b, Lisp_Object val) | |
308 | { | |
309 | b->INTERNAL_FIELD (mode_line_format) = val; | |
310 | } | |
311 | static inline void | |
312 | bset_mode_name (struct buffer *b, Lisp_Object val) | |
313 | { | |
314 | b->INTERNAL_FIELD (mode_name) = val; | |
315 | } | |
316 | static inline void | |
317 | bset_name (struct buffer *b, Lisp_Object val) | |
318 | { | |
319 | b->INTERNAL_FIELD (name) = val; | |
320 | } | |
321 | static inline void | |
322 | bset_overwrite_mode (struct buffer *b, Lisp_Object val) | |
323 | { | |
324 | b->INTERNAL_FIELD (overwrite_mode) = val; | |
325 | } | |
326 | static inline void | |
327 | bset_pt_marker (struct buffer *b, Lisp_Object val) | |
328 | { | |
329 | b->INTERNAL_FIELD (pt_marker) = val; | |
330 | } | |
331 | static inline void | |
332 | bset_right_fringe_width (struct buffer *b, Lisp_Object val) | |
333 | { | |
334 | b->INTERNAL_FIELD (right_fringe_width) = val; | |
335 | } | |
336 | static inline void | |
337 | bset_save_length (struct buffer *b, Lisp_Object val) | |
338 | { | |
339 | b->INTERNAL_FIELD (save_length) = val; | |
340 | } | |
341 | static inline void | |
342 | bset_scroll_bar_width (struct buffer *b, Lisp_Object val) | |
343 | { | |
344 | b->INTERNAL_FIELD (scroll_bar_width) = val; | |
345 | } | |
346 | static inline void | |
347 | bset_scroll_down_aggressively (struct buffer *b, Lisp_Object val) | |
348 | { | |
349 | b->INTERNAL_FIELD (scroll_down_aggressively) = val; | |
350 | } | |
351 | static inline void | |
352 | bset_scroll_up_aggressively (struct buffer *b, Lisp_Object val) | |
353 | { | |
354 | b->INTERNAL_FIELD (scroll_up_aggressively) = val; | |
355 | } | |
356 | static inline void | |
357 | bset_selective_display (struct buffer *b, Lisp_Object val) | |
358 | { | |
359 | b->INTERNAL_FIELD (selective_display) = val; | |
360 | } | |
361 | static inline void | |
362 | bset_selective_display_ellipses (struct buffer *b, Lisp_Object val) | |
363 | { | |
364 | b->INTERNAL_FIELD (selective_display_ellipses) = val; | |
365 | } | |
366 | static inline void | |
367 | bset_vertical_scroll_bar_type (struct buffer *b, Lisp_Object val) | |
368 | { | |
369 | b->INTERNAL_FIELD (vertical_scroll_bar_type) = val; | |
370 | } | |
371 | static inline void | |
372 | bset_word_wrap (struct buffer *b, Lisp_Object val) | |
373 | { | |
374 | b->INTERNAL_FIELD (word_wrap) = val; | |
375 | } | |
376 | static inline void | |
377 | bset_zv_marker (struct buffer *b, Lisp_Object val) | |
378 | { | |
379 | b->INTERNAL_FIELD (zv_marker) = val; | |
380 | } | |
381 | ||
1ab256cb RM |
382 | /* For debugging; temporary. See set_buffer_internal. */ |
383 | /* Lisp_Object Qlisp_mode, Vcheck_symbol; */ | |
384 | ||
01136e9b | 385 | void |
d3da34e0 | 386 | nsberror (Lisp_Object spec) |
1ab256cb | 387 | { |
a7a60ce9 | 388 | if (STRINGP (spec)) |
d5db4077 | 389 | error ("No buffer named %s", SDATA (spec)); |
1ab256cb RM |
390 | error ("Invalid buffer argument"); |
391 | } | |
392 | \f | |
a7ca3326 | 393 | DEFUN ("buffer-live-p", Fbuffer_live_p, Sbuffer_live_p, 1, 1, 0, |
7ee72033 MB |
394 | doc: /* Return non-nil if OBJECT is a buffer which has not been killed. |
395 | Value is nil if OBJECT is not a buffer or if it has been killed. */) | |
5842a27b | 396 | (Lisp_Object object) |
0dc88e60 | 397 | { |
4b4deea2 | 398 | return ((BUFFERP (object) && ! NILP (BVAR (XBUFFER (object), name))) |
0dc88e60 RS |
399 | ? Qt : Qnil); |
400 | } | |
401 | ||
08460cd4 | 402 | DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0, |
7ee72033 | 403 | doc: /* Return a list of all existing live buffers. |
9397e56f MR |
404 | If the optional arg FRAME is a frame, we return the buffer list in the |
405 | proper order for that frame: the buffers show in FRAME come first, | |
406 | followed by the rest of the buffers. */) | |
5842a27b | 407 | (Lisp_Object frame) |
1ab256cb | 408 | { |
a18b8cb5 | 409 | Lisp_Object general; |
08460cd4 RS |
410 | general = Fmapcar (Qcdr, Vbuffer_alist); |
411 | ||
412 | if (FRAMEP (frame)) | |
413 | { | |
a18b8cb5 KL |
414 | Lisp_Object framelist, prevlist, tail; |
415 | Lisp_Object args[3]; | |
08460cd4 | 416 | |
b7826503 | 417 | CHECK_FRAME (frame); |
e69b0960 | 418 | framelist = Fcopy_sequence (XFRAME (frame)->buffer_list); |
9397e56f | 419 | prevlist = Fnreverse (Fcopy_sequence |
e69b0960 | 420 | (XFRAME (frame)->buried_buffer_list)); |
08460cd4 | 421 | |
a18b8cb5 KL |
422 | /* Remove from GENERAL any buffer that duplicates one in |
423 | FRAMELIST or PREVLIST. */ | |
08460cd4 | 424 | tail = framelist; |
a18b8cb5 | 425 | while (CONSP (tail)) |
08460cd4 | 426 | { |
7539e11f KR |
427 | general = Fdelq (XCAR (tail), general); |
428 | tail = XCDR (tail); | |
08460cd4 | 429 | } |
a18b8cb5 KL |
430 | tail = prevlist; |
431 | while (CONSP (tail)) | |
432 | { | |
433 | general = Fdelq (XCAR (tail), general); | |
434 | tail = XCDR (tail); | |
435 | } | |
436 | ||
437 | args[0] = framelist; | |
438 | args[1] = general; | |
439 | args[2] = prevlist; | |
440 | return Fnconc (3, args); | |
08460cd4 | 441 | } |
9397e56f MR |
442 | else |
443 | return general; | |
1ab256cb RM |
444 | } |
445 | ||
04ae1b48 RS |
446 | /* Like Fassoc, but use Fstring_equal to compare |
447 | (which ignores text properties), | |
448 | and don't ever QUIT. */ | |
449 | ||
450 | static Lisp_Object | |
d3da34e0 | 451 | assoc_ignore_text_properties (register Lisp_Object key, Lisp_Object list) |
04ae1b48 RS |
452 | { |
453 | register Lisp_Object tail; | |
6d70a280 | 454 | for (tail = list; CONSP (tail); tail = XCDR (tail)) |
04ae1b48 RS |
455 | { |
456 | register Lisp_Object elt, tem; | |
6d70a280 | 457 | elt = XCAR (tail); |
04ae1b48 RS |
458 | tem = Fstring_equal (Fcar (elt), key); |
459 | if (!NILP (tem)) | |
460 | return elt; | |
461 | } | |
462 | return Qnil; | |
463 | } | |
464 | ||
a7ca3326 | 465 | DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0, |
c8804c4f MR |
466 | doc: /* Return the buffer named BUFFER-OR-NAME. |
467 | BUFFER-OR-NAME must be either a string or a buffer. If BUFFER-OR-NAME | |
468 | is a string and there is no buffer with that name, return nil. If | |
469 | BUFFER-OR-NAME is a buffer, return it as given. */) | |
5842a27b | 470 | (register Lisp_Object buffer_or_name) |
1ab256cb | 471 | { |
c8804c4f MR |
472 | if (BUFFERP (buffer_or_name)) |
473 | return buffer_or_name; | |
474 | CHECK_STRING (buffer_or_name); | |
1ab256cb | 475 | |
c8804c4f | 476 | return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist)); |
1ab256cb RM |
477 | } |
478 | ||
479 | DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0, | |
7ee72033 | 480 | doc: /* Return the buffer visiting file FILENAME (a string). |
018ba359 PJ |
481 | The buffer's `buffer-file-name' must match exactly the expansion of FILENAME. |
482 | If there is no such live buffer, return nil. | |
7ee72033 | 483 | See also `find-buffer-visiting'. */) |
5842a27b | 484 | (register Lisp_Object filename) |
1ab256cb RM |
485 | { |
486 | register Lisp_Object tail, buf, tem; | |
5fe0b67e RS |
487 | Lisp_Object handler; |
488 | ||
b7826503 | 489 | CHECK_STRING (filename); |
1ab256cb RM |
490 | filename = Fexpand_file_name (filename, Qnil); |
491 | ||
5fe0b67e RS |
492 | /* If the file name has special constructs in it, |
493 | call the corresponding file handler. */ | |
a617e913 | 494 | handler = Ffind_file_name_handler (filename, Qget_file_buffer); |
5fe0b67e | 495 | if (!NILP (handler)) |
a59225b1 CY |
496 | { |
497 | Lisp_Object handled_buf = call2 (handler, Qget_file_buffer, | |
498 | filename); | |
499 | return BUFFERP (handled_buf) ? handled_buf : Qnil; | |
500 | } | |
5fe0b67e | 501 | |
7539e11f | 502 | for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail)) |
1ab256cb | 503 | { |
7539e11f | 504 | buf = Fcdr (XCAR (tail)); |
a7a60ce9 | 505 | if (!BUFFERP (buf)) continue; |
4b4deea2 TT |
506 | if (!STRINGP (BVAR (XBUFFER (buf), filename))) continue; |
507 | tem = Fstring_equal (BVAR (XBUFFER (buf), filename), filename); | |
265a9e55 | 508 | if (!NILP (tem)) |
1ab256cb RM |
509 | return buf; |
510 | } | |
511 | return Qnil; | |
512 | } | |
513 | ||
52e01189 | 514 | Lisp_Object |
d3da34e0 | 515 | get_truename_buffer (register Lisp_Object filename) |
52e01189 RS |
516 | { |
517 | register Lisp_Object tail, buf, tem; | |
518 | ||
7539e11f | 519 | for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail)) |
52e01189 | 520 | { |
7539e11f | 521 | buf = Fcdr (XCAR (tail)); |
52e01189 | 522 | if (!BUFFERP (buf)) continue; |
4b4deea2 TT |
523 | if (!STRINGP (BVAR (XBUFFER (buf), file_truename))) continue; |
524 | tem = Fstring_equal (BVAR (XBUFFER (buf), file_truename), filename); | |
52e01189 RS |
525 | if (!NILP (tem)) |
526 | return buf; | |
527 | } | |
528 | return Qnil; | |
529 | } | |
530 | ||
a7ca3326 | 531 | DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0, |
cd265ca6 MR |
532 | doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed. |
533 | If BUFFER-OR-NAME is a string and a live buffer with that name exists, | |
534 | return that buffer. If no such buffer exists, create a new buffer with | |
535 | that name and return it. If BUFFER-OR-NAME starts with a space, the new | |
536 | buffer does not keep undo information. | |
537 | ||
538 | If BUFFER-OR-NAME is a buffer instead of a string, return it as given, | |
539 | even if it is dead. The return value is never nil. */) | |
5842a27b | 540 | (register Lisp_Object buffer_or_name) |
1ab256cb | 541 | { |
cd265ca6 | 542 | register Lisp_Object buffer, name; |
1ab256cb RM |
543 | register struct buffer *b; |
544 | ||
cd265ca6 MR |
545 | buffer = Fget_buffer (buffer_or_name); |
546 | if (!NILP (buffer)) | |
547 | return buffer; | |
1ab256cb | 548 | |
cd265ca6 | 549 | if (SCHARS (buffer_or_name) == 0) |
31cd83e9 KH |
550 | error ("Empty string for buffer name is not allowed"); |
551 | ||
cc648cef | 552 | b = allocate_buffer (); |
1ab256cb | 553 | |
336cd056 RS |
554 | /* An ordinary buffer uses its own struct buffer_text. */ |
555 | b->text = &b->own_text; | |
9928463d DA |
556 | b->base_buffer = NULL; |
557 | /* No one shares the text with us now. */ | |
558 | b->indirections = 0; | |
336cd056 | 559 | |
1ab256cb | 560 | BUF_GAP_SIZE (b) = 20; |
9ac0d9e0 | 561 | BLOCK_INPUT; |
3b06f880 KH |
562 | /* We allocate extra 1-byte at the tail and keep it always '\0' for |
563 | anchoring a search. */ | |
b86af064 | 564 | alloc_buffer_text (b, BUF_GAP_SIZE (b) + 1); |
9ac0d9e0 | 565 | UNBLOCK_INPUT; |
1ab256cb | 566 | if (! BUF_BEG_ADDR (b)) |
531b0165 | 567 | buffer_memory_full (BUF_GAP_SIZE (b) + 1); |
1ab256cb | 568 | |
cffc6f3b CY |
569 | b->pt = BEG; |
570 | b->begv = BEG; | |
571 | b->zv = BEG; | |
572 | b->pt_byte = BEG_BYTE; | |
573 | b->begv_byte = BEG_BYTE; | |
574 | b->zv_byte = BEG_BYTE; | |
575 | ||
6d70a280 | 576 | BUF_GPT (b) = BEG; |
6d70a280 | 577 | BUF_GPT_BYTE (b) = BEG_BYTE; |
cffc6f3b CY |
578 | |
579 | BUF_Z (b) = BEG; | |
6d70a280 | 580 | BUF_Z_BYTE (b) = BEG_BYTE; |
1ab256cb | 581 | BUF_MODIFF (b) = 1; |
3e145152 | 582 | BUF_CHARS_MODIFF (b) = 1; |
2509d356 | 583 | BUF_OVERLAY_MODIFF (b) = 1; |
336cd056 | 584 | BUF_SAVE_MODIFF (b) = 1; |
0c94c8d6 | 585 | set_buffer_intervals (b, NULL); |
b5a225b4 GM |
586 | BUF_UNCHANGED_MODIFIED (b) = 1; |
587 | BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1; | |
588 | BUF_END_UNCHANGED (b) = 0; | |
589 | BUF_BEG_UNCHANGED (b) = 0; | |
3b06f880 | 590 | *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'. */ |
2941c447 | 591 | b->text->inhibit_shrinking = 0; |
1ab256cb | 592 | |
28e969dd JB |
593 | b->newline_cache = 0; |
594 | b->width_run_cache = 0; | |
39eb03f1 | 595 | bset_width_table (b, Qnil); |
b5a225b4 | 596 | b->prevent_redisplay_optimizations_p = 1; |
28e969dd | 597 | |
1ab256cb | 598 | /* Put this on the chain of all buffers including killed ones. */ |
eab3844f | 599 | b->header.next.buffer = all_buffers; |
1ab256cb RM |
600 | all_buffers = b; |
601 | ||
336cd056 RS |
602 | /* An ordinary buffer normally doesn't need markers |
603 | to handle BEGV and ZV. */ | |
39eb03f1 PE |
604 | bset_pt_marker (b, Qnil); |
605 | bset_begv_marker (b, Qnil); | |
606 | bset_zv_marker (b, Qnil); | |
04ae1b48 | 607 | |
cd265ca6 | 608 | name = Fcopy_sequence (buffer_or_name); |
0c94c8d6 | 609 | set_string_intervals (name, NULL); |
39eb03f1 | 610 | bset_name (b, name); |
04ae1b48 | 611 | |
39eb03f1 | 612 | bset_undo_list (b, SREF (name, 0) != ' ' ? Qnil : Qt); |
1ab256cb RM |
613 | |
614 | reset_buffer (b); | |
13de9290 | 615 | reset_buffer_local_variables (b, 1); |
1ab256cb | 616 | |
39eb03f1 | 617 | bset_mark (b, Fmake_marker ()); |
65745fad | 618 | BUF_MARKERS (b) = NULL; |
1f57cb74 | 619 | |
1ab256cb | 620 | /* Put this in the alist of all live buffers. */ |
cd265ca6 MR |
621 | XSETBUFFER (buffer, b); |
622 | Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buffer), Qnil)); | |
9397e56f MR |
623 | /* And run buffer-list-update-hook. */ |
624 | if (!NILP (Vrun_hooks)) | |
625 | call1 (Vrun_hooks, Qbuffer_list_update_hook); | |
1ab256cb | 626 | |
cd265ca6 | 627 | return buffer; |
336cd056 RS |
628 | } |
629 | ||
7e9d5818 | 630 | |
04e4cb3a GM |
631 | /* Return a list of overlays which is a copy of the overlay list |
632 | LIST, but for buffer B. */ | |
633 | ||
2410d73a | 634 | static struct Lisp_Overlay * |
d3da34e0 | 635 | copy_overlays (struct buffer *b, struct Lisp_Overlay *list) |
04e4cb3a | 636 | { |
2410d73a | 637 | struct Lisp_Overlay *result = NULL, *tail = NULL; |
04e4cb3a | 638 | |
2410d73a | 639 | for (; list; list = list->next) |
04e4cb3a | 640 | { |
fa691a83 DA |
641 | Lisp_Object overlay, start, end; |
642 | struct Lisp_Marker *m; | |
04e4cb3a | 643 | |
c644523b DA |
644 | eassert (MARKERP (list->start)); |
645 | m = XMARKER (list->start); | |
fa691a83 DA |
646 | start = build_marker (b, m->charpos, m->bytepos); |
647 | XMARKER (start)->insertion_type = m->insertion_type; | |
04e4cb3a | 648 | |
c644523b DA |
649 | eassert (MARKERP (list->end)); |
650 | m = XMARKER (list->end); | |
fa691a83 DA |
651 | end = build_marker (b, m->charpos, m->bytepos); |
652 | XMARKER (end)->insertion_type = m->insertion_type; | |
177c0ea7 | 653 | |
c644523b | 654 | overlay = build_overlay (start, end, Fcopy_sequence (list->plist)); |
2410d73a SM |
655 | if (tail) |
656 | tail = tail->next = XOVERLAY (overlay); | |
657 | else | |
658 | result = tail = XOVERLAY (overlay); | |
04e4cb3a GM |
659 | } |
660 | ||
2410d73a | 661 | return result; |
04e4cb3a | 662 | } |
177c0ea7 | 663 | |
0c94c8d6 PE |
664 | /* Set an appropriate overlay of B. */ |
665 | ||
666 | static inline void | |
667 | set_buffer_overlays_before (struct buffer *b, struct Lisp_Overlay *o) | |
668 | { | |
669 | b->overlays_before = o; | |
670 | } | |
671 | ||
672 | static inline void | |
673 | set_buffer_overlays_after (struct buffer *b, struct Lisp_Overlay *o) | |
674 | { | |
675 | b->overlays_after = o; | |
676 | } | |
04e4cb3a | 677 | |
7e9d5818 GM |
678 | /* Clone per-buffer values of buffer FROM. |
679 | ||
680 | Buffer TO gets the same per-buffer values as FROM, with the | |
681 | following exceptions: (1) TO's name is left untouched, (2) markers | |
682 | are copied and made to refer to TO, and (3) overlay lists are | |
683 | copied. */ | |
684 | ||
685 | static void | |
d3da34e0 | 686 | clone_per_buffer_values (struct buffer *from, struct buffer *to) |
7e9d5818 | 687 | { |
7e9d5818 GM |
688 | int offset; |
689 | ||
52b852c7 | 690 | FOR_EACH_PER_BUFFER_OBJECT_AT (offset) |
7e9d5818 GM |
691 | { |
692 | Lisp_Object obj; | |
693 | ||
4b7cdc0e SM |
694 | /* Don't touch the `name' which should be unique for every buffer. */ |
695 | if (offset == PER_BUFFER_VAR_OFFSET (name)) | |
696 | continue; | |
697 | ||
4ce60d2e | 698 | obj = per_buffer_value (from, offset); |
ce5b453a | 699 | if (MARKERP (obj) && XMARKER (obj)->buffer == from) |
7e9d5818 GM |
700 | { |
701 | struct Lisp_Marker *m = XMARKER (obj); | |
657924ff DA |
702 | |
703 | obj = build_marker (to, m->charpos, m->bytepos); | |
7e9d5818 | 704 | XMARKER (obj)->insertion_type = m->insertion_type; |
7e9d5818 GM |
705 | } |
706 | ||
4ce60d2e | 707 | set_per_buffer_value (to, offset, obj); |
7e9d5818 GM |
708 | } |
709 | ||
72af86bd | 710 | memcpy (to->local_flags, from->local_flags, sizeof to->local_flags); |
177c0ea7 | 711 | |
0c94c8d6 PE |
712 | set_buffer_overlays_before (to, copy_overlays (to, from->overlays_before)); |
713 | set_buffer_overlays_after (to, copy_overlays (to, from->overlays_after)); | |
02f28bbd | 714 | |
e1688f54 RS |
715 | /* Get (a copy of) the alist of Lisp-level local variables of FROM |
716 | and install that in TO. */ | |
39eb03f1 | 717 | bset_local_var_alist (to, buffer_lisp_local_variables (from, 1)); |
7e9d5818 GM |
718 | } |
719 | ||
cffc6f3b CY |
720 | |
721 | /* If buffer B has markers to record PT, BEGV and ZV when it is not | |
722 | current, update these markers. */ | |
723 | ||
724 | static void | |
725 | record_buffer_markers (struct buffer *b) | |
726 | { | |
727 | if (! NILP (BVAR (b, pt_marker))) | |
728 | { | |
729 | Lisp_Object buffer; | |
730 | ||
731 | eassert (!NILP (BVAR (b, begv_marker))); | |
732 | eassert (!NILP (BVAR (b, zv_marker))); | |
733 | ||
734 | XSETBUFFER (buffer, b); | |
735 | set_marker_both (BVAR (b, pt_marker), buffer, b->pt, b->pt_byte); | |
736 | set_marker_both (BVAR (b, begv_marker), buffer, b->begv, b->begv_byte); | |
737 | set_marker_both (BVAR (b, zv_marker), buffer, b->zv, b->zv_byte); | |
738 | } | |
739 | } | |
740 | ||
741 | ||
742 | /* If buffer B has markers to record PT, BEGV and ZV when it is not | |
743 | current, fetch these values into B->begv etc. */ | |
744 | ||
745 | static void | |
746 | fetch_buffer_markers (struct buffer *b) | |
747 | { | |
748 | if (! NILP (BVAR (b, pt_marker))) | |
749 | { | |
750 | Lisp_Object m; | |
751 | ||
752 | eassert (!NILP (BVAR (b, begv_marker))); | |
753 | eassert (!NILP (BVAR (b, zv_marker))); | |
754 | ||
755 | m = BVAR (b, pt_marker); | |
756 | SET_BUF_PT_BOTH (b, marker_position (m), marker_byte_position (m)); | |
757 | ||
758 | m = BVAR (b, begv_marker); | |
759 | SET_BUF_BEGV_BOTH (b, marker_position (m), marker_byte_position (m)); | |
760 | ||
761 | m = BVAR (b, zv_marker); | |
762 | SET_BUF_ZV_BOTH (b, marker_position (m), marker_byte_position (m)); | |
763 | } | |
764 | } | |
765 | ||
766 | ||
7e9d5818 GM |
767 | DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer, |
768 | 2, 3, | |
193c3837 | 769 | "bMake indirect buffer (to buffer): \nBName of indirect buffer: ", |
7ee72033 | 770 | doc: /* Create and return an indirect buffer for buffer BASE-BUFFER, named NAME. |
6b61353c | 771 | BASE-BUFFER should be a live buffer, or the name of an existing buffer. |
018ba359 PJ |
772 | NAME should be a string which is not the name of an existing buffer. |
773 | Optional argument CLONE non-nil means preserve BASE-BUFFER's state, | |
774 | such as major and minor modes, in the indirect buffer. | |
7ee72033 | 775 | CLONE nil means the indirect buffer's state is reset to default values. */) |
5842a27b | 776 | (Lisp_Object base_buffer, Lisp_Object name, Lisp_Object clone) |
336cd056 | 777 | { |
6b61353c | 778 | Lisp_Object buf, tem; |
7e9d5818 | 779 | struct buffer *b; |
336cd056 | 780 | |
6b61353c | 781 | CHECK_STRING (name); |
336cd056 RS |
782 | buf = Fget_buffer (name); |
783 | if (!NILP (buf)) | |
d5db4077 | 784 | error ("Buffer name `%s' is in use", SDATA (name)); |
336cd056 | 785 | |
6b61353c | 786 | tem = base_buffer; |
336cd056 RS |
787 | base_buffer = Fget_buffer (base_buffer); |
788 | if (NILP (base_buffer)) | |
6b61353c | 789 | error ("No such buffer: `%s'", SDATA (tem)); |
4b4deea2 | 790 | if (NILP (BVAR (XBUFFER (base_buffer), name))) |
6b61353c | 791 | error ("Base buffer has been killed"); |
336cd056 | 792 | |
d5db4077 | 793 | if (SCHARS (name) == 0) |
336cd056 RS |
794 | error ("Empty string for buffer name is not allowed"); |
795 | ||
cc648cef | 796 | b = allocate_buffer (); |
336cd056 | 797 | |
9928463d DA |
798 | /* No double indirection - if base buffer is indirect, |
799 | new buffer becomes an indirect to base's base. */ | |
67ee9f6e SM |
800 | b->base_buffer = (XBUFFER (base_buffer)->base_buffer |
801 | ? XBUFFER (base_buffer)->base_buffer | |
802 | : XBUFFER (base_buffer)); | |
336cd056 RS |
803 | |
804 | /* Use the base buffer's text object. */ | |
805 | b->text = b->base_buffer->text; | |
9928463d DA |
806 | /* We have no own text. */ |
807 | b->indirections = -1; | |
808 | /* Notify base buffer that we share the text now. */ | |
809 | b->base_buffer->indirections++; | |
336cd056 | 810 | |
cffc6f3b CY |
811 | b->pt = b->base_buffer->pt; |
812 | b->begv = b->base_buffer->begv; | |
813 | b->zv = b->base_buffer->zv; | |
814 | b->pt_byte = b->base_buffer->pt_byte; | |
815 | b->begv_byte = b->base_buffer->begv_byte; | |
816 | b->zv_byte = b->base_buffer->zv_byte; | |
336cd056 RS |
817 | |
818 | b->newline_cache = 0; | |
819 | b->width_run_cache = 0; | |
39eb03f1 | 820 | bset_width_table (b, Qnil); |
336cd056 RS |
821 | |
822 | /* Put this on the chain of all buffers including killed ones. */ | |
eab3844f | 823 | b->header.next.buffer = all_buffers; |
336cd056 RS |
824 | all_buffers = b; |
825 | ||
826 | name = Fcopy_sequence (name); | |
0c94c8d6 | 827 | set_string_intervals (name, NULL); |
39eb03f1 | 828 | bset_name (b, name); |
336cd056 RS |
829 | |
830 | reset_buffer (b); | |
13de9290 | 831 | reset_buffer_local_variables (b, 1); |
336cd056 RS |
832 | |
833 | /* Put this in the alist of all live buffers. */ | |
834 | XSETBUFFER (buf, b); | |
835 | Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil)); | |
836 | ||
39eb03f1 | 837 | bset_mark (b, Fmake_marker ()); |
336cd056 | 838 | |
abc9d959 | 839 | /* The multibyte status belongs to the base buffer. */ |
39eb03f1 PE |
840 | bset_enable_multibyte_characters |
841 | (b, BVAR (b->base_buffer, enable_multibyte_characters)); | |
abc9d959 | 842 | |
336cd056 | 843 | /* Make sure the base buffer has markers for its narrowing. */ |
4b4deea2 | 844 | if (NILP (BVAR (b->base_buffer, pt_marker))) |
336cd056 | 845 | { |
2aa46d6c CY |
846 | eassert (NILP (BVAR (b->base_buffer, begv_marker))); |
847 | eassert (NILP (BVAR (b->base_buffer, zv_marker))); | |
cffc6f3b | 848 | |
39eb03f1 PE |
849 | bset_pt_marker (b->base_buffer, |
850 | build_marker (b->base_buffer, b->base_buffer->pt, | |
851 | b->base_buffer->pt_byte)); | |
657924ff | 852 | |
39eb03f1 PE |
853 | bset_begv_marker (b->base_buffer, |
854 | build_marker (b->base_buffer, b->base_buffer->begv, | |
855 | b->base_buffer->begv_byte)); | |
cffc6f3b | 856 | |
39eb03f1 PE |
857 | bset_zv_marker (b->base_buffer, |
858 | build_marker (b->base_buffer, b->base_buffer->zv, | |
859 | b->base_buffer->zv_byte)); | |
cffc6f3b | 860 | |
4b4deea2 | 861 | XMARKER (BVAR (b->base_buffer, zv_marker))->insertion_type = 1; |
336cd056 RS |
862 | } |
863 | ||
7e9d5818 GM |
864 | if (NILP (clone)) |
865 | { | |
866 | /* Give the indirect buffer markers for its narrowing. */ | |
39eb03f1 PE |
867 | bset_pt_marker (b, build_marker (b, b->pt, b->pt_byte)); |
868 | bset_begv_marker (b, build_marker (b, b->begv, b->begv_byte)); | |
869 | bset_zv_marker (b, build_marker (b, b->zv, b->zv_byte)); | |
4b4deea2 | 870 | XMARKER (BVAR (b, zv_marker))->insertion_type = 1; |
7e9d5818 GM |
871 | } |
872 | else | |
7fa57e45 RS |
873 | { |
874 | struct buffer *old_b = current_buffer; | |
875 | ||
876 | clone_per_buffer_values (b->base_buffer, b); | |
39eb03f1 PE |
877 | bset_filename (b, Qnil); |
878 | bset_file_truename (b, Qnil); | |
879 | bset_display_count (b, make_number (0)); | |
880 | bset_backed_up (b, Qnil); | |
881 | bset_auto_save_file_name (b, Qnil); | |
7fa57e45 RS |
882 | set_buffer_internal_1 (b); |
883 | Fset (intern ("buffer-save-without-query"), Qnil); | |
884 | Fset (intern ("buffer-file-number"), Qnil); | |
885 | Fset (intern ("buffer-stale-function"), Qnil); | |
886 | set_buffer_internal_1 (old_b); | |
887 | } | |
336cd056 | 888 | |
9397e56f MR |
889 | /* Run buffer-list-update-hook. */ |
890 | if (!NILP (Vrun_hooks)) | |
891 | call1 (Vrun_hooks, Qbuffer_list_update_hook); | |
892 | ||
a9ee7a59 | 893 | return buf; |
1ab256cb RM |
894 | } |
895 | ||
041a49a6 DA |
896 | /* Mark OV as no longer associated with B. */ |
897 | ||
898 | static void | |
899 | drop_overlay (struct buffer *b, struct Lisp_Overlay *ov) | |
900 | { | |
c644523b DA |
901 | eassert (b == XBUFFER (Fmarker_buffer (ov->start))); |
902 | modify_overlay (b, marker_position (ov->start), | |
903 | marker_position (ov->end)); | |
904 | Fset_marker (ov->start, Qnil, Qnil); | |
905 | Fset_marker (ov->end, Qnil, Qnil); | |
041a49a6 DA |
906 | |
907 | } | |
908 | ||
909 | /* Delete all overlays of B and reset it's overlay lists. */ | |
910 | ||
d4f5719a | 911 | void |
d3da34e0 | 912 | delete_all_overlays (struct buffer *b) |
d4f5719a | 913 | { |
041a49a6 | 914 | struct Lisp_Overlay *ov, *next; |
d4f5719a | 915 | |
fd318b54 | 916 | for (ov = b->overlays_before; ov; ov = next) |
d4f5719a | 917 | { |
041a49a6 DA |
918 | drop_overlay (b, ov); |
919 | next = ov->next; | |
920 | ov->next = NULL; | |
d4f5719a | 921 | } |
041a49a6 | 922 | |
fd318b54 | 923 | for (ov = b->overlays_after; ov; ov = next) |
d4f5719a | 924 | { |
041a49a6 DA |
925 | drop_overlay (b, ov); |
926 | next = ov->next; | |
927 | ov->next = NULL; | |
d4f5719a | 928 | } |
041a49a6 | 929 | |
0c94c8d6 PE |
930 | set_buffer_overlays_before (b, NULL); |
931 | set_buffer_overlays_after (b, NULL); | |
d4f5719a SM |
932 | } |
933 | ||
bcd40520 | 934 | /* Reinitialize everything about a buffer except its name and contents |
6b61353c | 935 | and local variables. |
d4f5719a SM |
936 | If called on an already-initialized buffer, the list of overlays |
937 | should be deleted before calling this function, otherwise we end up | |
938 | with overlays that claim to belong to the buffer but the buffer | |
939 | claims it doesn't belong to it. */ | |
1ab256cb RM |
940 | |
941 | void | |
d3da34e0 | 942 | reset_buffer (register struct buffer *b) |
1ab256cb | 943 | { |
39eb03f1 PE |
944 | bset_filename (b, Qnil); |
945 | bset_file_truename (b, Qnil); | |
946 | bset_directory (b, current_buffer ? BVAR (current_buffer, directory) : Qnil); | |
e9a9ae03 | 947 | b->modtime = make_emacs_time (0, UNKNOWN_MODTIME_NSECS); |
58b963f7 | 948 | b->modtime_size = -1; |
4b4deea2 | 949 | XSETFASTINT (BVAR (b, save_length), 0); |
1ab256cb | 950 | b->last_window_start = 1; |
8b264726 | 951 | /* It is more conservative to start out "changed" than "unchanged". */ |
b5a225b4 GM |
952 | b->clip_changed = 0; |
953 | b->prevent_redisplay_optimizations_p = 1; | |
39eb03f1 | 954 | bset_backed_up (b, Qnil); |
0b5397c2 | 955 | BUF_AUTOSAVE_MODIFF (b) = 0; |
d311d28c | 956 | b->auto_save_failure_time = 0; |
39eb03f1 PE |
957 | bset_auto_save_file_name (b, Qnil); |
958 | bset_read_only (b, Qnil); | |
0c94c8d6 PE |
959 | set_buffer_overlays_before (b, NULL); |
960 | set_buffer_overlays_after (b, NULL); | |
c2d5b10f | 961 | b->overlay_center = BEG; |
39eb03f1 PE |
962 | bset_mark_active (b, Qnil); |
963 | bset_point_before_scroll (b, Qnil); | |
964 | bset_file_format (b, Qnil); | |
965 | bset_auto_save_file_format (b, Qt); | |
966 | bset_last_selected_window (b, Qnil); | |
967 | bset_display_count (b, make_number (0)); | |
968 | bset_display_time (b, Qnil); | |
969 | bset_enable_multibyte_characters | |
970 | (b, BVAR (&buffer_defaults, enable_multibyte_characters)); | |
971 | bset_cursor_type (b, BVAR (&buffer_defaults, cursor_type)); | |
972 | bset_extra_line_spacing (b, BVAR (&buffer_defaults, extra_line_spacing)); | |
0522997d RS |
973 | |
974 | b->display_error_modiff = 0; | |
1ab256cb RM |
975 | } |
976 | ||
bcd40520 RS |
977 | /* Reset buffer B's local variables info. |
978 | Don't use this on a buffer that has already been in use; | |
979 | it does not treat permanent locals consistently. | |
13de9290 RS |
980 | Instead, use Fkill_all_local_variables. |
981 | ||
37ef52bb PE |
982 | If PERMANENT_TOO, reset permanent buffer-local variables. |
983 | If not, preserve those. */ | |
bcd40520 | 984 | |
13de9290 | 985 | static void |
37ef52bb | 986 | reset_buffer_local_variables (struct buffer *b, bool permanent_too) |
1ab256cb | 987 | { |
37ef52bb | 988 | int offset, i; |
1ab256cb RM |
989 | |
990 | /* Reset the major mode to Fundamental, together with all the | |
991 | things that depend on the major mode. | |
992 | default-major-mode is handled at a higher level. | |
993 | We ignore it here. */ | |
39eb03f1 PE |
994 | bset_major_mode (b, Qfundamental_mode); |
995 | bset_keymap (b, Qnil); | |
996 | bset_mode_name (b, QSFundamental); | |
997 | bset_minor_modes (b, Qnil); | |
3446af9c RS |
998 | |
999 | /* If the standard case table has been altered and invalidated, | |
1000 | fix up its insides first. */ | |
1001 | if (! (CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[0]) | |
1002 | && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[1]) | |
1003 | && CHAR_TABLE_P (XCHAR_TABLE (Vascii_downcase_table)->extras[2]))) | |
1004 | Fset_standard_case_table (Vascii_downcase_table); | |
1005 | ||
39eb03f1 PE |
1006 | bset_downcase_table (b, Vascii_downcase_table); |
1007 | bset_upcase_table (b, XCHAR_TABLE (Vascii_downcase_table)->extras[0]); | |
1008 | bset_case_canon_table (b, XCHAR_TABLE (Vascii_downcase_table)->extras[1]); | |
1009 | bset_case_eqv_table (b, XCHAR_TABLE (Vascii_downcase_table)->extras[2]); | |
1010 | bset_invisibility_spec (b, Qt); | |
3cb719bd | 1011 | |
13de9290 | 1012 | /* Reset all (or most) per-buffer variables to their defaults. */ |
3709505e | 1013 | if (permanent_too) |
39eb03f1 | 1014 | bset_local_var_alist (b, Qnil); |
3709505e SM |
1015 | else |
1016 | { | |
2f7a359d | 1017 | Lisp_Object tmp, prop, last = Qnil; |
4b4deea2 | 1018 | for (tmp = BVAR (b, local_var_alist); CONSP (tmp); tmp = XCDR (tmp)) |
ce5b453a | 1019 | if (!NILP (prop = Fget (XCAR (XCAR (tmp)), Qpermanent_local))) |
2f7a359d RS |
1020 | { |
1021 | /* If permanent-local, keep it. */ | |
1022 | last = tmp; | |
1023 | if (EQ (prop, Qpermanent_local_hook)) | |
1024 | { | |
1025 | /* This is a partially permanent hook variable. | |
1026 | Preserve only the elements that want to be preserved. */ | |
1027 | Lisp_Object list, newlist; | |
1028 | list = XCDR (XCAR (tmp)); | |
1029 | if (!CONSP (list)) | |
1030 | newlist = list; | |
1031 | else | |
1032 | for (newlist = Qnil; CONSP (list); list = XCDR (list)) | |
1033 | { | |
1034 | Lisp_Object elt = XCAR (list); | |
1035 | /* Preserve element ELT if it's t, | |
1036 | if it is a function with a `permanent-local-hook' property, | |
1037 | or if it's not a symbol. */ | |
1038 | if (! SYMBOLP (elt) | |
1039 | || EQ (elt, Qt) | |
1040 | || !NILP (Fget (elt, Qpermanent_local_hook))) | |
1041 | newlist = Fcons (elt, newlist); | |
1042 | } | |
1043 | XSETCDR (XCAR (tmp), Fnreverse (newlist)); | |
1044 | } | |
1045 | } | |
1046 | /* Delete this local variable. */ | |
3709505e | 1047 | else if (NILP (last)) |
39eb03f1 | 1048 | bset_local_var_alist (b, XCDR (tmp)); |
3709505e SM |
1049 | else |
1050 | XSETCDR (last, XCDR (tmp)); | |
1051 | } | |
1052 | ||
7313acd0 | 1053 | for (i = 0; i < last_per_buffer_idx; ++i) |
7c02e886 | 1054 | if (permanent_too || buffer_permanent_local_flags[i] == 0) |
7313acd0 | 1055 | SET_PER_BUFFER_VALUE_P (b, i, 0); |
1ab256cb | 1056 | |
36429c89 | 1057 | /* For each slot that has a default value, copy that into the slot. */ |
52b852c7 | 1058 | FOR_EACH_PER_BUFFER_OBJECT_AT (offset) |
aab80822 | 1059 | { |
7313acd0 | 1060 | int idx = PER_BUFFER_IDX (offset); |
7c02e886 GM |
1061 | if ((idx > 0 |
1062 | && (permanent_too | |
ce5b453a | 1063 | || buffer_permanent_local_flags[idx] == 0))) |
4ce60d2e | 1064 | set_per_buffer_value (b, offset, per_buffer_default (offset)); |
aab80822 | 1065 | } |
1ab256cb RM |
1066 | } |
1067 | ||
01050cb5 RM |
1068 | /* We split this away from generate-new-buffer, because rename-buffer |
1069 | and set-visited-file-name ought to be able to use this to really | |
1070 | rename the buffer properly. */ | |
1071 | ||
a7ca3326 | 1072 | DEFUN ("generate-new-buffer-name", Fgenerate_new_buffer_name, |
16a97296 | 1073 | Sgenerate_new_buffer_name, 1, 2, 0, |
7ee72033 | 1074 | doc: /* Return a string that is the name of no existing buffer based on NAME. |
018ba359 PJ |
1075 | If there is no live buffer named NAME, then return NAME. |
1076 | Otherwise modify name by appending `<NUMBER>', incrementing NUMBER | |
6b61353c | 1077 | \(starting at 2) until an unused name is found, and then return that name. |
2f064abf | 1078 | Optional second argument IGNORE specifies a name that is okay to use (if |
8e4fd1e1 GM |
1079 | it is in the sequence to be tried) even if a buffer with that name exists. |
1080 | ||
1081 | If NAME begins with a space (i.e., a buffer that is not normally | |
1082 | visible to users), then if buffer NAME already exists a random number | |
1083 | is first appended to NAME, to speed up finding a non-existent buffer. */) | |
5842a27b | 1084 | (register Lisp_Object name, Lisp_Object ignore) |
1ab256cb | 1085 | { |
8e4fd1e1 | 1086 | register Lisp_Object gentemp, tem, tem2; |
d311d28c PE |
1087 | ptrdiff_t count; |
1088 | char number[INT_BUFSIZE_BOUND (ptrdiff_t) + sizeof "<>"]; | |
1ab256cb | 1089 | |
b7826503 | 1090 | CHECK_STRING (name); |
1ab256cb | 1091 | |
6b61353c KH |
1092 | tem = Fstring_equal (name, ignore); |
1093 | if (!NILP (tem)) | |
1094 | return name; | |
1ab256cb | 1095 | tem = Fget_buffer (name); |
265a9e55 | 1096 | if (NILP (tem)) |
01050cb5 | 1097 | return name; |
1ab256cb | 1098 | |
8e4fd1e1 GM |
1099 | if (!strncmp (SSDATA (name), " ", 1)) /* see bug#1229 */ |
1100 | { | |
1101 | /* Note fileio.c:make_temp_name does random differently. */ | |
a8290ec3 | 1102 | tem2 = concat2 (name, make_formatted_string |
52b852c7 | 1103 | (number, "-%"pI"d", |
a8290ec3 | 1104 | XFASTINT (Frandom (make_number (999999))))); |
8e4fd1e1 GM |
1105 | tem = Fget_buffer (tem2); |
1106 | if (NILP (tem)) | |
1107 | return tem2; | |
1108 | } | |
1109 | else | |
1110 | tem2 = name; | |
1111 | ||
1ab256cb RM |
1112 | count = 1; |
1113 | while (1) | |
1114 | { | |
a8290ec3 DA |
1115 | gentemp = concat2 (tem2, make_formatted_string |
1116 | (number, "<%"pD"d>", ++count)); | |
638e4fc3 | 1117 | tem = Fstring_equal (gentemp, ignore); |
c273e647 RS |
1118 | if (!NILP (tem)) |
1119 | return gentemp; | |
1ab256cb | 1120 | tem = Fget_buffer (gentemp); |
265a9e55 | 1121 | if (NILP (tem)) |
01050cb5 | 1122 | return gentemp; |
1ab256cb RM |
1123 | } |
1124 | } | |
1125 | ||
1126 | \f | |
a7ca3326 | 1127 | DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0, |
7ee72033 | 1128 | doc: /* Return the name of BUFFER, as a string. |
35f5c1d2 JB |
1129 | BUFFER defaults to the current buffer. |
1130 | Return nil if BUFFER has been killed. */) | |
5842a27b | 1131 | (register Lisp_Object buffer) |
1ab256cb | 1132 | { |
265a9e55 | 1133 | if (NILP (buffer)) |
4b4deea2 | 1134 | return BVAR (current_buffer, name); |
b7826503 | 1135 | CHECK_BUFFER (buffer); |
4b4deea2 | 1136 | return BVAR (XBUFFER (buffer), name); |
1ab256cb RM |
1137 | } |
1138 | ||
a7ca3326 | 1139 | DEFUN ("buffer-file-name", Fbuffer_file_name, Sbuffer_file_name, 0, 1, 0, |
7ee72033 MB |
1140 | doc: /* Return name of file BUFFER is visiting, or nil if none. |
1141 | No argument or nil as argument means use the current buffer. */) | |
5842a27b | 1142 | (register Lisp_Object buffer) |
1ab256cb | 1143 | { |
265a9e55 | 1144 | if (NILP (buffer)) |
4b4deea2 | 1145 | return BVAR (current_buffer, filename); |
b7826503 | 1146 | CHECK_BUFFER (buffer); |
4b4deea2 | 1147 | return BVAR (XBUFFER (buffer), filename); |
1ab256cb RM |
1148 | } |
1149 | ||
336cd056 RS |
1150 | DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer, |
1151 | 0, 1, 0, | |
7ee72033 | 1152 | doc: /* Return the base buffer of indirect buffer BUFFER. |
5a72efd4 LT |
1153 | If BUFFER is not indirect, return nil. |
1154 | BUFFER defaults to the current buffer. */) | |
5842a27b | 1155 | (register Lisp_Object buffer) |
336cd056 RS |
1156 | { |
1157 | struct buffer *base; | |
1158 | Lisp_Object base_buffer; | |
1159 | ||
1160 | if (NILP (buffer)) | |
1161 | base = current_buffer->base_buffer; | |
1162 | else | |
1163 | { | |
b7826503 | 1164 | CHECK_BUFFER (buffer); |
336cd056 RS |
1165 | base = XBUFFER (buffer)->base_buffer; |
1166 | } | |
1167 | ||
1168 | if (! base) | |
1169 | return Qnil; | |
1170 | XSETBUFFER (base_buffer, base); | |
1171 | return base_buffer; | |
1172 | } | |
1173 | ||
a7ca3326 | 1174 | DEFUN ("buffer-local-value", Fbuffer_local_value, |
177c0ea7 | 1175 | Sbuffer_local_value, 2, 2, 0, |
79aa712d RS |
1176 | doc: /* Return the value of VARIABLE in BUFFER. |
1177 | If VARIABLE does not have a buffer-local binding in BUFFER, the value | |
5e2ad10b | 1178 | is the default binding of the variable. */) |
5842a27b | 1179 | (register Lisp_Object variable, register Lisp_Object buffer) |
5f2c76c6 CY |
1180 | { |
1181 | register Lisp_Object result = buffer_local_value_1 (variable, buffer); | |
1182 | ||
1183 | if (EQ (result, Qunbound)) | |
1184 | xsignal1 (Qvoid_variable, variable); | |
1185 | ||
1186 | return result; | |
1187 | } | |
1188 | ||
1189 | ||
1190 | /* Like Fbuffer_local_value, but return Qunbound if the variable is | |
1191 | locally unbound. */ | |
1192 | ||
1193 | Lisp_Object | |
1194 | buffer_local_value_1 (Lisp_Object variable, Lisp_Object buffer) | |
79aa712d RS |
1195 | { |
1196 | register struct buffer *buf; | |
1197 | register Lisp_Object result; | |
ad97b375 | 1198 | struct Lisp_Symbol *sym; |
79aa712d | 1199 | |
5e2ad10b | 1200 | CHECK_SYMBOL (variable); |
ae69175b | 1201 | CHECK_BUFFER (buffer); |
79aa712d | 1202 | buf = XBUFFER (buffer); |
ce5b453a | 1203 | sym = XSYMBOL (variable); |
79aa712d | 1204 | |
ce5b453a SM |
1205 | start: |
1206 | switch (sym->redirect) | |
f0bac7de | 1207 | { |
ce5b453a SM |
1208 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
1209 | case SYMBOL_PLAINVAL: result = SYMBOL_VAL (sym); break; | |
1210 | case SYMBOL_LOCALIZED: | |
1211 | { /* Look in local_var_alist. */ | |
1212 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym); | |
1213 | XSETSYMBOL (variable, sym); /* Update In case of aliasing. */ | |
4b4deea2 | 1214 | result = Fassoc (variable, BVAR (buf, local_var_alist)); |
ce5b453a SM |
1215 | if (!NILP (result)) |
1216 | { | |
1217 | if (blv->fwd) | |
1218 | { /* What binding is loaded right now? */ | |
1219 | Lisp_Object current_alist_element = blv->valcell; | |
f0bac7de | 1220 | |
ce5b453a SM |
1221 | /* The value of the currently loaded binding is not |
1222 | stored in it, but rather in the realvalue slot. | |
1223 | Store that value into the binding it belongs to | |
1224 | in case that is the one we are about to use. */ | |
f0bac7de | 1225 | |
ce5b453a SM |
1226 | XSETCDR (current_alist_element, |
1227 | do_symval_forwarding (blv->fwd)); | |
1228 | } | |
1229 | /* Now get the (perhaps updated) value out of the binding. */ | |
1230 | result = XCDR (result); | |
1231 | } | |
1232 | else | |
1233 | result = Fdefault_value (variable); | |
1234 | break; | |
1235 | } | |
1236 | case SYMBOL_FORWARDED: | |
1237 | { | |
1238 | union Lisp_Fwd *fwd = SYMBOL_FWD (sym); | |
1239 | if (BUFFER_OBJFWDP (fwd)) | |
4ce60d2e | 1240 | result = per_buffer_value (buf, XBUFFER_OBJFWD (fwd)->offset); |
ce5b453a SM |
1241 | else |
1242 | result = Fdefault_value (variable); | |
1243 | break; | |
1244 | } | |
1088b922 | 1245 | default: emacs_abort (); |
f0bac7de | 1246 | } |
79aa712d | 1247 | |
5f2c76c6 | 1248 | return result; |
79aa712d RS |
1249 | } |
1250 | ||
e1688f54 | 1251 | /* Return an alist of the Lisp-level buffer-local bindings of |
7fa57e45 | 1252 | buffer BUF. That is, don't include the variables maintained |
8a05d57a | 1253 | in special slots in the buffer object. |
37ef52bb | 1254 | If not CLONE, replace elements of the form (VAR . unbound) |
8a05d57a | 1255 | by VAR. */ |
e1688f54 RS |
1256 | |
1257 | static Lisp_Object | |
37ef52bb | 1258 | buffer_lisp_local_variables (struct buffer *buf, bool clone) |
e1688f54 RS |
1259 | { |
1260 | Lisp_Object result = Qnil; | |
37ef52bb | 1261 | Lisp_Object tail; |
4b4deea2 | 1262 | for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail)) |
e1688f54 RS |
1263 | { |
1264 | Lisp_Object val, elt; | |
1265 | ||
1266 | elt = XCAR (tail); | |
1267 | ||
1268 | /* Reference each variable in the alist in buf. | |
1269 | If inquiring about the current buffer, this gets the current values, | |
1270 | so store them into the alist so the alist is up to date. | |
1271 | If inquiring about some other buffer, this swaps out any values | |
1272 | for that buffer, making the alist up to date automatically. */ | |
1273 | val = find_symbol_value (XCAR (elt)); | |
1274 | /* Use the current buffer value only if buf is the current buffer. */ | |
1275 | if (buf != current_buffer) | |
1276 | val = XCDR (elt); | |
1277 | ||
8a05d57a | 1278 | result = Fcons (!clone && EQ (val, Qunbound) |
0992bd9c CY |
1279 | ? XCAR (elt) |
1280 | : Fcons (XCAR (elt), val), | |
1281 | result); | |
e1688f54 RS |
1282 | } |
1283 | ||
1284 | return result; | |
1285 | } | |
1286 | ||
1ab256cb | 1287 | DEFUN ("buffer-local-variables", Fbuffer_local_variables, |
efc7e75f | 1288 | Sbuffer_local_variables, 0, 1, 0, |
7ee72033 | 1289 | doc: /* Return an alist of variables that are buffer-local in BUFFER. |
018ba359 PJ |
1290 | Most elements look like (SYMBOL . VALUE), describing one variable. |
1291 | For a symbol that is locally unbound, just the symbol appears in the value. | |
1292 | Note that storing new VALUEs in these elements doesn't change the variables. | |
7ee72033 | 1293 | No argument or nil as argument means use current buffer as BUFFER. */) |
5842a27b | 1294 | (register Lisp_Object buffer) |
1ab256cb RM |
1295 | { |
1296 | register struct buffer *buf; | |
553defa4 | 1297 | register Lisp_Object result; |
1ab256cb | 1298 | |
265a9e55 | 1299 | if (NILP (buffer)) |
1ab256cb RM |
1300 | buf = current_buffer; |
1301 | else | |
1302 | { | |
b7826503 | 1303 | CHECK_BUFFER (buffer); |
1ab256cb RM |
1304 | buf = XBUFFER (buffer); |
1305 | } | |
1306 | ||
8a05d57a | 1307 | result = buffer_lisp_local_variables (buf, 0); |
1ab256cb | 1308 | |
1ab256cb RM |
1309 | /* Add on all the variables stored in special slots. */ |
1310 | { | |
7c02e886 | 1311 | int offset, idx; |
1ab256cb | 1312 | |
52b852c7 | 1313 | FOR_EACH_PER_BUFFER_OBJECT_AT (offset) |
1ab256cb | 1314 | { |
7313acd0 GM |
1315 | idx = PER_BUFFER_IDX (offset); |
1316 | if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx)) | |
1317 | && SYMBOLP (PER_BUFFER_SYMBOL (offset))) | |
0992bd9c CY |
1318 | { |
1319 | Lisp_Object sym = PER_BUFFER_SYMBOL (offset); | |
4ce60d2e | 1320 | Lisp_Object val = per_buffer_value (buf, offset); |
0992bd9c CY |
1321 | result = Fcons (EQ (val, Qunbound) ? sym : Fcons (sym, val), |
1322 | result); | |
1323 | } | |
1ab256cb RM |
1324 | } |
1325 | } | |
553defa4 RS |
1326 | |
1327 | return result; | |
1ab256cb | 1328 | } |
1ab256cb | 1329 | \f |
a7ca3326 | 1330 | DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p, |
efc7e75f | 1331 | 0, 1, 0, |
7ee72033 MB |
1332 | doc: /* Return t if BUFFER was modified since its file was last read or saved. |
1333 | No argument or nil as argument means use current buffer as BUFFER. */) | |
5842a27b | 1334 | (register Lisp_Object buffer) |
1ab256cb RM |
1335 | { |
1336 | register struct buffer *buf; | |
265a9e55 | 1337 | if (NILP (buffer)) |
1ab256cb RM |
1338 | buf = current_buffer; |
1339 | else | |
1340 | { | |
b7826503 | 1341 | CHECK_BUFFER (buffer); |
1ab256cb RM |
1342 | buf = XBUFFER (buffer); |
1343 | } | |
1344 | ||
336cd056 | 1345 | return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil; |
1ab256cb RM |
1346 | } |
1347 | ||
a7ca3326 | 1348 | DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p, |
efc7e75f | 1349 | 1, 1, 0, |
7ee72033 MB |
1350 | doc: /* Mark current buffer as modified or unmodified according to FLAG. |
1351 | A non-nil FLAG means mark the buffer modified. */) | |
37ef52bb | 1352 | (Lisp_Object flag) |
1ab256cb | 1353 | { |
37ef52bb | 1354 | Lisp_Object fn, buffer, window; |
1ab256cb RM |
1355 | |
1356 | #ifdef CLASH_DETECTION | |
1357 | /* If buffer becoming modified, lock the file. | |
1358 | If buffer becoming unmodified, unlock the file. */ | |
1359 | ||
4b4deea2 | 1360 | fn = BVAR (current_buffer, file_truename); |
90d456d2 | 1361 | /* Test buffer-file-name so that binding it to nil is effective. */ |
4b4deea2 | 1362 | if (!NILP (fn) && ! NILP (BVAR (current_buffer, filename))) |
1ab256cb | 1363 | { |
37ef52bb | 1364 | bool already = SAVE_MODIFF < MODIFF; |
265a9e55 | 1365 | if (!already && !NILP (flag)) |
1ab256cb | 1366 | lock_file (fn); |
265a9e55 | 1367 | else if (already && NILP (flag)) |
1ab256cb RM |
1368 | unlock_file (fn); |
1369 | } | |
1370 | #endif /* CLASH_DETECTION */ | |
1371 | ||
0b5397c2 SM |
1372 | /* Here we have a problem. SAVE_MODIFF is used here to encode |
1373 | buffer-modified-p (as SAVE_MODIFF<MODIFF) as well as | |
1374 | recent-auto-save-p (as SAVE_MODIFF<auto_save_modified). So if we | |
1375 | modify SAVE_MODIFF to affect one, we may affect the other | |
1376 | as well. | |
1377 | E.g. if FLAG is nil we need to set SAVE_MODIFF to MODIFF, but | |
1378 | if SAVE_MODIFF<auto_save_modified that means we risk changing | |
1379 | recent-auto-save-p from t to nil. | |
1380 | Vice versa, if FLAG is non-nil and SAVE_MODIFF>=auto_save_modified | |
1381 | we risk changing recent-auto-save-p from nil to t. */ | |
1382 | SAVE_MODIFF = (NILP (flag) | |
1383 | /* FIXME: This unavoidably sets recent-auto-save-p to nil. */ | |
1384 | ? MODIFF | |
1385 | /* Let's try to preserve recent-auto-save-p. */ | |
1386 | : SAVE_MODIFF < MODIFF ? SAVE_MODIFF | |
1387 | /* If SAVE_MODIFF == auto_save_modified == MODIFF, | |
1388 | we can either decrease SAVE_MODIFF and auto_save_modified | |
1389 | or increase MODIFF. */ | |
1390 | : MODIFF++); | |
177c0ea7 | 1391 | |
8ec01c70 GM |
1392 | /* Set update_mode_lines only if buffer is displayed in some window. |
1393 | Packages like jit-lock or lazy-lock preserve a buffer's modified | |
1394 | state by recording/restoring the state around blocks of code. | |
1395 | Setting update_mode_lines makes redisplay consider all windows | |
1396 | (on all frames). Stealth fontification of buffers not displayed | |
1397 | would incur additional redisplay costs if we'd set | |
1398 | update_modes_lines unconditionally. | |
1399 | ||
1400 | Ideally, I think there should be another mechanism for fontifying | |
1401 | buffers without "modifying" buffers, or redisplay should be | |
1402 | smarter about updating the `*' in mode lines. --gerd */ | |
1403 | XSETBUFFER (buffer, current_buffer); | |
1404 | window = Fget_buffer_window (buffer, Qt); | |
1405 | if (WINDOWP (window)) | |
d57b83b3 GM |
1406 | { |
1407 | ++update_mode_lines; | |
1408 | current_buffer->prevent_redisplay_optimizations_p = 1; | |
1409 | } | |
177c0ea7 | 1410 | |
1ab256cb RM |
1411 | return flag; |
1412 | } | |
1413 | ||
a8c21b48 GM |
1414 | DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p, |
1415 | Srestore_buffer_modified_p, 1, 1, 0, | |
42f47086 | 1416 | doc: /* Like `set-buffer-modified-p', with a difference concerning redisplay. |
018ba359 | 1417 | It is not ensured that mode lines will be updated to show the modified |
7ee72033 | 1418 | state of the current buffer. Use with care. */) |
5842a27b | 1419 | (Lisp_Object flag) |
a8c21b48 GM |
1420 | { |
1421 | #ifdef CLASH_DETECTION | |
1422 | Lisp_Object fn; | |
177c0ea7 | 1423 | |
a8c21b48 GM |
1424 | /* If buffer becoming modified, lock the file. |
1425 | If buffer becoming unmodified, unlock the file. */ | |
1426 | ||
4b4deea2 | 1427 | fn = BVAR (current_buffer, file_truename); |
a8c21b48 | 1428 | /* Test buffer-file-name so that binding it to nil is effective. */ |
4b4deea2 | 1429 | if (!NILP (fn) && ! NILP (BVAR (current_buffer, filename))) |
a8c21b48 | 1430 | { |
37ef52bb | 1431 | bool already = SAVE_MODIFF < MODIFF; |
a8c21b48 GM |
1432 | if (!already && !NILP (flag)) |
1433 | lock_file (fn); | |
1434 | else if (already && NILP (flag)) | |
1435 | unlock_file (fn); | |
1436 | } | |
1437 | #endif /* CLASH_DETECTION */ | |
177c0ea7 | 1438 | |
a8c21b48 GM |
1439 | SAVE_MODIFF = NILP (flag) ? MODIFF : 0; |
1440 | return flag; | |
1441 | } | |
1442 | ||
1ab256cb | 1443 | DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick, |
efc7e75f | 1444 | 0, 1, 0, |
7ee72033 | 1445 | doc: /* Return BUFFER's tick counter, incremented for each change in text. |
2f064abf JB |
1446 | Each buffer has a tick counter which is incremented each time the |
1447 | text in that buffer is changed. It wraps around occasionally. | |
7ee72033 | 1448 | No argument or nil as argument means use current buffer as BUFFER. */) |
5842a27b | 1449 | (register Lisp_Object buffer) |
1ab256cb RM |
1450 | { |
1451 | register struct buffer *buf; | |
265a9e55 | 1452 | if (NILP (buffer)) |
1ab256cb RM |
1453 | buf = current_buffer; |
1454 | else | |
1455 | { | |
b7826503 | 1456 | CHECK_BUFFER (buffer); |
1ab256cb RM |
1457 | buf = XBUFFER (buffer); |
1458 | } | |
1459 | ||
1460 | return make_number (BUF_MODIFF (buf)); | |
1461 | } | |
3e145152 CY |
1462 | |
1463 | DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick, | |
1464 | Sbuffer_chars_modified_tick, 0, 1, 0, | |
1465 | doc: /* Return BUFFER's character-change tick counter. | |
1466 | Each buffer has a character-change tick counter, which is set to the | |
1467 | value of the buffer's tick counter \(see `buffer-modified-tick'), each | |
1468 | time text in that buffer is inserted or deleted. By comparing the | |
12bd42be | 1469 | values returned by two individual calls of `buffer-chars-modified-tick', |
3e145152 CY |
1470 | you can tell whether a character change occurred in that buffer in |
1471 | between these calls. No argument or nil as argument means use current | |
1472 | buffer as BUFFER. */) | |
5842a27b | 1473 | (register Lisp_Object buffer) |
3e145152 CY |
1474 | { |
1475 | register struct buffer *buf; | |
1476 | if (NILP (buffer)) | |
1477 | buf = current_buffer; | |
1478 | else | |
1479 | { | |
1480 | CHECK_BUFFER (buffer); | |
1481 | buf = XBUFFER (buffer); | |
1482 | } | |
1483 | ||
1484 | return make_number (BUF_CHARS_MODIFF (buf)); | |
1485 | } | |
1ab256cb | 1486 | \f |
01050cb5 | 1487 | DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2, |
c7d97628 JL |
1488 | "(list (read-string \"Rename buffer (to new name): \" \ |
1489 | nil 'buffer-name-history (buffer-name (current-buffer))) \ | |
1490 | current-prefix-arg)", | |
7ee72033 | 1491 | doc: /* Change current buffer's name to NEWNAME (a string). |
018ba359 PJ |
1492 | If second arg UNIQUE is nil or omitted, it is an error if a |
1493 | buffer named NEWNAME already exists. | |
1494 | If UNIQUE is non-nil, come up with a new name using | |
1495 | `generate-new-buffer-name'. | |
1496 | Interactively, you can set UNIQUE with a prefix argument. | |
1497 | We return the name we actually gave the buffer. | |
7ee72033 | 1498 | This does not change the name of the visited file (if any). */) |
5842a27b | 1499 | (register Lisp_Object newname, Lisp_Object unique) |
1ab256cb RM |
1500 | { |
1501 | register Lisp_Object tem, buf; | |
1502 | ||
b7826503 | 1503 | CHECK_STRING (newname); |
d59698c4 | 1504 | |
d5db4077 | 1505 | if (SCHARS (newname) == 0) |
d59698c4 RS |
1506 | error ("Empty string is invalid as a buffer name"); |
1507 | ||
489c043a | 1508 | tem = Fget_buffer (newname); |
265a9e55 | 1509 | if (!NILP (tem)) |
01050cb5 | 1510 | { |
8801a864 KR |
1511 | /* Don't short-circuit if UNIQUE is t. That is a useful way to |
1512 | rename the buffer automatically so you can create another | |
1513 | with the original name. It makes UNIQUE equivalent to | |
1514 | (rename-buffer (generate-new-buffer-name NEWNAME)). */ | |
1515 | if (NILP (unique) && XBUFFER (tem) == current_buffer) | |
4b4deea2 | 1516 | return BVAR (current_buffer, name); |
3bd779aa | 1517 | if (!NILP (unique)) |
4b4deea2 | 1518 | newname = Fgenerate_new_buffer_name (newname, BVAR (current_buffer, name)); |
01050cb5 | 1519 | else |
d5db4077 | 1520 | error ("Buffer name `%s' is in use", SDATA (newname)); |
01050cb5 | 1521 | } |
1ab256cb | 1522 | |
39eb03f1 | 1523 | bset_name (current_buffer, newname); |
76f590d7 JB |
1524 | |
1525 | /* Catch redisplay's attention. Unless we do this, the mode lines for | |
1526 | any windows displaying current_buffer will stay unchanged. */ | |
1527 | update_mode_lines++; | |
1528 | ||
67180c6a | 1529 | XSETBUFFER (buf, current_buffer); |
489c043a | 1530 | Fsetcar (Frassq (buf, Vbuffer_alist), newname); |
4b4deea2 TT |
1531 | if (NILP (BVAR (current_buffer, filename)) |
1532 | && !NILP (BVAR (current_buffer, auto_save_file_name))) | |
1ab256cb | 1533 | call0 (intern ("rename-auto-save-file")); |
9397e56f MR |
1534 | |
1535 | /* Run buffer-list-update-hook. */ | |
1536 | if (!NILP (Vrun_hooks)) | |
1537 | call1 (Vrun_hooks, Qbuffer_list_update_hook); | |
1538 | ||
fb5eba9c | 1539 | /* Refetch since that last call may have done GC. */ |
4b4deea2 | 1540 | return BVAR (current_buffer, name); |
1ab256cb RM |
1541 | } |
1542 | ||
a7ca3326 | 1543 | DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0, |
7ee72033 | 1544 | doc: /* Return most recently selected buffer other than BUFFER. |
9397e56f MR |
1545 | Buffers not visible in windows are preferred to visible buffers, unless |
1546 | optional second argument VISIBLE-OK is non-nil. Ignore the argument | |
1547 | BUFFER unless it denotes a live buffer. If the optional third argument | |
1548 | FRAME is non-nil, use that frame's buffer list instead of the selected | |
1549 | frame's buffer list. | |
1550 | ||
1551 | The buffer is found by scanning the selected or specified frame's buffer | |
1552 | list first, followed by the list of all buffers. If no other buffer | |
1553 | exists, return the buffer `*scratch*' (creating it if necessary). */) | |
5842a27b | 1554 | (register Lisp_Object buffer, Lisp_Object visible_ok, Lisp_Object frame) |
1ab256cb | 1555 | { |
9397e56f MR |
1556 | Lisp_Object tail, buf, pred; |
1557 | Lisp_Object notsogood = Qnil; | |
1ab256cb | 1558 | |
773fbdb9 | 1559 | if (NILP (frame)) |
9ba9623d | 1560 | frame = selected_frame; |
773fbdb9 | 1561 | |
d234d13e JL |
1562 | CHECK_FRAME (frame); |
1563 | ||
773fbdb9 | 1564 | pred = frame_buffer_predicate (frame); |
9397e56f | 1565 | /* Consider buffers that have been seen in the frame first. */ |
e69b0960 | 1566 | tail = XFRAME (frame)->buffer_list; |
9397e56f | 1567 | for (; CONSP (tail); tail = XCDR (tail)) |
7962a441 | 1568 | { |
9397e56f MR |
1569 | buf = XCAR (tail); |
1570 | if (BUFFERP (buf) && !EQ (buf, buffer) | |
1571 | && !NILP (BVAR (XBUFFER (buf), name)) | |
1572 | && (SREF (BVAR (XBUFFER (buf), name), 0) != ' ') | |
1573 | /* If the frame has a buffer_predicate, disregard buffers that | |
1574 | don't fit the predicate. */ | |
1575 | && (NILP (pred) || !NILP (call1 (pred, buf)))) | |
1576 | { | |
1577 | if (!NILP (visible_ok) | |
1578 | || NILP (Fget_buffer_window (buf, Qvisible))) | |
1579 | return buf; | |
1580 | else if (NILP (notsogood)) | |
1581 | notsogood = buf; | |
1582 | } | |
7962a441 | 1583 | } |
7962a441 | 1584 | |
9397e56f MR |
1585 | /* Consider alist of all buffers next. */ |
1586 | tail = Vbuffer_alist; | |
6d70a280 | 1587 | for (; CONSP (tail); tail = XCDR (tail)) |
1ab256cb | 1588 | { |
6d70a280 | 1589 | buf = Fcdr (XCAR (tail)); |
9397e56f MR |
1590 | if (BUFFERP (buf) && !EQ (buf, buffer) |
1591 | && !NILP (BVAR (XBUFFER (buf), name)) | |
1592 | && (SREF (BVAR (XBUFFER (buf), name), 0) != ' ') | |
1593 | /* If the frame has a buffer_predicate, disregard buffers that | |
1594 | don't fit the predicate. */ | |
1595 | && (NILP (pred) || !NILP (call1 (pred, buf)))) | |
1596 | { | |
1597 | if (!NILP (visible_ok) | |
1598 | || NILP (Fget_buffer_window (buf, Qvisible))) | |
1599 | return buf; | |
1600 | else if (NILP (notsogood)) | |
1601 | notsogood = buf; | |
1602 | } | |
1603 | } | |
1604 | ||
1605 | if (!NILP (notsogood)) | |
1606 | return notsogood; | |
1607 | else | |
1608 | { | |
1609 | buf = Fget_buffer (build_string ("*scratch*")); | |
6b61353c | 1610 | if (NILP (buf)) |
04ae1b48 | 1611 | { |
9397e56f MR |
1612 | buf = Fget_buffer_create (build_string ("*scratch*")); |
1613 | Fset_buffer_major_mode (buf); | |
04ae1b48 | 1614 | } |
9397e56f MR |
1615 | return buf; |
1616 | } | |
1617 | } | |
04ae1b48 | 1618 | |
9397e56f MR |
1619 | /* The following function is a safe variant of Fother_buffer: It doesn't |
1620 | pay attention to any frame-local buffer lists, doesn't care about | |
1621 | visibility of buffers, and doesn't evaluate any frame predicates. */ | |
1622 | ||
1623 | Lisp_Object | |
1624 | other_buffer_safely (Lisp_Object buffer) | |
1625 | { | |
9397e56f MR |
1626 | Lisp_Object tail, buf; |
1627 | ||
1628 | tail = Vbuffer_alist; | |
1629 | for (; CONSP (tail); tail = XCDR (tail)) | |
1630 | { | |
1631 | buf = Fcdr (XCAR (tail)); | |
1632 | if (BUFFERP (buf) && !EQ (buf, buffer) | |
1633 | && !NILP (BVAR (XBUFFER (buf), name)) | |
1634 | && (SREF (BVAR (XBUFFER (buf), name), 0) != ' ')) | |
1ab256cb | 1635 | return buf; |
1ab256cb | 1636 | } |
9397e56f | 1637 | |
dba1a30a GM |
1638 | buf = Fget_buffer (build_string ("*scratch*")); |
1639 | if (NILP (buf)) | |
1640 | { | |
1641 | buf = Fget_buffer_create (build_string ("*scratch*")); | |
1642 | Fset_buffer_major_mode (buf); | |
1643 | } | |
9397e56f | 1644 | |
89132f25 | 1645 | return buf; |
1ab256cb RM |
1646 | } |
1647 | \f | |
a7ca3326 | 1648 | DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo, |
1ab256cb | 1649 | 0, 1, "", |
7ee72033 MB |
1650 | doc: /* Start keeping undo information for buffer BUFFER. |
1651 | No argument or nil as argument means do this for the current buffer. */) | |
5842a27b | 1652 | (register Lisp_Object buffer) |
1ab256cb | 1653 | { |
ffd56f97 | 1654 | Lisp_Object real_buffer; |
1ab256cb | 1655 | |
ffd56f97 | 1656 | if (NILP (buffer)) |
67180c6a | 1657 | XSETBUFFER (real_buffer, current_buffer); |
1ab256cb RM |
1658 | else |
1659 | { | |
ffd56f97 JB |
1660 | real_buffer = Fget_buffer (buffer); |
1661 | if (NILP (real_buffer)) | |
1662 | nsberror (buffer); | |
1ab256cb RM |
1663 | } |
1664 | ||
4b4deea2 | 1665 | if (EQ (BVAR (XBUFFER (real_buffer), undo_list), Qt)) |
39eb03f1 | 1666 | bset_undo_list (XBUFFER (real_buffer), Qnil); |
1ab256cb RM |
1667 | |
1668 | return Qnil; | |
1669 | } | |
1670 | ||
9cd47b72 DA |
1671 | /* Truncate undo list and shrink the gap of BUFFER. */ |
1672 | ||
37ef52bb | 1673 | void |
9cd47b72 DA |
1674 | compact_buffer (struct buffer *buffer) |
1675 | { | |
9928463d DA |
1676 | /* Verify indirection counters. */ |
1677 | if (buffer->base_buffer) | |
1678 | { | |
1679 | eassert (buffer->indirections == -1); | |
1680 | eassert (buffer->base_buffer->indirections > 0); | |
1681 | } | |
1682 | else | |
1683 | eassert (buffer->indirections >= 0); | |
1684 | ||
9cd47b72 DA |
1685 | /* Skip dead buffers, indirect buffers and buffers |
1686 | which aren't changed since last compaction. */ | |
e34f7f79 | 1687 | if (!NILP (buffer->INTERNAL_FIELD (name)) |
9cd47b72 DA |
1688 | && (buffer->base_buffer == NULL) |
1689 | && (buffer->text->compact != buffer->text->modiff)) | |
1690 | { | |
1691 | /* If a buffer's undo list is Qt, that means that undo is | |
1692 | turned off in that buffer. Calling truncate_undo_list on | |
1693 | Qt tends to return NULL, which effectively turns undo back on. | |
1694 | So don't call truncate_undo_list if undo_list is Qt. */ | |
e34f7f79 | 1695 | if (!EQ (buffer->INTERNAL_FIELD (undo_list), Qt)) |
9cd47b72 DA |
1696 | truncate_undo_list (buffer); |
1697 | ||
1698 | /* Shrink buffer gaps. */ | |
1699 | if (!buffer->text->inhibit_shrinking) | |
1700 | { | |
1701 | /* If a buffer's gap size is more than 10% of the buffer | |
1702 | size, or larger than 2000 bytes, then shrink it | |
1703 | accordingly. Keep a minimum size of 20 bytes. */ | |
1704 | int size = min (2000, max (20, (buffer->text->z_byte / 10))); | |
1705 | ||
1706 | if (buffer->text->gap_size > size) | |
1707 | { | |
1708 | struct buffer *save_current = current_buffer; | |
1709 | current_buffer = buffer; | |
1710 | make_gap (-(buffer->text->gap_size - size)); | |
1711 | current_buffer = save_current; | |
1712 | } | |
1713 | } | |
1714 | buffer->text->compact = buffer->text->modiff; | |
9cd47b72 | 1715 | } |
9cd47b72 DA |
1716 | } |
1717 | ||
a7ca3326 | 1718 | DEFUN ("kill-buffer", Fkill_buffer, Skill_buffer, 0, 1, "bKill buffer: ", |
b7e8d081 | 1719 | doc: /* Kill the buffer specified by BUFFER-OR-NAME. |
c8804c4f MR |
1720 | The argument may be a buffer or the name of an existing buffer. |
1721 | Argument nil or omitted means kill the current buffer. Return t if the | |
1722 | buffer is actually killed, nil otherwise. | |
1723 | ||
b7e8d081 MR |
1724 | The functions in `kill-buffer-query-functions' are called with the |
1725 | buffer to be killed as the current buffer. If any of them returns nil, | |
1726 | the buffer is not killed. The hook `kill-buffer-hook' is run before the | |
1727 | buffer is actually killed. The buffer being killed will be current | |
1728 | while the hook is running. Functions called by any of these hooks are | |
1729 | supposed to not change the current buffer. | |
018ba359 PJ |
1730 | |
1731 | Any processes that have this buffer as the `process-buffer' are killed | |
b7e8d081 MR |
1732 | with SIGHUP. This function calls `replace-buffer-in-windows' for |
1733 | cleaning up all windows currently displaying the buffer to be killed. */) | |
5842a27b | 1734 | (Lisp_Object buffer_or_name) |
1ab256cb | 1735 | { |
c8804c4f | 1736 | Lisp_Object buffer; |
1ab256cb RM |
1737 | register struct buffer *b; |
1738 | register Lisp_Object tem; | |
1739 | register struct Lisp_Marker *m; | |
6af718a4 | 1740 | struct gcpro gcpro1; |
1ab256cb | 1741 | |
c8804c4f MR |
1742 | if (NILP (buffer_or_name)) |
1743 | buffer = Fcurrent_buffer (); | |
1ab256cb | 1744 | else |
c8804c4f MR |
1745 | buffer = Fget_buffer (buffer_or_name); |
1746 | if (NILP (buffer)) | |
1747 | nsberror (buffer_or_name); | |
1ab256cb | 1748 | |
c8804c4f | 1749 | b = XBUFFER (buffer); |
1ab256cb | 1750 | |
4a4a9db5 | 1751 | /* Avoid trouble for buffer already dead. */ |
4b4deea2 | 1752 | if (NILP (BVAR (b, name))) |
4a4a9db5 KH |
1753 | return Qnil; |
1754 | ||
1ab256cb | 1755 | /* Query if the buffer is still modified. */ |
4b4deea2 | 1756 | if (INTERACTIVE && !NILP (BVAR (b, filename)) |
336cd056 | 1757 | && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)) |
1ab256cb | 1758 | { |
c8804c4f | 1759 | GCPRO1 (buffer); |
5c4930b0 | 1760 | tem = do_yes_or_no_p (format2 ("Buffer %s modified; kill anyway? ", |
4b4deea2 | 1761 | BVAR (b, name), make_number (0))); |
1ab256cb | 1762 | UNGCPRO; |
265a9e55 | 1763 | if (NILP (tem)) |
1ab256cb RM |
1764 | return Qnil; |
1765 | } | |
1766 | ||
dcdffbf6 | 1767 | /* Run hooks with the buffer to be killed the current buffer. */ |
1ab256cb | 1768 | { |
d311d28c | 1769 | ptrdiff_t count = SPECPDL_INDEX (); |
5b20caf0 | 1770 | Lisp_Object arglist[1]; |
1ab256cb RM |
1771 | |
1772 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
1773 | set_buffer_internal (b); | |
dcdffbf6 RS |
1774 | |
1775 | /* First run the query functions; if any query is answered no, | |
1776 | don't kill the buffer. */ | |
5b20caf0 | 1777 | arglist[0] = Qkill_buffer_query_functions; |
09706e1f KR |
1778 | tem = Frun_hook_with_args_until_failure (1, arglist); |
1779 | if (NILP (tem)) | |
5b20caf0 | 1780 | return unbind_to (count, Qnil); |
dcdffbf6 RS |
1781 | |
1782 | /* Then run the hooks. */ | |
f1597a3a | 1783 | Frun_hooks (1, &Qkill_buffer_hook); |
1ab256cb RM |
1784 | unbind_to (count, Qnil); |
1785 | } | |
1786 | ||
b7e8d081 MR |
1787 | /* If the hooks have killed the buffer, exit now. */ |
1788 | if (NILP (BVAR (b, name))) | |
1789 | return Qt; | |
1790 | ||
1ab256cb RM |
1791 | /* We have no more questions to ask. Verify that it is valid |
1792 | to kill the buffer. This must be done after the questions | |
1793 | since anything can happen within do_yes_or_no_p. */ | |
1794 | ||
1795 | /* Don't kill the minibuffer now current. */ | |
d3d50620 | 1796 | if (EQ (buffer, XWINDOW (minibuf_window)->buffer)) |
1ab256cb RM |
1797 | return Qnil; |
1798 | ||
9928463d DA |
1799 | /* When we kill an ordinary buffer which shares it's buffer text |
1800 | with indirect buffer(s), we must kill indirect buffer(s) too. | |
336cd056 RS |
1801 | We do it at this stage so nothing terrible happens if they |
1802 | ask questions or their hooks get errors. */ | |
9928463d | 1803 | if (!b->base_buffer && b->indirections > 0) |
336cd056 RS |
1804 | { |
1805 | struct buffer *other; | |
1806 | ||
c8804c4f | 1807 | GCPRO1 (buffer); |
336cd056 | 1808 | |
52b852c7 | 1809 | FOR_EACH_BUFFER (other) |
d17337e5 | 1810 | if (other->base_buffer == b) |
336cd056 | 1811 | { |
8f54f30a PE |
1812 | Lisp_Object buf; |
1813 | XSETBUFFER (buf, other); | |
1814 | Fkill_buffer (buf); | |
336cd056 RS |
1815 | } |
1816 | ||
1817 | UNGCPRO; | |
b7e8d081 MR |
1818 | |
1819 | /* Exit if we now have killed the base buffer (Bug#11665). */ | |
1820 | if (NILP (BVAR (b, name))) | |
1821 | return Qt; | |
336cd056 | 1822 | } |
177c0ea7 | 1823 | |
56e2e794 MR |
1824 | /* Run replace_buffer_in_windows before making another buffer current |
1825 | since set-window-buffer-start-and-point will refuse to make another | |
1826 | buffer current if the selected window does not show the current | |
1827 | buffer. (Bug#10114) */ | |
1828 | replace_buffer_in_windows (buffer); | |
1829 | ||
b7e8d081 MR |
1830 | /* Exit if replacing the buffer in windows has killed our buffer. */ |
1831 | if (NILP (BVAR (b, name))) | |
1832 | return Qt; | |
1833 | ||
1834 | /* Make this buffer not be current. Exit if it is the sole visible | |
1835 | buffer. */ | |
1ab256cb RM |
1836 | if (b == current_buffer) |
1837 | { | |
c8804c4f | 1838 | tem = Fother_buffer (buffer, Qnil, Qnil); |
1ab256cb RM |
1839 | Fset_buffer (tem); |
1840 | if (b == current_buffer) | |
1841 | return Qnil; | |
1842 | } | |
1843 | ||
b7e8d081 MR |
1844 | /* If the buffer now current is shown in the minibuffer and our buffer |
1845 | is the sole other buffer give up. */ | |
77270fac | 1846 | XSETBUFFER (tem, current_buffer); |
d3d50620 | 1847 | if (EQ (tem, XWINDOW (minibuf_window)->buffer) |
b7e8d081 MR |
1848 | && EQ (buffer, Fother_buffer (buffer, Qnil, Qnil))) |
1849 | return Qnil; | |
77270fac | 1850 | |
1ab256cb RM |
1851 | /* Now there is no question: we can kill the buffer. */ |
1852 | ||
1853 | #ifdef CLASH_DETECTION | |
1854 | /* Unlock this buffer's file, if it is locked. */ | |
1855 | unlock_buffer (b); | |
1856 | #endif /* CLASH_DETECTION */ | |
1857 | ||
c8804c4f MR |
1858 | GCPRO1 (buffer); |
1859 | kill_buffer_processes (buffer); | |
49da74e6 KS |
1860 | UNGCPRO; |
1861 | ||
b7e8d081 MR |
1862 | /* Killing buffer processes may run sentinels which may have killed |
1863 | our buffer. */ | |
4b4deea2 | 1864 | if (NILP (BVAR (b, name))) |
b7e8d081 | 1865 | return Qt; |
49da74e6 | 1866 | |
9397e56f MR |
1867 | /* These may run Lisp code and into infinite loops (if someone |
1868 | insisted on circular lists) so allow quitting here. */ | |
9397e56f MR |
1869 | frames_discard_buffer (buffer); |
1870 | ||
b93fb365 | 1871 | clear_charpos_cache (b); |
1ab256cb RM |
1872 | |
1873 | tem = Vinhibit_quit; | |
1874 | Vinhibit_quit = Qt; | |
9397e56f | 1875 | /* Remove the buffer from the list of all buffers. */ |
c8804c4f | 1876 | Vbuffer_alist = Fdelq (Frassq (buffer, Vbuffer_alist), Vbuffer_alist); |
b7e8d081 | 1877 | /* If replace_buffer_in_windows didn't do its job fix that now. */ |
9397e56f | 1878 | replace_buffer_in_windows_safely (buffer); |
1ab256cb RM |
1879 | Vinhibit_quit = tem; |
1880 | ||
9cf712eb RS |
1881 | /* Delete any auto-save file, if we saved it in this session. |
1882 | But not if the buffer is modified. */ | |
4b4deea2 | 1883 | if (STRINGP (BVAR (b, auto_save_file_name)) |
0b5397c2 SM |
1884 | && BUF_AUTOSAVE_MODIFF (b) != 0 |
1885 | && BUF_SAVE_MODIFF (b) < BUF_AUTOSAVE_MODIFF (b) | |
6b61353c KH |
1886 | && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b) |
1887 | && NILP (Fsymbol_value (intern ("auto-save-visited-file-name")))) | |
1ab256cb | 1888 | { |
8f54f30a PE |
1889 | Lisp_Object delete; |
1890 | delete = Fsymbol_value (intern ("delete-auto-save-files")); | |
1891 | if (! NILP (delete)) | |
4b4deea2 | 1892 | internal_delete_file (BVAR (b, auto_save_file_name)); |
1ab256cb RM |
1893 | } |
1894 | ||
b7e8d081 MR |
1895 | /* Deleting an auto-save file could have killed our buffer. */ |
1896 | if (NILP (BVAR (b, name))) | |
1897 | return Qt; | |
1898 | ||
4a4a9db5 KH |
1899 | if (b->base_buffer) |
1900 | { | |
1901 | /* Unchain all markers that belong to this indirect buffer. | |
1902 | Don't unchain the markers that belong to the base buffer | |
1903 | or its other indirect buffers. */ | |
65745fad | 1904 | for (m = BUF_MARKERS (b); m; ) |
4a4a9db5 | 1905 | { |
65745fad | 1906 | struct Lisp_Marker *next = m->next; |
4a4a9db5 | 1907 | if (m->buffer == b) |
65745fad SM |
1908 | unchain_marker (m); |
1909 | m = next; | |
4a4a9db5 KH |
1910 | } |
1911 | } | |
1912 | else | |
1ab256cb | 1913 | { |
4a4a9db5 | 1914 | /* Unchain all markers of this buffer and its indirect buffers. |
336cd056 | 1915 | and leave them pointing nowhere. */ |
65745fad | 1916 | for (m = BUF_MARKERS (b); m; ) |
336cd056 | 1917 | { |
65745fad | 1918 | struct Lisp_Marker *next = m->next; |
336cd056 | 1919 | m->buffer = 0; |
65745fad SM |
1920 | m->next = NULL; |
1921 | m = next; | |
336cd056 | 1922 | } |
65745fad | 1923 | BUF_MARKERS (b) = NULL; |
0c94c8d6 | 1924 | set_buffer_intervals (b, NULL); |
336cd056 RS |
1925 | |
1926 | /* Perhaps we should explicitly free the interval tree here... */ | |
1927 | } | |
33f7013e | 1928 | |
2f3f993b RS |
1929 | /* Reset the local variables, so that this buffer's local values |
1930 | won't be protected from GC. They would be protected | |
e4769531 | 1931 | if they happened to remain cached in their symbols. |
2f3f993b RS |
1932 | This gets rid of them for certain. */ |
1933 | swap_out_buffer_local_variables (b); | |
13de9290 | 1934 | reset_buffer_local_variables (b, 1); |
2f3f993b | 1935 | |
39eb03f1 | 1936 | bset_name (b, Qnil); |
336cd056 | 1937 | |
9ac0d9e0 | 1938 | BLOCK_INPUT; |
04e9897c DA |
1939 | if (b->base_buffer) |
1940 | { | |
1941 | /* Notify our base buffer that we don't share the text anymore. */ | |
1942 | eassert (b->indirections == -1); | |
1943 | b->base_buffer->indirections--; | |
1944 | eassert (b->base_buffer->indirections >= 0); | |
1945 | } | |
1946 | else | |
1947 | /* No one shares our buffer text, can free it. */ | |
b86af064 | 1948 | free_buffer_text (b); |
336cd056 | 1949 | |
28e969dd JB |
1950 | if (b->newline_cache) |
1951 | { | |
1952 | free_region_cache (b->newline_cache); | |
1953 | b->newline_cache = 0; | |
1954 | } | |
1955 | if (b->width_run_cache) | |
1956 | { | |
1957 | free_region_cache (b->width_run_cache); | |
1958 | b->width_run_cache = 0; | |
1959 | } | |
39eb03f1 | 1960 | bset_width_table (b, Qnil); |
9ac0d9e0 | 1961 | UNBLOCK_INPUT; |
39eb03f1 | 1962 | bset_undo_list (b, Qnil); |
1ab256cb | 1963 | |
9397e56f MR |
1964 | /* Run buffer-list-update-hook. */ |
1965 | if (!NILP (Vrun_hooks)) | |
1966 | call1 (Vrun_hooks, Qbuffer_list_update_hook); | |
1967 | ||
1ab256cb RM |
1968 | return Qt; |
1969 | } | |
1970 | \f | |
9397e56f MR |
1971 | /* Move association for BUFFER to the front of buffer (a)lists. Since |
1972 | we do this each time BUFFER is selected visibly, the more recently | |
1973 | selected buffers are always closer to the front of those lists. This | |
1974 | means that other_buffer is more likely to choose a relevant buffer. | |
1975 | ||
1976 | Note that this moves BUFFER to the front of the buffer lists of the | |
1977 | selected frame even if BUFFER is not shown there. If BUFFER is not | |
1978 | shown in the selected frame, consider the present behavior a feature. | |
1979 | `select-window' gets this right since it shows BUFFER in the selected | |
1980 | window when calling us. */ | |
1ab256cb | 1981 | |
01136e9b | 1982 | void |
9397e56f | 1983 | record_buffer (Lisp_Object buffer) |
1ab256cb | 1984 | { |
4475bec4 | 1985 | Lisp_Object aelt, aelt_cons, tem; |
9397e56f | 1986 | register struct frame *f = XFRAME (selected_frame); |
1ab256cb | 1987 | |
9397e56f | 1988 | CHECK_BUFFER (buffer); |
1ab256cb | 1989 | |
9397e56f MR |
1990 | /* Update Vbuffer_alist (we know that it has an entry for BUFFER). |
1991 | Don't allow quitting since this might leave the buffer list in an | |
1992 | inconsistent state. */ | |
1993 | tem = Vinhibit_quit; | |
1994 | Vinhibit_quit = Qt; | |
1995 | aelt = Frassq (buffer, Vbuffer_alist); | |
4475bec4 | 1996 | aelt_cons = Fmemq (aelt, Vbuffer_alist); |
9397e56f | 1997 | Vbuffer_alist = Fdelq (aelt, Vbuffer_alist); |
4475bec4 PE |
1998 | XSETCDR (aelt_cons, Vbuffer_alist); |
1999 | Vbuffer_alist = aelt_cons; | |
9397e56f | 2000 | Vinhibit_quit = tem; |
1ab256cb | 2001 | |
9397e56f | 2002 | /* Update buffer list of selected frame. */ |
f00af5b1 PE |
2003 | fset_buffer_list (f, Fcons (buffer, Fdelq (buffer, f->buffer_list))); |
2004 | fset_buried_buffer_list (f, Fdelq (buffer, f->buried_buffer_list)); | |
177c0ea7 | 2005 | |
9397e56f MR |
2006 | /* Run buffer-list-update-hook. */ |
2007 | if (!NILP (Vrun_hooks)) | |
2008 | call1 (Vrun_hooks, Qbuffer_list_update_hook); | |
2009 | } | |
7962a441 | 2010 | |
7962a441 | 2011 | |
e4ed06f1 CY |
2012 | /* Move BUFFER to the end of the buffer (a)lists. Do nothing if the |
2013 | buffer is killed. For the selected frame's buffer list this moves | |
2014 | BUFFER to its end even if it was never shown in that frame. If | |
96a72ee9 | 2015 | this happens we have a feature, hence `bury-buffer-internal' should be |
e4ed06f1 | 2016 | called only when BUFFER was shown in the selected frame. */ |
7962a441 | 2017 | |
e4ed06f1 CY |
2018 | DEFUN ("bury-buffer-internal", Fbury_buffer_internal, Sbury_buffer_internal, |
2019 | 1, 1, 0, | |
2020 | doc: /* Move BUFFER to the end of the buffer list. */) | |
9397e56f MR |
2021 | (Lisp_Object buffer) |
2022 | { | |
4475bec4 | 2023 | Lisp_Object aelt, aelt_cons, tem; |
9397e56f | 2024 | register struct frame *f = XFRAME (selected_frame); |
7962a441 | 2025 | |
9397e56f | 2026 | CHECK_BUFFER (buffer); |
177c0ea7 | 2027 | |
9397e56f MR |
2028 | /* Update Vbuffer_alist (we know that it has an entry for BUFFER). |
2029 | Don't allow quitting since this might leave the buffer list in an | |
2030 | inconsistent state. */ | |
2031 | tem = Vinhibit_quit; | |
2032 | Vinhibit_quit = Qt; | |
2033 | aelt = Frassq (buffer, Vbuffer_alist); | |
4475bec4 | 2034 | aelt_cons = Fmemq (aelt, Vbuffer_alist); |
9397e56f | 2035 | Vbuffer_alist = Fdelq (aelt, Vbuffer_alist); |
4475bec4 PE |
2036 | XSETCDR (aelt_cons, Qnil); |
2037 | Vbuffer_alist = nconc2 (Vbuffer_alist, aelt_cons); | |
9397e56f MR |
2038 | Vinhibit_quit = tem; |
2039 | ||
2040 | /* Update buffer lists of selected frame. */ | |
f00af5b1 PE |
2041 | fset_buffer_list (f, Fdelq (buffer, f->buffer_list)); |
2042 | fset_buried_buffer_list | |
2043 | (f, Fcons (buffer, Fdelq (buffer, f->buried_buffer_list))); | |
9397e56f MR |
2044 | |
2045 | /* Run buffer-list-update-hook. */ | |
2046 | if (!NILP (Vrun_hooks)) | |
2047 | call1 (Vrun_hooks, Qbuffer_list_update_hook); | |
2048 | ||
e4ed06f1 | 2049 | return Qnil; |
1ab256cb RM |
2050 | } |
2051 | ||
a9ee7a59 | 2052 | DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0, |
7ee72033 | 2053 | doc: /* Set an appropriate major mode for BUFFER. |
864b90c9 | 2054 | For the *scratch* buffer, use `initial-major-mode', otherwise choose a mode |
71a0f2c6 | 2055 | according to `default-major-mode'. |
018ba359 | 2056 | Use this function before selecting the buffer, since it may need to inspect |
7ee72033 | 2057 | the current buffer's major mode. */) |
5842a27b | 2058 | (Lisp_Object buffer) |
a9ee7a59 | 2059 | { |
d311d28c | 2060 | ptrdiff_t count; |
a9ee7a59 KH |
2061 | Lisp_Object function; |
2062 | ||
ea4fddd8 JB |
2063 | CHECK_BUFFER (buffer); |
2064 | ||
4f2daf31 DA |
2065 | if (NILP (BVAR (XBUFFER (buffer), name))) |
2066 | error ("Attempt to set major mode for a dead buffer"); | |
2067 | ||
2068 | if (strcmp (SSDATA (BVAR (XBUFFER (buffer), name)), "*scratch*") == 0) | |
71a0f2c6 GM |
2069 | function = find_symbol_value (intern ("initial-major-mode")); |
2070 | else | |
2071 | { | |
4b4deea2 | 2072 | function = BVAR (&buffer_defaults, major_mode); |
71a0f2c6 | 2073 | if (NILP (function) |
4b4deea2 TT |
2074 | && NILP (Fget (BVAR (current_buffer, major_mode), Qmode_class))) |
2075 | function = BVAR (current_buffer, major_mode); | |
71a0f2c6 | 2076 | } |
177c0ea7 | 2077 | |
48265e61 DL |
2078 | if (NILP (function) || EQ (function, Qfundamental_mode)) |
2079 | return Qnil; | |
2080 | ||
aed13378 | 2081 | count = SPECPDL_INDEX (); |
a9ee7a59 | 2082 | |
48265e61 DL |
2083 | /* To select a nonfundamental mode, |
2084 | select the buffer temporarily and then call the mode function. */ | |
a9ee7a59 KH |
2085 | |
2086 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
2087 | ||
a2428fa2 | 2088 | Fset_buffer (buffer); |
48265e61 | 2089 | call0 (function); |
a9ee7a59 KH |
2090 | |
2091 | return unbind_to (count, Qnil); | |
2092 | } | |
2093 | ||
a7ca3326 | 2094 | DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0, |
7ee72033 | 2095 | doc: /* Return the current buffer as a Lisp object. */) |
5842a27b | 2096 | (void) |
1ab256cb RM |
2097 | { |
2098 | register Lisp_Object buf; | |
67180c6a | 2099 | XSETBUFFER (buf, current_buffer); |
1ab256cb RM |
2100 | return buf; |
2101 | } | |
c7aa5005 KH |
2102 | |
2103 | /* Set the current buffer to B, and do not set windows_or_buffers_changed. | |
2104 | This is used by redisplay. */ | |
2105 | ||
2106 | void | |
d3da34e0 | 2107 | set_buffer_internal_1 (register struct buffer *b) |
c7aa5005 KH |
2108 | { |
2109 | register struct buffer *old_buf; | |
ce5b453a | 2110 | register Lisp_Object tail; |
c7aa5005 | 2111 | |
b86af064 | 2112 | #ifdef USE_MMAP_FOR_BUFFERS |
684b01ee | 2113 | if (b->text->beg == NULL) |
b86af064 GM |
2114 | enlarge_buffer_text (b, 0); |
2115 | #endif /* USE_MMAP_FOR_BUFFERS */ | |
177c0ea7 | 2116 | |
c7aa5005 KH |
2117 | if (current_buffer == b) |
2118 | return; | |
2119 | ||
1ab256cb RM |
2120 | old_buf = current_buffer; |
2121 | current_buffer = b; | |
2122 | last_known_column_point = -1; /* invalidate indentation cache */ | |
2123 | ||
336cd056 RS |
2124 | if (old_buf) |
2125 | { | |
2126 | /* Put the undo list back in the base buffer, so that it appears | |
2127 | that an indirect buffer shares the undo list of its base. */ | |
2128 | if (old_buf->base_buffer) | |
39eb03f1 | 2129 | bset_undo_list (old_buf->base_buffer, BVAR (old_buf, undo_list)); |
336cd056 RS |
2130 | |
2131 | /* If the old current buffer has markers to record PT, BEGV and ZV | |
2132 | when it is not current, update them now. */ | |
cffc6f3b | 2133 | record_buffer_markers (old_buf); |
336cd056 RS |
2134 | } |
2135 | ||
2136 | /* Get the undo list from the base buffer, so that it appears | |
2137 | that an indirect buffer shares the undo list of its base. */ | |
2138 | if (b->base_buffer) | |
39eb03f1 | 2139 | bset_undo_list (b, BVAR (b->base_buffer, undo_list)); |
336cd056 RS |
2140 | |
2141 | /* If the new current buffer has markers to record PT, BEGV and ZV | |
2142 | when it is not current, fetch them now. */ | |
cffc6f3b | 2143 | fetch_buffer_markers (b); |
336cd056 | 2144 | |
1ab256cb RM |
2145 | /* Look down buffer's list of local Lisp variables |
2146 | to find and update any that forward into C variables. */ | |
2147 | ||
ce5b453a | 2148 | do |
1ab256cb | 2149 | { |
4b4deea2 | 2150 | for (tail = BVAR (b, local_var_alist); CONSP (tail); tail = XCDR (tail)) |
ce5b453a SM |
2151 | { |
2152 | Lisp_Object var = XCAR (XCAR (tail)); | |
2153 | struct Lisp_Symbol *sym = XSYMBOL (var); | |
2154 | if (sym->redirect == SYMBOL_LOCALIZED /* Just to be sure. */ | |
2155 | && SYMBOL_BLV (sym)->fwd) | |
2156 | /* Just reference the variable | |
2157 | to cause it to become set for this buffer. */ | |
2158 | Fsymbol_value (var); | |
2159 | } | |
1ab256cb | 2160 | } |
1ab256cb | 2161 | /* Do the same with any others that were local to the previous buffer */ |
ce5b453a | 2162 | while (b != old_buf && (b = old_buf, b)); |
1ab256cb RM |
2163 | } |
2164 | ||
336cd056 | 2165 | /* Switch to buffer B temporarily for redisplay purposes. |
bbbe9545 | 2166 | This avoids certain things that don't need to be done within redisplay. */ |
336cd056 RS |
2167 | |
2168 | void | |
d3da34e0 | 2169 | set_buffer_temp (struct buffer *b) |
336cd056 RS |
2170 | { |
2171 | register struct buffer *old_buf; | |
2172 | ||
2173 | if (current_buffer == b) | |
2174 | return; | |
2175 | ||
2176 | old_buf = current_buffer; | |
2177 | current_buffer = b; | |
2178 | ||
cffc6f3b CY |
2179 | /* If the old current buffer has markers to record PT, BEGV and ZV |
2180 | when it is not current, update them now. */ | |
2181 | record_buffer_markers (old_buf); | |
336cd056 RS |
2182 | |
2183 | /* If the new current buffer has markers to record PT, BEGV and ZV | |
2184 | when it is not current, fetch them now. */ | |
cffc6f3b | 2185 | fetch_buffer_markers (b); |
336cd056 RS |
2186 | } |
2187 | ||
a7ca3326 | 2188 | DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0, |
c8804c4f MR |
2189 | doc: /* Make buffer BUFFER-OR-NAME current for editing operations. |
2190 | BUFFER-OR-NAME may be a buffer or the name of an existing buffer. See | |
2191 | also `save-excursion' when you want to make a buffer current | |
2192 | temporarily. This function does not display the buffer, so its effect | |
2193 | ends when the current command terminates. Use `switch-to-buffer' or | |
2194 | `pop-to-buffer' to switch buffers permanently. */) | |
5842a27b | 2195 | (register Lisp_Object buffer_or_name) |
1ab256cb | 2196 | { |
c8804c4f MR |
2197 | register Lisp_Object buffer; |
2198 | buffer = Fget_buffer (buffer_or_name); | |
2199 | if (NILP (buffer)) | |
2200 | nsberror (buffer_or_name); | |
4b4deea2 | 2201 | if (NILP (BVAR (XBUFFER (buffer), name))) |
1ab256cb | 2202 | error ("Selecting deleted buffer"); |
c8804c4f MR |
2203 | set_buffer_internal (XBUFFER (buffer)); |
2204 | return buffer; | |
1ab256cb | 2205 | } |
d0628b06 | 2206 | |
66322887 | 2207 | /* Set the current buffer to BUFFER provided if it is alive. */ |
d0628b06 RS |
2208 | |
2209 | Lisp_Object | |
d3da34e0 | 2210 | set_buffer_if_live (Lisp_Object buffer) |
d0628b06 | 2211 | { |
4b4deea2 | 2212 | if (! NILP (BVAR (XBUFFER (buffer), name))) |
a3d794a1 | 2213 | set_buffer_internal (XBUFFER (buffer)); |
d0628b06 RS |
2214 | return Qnil; |
2215 | } | |
1ab256cb | 2216 | \f |
a7ca3326 | 2217 | DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only, |
1ab256cb | 2218 | Sbarf_if_buffer_read_only, 0, 0, 0, |
7ee72033 | 2219 | doc: /* Signal a `buffer-read-only' error if the current buffer is read-only. */) |
5842a27b | 2220 | (void) |
1ab256cb | 2221 | { |
4b4deea2 | 2222 | if (!NILP (BVAR (current_buffer, read_only)) |
a96b68f1 | 2223 | && NILP (Vinhibit_read_only)) |
4c4dc0b0 | 2224 | xsignal1 (Qbuffer_read_only, Fcurrent_buffer ()); |
1ab256cb RM |
2225 | return Qnil; |
2226 | } | |
1ab256cb | 2227 | \f |
a7ca3326 | 2228 | DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*", |
7ee72033 | 2229 | doc: /* Delete the entire contents of the current buffer. |
018ba359 | 2230 | Any narrowing restriction in effect (see `narrow-to-region') is removed, |
7ee72033 | 2231 | so the buffer is truly empty after this. */) |
5842a27b | 2232 | (void) |
1ab256cb RM |
2233 | { |
2234 | Fwiden (); | |
c0d9a0c3 GM |
2235 | |
2236 | del_range (BEG, Z); | |
c280bc6a | 2237 | |
1ab256cb RM |
2238 | current_buffer->last_window_start = 1; |
2239 | /* Prevent warnings, or suspension of auto saving, that would happen | |
2240 | if future size is less than past size. Use of erase-buffer | |
2241 | implies that the future text is not really related to the past text. */ | |
4b4deea2 | 2242 | XSETFASTINT (BVAR (current_buffer, save_length), 0); |
1ab256cb RM |
2243 | return Qnil; |
2244 | } | |
2245 | ||
01136e9b | 2246 | void |
d3da34e0 | 2247 | validate_region (register Lisp_Object *b, register Lisp_Object *e) |
1ab256cb | 2248 | { |
b7826503 PJ |
2249 | CHECK_NUMBER_COERCE_MARKER (*b); |
2250 | CHECK_NUMBER_COERCE_MARKER (*e); | |
1ab256cb RM |
2251 | |
2252 | if (XINT (*b) > XINT (*e)) | |
2253 | { | |
03192067 KH |
2254 | Lisp_Object tem; |
2255 | tem = *b; *b = *e; *e = tem; | |
1ab256cb RM |
2256 | } |
2257 | ||
d311d28c | 2258 | if (! (BEGV <= XINT (*b) && XINT (*e) <= ZV)) |
1ab256cb RM |
2259 | args_out_of_range (*b, *e); |
2260 | } | |
2261 | \f | |
b05525fa RS |
2262 | /* Advance BYTE_POS up to a character boundary |
2263 | and return the adjusted position. */ | |
2264 | ||
d311d28c PE |
2265 | static ptrdiff_t |
2266 | advance_to_char_boundary (ptrdiff_t byte_pos) | |
b05525fa | 2267 | { |
f8449323 | 2268 | int c; |
b05525fa | 2269 | |
f8449323 RS |
2270 | if (byte_pos == BEG) |
2271 | /* Beginning of buffer is always a character boundary. */ | |
6d70a280 | 2272 | return BEG; |
f8449323 RS |
2273 | |
2274 | c = FETCH_BYTE (byte_pos); | |
2275 | if (! CHAR_HEAD_P (c)) | |
b05525fa | 2276 | { |
1be6387d | 2277 | /* We should advance BYTE_POS only when C is a constituent of a |
f8449323 | 2278 | multibyte sequence. */ |
d311d28c | 2279 | ptrdiff_t orig_byte_pos = byte_pos; |
a9bcded1 KH |
2280 | |
2281 | do | |
2282 | { | |
2283 | byte_pos--; | |
2284 | c = FETCH_BYTE (byte_pos); | |
2285 | } | |
2286 | while (! CHAR_HEAD_P (c) && byte_pos > BEG); | |
f8449323 | 2287 | INC_POS (byte_pos); |
a9bcded1 KH |
2288 | if (byte_pos < orig_byte_pos) |
2289 | byte_pos = orig_byte_pos; | |
f8449323 RS |
2290 | /* If C is a constituent of a multibyte sequence, BYTE_POS was |
2291 | surely advance to the correct character boundary. If C is | |
2292 | not, BYTE_POS was unchanged. */ | |
b05525fa RS |
2293 | } |
2294 | ||
20773569 | 2295 | return byte_pos; |
b05525fa RS |
2296 | } |
2297 | ||
13cda5f9 SM |
2298 | DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text, |
2299 | 1, 1, 0, | |
2300 | doc: /* Swap the text between current buffer and BUFFER. */) | |
5842a27b | 2301 | (Lisp_Object buffer) |
13cda5f9 SM |
2302 | { |
2303 | struct buffer *other_buffer; | |
2304 | CHECK_BUFFER (buffer); | |
2305 | other_buffer = XBUFFER (buffer); | |
2306 | ||
4b4deea2 | 2307 | if (NILP (BVAR (other_buffer, name))) |
409f2919 | 2308 | error ("Cannot swap a dead buffer's text"); |
dc9cc574 | 2309 | |
13cda5f9 SM |
2310 | /* Actually, it probably works just fine. |
2311 | * if (other_buffer == current_buffer) | |
2312 | * error ("Cannot swap a buffer's text with itself"); */ | |
2313 | ||
2314 | /* Actually, this may be workable as well, tho probably only if they're | |
2315 | *both* indirect. */ | |
2316 | if (other_buffer->base_buffer | |
2317 | || current_buffer->base_buffer) | |
2318 | error ("Cannot swap indirect buffers's text"); | |
2319 | ||
2320 | { /* This is probably harder to make work. */ | |
2321 | struct buffer *other; | |
52b852c7 | 2322 | FOR_EACH_BUFFER (other) |
13cda5f9 SM |
2323 | if (other->base_buffer == other_buffer |
2324 | || other->base_buffer == current_buffer) | |
2325 | error ("One of the buffers to swap has indirect buffers"); | |
2326 | } | |
2327 | ||
2328 | #define swapfield(field, type) \ | |
2329 | do { \ | |
2330 | type tmp##field = other_buffer->field; \ | |
2331 | other_buffer->field = current_buffer->field; \ | |
2332 | current_buffer->field = tmp##field; \ | |
2333 | } while (0) | |
5d8ea120 TT |
2334 | #define swapfield_(field, type) \ |
2335 | do { \ | |
4b4deea2 | 2336 | type tmp##field = BVAR (other_buffer, field); \ |
39eb03f1 PE |
2337 | bset_##field (other_buffer, BVAR (current_buffer, field)); \ |
2338 | bset_##field (current_buffer, tmp##field); \ | |
5d8ea120 | 2339 | } while (0) |
13cda5f9 SM |
2340 | |
2341 | swapfield (own_text, struct buffer_text); | |
2342 | eassert (current_buffer->text == ¤t_buffer->own_text); | |
2343 | eassert (other_buffer->text == &other_buffer->own_text); | |
baae5c2d | 2344 | #ifdef REL_ALLOC |
261cb4bb PE |
2345 | r_alloc_reset_variable ((void **) ¤t_buffer->own_text.beg, |
2346 | (void **) &other_buffer->own_text.beg); | |
2347 | r_alloc_reset_variable ((void **) &other_buffer->own_text.beg, | |
2348 | (void **) ¤t_buffer->own_text.beg); | |
baae5c2d JR |
2349 | #endif /* REL_ALLOC */ |
2350 | ||
d311d28c PE |
2351 | swapfield (pt, ptrdiff_t); |
2352 | swapfield (pt_byte, ptrdiff_t); | |
2353 | swapfield (begv, ptrdiff_t); | |
2354 | swapfield (begv_byte, ptrdiff_t); | |
2355 | swapfield (zv, ptrdiff_t); | |
2356 | swapfield (zv_byte, ptrdiff_t); | |
13cda5f9 SM |
2357 | eassert (!current_buffer->base_buffer); |
2358 | eassert (!other_buffer->base_buffer); | |
372f8ffc | 2359 | swapfield (indirections, ptrdiff_t); |
13cda5f9 SM |
2360 | current_buffer->clip_changed = 1; other_buffer->clip_changed = 1; |
2361 | swapfield (newline_cache, struct region_cache *); | |
2362 | swapfield (width_run_cache, struct region_cache *); | |
2363 | current_buffer->prevent_redisplay_optimizations_p = 1; | |
2364 | other_buffer->prevent_redisplay_optimizations_p = 1; | |
2365 | swapfield (overlays_before, struct Lisp_Overlay *); | |
2366 | swapfield (overlays_after, struct Lisp_Overlay *); | |
d311d28c | 2367 | swapfield (overlay_center, ptrdiff_t); |
5d8ea120 TT |
2368 | swapfield_ (undo_list, Lisp_Object); |
2369 | swapfield_ (mark, Lisp_Object); | |
2370 | swapfield_ (enable_multibyte_characters, Lisp_Object); | |
2371 | swapfield_ (bidi_display_reordering, Lisp_Object); | |
2372 | swapfield_ (bidi_paragraph_direction, Lisp_Object); | |
13cda5f9 SM |
2373 | /* FIXME: Not sure what we should do with these *_marker fields. |
2374 | Hopefully they're just nil anyway. */ | |
5d8ea120 TT |
2375 | swapfield_ (pt_marker, Lisp_Object); |
2376 | swapfield_ (begv_marker, Lisp_Object); | |
2377 | swapfield_ (zv_marker, Lisp_Object); | |
39eb03f1 PE |
2378 | bset_point_before_scroll (current_buffer, Qnil); |
2379 | bset_point_before_scroll (other_buffer, Qnil); | |
13cda5f9 SM |
2380 | |
2381 | current_buffer->text->modiff++; other_buffer->text->modiff++; | |
2382 | current_buffer->text->chars_modiff++; other_buffer->text->chars_modiff++; | |
2383 | current_buffer->text->overlay_modiff++; other_buffer->text->overlay_modiff++; | |
2384 | current_buffer->text->beg_unchanged = current_buffer->text->gpt; | |
2385 | current_buffer->text->end_unchanged = current_buffer->text->gpt; | |
b8ff72fa SM |
2386 | other_buffer->text->beg_unchanged = other_buffer->text->gpt; |
2387 | other_buffer->text->end_unchanged = other_buffer->text->gpt; | |
13cda5f9 SM |
2388 | { |
2389 | struct Lisp_Marker *m; | |
2390 | for (m = BUF_MARKERS (current_buffer); m; m = m->next) | |
2391 | if (m->buffer == other_buffer) | |
2392 | m->buffer = current_buffer; | |
b8ff72fa SM |
2393 | else |
2394 | /* Since there's no indirect buffer in sight, markers on | |
2395 | BUF_MARKERS(buf) should either be for `buf' or dead. */ | |
2396 | eassert (!m->buffer); | |
13cda5f9 SM |
2397 | for (m = BUF_MARKERS (other_buffer); m; m = m->next) |
2398 | if (m->buffer == current_buffer) | |
2399 | m->buffer = other_buffer; | |
b8ff72fa SM |
2400 | else |
2401 | /* Since there's no indirect buffer in sight, markers on | |
2402 | BUF_MARKERS(buf) should either be for `buf' or dead. */ | |
2403 | eassert (!m->buffer); | |
13cda5f9 | 2404 | } |
126f1fc1 SM |
2405 | { /* Some of the C code expects that w->buffer == w->pointm->buffer. |
2406 | So since we just swapped the markers between the two buffers, we need | |
2407 | to undo the effect of this swap for window markers. */ | |
2408 | Lisp_Object w = Fselected_window (), ws = Qnil; | |
2409 | Lisp_Object buf1, buf2; | |
2410 | XSETBUFFER (buf1, current_buffer); XSETBUFFER (buf2, other_buffer); | |
2411 | ||
2412 | while (NILP (Fmemq (w, ws))) | |
2413 | { | |
2414 | ws = Fcons (w, ws); | |
d3d50620 DA |
2415 | if (MARKERP (XWINDOW (w)->pointm) |
2416 | && (EQ (XWINDOW (w)->buffer, buf1) | |
2417 | || EQ (XWINDOW (w)->buffer, buf2))) | |
2418 | Fset_marker (XWINDOW (w)->pointm, | |
3a45383a | 2419 | make_number |
d3d50620 DA |
2420 | (BUF_BEGV (XBUFFER (XWINDOW (w)->buffer))), |
2421 | XWINDOW (w)->buffer); | |
126f1fc1 SM |
2422 | w = Fnext_window (w, Qt, Qt); |
2423 | } | |
2424 | } | |
2425 | ||
13cda5f9 SM |
2426 | if (current_buffer->text->intervals) |
2427 | (eassert (EQ (current_buffer->text->intervals->up.obj, buffer)), | |
2428 | XSETBUFFER (current_buffer->text->intervals->up.obj, current_buffer)); | |
2429 | if (other_buffer->text->intervals) | |
2430 | (eassert (EQ (other_buffer->text->intervals->up.obj, Fcurrent_buffer ())), | |
2431 | XSETBUFFER (other_buffer->text->intervals->up.obj, other_buffer)); | |
2432 | ||
2433 | return Qnil; | |
2434 | } | |
2435 | ||
a7ca3326 | 2436 | DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte, |
3ac81adb | 2437 | 1, 1, 0, |
7ee72033 | 2438 | doc: /* Set the multibyte flag of the current buffer to FLAG. |
018ba359 PJ |
2439 | If FLAG is t, this makes the buffer a multibyte buffer. |
2440 | If FLAG is nil, this makes the buffer a single-byte buffer. | |
8f924df7 KH |
2441 | In these cases, the buffer contents remain unchanged as a sequence of |
2442 | bytes but the contents viewed as characters do change. | |
2443 | If FLAG is `to', this makes the buffer a multibyte buffer by changing | |
6b61353c KH |
2444 | all eight-bit bytes to eight-bit characters. |
2445 | If the multibyte flag was really changed, undo information of the | |
2446 | current buffer is cleared. */) | |
5842a27b | 2447 | (Lisp_Object flag) |
3ac81adb | 2448 | { |
65745fad | 2449 | struct Lisp_Marker *tail, *markers; |
abc9d959 | 2450 | struct buffer *other; |
d311d28c | 2451 | ptrdiff_t begv, zv; |
37ef52bb PE |
2452 | bool narrowed = (BEG != BEGV || Z != ZV); |
2453 | bool modified_p = !NILP (Fbuffer_modified_p (Qnil)); | |
4b4deea2 | 2454 | Lisp_Object old_undo = BVAR (current_buffer, undo_list); |
38babc07 | 2455 | struct gcpro gcpro1; |
3ac81adb | 2456 | |
6e553d5e RS |
2457 | if (current_buffer->base_buffer) |
2458 | error ("Cannot do `set-buffer-multibyte' on an indirect buffer"); | |
2459 | ||
70e77119 | 2460 | /* Do nothing if nothing actually changes. */ |
4b4deea2 | 2461 | if (NILP (flag) == NILP (BVAR (current_buffer, enable_multibyte_characters))) |
70e77119 AS |
2462 | return flag; |
2463 | ||
38babc07 KS |
2464 | GCPRO1 (old_undo); |
2465 | ||
2466 | /* Don't record these buffer changes. We will put a special undo entry | |
2467 | instead. */ | |
39eb03f1 | 2468 | bset_undo_list (current_buffer, Qt); |
b05525fa | 2469 | |
3ac81adb RS |
2470 | /* If the cached position is for this buffer, clear it out. */ |
2471 | clear_charpos_cache (current_buffer); | |
2472 | ||
458c8af4 KH |
2473 | if (NILP (flag)) |
2474 | begv = BEGV_BYTE, zv = ZV_BYTE; | |
2475 | else | |
2476 | begv = BEGV, zv = ZV; | |
2477 | ||
a9bcded1 KH |
2478 | if (narrowed) |
2479 | Fwiden (); | |
2480 | ||
3ac81adb RS |
2481 | if (NILP (flag)) |
2482 | { | |
d311d28c | 2483 | ptrdiff_t pos, stop; |
a9bcded1 KH |
2484 | unsigned char *p; |
2485 | ||
3ac81adb RS |
2486 | /* Do this first, so it can use CHAR_TO_BYTE |
2487 | to calculate the old correspondences. */ | |
2488 | set_intervals_multibyte (0); | |
2489 | ||
39eb03f1 | 2490 | bset_enable_multibyte_characters (current_buffer, Qnil); |
3ac81adb RS |
2491 | |
2492 | Z = Z_BYTE; | |
2493 | BEGV = BEGV_BYTE; | |
2494 | ZV = ZV_BYTE; | |
2495 | GPT = GPT_BYTE; | |
2496 | TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE); | |
2497 | ||
60ebfdf3 | 2498 | |
65745fad SM |
2499 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) |
2500 | tail->charpos = tail->bytepos; | |
a9bcded1 KH |
2501 | |
2502 | /* Convert multibyte form of 8-bit characters to unibyte. */ | |
2503 | pos = BEG; | |
2504 | stop = GPT; | |
2505 | p = BEG_ADDR; | |
2506 | while (1) | |
2507 | { | |
2508 | int c, bytes; | |
2509 | ||
2510 | if (pos == stop) | |
2511 | { | |
2512 | if (pos == Z) | |
2513 | break; | |
2514 | p = GAP_END_ADDR; | |
2515 | stop = Z; | |
2516 | } | |
8f348ed5 KH |
2517 | if (ASCII_BYTE_P (*p)) |
2518 | p++, pos++; | |
2519 | else if (CHAR_BYTE8_HEAD_P (*p)) | |
a9bcded1 | 2520 | { |
62a6e103 | 2521 | c = STRING_CHAR_AND_LENGTH (p, bytes); |
a9bcded1 | 2522 | /* Delete all bytes for this 8-bit character but the |
3b59c351 | 2523 | last one, and change the last one to the character |
a9bcded1 KH |
2524 | code. */ |
2525 | bytes--; | |
2526 | del_range_2 (pos, pos, pos + bytes, pos + bytes, 0); | |
2527 | p = GAP_END_ADDR; | |
2528 | *p++ = c; | |
2529 | pos++; | |
2530 | if (begv > pos) | |
2531 | begv -= bytes; | |
2532 | if (zv > pos) | |
2533 | zv -= bytes; | |
2534 | stop = Z; | |
2535 | } | |
8f924df7 | 2536 | else |
8f348ed5 KH |
2537 | { |
2538 | bytes = BYTES_BY_CHAR_HEAD (*p); | |
2539 | p += bytes, pos += bytes; | |
2540 | } | |
a9bcded1 KH |
2541 | } |
2542 | if (narrowed) | |
2543 | Fnarrow_to_region (make_number (begv), make_number (zv)); | |
3ac81adb RS |
2544 | } |
2545 | else | |
2546 | { | |
d311d28c PE |
2547 | ptrdiff_t pt = PT; |
2548 | ptrdiff_t pos, stop; | |
8f348ed5 | 2549 | unsigned char *p, *pend; |
a9bcded1 | 2550 | |
673c57d2 | 2551 | /* Be sure not to have a multibyte sequence striding over the GAP. |
8f348ed5 KH |
2552 | Ex: We change this: "...abc\302 _GAP_ \241def..." |
2553 | to: "...abc _GAP_ \302\241def..." */ | |
673c57d2 | 2554 | |
8f924df7 | 2555 | if (EQ (flag, Qt) |
a3a303df | 2556 | && GPT_BYTE > 1 && GPT_BYTE < Z_BYTE |
673c57d2 KH |
2557 | && ! CHAR_HEAD_P (*(GAP_END_ADDR))) |
2558 | { | |
8f54f30a | 2559 | unsigned char *q = GPT_ADDR - 1; |
673c57d2 | 2560 | |
8f54f30a PE |
2561 | while (! CHAR_HEAD_P (*q) && q > BEG_ADDR) q--; |
2562 | if (LEADING_CODE_P (*q)) | |
673c57d2 | 2563 | { |
d311d28c | 2564 | ptrdiff_t new_gpt = GPT_BYTE - (GPT_ADDR - q); |
673c57d2 KH |
2565 | |
2566 | move_gap_both (new_gpt, new_gpt); | |
2567 | } | |
2568 | } | |
2569 | ||
a9bcded1 KH |
2570 | /* Make the buffer contents valid as multibyte by converting |
2571 | 8-bit characters to multibyte form. */ | |
2572 | pos = BEG; | |
2573 | stop = GPT; | |
2574 | p = BEG_ADDR; | |
8f348ed5 | 2575 | pend = GPT_ADDR; |
a9bcded1 KH |
2576 | while (1) |
2577 | { | |
2578 | int bytes; | |
2579 | ||
2580 | if (pos == stop) | |
2581 | { | |
2582 | if (pos == Z) | |
2583 | break; | |
2584 | p = GAP_END_ADDR; | |
8f348ed5 | 2585 | pend = Z_ADDR; |
a9bcded1 KH |
2586 | stop = Z; |
2587 | } | |
177c0ea7 | 2588 | |
a3a303df KH |
2589 | if (ASCII_BYTE_P (*p)) |
2590 | p++, pos++; | |
dcfb9bc4 KH |
2591 | else if (EQ (flag, Qt) |
2592 | && ! CHAR_BYTE8_HEAD_P (*p) | |
2593 | && (bytes = MULTIBYTE_LENGTH (p, pend)) > 0) | |
a9bcded1 KH |
2594 | p += bytes, pos += bytes; |
2595 | else | |
2596 | { | |
2597 | unsigned char tmp[MAX_MULTIBYTE_LENGTH]; | |
a3a303df | 2598 | int c; |
a9bcded1 | 2599 | |
8f924df7 | 2600 | c = BYTE8_TO_CHAR (*p); |
8c2fc311 | 2601 | bytes = CHAR_STRING (c, tmp); |
a9bcded1 KH |
2602 | *p = tmp[0]; |
2603 | TEMP_SET_PT_BOTH (pos + 1, pos + 1); | |
2604 | bytes--; | |
b68864e5 | 2605 | insert_1_both ((char *) tmp + 1, bytes, bytes, 1, 0, 0); |
a9bcded1 KH |
2606 | /* Now the gap is after the just inserted data. */ |
2607 | pos = GPT; | |
2608 | p = GAP_END_ADDR; | |
2609 | if (pos <= begv) | |
2610 | begv += bytes; | |
2611 | if (pos <= zv) | |
2612 | zv += bytes; | |
2613 | if (pos <= pt) | |
2614 | pt += bytes; | |
31285a8f | 2615 | pend = Z_ADDR; |
a9bcded1 KH |
2616 | stop = Z; |
2617 | } | |
2618 | } | |
2619 | ||
2620 | if (pt != PT) | |
2621 | TEMP_SET_PT (pt); | |
2622 | ||
2623 | if (narrowed) | |
2624 | Fnarrow_to_region (make_number (begv), make_number (zv)); | |
2625 | ||
3ac81adb RS |
2626 | /* Do this first, so that chars_in_text asks the right question. |
2627 | set_intervals_multibyte needs it too. */ | |
39eb03f1 | 2628 | bset_enable_multibyte_characters (current_buffer, Qt); |
3ac81adb | 2629 | |
b05525fa | 2630 | GPT_BYTE = advance_to_char_boundary (GPT_BYTE); |
3ac81adb | 2631 | GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG; |
b05525fa | 2632 | |
673c57d2 | 2633 | Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT; |
b05525fa RS |
2634 | |
2635 | BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE); | |
3ac81adb | 2636 | if (BEGV_BYTE > GPT_BYTE) |
673c57d2 | 2637 | BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT; |
3ac81adb RS |
2638 | else |
2639 | BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG; | |
b05525fa RS |
2640 | |
2641 | ZV_BYTE = advance_to_char_boundary (ZV_BYTE); | |
3ac81adb | 2642 | if (ZV_BYTE > GPT_BYTE) |
673c57d2 | 2643 | ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT; |
3ac81adb RS |
2644 | else |
2645 | ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG; | |
b05525fa RS |
2646 | |
2647 | { | |
d311d28c PE |
2648 | ptrdiff_t byte = advance_to_char_boundary (PT_BYTE); |
2649 | ptrdiff_t position; | |
b05525fa | 2650 | |
8f54f30a PE |
2651 | if (byte > GPT_BYTE) |
2652 | position = chars_in_text (GAP_END_ADDR, byte - GPT_BYTE) + GPT; | |
b05525fa | 2653 | else |
8f54f30a PE |
2654 | position = chars_in_text (BEG_ADDR, byte - BEG_BYTE) + BEG; |
2655 | TEMP_SET_PT_BOTH (position, byte); | |
b05525fa | 2656 | } |
3ac81adb RS |
2657 | |
2658 | tail = markers = BUF_MARKERS (current_buffer); | |
95fb069b RS |
2659 | |
2660 | /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from | |
2661 | getting confused by the markers that have not yet been updated. | |
2662 | It is also a signal that it should never create a marker. */ | |
65745fad | 2663 | BUF_MARKERS (current_buffer) = NULL; |
3ac81adb | 2664 | |
65745fad | 2665 | for (; tail; tail = tail->next) |
3ac81adb | 2666 | { |
65745fad SM |
2667 | tail->bytepos = advance_to_char_boundary (tail->bytepos); |
2668 | tail->charpos = BYTE_TO_CHAR (tail->bytepos); | |
3ac81adb | 2669 | } |
b69f9797 RS |
2670 | |
2671 | /* Make sure no markers were put on the chain | |
2672 | while the chain value was incorrect. */ | |
65745fad | 2673 | if (BUF_MARKERS (current_buffer)) |
1088b922 | 2674 | emacs_abort (); |
b69f9797 | 2675 | |
3ac81adb RS |
2676 | BUF_MARKERS (current_buffer) = markers; |
2677 | ||
2678 | /* Do this last, so it can calculate the new correspondences | |
2679 | between chars and bytes. */ | |
2680 | set_intervals_multibyte (1); | |
2681 | } | |
2682 | ||
38babc07 KS |
2683 | if (!EQ (old_undo, Qt)) |
2684 | { | |
2685 | /* Represent all the above changes by a special undo entry. */ | |
39eb03f1 PE |
2686 | bset_undo_list (current_buffer, |
2687 | Fcons (list3 (Qapply, | |
2688 | intern ("set-buffer-multibyte"), | |
2689 | NILP (flag) ? Qt : Qnil), | |
2690 | old_undo)); | |
38babc07 KS |
2691 | } |
2692 | ||
2693 | UNGCPRO; | |
a9bcded1 | 2694 | |
724b203f GM |
2695 | /* Changing the multibyteness of a buffer means that all windows |
2696 | showing that buffer must be updated thoroughly. */ | |
2697 | current_buffer->prevent_redisplay_optimizations_p = 1; | |
2698 | ++windows_or_buffers_changed; | |
2699 | ||
abc9d959 RS |
2700 | /* Copy this buffer's new multibyte status |
2701 | into all of its indirect buffers. */ | |
52b852c7 | 2702 | FOR_EACH_BUFFER (other) |
4b4deea2 | 2703 | if (other->base_buffer == current_buffer && !NILP (BVAR (other, name))) |
724b203f | 2704 | { |
4b4deea2 TT |
2705 | BVAR (other, enable_multibyte_characters) |
2706 | = BVAR (current_buffer, enable_multibyte_characters); | |
724b203f GM |
2707 | other->prevent_redisplay_optimizations_p = 1; |
2708 | } | |
abc9d959 | 2709 | |
ed00559d KH |
2710 | /* Restore the modifiedness of the buffer. */ |
2711 | if (!modified_p && !NILP (Fbuffer_modified_p (Qnil))) | |
2712 | Fset_buffer_modified_p (Qnil); | |
2713 | ||
172f9454 KH |
2714 | /* Update coding systems of this buffer's process (if any). */ |
2715 | { | |
2716 | Lisp_Object process; | |
2717 | ||
2718 | process = Fget_buffer_process (Fcurrent_buffer ()); | |
2719 | if (PROCESSP (process)) | |
2720 | setup_process_coding_systems (process); | |
2721 | } | |
2722 | ||
3ac81adb RS |
2723 | return flag; |
2724 | } | |
2725 | \f | |
a7ca3326 | 2726 | DEFUN ("kill-all-local-variables", Fkill_all_local_variables, |
16a97296 | 2727 | Skill_all_local_variables, 0, 0, 0, |
7ee72033 | 2728 | doc: /* Switch to Fundamental mode by killing current buffer's local variables. |
018ba359 PJ |
2729 | Most local variable bindings are eliminated so that the default values |
2730 | become effective once more. Also, the syntax table is set from | |
2731 | `standard-syntax-table', the local keymap is set to nil, | |
2732 | and the abbrev table from `fundamental-mode-abbrev-table'. | |
2733 | This function also forces redisplay of the mode line. | |
2734 | ||
2735 | Every function to select a new major mode starts by | |
2736 | calling this function. | |
2737 | ||
2738 | As a special exception, local variables whose names have | |
2739 | a non-nil `permanent-local' property are not eliminated by this function. | |
2740 | ||
2741 | The first thing this function does is run | |
7ee72033 | 2742 | the normal hook `change-major-mode-hook'. */) |
5842a27b | 2743 | (void) |
1ab256cb | 2744 | { |
dee091a3 | 2745 | Frun_hooks (1, &Qchange_major_mode_hook); |
1ab256cb | 2746 | |
3709505e | 2747 | /* Make sure none of the bindings in local_var_alist |
2f3f993b | 2748 | remain swapped in, in their symbols. */ |
1ab256cb | 2749 | |
2f3f993b | 2750 | swap_out_buffer_local_variables (current_buffer); |
1ab256cb RM |
2751 | |
2752 | /* Actually eliminate all local bindings of this buffer. */ | |
2753 | ||
13de9290 | 2754 | reset_buffer_local_variables (current_buffer, 0); |
1ab256cb | 2755 | |
1ab256cb RM |
2756 | /* Force mode-line redisplay. Useful here because all major mode |
2757 | commands call this function. */ | |
2758 | update_mode_lines++; | |
2759 | ||
2760 | return Qnil; | |
2761 | } | |
2f3f993b RS |
2762 | |
2763 | /* Make sure no local variables remain set up with buffer B | |
2764 | for their current values. */ | |
2765 | ||
2766 | static void | |
d3da34e0 | 2767 | swap_out_buffer_local_variables (struct buffer *b) |
2f3f993b | 2768 | { |
ce5b453a | 2769 | Lisp_Object oalist, alist, buffer; |
2f3f993b RS |
2770 | |
2771 | XSETBUFFER (buffer, b); | |
4b4deea2 | 2772 | oalist = BVAR (b, local_var_alist); |
2f3f993b | 2773 | |
67ee9f6e | 2774 | for (alist = oalist; CONSP (alist); alist = XCDR (alist)) |
2f3f993b | 2775 | { |
ce5b453a SM |
2776 | Lisp_Object sym = XCAR (XCAR (alist)); |
2777 | eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED); | |
2778 | /* Need not do anything if some other buffer's binding is | |
e4769531 | 2779 | now cached. */ |
ce5b453a | 2780 | if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer)) |
2f3f993b | 2781 | { |
e7c10f83 SM |
2782 | /* Symbol is set up for this buffer's old local value: |
2783 | swap it out! */ | |
ce5b453a | 2784 | swap_in_global_binding (XSYMBOL (sym)); |
2f3f993b RS |
2785 | } |
2786 | } | |
2787 | } | |
1ab256cb | 2788 | \f |
2eec3b4e | 2789 | /* Find all the overlays in the current buffer that contain position POS. |
177c0ea7 | 2790 | Return the number found, and store them in a vector in *VEC_PTR. |
2eec3b4e | 2791 | Store in *LEN_PTR the size allocated for the vector. |
52f8ec73 | 2792 | Store in *NEXT_PTR the next position after POS where an overlay starts, |
624d2678 | 2793 | or ZV if there are no more overlays between POS and ZV. |
bbbe9545 | 2794 | Store in *PREV_PTR the previous position before POS where an overlay ends, |
413e06a4 | 2795 | or where an overlay starts which ends at or after POS; |
624d2678 | 2796 | or BEGV if there are no such overlays from BEGV to POS. |
239c932b | 2797 | NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info. |
2eec3b4e RS |
2798 | |
2799 | *VEC_PTR and *LEN_PTR should contain a valid vector and size | |
61d54cd5 RS |
2800 | when this function is called. |
2801 | ||
37ef52bb PE |
2802 | If EXTEND, make the vector bigger if necessary. |
2803 | If not, never extend the vector, | |
2804 | and store only as many overlays as will fit. | |
2805 | But still return the total number of overlays. | |
ac869cf7 | 2806 | |
37ef52bb | 2807 | If CHANGE_REQ, any position written into *PREV_PTR or |
ac869cf7 MB |
2808 | *NEXT_PTR is guaranteed to be not equal to POS, unless it is the |
2809 | default (BEGV or ZV). */ | |
2eec3b4e | 2810 | |
b081724f | 2811 | ptrdiff_t |
37ef52bb | 2812 | overlays_at (EMACS_INT pos, bool extend, Lisp_Object **vec_ptr, |
b081724f | 2813 | ptrdiff_t *len_ptr, |
37ef52bb | 2814 | ptrdiff_t *next_ptr, ptrdiff_t *prev_ptr, bool change_req) |
1ab256cb | 2815 | { |
2410d73a SM |
2816 | Lisp_Object overlay, start, end; |
2817 | struct Lisp_Overlay *tail; | |
b081724f PE |
2818 | ptrdiff_t idx = 0; |
2819 | ptrdiff_t len = *len_ptr; | |
2eec3b4e | 2820 | Lisp_Object *vec = *vec_ptr; |
d311d28c PE |
2821 | ptrdiff_t next = ZV; |
2822 | ptrdiff_t prev = BEGV; | |
37ef52bb | 2823 | bool inhibit_storing = 0; |
61d54cd5 | 2824 | |
fd318b54 | 2825 | for (tail = current_buffer->overlays_before; tail; tail = tail->next) |
2eec3b4e | 2826 | { |
d311d28c | 2827 | ptrdiff_t startpos, endpos; |
52f8ec73 | 2828 | |
2410d73a | 2829 | XSETMISC (overlay, tail); |
1ab256cb | 2830 | |
2eec3b4e RS |
2831 | start = OVERLAY_START (overlay); |
2832 | end = OVERLAY_END (overlay); | |
239c932b RS |
2833 | endpos = OVERLAY_POSITION (end); |
2834 | if (endpos < pos) | |
2835 | { | |
2836 | if (prev < endpos) | |
2837 | prev = endpos; | |
2838 | break; | |
2839 | } | |
413e06a4 RS |
2840 | startpos = OVERLAY_POSITION (start); |
2841 | /* This one ends at or after POS | |
daa1c109 | 2842 | so its start counts for PREV_PTR if it's before POS. */ |
413e06a4 RS |
2843 | if (prev < startpos && startpos < pos) |
2844 | prev = startpos; | |
239c932b RS |
2845 | if (endpos == pos) |
2846 | continue; | |
2eec3b4e RS |
2847 | if (startpos <= pos) |
2848 | { | |
2849 | if (idx == len) | |
2850 | { | |
61d54cd5 RS |
2851 | /* The supplied vector is full. |
2852 | Either make it bigger, or don't store any more in it. */ | |
2853 | if (extend) | |
2854 | { | |
0065d054 PE |
2855 | vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX, |
2856 | sizeof *vec); | |
61d54cd5 | 2857 | *vec_ptr = vec; |
0065d054 | 2858 | len = *len_ptr; |
61d54cd5 RS |
2859 | } |
2860 | else | |
2861 | inhibit_storing = 1; | |
2eec3b4e | 2862 | } |
61d54cd5 RS |
2863 | |
2864 | if (!inhibit_storing) | |
2865 | vec[idx] = overlay; | |
2866 | /* Keep counting overlays even if we can't return them all. */ | |
2867 | idx++; | |
2eec3b4e RS |
2868 | } |
2869 | else if (startpos < next) | |
2870 | next = startpos; | |
2871 | } | |
2872 | ||
fd318b54 | 2873 | for (tail = current_buffer->overlays_after; tail; tail = tail->next) |
1ab256cb | 2874 | { |
d311d28c | 2875 | ptrdiff_t startpos, endpos; |
52f8ec73 | 2876 | |
2410d73a | 2877 | XSETMISC (overlay, tail); |
2eec3b4e RS |
2878 | |
2879 | start = OVERLAY_START (overlay); | |
2880 | end = OVERLAY_END (overlay); | |
2881 | startpos = OVERLAY_POSITION (start); | |
52f8ec73 | 2882 | if (pos < startpos) |
2eec3b4e RS |
2883 | { |
2884 | if (startpos < next) | |
2885 | next = startpos; | |
2886 | break; | |
2887 | } | |
239c932b RS |
2888 | endpos = OVERLAY_POSITION (end); |
2889 | if (pos < endpos) | |
2eec3b4e RS |
2890 | { |
2891 | if (idx == len) | |
2892 | { | |
61d54cd5 RS |
2893 | if (extend) |
2894 | { | |
0065d054 PE |
2895 | vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX, |
2896 | sizeof *vec); | |
61d54cd5 | 2897 | *vec_ptr = vec; |
0065d054 | 2898 | len = *len_ptr; |
61d54cd5 RS |
2899 | } |
2900 | else | |
2901 | inhibit_storing = 1; | |
2eec3b4e | 2902 | } |
61d54cd5 RS |
2903 | |
2904 | if (!inhibit_storing) | |
2905 | vec[idx] = overlay; | |
2906 | idx++; | |
413e06a4 RS |
2907 | |
2908 | if (startpos < pos && startpos > prev) | |
2909 | prev = startpos; | |
2eec3b4e | 2910 | } |
239c932b RS |
2911 | else if (endpos < pos && endpos > prev) |
2912 | prev = endpos; | |
1d5f4c1d MB |
2913 | else if (endpos == pos && startpos > prev |
2914 | && (!change_req || startpos < pos)) | |
413e06a4 | 2915 | prev = startpos; |
1ab256cb RM |
2916 | } |
2917 | ||
239c932b RS |
2918 | if (next_ptr) |
2919 | *next_ptr = next; | |
2920 | if (prev_ptr) | |
2921 | *prev_ptr = prev; | |
2eec3b4e RS |
2922 | return idx; |
2923 | } | |
74514898 | 2924 | \f |
7723e095 MR |
2925 | /* Find all the overlays in the current buffer that overlap the range |
2926 | BEG-END, or are empty at BEG, or are empty at END provided END | |
2927 | denotes the position at the end of the current buffer. | |
2a3eeee7 | 2928 | |
177c0ea7 | 2929 | Return the number found, and store them in a vector in *VEC_PTR. |
74514898 RS |
2930 | Store in *LEN_PTR the size allocated for the vector. |
2931 | Store in *NEXT_PTR the next position after POS where an overlay starts, | |
2932 | or ZV if there are no more overlays. | |
2933 | Store in *PREV_PTR the previous position before POS where an overlay ends, | |
2934 | or BEGV if there are no previous overlays. | |
2935 | NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info. | |
2936 | ||
2937 | *VEC_PTR and *LEN_PTR should contain a valid vector and size | |
2938 | when this function is called. | |
2939 | ||
37ef52bb PE |
2940 | If EXTEND, make the vector bigger if necessary. |
2941 | If not, never extend the vector, | |
2942 | and store only as many overlays as will fit. | |
2943 | But still return the total number of overlays. */ | |
74514898 | 2944 | |
21514da7 | 2945 | static ptrdiff_t |
37ef52bb | 2946 | overlays_in (EMACS_INT beg, EMACS_INT end, bool extend, |
21514da7 | 2947 | Lisp_Object **vec_ptr, ptrdiff_t *len_ptr, |
d311d28c | 2948 | ptrdiff_t *next_ptr, ptrdiff_t *prev_ptr) |
74514898 | 2949 | { |
2410d73a SM |
2950 | Lisp_Object overlay, ostart, oend; |
2951 | struct Lisp_Overlay *tail; | |
21514da7 PE |
2952 | ptrdiff_t idx = 0; |
2953 | ptrdiff_t len = *len_ptr; | |
74514898 | 2954 | Lisp_Object *vec = *vec_ptr; |
d311d28c PE |
2955 | ptrdiff_t next = ZV; |
2956 | ptrdiff_t prev = BEGV; | |
37ef52bb PE |
2957 | bool inhibit_storing = 0; |
2958 | bool end_is_Z = end == Z; | |
74514898 | 2959 | |
fd318b54 | 2960 | for (tail = current_buffer->overlays_before; tail; tail = tail->next) |
74514898 | 2961 | { |
d311d28c | 2962 | ptrdiff_t startpos, endpos; |
74514898 | 2963 | |
2410d73a | 2964 | XSETMISC (overlay, tail); |
74514898 RS |
2965 | |
2966 | ostart = OVERLAY_START (overlay); | |
2967 | oend = OVERLAY_END (overlay); | |
2968 | endpos = OVERLAY_POSITION (oend); | |
2969 | if (endpos < beg) | |
2970 | { | |
2971 | if (prev < endpos) | |
2972 | prev = endpos; | |
2973 | break; | |
2974 | } | |
2975 | startpos = OVERLAY_POSITION (ostart); | |
7723e095 MR |
2976 | /* Count an interval if it overlaps the range, is empty at the |
2977 | start of the range, or is empty at END provided END denotes the | |
2978 | end of the buffer. */ | |
74514898 | 2979 | if ((beg < endpos && startpos < end) |
7723e095 MR |
2980 | || (startpos == endpos |
2981 | && (beg == endpos || (end_is_Z && endpos == end)))) | |
74514898 RS |
2982 | { |
2983 | if (idx == len) | |
2984 | { | |
2985 | /* The supplied vector is full. | |
2986 | Either make it bigger, or don't store any more in it. */ | |
2987 | if (extend) | |
2988 | { | |
0065d054 PE |
2989 | vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX, |
2990 | sizeof *vec); | |
74514898 | 2991 | *vec_ptr = vec; |
0065d054 | 2992 | len = *len_ptr; |
74514898 RS |
2993 | } |
2994 | else | |
2995 | inhibit_storing = 1; | |
2996 | } | |
2997 | ||
2998 | if (!inhibit_storing) | |
2999 | vec[idx] = overlay; | |
3000 | /* Keep counting overlays even if we can't return them all. */ | |
3001 | idx++; | |
3002 | } | |
3003 | else if (startpos < next) | |
3004 | next = startpos; | |
3005 | } | |
3006 | ||
fd318b54 | 3007 | for (tail = current_buffer->overlays_after; tail; tail = tail->next) |
74514898 | 3008 | { |
d311d28c | 3009 | ptrdiff_t startpos, endpos; |
74514898 | 3010 | |
2410d73a | 3011 | XSETMISC (overlay, tail); |
74514898 RS |
3012 | |
3013 | ostart = OVERLAY_START (overlay); | |
3014 | oend = OVERLAY_END (overlay); | |
3015 | startpos = OVERLAY_POSITION (ostart); | |
3016 | if (end < startpos) | |
3017 | { | |
3018 | if (startpos < next) | |
3019 | next = startpos; | |
3020 | break; | |
3021 | } | |
3022 | endpos = OVERLAY_POSITION (oend); | |
7723e095 MR |
3023 | /* Count an interval if it overlaps the range, is empty at the |
3024 | start of the range, or is empty at END provided END denotes the | |
3025 | end of the buffer. */ | |
74514898 | 3026 | if ((beg < endpos && startpos < end) |
7723e095 MR |
3027 | || (startpos == endpos |
3028 | && (beg == endpos || (end_is_Z && endpos == end)))) | |
74514898 RS |
3029 | { |
3030 | if (idx == len) | |
3031 | { | |
3032 | if (extend) | |
3033 | { | |
0065d054 PE |
3034 | vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX, |
3035 | sizeof *vec); | |
74514898 | 3036 | *vec_ptr = vec; |
0065d054 | 3037 | len = *len_ptr; |
74514898 RS |
3038 | } |
3039 | else | |
3040 | inhibit_storing = 1; | |
3041 | } | |
3042 | ||
3043 | if (!inhibit_storing) | |
3044 | vec[idx] = overlay; | |
3045 | idx++; | |
3046 | } | |
3047 | else if (endpos < beg && endpos > prev) | |
3048 | prev = endpos; | |
3049 | } | |
fc04fa47 | 3050 | |
74514898 RS |
3051 | if (next_ptr) |
3052 | *next_ptr = next; | |
3053 | if (prev_ptr) | |
3054 | *prev_ptr = prev; | |
3055 | return idx; | |
3056 | } | |
09a22085 GM |
3057 | |
3058 | ||
37ef52bb | 3059 | /* Return true if there exists an overlay with a non-nil |
09a22085 GM |
3060 | `mouse-face' property overlapping OVERLAY. */ |
3061 | ||
37ef52bb | 3062 | bool |
d3da34e0 | 3063 | mouse_face_overlay_overlaps (Lisp_Object overlay) |
09a22085 | 3064 | { |
d311d28c PE |
3065 | ptrdiff_t start = OVERLAY_POSITION (OVERLAY_START (overlay)); |
3066 | ptrdiff_t end = OVERLAY_POSITION (OVERLAY_END (overlay)); | |
21514da7 | 3067 | ptrdiff_t n, i, size; |
09a22085 | 3068 | Lisp_Object *v, tem; |
177c0ea7 | 3069 | |
bfd8410f | 3070 | size = 10; |
38182d90 | 3071 | v = alloca (size * sizeof *v); |
bfd8410f GM |
3072 | n = overlays_in (start, end, 0, &v, &size, NULL, NULL); |
3073 | if (n > size) | |
09a22085 | 3074 | { |
38182d90 | 3075 | v = alloca (n * sizeof *v); |
09a22085 GM |
3076 | overlays_in (start, end, 0, &v, &n, NULL, NULL); |
3077 | } | |
3078 | ||
3079 | for (i = 0; i < n; ++i) | |
3080 | if (!EQ (v[i], overlay) | |
3081 | && (tem = Foverlay_get (overlay, Qmouse_face), | |
3082 | !NILP (tem))) | |
3083 | break; | |
3084 | ||
3085 | return i < n; | |
3086 | } | |
3087 | ||
3088 | ||
74514898 | 3089 | \f |
fc04fa47 | 3090 | /* Fast function to just test if we're at an overlay boundary. */ |
37ef52bb | 3091 | bool |
d311d28c | 3092 | overlay_touches_p (ptrdiff_t pos) |
fc04fa47 | 3093 | { |
2410d73a SM |
3094 | Lisp_Object overlay; |
3095 | struct Lisp_Overlay *tail; | |
fc04fa47 | 3096 | |
fd318b54 | 3097 | for (tail = current_buffer->overlays_before; tail; tail = tail->next) |
fc04fa47 | 3098 | { |
d311d28c | 3099 | ptrdiff_t endpos; |
fc04fa47 | 3100 | |
2410d73a | 3101 | XSETMISC (overlay ,tail); |
22657b40 | 3102 | eassert (OVERLAYP (overlay)); |
fc04fa47 KH |
3103 | |
3104 | endpos = OVERLAY_POSITION (OVERLAY_END (overlay)); | |
3105 | if (endpos < pos) | |
3106 | break; | |
3107 | if (endpos == pos || OVERLAY_POSITION (OVERLAY_START (overlay)) == pos) | |
3108 | return 1; | |
3109 | } | |
3110 | ||
fd318b54 | 3111 | for (tail = current_buffer->overlays_after; tail; tail = tail->next) |
fc04fa47 | 3112 | { |
d311d28c | 3113 | ptrdiff_t startpos; |
fc04fa47 | 3114 | |
2410d73a | 3115 | XSETMISC (overlay, tail); |
22657b40 | 3116 | eassert (OVERLAYP (overlay)); |
fc04fa47 KH |
3117 | |
3118 | startpos = OVERLAY_POSITION (OVERLAY_START (overlay)); | |
3119 | if (pos < startpos) | |
3120 | break; | |
3121 | if (startpos == pos || OVERLAY_POSITION (OVERLAY_END (overlay)) == pos) | |
3122 | return 1; | |
3123 | } | |
3124 | return 0; | |
3125 | } | |
2eec3b4e | 3126 | \f |
5985d248 KH |
3127 | struct sortvec |
3128 | { | |
3129 | Lisp_Object overlay; | |
d311d28c | 3130 | ptrdiff_t beg, end; |
fadf4e30 | 3131 | EMACS_INT priority; |
5985d248 KH |
3132 | }; |
3133 | ||
3134 | static int | |
d3da34e0 | 3135 | compare_overlays (const void *v1, const void *v2) |
5985d248 | 3136 | { |
dfcf069d AS |
3137 | const struct sortvec *s1 = (const struct sortvec *) v1; |
3138 | const struct sortvec *s2 = (const struct sortvec *) v2; | |
5985d248 | 3139 | if (s1->priority != s2->priority) |
c20998a7 | 3140 | return s1->priority < s2->priority ? -1 : 1; |
5985d248 | 3141 | if (s1->beg != s2->beg) |
c20998a7 | 3142 | return s1->beg < s2->beg ? -1 : 1; |
5985d248 | 3143 | if (s1->end != s2->end) |
c20998a7 | 3144 | return s2->end < s1->end ? -1 : 1; |
cae07000 SM |
3145 | /* Avoid the non-determinism of qsort by choosing an arbitrary ordering |
3146 | between "equal" overlays. The result can still change between | |
3147 | invocations of Emacs, but it won't change in the middle of | |
3148 | `find_field' (bug#6830). */ | |
9af5ed87 PE |
3149 | if (XHASH (s1->overlay) != XHASH (s2->overlay)) |
3150 | return XHASH (s1->overlay) < XHASH (s2->overlay) ? -1 : 1; | |
5985d248 KH |
3151 | return 0; |
3152 | } | |
3153 | ||
3154 | /* Sort an array of overlays by priority. The array is modified in place. | |
3155 | The return value is the new size; this may be smaller than the original | |
3156 | size if some of the overlays were invalid or were window-specific. */ | |
b081724f PE |
3157 | ptrdiff_t |
3158 | sort_overlays (Lisp_Object *overlay_vec, ptrdiff_t noverlays, struct window *w) | |
5985d248 | 3159 | { |
b081724f | 3160 | ptrdiff_t i, j; |
38182d90 | 3161 | struct sortvec *sortvec = alloca (noverlays * sizeof *sortvec); |
5985d248 KH |
3162 | |
3163 | /* Put the valid and relevant overlays into sortvec. */ | |
3164 | ||
3165 | for (i = 0, j = 0; i < noverlays; i++) | |
3166 | { | |
0fa767e7 | 3167 | Lisp_Object tem; |
c99fc30f | 3168 | Lisp_Object overlay; |
5985d248 | 3169 | |
c99fc30f | 3170 | overlay = overlay_vec[i]; |
22657b40 | 3171 | if (OVERLAYP (overlay) |
5985d248 KH |
3172 | && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0 |
3173 | && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0) | |
3174 | { | |
0fa767e7 KH |
3175 | /* If we're interested in a specific window, then ignore |
3176 | overlays that are limited to some other window. */ | |
3177 | if (w) | |
5985d248 | 3178 | { |
28545e04 | 3179 | Lisp_Object window; |
0fa767e7 KH |
3180 | |
3181 | window = Foverlay_get (overlay, Qwindow); | |
28545e04 | 3182 | if (WINDOWP (window) && XWINDOW (window) != w) |
0fa767e7 | 3183 | continue; |
5985d248 | 3184 | } |
0fa767e7 KH |
3185 | |
3186 | /* This overlay is good and counts: put it into sortvec. */ | |
3187 | sortvec[j].overlay = overlay; | |
3188 | sortvec[j].beg = OVERLAY_POSITION (OVERLAY_START (overlay)); | |
3189 | sortvec[j].end = OVERLAY_POSITION (OVERLAY_END (overlay)); | |
3190 | tem = Foverlay_get (overlay, Qpriority); | |
3191 | if (INTEGERP (tem)) | |
3192 | sortvec[j].priority = XINT (tem); | |
3193 | else | |
3194 | sortvec[j].priority = 0; | |
3195 | j++; | |
5985d248 KH |
3196 | } |
3197 | } | |
3198 | noverlays = j; | |
3199 | ||
3200 | /* Sort the overlays into the proper order: increasing priority. */ | |
3201 | ||
3202 | if (noverlays > 1) | |
3203 | qsort (sortvec, noverlays, sizeof (struct sortvec), compare_overlays); | |
3204 | ||
3205 | for (i = 0; i < noverlays; i++) | |
3206 | overlay_vec[i] = sortvec[i].overlay; | |
3207 | return (noverlays); | |
3208 | } | |
3209 | \f | |
bbbe9545 KH |
3210 | struct sortstr |
3211 | { | |
cb26008f | 3212 | Lisp_Object string, string2; |
93cb6be3 | 3213 | ptrdiff_t size; |
8961a454 | 3214 | EMACS_INT priority; |
bbbe9545 KH |
3215 | }; |
3216 | ||
e8185fa8 KH |
3217 | struct sortstrlist |
3218 | { | |
3219 | struct sortstr *buf; /* An array that expands as needed; never freed. */ | |
67c36fce PE |
3220 | ptrdiff_t size; /* Allocated length of that array. */ |
3221 | ptrdiff_t used; /* How much of the array is currently in use. */ | |
0065d054 | 3222 | ptrdiff_t bytes; /* Total length of the strings in buf. */ |
e8185fa8 KH |
3223 | }; |
3224 | ||
3225 | /* Buffers for storing information about the overlays touching a given | |
3226 | position. These could be automatic variables in overlay_strings, but | |
3227 | it's more efficient to hold onto the memory instead of repeatedly | |
3228 | allocating and freeing it. */ | |
3229 | static struct sortstrlist overlay_heads, overlay_tails; | |
9492daf2 | 3230 | static unsigned char *overlay_str_buf; |
e8185fa8 KH |
3231 | |
3232 | /* Allocated length of overlay_str_buf. */ | |
fe5c5d37 | 3233 | static ptrdiff_t overlay_str_len; |
e8185fa8 | 3234 | |
bbbe9545 KH |
3235 | /* A comparison function suitable for passing to qsort. */ |
3236 | static int | |
d3da34e0 | 3237 | cmp_for_strings (const void *as1, const void *as2) |
bbbe9545 KH |
3238 | { |
3239 | struct sortstr *s1 = (struct sortstr *)as1; | |
3240 | struct sortstr *s2 = (struct sortstr *)as2; | |
3241 | if (s1->size != s2->size) | |
c20998a7 | 3242 | return s2->size < s1->size ? -1 : 1; |
bbbe9545 | 3243 | if (s1->priority != s2->priority) |
c20998a7 | 3244 | return s1->priority < s2->priority ? -1 : 1; |
bbbe9545 KH |
3245 | return 0; |
3246 | } | |
3247 | ||
e8185fa8 | 3248 | static void |
93cb6be3 PE |
3249 | record_overlay_string (struct sortstrlist *ssl, Lisp_Object str, |
3250 | Lisp_Object str2, Lisp_Object pri, ptrdiff_t size) | |
e8185fa8 | 3251 | { |
d311d28c | 3252 | ptrdiff_t nbytes; |
43d27a72 | 3253 | |
e8185fa8 | 3254 | if (ssl->used == ssl->size) |
0065d054 | 3255 | ssl->buf = xpalloc (ssl->buf, &ssl->size, 5, -1, sizeof *ssl->buf); |
e8185fa8 | 3256 | ssl->buf[ssl->used].string = str; |
cb26008f | 3257 | ssl->buf[ssl->used].string2 = str2; |
e8185fa8 KH |
3258 | ssl->buf[ssl->used].size = size; |
3259 | ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0); | |
3260 | ssl->used++; | |
43d27a72 | 3261 | |
4b4deea2 | 3262 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
d5db4077 | 3263 | nbytes = SCHARS (str); |
43d27a72 | 3264 | else if (! STRING_MULTIBYTE (str)) |
d5db4077 KR |
3265 | nbytes = count_size_as_multibyte (SDATA (str), |
3266 | SBYTES (str)); | |
43d27a72 | 3267 | else |
d5db4077 | 3268 | nbytes = SBYTES (str); |
43d27a72 | 3269 | |
0065d054 PE |
3270 | if (INT_ADD_OVERFLOW (ssl->bytes, nbytes)) |
3271 | memory_full (SIZE_MAX); | |
43d27a72 RS |
3272 | ssl->bytes += nbytes; |
3273 | ||
cb26008f | 3274 | if (STRINGP (str2)) |
43d27a72 | 3275 | { |
4b4deea2 | 3276 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
d5db4077 | 3277 | nbytes = SCHARS (str2); |
43d27a72 | 3278 | else if (! STRING_MULTIBYTE (str2)) |
d5db4077 KR |
3279 | nbytes = count_size_as_multibyte (SDATA (str2), |
3280 | SBYTES (str2)); | |
43d27a72 | 3281 | else |
d5db4077 | 3282 | nbytes = SBYTES (str2); |
43d27a72 | 3283 | |
0065d054 PE |
3284 | if (INT_ADD_OVERFLOW (ssl->bytes, nbytes)) |
3285 | memory_full (SIZE_MAX); | |
43d27a72 RS |
3286 | ssl->bytes += nbytes; |
3287 | } | |
e8185fa8 | 3288 | } |
bbbe9545 KH |
3289 | |
3290 | /* Return the concatenation of the strings associated with overlays that | |
3291 | begin or end at POS, ignoring overlays that are specific to a window | |
3292 | other than W. The strings are concatenated in the appropriate order: | |
3293 | shorter overlays nest inside longer ones, and higher priority inside | |
cb26008f KH |
3294 | lower. Normally all of the after-strings come first, but zero-sized |
3295 | overlays have their after-strings ride along with the before-strings | |
3296 | because it would look strange to print them inside-out. | |
3297 | ||
3298 | Returns the string length, and stores the contents indirectly through | |
3299 | PSTR, if that variable is non-null. The string may be overwritten by | |
3300 | subsequent calls. */ | |
6b5d3b89 | 3301 | |
d311d28c PE |
3302 | ptrdiff_t |
3303 | overlay_strings (ptrdiff_t pos, struct window *w, unsigned char **pstr) | |
bbbe9545 | 3304 | { |
28545e04 | 3305 | Lisp_Object overlay, window, str; |
2410d73a | 3306 | struct Lisp_Overlay *ov; |
d311d28c | 3307 | ptrdiff_t startpos, endpos; |
37ef52bb | 3308 | bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
bbbe9545 | 3309 | |
e8185fa8 KH |
3310 | overlay_heads.used = overlay_heads.bytes = 0; |
3311 | overlay_tails.used = overlay_tails.bytes = 0; | |
fd318b54 | 3312 | for (ov = current_buffer->overlays_before; ov; ov = ov->next) |
bbbe9545 | 3313 | { |
2410d73a | 3314 | XSETMISC (overlay, ov); |
c2d5b10f | 3315 | eassert (OVERLAYP (overlay)); |
bbbe9545 KH |
3316 | |
3317 | startpos = OVERLAY_POSITION (OVERLAY_START (overlay)); | |
3318 | endpos = OVERLAY_POSITION (OVERLAY_END (overlay)); | |
3319 | if (endpos < pos) | |
3320 | break; | |
3321 | if (endpos != pos && startpos != pos) | |
3322 | continue; | |
3323 | window = Foverlay_get (overlay, Qwindow); | |
28545e04 | 3324 | if (WINDOWP (window) && XWINDOW (window) != w) |
bbbe9545 | 3325 | continue; |
e8185fa8 KH |
3326 | if (startpos == pos |
3327 | && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))) | |
3328 | record_overlay_string (&overlay_heads, str, | |
cb26008f KH |
3329 | (startpos == endpos |
3330 | ? Foverlay_get (overlay, Qafter_string) | |
3331 | : Qnil), | |
3332 | Foverlay_get (overlay, Qpriority), | |
3333 | endpos - startpos); | |
3334 | else if (endpos == pos | |
3335 | && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))) | |
3336 | record_overlay_string (&overlay_tails, str, Qnil, | |
e8185fa8 KH |
3337 | Foverlay_get (overlay, Qpriority), |
3338 | endpos - startpos); | |
bbbe9545 | 3339 | } |
fd318b54 | 3340 | for (ov = current_buffer->overlays_after; ov; ov = ov->next) |
bbbe9545 | 3341 | { |
2410d73a SM |
3342 | XSETMISC (overlay, ov); |
3343 | eassert (OVERLAYP (overlay)); | |
bbbe9545 KH |
3344 | |
3345 | startpos = OVERLAY_POSITION (OVERLAY_START (overlay)); | |
3346 | endpos = OVERLAY_POSITION (OVERLAY_END (overlay)); | |
3347 | if (startpos > pos) | |
3348 | break; | |
e8185fa8 KH |
3349 | if (endpos != pos && startpos != pos) |
3350 | continue; | |
3351 | window = Foverlay_get (overlay, Qwindow); | |
28545e04 | 3352 | if (WINDOWP (window) && XWINDOW (window) != w) |
e8185fa8 | 3353 | continue; |
e8185fa8 KH |
3354 | if (startpos == pos |
3355 | && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))) | |
3356 | record_overlay_string (&overlay_heads, str, | |
cb26008f KH |
3357 | (startpos == endpos |
3358 | ? Foverlay_get (overlay, Qafter_string) | |
3359 | : Qnil), | |
3360 | Foverlay_get (overlay, Qpriority), | |
3361 | endpos - startpos); | |
3362 | else if (endpos == pos | |
3363 | && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))) | |
3364 | record_overlay_string (&overlay_tails, str, Qnil, | |
e8185fa8 KH |
3365 | Foverlay_get (overlay, Qpriority), |
3366 | endpos - startpos); | |
bbbe9545 | 3367 | } |
e8185fa8 KH |
3368 | if (overlay_tails.used > 1) |
3369 | qsort (overlay_tails.buf, overlay_tails.used, sizeof (struct sortstr), | |
3370 | cmp_for_strings); | |
3371 | if (overlay_heads.used > 1) | |
3372 | qsort (overlay_heads.buf, overlay_heads.used, sizeof (struct sortstr), | |
3373 | cmp_for_strings); | |
3374 | if (overlay_heads.bytes || overlay_tails.bytes) | |
bbbe9545 | 3375 | { |
e8185fa8 | 3376 | Lisp_Object tem; |
d311d28c | 3377 | ptrdiff_t i; |
9f4d7cde | 3378 | unsigned char *p; |
0065d054 | 3379 | ptrdiff_t total; |
bbbe9545 | 3380 | |
0065d054 PE |
3381 | if (INT_ADD_OVERFLOW (overlay_heads.bytes, overlay_tails.bytes)) |
3382 | memory_full (SIZE_MAX); | |
3383 | total = overlay_heads.bytes + overlay_tails.bytes; | |
bbbe9545 | 3384 | if (total > overlay_str_len) |
0065d054 PE |
3385 | overlay_str_buf = xpalloc (overlay_str_buf, &overlay_str_len, |
3386 | total - overlay_str_len, -1, 1); | |
3387 | ||
bbbe9545 | 3388 | p = overlay_str_buf; |
e8185fa8 | 3389 | for (i = overlay_tails.used; --i >= 0;) |
bbbe9545 | 3390 | { |
d311d28c | 3391 | ptrdiff_t nbytes; |
e8185fa8 | 3392 | tem = overlay_tails.buf[i].string; |
d5db4077 KR |
3393 | nbytes = copy_text (SDATA (tem), p, |
3394 | SBYTES (tem), | |
43d27a72 RS |
3395 | STRING_MULTIBYTE (tem), multibyte); |
3396 | p += nbytes; | |
bbbe9545 | 3397 | } |
e8185fa8 | 3398 | for (i = 0; i < overlay_heads.used; ++i) |
bbbe9545 | 3399 | { |
d311d28c | 3400 | ptrdiff_t nbytes; |
e8185fa8 | 3401 | tem = overlay_heads.buf[i].string; |
d5db4077 KR |
3402 | nbytes = copy_text (SDATA (tem), p, |
3403 | SBYTES (tem), | |
43d27a72 RS |
3404 | STRING_MULTIBYTE (tem), multibyte); |
3405 | p += nbytes; | |
cb26008f KH |
3406 | tem = overlay_heads.buf[i].string2; |
3407 | if (STRINGP (tem)) | |
3408 | { | |
d5db4077 KR |
3409 | nbytes = copy_text (SDATA (tem), p, |
3410 | SBYTES (tem), | |
43d27a72 RS |
3411 | STRING_MULTIBYTE (tem), multibyte); |
3412 | p += nbytes; | |
cb26008f | 3413 | } |
bbbe9545 | 3414 | } |
cb26008f | 3415 | if (p != overlay_str_buf + total) |
1088b922 | 3416 | emacs_abort (); |
bbbe9545 KH |
3417 | if (pstr) |
3418 | *pstr = overlay_str_buf; | |
e8185fa8 | 3419 | return total; |
bbbe9545 | 3420 | } |
e8185fa8 | 3421 | return 0; |
bbbe9545 KH |
3422 | } |
3423 | \f | |
5c4f68f1 | 3424 | /* Shift overlays in BUF's overlay lists, to center the lists at POS. */ |
1ab256cb | 3425 | |
2eec3b4e | 3426 | void |
d311d28c | 3427 | recenter_overlay_lists (struct buffer *buf, ptrdiff_t pos) |
2eec3b4e | 3428 | { |
2410d73a SM |
3429 | Lisp_Object overlay, beg, end; |
3430 | struct Lisp_Overlay *prev, *tail, *next; | |
2eec3b4e RS |
3431 | |
3432 | /* See if anything in overlays_before should move to overlays_after. */ | |
3433 | ||
3434 | /* We don't strictly need prev in this loop; it should always be nil. | |
3435 | But we use it for symmetry and in case that should cease to be true | |
3436 | with some future change. */ | |
2410d73a | 3437 | prev = NULL; |
fd318b54 | 3438 | for (tail = buf->overlays_before; tail; prev = tail, tail = next) |
1ab256cb | 3439 | { |
2410d73a SM |
3440 | next = tail->next; |
3441 | XSETMISC (overlay, tail); | |
22657b40 | 3442 | eassert (OVERLAYP (overlay)); |
1ab256cb | 3443 | |
2eec3b4e RS |
3444 | beg = OVERLAY_START (overlay); |
3445 | end = OVERLAY_END (overlay); | |
1ab256cb | 3446 | |
2eec3b4e | 3447 | if (OVERLAY_POSITION (end) > pos) |
1ab256cb | 3448 | { |
2eec3b4e | 3449 | /* OVERLAY needs to be moved. */ |
d311d28c | 3450 | ptrdiff_t where = OVERLAY_POSITION (beg); |
2410d73a | 3451 | struct Lisp_Overlay *other, *other_prev; |
2eec3b4e RS |
3452 | |
3453 | /* Splice the cons cell TAIL out of overlays_before. */ | |
2410d73a SM |
3454 | if (prev) |
3455 | prev->next = next; | |
2eec3b4e | 3456 | else |
0c94c8d6 | 3457 | set_buffer_overlays_before (buf, next); |
2eec3b4e RS |
3458 | |
3459 | /* Search thru overlays_after for where to put it. */ | |
2410d73a | 3460 | other_prev = NULL; |
fd318b54 | 3461 | for (other = buf->overlays_after; other; |
2410d73a | 3462 | other_prev = other, other = other->next) |
1ab256cb | 3463 | { |
6af718a4 | 3464 | Lisp_Object otherbeg, otheroverlay; |
2eec3b4e | 3465 | |
2410d73a | 3466 | XSETMISC (otheroverlay, other); |
22657b40 | 3467 | eassert (OVERLAYP (otheroverlay)); |
2eec3b4e RS |
3468 | |
3469 | otherbeg = OVERLAY_START (otheroverlay); | |
3470 | if (OVERLAY_POSITION (otherbeg) >= where) | |
3471 | break; | |
1ab256cb | 3472 | } |
2eec3b4e RS |
3473 | |
3474 | /* Add TAIL to overlays_after before OTHER. */ | |
2410d73a SM |
3475 | tail->next = other; |
3476 | if (other_prev) | |
3477 | other_prev->next = tail; | |
1ab256cb | 3478 | else |
0c94c8d6 | 3479 | set_buffer_overlays_after (buf, tail); |
2eec3b4e | 3480 | tail = prev; |
1ab256cb | 3481 | } |
2eec3b4e RS |
3482 | else |
3483 | /* We've reached the things that should stay in overlays_before. | |
3484 | All the rest of overlays_before must end even earlier, | |
3485 | so stop now. */ | |
3486 | break; | |
3487 | } | |
3488 | ||
3489 | /* See if anything in overlays_after should be in overlays_before. */ | |
2410d73a | 3490 | prev = NULL; |
fd318b54 | 3491 | for (tail = buf->overlays_after; tail; prev = tail, tail = next) |
2eec3b4e | 3492 | { |
2410d73a SM |
3493 | next = tail->next; |
3494 | XSETMISC (overlay, tail); | |
22657b40 | 3495 | eassert (OVERLAYP (overlay)); |
2eec3b4e RS |
3496 | |
3497 | beg = OVERLAY_START (overlay); | |
3498 | end = OVERLAY_END (overlay); | |
3499 | ||
3500 | /* Stop looking, when we know that nothing further | |
3501 | can possibly end before POS. */ | |
3502 | if (OVERLAY_POSITION (beg) > pos) | |
3503 | break; | |
3504 | ||
3505 | if (OVERLAY_POSITION (end) <= pos) | |
3506 | { | |
3507 | /* OVERLAY needs to be moved. */ | |
d311d28c | 3508 | ptrdiff_t where = OVERLAY_POSITION (end); |
2410d73a | 3509 | struct Lisp_Overlay *other, *other_prev; |
2eec3b4e RS |
3510 | |
3511 | /* Splice the cons cell TAIL out of overlays_after. */ | |
2410d73a SM |
3512 | if (prev) |
3513 | prev->next = next; | |
2eec3b4e | 3514 | else |
0c94c8d6 | 3515 | set_buffer_overlays_after (buf, next); |
2eec3b4e RS |
3516 | |
3517 | /* Search thru overlays_before for where to put it. */ | |
2410d73a | 3518 | other_prev = NULL; |
fd318b54 | 3519 | for (other = buf->overlays_before; other; |
2410d73a | 3520 | other_prev = other, other = other->next) |
2eec3b4e RS |
3521 | { |
3522 | Lisp_Object otherend, otheroverlay; | |
2eec3b4e | 3523 | |
2410d73a | 3524 | XSETMISC (otheroverlay, other); |
22657b40 | 3525 | eassert (OVERLAYP (otheroverlay)); |
2eec3b4e RS |
3526 | |
3527 | otherend = OVERLAY_END (otheroverlay); | |
3528 | if (OVERLAY_POSITION (otherend) <= where) | |
3529 | break; | |
3530 | } | |
3531 | ||
3532 | /* Add TAIL to overlays_before before OTHER. */ | |
2410d73a SM |
3533 | tail->next = other; |
3534 | if (other_prev) | |
3535 | other_prev->next = tail; | |
2eec3b4e | 3536 | else |
0c94c8d6 | 3537 | set_buffer_overlays_before (buf, tail); |
2eec3b4e RS |
3538 | tail = prev; |
3539 | } | |
3540 | } | |
3541 | ||
c2d5b10f | 3542 | buf->overlay_center = pos; |
2eec3b4e | 3543 | } |
2b1bdf65 | 3544 | |
423cdb46 | 3545 | void |
d311d28c | 3546 | adjust_overlays_for_insert (ptrdiff_t pos, ptrdiff_t length) |
423cdb46 KH |
3547 | { |
3548 | /* After an insertion, the lists are still sorted properly, | |
3549 | but we may need to update the value of the overlay center. */ | |
c2d5b10f SM |
3550 | if (current_buffer->overlay_center >= pos) |
3551 | current_buffer->overlay_center += length; | |
423cdb46 KH |
3552 | } |
3553 | ||
3554 | void | |
d311d28c | 3555 | adjust_overlays_for_delete (ptrdiff_t pos, ptrdiff_t length) |
423cdb46 | 3556 | { |
c2d5b10f | 3557 | if (current_buffer->overlay_center < pos) |
423cdb46 KH |
3558 | /* The deletion was to our right. No change needed; the before- and |
3559 | after-lists are still consistent. */ | |
3560 | ; | |
d311d28c | 3561 | else if (current_buffer->overlay_center - pos > length) |
423cdb46 KH |
3562 | /* The deletion was to our left. We need to adjust the center value |
3563 | to account for the change in position, but the lists are consistent | |
3564 | given the new value. */ | |
c2d5b10f | 3565 | current_buffer->overlay_center -= length; |
423cdb46 KH |
3566 | else |
3567 | /* We're right in the middle. There might be things on the after-list | |
3568 | that now belong on the before-list. Recentering will move them, | |
3569 | and also update the center point. */ | |
3570 | recenter_overlay_lists (current_buffer, pos); | |
3571 | } | |
3572 | ||
2b1bdf65 KH |
3573 | /* Fix up overlays that were garbled as a result of permuting markers |
3574 | in the range START through END. Any overlay with at least one | |
3575 | endpoint in this range will need to be unlinked from the overlay | |
3576 | list and reinserted in its proper place. | |
3577 | Such an overlay might even have negative size at this point. | |
6b61353c | 3578 | If so, we'll make the overlay empty. */ |
2b1bdf65 | 3579 | void |
d311d28c | 3580 | fix_start_end_in_overlays (register ptrdiff_t start, register ptrdiff_t end) |
2b1bdf65 | 3581 | { |
6af718a4 | 3582 | Lisp_Object overlay; |
5df8f01b PE |
3583 | struct Lisp_Overlay *before_list IF_LINT (= NULL); |
3584 | struct Lisp_Overlay *after_list IF_LINT (= NULL); | |
1138e742 KR |
3585 | /* These are either nil, indicating that before_list or after_list |
3586 | should be assigned, or the cons cell the cdr of which should be | |
3587 | assigned. */ | |
2410d73a | 3588 | struct Lisp_Overlay *beforep = NULL, *afterp = NULL; |
1138e742 | 3589 | /* 'Parent', likewise, indicates a cons cell or |
fd318b54 | 3590 | current_buffer->overlays_before or overlays_after, depending |
1138e742 | 3591 | which loop we're in. */ |
2410d73a | 3592 | struct Lisp_Overlay *tail, *parent; |
d311d28c | 3593 | ptrdiff_t startpos, endpos; |
2b1bdf65 KH |
3594 | |
3595 | /* This algorithm shifts links around instead of consing and GCing. | |
3596 | The loop invariant is that before_list (resp. after_list) is a | |
1138e742 KR |
3597 | well-formed list except that its last element, the CDR of beforep |
3598 | (resp. afterp) if beforep (afterp) isn't nil or before_list | |
3599 | (after_list) if it is, is still uninitialized. So it's not a bug | |
3600 | that before_list isn't initialized, although it may look | |
3601 | strange. */ | |
fd318b54 | 3602 | for (parent = NULL, tail = current_buffer->overlays_before; tail;) |
2b1bdf65 | 3603 | { |
2410d73a | 3604 | XSETMISC (overlay, tail); |
6b61353c | 3605 | |
2b1bdf65 | 3606 | endpos = OVERLAY_POSITION (OVERLAY_END (overlay)); |
6b61353c KH |
3607 | startpos = OVERLAY_POSITION (OVERLAY_START (overlay)); |
3608 | ||
3609 | /* If the overlay is backwards, make it empty. */ | |
3610 | if (endpos < startpos) | |
3611 | { | |
3612 | startpos = endpos; | |
3613 | Fset_marker (OVERLAY_START (overlay), make_number (startpos), | |
3614 | Qnil); | |
3615 | } | |
3616 | ||
2b1bdf65 KH |
3617 | if (endpos < start) |
3618 | break; | |
60ebfdf3 | 3619 | |
2b1bdf65 KH |
3620 | if (endpos < end |
3621 | || (startpos >= start && startpos < end)) | |
3622 | { | |
2b1bdf65 KH |
3623 | /* Add it to the end of the wrong list. Later on, |
3624 | recenter_overlay_lists will move it to the right place. */ | |
c2d5b10f | 3625 | if (endpos < current_buffer->overlay_center) |
2b1bdf65 | 3626 | { |
2410d73a | 3627 | if (!afterp) |
1138e742 KR |
3628 | after_list = tail; |
3629 | else | |
2410d73a | 3630 | afterp->next = tail; |
1138e742 | 3631 | afterp = tail; |
2b1bdf65 KH |
3632 | } |
3633 | else | |
3634 | { | |
2410d73a | 3635 | if (!beforep) |
1138e742 KR |
3636 | before_list = tail; |
3637 | else | |
2410d73a | 3638 | beforep->next = tail; |
1138e742 | 3639 | beforep = tail; |
2b1bdf65 | 3640 | } |
2410d73a | 3641 | if (!parent) |
0c94c8d6 | 3642 | set_buffer_overlays_before (current_buffer, tail->next); |
1138e742 | 3643 | else |
2410d73a SM |
3644 | parent->next = tail->next; |
3645 | tail = tail->next; | |
2b1bdf65 KH |
3646 | } |
3647 | else | |
2410d73a | 3648 | parent = tail, tail = parent->next; |
2b1bdf65 | 3649 | } |
fd318b54 | 3650 | for (parent = NULL, tail = current_buffer->overlays_after; tail;) |
2b1bdf65 | 3651 | { |
2410d73a | 3652 | XSETMISC (overlay, tail); |
6b61353c | 3653 | |
2b1bdf65 | 3654 | startpos = OVERLAY_POSITION (OVERLAY_START (overlay)); |
6b61353c KH |
3655 | endpos = OVERLAY_POSITION (OVERLAY_END (overlay)); |
3656 | ||
3657 | /* If the overlay is backwards, make it empty. */ | |
3658 | if (endpos < startpos) | |
3659 | { | |
3660 | startpos = endpos; | |
3661 | Fset_marker (OVERLAY_START (overlay), make_number (startpos), | |
60ebfdf3 | 3662 | Qnil); |
6b61353c KH |
3663 | } |
3664 | ||
2b1bdf65 KH |
3665 | if (startpos >= end) |
3666 | break; | |
6b61353c | 3667 | |
2b1bdf65 KH |
3668 | if (startpos >= start |
3669 | || (endpos >= start && endpos < end)) | |
3670 | { | |
c2d5b10f | 3671 | if (endpos < current_buffer->overlay_center) |
2b1bdf65 | 3672 | { |
2410d73a | 3673 | if (!afterp) |
1138e742 KR |
3674 | after_list = tail; |
3675 | else | |
2410d73a | 3676 | afterp->next = tail; |
1138e742 | 3677 | afterp = tail; |
2b1bdf65 KH |
3678 | } |
3679 | else | |
3680 | { | |
2410d73a | 3681 | if (!beforep) |
1138e742 KR |
3682 | before_list = tail; |
3683 | else | |
2410d73a | 3684 | beforep->next = tail; |
1138e742 | 3685 | beforep = tail; |
2b1bdf65 | 3686 | } |
2410d73a | 3687 | if (!parent) |
0c94c8d6 | 3688 | set_buffer_overlays_after (current_buffer, tail->next); |
1138e742 | 3689 | else |
2410d73a SM |
3690 | parent->next = tail->next; |
3691 | tail = tail->next; | |
2b1bdf65 KH |
3692 | } |
3693 | else | |
2410d73a | 3694 | parent = tail, tail = parent->next; |
2b1bdf65 KH |
3695 | } |
3696 | ||
3697 | /* Splice the constructed (wrong) lists into the buffer's lists, | |
3698 | and let the recenter function make it sane again. */ | |
2410d73a | 3699 | if (beforep) |
1138e742 | 3700 | { |
fd318b54 | 3701 | beforep->next = current_buffer->overlays_before; |
0c94c8d6 | 3702 | set_buffer_overlays_before (current_buffer, before_list); |
1138e742 | 3703 | } |
c2d5b10f | 3704 | recenter_overlay_lists (current_buffer, current_buffer->overlay_center); |
2b1bdf65 | 3705 | |
2410d73a | 3706 | if (afterp) |
1138e742 | 3707 | { |
fd318b54 | 3708 | afterp->next = current_buffer->overlays_after; |
0c94c8d6 | 3709 | set_buffer_overlays_after (current_buffer, after_list); |
1138e742 | 3710 | } |
c2d5b10f | 3711 | recenter_overlay_lists (current_buffer, current_buffer->overlay_center); |
2b1bdf65 | 3712 | } |
3b06f880 KH |
3713 | |
3714 | /* We have two types of overlay: the one whose ending marker is | |
3715 | after-insertion-marker (this is the usual case) and the one whose | |
3716 | ending marker is before-insertion-marker. When `overlays_before' | |
3717 | contains overlays of the latter type and the former type in this | |
3718 | order and both overlays end at inserting position, inserting a text | |
3719 | increases only the ending marker of the latter type, which results | |
3720 | in incorrect ordering of `overlays_before'. | |
3721 | ||
3722 | This function fixes ordering of overlays in the slot | |
3723 | `overlays_before' of the buffer *BP. Before the insertion, `point' | |
3724 | was at PREV, and now is at POS. */ | |
3725 | ||
01136e9b | 3726 | void |
d311d28c | 3727 | fix_overlays_before (struct buffer *bp, ptrdiff_t prev, ptrdiff_t pos) |
3b06f880 | 3728 | { |
2410d73a | 3729 | /* If parent is nil, replace overlays_before; otherwise, parent->next. */ |
fd318b54 | 3730 | struct Lisp_Overlay *tail = bp->overlays_before, *parent = NULL, *right_pair; |
2410d73a | 3731 | Lisp_Object tem; |
d311d28c | 3732 | ptrdiff_t end IF_LINT (= 0); |
3b06f880 KH |
3733 | |
3734 | /* After the insertion, the several overlays may be in incorrect | |
3735 | order. The possibility is that, in the list `overlays_before', | |
3736 | an overlay which ends at POS appears after an overlay which ends | |
3737 | at PREV. Since POS is greater than PREV, we must fix the | |
3738 | ordering of these overlays, by moving overlays ends at POS before | |
3739 | the overlays ends at PREV. */ | |
3740 | ||
3741 | /* At first, find a place where disordered overlays should be linked | |
3742 | in. It is where an overlay which end before POS exists. (i.e. an | |
3743 | overlay whose ending marker is after-insertion-marker if disorder | |
3744 | exists). */ | |
2410d73a SM |
3745 | while (tail |
3746 | && (XSETMISC (tem, tail), | |
3747 | (end = OVERLAY_POSITION (OVERLAY_END (tem))) >= pos)) | |
1138e742 KR |
3748 | { |
3749 | parent = tail; | |
2410d73a | 3750 | tail = tail->next; |
1138e742 | 3751 | } |
3b06f880 KH |
3752 | |
3753 | /* If we don't find such an overlay, | |
3754 | or the found one ends before PREV, | |
3755 | or the found one is the last one in the list, | |
3756 | we don't have to fix anything. */ | |
f93ad4cf | 3757 | if (!tail || end < prev || !tail->next) |
3b06f880 KH |
3758 | return; |
3759 | ||
1138e742 KR |
3760 | right_pair = parent; |
3761 | parent = tail; | |
2410d73a | 3762 | tail = tail->next; |
3b06f880 | 3763 | |
1138e742 | 3764 | /* Now, end position of overlays in the list TAIL should be before |
3b06f880 | 3765 | or equal to PREV. In the loop, an overlay which ends at POS is |
1138e742 KR |
3766 | moved ahead to the place indicated by the CDR of RIGHT_PAIR. If |
3767 | we found an overlay which ends before PREV, the remaining | |
3768 | overlays are in correct order. */ | |
2410d73a | 3769 | while (tail) |
3b06f880 | 3770 | { |
2410d73a SM |
3771 | XSETMISC (tem, tail); |
3772 | end = OVERLAY_POSITION (OVERLAY_END (tem)); | |
3b06f880 KH |
3773 | |
3774 | if (end == pos) | |
3775 | { /* This overlay is disordered. */ | |
2410d73a | 3776 | struct Lisp_Overlay *found = tail; |
3b06f880 KH |
3777 | |
3778 | /* Unlink the found overlay. */ | |
2410d73a SM |
3779 | tail = found->next; |
3780 | parent->next = tail; | |
1138e742 KR |
3781 | /* Move an overlay at RIGHT_PLACE to the next of the found one, |
3782 | and link it into the right place. */ | |
2410d73a | 3783 | if (!right_pair) |
1138e742 | 3784 | { |
fd318b54 | 3785 | found->next = bp->overlays_before; |
0c94c8d6 | 3786 | set_buffer_overlays_before (bp, found); |
1138e742 KR |
3787 | } |
3788 | else | |
3789 | { | |
2410d73a SM |
3790 | found->next = right_pair->next; |
3791 | right_pair->next = found; | |
1138e742 | 3792 | } |
3b06f880 KH |
3793 | } |
3794 | else if (end == prev) | |
1138e742 KR |
3795 | { |
3796 | parent = tail; | |
2410d73a | 3797 | tail = tail->next; |
1138e742 | 3798 | } |
3b06f880 KH |
3799 | else /* No more disordered overlay. */ |
3800 | break; | |
3801 | } | |
3802 | } | |
2eec3b4e | 3803 | \f |
52f8ec73 | 3804 | DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0, |
7ee72033 | 3805 | doc: /* Return t if OBJECT is an overlay. */) |
5842a27b | 3806 | (Lisp_Object object) |
52f8ec73 JB |
3807 | { |
3808 | return (OVERLAYP (object) ? Qt : Qnil); | |
3809 | } | |
3810 | ||
acac2700 | 3811 | DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0, |
7ee72033 | 3812 | doc: /* Create a new overlay with range BEG to END in BUFFER. |
018ba359 PJ |
3813 | If omitted, BUFFER defaults to the current buffer. |
3814 | BEG and END may be integers or markers. | |
a625ee20 RS |
3815 | The fourth arg FRONT-ADVANCE, if non-nil, makes the marker |
3816 | for the front of the overlay advance when text is inserted there | |
63af6055 | 3817 | \(which means the text *is not* included in the overlay). |
a625ee20 RS |
3818 | The fifth arg REAR-ADVANCE, if non-nil, makes the marker |
3819 | for the rear of the overlay advance when text is inserted there | |
63af6055 | 3820 | \(which means the text *is* included in the overlay). */) |
5842a27b | 3821 | (Lisp_Object beg, Lisp_Object end, Lisp_Object buffer, Lisp_Object front_advance, Lisp_Object rear_advance) |
2eec3b4e RS |
3822 | { |
3823 | Lisp_Object overlay; | |
5c4f68f1 | 3824 | struct buffer *b; |
2eec3b4e | 3825 | |
5c4f68f1 | 3826 | if (NILP (buffer)) |
67180c6a | 3827 | XSETBUFFER (buffer, current_buffer); |
883047b9 | 3828 | else |
b7826503 | 3829 | CHECK_BUFFER (buffer); |
883047b9 JB |
3830 | if (MARKERP (beg) |
3831 | && ! EQ (Fmarker_buffer (beg), buffer)) | |
3832 | error ("Marker points into wrong buffer"); | |
3833 | if (MARKERP (end) | |
3834 | && ! EQ (Fmarker_buffer (end), buffer)) | |
3835 | error ("Marker points into wrong buffer"); | |
2eec3b4e | 3836 | |
b7826503 PJ |
3837 | CHECK_NUMBER_COERCE_MARKER (beg); |
3838 | CHECK_NUMBER_COERCE_MARKER (end); | |
5c4f68f1 | 3839 | |
883047b9 | 3840 | if (XINT (beg) > XINT (end)) |
5c4f68f1 | 3841 | { |
c99fc30f KH |
3842 | Lisp_Object temp; |
3843 | temp = beg; beg = end; end = temp; | |
5c4f68f1 | 3844 | } |
883047b9 JB |
3845 | |
3846 | b = XBUFFER (buffer); | |
3847 | ||
3848 | beg = Fset_marker (Fmake_marker (), beg, buffer); | |
3849 | end = Fset_marker (Fmake_marker (), end, buffer); | |
5c4f68f1 | 3850 | |
acac2700 RS |
3851 | if (!NILP (front_advance)) |
3852 | XMARKER (beg)->insertion_type = 1; | |
3853 | if (!NILP (rear_advance)) | |
3854 | XMARKER (end)->insertion_type = 1; | |
597dd755 | 3855 | |
d7a7fda3 | 3856 | overlay = build_overlay (beg, end, Qnil); |
2eec3b4e | 3857 | |
177c0ea7 | 3858 | /* Put the new overlay on the wrong list. */ |
2eec3b4e | 3859 | end = OVERLAY_END (overlay); |
c2d5b10f | 3860 | if (OVERLAY_POSITION (end) < b->overlay_center) |
2410d73a | 3861 | { |
fd318b54 DA |
3862 | if (b->overlays_after) |
3863 | XOVERLAY (overlay)->next = b->overlays_after; | |
0c94c8d6 | 3864 | set_buffer_overlays_after (b, XOVERLAY (overlay)); |
2410d73a | 3865 | } |
2eec3b4e | 3866 | else |
2410d73a | 3867 | { |
fd318b54 DA |
3868 | if (b->overlays_before) |
3869 | XOVERLAY (overlay)->next = b->overlays_before; | |
0c94c8d6 | 3870 | set_buffer_overlays_before (b, XOVERLAY (overlay)); |
2410d73a | 3871 | } |
2eec3b4e RS |
3872 | |
3873 | /* This puts it in the right list, and in the right order. */ | |
c2d5b10f | 3874 | recenter_overlay_lists (b, b->overlay_center); |
2eec3b4e | 3875 | |
b61982dd JB |
3876 | /* We don't need to redisplay the region covered by the overlay, because |
3877 | the overlay has no properties at the moment. */ | |
3878 | ||
2eec3b4e RS |
3879 | return overlay; |
3880 | } | |
876aa27c RS |
3881 | \f |
3882 | /* Mark a section of BUF as needing redisplay because of overlays changes. */ | |
3883 | ||
3884 | static void | |
d311d28c | 3885 | modify_overlay (struct buffer *buf, ptrdiff_t start, ptrdiff_t end) |
876aa27c | 3886 | { |
876aa27c RS |
3887 | if (start > end) |
3888 | { | |
d311d28c | 3889 | ptrdiff_t temp = start; |
26f545d7 GM |
3890 | start = end; |
3891 | end = temp; | |
876aa27c RS |
3892 | } |
3893 | ||
481b5054 | 3894 | BUF_COMPUTE_UNCHANGED (buf, start, end); |
177c0ea7 | 3895 | |
876aa27c RS |
3896 | /* If this is a buffer not in the selected window, |
3897 | we must do other windows. */ | |
d3d50620 | 3898 | if (buf != XBUFFER (XWINDOW (selected_window)->buffer)) |
876aa27c | 3899 | windows_or_buffers_changed = 1; |
876aa27c RS |
3900 | /* If multiple windows show this buffer, we must do other windows. */ |
3901 | else if (buffer_shared > 1) | |
3902 | windows_or_buffers_changed = 1; | |
18700091 KS |
3903 | /* If we modify an overlay at the end of the buffer, we cannot |
3904 | be sure that window end is still valid. */ | |
3905 | else if (end >= ZV && start <= ZV) | |
3906 | windows_or_buffers_changed = 1; | |
876aa27c | 3907 | |
d8b9150f | 3908 | ++BUF_OVERLAY_MODIFF (buf); |
876aa27c | 3909 | } |
2eec3b4e | 3910 | |
7b7ae965 DA |
3911 | /* Remove OVERLAY from LIST. */ |
3912 | ||
2410d73a | 3913 | static struct Lisp_Overlay * |
d3da34e0 | 3914 | unchain_overlay (struct Lisp_Overlay *list, struct Lisp_Overlay *overlay) |
2410d73a | 3915 | { |
7b7ae965 DA |
3916 | register struct Lisp_Overlay *tail, **prev = &list; |
3917 | ||
3918 | for (tail = list; tail; prev = &tail->next, tail = *prev) | |
3919 | if (tail == overlay) | |
2410d73a | 3920 | { |
7b7ae965 | 3921 | *prev = overlay->next; |
2410d73a SM |
3922 | overlay->next = NULL; |
3923 | break; | |
3924 | } | |
3925 | return list; | |
3926 | } | |
3927 | ||
4cb3e6b3 DA |
3928 | /* Remove OVERLAY from both overlay lists of B. */ |
3929 | ||
3930 | static void | |
3931 | unchain_both (struct buffer *b, Lisp_Object overlay) | |
3932 | { | |
3933 | struct Lisp_Overlay *ov = XOVERLAY (overlay); | |
3934 | ||
0c94c8d6 PE |
3935 | set_buffer_overlays_before (b, unchain_overlay (b->overlays_before, ov)); |
3936 | set_buffer_overlays_after (b, unchain_overlay (b->overlays_after, ov)); | |
fd318b54 | 3937 | eassert (XOVERLAY (overlay)->next == NULL); |
4cb3e6b3 DA |
3938 | } |
3939 | ||
5c4f68f1 | 3940 | DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0, |
7ee72033 | 3941 | doc: /* Set the endpoints of OVERLAY to BEG and END in BUFFER. |
018ba359 PJ |
3942 | If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now. |
3943 | If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current | |
7ee72033 | 3944 | buffer. */) |
5842a27b | 3945 | (Lisp_Object overlay, Lisp_Object beg, Lisp_Object end, Lisp_Object buffer) |
2eec3b4e | 3946 | { |
471fe23d | 3947 | struct buffer *b, *ob = 0; |
0a4469c9 | 3948 | Lisp_Object obuffer; |
d311d28c | 3949 | ptrdiff_t count = SPECPDL_INDEX (); |
471fe23d | 3950 | ptrdiff_t n_beg, n_end, o_beg IF_LINT (= 0), o_end IF_LINT (= 0); |
5c4f68f1 | 3951 | |
b7826503 | 3952 | CHECK_OVERLAY (overlay); |
5c4f68f1 JB |
3953 | if (NILP (buffer)) |
3954 | buffer = Fmarker_buffer (OVERLAY_START (overlay)); | |
3ece337a | 3955 | if (NILP (buffer)) |
67180c6a | 3956 | XSETBUFFER (buffer, current_buffer); |
b7826503 | 3957 | CHECK_BUFFER (buffer); |
883047b9 | 3958 | |
471fe23d TN |
3959 | if (NILP (Fbuffer_live_p (buffer))) |
3960 | error ("Attempt to move overlay to a dead buffer"); | |
3961 | ||
883047b9 JB |
3962 | if (MARKERP (beg) |
3963 | && ! EQ (Fmarker_buffer (beg), buffer)) | |
3964 | error ("Marker points into wrong buffer"); | |
3965 | if (MARKERP (end) | |
3966 | && ! EQ (Fmarker_buffer (end), buffer)) | |
3967 | error ("Marker points into wrong buffer"); | |
3968 | ||
b7826503 PJ |
3969 | CHECK_NUMBER_COERCE_MARKER (beg); |
3970 | CHECK_NUMBER_COERCE_MARKER (end); | |
d311d28c | 3971 | |
471fe23d | 3972 | if (XINT (beg) > XINT (end)) |
02481186 | 3973 | { |
471fe23d TN |
3974 | Lisp_Object temp; |
3975 | temp = beg; beg = end; end = temp; | |
02481186 PE |
3976 | } |
3977 | ||
9d7608b7 KH |
3978 | specbind (Qinhibit_quit, Qt); |
3979 | ||
0a4469c9 | 3980 | obuffer = Fmarker_buffer (OVERLAY_START (overlay)); |
79cce3f2 | 3981 | b = XBUFFER (buffer); |
471fe23d TN |
3982 | |
3983 | if (!NILP (obuffer)) | |
3984 | { | |
3985 | ob = XBUFFER (obuffer); | |
3986 | ||
3987 | o_beg = OVERLAY_POSITION (OVERLAY_START (overlay)); | |
3988 | o_end = OVERLAY_POSITION (OVERLAY_END (overlay)); | |
3989 | ||
4cb3e6b3 | 3990 | unchain_both (ob, overlay); |
471fe23d TN |
3991 | } |
3992 | ||
3993 | /* Set the overlay boundaries, which may clip them. */ | |
3994 | Fset_marker (OVERLAY_START (overlay), beg, buffer); | |
3995 | Fset_marker (OVERLAY_END (overlay), end, buffer); | |
3996 | ||
3997 | n_beg = marker_position (OVERLAY_START (overlay)); | |
3998 | n_end = marker_position (OVERLAY_END (overlay)); | |
2eec3b4e | 3999 | |
c82ed728 | 4000 | /* If the overlay has changed buffers, do a thorough redisplay. */ |
0a4469c9 | 4001 | if (!EQ (buffer, obuffer)) |
50760c4a RS |
4002 | { |
4003 | /* Redisplay where the overlay was. */ | |
471fe23d TN |
4004 | if (ob) |
4005 | modify_overlay (ob, o_beg, o_end); | |
50760c4a RS |
4006 | |
4007 | /* Redisplay where the overlay is going to be. */ | |
79cce3f2 | 4008 | modify_overlay (b, n_beg, n_end); |
50760c4a | 4009 | } |
c82ed728 JB |
4010 | else |
4011 | /* Redisplay the area the overlay has just left, or just enclosed. */ | |
4012 | { | |
79cce3f2 PE |
4013 | if (o_beg == n_beg) |
4014 | modify_overlay (b, o_end, n_end); | |
4015 | else if (o_end == n_end) | |
4016 | modify_overlay (b, o_beg, n_beg); | |
c82ed728 | 4017 | else |
79cce3f2 | 4018 | modify_overlay (b, min (o_beg, n_beg), max (o_end, n_end)); |
c82ed728 | 4019 | } |
b61982dd | 4020 | |
471fe23d TN |
4021 | /* Delete the overlay if it is empty after clipping and has the |
4022 | evaporate property. */ | |
4023 | if (n_beg == n_end && !NILP (Foverlay_get (overlay, Qevaporate))) | |
4024 | return unbind_to (count, Fdelete_overlay (overlay)); | |
02481186 | 4025 | |
471fe23d TN |
4026 | /* Put the overlay into the new buffer's overlay lists, first on the |
4027 | wrong list. */ | |
4028 | if (n_end < b->overlay_center) | |
2410d73a | 4029 | { |
fd318b54 | 4030 | XOVERLAY (overlay)->next = b->overlays_after; |
0c94c8d6 | 4031 | set_buffer_overlays_after (b, XOVERLAY (overlay)); |
2410d73a | 4032 | } |
2eec3b4e | 4033 | else |
2410d73a | 4034 | { |
fd318b54 | 4035 | XOVERLAY (overlay)->next = b->overlays_before; |
0c94c8d6 | 4036 | set_buffer_overlays_before (b, XOVERLAY (overlay)); |
2410d73a | 4037 | } |
2eec3b4e RS |
4038 | |
4039 | /* This puts it in the right list, and in the right order. */ | |
c2d5b10f | 4040 | recenter_overlay_lists (b, b->overlay_center); |
2eec3b4e | 4041 | |
0a4469c9 | 4042 | return unbind_to (count, overlay); |
2eec3b4e RS |
4043 | } |
4044 | ||
4045 | DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0, | |
7ee72033 | 4046 | doc: /* Delete the overlay OVERLAY from its buffer. */) |
5842a27b | 4047 | (Lisp_Object overlay) |
2eec3b4e | 4048 | { |
0a4469c9 | 4049 | Lisp_Object buffer; |
5c4f68f1 | 4050 | struct buffer *b; |
d311d28c | 4051 | ptrdiff_t count = SPECPDL_INDEX (); |
5c4f68f1 | 4052 | |
b7826503 | 4053 | CHECK_OVERLAY (overlay); |
52f8ec73 | 4054 | |
0a4469c9 RS |
4055 | buffer = Fmarker_buffer (OVERLAY_START (overlay)); |
4056 | if (NILP (buffer)) | |
4057 | return Qnil; | |
4058 | ||
4059 | b = XBUFFER (buffer); | |
0a4469c9 | 4060 | specbind (Qinhibit_quit, Qt); |
177c0ea7 | 4061 | |
4cb3e6b3 | 4062 | unchain_both (b, overlay); |
041a49a6 | 4063 | drop_overlay (b, XOVERLAY (overlay)); |
3ece337a | 4064 | |
e58c389d | 4065 | /* When deleting an overlay with before or after strings, turn off |
26f545d7 GM |
4066 | display optimizations for the affected buffer, on the basis that |
4067 | these strings may contain newlines. This is easier to do than to | |
4068 | check for that situation during redisplay. */ | |
4069 | if (!windows_or_buffers_changed | |
4070 | && (!NILP (Foverlay_get (overlay, Qbefore_string)) | |
4071 | || !NILP (Foverlay_get (overlay, Qafter_string)))) | |
4072 | b->prevent_redisplay_optimizations_p = 1; | |
4073 | ||
0a4469c9 | 4074 | return unbind_to (count, Qnil); |
2eec3b4e | 4075 | } |
c5e28e39 MR |
4076 | |
4077 | DEFUN ("delete-all-overlays", Fdelete_all_overlays, Sdelete_all_overlays, 0, 1, 0, | |
4078 | doc: /* Delete all overlays of BUFFER. | |
4079 | BUFFER omitted or nil means delete all overlays of the current | |
4080 | buffer. */) | |
4081 | (Lisp_Object buffer) | |
4082 | { | |
4083 | register struct buffer *buf; | |
4084 | ||
4085 | if (NILP (buffer)) | |
4086 | buf = current_buffer; | |
4087 | else | |
4088 | { | |
4089 | CHECK_BUFFER (buffer); | |
4090 | buf = XBUFFER (buffer); | |
4091 | } | |
4092 | ||
4093 | delete_all_overlays (buf); | |
26d4541d | 4094 | return Qnil; |
c5e28e39 | 4095 | } |
2eec3b4e | 4096 | \f |
8ebafa8d JB |
4097 | /* Overlay dissection functions. */ |
4098 | ||
a7ca3326 | 4099 | DEFUN ("overlay-start", Foverlay_start, Soverlay_start, 1, 1, 0, |
7ee72033 | 4100 | doc: /* Return the position at which OVERLAY starts. */) |
5842a27b | 4101 | (Lisp_Object overlay) |
8ebafa8d | 4102 | { |
b7826503 | 4103 | CHECK_OVERLAY (overlay); |
8ebafa8d JB |
4104 | |
4105 | return (Fmarker_position (OVERLAY_START (overlay))); | |
4106 | } | |
4107 | ||
a7ca3326 | 4108 | DEFUN ("overlay-end", Foverlay_end, Soverlay_end, 1, 1, 0, |
7ee72033 | 4109 | doc: /* Return the position at which OVERLAY ends. */) |
5842a27b | 4110 | (Lisp_Object overlay) |
8ebafa8d | 4111 | { |
b7826503 | 4112 | CHECK_OVERLAY (overlay); |
8ebafa8d JB |
4113 | |
4114 | return (Fmarker_position (OVERLAY_END (overlay))); | |
4115 | } | |
4116 | ||
4117 | DEFUN ("overlay-buffer", Foverlay_buffer, Soverlay_buffer, 1, 1, 0, | |
563f7128 LK |
4118 | doc: /* Return the buffer OVERLAY belongs to. |
4119 | Return nil if OVERLAY has been deleted. */) | |
5842a27b | 4120 | (Lisp_Object overlay) |
8ebafa8d | 4121 | { |
b7826503 | 4122 | CHECK_OVERLAY (overlay); |
8ebafa8d JB |
4123 | |
4124 | return Fmarker_buffer (OVERLAY_START (overlay)); | |
4125 | } | |
4126 | ||
4127 | DEFUN ("overlay-properties", Foverlay_properties, Soverlay_properties, 1, 1, 0, | |
7ee72033 | 4128 | doc: /* Return a list of the properties on OVERLAY. |
018ba359 | 4129 | This is a copy of OVERLAY's plist; modifying its conses has no effect on |
7ee72033 | 4130 | OVERLAY. */) |
5842a27b | 4131 | (Lisp_Object overlay) |
8ebafa8d | 4132 | { |
b7826503 | 4133 | CHECK_OVERLAY (overlay); |
8ebafa8d | 4134 | |
c644523b | 4135 | return Fcopy_sequence (XOVERLAY (overlay)->plist); |
8ebafa8d JB |
4136 | } |
4137 | ||
4138 | \f | |
2eec3b4e | 4139 | DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0, |
aabc29c8 | 4140 | doc: /* Return a list of the overlays that contain the character at POS. */) |
5842a27b | 4141 | (Lisp_Object pos) |
2eec3b4e | 4142 | { |
b081724f | 4143 | ptrdiff_t len, noverlays; |
2eec3b4e | 4144 | Lisp_Object *overlay_vec; |
2eec3b4e RS |
4145 | Lisp_Object result; |
4146 | ||
b7826503 | 4147 | CHECK_NUMBER_COERCE_MARKER (pos); |
2eec3b4e RS |
4148 | |
4149 | len = 10; | |
a9800ae8 | 4150 | /* We can't use alloca here because overlays_at can call xrealloc. */ |
38182d90 | 4151 | overlay_vec = xmalloc (len * sizeof *overlay_vec); |
2eec3b4e RS |
4152 | |
4153 | /* Put all the overlays we want in a vector in overlay_vec. | |
4154 | Store the length in len. */ | |
2a77a7d7 | 4155 | noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, |
d311d28c | 4156 | 0, 0, 0); |
2eec3b4e RS |
4157 | |
4158 | /* Make a list of them all. */ | |
4159 | result = Flist (noverlays, overlay_vec); | |
4160 | ||
9ac0d9e0 | 4161 | xfree (overlay_vec); |
2eec3b4e RS |
4162 | return result; |
4163 | } | |
4164 | ||
74514898 | 4165 | DEFUN ("overlays-in", Foverlays_in, Soverlays_in, 2, 2, 0, |
7ee72033 | 4166 | doc: /* Return a list of the overlays that overlap the region BEG ... END. |
018ba359 PJ |
4167 | Overlap means that at least one character is contained within the overlay |
4168 | and also contained within the specified region. | |
7723e095 MR |
4169 | Empty overlays are included in the result if they are located at BEG, |
4170 | between BEG and END, or at END provided END denotes the position at the | |
4171 | end of the buffer. */) | |
5842a27b | 4172 | (Lisp_Object beg, Lisp_Object end) |
74514898 | 4173 | { |
21514da7 | 4174 | ptrdiff_t len, noverlays; |
74514898 | 4175 | Lisp_Object *overlay_vec; |
74514898 RS |
4176 | Lisp_Object result; |
4177 | ||
b7826503 PJ |
4178 | CHECK_NUMBER_COERCE_MARKER (beg); |
4179 | CHECK_NUMBER_COERCE_MARKER (end); | |
74514898 RS |
4180 | |
4181 | len = 10; | |
38182d90 | 4182 | overlay_vec = xmalloc (len * sizeof *overlay_vec); |
74514898 RS |
4183 | |
4184 | /* Put all the overlays we want in a vector in overlay_vec. | |
4185 | Store the length in len. */ | |
4186 | noverlays = overlays_in (XINT (beg), XINT (end), 1, &overlay_vec, &len, | |
145582a0 | 4187 | NULL, NULL); |
74514898 RS |
4188 | |
4189 | /* Make a list of them all. */ | |
4190 | result = Flist (noverlays, overlay_vec); | |
4191 | ||
4192 | xfree (overlay_vec); | |
4193 | return result; | |
4194 | } | |
4195 | ||
a7ca3326 | 4196 | DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change, |
efc7e75f | 4197 | 1, 1, 0, |
7ee72033 | 4198 | doc: /* Return the next position after POS where an overlay starts or ends. |
624d2678 RS |
4199 | If there are no overlay boundaries from POS to (point-max), |
4200 | the value is (point-max). */) | |
5842a27b | 4201 | (Lisp_Object pos) |
2eec3b4e | 4202 | { |
b081724f | 4203 | ptrdiff_t i, len, noverlays; |
d311d28c | 4204 | ptrdiff_t endpos; |
2eec3b4e | 4205 | Lisp_Object *overlay_vec; |
2eec3b4e | 4206 | |
b7826503 | 4207 | CHECK_NUMBER_COERCE_MARKER (pos); |
2eec3b4e RS |
4208 | |
4209 | len = 10; | |
38182d90 | 4210 | overlay_vec = xmalloc (len * sizeof *overlay_vec); |
2eec3b4e RS |
4211 | |
4212 | /* Put all the overlays we want in a vector in overlay_vec. | |
4213 | Store the length in len. | |
4214 | endpos gets the position where the next overlay starts. */ | |
2a77a7d7 | 4215 | noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len, |
d311d28c | 4216 | &endpos, 0, 1); |
2eec3b4e RS |
4217 | |
4218 | /* If any of these overlays ends before endpos, | |
4219 | use its ending point instead. */ | |
4220 | for (i = 0; i < noverlays; i++) | |
4221 | { | |
4222 | Lisp_Object oend; | |
d311d28c | 4223 | ptrdiff_t oendpos; |
2eec3b4e RS |
4224 | |
4225 | oend = OVERLAY_END (overlay_vec[i]); | |
4226 | oendpos = OVERLAY_POSITION (oend); | |
4227 | if (oendpos < endpos) | |
4228 | endpos = oendpos; | |
1ab256cb RM |
4229 | } |
4230 | ||
9ac0d9e0 | 4231 | xfree (overlay_vec); |
2eec3b4e RS |
4232 | return make_number (endpos); |
4233 | } | |
239c932b | 4234 | |
a7ca3326 | 4235 | DEFUN ("previous-overlay-change", Fprevious_overlay_change, |
239c932b | 4236 | Sprevious_overlay_change, 1, 1, 0, |
7ee72033 | 4237 | doc: /* Return the previous position before POS where an overlay starts or ends. |
624d2678 RS |
4238 | If there are no overlay boundaries from (point-min) to POS, |
4239 | the value is (point-min). */) | |
5842a27b | 4240 | (Lisp_Object pos) |
239c932b | 4241 | { |
d311d28c | 4242 | ptrdiff_t prevpos; |
239c932b | 4243 | Lisp_Object *overlay_vec; |
b081724f | 4244 | ptrdiff_t len; |
239c932b | 4245 | |
b7826503 | 4246 | CHECK_NUMBER_COERCE_MARKER (pos); |
239c932b | 4247 | |
624bbdc4 RS |
4248 | /* At beginning of buffer, we know the answer; |
4249 | avoid bug subtracting 1 below. */ | |
4250 | if (XINT (pos) == BEGV) | |
4251 | return pos; | |
4252 | ||
017f0539 | 4253 | len = 10; |
38182d90 | 4254 | overlay_vec = xmalloc (len * sizeof *overlay_vec); |
017f0539 | 4255 | |
239c932b RS |
4256 | /* Put all the overlays we want in a vector in overlay_vec. |
4257 | Store the length in len. | |
daa1c109 | 4258 | prevpos gets the position of the previous change. */ |
328ab8e7 | 4259 | overlays_at (XINT (pos), 1, &overlay_vec, &len, |
d311d28c | 4260 | 0, &prevpos, 1); |
239c932b | 4261 | |
239c932b RS |
4262 | xfree (overlay_vec); |
4263 | return make_number (prevpos); | |
4264 | } | |
2eec3b4e RS |
4265 | \f |
4266 | /* These functions are for debugging overlays. */ | |
4267 | ||
4268 | DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0, | |
7ee72033 | 4269 | doc: /* Return a pair of lists giving all the overlays of the current buffer. |
018ba359 PJ |
4270 | The car has all the overlays before the overlay center; |
4271 | the cdr has all the overlays after the overlay center. | |
4272 | Recentering overlays moves overlays between these lists. | |
4273 | The lists you get are copies, so that changing them has no effect. | |
7ee72033 | 4274 | However, the overlays you get are the real objects that the buffer uses. */) |
5842a27b | 4275 | (void) |
2eec3b4e | 4276 | { |
2410d73a SM |
4277 | struct Lisp_Overlay *ol; |
4278 | Lisp_Object before = Qnil, after = Qnil, tmp; | |
4cb3e6b3 | 4279 | |
fd318b54 | 4280 | for (ol = current_buffer->overlays_before; ol; ol = ol->next) |
2410d73a SM |
4281 | { |
4282 | XSETMISC (tmp, ol); | |
4283 | before = Fcons (tmp, before); | |
4284 | } | |
fd318b54 | 4285 | for (ol = current_buffer->overlays_after; ol; ol = ol->next) |
2410d73a SM |
4286 | { |
4287 | XSETMISC (tmp, ol); | |
4288 | after = Fcons (tmp, after); | |
4289 | } | |
4cb3e6b3 | 4290 | |
2410d73a | 4291 | return Fcons (Fnreverse (before), Fnreverse (after)); |
2eec3b4e RS |
4292 | } |
4293 | ||
4294 | DEFUN ("overlay-recenter", Foverlay_recenter, Soverlay_recenter, 1, 1, 0, | |
c87426c5 RS |
4295 | doc: /* Recenter the overlays of the current buffer around position POS. |
4296 | That makes overlay lookup faster for positions near POS (but perhaps slower | |
4297 | for positions far away from POS). */) | |
5842a27b | 4298 | (Lisp_Object pos) |
2eec3b4e | 4299 | { |
d311d28c | 4300 | ptrdiff_t p; |
b7826503 | 4301 | CHECK_NUMBER_COERCE_MARKER (pos); |
2eec3b4e | 4302 | |
d311d28c PE |
4303 | p = clip_to_bounds (PTRDIFF_MIN, XINT (pos), PTRDIFF_MAX); |
4304 | recenter_overlay_lists (current_buffer, p); | |
2eec3b4e RS |
4305 | return Qnil; |
4306 | } | |
4307 | \f | |
a7ca3326 | 4308 | DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0, |
7ee72033 | 4309 | doc: /* Get the property of overlay OVERLAY with property name PROP. */) |
5842a27b | 4310 | (Lisp_Object overlay, Lisp_Object prop) |
2eec3b4e | 4311 | { |
b7826503 | 4312 | CHECK_OVERLAY (overlay); |
c644523b | 4313 | return lookup_char_property (XOVERLAY (overlay)->plist, prop, 0); |
2eec3b4e RS |
4314 | } |
4315 | ||
4316 | DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0, | |
7c301272 LMI |
4317 | doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE. |
4318 | VALUE will be returned.*/) | |
5842a27b | 4319 | (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value) |
2eec3b4e | 4320 | { |
48e2e3ba | 4321 | Lisp_Object tail, buffer; |
37ef52bb | 4322 | bool changed; |
2eec3b4e | 4323 | |
b7826503 | 4324 | CHECK_OVERLAY (overlay); |
b61982dd | 4325 | |
274a9425 RS |
4326 | buffer = Fmarker_buffer (OVERLAY_START (overlay)); |
4327 | ||
c644523b | 4328 | for (tail = XOVERLAY (overlay)->plist; |
7539e11f KR |
4329 | CONSP (tail) && CONSP (XCDR (tail)); |
4330 | tail = XCDR (XCDR (tail))) | |
4331 | if (EQ (XCAR (tail), prop)) | |
274a9425 | 4332 | { |
7539e11f | 4333 | changed = !EQ (XCAR (XCDR (tail)), value); |
f3fbd155 | 4334 | XSETCAR (XCDR (tail), value); |
9d7608b7 | 4335 | goto found; |
274a9425 | 4336 | } |
9d7608b7 KH |
4337 | /* It wasn't in the list, so add it to the front. */ |
4338 | changed = !NILP (value); | |
c644523b DA |
4339 | set_overlay_plist |
4340 | (overlay, Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist))); | |
9d7608b7 KH |
4341 | found: |
4342 | if (! NILP (buffer)) | |
4343 | { | |
4344 | if (changed) | |
876aa27c | 4345 | modify_overlay (XBUFFER (buffer), |
26f545d7 GM |
4346 | marker_position (OVERLAY_START (overlay)), |
4347 | marker_position (OVERLAY_END (overlay))); | |
9d7608b7 KH |
4348 | if (EQ (prop, Qevaporate) && ! NILP (value) |
4349 | && (OVERLAY_POSITION (OVERLAY_START (overlay)) | |
4350 | == OVERLAY_POSITION (OVERLAY_END (overlay)))) | |
4351 | Fdelete_overlay (overlay); | |
4352 | } | |
7d63db98 | 4353 | |
2eec3b4e | 4354 | return value; |
1ab256cb RM |
4355 | } |
4356 | \f | |
9115729e KH |
4357 | /* Subroutine of report_overlay_modification. */ |
4358 | ||
4359 | /* Lisp vector holding overlay hook functions to call. | |
4360 | Vector elements come in pairs. | |
4361 | Each even-index element is a list of hook functions. | |
4362 | The following odd-index element is the overlay they came from. | |
4363 | ||
4364 | Before the buffer change, we fill in this vector | |
4365 | as we call overlay hook functions. | |
4366 | After the buffer change, we get the functions to call from this vector. | |
4367 | This way we always call the same functions before and after the change. */ | |
4368 | static Lisp_Object last_overlay_modification_hooks; | |
4369 | ||
4370 | /* Number of elements actually used in last_overlay_modification_hooks. */ | |
d311d28c | 4371 | static ptrdiff_t last_overlay_modification_hooks_used; |
9115729e KH |
4372 | |
4373 | /* Add one functionlist/overlay pair | |
4374 | to the end of last_overlay_modification_hooks. */ | |
4375 | ||
4376 | static void | |
d3da34e0 | 4377 | add_overlay_mod_hooklist (Lisp_Object functionlist, Lisp_Object overlay) |
9115729e | 4378 | { |
d311d28c | 4379 | ptrdiff_t oldsize = ASIZE (last_overlay_modification_hooks); |
9115729e | 4380 | |
d311d28c PE |
4381 | if (oldsize - 1 <= last_overlay_modification_hooks_used) |
4382 | last_overlay_modification_hooks = | |
4383 | larger_vector (last_overlay_modification_hooks, 2, -1); | |
3ae565b3 SM |
4384 | ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used, |
4385 | functionlist); last_overlay_modification_hooks_used++; | |
4386 | ASET (last_overlay_modification_hooks, last_overlay_modification_hooks_used, | |
4387 | overlay); last_overlay_modification_hooks_used++; | |
9115729e KH |
4388 | } |
4389 | \f | |
173f2a64 RS |
4390 | /* Run the modification-hooks of overlays that include |
4391 | any part of the text in START to END. | |
9115729e KH |
4392 | If this change is an insertion, also |
4393 | run the insert-before-hooks of overlay starting at END, | |
930a9140 RS |
4394 | and the insert-after-hooks of overlay ending at START. |
4395 | ||
4396 | This is called both before and after the modification. | |
37ef52bb | 4397 | AFTER is true when we call after the modification. |
930a9140 | 4398 | |
9115729e KH |
4399 | ARG1, ARG2, ARG3 are arguments to pass to the hook functions. |
4400 | When AFTER is nonzero, they are the start position, | |
4401 | the position after the inserted new text, | |
4402 | and the length of deleted or replaced old text. */ | |
173f2a64 RS |
4403 | |
4404 | void | |
37ef52bb | 4405 | report_overlay_modification (Lisp_Object start, Lisp_Object end, bool after, |
d3da34e0 | 4406 | Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3) |
173f2a64 | 4407 | { |
2410d73a SM |
4408 | Lisp_Object prop, overlay; |
4409 | struct Lisp_Overlay *tail; | |
37ef52bb PE |
4410 | /* True if this change is an insertion. */ |
4411 | bool insertion = (after ? XFASTINT (arg3) == 0 : EQ (start, end)); | |
a615c6dc | 4412 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
55b48893 RS |
4413 | |
4414 | overlay = Qnil; | |
2410d73a | 4415 | tail = NULL; |
9115729e | 4416 | |
27c6b98e SM |
4417 | /* We used to run the functions as soon as we found them and only register |
4418 | them in last_overlay_modification_hooks for the purpose of the `after' | |
4419 | case. But running elisp code as we traverse the list of overlays is | |
4420 | painful because the list can be modified by the elisp code so we had to | |
4421 | copy at several places. We now simply do a read-only traversal that | |
4422 | only collects the functions to run and we run them afterwards. It's | |
4423 | simpler, especially since all the code was already there. -stef */ | |
4424 | ||
a615c6dc | 4425 | if (!after) |
173f2a64 | 4426 | { |
a615c6dc SM |
4427 | /* We are being called before a change. |
4428 | Scan the overlays to find the functions to call. */ | |
4429 | last_overlay_modification_hooks_used = 0; | |
fd318b54 | 4430 | for (tail = current_buffer->overlays_before; tail; tail = tail->next) |
173f2a64 | 4431 | { |
d311d28c | 4432 | ptrdiff_t startpos, endpos; |
a615c6dc SM |
4433 | Lisp_Object ostart, oend; |
4434 | ||
2410d73a | 4435 | XSETMISC (overlay, tail); |
a615c6dc SM |
4436 | |
4437 | ostart = OVERLAY_START (overlay); | |
4438 | oend = OVERLAY_END (overlay); | |
4439 | endpos = OVERLAY_POSITION (oend); | |
4440 | if (XFASTINT (start) > endpos) | |
4441 | break; | |
4442 | startpos = OVERLAY_POSITION (ostart); | |
4443 | if (insertion && (XFASTINT (start) == startpos | |
4444 | || XFASTINT (end) == startpos)) | |
5fb5aa33 | 4445 | { |
a615c6dc SM |
4446 | prop = Foverlay_get (overlay, Qinsert_in_front_hooks); |
4447 | if (!NILP (prop)) | |
4448 | add_overlay_mod_hooklist (prop, overlay); | |
5fb5aa33 | 4449 | } |
a615c6dc SM |
4450 | if (insertion && (XFASTINT (start) == endpos |
4451 | || XFASTINT (end) == endpos)) | |
5fb5aa33 | 4452 | { |
a615c6dc SM |
4453 | prop = Foverlay_get (overlay, Qinsert_behind_hooks); |
4454 | if (!NILP (prop)) | |
4455 | add_overlay_mod_hooklist (prop, overlay); | |
5fb5aa33 | 4456 | } |
a615c6dc SM |
4457 | /* Test for intersecting intervals. This does the right thing |
4458 | for both insertion and deletion. */ | |
4459 | if (XFASTINT (end) > startpos && XFASTINT (start) < endpos) | |
5fb5aa33 | 4460 | { |
a615c6dc SM |
4461 | prop = Foverlay_get (overlay, Qmodification_hooks); |
4462 | if (!NILP (prop)) | |
4463 | add_overlay_mod_hooklist (prop, overlay); | |
5fb5aa33 | 4464 | } |
173f2a64 | 4465 | } |
60ebfdf3 | 4466 | |
fd318b54 | 4467 | for (tail = current_buffer->overlays_after; tail; tail = tail->next) |
173f2a64 | 4468 | { |
d311d28c | 4469 | ptrdiff_t startpos, endpos; |
a615c6dc SM |
4470 | Lisp_Object ostart, oend; |
4471 | ||
2410d73a | 4472 | XSETMISC (overlay, tail); |
a615c6dc SM |
4473 | |
4474 | ostart = OVERLAY_START (overlay); | |
4475 | oend = OVERLAY_END (overlay); | |
4476 | startpos = OVERLAY_POSITION (ostart); | |
4477 | endpos = OVERLAY_POSITION (oend); | |
4478 | if (XFASTINT (end) < startpos) | |
4479 | break; | |
4480 | if (insertion && (XFASTINT (start) == startpos | |
4481 | || XFASTINT (end) == startpos)) | |
5fb5aa33 | 4482 | { |
a615c6dc SM |
4483 | prop = Foverlay_get (overlay, Qinsert_in_front_hooks); |
4484 | if (!NILP (prop)) | |
4485 | add_overlay_mod_hooklist (prop, overlay); | |
5fb5aa33 | 4486 | } |
a615c6dc SM |
4487 | if (insertion && (XFASTINT (start) == endpos |
4488 | || XFASTINT (end) == endpos)) | |
5fb5aa33 | 4489 | { |
a615c6dc SM |
4490 | prop = Foverlay_get (overlay, Qinsert_behind_hooks); |
4491 | if (!NILP (prop)) | |
4492 | add_overlay_mod_hooklist (prop, overlay); | |
5fb5aa33 | 4493 | } |
a615c6dc SM |
4494 | /* Test for intersecting intervals. This does the right thing |
4495 | for both insertion and deletion. */ | |
4496 | if (XFASTINT (end) > startpos && XFASTINT (start) < endpos) | |
5fb5aa33 | 4497 | { |
a615c6dc SM |
4498 | prop = Foverlay_get (overlay, Qmodification_hooks); |
4499 | if (!NILP (prop)) | |
4500 | add_overlay_mod_hooklist (prop, overlay); | |
5fb5aa33 | 4501 | } |
173f2a64 RS |
4502 | } |
4503 | } | |
55b48893 | 4504 | |
a615c6dc SM |
4505 | GCPRO4 (overlay, arg1, arg2, arg3); |
4506 | { | |
4507 | /* Call the functions recorded in last_overlay_modification_hooks. | |
4508 | First copy the vector contents, in case some of these hooks | |
4509 | do subsequent modification of the buffer. */ | |
d311d28c | 4510 | ptrdiff_t size = last_overlay_modification_hooks_used; |
38182d90 | 4511 | Lisp_Object *copy = alloca (size * sizeof *copy); |
d311d28c | 4512 | ptrdiff_t i; |
a615c6dc | 4513 | |
72af86bd | 4514 | memcpy (copy, XVECTOR (last_overlay_modification_hooks)->contents, |
663e2b3f | 4515 | size * word_size); |
a615c6dc SM |
4516 | gcpro1.var = copy; |
4517 | gcpro1.nvars = size; | |
4518 | ||
4519 | for (i = 0; i < size;) | |
4520 | { | |
8f54f30a PE |
4521 | Lisp_Object prop_i, overlay_i; |
4522 | prop_i = copy[i++]; | |
4523 | overlay_i = copy[i++]; | |
4524 | call_overlay_mod_hooks (prop_i, overlay_i, after, arg1, arg2, arg3); | |
a615c6dc SM |
4525 | } |
4526 | } | |
55b48893 | 4527 | UNGCPRO; |
173f2a64 RS |
4528 | } |
4529 | ||
4530 | static void | |
37ef52bb | 4531 | call_overlay_mod_hooks (Lisp_Object list, Lisp_Object overlay, bool after, |
d3da34e0 | 4532 | Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3) |
173f2a64 | 4533 | { |
930a9140 | 4534 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
9115729e | 4535 | |
930a9140 | 4536 | GCPRO4 (list, arg1, arg2, arg3); |
9115729e | 4537 | |
6d70a280 | 4538 | while (CONSP (list)) |
173f2a64 | 4539 | { |
930a9140 | 4540 | if (NILP (arg3)) |
6d70a280 | 4541 | call4 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2); |
930a9140 | 4542 | else |
6d70a280 SM |
4543 | call5 (XCAR (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3); |
4544 | list = XCDR (list); | |
173f2a64 RS |
4545 | } |
4546 | UNGCPRO; | |
4547 | } | |
9d7608b7 KH |
4548 | |
4549 | /* Delete any zero-sized overlays at position POS, if the `evaporate' | |
4550 | property is set. */ | |
4551 | void | |
d311d28c | 4552 | evaporate_overlays (ptrdiff_t pos) |
9d7608b7 | 4553 | { |
2410d73a SM |
4554 | Lisp_Object overlay, hit_list; |
4555 | struct Lisp_Overlay *tail; | |
9d7608b7 KH |
4556 | |
4557 | hit_list = Qnil; | |
c2d5b10f | 4558 | if (pos <= current_buffer->overlay_center) |
fd318b54 | 4559 | for (tail = current_buffer->overlays_before; tail; tail = tail->next) |
9d7608b7 | 4560 | { |
d311d28c | 4561 | ptrdiff_t endpos; |
2410d73a | 4562 | XSETMISC (overlay, tail); |
9d7608b7 KH |
4563 | endpos = OVERLAY_POSITION (OVERLAY_END (overlay)); |
4564 | if (endpos < pos) | |
4565 | break; | |
4566 | if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos | |
c3935f9d | 4567 | && ! NILP (Foverlay_get (overlay, Qevaporate))) |
9d7608b7 KH |
4568 | hit_list = Fcons (overlay, hit_list); |
4569 | } | |
4570 | else | |
fd318b54 | 4571 | for (tail = current_buffer->overlays_after; tail; tail = tail->next) |
9d7608b7 | 4572 | { |
d311d28c | 4573 | ptrdiff_t startpos; |
2410d73a | 4574 | XSETMISC (overlay, tail); |
9d7608b7 KH |
4575 | startpos = OVERLAY_POSITION (OVERLAY_START (overlay)); |
4576 | if (startpos > pos) | |
4577 | break; | |
4578 | if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos | |
c3935f9d | 4579 | && ! NILP (Foverlay_get (overlay, Qevaporate))) |
9d7608b7 KH |
4580 | hit_list = Fcons (overlay, hit_list); |
4581 | } | |
7539e11f KR |
4582 | for (; CONSP (hit_list); hit_list = XCDR (hit_list)) |
4583 | Fdelete_overlay (XCAR (hit_list)); | |
9d7608b7 | 4584 | } |
173f2a64 | 4585 | \f |
54dfdeb0 | 4586 | /* Somebody has tried to store a value with an unacceptable type |
1bf08baf KH |
4587 | in the slot with offset OFFSET. */ |
4588 | ||
0fa3ba92 | 4589 | void |
d3da34e0 | 4590 | buffer_slot_type_mismatch (Lisp_Object newval, int type) |
0fa3ba92 | 4591 | { |
ad97b375 | 4592 | Lisp_Object predicate; |
177c0ea7 | 4593 | |
64e16c3c | 4594 | switch (type) |
0fa3ba92 | 4595 | { |
2de9f71c | 4596 | case_Lisp_Int: predicate = Qintegerp; break; |
ad97b375 SM |
4597 | case Lisp_String: predicate = Qstringp; break; |
4598 | case Lisp_Symbol: predicate = Qsymbolp; break; | |
1088b922 | 4599 | default: emacs_abort (); |
0fa3ba92 JB |
4600 | } |
4601 | ||
ad97b375 | 4602 | wrong_type_argument (predicate, newval); |
0fa3ba92 | 4603 | } |
7c02e886 | 4604 | |
0fa3ba92 | 4605 | \f |
b86af064 GM |
4606 | /*********************************************************************** |
4607 | Allocation with mmap | |
4608 | ***********************************************************************/ | |
4609 | ||
4610 | #ifdef USE_MMAP_FOR_BUFFERS | |
4611 | ||
4612 | #include <sys/types.h> | |
4613 | #include <sys/mman.h> | |
4614 | ||
4615 | #ifndef MAP_ANON | |
4616 | #ifdef MAP_ANONYMOUS | |
4617 | #define MAP_ANON MAP_ANONYMOUS | |
4618 | #else | |
4619 | #define MAP_ANON 0 | |
4620 | #endif | |
4621 | #endif | |
4622 | ||
09dfdf85 GM |
4623 | #ifndef MAP_FAILED |
4624 | #define MAP_FAILED ((void *) -1) | |
4625 | #endif | |
4626 | ||
b86af064 | 4627 | #include <stdio.h> |
b86af064 GM |
4628 | |
4629 | #if MAP_ANON == 0 | |
4630 | #include <fcntl.h> | |
4631 | #endif | |
4632 | ||
4633 | #include "coding.h" | |
4634 | ||
4635 | ||
4636 | /* Memory is allocated in regions which are mapped using mmap(2). | |
4637 | The current implementation lets the system select mapped | |
4638 | addresses; we're not using MAP_FIXED in general, except when | |
4639 | trying to enlarge regions. | |
4640 | ||
4641 | Each mapped region starts with a mmap_region structure, the user | |
4642 | area starts after that structure, aligned to MEM_ALIGN. | |
4643 | ||
4644 | +-----------------------+ | |
4645 | | struct mmap_info + | | |
4646 | | padding | | |
4647 | +-----------------------+ | |
4648 | | user data | | |
4649 | | | | |
4650 | | | | |
4651 | +-----------------------+ */ | |
4652 | ||
4653 | struct mmap_region | |
4654 | { | |
4655 | /* User-specified size. */ | |
4656 | size_t nbytes_specified; | |
177c0ea7 | 4657 | |
b86af064 GM |
4658 | /* Number of bytes mapped */ |
4659 | size_t nbytes_mapped; | |
4660 | ||
4661 | /* Pointer to the location holding the address of the memory | |
4662 | allocated with the mmap'd block. The variable actually points | |
4663 | after this structure. */ | |
261cb4bb | 4664 | void **var; |
b86af064 GM |
4665 | |
4666 | /* Next and previous in list of all mmap'd regions. */ | |
4667 | struct mmap_region *next, *prev; | |
4668 | }; | |
4669 | ||
4670 | /* Doubly-linked list of mmap'd regions. */ | |
4671 | ||
4672 | static struct mmap_region *mmap_regions; | |
4673 | ||
4674 | /* File descriptor for mmap. If we don't have anonymous mapping, | |
4675 | /dev/zero will be opened on it. */ | |
4676 | ||
4677 | static int mmap_fd; | |
4678 | ||
4679 | /* Temporary storage for mmap_set_vars, see there. */ | |
4680 | ||
4681 | static struct mmap_region *mmap_regions_1; | |
4682 | static int mmap_fd_1; | |
4683 | ||
4684 | /* Page size on this system. */ | |
4685 | ||
4686 | static int mmap_page_size; | |
4687 | ||
cd1181db | 4688 | /* 1 means mmap has been initialized. */ |
b86af064 | 4689 | |
37ef52bb | 4690 | static bool mmap_initialized_p; |
b86af064 GM |
4691 | |
4692 | /* Value is X rounded up to the next multiple of N. */ | |
4693 | ||
4694 | #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N)) | |
4695 | ||
4696 | /* Size of mmap_region structure plus padding. */ | |
4697 | ||
4698 | #define MMAP_REGION_STRUCT_SIZE \ | |
4699 | ROUND (sizeof (struct mmap_region), MEM_ALIGN) | |
4700 | ||
4701 | /* Given a pointer P to the start of the user-visible part of a mapped | |
4702 | region, return a pointer to the start of the region. */ | |
4703 | ||
4704 | #define MMAP_REGION(P) \ | |
4705 | ((struct mmap_region *) ((char *) (P) - MMAP_REGION_STRUCT_SIZE)) | |
4706 | ||
4707 | /* Given a pointer P to the start of a mapped region, return a pointer | |
4708 | to the start of the user-visible part of the region. */ | |
4709 | ||
4710 | #define MMAP_USER_AREA(P) \ | |
261cb4bb | 4711 | ((void *) ((char *) (P) + MMAP_REGION_STRUCT_SIZE)) |
b86af064 GM |
4712 | |
4713 | #define MEM_ALIGN sizeof (double) | |
4714 | ||
7273faa1 DL |
4715 | /* Predicate returning true if part of the address range [START .. END] |
4716 | is currently mapped. Used to prevent overwriting an existing | |
08327b22 GM |
4717 | memory mapping. |
4718 | ||
da6062e6 | 4719 | Default is to conservatively assume the address range is occupied by |
08327b22 GM |
4720 | something else. This can be overridden by system configuration |
4721 | files if system-specific means to determine this exists. */ | |
4722 | ||
4723 | #ifndef MMAP_ALLOCATED_P | |
4724 | #define MMAP_ALLOCATED_P(start, end) 1 | |
4725 | #endif | |
4726 | ||
53964682 | 4727 | /* Perform necessary initializations for the use of mmap. */ |
b86af064 | 4728 | |
1dae0f0a AS |
4729 | static void |
4730 | mmap_init (void) | |
4731 | { | |
4732 | #if MAP_ANON == 0 | |
4733 | /* The value of mmap_fd is initially 0 in temacs, and -1 | |
4734 | in a dumped Emacs. */ | |
4735 | if (mmap_fd <= 0) | |
4736 | { | |
4737 | /* No anonymous mmap -- we need the file descriptor. */ | |
4738 | mmap_fd = open ("/dev/zero", O_RDONLY); | |
4739 | if (mmap_fd == -1) | |
4740 | fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno)); | |
4741 | } | |
4742 | #endif /* MAP_ANON == 0 */ | |
4743 | ||
4744 | if (mmap_initialized_p) | |
4745 | return; | |
4746 | mmap_initialized_p = 1; | |
b86af064 | 4747 | |
1dae0f0a AS |
4748 | #if MAP_ANON != 0 |
4749 | mmap_fd = -1; | |
4750 | #endif | |
4751 | ||
4752 | mmap_page_size = getpagesize (); | |
4753 | } | |
b86af064 GM |
4754 | |
4755 | /* Return a region overlapping address range START...END, or null if | |
4756 | none. END is not including, i.e. the last byte in the range | |
4757 | is at END - 1. */ | |
4758 | ||
4759 | static struct mmap_region * | |
261cb4bb | 4760 | mmap_find (void *start, void *end) |
b86af064 GM |
4761 | { |
4762 | struct mmap_region *r; | |
4763 | char *s = (char *) start, *e = (char *) end; | |
177c0ea7 | 4764 | |
b86af064 GM |
4765 | for (r = mmap_regions; r; r = r->next) |
4766 | { | |
4767 | char *rstart = (char *) r; | |
4768 | char *rend = rstart + r->nbytes_mapped; | |
4769 | ||
4770 | if (/* First byte of range, i.e. START, in this region? */ | |
4771 | (s >= rstart && s < rend) | |
4772 | /* Last byte of range, i.e. END - 1, in this region? */ | |
4773 | || (e > rstart && e <= rend) | |
4774 | /* First byte of this region in the range? */ | |
4775 | || (rstart >= s && rstart < e) | |
4776 | /* Last byte of this region in the range? */ | |
4777 | || (rend > s && rend <= e)) | |
4778 | break; | |
4779 | } | |
4780 | ||
4781 | return r; | |
4782 | } | |
4783 | ||
4784 | ||
4785 | /* Unmap a region. P is a pointer to the start of the user-araa of | |
37ef52bb | 4786 | the region. */ |
b86af064 | 4787 | |
37ef52bb | 4788 | static void |
1dae0f0a | 4789 | mmap_free_1 (struct mmap_region *r) |
b86af064 GM |
4790 | { |
4791 | if (r->next) | |
4792 | r->next->prev = r->prev; | |
4793 | if (r->prev) | |
4794 | r->prev->next = r->next; | |
4795 | else | |
4796 | mmap_regions = r->next; | |
177c0ea7 | 4797 | |
261cb4bb | 4798 | if (munmap (r, r->nbytes_mapped) == -1) |
37ef52bb | 4799 | fprintf (stderr, "munmap: %s\n", emacs_strerror (errno)); |
b86af064 GM |
4800 | } |
4801 | ||
4802 | ||
4803 | /* Enlarge region R by NPAGES pages. NPAGES < 0 means shrink R. | |
37ef52bb | 4804 | Value is true if successful. */ |
b86af064 | 4805 | |
37ef52bb | 4806 | static bool |
1dae0f0a | 4807 | mmap_enlarge (struct mmap_region *r, int npages) |
b86af064 GM |
4808 | { |
4809 | char *region_end = (char *) r + r->nbytes_mapped; | |
4810 | size_t nbytes; | |
37ef52bb | 4811 | bool success = 0; |
b86af064 GM |
4812 | |
4813 | if (npages < 0) | |
4814 | { | |
4815 | /* Unmap pages at the end of the region. */ | |
4816 | nbytes = - npages * mmap_page_size; | |
4817 | if (munmap (region_end - nbytes, nbytes) == -1) | |
4818 | fprintf (stderr, "munmap: %s\n", emacs_strerror (errno)); | |
4819 | else | |
4820 | { | |
4821 | r->nbytes_mapped -= nbytes; | |
4822 | success = 1; | |
4823 | } | |
4824 | } | |
4825 | else if (npages > 0) | |
4826 | { | |
b86af064 | 4827 | nbytes = npages * mmap_page_size; |
177c0ea7 | 4828 | |
b86af064 GM |
4829 | /* Try to map additional pages at the end of the region. We |
4830 | cannot do this if the address range is already occupied by | |
4831 | something else because mmap deletes any previous mapping. | |
4832 | I'm not sure this is worth doing, let's see. */ | |
08327b22 | 4833 | if (!MMAP_ALLOCATED_P (region_end, region_end + nbytes)) |
b86af064 | 4834 | { |
261cb4bb | 4835 | void *p; |
177c0ea7 | 4836 | |
b86af064 GM |
4837 | p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE, |
4838 | MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0); | |
4839 | if (p == MAP_FAILED) | |
edaa9aed | 4840 | ; /* fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); */ |
261cb4bb | 4841 | else if (p != region_end) |
b86af064 GM |
4842 | { |
4843 | /* Kernels are free to choose a different address. In | |
4844 | that case, unmap what we've mapped above; we have | |
4845 | no use for it. */ | |
4846 | if (munmap (p, nbytes) == -1) | |
4847 | fprintf (stderr, "munmap: %s\n", emacs_strerror (errno)); | |
4848 | } | |
4849 | else | |
4850 | { | |
4851 | r->nbytes_mapped += nbytes; | |
4852 | success = 1; | |
4853 | } | |
4854 | } | |
4855 | } | |
4856 | ||
4857 | return success; | |
4858 | } | |
4859 | ||
4860 | ||
37ef52bb PE |
4861 | /* Set or reset variables holding references to mapped regions. |
4862 | If not RESTORE_P, set all variables to null. If RESTORE_P, set all | |
4863 | variables to the start of the user-areas of mapped regions. | |
b86af064 GM |
4864 | |
4865 | This function is called from Fdump_emacs to ensure that the dumped | |
4866 | Emacs doesn't contain references to memory that won't be mapped | |
4867 | when Emacs starts. */ | |
4868 | ||
4869 | void | |
37ef52bb | 4870 | mmap_set_vars (bool restore_p) |
b86af064 GM |
4871 | { |
4872 | struct mmap_region *r; | |
4873 | ||
4874 | if (restore_p) | |
4875 | { | |
4876 | mmap_regions = mmap_regions_1; | |
4877 | mmap_fd = mmap_fd_1; | |
4878 | for (r = mmap_regions; r; r = r->next) | |
4879 | *r->var = MMAP_USER_AREA (r); | |
4880 | } | |
4881 | else | |
4882 | { | |
4883 | for (r = mmap_regions; r; r = r->next) | |
4884 | *r->var = NULL; | |
4885 | mmap_regions_1 = mmap_regions; | |
4886 | mmap_regions = NULL; | |
4887 | mmap_fd_1 = mmap_fd; | |
4888 | mmap_fd = -1; | |
4889 | } | |
4890 | } | |
4891 | ||
4892 | ||
4893 | /* Allocate a block of storage large enough to hold NBYTES bytes of | |
4894 | data. A pointer to the data is returned in *VAR. VAR is thus the | |
4895 | address of some variable which will use the data area. | |
4896 | ||
4897 | The allocation of 0 bytes is valid. | |
4898 | ||
4899 | If we can't allocate the necessary memory, set *VAR to null, and | |
4900 | return null. */ | |
4901 | ||
261cb4bb PE |
4902 | static void * |
4903 | mmap_alloc (void **var, size_t nbytes) | |
b86af064 GM |
4904 | { |
4905 | void *p; | |
4906 | size_t map; | |
4907 | ||
4908 | mmap_init (); | |
4909 | ||
4910 | map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, mmap_page_size); | |
4911 | p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
4912 | mmap_fd, 0); | |
177c0ea7 | 4913 | |
b86af064 GM |
4914 | if (p == MAP_FAILED) |
4915 | { | |
4916 | if (errno != ENOMEM) | |
4917 | fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); | |
4918 | p = NULL; | |
4919 | } | |
4920 | else | |
4921 | { | |
4922 | struct mmap_region *r = (struct mmap_region *) p; | |
177c0ea7 | 4923 | |
b86af064 GM |
4924 | r->nbytes_specified = nbytes; |
4925 | r->nbytes_mapped = map; | |
4926 | r->var = var; | |
4927 | r->prev = NULL; | |
4928 | r->next = mmap_regions; | |
4929 | if (r->next) | |
4930 | r->next->prev = r; | |
4931 | mmap_regions = r; | |
177c0ea7 | 4932 | |
b86af064 GM |
4933 | p = MMAP_USER_AREA (p); |
4934 | } | |
177c0ea7 | 4935 | |
b86af064 GM |
4936 | return *var = p; |
4937 | } | |
4938 | ||
4939 | ||
1dae0f0a AS |
4940 | /* Free a block of relocatable storage whose data is pointed to by |
4941 | PTR. Store 0 in *PTR to show there's no block allocated. */ | |
4942 | ||
4943 | static void | |
261cb4bb | 4944 | mmap_free (void **var) |
1dae0f0a AS |
4945 | { |
4946 | mmap_init (); | |
4947 | ||
4948 | if (*var) | |
4949 | { | |
4950 | mmap_free_1 (MMAP_REGION (*var)); | |
4951 | *var = NULL; | |
4952 | } | |
4953 | } | |
4954 | ||
4955 | ||
b86af064 GM |
4956 | /* Given a pointer at address VAR to data allocated with mmap_alloc, |
4957 | resize it to size NBYTES. Change *VAR to reflect the new block, | |
4958 | and return this value. If more memory cannot be allocated, then | |
4959 | leave *VAR unchanged, and return null. */ | |
4960 | ||
261cb4bb PE |
4961 | static void * |
4962 | mmap_realloc (void **var, size_t nbytes) | |
b86af064 | 4963 | { |
261cb4bb | 4964 | void *result; |
177c0ea7 | 4965 | |
b86af064 GM |
4966 | mmap_init (); |
4967 | ||
4968 | if (*var == NULL) | |
4969 | result = mmap_alloc (var, nbytes); | |
177c0ea7 | 4970 | else if (nbytes == 0) |
b86af064 GM |
4971 | { |
4972 | mmap_free (var); | |
4973 | result = mmap_alloc (var, nbytes); | |
4974 | } | |
4975 | else | |
4976 | { | |
4977 | struct mmap_region *r = MMAP_REGION (*var); | |
4978 | size_t room = r->nbytes_mapped - MMAP_REGION_STRUCT_SIZE; | |
177c0ea7 | 4979 | |
b86af064 GM |
4980 | if (room < nbytes) |
4981 | { | |
4982 | /* Must enlarge. */ | |
261cb4bb | 4983 | void *old_ptr = *var; |
b86af064 GM |
4984 | |
4985 | /* Try to map additional pages at the end of the region. | |
4986 | If that fails, allocate a new region, copy data | |
4987 | from the old region, then free it. */ | |
4988 | if (mmap_enlarge (r, (ROUND (nbytes - room, mmap_page_size) | |
4989 | / mmap_page_size))) | |
4990 | { | |
4991 | r->nbytes_specified = nbytes; | |
4992 | *var = result = old_ptr; | |
4993 | } | |
4994 | else if (mmap_alloc (var, nbytes)) | |
4995 | { | |
72af86bd | 4996 | memcpy (*var, old_ptr, r->nbytes_specified); |
b86af064 GM |
4997 | mmap_free_1 (MMAP_REGION (old_ptr)); |
4998 | result = *var; | |
4999 | r = MMAP_REGION (result); | |
5000 | r->nbytes_specified = nbytes; | |
5001 | } | |
5002 | else | |
5003 | { | |
5004 | *var = old_ptr; | |
5005 | result = NULL; | |
5006 | } | |
5007 | } | |
5008 | else if (room - nbytes >= mmap_page_size) | |
5009 | { | |
5010 | /* Shrinking by at least a page. Let's give some | |
6bcdeb8c KR |
5011 | memory back to the system. |
5012 | ||
5013 | The extra parens are to make the division happens first, | |
5014 | on positive values, so we know it will round towards | |
5015 | zero. */ | |
bb63c5c9 | 5016 | mmap_enlarge (r, - ((room - nbytes) / mmap_page_size)); |
b86af064 GM |
5017 | result = *var; |
5018 | r->nbytes_specified = nbytes; | |
5019 | } | |
5020 | else | |
5021 | { | |
5022 | /* Leave it alone. */ | |
5023 | result = *var; | |
5024 | r->nbytes_specified = nbytes; | |
5025 | } | |
5026 | } | |
5027 | ||
5028 | return result; | |
5029 | } | |
5030 | ||
5031 | ||
b86af064 GM |
5032 | #endif /* USE_MMAP_FOR_BUFFERS */ |
5033 | ||
5034 | ||
5035 | \f | |
5036 | /*********************************************************************** | |
5037 | Buffer-text Allocation | |
5038 | ***********************************************************************/ | |
5039 | ||
b86af064 GM |
5040 | /* Allocate NBYTES bytes for buffer B's text buffer. */ |
5041 | ||
5042 | static void | |
fd05c7e9 | 5043 | alloc_buffer_text (struct buffer *b, ptrdiff_t nbytes) |
b86af064 | 5044 | { |
261cb4bb | 5045 | void *p; |
177c0ea7 | 5046 | |
b86af064 GM |
5047 | BLOCK_INPUT; |
5048 | #if defined USE_MMAP_FOR_BUFFERS | |
261cb4bb | 5049 | p = mmap_alloc ((void **) &b->text->beg, nbytes); |
b86af064 | 5050 | #elif defined REL_ALLOC |
261cb4bb | 5051 | p = r_alloc ((void **) &b->text->beg, nbytes); |
b86af064 | 5052 | #else |
815add84 | 5053 | p = xmalloc (nbytes); |
b86af064 | 5054 | #endif |
177c0ea7 | 5055 | |
b86af064 GM |
5056 | if (p == NULL) |
5057 | { | |
5058 | UNBLOCK_INPUT; | |
531b0165 | 5059 | memory_full (nbytes); |
b86af064 GM |
5060 | } |
5061 | ||
5062 | b->text->beg = (unsigned char *) p; | |
5063 | UNBLOCK_INPUT; | |
5064 | } | |
5065 | ||
5066 | /* Enlarge buffer B's text buffer by DELTA bytes. DELTA < 0 means | |
5067 | shrink it. */ | |
5068 | ||
5069 | void | |
d311d28c | 5070 | enlarge_buffer_text (struct buffer *b, ptrdiff_t delta) |
b86af064 | 5071 | { |
261cb4bb | 5072 | void *p; |
fd05c7e9 PE |
5073 | ptrdiff_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1 |
5074 | + delta); | |
b86af064 GM |
5075 | BLOCK_INPUT; |
5076 | #if defined USE_MMAP_FOR_BUFFERS | |
261cb4bb | 5077 | p = mmap_realloc ((void **) &b->text->beg, nbytes); |
b86af064 | 5078 | #elif defined REL_ALLOC |
261cb4bb | 5079 | p = r_re_alloc ((void **) &b->text->beg, nbytes); |
b86af064 GM |
5080 | #else |
5081 | p = xrealloc (b->text->beg, nbytes); | |
5082 | #endif | |
177c0ea7 | 5083 | |
b86af064 GM |
5084 | if (p == NULL) |
5085 | { | |
5086 | UNBLOCK_INPUT; | |
531b0165 | 5087 | memory_full (nbytes); |
b86af064 GM |
5088 | } |
5089 | ||
5090 | BUF_BEG_ADDR (b) = (unsigned char *) p; | |
5091 | UNBLOCK_INPUT; | |
5092 | } | |
5093 | ||
5094 | ||
5095 | /* Free buffer B's text buffer. */ | |
5096 | ||
5097 | static void | |
d3da34e0 | 5098 | free_buffer_text (struct buffer *b) |
b86af064 GM |
5099 | { |
5100 | BLOCK_INPUT; | |
5101 | ||
5102 | #if defined USE_MMAP_FOR_BUFFERS | |
261cb4bb | 5103 | mmap_free ((void **) &b->text->beg); |
b86af064 | 5104 | #elif defined REL_ALLOC |
261cb4bb | 5105 | r_alloc_free ((void **) &b->text->beg); |
b86af064 GM |
5106 | #else |
5107 | xfree (b->text->beg); | |
5108 | #endif | |
177c0ea7 | 5109 | |
b86af064 GM |
5110 | BUF_BEG_ADDR (b) = NULL; |
5111 | UNBLOCK_INPUT; | |
5112 | } | |
5113 | ||
5114 | ||
5115 | \f | |
5116 | /*********************************************************************** | |
5117 | Initialization | |
5118 | ***********************************************************************/ | |
5119 | ||
dfcf069d | 5120 | void |
d3da34e0 | 5121 | init_buffer_once (void) |
1ab256cb | 5122 | { |
7c02e886 | 5123 | int idx; |
3884d954 DA |
5124 | /* If you add, remove, or reorder Lisp_Objects in a struct buffer, make |
5125 | sure that this is still correct. Otherwise, mark_vectorlike may not | |
5126 | trace all Lisp_Objects in buffer_defaults and buffer_local_symbols. */ | |
38182d90 | 5127 | const int pvecsize |
663e2b3f | 5128 | = (offsetof (struct buffer, own_text) - header_size) / word_size; |
7c02e886 | 5129 | |
72af86bd | 5130 | memset (buffer_permanent_local_flags, 0, sizeof buffer_permanent_local_flags); |
13de9290 | 5131 | |
1ab256cb RM |
5132 | /* Make sure all markable slots in buffer_defaults |
5133 | are initialized reasonably, so mark_buffer won't choke. */ | |
5134 | reset_buffer (&buffer_defaults); | |
4b4deea2 | 5135 | eassert (EQ (BVAR (&buffer_defaults, name), make_number (0))); |
13de9290 | 5136 | reset_buffer_local_variables (&buffer_defaults, 1); |
4b4deea2 | 5137 | eassert (EQ (BVAR (&buffer_local_symbols, name), make_number (0))); |
1ab256cb | 5138 | reset_buffer (&buffer_local_symbols); |
13de9290 | 5139 | reset_buffer_local_variables (&buffer_local_symbols, 1); |
336cd056 RS |
5140 | /* Prevent GC from getting confused. */ |
5141 | buffer_defaults.text = &buffer_defaults.own_text; | |
5142 | buffer_local_symbols.text = &buffer_local_symbols.own_text; | |
04e9897c DA |
5143 | /* No one will share the text with these buffers, but let's play it safe. */ |
5144 | buffer_defaults.indirections = 0; | |
5145 | buffer_local_symbols.indirections = 0; | |
0c94c8d6 PE |
5146 | set_buffer_intervals (&buffer_defaults, NULL); |
5147 | set_buffer_intervals (&buffer_local_symbols, NULL); | |
3884d954 | 5148 | XSETPVECTYPESIZE (&buffer_defaults, PVEC_BUFFER, pvecsize); |
67180c6a | 5149 | XSETBUFFER (Vbuffer_defaults, &buffer_defaults); |
3884d954 | 5150 | XSETPVECTYPESIZE (&buffer_local_symbols, PVEC_BUFFER, pvecsize); |
67180c6a | 5151 | XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols); |
1ab256cb RM |
5152 | |
5153 | /* Set up the default values of various buffer slots. */ | |
5154 | /* Must do these before making the first buffer! */ | |
5155 | ||
f532dca0 | 5156 | /* real setup is done in bindings.el */ |
39eb03f1 PE |
5157 | bset_mode_line_format (&buffer_defaults, build_pure_c_string ("%-")); |
5158 | bset_header_line_format (&buffer_defaults, Qnil); | |
5159 | bset_abbrev_mode (&buffer_defaults, Qnil); | |
5160 | bset_overwrite_mode (&buffer_defaults, Qnil); | |
5161 | bset_case_fold_search (&buffer_defaults, Qt); | |
5162 | bset_auto_fill_function (&buffer_defaults, Qnil); | |
5163 | bset_selective_display (&buffer_defaults, Qnil); | |
5164 | bset_selective_display_ellipses (&buffer_defaults, Qt); | |
5165 | bset_abbrev_table (&buffer_defaults, Qnil); | |
5166 | bset_display_table (&buffer_defaults, Qnil); | |
5167 | bset_undo_list (&buffer_defaults, Qnil); | |
5168 | bset_mark_active (&buffer_defaults, Qnil); | |
5169 | bset_file_format (&buffer_defaults, Qnil); | |
5170 | bset_auto_save_file_format (&buffer_defaults, Qt); | |
0c94c8d6 PE |
5171 | set_buffer_overlays_before (&buffer_defaults, NULL); |
5172 | set_buffer_overlays_after (&buffer_defaults, NULL); | |
c2d5b10f | 5173 | buffer_defaults.overlay_center = BEG; |
1ab256cb | 5174 | |
4b4deea2 | 5175 | XSETFASTINT (BVAR (&buffer_defaults, tab_width), 8); |
39eb03f1 PE |
5176 | bset_truncate_lines (&buffer_defaults, Qnil); |
5177 | bset_word_wrap (&buffer_defaults, Qnil); | |
5178 | bset_ctl_arrow (&buffer_defaults, Qt); | |
5179 | bset_bidi_display_reordering (&buffer_defaults, Qt); | |
5180 | bset_bidi_paragraph_direction (&buffer_defaults, Qnil); | |
5181 | bset_cursor_type (&buffer_defaults, Qt); | |
5182 | bset_extra_line_spacing (&buffer_defaults, Qnil); | |
5183 | bset_cursor_in_non_selected_windows (&buffer_defaults, Qt); | |
5184 | ||
5185 | bset_enable_multibyte_characters (&buffer_defaults, Qt); | |
5186 | bset_buffer_file_coding_system (&buffer_defaults, Qnil); | |
4b4deea2 TT |
5187 | XSETFASTINT (BVAR (&buffer_defaults, fill_column), 70); |
5188 | XSETFASTINT (BVAR (&buffer_defaults, left_margin), 0); | |
39eb03f1 PE |
5189 | bset_cache_long_line_scans (&buffer_defaults, Qnil); |
5190 | bset_file_truename (&buffer_defaults, Qnil); | |
4b4deea2 TT |
5191 | XSETFASTINT (BVAR (&buffer_defaults, display_count), 0); |
5192 | XSETFASTINT (BVAR (&buffer_defaults, left_margin_cols), 0); | |
5193 | XSETFASTINT (BVAR (&buffer_defaults, right_margin_cols), 0); | |
39eb03f1 PE |
5194 | bset_left_fringe_width (&buffer_defaults, Qnil); |
5195 | bset_right_fringe_width (&buffer_defaults, Qnil); | |
5196 | bset_fringes_outside_margins (&buffer_defaults, Qnil); | |
5197 | bset_scroll_bar_width (&buffer_defaults, Qnil); | |
5198 | bset_vertical_scroll_bar_type (&buffer_defaults, Qt); | |
5199 | bset_indicate_empty_lines (&buffer_defaults, Qnil); | |
5200 | bset_indicate_buffer_boundaries (&buffer_defaults, Qnil); | |
5201 | bset_fringe_indicator_alist (&buffer_defaults, Qnil); | |
5202 | bset_fringe_cursor_alist (&buffer_defaults, Qnil); | |
5203 | bset_scroll_up_aggressively (&buffer_defaults, Qnil); | |
5204 | bset_scroll_down_aggressively (&buffer_defaults, Qnil); | |
5205 | bset_display_time (&buffer_defaults, Qnil); | |
1ab256cb RM |
5206 | |
5207 | /* Assign the local-flags to the slots that have default values. | |
5208 | The local flag is a bit that is used in the buffer | |
5209 | to say that it has its own local value for the slot. | |
5210 | The local flag bits are in the local_var_flags slot of the buffer. */ | |
5211 | ||
5212 | /* Nothing can work if this isn't true */ | |
663e2b3f | 5213 | { verify (sizeof (EMACS_INT) == word_size); } |
1ab256cb RM |
5214 | |
5215 | /* 0 means not a lisp var, -1 means always local, else mask */ | |
72af86bd | 5216 | memset (&buffer_local_flags, 0, sizeof buffer_local_flags); |
39eb03f1 PE |
5217 | bset_filename (&buffer_local_flags, make_number (-1)); |
5218 | bset_directory (&buffer_local_flags, make_number (-1)); | |
5219 | bset_backed_up (&buffer_local_flags, make_number (-1)); | |
5220 | bset_save_length (&buffer_local_flags, make_number (-1)); | |
5221 | bset_auto_save_file_name (&buffer_local_flags, make_number (-1)); | |
5222 | bset_read_only (&buffer_local_flags, make_number (-1)); | |
5223 | bset_major_mode (&buffer_local_flags, make_number (-1)); | |
5224 | bset_mode_name (&buffer_local_flags, make_number (-1)); | |
5225 | bset_undo_list (&buffer_local_flags, make_number (-1)); | |
5226 | bset_mark_active (&buffer_local_flags, make_number (-1)); | |
5227 | bset_point_before_scroll (&buffer_local_flags, make_number (-1)); | |
5228 | bset_file_truename (&buffer_local_flags, make_number (-1)); | |
5229 | bset_invisibility_spec (&buffer_local_flags, make_number (-1)); | |
5230 | bset_file_format (&buffer_local_flags, make_number (-1)); | |
5231 | bset_auto_save_file_format (&buffer_local_flags, make_number (-1)); | |
5232 | bset_display_count (&buffer_local_flags, make_number (-1)); | |
5233 | bset_display_time (&buffer_local_flags, make_number (-1)); | |
5234 | bset_enable_multibyte_characters (&buffer_local_flags, make_number (-1)); | |
8d7a4592 | 5235 | |
7c02e886 | 5236 | idx = 1; |
4b4deea2 TT |
5237 | XSETFASTINT (BVAR (&buffer_local_flags, mode_line_format), idx); ++idx; |
5238 | XSETFASTINT (BVAR (&buffer_local_flags, abbrev_mode), idx); ++idx; | |
5239 | XSETFASTINT (BVAR (&buffer_local_flags, overwrite_mode), idx); ++idx; | |
5240 | XSETFASTINT (BVAR (&buffer_local_flags, case_fold_search), idx); ++idx; | |
5241 | XSETFASTINT (BVAR (&buffer_local_flags, auto_fill_function), idx); ++idx; | |
5242 | XSETFASTINT (BVAR (&buffer_local_flags, selective_display), idx); ++idx; | |
4b4deea2 | 5243 | XSETFASTINT (BVAR (&buffer_local_flags, selective_display_ellipses), idx); ++idx; |
4b4deea2 TT |
5244 | XSETFASTINT (BVAR (&buffer_local_flags, tab_width), idx); ++idx; |
5245 | XSETFASTINT (BVAR (&buffer_local_flags, truncate_lines), idx); ++idx; | |
5246 | XSETFASTINT (BVAR (&buffer_local_flags, word_wrap), idx); ++idx; | |
5247 | XSETFASTINT (BVAR (&buffer_local_flags, ctl_arrow), idx); ++idx; | |
5248 | XSETFASTINT (BVAR (&buffer_local_flags, fill_column), idx); ++idx; | |
5249 | XSETFASTINT (BVAR (&buffer_local_flags, left_margin), idx); ++idx; | |
5250 | XSETFASTINT (BVAR (&buffer_local_flags, abbrev_table), idx); ++idx; | |
5251 | XSETFASTINT (BVAR (&buffer_local_flags, display_table), idx); ++idx; | |
4b4deea2 TT |
5252 | XSETFASTINT (BVAR (&buffer_local_flags, syntax_table), idx); ++idx; |
5253 | XSETFASTINT (BVAR (&buffer_local_flags, cache_long_line_scans), idx); ++idx; | |
5254 | XSETFASTINT (BVAR (&buffer_local_flags, category_table), idx); ++idx; | |
5255 | XSETFASTINT (BVAR (&buffer_local_flags, bidi_display_reordering), idx); ++idx; | |
5256 | XSETFASTINT (BVAR (&buffer_local_flags, bidi_paragraph_direction), idx); ++idx; | |
5257 | XSETFASTINT (BVAR (&buffer_local_flags, buffer_file_coding_system), idx); | |
a1a17b61 | 5258 | /* Make this one a permanent local. */ |
7c02e886 | 5259 | buffer_permanent_local_flags[idx++] = 1; |
4b4deea2 TT |
5260 | XSETFASTINT (BVAR (&buffer_local_flags, left_margin_cols), idx); ++idx; |
5261 | XSETFASTINT (BVAR (&buffer_local_flags, right_margin_cols), idx); ++idx; | |
5262 | XSETFASTINT (BVAR (&buffer_local_flags, left_fringe_width), idx); ++idx; | |
5263 | XSETFASTINT (BVAR (&buffer_local_flags, right_fringe_width), idx); ++idx; | |
5264 | XSETFASTINT (BVAR (&buffer_local_flags, fringes_outside_margins), idx); ++idx; | |
5265 | XSETFASTINT (BVAR (&buffer_local_flags, scroll_bar_width), idx); ++idx; | |
5266 | XSETFASTINT (BVAR (&buffer_local_flags, vertical_scroll_bar_type), idx); ++idx; | |
5267 | XSETFASTINT (BVAR (&buffer_local_flags, indicate_empty_lines), idx); ++idx; | |
5268 | XSETFASTINT (BVAR (&buffer_local_flags, indicate_buffer_boundaries), idx); ++idx; | |
5269 | XSETFASTINT (BVAR (&buffer_local_flags, fringe_indicator_alist), idx); ++idx; | |
5270 | XSETFASTINT (BVAR (&buffer_local_flags, fringe_cursor_alist), idx); ++idx; | |
5271 | XSETFASTINT (BVAR (&buffer_local_flags, scroll_up_aggressively), idx); ++idx; | |
5272 | XSETFASTINT (BVAR (&buffer_local_flags, scroll_down_aggressively), idx); ++idx; | |
5273 | XSETFASTINT (BVAR (&buffer_local_flags, header_line_format), idx); ++idx; | |
5274 | XSETFASTINT (BVAR (&buffer_local_flags, cursor_type), idx); ++idx; | |
5275 | XSETFASTINT (BVAR (&buffer_local_flags, extra_line_spacing), idx); ++idx; | |
5276 | XSETFASTINT (BVAR (&buffer_local_flags, cursor_in_non_selected_windows), idx); ++idx; | |
7c02e886 GM |
5277 | |
5278 | /* Need more room? */ | |
7313acd0 | 5279 | if (idx >= MAX_PER_BUFFER_VARS) |
1088b922 | 5280 | emacs_abort (); |
7313acd0 | 5281 | last_per_buffer_idx = idx; |
177c0ea7 | 5282 | |
1ab256cb RM |
5283 | Vbuffer_alist = Qnil; |
5284 | current_buffer = 0; | |
5285 | all_buffers = 0; | |
5286 | ||
2a0213a6 | 5287 | QSFundamental = build_pure_c_string ("Fundamental"); |
1ab256cb | 5288 | |
d67b4f80 | 5289 | Qfundamental_mode = intern_c_string ("fundamental-mode"); |
39eb03f1 | 5290 | bset_major_mode (&buffer_defaults, Qfundamental_mode); |
1ab256cb | 5291 | |
d67b4f80 | 5292 | Qmode_class = intern_c_string ("mode-class"); |
1ab256cb | 5293 | |
d67b4f80 | 5294 | Qprotected_field = intern_c_string ("protected-field"); |
1ab256cb | 5295 | |
d67b4f80 | 5296 | Qpermanent_local = intern_c_string ("permanent-local"); |
1ab256cb | 5297 | |
d67b4f80 | 5298 | Qkill_buffer_hook = intern_c_string ("kill-buffer-hook"); |
fd6cfe11 | 5299 | Fput (Qkill_buffer_hook, Qpermanent_local, Qt); |
1ab256cb | 5300 | |
1ab256cb | 5301 | /* super-magic invisible buffer */ |
2a0213a6 | 5302 | Vprin1_to_string_buffer = Fget_buffer_create (build_pure_c_string (" prin1")); |
1ab256cb RM |
5303 | Vbuffer_alist = Qnil; |
5304 | ||
2a0213a6 | 5305 | Fset_buffer (Fget_buffer_create (build_pure_c_string ("*scratch*"))); |
7775635d KH |
5306 | |
5307 | inhibit_modification_hooks = 0; | |
1ab256cb RM |
5308 | } |
5309 | ||
dfcf069d | 5310 | void |
d3da34e0 | 5311 | init_buffer (void) |
1ab256cb | 5312 | { |
2381d133 | 5313 | char *pwd; |
136351b7 | 5314 | Lisp_Object temp; |
965d34eb | 5315 | ptrdiff_t len; |
1ab256cb | 5316 | |
b86af064 | 5317 | #ifdef USE_MMAP_FOR_BUFFERS |
93c27ef1 GM |
5318 | { |
5319 | /* When using the ralloc implementation based on mmap(2), buffer | |
5320 | text pointers will have been set to null in the dumped Emacs. | |
5321 | Map new memory. */ | |
5322 | struct buffer *b; | |
177c0ea7 | 5323 | |
52b852c7 | 5324 | FOR_EACH_BUFFER (b) |
93c27ef1 | 5325 | if (b->text->beg == NULL) |
b86af064 | 5326 | enlarge_buffer_text (b, 0); |
93c27ef1 | 5327 | } |
b86af064 | 5328 | #endif /* USE_MMAP_FOR_BUFFERS */ |
177c0ea7 | 5329 | |
1ab256cb | 5330 | Fset_buffer (Fget_buffer_create (build_string ("*scratch*"))); |
4b4deea2 | 5331 | if (NILP (BVAR (&buffer_defaults, enable_multibyte_characters))) |
3d871c85 | 5332 | Fset_buffer_multibyte (Qnil); |
2381d133 | 5333 | |
01537133 | 5334 | pwd = get_current_dir_name (); |
a17b5ed1 | 5335 | |
156bdb41 | 5336 | if (!pwd) |
a17b5ed1 | 5337 | fatal ("`get_current_dir_name' failed: %s\n", strerror (errno)); |
1ab256cb | 5338 | |
1ab256cb RM |
5339 | /* Maybe this should really use some standard subroutine |
5340 | whose definition is filename syntax dependent. */ | |
b639c9be RF |
5341 | len = strlen (pwd); |
5342 | if (!(IS_DIRECTORY_SEP (pwd[len - 1]))) | |
f7975d07 | 5343 | { |
156bdb41 | 5344 | /* Grow buffer to add directory separator and '\0'. */ |
38182d90 | 5345 | pwd = realloc (pwd, len + 2); |
8b146312 AS |
5346 | if (!pwd) |
5347 | fatal ("`get_current_dir_name' failed: %s\n", strerror (errno)); | |
b639c9be RF |
5348 | pwd[len] = DIRECTORY_SEP; |
5349 | pwd[len + 1] = '\0'; | |
cb1caeaf | 5350 | len++; |
f7975d07 | 5351 | } |
0995fa35 | 5352 | |
39eb03f1 | 5353 | bset_directory (current_buffer, make_unibyte_string (pwd, len)); |
4b4deea2 | 5354 | if (! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))) |
f9962371 | 5355 | /* At this moment, we still don't know how to decode the |
156bdb41 | 5356 | directory name. So, we keep the bytes in multibyte form so |
dcd74c5f | 5357 | that ENCODE_FILE correctly gets the original bytes. */ |
39eb03f1 PE |
5358 | bset_directory |
5359 | (current_buffer, string_to_multibyte (BVAR (current_buffer, directory))); | |
136351b7 | 5360 | |
0995fa35 RS |
5361 | /* Add /: to the front of the name |
5362 | if it would otherwise be treated as magic. */ | |
4b4deea2 | 5363 | temp = Ffind_file_name_handler (BVAR (current_buffer, directory), Qt); |
81ab2e07 KH |
5364 | if (! NILP (temp) |
5365 | /* If the default dir is just /, TEMP is non-nil | |
5366 | because of the ange-ftp completion handler. | |
5367 | However, it is not necessary to turn / into /:/. | |
5368 | So avoid doing that. */ | |
4b4deea2 | 5369 | && strcmp ("/", SSDATA (BVAR (current_buffer, directory)))) |
39eb03f1 PE |
5370 | bset_directory |
5371 | (current_buffer, | |
5372 | concat2 (build_string ("/:"), BVAR (current_buffer, directory))); | |
0995fa35 | 5373 | |
136351b7 | 5374 | temp = get_minibuffer (0); |
39eb03f1 | 5375 | bset_directory (XBUFFER (temp), BVAR (current_buffer, directory)); |
01537133 EZ |
5376 | |
5377 | free (pwd); | |
1ab256cb RM |
5378 | } |
5379 | ||
d6aa1876 SM |
5380 | /* Similar to defvar_lisp but define a variable whose value is the Lisp |
5381 | Object stored in the current buffer. address is the address of the slot | |
5382 | in the buffer that is current now. */ | |
5383 | ||
5384 | /* TYPE is nil for a general Lisp variable. | |
ce5b453a | 5385 | An integer specifies a type; then only Lisp values |
d6aa1876 | 5386 | with that type code are allowed (except that nil is allowed too). |
ce5b453a | 5387 | LNAME is the Lisp-level variable name. |
d6aa1876 SM |
5388 | VNAME is the name of the buffer slot. |
5389 | DOC is a dummy where you write the doc string as a comment. */ | |
ce5b453a SM |
5390 | #define DEFVAR_PER_BUFFER(lname, vname, type, doc) \ |
5391 | do { \ | |
5392 | static struct Lisp_Buffer_Objfwd bo_fwd; \ | |
6b1f9ba4 | 5393 | defvar_per_buffer (&bo_fwd, lname, vname, type); \ |
ce5b453a | 5394 | } while (0) |
d6aa1876 SM |
5395 | |
5396 | static void | |
8ea90aa3 | 5397 | defvar_per_buffer (struct Lisp_Buffer_Objfwd *bo_fwd, const char *namestring, |
6b1f9ba4 | 5398 | Lisp_Object *address, Lisp_Object type) |
d6aa1876 | 5399 | { |
ce5b453a | 5400 | struct Lisp_Symbol *sym; |
d6aa1876 SM |
5401 | int offset; |
5402 | ||
ce5b453a | 5403 | sym = XSYMBOL (intern (namestring)); |
d6aa1876 SM |
5404 | offset = (char *)address - (char *)current_buffer; |
5405 | ||
ce5b453a SM |
5406 | bo_fwd->type = Lisp_Fwd_Buffer_Obj; |
5407 | bo_fwd->offset = offset; | |
5408 | bo_fwd->slottype = type; | |
b9598260 | 5409 | sym->declared_special = 1; |
ce5b453a SM |
5410 | sym->redirect = SYMBOL_FORWARDED; |
5411 | { | |
5412 | /* I tried to do the job without a cast, but it seems impossible. | |
5413 | union Lisp_Fwd *fwd; &(fwd->u_buffer_objfwd) = bo_fwd; */ | |
5414 | SET_SYMBOL_FWD (sym, (union Lisp_Fwd *)bo_fwd); | |
5415 | } | |
5416 | XSETSYMBOL (PER_BUFFER_SYMBOL (offset), sym); | |
d6aa1876 SM |
5417 | |
5418 | if (PER_BUFFER_IDX (offset) == 0) | |
5419 | /* Did a DEFVAR_PER_BUFFER without initializing the corresponding | |
5420 | slot of buffer_local_flags */ | |
1088b922 | 5421 | emacs_abort (); |
d6aa1876 SM |
5422 | } |
5423 | ||
5424 | ||
1ab256cb | 5425 | /* initialize the buffer routines */ |
dfcf069d | 5426 | void |
d3da34e0 | 5427 | syms_of_buffer (void) |
1ab256cb | 5428 | { |
9115729e KH |
5429 | staticpro (&last_overlay_modification_hooks); |
5430 | last_overlay_modification_hooks | |
5431 | = Fmake_vector (make_number (10), Qnil); | |
5432 | ||
1ab256cb RM |
5433 | staticpro (&Vbuffer_defaults); |
5434 | staticpro (&Vbuffer_local_symbols); | |
5435 | staticpro (&Qfundamental_mode); | |
5436 | staticpro (&Qmode_class); | |
5437 | staticpro (&QSFundamental); | |
5438 | staticpro (&Vbuffer_alist); | |
5439 | staticpro (&Qprotected_field); | |
5440 | staticpro (&Qpermanent_local); | |
5441 | staticpro (&Qkill_buffer_hook); | |
cd3520a4 JB |
5442 | |
5443 | DEFSYM (Qpermanent_local_hook, "permanent-local-hook"); | |
5444 | DEFSYM (Qoverlayp, "overlayp"); | |
5445 | DEFSYM (Qevaporate, "evaporate"); | |
5446 | DEFSYM (Qmodification_hooks, "modification-hooks"); | |
5447 | DEFSYM (Qinsert_in_front_hooks, "insert-in-front-hooks"); | |
5448 | DEFSYM (Qinsert_behind_hooks, "insert-behind-hooks"); | |
5449 | DEFSYM (Qget_file_buffer, "get-file-buffer"); | |
5450 | DEFSYM (Qpriority, "priority"); | |
5451 | DEFSYM (Qbefore_string, "before-string"); | |
5452 | DEFSYM (Qafter_string, "after-string"); | |
5453 | DEFSYM (Qfirst_change_hook, "first-change-hook"); | |
5454 | DEFSYM (Qbefore_change_functions, "before-change-functions"); | |
5455 | DEFSYM (Qafter_change_functions, "after-change-functions"); | |
5456 | DEFSYM (Qkill_buffer_query_functions, "kill-buffer-query-functions"); | |
5457 | ||
1ab256cb | 5458 | Fput (Qprotected_field, Qerror_conditions, |
3438fe21 | 5459 | listn (CONSTYPE_PURE, 2, Qprotected_field, Qerror)); |
1ab256cb | 5460 | Fput (Qprotected_field, Qerror_message, |
2a0213a6 | 5461 | build_pure_c_string ("Attempt to modify a protected field")); |
1ab256cb | 5462 | |
422745d0 TT |
5463 | DEFVAR_BUFFER_DEFAULTS ("default-mode-line-format", |
5464 | mode_line_format, | |
5465 | doc: /* Default value of `mode-line-format' for buffers that don't override it. | |
018ba359 | 5466 | This is the same as (default-value 'mode-line-format). */); |
1ab256cb | 5467 | |
422745d0 TT |
5468 | DEFVAR_BUFFER_DEFAULTS ("default-header-line-format", |
5469 | header_line_format, | |
5470 | doc: /* Default value of `header-line-format' for buffers that don't override it. | |
018ba359 | 5471 | This is the same as (default-value 'header-line-format). */); |
0552666b | 5472 | |
422745d0 TT |
5473 | DEFVAR_BUFFER_DEFAULTS ("default-cursor-type", cursor_type, |
5474 | doc: /* Default value of `cursor-type' for buffers that don't override it. | |
018ba359 | 5475 | This is the same as (default-value 'cursor-type). */); |
bd96bd79 | 5476 | |
422745d0 TT |
5477 | DEFVAR_BUFFER_DEFAULTS ("default-line-spacing", |
5478 | extra_line_spacing, | |
5479 | doc: /* Default value of `line-spacing' for buffers that don't override it. | |
018ba359 | 5480 | This is the same as (default-value 'line-spacing). */); |
a3bbced0 | 5481 | |
422745d0 TT |
5482 | DEFVAR_BUFFER_DEFAULTS ("default-cursor-in-non-selected-windows", |
5483 | cursor_in_non_selected_windows, | |
5484 | doc: /* Default value of `cursor-in-non-selected-windows'. | |
187ccf49 KS |
5485 | This is the same as (default-value 'cursor-in-non-selected-windows). */); |
5486 | ||
422745d0 TT |
5487 | DEFVAR_BUFFER_DEFAULTS ("default-abbrev-mode", |
5488 | abbrev_mode, | |
5489 | doc: /* Default value of `abbrev-mode' for buffers that do not override it. | |
018ba359 | 5490 | This is the same as (default-value 'abbrev-mode). */); |
1ab256cb | 5491 | |
422745d0 TT |
5492 | DEFVAR_BUFFER_DEFAULTS ("default-ctl-arrow", |
5493 | ctl_arrow, | |
5494 | doc: /* Default value of `ctl-arrow' for buffers that do not override it. | |
018ba359 | 5495 | This is the same as (default-value 'ctl-arrow). */); |
1ab256cb | 5496 | |
422745d0 TT |
5497 | DEFVAR_BUFFER_DEFAULTS ("default-enable-multibyte-characters", |
5498 | enable_multibyte_characters, | |
fb7ada5f | 5499 | doc: /* Default value of `enable-multibyte-characters' for buffers not overriding it. |
018ba359 | 5500 | This is the same as (default-value 'enable-multibyte-characters). */); |
177c0ea7 | 5501 | |
422745d0 TT |
5502 | DEFVAR_BUFFER_DEFAULTS ("default-buffer-file-coding-system", |
5503 | buffer_file_coding_system, | |
5504 | doc: /* Default value of `buffer-file-coding-system' for buffers not overriding it. | |
018ba359 | 5505 | This is the same as (default-value 'buffer-file-coding-system). */); |
177c0ea7 | 5506 | |
422745d0 TT |
5507 | DEFVAR_BUFFER_DEFAULTS ("default-truncate-lines", |
5508 | truncate_lines, | |
5509 | doc: /* Default value of `truncate-lines' for buffers that do not override it. | |
018ba359 | 5510 | This is the same as (default-value 'truncate-lines). */); |
1ab256cb | 5511 | |
422745d0 TT |
5512 | DEFVAR_BUFFER_DEFAULTS ("default-fill-column", |
5513 | fill_column, | |
5514 | doc: /* Default value of `fill-column' for buffers that do not override it. | |
018ba359 | 5515 | This is the same as (default-value 'fill-column). */); |
1ab256cb | 5516 | |
422745d0 TT |
5517 | DEFVAR_BUFFER_DEFAULTS ("default-left-margin", |
5518 | left_margin, | |
5519 | doc: /* Default value of `left-margin' for buffers that do not override it. | |
018ba359 | 5520 | This is the same as (default-value 'left-margin). */); |
1ab256cb | 5521 | |
422745d0 TT |
5522 | DEFVAR_BUFFER_DEFAULTS ("default-tab-width", |
5523 | tab_width, | |
5524 | doc: /* Default value of `tab-width' for buffers that do not override it. | |
018ba359 | 5525 | This is the same as (default-value 'tab-width). */); |
1ab256cb | 5526 | |
422745d0 TT |
5527 | DEFVAR_BUFFER_DEFAULTS ("default-case-fold-search", |
5528 | case_fold_search, | |
5529 | doc: /* Default value of `case-fold-search' for buffers that don't override it. | |
018ba359 | 5530 | This is the same as (default-value 'case-fold-search). */); |
1ab256cb | 5531 | |
422745d0 TT |
5532 | DEFVAR_BUFFER_DEFAULTS ("default-left-margin-width", |
5533 | left_margin_cols, | |
5534 | doc: /* Default value of `left-margin-width' for buffers that don't override it. | |
018ba359 | 5535 | This is the same as (default-value 'left-margin-width). */); |
0552666b | 5536 | |
422745d0 TT |
5537 | DEFVAR_BUFFER_DEFAULTS ("default-right-margin-width", |
5538 | right_margin_cols, | |
5539 | doc: /* Default value of `right-margin-width' for buffers that don't override it. | |
018ba359 | 5540 | This is the same as (default-value 'right-margin-width). */); |
177c0ea7 | 5541 | |
422745d0 TT |
5542 | DEFVAR_BUFFER_DEFAULTS ("default-left-fringe-width", |
5543 | left_fringe_width, | |
5544 | doc: /* Default value of `left-fringe-width' for buffers that don't override it. | |
2ad8731a KS |
5545 | This is the same as (default-value 'left-fringe-width). */); |
5546 | ||
422745d0 TT |
5547 | DEFVAR_BUFFER_DEFAULTS ("default-right-fringe-width", |
5548 | right_fringe_width, | |
5549 | doc: /* Default value of `right-fringe-width' for buffers that don't override it. | |
2ad8731a KS |
5550 | This is the same as (default-value 'right-fringe-width). */); |
5551 | ||
422745d0 TT |
5552 | DEFVAR_BUFFER_DEFAULTS ("default-fringes-outside-margins", |
5553 | fringes_outside_margins, | |
5554 | doc: /* Default value of `fringes-outside-margins' for buffers that don't override it. | |
2ad8731a KS |
5555 | This is the same as (default-value 'fringes-outside-margins). */); |
5556 | ||
422745d0 TT |
5557 | DEFVAR_BUFFER_DEFAULTS ("default-scroll-bar-width", |
5558 | scroll_bar_width, | |
5559 | doc: /* Default value of `scroll-bar-width' for buffers that don't override it. | |
2ad8731a KS |
5560 | This is the same as (default-value 'scroll-bar-width). */); |
5561 | ||
422745d0 TT |
5562 | DEFVAR_BUFFER_DEFAULTS ("default-vertical-scroll-bar", |
5563 | vertical_scroll_bar_type, | |
5564 | doc: /* Default value of `vertical-scroll-bar' for buffers that don't override it. | |
2ad8731a KS |
5565 | This is the same as (default-value 'vertical-scroll-bar). */); |
5566 | ||
422745d0 TT |
5567 | DEFVAR_BUFFER_DEFAULTS ("default-indicate-empty-lines", |
5568 | indicate_empty_lines, | |
5569 | doc: /* Default value of `indicate-empty-lines' for buffers that don't override it. | |
018ba359 | 5570 | This is the same as (default-value 'indicate-empty-lines). */); |
177c0ea7 | 5571 | |
422745d0 TT |
5572 | DEFVAR_BUFFER_DEFAULTS ("default-indicate-buffer-boundaries", |
5573 | indicate_buffer_boundaries, | |
5574 | doc: /* Default value of `indicate-buffer-boundaries' for buffers that don't override it. | |
6b61353c KH |
5575 | This is the same as (default-value 'indicate-buffer-boundaries). */); |
5576 | ||
422745d0 TT |
5577 | DEFVAR_BUFFER_DEFAULTS ("default-fringe-indicator-alist", |
5578 | fringe_indicator_alist, | |
5579 | doc: /* Default value of `fringe-indicator-alist' for buffers that don't override it. | |
c6a46372 KS |
5580 | This is the same as (default-value 'fringe-indicator-alist'). */); |
5581 | ||
422745d0 TT |
5582 | DEFVAR_BUFFER_DEFAULTS ("default-fringe-cursor-alist", |
5583 | fringe_cursor_alist, | |
5584 | doc: /* Default value of `fringe-cursor-alist' for buffers that don't override it. | |
c6a46372 KS |
5585 | This is the same as (default-value 'fringe-cursor-alist'). */); |
5586 | ||
422745d0 TT |
5587 | DEFVAR_BUFFER_DEFAULTS ("default-scroll-up-aggressively", |
5588 | scroll_up_aggressively, | |
5589 | doc: /* Default value of `scroll-up-aggressively'. | |
7614d762 | 5590 | This value applies in buffers that don't have their own local values. |
fc961256 | 5591 | This is the same as (default-value 'scroll-up-aggressively). */); |
177c0ea7 | 5592 | |
422745d0 TT |
5593 | DEFVAR_BUFFER_DEFAULTS ("default-scroll-down-aggressively", |
5594 | scroll_down_aggressively, | |
5595 | doc: /* Default value of `scroll-down-aggressively'. | |
7614d762 | 5596 | This value applies in buffers that don't have their own local values. |
fc961256 | 5597 | This is the same as (default-value 'scroll-down-aggressively). */); |
177c0ea7 | 5598 | |
045dee35 | 5599 | DEFVAR_PER_BUFFER ("header-line-format", |
4b4deea2 | 5600 | &BVAR (current_buffer, header_line_format), |
7ee72033 | 5601 | Qnil, |
7614d762 RS |
5602 | doc: /* Analogous to `mode-line-format', but controls the header line. |
5603 | The header line appears, optionally, at the top of a window; | |
5604 | the mode line appears at the bottom. */); | |
177c0ea7 | 5605 | |
4b4deea2 | 5606 | DEFVAR_PER_BUFFER ("mode-line-format", &BVAR (current_buffer, mode_line_format), |
efc7e75f PJ |
5607 | Qnil, |
5608 | doc: /* Template for displaying mode line for current buffer. | |
5f2c76c6 CY |
5609 | |
5610 | The value may be nil, a string, a symbol or a list. | |
5611 | ||
018ba359 | 5612 | A value of nil means don't display a mode line. |
5f2c76c6 CY |
5613 | |
5614 | For any symbol other than t or nil, the symbol's value is processed as | |
5615 | a mode line construct. As a special exception, if that value is a | |
5616 | string, the string is processed verbatim, without handling any | |
5617 | %-constructs (see below). Also, unless the symbol has a non-nil | |
5618 | `risky-local-variable' property, all properties in any strings, as | |
5619 | well as all :eval and :propertize forms in the value, are ignored. | |
5620 | ||
5621 | A list whose car is a string or list is processed by processing each | |
5622 | of the list elements recursively, as separate mode line constructs, | |
5623 | and concatenating the results. | |
5624 | ||
5625 | A list of the form `(:eval FORM)' is processed by evaluating FORM and | |
5626 | using the result as a mode line construct. Be careful--FORM should | |
5627 | not load any files, because that can cause an infinite recursion. | |
5628 | ||
5629 | A list of the form `(:propertize ELT PROPS...)' is processed by | |
5630 | processing ELT as the mode line construct, and adding the text | |
5631 | properties PROPS to the result. | |
5632 | ||
5633 | A list whose car is a symbol is processed by examining the symbol's | |
5634 | value, and, if that value is non-nil, processing the cadr of the list | |
5635 | recursively; and if that value is nil, processing the caddr of the | |
5636 | list recursively. | |
5637 | ||
5638 | A list whose car is an integer is processed by processing the cadr of | |
5639 | the list, and padding (if the number is positive) or truncating (if | |
5640 | negative) to the width specified by that number. | |
5641 | ||
018ba359 | 5642 | A string is printed verbatim in the mode line except for %-constructs: |
018ba359 PJ |
5643 | %b -- print buffer name. %f -- print visited file name. |
5644 | %F -- print frame name. | |
5645 | %* -- print %, * or hyphen. %+ -- print *, % or hyphen. | |
5646 | %& is like %*, but ignore read-only-ness. | |
5647 | % means buffer is read-only and * means it is modified. | |
5648 | For a modified read-only buffer, %* gives % and %+ gives *. | |
5649 | %s -- print process status. %l -- print the current line number. | |
5650 | %c -- print the current column number (this makes editing slower). | |
5651 | To make the column number update correctly in all cases, | |
5652 | `column-number-mode' must be non-nil. | |
6b61353c KH |
5653 | %i -- print the size of the buffer. |
5654 | %I -- like %i, but use k, M, G, etc., to abbreviate. | |
018ba359 PJ |
5655 | %p -- print percent of buffer above top of window, or Top, Bot or All. |
5656 | %P -- print percent of buffer above bottom of window, perhaps plus Top, | |
5657 | or print Bottom or All. | |
018ba359 | 5658 | %n -- print Narrow if appropriate. |
dafbe726 | 5659 | %t -- visited file is text or binary (if OS supports this distinction). |
47419860 | 5660 | %z -- print mnemonics of keyboard, terminal, and buffer coding systems. |
018ba359 | 5661 | %Z -- like %z, but including the end-of-line format. |
dafbe726 | 5662 | %e -- print error message about full memory. |
f7165034 NR |
5663 | %@ -- print @ or hyphen. @ means that default-directory is on a |
5664 | remote machine. | |
018ba359 PJ |
5665 | %[ -- print one [ for each recursive editing level. %] similar. |
5666 | %% -- print %. %- -- print infinitely many dashes. | |
5667 | Decimal digits after the % specify field width to which to pad. */); | |
5668 | ||
422745d0 | 5669 | DEFVAR_BUFFER_DEFAULTS ("default-major-mode", major_mode, |
fb7ada5f | 5670 | doc: /* Value of `major-mode' for new buffers. */); |
1ab256cb | 5671 | |
4b4deea2 | 5672 | DEFVAR_PER_BUFFER ("major-mode", &BVAR (current_buffer, major_mode), |
7ee72033 | 5673 | make_number (Lisp_Symbol), |
5a021dd0 GM |
5674 | doc: /* Symbol for current buffer's major mode. |
5675 | The default value (normally `fundamental-mode') affects new buffers. | |
5676 | A value of nil means to use the current buffer's major mode, provided | |
5677 | it is not marked as "special". | |
5678 | ||
5679 | When a mode is used by default, `find-file' switches to it before it | |
5680 | reads the contents into the buffer and before it finishes setting up | |
5681 | the buffer. Thus, the mode and its hooks should not expect certain | |
5682 | variables such as `buffer-read-only' and `buffer-file-coding-system' | |
5683 | to be set up. */); | |
1ab256cb | 5684 | |
4b4deea2 | 5685 | DEFVAR_PER_BUFFER ("mode-name", &BVAR (current_buffer, mode_name), |
c01d0677 | 5686 | Qnil, |
64a7c220 | 5687 | doc: /* Pretty name of current buffer's major mode. |
7cb70974 EZ |
5688 | Usually a string, but can use any of the constructs for `mode-line-format', |
5689 | which see. | |
5690 | Format with `format-mode-line' to produce a string value. */); | |
1ab256cb | 5691 | |
4b4deea2 | 5692 | DEFVAR_PER_BUFFER ("local-abbrev-table", &BVAR (current_buffer, abbrev_table), Qnil, |
d6aa1876 SM |
5693 | doc: /* Local (mode-specific) abbrev table of current buffer. */); |
5694 | ||
4b4deea2 | 5695 | DEFVAR_PER_BUFFER ("abbrev-mode", &BVAR (current_buffer, abbrev_mode), Qnil, |
9d794026 GM |
5696 | doc: /* Non-nil if Abbrev mode is enabled. |
5697 | Use the command `abbrev-mode' to change this variable. */); | |
1ab256cb | 5698 | |
4b4deea2 | 5699 | DEFVAR_PER_BUFFER ("case-fold-search", &BVAR (current_buffer, case_fold_search), |
7ee72033 | 5700 | Qnil, |
fb7ada5f | 5701 | doc: /* Non-nil if searches and matches should ignore case. */); |
1ab256cb | 5702 | |
4b4deea2 | 5703 | DEFVAR_PER_BUFFER ("fill-column", &BVAR (current_buffer, fill_column), |
d5040d2d | 5704 | make_number (Lisp_Int0), |
fb7ada5f | 5705 | doc: /* Column beyond which automatic line-wrapping should happen. |
f1ccb329 | 5706 | Interactively, you can set the buffer local value using \\[set-fill-column]. */); |
1ab256cb | 5707 | |
4b4deea2 | 5708 | DEFVAR_PER_BUFFER ("left-margin", &BVAR (current_buffer, left_margin), |
d5040d2d | 5709 | make_number (Lisp_Int0), |
fb7ada5f | 5710 | doc: /* Column for the default `indent-line-function' to indent to. |
018ba359 | 5711 | Linefeed indents to this column in Fundamental mode. */); |
1ab256cb | 5712 | |
4b4deea2 | 5713 | DEFVAR_PER_BUFFER ("tab-width", &BVAR (current_buffer, tab_width), |
d5040d2d | 5714 | make_number (Lisp_Int0), |
fb7ada5f | 5715 | doc: /* Distance between tab stops (for display of tab characters), in columns. |
fde4eb86 | 5716 | This should be an integer greater than zero. */); |
1ab256cb | 5717 | |
4b4deea2 | 5718 | DEFVAR_PER_BUFFER ("ctl-arrow", &BVAR (current_buffer, ctl_arrow), Qnil, |
fb7ada5f | 5719 | doc: /* Non-nil means display control chars with uparrow. |
018ba359 PJ |
5720 | A value of nil means use backslash and octal digits. |
5721 | This variable does not apply to characters whose display is specified | |
5722 | in the current display table (if there is one). */); | |
1ab256cb | 5723 | |
3b06f880 | 5724 | DEFVAR_PER_BUFFER ("enable-multibyte-characters", |
4b4deea2 | 5725 | &BVAR (current_buffer, enable_multibyte_characters), |
a9b9a780 | 5726 | Qnil, |
efc7e75f | 5727 | doc: /* Non-nil means the buffer contents are regarded as multi-byte characters. |
018ba359 PJ |
5728 | Otherwise they are regarded as unibyte. This affects the display, |
5729 | file I/O and the behavior of various editing commands. | |
5730 | ||
5731 | This variable is buffer-local but you cannot set it directly; | |
5732 | use the function `set-buffer-multibyte' to change a buffer's representation. | |
a66cfb1c | 5733 | See also Info node `(elisp)Text Representations'. */); |
d67b4f80 | 5734 | XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1; |
3b06f880 | 5735 | |
c71b5d9b | 5736 | DEFVAR_PER_BUFFER ("buffer-file-coding-system", |
4b4deea2 | 5737 | &BVAR (current_buffer, buffer_file_coding_system), Qnil, |
efc7e75f | 5738 | doc: /* Coding system to be used for encoding the buffer contents on saving. |
018ba359 PJ |
5739 | This variable applies to saving the buffer, and also to `write-region' |
5740 | and other functions that use `write-region'. | |
5741 | It does not apply to sending output to subprocesses, however. | |
5742 | ||
5743 | If this is nil, the buffer is saved without any code conversion | |
5744 | unless some coding system is specified in `file-coding-system-alist' | |
5745 | for the buffer file. | |
5746 | ||
31a6cb06 EZ |
5747 | If the text to be saved cannot be encoded as specified by this variable, |
5748 | an alternative encoding is selected by `select-safe-coding-system', which see. | |
5749 | ||
018ba359 PJ |
5750 | The variable `coding-system-for-write', if non-nil, overrides this variable. |
5751 | ||
5752 | This variable is never applied to a way of decoding a file while reading it. */); | |
c71b5d9b | 5753 | |
f44e260c | 5754 | DEFVAR_PER_BUFFER ("bidi-display-reordering", |
4b4deea2 | 5755 | &BVAR (current_buffer, bidi_display_reordering), Qnil, |
938efb77 | 5756 | doc: /* Non-nil means reorder bidirectional text for display in the visual order. */); |
3b06f880 | 5757 | |
6c0cf218 | 5758 | DEFVAR_PER_BUFFER ("bidi-paragraph-direction", |
4b4deea2 | 5759 | &BVAR (current_buffer, bidi_paragraph_direction), Qnil, |
fb7ada5f | 5760 | doc: /* If non-nil, forces directionality of text paragraphs in the buffer. |
b4bf28b7 | 5761 | |
b44d9321 EZ |
5762 | If this is nil (the default), the direction of each paragraph is |
5763 | determined by the first strong directional character of its text. | |
5764 | The values of `right-to-left' and `left-to-right' override that. | |
5765 | Any other value is treated as nil. | |
b4bf28b7 | 5766 | |
b44d9321 EZ |
5767 | This variable has no effect unless the buffer's value of |
5768 | \`bidi-display-reordering' is non-nil. */); | |
5769 | ||
4b4deea2 | 5770 | DEFVAR_PER_BUFFER ("truncate-lines", &BVAR (current_buffer, truncate_lines), Qnil, |
fb7ada5f | 5771 | doc: /* Non-nil means do not display continuation lines. |
7614d762 | 5772 | Instead, give each line of text just one screen line. |
018ba359 PJ |
5773 | |
5774 | Note that this is overridden by the variable | |
5775 | `truncate-partial-width-windows' if that variable is non-nil | |
32bbb17c GM |
5776 | and this buffer is not full-frame width. |
5777 | ||
5778 | Minibuffers set this variable to nil. */); | |
1ab256cb | 5779 | |
4b4deea2 | 5780 | DEFVAR_PER_BUFFER ("word-wrap", &BVAR (current_buffer, word_wrap), Qnil, |
fb7ada5f | 5781 | doc: /* Non-nil means to use word-wrapping for continuation lines. |
0858cd02 CY |
5782 | When word-wrapping is on, continuation lines are wrapped at the space |
5783 | or tab character nearest to the right window edge. | |
5784 | If nil, continuation lines are wrapped at the right screen edge. | |
5785 | ||
5786 | This variable has no effect if long lines are truncated (see | |
eb577e27 GM |
5787 | `truncate-lines' and `truncate-partial-width-windows'). If you use |
5788 | word-wrapping, you might want to reduce the value of | |
5789 | `truncate-partial-width-windows', since wrapping can make text readable | |
c5cfcbe0 CY |
5790 | in narrower windows. |
5791 | ||
5792 | Instead of setting this variable directly, most users should use | |
5793 | Visual Line mode . Visual Line mode, when enabled, sets `word-wrap' | |
5794 | to t, and additionally redefines simple editing commands to act on | |
5795 | visual lines rather than logical lines. See the documentation of | |
5796 | `visual-line-mode'. */); | |
0858cd02 | 5797 | |
4b4deea2 | 5798 | DEFVAR_PER_BUFFER ("default-directory", &BVAR (current_buffer, directory), |
7ee72033 | 5799 | make_number (Lisp_String), |
efc7e75f | 5800 | doc: /* Name of default directory of current buffer. Should end with slash. |
018ba359 | 5801 | To interactively change the default directory, use command `cd'. */); |
1ab256cb | 5802 | |
4b4deea2 | 5803 | DEFVAR_PER_BUFFER ("auto-fill-function", &BVAR (current_buffer, auto_fill_function), |
7ee72033 | 5804 | Qnil, |
efc7e75f | 5805 | doc: /* Function called (if non-nil) to perform auto-fill. |
018ba359 PJ |
5806 | It is called after self-inserting any character specified in |
5807 | the `auto-fill-chars' table. | |
5808 | NOTE: This variable is not a hook; | |
5809 | its value may not be a list of functions. */); | |
1ab256cb | 5810 | |
4b4deea2 | 5811 | DEFVAR_PER_BUFFER ("buffer-file-name", &BVAR (current_buffer, filename), |
7ee72033 | 5812 | make_number (Lisp_String), |
efc7e75f | 5813 | doc: /* Name of file visited in current buffer, or nil if not visiting a file. */); |
1ab256cb | 5814 | |
4b4deea2 | 5815 | DEFVAR_PER_BUFFER ("buffer-file-truename", &BVAR (current_buffer, file_truename), |
7ee72033 | 5816 | make_number (Lisp_String), |
efc7e75f | 5817 | doc: /* Abbreviated truename of file visited in current buffer, or nil if none. |
018ba359 PJ |
5818 | The truename of a file is calculated by `file-truename' |
5819 | and then abbreviated with `abbreviate-file-name'. */); | |
f6ed2e84 | 5820 | |
1ab256cb | 5821 | DEFVAR_PER_BUFFER ("buffer-auto-save-file-name", |
4b4deea2 | 5822 | &BVAR (current_buffer, auto_save_file_name), |
7ee72033 | 5823 | make_number (Lisp_String), |
7614d762 RS |
5824 | doc: /* Name of file for auto-saving current buffer. |
5825 | If it is nil, that means don't auto-save this buffer. */); | |
1ab256cb | 5826 | |
4b4deea2 | 5827 | DEFVAR_PER_BUFFER ("buffer-read-only", &BVAR (current_buffer, read_only), Qnil, |
efc7e75f | 5828 | doc: /* Non-nil if this buffer is read-only. */); |
1ab256cb | 5829 | |
4b4deea2 | 5830 | DEFVAR_PER_BUFFER ("buffer-backed-up", &BVAR (current_buffer, backed_up), Qnil, |
efc7e75f | 5831 | doc: /* Non-nil if this buffer's file has been backed up. |
018ba359 | 5832 | Backing up is done before the first time the file is saved. */); |
1ab256cb | 5833 | |
4b4deea2 | 5834 | DEFVAR_PER_BUFFER ("buffer-saved-size", &BVAR (current_buffer, save_length), |
d5040d2d | 5835 | make_number (Lisp_Int0), |
efc7e75f | 5836 | doc: /* Length of current buffer when last read in, saved or auto-saved. |
4be941e3 RS |
5837 | 0 initially. |
5838 | -1 means auto-saving turned off until next real save. | |
5839 | ||
5840 | If you set this to -2, that means don't turn off auto-saving in this buffer | |
5841 | if its text size shrinks. If you use `buffer-swap-text' on a buffer, | |
5842 | you probably should set this to -2 in that buffer. */); | |
1ab256cb | 5843 | |
4b4deea2 | 5844 | DEFVAR_PER_BUFFER ("selective-display", &BVAR (current_buffer, selective_display), |
7ee72033 | 5845 | Qnil, |
7614d762 | 5846 | doc: /* Non-nil enables selective display. |
a66f285a JB |
5847 | An integer N as value means display only lines |
5848 | that start with less than N columns of space. | |
7614d762 RS |
5849 | A value of t means that the character ^M makes itself and |
5850 | all the rest of the line invisible; also, when saving the buffer | |
5851 | in a file, save the ^M as a newline. */); | |
1ab256cb | 5852 | |
1ab256cb | 5853 | DEFVAR_PER_BUFFER ("selective-display-ellipses", |
4b4deea2 | 5854 | &BVAR (current_buffer, selective_display_ellipses), |
7ee72033 | 5855 | Qnil, |
3f676284 | 5856 | doc: /* Non-nil means display ... on previous line when a line is invisible. */); |
1ab256cb | 5857 | |
4b4deea2 | 5858 | DEFVAR_PER_BUFFER ("overwrite-mode", &BVAR (current_buffer, overwrite_mode), Qnil, |
efc7e75f | 5859 | doc: /* Non-nil if self-insertion should replace existing text. |
018ba359 PJ |
5860 | The value should be one of `overwrite-mode-textual', |
5861 | `overwrite-mode-binary', or nil. | |
5862 | If it is `overwrite-mode-textual', self-insertion still | |
5863 | inserts at the end of a line, and inserts when point is before a tab, | |
5864 | until the tab is filled in. | |
5865 | If `overwrite-mode-binary', self-insertion replaces newlines and tabs too. */); | |
5866 | ||
4b4deea2 | 5867 | DEFVAR_PER_BUFFER ("buffer-display-table", &BVAR (current_buffer, display_table), |
7ee72033 | 5868 | Qnil, |
efc7e75f | 5869 | doc: /* Display table that controls display of the contents of current buffer. |
018ba359 PJ |
5870 | |
5871 | If this variable is nil, the value of `standard-display-table' is used. | |
5872 | Each window can have its own, overriding display table, see | |
5873 | `set-window-display-table' and `window-display-table'. | |
5874 | ||
5875 | The display table is a char-table created with `make-display-table'. | |
5876 | A char-table is an array indexed by character codes. Normal array | |
5877 | primitives `aref' and `aset' can be used to access elements of a char-table. | |
5878 | ||
5879 | Each of the char-table elements control how to display the corresponding | |
5880 | text character: the element at index C in the table says how to display | |
5881 | the character whose code is C. Each element should be a vector of | |
426a9163 JB |
5882 | characters or nil. The value nil means display the character in the |
5883 | default fashion; otherwise, the characters from the vector are delivered | |
5884 | to the screen instead of the original character. | |
018ba359 | 5885 | |
5fd11dc8 | 5886 | For example, (aset buffer-display-table ?X [?Y]) tells Emacs |
adbb3b05 | 5887 | to display a capital Y instead of each X character. |
018ba359 PJ |
5888 | |
5889 | In addition, a char-table has six extra slots to control the display of: | |
5890 | ||
5891 | the end of a truncated screen line (extra-slot 0, a single character); | |
5892 | the end of a continued line (extra-slot 1, a single character); | |
5893 | the escape character used to display character codes in octal | |
5894 | (extra-slot 2, a single character); | |
5895 | the character used as an arrow for control characters (extra-slot 3, | |
5896 | a single character); | |
5897 | the decoration indicating the presence of invisible lines (extra-slot 4, | |
5898 | a vector of characters); | |
5899 | the character used to draw the border between side-by-side windows | |
5900 | (extra-slot 5, a single character). | |
5901 | ||
5902 | See also the functions `display-table-slot' and `set-display-table-slot'. */); | |
1ab256cb | 5903 | |
4b4deea2 | 5904 | DEFVAR_PER_BUFFER ("left-margin-width", &BVAR (current_buffer, left_margin_cols), |
7ee72033 | 5905 | Qnil, |
fb7ada5f | 5906 | doc: /* Width of left marginal area for display of a buffer. |
018ba359 | 5907 | A value of nil means no marginal area. */); |
177c0ea7 | 5908 | |
4b4deea2 | 5909 | DEFVAR_PER_BUFFER ("right-margin-width", &BVAR (current_buffer, right_margin_cols), |
7ee72033 | 5910 | Qnil, |
fb7ada5f | 5911 | doc: /* Width of right marginal area for display of a buffer. |
018ba359 | 5912 | A value of nil means no marginal area. */); |
177c0ea7 | 5913 | |
4b4deea2 | 5914 | DEFVAR_PER_BUFFER ("left-fringe-width", &BVAR (current_buffer, left_fringe_width), |
2ad8731a | 5915 | Qnil, |
fb7ada5f | 5916 | doc: /* Width of this buffer's left fringe (in pixels). |
2ad8731a KS |
5917 | A value of 0 means no left fringe is shown in this buffer's window. |
5918 | A value of nil means to use the left fringe width from the window's frame. */); | |
5919 | ||
4b4deea2 | 5920 | DEFVAR_PER_BUFFER ("right-fringe-width", &BVAR (current_buffer, right_fringe_width), |
2ad8731a | 5921 | Qnil, |
fb7ada5f | 5922 | doc: /* Width of this buffer's right fringe (in pixels). |
2ad8731a KS |
5923 | A value of 0 means no right fringe is shown in this buffer's window. |
5924 | A value of nil means to use the right fringe width from the window's frame. */); | |
5925 | ||
4b4deea2 | 5926 | DEFVAR_PER_BUFFER ("fringes-outside-margins", &BVAR (current_buffer, fringes_outside_margins), |
2ad8731a | 5927 | Qnil, |
fb7ada5f | 5928 | doc: /* Non-nil means to display fringes outside display margins. |
2ad8731a KS |
5929 | A value of nil means to display fringes between margins and buffer text. */); |
5930 | ||
4b4deea2 | 5931 | DEFVAR_PER_BUFFER ("scroll-bar-width", &BVAR (current_buffer, scroll_bar_width), |
2ad8731a | 5932 | Qnil, |
fb7ada5f | 5933 | doc: /* Width of this buffer's scroll bars in pixels. |
2ad8731a KS |
5934 | A value of nil means to use the scroll bar width from the window's frame. */); |
5935 | ||
4b4deea2 | 5936 | DEFVAR_PER_BUFFER ("vertical-scroll-bar", &BVAR (current_buffer, vertical_scroll_bar_type), |
2ad8731a | 5937 | Qnil, |
fb7ada5f | 5938 | doc: /* Position of this buffer's vertical scroll bar. |
7c6b2007 | 5939 | The value takes effect whenever you tell a window to display this buffer; |
188577ce | 5940 | for instance, with `set-window-buffer' or when `display-buffer' displays it. |
7c6b2007 | 5941 | |
fc2c8887 RS |
5942 | A value of `left' or `right' means put the vertical scroll bar at that side |
5943 | of the window; a value of nil means don't show any vertical scroll bars. | |
5944 | A value of t (the default) means do whatever the window's frame specifies. */); | |
2ad8731a | 5945 | |
0552666b | 5946 | DEFVAR_PER_BUFFER ("indicate-empty-lines", |
4b4deea2 | 5947 | &BVAR (current_buffer, indicate_empty_lines), Qnil, |
fb7ada5f | 5948 | doc: /* Visually indicate empty lines after the buffer end. |
018ba359 PJ |
5949 | If non-nil, a bitmap is displayed in the left fringe of a window on |
5950 | window-systems. */); | |
177c0ea7 | 5951 | |
6b61353c | 5952 | DEFVAR_PER_BUFFER ("indicate-buffer-boundaries", |
4b4deea2 | 5953 | &BVAR (current_buffer, indicate_buffer_boundaries), Qnil, |
fb7ada5f | 5954 | doc: /* Visually indicate buffer boundaries and scrolling. |
6b61353c KH |
5955 | If non-nil, the first and last line of the buffer are marked in the fringe |
5956 | of a window on window-systems with angle bitmaps, or if the window can be | |
5957 | scrolled, the top and bottom line of the window are marked with up and down | |
5958 | arrow bitmaps. | |
b2229037 KS |
5959 | |
5960 | If value is a symbol `left' or `right', both angle and arrow bitmaps | |
79e3497d | 5961 | are displayed in the left or right fringe, resp. Any other value |
845a78b4 | 5962 | that doesn't look like an alist means display the angle bitmaps in |
79e3497d | 5963 | the left fringe but no arrows. |
b2229037 | 5964 | |
79e3497d RS |
5965 | You can exercise more precise control by using an alist as the |
5966 | value. Each alist element (INDICATOR . POSITION) specifies | |
5967 | where to show one of the indicators. INDICATOR is one of `top', | |
b2229037 KS |
5968 | `bottom', `up', `down', or t, which specifies the default position, |
5969 | and POSITION is one of `left', `right', or nil, meaning do not show | |
5970 | this indicator. | |
5971 | ||
5972 | For example, ((top . left) (t . right)) places the top angle bitmap in | |
5973 | left fringe, the bottom angle bitmap in right fringe, and both arrow | |
6b61353c | 5974 | bitmaps in right fringe. To show just the angle bitmaps in the left |
b2229037 | 5975 | fringe, but no arrow bitmaps, use ((top . left) (bottom . left)). */); |
6b61353c | 5976 | |
c6a46372 | 5977 | DEFVAR_PER_BUFFER ("fringe-indicator-alist", |
4b4deea2 | 5978 | &BVAR (current_buffer, fringe_indicator_alist), Qnil, |
fb7ada5f | 5979 | doc: /* Mapping from logical to physical fringe indicator bitmaps. |
c6a46372 KS |
5980 | The value is an alist where each element (INDICATOR . BITMAPS) |
5981 | specifies the fringe bitmaps used to display a specific logical | |
5982 | fringe indicator. | |
5983 | ||
5984 | INDICATOR specifies the logical indicator type which is one of the | |
5985 | following symbols: `truncation' , `continuation', `overlay-arrow', | |
14fb5704 | 5986 | `top', `bottom', `top-bottom', `up', `down', empty-line', or `unknown'. |
c6a46372 | 5987 | |
14fb5704 | 5988 | BITMAPS is a list of symbols (LEFT RIGHT [LEFT1 RIGHT1]) which specifies |
c6a46372 KS |
5989 | the actual bitmap shown in the left or right fringe for the logical |
5990 | indicator. LEFT and RIGHT are the bitmaps shown in the left and/or | |
5991 | right fringe for the specific indicator. The LEFT1 or RIGHT1 bitmaps | |
14fb5704 JB |
5992 | are used only for the `bottom' and `top-bottom' indicators when the |
5993 | last (only) line has no final newline. BITMAPS may also be a single | |
c6a46372 KS |
5994 | symbol which is used in both left and right fringes. */); |
5995 | ||
5996 | DEFVAR_PER_BUFFER ("fringe-cursor-alist", | |
4b4deea2 | 5997 | &BVAR (current_buffer, fringe_cursor_alist), Qnil, |
fb7ada5f | 5998 | doc: /* Mapping from logical to physical fringe cursor bitmaps. |
c6a46372 KS |
5999 | The value is an alist where each element (CURSOR . BITMAP) |
6000 | specifies the fringe bitmaps used to display a specific logical | |
6001 | cursor type in the fringe. | |
6002 | ||
6003 | CURSOR specifies the logical cursor type which is one of the following | |
6004 | symbols: `box' , `hollow', `bar', `hbar', or `hollow-small'. The last | |
6005 | one is used to show a hollow cursor on narrow lines display lines | |
6006 | where the normal hollow cursor will not fit. | |
6007 | ||
6008 | BITMAP is the corresponding fringe bitmap shown for the logical | |
6009 | cursor type. */); | |
6010 | ||
0552666b | 6011 | DEFVAR_PER_BUFFER ("scroll-up-aggressively", |
4b4deea2 | 6012 | &BVAR (current_buffer, scroll_up_aggressively), Qnil, |
4e0692c1 RS |
6013 | doc: /* How far to scroll windows upward. |
6014 | If you move point off the bottom, the window scrolls automatically. | |
426a9163 | 6015 | This variable controls how far it scrolls. The value nil, the default, |
4e0692c1 RS |
6016 | means scroll to center point. A fraction means scroll to put point |
6017 | that fraction of the window's height from the bottom of the window. | |
d765e3a3 JB |
6018 | When the value is 0.0, point goes at the bottom line, which in the |
6019 | simple case that you moved off with C-f means scrolling just one line. | |
6020 | 1.0 means point goes at the top, so that in that simple case, the | |
6021 | window scrolls by a full window height. Meaningful values are | |
175e9712 | 6022 | between 0.0 and 1.0, inclusive. */); |
177c0ea7 | 6023 | |
0552666b | 6024 | DEFVAR_PER_BUFFER ("scroll-down-aggressively", |
4b4deea2 | 6025 | &BVAR (current_buffer, scroll_down_aggressively), Qnil, |
4e0692c1 RS |
6026 | doc: /* How far to scroll windows downward. |
6027 | If you move point off the top, the window scrolls automatically. | |
426a9163 | 6028 | This variable controls how far it scrolls. The value nil, the default, |
4e0692c1 RS |
6029 | means scroll to center point. A fraction means scroll to put point |
6030 | that fraction of the window's height from the top of the window. | |
d765e3a3 JB |
6031 | When the value is 0.0, point goes at the top line, which in the |
6032 | simple case that you moved off with C-b means scrolling just one line. | |
6033 | 1.0 means point goes at the bottom, so that in that simple case, the | |
6034 | window scrolls by a full window height. Meaningful values are | |
175e9712 | 6035 | between 0.0 and 1.0, inclusive. */); |
177c0ea7 | 6036 | |
1ab256cb RM |
6037 | /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol, |
6038 | "Don't ask."); | |
6039 | */ | |
1ab256cb | 6040 | |
29208e82 | 6041 | DEFVAR_LISP ("before-change-functions", Vbefore_change_functions, |
7ee72033 | 6042 | doc: /* List of functions to call before each text change. |
018ba359 PJ |
6043 | Two arguments are passed to each function: the positions of |
6044 | the beginning and end of the range of old text to be changed. | |
6045 | \(For an insertion, the beginning and end are at the same place.) | |
6046 | No information is given about the length of the text after the change. | |
6047 | ||
6048 | Buffer changes made while executing the `before-change-functions' | |
6049 | don't call any before-change or after-change functions. | |
7b2bf907 | 6050 | That's because `inhibit-modification-hooks' is temporarily set non-nil. |
018ba359 PJ |
6051 | |
6052 | If an unhandled error happens in running these functions, | |
6053 | the variable's value remains nil. That prevents the error | |
6054 | from happening repeatedly and making Emacs nonfunctional. */); | |
5f079267 RS |
6055 | Vbefore_change_functions = Qnil; |
6056 | ||
29208e82 | 6057 | DEFVAR_LISP ("after-change-functions", Vafter_change_functions, |
eacdfade | 6058 | doc: /* List of functions to call after each text change. |
018ba359 PJ |
6059 | Three arguments are passed to each function: the positions of |
6060 | the beginning and end of the range of changed text, | |
6061 | and the length in bytes of the pre-change text replaced by that range. | |
6062 | \(For an insertion, the pre-change length is zero; | |
6063 | for a deletion, that length is the number of bytes deleted, | |
6064 | and the post-change beginning and end are at the same place.) | |
6065 | ||
6066 | Buffer changes made while executing the `after-change-functions' | |
6067 | don't call any before-change or after-change functions. | |
7b2bf907 | 6068 | That's because `inhibit-modification-hooks' is temporarily set non-nil. |
018ba359 PJ |
6069 | |
6070 | If an unhandled error happens in running these functions, | |
6071 | the variable's value remains nil. That prevents the error | |
6072 | from happening repeatedly and making Emacs nonfunctional. */); | |
5f079267 RS |
6073 | Vafter_change_functions = Qnil; |
6074 | ||
29208e82 | 6075 | DEFVAR_LISP ("first-change-hook", Vfirst_change_hook, |
efc7e75f | 6076 | doc: /* A list of functions to call before changing a buffer which is unmodified. |
018ba359 | 6077 | The functions are run using the `run-hooks' function. */); |
dbc4e1c1 | 6078 | Vfirst_change_hook = Qnil; |
1ab256cb | 6079 | |
4b4deea2 | 6080 | DEFVAR_PER_BUFFER ("buffer-undo-list", &BVAR (current_buffer, undo_list), Qnil, |
7ee72033 | 6081 | doc: /* List of undo entries in current buffer. |
018ba359 PJ |
6082 | Recent changes come first; older changes follow newer. |
6083 | ||
6084 | An entry (BEG . END) represents an insertion which begins at | |
6085 | position BEG and ends at position END. | |
6086 | ||
6087 | An entry (TEXT . POSITION) represents the deletion of the string TEXT | |
6088 | from (abs POSITION). If POSITION is positive, point was at the front | |
6089 | of the text being deleted; if negative, point was at the end. | |
6090 | ||
d35af63c PE |
6091 | An entry (t HIGH LOW USEC PSEC) indicates that the buffer was previously |
6092 | unmodified; (HIGH LOW USEC PSEC) is in the same style as (current-time) | |
6093 | and is the visited file's modification time, as of that time. If the | |
018ba359 PJ |
6094 | modification time of the most recent save is different, this entry is |
6095 | obsolete. | |
6096 | ||
6097 | An entry (nil PROPERTY VALUE BEG . END) indicates that a text property | |
6098 | was modified between BEG and END. PROPERTY is the property name, | |
6099 | and VALUE is the old value. | |
6100 | ||
7405f386 KS |
6101 | An entry (apply FUN-NAME . ARGS) means undo the change with |
6102 | \(apply FUN-NAME ARGS). | |
6103 | ||
6104 | An entry (apply DELTA BEG END FUN-NAME . ARGS) supports selective undo | |
6105 | in the active region. BEG and END is the range affected by this entry | |
6106 | and DELTA is the number of bytes added or deleted in that range by | |
6107 | this change. | |
c6c7dc03 | 6108 | |
018ba359 PJ |
6109 | An entry (MARKER . DISTANCE) indicates that the marker MARKER |
6110 | was adjusted in position by the offset DISTANCE (an integer). | |
6111 | ||
6112 | An entry of the form POSITION indicates that point was at the buffer | |
6113 | location given by the integer. Undoing an entry of this form places | |
6114 | point at POSITION. | |
6115 | ||
b4c4f2f4 JB |
6116 | Entries with value `nil' mark undo boundaries. The undo command treats |
6117 | the changes between two undo boundaries as a single step to be undone. | |
018ba359 PJ |
6118 | |
6119 | If the value of the variable is t, undo information is not recorded. */); | |
6120 | ||
4b4deea2 | 6121 | DEFVAR_PER_BUFFER ("mark-active", &BVAR (current_buffer, mark_active), Qnil, |
7ee72033 | 6122 | doc: /* Non-nil means the mark and region are currently active in this buffer. */); |
018ba359 | 6123 | |
4b4deea2 | 6124 | DEFVAR_PER_BUFFER ("cache-long-line-scans", &BVAR (current_buffer, cache_long_line_scans), Qnil, |
7ee72033 | 6125 | doc: /* Non-nil means that Emacs should use caches to handle long lines more quickly. |
018ba359 PJ |
6126 | |
6127 | Normally, the line-motion functions work by scanning the buffer for | |
5629f29b DK |
6128 | newlines. Columnar operations (like `move-to-column' and |
6129 | `compute-motion') also work by scanning the buffer, summing character | |
018ba359 PJ |
6130 | widths as they go. This works well for ordinary text, but if the |
6131 | buffer's lines are very long (say, more than 500 characters), these | |
6132 | motion functions will take longer to execute. Emacs may also take | |
6133 | longer to update the display. | |
6134 | ||
5629f29b | 6135 | If `cache-long-line-scans' is non-nil, these motion functions cache the |
018ba359 PJ |
6136 | results of their scans, and consult the cache to avoid rescanning |
6137 | regions of the buffer until the text is modified. The caches are most | |
6138 | beneficial when they prevent the most searching---that is, when the | |
6139 | buffer contains long lines and large regions of characters with the | |
6140 | same, fixed screen width. | |
6141 | ||
5629f29b | 6142 | When `cache-long-line-scans' is non-nil, processing short lines will |
018ba359 PJ |
6143 | become slightly slower (because of the overhead of consulting the |
6144 | cache), and the caches will use memory roughly proportional to the | |
6145 | number of newlines and characters whose screen width varies. | |
6146 | ||
6147 | The caches require no explicit maintenance; their accuracy is | |
6148 | maintained internally by the Emacs primitives. Enabling or disabling | |
6149 | the cache should not affect the behavior of any of the motion | |
6150 | functions; it should only affect their performance. */); | |
6151 | ||
4b4deea2 | 6152 | DEFVAR_PER_BUFFER ("point-before-scroll", &BVAR (current_buffer, point_before_scroll), Qnil, |
7ee72033 | 6153 | doc: /* Value of point before the last series of scroll operations, or nil. */); |
018ba359 | 6154 | |
4b4deea2 | 6155 | DEFVAR_PER_BUFFER ("buffer-file-format", &BVAR (current_buffer, file_format), Qnil, |
7ee72033 | 6156 | doc: /* List of formats to use when saving this buffer. |
018ba359 | 6157 | Formats are defined by `format-alist'. This variable is |
a9b9a780 | 6158 | set when a file is visited. */); |
be9aafdd | 6159 | |
71ed49fa | 6160 | DEFVAR_PER_BUFFER ("buffer-auto-save-file-format", |
4b4deea2 | 6161 | &BVAR (current_buffer, auto_save_file_format), Qnil, |
fb7ada5f | 6162 | doc: /* Format in which to write auto-save files. |
71ed49fa LT |
6163 | Should be a list of symbols naming formats that are defined in `format-alist'. |
6164 | If it is t, which is the default, auto-save files are written in the | |
6165 | same format as a regular save would use. */); | |
6166 | ||
3cb719bd | 6167 | DEFVAR_PER_BUFFER ("buffer-invisibility-spec", |
4b4deea2 | 6168 | &BVAR (current_buffer, invisibility_spec), Qnil, |
7ee72033 | 6169 | doc: /* Invisibility spec of this buffer. |
018ba359 PJ |
6170 | The default is t, which means that text is invisible |
6171 | if it has a non-nil `invisible' property. | |
6172 | If the value is a list, a text character is invisible if its `invisible' | |
b49dd850 | 6173 | property is an element in that list (or is a list with members in common). |
018ba359 PJ |
6174 | If an element is a cons cell of the form (PROP . ELLIPSIS), |
6175 | then characters with property value PROP are invisible, | |
6176 | and they have an ellipsis as well if ELLIPSIS is non-nil. */); | |
3cb719bd | 6177 | |
7962a441 | 6178 | DEFVAR_PER_BUFFER ("buffer-display-count", |
4b4deea2 | 6179 | &BVAR (current_buffer, display_count), Qnil, |
7ee72033 | 6180 | doc: /* A number incremented each time this buffer is displayed in a window. |
018ba359 | 6181 | The function `set-window-buffer' increments it. */); |
3fd364db RS |
6182 | |
6183 | DEFVAR_PER_BUFFER ("buffer-display-time", | |
4b4deea2 | 6184 | &BVAR (current_buffer, display_time), Qnil, |
7ee72033 | 6185 | doc: /* Time stamp updated each time this buffer is displayed in a window. |
018ba359 PJ |
6186 | The function `set-window-buffer' updates this variable |
6187 | to the value obtained by calling `current-time'. | |
6188 | If the buffer has never been shown in a window, the value is nil. */); | |
6189 | ||
29208e82 | 6190 | DEFVAR_LISP ("transient-mark-mode", Vtransient_mark_mode, |
9d794026 GM |
6191 | doc: /* Non-nil if Transient Mark mode is enabled. |
6192 | See the command `transient-mark-mode' for a description of this minor mode. | |
6193 | ||
6194 | Non-nil also enables highlighting of the region whenever the mark is active. | |
6195 | The variable `highlight-nonselected-windows' controls whether to highlight | |
6196 | all windows or just the selected window. | |
6197 | ||
f49d1f52 | 6198 | Lisp programs may give this variable certain special values: |
9d794026 | 6199 | |
f49d1f52 SM |
6200 | - A value of `lambda' enables Transient Mark mode temporarily. |
6201 | It is disabled again after any subsequent action that would | |
6202 | normally deactivate the mark (e.g. buffer modification). | |
6203 | ||
6204 | - A value of (only . OLDVAL) enables Transient Mark mode | |
6205 | temporarily. After any subsequent point motion command that is | |
6206 | not shift-translated, or any other action that would normally | |
6207 | deactivate the mark (e.g. buffer modification), the value of | |
6208 | `transient-mark-mode' is set to OLDVAL. */); | |
c48f61ef RS |
6209 | Vtransient_mark_mode = Qnil; |
6210 | ||
29208e82 | 6211 | DEFVAR_LISP ("inhibit-read-only", Vinhibit_read_only, |
fb7ada5f | 6212 | doc: /* Non-nil means disregard read-only status of buffers or characters. |
018ba359 PJ |
6213 | If the value is t, disregard `buffer-read-only' and all `read-only' |
6214 | text properties. If the value is a list, disregard `buffer-read-only' | |
6215 | and disregard a `read-only' text property if the property value | |
6216 | is a member of the list. */); | |
a96b68f1 RS |
6217 | Vinhibit_read_only = Qnil; |
6218 | ||
4b4deea2 | 6219 | DEFVAR_PER_BUFFER ("cursor-type", &BVAR (current_buffer, cursor_type), Qnil, |
f6e22881 | 6220 | doc: /* Cursor to use when this buffer is in the selected window. |
018ba359 PJ |
6221 | Values are interpreted as follows: |
6222 | ||
b8dc613f JB |
6223 | t use the cursor specified for the frame |
6224 | nil don't display a cursor | |
6225 | box display a filled box cursor | |
6226 | hollow display a hollow box cursor | |
6227 | bar display a vertical bar cursor with default width | |
6228 | (bar . WIDTH) display a vertical bar cursor with width WIDTH | |
6229 | hbar display a horizontal bar cursor with default height | |
b4234f4c | 6230 | (hbar . HEIGHT) display a horizontal bar cursor with height HEIGHT |
b8dc613f | 6231 | ANYTHING ELSE display a hollow box cursor |
cd8d5236 | 6232 | |
e08b1705 MR |
6233 | When the buffer is displayed in a non-selected window, the |
6234 | cursor's appearance is instead controlled by the variable | |
6235 | `cursor-in-non-selected-windows'. */); | |
bb2ec976 | 6236 | |
a3bbced0 | 6237 | DEFVAR_PER_BUFFER ("line-spacing", |
4b4deea2 | 6238 | &BVAR (current_buffer, extra_line_spacing), Qnil, |
7ee72033 | 6239 | doc: /* Additional space to put between lines when displaying a buffer. |
3ba010e5 EZ |
6240 | The space is measured in pixels, and put below lines on graphic displays, |
6241 | see `display-graphic-p'. | |
60ebfdf3 | 6242 | If value is a floating point number, it specifies the spacing relative |
fc961256 | 6243 | to the default frame line height. A value of nil means add no extra space. */); |
a3bbced0 | 6244 | |
0124c5bd | 6245 | DEFVAR_PER_BUFFER ("cursor-in-non-selected-windows", |
4b4deea2 | 6246 | &BVAR (current_buffer, cursor_in_non_selected_windows), Qnil, |
fb7ada5f | 6247 | doc: /* Non-nil means show a cursor in non-selected windows. |
66c6abf0 GM |
6248 | If nil, only shows a cursor in the selected window. |
6249 | If t, displays a cursor related to the usual cursor type | |
6250 | \(a solid box becomes hollow, a bar becomes a narrower bar). | |
6251 | You can also specify the cursor type as in the `cursor-type' variable. | |
6252 | Use Custom to set this variable and update the display." */); | |
0124c5bd | 6253 | |
29208e82 | 6254 | DEFVAR_LISP ("kill-buffer-query-functions", Vkill_buffer_query_functions, |
f6e22881 JB |
6255 | doc: /* List of functions called with no args to query before killing a buffer. |
6256 | The buffer being killed will be current while the functions are running. | |
b7e8d081 MR |
6257 | |
6258 | If any of them returns nil, the buffer is not killed. Functions run by | |
6259 | this hook are supposed to not change the current buffer. */); | |
dcdffbf6 RS |
6260 | Vkill_buffer_query_functions = Qnil; |
6261 | ||
29208e82 | 6262 | DEFVAR_LISP ("change-major-mode-hook", Vchange_major_mode_hook, |
43ed3b8d CY |
6263 | doc: /* Normal hook run before changing the major mode of a buffer. |
6264 | The function `kill-all-local-variables' runs this before doing anything else. */); | |
6265 | Vchange_major_mode_hook = Qnil; | |
cd3520a4 | 6266 | DEFSYM (Qchange_major_mode_hook, "change-major-mode-hook"); |
43ed3b8d | 6267 | |
9397e56f MR |
6268 | DEFVAR_LISP ("buffer-list-update-hook", Vbuffer_list_update_hook, |
6269 | doc: /* Hook run when the buffer list changes. | |
6270 | Functions running this hook are `get-buffer-create', | |
6271 | `make-indirect-buffer', `rename-buffer', `kill-buffer', | |
96a72ee9 | 6272 | and `bury-buffer-internal'. */); |
9397e56f | 6273 | Vbuffer_list_update_hook = Qnil; |
cd3520a4 | 6274 | DEFSYM (Qbuffer_list_update_hook, "buffer-list-update-hook"); |
9397e56f | 6275 | |
0dc88e60 | 6276 | defsubr (&Sbuffer_live_p); |
1ab256cb RM |
6277 | defsubr (&Sbuffer_list); |
6278 | defsubr (&Sget_buffer); | |
6279 | defsubr (&Sget_file_buffer); | |
6280 | defsubr (&Sget_buffer_create); | |
336cd056 | 6281 | defsubr (&Smake_indirect_buffer); |
01050cb5 | 6282 | defsubr (&Sgenerate_new_buffer_name); |
1ab256cb | 6283 | defsubr (&Sbuffer_name); |
1ab256cb | 6284 | defsubr (&Sbuffer_file_name); |
336cd056 | 6285 | defsubr (&Sbuffer_base_buffer); |
79aa712d | 6286 | defsubr (&Sbuffer_local_value); |
1ab256cb RM |
6287 | defsubr (&Sbuffer_local_variables); |
6288 | defsubr (&Sbuffer_modified_p); | |
6289 | defsubr (&Sset_buffer_modified_p); | |
6290 | defsubr (&Sbuffer_modified_tick); | |
3e145152 | 6291 | defsubr (&Sbuffer_chars_modified_tick); |
1ab256cb RM |
6292 | defsubr (&Srename_buffer); |
6293 | defsubr (&Sother_buffer); | |
1ab256cb RM |
6294 | defsubr (&Sbuffer_enable_undo); |
6295 | defsubr (&Skill_buffer); | |
e4ed06f1 | 6296 | defsubr (&Sbury_buffer_internal); |
a9ee7a59 | 6297 | defsubr (&Sset_buffer_major_mode); |
1ab256cb RM |
6298 | defsubr (&Scurrent_buffer); |
6299 | defsubr (&Sset_buffer); | |
6300 | defsubr (&Sbarf_if_buffer_read_only); | |
3ac81adb | 6301 | defsubr (&Serase_buffer); |
13cda5f9 | 6302 | defsubr (&Sbuffer_swap_text); |
3ac81adb | 6303 | defsubr (&Sset_buffer_multibyte); |
1ab256cb | 6304 | defsubr (&Skill_all_local_variables); |
2eec3b4e | 6305 | |
52f8ec73 | 6306 | defsubr (&Soverlayp); |
2eec3b4e RS |
6307 | defsubr (&Smake_overlay); |
6308 | defsubr (&Sdelete_overlay); | |
c5e28e39 | 6309 | defsubr (&Sdelete_all_overlays); |
2eec3b4e | 6310 | defsubr (&Smove_overlay); |
8ebafa8d JB |
6311 | defsubr (&Soverlay_start); |
6312 | defsubr (&Soverlay_end); | |
6313 | defsubr (&Soverlay_buffer); | |
6314 | defsubr (&Soverlay_properties); | |
2eec3b4e | 6315 | defsubr (&Soverlays_at); |
74514898 | 6316 | defsubr (&Soverlays_in); |
2eec3b4e | 6317 | defsubr (&Snext_overlay_change); |
239c932b | 6318 | defsubr (&Sprevious_overlay_change); |
2eec3b4e RS |
6319 | defsubr (&Soverlay_recenter); |
6320 | defsubr (&Soverlay_lists); | |
6321 | defsubr (&Soverlay_get); | |
6322 | defsubr (&Soverlay_put); | |
a8c21b48 | 6323 | defsubr (&Srestore_buffer_modified_p); |
1ab256cb RM |
6324 | } |
6325 | ||
dfcf069d | 6326 | void |
d3da34e0 | 6327 | keys_of_buffer (void) |
1ab256cb RM |
6328 | { |
6329 | initial_define_key (control_x_map, 'b', "switch-to-buffer"); | |
6330 | initial_define_key (control_x_map, 'k', "kill-buffer"); | |
4158c17d RM |
6331 | |
6332 | /* This must not be in syms_of_buffer, because Qdisabled is not | |
6333 | initialized when that function gets called. */ | |
d67b4f80 | 6334 | Fput (intern_c_string ("erase-buffer"), Qdisabled, Qt); |
1ab256cb | 6335 | } |