1 /* Indentation functions.
2 Copyright (C) 1985,86,87,88,93,94,95,98, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
34 #include "intervals.h"
35 #include "region-cache.h"
37 /* Indentation can insert tabs if this is non-zero;
38 otherwise always uses spaces. */
42 #define min(a, b) ((a) < (b) ? (a) : (b))
43 #define max(a, b) ((a) > (b) ? (a) : (b))
47 /* These three values memoize the current column to avoid recalculation. */
49 /* Last value returned by current_column.
50 Some things in set last_known_column_point to -1
51 to mark the memoized value as invalid. */
53 int last_known_column
;
55 /* Value of point when current_column was called. */
57 int last_known_column_point
;
59 /* Value of MODIFF when current_column was called. */
61 int last_known_column_modified
;
63 static int current_column_1
P_ ((void));
64 static int position_indentation
P_ ((int));
66 /* Cache of beginning of line found by the last call of
69 int current_column_bol_cache
;
71 /* Get the display table to use for the current buffer. */
73 struct Lisp_Char_Table
*
74 buffer_display_table ()
78 thisbuf
= current_buffer
->display_table
;
79 if (DISP_TABLE_P (thisbuf
))
80 return XCHAR_TABLE (thisbuf
);
81 if (DISP_TABLE_P (Vstandard_display_table
))
82 return XCHAR_TABLE (Vstandard_display_table
);
86 /* Width run cache considerations. */
88 /* Return the width of character C under display table DP. */
91 character_width (c
, dp
)
93 struct Lisp_Char_Table
*dp
;
97 /* These width computations were determined by examining the cases
98 in display_text_line. */
100 /* Everything can be handled by the display table, if it's
101 present and the element is right. */
102 if (dp
&& (elt
= DISP_CHAR_VECTOR (dp
, c
), VECTORP (elt
)))
103 return XVECTOR (elt
)->size
;
105 /* Some characters are special. */
106 if (c
== '\n' || c
== '\t' || c
== '\015')
109 /* Printing characters have width 1. */
110 else if (c
>= 040 && c
< 0177)
113 /* Everybody else (control characters, metacharacters) has other
114 widths. We could return their actual widths here, but they
115 depend on things like ctl_arrow and crud like that, and they're
116 not very common at all. So we'll just claim we don't know their
122 /* Return true iff the display table DISPTAB specifies the same widths
123 for characters as WIDTHTAB. We use this to decide when to
124 invalidate the buffer's width_run_cache. */
127 disptab_matches_widthtab (disptab
, widthtab
)
128 struct Lisp_Char_Table
*disptab
;
129 struct Lisp_Vector
*widthtab
;
133 if (widthtab
->size
!= 256)
136 for (i
= 0; i
< 256; i
++)
137 if (character_width (i
, disptab
)
138 != XFASTINT (widthtab
->contents
[i
]))
144 /* Recompute BUF's width table, using the display table DISPTAB. */
147 recompute_width_table (buf
, disptab
)
149 struct Lisp_Char_Table
*disptab
;
152 struct Lisp_Vector
*widthtab
;
154 if (!VECTORP (buf
->width_table
))
155 buf
->width_table
= Fmake_vector (make_number (256), make_number (0));
156 widthtab
= XVECTOR (buf
->width_table
);
157 if (widthtab
->size
!= 256)
160 for (i
= 0; i
< 256; i
++)
161 XSETFASTINT (widthtab
->contents
[i
], character_width (i
, disptab
));
164 /* Allocate or free the width run cache, as requested by the current
165 state of current_buffer's cache_long_line_scans variable. */
168 width_run_cache_on_off ()
170 if (NILP (current_buffer
->cache_long_line_scans
)
171 /* And, for the moment, this feature doesn't work on multibyte
173 || !NILP (current_buffer
->enable_multibyte_characters
))
175 /* It should be off. */
176 if (current_buffer
->width_run_cache
)
178 free_region_cache (current_buffer
->width_run_cache
);
179 current_buffer
->width_run_cache
= 0;
180 current_buffer
->width_table
= Qnil
;
185 /* It should be on. */
186 if (current_buffer
->width_run_cache
== 0)
188 current_buffer
->width_run_cache
= new_region_cache ();
189 recompute_width_table (current_buffer
, buffer_display_table ());
195 /* Skip some invisible characters starting from POS.
196 This includes characters invisible because of text properties
197 and characters invisible because of overlays.
199 If position POS is followed by invisible characters,
200 skip some of them and return the position after them.
201 Otherwise return POS itself.
203 Set *NEXT_BOUNDARY_P to the next position at which
204 it will be necessary to call this function again.
206 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
207 to a value greater than TO.
209 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
210 take account of overlays that apply only in WINDOW.
212 We don't necessarily skip all the invisible characters after POS
213 because that could take a long time. We skip a reasonable number
214 which can be skipped quickly. If there might be more invisible
215 characters immediately following, then *NEXT_BOUNDARY_P
216 will equal the return value. */
219 skip_invisible (pos
, next_boundary_p
, to
, window
)
221 int *next_boundary_p
;
225 Lisp_Object prop
, position
, overlay_limit
, proplimit
;
229 XSETFASTINT (position
, pos
);
230 XSETBUFFER (buffer
, current_buffer
);
232 /* Give faster response for overlay lookup near POS. */
233 recenter_overlay_lists (current_buffer
, pos
);
235 /* We must not advance farther than the next overlay change.
236 The overlay change might change the invisible property;
237 or there might be overlay strings to be displayed there. */
238 overlay_limit
= Fnext_overlay_change (position
);
239 /* As for text properties, this gives a lower bound
240 for where the invisible text property could change. */
241 proplimit
= Fnext_property_change (position
, buffer
, Qt
);
242 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
243 proplimit
= overlay_limit
;
244 /* PROPLIMIT is now a lower bound for the next change
245 in invisible status. If that is plenty far away,
246 use that lower bound. */
247 if (XFASTINT (proplimit
) > pos
+ 100 || XFASTINT (proplimit
) >= to
)
248 *next_boundary_p
= XFASTINT (proplimit
);
249 /* Otherwise, scan for the next `invisible' property change. */
252 /* Don't scan terribly far. */
253 XSETFASTINT (proplimit
, min (pos
+ 100, to
));
254 /* No matter what. don't go past next overlay change. */
255 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
256 proplimit
= overlay_limit
;
257 end
= XFASTINT (Fnext_single_property_change (position
, Qinvisible
,
260 /* Don't put the boundary in the middle of multibyte form if
261 there is no actual property change. */
263 && !NILP (current_buffer
->enable_multibyte_characters
)
265 while (pos
< end
&& !CHAR_HEAD_P (POS_ADDR (end
)))
268 *next_boundary_p
= end
;
270 /* if the `invisible' property is set, we can skip to
271 the next property change */
272 if (!NILP (window
) && EQ (XWINDOW (window
)->buffer
, buffer
))
273 prop
= Fget_char_property (position
, Qinvisible
, window
);
275 prop
= Fget_char_property (position
, Qinvisible
, buffer
);
276 if (TEXT_PROP_MEANS_INVISIBLE (prop
))
277 return *next_boundary_p
;
281 /* If a composition starts at POS/POS_BYTE and it doesn't stride over
282 POINT, set *LEN / *LEN_BYTE to the character and byte lengths, *WIDTH
283 to the width, and return 1. Otherwise, return 0. */
286 check_composition (pos
, pos_byte
, point
, len
, len_byte
, width
)
287 int pos
, pos_byte
, point
;
288 int *len
, *len_byte
, *width
;
294 if (! find_composition (pos
, -1, &start
, &end
, &prop
, Qnil
)
295 || pos
!= start
|| point
< end
296 || !COMPOSITION_VALID_P (start
, end
, prop
))
298 if ((id
= get_composition_id (pos
, pos_byte
, end
- pos
, prop
, Qnil
)) < 0)
301 *len
= COMPOSITION_LENGTH (prop
);
302 *len_byte
= CHAR_TO_BYTE (end
) - pos_byte
;
303 *width
= composition_table
[id
]->width
;
307 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
309 DP is a display table or NULL.
311 This macro is used in current_column_1, Fmove_to_column, and
314 #define MULTIBYTE_BYTES_WIDTH(p, dp) \
319 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, bytes); \
320 if (BYTES_BY_CHAR_HEAD (*p) != bytes) \
324 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))) \
325 width = XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; \
327 width = WIDTH_BY_CHAR_HEAD (*p); \
329 wide_column = width; \
333 DEFUN ("current-column", Fcurrent_column
, Scurrent_column
, 0, 0, 0,
334 "Return the horizontal position of point. Beginning of line is column 0.\n\
335 This is calculated by adding together the widths of all the displayed\n\
336 representations of the character between the start of the previous line\n\
337 and point. (eg control characters will have a width of 2 or 4, tabs\n\
338 will have a variable width)\n\
339 Ignores finite width of frame, which means that this function may return\n\
340 values greater than (frame-width).\n\
341 Whether the line is visible (if `selective-display' is t) has no effect;\n\
342 however, ^M is treated as end of line when `selective-display' is t.")
346 XSETFASTINT (temp
, current_column ());
350 /* Cancel any recorded value of the horizontal position. */
353 invalidate_current_column ()
355 last_known_column_point
= 0;
362 register unsigned char *ptr
, *stop
;
363 register int tab_seen
;
366 register int tab_width
= XINT (current_buffer
->tab_width
);
367 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
368 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
370 if (PT
== last_known_column_point
371 && MODIFF
== last_known_column_modified
)
372 return last_known_column
;
374 /* If the buffer has overlays, text properties,
375 or multibyte characters, use a more general algorithm. */
376 if (BUF_INTERVALS (current_buffer
)
377 || !NILP (current_buffer
->overlays_before
)
378 || !NILP (current_buffer
->overlays_after
)
380 return current_column_1 ();
382 /* Scan backwards from point to the previous newline,
383 counting width. Tab characters are the only complicated case. */
385 /* Make a pointer for decrementing through the chars before point. */
386 ptr
= BYTE_POS_ADDR (PT_BYTE
- 1) + 1;
387 /* Make a pointer to where consecutive chars leave off,
388 going backwards from point. */
391 else if (PT
<= GPT
|| BEGV
> GPT
)
396 if (tab_width
<= 0 || tab_width
> 1000)
399 col
= 0, tab_seen
= 0, post_tab
= 0;
408 /* We stopped either for the beginning of the buffer
410 if (ptr
== BEGV_ADDR
)
413 /* It was the gap. Jump back over it. */
417 /* Check whether that brings us to beginning of buffer. */
424 if (dp
&& VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
426 charvec
= DISP_CHAR_VECTOR (dp
, c
);
435 for (i
= n
- 1; i
>= 0; --i
)
437 if (VECTORP (charvec
))
439 /* This should be handled the same as
440 next_element_from_display_vector does it. */
441 Lisp_Object entry
= AREF (charvec
, i
);
444 && GLYPH_CHAR_VALID_P (XFASTINT (entry
)))
445 c
= FAST_GLYPH_CHAR (XFASTINT (entry
));
450 if (c
>= 040 && c
< 0177)
454 && EQ (current_buffer
->selective_display
, Qt
)))
457 goto start_of_line_found
;
462 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
469 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
477 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
481 if (ptr
== BEGV_ADDR
)
482 current_column_bol_cache
= BEGV
;
484 current_column_bol_cache
= BYTE_TO_CHAR (PTR_BYTE_POS (ptr
));
486 last_known_column
= col
;
487 last_known_column_point
= PT
;
488 last_known_column_modified
= MODIFF
;
493 /* Return the column number of position POS
494 by scanning forward from the beginning of the line.
495 This function handles characters that are invisible
496 due to text properties or overlays. */
501 register int tab_width
= XINT (current_buffer
->tab_width
);
502 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
503 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
504 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
506 /* Start the scan at the beginning of this line with column number 0. */
507 register int col
= 0;
509 int next_boundary
, next_boundary_byte
;
510 int opoint
= PT
, opoint_byte
= PT_BYTE
;
512 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
513 current_column_bol_cache
= PT
;
514 scan
= PT
, scan_byte
= PT_BYTE
;
515 SET_PT_BOTH (opoint
, opoint_byte
);
516 next_boundary
= scan
;
517 next_boundary_byte
= scan_byte
;
519 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
521 /* Scan forward to the target position. */
522 while (scan
< opoint
)
528 /* Occasionally we may need to skip invisible text. */
529 while (scan
== next_boundary
)
532 /* This updates NEXT_BOUNDARY to the next place
533 where we might need to skip more invisible text. */
534 scan
= skip_invisible (scan
, &next_boundary
, opoint
, Qnil
);
537 if (scan
!= old_scan
)
538 scan_byte
= CHAR_TO_BYTE (scan
);
539 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
542 /* Check composition sequence. */
544 int len
, len_byte
, width
;
546 if (check_composition (scan
, scan_byte
, opoint
,
547 &len
, &len_byte
, &width
))
550 scan_byte
+= len_byte
;
557 c
= FETCH_BYTE (scan_byte
);
560 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
561 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
563 charvec
= DISP_CHAR_VECTOR (dp
, c
);
572 for (i
= n
- 1; i
>= 0; --i
)
574 if (VECTORP (charvec
))
576 /* This should be handled the same as
577 next_element_from_display_vector does it. */
578 Lisp_Object entry
= AREF (charvec
, i
);
581 && GLYPH_CHAR_VALID_P (XFASTINT (entry
)))
582 c
= FAST_GLYPH_CHAR (XFASTINT (entry
));
589 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
597 col
= col
/ tab_width
* tab_width
;
599 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
602 int bytes
, width
, wide_column
;
605 ptr
= BYTE_POS_ADDR (scan_byte
);
606 MULTIBYTE_BYTES_WIDTH (ptr
, dp
);
610 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
612 else if (c
< 040 || c
>= 0177)
620 last_known_column
= col
;
621 last_known_column_point
= PT
;
622 last_known_column_modified
= MODIFF
;
628 #if 0 /* Not used. */
630 /* Return the width in columns of the part of STRING from BEG to END.
631 If BEG is nil, that stands for the beginning of STRING.
632 If END is nil, that stands for the end of STRING. */
635 string_display_width (string
, beg
, end
)
636 Lisp_Object string
, beg
, end
;
639 register unsigned char *ptr
, *stop
;
640 register int tab_seen
;
643 register int tab_width
= XINT (current_buffer
->tab_width
);
644 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
645 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
649 e
= XSTRING (string
)->size
;
652 CHECK_NUMBER (end
, 0);
660 CHECK_NUMBER (beg
, 0);
664 /* Make a pointer for decrementing through the chars before point. */
665 ptr
= XSTRING (string
)->data
+ e
;
666 /* Make a pointer to where consecutive chars leave off,
667 going backwards from point. */
668 stop
= XSTRING (string
)->data
+ b
;
670 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
672 col
= 0, tab_seen
= 0, post_tab
= 0;
680 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
681 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
682 else if (c
>= 040 && c
< 0177)
689 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
696 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
701 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
711 DEFUN ("indent-to", Findent_to
, Sindent_to
, 1, 2, "NIndent to column: ",
712 "Indent from point with tabs and spaces until COLUMN is reached.\n\
713 Optional second argument MININUM says always do at least MININUM spaces\n\
714 even if that goes past COLUMN; by default, MININUM is zero.")
716 Lisp_Object column
, minimum
;
719 register int fromcol
;
720 register int tab_width
= XINT (current_buffer
->tab_width
);
722 CHECK_NUMBER (column
, 0);
724 XSETFASTINT (minimum
, 0);
725 CHECK_NUMBER (minimum
, 1);
727 fromcol
= current_column ();
728 mincol
= fromcol
+ XINT (minimum
);
729 if (mincol
< XINT (column
)) mincol
= XINT (column
);
731 if (fromcol
== mincol
)
732 return make_number (mincol
);
734 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
736 if (indent_tabs_mode
)
739 XSETFASTINT (n
, mincol
/ tab_width
- fromcol
/ tab_width
);
740 if (XFASTINT (n
) != 0)
742 Finsert_char (make_number ('\t'), n
, Qt
);
744 fromcol
= (mincol
/ tab_width
) * tab_width
;
748 XSETFASTINT (column
, mincol
- fromcol
);
749 Finsert_char (make_number (' '), column
, Qt
);
751 last_known_column
= mincol
;
752 last_known_column_point
= PT
;
753 last_known_column_modified
= MODIFF
;
755 XSETINT (column
, mincol
);
760 static int position_indentation
P_ ((int));
762 DEFUN ("current-indentation", Fcurrent_indentation
, Scurrent_indentation
,
764 "Return the indentation of the current line.\n\
765 This is the horizontal position of the character\n\
766 following any initial whitespace.")
770 int opoint
= PT
, opoint_byte
= PT_BYTE
;
772 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
774 XSETFASTINT (val
, position_indentation (PT_BYTE
));
775 SET_PT_BOTH (opoint
, opoint_byte
);
780 position_indentation (pos_byte
)
781 register int pos_byte
;
783 register int column
= 0;
784 register int tab_width
= XINT (current_buffer
->tab_width
);
785 register unsigned char *p
;
786 register unsigned char *stop
;
787 unsigned char *start
;
788 int next_boundary_byte
= pos_byte
;
789 int ceiling
= next_boundary_byte
;
791 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
793 p
= BYTE_POS_ADDR (pos_byte
);
794 /* STOP records the value of P at which we will need
795 to think about the gap, or about invisible text,
796 or about the end of the buffer. */
798 /* START records the starting value of P. */
806 /* If we have updated P, set POS_BYTE to match.
807 The first time we enter the loop, POS_BYTE is already right. */
809 pos_byte
= PTR_BYTE_POS (p
);
810 /* Consider the various reasons STOP might have been set here. */
811 if (pos_byte
== ZV_BYTE
)
813 if (pos_byte
== next_boundary_byte
)
816 int pos
= BYTE_TO_CHAR (pos_byte
);
817 pos
= skip_invisible (pos
, &next_boundary
, ZV
, Qnil
);
818 pos_byte
= CHAR_TO_BYTE (pos
);
819 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
821 if (pos_byte
>= ceiling
)
822 ceiling
= BUFFER_CEILING_OF (pos_byte
) + 1;
823 /* Compute the next place we need to stop and think,
824 and set STOP accordingly. */
825 stop_pos_byte
= min (ceiling
, next_boundary_byte
);
826 /* The -1 and +1 arrange to point at the first byte of gap
827 (if STOP_POS_BYTE is the position of the gap)
828 rather than at the data after the gap. */
830 stop
= BYTE_POS_ADDR (stop_pos_byte
- 1) + 1;
831 p
= BYTE_POS_ADDR (pos_byte
);
836 if (! NILP (current_buffer
->enable_multibyte_characters
))
842 column
+= tab_width
- column
% tab_width
;
845 if (ASCII_BYTE_P (p
[-1])
846 || NILP (current_buffer
->enable_multibyte_characters
))
850 pos_byte
= PTR_BYTE_POS (p
- 1);
851 c
= FETCH_MULTIBYTE_CHAR (pos_byte
);
852 if (CHAR_HAS_CATEGORY (c
, ' '))
856 p
= BYTE_POS_ADDR (pos_byte
);
865 /* Test whether the line beginning at POS is indented beyond COLUMN.
866 Blank lines are treated as if they had the same indentation as the
870 indented_beyond_p (pos
, pos_byte
, column
)
871 int pos
, pos_byte
, column
;
874 int opoint
= PT
, opoint_byte
= PT_BYTE
;
876 SET_PT_BOTH (pos
, pos_byte
);
877 while (PT
> BEGV
&& FETCH_BYTE (PT_BYTE
) == '\n')
878 scan_newline (PT
- 1, PT_BYTE
- 1, BEGV
, BEGV_BYTE
, -1, 0);
880 val
= position_indentation (PT_BYTE
);
881 SET_PT_BOTH (opoint
, opoint_byte
);
882 return val
>= column
;
885 DEFUN ("move-to-column", Fmove_to_column
, Smove_to_column
, 1, 2, "p",
886 "Move point to column COLUMN in the current line.\n\
887 The column of a character is calculated by adding together the widths\n\
888 as displayed of the previous characters in the line.\n\
889 This function ignores line-continuation;\n\
890 there is no upper limit on the column number a character can have\n\
891 and horizontal scrolling has no effect.\n\
893 If specified column is within a character, point goes after that character.\n\
894 If it's past end of line, point goes to end of line.\n\n\
895 A non-nil second (optional) argument FORCE means,\n\
896 if COLUMN is in the middle of a tab character, change it to spaces.\n\
897 In addition, if FORCE is t, and the line is too short\n\
898 to reach column COLUMN, add spaces/tabs to get there.\n\
900 The return value is the current column.")
902 Lisp_Object column
, force
;
905 register int col
= current_column ();
908 register int tab_width
= XINT (current_buffer
->tab_width
);
909 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
910 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
911 register int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
918 int pos_byte
, end_byte
, next_boundary_byte
;
920 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
921 CHECK_NATNUM (column
, 0);
922 goal
= XINT (column
);
929 next_boundary_byte
= PT_BYTE
;
931 /* If we're starting past the desired column,
932 back up to beginning of line and scan from there. */
936 pos
= current_column_bol_cache
;
937 pos_byte
= CHAR_TO_BYTE (pos
);
946 while (pos
== next_boundary
)
949 pos
= skip_invisible (pos
, &next_boundary
, end
, Qnil
);
951 pos_byte
= CHAR_TO_BYTE (pos
);
952 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
957 /* Test reaching the goal column. We do this after skipping
958 invisible characters, so that we put point before the
959 character on which the cursor will appear. */
963 /* Check composition sequence. */
965 int len
, len_byte
, width
;
967 if (check_composition (pos
, pos_byte
, Z
, &len
, &len_byte
, &width
))
970 pos_byte
+= len_byte
;
976 c
= FETCH_BYTE (pos_byte
);
979 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
980 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
982 charvec
= DISP_CHAR_VECTOR (dp
, c
);
991 for (i
= n
- 1; i
>= 0; --i
)
993 if (VECTORP (charvec
))
995 /* This should be handled the same as
996 next_element_from_display_vector does it. */
997 Lisp_Object entry
= AREF (charvec
, i
);
1000 && GLYPH_CHAR_VALID_P (XFASTINT (entry
)))
1001 c
= FAST_GLYPH_CHAR (XFASTINT (entry
));
1009 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
1017 col
= col
/ tab_width
* tab_width
;
1019 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
1021 else if (c
< 040 || c
== 0177)
1025 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
1027 /* Start of multi-byte form. */
1029 int bytes
, width
, wide_column
;
1032 ptr
= BYTE_POS_ADDR (pos_byte
);
1033 MULTIBYTE_BYTES_WIDTH (ptr
, dp
);
1043 SET_PT_BOTH (pos
, pos_byte
);
1045 /* If a tab char made us overshoot, change it to spaces
1046 and scan through it again. */
1047 if (!NILP (force
) && col
> goal
&& c
== '\t' && prev_col
< goal
)
1049 int goal_pt
, goal_pt_byte
;
1051 /* Insert spaces in front of the tab to reach GOAL. Do this
1052 first so that a marker at the end of the tab gets
1054 SET_PT_BOTH (PT
- 1, PT_BYTE
- 1);
1055 Finsert_char (make_number (' '), make_number (goal
- prev_col
), Qt
);
1057 /* Now delete the tab, and indent to COL. */
1058 del_range (PT
, PT
+ 1);
1060 goal_pt_byte
= PT_BYTE
;
1061 Findent_to (make_number (col
), Qnil
);
1062 SET_PT_BOTH (goal_pt
, goal_pt_byte
);
1064 /* Set the last_known... vars consistently. */
1068 /* If line ends prematurely, add space to the end. */
1069 if (col
< goal
&& EQ (force
, Qt
))
1070 Findent_to (make_number (col
= goal
), Qnil
);
1072 last_known_column
= col
;
1073 last_known_column_point
= PT
;
1074 last_known_column_modified
= MODIFF
;
1076 XSETFASTINT (val
, col
);
1080 /* compute_motion: compute buffer posn given screen posn and vice versa */
1082 struct position val_compute_motion
;
1084 /* Scan the current buffer forward from offset FROM, pretending that
1085 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1086 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1087 and return the ending buffer position and screen location. If we
1088 can't hit the requested column exactly (because of a tab or other
1089 multi-column character), overshoot.
1091 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
1092 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1093 earlier call to compute_motion. The other common case is that FROMHPOS
1094 is zero and FROM is a position that "belongs" at column zero, but might
1095 be shifted by overlay strings; in this case DID_MOTION should be 0.
1097 WIDTH is the number of columns available to display text;
1098 compute_motion uses this to handle continuation lines and such.
1099 HSCROLL is the number of columns not being displayed at the left
1100 margin; this is usually taken from a window's hscroll member.
1101 TAB_OFFSET is the number of columns of the first tab that aren't
1102 being displayed, perhaps because of a continuation line or
1105 compute_motion returns a pointer to a struct position. The bufpos
1106 member gives the buffer position at the end of the scan, and hpos
1107 and vpos give its cartesian location. prevhpos is the column at
1108 which the character before bufpos started, and contin is non-zero
1109 if we reached the current line by continuing the previous.
1111 Note that FROMHPOS and TOHPOS should be expressed in real screen
1112 columns, taking HSCROLL and the truncation glyph at the left margin
1113 into account. That is, beginning-of-line moves you to the hpos
1114 -HSCROLL + (HSCROLL > 0).
1116 For example, to find the buffer position of column COL of line LINE
1117 of a certain window, pass the window's starting location as FROM
1118 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1119 Pass the buffer's ZV as TO, to limit the scan to the end of the
1120 visible section of the buffer, and pass LINE and COL as TOVPOS and
1123 When displaying in window w, a typical formula for WIDTH is:
1126 - (has_vertical_scroll_bars
1127 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
1128 : (window_width + window_left != frame_width))
1131 window_width is XFASTINT (w->width),
1132 window_left is XFASTINT (w->left),
1133 has_vertical_scroll_bars is
1134 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
1135 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
1137 Or you can let window_internal_width do this all for you, and write:
1138 window_internal_width (w) - 1
1140 The `-1' accounts for the continuation-line backslashes; the rest
1141 accounts for window borders if the window is split horizontally, and
1142 the scroll bars if they are turned on. */
1145 compute_motion (from
, fromvpos
, fromhpos
, did_motion
, to
, tovpos
, tohpos
, width
, hscroll
, tab_offset
, win
)
1146 int from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
;
1149 int hscroll
, tab_offset
;
1152 register int hpos
= fromhpos
;
1153 register int vpos
= fromvpos
;
1158 register int tab_width
= XFASTINT (current_buffer
->tab_width
);
1159 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
1160 register struct Lisp_Char_Table
*dp
= window_display_table (win
);
1162 = (INTEGERP (current_buffer
->selective_display
)
1163 ? XINT (current_buffer
->selective_display
)
1164 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1167 = (selective
&& dp
&& VECTORP (DISP_INVIS_VECTOR (dp
))
1168 ? XVECTOR (DISP_INVIS_VECTOR (dp
))->size
: 0);
1169 /* The next location where the `invisible' property changes, or an
1170 overlay starts or ends. */
1171 int next_boundary
= from
;
1173 /* For computing runs of characters with similar widths.
1174 Invariant: width_run_width is zero, or all the characters
1175 from width_run_start to width_run_end have a fixed width of
1177 int width_run_start
= from
;
1178 int width_run_end
= from
;
1179 int width_run_width
= 0;
1180 Lisp_Object
*width_table
;
1183 /* The next buffer pos where we should consult the width run cache. */
1184 int next_width_run
= from
;
1187 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1188 /* If previous char scanned was a wide character,
1189 this is the column where it ended. Otherwise, this is 0. */
1190 int wide_column_end_hpos
= 0;
1191 int prev_pos
; /* Previous buffer position. */
1192 int prev_pos_byte
; /* Previous buffer position. */
1193 int contin_hpos
; /* HPOS of last column of continued line. */
1194 int prev_tab_offset
; /* Previous tab offset. */
1196 XSETBUFFER (buffer
, current_buffer
);
1197 XSETWINDOW (window
, win
);
1199 width_run_cache_on_off ();
1200 if (dp
== buffer_display_table ())
1201 width_table
= (VECTORP (current_buffer
->width_table
)
1202 ? XVECTOR (current_buffer
->width_table
)->contents
1205 /* If the window has its own display table, we can't use the width
1206 run cache, because that's based on the buffer's display table. */
1209 if (tab_width
<= 0 || tab_width
> 1000)
1215 pos
= prev_pos
= from
;
1216 pos_byte
= prev_pos_byte
= CHAR_TO_BYTE (from
);
1218 prev_tab_offset
= tab_offset
;
1221 while (pos
== next_boundary
)
1226 /* Don't skip invisible if we are already at the margin. */
1227 if (vpos
> tovpos
|| vpos
== tovpos
&& hpos
>= tohpos
)
1229 if (contin_hpos
&& prev_hpos
== 0
1231 && (contin_hpos
== width
|| wide_column_end_hpos
> width
))
1232 { /* Line breaks because we can't put the character at the
1233 previous line any more. It is not the multi-column
1234 character continued in middle. Go back to previous
1235 buffer position, screen position, and set tab offset
1236 to previous value. It's the beginning of the
1239 pos_byte
= prev_pos_byte
;
1241 tab_offset
= prev_tab_offset
;
1246 /* If the caller says that the screen position came from an earlier
1247 call to compute_motion, then we've already accounted for the
1248 overlay strings at point. This is only true the first time
1249 through, so clear the flag after testing it. */
1251 /* We need to skip past the overlay strings. Currently those
1252 strings must not contain TAB;
1253 if we want to relax that restriction, something will have
1254 to be changed here. */
1256 unsigned char *ovstr
;
1257 int ovlen
= overlay_strings (pos
, win
, &ovstr
);
1258 hpos
+= ((multibyte
&& ovlen
> 0)
1259 ? strwidth (ovstr
, ovlen
) : ovlen
);
1266 /* Advance POS past invisible characters
1267 (but not necessarily all that there are here),
1268 and store in next_boundary the next position where
1269 we need to call skip_invisible. */
1270 newpos
= skip_invisible (pos
, &next_boundary
, to
, window
);
1274 pos
= min (to
, newpos
);
1275 pos_byte
= CHAR_TO_BYTE (pos
);
1279 if (newpos
!= pos_here
)
1282 pos_byte
= CHAR_TO_BYTE (pos
);
1286 /* Handle right margin. */
1287 /* Note on a wide-column character.
1289 Characters are classified into the following three categories
1290 according to the width (columns occupied on screen).
1292 (1) single-column character: ex. `a'
1293 (2) multi-column character: ex. `^A', TAB, `\033'
1294 (3) wide-column character: ex. Japanese character, Chinese character
1295 (In the following example, `W_' stands for them.)
1297 Multi-column characters can be divided around the right margin,
1298 but wide-column characters cannot.
1302 (*) The cursor is placed on the next character after the point.
1306 j ^---- next after the point
1307 ^--- next char. after the point.
1309 In case of sigle-column character
1313 033 ^---- next after the point, next char. after the point.
1315 In case of multi-column character
1319 W_ ^---- next after the point
1320 ^---- next char. after the point.
1322 In case of wide-column character
1324 The problem here is continuation at a wide-column character.
1325 In this case, the line may shorter less than WIDTH.
1326 And we find the continuation AFTER it occurs.
1333 || (truncate_partial_width_windows
1334 && width
+ 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win
))))
1335 || !NILP (current_buffer
->truncate_lines
))
1337 /* Truncating: skip to newline, unless we are already past
1338 TO (we need to go back below). */
1341 pos
= find_before_next_newline (pos
, to
, 1);
1342 pos_byte
= CHAR_TO_BYTE (pos
);
1344 /* If we just skipped next_boundary,
1345 loop around in the main while
1347 if (pos
>= next_boundary
)
1348 next_boundary
= pos
+ 1;
1350 prev_tab_offset
= tab_offset
;
1356 /* Remember the previous value. */
1357 prev_tab_offset
= tab_offset
;
1359 if (wide_column_end_hpos
> width
)
1362 tab_offset
+= prev_hpos
;
1366 tab_offset
+= width
;
1370 contin_hpos
= prev_hpos
;
1375 /* Stop if past the target buffer position or screen position. */
1378 /* Go back to the previous position. */
1380 pos_byte
= prev_pos_byte
;
1382 tab_offset
= prev_tab_offset
;
1384 /* NOTE on contin_hpos, hpos, and prev_hpos.
1388 W_ ^---- contin_hpos
1394 if (contin_hpos
&& prev_hpos
== 0
1395 && contin_hpos
< width
&& !wide_column_end_hpos
)
1397 /* Line breaking occurs in the middle of multi-column
1398 character. Go back to previous line. */
1403 /* If previous character is NEWLINE,
1404 set VPOS back to previous line */
1409 if (vpos
> tovpos
|| vpos
== tovpos
&& hpos
>= tohpos
)
1411 if (contin_hpos
&& prev_hpos
== 0
1413 && (contin_hpos
== width
|| wide_column_end_hpos
> width
))
1414 { /* Line breaks because we can't put the character at the
1415 previous line any more. It is not the multi-column
1416 character continued in middle. Go back to previous
1417 buffer position, screen position, and set tab offset
1418 to previous value. It's the beginning of the
1421 pos_byte
= prev_pos_byte
;
1423 tab_offset
= prev_tab_offset
;
1427 if (pos
== ZV
) /* We cannot go beyond ZV. Stop here. */
1432 prev_pos_byte
= pos_byte
;
1433 wide_column_end_hpos
= 0;
1435 /* Consult the width run cache to see if we can avoid inspecting
1436 the text character-by-character. */
1437 if (current_buffer
->width_run_cache
&& pos
>= next_width_run
)
1441 = region_cache_forward (current_buffer
,
1442 current_buffer
->width_run_cache
,
1445 /* A width of zero means the character's width varies (like
1446 a tab), is meaningless (like a newline), or we just don't
1447 want to skip over it for some other reason. */
1448 if (common_width
!= 0)
1452 /* Don't go past the final buffer posn the user
1457 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1459 /* Don't go past the final horizontal position the user
1461 if (vpos
== tovpos
&& run_end_hpos
> tohpos
)
1463 run_end
= pos
+ (tohpos
- hpos
) / common_width
;
1464 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1467 /* Don't go past the margin. */
1468 if (run_end_hpos
>= width
)
1470 run_end
= pos
+ (width
- hpos
) / common_width
;
1471 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1474 hpos
= run_end_hpos
;
1476 prev_hpos
= hpos
- common_width
;
1480 pos_byte
= CHAR_TO_BYTE (pos
);
1484 next_width_run
= run_end
+ 1;
1487 /* We have to scan the text character-by-character. */
1491 Lisp_Object charvec
;
1493 c
= FETCH_BYTE (pos_byte
);
1495 /* Check composition sequence. */
1497 int len
, len_byte
, width
;
1499 if (check_composition (pos
, pos_byte
, to
, &len
, &len_byte
, &width
))
1502 pos_byte
+= len_byte
;
1510 /* Perhaps add some info to the width_run_cache. */
1511 if (current_buffer
->width_run_cache
)
1513 /* Is this character part of the current run? If so, extend
1515 if (pos
- 1 == width_run_end
1516 && XFASTINT (width_table
[c
]) == width_run_width
)
1517 width_run_end
= pos
;
1519 /* The previous run is over, since this is a character at a
1520 different position, or a different width. */
1523 /* Have we accumulated a run to put in the cache?
1524 (Currently, we only cache runs of width == 1). */
1525 if (width_run_start
< width_run_end
1526 && width_run_width
== 1)
1527 know_region_cache (current_buffer
,
1528 current_buffer
->width_run_cache
,
1529 width_run_start
, width_run_end
);
1531 /* Start recording a new width run. */
1532 width_run_width
= XFASTINT (width_table
[c
]);
1533 width_run_start
= pos
- 1;
1534 width_run_end
= pos
;
1539 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
1540 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
1542 charvec
= DISP_CHAR_VECTOR (dp
, c
);
1543 n
= ASIZE (charvec
);
1551 for (i
= n
- 1; i
>= 0; --i
)
1553 if (VECTORP (charvec
))
1555 /* This should be handled the same as
1556 next_element_from_display_vector does it. */
1557 Lisp_Object entry
= AREF (charvec
, i
);
1559 if (INTEGERP (entry
)
1560 && GLYPH_CHAR_VALID_P (XFASTINT (entry
)))
1561 c
= FAST_GLYPH_CHAR (XFASTINT (entry
));
1566 if (c
>= 040 && c
< 0177)
1570 int tem
= ((hpos
+ tab_offset
+ hscroll
- (hscroll
> 0))
1574 hpos
+= tab_width
- tem
;
1579 && indented_beyond_p (pos
, pos_byte
, selective
))
1581 /* If (pos == to), we don't have to take care of
1582 selective display. */
1585 /* Skip any number of invisible lines all at once */
1588 pos
= find_before_next_newline (pos
, to
, 1);
1591 pos_byte
= CHAR_TO_BYTE (pos
);
1594 && indented_beyond_p (pos
, pos_byte
, selective
));
1595 /* Allow for the " ..." that is displayed for them. */
1598 hpos
+= selective_rlen
;
1602 DEC_BOTH (pos
, pos_byte
);
1603 /* We have skipped the invis text, but not the
1609 /* A visible line. */
1613 /* Count the truncation glyph on column 0 */
1620 else if (c
== CR
&& selective
< 0)
1622 /* In selective display mode,
1623 everything from a ^M to the end of the line is invisible.
1624 Stop *before* the real newline. */
1627 pos
= find_before_next_newline (pos
, to
, 1);
1628 pos_byte
= CHAR_TO_BYTE (pos
);
1630 /* If we just skipped next_boundary,
1631 loop around in the main while
1633 if (pos
> next_boundary
)
1634 next_boundary
= pos
;
1635 /* Allow for the " ..." that is displayed for them. */
1638 hpos
+= selective_rlen
;
1643 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
1645 /* Start of multi-byte form. */
1647 int bytes
, width
, wide_column
;
1649 pos_byte
--; /* rewind POS_BYTE */
1650 ptr
= BYTE_POS_ADDR (pos_byte
);
1651 MULTIBYTE_BYTES_WIDTH (ptr
, dp
);
1654 wide_column_end_hpos
= hpos
+ wide_column
;
1658 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
1665 /* Remember any final width run in the cache. */
1666 if (current_buffer
->width_run_cache
1667 && width_run_width
== 1
1668 && width_run_start
< width_run_end
)
1669 know_region_cache (current_buffer
, current_buffer
->width_run_cache
,
1670 width_run_start
, width_run_end
);
1672 val_compute_motion
.bufpos
= pos
;
1673 val_compute_motion
.bytepos
= pos_byte
;
1674 val_compute_motion
.hpos
= hpos
;
1675 val_compute_motion
.vpos
= vpos
;
1676 if (contin_hpos
&& prev_hpos
== 0)
1677 val_compute_motion
.prevhpos
= contin_hpos
;
1679 val_compute_motion
.prevhpos
= prev_hpos
;
1680 /* We alalways handle all of them here; none of them remain to do. */
1681 val_compute_motion
.ovstring_chars_done
= 0;
1683 /* Nonzero if have just continued a line */
1684 val_compute_motion
.contin
= (contin_hpos
&& prev_hpos
== 0);
1687 return &val_compute_motion
;
1691 #if 0 /* The doc string is too long for some compilers,
1692 but make-docfile can find it in this comment. */
1693 DEFUN ("compute-motion", Ffoo
, Sfoo
, 7, 7, 0,
1694 "Scan through the current buffer, calculating screen position.\n\
1695 Scan the current buffer forward from offset FROM,\n\
1696 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1697 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1698 and return the ending buffer position and screen location.\n\
1700 There are three additional arguments:\n\
1702 WIDTH is the number of columns available to display text;\n\
1703 this affects handling of continuation lines.\n\
1704 This is usually the value returned by `window-width', less one (to allow\n\
1705 for the continuation glyph).\n\
1707 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1708 HSCROLL is the number of columns not being displayed at the left\n\
1709 margin; this is usually taken from a window's hscroll member.\n\
1710 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1711 being displayed, perhaps because the line was continued within it.\n\
1712 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1714 WINDOW is the window to operate on. It is used to choose the display table;\n\
1715 if it is showing the current buffer, it is used also for\n\
1716 deciding which overlay properties apply.\n\
1717 Note that `compute-motion' always operates on the current buffer.\n\
1719 The value is a list of five elements:\n\
1720 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1721 POS is the buffer position where the scan stopped.\n\
1722 VPOS is the vertical position where the scan stopped.\n\
1723 HPOS is the horizontal position where the scan stopped.\n\
1725 PREVHPOS is the horizontal position one character back from POS.\n\
1726 CONTIN is t if a line was continued after (or within) the previous character.\n\
1728 For example, to find the buffer position of column COL of line LINE\n\
1729 of a certain window, pass the window's starting location as FROM\n\
1730 and the window's upper-left coordinates as FROMPOS.\n\
1731 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1732 visible section of the buffer, and pass LINE and COL as TOPOS.")
1733 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1736 DEFUN ("compute-motion", Fcompute_motion
, Scompute_motion
, 7, 7, 0,
1738 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1739 Lisp_Object from
, frompos
, to
, topos
;
1740 Lisp_Object width
, offsets
, window
;
1742 Lisp_Object bufpos
, hpos
, vpos
, prevhpos
;
1743 struct position
*pos
;
1744 int hscroll
, tab_offset
;
1746 CHECK_NUMBER_COERCE_MARKER (from
, 0);
1747 CHECK_CONS (frompos
, 0);
1748 CHECK_NUMBER (XCAR (frompos
), 0);
1749 CHECK_NUMBER (XCDR (frompos
), 0);
1750 CHECK_NUMBER_COERCE_MARKER (to
, 0);
1751 CHECK_CONS (topos
, 0);
1752 CHECK_NUMBER (XCAR (topos
), 0);
1753 CHECK_NUMBER (XCDR (topos
), 0);
1754 CHECK_NUMBER (width
, 0);
1755 if (!NILP (offsets
))
1757 CHECK_CONS (offsets
, 0);
1758 CHECK_NUMBER (XCAR (offsets
), 0);
1759 CHECK_NUMBER (XCDR (offsets
), 0);
1760 hscroll
= XINT (XCAR (offsets
));
1761 tab_offset
= XINT (XCDR (offsets
));
1764 hscroll
= tab_offset
= 0;
1767 window
= Fselected_window ();
1769 CHECK_LIVE_WINDOW (window
, 0);
1771 if (XINT (from
) < BEGV
|| XINT (from
) > ZV
)
1772 args_out_of_range_3 (from
, make_number (BEGV
), make_number (ZV
));
1773 if (XINT (to
) < BEGV
|| XINT (to
) > ZV
)
1774 args_out_of_range_3 (to
, make_number (BEGV
), make_number (ZV
));
1776 pos
= compute_motion (XINT (from
), XINT (XCDR (frompos
)),
1777 XINT (XCAR (frompos
)), 0,
1778 XINT (to
), XINT (XCDR (topos
)),
1779 XINT (XCAR (topos
)),
1780 XINT (width
), hscroll
, tab_offset
,
1783 XSETFASTINT (bufpos
, pos
->bufpos
);
1784 XSETINT (hpos
, pos
->hpos
);
1785 XSETINT (vpos
, pos
->vpos
);
1786 XSETINT (prevhpos
, pos
->prevhpos
);
1788 return Fcons (bufpos
,
1792 Fcons (pos
->contin
? Qt
: Qnil
, Qnil
)))));
1796 /* Fvertical_motion and vmotion */
1798 struct position val_vmotion
;
1801 vmotion (from
, vtarget
, w
)
1802 register int from
, vtarget
;
1805 int width
= window_internal_width (w
) - 1;
1806 int hscroll
= XINT (w
->hscroll
);
1807 struct position pos
;
1808 /* vpos is cumulative vertical position, changed as from is changed */
1809 register int vpos
= 0;
1810 Lisp_Object prevline
;
1813 int lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
1815 = (INTEGERP (current_buffer
->selective_display
)
1816 ? XINT (current_buffer
->selective_display
)
1817 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1821 /* This is the object we use for fetching character properties. */
1822 Lisp_Object text_prop_object
;
1824 XSETWINDOW (window
, w
);
1826 /* If the window contains this buffer, use it for getting text properties.
1827 Otherwise use the current buffer as arg for doing that. */
1828 if (EQ (w
->buffer
, Fcurrent_buffer ()))
1829 text_prop_object
= window
;
1831 text_prop_object
= Fcurrent_buffer ();
1833 if (vpos
>= vtarget
)
1835 /* To move upward, go a line at a time until
1836 we have gone at least far enough. */
1840 while ((vpos
> vtarget
|| first
) && from
> BEGV
)
1842 Lisp_Object propval
;
1844 XSETFASTINT (prevline
, find_next_newline_no_quit (from
- 1, -1));
1845 while (XFASTINT (prevline
) > BEGV
1847 && indented_beyond_p (XFASTINT (prevline
),
1848 CHAR_TO_BYTE (XFASTINT (prevline
)),
1850 /* watch out for newlines with `invisible' property */
1851 || (propval
= Fget_char_property (prevline
,
1854 TEXT_PROP_MEANS_INVISIBLE (propval
))))
1855 XSETFASTINT (prevline
,
1856 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1858 pos
= *compute_motion (XFASTINT (prevline
), 0,
1859 lmargin
+ (XFASTINT (prevline
) == BEG
1863 /* Don't care for VPOS... */
1864 1 << (BITS_PER_SHORT
- 1),
1866 1 << (BITS_PER_SHORT
- 1),
1868 /* This compensates for start_hpos
1869 so that a tab as first character
1870 still occupies 8 columns. */
1871 (XFASTINT (prevline
) == BEG
1876 from
= XFASTINT (prevline
);
1879 /* If we made exactly the desired vertical distance,
1880 or if we hit beginning of buffer,
1881 return point found */
1882 if (vpos
>= vtarget
)
1884 val_vmotion
.bufpos
= from
;
1885 val_vmotion
.bytepos
= CHAR_TO_BYTE (from
);
1886 val_vmotion
.vpos
= vpos
;
1887 val_vmotion
.hpos
= lmargin
;
1888 val_vmotion
.contin
= 0;
1889 val_vmotion
.prevhpos
= 0;
1890 val_vmotion
.ovstring_chars_done
= 0;
1891 val_vmotion
.tab_offset
= 0; /* For accumulating tab offset. */
1892 return &val_vmotion
;
1895 /* Otherwise find the correct spot by moving down */
1897 /* Moving downward is simple, but must calculate from beg of line
1898 to determine hpos of starting point */
1899 from_byte
= CHAR_TO_BYTE (from
);
1900 if (from
> BEGV
&& FETCH_BYTE (from_byte
- 1) != '\n')
1902 Lisp_Object propval
;
1904 XSETFASTINT (prevline
, find_next_newline_no_quit (from
, -1));
1905 while (XFASTINT (prevline
) > BEGV
1907 && indented_beyond_p (XFASTINT (prevline
),
1908 CHAR_TO_BYTE (XFASTINT (prevline
)),
1910 /* watch out for newlines with `invisible' property */
1911 || (propval
= Fget_char_property (prevline
, Qinvisible
,
1913 TEXT_PROP_MEANS_INVISIBLE (propval
))))
1914 XSETFASTINT (prevline
,
1915 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1917 pos
= *compute_motion (XFASTINT (prevline
), 0,
1918 lmargin
+ (XFASTINT (prevline
) == BEG
1922 /* Don't care for VPOS... */
1923 1 << (BITS_PER_SHORT
- 1),
1925 1 << (BITS_PER_SHORT
- 1),
1927 (XFASTINT (prevline
) == BEG
? -start_hpos
: 0),
1933 pos
.hpos
= lmargin
+ (from
== BEG
? start_hpos
: 0);
1938 return compute_motion (from
, vpos
, pos
.hpos
, did_motion
,
1939 ZV
, vtarget
, - (1 << (BITS_PER_SHORT
- 1)),
1941 pos
.tab_offset
- (from
== BEG
? start_hpos
: 0),
1945 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 2, 0,
1946 "Move point to start of the screen line LINES lines down.\n\
1947 If LINES is negative, this means moving up.\n\
1949 This function is an ordinary cursor motion function\n\
1950 which calculates the new position based on how text would be displayed.\n\
1951 The new position may be the start of a line,\n\
1952 or just the start of a continuation line.\n\
1953 The function returns number of screen lines moved over;\n\
1954 that usually equals LINES, but may be closer to zero\n\
1955 if beginning or end of buffer was reached.\n\
1957 The optional second argument WINDOW specifies the window to use for\n\
1958 parameters such as width, horizontal scrolling, and so on.\n\
1959 The default is to use the selected window's parameters.\n\
1961 `vertical-motion' always uses the current buffer,\n\
1962 regardless of which buffer is displayed in WINDOW.\n\
1963 This is consistent with other cursor motion functions\n\
1964 and makes it possible to use `vertical-motion' in any buffer,\n\
1965 whether or not it is currently displayed in some window.")
1967 Lisp_Object lines
, window
;
1972 Lisp_Object old_buffer
;
1973 struct gcpro gcpro1
;
1975 CHECK_NUMBER (lines
, 0);
1976 if (! NILP (window
))
1977 CHECK_WINDOW (window
, 0);
1979 window
= selected_window
;
1980 w
= XWINDOW (window
);
1983 GCPRO1 (old_buffer
);
1984 if (XBUFFER (w
->buffer
) != current_buffer
)
1986 /* Set the window's buffer temporarily to the current buffer. */
1987 old_buffer
= w
->buffer
;
1988 XSETBUFFER (w
->buffer
, current_buffer
);
1991 SET_TEXT_POS (pt
, PT
, PT_BYTE
);
1992 start_display (&it
, w
, pt
);
1993 move_it_by_lines (&it
, XINT (lines
), 0);
1994 SET_PT_BOTH (IT_CHARPOS (it
), IT_BYTEPOS (it
));
1996 if (BUFFERP (old_buffer
))
1997 w
->buffer
= old_buffer
;
1999 RETURN_UNGCPRO (make_number (it
.vpos
));
2004 /* File's initialization. */
2009 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode
,
2010 "*Indentation can insert tabs if this is non-nil.\n\
2011 Setting this variable automatically makes it local to the current buffer.");
2012 indent_tabs_mode
= 1;
2014 defsubr (&Scurrent_indentation
);
2015 defsubr (&Sindent_to
);
2016 defsubr (&Scurrent_column
);
2017 defsubr (&Smove_to_column
);
2018 defsubr (&Svertical_motion
);
2019 defsubr (&Scompute_motion
);