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