Fix previous change.
[emacs.git] / src / indent.c
blob507b8e36cbef5d6c1b47029ac88b5fdd52d399c9
1 /* Indentation functions.
2 Copyright (C) 1985,86,87,88,93,94,95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "charset.h"
26 #include "indent.h"
27 #include "frame.h"
28 #include "window.h"
29 #include "termchar.h"
30 #include "termopts.h"
31 #include "disptab.h"
32 #include "intervals.h"
33 #include "region-cache.h"
35 /* Indentation can insert tabs if this is non-zero;
36 otherwise always uses spaces */
37 int indent_tabs_mode;
39 #define min(a, b) ((a) < (b) ? (a) : (b))
40 #define max(a, b) ((a) > (b) ? (a) : (b))
42 #define CR 015
44 /* These three values memoize the current column to avoid recalculation */
45 /* Some things in set last_known_column_point to -1
46 to mark the memoized value as invalid */
47 /* Last value returned by current_column */
48 int last_known_column;
49 /* Value of point when current_column was called */
50 int last_known_column_point;
51 /* Value of MODIFF when current_column was called */
52 int last_known_column_modified;
54 static int current_column_1 ();
56 /* Cache of beginning of line found by the last call of
57 current_column. */
58 int current_column_bol_cache;
60 /* Get the display table to use for the current buffer. */
62 struct Lisp_Char_Table *
63 buffer_display_table ()
65 Lisp_Object thisbuf;
67 thisbuf = current_buffer->display_table;
68 if (DISP_TABLE_P (thisbuf))
69 return XCHAR_TABLE (thisbuf);
70 if (DISP_TABLE_P (Vstandard_display_table))
71 return XCHAR_TABLE (Vstandard_display_table);
72 return 0;
75 /* Width run cache considerations. */
77 /* Return the width of character C under display table DP. */
79 static int
80 character_width (c, dp)
81 int c;
82 struct Lisp_Char_Table *dp;
84 Lisp_Object elt;
86 /* These width computations were determined by examining the cases
87 in display_text_line. */
89 /* Everything can be handled by the display table, if it's
90 present and the element is right. */
91 if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
92 return XVECTOR (elt)->size;
94 /* Some characters are special. */
95 if (c == '\n' || c == '\t' || c == '\015')
96 return 0;
98 /* Printing characters have width 1. */
99 else if (c >= 040 && c < 0177)
100 return 1;
102 /* Everybody else (control characters, metacharacters) has other
103 widths. We could return their actual widths here, but they
104 depend on things like ctl_arrow and crud like that, and they're
105 not very common at all. So we'll just claim we don't know their
106 widths. */
107 else
108 return 0;
111 /* Return true iff the display table DISPTAB specifies the same widths
112 for characters as WIDTHTAB. We use this to decide when to
113 invalidate the buffer's width_run_cache. */
115 disptab_matches_widthtab (disptab, widthtab)
116 struct Lisp_Char_Table *disptab;
117 struct Lisp_Vector *widthtab;
119 int i;
121 if (widthtab->size != 256)
122 abort ();
124 for (i = 0; i < 256; i++)
125 if (character_width (i, disptab)
126 != XFASTINT (widthtab->contents[i]))
127 return 0;
129 return 1;
132 /* Recompute BUF's width table, using the display table DISPTAB. */
133 void
134 recompute_width_table (buf, disptab)
135 struct buffer *buf;
136 struct Lisp_Char_Table *disptab;
138 int i;
139 struct Lisp_Vector *widthtab;
141 if (!VECTORP (buf->width_table))
142 buf->width_table = Fmake_vector (make_number (256), make_number (0));
143 widthtab = XVECTOR (buf->width_table);
144 if (widthtab->size != 256)
145 abort ();
147 for (i = 0; i < 256; i++)
148 XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
151 /* Allocate or free the width run cache, as requested by the current
152 state of current_buffer's cache_long_line_scans variable. */
153 static void
154 width_run_cache_on_off ()
156 if (NILP (current_buffer->cache_long_line_scans)
157 /* And, for the moment, this feature doesn't work on multibyte
158 characters. */
159 || !NILP (current_buffer->enable_multibyte_characters))
161 /* It should be off. */
162 if (current_buffer->width_run_cache)
164 free_region_cache (current_buffer->width_run_cache);
165 current_buffer->width_run_cache = 0;
166 current_buffer->width_table = Qnil;
169 else
171 /* It should be on. */
172 if (current_buffer->width_run_cache == 0)
174 current_buffer->width_run_cache = new_region_cache ();
175 recompute_width_table (current_buffer, buffer_display_table ());
181 /* Skip some invisible characters starting from POS.
182 This includes characters invisible because of text properties
183 and characters invisible because of overlays.
185 If position POS is followed by invisible characters,
186 skip some of them and return the position after them.
187 Otherwise return POS itself.
189 Set *NEXT_BOUNDARY_P to the next position at which
190 it will be necessary to call this function again.
192 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
193 to a value greater than TO.
195 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
196 take account of overlays that apply only in WINDOW.
198 We don't necessarily skip all the invisible characters after POS
199 because that could take a long time. We skip a reasonable number
200 which can be skipped quickly. If there might be more invisible
201 characters immediately following, then *NEXT_BOUNDARY_P
202 will equal the return value. */
204 static int
205 skip_invisible (pos, next_boundary_p, to, window)
206 int pos;
207 int *next_boundary_p;
208 int to;
209 Lisp_Object window;
211 Lisp_Object prop, position, end, overlay_limit, proplimit;
212 Lisp_Object buffer;
214 XSETFASTINT (position, pos);
215 XSETBUFFER (buffer, current_buffer);
217 /* Give faster response for overlay lookup near POS. */
218 recenter_overlay_lists (current_buffer, pos);
220 /* We must not advance farther than the next overlay change.
221 The overlay change might change the invisible property;
222 or there might be overlay strings to be displayed there. */
223 overlay_limit = Fnext_overlay_change (position);
224 /* As for text properties, this gives a lower bound
225 for where the invisible text property could change. */
226 proplimit = Fnext_property_change (position, buffer, Qt);
227 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
228 proplimit = overlay_limit;
229 /* PROPLIMIT is now a lower bound for the next change
230 in invisible status. If that is plenty far away,
231 use that lower bound. */
232 if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
233 *next_boundary_p = XFASTINT (proplimit);
234 /* Otherwise, scan for the next `invisible' property change. */
235 else
237 /* Don't scan terribly far. */
238 XSETFASTINT (proplimit, min (pos + 100, to));
239 /* No matter what. don't go past next overlay change. */
240 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
241 proplimit = overlay_limit;
242 end = Fnext_single_property_change (position, Qinvisible,
243 buffer, proplimit);
244 /* Don't put the boundary in the middle of multibyte form if
245 there is no actual property change. */
246 if (end == pos + 100
247 && !NILP (current_buffer->enable_multibyte_characters)
248 && end < ZV)
249 while (pos < end && !CHAR_HEAD_P (POS_ADDR (end)))
250 end--;
251 *next_boundary_p = XFASTINT (end);
253 /* if the `invisible' property is set, we can skip to
254 the next property change */
255 if (!NILP (window) && EQ (XWINDOW (window)->buffer, buffer))
256 prop = Fget_char_property (position, Qinvisible, window);
257 else
258 prop = Fget_char_property (position, Qinvisible, buffer);
259 if (TEXT_PROP_MEANS_INVISIBLE (prop))
260 return *next_boundary_p;
261 return pos;
264 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
265 "Return the horizontal position of point. Beginning of line is column 0.\n\
266 This is calculated by adding together the widths of all the displayed\n\
267 representations of the character between the start of the previous line\n\
268 and point. (eg control characters will have a width of 2 or 4, tabs\n\
269 will have a variable width)\n\
270 Ignores finite width of frame, which means that this function may return\n\
271 values greater than (frame-width).\n\
272 Whether the line is visible (if `selective-display' is t) has no effect;\n\
273 however, ^M is treated as end of line when `selective-display' is t.")
276 Lisp_Object temp;
277 XSETFASTINT (temp, current_column ());
278 return temp;
281 /* Cancel any recorded value of the horizontal position. */
283 invalidate_current_column ()
285 last_known_column_point = 0;
289 current_column ()
291 register int col;
292 register unsigned char *ptr, *stop;
293 register int tab_seen;
294 int post_tab;
295 register int c;
296 register int tab_width = XINT (current_buffer->tab_width);
297 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
298 register struct Lisp_Char_Table *dp = buffer_display_table ();
299 int stopchar;
301 if (PT == last_known_column_point
302 && MODIFF == last_known_column_modified)
303 return last_known_column;
305 /* If the buffer has overlays, text properties, or multibyte,
306 use a more general algorithm. */
307 if (BUF_INTERVALS (current_buffer)
308 || !NILP (current_buffer->overlays_before)
309 || !NILP (current_buffer->overlays_after)
310 || !NILP (current_buffer->enable_multibyte_characters))
311 return current_column_1 (PT);
313 /* Scan backwards from point to the previous newline,
314 counting width. Tab characters are the only complicated case. */
316 /* Make a pointer for decrementing through the chars before point. */
317 ptr = POS_ADDR (PT - 1) + 1;
318 /* Make a pointer to where consecutive chars leave off,
319 going backwards from point. */
320 if (PT == BEGV)
321 stop = ptr;
322 else if (PT <= GPT || BEGV > GPT)
323 stop = BEGV_ADDR;
324 else
325 stop = GAP_END_ADDR;
327 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
329 col = 0, tab_seen = 0, post_tab = 0;
331 while (1)
333 if (ptr == stop)
335 /* We stopped either for the beginning of the buffer
336 or for the gap. */
337 if (ptr == BEGV_ADDR)
338 break;
339 /* It was the gap. Jump back over it. */
340 stop = BEGV_ADDR;
341 ptr = GPT_ADDR;
342 /* Check whether that brings us to beginning of buffer. */
343 if (BEGV >= GPT) break;
346 c = *--ptr;
347 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
348 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
349 else if (c >= 040 && c < 0177)
350 col++;
351 else if (c == '\n')
352 break;
353 else if (c == '\r' && EQ (current_buffer->selective_display, Qt))
354 break;
355 else if (c == '\t')
357 if (tab_seen)
358 col = ((col + tab_width) / tab_width) * tab_width;
360 post_tab += col;
361 col = 0;
362 tab_seen = 1;
364 else
365 col += (ctl_arrow && c < 0200) ? 2 : 4;
368 if (tab_seen)
370 col = ((col + tab_width) / tab_width) * tab_width;
371 col += post_tab;
374 if (ptr == BEGV_ADDR)
375 current_column_bol_cache = BEGV;
376 else
377 current_column_bol_cache = PTR_CHAR_POS ((ptr+1));
378 last_known_column = col;
379 last_known_column_point = PT;
380 last_known_column_modified = MODIFF;
382 return col;
385 /* Return the column number of position POS
386 by scanning forward from the beginning of the line.
387 This function handles characters that are invisible
388 due to text properties or overlays. */
390 static int
391 current_column_1 (pos)
392 int pos;
394 register int tab_width = XINT (current_buffer->tab_width);
395 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
396 register struct Lisp_Char_Table *dp = buffer_display_table ();
398 /* Start the scan at the beginning of this line with column number 0. */
399 register int col = 0;
400 int scan = current_column_bol_cache = find_next_newline (pos, -1);
401 int next_boundary = scan;
402 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
404 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
406 /* Scan forward to the target position. */
407 while (scan < pos)
409 int c;
411 /* Occasionally we may need to skip invisible text. */
412 while (scan == next_boundary)
414 /* This updates NEXT_BOUNDARY to the next place
415 where we might need to skip more invisible text. */
416 scan = skip_invisible (scan, &next_boundary, pos, Qnil);
417 if (scan >= pos)
418 goto endloop;
421 c = FETCH_BYTE (scan);
422 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
424 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
425 scan++;
426 continue;
428 if (c == '\n')
429 break;
430 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
431 break;
432 scan++;
433 if (c == '\t')
435 int prev_col = col;
436 col += tab_width;
437 col = col / tab_width * tab_width;
439 else if (multibyte && BASE_LEADING_CODE_P (c))
441 scan--;
442 /* Start of multi-byte form. */
443 if (c == LEADING_CODE_COMPOSITION)
445 unsigned char *ptr = POS_ADDR (scan);
447 int cmpchar_id = str_cmpchar_id (ptr, next_boundary - scan);
448 if (cmpchar_id >= 0)
450 scan += cmpchar_table[cmpchar_id]->len,
451 col += cmpchar_table[cmpchar_id]->width;
453 else
454 { /* invalid composite character */
455 scan++;
456 col += 4;
459 else
461 /* Here, we check that the following bytes are valid
462 constituents of multi-byte form. */
463 int len = BYTES_BY_CHAR_HEAD (c), i;
465 for (i = 1, scan++; i < len; i++, scan++)
466 /* We don't need range checking for PTR because there
467 are anchors (`\0') at GAP and Z. */
468 if (CHAR_HEAD_P (POS_ADDR (scan))) break;
469 if (i < len)
470 col += 4, scan -= i - 1;
471 else
472 col += WIDTH_BY_CHAR_HEAD (c);
475 else if (ctl_arrow && (c < 040 || c == 0177))
476 col += 2;
477 else if (c < 040 || c >= 0177)
478 col += 4;
479 else
480 col++;
482 endloop:
484 last_known_column = col;
485 last_known_column_point = PT;
486 last_known_column_modified = MODIFF;
488 return col;
491 /* Return the width in columns of the part of STRING from BEG to END.
492 If BEG is nil, that stands for the beginning of STRING.
493 If END is nil, that stands for the end of STRING. */
495 static int
496 string_display_width (string, beg, end)
497 Lisp_Object string, beg, end;
499 register int col;
500 register unsigned char *ptr, *stop;
501 register int tab_seen;
502 int post_tab;
503 register int c;
504 register int tab_width = XINT (current_buffer->tab_width);
505 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
506 register struct Lisp_Char_Table *dp = buffer_display_table ();
507 int b, e;
509 if (NILP (end))
510 e = XSTRING (string)->size;
511 else
513 CHECK_NUMBER (end, 0);
514 e = XINT (end);
517 if (NILP (beg))
518 b = 0;
519 else
521 CHECK_NUMBER (beg, 0);
522 b = XINT (beg);
525 /* Make a pointer for decrementing through the chars before point. */
526 ptr = XSTRING (string)->data + e;
527 /* Make a pointer to where consecutive chars leave off,
528 going backwards from point. */
529 stop = XSTRING (string)->data + b;
531 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
533 col = 0, tab_seen = 0, post_tab = 0;
535 while (1)
537 if (ptr == stop)
538 break;
540 c = *--ptr;
541 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
542 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
543 else if (c >= 040 && c < 0177)
544 col++;
545 else if (c == '\n')
546 break;
547 else if (c == '\t')
549 if (tab_seen)
550 col = ((col + tab_width) / tab_width) * tab_width;
552 post_tab += col;
553 col = 0;
554 tab_seen = 1;
556 else
557 col += (ctl_arrow && c < 0200) ? 2 : 4;
560 if (tab_seen)
562 col = ((col + tab_width) / tab_width) * tab_width;
563 col += post_tab;
566 return col;
569 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
570 "Indent from point with tabs and spaces until COLUMN is reached.\n\
571 Optional second argument MININUM says always do at least MININUM spaces\n\
572 even if that goes past COLUMN; by default, MININUM is zero.")
573 (column, minimum)
574 Lisp_Object column, minimum;
576 int mincol;
577 register int fromcol;
578 register int tab_width = XINT (current_buffer->tab_width);
580 CHECK_NUMBER (column, 0);
581 if (NILP (minimum))
582 XSETFASTINT (minimum, 0);
583 CHECK_NUMBER (minimum, 1);
585 fromcol = current_column ();
586 mincol = fromcol + XINT (minimum);
587 if (mincol < XINT (column)) mincol = XINT (column);
589 if (fromcol == mincol)
590 return make_number (mincol);
592 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
594 if (indent_tabs_mode)
596 Lisp_Object n;
597 XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
598 if (XFASTINT (n) != 0)
600 Finsert_char (make_number ('\t'), n, Qt);
602 fromcol = (mincol / tab_width) * tab_width;
606 XSETFASTINT (column, mincol - fromcol);
607 Finsert_char (make_number (' '), column, Qt);
609 last_known_column = mincol;
610 last_known_column_point = PT;
611 last_known_column_modified = MODIFF;
613 XSETINT (column, mincol);
614 return column;
618 DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
619 0, 0, 0,
620 "Return the indentation of the current line.\n\
621 This is the horizontal position of the character\n\
622 following any initial whitespace.")
625 Lisp_Object val;
627 XSETFASTINT (val, position_indentation (find_next_newline (PT, -1)));
628 return val;
631 position_indentation (pos)
632 register int pos;
634 register int column = 0;
635 register int tab_width = XINT (current_buffer->tab_width);
636 register unsigned char *p;
637 register unsigned char *stop;
638 unsigned char *start;
639 int next_boundary = pos;
640 int ceiling = pos;
642 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
644 p = POS_ADDR (pos);
645 /* STOP records the value of P at which we will need
646 to think about the gap, or about invisible text,
647 or about the end of the buffer. */
648 stop = p;
649 /* START records the starting value of P. */
650 start = p;
651 while (1)
653 while (p == stop)
655 int stop_pos;
657 /* If we have updated P, set POS to match.
658 The first time we enter the loop, POS is already right. */
659 if (p != start)
660 pos = PTR_CHAR_POS (p);
661 /* Consider the various reasons STOP might have been set here. */
662 if (pos == ZV)
663 return column;
664 if (pos == next_boundary)
665 pos = skip_invisible (pos, &next_boundary, ZV, Qnil);
666 if (pos >= ceiling)
667 ceiling = BUFFER_CEILING_OF (pos) + 1;
668 /* Compute the next place we need to stop and think,
669 and set STOP accordingly. */
670 stop_pos = min (ceiling, next_boundary);
671 /* The -1 and +1 arrange to point at the first byte of gap
672 (if STOP_POS is the position of the gap)
673 rather than at the data after the gap. */
675 stop = POS_ADDR (stop_pos - 1) + 1;
676 p = POS_ADDR (pos);
678 switch (*p++)
680 case ' ':
681 column++;
682 break;
683 case '\t':
684 column += tab_width - column % tab_width;
685 break;
686 default:
687 return column;
692 /* Test whether the line beginning at POS is indented beyond COLUMN.
693 Blank lines are treated as if they had the same indentation as the
694 preceding line. */
696 indented_beyond_p (pos, column)
697 int pos, column;
699 while (pos > BEGV && FETCH_BYTE (pos) == '\n')
700 pos = find_next_newline_no_quit (pos - 1, -1);
701 return (position_indentation (pos) >= column);
704 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
705 "Move point to column COLUMN in the current line.\n\
706 The column of a character is calculated by adding together the widths\n\
707 as displayed of the previous characters in the line.\n\
708 This function ignores line-continuation;\n\
709 there is no upper limit on the column number a character can have\n\
710 and horizontal scrolling has no effect.\n\
712 If specified column is within a character, point goes after that character.\n\
713 If it's past end of line, point goes to end of line.\n\n\
714 A non-nil second (optional) argument FORCE means, if the line\n\
715 is too short to reach column COLUMN then add spaces/tabs to get there,\n\
716 and if COLUMN is in the middle of a tab character, change it to spaces.\n\
718 The return value is the current column.")
719 (column, force)
720 Lisp_Object column, force;
722 register int pos;
723 register int col = current_column ();
724 register int goal;
725 register int end;
726 register int tab_width = XINT (current_buffer->tab_width);
727 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
728 register struct Lisp_Char_Table *dp = buffer_display_table ();
729 register int multibyte = !NILP (current_buffer->enable_multibyte_characters);
731 Lisp_Object val;
732 int prev_col;
733 int c;
735 int next_boundary;
737 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
738 CHECK_NATNUM (column, 0);
739 goal = XINT (column);
741 pos = PT;
742 end = ZV;
743 next_boundary = pos;
745 /* If we're starting past the desired column,
746 back up to beginning of line and scan from there. */
747 if (col > goal)
749 end = pos;
750 pos = current_column_bol_cache;
751 col = 0;
754 while (pos < end)
756 while (pos == next_boundary)
758 pos = skip_invisible (pos, &next_boundary, end, Qnil);
759 if (pos >= end)
760 goto endloop;
763 /* Test reaching the goal column. We do this after skipping
764 invisible characters, so that we put point before the
765 character on which the cursor will appear. */
766 if (col >= goal)
767 break;
769 c = FETCH_BYTE (pos);
770 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
772 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
773 pos++;
774 continue;
776 if (c == '\n')
777 break;
778 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
779 break;
780 pos++;
781 if (c == '\t')
783 prev_col = col;
784 col += tab_width;
785 col = col / tab_width * tab_width;
787 else if (ctl_arrow && (c < 040 || c == 0177))
788 col += 2;
789 else if (c < 040 || c == 0177)
790 col += 4;
791 else if (c < 0177)
792 col++;
793 else if (multibyte && BASE_LEADING_CODE_P (c))
795 /* Start of multi-byte form. */
796 unsigned char *ptr;
798 pos--; /* rewind to the character head */
799 ptr = POS_ADDR (pos);
800 if (c == LEADING_CODE_COMPOSITION)
802 int cmpchar_id = str_cmpchar_id (ptr, end - pos);
804 if (cmpchar_id >= 0)
806 col += cmpchar_table[cmpchar_id]->width;
807 pos += cmpchar_table[cmpchar_id]->len;
809 else
810 { /* invalid composite character */
811 col += 4;
812 pos++;
815 else
817 /* Here, we check that the following bytes are valid
818 constituents of multi-byte form. */
819 int len = BYTES_BY_CHAR_HEAD (c), i;
821 for (i = 1, ptr++; i < len; i++, ptr++)
822 /* We don't need range checking for PTR because there
823 are anchors (`\0') both at GPT and Z. */
824 if (CHAR_HEAD_P (ptr)) break;
825 if (i < len)
826 col += 4, pos++;
827 else
828 col += WIDTH_BY_CHAR_HEAD (c), pos += i;
831 else
832 col += 4;
834 endloop:
836 SET_PT (pos);
838 /* If a tab char made us overshoot, change it to spaces
839 and scan through it again. */
840 if (!NILP (force) && col > goal && c == '\t' && prev_col < goal)
842 int old_point;
844 del_range (PT - 1, PT);
845 Findent_to (make_number (goal), Qnil);
846 old_point = PT;
847 Findent_to (make_number (col), Qnil);
848 SET_PT (old_point);
849 /* Set the last_known... vars consistently. */
850 col = goal;
853 /* If line ends prematurely, add space to the end. */
854 if (col < goal && !NILP (force))
855 Findent_to (make_number (col = goal), Qnil);
857 last_known_column = col;
858 last_known_column_point = PT;
859 last_known_column_modified = MODIFF;
861 XSETFASTINT (val, col);
862 return val;
865 /* compute_motion: compute buffer posn given screen posn and vice versa */
867 struct position val_compute_motion;
869 /* Scan the current buffer forward from offset FROM, pretending that
870 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
871 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
872 and return the ending buffer position and screen location. If we
873 can't hit the requested column exactly (because of a tab or other
874 multi-column character), overshoot.
876 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
877 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
878 earlier call to compute_motion. The other common case is that FROMHPOS
879 is zero and FROM is a position that "belongs" at column zero, but might
880 be shifted by overlay strings; in this case DID_MOTION should be 0.
882 WIDTH is the number of columns available to display text;
883 compute_motion uses this to handle continuation lines and such.
884 HSCROLL is the number of columns not being displayed at the left
885 margin; this is usually taken from a window's hscroll member.
886 TAB_OFFSET is the number of columns of the first tab that aren't
887 being displayed, perhaps because of a continuation line or
888 something.
890 compute_motion returns a pointer to a struct position. The bufpos
891 member gives the buffer position at the end of the scan, and hpos
892 and vpos give its cartesian location. prevhpos is the column at
893 which the character before bufpos started, and contin is non-zero
894 if we reached the current line by continuing the previous.
896 Note that FROMHPOS and TOHPOS should be expressed in real screen
897 columns, taking HSCROLL and the truncation glyph at the left margin
898 into account. That is, beginning-of-line moves you to the hpos
899 -HSCROLL + (HSCROLL > 0).
901 For example, to find the buffer position of column COL of line LINE
902 of a certain window, pass the window's starting location as FROM
903 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
904 Pass the buffer's ZV as TO, to limit the scan to the end of the
905 visible section of the buffer, and pass LINE and COL as TOVPOS and
906 TOHPOS.
908 When displaying in window w, a typical formula for WIDTH is:
910 window_width - 1
911 - (has_vertical_scroll_bars
912 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
913 : (window_width + window_left != frame_width))
915 where
916 window_width is XFASTINT (w->width),
917 window_left is XFASTINT (w->left),
918 has_vertical_scroll_bars is
919 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
920 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
922 Or you can let window_internal_width do this all for you, and write:
923 window_internal_width (w) - 1
925 The `-1' accounts for the continuation-line backslashes; the rest
926 accounts for window borders if the window is split horizontally, and
927 the scroll bars if they are turned on. */
929 struct position *
930 compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win)
931 int from, fromvpos, fromhpos, to, tovpos, tohpos;
932 int did_motion;
933 register int width;
934 int hscroll, tab_offset;
935 struct window *win;
937 register int hpos = fromhpos;
938 register int vpos = fromvpos;
940 register int pos;
941 register int c;
942 register int tab_width = XFASTINT (current_buffer->tab_width);
943 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
944 register struct Lisp_Char_Table *dp = window_display_table (win);
945 int selective
946 = (INTEGERP (current_buffer->selective_display)
947 ? XINT (current_buffer->selective_display)
948 : !NILP (current_buffer->selective_display) ? -1 : 0);
949 int prev_hpos = 0;
950 int selective_rlen
951 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
952 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0);
953 /* The next location where the `invisible' property changes, or an
954 overlay starts or ends. */
955 int next_boundary = from;
957 /* For computing runs of characters with similar widths.
958 Invariant: width_run_width is zero, or all the characters
959 from width_run_start to width_run_end have a fixed width of
960 width_run_width. */
961 int width_run_start = from;
962 int width_run_end = from;
963 int width_run_width = 0;
964 Lisp_Object *width_table;
965 Lisp_Object buffer;
967 /* The next buffer pos where we should consult the width run cache. */
968 int next_width_run = from;
969 Lisp_Object window;
971 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
972 int wide_column = 0; /* Set to 1 when a previous character
973 is wide-colomn. */
974 int prev_pos; /* Previous buffer position. */
975 int contin_hpos; /* HPOS of last column of continued line. */
976 int prev_tab_offset; /* Previous tab offset. */
978 XSETBUFFER (buffer, current_buffer);
979 XSETWINDOW (window, win);
981 width_run_cache_on_off ();
982 if (dp == buffer_display_table ())
983 width_table = (VECTORP (current_buffer->width_table)
984 ? XVECTOR (current_buffer->width_table)->contents
985 : 0);
986 else
987 /* If the window has its own display table, we can't use the width
988 run cache, because that's based on the buffer's display table. */
989 width_table = 0;
991 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
993 pos = prev_pos = from;
994 contin_hpos = 0;
995 prev_tab_offset = tab_offset;
996 while (1)
998 while (pos == next_boundary)
1000 /* If the caller says that the screen position came from an earlier
1001 call to compute_motion, then we've already accounted for the
1002 overlay strings at point. This is only true the first time
1003 through, so clear the flag after testing it. */
1004 if (!did_motion)
1005 /* We need to skip past the overlay strings. Currently those
1006 strings must not contain TAB;
1007 if we want to relax that restriction, something will have
1008 to be changed here. */
1010 unsigned char *ovstr;
1011 int ovlen = overlay_strings (pos, win, &ovstr);
1012 hpos += (multibyte ? strwidth (ovstr, ovlen) : ovlen);
1014 did_motion = 0;
1016 if (pos >= to)
1017 break;
1019 /* Advance POS past invisible characters
1020 (but not necessarily all that there are here),
1021 and store in next_boundary the next position where
1022 we need to call skip_invisible. */
1023 pos = skip_invisible (pos, &next_boundary, to, window);
1026 /* Handle right margin. */
1027 /* Note on a wide-column character.
1029 Characters are classified into the following three categories
1030 according to the width (columns occupied on screen).
1032 (1) single-column character: ex. `a'
1033 (2) multi-column character: ex. `^A', TAB, `\033'
1034 (3) wide-column character: ex. Japanese character, Chinese character
1035 (In the following example, `W_' stands for them.)
1037 Multi-column characters can be divided around the right margin,
1038 but wide-column characters cannot.
1040 NOTE:
1042 (*) The cursor is placed on the next character after the point.
1044 ----------
1045 abcdefghi\
1046 j ^---- next after the point
1047 ^--- next char. after the point.
1048 ----------
1049 In case of sigle-column character
1051 ----------
1052 abcdefgh\\
1053 033 ^---- next after the point, next char. after the point.
1054 ----------
1055 In case of multi-column character
1057 ----------
1058 abcdefgh\\
1059 W_ ^---- next after the point
1060 ^---- next char. after the point.
1061 ----------
1062 In case of wide-column character
1064 The problem here is continuation at a wide-column character.
1065 In this case, the line may shorter less than WIDTH.
1066 And we find the continuation AFTER it occurs.
1070 if (hpos > width)
1072 if (hscroll
1073 || (truncate_partial_width_windows
1074 && width + 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win))))
1075 || !NILP (current_buffer->truncate_lines))
1077 /* Truncating: skip to newline. */
1078 if (pos <= to) /* This IF is needed because we may past TO */
1079 pos = find_before_next_newline (pos, to, 1);
1080 hpos = width;
1081 /* If we just skipped next_boundary,
1082 loop around in the main while
1083 and handle it. */
1084 if (pos >= next_boundary)
1085 next_boundary = pos + 1;
1086 prev_hpos = width;
1087 prev_tab_offset = tab_offset;
1089 else
1091 /* Continuing. */
1092 /* Remember the previous value. */
1093 prev_tab_offset = tab_offset;
1095 if (wide_column)
1097 hpos -= prev_hpos;
1098 tab_offset += prev_hpos;
1100 else
1102 tab_offset += width;
1103 hpos -= width;
1105 vpos++;
1106 contin_hpos = prev_hpos;
1107 prev_hpos = 0;
1111 /* Stop if past the target buffer position or screen position. */
1112 if (pos > to)
1114 /* Go back to the previous position. */
1115 pos = prev_pos;
1116 hpos = prev_hpos;
1117 tab_offset = prev_tab_offset;
1119 /* NOTE on contin_hpos, hpos, and prev_hpos.
1121 ----------
1122 abcdefgh\\
1123 W_ ^---- contin_hpos
1124 | ^----- hpos
1125 \---- prev_hpos
1126 ----------
1129 if (contin_hpos && prev_hpos == 0
1130 && contin_hpos < width && !wide_column)
1132 /* Line breaking occurs in the middle of multi-column
1133 character. Go back to previous line. */
1134 hpos = contin_hpos;
1135 vpos = vpos - 1;
1137 else if (c == '\n')
1138 /* If previous character is NEWLINE,
1139 set VPOS back to previous line */
1140 vpos = vpos - 1;
1141 break;
1144 if (vpos > tovpos || vpos == tovpos && hpos >= tohpos)
1146 if (contin_hpos && prev_hpos == 0
1147 && (contin_hpos == width || wide_column))
1148 { /* Line breaks because we can't put the character at the
1149 previous line any more. It is not the multi-column
1150 character continued in middle. Go back to previous
1151 buffer position, screen position, and set tab offset
1152 to previous value. It's the beginning of the
1153 line. */
1154 pos = prev_pos;
1155 hpos = prev_hpos;
1156 tab_offset = prev_tab_offset;
1158 break;
1160 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1161 break;
1163 prev_hpos = hpos;
1164 prev_pos = pos;
1165 wide_column = 0;
1167 /* Consult the width run cache to see if we can avoid inspecting
1168 the text character-by-character. */
1169 if (current_buffer->width_run_cache && pos >= next_width_run)
1171 int run_end;
1172 int common_width
1173 = region_cache_forward (current_buffer,
1174 current_buffer->width_run_cache,
1175 pos, &run_end);
1177 /* A width of zero means the character's width varies (like
1178 a tab), is meaningless (like a newline), or we just don't
1179 want to skip over it for some other reason. */
1180 if (common_width != 0)
1182 int run_end_hpos;
1184 /* Don't go past the final buffer posn the user
1185 requested. */
1186 if (run_end > to)
1187 run_end = to;
1189 run_end_hpos = hpos + (run_end - pos) * common_width;
1191 /* Don't go past the final horizontal position the user
1192 requested. */
1193 if (vpos == tovpos && run_end_hpos > tohpos)
1195 run_end = pos + (tohpos - hpos) / common_width;
1196 run_end_hpos = hpos + (run_end - pos) * common_width;
1199 /* Don't go past the margin. */
1200 if (run_end_hpos >= width)
1202 run_end = pos + (width - hpos) / common_width;
1203 run_end_hpos = hpos + (run_end - pos) * common_width;
1206 hpos = run_end_hpos;
1207 if (run_end > pos)
1208 prev_hpos = hpos - common_width;
1209 pos = run_end;
1212 next_width_run = run_end + 1;
1215 /* We have to scan the text character-by-character. */
1216 else
1218 c = FETCH_BYTE (pos);
1219 pos++;
1221 /* Perhaps add some info to the width_run_cache. */
1222 if (current_buffer->width_run_cache)
1224 /* Is this character part of the current run? If so, extend
1225 the run. */
1226 if (pos - 1 == width_run_end
1227 && width_table[c] == width_run_width)
1228 width_run_end = pos;
1230 /* The previous run is over, since this is a character at a
1231 different position, or a different width. */
1232 else
1234 /* Have we accumulated a run to put in the cache?
1235 (Currently, we only cache runs of width == 1). */
1236 if (width_run_start < width_run_end
1237 && width_run_width == 1)
1238 know_region_cache (current_buffer,
1239 current_buffer->width_run_cache,
1240 width_run_start, width_run_end);
1242 /* Start recording a new width run. */
1243 width_run_width = width_table[c];
1244 width_run_start = pos - 1;
1245 width_run_end = pos;
1249 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1250 hpos += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
1251 else if (c >= 040 && c < 0177)
1252 hpos++;
1253 else if (c == '\t')
1255 int tem = (hpos + tab_offset + hscroll - (hscroll > 0)) % tab_width;
1256 if (tem < 0)
1257 tem += tab_width;
1258 hpos += tab_width - tem;
1260 else if (c == '\n')
1262 if (selective > 0 && indented_beyond_p (pos, selective))
1264 /* If (pos == to), we don't have to take care of
1265 selective display. */
1266 if (pos < to)
1268 /* Skip any number of invisible lines all at once */
1270 pos = find_before_next_newline (pos, to, 1) + 1;
1271 while (pos < to
1272 && indented_beyond_p (pos, selective));
1273 /* Allow for the " ..." that is displayed for them. */
1274 if (selective_rlen)
1276 hpos += selective_rlen;
1277 if (hpos >= width)
1278 hpos = width;
1280 --pos;
1281 /* We have skipped the invis text, but not the
1282 newline after. */
1285 else
1287 /* A visible line. */
1288 vpos++;
1289 hpos = 0;
1290 hpos -= hscroll;
1291 /* Count the truncation glyph on column 0 */
1292 if (hscroll > 0)
1293 hpos++;
1294 tab_offset = 0;
1296 contin_hpos = 0;
1298 else if (c == CR && selective < 0)
1300 /* In selective display mode,
1301 everything from a ^M to the end of the line is invisible.
1302 Stop *before* the real newline. */
1303 if (pos < to)
1304 pos = find_before_next_newline (pos, to, 1);
1305 /* If we just skipped next_boundary,
1306 loop around in the main while
1307 and handle it. */
1308 if (pos > next_boundary)
1309 next_boundary = pos;
1310 /* Allow for the " ..." that is displayed for them. */
1311 if (selective_rlen)
1313 hpos += selective_rlen;
1314 if (hpos >= width)
1315 hpos = width;
1318 else if (multibyte && BASE_LEADING_CODE_P (c))
1320 /* Start of multi-byte form. */
1321 unsigned char *ptr;
1323 pos--; /* rewind POS */
1324 ptr = POS_ADDR (pos);
1326 if (c == LEADING_CODE_COMPOSITION)
1328 int cmpchar_id = str_cmpchar_id (ptr, next_boundary - pos);
1330 if (cmpchar_id >= 0)
1332 if (cmpchar_table[cmpchar_id]->width >= 2)
1333 wide_column = 1;
1334 hpos += cmpchar_table[cmpchar_id]->width;
1335 pos += cmpchar_table[cmpchar_id]->len;
1337 else
1338 { /* invalid composite character */
1339 hpos += 4;
1340 pos ++;
1343 else
1345 /* Here, we check that the following bytes are valid
1346 constituents of multi-byte form. */
1347 int len = BYTES_BY_CHAR_HEAD (c), i;
1349 for (i = 1, ptr++; i < len; i++, ptr++)
1350 /* We don't need range checking for PTR because
1351 there are anchors ('\0') both at GPT and Z. */
1352 if (CHAR_HEAD_P (ptr)) break;
1353 if (i < len)
1354 hpos += 4, pos++;
1355 else
1356 hpos += WIDTH_BY_CHAR_HEAD (c), pos += i, wide_column = 1;
1359 else
1360 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1364 /* Remember any final width run in the cache. */
1365 if (current_buffer->width_run_cache
1366 && width_run_width == 1
1367 && width_run_start < width_run_end)
1368 know_region_cache (current_buffer, current_buffer->width_run_cache,
1369 width_run_start, width_run_end);
1371 val_compute_motion.bufpos = pos;
1372 val_compute_motion.hpos = hpos;
1373 val_compute_motion.vpos = vpos;
1374 val_compute_motion.prevhpos = prev_hpos;
1375 /* We alalways handle all of them here; none of them remain to do. */
1376 val_compute_motion.ovstring_chars_done = 0;
1378 /* Nonzero if have just continued a line */
1379 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1381 return &val_compute_motion;
1384 #if 0 /* The doc string is too long for some compilers,
1385 but make-docfile can find it in this comment. */
1386 DEFUN ("compute-motion", Ffoo, Sfoo, 7, 7, 0,
1387 "Scan through the current buffer, calculating screen position.\n\
1388 Scan the current buffer forward from offset FROM,\n\
1389 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1390 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1391 and return the ending buffer position and screen location.\n\
1393 There are three additional arguments:\n\
1395 WIDTH is the number of columns available to display text;\n\
1396 this affects handling of continuation lines.\n\
1397 This is usually the value returned by `window-width', less one (to allow\n\
1398 for the continuation glyph).\n\
1400 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1401 HSCROLL is the number of columns not being displayed at the left\n\
1402 margin; this is usually taken from a window's hscroll member.\n\
1403 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1404 being displayed, perhaps because the line was continued within it.\n\
1405 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1407 WINDOW is the window to operate on. It is used to choose the display table;\n\
1408 if it is showing the current buffer, it is used also for\n\
1409 deciding which overlay properties apply.\n\
1410 Note that `compute-motion' always operates on the current buffer.\n\
1412 The value is a list of five elements:\n\
1413 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1414 POS is the buffer position where the scan stopped.\n\
1415 VPOS is the vertical position where the scan stopped.\n\
1416 HPOS is the horizontal position where the scan stopped.\n\
1418 PREVHPOS is the horizontal position one character back from POS.\n\
1419 CONTIN is t if a line was continued after (or within) the previous character.\n\
1421 For example, to find the buffer position of column COL of line LINE\n\
1422 of a certain window, pass the window's starting location as FROM\n\
1423 and the window's upper-left coordinates as FROMPOS.\n\
1424 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1425 visible section of the buffer, and pass LINE and COL as TOPOS.")
1426 (from, frompos, to, topos, width, offsets, window)
1427 #endif
1429 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1431 (from, frompos, to, topos, width, offsets, window)
1432 Lisp_Object from, frompos, to, topos;
1433 Lisp_Object width, offsets, window;
1435 Lisp_Object bufpos, hpos, vpos, prevhpos, contin;
1436 struct position *pos;
1437 int hscroll, tab_offset;
1439 CHECK_NUMBER_COERCE_MARKER (from, 0);
1440 CHECK_CONS (frompos, 0);
1441 CHECK_NUMBER (XCONS (frompos)->car, 0);
1442 CHECK_NUMBER (XCONS (frompos)->cdr, 0);
1443 CHECK_NUMBER_COERCE_MARKER (to, 0);
1444 CHECK_CONS (topos, 0);
1445 CHECK_NUMBER (XCONS (topos)->car, 0);
1446 CHECK_NUMBER (XCONS (topos)->cdr, 0);
1447 CHECK_NUMBER (width, 0);
1448 if (!NILP (offsets))
1450 CHECK_CONS (offsets, 0);
1451 CHECK_NUMBER (XCONS (offsets)->car, 0);
1452 CHECK_NUMBER (XCONS (offsets)->cdr, 0);
1453 hscroll = XINT (XCONS (offsets)->car);
1454 tab_offset = XINT (XCONS (offsets)->cdr);
1456 else
1457 hscroll = tab_offset = 0;
1459 if (NILP (window))
1460 window = Fselected_window ();
1461 else
1462 CHECK_LIVE_WINDOW (window, 0);
1464 pos = compute_motion (XINT (from), XINT (XCONS (frompos)->cdr),
1465 XINT (XCONS (frompos)->car), 0,
1466 XINT (to), XINT (XCONS (topos)->cdr),
1467 XINT (XCONS (topos)->car),
1468 XINT (width), hscroll, tab_offset,
1469 XWINDOW (window));
1471 XSETFASTINT (bufpos, pos->bufpos);
1472 XSETINT (hpos, pos->hpos);
1473 XSETINT (vpos, pos->vpos);
1474 XSETINT (prevhpos, pos->prevhpos);
1476 return Fcons (bufpos,
1477 Fcons (hpos,
1478 Fcons (vpos,
1479 Fcons (prevhpos,
1480 Fcons (pos->contin ? Qt : Qnil, Qnil)))));
1484 /* Return the column of position POS in window W's buffer.
1485 The result is rounded down to a multiple of the internal width of W.
1486 This is the amount of indentation of position POS
1487 that is not visible in its horizontal position in the window. */
1490 pos_tab_offset (w, pos)
1491 struct window *w;
1492 register int pos;
1494 int opoint = PT;
1495 int col;
1496 int width = window_internal_width (w) - 1;
1498 if (pos == BEGV)
1499 return MINI_WINDOW_P (w) ? -minibuf_prompt_width : 0;
1500 if (FETCH_BYTE (pos - 1) == '\n')
1501 return 0;
1502 TEMP_SET_PT (pos);
1503 col = current_column ();
1504 TEMP_SET_PT (opoint);
1505 /* Modulo is no longer valid, as a line may get shorter than WIDTH
1506 columns by continuation of a wide-column character. Just return
1507 COL here. */
1508 #if 0
1509 /* In the continuation of the first line in a minibuffer we must
1510 take the width of the prompt into account. */
1511 if (MINI_WINDOW_P (w) && col >= width - minibuf_prompt_width
1512 && find_next_newline_no_quit (pos, -1) == BEGV)
1513 return col - (col + minibuf_prompt_width) % width;
1514 return col - (col % width);
1515 #endif
1516 return col;
1520 /* Fvertical_motion and vmotion */
1521 struct position val_vmotion;
1523 struct position *
1524 vmotion (from, vtarget, w)
1525 register int from, vtarget;
1526 struct window *w;
1528 int width = window_internal_width (w) - 1;
1529 int hscroll = XINT (w->hscroll);
1530 struct position pos;
1531 /* vpos is cumulative vertical position, changed as from is changed */
1532 register int vpos = 0;
1533 Lisp_Object prevline;
1534 register int first;
1535 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
1536 int selective
1537 = (INTEGERP (current_buffer->selective_display)
1538 ? XINT (current_buffer->selective_display)
1539 : !NILP (current_buffer->selective_display) ? -1 : 0);
1540 Lisp_Object window;
1541 int start_hpos = 0;
1542 int did_motion;
1544 XSETWINDOW (window, w);
1546 /* The omission of the clause
1547 && marker_position (w->start) == BEG
1548 here is deliberate; I think we want to measure from the prompt
1549 position even if the minibuffer window has scrolled. */
1550 if (EQ (window, minibuf_window))
1552 if (minibuf_prompt_width == 0 && STRINGP (minibuf_prompt))
1553 minibuf_prompt_width
1554 = string_display_width (minibuf_prompt, Qnil, Qnil);
1556 start_hpos = minibuf_prompt_width;
1559 if (vpos >= vtarget)
1561 /* To move upward, go a line at a time until
1562 we have gone at least far enough */
1564 first = 1;
1566 while ((vpos > vtarget || first) && from > BEGV)
1568 Lisp_Object propval;
1570 XSETFASTINT (prevline, find_next_newline_no_quit (from - 1, -1));
1571 while (XFASTINT (prevline) > BEGV
1572 && ((selective > 0
1573 && indented_beyond_p (XFASTINT (prevline), selective))
1574 #ifdef USE_TEXT_PROPERTIES
1575 /* watch out for newlines with `invisible' property */
1576 || (propval = Fget_char_property (prevline,
1577 Qinvisible,
1578 window),
1579 TEXT_PROP_MEANS_INVISIBLE (propval))
1580 #endif
1582 XSETFASTINT (prevline,
1583 find_next_newline_no_quit (XFASTINT (prevline) - 1,
1584 -1));
1585 pos = *compute_motion (XFASTINT (prevline), 0,
1586 lmargin + (XFASTINT (prevline) == BEG
1587 ? start_hpos : 0),
1589 from,
1590 /* Don't care for VPOS... */
1591 1 << (BITS_PER_SHORT - 1),
1592 /* ... nor HPOS. */
1593 1 << (BITS_PER_SHORT - 1),
1594 width, hscroll,
1595 /* This compensates for start_hpos
1596 so that a tab as first character
1597 still occupies 8 columns. */
1598 (XFASTINT (prevline) == BEG
1599 ? -start_hpos : 0),
1601 vpos -= pos.vpos;
1602 first = 0;
1603 from = XFASTINT (prevline);
1606 /* If we made exactly the desired vertical distance,
1607 or if we hit beginning of buffer,
1608 return point found */
1609 if (vpos >= vtarget)
1611 val_vmotion.bufpos = from;
1612 val_vmotion.vpos = vpos;
1613 val_vmotion.hpos = lmargin;
1614 val_vmotion.contin = 0;
1615 val_vmotion.prevhpos = 0;
1616 val_vmotion.ovstring_chars_done = 0;
1617 val_vmotion.tab_offset = 0; /* For accumulating tab offset. */
1618 return &val_vmotion;
1621 /* Otherwise find the correct spot by moving down */
1623 /* Moving downward is simple, but must calculate from beg of line
1624 to determine hpos of starting point */
1625 if (from > BEGV && FETCH_BYTE (from - 1) != '\n')
1627 Lisp_Object propval;
1629 XSETFASTINT (prevline, find_next_newline_no_quit (from, -1));
1630 while (XFASTINT (prevline) > BEGV
1631 && ((selective > 0
1632 && indented_beyond_p (XFASTINT (prevline), selective))
1633 #ifdef USE_TEXT_PROPERTIES
1634 /* watch out for newlines with `invisible' property */
1635 || (propval = Fget_char_property (prevline, Qinvisible,
1636 window),
1637 TEXT_PROP_MEANS_INVISIBLE (propval))
1638 #endif
1640 XSETFASTINT (prevline,
1641 find_next_newline_no_quit (XFASTINT (prevline) - 1,
1642 -1));
1643 pos = *compute_motion (XFASTINT (prevline), 0,
1644 lmargin + (XFASTINT (prevline) == BEG
1645 ? start_hpos : 0),
1647 from,
1648 /* Don't care for VPOS... */
1649 1 << (BITS_PER_SHORT - 1),
1650 /* ... nor HPOS. */
1651 1 << (BITS_PER_SHORT - 1),
1652 width, hscroll,
1653 (XFASTINT (prevline) == BEG ? -start_hpos : 0),
1655 did_motion = 1;
1657 else
1659 pos.hpos = lmargin + (from == BEG ? start_hpos : 0);
1660 pos.vpos = 0;
1661 pos.tab_offset = 0;
1662 did_motion = 0;
1664 return compute_motion (from, vpos, pos.hpos, did_motion,
1665 ZV, vtarget, - (1 << (BITS_PER_SHORT - 1)),
1666 width, hscroll,
1667 pos.tab_offset - (from == BEG ? start_hpos : 0),
1671 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
1672 "Move point to start of the screen line LINES lines down.\n\
1673 If LINES is negative, this means moving up.\n\
1675 This function is an ordinary cursor motion function\n\
1676 which calculates the new position based on how text would be displayed.\n\
1677 The new position may be the start of a line,\n\
1678 or just the start of a continuation line.\n\
1679 The function returns number of screen lines moved over;\n\
1680 that usually equals LINES, but may be closer to zero\n\
1681 if beginning or end of buffer was reached.\n\
1683 The optional second argument WINDOW specifies the window to use for\n\
1684 parameters such as width, horizontal scrolling, and so on.\n\
1685 The default is to use the selected window's parameters.\n\
1687 `vertical-motion' always uses the current buffer,\n\
1688 regardless of which buffer is displayed in WINDOW.\n\
1689 This is consistent with other cursor motion functions\n\
1690 and makes it possible to use `vertical-motion' in any buffer,\n\
1691 whether or not it is currently displayed in some window.")
1692 (lines, window)
1693 Lisp_Object lines, window;
1695 struct position pos;
1697 CHECK_NUMBER (lines, 0);
1698 if (! NILP (window))
1699 CHECK_WINDOW (window, 0);
1700 else
1701 window = selected_window;
1703 pos = *vmotion (PT, (int) XINT (lines), XWINDOW (window));
1705 SET_PT (pos.bufpos);
1706 return make_number (pos.vpos);
1709 /* file's initialization. */
1711 syms_of_indent ()
1713 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
1714 "*Indentation can insert tabs if this is non-nil.\n\
1715 Setting this variable automatically makes it local to the current buffer.");
1716 indent_tabs_mode = 1;
1718 defsubr (&Scurrent_indentation);
1719 defsubr (&Sindent_to);
1720 defsubr (&Scurrent_column);
1721 defsubr (&Smove_to_column);
1722 defsubr (&Svertical_motion);
1723 defsubr (&Scompute_motion);