Mercurial > hg > xemacs-beta
annotate src/buffer.h @ 5043:d0c14ea98592
various frame-geometry fixes
-------------------- ChangeLog entries follow: --------------------
src/ChangeLog addition:
2010-02-15 Ben Wing <ben@xemacs.org>
* EmacsFrame.c:
* EmacsFrame.c (EmacsFrameResize):
* console-msw-impl.h:
* console-msw-impl.h (struct mswindows_frame):
* console-msw-impl.h (FRAME_MSWINDOWS_TARGET_RECT):
* device-tty.c:
* device-tty.c (tty_asynch_device_change):
* event-msw.c:
* event-msw.c (mswindows_wnd_proc):
* faces.c (Fface_list):
* faces.h:
* frame-gtk.c:
* frame-gtk.c (gtk_set_initial_frame_size):
* frame-gtk.c (gtk_set_frame_size):
* frame-msw.c:
* frame-msw.c (mswindows_init_frame_1):
* frame-msw.c (mswindows_set_frame_size):
* frame-msw.c (mswindows_size_frame_internal):
* frame-msw.c (msprinter_init_frame_3):
* frame.c:
* frame.c (enum):
* frame.c (Fmake_frame):
* frame.c (adjust_frame_size):
* frame.c (store_minibuf_frame_prop):
* frame.c (Fframe_property):
* frame.c (Fframe_properties):
* frame.c (Fframe_displayable_pixel_height):
* frame.c (Fframe_displayable_pixel_width):
* frame.c (internal_set_frame_size):
* frame.c (Fset_frame_height):
* frame.c (Fset_frame_pixel_height):
* frame.c (Fset_frame_displayable_pixel_height):
* frame.c (Fset_frame_width):
* frame.c (Fset_frame_pixel_width):
* frame.c (Fset_frame_displayable_pixel_width):
* frame.c (Fset_frame_size):
* frame.c (Fset_frame_pixel_size):
* frame.c (Fset_frame_displayable_pixel_size):
* frame.c (frame_conversion_internal_1):
* frame.c (get_frame_displayable_pixel_size):
* frame.c (change_frame_size_1):
* frame.c (change_frame_size):
* frame.c (generate_title_string):
* frame.h:
* gtk-xemacs.c:
* gtk-xemacs.c (gtk_xemacs_size_request):
* gtk-xemacs.c (gtk_xemacs_size_allocate):
* gtk-xemacs.c (gtk_xemacs_paint):
* gutter.c:
* gutter.c (update_gutter_geometry):
* redisplay.c (end_hold_frame_size_changes):
* redisplay.c (redisplay_frame):
* toolbar.c:
* toolbar.c (update_frame_toolbars_geometry):
* window.c:
* window.c (frame_pixsize_valid_p):
* window.c (check_frame_size):
Various fixes to frame geometry to make it a bit easier to understand
and fix some bugs.
1. IMPORTANT: Some renamings. Will need to be applied carefully to
the carbon repository, in the following order:
-- pixel_to_char_size -> pixel_to_frame_unit_size
-- char_to_pixel_size -> frame_unit_to_pixel_size
-- pixel_to_real_char_size -> pixel_to_char_size
-- char_to_real_pixel_size -> char_to_pixel_size
-- Reverse second and third arguments of change_frame_size() and
change_frame_size_1() to try to make functions consistent in
putting width before height.
-- Eliminate old round_size_to_char, because it didn't really
do anything differently from round_size_to_real_char()
-- round_size_to_real_char -> round_size_to_char; any places that
called the old round_size_to_char should just call the new one.
2. IMPORTANT FOR CARBON: The set_frame_size() method is now passed
sizes in "frame units", like all other frame-sizing functions,
rather than some hacked-up combination of char-cell units and
total pixel size. This only affects window systems that use
"pixelated geometry", and I'm not sure if Carbon is one of them.
MS Windows is pixelated, X and GTK are not. For pixelated-geometry
systems, the size in set_frame_size() is in displayable pixels
rather than total pixels and needs to be converted appropriately;
take a look at the changes made to mswindows_set_frame_size()
method if necessary.
3. Add a big long comment in frame.c describing how frame geometry
works.
4. Remove MS Windows-specific character height and width fields,
duplicative and unused.
5. frame-displayable-pixel-* and set-frame-displayable-pixel-*
didn't use to work on MS Windows, but they do now.
6. In general, clean up the handling of "pixelated geometry" so
that fewer functions have to worry about this. This is really
an abomination that should be removed entirely but that will
have to happen later. Fix some buggy code in
frame_conversion_internal() that happened to "work" because it
was countered by oppositely buggy code in change_frame_size().
7. Clean up some frame-size code in toolbar.c and use functions
already provided in frame.c instead of rolling its own.
8. Fix check_frame_size() in window.c, which formerly didn't take
pixelated geometry into account.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Mon, 15 Feb 2010 22:14:11 -0600 |
parents | 16112448d484 |
children | b5df3737028a |
rev | line source |
---|---|
428 | 1 /* Header file for the buffer manipulation primitives. |
2 Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995 | |
3 Free Software Foundation, Inc. | |
4 Copyright (C) 1995 Sun Microsystems, Inc. | |
2367 | 5 Copyright (C) 2001, 2002, 2004 Ben Wing. |
428 | 6 |
7 This file is part of XEmacs. | |
8 | |
9 XEmacs is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
11 Free Software Foundation; either version 2, or (at your option) any | |
12 later version. | |
13 | |
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
20 along with XEmacs; see the file COPYING. If not, write to | |
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 Boston, MA 02111-1307, USA. */ | |
23 | |
24 /* Synched up with: FSF 19.30. */ | |
25 | |
26 /* Authorship: | |
27 | |
28 FSF: long ago. | |
29 JWZ: separated out bufslots.h, early in Lemacs. | |
30 Ben Wing: almost completely rewritten for Mule, 19.12. | |
31 */ | |
32 | |
440 | 33 #ifndef INCLUDED_buffer_h_ |
34 #define INCLUDED_buffer_h_ | |
428 | 35 |
446 | 36 #include "casetab.h" |
37 #include "chartab.h" | |
38 | |
428 | 39 /************************************************************************/ |
40 /* */ | |
41 /* definition of Lisp buffer object */ | |
42 /* */ | |
43 /************************************************************************/ | |
44 | |
665 | 45 /* Note: we keep both Bytebpos and Charbpos versions of some of the |
428 | 46 important buffer positions because they are accessed so much. |
47 If we didn't do this, we would constantly be invalidating the | |
665 | 48 charbpos<->bytebpos cache under Mule. |
428 | 49 |
50 Note that under non-Mule, both versions will always be the | |
51 same so we don't really need to keep track of them. But it | |
52 simplifies the logic to go ahead and do so all the time and | |
53 the memory loss is insignificant. */ | |
54 | |
55 /* Formerly, it didn't much matter what went inside the struct buffer_text | |
56 and what went outside it. Now it does, with the advent of "indirect | |
57 buffers" that share text with another buffer. An indirect buffer | |
58 shares the same *text* as another buffer, but has its own buffer-local | |
59 variables, its own accessible region, and its own markers and extents. | |
60 (Due to the nature of markers, it doesn't actually matter much whether | |
61 we stick them inside or out of the struct buffer_text -- the user won't | |
62 notice any difference -- but we go ahead and put them outside for | |
63 consistency and overall saneness of algorithm.) | |
64 | |
65 FSFmacs gets away with not maintaining any "children" pointers from | |
66 a buffer to the indirect buffers that refer to it by putting the | |
67 markers inside of the struct buffer_text, using markers to keep track | |
68 of BEGV and ZV in indirect buffers, and relying on the fact that | |
69 all intervals (text properties and overlays) use markers for their | |
70 start and end points. We don't do this for extents (markers are | |
71 inefficient anyway and take up space), so we have to maintain | |
72 children pointers. This is not terribly hard, though, and the | |
73 code to maintain this is just like the code already present in | |
74 extent-parent and extent-children. | |
75 */ | |
76 | |
2367 | 77 #define NUM_CACHED_POSITIONS 50 |
78 #define NUM_MOVED_POSITIONS 10 | |
79 | |
428 | 80 struct buffer_text |
81 { | |
3092 | 82 #ifdef NEW_GC |
83 struct lrecord_header header; | |
84 #endif /* NEW_GC */ | |
867 | 85 Ibyte *beg; /* Actual address of buffer contents. */ |
665 | 86 Bytebpos gpt; /* Index of gap in buffer. */ |
2367 | 87 Charbpos bufgpt; /* Equivalent as a Charbpos. */ |
665 | 88 Bytebpos z; /* Index of end of buffer. */ |
2367 | 89 Charbpos bufz; /* Equivalent as a Charbpos. */ |
665 | 90 Bytecount gap_size;/* Size of buffer's gap */ |
91 Bytecount end_gap_size;/* Size of buffer's end gap */ | |
428 | 92 long modiff; /* This counts buffer-modification events |
93 for this buffer. It is incremented for | |
94 each such event, and never otherwise | |
95 changed. */ | |
96 long save_modiff; /* Previous value of modiff, as of last | |
97 time buffer visited or saved a file. */ | |
98 | |
99 #ifdef MULE | |
2367 | 100 |
101 #ifdef OLD_BYTE_CHAR | |
771 | 102 /* We keep track of a "known" region for very fast access. This |
103 information is text-only so it goes here. We update this at each | |
104 change to the buffer, so if it's entirely ASCII, these will always | |
105 contain the minimum and maximum positions of the buffer. */ | |
665 | 106 Charbpos mule_bufmin, mule_bufmax; |
107 Bytebpos mule_bytmin, mule_bytmax; | |
428 | 108 int mule_shifter, mule_three_p; |
2367 | 109 #endif |
428 | 110 |
2367 | 111 /* And we also cache NUM_CACHED_POSITIONS positions for fairly fast |
112 access near those positions. */ | |
113 Charbpos mule_charbpos_cache[NUM_CACHED_POSITIONS]; | |
114 Bytebpos mule_bytebpos_cache[NUM_CACHED_POSITIONS]; | |
115 int next_cache_pos; | |
116 | |
117 Charbpos cached_charpos; | |
118 Bytebpos cached_bytepos; | |
771 | 119 |
826 | 120 /* True if all chars fit into one byte; |
121 == (format == FORMAT_8_BIT_FIXED || | |
122 (format == FORMAT_DEFAULT && num_ascii_chars == bufz - 1)) | |
123 kept around to speed up (slightly?) the byte-char conversion routines. */ | |
124 int entirely_one_byte_p; | |
125 /* Number of ASCII chars in buffer (0 - 127) */ | |
126 Charcount num_ascii_chars; | |
127 /* Number of chars in buffer that would fit in an 8-bit-fixed buffer. */ | |
128 Charcount num_8_bit_fixed_chars; | |
129 /* Number of chars in buffer that would fit in an 16-bit-fixed buffer. */ | |
130 Charcount num_16_bit_fixed_chars; | |
131 | |
132 /* Currently we only handle 8 bit fixed and default */ | |
133 Internal_Format format; | |
2367 | 134 #endif /* MULE */ |
428 | 135 |
136 /* Similar to the above, we keep track of positions for which line | |
137 number has last been calculated. See line-number.c. */ | |
138 Lisp_Object line_number_cache; | |
139 | |
140 /* Change data that goes with the text. */ | |
141 struct buffer_text_change_data *changes; | |
142 }; | |
143 | |
3092 | 144 #ifdef NEW_GC |
145 typedef struct buffer_text Lisp_Buffer_Text; | |
146 | |
147 DECLARE_LRECORD (buffer_text, Lisp_Buffer_Text); | |
148 | |
149 #define XBUFFER_TEXT(x) \ | |
150 XRECORD (x, buffer_text, Lisp_Buffer_Text) | |
151 #define wrap_buffer_text(p) wrap_record (p, buffer_text) | |
152 #define BUFFER_TEXT_P(x) RECORDP (x, buffer_text) | |
153 #define CHECK_BUFFER_TEXT(x) CHECK_RECORD (x, buffer_text) | |
154 #define CONCHECK_BUFFER_TEXT(x) CONCHECK_RECORD (x, buffer_text) | |
155 #endif /* NEW_GC */ | |
156 | |
157 | |
428 | 158 struct buffer |
159 { | |
3017 | 160 struct LCRECORD_HEADER header; |
428 | 161 |
162 /* This structure holds the coordinates of the buffer contents | |
163 in ordinary buffers. In indirect buffers, this is not used. */ | |
164 struct buffer_text own_text; | |
165 | |
166 /* This points to the `struct buffer_text' that is used for this buffer. | |
167 In an ordinary buffer, this is the own_text field above. | |
168 In an indirect buffer, this is the own_text field of another buffer. */ | |
169 struct buffer_text *text; | |
170 | |
665 | 171 Bytebpos pt; /* Position of point in buffer. */ |
172 Charbpos bufpt; /* Equivalent as a Charbpos. */ | |
173 Bytebpos begv; /* Index of beginning of accessible range. */ | |
174 Charbpos bufbegv; /* Equivalent as a Charbpos. */ | |
175 Bytebpos zv; /* Index of end of accessible range. */ | |
176 Charbpos bufzv; /* Equivalent as a Charbpos. */ | |
428 | 177 |
178 int face_change; /* This is set when a change in how the text should | |
179 be displayed (e.g., font, color) is made. */ | |
180 | |
448 | 181 /* Whether buffer specific face is specified. */ |
182 int buffer_local_face_property; | |
183 | |
428 | 184 /* change data indicating what portion of the text has changed |
185 since the last time this was reset. Used by redisplay. | |
186 Logically we should keep this with the text structure, but | |
187 redisplay resets it for each buffer individually and we don't | |
188 want interference between an indirect buffer and its base | |
189 buffer. */ | |
190 struct each_buffer_change_data *changes; | |
191 | |
192 #ifdef REGION_CACHE_NEEDS_WORK | |
193 /* If the long line scan cache is enabled (i.e. the buffer-local | |
194 variable cache-long-line-scans is non-nil), newline_cache | |
195 points to the newline cache, and width_run_cache points to the | |
196 width run cache. | |
197 | |
198 The newline cache records which stretches of the buffer are | |
199 known *not* to contain newlines, so that they can be skipped | |
200 quickly when we search for newlines. | |
201 | |
202 The width run cache records which stretches of the buffer are | |
203 known to contain characters whose widths are all the same. If | |
204 the width run cache maps a character to a value > 0, that value | |
205 is the character's width; if it maps a character to zero, we | |
206 don't know what its width is. This allows compute_motion to | |
207 process such regions very quickly, using algebra instead of | |
208 inspecting each character. See also width_table, below. */ | |
209 struct region_cache *newline_cache; | |
210 struct region_cache *width_run_cache; | |
211 #endif /* REGION_CACHE_NEEDS_WORK */ | |
212 | |
213 /* The markers that refer to this buffer. This is actually a single | |
214 marker -- successive elements in its marker `chain' are the other | |
215 markers referring to this buffer */ | |
440 | 216 Lisp_Marker *markers; |
428 | 217 |
218 /* The buffer's extent info. This is its own type, an extent-info | |
219 object (done this way for ease in marking / finalizing). */ | |
220 Lisp_Object extent_info; | |
221 | |
826 | 222 /* The buffer's syntax cache. This caches a known region where the |
223 `syntax-table' property is unchanged, for quick lookup in the routines | |
224 that scan a buffer looking for a particular syntax (regex routines, | |
225 parse-partial-sexp, etc.). */ | |
226 struct syntax_cache *syntax_cache; | |
227 | |
428 | 228 /* ----------------------------------------------------------------- */ |
229 /* All the stuff above this line is the responsibility of insdel.c, | |
826 | 230 with some help from marker.c, extents.c and syntax.c. |
428 | 231 All the stuff below this line is the responsibility of buffer.c. */ |
232 | |
233 /* In an indirect buffer, this points to the base buffer. | |
234 In an ordinary buffer, it is 0. | |
235 We DO mark through this slot. */ | |
236 struct buffer *base_buffer; | |
237 | |
238 /* List of indirect buffers whose base is this buffer. | |
239 If we are an indirect buffer, this will be nil. | |
240 Do NOT mark through this. */ | |
241 Lisp_Object indirect_children; | |
242 | |
243 /* Flags saying which DEFVAR_PER_BUFFER variables | |
244 are local to this buffer. */ | |
245 int local_var_flags; | |
246 | |
247 /* Set to the modtime of the visited file when read or written. | |
248 -1 means visited file was nonexistent. | |
249 0 means visited file modtime unknown; in no case complain | |
250 about any mismatch on next save attempt. */ | |
251 int modtime; | |
252 | |
253 /* the value of text->modiff at the last auto-save. */ | |
442 | 254 long auto_save_modified; |
428 | 255 |
256 /* The time at which we detected a failure to auto-save, | |
257 Or -1 if we didn't have a failure. */ | |
258 int auto_save_failure_time; | |
259 | |
260 /* Position in buffer at which display started | |
261 the last time this buffer was displayed. */ | |
262 int last_window_start; | |
263 | |
264 /* Everything from here down must be a Lisp_Object */ | |
265 | |
1204 | 266 #define MARKED_SLOT(x) Lisp_Object x; |
428 | 267 #include "bufslots.h" |
268 #undef MARKED_SLOT | |
269 }; | |
270 | |
271 DECLARE_LRECORD (buffer, struct buffer); | |
272 #define XBUFFER(x) XRECORD (x, buffer, struct buffer) | |
617 | 273 #define wrap_buffer(p) wrap_record (p, buffer) |
428 | 274 #define BUFFERP(x) RECORDP (x, buffer) |
275 #define CHECK_BUFFER(x) CHECK_RECORD (x, buffer) | |
276 #define CONCHECK_BUFFER(x) CONCHECK_RECORD (x, buffer) | |
277 | |
278 #define BUFFER_LIVE_P(b) (!NILP ((b)->name)) | |
279 | |
280 #define CHECK_LIVE_BUFFER(x) do { \ | |
281 CHECK_BUFFER (x); \ | |
282 if (!BUFFER_LIVE_P (XBUFFER (x))) \ | |
283 dead_wrong_type_argument (Qbuffer_live_p, (x)); \ | |
284 } while (0) | |
285 | |
286 #define CONCHECK_LIVE_BUFFER(x) do { \ | |
287 CONCHECK_BUFFER (x); \ | |
288 if (!BUFFER_LIVE_P (XBUFFER (x))) \ | |
289 x = wrong_type_argument (Qbuffer_live_p, (x)); \ | |
290 } while (0) | |
291 | |
292 | |
293 #define BUFFER_BASE_BUFFER(b) ((b)->base_buffer ? (b)->base_buffer : (b)) | |
294 | |
295 /* Map over buffers sharing the same text as MPS_BUF. MPS_BUFVAR is a | |
296 variable that gets the buffer values (beginning with the base | |
297 buffer, then the children), and MPS_BUFCONS should be a temporary | |
298 Lisp_Object variable. */ | |
647 | 299 #define MAP_INDIRECT_BUFFERS(mps_buf, mps_bufvar, mps_bufcons) \ |
300 for (mps_bufcons = Qunbound, \ | |
301 mps_bufvar = BUFFER_BASE_BUFFER (mps_buf); \ | |
302 UNBOUNDP (mps_bufcons) ? \ | |
303 (mps_bufcons = mps_bufvar->indirect_children, \ | |
304 1) \ | |
305 : (!NILP (mps_bufcons) \ | |
306 && (mps_bufvar = XBUFFER (XCAR (mps_bufcons)), 1) \ | |
307 && (mps_bufcons = XCDR (mps_bufcons), 1)); \ | |
428 | 308 ) |
309 | |
310 | |
826 | 311 /* All macros below follow the three golden rules of macros (see text.h), |
312 with the following exception: | |
313 | |
314 -- 'struct buffer *' arguments can be evaluated more than once. | |
315 */ | |
316 | |
428 | 317 /*----------------------------------------------------------------------*/ |
318 /* Accessor macros for important positions in a buffer */ | |
319 /*----------------------------------------------------------------------*/ | |
320 | |
321 /* We put them here because some stuff below wants them before the | |
322 place where we would normally put them. */ | |
323 | |
324 /* None of these are lvalues. Use the settor macros below to change | |
325 the positions. */ | |
326 | |
327 /* Beginning of buffer. */ | |
826 | 328 #define BYTE_BUF_BEG(buf) ((Bytebpos) 1) |
665 | 329 #define BUF_BEG(buf) ((Charbpos) 1) |
428 | 330 |
331 /* Beginning of accessible range of buffer. */ | |
826 | 332 #define BYTE_BUF_BEGV(buf) ((buf)->begv + 0) |
428 | 333 #define BUF_BEGV(buf) ((buf)->bufbegv + 0) |
334 | |
335 /* End of accessible range of buffer. */ | |
826 | 336 #define BYTE_BUF_ZV(buf) ((buf)->zv + 0) |
428 | 337 #define BUF_ZV(buf) ((buf)->bufzv + 0) |
338 | |
339 /* End of buffer. */ | |
826 | 340 #define BYTE_BUF_Z(buf) ((buf)->text->z + 0) |
428 | 341 #define BUF_Z(buf) ((buf)->text->bufz + 0) |
342 | |
2367 | 343 /* Gap location. */ |
344 #define BYTE_BUF_GPT(buf) ((buf)->text->gpt + 0) | |
345 #define BUF_GPT(buf) ((buf)->text->bufgpt + 0) | |
346 | |
428 | 347 /* Point. */ |
826 | 348 #define BYTE_BUF_PT(buf) ((buf)->pt + 0) |
428 | 349 #define BUF_PT(buf) ((buf)->bufpt + 0) |
350 | |
826 | 351 /* Internal format of buffer. */ |
352 #ifdef MULE | |
353 #define BUF_FORMAT(buf) ((buf)->text->format) | |
354 #else | |
355 #define BUF_FORMAT(buf) FORMAT_DEFAULT | |
356 #endif | |
357 | |
428 | 358 /*----------------------------------------------------------------------*/ |
826 | 359 /* Validating byte positions */ |
428 | 360 /*----------------------------------------------------------------------*/ |
361 | |
826 | 362 /* Address of byte at position POS in buffer, no error checking. */ |
363 DECLARE_INLINE_HEADER ( | |
867 | 364 Ibyte * |
826 | 365 BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (struct buffer *buf, Bytebpos pos) |
366 ) | |
428 | 367 { |
368 return (buf->text->beg + | |
369 ((pos >= buf->text->gpt ? (pos + buf->text->gap_size) : pos) | |
370 - 1)); | |
371 } | |
372 | |
826 | 373 /* Given a byte position, does it point to the beginning of a character? |
374 */ | |
375 #ifdef MULE | |
376 DECLARE_INLINE_HEADER ( | |
377 int | |
378 VALID_BYTEBPOS_P (struct buffer *buf, Bytebpos x) | |
379 ) | |
428 | 380 { |
826 | 381 switch (BUF_FORMAT (buf)) |
382 { | |
383 case FORMAT_DEFAULT: | |
867 | 384 return ibyte_first_byte_p (*BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, x)); |
826 | 385 case FORMAT_16_BIT_FIXED: |
386 return ((x - 1) & 1) == 0; | |
387 case FORMAT_32_BIT_FIXED: | |
388 return ((x - 1) & 3) == 0; | |
389 default: | |
390 return 1; | |
391 } | |
428 | 392 } |
393 #else | |
665 | 394 # define VALID_BYTEBPOS_P(buf, x) 1 |
428 | 395 #endif |
396 | |
826 | 397 /* If error-checking is enabled, assert that the given char position is |
398 within range. Otherwise, do nothing. | |
399 */ | |
400 # define ASSERT_VALID_CHARBPOS_UNSAFE(buf, x) do { \ | |
401 text_checking_assert (BUFFER_LIVE_P (buf)); \ | |
402 text_checking_assert ((x) >= BUF_BEG (buf) && x <= BUF_Z (buf)); \ | |
428 | 403 } while (0) |
826 | 404 |
405 /* If error-checking is enabled, assert that the given byte position is | |
406 within range and points to the beginning of a character or to the end of | |
407 the buffer. Otherwise, do nothing. | |
408 */ | |
409 # define ASSERT_VALID_BYTEBPOS_UNSAFE(buf, x) do { \ | |
410 text_checking_assert (BUFFER_LIVE_P (buf)); \ | |
411 text_checking_assert ((x) >= BYTE_BUF_BEG (buf) && x <= BYTE_BUF_Z (buf)); \ | |
412 text_checking_assert (VALID_BYTEBPOS_P (buf, x)); \ | |
428 | 413 } while (0) |
826 | 414 |
415 /* If error-checking is enabled, assert that the given byte position is | |
416 within range and satisfies ASSERT_VALID_BYTEBPOS() and also does not | |
417 refer to the beginning of the buffer. (i.e. movement backwards is OK.) | |
418 Otherwise, do nothing. | |
419 */ | |
420 # define ASSERT_VALID_BYTEBPOS_BACKWARD_UNSAFE(buf, x) do { \ | |
421 text_checking_assert (BUFFER_LIVE_P (buf)); \ | |
422 text_checking_assert ((x) > BYTE_BUF_BEG (buf) && x <= BYTE_BUF_Z (buf)); \ | |
423 text_checking_assert (VALID_BYTEBPOS_P (buf, x)); \ | |
428 | 424 } while (0) |
425 | |
826 | 426 /* If error-checking is enabled, assert that the given byte position is |
427 within range and satisfies ASSERT_VALID_BYTEBPOS() and also does not | |
428 refer to the end of the buffer. (i.e. movement forwards is OK.) | |
429 Otherwise, do nothing. | |
430 */ | |
431 # define ASSERT_VALID_BYTEBPOS_FORWARD_UNSAFE(buf, x) do { \ | |
432 text_checking_assert (BUFFER_LIVE_P (buf)); \ | |
433 text_checking_assert ((x) >= BYTE_BUF_BEG (buf) && x < BYTE_BUF_Z (buf)); \ | |
434 text_checking_assert (VALID_BYTEBPOS_P (buf, x)); \ | |
435 } while (0) | |
428 | 436 |
437 #ifdef MULE | |
826 | 438 /* Make sure that the given byte position is pointing to the beginning of a |
439 character. If not, back up until this is the case. Note that there are | |
440 not too many places where it is legitimate to do this sort of thing. | |
441 It's an error if you're passed an "invalid" byte position. | |
442 */ | |
443 # define VALIDATE_BYTEBPOS_BACKWARD(buf, x) do { \ | |
444 switch (BUF_FORMAT (buf)) \ | |
445 { \ | |
446 case FORMAT_DEFAULT: \ | |
447 { \ | |
867 | 448 Ibyte *VBB_ptr = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, x); \ |
449 while (!ibyte_first_byte_p (*VBB_ptr)) \ | |
826 | 450 VBB_ptr--, (x)--; \ |
451 } \ | |
452 break; \ | |
453 case FORMAT_16_BIT_FIXED: \ | |
454 if (((x - 1) & 1) != 0) \ | |
455 x--; \ | |
456 break; \ | |
457 case FORMAT_32_BIT_FIXED: \ | |
458 while (((x - 1) & 3) != 0) \ | |
459 x--; \ | |
460 break; \ | |
461 default: \ | |
462 break; \ | |
463 } \ | |
428 | 464 } while (0) |
465 #else | |
665 | 466 # define VALIDATE_BYTEBPOS_BACKWARD(buf, x) |
428 | 467 #endif |
468 | |
469 #ifdef MULE | |
826 | 470 /* Make sure that the given byte position is pointing to the beginning of a |
471 character. If not, move forward until this is the case. Note that | |
472 there are not too many places where it is legitimate to do this sort of | |
473 thing. It's an error if you're passed an "invalid" byte position. | |
474 */ | |
475 # define VALIDATE_BYTEBPOS_FORWARD(buf, x) do { \ | |
476 switch (BUF_FORMAT (buf)) \ | |
477 { \ | |
478 case FORMAT_DEFAULT: \ | |
479 { \ | |
867 | 480 Ibyte *VBF_ptr = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, x); \ |
481 while (!ibyte_first_byte_p (*VBF_ptr)) \ | |
826 | 482 VBF_ptr++, (x)++; \ |
483 } \ | |
484 break; \ | |
485 case FORMAT_16_BIT_FIXED: \ | |
486 if (((x - 1) & 1) != 0) \ | |
487 x++; \ | |
488 break; \ | |
489 case FORMAT_32_BIT_FIXED: \ | |
490 while (((x - 1) & 3) != 0) \ | |
491 x++; \ | |
492 break; \ | |
493 default: \ | |
494 break; \ | |
495 } \ | |
428 | 496 } while (0) |
497 #else | |
665 | 498 # define VALIDATE_BYTEBPOS_FORWARD(buf, x) |
428 | 499 #endif |
500 | |
826 | 501 /*----------------------------------------------------------------------*/ |
502 /* Working with byte positions */ | |
503 /*----------------------------------------------------------------------*/ | |
504 | |
505 | |
506 /* Given a byte position (assumed to point at the beginning of a | |
507 character), modify that value so it points to the beginning of the next | |
508 character. | |
509 | |
510 Note that in the simplest case (no MULE, no ERROR_CHECK_TEXT), | |
511 this crap reduces down to simply (x)++. */ | |
428 | 512 |
665 | 513 #define INC_BYTEBPOS(buf, x) do \ |
428 | 514 { \ |
826 | 515 ASSERT_VALID_BYTEBPOS_FORWARD_UNSAFE (buf, x); \ |
428 | 516 /* Note that we do the increment first to \ |
517 make sure that the pointer in \ | |
665 | 518 VALIDATE_BYTEBPOS_FORWARD() ends up on \ |
428 | 519 the correct side of the gap */ \ |
520 (x)++; \ | |
665 | 521 VALIDATE_BYTEBPOS_FORWARD (buf, x); \ |
428 | 522 } while (0) |
523 | |
826 | 524 /* Given a byte position (assumed to point at the beginning of a |
525 character), modify that value so it points to the beginning of the | |
867 | 526 previous character. Unlike for DEC_IBYTEPTR(), we can do all the |
826 | 527 assert()s because there are sentinels at the beginning of the gap and |
528 the end of the buffer. | |
529 | |
530 Note that in the simplest case (no MULE, no ERROR_CHECK_TEXT), this | |
531 crap reduces down to simply (x)--. */ | |
428 | 532 |
665 | 533 #define DEC_BYTEBPOS(buf, x) do \ |
428 | 534 { \ |
771 | 535 ASSERT_VALID_BYTEBPOS_BACKWARD_UNSAFE (buf, x); \ |
428 | 536 /* Note that we do the decrement first to \ |
537 make sure that the pointer in \ | |
665 | 538 VALIDATE_BYTEBPOS_BACKWARD() ends up on \ |
428 | 539 the correct side of the gap */ \ |
540 (x)--; \ | |
665 | 541 VALIDATE_BYTEBPOS_BACKWARD (buf, x); \ |
428 | 542 } while (0) |
543 | |
826 | 544 DECLARE_INLINE_HEADER ( |
545 Bytebpos | |
4860 | 546 prev_bytebpos (struct buffer *buf, Bytebpos x) |
826 | 547 ) |
428 | 548 { |
665 | 549 DEC_BYTEBPOS (buf, x); |
428 | 550 return x; |
551 } | |
552 | |
826 | 553 DECLARE_INLINE_HEADER ( |
554 Bytebpos | |
4860 | 555 next_bytebpos (struct buffer *buf, Bytebpos x) |
826 | 556 ) |
428 | 557 { |
665 | 558 INC_BYTEBPOS (buf, x); |
428 | 559 return x; |
560 } | |
561 | |
826 | 562 /* A constant representing an invalid Bytebpos. Valid Bytebposes |
563 can never have this value. */ | |
564 | |
665 | 565 #define BYTEBPOS_INVALID ((Bytebpos) -1) |
428 | 566 |
567 /*----------------------------------------------------------------------*/ | |
826 | 568 /* Converting between byte and character positions */ |
428 | 569 /*----------------------------------------------------------------------*/ |
570 | |
2367 | 571 /* |
771 | 572 |
2367 | 573 Info on Byte-Char conversion: |
428 | 574 |
2367 | 575 (Info-goto-node "(internals)Byte-Char Position Conversion") |
576 */ | |
428 | 577 |
2367 | 578 #ifdef MULE |
428 | 579 |
826 | 580 Bytebpos charbpos_to_bytebpos_func (struct buffer *buf, Charbpos x); |
581 Charbpos bytebpos_to_charbpos_func (struct buffer *buf, Bytebpos x); | |
428 | 582 extern short three_to_one_table[]; |
583 | |
826 | 584 #endif /* MULE */ |
585 | |
586 /* Given a Charbpos, return the equivalent Bytebpos. */ | |
587 | |
588 DECLARE_INLINE_HEADER ( | |
589 Bytebpos | |
4860 | 590 charbpos_to_bytebpos (struct buffer *buf, Charbpos x) |
826 | 591 ) |
428 | 592 { |
826 | 593 Bytebpos retval; |
594 ASSERT_VALID_CHARBPOS_UNSAFE (buf, x); | |
595 #ifdef MULE | |
596 if (buf->text->entirely_one_byte_p) | |
597 retval = (Bytebpos) x; | |
598 else if (BUF_FORMAT (buf) == FORMAT_16_BIT_FIXED) | |
599 retval = (Bytebpos) (x << 1); | |
600 else if (BUF_FORMAT (buf) == FORMAT_32_BIT_FIXED) | |
601 retval = (Bytebpos) (x << 2); | |
2367 | 602 #ifdef OLD_BYTE_CHAR |
826 | 603 else if (x >= buf->text->mule_bufmin && x <= buf->text->mule_bufmax) |
604 retval = (buf->text->mule_bytmin + | |
428 | 605 ((x - buf->text->mule_bufmin) << buf->text->mule_shifter) + |
814 | 606 (buf->text->mule_three_p ? (x - buf->text->mule_bufmin) : |
607 (Bytebpos) 0)); | |
2367 | 608 #endif /* OLD_BYTE_CHAR */ |
428 | 609 else |
826 | 610 retval = charbpos_to_bytebpos_func (buf, x); |
611 #else | |
612 retval = (Bytebpos) x; | |
613 #endif | |
614 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, retval); | |
615 return retval; | |
616 } | |
617 | |
618 /* Given a Bytebpos, return the equivalent Charbpos. */ | |
619 | |
620 DECLARE_INLINE_HEADER ( | |
621 Charbpos | |
4860 | 622 bytebpos_to_charbpos (struct buffer *buf, Bytebpos x) |
826 | 623 ) |
624 { | |
625 Charbpos retval; | |
626 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, x); | |
627 #ifdef MULE | |
628 if (buf->text->entirely_one_byte_p) | |
629 retval = (Charbpos) x; | |
630 else if (BUF_FORMAT (buf) == FORMAT_16_BIT_FIXED) | |
631 retval = (Charbpos) (x >> 1); | |
632 else if (BUF_FORMAT (buf) == FORMAT_32_BIT_FIXED) | |
633 retval = (Charbpos) (x >> 2); | |
2367 | 634 #ifdef OLD_BYTE_CHAR |
826 | 635 else if (x >= buf->text->mule_bytmin && x <= buf->text->mule_bytmax) |
636 retval = (buf->text->mule_bufmin + | |
637 ((buf->text->mule_three_p | |
638 ? three_to_one_table[x - buf->text->mule_bytmin] | |
639 : (x - buf->text->mule_bytmin) >> buf->text->mule_shifter))); | |
2367 | 640 #endif /* OLD_BYTE_CHAR */ |
826 | 641 else |
642 retval = bytebpos_to_charbpos_func (buf, x); | |
643 #else | |
644 retval = (Charbpos) x; | |
645 #endif | |
646 ASSERT_VALID_CHARBPOS_UNSAFE (buf, retval); | |
647 return retval; | |
648 } | |
649 | |
650 /* Given a Bytebpos, return the equivalent Charbpos as a Lisp Object. */ | |
651 | |
652 #define make_charbpos(buf, ind) make_int (bytebpos_to_charbpos (buf, ind)) | |
653 | |
654 /*----------------------------------------------------------------------*/ | |
655 /* Converting between byte and memory positions */ | |
656 /*----------------------------------------------------------------------*/ | |
657 | |
658 DECLARE_INLINE_HEADER ( | |
659 int | |
660 valid_membpos_p (struct buffer *buf, Membpos x) | |
661 ) | |
662 { | |
663 return ((x >= 1 && x <= (Membpos) buf->text->gpt) || | |
664 (x > (Membpos) (buf->text->gpt + buf->text->gap_size) && | |
665 x <= (Membpos) (buf->text->z + buf->text->gap_size))); | |
428 | 666 } |
667 | |
826 | 668 DECLARE_INLINE_HEADER ( |
669 Membpos | |
670 bytebpos_to_membpos (struct buffer *buf, Bytebpos x) | |
671 ) | |
672 { | |
673 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, x); | |
674 return (Membpos) ((x > buf->text->gpt) ? (x + buf->text->gap_size) : x); | |
675 } | |
676 | |
677 | |
678 DECLARE_INLINE_HEADER ( | |
679 Bytebpos | |
680 membpos_to_bytebpos (struct buffer *buf, Membpos x) | |
681 ) | |
682 { | |
683 Bytebpos retval; | |
684 text_checking_assert (valid_membpos_p (buf, x)); | |
685 retval = (Bytebpos) ((x > (Membpos) buf->text->gpt) ? | |
686 x - buf->text->gap_size : | |
687 x); | |
688 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, retval); | |
689 return retval; | |
690 } | |
691 | |
692 DECLARE_INLINE_HEADER ( | |
693 Charbpos | |
694 membpos_to_charbpos (struct buffer *buf, Membpos x) | |
695 ) | |
428 | 696 { |
826 | 697 return bytebpos_to_charbpos (buf, membpos_to_bytebpos (buf, x)); |
698 } | |
699 | |
700 DECLARE_INLINE_HEADER ( | |
701 Membpos | |
702 charbpos_to_membpos (struct buffer *buf, Charbpos x) | |
703 ) | |
704 { | |
705 return bytebpos_to_membpos (buf, charbpos_to_bytebpos (buf, x)); | |
706 } | |
707 | |
708 /*----------------------------------------------------------------------*/ | |
709 /* Generalized buffer/string position convertors */ | |
710 /*----------------------------------------------------------------------*/ | |
711 | |
712 /* These macros generalize many standard buffer-position functions to | |
713 either a buffer or a string. */ | |
714 | |
715 /* Converting between Memxpos's and Bytexpos's, for a buffer-or-string. | |
716 For strings, this is a no-op. For buffers, this resolves | |
717 to the standard membpos<->bytebpos converters. */ | |
718 | |
719 DECLARE_INLINE_HEADER ( | |
720 Memxpos buffer_or_string_bytexpos_to_memxpos (Lisp_Object obj, Bytexpos pos) | |
721 ) | |
722 { | |
723 return (BUFFERP (obj) ? bytebpos_to_membpos (XBUFFER (obj), pos) : | |
724 (Memxpos) pos); | |
428 | 725 } |
726 | |
826 | 727 DECLARE_INLINE_HEADER ( |
728 Bytexpos buffer_or_string_memxpos_to_bytexpos (Lisp_Object obj, Memxpos pos) | |
729 ) | |
730 { | |
731 return (BUFFERP (obj) ? membpos_to_bytebpos (XBUFFER (obj), pos) : | |
732 (Bytexpos) pos); | |
733 } | |
734 | |
735 /* Converting between Charxpos's and Bytexpos's, for a buffer-or-string. | |
736 For strings, this maps to the bytecount<->charcount converters. */ | |
737 | |
738 DECLARE_INLINE_HEADER ( | |
739 Bytexpos buffer_or_string_charxpos_to_bytexpos (Lisp_Object obj, Charxpos pos) | |
740 ) | |
741 { | |
742 return (BUFFERP (obj) ? charbpos_to_bytebpos (XBUFFER (obj), pos) : | |
743 (Bytexpos) string_index_char_to_byte (obj, pos)); | |
744 } | |
745 | |
746 DECLARE_INLINE_HEADER ( | |
747 Charxpos buffer_or_string_bytexpos_to_charxpos (Lisp_Object obj, Bytexpos pos) | |
748 ) | |
749 { | |
750 return (BUFFERP (obj) ? bytebpos_to_charbpos (XBUFFER (obj), pos) : | |
751 (Charxpos) string_index_byte_to_char (obj, pos)); | |
752 } | |
428 | 753 |
826 | 754 /* Similar for Charxpos's and Memxpos's. */ |
755 | |
756 DECLARE_INLINE_HEADER ( | |
757 Memxpos buffer_or_string_charxpos_to_memxpos (Lisp_Object obj, Charxpos pos) | |
758 ) | |
759 { | |
760 return (BUFFERP (obj) ? charbpos_to_membpos (XBUFFER (obj), pos) : | |
761 (Memxpos) string_index_char_to_byte (obj, pos)); | |
762 } | |
428 | 763 |
826 | 764 DECLARE_INLINE_HEADER ( |
765 Charxpos buffer_or_string_memxpos_to_charxpos (Lisp_Object obj, Memxpos pos) | |
766 ) | |
767 { | |
768 return (BUFFERP (obj) ? membpos_to_charbpos (XBUFFER (obj), pos) : | |
769 (Charxpos) string_index_byte_to_char (obj, pos)); | |
770 } | |
428 | 771 |
826 | 772 DECLARE_INLINE_HEADER ( |
773 Internal_Format buffer_or_other_internal_format (Lisp_Object obj) | |
774 ) | |
775 { | |
776 return BUFFERP (obj) ? BUF_FORMAT (XBUFFER (obj)) : FORMAT_DEFAULT; | |
777 } | |
778 | |
779 /* Return the index to the character before the one at X, | |
780 in a buffer or string. */ | |
428 | 781 |
826 | 782 DECLARE_INLINE_HEADER ( |
783 Bytebpos | |
784 prev_bytexpos (Lisp_Object obj, Bytebpos x) | |
785 ) | |
786 { | |
787 return BUFFERP (obj) ? prev_bytebpos (XBUFFER (obj), x) : | |
788 prev_string_index (obj, x); | |
789 } | |
790 | |
791 /* Return the index to the character after the one at X, | |
792 in a buffer or string. */ | |
428 | 793 |
826 | 794 DECLARE_INLINE_HEADER ( |
795 Bytebpos | |
796 next_bytexpos (Lisp_Object obj, Bytebpos x) | |
797 ) | |
798 { | |
799 return BUFFERP (obj) ? next_bytebpos (XBUFFER (obj), x) : | |
800 next_string_index (obj, x); | |
801 } | |
802 | |
803 /*----------------------------------------------------------------------*/ | |
804 /* Converting between positions and addresses */ | |
805 /*----------------------------------------------------------------------*/ | |
428 | 806 |
826 | 807 /* Convert the address of a byte in the buffer into a position. */ |
808 DECLARE_INLINE_HEADER ( | |
809 Bytebpos | |
867 | 810 BYTE_BUF_PTR_BYTE_POS (struct buffer *buf, Ibyte *ptr) |
826 | 811 ) |
812 { | |
813 Bytebpos retval = (ptr - buf->text->beg + 1 | |
814 - ((ptr - buf->text->beg + 1) > buf->text->gpt | |
815 ? buf->text->gap_size : (Bytebpos) 0)); | |
816 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, retval); | |
817 return retval; | |
818 } | |
819 | |
820 #define BUF_PTR_BYTE_POS(buf, ptr) \ | |
821 bytebpos_to_charbpos (buf, BYTE_BUF_PTR_BYTE_POS (buf, ptr)) | |
428 | 822 |
826 | 823 /* Address of byte at position POS in buffer. */ |
824 DECLARE_INLINE_HEADER ( | |
867 | 825 Ibyte * |
826 | 826 BYTE_BUF_BYTE_ADDRESS (struct buffer *buf, Bytebpos pos) |
827 ) | |
828 { | |
829 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, pos); | |
830 return BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos); | |
831 } | |
832 | |
833 #define BUF_BYTE_ADDRESS(buf, pos) \ | |
834 BYTE_BUF_BYTE_ADDRESS (buf, charbpos_to_bytebpos (buf, pos)) | |
428 | 835 |
826 | 836 /* Address of byte before position POS in buffer. */ |
837 DECLARE_INLINE_HEADER ( | |
867 | 838 Ibyte * |
826 | 839 BYTE_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytebpos pos) |
840 ) | |
841 { | |
842 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, pos); | |
843 return (buf->text->beg + | |
844 ((pos > buf->text->gpt ? (pos + buf->text->gap_size) : pos) | |
845 - 2)); | |
846 } | |
847 | |
848 #define BUF_BYTE_ADDRESS_BEFORE(buf, pos) \ | |
849 BYTE_BUF_BYTE_ADDRESS_BEFORE (buf, charbpos_to_bytebpos (buf, pos)) | |
428 | 850 |
851 /*----------------------------------------------------------------------*/ | |
852 /* Converting between buffer bytes and Emacs characters */ | |
853 /*----------------------------------------------------------------------*/ | |
854 | |
855 /* The character at position POS in buffer. */ | |
826 | 856 |
857 #define BYTE_BUF_FETCH_CHAR(buf, pos) \ | |
867 | 858 itext_ichar_fmt (BYTE_BUF_BYTE_ADDRESS (buf, pos), BUF_FORMAT (buf), \ |
826 | 859 wrap_buffer (buf)) |
428 | 860 #define BUF_FETCH_CHAR(buf, pos) \ |
826 | 861 BYTE_BUF_FETCH_CHAR (buf, charbpos_to_bytebpos (buf, pos)) |
862 | |
863 /* The "raw value" of the character at position POS in buffer. | |
867 | 864 See ichar_to_raw(). */ |
826 | 865 |
866 #define BYTE_BUF_FETCH_CHAR_RAW(buf, pos) \ | |
867 | 867 itext_ichar_raw_fmt (BYTE_BUF_BYTE_ADDRESS (buf, pos), BUF_FORMAT (buf)) |
826 | 868 #define BUF_FETCH_CHAR_RAW(buf, pos) \ |
869 BYTE_BUF_FETCH_CHAR_RAW (buf, charbpos_to_bytebpos (buf, pos)) | |
428 | 870 |
871 /* The character at position POS in buffer, as a string. This is | |
867 | 872 equivalent to set_itext_ichar (str, BUF_FETCH_CHAR (buf, pos)) |
428 | 873 but is faster for Mule. */ |
874 | |
867 | 875 # define BYTE_BUF_ITEXT_COPY_ICHAR(buf, pos, str) \ |
826 | 876 (BUF_FORMAT (buf) == FORMAT_DEFAULT ? \ |
867 | 877 itext_copy_ichar (BYTE_BUF_BYTE_ADDRESS (buf, pos), str) : \ |
878 set_itext_ichar (str, BYTE_BUF_FETCH_CHAR (buf, pos))) | |
879 #define BUF_ITEXT_COPY_ICHAR(buf, pos, str) \ | |
880 BYTE_BUF_ITEXT_COPY_ICHAR (buf, charbpos_to_bytebpos (buf, pos), str) | |
428 | 881 |
882 | |
883 /************************************************************************/ | |
440 | 884 /* */ |
428 | 885 /* higher-level buffer-position functions */ |
886 /* */ | |
887 /************************************************************************/ | |
888 | |
889 /*----------------------------------------------------------------------*/ | |
890 /* Settor macros for important positions in a buffer */ | |
891 /*----------------------------------------------------------------------*/ | |
892 | |
893 /* Set beginning of accessible range of buffer. */ | |
826 | 894 #define SET_BOTH_BUF_BEGV(buf, val, bpval) \ |
428 | 895 do \ |
896 { \ | |
826 | 897 (buf)->begv = (bpval); \ |
428 | 898 (buf)->bufbegv = (val); \ |
899 } while (0) | |
900 | |
901 /* Set end of accessible range of buffer. */ | |
826 | 902 #define SET_BOTH_BUF_ZV(buf, val, bpval) \ |
428 | 903 do \ |
904 { \ | |
826 | 905 (buf)->zv = (bpval); \ |
428 | 906 (buf)->bufzv = (val); \ |
907 } while (0) | |
908 | |
909 /* Set point. */ | |
910 /* Since BEGV and ZV are almost never set, it's reasonable to enforce | |
665 | 911 the restriction that the Charbpos and Bytebpos values must both be |
428 | 912 specified. However, point is set in lots and lots of places. So |
913 we provide the ability to specify both (for efficiency) or just | |
914 one. */ | |
826 | 915 #define BOTH_BUF_SET_PT(buf, val, bpval) set_buffer_point (buf, val, bpval) |
916 #define BYTE_BUF_SET_PT(buf, bpval) \ | |
917 do \ | |
918 { \ | |
919 Bytebpos __bpbsp_bpval = (bpval); \ | |
920 BOTH_BUF_SET_PT (buf, bytebpos_to_charbpos (buf, __bpbsp_bpval), \ | |
921 __bpbsp_bpval); \ | |
922 } while (0) | |
923 #define BUF_SET_PT(buf, value) \ | |
924 do \ | |
925 { \ | |
926 Bytebpos __bsp_val = (value); \ | |
927 BOTH_BUF_SET_PT (buf, __bsp_val, charbpos_to_bytebpos (buf, __bsp_val)); \ | |
928 } while (0) | |
428 | 929 |
930 | |
931 #if 0 /* FSFmacs */ | |
932 /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly | |
933 does too much stuff, such as moving out of invisible extents. */ | |
934 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer)) | |
935 #define SET_BUF_PT(buf, value) ((buf)->pt = (value)) | |
936 #endif /* FSFmacs */ | |
937 | |
938 /*----------------------------------------------------------------------*/ | |
939 /* Miscellaneous buffer values */ | |
940 /*----------------------------------------------------------------------*/ | |
941 | |
942 /* Number of characters in buffer */ | |
943 #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf)) | |
944 | |
945 /* Is this buffer narrowed? */ | |
946 #define BUF_NARROWED(buf) \ | |
826 | 947 ((BYTE_BUF_BEGV (buf) != BYTE_BUF_BEG (buf)) || \ |
948 (BYTE_BUF_ZV (buf) != BYTE_BUF_Z (buf))) | |
428 | 949 |
826 | 950 /* Modification count */ |
428 | 951 #define BUF_MODIFF(buf) ((buf)->text->modiff) |
952 | |
826 | 953 /* Saved modification count */ |
428 | 954 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff) |
955 | |
956 /* Face changed. */ | |
957 #define BUF_FACECHANGE(buf) ((buf)->face_change) | |
958 | |
826 | 959 DECLARE_INLINE_HEADER ( |
960 int | |
961 POINT_MARKER_P (Lisp_Object marker) | |
962 ) | |
963 { | |
964 return (XMARKER (marker)->buffer != 0 && | |
965 EQ (marker, XMARKER (marker)->buffer->point_marker)); | |
966 } | |
428 | 967 |
968 #define BUF_MARKERS(buf) ((buf)->markers) | |
969 | |
826 | 970 #ifdef MULE |
971 | |
972 DECLARE_INLINE_HEADER ( | |
973 Lisp_Object | |
974 BUFFER_CATEGORY_TABLE (struct buffer *buf) | |
975 ) | |
976 { | |
977 return buf ? buf->category_table : Vstandard_category_table; | |
978 } | |
979 | |
980 #endif /* MULE */ | |
981 | |
982 DECLARE_INLINE_HEADER ( | |
983 Lisp_Object | |
984 BUFFER_SYNTAX_TABLE (struct buffer *buf) | |
985 ) | |
986 { | |
987 return buf ? buf->syntax_table : Vstandard_syntax_table; | |
988 } | |
989 | |
990 DECLARE_INLINE_HEADER ( | |
991 Lisp_Object | |
992 BUFFER_MIRROR_SYNTAX_TABLE (struct buffer *buf) | |
993 ) | |
994 { | |
995 return buf ? buf->mirror_syntax_table : | |
996 XCHAR_TABLE (Vstandard_syntax_table)->mirror_table; | |
997 } | |
998 | |
428 | 999 /* WARNING: |
1000 | |
1001 The new definitions of CEILING_OF() and FLOOR_OF() differ semantically | |
1002 from the old ones (in FSF Emacs and XEmacs 19.11 and before). | |
1003 Conversion is as follows: | |
1004 | |
826 | 1005 OLD_BYTE_CEILING_OF(n) = NEW_BYTE_CEILING_OF(n) - 1 |
1006 OLD_BYTE_FLOOR_OF(n) = NEW_BYTE_FLOOR_OF(n + 1) | |
428 | 1007 |
1008 The definitions were changed because the new definitions are more | |
771 | 1009 consistent with the way everything else works in XEmacs. |
428 | 1010 */ |
1011 | |
826 | 1012 /* Properties of CEILING_OF and FLOOR_OF (also apply to BYTE_ variants): |
428 | 1013 |
1014 1) FLOOR_OF (CEILING_OF (n)) = n | |
1015 CEILING_OF (FLOOR_OF (n)) = n | |
1016 | |
1017 2) CEILING_OF (n) = n if and only if n = ZV | |
1018 FLOOR_OF (n) = n if and only if n = BEGV | |
1019 | |
1020 3) CEILING_OF (CEILING_OF (n)) = ZV | |
1021 FLOOR_OF (FLOOR_OF (n)) = BEGV | |
1022 | |
1023 4) The bytes in the regions | |
1024 | |
1025 [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))] | |
1026 | |
1027 and | |
1028 | |
1029 [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)] | |
1030 | |
1031 are contiguous. | |
771 | 1032 |
1033 A typical loop using CEILING_OF to process contiguous ranges of text | |
1034 between [from, to) looks like this: | |
1035 | |
1036 { | |
1037 Bytebpos pos = from; | |
1038 | |
1039 while (pos < to) | |
1040 { | |
826 | 1041 Bytebpos ceil = BYTE_BUF_CEILING_OF (buf, pos); |
771 | 1042 ceil = min (to, ceil); |
867 | 1043 process_ibyte_string (BYTE_BUF_BYTE_ADDRESS (buf, pos), ceil - pos); |
771 | 1044 pos = ceil; |
1045 } | |
1046 } | |
1047 | |
1048 Currently there will be at most two iterations in the loop, but it is | |
1049 written in such a way that it will still work if the buffer | |
1050 representation is changed to have multiple gaps in it. | |
1051 */ | |
428 | 1052 |
826 | 1053 /* Return the maximum position in the buffer it is safe to scan forwards |
428 | 1054 past N to. This is used to prevent buffer scans from running into |
1055 the gap (e.g. search.c). All characters between N and CEILING_OF(N) | |
1056 are located contiguous in memory. Note that the character *at* | |
1057 CEILING_OF(N) is not contiguous in memory. */ | |
826 | 1058 #define BYTE_BUF_CEILING_OF(b, n) \ |
2367 | 1059 ((n) < BYTE_BUF_GPT (b) && BYTE_BUF_GPT (b) < BYTE_BUF_ZV (b) ? \ |
1060 BYTE_BUF_GPT (b) : BYTE_BUF_ZV (b)) | |
1061 #define BUF_CEILING_OF(b, n) \ | |
1062 ((n) < BUF_GPT (b) && BUF_GPT (b) < BUF_ZV (b) ? \ | |
1063 BUF_GPT (b) : BUF_ZV (b)) | |
428 | 1064 |
826 | 1065 /* Return the minimum position in the buffer it is safe to scan backwards |
428 | 1066 past N to. All characters between FLOOR_OF(N) and N are located |
1067 contiguous in memory. Note that the character *at* N may not be | |
1068 contiguous in memory. */ | |
2367 | 1069 #define BYTE_BUF_FLOOR_OF(b, n) \ |
1070 (BYTE_BUF_BEGV (b) < BYTE_BUF_GPT (b) && BYTE_BUF_GPT (b) < (n) ? \ | |
1071 BYTE_BUF_GPT (b) : BYTE_BUF_BEGV (b)) | |
1072 #define BUF_FLOOR_OF(b, n) \ | |
1073 (BUF_BEGV (b) < BUF_GPT (b) && BUF_GPT (b) < (n) ? \ | |
1074 BUF_GPT (b) : BUF_BEGV (b)) | |
428 | 1075 |
826 | 1076 #define BYTE_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \ |
2367 | 1077 ((n) < BYTE_BUF_GPT (b) && BYTE_BUF_GPT (b) < BYTE_BUF_Z (b) ? \ |
1078 BYTE_BUF_GPT (b) : BYTE_BUF_Z (b)) | |
1079 #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \ | |
1080 ((n) < BUF_GPT (b) && BUF_GPT (b) < BUF_Z (b) ? \ | |
1081 BUF_GPT (b) : BUF_Z (b)) | |
428 | 1082 |
2367 | 1083 #define BYTE_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \ |
1084 (BYTE_BUF_BEG (b) < BYTE_BUF_GPT (b) && BYTE_BUF_GPT (b) < (n) ? \ | |
1085 BYTE_BUF_GPT (b) : BYTE_BUF_BEG (b)) | |
1086 #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \ | |
1087 (BUF_BEG (b) < BUF_GPT (b) && BUF_GPT (b) < (n) ? \ | |
1088 BUF_GPT (b) : BUF_BEG (b)) | |
826 | 1089 |
1090 /* Iterate over contiguous chunks of text in buffer BUF, starting at POS, | |
1091 of length LEN. Evaluates POS and LEN only once, but BUF multiply. In | |
1092 each iteration, store the current chunk into RUNPTR/RUNLEN, which will | |
1093 be automatically declared (don't declare them yourself). This does not | |
1094 respect the limits of accessibility (BUF_BEGV/BUF_ZV); if you want these | |
1095 limits respected, you need to impose them yourself. | |
1096 | |
1097 NOTE: This must be surrounded with braces! */ | |
1098 | |
1099 #define BUFFER_TEXT_LOOP(buf, pos, len, runptr, runlen) \ | |
2367 | 1100 Ibyte *runptr; \ |
826 | 1101 Bytecount runlen; \ |
1102 Bytebpos BTL_pos = (pos); \ | |
1103 Bytebpos BTL_len = (len); \ | |
1104 for (runptr = BYTE_BUF_BYTE_ADDRESS (buf, BTL_pos), \ | |
1105 runlen = BYTE_BUF_CEILING_OF_IGNORE_ACCESSIBLE (buf, BTL_pos) - BTL_pos, \ | |
1106 runlen = min (BTL_len, runlen); \ | |
1107 BTL_len > 0; \ | |
1108 BTL_pos += runlen, \ | |
1109 BTL_len -= runlen, \ | |
1110 runptr = BYTE_BUF_BYTE_ADDRESS (buf, BTL_pos), \ | |
1111 runlen = BYTE_BUF_CEILING_OF_IGNORE_ACCESSIBLE (buf, BTL_pos) - BTL_pos, \ | |
1112 runlen = min (BTL_len, runlen)) | |
428 | 1113 |
1114 /* This structure marks which slots in a buffer have corresponding | |
1115 default values in Vbuffer_defaults. | |
1116 Each such slot has a nonzero value in this structure. | |
1117 The value has only one nonzero bit. | |
1118 | |
1119 When a buffer has its own local value for a slot, | |
1120 the bit for that slot (found in the same slot in this structure) | |
1121 is turned on in the buffer's local_var_flags slot. | |
1122 | |
1123 If a slot in this structure is zero, then even though there may | |
1124 be a DEFVAR_BUFFER_LOCAL for the slot, there is no default value for it; | |
1125 and the corresponding slot in Vbuffer_defaults is not used. */ | |
1126 | |
1127 extern struct buffer buffer_local_flags; | |
1128 | |
1129 | |
1130 /* Allocation of buffer data. */ | |
1131 | |
1132 #ifdef REL_ALLOC | |
1133 | |
440 | 1134 char *r_alloc (unsigned char **, size_t); |
1135 char *r_re_alloc (unsigned char **, size_t); | |
428 | 1136 void r_alloc_free (unsigned char **); |
1137 | |
1138 #define BUFFER_ALLOC(data, size) \ | |
867 | 1139 ((Ibyte *) r_alloc ((unsigned char **) &data, (size) * sizeof(Ibyte))) |
428 | 1140 #define BUFFER_REALLOC(data, size) \ |
867 | 1141 ((Ibyte *) r_re_alloc ((unsigned char **) &data, (size) * sizeof(Ibyte))) |
428 | 1142 #define BUFFER_FREE(data) r_alloc_free ((unsigned char **) &(data)) |
1143 #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data) | |
1144 | |
1145 #else /* !REL_ALLOC */ | |
1146 | |
1147 #define BUFFER_ALLOC(data,size)\ | |
867 | 1148 (data = xnew_array (Ibyte, size)) |
428 | 1149 #define BUFFER_REALLOC(data,size)\ |
867 | 1150 ((Ibyte *) xrealloc (data, (size) * sizeof(Ibyte))) |
428 | 1151 /* Avoid excess parentheses, or syntax errors may rear their heads. */ |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4906
diff
changeset
|
1152 #define BUFFER_FREE(data) xfree (data) |
428 | 1153 #define R_ALLOC_DECLARE(var,data) |
1154 | |
1155 #endif /* !REL_ALLOC */ | |
1156 | |
1157 | |
1158 /************************************************************************/ | |
1159 /* Case conversion */ | |
1160 /************************************************************************/ | |
1161 | |
1162 /* A "trt" table is a mapping from characters to other characters, | |
826 | 1163 typically used to convert between uppercase and lowercase. |
428 | 1164 */ |
1165 | |
1166 /* The _1 macros are named as such because they assume that you have | |
1167 already guaranteed that the character values are all in the range | |
1168 0 - 255. Bad lossage will happen otherwise. */ | |
1169 | |
446 | 1170 #define MAKE_TRT_TABLE() Fmake_char_table (Qgeneric) |
826 | 1171 DECLARE_INLINE_HEADER ( |
867 | 1172 Ichar |
1173 TRT_TABLE_OF (Lisp_Object table, Ichar ch) | |
826 | 1174 ) |
446 | 1175 { |
1176 Lisp_Object TRT_char; | |
826 | 1177 TRT_char = get_char_table (ch, table); |
446 | 1178 if (NILP (TRT_char)) |
1179 return ch; | |
1180 else | |
1181 return XCHAR (TRT_char); | |
1182 } | |
826 | 1183 #define SET_TRT_TABLE_OF(table, ch1, ch2) \ |
1184 Fput_char_table (make_char (ch1), make_char (ch2), table) | |
428 | 1185 |
826 | 1186 DECLARE_INLINE_HEADER ( |
1187 Lisp_Object | |
771 | 1188 BUFFER_CASE_TABLE (struct buffer *buf) |
826 | 1189 ) |
771 | 1190 { |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1191 return buf ? buf->case_table : current_buffer->case_table; |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1192 /* When buf=0, was Vstandard_case_table, but this sucks. If I set a |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1193 different case table in this buffer, operations that use a case table |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1194 by default should use the current one. */ |
771 | 1195 } |
1196 | |
428 | 1197 /* Macros used below. */ |
446 | 1198 #define DOWNCASE_TABLE_OF(buf, c) \ |
771 | 1199 TRT_TABLE_OF (XCASE_TABLE_DOWNCASE (BUFFER_CASE_TABLE (buf)), c) |
446 | 1200 #define UPCASE_TABLE_OF(buf, c) \ |
771 | 1201 TRT_TABLE_OF (XCASE_TABLE_UPCASE (BUFFER_CASE_TABLE (buf)), c) |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1202 #define CANON_TABLE_OF(buf, c) \ |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1203 TRT_TABLE_OF (XCASE_TABLE_CANON (BUFFER_CASE_TABLE (buf)), c) |
428 | 1204 |
1205 /* 1 if CH is upper case. */ | |
1206 | |
826 | 1207 DECLARE_INLINE_HEADER ( |
1208 int | |
867 | 1209 UPPERCASEP (struct buffer *buf, Ichar ch) |
826 | 1210 ) |
428 | 1211 { |
1212 return DOWNCASE_TABLE_OF (buf, ch) != ch; | |
1213 } | |
1214 | |
1215 /* 1 if CH is lower case. */ | |
1216 | |
826 | 1217 DECLARE_INLINE_HEADER ( |
1218 int | |
867 | 1219 LOWERCASEP (struct buffer *buf, Ichar ch) |
826 | 1220 ) |
428 | 1221 { |
1222 return (UPCASE_TABLE_OF (buf, ch) != ch && | |
1223 DOWNCASE_TABLE_OF (buf, ch) == ch); | |
1224 } | |
1225 | |
1226 /* 1 if CH is neither upper nor lower case. */ | |
1227 | |
826 | 1228 DECLARE_INLINE_HEADER ( |
1229 int | |
867 | 1230 NOCASEP (struct buffer *buf, Ichar ch) |
826 | 1231 ) |
428 | 1232 { |
1233 return UPCASE_TABLE_OF (buf, ch) == ch; | |
1234 } | |
1235 | |
1236 /* Upcase a character, or make no change if that cannot be done. */ | |
1237 | |
826 | 1238 DECLARE_INLINE_HEADER ( |
867 | 1239 Ichar |
1240 UPCASE (struct buffer *buf, Ichar ch) | |
826 | 1241 ) |
428 | 1242 { |
1243 return (DOWNCASE_TABLE_OF (buf, ch) == ch) ? UPCASE_TABLE_OF (buf, ch) : ch; | |
1244 } | |
1245 | |
1246 /* Upcase a character known to be not upper case. Unused. */ | |
1247 | |
1248 #define UPCASE1(buf, ch) UPCASE_TABLE_OF (buf, ch) | |
1249 | |
1250 /* Downcase a character, or make no change if that cannot be done. */ | |
1251 | |
1252 #define DOWNCASE(buf, ch) DOWNCASE_TABLE_OF (buf, ch) | |
1253 | |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1254 /* Convert a character to a canonical representation, so that case-independent |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1255 comparisons will work. */ |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1256 |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1257 #define CANONCASE(buf, ch) CANON_TABLE_OF (buf, ch) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4860
diff
changeset
|
1258 |
440 | 1259 #endif /* INCLUDED_buffer_h_ */ |