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. */
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 DEFUN ("current-column", Fcurrent_column
, Scurrent_column
, 0, 0, 0,
270 "Return the horizontal position of point. Beginning of line is column 0.\n\
271 This is calculated by adding together the widths of all the displayed\n\
272 representations of the character between the start of the previous line\n\
273 and point. (eg control characters will have a width of 2 or 4, tabs\n\
274 will have a variable width)\n\
275 Ignores finite width of frame, which means that this function may return\n\
276 values greater than (frame-width).\n\
277 Whether the line is visible (if `selective-display' is t) has no effect;\n\
278 however, ^M is treated as end of line when `selective-display' is t.")
282 XSETFASTINT (temp
, current_column ());
286 /* Cancel any recorded value of the horizontal position. */
289 invalidate_current_column ()
291 last_known_column_point
= 0;
298 register unsigned char *ptr
, *stop
;
299 register int tab_seen
;
302 register int tab_width
= XINT (current_buffer
->tab_width
);
303 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
304 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
307 if (PT
== last_known_column_point
308 && MODIFF
== last_known_column_modified
)
309 return last_known_column
;
311 /* If the buffer has overlays, text properties, or multibyte,
312 use a more general algorithm. */
313 if (BUF_INTERVALS (current_buffer
)
314 || !NILP (current_buffer
->overlays_before
)
315 || !NILP (current_buffer
->overlays_after
)
316 || !NILP (current_buffer
->enable_multibyte_characters
))
317 return current_column_1 (PT
);
319 /* Scan backwards from point to the previous newline,
320 counting width. Tab characters are the only complicated case. */
322 /* Make a pointer for decrementing through the chars before point. */
323 ptr
= BYTE_POS_ADDR (PT_BYTE
- 1) + 1;
324 /* Make a pointer to where consecutive chars leave off,
325 going backwards from point. */
328 else if (PT
<= GPT
|| BEGV
> GPT
)
333 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
335 col
= 0, tab_seen
= 0, post_tab
= 0;
341 /* We stopped either for the beginning of the buffer
343 if (ptr
== BEGV_ADDR
)
345 /* It was the gap. Jump back over it. */
348 /* Check whether that brings us to beginning of buffer. */
349 if (BEGV
>= GPT
) break;
353 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
354 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
355 else if (c
>= 040 && c
< 0177)
358 || (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
)))
366 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
373 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
378 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
382 if (ptr
== BEGV_ADDR
)
383 current_column_bol_cache
= BEGV
;
385 current_column_bol_cache
= BYTE_TO_CHAR (PTR_BYTE_POS (ptr
));
387 last_known_column
= col
;
388 last_known_column_point
= PT
;
389 last_known_column_modified
= MODIFF
;
394 /* Return the column number of position POS
395 by scanning forward from the beginning of the line.
396 This function handles characters that are invisible
397 due to text properties or overlays. */
400 current_column_1 (pos
)
403 register int tab_width
= XINT (current_buffer
->tab_width
);
404 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
405 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
406 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
408 /* Start the scan at the beginning of this line with column number 0. */
409 register int col
= 0;
411 int next_boundary
, next_boundary_byte
;
412 int opoint
= PT
, opoint_byte
= PT_BYTE
;
414 scan_newline (pos
, CHAR_TO_BYTE (pos
), BEGV
, BEGV_BYTE
, -1, 1);
415 current_column_bol_cache
= PT
;
416 scan
= PT
, scan_byte
= PT_BYTE
;
417 SET_PT_BOTH (opoint
, opoint_byte
);
418 next_boundary
= scan
;
419 next_boundary_byte
= scan_byte
;
421 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
423 /* Scan forward to the target position. */
428 /* Occasionally we may need to skip invisible text. */
429 while (scan
== next_boundary
)
432 /* This updates NEXT_BOUNDARY to the next place
433 where we might need to skip more invisible text. */
434 scan
= skip_invisible (scan
, &next_boundary
, pos
, Qnil
);
437 if (scan
!= old_scan
)
438 scan_byte
= CHAR_TO_BYTE (scan
);
439 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
442 c
= FETCH_BYTE (scan
);
443 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
445 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
452 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
460 col
= col
/ tab_width
* tab_width
;
462 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
465 /* Start of multi-byte form. */
466 if (c
== LEADING_CODE_COMPOSITION
)
468 unsigned char *ptr
= BYTE_POS_ADDR (scan_byte
);
471 = str_cmpchar_id (ptr
, next_boundary_byte
- scan_byte
);
474 scan_byte
+= cmpchar_table
[cmpchar_id
]->len
;
475 col
+= cmpchar_table
[cmpchar_id
]->width
;
478 { /* invalid composite character */
485 /* Here, we check that the following bytes are valid
486 constituents of multi-byte form. */
487 int len
= BYTES_BY_CHAR_HEAD (c
), i
;
489 for (i
= 1, scan_byte
++; i
< len
; i
++, scan_byte
++)
490 /* We don't need range checking for PTR because there
491 are anchors (`\0') at GAP and Z. */
492 if (CHAR_HEAD_P (FETCH_BYTE (scan_byte
)))
496 col
+= 4, scan_byte
-= i
- 1;
498 col
+= WIDTH_BY_CHAR_HEAD (c
);
501 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
503 else if (c
< 040 || c
>= 0177)
510 last_known_column
= col
;
511 last_known_column_point
= PT
;
512 last_known_column_modified
= MODIFF
;
517 /* Return the width in columns of the part of STRING from BEG to END.
518 If BEG is nil, that stands for the beginning of STRING.
519 If END is nil, that stands for the end of STRING. */
522 string_display_width (string
, beg
, end
)
523 Lisp_Object string
, beg
, end
;
526 register unsigned char *ptr
, *stop
;
527 register int tab_seen
;
530 register int tab_width
= XINT (current_buffer
->tab_width
);
531 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
532 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
536 e
= XSTRING (string
)->size
;
539 CHECK_NUMBER (end
, 0);
547 CHECK_NUMBER (beg
, 0);
551 /* Make a pointer for decrementing through the chars before point. */
552 ptr
= XSTRING (string
)->data
+ e
;
553 /* Make a pointer to where consecutive chars leave off,
554 going backwards from point. */
555 stop
= XSTRING (string
)->data
+ b
;
557 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
559 col
= 0, tab_seen
= 0, post_tab
= 0;
567 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
568 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
569 else if (c
>= 040 && c
< 0177)
576 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
583 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
588 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
595 DEFUN ("indent-to", Findent_to
, Sindent_to
, 1, 2, "NIndent to column: ",
596 "Indent from point with tabs and spaces until COLUMN is reached.\n\
597 Optional second argument MININUM says always do at least MININUM spaces\n\
598 even if that goes past COLUMN; by default, MININUM is zero.")
600 Lisp_Object column
, minimum
;
603 register int fromcol
;
604 register int tab_width
= XINT (current_buffer
->tab_width
);
606 CHECK_NUMBER (column
, 0);
608 XSETFASTINT (minimum
, 0);
609 CHECK_NUMBER (minimum
, 1);
611 fromcol
= current_column ();
612 mincol
= fromcol
+ XINT (minimum
);
613 if (mincol
< XINT (column
)) mincol
= XINT (column
);
615 if (fromcol
== mincol
)
616 return make_number (mincol
);
618 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
620 if (indent_tabs_mode
)
623 XSETFASTINT (n
, mincol
/ tab_width
- fromcol
/ tab_width
);
624 if (XFASTINT (n
) != 0)
626 Finsert_char (make_number ('\t'), n
, Qt
);
628 fromcol
= (mincol
/ tab_width
) * tab_width
;
632 XSETFASTINT (column
, mincol
- fromcol
);
633 Finsert_char (make_number (' '), column
, Qt
);
635 last_known_column
= mincol
;
636 last_known_column_point
= PT
;
637 last_known_column_modified
= MODIFF
;
639 XSETINT (column
, mincol
);
644 DEFUN ("current-indentation", Fcurrent_indentation
, Scurrent_indentation
,
646 "Return the indentation of the current line.\n\
647 This is the horizontal position of the character\n\
648 following any initial whitespace.")
652 int opoint
= PT
, opoint_byte
= PT_BYTE
;
654 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
656 XSETFASTINT (val
, position_indentation (PT_BYTE
));
657 SET_PT_BOTH (opoint
, opoint_byte
);
662 position_indentation (pos_byte
)
663 register int pos_byte
;
665 register int column
= 0;
666 register int tab_width
= XINT (current_buffer
->tab_width
);
667 register unsigned char *p
;
668 register unsigned char *stop
;
669 unsigned char *start
;
670 int next_boundary_byte
= pos_byte
;
671 int ceiling
= next_boundary_byte
;
673 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
675 p
= BYTE_POS_ADDR (pos_byte
);
676 /* STOP records the value of P at which we will need
677 to think about the gap, or about invisible text,
678 or about the end of the buffer. */
680 /* START records the starting value of P. */
688 /* If we have updated P, set POS_BYTE to match.
689 The first time we enter the loop, POS_BYTE is already right. */
691 pos_byte
= PTR_BYTE_POS (p
);
692 /* Consider the various reasons STOP might have been set here. */
693 if (pos_byte
== ZV_BYTE
)
695 if (pos_byte
== next_boundary_byte
)
698 int pos
= BYTE_TO_CHAR (pos_byte
);
699 pos
= skip_invisible (pos
, &next_boundary
, ZV
, Qnil
);
700 pos_byte
= CHAR_TO_BYTE (pos
);
701 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
703 if (pos_byte
>= ceiling
)
704 ceiling
= BUFFER_CEILING_OF (pos_byte
) + 1;
705 /* Compute the next place we need to stop and think,
706 and set STOP accordingly. */
707 stop_pos_byte
= min (ceiling
, next_boundary_byte
);
708 /* The -1 and +1 arrange to point at the first byte of gap
709 (if STOP_POS_BYTE is the position of the gap)
710 rather than at the data after the gap. */
712 stop
= BYTE_POS_ADDR (stop_pos_byte
- 1) + 1;
713 p
= BYTE_POS_ADDR (pos_byte
);
718 if (! NILP (current_buffer
->enable_multibyte_characters
))
724 column
+= tab_width
- column
% tab_width
;
727 if (ASCII_BYTE_P (p
[-1])
728 || NILP (current_buffer
->enable_multibyte_characters
))
732 pos_byte
= PTR_BYTE_POS (p
- 1);
733 c
= FETCH_MULTIBYTE_CHAR (pos_byte
);
734 if (CHAR_HAS_CATEGORY (c
, ' '))
738 p
= BYTE_POS_ADDR (pos_byte
);
747 /* Test whether the line beginning at POS is indented beyond COLUMN.
748 Blank lines are treated as if they had the same indentation as the
752 indented_beyond_p (pos
, pos_byte
, column
)
753 int pos
, pos_byte
, column
;
756 int opoint
= PT
, opoint_byte
= PT_BYTE
;
758 SET_PT_BOTH (pos
, pos_byte
);
759 while (PT
> BEGV
&& FETCH_BYTE (PT_BYTE
) == '\n')
760 scan_newline (PT
- 1, PT_BYTE
- 1, BEGV
, BEGV_BYTE
, -1, 0);
762 XSETFASTINT (val
, position_indentation (PT_BYTE
));
763 SET_PT_BOTH (opoint
, opoint_byte
);
767 DEFUN ("move-to-column", Fmove_to_column
, Smove_to_column
, 1, 2, "p",
768 "Move point to column COLUMN in the current line.\n\
769 The column of a character is calculated by adding together the widths\n\
770 as displayed of the previous characters in the line.\n\
771 This function ignores line-continuation;\n\
772 there is no upper limit on the column number a character can have\n\
773 and horizontal scrolling has no effect.\n\
775 If specified column is within a character, point goes after that character.\n\
776 If it's past end of line, point goes to end of line.\n\n\
777 A non-nil second (optional) argument FORCE means, if the line\n\
778 is too short to reach column COLUMN then add spaces/tabs to get there,\n\
779 and if COLUMN is in the middle of a tab character, change it to spaces.\n\
781 The return value is the current column.")
783 Lisp_Object column
, force
;
786 register int col
= current_column ();
789 register int tab_width
= XINT (current_buffer
->tab_width
);
790 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
791 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
792 register int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
799 int pos_byte
, end_byte
, next_boundary_byte
;
801 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
802 CHECK_NATNUM (column
, 0);
803 goal
= XINT (column
);
810 next_boundary_byte
= PT_BYTE
;
812 /* If we're starting past the desired column,
813 back up to beginning of line and scan from there. */
817 pos
= current_column_bol_cache
;
818 pos_byte
= CHAR_TO_BYTE (pos
);
824 while (pos
== next_boundary
)
827 pos
= skip_invisible (pos
, &next_boundary
, end
, Qnil
);
829 pos_byte
= CHAR_TO_BYTE (pos
);
830 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
835 /* Test reaching the goal column. We do this after skipping
836 invisible characters, so that we put point before the
837 character on which the cursor will appear. */
841 c
= FETCH_BYTE (pos_byte
);
842 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
844 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
851 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
859 col
= col
/ tab_width
* tab_width
;
861 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
863 else if (c
< 040 || c
== 0177)
867 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
869 /* Start of multi-byte form. */
872 pos_byte
--; /* rewind to the character head */
873 ptr
= BYTE_POS_ADDR (pos_byte
);
874 if (c
== LEADING_CODE_COMPOSITION
)
876 int cmpchar_id
= str_cmpchar_id (ptr
, end_byte
- pos_byte
);
880 col
+= cmpchar_table
[cmpchar_id
]->width
;
881 pos_byte
+= cmpchar_table
[cmpchar_id
]->len
;
884 { /* invalid composite character */
891 /* Here, we check that the following bytes are valid
892 constituents of multi-byte form. */
893 int len
= BYTES_BY_CHAR_HEAD (c
), i
;
895 for (i
= 1, ptr
++; i
< len
; i
++, ptr
++)
896 /* We don't need range checking for PTR because there
897 are anchors (`\0') both at GPT and Z. */
898 if (CHAR_HEAD_P (*ptr
))
902 col
+= 4, pos_byte
++;
904 col
+= WIDTH_BY_CHAR_HEAD (c
), pos_byte
+= i
;
912 SET_PT_BOTH (pos
, pos_byte
);
914 /* If a tab char made us overshoot, change it to spaces
915 and scan through it again. */
916 if (!NILP (force
) && col
> goal
&& c
== '\t' && prev_col
< goal
)
918 int old_point
, old_point_byte
;
920 del_range (PT
- 1, PT
);
921 Findent_to (make_number (goal
), Qnil
);
923 old_point_byte
= PT_BYTE
;
924 Findent_to (make_number (col
), Qnil
);
925 SET_PT_BOTH (old_point
, old_point_byte
);
926 /* Set the last_known... vars consistently. */
930 /* If line ends prematurely, add space to the end. */
931 if (col
< goal
&& !NILP (force
))
932 Findent_to (make_number (col
= goal
), Qnil
);
934 last_known_column
= col
;
935 last_known_column_point
= PT
;
936 last_known_column_modified
= MODIFF
;
938 XSETFASTINT (val
, col
);
942 /* compute_motion: compute buffer posn given screen posn and vice versa */
944 struct position val_compute_motion
;
946 /* Scan the current buffer forward from offset FROM, pretending that
947 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
948 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
949 and return the ending buffer position and screen location. If we
950 can't hit the requested column exactly (because of a tab or other
951 multi-column character), overshoot.
953 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
954 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
955 earlier call to compute_motion. The other common case is that FROMHPOS
956 is zero and FROM is a position that "belongs" at column zero, but might
957 be shifted by overlay strings; in this case DID_MOTION should be 0.
959 WIDTH is the number of columns available to display text;
960 compute_motion uses this to handle continuation lines and such.
961 HSCROLL is the number of columns not being displayed at the left
962 margin; this is usually taken from a window's hscroll member.
963 TAB_OFFSET is the number of columns of the first tab that aren't
964 being displayed, perhaps because of a continuation line or
967 compute_motion returns a pointer to a struct position. The bufpos
968 member gives the buffer position at the end of the scan, and hpos
969 and vpos give its cartesian location. prevhpos is the column at
970 which the character before bufpos started, and contin is non-zero
971 if we reached the current line by continuing the previous.
973 Note that FROMHPOS and TOHPOS should be expressed in real screen
974 columns, taking HSCROLL and the truncation glyph at the left margin
975 into account. That is, beginning-of-line moves you to the hpos
976 -HSCROLL + (HSCROLL > 0).
978 For example, to find the buffer position of column COL of line LINE
979 of a certain window, pass the window's starting location as FROM
980 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
981 Pass the buffer's ZV as TO, to limit the scan to the end of the
982 visible section of the buffer, and pass LINE and COL as TOVPOS and
985 When displaying in window w, a typical formula for WIDTH is:
988 - (has_vertical_scroll_bars
989 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
990 : (window_width + window_left != frame_width))
993 window_width is XFASTINT (w->width),
994 window_left is XFASTINT (w->left),
995 has_vertical_scroll_bars is
996 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
997 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
999 Or you can let window_internal_width do this all for you, and write:
1000 window_internal_width (w) - 1
1002 The `-1' accounts for the continuation-line backslashes; the rest
1003 accounts for window borders if the window is split horizontally, and
1004 the scroll bars if they are turned on. */
1007 compute_motion (from
, fromvpos
, fromhpos
, did_motion
, to
, tovpos
, tohpos
, width
, hscroll
, tab_offset
, win
)
1008 int from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
;
1011 int hscroll
, tab_offset
;
1014 register int hpos
= fromhpos
;
1015 register int vpos
= fromvpos
;
1020 register int tab_width
= XFASTINT (current_buffer
->tab_width
);
1021 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
1022 register struct Lisp_Char_Table
*dp
= window_display_table (win
);
1024 = (INTEGERP (current_buffer
->selective_display
)
1025 ? XINT (current_buffer
->selective_display
)
1026 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1029 = (selective
&& dp
&& VECTORP (DISP_INVIS_VECTOR (dp
))
1030 ? XVECTOR (DISP_INVIS_VECTOR (dp
))->size
: 0);
1031 /* The next location where the `invisible' property changes, or an
1032 overlay starts or ends. */
1033 int next_boundary
= from
;
1035 /* For computing runs of characters with similar widths.
1036 Invariant: width_run_width is zero, or all the characters
1037 from width_run_start to width_run_end have a fixed width of
1039 int width_run_start
= from
;
1040 int width_run_end
= from
;
1041 int width_run_width
= 0;
1042 Lisp_Object
*width_table
;
1045 /* The next buffer pos where we should consult the width run cache. */
1046 int next_width_run
= from
;
1049 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1050 int wide_column
= 0; /* Set to 1 when a previous character
1052 int prev_pos
; /* Previous buffer position. */
1053 int prev_pos_byte
; /* Previous buffer position. */
1054 int contin_hpos
; /* HPOS of last column of continued line. */
1055 int prev_tab_offset
; /* Previous tab offset. */
1057 XSETBUFFER (buffer
, current_buffer
);
1058 XSETWINDOW (window
, win
);
1060 width_run_cache_on_off ();
1061 if (dp
== buffer_display_table ())
1062 width_table
= (VECTORP (current_buffer
->width_table
)
1063 ? XVECTOR (current_buffer
->width_table
)->contents
1066 /* If the window has its own display table, we can't use the width
1067 run cache, because that's based on the buffer's display table. */
1070 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
1072 pos
= prev_pos
= from
;
1073 pos_byte
= prev_pos_byte
= CHAR_TO_BYTE (from
);
1075 prev_tab_offset
= tab_offset
;
1078 while (pos
== next_boundary
)
1083 /* If the caller says that the screen position came from an earlier
1084 call to compute_motion, then we've already accounted for the
1085 overlay strings at point. This is only true the first time
1086 through, so clear the flag after testing it. */
1088 /* We need to skip past the overlay strings. Currently those
1089 strings must not contain TAB;
1090 if we want to relax that restriction, something will have
1091 to be changed here. */
1093 unsigned char *ovstr
;
1094 int ovlen
= overlay_strings (pos
, win
, &ovstr
);
1095 hpos
+= (multibyte
? strwidth (ovstr
, ovlen
) : ovlen
);
1102 /* Advance POS past invisible characters
1103 (but not necessarily all that there are here),
1104 and store in next_boundary the next position where
1105 we need to call skip_invisible. */
1106 newpos
= skip_invisible (pos
, &next_boundary
, to
, window
);
1111 if (newpos
!= pos_here
)
1114 pos_byte
= CHAR_TO_BYTE (pos
);
1118 /* Handle right margin. */
1119 /* Note on a wide-column character.
1121 Characters are classified into the following three categories
1122 according to the width (columns occupied on screen).
1124 (1) single-column character: ex. `a'
1125 (2) multi-column character: ex. `^A', TAB, `\033'
1126 (3) wide-column character: ex. Japanese character, Chinese character
1127 (In the following example, `W_' stands for them.)
1129 Multi-column characters can be divided around the right margin,
1130 but wide-column characters cannot.
1134 (*) The cursor is placed on the next character after the point.
1138 j ^---- next after the point
1139 ^--- next char. after the point.
1141 In case of sigle-column character
1145 033 ^---- next after the point, next char. after the point.
1147 In case of multi-column character
1151 W_ ^---- next after the point
1152 ^---- next char. after the point.
1154 In case of wide-column character
1156 The problem here is continuation at a wide-column character.
1157 In this case, the line may shorter less than WIDTH.
1158 And we find the continuation AFTER it occurs.
1165 || (truncate_partial_width_windows
1166 && width
+ 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win
))))
1167 || !NILP (current_buffer
->truncate_lines
))
1169 /* Truncating: skip to newline. */
1170 if (pos
<= to
) /* This IF is needed because we may past TO */
1172 pos
= find_before_next_newline (pos
, to
, 1);
1173 pos_byte
= CHAR_TO_BYTE (pos
);
1176 /* If we just skipped next_boundary,
1177 loop around in the main while
1179 if (pos
>= next_boundary
)
1180 next_boundary
= pos
+ 1;
1182 prev_tab_offset
= tab_offset
;
1187 /* Remember the previous value. */
1188 prev_tab_offset
= tab_offset
;
1193 tab_offset
+= prev_hpos
;
1197 tab_offset
+= width
;
1201 contin_hpos
= prev_hpos
;
1206 /* Stop if past the target buffer position or screen position. */
1209 /* Go back to the previous position. */
1211 pos_byte
= prev_pos_byte
;
1213 tab_offset
= prev_tab_offset
;
1215 /* NOTE on contin_hpos, hpos, and prev_hpos.
1219 W_ ^---- contin_hpos
1225 if (contin_hpos
&& prev_hpos
== 0
1226 && contin_hpos
< width
&& !wide_column
)
1228 /* Line breaking occurs in the middle of multi-column
1229 character. Go back to previous line. */
1234 /* If previous character is NEWLINE,
1235 set VPOS back to previous line */
1240 if (vpos
> tovpos
|| vpos
== tovpos
&& hpos
>= tohpos
)
1242 if (contin_hpos
&& prev_hpos
== 0
1243 && ((hpos
> tohpos
&& contin_hpos
== width
) || wide_column
))
1244 { /* Line breaks because we can't put the character at the
1245 previous line any more. It is not the multi-column
1246 character continued in middle. Go back to previous
1247 buffer position, screen position, and set tab offset
1248 to previous value. It's the beginning of the
1251 pos_byte
= prev_pos_byte
;
1253 tab_offset
= prev_tab_offset
;
1257 if (pos
== ZV
) /* We cannot go beyond ZV. Stop here. */
1262 prev_pos_byte
= pos_byte
;
1265 /* Consult the width run cache to see if we can avoid inspecting
1266 the text character-by-character. */
1267 if (current_buffer
->width_run_cache
&& pos
>= next_width_run
)
1271 = region_cache_forward (current_buffer
,
1272 current_buffer
->width_run_cache
,
1275 /* A width of zero means the character's width varies (like
1276 a tab), is meaningless (like a newline), or we just don't
1277 want to skip over it for some other reason. */
1278 if (common_width
!= 0)
1282 /* Don't go past the final buffer posn the user
1287 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1289 /* Don't go past the final horizontal position the user
1291 if (vpos
== tovpos
&& run_end_hpos
> tohpos
)
1293 run_end
= pos
+ (tohpos
- hpos
) / common_width
;
1294 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1297 /* Don't go past the margin. */
1298 if (run_end_hpos
>= width
)
1300 run_end
= pos
+ (width
- hpos
) / common_width
;
1301 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1304 hpos
= run_end_hpos
;
1306 prev_hpos
= hpos
- common_width
;
1310 pos_byte
= CHAR_TO_BYTE (pos
);
1314 next_width_run
= run_end
+ 1;
1317 /* We have to scan the text character-by-character. */
1320 c
= FETCH_BYTE (pos_byte
);
1323 /* Perhaps add some info to the width_run_cache. */
1324 if (current_buffer
->width_run_cache
)
1326 /* Is this character part of the current run? If so, extend
1328 if (pos
- 1 == width_run_end
1329 && XFASTINT (width_table
[c
]) == width_run_width
)
1330 width_run_end
= pos
;
1332 /* The previous run is over, since this is a character at a
1333 different position, or a different width. */
1336 /* Have we accumulated a run to put in the cache?
1337 (Currently, we only cache runs of width == 1). */
1338 if (width_run_start
< width_run_end
1339 && width_run_width
== 1)
1340 know_region_cache (current_buffer
,
1341 current_buffer
->width_run_cache
,
1342 width_run_start
, width_run_end
);
1344 /* Start recording a new width run. */
1345 width_run_width
= XFASTINT (width_table
[c
]);
1346 width_run_start
= pos
- 1;
1347 width_run_end
= pos
;
1351 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
))
1352 && ! (multibyte
&& BASE_LEADING_CODE_P (c
)))
1353 hpos
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
1354 else if (c
>= 040 && c
< 0177)
1358 int tem
= (hpos
+ tab_offset
+ hscroll
- (hscroll
> 0)) % tab_width
;
1361 hpos
+= tab_width
- tem
;
1366 && indented_beyond_p (pos
, pos_byte
, selective
))
1368 /* If (pos == to), we don't have to take care of
1369 selective display. */
1372 /* Skip any number of invisible lines all at once */
1375 pos
= find_before_next_newline (pos
, to
, 1) + 1;
1376 pos_byte
= CHAR_TO_BYTE (pos
);
1379 && indented_beyond_p (pos
, pos_byte
, selective
));
1380 /* Allow for the " ..." that is displayed for them. */
1383 hpos
+= selective_rlen
;
1387 DEC_BOTH (pos
, pos_byte
);
1388 /* We have skipped the invis text, but not the
1394 /* A visible line. */
1398 /* Count the truncation glyph on column 0 */
1405 else if (c
== CR
&& selective
< 0)
1407 /* In selective display mode,
1408 everything from a ^M to the end of the line is invisible.
1409 Stop *before* the real newline. */
1412 pos
= find_before_next_newline (pos
, to
, 1);
1413 pos_byte
= CHAR_TO_BYTE (pos
);
1415 /* If we just skipped next_boundary,
1416 loop around in the main while
1418 if (pos
> next_boundary
)
1419 next_boundary
= pos
;
1420 /* Allow for the " ..." that is displayed for them. */
1423 hpos
+= selective_rlen
;
1428 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
1430 /* Start of multi-byte form. */
1432 int len
, actual_len
;
1434 pos
--, pos_byte
--; /* rewind POS */
1436 ptr
= BYTE_POS_ADDR (pos_byte
);
1437 len
= BUFFER_CEILING_OF (pos_byte
) - pos_byte
+ 1;
1439 c
= STRING_CHAR_AND_LENGTH (ptr
, len
, actual_len
);
1441 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
1442 hpos
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
1443 else if (actual_len
== 1)
1445 else if (COMPOSITE_CHAR_P (c
))
1447 int id
= COMPOSITE_CHAR_ID (c
);
1448 int width
= (id
< n_cmpchars
) ? cmpchar_table
[id
]->width
: 0;
1455 int width
= WIDTH_BY_CHAR_HEAD (*ptr
);
1462 pos_byte
+= actual_len
;
1465 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
1471 /* Remember any final width run in the cache. */
1472 if (current_buffer
->width_run_cache
1473 && width_run_width
== 1
1474 && width_run_start
< width_run_end
)
1475 know_region_cache (current_buffer
, current_buffer
->width_run_cache
,
1476 width_run_start
, width_run_end
);
1478 val_compute_motion
.bufpos
= pos
;
1479 val_compute_motion
.bytepos
= pos_byte
;
1480 val_compute_motion
.hpos
= hpos
;
1481 val_compute_motion
.vpos
= vpos
;
1482 val_compute_motion
.prevhpos
= prev_hpos
;
1483 /* We alalways handle all of them here; none of them remain to do. */
1484 val_compute_motion
.ovstring_chars_done
= 0;
1486 /* Nonzero if have just continued a line */
1487 val_compute_motion
.contin
= (contin_hpos
&& prev_hpos
== 0);
1489 return &val_compute_motion
;
1492 #if 0 /* The doc string is too long for some compilers,
1493 but make-docfile can find it in this comment. */
1494 DEFUN ("compute-motion", Ffoo
, Sfoo
, 7, 7, 0,
1495 "Scan through the current buffer, calculating screen position.\n\
1496 Scan the current buffer forward from offset FROM,\n\
1497 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1498 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1499 and return the ending buffer position and screen location.\n\
1501 There are three additional arguments:\n\
1503 WIDTH is the number of columns available to display text;\n\
1504 this affects handling of continuation lines.\n\
1505 This is usually the value returned by `window-width', less one (to allow\n\
1506 for the continuation glyph).\n\
1508 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1509 HSCROLL is the number of columns not being displayed at the left\n\
1510 margin; this is usually taken from a window's hscroll member.\n\
1511 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1512 being displayed, perhaps because the line was continued within it.\n\
1513 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1515 WINDOW is the window to operate on. It is used to choose the display table;\n\
1516 if it is showing the current buffer, it is used also for\n\
1517 deciding which overlay properties apply.\n\
1518 Note that `compute-motion' always operates on the current buffer.\n\
1520 The value is a list of five elements:\n\
1521 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1522 POS is the buffer position where the scan stopped.\n\
1523 VPOS is the vertical position where the scan stopped.\n\
1524 HPOS is the horizontal position where the scan stopped.\n\
1526 PREVHPOS is the horizontal position one character back from POS.\n\
1527 CONTIN is t if a line was continued after (or within) the previous character.\n\
1529 For example, to find the buffer position of column COL of line LINE\n\
1530 of a certain window, pass the window's starting location as FROM\n\
1531 and the window's upper-left coordinates as FROMPOS.\n\
1532 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1533 visible section of the buffer, and pass LINE and COL as TOPOS.")
1534 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1537 DEFUN ("compute-motion", Fcompute_motion
, Scompute_motion
, 7, 7, 0,
1539 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1540 Lisp_Object from
, frompos
, to
, topos
;
1541 Lisp_Object width
, offsets
, window
;
1543 Lisp_Object bufpos
, hpos
, vpos
, prevhpos
, contin
;
1544 struct position
*pos
;
1545 int hscroll
, tab_offset
;
1547 CHECK_NUMBER_COERCE_MARKER (from
, 0);
1548 CHECK_CONS (frompos
, 0);
1549 CHECK_NUMBER (XCONS (frompos
)->car
, 0);
1550 CHECK_NUMBER (XCONS (frompos
)->cdr
, 0);
1551 CHECK_NUMBER_COERCE_MARKER (to
, 0);
1552 CHECK_CONS (topos
, 0);
1553 CHECK_NUMBER (XCONS (topos
)->car
, 0);
1554 CHECK_NUMBER (XCONS (topos
)->cdr
, 0);
1555 CHECK_NUMBER (width
, 0);
1556 if (!NILP (offsets
))
1558 CHECK_CONS (offsets
, 0);
1559 CHECK_NUMBER (XCONS (offsets
)->car
, 0);
1560 CHECK_NUMBER (XCONS (offsets
)->cdr
, 0);
1561 hscroll
= XINT (XCONS (offsets
)->car
);
1562 tab_offset
= XINT (XCONS (offsets
)->cdr
);
1565 hscroll
= tab_offset
= 0;
1568 window
= Fselected_window ();
1570 CHECK_LIVE_WINDOW (window
, 0);
1572 pos
= compute_motion (XINT (from
), XINT (XCONS (frompos
)->cdr
),
1573 XINT (XCONS (frompos
)->car
), 0,
1574 XINT (to
), XINT (XCONS (topos
)->cdr
),
1575 XINT (XCONS (topos
)->car
),
1576 XINT (width
), hscroll
, tab_offset
,
1579 XSETFASTINT (bufpos
, pos
->bufpos
);
1580 XSETINT (hpos
, pos
->hpos
);
1581 XSETINT (vpos
, pos
->vpos
);
1582 XSETINT (prevhpos
, pos
->prevhpos
);
1584 return Fcons (bufpos
,
1588 Fcons (pos
->contin
? Qt
: Qnil
, Qnil
)))));
1592 /* Fvertical_motion and vmotion */
1593 struct position val_vmotion
;
1596 vmotion (from
, vtarget
, w
)
1597 register int from
, vtarget
;
1600 int width
= window_internal_width (w
) - 1;
1601 int hscroll
= XINT (w
->hscroll
);
1602 struct position pos
;
1603 /* vpos is cumulative vertical position, changed as from is changed */
1604 register int vpos
= 0;
1605 Lisp_Object prevline
;
1608 int lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
1610 = (INTEGERP (current_buffer
->selective_display
)
1611 ? XINT (current_buffer
->selective_display
)
1612 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1617 XSETWINDOW (window
, w
);
1619 /* The omission of the clause
1620 && marker_position (w->start) == BEG
1621 here is deliberate; I think we want to measure from the prompt
1622 position even if the minibuffer window has scrolled. */
1623 if (EQ (window
, minibuf_window
))
1625 if (minibuf_prompt_width
== 0 && STRINGP (minibuf_prompt
))
1626 minibuf_prompt_width
1627 = string_display_width (minibuf_prompt
, Qnil
, Qnil
);
1629 start_hpos
= minibuf_prompt_width
;
1632 if (vpos
>= vtarget
)
1634 /* To move upward, go a line at a time until
1635 we have gone at least far enough. */
1639 while ((vpos
> vtarget
|| first
) && from
> BEGV
)
1641 Lisp_Object propval
;
1643 XSETFASTINT (prevline
, find_next_newline_no_quit (from
- 1, -1));
1644 while (XFASTINT (prevline
) > BEGV
1646 && indented_beyond_p (XFASTINT (prevline
),
1647 CHAR_TO_BYTE (XFASTINT (prevline
)),
1649 #ifdef USE_TEXT_PROPERTIES
1650 /* watch out for newlines with `invisible' property */
1651 || (propval
= Fget_char_property (prevline
,
1654 TEXT_PROP_MEANS_INVISIBLE (propval
))
1657 XSETFASTINT (prevline
,
1658 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1660 pos
= *compute_motion (XFASTINT (prevline
), 0,
1661 lmargin
+ (XFASTINT (prevline
) == BEG
1665 /* Don't care for VPOS... */
1666 1 << (BITS_PER_SHORT
- 1),
1668 1 << (BITS_PER_SHORT
- 1),
1670 /* This compensates for start_hpos
1671 so that a tab as first character
1672 still occupies 8 columns. */
1673 (XFASTINT (prevline
) == BEG
1678 from
= XFASTINT (prevline
);
1681 /* If we made exactly the desired vertical distance,
1682 or if we hit beginning of buffer,
1683 return point found */
1684 if (vpos
>= vtarget
)
1686 val_vmotion
.bufpos
= from
;
1687 val_vmotion
.bytepos
= CHAR_TO_BYTE (from
);
1688 val_vmotion
.vpos
= vpos
;
1689 val_vmotion
.hpos
= lmargin
;
1690 val_vmotion
.contin
= 0;
1691 val_vmotion
.prevhpos
= 0;
1692 val_vmotion
.ovstring_chars_done
= 0;
1693 val_vmotion
.tab_offset
= 0; /* For accumulating tab offset. */
1694 return &val_vmotion
;
1697 /* Otherwise find the correct spot by moving down */
1699 /* Moving downward is simple, but must calculate from beg of line
1700 to determine hpos of starting point */
1701 from_byte
= CHAR_TO_BYTE (from
);
1702 if (from
> BEGV
&& FETCH_BYTE (from_byte
- 1) != '\n')
1704 Lisp_Object propval
;
1706 XSETFASTINT (prevline
, find_next_newline_no_quit (from
, -1));
1707 while (XFASTINT (prevline
) > BEGV
1709 && indented_beyond_p (XFASTINT (prevline
),
1710 CHAR_TO_BYTE (XFASTINT (prevline
)),
1712 #ifdef USE_TEXT_PROPERTIES
1713 /* watch out for newlines with `invisible' property */
1714 || (propval
= Fget_char_property (prevline
, Qinvisible
,
1716 TEXT_PROP_MEANS_INVISIBLE (propval
))
1719 XSETFASTINT (prevline
,
1720 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1722 pos
= *compute_motion (XFASTINT (prevline
), 0,
1723 lmargin
+ (XFASTINT (prevline
) == BEG
1727 /* Don't care for VPOS... */
1728 1 << (BITS_PER_SHORT
- 1),
1730 1 << (BITS_PER_SHORT
- 1),
1732 (XFASTINT (prevline
) == BEG
? -start_hpos
: 0),
1738 pos
.hpos
= lmargin
+ (from
== BEG
? start_hpos
: 0);
1743 return compute_motion (from
, vpos
, pos
.hpos
, did_motion
,
1744 ZV
, vtarget
, - (1 << (BITS_PER_SHORT
- 1)),
1746 pos
.tab_offset
- (from
== BEG
? start_hpos
: 0),
1750 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 2, 0,
1751 "Move point to start of the screen line LINES lines down.\n\
1752 If LINES is negative, this means moving up.\n\
1754 This function is an ordinary cursor motion function\n\
1755 which calculates the new position based on how text would be displayed.\n\
1756 The new position may be the start of a line,\n\
1757 or just the start of a continuation line.\n\
1758 The function returns number of screen lines moved over;\n\
1759 that usually equals LINES, but may be closer to zero\n\
1760 if beginning or end of buffer was reached.\n\
1762 The optional second argument WINDOW specifies the window to use for\n\
1763 parameters such as width, horizontal scrolling, and so on.\n\
1764 The default is to use the selected window's parameters.\n\
1766 `vertical-motion' always uses the current buffer,\n\
1767 regardless of which buffer is displayed in WINDOW.\n\
1768 This is consistent with other cursor motion functions\n\
1769 and makes it possible to use `vertical-motion' in any buffer,\n\
1770 whether or not it is currently displayed in some window.")
1772 Lisp_Object lines
, window
;
1774 struct position pos
;
1776 CHECK_NUMBER (lines
, 0);
1777 if (! NILP (window
))
1778 CHECK_WINDOW (window
, 0);
1780 window
= selected_window
;
1782 pos
= *vmotion (PT
, (int) XINT (lines
), XWINDOW (window
));
1784 SET_PT (pos
.bufpos
);
1785 return make_number (pos
.vpos
);
1788 /* file's initialization. */
1792 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode
,
1793 "*Indentation can insert tabs if this is non-nil.\n\
1794 Setting this variable automatically makes it local to the current buffer.");
1795 indent_tabs_mode
= 1;
1797 defsubr (&Scurrent_indentation
);
1798 defsubr (&Sindent_to
);
1799 defsubr (&Scurrent_column
);
1800 defsubr (&Smove_to_column
);
1801 defsubr (&Svertical_motion
);
1802 defsubr (&Scompute_motion
);