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