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