Commit | Line | Data |
---|---|---|
993b6404 | 1 | /* Indentation functions. |
31c8f881 | 2 | Copyright (C) 1985,86,87,88,93,94,95,98 Free Software Foundation, Inc. |
993b6404 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
7c938215 | 8 | the Free Software Foundation; either version 2, or (at your option) |
993b6404 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
993b6404 JB |
20 | |
21 | ||
18160b98 | 22 | #include <config.h> |
993b6404 JB |
23 | #include "lisp.h" |
24 | #include "buffer.h" | |
a997a7ca | 25 | #include "charset.h" |
fb911777 | 26 | #include "category.h" |
993b6404 | 27 | #include "indent.h" |
502b9b64 | 28 | #include "frame.h" |
993b6404 JB |
29 | #include "window.h" |
30 | #include "termchar.h" | |
31 | #include "termopts.h" | |
32 | #include "disptab.h" | |
5a05d3d2 | 33 | #include "intervals.h" |
0aa01123 | 34 | #include "region-cache.h" |
993b6404 JB |
35 | |
36 | /* Indentation can insert tabs if this is non-zero; | |
37 | otherwise always uses spaces */ | |
38 | int indent_tabs_mode; | |
39 | ||
40 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
41 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
42 | ||
43 | #define CR 015 | |
44 | ||
45 | /* These three values memoize the current column to avoid recalculation */ | |
46 | /* Some things in set last_known_column_point to -1 | |
47 | to mark the memoized value as invalid */ | |
48 | /* Last value returned by current_column */ | |
49 | int last_known_column; | |
50 | /* Value of point when current_column was called */ | |
51 | int last_known_column_point; | |
52 | /* Value of MODIFF when current_column was called */ | |
53 | int last_known_column_modified; | |
54 | ||
82d593eb | 55 | static int current_column_1 (); |
fe0066f5 | 56 | static int position_indentation (); |
82d593eb | 57 | |
a997a7ca KH |
58 | /* Cache of beginning of line found by the last call of |
59 | current_column. */ | |
60 | int current_column_bol_cache; | |
61 | ||
993b6404 JB |
62 | /* Get the display table to use for the current buffer. */ |
63 | ||
d44f12b4 | 64 | struct Lisp_Char_Table * |
993b6404 JB |
65 | buffer_display_table () |
66 | { | |
67 | Lisp_Object thisbuf; | |
68 | ||
69 | thisbuf = current_buffer->display_table; | |
d44f12b4 RS |
70 | if (DISP_TABLE_P (thisbuf)) |
71 | return XCHAR_TABLE (thisbuf); | |
72 | if (DISP_TABLE_P (Vstandard_display_table)) | |
73 | return XCHAR_TABLE (Vstandard_display_table); | |
993b6404 JB |
74 | return 0; |
75 | } | |
76 | \f | |
0aa01123 JB |
77 | /* Width run cache considerations. */ |
78 | ||
79 | /* Return the width of character C under display table DP. */ | |
f845b8b2 | 80 | |
0aa01123 JB |
81 | static int |
82 | character_width (c, dp) | |
83 | int c; | |
d44f12b4 | 84 | struct Lisp_Char_Table *dp; |
0aa01123 JB |
85 | { |
86 | Lisp_Object elt; | |
87 | ||
88 | /* These width computations were determined by examining the cases | |
89 | in display_text_line. */ | |
90 | ||
f845b8b2 | 91 | /* Everything can be handled by the display table, if it's |
0aa01123 | 92 | present and the element is right. */ |
f845b8b2 | 93 | if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt))) |
0aa01123 JB |
94 | return XVECTOR (elt)->size; |
95 | ||
f845b8b2 RS |
96 | /* Some characters are special. */ |
97 | if (c == '\n' || c == '\t' || c == '\015') | |
98 | return 0; | |
99 | ||
100 | /* Printing characters have width 1. */ | |
0aa01123 JB |
101 | else if (c >= 040 && c < 0177) |
102 | return 1; | |
103 | ||
104 | /* Everybody else (control characters, metacharacters) has other | |
105 | widths. We could return their actual widths here, but they | |
106 | depend on things like ctl_arrow and crud like that, and they're | |
107 | not very common at all. So we'll just claim we don't know their | |
108 | widths. */ | |
109 | else | |
110 | return 0; | |
111 | } | |
112 | ||
113 | /* Return true iff the display table DISPTAB specifies the same widths | |
114 | for characters as WIDTHTAB. We use this to decide when to | |
115 | invalidate the buffer's width_run_cache. */ | |
116 | int | |
117 | disptab_matches_widthtab (disptab, widthtab) | |
d44f12b4 | 118 | struct Lisp_Char_Table *disptab; |
0aa01123 JB |
119 | struct Lisp_Vector *widthtab; |
120 | { | |
121 | int i; | |
122 | ||
123 | if (widthtab->size != 256) | |
124 | abort (); | |
125 | ||
126 | for (i = 0; i < 256; i++) | |
127 | if (character_width (i, disptab) | |
128 | != XFASTINT (widthtab->contents[i])) | |
129 | return 0; | |
130 | ||
131 | return 1; | |
2ff4775b | 132 | } |
0aa01123 JB |
133 | |
134 | /* Recompute BUF's width table, using the display table DISPTAB. */ | |
135 | void | |
136 | recompute_width_table (buf, disptab) | |
137 | struct buffer *buf; | |
d44f12b4 | 138 | struct Lisp_Char_Table *disptab; |
0aa01123 JB |
139 | { |
140 | int i; | |
228a2e1a | 141 | struct Lisp_Vector *widthtab; |
0aa01123 | 142 | |
228a2e1a KH |
143 | if (!VECTORP (buf->width_table)) |
144 | buf->width_table = Fmake_vector (make_number (256), make_number (0)); | |
145 | widthtab = XVECTOR (buf->width_table); | |
0aa01123 JB |
146 | if (widthtab->size != 256) |
147 | abort (); | |
148 | ||
149 | for (i = 0; i < 256; i++) | |
228a2e1a | 150 | XSETFASTINT (widthtab->contents[i], character_width (i, disptab)); |
0aa01123 JB |
151 | } |
152 | ||
153 | /* Allocate or free the width run cache, as requested by the current | |
154 | state of current_buffer's cache_long_line_scans variable. */ | |
155 | static void | |
156 | width_run_cache_on_off () | |
157 | { | |
a997a7ca KH |
158 | if (NILP (current_buffer->cache_long_line_scans) |
159 | /* And, for the moment, this feature doesn't work on multibyte | |
160 | characters. */ | |
161 | || !NILP (current_buffer->enable_multibyte_characters)) | |
0aa01123 JB |
162 | { |
163 | /* It should be off. */ | |
164 | if (current_buffer->width_run_cache) | |
165 | { | |
166 | free_region_cache (current_buffer->width_run_cache); | |
167 | current_buffer->width_run_cache = 0; | |
168 | current_buffer->width_table = Qnil; | |
169 | } | |
170 | } | |
171 | else | |
172 | { | |
173 | /* It should be on. */ | |
174 | if (current_buffer->width_run_cache == 0) | |
2ff4775b | 175 | { |
0aa01123 | 176 | current_buffer->width_run_cache = new_region_cache (); |
0aa01123 JB |
177 | recompute_width_table (current_buffer, buffer_display_table ()); |
178 | } | |
179 | } | |
180 | } | |
181 | ||
182 | \f | |
9a21bb64 RS |
183 | /* Skip some invisible characters starting from POS. |
184 | This includes characters invisible because of text properties | |
185 | and characters invisible because of overlays. | |
186 | ||
187 | If position POS is followed by invisible characters, | |
188 | skip some of them and return the position after them. | |
189 | Otherwise return POS itself. | |
190 | ||
191 | Set *NEXT_BOUNDARY_P to the next position at which | |
192 | it will be necessary to call this function again. | |
193 | ||
194 | Don't scan past TO, and don't set *NEXT_BOUNDARY_P | |
195 | to a value greater than TO. | |
196 | ||
197 | If WINDOW is non-nil, and this buffer is displayed in WINDOW, | |
198 | take account of overlays that apply only in WINDOW. | |
199 | ||
200 | We don't necessarily skip all the invisible characters after POS | |
201 | because that could take a long time. We skip a reasonable number | |
202 | which can be skipped quickly. If there might be more invisible | |
203 | characters immediately following, then *NEXT_BOUNDARY_P | |
204 | will equal the return value. */ | |
205 | ||
8720a429 | 206 | int |
9a21bb64 RS |
207 | skip_invisible (pos, next_boundary_p, to, window) |
208 | int pos; | |
209 | int *next_boundary_p; | |
210 | int to; | |
211 | Lisp_Object window; | |
212 | { | |
2e34157c | 213 | Lisp_Object prop, position, overlay_limit, proplimit; |
9a21bb64 | 214 | Lisp_Object buffer; |
2e34157c | 215 | int end; |
9a21bb64 RS |
216 | |
217 | XSETFASTINT (position, pos); | |
218 | XSETBUFFER (buffer, current_buffer); | |
219 | ||
220 | /* Give faster response for overlay lookup near POS. */ | |
221 | recenter_overlay_lists (current_buffer, pos); | |
222 | ||
223 | /* We must not advance farther than the next overlay change. | |
224 | The overlay change might change the invisible property; | |
225 | or there might be overlay strings to be displayed there. */ | |
226 | overlay_limit = Fnext_overlay_change (position); | |
227 | /* As for text properties, this gives a lower bound | |
228 | for where the invisible text property could change. */ | |
229 | proplimit = Fnext_property_change (position, buffer, Qt); | |
230 | if (XFASTINT (overlay_limit) < XFASTINT (proplimit)) | |
231 | proplimit = overlay_limit; | |
232 | /* PROPLIMIT is now a lower bound for the next change | |
233 | in invisible status. If that is plenty far away, | |
234 | use that lower bound. */ | |
235 | if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to) | |
236 | *next_boundary_p = XFASTINT (proplimit); | |
237 | /* Otherwise, scan for the next `invisible' property change. */ | |
238 | else | |
239 | { | |
240 | /* Don't scan terribly far. */ | |
241 | XSETFASTINT (proplimit, min (pos + 100, to)); | |
242 | /* No matter what. don't go past next overlay change. */ | |
243 | if (XFASTINT (overlay_limit) < XFASTINT (proplimit)) | |
244 | proplimit = overlay_limit; | |
2e34157c RS |
245 | end = XFASTINT (Fnext_single_property_change (position, Qinvisible, |
246 | buffer, proplimit)); | |
fe0066f5 | 247 | #if 0 |
a997a7ca KH |
248 | /* Don't put the boundary in the middle of multibyte form if |
249 | there is no actual property change. */ | |
250 | if (end == pos + 100 | |
251 | && !NILP (current_buffer->enable_multibyte_characters) | |
252 | && end < ZV) | |
253 | while (pos < end && !CHAR_HEAD_P (POS_ADDR (end))) | |
254 | end--; | |
fe0066f5 | 255 | #endif |
2e34157c | 256 | *next_boundary_p = end; |
9a21bb64 RS |
257 | } |
258 | /* if the `invisible' property is set, we can skip to | |
259 | the next property change */ | |
260 | if (!NILP (window) && EQ (XWINDOW (window)->buffer, buffer)) | |
261 | prop = Fget_char_property (position, Qinvisible, window); | |
262 | else | |
263 | prop = Fget_char_property (position, Qinvisible, buffer); | |
264 | if (TEXT_PROP_MEANS_INVISIBLE (prop)) | |
265 | return *next_boundary_p; | |
266 | return pos; | |
267 | } | |
268 | \f | |
c9c0f7cf KH |
269 | /* Set variables WIDTH and BYTES for a multibyte sequence starting at P. |
270 | ||
c9c0f7cf KH |
271 | DP is a display table or NULL. |
272 | ||
273 | This macro is used in current_column_1, Fmove_to_column, and | |
274 | compute_motion. */ | |
275 | ||
0377370f KH |
276 | #define MULTIBYTE_BYTES_WIDTH(p, dp) \ |
277 | do { \ | |
278 | int c; \ | |
279 | \ | |
280 | wide_column = 0; \ | |
281 | c = STRING_CHAR_AND_LENGTH (p, MAX_LENGTH_OF_MULTI_BYTE_FORM, bytes); \ | |
282 | if (BYTES_BY_CHAR_HEAD (*p) != bytes) \ | |
283 | width = bytes * 4; \ | |
284 | else \ | |
285 | { \ | |
286 | if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))) \ | |
287 | width = XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; \ | |
288 | else \ | |
289 | width = WIDTH_BY_CHAR_HEAD (*p); \ | |
290 | if (width > 1) \ | |
291 | wide_column = width; \ | |
292 | } \ | |
c9c0f7cf KH |
293 | } while (0) |
294 | ||
993b6404 JB |
295 | DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0, |
296 | "Return the horizontal position of point. Beginning of line is column 0.\n\ | |
297 | This is calculated by adding together the widths of all the displayed\n\ | |
298 | representations of the character between the start of the previous line\n\ | |
299 | and point. (eg control characters will have a width of 2 or 4, tabs\n\ | |
300 | will have a variable width)\n\ | |
502b9b64 JB |
301 | Ignores finite width of frame, which means that this function may return\n\ |
302 | values greater than (frame-width).\n\ | |
993b6404 JB |
303 | Whether the line is visible (if `selective-display' is t) has no effect;\n\ |
304 | however, ^M is treated as end of line when `selective-display' is t.") | |
305 | () | |
306 | { | |
307 | Lisp_Object temp; | |
94d92e9c | 308 | XSETFASTINT (temp, current_column ()); |
993b6404 JB |
309 | return temp; |
310 | } | |
311 | ||
e74928fc JB |
312 | /* Cancel any recorded value of the horizontal position. */ |
313 | ||
c9667ae1 | 314 | void |
e74928fc JB |
315 | invalidate_current_column () |
316 | { | |
317 | last_known_column_point = 0; | |
318 | } | |
319 | ||
993b6404 JB |
320 | int |
321 | current_column () | |
322 | { | |
323 | register int col; | |
324 | register unsigned char *ptr, *stop; | |
325 | register int tab_seen; | |
326 | int post_tab; | |
327 | register int c; | |
328 | register int tab_width = XINT (current_buffer->tab_width); | |
56a98455 | 329 | int ctl_arrow = !NILP (current_buffer->ctl_arrow); |
d44f12b4 | 330 | register struct Lisp_Char_Table *dp = buffer_display_table (); |
993b6404 JB |
331 | int stopchar; |
332 | ||
6ec8bbd2 | 333 | if (PT == last_known_column_point |
993b6404 JB |
334 | && MODIFF == last_known_column_modified) |
335 | return last_known_column; | |
336 | ||
813b81b3 RS |
337 | /* If the buffer has overlays, text properties, |
338 | or multibyte characters, use a more general algorithm. */ | |
9a21bb64 RS |
339 | if (BUF_INTERVALS (current_buffer) |
340 | || !NILP (current_buffer->overlays_before) | |
a997a7ca | 341 | || !NILP (current_buffer->overlays_after) |
813b81b3 RS |
342 | || Z != Z_BYTE) |
343 | return current_column_1 (); | |
9a21bb64 RS |
344 | |
345 | /* Scan backwards from point to the previous newline, | |
346 | counting width. Tab characters are the only complicated case. */ | |
347 | ||
993b6404 | 348 | /* Make a pointer for decrementing through the chars before point. */ |
fe0066f5 | 349 | ptr = BYTE_POS_ADDR (PT_BYTE - 1) + 1; |
993b6404 JB |
350 | /* Make a pointer to where consecutive chars leave off, |
351 | going backwards from point. */ | |
6ec8bbd2 | 352 | if (PT == BEGV) |
993b6404 | 353 | stop = ptr; |
6ec8bbd2 | 354 | else if (PT <= GPT || BEGV > GPT) |
993b6404 JB |
355 | stop = BEGV_ADDR; |
356 | else | |
357 | stop = GAP_END_ADDR; | |
358 | ||
ccdcf1f5 | 359 | if (tab_width <= 0 || tab_width > 1000) tab_width = 8; |
993b6404 JB |
360 | |
361 | col = 0, tab_seen = 0, post_tab = 0; | |
362 | ||
363 | while (1) | |
364 | { | |
365 | if (ptr == stop) | |
366 | { | |
367 | /* We stopped either for the beginning of the buffer | |
368 | or for the gap. */ | |
369 | if (ptr == BEGV_ADDR) | |
370 | break; | |
371 | /* It was the gap. Jump back over it. */ | |
372 | stop = BEGV_ADDR; | |
373 | ptr = GPT_ADDR; | |
374 | /* Check whether that brings us to beginning of buffer. */ | |
375 | if (BEGV >= GPT) break; | |
376 | } | |
377 | ||
378 | c = *--ptr; | |
f845b8b2 RS |
379 | if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))) |
380 | col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; | |
381 | else if (c >= 040 && c < 0177) | |
382 | col++; | |
7050d530 RS |
383 | else if (c == '\n' |
384 | || (c == '\r' && EQ (current_buffer->selective_display, Qt))) | |
385 | { | |
386 | ptr++; | |
387 | break; | |
388 | } | |
993b6404 JB |
389 | else if (c == '\t') |
390 | { | |
391 | if (tab_seen) | |
392 | col = ((col + tab_width) / tab_width) * tab_width; | |
393 | ||
394 | post_tab += col; | |
395 | col = 0; | |
396 | tab_seen = 1; | |
397 | } | |
993b6404 JB |
398 | else |
399 | col += (ctl_arrow && c < 0200) ? 2 : 4; | |
400 | } | |
401 | ||
402 | if (tab_seen) | |
403 | { | |
404 | col = ((col + tab_width) / tab_width) * tab_width; | |
405 | col += post_tab; | |
406 | } | |
407 | ||
a997a7ca KH |
408 | if (ptr == BEGV_ADDR) |
409 | current_column_bol_cache = BEGV; | |
410 | else | |
fe0066f5 RS |
411 | current_column_bol_cache = BYTE_TO_CHAR (PTR_BYTE_POS (ptr)); |
412 | ||
993b6404 | 413 | last_known_column = col; |
6ec8bbd2 | 414 | last_known_column_point = PT; |
993b6404 JB |
415 | last_known_column_modified = MODIFF; |
416 | ||
417 | return col; | |
418 | } | |
419 | \f | |
9a21bb64 RS |
420 | /* Return the column number of position POS |
421 | by scanning forward from the beginning of the line. | |
422 | This function handles characters that are invisible | |
423 | due to text properties or overlays. */ | |
424 | ||
425 | static int | |
813b81b3 | 426 | current_column_1 () |
9a21bb64 RS |
427 | { |
428 | register int tab_width = XINT (current_buffer->tab_width); | |
429 | register int ctl_arrow = !NILP (current_buffer->ctl_arrow); | |
430 | register struct Lisp_Char_Table *dp = buffer_display_table (); | |
fe0066f5 | 431 | int multibyte = !NILP (current_buffer->enable_multibyte_characters); |
9a21bb64 RS |
432 | |
433 | /* Start the scan at the beginning of this line with column number 0. */ | |
434 | register int col = 0; | |
fe0066f5 RS |
435 | int scan, scan_byte; |
436 | int next_boundary, next_boundary_byte; | |
437 | int opoint = PT, opoint_byte = PT_BYTE; | |
438 | ||
813b81b3 | 439 | scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1); |
fe0066f5 RS |
440 | current_column_bol_cache = PT; |
441 | scan = PT, scan_byte = PT_BYTE; | |
442 | SET_PT_BOTH (opoint, opoint_byte); | |
443 | next_boundary = scan; | |
444 | next_boundary_byte = scan_byte; | |
9a21bb64 RS |
445 | |
446 | if (tab_width <= 0 || tab_width > 1000) tab_width = 8; | |
447 | ||
448 | /* Scan forward to the target position. */ | |
813b81b3 | 449 | while (scan < opoint) |
9a21bb64 RS |
450 | { |
451 | int c; | |
452 | ||
453 | /* Occasionally we may need to skip invisible text. */ | |
454 | while (scan == next_boundary) | |
455 | { | |
fe0066f5 | 456 | int old_scan = scan; |
9a21bb64 RS |
457 | /* This updates NEXT_BOUNDARY to the next place |
458 | where we might need to skip more invisible text. */ | |
813b81b3 RS |
459 | scan = skip_invisible (scan, &next_boundary, opoint, Qnil); |
460 | if (scan >= opoint) | |
9a21bb64 | 461 | goto endloop; |
fe0066f5 RS |
462 | if (scan != old_scan) |
463 | scan_byte = CHAR_TO_BYTE (scan); | |
464 | next_boundary_byte = CHAR_TO_BYTE (next_boundary); | |
9a21bb64 RS |
465 | } |
466 | ||
813b81b3 | 467 | c = FETCH_BYTE (scan_byte); |
c9c0f7cf KH |
468 | if (dp != 0 |
469 | && ! (multibyte && BASE_LEADING_CODE_P (c)) | |
470 | && VECTORP (DISP_CHAR_VECTOR (dp, c))) | |
9a21bb64 RS |
471 | { |
472 | col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; | |
473 | scan++; | |
fe0066f5 | 474 | scan_byte++; |
9a21bb64 RS |
475 | continue; |
476 | } | |
477 | if (c == '\n') | |
478 | break; | |
479 | if (c == '\r' && EQ (current_buffer->selective_display, Qt)) | |
480 | break; | |
481 | scan++; | |
fe0066f5 | 482 | scan_byte++; |
9a21bb64 RS |
483 | if (c == '\t') |
484 | { | |
485 | int prev_col = col; | |
486 | col += tab_width; | |
487 | col = col / tab_width * tab_width; | |
488 | } | |
a997a7ca KH |
489 | else if (multibyte && BASE_LEADING_CODE_P (c)) |
490 | { | |
c9c0f7cf KH |
491 | unsigned char *ptr; |
492 | int bytes, width, wide_column; | |
a997a7ca | 493 | |
c9c0f7cf KH |
494 | scan_byte--; |
495 | ptr = BYTE_POS_ADDR (scan_byte); | |
0377370f | 496 | MULTIBYTE_BYTES_WIDTH (ptr, dp); |
c9c0f7cf KH |
497 | scan_byte += bytes; |
498 | col += width; | |
a997a7ca | 499 | } |
9a21bb64 RS |
500 | else if (ctl_arrow && (c < 040 || c == 0177)) |
501 | col += 2; | |
502 | else if (c < 040 || c >= 0177) | |
503 | col += 4; | |
504 | else | |
505 | col++; | |
506 | } | |
507 | endloop: | |
508 | ||
509 | last_known_column = col; | |
6ec8bbd2 | 510 | last_known_column_point = PT; |
9a21bb64 RS |
511 | last_known_column_modified = MODIFF; |
512 | ||
513 | return col; | |
514 | } | |
515 | \f | |
c412c808 RS |
516 | /* Return the width in columns of the part of STRING from BEG to END. |
517 | If BEG is nil, that stands for the beginning of STRING. | |
518 | If END is nil, that stands for the end of STRING. */ | |
519 | ||
520 | static int | |
382ac0bd | 521 | string_display_width (string, beg, end) |
c412c808 RS |
522 | Lisp_Object string, beg, end; |
523 | { | |
524 | register int col; | |
525 | register unsigned char *ptr, *stop; | |
526 | register int tab_seen; | |
527 | int post_tab; | |
528 | register int c; | |
529 | register int tab_width = XINT (current_buffer->tab_width); | |
530 | int ctl_arrow = !NILP (current_buffer->ctl_arrow); | |
d44f12b4 | 531 | register struct Lisp_Char_Table *dp = buffer_display_table (); |
c412c808 RS |
532 | int b, e; |
533 | ||
534 | if (NILP (end)) | |
535 | e = XSTRING (string)->size; | |
536 | else | |
537 | { | |
538 | CHECK_NUMBER (end, 0); | |
539 | e = XINT (end); | |
540 | } | |
541 | ||
542 | if (NILP (beg)) | |
543 | b = 0; | |
544 | else | |
545 | { | |
546 | CHECK_NUMBER (beg, 0); | |
547 | b = XINT (beg); | |
548 | } | |
549 | ||
550 | /* Make a pointer for decrementing through the chars before point. */ | |
551 | ptr = XSTRING (string)->data + e; | |
552 | /* Make a pointer to where consecutive chars leave off, | |
553 | going backwards from point. */ | |
554 | stop = XSTRING (string)->data + b; | |
555 | ||
556 | if (tab_width <= 0 || tab_width > 1000) tab_width = 8; | |
557 | ||
558 | col = 0, tab_seen = 0, post_tab = 0; | |
559 | ||
560 | while (1) | |
561 | { | |
562 | if (ptr == stop) | |
563 | break; | |
564 | ||
565 | c = *--ptr; | |
566 | if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))) | |
567 | col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; | |
568 | else if (c >= 040 && c < 0177) | |
569 | col++; | |
570 | else if (c == '\n') | |
571 | break; | |
572 | else if (c == '\t') | |
573 | { | |
574 | if (tab_seen) | |
575 | col = ((col + tab_width) / tab_width) * tab_width; | |
576 | ||
577 | post_tab += col; | |
578 | col = 0; | |
579 | tab_seen = 1; | |
580 | } | |
581 | else | |
582 | col += (ctl_arrow && c < 0200) ? 2 : 4; | |
583 | } | |
584 | ||
585 | if (tab_seen) | |
586 | { | |
587 | col = ((col + tab_width) / tab_width) * tab_width; | |
588 | col += post_tab; | |
589 | } | |
590 | ||
591 | return col; | |
592 | } | |
593 | \f | |
993b6404 JB |
594 | DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ", |
595 | "Indent from point with tabs and spaces until COLUMN is reached.\n\ | |
04c98432 EN |
596 | Optional second argument MININUM says always do at least MININUM spaces\n\ |
597 | even if that goes past COLUMN; by default, MININUM is zero.") | |
598 | (column, minimum) | |
599 | Lisp_Object column, minimum; | |
993b6404 JB |
600 | { |
601 | int mincol; | |
602 | register int fromcol; | |
603 | register int tab_width = XINT (current_buffer->tab_width); | |
604 | ||
04c98432 | 605 | CHECK_NUMBER (column, 0); |
56a98455 | 606 | if (NILP (minimum)) |
94d92e9c | 607 | XSETFASTINT (minimum, 0); |
993b6404 JB |
608 | CHECK_NUMBER (minimum, 1); |
609 | ||
610 | fromcol = current_column (); | |
611 | mincol = fromcol + XINT (minimum); | |
04c98432 | 612 | if (mincol < XINT (column)) mincol = XINT (column); |
993b6404 JB |
613 | |
614 | if (fromcol == mincol) | |
615 | return make_number (mincol); | |
616 | ||
ccdcf1f5 | 617 | if (tab_width <= 0 || tab_width > 1000) tab_width = 8; |
993b6404 JB |
618 | |
619 | if (indent_tabs_mode) | |
620 | { | |
621 | Lisp_Object n; | |
94d92e9c | 622 | XSETFASTINT (n, mincol / tab_width - fromcol / tab_width); |
993b6404 JB |
623 | if (XFASTINT (n) != 0) |
624 | { | |
6d1bd1a5 | 625 | Finsert_char (make_number ('\t'), n, Qt); |
993b6404 JB |
626 | |
627 | fromcol = (mincol / tab_width) * tab_width; | |
628 | } | |
629 | } | |
630 | ||
04c98432 EN |
631 | XSETFASTINT (column, mincol - fromcol); |
632 | Finsert_char (make_number (' '), column, Qt); | |
993b6404 JB |
633 | |
634 | last_known_column = mincol; | |
6ec8bbd2 | 635 | last_known_column_point = PT; |
993b6404 JB |
636 | last_known_column_modified = MODIFF; |
637 | ||
04c98432 EN |
638 | XSETINT (column, mincol); |
639 | return column; | |
993b6404 | 640 | } |
0aa01123 | 641 | |
993b6404 | 642 | \f |
dfcf069d AS |
643 | static int position_indentation P_ ((int)); |
644 | ||
993b6404 JB |
645 | DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation, |
646 | 0, 0, 0, | |
647 | "Return the indentation of the current line.\n\ | |
648 | This is the horizontal position of the character\n\ | |
649 | following any initial whitespace.") | |
650 | () | |
651 | { | |
652 | Lisp_Object val; | |
fe0066f5 RS |
653 | int opoint = PT, opoint_byte = PT_BYTE; |
654 | ||
655 | scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1); | |
993b6404 | 656 | |
fe0066f5 RS |
657 | XSETFASTINT (val, position_indentation (PT_BYTE)); |
658 | SET_PT_BOTH (opoint, opoint_byte); | |
993b6404 JB |
659 | return val; |
660 | } | |
661 | ||
fe0066f5 RS |
662 | static int |
663 | position_indentation (pos_byte) | |
664 | register int pos_byte; | |
993b6404 JB |
665 | { |
666 | register int column = 0; | |
667 | register int tab_width = XINT (current_buffer->tab_width); | |
668 | register unsigned char *p; | |
669 | register unsigned char *stop; | |
9a21bb64 | 670 | unsigned char *start; |
fe0066f5 RS |
671 | int next_boundary_byte = pos_byte; |
672 | int ceiling = next_boundary_byte; | |
2ff4775b | 673 | |
ccdcf1f5 | 674 | if (tab_width <= 0 || tab_width > 1000) tab_width = 8; |
2ff4775b | 675 | |
fe0066f5 | 676 | p = BYTE_POS_ADDR (pos_byte); |
9a21bb64 RS |
677 | /* STOP records the value of P at which we will need |
678 | to think about the gap, or about invisible text, | |
679 | or about the end of the buffer. */ | |
680 | stop = p; | |
681 | /* START records the starting value of P. */ | |
682 | start = p; | |
993b6404 JB |
683 | while (1) |
684 | { | |
685 | while (p == stop) | |
686 | { | |
fe0066f5 | 687 | int stop_pos_byte; |
9a21bb64 | 688 | |
fe0066f5 RS |
689 | /* If we have updated P, set POS_BYTE to match. |
690 | The first time we enter the loop, POS_BYTE is already right. */ | |
9a21bb64 | 691 | if (p != start) |
fe0066f5 | 692 | pos_byte = PTR_BYTE_POS (p); |
9a21bb64 | 693 | /* Consider the various reasons STOP might have been set here. */ |
fe0066f5 | 694 | if (pos_byte == ZV_BYTE) |
993b6404 | 695 | return column; |
fe0066f5 RS |
696 | if (pos_byte == next_boundary_byte) |
697 | { | |
698 | int next_boundary; | |
699 | int pos = BYTE_TO_CHAR (pos_byte); | |
700 | pos = skip_invisible (pos, &next_boundary, ZV, Qnil); | |
701 | pos_byte = CHAR_TO_BYTE (pos); | |
702 | next_boundary_byte = CHAR_TO_BYTE (next_boundary); | |
703 | } | |
704 | if (pos_byte >= ceiling) | |
705 | ceiling = BUFFER_CEILING_OF (pos_byte) + 1; | |
9a21bb64 RS |
706 | /* Compute the next place we need to stop and think, |
707 | and set STOP accordingly. */ | |
fe0066f5 | 708 | stop_pos_byte = min (ceiling, next_boundary_byte); |
9a21bb64 | 709 | /* The -1 and +1 arrange to point at the first byte of gap |
fe0066f5 | 710 | (if STOP_POS_BYTE is the position of the gap) |
9a21bb64 RS |
711 | rather than at the data after the gap. */ |
712 | ||
fe0066f5 RS |
713 | stop = BYTE_POS_ADDR (stop_pos_byte - 1) + 1; |
714 | p = BYTE_POS_ADDR (pos_byte); | |
993b6404 JB |
715 | } |
716 | switch (*p++) | |
717 | { | |
fb911777 KH |
718 | case 0240: |
719 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
720 | return column; | |
993b6404 JB |
721 | case ' ': |
722 | column++; | |
723 | break; | |
724 | case '\t': | |
725 | column += tab_width - column % tab_width; | |
726 | break; | |
727 | default: | |
fb911777 KH |
728 | if (ASCII_BYTE_P (p[-1]) |
729 | || NILP (current_buffer->enable_multibyte_characters)) | |
730 | return column; | |
731 | { | |
fe0066f5 RS |
732 | int c; |
733 | pos_byte = PTR_BYTE_POS (p - 1); | |
734 | c = FETCH_MULTIBYTE_CHAR (pos_byte); | |
fb911777 KH |
735 | if (CHAR_HAS_CATEGORY (c, ' ')) |
736 | { | |
737 | column++; | |
fe0066f5 RS |
738 | INC_POS (pos_byte); |
739 | p = BYTE_POS_ADDR (pos_byte); | |
fb911777 KH |
740 | } |
741 | else | |
742 | return column; | |
743 | } | |
993b6404 JB |
744 | } |
745 | } | |
746 | } | |
1b15e576 KH |
747 | |
748 | /* Test whether the line beginning at POS is indented beyond COLUMN. | |
749 | Blank lines are treated as if they had the same indentation as the | |
750 | preceding line. */ | |
fe0066f5 | 751 | |
1b15e576 | 752 | int |
fe0066f5 RS |
753 | indented_beyond_p (pos, pos_byte, column) |
754 | int pos, pos_byte, column; | |
1b15e576 | 755 | { |
d5d6f706 | 756 | int val; |
fe0066f5 RS |
757 | int opoint = PT, opoint_byte = PT_BYTE; |
758 | ||
759 | SET_PT_BOTH (pos, pos_byte); | |
760 | while (PT > BEGV && FETCH_BYTE (PT_BYTE) == '\n') | |
761 | scan_newline (PT - 1, PT_BYTE - 1, BEGV, BEGV_BYTE, -1, 0); | |
762 | ||
d5d6f706 | 763 | val = position_indentation (PT_BYTE); |
fe0066f5 | 764 | SET_PT_BOTH (opoint, opoint_byte); |
8f9db2b7 | 765 | return val >= column; |
1b15e576 | 766 | } |
993b6404 | 767 | \f |
782d260e | 768 | DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p", |
993b6404 JB |
769 | "Move point to column COLUMN in the current line.\n\ |
770 | The column of a character is calculated by adding together the widths\n\ | |
771 | as displayed of the previous characters in the line.\n\ | |
772 | This function ignores line-continuation;\n\ | |
773 | there is no upper limit on the column number a character can have\n\ | |
230a4cbd JB |
774 | and horizontal scrolling has no effect.\n\ |
775 | \n\ | |
993b6404 JB |
776 | If specified column is within a character, point goes after that character.\n\ |
777 | If it's past end of line, point goes to end of line.\n\n\ | |
de4075cf KH |
778 | A non-nil second (optional) argument FORCE means,\n\ |
779 | if COLUMN is in the middle of a tab character, change it to spaces.\n\ | |
780 | In addition, if FORCE is t, and the line is too short\n\ | |
781 | to reach column COLUMN, add spaces/tabs to get there.\n\ | |
55f7a32b RS |
782 | \n\ |
783 | The return value is the current column.") | |
993b6404 JB |
784 | (column, force) |
785 | Lisp_Object column, force; | |
786 | { | |
787 | register int pos; | |
788 | register int col = current_column (); | |
789 | register int goal; | |
790 | register int end; | |
791 | register int tab_width = XINT (current_buffer->tab_width); | |
56a98455 | 792 | register int ctl_arrow = !NILP (current_buffer->ctl_arrow); |
d44f12b4 | 793 | register struct Lisp_Char_Table *dp = buffer_display_table (); |
a997a7ca | 794 | register int multibyte = !NILP (current_buffer->enable_multibyte_characters); |
993b6404 JB |
795 | |
796 | Lisp_Object val; | |
797 | int prev_col; | |
798 | int c; | |
9a21bb64 RS |
799 | int next_boundary; |
800 | ||
fe0066f5 RS |
801 | int pos_byte, end_byte, next_boundary_byte; |
802 | ||
ccdcf1f5 | 803 | if (tab_width <= 0 || tab_width > 1000) tab_width = 8; |
993b6404 JB |
804 | CHECK_NATNUM (column, 0); |
805 | goal = XINT (column); | |
806 | ||
6ec8bbd2 | 807 | pos = PT; |
fe0066f5 | 808 | pos_byte = PT_BYTE; |
993b6404 | 809 | end = ZV; |
fe0066f5 | 810 | end_byte = ZV_BYTE; |
9a21bb64 | 811 | next_boundary = pos; |
fe0066f5 | 812 | next_boundary_byte = PT_BYTE; |
993b6404 JB |
813 | |
814 | /* If we're starting past the desired column, | |
815 | back up to beginning of line and scan from there. */ | |
816 | if (col > goal) | |
817 | { | |
9a21bb64 | 818 | end = pos; |
a997a7ca | 819 | pos = current_column_bol_cache; |
fe0066f5 | 820 | pos_byte = CHAR_TO_BYTE (pos); |
993b6404 JB |
821 | col = 0; |
822 | } | |
823 | ||
8861f16f | 824 | while (pos < end) |
993b6404 | 825 | { |
9a21bb64 RS |
826 | while (pos == next_boundary) |
827 | { | |
fe0066f5 | 828 | int prev = pos; |
9a21bb64 | 829 | pos = skip_invisible (pos, &next_boundary, end, Qnil); |
fe0066f5 RS |
830 | if (pos != prev) |
831 | pos_byte = CHAR_TO_BYTE (pos); | |
832 | next_boundary_byte = CHAR_TO_BYTE (next_boundary); | |
9a21bb64 RS |
833 | if (pos >= end) |
834 | goto endloop; | |
835 | } | |
836 | ||
8861f16f RS |
837 | /* Test reaching the goal column. We do this after skipping |
838 | invisible characters, so that we put point before the | |
839 | character on which the cursor will appear. */ | |
840 | if (col >= goal) | |
841 | break; | |
842 | ||
fe0066f5 | 843 | c = FETCH_BYTE (pos_byte); |
c9c0f7cf KH |
844 | if (dp != 0 |
845 | && ! (multibyte && BASE_LEADING_CODE_P (c)) | |
846 | && VECTORP (DISP_CHAR_VECTOR (dp, c))) | |
f845b8b2 RS |
847 | { |
848 | col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; | |
fe0066f5 | 849 | pos_byte++; |
f845b8b2 | 850 | pos++; |
0088a46e | 851 | continue; |
f845b8b2 | 852 | } |
993b6404 JB |
853 | if (c == '\n') |
854 | break; | |
855 | if (c == '\r' && EQ (current_buffer->selective_display, Qt)) | |
856 | break; | |
857 | pos++; | |
fe0066f5 | 858 | pos_byte++; |
993b6404 JB |
859 | if (c == '\t') |
860 | { | |
861 | prev_col = col; | |
862 | col += tab_width; | |
863 | col = col / tab_width * tab_width; | |
864 | } | |
993b6404 | 865 | else if (ctl_arrow && (c < 040 || c == 0177)) |
bbc2998f | 866 | col += 2; |
a997a7ca | 867 | else if (c < 040 || c == 0177) |
bbc2998f | 868 | col += 4; |
a997a7ca | 869 | else if (c < 0177) |
993b6404 | 870 | col++; |
a997a7ca KH |
871 | else if (multibyte && BASE_LEADING_CODE_P (c)) |
872 | { | |
873 | /* Start of multi-byte form. */ | |
874 | unsigned char *ptr; | |
c9c0f7cf | 875 | int bytes, width, wide_column; |
a997a7ca | 876 | |
c9c0f7cf | 877 | pos_byte--; |
fe0066f5 | 878 | ptr = BYTE_POS_ADDR (pos_byte); |
0377370f | 879 | MULTIBYTE_BYTES_WIDTH (ptr, dp); |
c9c0f7cf KH |
880 | pos_byte += bytes; |
881 | col += width; | |
a997a7ca KH |
882 | } |
883 | else | |
884 | col += 4; | |
993b6404 | 885 | } |
9a21bb64 | 886 | endloop: |
993b6404 | 887 | |
fe0066f5 | 888 | SET_PT_BOTH (pos, pos_byte); |
993b6404 JB |
889 | |
890 | /* If a tab char made us overshoot, change it to spaces | |
891 | and scan through it again. */ | |
56a98455 | 892 | if (!NILP (force) && col > goal && c == '\t' && prev_col < goal) |
993b6404 | 893 | { |
fe0066f5 | 894 | int old_point, old_point_byte; |
70ee42f7 | 895 | |
6ec8bbd2 | 896 | del_range (PT - 1, PT); |
70ee42f7 | 897 | Findent_to (make_number (goal), Qnil); |
6ec8bbd2 | 898 | old_point = PT; |
fe0066f5 | 899 | old_point_byte = PT_BYTE; |
70ee42f7 | 900 | Findent_to (make_number (col), Qnil); |
fe0066f5 | 901 | SET_PT_BOTH (old_point, old_point_byte); |
5a05d3d2 RS |
902 | /* Set the last_known... vars consistently. */ |
903 | col = goal; | |
993b6404 JB |
904 | } |
905 | ||
906 | /* If line ends prematurely, add space to the end. */ | |
de4075cf | 907 | if (col < goal && EQ (force, Qt)) |
230a4cbd | 908 | Findent_to (make_number (col = goal), Qnil); |
993b6404 JB |
909 | |
910 | last_known_column = col; | |
6ec8bbd2 | 911 | last_known_column_point = PT; |
993b6404 JB |
912 | last_known_column_modified = MODIFF; |
913 | ||
94d92e9c | 914 | XSETFASTINT (val, col); |
993b6404 JB |
915 | return val; |
916 | } | |
917 | \f | |
0aa01123 JB |
918 | /* compute_motion: compute buffer posn given screen posn and vice versa */ |
919 | ||
993b6404 JB |
920 | struct position val_compute_motion; |
921 | ||
922 | /* Scan the current buffer forward from offset FROM, pretending that | |
923 | this is at line FROMVPOS, column FROMHPOS, until reaching buffer | |
924 | offset TO or line TOVPOS, column TOHPOS (whichever comes first), | |
0aa01123 JB |
925 | and return the ending buffer position and screen location. If we |
926 | can't hit the requested column exactly (because of a tab or other | |
927 | multi-column character), overshoot. | |
993b6404 | 928 | |
2ab90d49 KH |
929 | DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings |
930 | at FROM. This is the case if FROMVPOS and FROMVPOS came from an | |
931 | earlier call to compute_motion. The other common case is that FROMHPOS | |
932 | is zero and FROM is a position that "belongs" at column zero, but might | |
933 | be shifted by overlay strings; in this case DID_MOTION should be 0. | |
934 | ||
993b6404 JB |
935 | WIDTH is the number of columns available to display text; |
936 | compute_motion uses this to handle continuation lines and such. | |
937 | HSCROLL is the number of columns not being displayed at the left | |
938 | margin; this is usually taken from a window's hscroll member. | |
a9764248 JB |
939 | TAB_OFFSET is the number of columns of the first tab that aren't |
940 | being displayed, perhaps because of a continuation line or | |
941 | something. | |
993b6404 JB |
942 | |
943 | compute_motion returns a pointer to a struct position. The bufpos | |
944 | member gives the buffer position at the end of the scan, and hpos | |
0aa01123 JB |
945 | and vpos give its cartesian location. prevhpos is the column at |
946 | which the character before bufpos started, and contin is non-zero | |
947 | if we reached the current line by continuing the previous. | |
948 | ||
949 | Note that FROMHPOS and TOHPOS should be expressed in real screen | |
950 | columns, taking HSCROLL and the truncation glyph at the left margin | |
951 | into account. That is, beginning-of-line moves you to the hpos | |
952 | -HSCROLL + (HSCROLL > 0). | |
993b6404 JB |
953 | |
954 | For example, to find the buffer position of column COL of line LINE | |
955 | of a certain window, pass the window's starting location as FROM | |
956 | and the window's upper-left coordinates as FROMVPOS and FROMHPOS. | |
957 | Pass the buffer's ZV as TO, to limit the scan to the end of the | |
958 | visible section of the buffer, and pass LINE and COL as TOVPOS and | |
2ff4775b | 959 | TOHPOS. |
993b6404 JB |
960 | |
961 | When displaying in window w, a typical formula for WIDTH is: | |
962 | ||
963 | window_width - 1 | |
a3c87d4e | 964 | - (has_vertical_scroll_bars |
40284d6b | 965 | ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame)) |
fa61c701 | 966 | : (window_width + window_left != frame_width)) |
993b6404 JB |
967 | |
968 | where | |
969 | window_width is XFASTINT (w->width), | |
970 | window_left is XFASTINT (w->left), | |
a3c87d4e JB |
971 | has_vertical_scroll_bars is |
972 | FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window))) | |
fa61c701 | 973 | and frame_width = FRAME_WIDTH (XFRAME (window->frame)) |
993b6404 | 974 | |
1827d036 KH |
975 | Or you can let window_internal_width do this all for you, and write: |
976 | window_internal_width (w) - 1 | |
fa61c701 JB |
977 | |
978 | The `-1' accounts for the continuation-line backslashes; the rest | |
7e7a76b5 | 979 | accounts for window borders if the window is split horizontally, and |
1827d036 | 980 | the scroll bars if they are turned on. */ |
993b6404 JB |
981 | |
982 | struct position * | |
2ab90d49 | 983 | compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win) |
993b6404 | 984 | int from, fromvpos, fromhpos, to, tovpos, tohpos; |
2ab90d49 | 985 | int did_motion; |
993b6404 JB |
986 | register int width; |
987 | int hscroll, tab_offset; | |
88af3af4 | 988 | struct window *win; |
993b6404 | 989 | { |
cde9337b JB |
990 | register int hpos = fromhpos; |
991 | register int vpos = fromvpos; | |
992 | ||
993b6404 | 993 | register int pos; |
fe0066f5 | 994 | int pos_byte; |
993b6404 JB |
995 | register int c; |
996 | register int tab_width = XFASTINT (current_buffer->tab_width); | |
56a98455 | 997 | register int ctl_arrow = !NILP (current_buffer->ctl_arrow); |
d44f12b4 | 998 | register struct Lisp_Char_Table *dp = window_display_table (win); |
993b6404 | 999 | int selective |
eeaafd4f | 1000 | = (INTEGERP (current_buffer->selective_display) |
69eaf10d RS |
1001 | ? XINT (current_buffer->selective_display) |
1002 | : !NILP (current_buffer->selective_display) ? -1 : 0); | |
a997a7ca | 1003 | int prev_hpos = 0; |
993b6404 | 1004 | int selective_rlen |
eeaafd4f | 1005 | = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp)) |
dea4d2e6 | 1006 | ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0); |
2ab90d49 KH |
1007 | /* The next location where the `invisible' property changes, or an |
1008 | overlay starts or ends. */ | |
1009 | int next_boundary = from; | |
993b6404 | 1010 | |
0aa01123 JB |
1011 | /* For computing runs of characters with similar widths. |
1012 | Invariant: width_run_width is zero, or all the characters | |
2ff4775b | 1013 | from width_run_start to width_run_end have a fixed width of |
0aa01123 JB |
1014 | width_run_width. */ |
1015 | int width_run_start = from; | |
1016 | int width_run_end = from; | |
1017 | int width_run_width = 0; | |
1018 | Lisp_Object *width_table; | |
66c75ca5 | 1019 | Lisp_Object buffer; |
0aa01123 JB |
1020 | |
1021 | /* The next buffer pos where we should consult the width run cache. */ | |
1022 | int next_width_run = from; | |
0e435804 | 1023 | Lisp_Object window; |
0aa01123 | 1024 | |
a997a7ca | 1025 | int multibyte = !NILP (current_buffer->enable_multibyte_characters); |
c71c19f4 RS |
1026 | /* If previous char scanned was a wide character, |
1027 | this is the column where it ended. Otherwise, this is 0. */ | |
1028 | int wide_column_end_hpos = 0; | |
a997a7ca | 1029 | int prev_pos; /* Previous buffer position. */ |
fe0066f5 | 1030 | int prev_pos_byte; /* Previous buffer position. */ |
a997a7ca KH |
1031 | int contin_hpos; /* HPOS of last column of continued line. */ |
1032 | int prev_tab_offset; /* Previous tab offset. */ | |
1033 | ||
66c75ca5 | 1034 | XSETBUFFER (buffer, current_buffer); |
0e435804 | 1035 | XSETWINDOW (window, win); |
66c75ca5 | 1036 | |
0aa01123 JB |
1037 | width_run_cache_on_off (); |
1038 | if (dp == buffer_display_table ()) | |
1039 | width_table = (VECTORP (current_buffer->width_table) | |
1040 | ? XVECTOR (current_buffer->width_table)->contents | |
1041 | : 0); | |
1042 | else | |
1043 | /* If the window has its own display table, we can't use the width | |
1044 | run cache, because that's based on the buffer's display table. */ | |
1045 | width_table = 0; | |
1046 | ||
ccdcf1f5 | 1047 | if (tab_width <= 0 || tab_width > 1000) tab_width = 8; |
cde9337b | 1048 | |
a997a7ca | 1049 | pos = prev_pos = from; |
fe0066f5 | 1050 | pos_byte = prev_pos_byte = CHAR_TO_BYTE (from); |
a997a7ca KH |
1051 | contin_hpos = 0; |
1052 | prev_tab_offset = tab_offset; | |
2ab90d49 KH |
1053 | while (1) |
1054 | { | |
1055 | while (pos == next_boundary) | |
f75c0f8a | 1056 | { |
fe0066f5 | 1057 | int pos_here = pos; |
98136db3 RS |
1058 | int newpos; |
1059 | ||
f9ba10b0 RS |
1060 | /* Don't skip invisible if we are already at the margin. */ |
1061 | if (vpos > tovpos || vpos == tovpos && hpos >= tohpos) | |
1062 | { | |
1063 | if (contin_hpos && prev_hpos == 0 | |
1064 | && hpos > tohpos | |
1065 | && (contin_hpos == width || wide_column_end_hpos > width)) | |
1066 | { /* Line breaks because we can't put the character at the | |
1067 | previous line any more. It is not the multi-column | |
1068 | character continued in middle. Go back to previous | |
1069 | buffer position, screen position, and set tab offset | |
1070 | to previous value. It's the beginning of the | |
1071 | line. */ | |
1072 | pos = prev_pos; | |
1073 | pos_byte = prev_pos_byte; | |
1074 | hpos = prev_hpos; | |
1075 | tab_offset = prev_tab_offset; | |
1076 | } | |
1077 | break; | |
1078 | } | |
1079 | ||
2ab90d49 KH |
1080 | /* If the caller says that the screen position came from an earlier |
1081 | call to compute_motion, then we've already accounted for the | |
1082 | overlay strings at point. This is only true the first time | |
1083 | through, so clear the flag after testing it. */ | |
1084 | if (!did_motion) | |
1085 | /* We need to skip past the overlay strings. Currently those | |
a997a7ca | 1086 | strings must not contain TAB; |
2ab90d49 KH |
1087 | if we want to relax that restriction, something will have |
1088 | to be changed here. */ | |
a997a7ca KH |
1089 | { |
1090 | unsigned char *ovstr; | |
1091 | int ovlen = overlay_strings (pos, win, &ovstr); | |
4116deee KH |
1092 | hpos += ((multibyte && ovlen > 0) |
1093 | ? strwidth (ovstr, ovlen) : ovlen); | |
a997a7ca | 1094 | } |
2ab90d49 KH |
1095 | did_motion = 0; |
1096 | ||
1097 | if (pos >= to) | |
1098 | break; | |
66c75ca5 | 1099 | |
9a21bb64 RS |
1100 | /* Advance POS past invisible characters |
1101 | (but not necessarily all that there are here), | |
1102 | and store in next_boundary the next position where | |
1103 | we need to call skip_invisible. */ | |
98136db3 RS |
1104 | newpos = skip_invisible (pos, &next_boundary, to, window); |
1105 | ||
1106 | if (newpos >= to) | |
e8cb089b RS |
1107 | { |
1108 | pos = min (to, newpos); | |
1109 | goto after_loop; | |
1110 | } | |
98136db3 | 1111 | |
fe0066f5 RS |
1112 | if (newpos != pos_here) |
1113 | { | |
1114 | pos = newpos; | |
1115 | pos_byte = CHAR_TO_BYTE (pos); | |
1116 | } | |
f75c0f8a | 1117 | } |
2ab90d49 KH |
1118 | |
1119 | /* Handle right margin. */ | |
a997a7ca KH |
1120 | /* Note on a wide-column character. |
1121 | ||
1122 | Characters are classified into the following three categories | |
1123 | according to the width (columns occupied on screen). | |
1124 | ||
1125 | (1) single-column character: ex. `a' | |
1126 | (2) multi-column character: ex. `^A', TAB, `\033' | |
1127 | (3) wide-column character: ex. Japanese character, Chinese character | |
1128 | (In the following example, `W_' stands for them.) | |
1129 | ||
1130 | Multi-column characters can be divided around the right margin, | |
1131 | but wide-column characters cannot. | |
1132 | ||
1133 | NOTE: | |
1134 | ||
1135 | (*) The cursor is placed on the next character after the point. | |
1136 | ||
1137 | ---------- | |
1138 | abcdefghi\ | |
1139 | j ^---- next after the point | |
1140 | ^--- next char. after the point. | |
1141 | ---------- | |
1142 | In case of sigle-column character | |
1143 | ||
1144 | ---------- | |
1145 | abcdefgh\\ | |
1146 | 033 ^---- next after the point, next char. after the point. | |
1147 | ---------- | |
1148 | In case of multi-column character | |
1149 | ||
1150 | ---------- | |
1151 | abcdefgh\\ | |
1152 | W_ ^---- next after the point | |
1153 | ^---- next char. after the point. | |
1154 | ---------- | |
1155 | In case of wide-column character | |
1156 | ||
1157 | The problem here is continuation at a wide-column character. | |
1158 | In this case, the line may shorter less than WIDTH. | |
1159 | And we find the continuation AFTER it occurs. | |
1160 | ||
1161 | */ | |
1162 | ||
1163 | if (hpos > width) | |
2ab90d49 KH |
1164 | { |
1165 | if (hscroll | |
1166 | || (truncate_partial_width_windows | |
1167 | && width + 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win)))) | |
1168 | || !NILP (current_buffer->truncate_lines)) | |
1169 | { | |
529724fe AS |
1170 | /* Truncating: skip to newline, unless we are already past |
1171 | TO (we need to go back below). */ | |
1172 | if (pos <= to) | |
fe0066f5 RS |
1173 | { |
1174 | pos = find_before_next_newline (pos, to, 1); | |
1175 | pos_byte = CHAR_TO_BYTE (pos); | |
529724fe AS |
1176 | hpos = width; |
1177 | /* If we just skipped next_boundary, | |
1178 | loop around in the main while | |
1179 | and handle it. */ | |
1180 | if (pos >= next_boundary) | |
1181 | next_boundary = pos + 1; | |
1182 | prev_hpos = width; | |
1183 | prev_tab_offset = tab_offset; | |
fe0066f5 | 1184 | } |
2ab90d49 KH |
1185 | } |
1186 | else | |
1187 | { | |
1188 | /* Continuing. */ | |
a997a7ca KH |
1189 | /* Remember the previous value. */ |
1190 | prev_tab_offset = tab_offset; | |
1191 | ||
c9c0f7cf | 1192 | if (wide_column_end_hpos > width) |
a997a7ca KH |
1193 | { |
1194 | hpos -= prev_hpos; | |
1195 | tab_offset += prev_hpos; | |
1196 | } | |
1197 | else | |
1198 | { | |
1199 | tab_offset += width; | |
1200 | hpos -= width; | |
1201 | } | |
1202 | vpos++; | |
1203 | contin_hpos = prev_hpos; | |
1204 | prev_hpos = 0; | |
2ab90d49 KH |
1205 | } |
1206 | } | |
1207 | ||
1208 | /* Stop if past the target buffer position or screen position. */ | |
33fe7d20 | 1209 | if (pos > to) |
a997a7ca KH |
1210 | { |
1211 | /* Go back to the previous position. */ | |
1212 | pos = prev_pos; | |
fe0066f5 | 1213 | pos_byte = prev_pos_byte; |
a997a7ca KH |
1214 | hpos = prev_hpos; |
1215 | tab_offset = prev_tab_offset; | |
1216 | ||
1217 | /* NOTE on contin_hpos, hpos, and prev_hpos. | |
1218 | ||
1219 | ---------- | |
1220 | abcdefgh\\ | |
1221 | W_ ^---- contin_hpos | |
1222 | | ^----- hpos | |
1223 | \---- prev_hpos | |
1224 | ---------- | |
1225 | */ | |
1226 | ||
1227 | if (contin_hpos && prev_hpos == 0 | |
c9c0f7cf | 1228 | && contin_hpos < width && !wide_column_end_hpos) |
a997a7ca KH |
1229 | { |
1230 | /* Line breaking occurs in the middle of multi-column | |
1231 | character. Go back to previous line. */ | |
1232 | hpos = contin_hpos; | |
1233 | vpos = vpos - 1; | |
1234 | } | |
1235 | else if (c == '\n') | |
1236 | /* If previous character is NEWLINE, | |
1237 | set VPOS back to previous line */ | |
1238 | vpos = vpos - 1; | |
1239 | break; | |
1240 | } | |
1241 | ||
33fe7d20 RS |
1242 | if (vpos > tovpos || vpos == tovpos && hpos >= tohpos) |
1243 | { | |
1244 | if (contin_hpos && prev_hpos == 0 | |
9129bcc3 KH |
1245 | && hpos > tohpos |
1246 | && (contin_hpos == width || wide_column_end_hpos > width)) | |
33fe7d20 RS |
1247 | { /* Line breaks because we can't put the character at the |
1248 | previous line any more. It is not the multi-column | |
1249 | character continued in middle. Go back to previous | |
1250 | buffer position, screen position, and set tab offset | |
1251 | to previous value. It's the beginning of the | |
1252 | line. */ | |
1253 | pos = prev_pos; | |
1254 | pos_byte = prev_pos_byte; | |
1255 | hpos = prev_hpos; | |
1256 | tab_offset = prev_tab_offset; | |
1257 | } | |
1258 | break; | |
1259 | } | |
a997a7ca | 1260 | if (pos == ZV) /* We cannot go beyond ZV. Stop here. */ |
2ab90d49 KH |
1261 | break; |
1262 | ||
2ab90d49 | 1263 | prev_hpos = hpos; |
a997a7ca | 1264 | prev_pos = pos; |
fe0066f5 | 1265 | prev_pos_byte = pos_byte; |
c9c0f7cf | 1266 | wide_column_end_hpos = 0; |
0aa01123 JB |
1267 | |
1268 | /* Consult the width run cache to see if we can avoid inspecting | |
1269 | the text character-by-character. */ | |
1270 | if (current_buffer->width_run_cache && pos >= next_width_run) | |
1271 | { | |
1272 | int run_end; | |
1273 | int common_width | |
1274 | = region_cache_forward (current_buffer, | |
1275 | current_buffer->width_run_cache, | |
1276 | pos, &run_end); | |
1277 | ||
1278 | /* A width of zero means the character's width varies (like | |
1279 | a tab), is meaningless (like a newline), or we just don't | |
1280 | want to skip over it for some other reason. */ | |
1281 | if (common_width != 0) | |
1282 | { | |
1283 | int run_end_hpos; | |
1284 | ||
1285 | /* Don't go past the final buffer posn the user | |
1286 | requested. */ | |
1287 | if (run_end > to) | |
1288 | run_end = to; | |
1289 | ||
1290 | run_end_hpos = hpos + (run_end - pos) * common_width; | |
1291 | ||
1292 | /* Don't go past the final horizontal position the user | |
1293 | requested. */ | |
1294 | if (vpos == tovpos && run_end_hpos > tohpos) | |
1295 | { | |
1296 | run_end = pos + (tohpos - hpos) / common_width; | |
1297 | run_end_hpos = hpos + (run_end - pos) * common_width; | |
1298 | } | |
2ff4775b | 1299 | |
0aa01123 JB |
1300 | /* Don't go past the margin. */ |
1301 | if (run_end_hpos >= width) | |
1302 | { | |
1303 | run_end = pos + (width - hpos) / common_width; | |
1304 | run_end_hpos = hpos + (run_end - pos) * common_width; | |
1305 | } | |
1306 | ||
1307 | hpos = run_end_hpos; | |
1308 | if (run_end > pos) | |
1309 | prev_hpos = hpos - common_width; | |
fe0066f5 RS |
1310 | if (pos != run_end) |
1311 | { | |
1312 | pos = run_end; | |
1313 | pos_byte = CHAR_TO_BYTE (pos); | |
1314 | } | |
0aa01123 JB |
1315 | } |
1316 | ||
1317 | next_width_run = run_end + 1; | |
1318 | } | |
1319 | ||
1320 | /* We have to scan the text character-by-character. */ | |
993b6404 | 1321 | else |
2ab90d49 | 1322 | { |
fe0066f5 RS |
1323 | c = FETCH_BYTE (pos_byte); |
1324 | pos++, pos_byte++; | |
0aa01123 | 1325 | |
2ab90d49 KH |
1326 | /* Perhaps add some info to the width_run_cache. */ |
1327 | if (current_buffer->width_run_cache) | |
1328 | { | |
1329 | /* Is this character part of the current run? If so, extend | |
1330 | the run. */ | |
1331 | if (pos - 1 == width_run_end | |
5db0afb7 | 1332 | && XFASTINT (width_table[c]) == width_run_width) |
2ab90d49 KH |
1333 | width_run_end = pos; |
1334 | ||
1335 | /* The previous run is over, since this is a character at a | |
1336 | different position, or a different width. */ | |
1337 | else | |
1338 | { | |
1339 | /* Have we accumulated a run to put in the cache? | |
1340 | (Currently, we only cache runs of width == 1). */ | |
1341 | if (width_run_start < width_run_end | |
1342 | && width_run_width == 1) | |
1343 | know_region_cache (current_buffer, | |
1344 | current_buffer->width_run_cache, | |
1345 | width_run_start, width_run_end); | |
1346 | ||
1347 | /* Start recording a new width run. */ | |
5db0afb7 | 1348 | width_run_width = XFASTINT (width_table[c]); |
2ab90d49 KH |
1349 | width_run_start = pos - 1; |
1350 | width_run_end = pos; | |
1351 | } | |
1352 | } | |
993b6404 | 1353 | |
c9c0f7cf KH |
1354 | if (dp != 0 |
1355 | && ! (multibyte && BASE_LEADING_CODE_P (c)) | |
1356 | && VECTORP (DISP_CHAR_VECTOR (dp, c))) | |
2ab90d49 KH |
1357 | hpos += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; |
1358 | else if (c >= 040 && c < 0177) | |
1359 | hpos++; | |
1360 | else if (c == '\t') | |
993b6404 | 1361 | { |
2ab90d49 KH |
1362 | int tem = (hpos + tab_offset + hscroll - (hscroll > 0)) % tab_width; |
1363 | if (tem < 0) | |
1364 | tem += tab_width; | |
1365 | hpos += tab_width - tem; | |
993b6404 | 1366 | } |
2ab90d49 | 1367 | else if (c == '\n') |
993b6404 | 1368 | { |
fe0066f5 RS |
1369 | if (selective > 0 |
1370 | && indented_beyond_p (pos, pos_byte, selective)) | |
2ab90d49 | 1371 | { |
626665b4 KH |
1372 | /* If (pos == to), we don't have to take care of |
1373 | selective display. */ | |
1374 | if (pos < to) | |
2ab90d49 | 1375 | { |
626665b4 KH |
1376 | /* Skip any number of invisible lines all at once */ |
1377 | do | |
fe0066f5 | 1378 | { |
a9a679b7 KH |
1379 | pos = find_before_next_newline (pos, to, 1); |
1380 | if (pos < to) | |
1381 | pos++; | |
fe0066f5 RS |
1382 | pos_byte = CHAR_TO_BYTE (pos); |
1383 | } | |
626665b4 | 1384 | while (pos < to |
fe0066f5 | 1385 | && indented_beyond_p (pos, pos_byte, selective)); |
626665b4 KH |
1386 | /* Allow for the " ..." that is displayed for them. */ |
1387 | if (selective_rlen) | |
1388 | { | |
1389 | hpos += selective_rlen; | |
1390 | if (hpos >= width) | |
1391 | hpos = width; | |
1392 | } | |
fe0066f5 | 1393 | DEC_BOTH (pos, pos_byte); |
626665b4 KH |
1394 | /* We have skipped the invis text, but not the |
1395 | newline after. */ | |
2ab90d49 | 1396 | } |
2ab90d49 KH |
1397 | } |
1398 | else | |
1399 | { | |
1400 | /* A visible line. */ | |
1401 | vpos++; | |
1402 | hpos = 0; | |
1403 | hpos -= hscroll; | |
1404 | /* Count the truncation glyph on column 0 */ | |
1405 | if (hscroll > 0) | |
1406 | hpos++; | |
1407 | tab_offset = 0; | |
1408 | } | |
a997a7ca | 1409 | contin_hpos = 0; |
993b6404 | 1410 | } |
2ab90d49 KH |
1411 | else if (c == CR && selective < 0) |
1412 | { | |
1413 | /* In selective display mode, | |
1414 | everything from a ^M to the end of the line is invisible. | |
1415 | Stop *before* the real newline. */ | |
626665b4 | 1416 | if (pos < to) |
fe0066f5 RS |
1417 | { |
1418 | pos = find_before_next_newline (pos, to, 1); | |
1419 | pos_byte = CHAR_TO_BYTE (pos); | |
1420 | } | |
55f7a32b RS |
1421 | /* If we just skipped next_boundary, |
1422 | loop around in the main while | |
1423 | and handle it. */ | |
1424 | if (pos > next_boundary) | |
1425 | next_boundary = pos; | |
2ab90d49 KH |
1426 | /* Allow for the " ..." that is displayed for them. */ |
1427 | if (selective_rlen) | |
1428 | { | |
1429 | hpos += selective_rlen; | |
1430 | if (hpos >= width) | |
1431 | hpos = width; | |
1432 | } | |
1433 | } | |
a997a7ca KH |
1434 | else if (multibyte && BASE_LEADING_CODE_P (c)) |
1435 | { | |
1436 | /* Start of multi-byte form. */ | |
1437 | unsigned char *ptr; | |
c9c0f7cf | 1438 | int bytes, width, wide_column; |
a997a7ca | 1439 | |
c9c0f7cf | 1440 | pos_byte--; /* rewind POS_BYTE */ |
fe0066f5 | 1441 | ptr = BYTE_POS_ADDR (pos_byte); |
0377370f | 1442 | MULTIBYTE_BYTES_WIDTH (ptr, dp); |
c9c0f7cf | 1443 | pos_byte += bytes; |
4116deee KH |
1444 | if (wide_column) |
1445 | wide_column_end_hpos = hpos + wide_column; | |
c9c0f7cf | 1446 | hpos += width; |
a997a7ca | 1447 | } |
2ab90d49 KH |
1448 | else |
1449 | hpos += (ctl_arrow && c < 0200) ? 2 : 4; | |
993b6404 JB |
1450 | } |
1451 | } | |
1452 | ||
98136db3 RS |
1453 | after_loop: |
1454 | ||
0aa01123 JB |
1455 | /* Remember any final width run in the cache. */ |
1456 | if (current_buffer->width_run_cache | |
1457 | && width_run_width == 1 | |
1458 | && width_run_start < width_run_end) | |
1459 | know_region_cache (current_buffer, current_buffer->width_run_cache, | |
1460 | width_run_start, width_run_end); | |
1461 | ||
993b6404 | 1462 | val_compute_motion.bufpos = pos; |
fe0066f5 | 1463 | val_compute_motion.bytepos = pos_byte; |
cde9337b JB |
1464 | val_compute_motion.hpos = hpos; |
1465 | val_compute_motion.vpos = vpos; | |
ef3af330 AS |
1466 | if (contin_hpos && prev_hpos == 0) |
1467 | val_compute_motion.prevhpos = contin_hpos; | |
1468 | else | |
1469 | val_compute_motion.prevhpos = prev_hpos; | |
927b5a55 RS |
1470 | /* We alalways handle all of them here; none of them remain to do. */ |
1471 | val_compute_motion.ovstring_chars_done = 0; | |
993b6404 JB |
1472 | |
1473 | /* Nonzero if have just continued a line */ | |
a997a7ca | 1474 | val_compute_motion.contin = (contin_hpos && prev_hpos == 0); |
993b6404 JB |
1475 | |
1476 | return &val_compute_motion; | |
1477 | } | |
993b6404 | 1478 | |
8720a429 | 1479 | |
992371ca KH |
1480 | #if 0 /* The doc string is too long for some compilers, |
1481 | but make-docfile can find it in this comment. */ | |
88af3af4 | 1482 | DEFUN ("compute-motion", Ffoo, Sfoo, 7, 7, 0, |
42918ba5 RS |
1483 | "Scan through the current buffer, calculating screen position.\n\ |
1484 | Scan the current buffer forward from offset FROM,\n\ | |
1485 | assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\ | |
1486 | to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\ | |
1487 | and return the ending buffer position and screen location.\n\ | |
1488 | \n\ | |
88af3af4 | 1489 | There are three additional arguments:\n\ |
42918ba5 RS |
1490 | \n\ |
1491 | WIDTH is the number of columns available to display text;\n\ | |
1492 | this affects handling of continuation lines.\n\ | |
992371ca KH |
1493 | This is usually the value returned by `window-width', less one (to allow\n\ |
1494 | for the continuation glyph).\n\ | |
42918ba5 RS |
1495 | \n\ |
1496 | OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\ | |
1497 | HSCROLL is the number of columns not being displayed at the left\n\ | |
1498 | margin; this is usually taken from a window's hscroll member.\n\ | |
1499 | TAB-OFFSET is the number of columns of the first tab that aren't\n\ | |
1500 | being displayed, perhaps because the line was continued within it.\n\ | |
4fb76787 | 1501 | If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\ |
69eaf10d | 1502 | \n\ |
ef23bc07 KH |
1503 | WINDOW is the window to operate on. It is used to choose the display table;\n\ |
1504 | if it is showing the current buffer, it is used also for\n\ | |
1505 | deciding which overlay properties apply.\n\ | |
1506 | Note that `compute-motion' always operates on the current buffer.\n\ | |
42918ba5 RS |
1507 | \n\ |
1508 | The value is a list of five elements:\n\ | |
faa5c515 | 1509 | (POS HPOS VPOS PREVHPOS CONTIN)\n\ |
42918ba5 RS |
1510 | POS is the buffer position where the scan stopped.\n\ |
1511 | VPOS is the vertical position where the scan stopped.\n\ | |
1512 | HPOS is the horizontal position where the scan stopped.\n\ | |
1513 | \n\ | |
1514 | PREVHPOS is the horizontal position one character back from POS.\n\ | |
1515 | CONTIN is t if a line was continued after (or within) the previous character.\n\ | |
1516 | \n\ | |
1517 | For example, to find the buffer position of column COL of line LINE\n\ | |
1518 | of a certain window, pass the window's starting location as FROM\n\ | |
1519 | and the window's upper-left coordinates as FROMPOS.\n\ | |
1520 | Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\ | |
1521 | visible section of the buffer, and pass LINE and COL as TOPOS.") | |
5844e1c4 | 1522 | (from, frompos, to, topos, width, offsets, window) |
992371ca KH |
1523 | #endif |
1524 | ||
88af3af4 | 1525 | DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0, |
992371ca | 1526 | 0) |
88af3af4 | 1527 | (from, frompos, to, topos, width, offsets, window) |
42918ba5 | 1528 | Lisp_Object from, frompos, to, topos; |
88af3af4 | 1529 | Lisp_Object width, offsets, window; |
42918ba5 RS |
1530 | { |
1531 | Lisp_Object bufpos, hpos, vpos, prevhpos, contin; | |
1532 | struct position *pos; | |
1533 | int hscroll, tab_offset; | |
1534 | ||
26a8d14b | 1535 | CHECK_NUMBER_COERCE_MARKER (from, 0); |
42918ba5 | 1536 | CHECK_CONS (frompos, 0); |
70949dac KR |
1537 | CHECK_NUMBER (XCAR (frompos), 0); |
1538 | CHECK_NUMBER (XCDR (frompos), 0); | |
26a8d14b | 1539 | CHECK_NUMBER_COERCE_MARKER (to, 0); |
42918ba5 | 1540 | CHECK_CONS (topos, 0); |
70949dac KR |
1541 | CHECK_NUMBER (XCAR (topos), 0); |
1542 | CHECK_NUMBER (XCDR (topos), 0); | |
26a8d14b | 1543 | CHECK_NUMBER (width, 0); |
42918ba5 RS |
1544 | if (!NILP (offsets)) |
1545 | { | |
1546 | CHECK_CONS (offsets, 0); | |
70949dac KR |
1547 | CHECK_NUMBER (XCAR (offsets), 0); |
1548 | CHECK_NUMBER (XCDR (offsets), 0); | |
1549 | hscroll = XINT (XCAR (offsets)); | |
1550 | tab_offset = XINT (XCDR (offsets)); | |
42918ba5 RS |
1551 | } |
1552 | else | |
1553 | hscroll = tab_offset = 0; | |
1554 | ||
88af3af4 KH |
1555 | if (NILP (window)) |
1556 | window = Fselected_window (); | |
1557 | else | |
1558 | CHECK_LIVE_WINDOW (window, 0); | |
1559 | ||
9fdae274 KH |
1560 | if (XINT (from) < BEGV || XINT (from) > ZV) |
1561 | args_out_of_range_3 (from, make_number (BEGV), make_number (ZV)); | |
1562 | if (XINT (to) < BEGV || XINT (to) > ZV) | |
1563 | args_out_of_range_3 (to, make_number (BEGV), make_number (ZV)); | |
1564 | ||
70949dac KR |
1565 | pos = compute_motion (XINT (from), XINT (XCDR (frompos)), |
1566 | XINT (XCAR (frompos)), 0, | |
1567 | XINT (to), XINT (XCDR (topos)), | |
1568 | XINT (XCAR (topos)), | |
88af3af4 KH |
1569 | XINT (width), hscroll, tab_offset, |
1570 | XWINDOW (window)); | |
42918ba5 | 1571 | |
94d92e9c | 1572 | XSETFASTINT (bufpos, pos->bufpos); |
f8f645a1 KH |
1573 | XSETINT (hpos, pos->hpos); |
1574 | XSETINT (vpos, pos->vpos); | |
1575 | XSETINT (prevhpos, pos->prevhpos); | |
42918ba5 RS |
1576 | |
1577 | return Fcons (bufpos, | |
1578 | Fcons (hpos, | |
1579 | Fcons (vpos, | |
1580 | Fcons (prevhpos, | |
1581 | Fcons (pos->contin ? Qt : Qnil, Qnil))))); | |
1582 | ||
1583 | } | |
993b6404 | 1584 | \f |
0aa01123 | 1585 | /* Fvertical_motion and vmotion */ |
993b6404 JB |
1586 | struct position val_vmotion; |
1587 | ||
1588 | struct position * | |
99ce22d5 KH |
1589 | vmotion (from, vtarget, w) |
1590 | register int from, vtarget; | |
1591 | struct window *w; | |
993b6404 | 1592 | { |
99ce22d5 KH |
1593 | int width = window_internal_width (w) - 1; |
1594 | int hscroll = XINT (w->hscroll); | |
993b6404 JB |
1595 | struct position pos; |
1596 | /* vpos is cumulative vertical position, changed as from is changed */ | |
1597 | register int vpos = 0; | |
92992c7e | 1598 | Lisp_Object prevline; |
993b6404 | 1599 | register int first; |
fe0066f5 | 1600 | int from_byte; |
993b6404 JB |
1601 | int lmargin = hscroll > 0 ? 1 - hscroll : 0; |
1602 | int selective | |
eeaafd4f KH |
1603 | = (INTEGERP (current_buffer->selective_display) |
1604 | ? XINT (current_buffer->selective_display) | |
1605 | : !NILP (current_buffer->selective_display) ? -1 : 0); | |
99ce22d5 KH |
1606 | Lisp_Object window; |
1607 | int start_hpos = 0; | |
2ab90d49 | 1608 | int did_motion; |
7ac57cb3 RS |
1609 | /* This is the object we use for fetching character properties. */ |
1610 | Lisp_Object text_prop_object; | |
99ce22d5 KH |
1611 | |
1612 | XSETWINDOW (window, w); | |
1613 | ||
7ac57cb3 RS |
1614 | /* If the window contains this buffer, use it for getting text properties. |
1615 | Otherwise use the current buffer as arg for doing that. */ | |
1616 | if (EQ (w->buffer, Fcurrent_buffer ())) | |
1617 | text_prop_object = window; | |
1618 | else | |
1619 | text_prop_object = Fcurrent_buffer (); | |
1620 | ||
99ce22d5 | 1621 | if (vpos >= vtarget) |
993b6404 | 1622 | { |
99ce22d5 | 1623 | /* To move upward, go a line at a time until |
fe0066f5 | 1624 | we have gone at least far enough. */ |
99ce22d5 KH |
1625 | |
1626 | first = 1; | |
1627 | ||
1628 | while ((vpos > vtarget || first) && from > BEGV) | |
993b6404 | 1629 | { |
66c75ca5 RS |
1630 | Lisp_Object propval; |
1631 | ||
99ce22d5 | 1632 | XSETFASTINT (prevline, find_next_newline_no_quit (from - 1, -1)); |
92992c7e | 1633 | while (XFASTINT (prevline) > BEGV |
5a05d3d2 | 1634 | && ((selective > 0 |
fe0066f5 RS |
1635 | && indented_beyond_p (XFASTINT (prevline), |
1636 | CHAR_TO_BYTE (XFASTINT (prevline)), | |
1637 | selective)) | |
5a05d3d2 RS |
1638 | #ifdef USE_TEXT_PROPERTIES |
1639 | /* watch out for newlines with `invisible' property */ | |
66c75ca5 RS |
1640 | || (propval = Fget_char_property (prevline, |
1641 | Qinvisible, | |
7ac57cb3 | 1642 | text_prop_object), |
66c75ca5 | 1643 | TEXT_PROP_MEANS_INVISIBLE (propval)) |
5a05d3d2 | 1644 | #endif |
99ce22d5 | 1645 | )) |
94d92e9c KH |
1646 | XSETFASTINT (prevline, |
1647 | find_next_newline_no_quit (XFASTINT (prevline) - 1, | |
1648 | -1)); | |
92992c7e | 1649 | pos = *compute_motion (XFASTINT (prevline), 0, |
99ce22d5 | 1650 | lmargin + (XFASTINT (prevline) == BEG |
92992c7e | 1651 | ? start_hpos : 0), |
2ab90d49 | 1652 | 0, |
a997a7ca KH |
1653 | from, |
1654 | /* Don't care for VPOS... */ | |
1655 | 1 << (BITS_PER_SHORT - 1), | |
1656 | /* ... nor HPOS. */ | |
1657 | 1 << (BITS_PER_SHORT - 1), | |
2dd4e608 RS |
1658 | width, hscroll, |
1659 | /* This compensates for start_hpos | |
1660 | so that a tab as first character | |
1661 | still occupies 8 columns. */ | |
1662 | (XFASTINT (prevline) == BEG | |
1663 | ? -start_hpos : 0), | |
1664 | w); | |
99ce22d5 KH |
1665 | vpos -= pos.vpos; |
1666 | first = 0; | |
1667 | from = XFASTINT (prevline); | |
993b6404 | 1668 | } |
99ce22d5 KH |
1669 | |
1670 | /* If we made exactly the desired vertical distance, | |
1671 | or if we hit beginning of buffer, | |
1672 | return point found */ | |
1673 | if (vpos >= vtarget) | |
993b6404 | 1674 | { |
99ce22d5 | 1675 | val_vmotion.bufpos = from; |
fe0066f5 | 1676 | val_vmotion.bytepos = CHAR_TO_BYTE (from); |
99ce22d5 KH |
1677 | val_vmotion.vpos = vpos; |
1678 | val_vmotion.hpos = lmargin; | |
1679 | val_vmotion.contin = 0; | |
1680 | val_vmotion.prevhpos = 0; | |
927b5a55 | 1681 | val_vmotion.ovstring_chars_done = 0; |
a997a7ca | 1682 | val_vmotion.tab_offset = 0; /* For accumulating tab offset. */ |
99ce22d5 | 1683 | return &val_vmotion; |
993b6404 | 1684 | } |
993b6404 | 1685 | |
99ce22d5 KH |
1686 | /* Otherwise find the correct spot by moving down */ |
1687 | } | |
1688 | /* Moving downward is simple, but must calculate from beg of line | |
1689 | to determine hpos of starting point */ | |
fe0066f5 RS |
1690 | from_byte = CHAR_TO_BYTE (from); |
1691 | if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n') | |
993b6404 | 1692 | { |
2ab90d49 | 1693 | Lisp_Object propval; |
66c75ca5 | 1694 | |
99ce22d5 KH |
1695 | XSETFASTINT (prevline, find_next_newline_no_quit (from, -1)); |
1696 | while (XFASTINT (prevline) > BEGV | |
1697 | && ((selective > 0 | |
fe0066f5 RS |
1698 | && indented_beyond_p (XFASTINT (prevline), |
1699 | CHAR_TO_BYTE (XFASTINT (prevline)), | |
1700 | selective)) | |
5a05d3d2 | 1701 | #ifdef USE_TEXT_PROPERTIES |
99ce22d5 KH |
1702 | /* watch out for newlines with `invisible' property */ |
1703 | || (propval = Fget_char_property (prevline, Qinvisible, | |
7ac57cb3 | 1704 | text_prop_object), |
99ce22d5 | 1705 | TEXT_PROP_MEANS_INVISIBLE (propval)) |
5a05d3d2 | 1706 | #endif |
99ce22d5 KH |
1707 | )) |
1708 | XSETFASTINT (prevline, | |
1709 | find_next_newline_no_quit (XFASTINT (prevline) - 1, | |
1710 | -1)); | |
92992c7e | 1711 | pos = *compute_motion (XFASTINT (prevline), 0, |
99ce22d5 | 1712 | lmargin + (XFASTINT (prevline) == BEG |
92992c7e | 1713 | ? start_hpos : 0), |
2ab90d49 | 1714 | 0, |
a997a7ca KH |
1715 | from, |
1716 | /* Don't care for VPOS... */ | |
1717 | 1 << (BITS_PER_SHORT - 1), | |
1718 | /* ... nor HPOS. */ | |
1719 | 1 << (BITS_PER_SHORT - 1), | |
2dd4e608 RS |
1720 | width, hscroll, |
1721 | (XFASTINT (prevline) == BEG ? -start_hpos : 0), | |
1722 | w); | |
2ab90d49 | 1723 | did_motion = 1; |
993b6404 | 1724 | } |
99ce22d5 | 1725 | else |
993b6404 | 1726 | { |
99ce22d5 KH |
1727 | pos.hpos = lmargin + (from == BEG ? start_hpos : 0); |
1728 | pos.vpos = 0; | |
a997a7ca | 1729 | pos.tab_offset = 0; |
2ab90d49 | 1730 | did_motion = 0; |
993b6404 | 1731 | } |
2ab90d49 | 1732 | return compute_motion (from, vpos, pos.hpos, did_motion, |
a997a7ca | 1733 | ZV, vtarget, - (1 << (BITS_PER_SHORT - 1)), |
2dd4e608 | 1734 | width, hscroll, |
a997a7ca | 1735 | pos.tab_offset - (from == BEG ? start_hpos : 0), |
2dd4e608 | 1736 | w); |
993b6404 JB |
1737 | } |
1738 | ||
f1ecfe9b | 1739 | DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0, |
5f25660c KH |
1740 | "Move point to start of the screen line LINES lines down.\n\ |
1741 | If LINES is negative, this means moving up.\n\ | |
1742 | \n\ | |
1743 | This function is an ordinary cursor motion function\n\ | |
1744 | which calculates the new position based on how text would be displayed.\n\ | |
1745 | The new position may be the start of a line,\n\ | |
1746 | or just the start of a continuation line.\n\ | |
1747 | The function returns number of screen lines moved over;\n\ | |
1748 | that usually equals LINES, but may be closer to zero\n\ | |
1749 | if beginning or end of buffer was reached.\n\ | |
69eaf10d RS |
1750 | \n\ |
1751 | The optional second argument WINDOW specifies the window to use for\n\ | |
1752 | parameters such as width, horizontal scrolling, and so on.\n\ | |
5f25660c | 1753 | The default is to use the selected window's parameters.\n\ |
69eaf10d | 1754 | \n\ |
5f25660c KH |
1755 | `vertical-motion' always uses the current buffer,\n\ |
1756 | regardless of which buffer is displayed in WINDOW.\n\ | |
1757 | This is consistent with other cursor motion functions\n\ | |
1758 | and makes it possible to use `vertical-motion' in any buffer,\n\ | |
1759 | whether or not it is currently displayed in some window.") | |
f1ecfe9b RS |
1760 | (lines, window) |
1761 | Lisp_Object lines, window; | |
993b6404 | 1762 | { |
8720a429 GM |
1763 | struct it it; |
1764 | struct text_pos pt; | |
1765 | struct buffer *old, *b; | |
1766 | struct window *w; | |
993b6404 JB |
1767 | |
1768 | CHECK_NUMBER (lines, 0); | |
f1ecfe9b RS |
1769 | if (! NILP (window)) |
1770 | CHECK_WINDOW (window, 0); | |
1771 | else | |
92992c7e | 1772 | window = selected_window; |
993b6404 | 1773 | |
8720a429 GM |
1774 | w = XWINDOW (window); |
1775 | b = XBUFFER (w->buffer); | |
1776 | if (b != current_buffer) | |
1777 | { | |
1778 | old = current_buffer; | |
1779 | set_buffer_internal_1 (b); | |
1780 | } | |
1781 | else | |
1782 | old = NULL; | |
1783 | ||
1784 | SET_TEXT_POS (pt, PT, PT_BYTE); | |
1785 | start_display (&it, w, pt); | |
1786 | move_it_by_lines (&it, XINT (lines), 0); | |
1787 | SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it)); | |
1788 | ||
1789 | if (old) | |
1790 | set_buffer_internal_1 (old); | |
1791 | ||
1792 | return make_number (it.vpos); | |
993b6404 | 1793 | } |
8720a429 GM |
1794 | |
1795 | ||
993b6404 | 1796 | \f |
0aa01123 JB |
1797 | /* file's initialization. */ |
1798 | ||
dfcf069d | 1799 | void |
993b6404 JB |
1800 | syms_of_indent () |
1801 | { | |
1802 | DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode, | |
1803 | "*Indentation can insert tabs if this is non-nil.\n\ | |
1804 | Setting this variable automatically makes it local to the current buffer."); | |
1805 | indent_tabs_mode = 1; | |
1806 | ||
1807 | defsubr (&Scurrent_indentation); | |
1808 | defsubr (&Sindent_to); | |
1809 | defsubr (&Scurrent_column); | |
1810 | defsubr (&Smove_to_column); | |
1811 | defsubr (&Svertical_motion); | |
42918ba5 | 1812 | defsubr (&Scompute_motion); |
993b6404 | 1813 | } |