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