New version.
[emacs.git] / src / indent.c
blob60641ae54dc57b0291f7685ba30bd1d70cff9020
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 "indent.h"
26 #include "frame.h"
27 #include "window.h"
28 #include "termchar.h"
29 #include "termopts.h"
30 #include "disptab.h"
31 #include "intervals.h"
32 #include "region-cache.h"
34 /* Indentation can insert tabs if this is non-zero;
35 otherwise always uses spaces */
36 int indent_tabs_mode;
38 #define min(a, b) ((a) < (b) ? (a) : (b))
39 #define max(a, b) ((a) > (b) ? (a) : (b))
41 #define CR 015
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 */
47 int last_known_column;
48 /* Value of point when current_column was called */
49 int last_known_column_point;
50 /* Value of MODIFF when current_column was called */
51 int last_known_column_modified;
53 static int current_column_1 ();
55 /* Get the display table to use for the current buffer. */
57 struct Lisp_Char_Table *
58 buffer_display_table ()
60 Lisp_Object thisbuf;
62 thisbuf = current_buffer->display_table;
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);
67 return 0;
70 /* Width run cache considerations. */
72 /* Return the width of character C under display table DP. */
74 static int
75 character_width (c, dp)
76 int c;
77 struct Lisp_Char_Table *dp;
79 Lisp_Object elt;
81 /* These width computations were determined by examining the cases
82 in display_text_line. */
84 /* Everything can be handled by the display table, if it's
85 present and the element is right. */
86 if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
87 return XVECTOR (elt)->size;
89 /* Some characters are special. */
90 if (c == '\n' || c == '\t' || c == '\015')
91 return 0;
93 /* Printing characters have width 1. */
94 else if (c >= 040 && c < 0177)
95 return 1;
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;
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. */
110 disptab_matches_widthtab (disptab, widthtab)
111 struct Lisp_Char_Table *disptab;
112 struct Lisp_Vector *widthtab;
114 int i;
116 if (widthtab->size != 256)
117 abort ();
119 for (i = 0; i < 256; i++)
120 if (character_width (i, disptab)
121 != XFASTINT (widthtab->contents[i]))
122 return 0;
124 return 1;
127 /* Recompute BUF's width table, using the display table DISPTAB. */
128 void
129 recompute_width_table (buf, disptab)
130 struct buffer *buf;
131 struct Lisp_Char_Table *disptab;
133 int i;
134 struct Lisp_Vector *widthtab;
136 if (!VECTORP (buf->width_table))
137 buf->width_table = Fmake_vector (make_number (256), make_number (0));
138 widthtab = XVECTOR (buf->width_table);
139 if (widthtab->size != 256)
140 abort ();
142 for (i = 0; i < 256; i++)
143 XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
146 /* Allocate or free the width run cache, as requested by the current
147 state of current_buffer's cache_long_line_scans variable. */
148 static void
149 width_run_cache_on_off ()
151 if (NILP (current_buffer->cache_long_line_scans))
153 /* It should be off. */
154 if (current_buffer->width_run_cache)
156 free_region_cache (current_buffer->width_run_cache);
157 current_buffer->width_run_cache = 0;
158 current_buffer->width_table = Qnil;
161 else
163 /* It should be on. */
164 if (current_buffer->width_run_cache == 0)
166 current_buffer->width_run_cache = new_region_cache ();
167 recompute_width_table (current_buffer, buffer_display_table ());
173 /* Skip some invisible characters starting from POS.
174 This includes characters invisible because of text properties
175 and characters invisible because of overlays.
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.
181 Set *NEXT_BOUNDARY_P to the next position at which
182 it will be necessary to call this function again.
184 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
185 to a value greater than TO.
187 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
188 take account of overlays that apply only in WINDOW.
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. */
196 static int
197 skip_invisible (pos, next_boundary_p, to, window)
198 int pos;
199 int *next_boundary_p;
200 int to;
201 Lisp_Object window;
203 Lisp_Object prop, position, end, overlay_limit, proplimit;
204 Lisp_Object buffer;
206 XSETFASTINT (position, pos);
207 XSETBUFFER (buffer, current_buffer);
209 /* Give faster response for overlay lookup near POS. */
210 recenter_overlay_lists (current_buffer, pos);
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
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);
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;
249 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
250 "Return the horizontal position of point. Beginning of line is column 0.\n\
251 This is calculated by adding together the widths of all the displayed\n\
252 representations of the character between the start of the previous line\n\
253 and point. (eg control characters will have a width of 2 or 4, tabs\n\
254 will have a variable width)\n\
255 Ignores finite width of frame, which means that this function may return\n\
256 values greater than (frame-width).\n\
257 Whether the line is visible (if `selective-display' is t) has no effect;\n\
258 however, ^M is treated as end of line when `selective-display' is t.")
261 Lisp_Object temp;
262 XSETFASTINT (temp, current_column ());
263 return temp;
266 /* Cancel any recorded value of the horizontal position. */
268 invalidate_current_column ()
270 last_known_column_point = 0;
274 current_column ()
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);
282 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
283 register struct Lisp_Char_Table *dp = buffer_display_table ();
284 int stopchar;
286 if (point == last_known_column_point
287 && MODIFF == last_known_column_modified)
288 return last_known_column;
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))
295 return current_column_1 (point);
297 /* Scan backwards from point to the previous newline,
298 counting width. Tab characters are the only complicated case. */
300 /* Make a pointer for decrementing through the chars before point. */
301 ptr = &FETCH_CHAR (point - 1) + 1;
302 /* Make a pointer to where consecutive chars leave off,
303 going backwards from point. */
304 if (point == BEGV)
305 stop = ptr;
306 else if (point <= GPT || BEGV > GPT)
307 stop = BEGV_ADDR;
308 else
309 stop = GAP_END_ADDR;
311 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
313 col = 0, tab_seen = 0, post_tab = 0;
315 while (1)
317 if (ptr == stop)
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;
330 c = *--ptr;
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++;
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')
341 if (tab_seen)
342 col = ((col + tab_width) / tab_width) * tab_width;
344 post_tab += col;
345 col = 0;
346 tab_seen = 1;
348 else
349 col += (ctl_arrow && c < 0200) ? 2 : 4;
352 if (tab_seen)
354 col = ((col + tab_width) / tab_width) * tab_width;
355 col += post_tab;
358 last_known_column = col;
359 last_known_column_point = point;
360 last_known_column_modified = MODIFF;
362 return col;
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. */
370 static int
371 current_column_1 (pos)
372 int pos;
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 ();
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;
383 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
385 /* Scan forward to the target position. */
386 while (scan < pos)
388 int c;
390 /* Occasionally we may need to skip invisible text. */
391 while (scan == next_boundary)
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;
400 c = FETCH_CHAR (scan);
401 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
403 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
404 scan++;
405 continue;
407 if (c == '\n')
408 break;
409 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
410 break;
411 scan++;
412 if (c == '\t')
414 int prev_col = col;
415 col += tab_width;
416 col = col / tab_width * tab_width;
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++;
425 endloop:
427 last_known_column = col;
428 last_known_column_point = point;
429 last_known_column_modified = MODIFF;
431 return col;
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. */
438 static int
439 string_display_width (string, beg, end)
440 Lisp_Object string, beg, end;
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);
449 register struct Lisp_Char_Table *dp = buffer_display_table ();
450 int b, e;
452 if (NILP (end))
453 e = XSTRING (string)->size;
454 else
456 CHECK_NUMBER (end, 0);
457 e = XINT (end);
460 if (NILP (beg))
461 b = 0;
462 else
464 CHECK_NUMBER (beg, 0);
465 b = XINT (beg);
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;
474 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
476 col = 0, tab_seen = 0, post_tab = 0;
478 while (1)
480 if (ptr == stop)
481 break;
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')
492 if (tab_seen)
493 col = ((col + tab_width) / tab_width) * tab_width;
495 post_tab += col;
496 col = 0;
497 tab_seen = 1;
499 else
500 col += (ctl_arrow && c < 0200) ? 2 : 4;
503 if (tab_seen)
505 col = ((col + tab_width) / tab_width) * tab_width;
506 col += post_tab;
509 return col;
512 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
513 "Indent from point with tabs and spaces until COLUMN is reached.\n\
514 Optional second argument MININUM says always do at least MININUM spaces\n\
515 even if that goes past COLUMN; by default, MININUM is zero.")
516 (column, minimum)
517 Lisp_Object column, minimum;
519 int mincol;
520 register int fromcol;
521 register int tab_width = XINT (current_buffer->tab_width);
523 CHECK_NUMBER (column, 0);
524 if (NILP (minimum))
525 XSETFASTINT (minimum, 0);
526 CHECK_NUMBER (minimum, 1);
528 fromcol = current_column ();
529 mincol = fromcol + XINT (minimum);
530 if (mincol < XINT (column)) mincol = XINT (column);
532 if (fromcol == mincol)
533 return make_number (mincol);
535 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
537 if (indent_tabs_mode)
539 Lisp_Object n;
540 XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
541 if (XFASTINT (n) != 0)
543 Finsert_char (make_number ('\t'), n, Qt);
545 fromcol = (mincol / tab_width) * tab_width;
549 XSETFASTINT (column, mincol - fromcol);
550 Finsert_char (make_number (' '), column, Qt);
552 last_known_column = mincol;
553 last_known_column_point = point;
554 last_known_column_modified = MODIFF;
556 XSETINT (column, mincol);
557 return column;
561 DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
562 0, 0, 0,
563 "Return the indentation of the current line.\n\
564 This is the horizontal position of the character\n\
565 following any initial whitespace.")
568 Lisp_Object val;
570 XSETFASTINT (val, position_indentation (find_next_newline (point, -1)));
571 return val;
574 position_indentation (pos)
575 register int pos;
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;
581 unsigned char *start;
582 int next_boundary = pos;
583 int ceiling = pos;
585 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
587 p = &FETCH_CHAR (pos);
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;
594 while (1)
596 while (p == stop)
598 int stop_pos;
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. */
605 if (pos == ZV)
606 return column;
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. */
618 stop = &FETCH_CHAR (stop_pos - 1) + 1;
619 p = &FETCH_CHAR (pos);
621 switch (*p++)
623 case ' ':
624 column++;
625 break;
626 case '\t':
627 column += tab_width - column % tab_width;
628 break;
629 default:
630 return column;
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. */
639 indented_beyond_p (pos, column)
640 int pos, column;
642 while (pos > BEGV && FETCH_CHAR (pos) == '\n')
643 pos = find_next_newline_no_quit (pos - 1, -1);
644 return (position_indentation (pos) >= column);
647 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
648 "Move point to column COLUMN in the current line.\n\
649 The column of a character is calculated by adding together the widths\n\
650 as displayed of the previous characters in the line.\n\
651 This function ignores line-continuation;\n\
652 there is no upper limit on the column number a character can have\n\
653 and horizontal scrolling has no effect.\n\
655 If specified column is within a character, point goes after that character.\n\
656 If it's past end of line, point goes to end of line.\n\n\
657 A non-nil second (optional) argument FORCE means, if the line\n\
658 is too short to reach column COLUMN then add spaces/tabs to get there,\n\
659 and if COLUMN is in the middle of a tab character, change it to spaces.\n\
661 The return value is the current column.")
662 (column, force)
663 Lisp_Object column, force;
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);
670 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
671 register struct Lisp_Char_Table *dp = buffer_display_table ();
673 Lisp_Object val;
674 int prev_col;
675 int c;
677 int next_boundary;
679 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
680 CHECK_NATNUM (column, 0);
681 goal = XINT (column);
683 pos = point;
684 end = ZV;
685 next_boundary = pos;
687 /* If we're starting past the desired column,
688 back up to beginning of line and scan from there. */
689 if (col > goal)
691 end = pos;
692 pos = find_next_newline (pos, -1);
693 col = 0;
696 while (col < goal && pos < end)
698 while (pos == next_boundary)
700 pos = skip_invisible (pos, &next_boundary, end, Qnil);
701 if (pos >= end)
702 goto endloop;
705 c = FETCH_CHAR (pos);
706 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
708 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
709 pos++;
710 continue;
712 if (c == '\n')
713 break;
714 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
715 break;
716 pos++;
717 if (c == '\t')
719 prev_col = col;
720 col += tab_width;
721 col = col / tab_width * tab_width;
723 else if (ctl_arrow && (c < 040 || c == 0177))
724 col += 2;
725 else if (c < 040 || c >= 0177)
726 col += 4;
727 else
728 col++;
730 endloop:
732 SET_PT (pos);
734 /* If a tab char made us overshoot, change it to spaces
735 and scan through it again. */
736 if (!NILP (force) && col > goal && c == '\t' && prev_col < goal)
738 int old_point;
740 del_range (point - 1, point);
741 Findent_to (make_number (goal), Qnil);
742 old_point = point;
743 Findent_to (make_number (col), Qnil);
744 SET_PT (old_point);
745 /* Set the last_known... vars consistently. */
746 col = goal;
749 /* If line ends prematurely, add space to the end. */
750 if (col < goal && !NILP (force))
751 Findent_to (make_number (col = goal), Qnil);
753 last_known_column = col;
754 last_known_column_point = point;
755 last_known_column_modified = MODIFF;
757 XSETFASTINT (val, col);
758 return val;
761 /* compute_motion: compute buffer posn given screen posn and vice versa */
763 struct position val_compute_motion;
765 /* Scan the current buffer forward from offset FROM, pretending that
766 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
767 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
768 and return the ending buffer position and screen location. If we
769 can't hit the requested column exactly (because of a tab or other
770 multi-column character), overshoot.
772 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
773 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
774 earlier call to compute_motion. The other common case is that FROMHPOS
775 is zero and FROM is a position that "belongs" at column zero, but might
776 be shifted by overlay strings; in this case DID_MOTION should be 0.
778 WIDTH is the number of columns available to display text;
779 compute_motion uses this to handle continuation lines and such.
780 HSCROLL is the number of columns not being displayed at the left
781 margin; this is usually taken from a window's hscroll member.
782 TAB_OFFSET is the number of columns of the first tab that aren't
783 being displayed, perhaps because of a continuation line or
784 something.
786 compute_motion returns a pointer to a struct position. The bufpos
787 member gives the buffer position at the end of the scan, and hpos
788 and vpos give its cartesian location. prevhpos is the column at
789 which the character before bufpos started, and contin is non-zero
790 if we reached the current line by continuing the previous.
792 Note that FROMHPOS and TOHPOS should be expressed in real screen
793 columns, taking HSCROLL and the truncation glyph at the left margin
794 into account. That is, beginning-of-line moves you to the hpos
795 -HSCROLL + (HSCROLL > 0).
797 Note that FROMHPOS and TOHPOS should be expressed in real screen
798 columns, taking HSCROLL and the truncation glyph at the left margin
799 into account. That is, beginning-of-line moves you to the hpos
800 -HSCROLL + (HSCROLL > 0).
802 For example, to find the buffer position of column COL of line LINE
803 of a certain window, pass the window's starting location as FROM
804 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
805 Pass the buffer's ZV as TO, to limit the scan to the end of the
806 visible section of the buffer, and pass LINE and COL as TOVPOS and
807 TOHPOS.
809 When displaying in window w, a typical formula for WIDTH is:
811 window_width - 1
812 - (has_vertical_scroll_bars
813 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
814 : (window_width + window_left != frame_width))
816 where
817 window_width is XFASTINT (w->width),
818 window_left is XFASTINT (w->left),
819 has_vertical_scroll_bars is
820 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
821 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
823 Or you can let window_internal_width do this all for you, and write:
824 window_internal_width (w) - 1
826 The `-1' accounts for the continuation-line backslashes; the rest
827 accounts for window borders if the window is split horizontally, and
828 the scroll bars if they are turned on. */
830 struct position *
831 compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win)
832 int from, fromvpos, fromhpos, to, tovpos, tohpos;
833 int did_motion;
834 register int width;
835 int hscroll, tab_offset;
836 struct window *win;
838 register int hpos = fromhpos;
839 register int vpos = fromvpos;
841 register int pos;
842 register int c;
843 register int tab_width = XFASTINT (current_buffer->tab_width);
844 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
845 register struct Lisp_Char_Table *dp = window_display_table (win);
846 int selective
847 = (INTEGERP (current_buffer->selective_display)
848 ? XINT (current_buffer->selective_display)
849 : !NILP (current_buffer->selective_display) ? -1 : 0);
850 int prev_vpos = vpos, prev_hpos = 0;
851 int selective_rlen
852 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
853 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0);
854 /* The next location where the `invisible' property changes, or an
855 overlay starts or ends. */
856 int next_boundary = from;
858 /* For computing runs of characters with similar widths.
859 Invariant: width_run_width is zero, or all the characters
860 from width_run_start to width_run_end have a fixed width of
861 width_run_width. */
862 int width_run_start = from;
863 int width_run_end = from;
864 int width_run_width = 0;
865 Lisp_Object *width_table;
866 Lisp_Object buffer;
868 /* The next buffer pos where we should consult the width run cache. */
869 int next_width_run = from;
870 Lisp_Object window;
872 XSETBUFFER (buffer, current_buffer);
873 XSETWINDOW (window, win);
875 width_run_cache_on_off ();
876 if (dp == buffer_display_table ())
877 width_table = (VECTORP (current_buffer->width_table)
878 ? XVECTOR (current_buffer->width_table)->contents
879 : 0);
880 else
881 /* If the window has its own display table, we can't use the width
882 run cache, because that's based on the buffer's display table. */
883 width_table = 0;
885 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
887 pos = from;
888 while (1)
890 while (pos == next_boundary)
892 /* If the caller says that the screen position came from an earlier
893 call to compute_motion, then we've already accounted for the
894 overlay strings at point. This is only true the first time
895 through, so clear the flag after testing it. */
896 if (!did_motion)
897 /* We need to skip past the overlay strings. Currently those
898 strings must contain single-column printing characters;
899 if we want to relax that restriction, something will have
900 to be changed here. */
901 hpos += overlay_strings (pos, win, (char **)0);
902 did_motion = 0;
904 if (pos >= to)
905 break;
907 /* Advance POS past invisible characters
908 (but not necessarily all that there are here),
909 and store in next_boundary the next position where
910 we need to call skip_invisible. */
911 pos = skip_invisible (pos, &next_boundary, to, window);
914 /* Handle right margin. */
915 if (hpos >= width
916 && (hpos > width
917 || (pos < ZV && FETCH_CHAR (pos) != '\n')))
919 if (hscroll
920 || (truncate_partial_width_windows
921 && width + 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win))))
922 || !NILP (current_buffer->truncate_lines))
924 /* Truncating: skip to newline. */
925 pos = find_before_next_newline (pos, to, 1);
926 hpos = width;
927 /* If we just skipped next_boundary,
928 loop around in the main while
929 and handle it. */
930 if (pos >= next_boundary)
931 next_boundary = pos + 1;
933 else
935 /* Continuing. */
936 vpos += hpos / width;
937 tab_offset += hpos - hpos % width;
938 hpos %= width;
942 /* Stop if past the target buffer position or screen position. */
943 if (pos >= to)
944 break;
945 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
946 break;
948 prev_vpos = vpos;
949 prev_hpos = hpos;
951 /* Consult the width run cache to see if we can avoid inspecting
952 the text character-by-character. */
953 if (current_buffer->width_run_cache && pos >= next_width_run)
955 int run_end;
956 int common_width
957 = region_cache_forward (current_buffer,
958 current_buffer->width_run_cache,
959 pos, &run_end);
961 /* A width of zero means the character's width varies (like
962 a tab), is meaningless (like a newline), or we just don't
963 want to skip over it for some other reason. */
964 if (common_width != 0)
966 int run_end_hpos;
968 /* Don't go past the final buffer posn the user
969 requested. */
970 if (run_end > to)
971 run_end = to;
973 run_end_hpos = hpos + (run_end - pos) * common_width;
975 /* Don't go past the final horizontal position the user
976 requested. */
977 if (vpos == tovpos && run_end_hpos > tohpos)
979 run_end = pos + (tohpos - hpos) / common_width;
980 run_end_hpos = hpos + (run_end - pos) * common_width;
983 /* Don't go past the margin. */
984 if (run_end_hpos >= width)
986 run_end = pos + (width - hpos) / common_width;
987 run_end_hpos = hpos + (run_end - pos) * common_width;
990 hpos = run_end_hpos;
991 if (run_end > pos)
992 prev_hpos = hpos - common_width;
993 pos = run_end;
996 next_width_run = run_end + 1;
999 /* We have to scan the text character-by-character. */
1000 else
1002 c = FETCH_CHAR (pos);
1003 pos++;
1005 /* Perhaps add some info to the width_run_cache. */
1006 if (current_buffer->width_run_cache)
1008 /* Is this character part of the current run? If so, extend
1009 the run. */
1010 if (pos - 1 == width_run_end
1011 && width_table[c] == width_run_width)
1012 width_run_end = pos;
1014 /* The previous run is over, since this is a character at a
1015 different position, or a different width. */
1016 else
1018 /* Have we accumulated a run to put in the cache?
1019 (Currently, we only cache runs of width == 1). */
1020 if (width_run_start < width_run_end
1021 && width_run_width == 1)
1022 know_region_cache (current_buffer,
1023 current_buffer->width_run_cache,
1024 width_run_start, width_run_end);
1026 /* Start recording a new width run. */
1027 width_run_width = width_table[c];
1028 width_run_start = pos - 1;
1029 width_run_end = pos;
1033 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1034 hpos += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
1035 else if (c >= 040 && c < 0177)
1036 hpos++;
1037 else if (c == '\t')
1039 int tem = (hpos + tab_offset + hscroll - (hscroll > 0)) % tab_width;
1040 if (tem < 0)
1041 tem += tab_width;
1042 hpos += tab_width - tem;
1044 else if (c == '\n')
1046 if (selective > 0 && indented_beyond_p (pos, selective))
1048 /* Skip any number of invisible lines all at once */
1050 pos = find_before_next_newline (pos, to, 1) + 1;
1051 while (pos < to
1052 && indented_beyond_p (pos, selective));
1053 /* Allow for the " ..." that is displayed for them. */
1054 if (selective_rlen)
1056 hpos += selective_rlen;
1057 if (hpos >= width)
1058 hpos = width;
1060 --pos;
1061 /* We have skipped the invis text, but not the
1062 newline after. */
1064 else
1066 /* A visible line. */
1067 vpos++;
1068 hpos = 0;
1069 hpos -= hscroll;
1070 /* Count the truncation glyph on column 0 */
1071 if (hscroll > 0)
1072 hpos++;
1073 tab_offset = 0;
1076 else if (c == CR && selective < 0)
1078 /* In selective display mode,
1079 everything from a ^M to the end of the line is invisible.
1080 Stop *before* the real newline. */
1081 pos = find_before_next_newline (pos, to, 1);
1082 /* If we just skipped next_boundary,
1083 loop around in the main while
1084 and handle it. */
1085 if (pos > next_boundary)
1086 next_boundary = pos;
1087 /* Allow for the " ..." that is displayed for them. */
1088 if (selective_rlen)
1090 hpos += selective_rlen;
1091 if (hpos >= width)
1092 hpos = width;
1095 else
1096 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1100 /* Remember any final width run in the cache. */
1101 if (current_buffer->width_run_cache
1102 && width_run_width == 1
1103 && width_run_start < width_run_end)
1104 know_region_cache (current_buffer, current_buffer->width_run_cache,
1105 width_run_start, width_run_end);
1107 val_compute_motion.bufpos = pos;
1108 val_compute_motion.hpos = hpos;
1109 val_compute_motion.vpos = vpos;
1110 val_compute_motion.prevhpos = prev_hpos;
1112 /* Nonzero if have just continued a line */
1113 val_compute_motion.contin
1114 = (pos != from
1115 && (val_compute_motion.vpos != prev_vpos)
1116 && c != '\n');
1118 return &val_compute_motion;
1121 #if 0 /* The doc string is too long for some compilers,
1122 but make-docfile can find it in this comment. */
1123 DEFUN ("compute-motion", Ffoo, Sfoo, 7, 7, 0,
1124 "Scan through the current buffer, calculating screen position.\n\
1125 Scan the current buffer forward from offset FROM,\n\
1126 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1127 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1128 and return the ending buffer position and screen location.\n\
1130 There are three additional arguments:\n\
1132 WIDTH is the number of columns available to display text;\n\
1133 this affects handling of continuation lines.\n\
1134 This is usually the value returned by `window-width', less one (to allow\n\
1135 for the continuation glyph).\n\
1137 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1138 HSCROLL is the number of columns not being displayed at the left\n\
1139 margin; this is usually taken from a window's hscroll member.\n\
1140 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1141 being displayed, perhaps because the line was continued within it.\n\
1142 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1144 WINDOW is the window to operate on. It is used to choose the display table;\n\
1145 if it is showing the current buffer, it is used also for\n\
1146 deciding which overlay properties apply.\n\
1147 Note that `compute-motion' always operates on the current buffer.\n\
1149 The value is a list of five elements:\n\
1150 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1151 POS is the buffer position where the scan stopped.\n\
1152 VPOS is the vertical position where the scan stopped.\n\
1153 HPOS is the horizontal position where the scan stopped.\n\
1155 PREVHPOS is the horizontal position one character back from POS.\n\
1156 CONTIN is t if a line was continued after (or within) the previous character.\n\
1158 For example, to find the buffer position of column COL of line LINE\n\
1159 of a certain window, pass the window's starting location as FROM\n\
1160 and the window's upper-left coordinates as FROMPOS.\n\
1161 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1162 visible section of the buffer, and pass LINE and COL as TOPOS.")
1163 (from, frompos, to, topos, width, offsets, window)
1164 #endif
1166 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1168 (from, frompos, to, topos, width, offsets, window)
1169 Lisp_Object from, frompos, to, topos;
1170 Lisp_Object width, offsets, window;
1172 Lisp_Object bufpos, hpos, vpos, prevhpos, contin;
1173 struct position *pos;
1174 int hscroll, tab_offset;
1176 CHECK_NUMBER_COERCE_MARKER (from, 0);
1177 CHECK_CONS (frompos, 0);
1178 CHECK_NUMBER (XCONS (frompos)->car, 0);
1179 CHECK_NUMBER (XCONS (frompos)->cdr, 0);
1180 CHECK_NUMBER_COERCE_MARKER (to, 0);
1181 CHECK_CONS (topos, 0);
1182 CHECK_NUMBER (XCONS (topos)->car, 0);
1183 CHECK_NUMBER (XCONS (topos)->cdr, 0);
1184 CHECK_NUMBER (width, 0);
1185 if (!NILP (offsets))
1187 CHECK_CONS (offsets, 0);
1188 CHECK_NUMBER (XCONS (offsets)->car, 0);
1189 CHECK_NUMBER (XCONS (offsets)->cdr, 0);
1190 hscroll = XINT (XCONS (offsets)->car);
1191 tab_offset = XINT (XCONS (offsets)->cdr);
1193 else
1194 hscroll = tab_offset = 0;
1196 if (NILP (window))
1197 window = Fselected_window ();
1198 else
1199 CHECK_LIVE_WINDOW (window, 0);
1201 pos = compute_motion (XINT (from), XINT (XCONS (frompos)->cdr),
1202 XINT (XCONS (frompos)->car), 0,
1203 XINT (to), XINT (XCONS (topos)->cdr),
1204 XINT (XCONS (topos)->car),
1205 XINT (width), hscroll, tab_offset,
1206 XWINDOW (window));
1208 XSETFASTINT (bufpos, pos->bufpos);
1209 XSETINT (hpos, pos->hpos);
1210 XSETINT (vpos, pos->vpos);
1211 XSETINT (prevhpos, pos->prevhpos);
1213 return Fcons (bufpos,
1214 Fcons (hpos,
1215 Fcons (vpos,
1216 Fcons (prevhpos,
1217 Fcons (pos->contin ? Qt : Qnil, Qnil)))));
1221 /* Return the column of position POS in window W's buffer.
1222 The result is rounded down to a multiple of the internal width of W.
1223 This is the amount of indentation of position POS
1224 that is not visible in its horizontal position in the window. */
1227 pos_tab_offset (w, pos)
1228 struct window *w;
1229 register int pos;
1231 int opoint = PT;
1232 int col;
1233 int width = window_internal_width (w) - 1;
1235 if (pos == BEGV || FETCH_CHAR (pos - 1) == '\n')
1236 return 0;
1237 TEMP_SET_PT (pos);
1238 col = current_column ();
1239 TEMP_SET_PT (opoint);
1240 return col - (col % width);
1244 /* Fvertical_motion and vmotion */
1245 struct position val_vmotion;
1247 struct position *
1248 vmotion (from, vtarget, w)
1249 register int from, vtarget;
1250 struct window *w;
1252 int width = window_internal_width (w) - 1;
1253 int hscroll = XINT (w->hscroll);
1254 struct position pos;
1255 /* vpos is cumulative vertical position, changed as from is changed */
1256 register int vpos = 0;
1257 Lisp_Object prevline;
1258 register int first;
1259 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
1260 int selective
1261 = (INTEGERP (current_buffer->selective_display)
1262 ? XINT (current_buffer->selective_display)
1263 : !NILP (current_buffer->selective_display) ? -1 : 0);
1264 Lisp_Object window;
1265 int start_hpos = 0;
1266 int did_motion;
1268 XSETWINDOW (window, w);
1270 /* The omission of the clause
1271 && marker_position (w->start) == BEG
1272 here is deliberate; I think we want to measure from the prompt
1273 position even if the minibuffer window has scrolled. */
1274 if (EQ (window, minibuf_window))
1276 if (minibuf_prompt_width == 0 && STRINGP (minibuf_prompt))
1277 minibuf_prompt_width
1278 = string_display_width (minibuf_prompt, Qnil, Qnil);
1280 start_hpos = minibuf_prompt_width;
1283 if (vpos >= vtarget)
1285 /* To move upward, go a line at a time until
1286 we have gone at least far enough */
1288 first = 1;
1290 while ((vpos > vtarget || first) && from > BEGV)
1292 Lisp_Object propval;
1294 XSETFASTINT (prevline, find_next_newline_no_quit (from - 1, -1));
1295 while (XFASTINT (prevline) > BEGV
1296 && ((selective > 0
1297 && indented_beyond_p (XFASTINT (prevline), selective))
1298 #ifdef USE_TEXT_PROPERTIES
1299 /* watch out for newlines with `invisible' property */
1300 || (propval = Fget_char_property (prevline,
1301 Qinvisible,
1302 window),
1303 TEXT_PROP_MEANS_INVISIBLE (propval))
1304 #endif
1306 XSETFASTINT (prevline,
1307 find_next_newline_no_quit (XFASTINT (prevline) - 1,
1308 -1));
1309 pos = *compute_motion (XFASTINT (prevline), 0,
1310 lmargin + (XFASTINT (prevline) == BEG
1311 ? start_hpos : 0),
1313 from, 1 << (BITS_PER_INT - 2), 0,
1314 width, hscroll, 0, w);
1315 vpos -= pos.vpos;
1316 first = 0;
1317 from = XFASTINT (prevline);
1320 /* If we made exactly the desired vertical distance,
1321 or if we hit beginning of buffer,
1322 return point found */
1323 if (vpos >= vtarget)
1325 val_vmotion.bufpos = from;
1326 val_vmotion.vpos = vpos;
1327 val_vmotion.hpos = lmargin;
1328 val_vmotion.contin = 0;
1329 val_vmotion.prevhpos = 0;
1330 return &val_vmotion;
1333 /* Otherwise find the correct spot by moving down */
1335 /* Moving downward is simple, but must calculate from beg of line
1336 to determine hpos of starting point */
1337 if (from > BEGV && FETCH_CHAR (from - 1) != '\n')
1339 Lisp_Object propval;
1341 XSETFASTINT (prevline, find_next_newline_no_quit (from, -1));
1342 while (XFASTINT (prevline) > BEGV
1343 && ((selective > 0
1344 && indented_beyond_p (XFASTINT (prevline), selective))
1345 #ifdef USE_TEXT_PROPERTIES
1346 /* watch out for newlines with `invisible' property */
1347 || (propval = Fget_char_property (prevline, Qinvisible,
1348 window),
1349 TEXT_PROP_MEANS_INVISIBLE (propval))
1350 #endif
1352 XSETFASTINT (prevline,
1353 find_next_newline_no_quit (XFASTINT (prevline) - 1,
1354 -1));
1355 pos = *compute_motion (XFASTINT (prevline), 0,
1356 lmargin + (XFASTINT (prevline) == BEG
1357 ? start_hpos : 0),
1359 from, 1 << (BITS_PER_INT - 2), 0,
1360 width, hscroll, 0, w);
1361 did_motion = 1;
1363 else
1365 pos.hpos = lmargin + (from == BEG ? start_hpos : 0);
1366 pos.vpos = 0;
1367 did_motion = 0;
1369 return compute_motion (from, vpos, pos.hpos, did_motion,
1370 ZV, vtarget, - (1 << (BITS_PER_INT - 2)),
1371 width, hscroll, pos.vpos * width, w);
1374 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
1375 "Move to start of screen line LINES lines down.\n\
1376 If LINES is negative, this is moving up.\n\
1378 The optional second argument WINDOW specifies the window to use for\n\
1379 parameters such as width, horizontal scrolling, and so on.\n\
1380 the default is the selected window.\n\
1381 It does not matter what buffer is displayed in WINDOW.\n\
1382 `vertical-motion' always uses the current buffer.\n\
1384 Sets point to position found; this may be start of line\n\
1385 or just the start of a continuation line.\n\
1386 Returns number of lines moved; may be closer to zero than LINES\n\
1387 if beginning or end of buffer was reached.")
1388 (lines, window)
1389 Lisp_Object lines, window;
1391 struct position pos;
1393 CHECK_NUMBER (lines, 0);
1394 if (! NILP (window))
1395 CHECK_WINDOW (window, 0);
1396 else
1397 window = selected_window;
1399 pos = *vmotion (point, (int) XINT (lines), XWINDOW (window));
1401 SET_PT (pos.bufpos);
1402 return make_number (pos.vpos);
1405 /* file's initialization. */
1407 syms_of_indent ()
1409 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
1410 "*Indentation can insert tabs if this is non-nil.\n\
1411 Setting this variable automatically makes it local to the current buffer.");
1412 indent_tabs_mode = 1;
1414 defsubr (&Scurrent_indentation);
1415 defsubr (&Sindent_to);
1416 defsubr (&Scurrent_column);
1417 defsubr (&Smove_to_column);
1418 defsubr (&Svertical_motion);
1419 defsubr (&Scompute_motion);