1 /* Indentation functions.
2 Copyright (C) 1985,86,87,88,93,94,95,98 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. */
33 #include "intervals.h"
34 #include "region-cache.h"
36 /* Indentation can insert tabs if this is non-zero;
37 otherwise always uses spaces */
40 #define min(a, b) ((a) < (b) ? (a) : (b))
41 #define max(a, b) ((a) > (b) ? (a) : (b))
45 /* These three values memoize the current column to avoid recalculation */
46 /* Some things in set last_known_column_point to -1
47 to mark the memoized value as invalid */
48 /* Last value returned by current_column */
49 int last_known_column
;
50 /* Value of point when current_column was called */
51 int last_known_column_point
;
52 /* Value of MODIFF when current_column was called */
53 int last_known_column_modified
;
55 static int current_column_1 ();
56 static int position_indentation ();
58 /* Cache of beginning of line found by the last call of
60 int current_column_bol_cache
;
62 /* Get the display table to use for the current buffer. */
64 struct Lisp_Char_Table
*
65 buffer_display_table ()
69 thisbuf
= current_buffer
->display_table
;
70 if (DISP_TABLE_P (thisbuf
))
71 return XCHAR_TABLE (thisbuf
);
72 if (DISP_TABLE_P (Vstandard_display_table
))
73 return XCHAR_TABLE (Vstandard_display_table
);
77 /* Width run cache considerations. */
79 /* Return the width of character C under display table DP. */
82 character_width (c
, dp
)
84 struct Lisp_Char_Table
*dp
;
88 /* These width computations were determined by examining the cases
89 in display_text_line. */
91 /* Everything can be handled by the display table, if it's
92 present and the element is right. */
93 if (dp
&& (elt
= DISP_CHAR_VECTOR (dp
, c
), VECTORP (elt
)))
94 return XVECTOR (elt
)->size
;
96 /* Some characters are special. */
97 if (c
== '\n' || c
== '\t' || c
== '\015')
100 /* Printing characters have width 1. */
101 else if (c
>= 040 && c
< 0177)
104 /* Everybody else (control characters, metacharacters) has other
105 widths. We could return their actual widths here, but they
106 depend on things like ctl_arrow and crud like that, and they're
107 not very common at all. So we'll just claim we don't know their
113 /* Return true iff the display table DISPTAB specifies the same widths
114 for characters as WIDTHTAB. We use this to decide when to
115 invalidate the buffer's width_run_cache. */
117 disptab_matches_widthtab (disptab
, widthtab
)
118 struct Lisp_Char_Table
*disptab
;
119 struct Lisp_Vector
*widthtab
;
123 if (widthtab
->size
!= 256)
126 for (i
= 0; i
< 256; i
++)
127 if (character_width (i
, disptab
)
128 != XFASTINT (widthtab
->contents
[i
]))
134 /* Recompute BUF's width table, using the display table DISPTAB. */
136 recompute_width_table (buf
, disptab
)
138 struct Lisp_Char_Table
*disptab
;
141 struct Lisp_Vector
*widthtab
;
143 if (!VECTORP (buf
->width_table
))
144 buf
->width_table
= Fmake_vector (make_number (256), make_number (0));
145 widthtab
= XVECTOR (buf
->width_table
);
146 if (widthtab
->size
!= 256)
149 for (i
= 0; i
< 256; i
++)
150 XSETFASTINT (widthtab
->contents
[i
], character_width (i
, disptab
));
153 /* Allocate or free the width run cache, as requested by the current
154 state of current_buffer's cache_long_line_scans variable. */
156 width_run_cache_on_off ()
158 if (NILP (current_buffer
->cache_long_line_scans
)
159 /* And, for the moment, this feature doesn't work on multibyte
161 || !NILP (current_buffer
->enable_multibyte_characters
))
163 /* It should be off. */
164 if (current_buffer
->width_run_cache
)
166 free_region_cache (current_buffer
->width_run_cache
);
167 current_buffer
->width_run_cache
= 0;
168 current_buffer
->width_table
= Qnil
;
173 /* It should be on. */
174 if (current_buffer
->width_run_cache
== 0)
176 current_buffer
->width_run_cache
= new_region_cache ();
177 recompute_width_table (current_buffer
, buffer_display_table ());
183 /* Skip some invisible characters starting from POS.
184 This includes characters invisible because of text properties
185 and characters invisible because of overlays.
187 If position POS is followed by invisible characters,
188 skip some of them and return the position after them.
189 Otherwise return POS itself.
191 Set *NEXT_BOUNDARY_P to the next position at which
192 it will be necessary to call this function again.
194 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
195 to a value greater than TO.
197 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
198 take account of overlays that apply only in WINDOW.
200 We don't necessarily skip all the invisible characters after POS
201 because that could take a long time. We skip a reasonable number
202 which can be skipped quickly. If there might be more invisible
203 characters immediately following, then *NEXT_BOUNDARY_P
204 will equal the return value. */
207 skip_invisible (pos
, next_boundary_p
, to
, window
)
209 int *next_boundary_p
;
213 Lisp_Object prop
, position
, overlay_limit
, proplimit
;
217 XSETFASTINT (position
, pos
);
218 XSETBUFFER (buffer
, current_buffer
);
220 /* Give faster response for overlay lookup near POS. */
221 recenter_overlay_lists (current_buffer
, pos
);
223 /* We must not advance farther than the next overlay change.
224 The overlay change might change the invisible property;
225 or there might be overlay strings to be displayed there. */
226 overlay_limit
= Fnext_overlay_change (position
);
227 /* As for text properties, this gives a lower bound
228 for where the invisible text property could change. */
229 proplimit
= Fnext_property_change (position
, buffer
, Qt
);
230 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
231 proplimit
= overlay_limit
;
232 /* PROPLIMIT is now a lower bound for the next change
233 in invisible status. If that is plenty far away,
234 use that lower bound. */
235 if (XFASTINT (proplimit
) > pos
+ 100 || XFASTINT (proplimit
) >= to
)
236 *next_boundary_p
= XFASTINT (proplimit
);
237 /* Otherwise, scan for the next `invisible' property change. */
240 /* Don't scan terribly far. */
241 XSETFASTINT (proplimit
, min (pos
+ 100, to
));
242 /* No matter what. don't go past next overlay change. */
243 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
244 proplimit
= overlay_limit
;
245 end
= XFASTINT (Fnext_single_property_change (position
, Qinvisible
,
248 /* Don't put the boundary in the middle of multibyte form if
249 there is no actual property change. */
251 && !NILP (current_buffer
->enable_multibyte_characters
)
253 while (pos
< end
&& !CHAR_HEAD_P (POS_ADDR (end
)))
256 *next_boundary_p
= end
;
258 /* if the `invisible' property is set, we can skip to
259 the next property change */
260 if (!NILP (window
) && EQ (XWINDOW (window
)->buffer
, buffer
))
261 prop
= Fget_char_property (position
, Qinvisible
, window
);
263 prop
= Fget_char_property (position
, Qinvisible
, buffer
);
264 if (TEXT_PROP_MEANS_INVISIBLE (prop
))
265 return *next_boundary_p
;
269 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
271 C is *P which should satisfy `BASE_LEADING_CODE_P (c)'.
273 DP is a display table or NULL.
275 This macro is used in current_column_1, Fmove_to_column, and
278 #define MULTIBYTE_BYTES_WIDTH(p, c, dp) \
280 unsigned char *pend = p + 1; \
283 while (! CHAR_HEAD_P (*pend)) pend++; \
285 if (c == LEADING_CODE_COMPOSITION) \
287 int id = str_cmpchar_id (p, pend - p); \
288 int ch = MAKE_COMPOSITE_CHAR (id); \
292 bytes = cmpchar_table[id]->len; \
293 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, ch))) \
294 width = XVECTOR (DISP_CHAR_VECTOR (dp, ch))->size; \
296 width = cmpchar_table[id]->width; \
298 wide_column = width; \
308 bytes = BYTES_BY_CHAR_HEAD (c); \
309 if (bytes >= 2 && bytes <= pend - p) \
313 if (dp && (ch = STRING_CHAR (p, bytes), \
314 VECTORP (DISP_CHAR_VECTOR (dp, ch)))) \
315 width = XVECTOR (DISP_CHAR_VECTOR (dp, ch))->size; \
317 width = WIDTH_BY_CHAR_HEAD (c); \
319 wide_column = width; \
327 if (p + bytes < pend) \
329 width += 4 * (pend - (p + bytes)); \
334 DEFUN ("current-column", Fcurrent_column
, Scurrent_column
, 0, 0, 0,
335 "Return the horizontal position of point. Beginning of line is column 0.\n\
336 This is calculated by adding together the widths of all the displayed\n\
337 representations of the character between the start of the previous line\n\
338 and point. (eg control characters will have a width of 2 or 4, tabs\n\
339 will have a variable width)\n\
340 Ignores finite width of frame, which means that this function may return\n\
341 values greater than (frame-width).\n\
342 Whether the line is visible (if `selective-display' is t) has no effect;\n\
343 however, ^M is treated as end of line when `selective-display' is t.")
347 XSETFASTINT (temp
, current_column ());
351 /* Cancel any recorded value of the horizontal position. */
354 invalidate_current_column ()
356 last_known_column_point
= 0;
363 register unsigned char *ptr
, *stop
;
364 register int tab_seen
;
367 register int tab_width
= XINT (current_buffer
->tab_width
);
368 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
369 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
372 if (PT
== last_known_column_point
373 && MODIFF
== last_known_column_modified
)
374 return last_known_column
;
376 /* If the buffer has overlays, text properties,
377 or multibyte characters, use a more general algorithm. */
378 if (BUF_INTERVALS (current_buffer
)
379 || !NILP (current_buffer
->overlays_before
)
380 || !NILP (current_buffer
->overlays_after
)
382 return current_column_1 ();
384 /* Scan backwards from point to the previous newline,
385 counting width. Tab characters are the only complicated case. */
387 /* Make a pointer for decrementing through the chars before point. */
388 ptr
= BYTE_POS_ADDR (PT_BYTE
- 1) + 1;
389 /* Make a pointer to where consecutive chars leave off,
390 going backwards from point. */
393 else if (PT
<= GPT
|| BEGV
> GPT
)
398 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
400 col
= 0, tab_seen
= 0, post_tab
= 0;
406 /* We stopped either for the beginning of the buffer
408 if (ptr
== BEGV_ADDR
)
410 /* It was the gap. Jump back over it. */
413 /* Check whether that brings us to beginning of buffer. */
414 if (BEGV
>= GPT
) break;
418 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
419 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
420 else if (c
>= 040 && c
< 0177)
423 || (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
)))
431 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
438 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
443 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
447 if (ptr
== BEGV_ADDR
)
448 current_column_bol_cache
= BEGV
;
450 current_column_bol_cache
= BYTE_TO_CHAR (PTR_BYTE_POS (ptr
));
452 last_known_column
= col
;
453 last_known_column_point
= PT
;
454 last_known_column_modified
= MODIFF
;
459 /* Return the column number of position POS
460 by scanning forward from the beginning of the line.
461 This function handles characters that are invisible
462 due to text properties or overlays. */
467 register int tab_width
= XINT (current_buffer
->tab_width
);
468 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
469 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
470 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
472 /* Start the scan at the beginning of this line with column number 0. */
473 register int col
= 0;
475 int next_boundary
, next_boundary_byte
;
476 int opoint
= PT
, opoint_byte
= PT_BYTE
;
478 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
479 current_column_bol_cache
= PT
;
480 scan
= PT
, scan_byte
= PT_BYTE
;
481 SET_PT_BOTH (opoint
, opoint_byte
);
482 next_boundary
= scan
;
483 next_boundary_byte
= scan_byte
;
485 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
487 /* Scan forward to the target position. */
488 while (scan
< opoint
)
492 /* Occasionally we may need to skip invisible text. */
493 while (scan
== next_boundary
)
496 /* This updates NEXT_BOUNDARY to the next place
497 where we might need to skip more invisible text. */
498 scan
= skip_invisible (scan
, &next_boundary
, opoint
, Qnil
);
501 if (scan
!= old_scan
)
502 scan_byte
= CHAR_TO_BYTE (scan
);
503 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
506 c
= FETCH_BYTE (scan_byte
);
508 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
509 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
511 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
518 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
526 col
= col
/ tab_width
* tab_width
;
528 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
531 int bytes
, width
, wide_column
;
534 ptr
= BYTE_POS_ADDR (scan_byte
);
535 MULTIBYTE_BYTES_WIDTH (ptr
, c
, dp
);
539 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
541 else if (c
< 040 || c
>= 0177)
548 last_known_column
= col
;
549 last_known_column_point
= PT
;
550 last_known_column_modified
= MODIFF
;
555 /* Return the width in columns of the part of STRING from BEG to END.
556 If BEG is nil, that stands for the beginning of STRING.
557 If END is nil, that stands for the end of STRING. */
560 string_display_width (string
, beg
, end
)
561 Lisp_Object string
, beg
, end
;
564 register unsigned char *ptr
, *stop
;
565 register int tab_seen
;
568 register int tab_width
= XINT (current_buffer
->tab_width
);
569 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
570 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
574 e
= XSTRING (string
)->size
;
577 CHECK_NUMBER (end
, 0);
585 CHECK_NUMBER (beg
, 0);
589 /* Make a pointer for decrementing through the chars before point. */
590 ptr
= XSTRING (string
)->data
+ e
;
591 /* Make a pointer to where consecutive chars leave off,
592 going backwards from point. */
593 stop
= XSTRING (string
)->data
+ b
;
595 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
597 col
= 0, tab_seen
= 0, post_tab
= 0;
605 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
606 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
607 else if (c
>= 040 && c
< 0177)
614 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
621 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
626 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
633 DEFUN ("indent-to", Findent_to
, Sindent_to
, 1, 2, "NIndent to column: ",
634 "Indent from point with tabs and spaces until COLUMN is reached.\n\
635 Optional second argument MININUM says always do at least MININUM spaces\n\
636 even if that goes past COLUMN; by default, MININUM is zero.")
638 Lisp_Object column
, minimum
;
641 register int fromcol
;
642 register int tab_width
= XINT (current_buffer
->tab_width
);
644 CHECK_NUMBER (column
, 0);
646 XSETFASTINT (minimum
, 0);
647 CHECK_NUMBER (minimum
, 1);
649 fromcol
= current_column ();
650 mincol
= fromcol
+ XINT (minimum
);
651 if (mincol
< XINT (column
)) mincol
= XINT (column
);
653 if (fromcol
== mincol
)
654 return make_number (mincol
);
656 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
658 if (indent_tabs_mode
)
661 XSETFASTINT (n
, mincol
/ tab_width
- fromcol
/ tab_width
);
662 if (XFASTINT (n
) != 0)
664 Finsert_char (make_number ('\t'), n
, Qt
);
666 fromcol
= (mincol
/ tab_width
) * tab_width
;
670 XSETFASTINT (column
, mincol
- fromcol
);
671 Finsert_char (make_number (' '), column
, Qt
);
673 last_known_column
= mincol
;
674 last_known_column_point
= PT
;
675 last_known_column_modified
= MODIFF
;
677 XSETINT (column
, mincol
);
682 DEFUN ("current-indentation", Fcurrent_indentation
, Scurrent_indentation
,
684 "Return the indentation of the current line.\n\
685 This is the horizontal position of the character\n\
686 following any initial whitespace.")
690 int opoint
= PT
, opoint_byte
= PT_BYTE
;
692 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
694 XSETFASTINT (val
, position_indentation (PT_BYTE
));
695 SET_PT_BOTH (opoint
, opoint_byte
);
700 position_indentation (pos_byte
)
701 register int pos_byte
;
703 register int column
= 0;
704 register int tab_width
= XINT (current_buffer
->tab_width
);
705 register unsigned char *p
;
706 register unsigned char *stop
;
707 unsigned char *start
;
708 int next_boundary_byte
= pos_byte
;
709 int ceiling
= next_boundary_byte
;
711 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
713 p
= BYTE_POS_ADDR (pos_byte
);
714 /* STOP records the value of P at which we will need
715 to think about the gap, or about invisible text,
716 or about the end of the buffer. */
718 /* START records the starting value of P. */
726 /* If we have updated P, set POS_BYTE to match.
727 The first time we enter the loop, POS_BYTE is already right. */
729 pos_byte
= PTR_BYTE_POS (p
);
730 /* Consider the various reasons STOP might have been set here. */
731 if (pos_byte
== ZV_BYTE
)
733 if (pos_byte
== next_boundary_byte
)
736 int pos
= BYTE_TO_CHAR (pos_byte
);
737 pos
= skip_invisible (pos
, &next_boundary
, ZV
, Qnil
);
738 pos_byte
= CHAR_TO_BYTE (pos
);
739 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
741 if (pos_byte
>= ceiling
)
742 ceiling
= BUFFER_CEILING_OF (pos_byte
) + 1;
743 /* Compute the next place we need to stop and think,
744 and set STOP accordingly. */
745 stop_pos_byte
= min (ceiling
, next_boundary_byte
);
746 /* The -1 and +1 arrange to point at the first byte of gap
747 (if STOP_POS_BYTE is the position of the gap)
748 rather than at the data after the gap. */
750 stop
= BYTE_POS_ADDR (stop_pos_byte
- 1) + 1;
751 p
= BYTE_POS_ADDR (pos_byte
);
756 if (! NILP (current_buffer
->enable_multibyte_characters
))
762 column
+= tab_width
- column
% tab_width
;
765 if (ASCII_BYTE_P (p
[-1])
766 || NILP (current_buffer
->enable_multibyte_characters
))
770 pos_byte
= PTR_BYTE_POS (p
- 1);
771 c
= FETCH_MULTIBYTE_CHAR (pos_byte
);
772 if (CHAR_HAS_CATEGORY (c
, ' '))
776 p
= BYTE_POS_ADDR (pos_byte
);
785 /* Test whether the line beginning at POS is indented beyond COLUMN.
786 Blank lines are treated as if they had the same indentation as the
790 indented_beyond_p (pos
, pos_byte
, column
)
791 int pos
, pos_byte
, column
;
794 int opoint
= PT
, opoint_byte
= PT_BYTE
;
796 SET_PT_BOTH (pos
, pos_byte
);
797 while (PT
> BEGV
&& FETCH_BYTE (PT_BYTE
) == '\n')
798 scan_newline (PT
- 1, PT_BYTE
- 1, BEGV
, BEGV_BYTE
, -1, 0);
800 XSETFASTINT (val
, position_indentation (PT_BYTE
));
801 SET_PT_BOTH (opoint
, opoint_byte
);
805 DEFUN ("move-to-column", Fmove_to_column
, Smove_to_column
, 1, 2, "p",
806 "Move point to column COLUMN in the current line.\n\
807 The column of a character is calculated by adding together the widths\n\
808 as displayed of the previous characters in the line.\n\
809 This function ignores line-continuation;\n\
810 there is no upper limit on the column number a character can have\n\
811 and horizontal scrolling has no effect.\n\
813 If specified column is within a character, point goes after that character.\n\
814 If it's past end of line, point goes to end of line.\n\n\
815 A non-nil second (optional) argument FORCE means, if the line\n\
816 is too short to reach column COLUMN then add spaces/tabs to get there,\n\
817 and if COLUMN is in the middle of a tab character, change it to spaces.\n\
819 The return value is the current column.")
821 Lisp_Object column
, force
;
824 register int col
= current_column ();
827 register int tab_width
= XINT (current_buffer
->tab_width
);
828 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
829 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
830 register int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
837 int pos_byte
, end_byte
, next_boundary_byte
;
839 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
840 CHECK_NATNUM (column
, 0);
841 goal
= XINT (column
);
848 next_boundary_byte
= PT_BYTE
;
850 /* If we're starting past the desired column,
851 back up to beginning of line and scan from there. */
855 pos
= current_column_bol_cache
;
856 pos_byte
= CHAR_TO_BYTE (pos
);
862 while (pos
== next_boundary
)
865 pos
= skip_invisible (pos
, &next_boundary
, end
, Qnil
);
867 pos_byte
= CHAR_TO_BYTE (pos
);
868 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
873 /* Test reaching the goal column. We do this after skipping
874 invisible characters, so that we put point before the
875 character on which the cursor will appear. */
879 c
= FETCH_BYTE (pos_byte
);
881 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
882 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
884 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
891 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
899 col
= col
/ tab_width
* tab_width
;
901 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
903 else if (c
< 040 || c
== 0177)
907 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
909 /* Start of multi-byte form. */
911 int bytes
, width
, wide_column
;
914 ptr
= BYTE_POS_ADDR (pos_byte
);
915 MULTIBYTE_BYTES_WIDTH (ptr
, c
, dp
);
924 SET_PT_BOTH (pos
, pos_byte
);
926 /* If a tab char made us overshoot, change it to spaces
927 and scan through it again. */
928 if (!NILP (force
) && col
> goal
&& c
== '\t' && prev_col
< goal
)
930 int old_point
, old_point_byte
;
932 del_range (PT
- 1, PT
);
933 Findent_to (make_number (goal
), Qnil
);
935 old_point_byte
= PT_BYTE
;
936 Findent_to (make_number (col
), Qnil
);
937 SET_PT_BOTH (old_point
, old_point_byte
);
938 /* Set the last_known... vars consistently. */
942 /* If line ends prematurely, add space to the end. */
943 if (col
< goal
&& !NILP (force
))
944 Findent_to (make_number (col
= goal
), Qnil
);
946 last_known_column
= col
;
947 last_known_column_point
= PT
;
948 last_known_column_modified
= MODIFF
;
950 XSETFASTINT (val
, col
);
954 /* compute_motion: compute buffer posn given screen posn and vice versa */
956 struct position val_compute_motion
;
958 /* Scan the current buffer forward from offset FROM, pretending that
959 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
960 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
961 and return the ending buffer position and screen location. If we
962 can't hit the requested column exactly (because of a tab or other
963 multi-column character), overshoot.
965 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
966 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
967 earlier call to compute_motion. The other common case is that FROMHPOS
968 is zero and FROM is a position that "belongs" at column zero, but might
969 be shifted by overlay strings; in this case DID_MOTION should be 0.
971 WIDTH is the number of columns available to display text;
972 compute_motion uses this to handle continuation lines and such.
973 HSCROLL is the number of columns not being displayed at the left
974 margin; this is usually taken from a window's hscroll member.
975 TAB_OFFSET is the number of columns of the first tab that aren't
976 being displayed, perhaps because of a continuation line or
979 compute_motion returns a pointer to a struct position. The bufpos
980 member gives the buffer position at the end of the scan, and hpos
981 and vpos give its cartesian location. prevhpos is the column at
982 which the character before bufpos started, and contin is non-zero
983 if we reached the current line by continuing the previous.
985 Note that FROMHPOS and TOHPOS should be expressed in real screen
986 columns, taking HSCROLL and the truncation glyph at the left margin
987 into account. That is, beginning-of-line moves you to the hpos
988 -HSCROLL + (HSCROLL > 0).
990 For example, to find the buffer position of column COL of line LINE
991 of a certain window, pass the window's starting location as FROM
992 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
993 Pass the buffer's ZV as TO, to limit the scan to the end of the
994 visible section of the buffer, and pass LINE and COL as TOVPOS and
997 When displaying in window w, a typical formula for WIDTH is:
1000 - (has_vertical_scroll_bars
1001 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
1002 : (window_width + window_left != frame_width))
1005 window_width is XFASTINT (w->width),
1006 window_left is XFASTINT (w->left),
1007 has_vertical_scroll_bars is
1008 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
1009 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
1011 Or you can let window_internal_width do this all for you, and write:
1012 window_internal_width (w) - 1
1014 The `-1' accounts for the continuation-line backslashes; the rest
1015 accounts for window borders if the window is split horizontally, and
1016 the scroll bars if they are turned on. */
1019 compute_motion (from
, fromvpos
, fromhpos
, did_motion
, to
, tovpos
, tohpos
, width
, hscroll
, tab_offset
, win
)
1020 int from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
;
1023 int hscroll
, tab_offset
;
1026 register int hpos
= fromhpos
;
1027 register int vpos
= fromvpos
;
1032 register int tab_width
= XFASTINT (current_buffer
->tab_width
);
1033 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
1034 register struct Lisp_Char_Table
*dp
= window_display_table (win
);
1036 = (INTEGERP (current_buffer
->selective_display
)
1037 ? XINT (current_buffer
->selective_display
)
1038 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1041 = (selective
&& dp
&& VECTORP (DISP_INVIS_VECTOR (dp
))
1042 ? XVECTOR (DISP_INVIS_VECTOR (dp
))->size
: 0);
1043 /* The next location where the `invisible' property changes, or an
1044 overlay starts or ends. */
1045 int next_boundary
= from
;
1047 /* For computing runs of characters with similar widths.
1048 Invariant: width_run_width is zero, or all the characters
1049 from width_run_start to width_run_end have a fixed width of
1051 int width_run_start
= from
;
1052 int width_run_end
= from
;
1053 int width_run_width
= 0;
1054 Lisp_Object
*width_table
;
1057 /* The next buffer pos where we should consult the width run cache. */
1058 int next_width_run
= from
;
1061 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1062 int wide_column_end_hpos
= 0; /* Horizontal position at the end of
1063 last wide-column character. */
1064 int prev_pos
; /* Previous buffer position. */
1065 int prev_pos_byte
; /* Previous buffer position. */
1066 int contin_hpos
; /* HPOS of last column of continued line. */
1067 int prev_tab_offset
; /* Previous tab offset. */
1069 XSETBUFFER (buffer
, current_buffer
);
1070 XSETWINDOW (window
, win
);
1072 width_run_cache_on_off ();
1073 if (dp
== buffer_display_table ())
1074 width_table
= (VECTORP (current_buffer
->width_table
)
1075 ? XVECTOR (current_buffer
->width_table
)->contents
1078 /* If the window has its own display table, we can't use the width
1079 run cache, because that's based on the buffer's display table. */
1082 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
1084 pos
= prev_pos
= from
;
1085 pos_byte
= prev_pos_byte
= CHAR_TO_BYTE (from
);
1087 prev_tab_offset
= tab_offset
;
1090 while (pos
== next_boundary
)
1095 /* If the caller says that the screen position came from an earlier
1096 call to compute_motion, then we've already accounted for the
1097 overlay strings at point. This is only true the first time
1098 through, so clear the flag after testing it. */
1100 /* We need to skip past the overlay strings. Currently those
1101 strings must not contain TAB;
1102 if we want to relax that restriction, something will have
1103 to be changed here. */
1105 unsigned char *ovstr
;
1106 int ovlen
= overlay_strings (pos
, win
, &ovstr
);
1107 hpos
+= ((multibyte
&& ovlen
> 0)
1108 ? strwidth (ovstr
, ovlen
) : ovlen
);
1115 /* Advance POS past invisible characters
1116 (but not necessarily all that there are here),
1117 and store in next_boundary the next position where
1118 we need to call skip_invisible. */
1119 newpos
= skip_invisible (pos
, &next_boundary
, to
, window
);
1124 if (newpos
!= pos_here
)
1127 pos_byte
= CHAR_TO_BYTE (pos
);
1131 /* Handle right margin. */
1132 /* Note on a wide-column character.
1134 Characters are classified into the following three categories
1135 according to the width (columns occupied on screen).
1137 (1) single-column character: ex. `a'
1138 (2) multi-column character: ex. `^A', TAB, `\033'
1139 (3) wide-column character: ex. Japanese character, Chinese character
1140 (In the following example, `W_' stands for them.)
1142 Multi-column characters can be divided around the right margin,
1143 but wide-column characters cannot.
1147 (*) The cursor is placed on the next character after the point.
1151 j ^---- next after the point
1152 ^--- next char. after the point.
1154 In case of sigle-column character
1158 033 ^---- next after the point, next char. after the point.
1160 In case of multi-column character
1164 W_ ^---- next after the point
1165 ^---- next char. after the point.
1167 In case of wide-column character
1169 The problem here is continuation at a wide-column character.
1170 In this case, the line may shorter less than WIDTH.
1171 And we find the continuation AFTER it occurs.
1178 || (truncate_partial_width_windows
1179 && width
+ 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win
))))
1180 || !NILP (current_buffer
->truncate_lines
))
1182 /* Truncating: skip to newline, unless we are already past
1183 TO (we need to go back below). */
1186 pos
= find_before_next_newline (pos
, to
, 1);
1187 pos_byte
= CHAR_TO_BYTE (pos
);
1189 /* If we just skipped next_boundary,
1190 loop around in the main while
1192 if (pos
>= next_boundary
)
1193 next_boundary
= pos
+ 1;
1195 prev_tab_offset
= tab_offset
;
1201 /* Remember the previous value. */
1202 prev_tab_offset
= tab_offset
;
1204 if (wide_column_end_hpos
> width
)
1207 tab_offset
+= prev_hpos
;
1211 tab_offset
+= width
;
1215 contin_hpos
= prev_hpos
;
1220 /* Stop if past the target buffer position or screen position. */
1223 /* Go back to the previous position. */
1225 pos_byte
= prev_pos_byte
;
1227 tab_offset
= prev_tab_offset
;
1229 /* NOTE on contin_hpos, hpos, and prev_hpos.
1233 W_ ^---- contin_hpos
1239 if (contin_hpos
&& prev_hpos
== 0
1240 && contin_hpos
< width
&& !wide_column_end_hpos
)
1242 /* Line breaking occurs in the middle of multi-column
1243 character. Go back to previous line. */
1248 /* If previous character is NEWLINE,
1249 set VPOS back to previous line */
1254 if (vpos
> tovpos
|| vpos
== tovpos
&& hpos
>= tohpos
)
1256 if (contin_hpos
&& prev_hpos
== 0
1257 && ((hpos
> tohpos
&& contin_hpos
== width
)
1258 || (wide_column_end_hpos
> width
)))
1259 { /* Line breaks because we can't put the character at the
1260 previous line any more. It is not the multi-column
1261 character continued in middle. Go back to previous
1262 buffer position, screen position, and set tab offset
1263 to previous value. It's the beginning of the
1266 pos_byte
= prev_pos_byte
;
1268 tab_offset
= prev_tab_offset
;
1272 if (pos
== ZV
) /* We cannot go beyond ZV. Stop here. */
1277 prev_pos_byte
= pos_byte
;
1278 wide_column_end_hpos
= 0;
1280 /* Consult the width run cache to see if we can avoid inspecting
1281 the text character-by-character. */
1282 if (current_buffer
->width_run_cache
&& pos
>= next_width_run
)
1286 = region_cache_forward (current_buffer
,
1287 current_buffer
->width_run_cache
,
1290 /* A width of zero means the character's width varies (like
1291 a tab), is meaningless (like a newline), or we just don't
1292 want to skip over it for some other reason. */
1293 if (common_width
!= 0)
1297 /* Don't go past the final buffer posn the user
1302 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1304 /* Don't go past the final horizontal position the user
1306 if (vpos
== tovpos
&& run_end_hpos
> tohpos
)
1308 run_end
= pos
+ (tohpos
- hpos
) / common_width
;
1309 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1312 /* Don't go past the margin. */
1313 if (run_end_hpos
>= width
)
1315 run_end
= pos
+ (width
- hpos
) / common_width
;
1316 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1319 hpos
= run_end_hpos
;
1321 prev_hpos
= hpos
- common_width
;
1325 pos_byte
= CHAR_TO_BYTE (pos
);
1329 next_width_run
= run_end
+ 1;
1332 /* We have to scan the text character-by-character. */
1335 c
= FETCH_BYTE (pos_byte
);
1338 /* Perhaps add some info to the width_run_cache. */
1339 if (current_buffer
->width_run_cache
)
1341 /* Is this character part of the current run? If so, extend
1343 if (pos
- 1 == width_run_end
1344 && XFASTINT (width_table
[c
]) == width_run_width
)
1345 width_run_end
= pos
;
1347 /* The previous run is over, since this is a character at a
1348 different position, or a different width. */
1351 /* Have we accumulated a run to put in the cache?
1352 (Currently, we only cache runs of width == 1). */
1353 if (width_run_start
< width_run_end
1354 && width_run_width
== 1)
1355 know_region_cache (current_buffer
,
1356 current_buffer
->width_run_cache
,
1357 width_run_start
, width_run_end
);
1359 /* Start recording a new width run. */
1360 width_run_width
= XFASTINT (width_table
[c
]);
1361 width_run_start
= pos
- 1;
1362 width_run_end
= pos
;
1367 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
1368 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
1369 hpos
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
1370 else if (c
>= 040 && c
< 0177)
1374 int tem
= (hpos
+ tab_offset
+ hscroll
- (hscroll
> 0)) % tab_width
;
1377 hpos
+= tab_width
- tem
;
1382 && indented_beyond_p (pos
, pos_byte
, selective
))
1384 /* If (pos == to), we don't have to take care of
1385 selective display. */
1388 /* Skip any number of invisible lines all at once */
1391 pos
= find_before_next_newline (pos
, to
, 1) + 1;
1392 pos_byte
= CHAR_TO_BYTE (pos
);
1395 && indented_beyond_p (pos
, pos_byte
, selective
));
1396 /* Allow for the " ..." that is displayed for them. */
1399 hpos
+= selective_rlen
;
1403 DEC_BOTH (pos
, pos_byte
);
1404 /* We have skipped the invis text, but not the
1410 /* A visible line. */
1414 /* Count the truncation glyph on column 0 */
1421 else if (c
== CR
&& selective
< 0)
1423 /* In selective display mode,
1424 everything from a ^M to the end of the line is invisible.
1425 Stop *before* the real newline. */
1428 pos
= find_before_next_newline (pos
, to
, 1);
1429 pos_byte
= CHAR_TO_BYTE (pos
);
1431 /* If we just skipped next_boundary,
1432 loop around in the main while
1434 if (pos
> next_boundary
)
1435 next_boundary
= pos
;
1436 /* Allow for the " ..." that is displayed for them. */
1439 hpos
+= selective_rlen
;
1444 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
1446 /* Start of multi-byte form. */
1448 int bytes
, width
, wide_column
;
1450 pos_byte
--; /* rewind POS_BYTE */
1451 ptr
= BYTE_POS_ADDR (pos_byte
);
1452 MULTIBYTE_BYTES_WIDTH (ptr
, c
, dp
);
1455 wide_column_end_hpos
= hpos
+ wide_column
;
1459 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
1465 /* Remember any final width run in the cache. */
1466 if (current_buffer
->width_run_cache
1467 && width_run_width
== 1
1468 && width_run_start
< width_run_end
)
1469 know_region_cache (current_buffer
, current_buffer
->width_run_cache
,
1470 width_run_start
, width_run_end
);
1472 val_compute_motion
.bufpos
= pos
;
1473 val_compute_motion
.bytepos
= pos_byte
;
1474 val_compute_motion
.hpos
= hpos
;
1475 val_compute_motion
.vpos
= vpos
;
1476 if (contin_hpos
&& prev_hpos
== 0)
1477 val_compute_motion
.prevhpos
= contin_hpos
;
1479 val_compute_motion
.prevhpos
= prev_hpos
;
1480 /* We alalways handle all of them here; none of them remain to do. */
1481 val_compute_motion
.ovstring_chars_done
= 0;
1483 /* Nonzero if have just continued a line */
1484 val_compute_motion
.contin
= (contin_hpos
&& prev_hpos
== 0);
1486 return &val_compute_motion
;
1489 #if 0 /* The doc string is too long for some compilers,
1490 but make-docfile can find it in this comment. */
1491 DEFUN ("compute-motion", Ffoo
, Sfoo
, 7, 7, 0,
1492 "Scan through the current buffer, calculating screen position.\n\
1493 Scan the current buffer forward from offset FROM,\n\
1494 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1495 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1496 and return the ending buffer position and screen location.\n\
1498 There are three additional arguments:\n\
1500 WIDTH is the number of columns available to display text;\n\
1501 this affects handling of continuation lines.\n\
1502 This is usually the value returned by `window-width', less one (to allow\n\
1503 for the continuation glyph).\n\
1505 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1506 HSCROLL is the number of columns not being displayed at the left\n\
1507 margin; this is usually taken from a window's hscroll member.\n\
1508 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1509 being displayed, perhaps because the line was continued within it.\n\
1510 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1512 WINDOW is the window to operate on. It is used to choose the display table;\n\
1513 if it is showing the current buffer, it is used also for\n\
1514 deciding which overlay properties apply.\n\
1515 Note that `compute-motion' always operates on the current buffer.\n\
1517 The value is a list of five elements:\n\
1518 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1519 POS is the buffer position where the scan stopped.\n\
1520 VPOS is the vertical position where the scan stopped.\n\
1521 HPOS is the horizontal position where the scan stopped.\n\
1523 PREVHPOS is the horizontal position one character back from POS.\n\
1524 CONTIN is t if a line was continued after (or within) the previous character.\n\
1526 For example, to find the buffer position of column COL of line LINE\n\
1527 of a certain window, pass the window's starting location as FROM\n\
1528 and the window's upper-left coordinates as FROMPOS.\n\
1529 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1530 visible section of the buffer, and pass LINE and COL as TOPOS.")
1531 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1534 DEFUN ("compute-motion", Fcompute_motion
, Scompute_motion
, 7, 7, 0,
1536 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1537 Lisp_Object from
, frompos
, to
, topos
;
1538 Lisp_Object width
, offsets
, window
;
1540 Lisp_Object bufpos
, hpos
, vpos
, prevhpos
, contin
;
1541 struct position
*pos
;
1542 int hscroll
, tab_offset
;
1544 CHECK_NUMBER_COERCE_MARKER (from
, 0);
1545 CHECK_CONS (frompos
, 0);
1546 CHECK_NUMBER (XCONS (frompos
)->car
, 0);
1547 CHECK_NUMBER (XCONS (frompos
)->cdr
, 0);
1548 CHECK_NUMBER_COERCE_MARKER (to
, 0);
1549 CHECK_CONS (topos
, 0);
1550 CHECK_NUMBER (XCONS (topos
)->car
, 0);
1551 CHECK_NUMBER (XCONS (topos
)->cdr
, 0);
1552 CHECK_NUMBER (width
, 0);
1553 if (!NILP (offsets
))
1555 CHECK_CONS (offsets
, 0);
1556 CHECK_NUMBER (XCONS (offsets
)->car
, 0);
1557 CHECK_NUMBER (XCONS (offsets
)->cdr
, 0);
1558 hscroll
= XINT (XCONS (offsets
)->car
);
1559 tab_offset
= XINT (XCONS (offsets
)->cdr
);
1562 hscroll
= tab_offset
= 0;
1565 window
= Fselected_window ();
1567 CHECK_LIVE_WINDOW (window
, 0);
1569 if (XINT (from
) < BEGV
|| XINT (from
) > ZV
)
1570 args_out_of_range_3 (from
, make_number (BEGV
), make_number (ZV
));
1571 if (XINT (to
) < BEGV
|| XINT (to
) > ZV
)
1572 args_out_of_range_3 (to
, make_number (BEGV
), make_number (ZV
));
1574 pos
= compute_motion (XINT (from
), XINT (XCONS (frompos
)->cdr
),
1575 XINT (XCONS (frompos
)->car
), 0,
1576 XINT (to
), XINT (XCONS (topos
)->cdr
),
1577 XINT (XCONS (topos
)->car
),
1578 XINT (width
), hscroll
, tab_offset
,
1581 XSETFASTINT (bufpos
, pos
->bufpos
);
1582 XSETINT (hpos
, pos
->hpos
);
1583 XSETINT (vpos
, pos
->vpos
);
1584 XSETINT (prevhpos
, pos
->prevhpos
);
1586 return Fcons (bufpos
,
1590 Fcons (pos
->contin
? Qt
: Qnil
, Qnil
)))));
1594 /* Fvertical_motion and vmotion */
1595 struct position val_vmotion
;
1598 vmotion (from
, vtarget
, w
)
1599 register int from
, vtarget
;
1602 int width
= window_internal_width (w
) - 1;
1603 int hscroll
= XINT (w
->hscroll
);
1604 struct position pos
;
1605 /* vpos is cumulative vertical position, changed as from is changed */
1606 register int vpos
= 0;
1607 Lisp_Object prevline
;
1610 int lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
1612 = (INTEGERP (current_buffer
->selective_display
)
1613 ? XINT (current_buffer
->selective_display
)
1614 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1619 XSETWINDOW (window
, w
);
1621 /* The omission of the clause
1622 && marker_position (w->start) == BEG
1623 here is deliberate; I think we want to measure from the prompt
1624 position even if the minibuffer window has scrolled. */
1625 if (EQ (window
, minibuf_window
))
1627 if (minibuf_prompt_width
== 0 && STRINGP (minibuf_prompt
))
1628 minibuf_prompt_width
1629 = string_display_width (minibuf_prompt
, Qnil
, Qnil
);
1631 start_hpos
= minibuf_prompt_width
;
1634 if (vpos
>= vtarget
)
1636 /* To move upward, go a line at a time until
1637 we have gone at least far enough. */
1641 while ((vpos
> vtarget
|| first
) && from
> BEGV
)
1643 Lisp_Object propval
;
1645 XSETFASTINT (prevline
, find_next_newline_no_quit (from
- 1, -1));
1646 while (XFASTINT (prevline
) > BEGV
1648 && indented_beyond_p (XFASTINT (prevline
),
1649 CHAR_TO_BYTE (XFASTINT (prevline
)),
1651 #ifdef USE_TEXT_PROPERTIES
1652 /* watch out for newlines with `invisible' property */
1653 || (propval
= Fget_char_property (prevline
,
1656 TEXT_PROP_MEANS_INVISIBLE (propval
))
1659 XSETFASTINT (prevline
,
1660 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1662 pos
= *compute_motion (XFASTINT (prevline
), 0,
1663 lmargin
+ (XFASTINT (prevline
) == BEG
1667 /* Don't care for VPOS... */
1668 1 << (BITS_PER_SHORT
- 1),
1670 1 << (BITS_PER_SHORT
- 1),
1672 /* This compensates for start_hpos
1673 so that a tab as first character
1674 still occupies 8 columns. */
1675 (XFASTINT (prevline
) == BEG
1680 from
= XFASTINT (prevline
);
1683 /* If we made exactly the desired vertical distance,
1684 or if we hit beginning of buffer,
1685 return point found */
1686 if (vpos
>= vtarget
)
1688 val_vmotion
.bufpos
= from
;
1689 val_vmotion
.bytepos
= CHAR_TO_BYTE (from
);
1690 val_vmotion
.vpos
= vpos
;
1691 val_vmotion
.hpos
= lmargin
;
1692 val_vmotion
.contin
= 0;
1693 val_vmotion
.prevhpos
= 0;
1694 val_vmotion
.ovstring_chars_done
= 0;
1695 val_vmotion
.tab_offset
= 0; /* For accumulating tab offset. */
1696 return &val_vmotion
;
1699 /* Otherwise find the correct spot by moving down */
1701 /* Moving downward is simple, but must calculate from beg of line
1702 to determine hpos of starting point */
1703 from_byte
= CHAR_TO_BYTE (from
);
1704 if (from
> BEGV
&& FETCH_BYTE (from_byte
- 1) != '\n')
1706 Lisp_Object propval
;
1708 XSETFASTINT (prevline
, find_next_newline_no_quit (from
, -1));
1709 while (XFASTINT (prevline
) > BEGV
1711 && indented_beyond_p (XFASTINT (prevline
),
1712 CHAR_TO_BYTE (XFASTINT (prevline
)),
1714 #ifdef USE_TEXT_PROPERTIES
1715 /* watch out for newlines with `invisible' property */
1716 || (propval
= Fget_char_property (prevline
, Qinvisible
,
1718 TEXT_PROP_MEANS_INVISIBLE (propval
))
1721 XSETFASTINT (prevline
,
1722 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1724 pos
= *compute_motion (XFASTINT (prevline
), 0,
1725 lmargin
+ (XFASTINT (prevline
) == BEG
1729 /* Don't care for VPOS... */
1730 1 << (BITS_PER_SHORT
- 1),
1732 1 << (BITS_PER_SHORT
- 1),
1734 (XFASTINT (prevline
) == BEG
? -start_hpos
: 0),
1740 pos
.hpos
= lmargin
+ (from
== BEG
? start_hpos
: 0);
1745 return compute_motion (from
, vpos
, pos
.hpos
, did_motion
,
1746 ZV
, vtarget
, - (1 << (BITS_PER_SHORT
- 1)),
1748 pos
.tab_offset
- (from
== BEG
? start_hpos
: 0),
1752 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 2, 0,
1753 "Move point to start of the screen line LINES lines down.\n\
1754 If LINES is negative, this means moving up.\n\
1756 This function is an ordinary cursor motion function\n\
1757 which calculates the new position based on how text would be displayed.\n\
1758 The new position may be the start of a line,\n\
1759 or just the start of a continuation line.\n\
1760 The function returns number of screen lines moved over;\n\
1761 that usually equals LINES, but may be closer to zero\n\
1762 if beginning or end of buffer was reached.\n\
1764 The optional second argument WINDOW specifies the window to use for\n\
1765 parameters such as width, horizontal scrolling, and so on.\n\
1766 The default is to use the selected window's parameters.\n\
1768 `vertical-motion' always uses the current buffer,\n\
1769 regardless of which buffer is displayed in WINDOW.\n\
1770 This is consistent with other cursor motion functions\n\
1771 and makes it possible to use `vertical-motion' in any buffer,\n\
1772 whether or not it is currently displayed in some window.")
1774 Lisp_Object lines
, window
;
1776 struct position pos
;
1778 CHECK_NUMBER (lines
, 0);
1779 if (! NILP (window
))
1780 CHECK_WINDOW (window
, 0);
1782 window
= selected_window
;
1784 pos
= *vmotion (PT
, (int) XINT (lines
), XWINDOW (window
));
1786 SET_PT (pos
.bufpos
);
1787 return make_number (pos
.vpos
);
1790 /* file's initialization. */
1794 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode
,
1795 "*Indentation can insert tabs if this is non-nil.\n\
1796 Setting this variable automatically makes it local to the current buffer.");
1797 indent_tabs_mode
= 1;
1799 defsubr (&Scurrent_indentation
);
1800 defsubr (&Sindent_to
);
1801 defsubr (&Scurrent_column
);
1802 defsubr (&Smove_to_column
);
1803 defsubr (&Svertical_motion
);
1804 defsubr (&Scompute_motion
);