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 (PT
== 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 (PT
);
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 (PT
- 1) + 1;
302 /* Make a pointer to where consecutive chars leave off,
303 going backwards from point. */
306 else if (PT
<= 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
= PT
;
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
= PT
;
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
= PT
;
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 (PT
, -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 (PT
- 1, PT
);
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
= PT
;
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 For example, to find the buffer position of column COL of line LINE
804 of a certain window, pass the window's starting location as FROM
805 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
806 Pass the buffer's ZV as TO, to limit the scan to the end of the
807 visible section of the buffer, and pass LINE and COL as TOVPOS and
810 When displaying in window w, a typical formula for WIDTH is:
813 - (has_vertical_scroll_bars
814 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
815 : (window_width + window_left != frame_width))
818 window_width is XFASTINT (w->width),
819 window_left is XFASTINT (w->left),
820 has_vertical_scroll_bars is
821 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
822 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
824 Or you can let window_internal_width do this all for you, and write:
825 window_internal_width (w) - 1
827 The `-1' accounts for the continuation-line backslashes; the rest
828 accounts for window borders if the window is split horizontally, and
829 the scroll bars if they are turned on. */
832 compute_motion (from
, fromvpos
, fromhpos
, did_motion
, to
, tovpos
, tohpos
, width
, hscroll
, tab_offset
, win
)
833 int from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
;
836 int hscroll
, tab_offset
;
839 register int hpos
= fromhpos
;
840 register int vpos
= fromvpos
;
844 register int tab_width
= XFASTINT (current_buffer
->tab_width
);
845 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
846 register struct Lisp_Char_Table
*dp
= window_display_table (win
);
848 = (INTEGERP (current_buffer
->selective_display
)
849 ? XINT (current_buffer
->selective_display
)
850 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
851 int prev_vpos
= vpos
, prev_hpos
= 0;
853 = (selective
&& dp
&& VECTORP (DISP_INVIS_VECTOR (dp
))
854 ? XVECTOR (DISP_INVIS_VECTOR (dp
))->size
: 0);
855 /* The next location where the `invisible' property changes, or an
856 overlay starts or ends. */
857 int next_boundary
= from
;
859 /* For computing runs of characters with similar widths.
860 Invariant: width_run_width is zero, or all the characters
861 from width_run_start to width_run_end have a fixed width of
863 int width_run_start
= from
;
864 int width_run_end
= from
;
865 int width_run_width
= 0;
866 Lisp_Object
*width_table
;
869 /* The next buffer pos where we should consult the width run cache. */
870 int next_width_run
= from
;
873 XSETBUFFER (buffer
, current_buffer
);
874 XSETWINDOW (window
, win
);
876 width_run_cache_on_off ();
877 if (dp
== buffer_display_table ())
878 width_table
= (VECTORP (current_buffer
->width_table
)
879 ? XVECTOR (current_buffer
->width_table
)->contents
882 /* If the window has its own display table, we can't use the width
883 run cache, because that's based on the buffer's display table. */
886 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
891 while (pos
== next_boundary
)
893 /* If the caller says that the screen position came from an earlier
894 call to compute_motion, then we've already accounted for the
895 overlay strings at point. This is only true the first time
896 through, so clear the flag after testing it. */
898 /* We need to skip past the overlay strings. Currently those
899 strings must contain single-column printing characters;
900 if we want to relax that restriction, something will have
901 to be changed here. */
902 hpos
+= overlay_strings (pos
, win
, (char **)0);
908 /* Advance POS past invisible characters
909 (but not necessarily all that there are here),
910 and store in next_boundary the next position where
911 we need to call skip_invisible. */
912 pos
= skip_invisible (pos
, &next_boundary
, to
, window
);
915 /* Handle right margin. */
918 || (pos
< ZV
&& FETCH_CHAR (pos
) != '\n')))
921 || (truncate_partial_width_windows
922 && width
+ 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win
))))
923 || !NILP (current_buffer
->truncate_lines
))
925 /* Truncating: skip to newline. */
926 pos
= find_before_next_newline (pos
, to
, 1);
928 /* If we just skipped next_boundary,
929 loop around in the main while
931 if (pos
>= next_boundary
)
932 next_boundary
= pos
+ 1;
937 vpos
+= hpos
/ width
;
938 tab_offset
+= hpos
- hpos
% width
;
943 /* Stop if past the target buffer position or screen position. */
946 if (vpos
> tovpos
|| (vpos
== tovpos
&& hpos
>= tohpos
))
952 /* Consult the width run cache to see if we can avoid inspecting
953 the text character-by-character. */
954 if (current_buffer
->width_run_cache
&& pos
>= next_width_run
)
958 = region_cache_forward (current_buffer
,
959 current_buffer
->width_run_cache
,
962 /* A width of zero means the character's width varies (like
963 a tab), is meaningless (like a newline), or we just don't
964 want to skip over it for some other reason. */
965 if (common_width
!= 0)
969 /* Don't go past the final buffer posn the user
974 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
976 /* Don't go past the final horizontal position the user
978 if (vpos
== tovpos
&& run_end_hpos
> tohpos
)
980 run_end
= pos
+ (tohpos
- hpos
) / common_width
;
981 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
984 /* Don't go past the margin. */
985 if (run_end_hpos
>= width
)
987 run_end
= pos
+ (width
- hpos
) / common_width
;
988 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
993 prev_hpos
= hpos
- common_width
;
997 next_width_run
= run_end
+ 1;
1000 /* We have to scan the text character-by-character. */
1003 c
= FETCH_CHAR (pos
);
1006 /* Perhaps add some info to the width_run_cache. */
1007 if (current_buffer
->width_run_cache
)
1009 /* Is this character part of the current run? If so, extend
1011 if (pos
- 1 == width_run_end
1012 && width_table
[c
] == width_run_width
)
1013 width_run_end
= pos
;
1015 /* The previous run is over, since this is a character at a
1016 different position, or a different width. */
1019 /* Have we accumulated a run to put in the cache?
1020 (Currently, we only cache runs of width == 1). */
1021 if (width_run_start
< width_run_end
1022 && width_run_width
== 1)
1023 know_region_cache (current_buffer
,
1024 current_buffer
->width_run_cache
,
1025 width_run_start
, width_run_end
);
1027 /* Start recording a new width run. */
1028 width_run_width
= width_table
[c
];
1029 width_run_start
= pos
- 1;
1030 width_run_end
= pos
;
1034 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
1035 hpos
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
1036 else if (c
>= 040 && c
< 0177)
1040 int tem
= (hpos
+ tab_offset
+ hscroll
- (hscroll
> 0)) % tab_width
;
1043 hpos
+= tab_width
- tem
;
1047 if (selective
> 0 && indented_beyond_p (pos
, selective
))
1049 /* Skip any number of invisible lines all at once */
1051 pos
= find_before_next_newline (pos
, to
, 1) + 1;
1053 && indented_beyond_p (pos
, selective
));
1054 /* Allow for the " ..." that is displayed for them. */
1057 hpos
+= selective_rlen
;
1062 /* We have skipped the invis text, but not the
1067 /* A visible line. */
1071 /* Count the truncation glyph on column 0 */
1077 else if (c
== CR
&& selective
< 0)
1079 /* In selective display mode,
1080 everything from a ^M to the end of the line is invisible.
1081 Stop *before* the real newline. */
1082 pos
= find_before_next_newline (pos
, to
, 1);
1083 /* If we just skipped next_boundary,
1084 loop around in the main while
1086 if (pos
> next_boundary
)
1087 next_boundary
= pos
;
1088 /* Allow for the " ..." that is displayed for them. */
1091 hpos
+= selective_rlen
;
1097 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
1101 /* Remember any final width run in the cache. */
1102 if (current_buffer
->width_run_cache
1103 && width_run_width
== 1
1104 && width_run_start
< width_run_end
)
1105 know_region_cache (current_buffer
, current_buffer
->width_run_cache
,
1106 width_run_start
, width_run_end
);
1108 val_compute_motion
.bufpos
= pos
;
1109 val_compute_motion
.hpos
= hpos
;
1110 val_compute_motion
.vpos
= vpos
;
1111 val_compute_motion
.prevhpos
= prev_hpos
;
1113 /* Nonzero if have just continued a line */
1114 val_compute_motion
.contin
1116 && (val_compute_motion
.vpos
!= prev_vpos
)
1119 return &val_compute_motion
;
1122 #if 0 /* The doc string is too long for some compilers,
1123 but make-docfile can find it in this comment. */
1124 DEFUN ("compute-motion", Ffoo
, Sfoo
, 7, 7, 0,
1125 "Scan through the current buffer, calculating screen position.\n\
1126 Scan the current buffer forward from offset FROM,\n\
1127 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1128 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1129 and return the ending buffer position and screen location.\n\
1131 There are three additional arguments:\n\
1133 WIDTH is the number of columns available to display text;\n\
1134 this affects handling of continuation lines.\n\
1135 This is usually the value returned by `window-width', less one (to allow\n\
1136 for the continuation glyph).\n\
1138 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1139 HSCROLL is the number of columns not being displayed at the left\n\
1140 margin; this is usually taken from a window's hscroll member.\n\
1141 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1142 being displayed, perhaps because the line was continued within it.\n\
1143 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1145 WINDOW is the window to operate on. It is used to choose the display table;\n\
1146 if it is showing the current buffer, it is used also for\n\
1147 deciding which overlay properties apply.\n\
1148 Note that `compute-motion' always operates on the current buffer.\n\
1150 The value is a list of five elements:\n\
1151 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1152 POS is the buffer position where the scan stopped.\n\
1153 VPOS is the vertical position where the scan stopped.\n\
1154 HPOS is the horizontal position where the scan stopped.\n\
1156 PREVHPOS is the horizontal position one character back from POS.\n\
1157 CONTIN is t if a line was continued after (or within) the previous character.\n\
1159 For example, to find the buffer position of column COL of line LINE\n\
1160 of a certain window, pass the window's starting location as FROM\n\
1161 and the window's upper-left coordinates as FROMPOS.\n\
1162 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1163 visible section of the buffer, and pass LINE and COL as TOPOS.")
1164 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1167 DEFUN ("compute-motion", Fcompute_motion
, Scompute_motion
, 7, 7, 0,
1169 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1170 Lisp_Object from
, frompos
, to
, topos
;
1171 Lisp_Object width
, offsets
, window
;
1173 Lisp_Object bufpos
, hpos
, vpos
, prevhpos
, contin
;
1174 struct position
*pos
;
1175 int hscroll
, tab_offset
;
1177 CHECK_NUMBER_COERCE_MARKER (from
, 0);
1178 CHECK_CONS (frompos
, 0);
1179 CHECK_NUMBER (XCONS (frompos
)->car
, 0);
1180 CHECK_NUMBER (XCONS (frompos
)->cdr
, 0);
1181 CHECK_NUMBER_COERCE_MARKER (to
, 0);
1182 CHECK_CONS (topos
, 0);
1183 CHECK_NUMBER (XCONS (topos
)->car
, 0);
1184 CHECK_NUMBER (XCONS (topos
)->cdr
, 0);
1185 CHECK_NUMBER (width
, 0);
1186 if (!NILP (offsets
))
1188 CHECK_CONS (offsets
, 0);
1189 CHECK_NUMBER (XCONS (offsets
)->car
, 0);
1190 CHECK_NUMBER (XCONS (offsets
)->cdr
, 0);
1191 hscroll
= XINT (XCONS (offsets
)->car
);
1192 tab_offset
= XINT (XCONS (offsets
)->cdr
);
1195 hscroll
= tab_offset
= 0;
1198 window
= Fselected_window ();
1200 CHECK_LIVE_WINDOW (window
, 0);
1202 pos
= compute_motion (XINT (from
), XINT (XCONS (frompos
)->cdr
),
1203 XINT (XCONS (frompos
)->car
), 0,
1204 XINT (to
), XINT (XCONS (topos
)->cdr
),
1205 XINT (XCONS (topos
)->car
),
1206 XINT (width
), hscroll
, tab_offset
,
1209 XSETFASTINT (bufpos
, pos
->bufpos
);
1210 XSETINT (hpos
, pos
->hpos
);
1211 XSETINT (vpos
, pos
->vpos
);
1212 XSETINT (prevhpos
, pos
->prevhpos
);
1214 return Fcons (bufpos
,
1218 Fcons (pos
->contin
? Qt
: Qnil
, Qnil
)))));
1222 /* Return the column of position POS in window W's buffer.
1223 The result is rounded down to a multiple of the internal width of W.
1224 This is the amount of indentation of position POS
1225 that is not visible in its horizontal position in the window. */
1228 pos_tab_offset (w
, pos
)
1234 int width
= window_internal_width (w
) - 1;
1236 if (pos
== BEGV
|| FETCH_CHAR (pos
- 1) == '\n')
1239 col
= current_column ();
1240 TEMP_SET_PT (opoint
);
1241 return col
- (col
% width
);
1245 /* Fvertical_motion and vmotion */
1246 struct position val_vmotion
;
1249 vmotion (from
, vtarget
, w
)
1250 register int from
, vtarget
;
1253 int width
= window_internal_width (w
) - 1;
1254 int hscroll
= XINT (w
->hscroll
);
1255 struct position pos
;
1256 /* vpos is cumulative vertical position, changed as from is changed */
1257 register int vpos
= 0;
1258 Lisp_Object prevline
;
1260 int lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
1262 = (INTEGERP (current_buffer
->selective_display
)
1263 ? XINT (current_buffer
->selective_display
)
1264 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1269 XSETWINDOW (window
, w
);
1271 /* The omission of the clause
1272 && marker_position (w->start) == BEG
1273 here is deliberate; I think we want to measure from the prompt
1274 position even if the minibuffer window has scrolled. */
1275 if (EQ (window
, minibuf_window
))
1277 if (minibuf_prompt_width
== 0 && STRINGP (minibuf_prompt
))
1278 minibuf_prompt_width
1279 = string_display_width (minibuf_prompt
, Qnil
, Qnil
);
1281 start_hpos
= minibuf_prompt_width
;
1284 if (vpos
>= vtarget
)
1286 /* To move upward, go a line at a time until
1287 we have gone at least far enough */
1291 while ((vpos
> vtarget
|| first
) && from
> BEGV
)
1293 Lisp_Object propval
;
1295 XSETFASTINT (prevline
, find_next_newline_no_quit (from
- 1, -1));
1296 while (XFASTINT (prevline
) > BEGV
1298 && indented_beyond_p (XFASTINT (prevline
), selective
))
1299 #ifdef USE_TEXT_PROPERTIES
1300 /* watch out for newlines with `invisible' property */
1301 || (propval
= Fget_char_property (prevline
,
1304 TEXT_PROP_MEANS_INVISIBLE (propval
))
1307 XSETFASTINT (prevline
,
1308 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1310 pos
= *compute_motion (XFASTINT (prevline
), 0,
1311 lmargin
+ (XFASTINT (prevline
) == BEG
1314 from
, 1 << (BITS_PER_INT
- 2), 0,
1315 width
, hscroll
, 0, w
);
1318 from
= XFASTINT (prevline
);
1321 /* If we made exactly the desired vertical distance,
1322 or if we hit beginning of buffer,
1323 return point found */
1324 if (vpos
>= vtarget
)
1326 val_vmotion
.bufpos
= from
;
1327 val_vmotion
.vpos
= vpos
;
1328 val_vmotion
.hpos
= lmargin
;
1329 val_vmotion
.contin
= 0;
1330 val_vmotion
.prevhpos
= 0;
1331 return &val_vmotion
;
1334 /* Otherwise find the correct spot by moving down */
1336 /* Moving downward is simple, but must calculate from beg of line
1337 to determine hpos of starting point */
1338 if (from
> BEGV
&& FETCH_CHAR (from
- 1) != '\n')
1340 Lisp_Object propval
;
1342 XSETFASTINT (prevline
, find_next_newline_no_quit (from
, -1));
1343 while (XFASTINT (prevline
) > BEGV
1345 && indented_beyond_p (XFASTINT (prevline
), selective
))
1346 #ifdef USE_TEXT_PROPERTIES
1347 /* watch out for newlines with `invisible' property */
1348 || (propval
= Fget_char_property (prevline
, Qinvisible
,
1350 TEXT_PROP_MEANS_INVISIBLE (propval
))
1353 XSETFASTINT (prevline
,
1354 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1356 pos
= *compute_motion (XFASTINT (prevline
), 0,
1357 lmargin
+ (XFASTINT (prevline
) == BEG
1360 from
, 1 << (BITS_PER_INT
- 2), 0,
1361 width
, hscroll
, 0, w
);
1366 pos
.hpos
= lmargin
+ (from
== BEG
? start_hpos
: 0);
1370 return compute_motion (from
, vpos
, pos
.hpos
, did_motion
,
1371 ZV
, vtarget
, - (1 << (BITS_PER_INT
- 2)),
1372 width
, hscroll
, pos
.vpos
* width
, w
);
1375 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 2, 0,
1376 "Move point to start of the screen line LINES lines down.\n\
1377 If LINES is negative, this means moving up.\n\
1379 This function is an ordinary cursor motion function\n\
1380 which calculates the new position based on how text would be displayed.\n\
1381 The new position may be the start of a line,\n\
1382 or just the start of a continuation line.\n\
1383 The function returns number of screen lines moved over;\n\
1384 that usually equals LINES, but may be closer to zero\n\
1385 if beginning or end of buffer was reached.\n\
1387 The optional second argument WINDOW specifies the window to use for\n\
1388 parameters such as width, horizontal scrolling, and so on.\n\
1389 The default is to use the selected window's parameters.\n\
1391 `vertical-motion' always uses the current buffer,\n\
1392 regardless of which buffer is displayed in WINDOW.\n\
1393 This is consistent with other cursor motion functions\n\
1394 and makes it possible to use `vertical-motion' in any buffer,\n\
1395 whether or not it is currently displayed in some window.")
1397 Lisp_Object lines
, window
;
1399 struct position pos
;
1401 CHECK_NUMBER (lines
, 0);
1402 if (! NILP (window
))
1403 CHECK_WINDOW (window
, 0);
1405 window
= selected_window
;
1407 pos
= *vmotion (PT
, (int) XINT (lines
), XWINDOW (window
));
1409 SET_PT (pos
.bufpos
);
1410 return make_number (pos
.vpos
);
1413 /* file's initialization. */
1417 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode
,
1418 "*Indentation can insert tabs if this is non-nil.\n\
1419 Setting this variable automatically makes it local to the current buffer.");
1420 indent_tabs_mode
= 1;
1422 defsubr (&Scurrent_indentation
);
1423 defsubr (&Sindent_to
);
1424 defsubr (&Scurrent_column
);
1425 defsubr (&Smove_to_column
);
1426 defsubr (&Svertical_motion
);
1427 defsubr (&Scompute_motion
);