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)
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. */
31 #include "intervals.h"
32 #include "region-cache.h"
34 /* Indentation can insert tabs if this is non-zero;
35 otherwise always uses spaces */
38 #define min(a, b) ((a) < (b) ? (a) : (b))
39 #define max(a, b) ((a) > (b) ? (a) : (b))
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 ()
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
);
70 /* Width run cache considerations. */
72 /* Return the width of character C under display table DP. */
75 character_width (c
, dp
)
77 struct Lisp_Char_Table
*dp
;
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')
93 /* Printing characters have width 1. */
94 else if (c
>= 040 && c
< 0177)
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
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
;
116 if (widthtab
->size
!= 256)
119 for (i
= 0; i
< 256; i
++)
120 if (character_width (i
, disptab
)
121 != XFASTINT (widthtab
->contents
[i
]))
127 /* Recompute BUF's width table, using the display table DISPTAB. */
129 recompute_width_table (buf
, disptab
)
131 struct Lisp_Char_Table
*disptab
;
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)
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. */
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
;
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. */
197 skip_invisible (pos
, next_boundary_p
, to
, window
)
199 int *next_boundary_p
;
203 Lisp_Object prop
, position
, end
, overlay_limit
, proplimit
;
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. */
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
,
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
);
243 prop
= Fget_char_property (position
, Qinvisible
, buffer
);
244 if (TEXT_PROP_MEANS_INVISIBLE (prop
))
245 return *next_boundary_p
;
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.")
262 XSETFASTINT (temp
, current_column ());
266 /* Cancel any recorded value of the horizontal position. */
268 invalidate_current_column ()
270 last_known_column_point
= 0;
277 register unsigned char *ptr
, *stop
;
278 register int tab_seen
;
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 ();
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. */
306 else if (point
<= GPT
|| BEGV
> GPT
)
311 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
313 col
= 0, tab_seen
= 0, post_tab
= 0;
319 /* We stopped either for the beginning of the buffer
321 if (ptr
== BEGV_ADDR
)
323 /* It was the gap. Jump back over it. */
326 /* Check whether that brings us to beginning of buffer. */
327 if (BEGV
>= GPT
) break;
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)
337 else if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
342 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
349 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
354 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
358 last_known_column
= col
;
359 last_known_column_point
= point
;
360 last_known_column_modified
= MODIFF
;
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. */
371 current_column_1 (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. */
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
);
400 c
= FETCH_CHAR (scan
);
401 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
403 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
409 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
416 col
= col
/ tab_width
* tab_width
;
418 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
420 else if (c
< 040 || c
>= 0177)
427 last_known_column
= col
;
428 last_known_column_point
= point
;
429 last_known_column_modified
= MODIFF
;
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. */
439 string_display_width (string
, beg
, end
)
440 Lisp_Object string
, beg
, end
;
443 register unsigned char *ptr
, *stop
;
444 register int tab_seen
;
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 ();
453 e
= XSTRING (string
)->size
;
456 CHECK_NUMBER (end
, 0);
464 CHECK_NUMBER (beg
, 0);
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;
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)
493 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
500 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
505 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
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.")
517 Lisp_Object column
, minimum
;
520 register int fromcol
;
521 register int tab_width
= XINT (current_buffer
->tab_width
);
523 CHECK_NUMBER (column
, 0);
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
)
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
);
561 DEFUN ("current-indentation", Fcurrent_indentation
, Scurrent_indentation
,
563 "Return the indentation of the current line.\n\
564 This is the horizontal position of the character\n\
565 following any initial whitespace.")
570 XSETFASTINT (val
, position_indentation (find_next_newline (point
, -1)));
574 position_indentation (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
;
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. */
592 /* START records the starting value of P. */
600 /* If we have updated P, set POS to match.
601 The first time we enter the loop, POS is already right. */
603 pos
= PTR_CHAR_POS (p
);
604 /* Consider the various reasons STOP might have been set here. */
607 if (pos
== next_boundary
)
608 pos
= skip_invisible (pos
, &next_boundary
, ZV
, Qnil
);
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
);
627 column
+= tab_width
- column
% tab_width
;
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
639 indented_beyond_p (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.")
663 Lisp_Object column
, force
;
666 register int col
= current_column ();
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 ();
679 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
680 CHECK_NATNUM (column
, 0);
681 goal
= XINT (column
);
687 /* If we're starting past the desired column,
688 back up to beginning of line and scan from there. */
692 pos
= find_next_newline (pos
, -1);
698 while (pos
== next_boundary
)
700 pos
= skip_invisible (pos
, &next_boundary
, end
, Qnil
);
705 /* Test reaching the goal column. We do this after skipping
706 invisible characters, so that we put point before the
707 character on which the cursor will appear. */
711 c
= FETCH_CHAR (pos
);
712 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
714 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
720 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
727 col
= col
/ tab_width
* tab_width
;
729 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
731 else if (c
< 040 || c
>= 0177)
740 /* If a tab char made us overshoot, change it to spaces
741 and scan through it again. */
742 if (!NILP (force
) && col
> goal
&& c
== '\t' && prev_col
< goal
)
746 del_range (point
- 1, point
);
747 Findent_to (make_number (goal
), Qnil
);
749 Findent_to (make_number (col
), Qnil
);
751 /* Set the last_known... vars consistently. */
755 /* If line ends prematurely, add space to the end. */
756 if (col
< goal
&& !NILP (force
))
757 Findent_to (make_number (col
= goal
), Qnil
);
759 last_known_column
= col
;
760 last_known_column_point
= point
;
761 last_known_column_modified
= MODIFF
;
763 XSETFASTINT (val
, col
);
767 /* compute_motion: compute buffer posn given screen posn and vice versa */
769 struct position val_compute_motion
;
771 /* Scan the current buffer forward from offset FROM, pretending that
772 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
773 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
774 and return the ending buffer position and screen location. If we
775 can't hit the requested column exactly (because of a tab or other
776 multi-column character), overshoot.
778 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
779 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
780 earlier call to compute_motion. The other common case is that FROMHPOS
781 is zero and FROM is a position that "belongs" at column zero, but might
782 be shifted by overlay strings; in this case DID_MOTION should be 0.
784 WIDTH is the number of columns available to display text;
785 compute_motion uses this to handle continuation lines and such.
786 HSCROLL is the number of columns not being displayed at the left
787 margin; this is usually taken from a window's hscroll member.
788 TAB_OFFSET is the number of columns of the first tab that aren't
789 being displayed, perhaps because of a continuation line or
792 compute_motion returns a pointer to a struct position. The bufpos
793 member gives the buffer position at the end of the scan, and hpos
794 and vpos give its cartesian location. prevhpos is the column at
795 which the character before bufpos started, and contin is non-zero
796 if we reached the current line by continuing the previous.
798 Note that FROMHPOS and TOHPOS should be expressed in real screen
799 columns, taking HSCROLL and the truncation glyph at the left margin
800 into account. That is, beginning-of-line moves you to the hpos
801 -HSCROLL + (HSCROLL > 0).
803 Note that FROMHPOS and TOHPOS should be expressed in real screen
804 columns, taking HSCROLL and the truncation glyph at the left margin
805 into account. That is, beginning-of-line moves you to the hpos
806 -HSCROLL + (HSCROLL > 0).
808 For example, to find the buffer position of column COL of line LINE
809 of a certain window, pass the window's starting location as FROM
810 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
811 Pass the buffer's ZV as TO, to limit the scan to the end of the
812 visible section of the buffer, and pass LINE and COL as TOVPOS and
815 When displaying in window w, a typical formula for WIDTH is:
818 - (has_vertical_scroll_bars
819 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
820 : (window_width + window_left != frame_width))
823 window_width is XFASTINT (w->width),
824 window_left is XFASTINT (w->left),
825 has_vertical_scroll_bars is
826 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
827 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
829 Or you can let window_internal_width do this all for you, and write:
830 window_internal_width (w) - 1
832 The `-1' accounts for the continuation-line backslashes; the rest
833 accounts for window borders if the window is split horizontally, and
834 the scroll bars if they are turned on. */
837 compute_motion (from
, fromvpos
, fromhpos
, did_motion
, to
, tovpos
, tohpos
, width
, hscroll
, tab_offset
, win
)
838 int from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
;
841 int hscroll
, tab_offset
;
844 register int hpos
= fromhpos
;
845 register int vpos
= fromvpos
;
849 register int tab_width
= XFASTINT (current_buffer
->tab_width
);
850 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
851 register struct Lisp_Char_Table
*dp
= window_display_table (win
);
853 = (INTEGERP (current_buffer
->selective_display
)
854 ? XINT (current_buffer
->selective_display
)
855 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
856 int prev_vpos
= vpos
, prev_hpos
= 0;
858 = (selective
&& dp
&& VECTORP (DISP_INVIS_VECTOR (dp
))
859 ? XVECTOR (DISP_INVIS_VECTOR (dp
))->size
: 0);
860 /* The next location where the `invisible' property changes, or an
861 overlay starts or ends. */
862 int next_boundary
= from
;
864 /* For computing runs of characters with similar widths.
865 Invariant: width_run_width is zero, or all the characters
866 from width_run_start to width_run_end have a fixed width of
868 int width_run_start
= from
;
869 int width_run_end
= from
;
870 int width_run_width
= 0;
871 Lisp_Object
*width_table
;
874 /* The next buffer pos where we should consult the width run cache. */
875 int next_width_run
= from
;
878 XSETBUFFER (buffer
, current_buffer
);
879 XSETWINDOW (window
, win
);
881 width_run_cache_on_off ();
882 if (dp
== buffer_display_table ())
883 width_table
= (VECTORP (current_buffer
->width_table
)
884 ? XVECTOR (current_buffer
->width_table
)->contents
887 /* If the window has its own display table, we can't use the width
888 run cache, because that's based on the buffer's display table. */
891 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
896 while (pos
== next_boundary
)
898 /* If the caller says that the screen position came from an earlier
899 call to compute_motion, then we've already accounted for the
900 overlay strings at point. This is only true the first time
901 through, so clear the flag after testing it. */
903 /* We need to skip past the overlay strings. Currently those
904 strings must contain single-column printing characters;
905 if we want to relax that restriction, something will have
906 to be changed here. */
907 hpos
+= overlay_strings (pos
, win
, (char **)0);
913 /* Advance POS past invisible characters
914 (but not necessarily all that there are here),
915 and store in next_boundary the next position where
916 we need to call skip_invisible. */
917 pos
= skip_invisible (pos
, &next_boundary
, to
, window
);
920 /* Handle right margin. */
923 || (pos
< ZV
&& FETCH_CHAR (pos
) != '\n')))
926 || (truncate_partial_width_windows
927 && width
+ 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win
))))
928 || !NILP (current_buffer
->truncate_lines
))
930 /* Truncating: skip to newline. */
931 pos
= find_before_next_newline (pos
, to
, 1);
933 /* If we just skipped next_boundary,
934 loop around in the main while
936 if (pos
>= next_boundary
)
937 next_boundary
= pos
+ 1;
942 vpos
+= hpos
/ width
;
943 tab_offset
+= hpos
- hpos
% width
;
948 /* Stop if past the target buffer position or screen position. */
951 if (vpos
> tovpos
|| (vpos
== tovpos
&& hpos
>= tohpos
))
957 /* Consult the width run cache to see if we can avoid inspecting
958 the text character-by-character. */
959 if (current_buffer
->width_run_cache
&& pos
>= next_width_run
)
963 = region_cache_forward (current_buffer
,
964 current_buffer
->width_run_cache
,
967 /* A width of zero means the character's width varies (like
968 a tab), is meaningless (like a newline), or we just don't
969 want to skip over it for some other reason. */
970 if (common_width
!= 0)
974 /* Don't go past the final buffer posn the user
979 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
981 /* Don't go past the final horizontal position the user
983 if (vpos
== tovpos
&& run_end_hpos
> tohpos
)
985 run_end
= pos
+ (tohpos
- hpos
) / common_width
;
986 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
989 /* Don't go past the margin. */
990 if (run_end_hpos
>= width
)
992 run_end
= pos
+ (width
- hpos
) / common_width
;
993 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
998 prev_hpos
= hpos
- common_width
;
1002 next_width_run
= run_end
+ 1;
1005 /* We have to scan the text character-by-character. */
1008 c
= FETCH_CHAR (pos
);
1011 /* Perhaps add some info to the width_run_cache. */
1012 if (current_buffer
->width_run_cache
)
1014 /* Is this character part of the current run? If so, extend
1016 if (pos
- 1 == width_run_end
1017 && width_table
[c
] == width_run_width
)
1018 width_run_end
= pos
;
1020 /* The previous run is over, since this is a character at a
1021 different position, or a different width. */
1024 /* Have we accumulated a run to put in the cache?
1025 (Currently, we only cache runs of width == 1). */
1026 if (width_run_start
< width_run_end
1027 && width_run_width
== 1)
1028 know_region_cache (current_buffer
,
1029 current_buffer
->width_run_cache
,
1030 width_run_start
, width_run_end
);
1032 /* Start recording a new width run. */
1033 width_run_width
= width_table
[c
];
1034 width_run_start
= pos
- 1;
1035 width_run_end
= pos
;
1039 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
1040 hpos
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
1041 else if (c
>= 040 && c
< 0177)
1045 int tem
= (hpos
+ tab_offset
+ hscroll
- (hscroll
> 0)) % tab_width
;
1048 hpos
+= tab_width
- tem
;
1052 if (selective
> 0 && indented_beyond_p (pos
, selective
))
1054 /* Skip any number of invisible lines all at once */
1056 pos
= find_before_next_newline (pos
, to
, 1) + 1;
1058 && indented_beyond_p (pos
, selective
));
1059 /* Allow for the " ..." that is displayed for them. */
1062 hpos
+= selective_rlen
;
1067 /* We have skipped the invis text, but not the
1072 /* A visible line. */
1076 /* Count the truncation glyph on column 0 */
1082 else if (c
== CR
&& selective
< 0)
1084 /* In selective display mode,
1085 everything from a ^M to the end of the line is invisible.
1086 Stop *before* the real newline. */
1087 pos
= find_before_next_newline (pos
, to
, 1);
1088 /* If we just skipped next_boundary,
1089 loop around in the main while
1091 if (pos
> next_boundary
)
1092 next_boundary
= pos
;
1093 /* Allow for the " ..." that is displayed for them. */
1096 hpos
+= selective_rlen
;
1102 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
1106 /* Remember any final width run in the cache. */
1107 if (current_buffer
->width_run_cache
1108 && width_run_width
== 1
1109 && width_run_start
< width_run_end
)
1110 know_region_cache (current_buffer
, current_buffer
->width_run_cache
,
1111 width_run_start
, width_run_end
);
1113 val_compute_motion
.bufpos
= pos
;
1114 val_compute_motion
.hpos
= hpos
;
1115 val_compute_motion
.vpos
= vpos
;
1116 val_compute_motion
.prevhpos
= prev_hpos
;
1118 /* Nonzero if have just continued a line */
1119 val_compute_motion
.contin
1121 && (val_compute_motion
.vpos
!= prev_vpos
)
1124 return &val_compute_motion
;
1127 #if 0 /* The doc string is too long for some compilers,
1128 but make-docfile can find it in this comment. */
1129 DEFUN ("compute-motion", Ffoo
, Sfoo
, 7, 7, 0,
1130 "Scan through the current buffer, calculating screen position.\n\
1131 Scan the current buffer forward from offset FROM,\n\
1132 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1133 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1134 and return the ending buffer position and screen location.\n\
1136 There are three additional arguments:\n\
1138 WIDTH is the number of columns available to display text;\n\
1139 this affects handling of continuation lines.\n\
1140 This is usually the value returned by `window-width', less one (to allow\n\
1141 for the continuation glyph).\n\
1143 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1144 HSCROLL is the number of columns not being displayed at the left\n\
1145 margin; this is usually taken from a window's hscroll member.\n\
1146 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1147 being displayed, perhaps because the line was continued within it.\n\
1148 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1150 WINDOW is the window to operate on. It is used to choose the display table;\n\
1151 if it is showing the current buffer, it is used also for\n\
1152 deciding which overlay properties apply.\n\
1153 Note that `compute-motion' always operates on the current buffer.\n\
1155 The value is a list of five elements:\n\
1156 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1157 POS is the buffer position where the scan stopped.\n\
1158 VPOS is the vertical position where the scan stopped.\n\
1159 HPOS is the horizontal position where the scan stopped.\n\
1161 PREVHPOS is the horizontal position one character back from POS.\n\
1162 CONTIN is t if a line was continued after (or within) the previous character.\n\
1164 For example, to find the buffer position of column COL of line LINE\n\
1165 of a certain window, pass the window's starting location as FROM\n\
1166 and the window's upper-left coordinates as FROMPOS.\n\
1167 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1168 visible section of the buffer, and pass LINE and COL as TOPOS.")
1169 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1172 DEFUN ("compute-motion", Fcompute_motion
, Scompute_motion
, 7, 7, 0,
1174 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1175 Lisp_Object from
, frompos
, to
, topos
;
1176 Lisp_Object width
, offsets
, window
;
1178 Lisp_Object bufpos
, hpos
, vpos
, prevhpos
, contin
;
1179 struct position
*pos
;
1180 int hscroll
, tab_offset
;
1182 CHECK_NUMBER_COERCE_MARKER (from
, 0);
1183 CHECK_CONS (frompos
, 0);
1184 CHECK_NUMBER (XCONS (frompos
)->car
, 0);
1185 CHECK_NUMBER (XCONS (frompos
)->cdr
, 0);
1186 CHECK_NUMBER_COERCE_MARKER (to
, 0);
1187 CHECK_CONS (topos
, 0);
1188 CHECK_NUMBER (XCONS (topos
)->car
, 0);
1189 CHECK_NUMBER (XCONS (topos
)->cdr
, 0);
1190 CHECK_NUMBER (width
, 0);
1191 if (!NILP (offsets
))
1193 CHECK_CONS (offsets
, 0);
1194 CHECK_NUMBER (XCONS (offsets
)->car
, 0);
1195 CHECK_NUMBER (XCONS (offsets
)->cdr
, 0);
1196 hscroll
= XINT (XCONS (offsets
)->car
);
1197 tab_offset
= XINT (XCONS (offsets
)->cdr
);
1200 hscroll
= tab_offset
= 0;
1203 window
= Fselected_window ();
1205 CHECK_LIVE_WINDOW (window
, 0);
1207 pos
= compute_motion (XINT (from
), XINT (XCONS (frompos
)->cdr
),
1208 XINT (XCONS (frompos
)->car
), 0,
1209 XINT (to
), XINT (XCONS (topos
)->cdr
),
1210 XINT (XCONS (topos
)->car
),
1211 XINT (width
), hscroll
, tab_offset
,
1214 XSETFASTINT (bufpos
, pos
->bufpos
);
1215 XSETINT (hpos
, pos
->hpos
);
1216 XSETINT (vpos
, pos
->vpos
);
1217 XSETINT (prevhpos
, pos
->prevhpos
);
1219 return Fcons (bufpos
,
1223 Fcons (pos
->contin
? Qt
: Qnil
, Qnil
)))));
1227 /* Return the column of position POS in window W's buffer.
1228 The result is rounded down to a multiple of the internal width of W.
1229 This is the amount of indentation of position POS
1230 that is not visible in its horizontal position in the window. */
1233 pos_tab_offset (w
, pos
)
1239 int width
= window_internal_width (w
) - 1;
1241 if (pos
== BEGV
|| FETCH_CHAR (pos
- 1) == '\n')
1244 col
= current_column ();
1245 TEMP_SET_PT (opoint
);
1246 return col
- (col
% width
);
1250 /* Fvertical_motion and vmotion */
1251 struct position val_vmotion
;
1254 vmotion (from
, vtarget
, w
)
1255 register int from
, vtarget
;
1258 int width
= window_internal_width (w
) - 1;
1259 int hscroll
= XINT (w
->hscroll
);
1260 struct position pos
;
1261 /* vpos is cumulative vertical position, changed as from is changed */
1262 register int vpos
= 0;
1263 Lisp_Object prevline
;
1265 int lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
1267 = (INTEGERP (current_buffer
->selective_display
)
1268 ? XINT (current_buffer
->selective_display
)
1269 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1274 XSETWINDOW (window
, w
);
1276 /* The omission of the clause
1277 && marker_position (w->start) == BEG
1278 here is deliberate; I think we want to measure from the prompt
1279 position even if the minibuffer window has scrolled. */
1280 if (EQ (window
, minibuf_window
))
1282 if (minibuf_prompt_width
== 0 && STRINGP (minibuf_prompt
))
1283 minibuf_prompt_width
1284 = string_display_width (minibuf_prompt
, Qnil
, Qnil
);
1286 start_hpos
= minibuf_prompt_width
;
1289 if (vpos
>= vtarget
)
1291 /* To move upward, go a line at a time until
1292 we have gone at least far enough */
1296 while ((vpos
> vtarget
|| first
) && from
> BEGV
)
1298 Lisp_Object propval
;
1300 XSETFASTINT (prevline
, find_next_newline_no_quit (from
- 1, -1));
1301 while (XFASTINT (prevline
) > BEGV
1303 && indented_beyond_p (XFASTINT (prevline
), selective
))
1304 #ifdef USE_TEXT_PROPERTIES
1305 /* watch out for newlines with `invisible' property */
1306 || (propval
= Fget_char_property (prevline
,
1309 TEXT_PROP_MEANS_INVISIBLE (propval
))
1312 XSETFASTINT (prevline
,
1313 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1315 pos
= *compute_motion (XFASTINT (prevline
), 0,
1316 lmargin
+ (XFASTINT (prevline
) == BEG
1319 from
, 1 << (BITS_PER_INT
- 2), 0,
1320 width
, hscroll
, 0, w
);
1323 from
= XFASTINT (prevline
);
1326 /* If we made exactly the desired vertical distance,
1327 or if we hit beginning of buffer,
1328 return point found */
1329 if (vpos
>= vtarget
)
1331 val_vmotion
.bufpos
= from
;
1332 val_vmotion
.vpos
= vpos
;
1333 val_vmotion
.hpos
= lmargin
;
1334 val_vmotion
.contin
= 0;
1335 val_vmotion
.prevhpos
= 0;
1336 return &val_vmotion
;
1339 /* Otherwise find the correct spot by moving down */
1341 /* Moving downward is simple, but must calculate from beg of line
1342 to determine hpos of starting point */
1343 if (from
> BEGV
&& FETCH_CHAR (from
- 1) != '\n')
1345 Lisp_Object propval
;
1347 XSETFASTINT (prevline
, find_next_newline_no_quit (from
, -1));
1348 while (XFASTINT (prevline
) > BEGV
1350 && indented_beyond_p (XFASTINT (prevline
), selective
))
1351 #ifdef USE_TEXT_PROPERTIES
1352 /* watch out for newlines with `invisible' property */
1353 || (propval
= Fget_char_property (prevline
, Qinvisible
,
1355 TEXT_PROP_MEANS_INVISIBLE (propval
))
1358 XSETFASTINT (prevline
,
1359 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1361 pos
= *compute_motion (XFASTINT (prevline
), 0,
1362 lmargin
+ (XFASTINT (prevline
) == BEG
1365 from
, 1 << (BITS_PER_INT
- 2), 0,
1366 width
, hscroll
, 0, w
);
1371 pos
.hpos
= lmargin
+ (from
== BEG
? start_hpos
: 0);
1375 return compute_motion (from
, vpos
, pos
.hpos
, did_motion
,
1376 ZV
, vtarget
, - (1 << (BITS_PER_INT
- 2)),
1377 width
, hscroll
, pos
.vpos
* width
, w
);
1380 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 2, 0,
1381 "Move point to start of the screen line LINES lines down.\n\
1382 If LINES is negative, this means moving up.\n\
1384 This function is an ordinary cursor motion function\n\
1385 which calculates the new position based on how text would be displayed.\n\
1386 The new position may be the start of a line,\n\
1387 or just the start of a continuation line.\n\
1388 The function returns number of screen lines moved over;\n\
1389 that usually equals LINES, but may be closer to zero\n\
1390 if beginning or end of buffer was reached.\n\
1392 The optional second argument WINDOW specifies the window to use for\n\
1393 parameters such as width, horizontal scrolling, and so on.\n\
1394 The default is to use the selected window's parameters.\n\
1396 `vertical-motion' always uses the current buffer,\n\
1397 regardless of which buffer is displayed in WINDOW.\n\
1398 This is consistent with other cursor motion functions\n\
1399 and makes it possible to use `vertical-motion' in any buffer,\n\
1400 whether or not it is currently displayed in some window.")
1402 Lisp_Object lines
, window
;
1404 struct position pos
;
1406 CHECK_NUMBER (lines
, 0);
1407 if (! NILP (window
))
1408 CHECK_WINDOW (window
, 0);
1410 window
= selected_window
;
1412 pos
= *vmotion (point
, (int) XINT (lines
), XWINDOW (window
));
1414 SET_PT (pos
.bufpos
);
1415 return make_number (pos
.vpos
);
1418 /* file's initialization. */
1422 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode
,
1423 "*Indentation can insert tabs if this is non-nil.\n\
1424 Setting this variable automatically makes it local to the current buffer.");
1425 indent_tabs_mode
= 1;
1427 defsubr (&Scurrent_indentation
);
1428 defsubr (&Sindent_to
);
1429 defsubr (&Scurrent_column
);
1430 defsubr (&Smove_to_column
);
1431 defsubr (&Svertical_motion
);
1432 defsubr (&Scompute_motion
);