1 /* Indentation functions.
2 Copyright (C) 1985-1988, 1993-1995, 1998, 2000-2017 Free Software
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 3 of the License, or (at
10 your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
24 #include "character.h"
27 #include "composite.h"
32 #include "intervals.h"
33 #include "dispextern.h"
34 #include "region-cache.h"
38 /* These three values memorize the current column to avoid recalculation. */
40 /* Last value returned by current_column.
41 Some things in set last_known_column_point to -1
42 to mark the memorized value as invalid. */
44 static ptrdiff_t last_known_column
;
46 /* Value of point when current_column was called. */
48 ptrdiff_t last_known_column_point
;
50 /* Value of MODIFF when current_column was called. */
52 static EMACS_INT last_known_column_modified
;
54 static ptrdiff_t current_column_1 (void);
55 static ptrdiff_t position_indentation (ptrdiff_t);
57 /* Get the display table to use for the current buffer. */
59 struct Lisp_Char_Table
*
60 buffer_display_table (void)
64 thisbuf
= BVAR (current_buffer
, display_table
);
65 if (DISP_TABLE_P (thisbuf
))
66 return XCHAR_TABLE (thisbuf
);
67 if (DISP_TABLE_P (Vstandard_display_table
))
68 return XCHAR_TABLE (Vstandard_display_table
);
72 /* Width run cache considerations. */
74 /* Return the width of character C under display table DP. */
77 character_width (int c
, struct Lisp_Char_Table
*dp
)
81 /* These width computations were determined by examining the cases
82 in display_text_line. */
84 /* Everything can be handled by the display table, if it's
85 present and the element is right. */
86 if (dp
&& (elt
= DISP_CHAR_VECTOR (dp
, c
), VECTORP (elt
)))
89 /* Some characters are special. */
90 if (c
== '\n' || c
== '\t' || c
== '\015')
93 /* Printing characters have width 1. */
94 else if (c
>= 040 && c
< 0177)
97 /* Everybody else (control characters, metacharacters) has other
98 widths. We could return their actual widths here, but they
99 depend on things like ctl_arrow and crud like that, and they're
100 not very common at all. So we'll just claim we don't know their
106 /* Return true if the display table DISPTAB specifies the same widths
107 for characters as WIDTHTAB. We use this to decide when to
108 invalidate the buffer's width_run_cache. */
111 disptab_matches_widthtab (struct Lisp_Char_Table
*disptab
, struct Lisp_Vector
*widthtab
)
115 eassert (widthtab
->header
.size
== 256);
117 for (i
= 0; i
< 256; i
++)
118 if (character_width (i
, disptab
)
119 != XFASTINT (widthtab
->contents
[i
]))
125 /* Recompute BUF's width table, using the display table DISPTAB. */
128 recompute_width_table (struct buffer
*buf
, struct Lisp_Char_Table
*disptab
)
131 struct Lisp_Vector
*widthtab
;
133 if (!VECTORP (BVAR (buf
, width_table
)))
134 bset_width_table (buf
, make_uninit_vector (256));
135 widthtab
= XVECTOR (BVAR (buf
, width_table
));
136 eassert (widthtab
->header
.size
== 256);
138 for (i
= 0; i
< 256; i
++)
139 XSETFASTINT (widthtab
->contents
[i
], character_width (i
, disptab
));
142 /* Allocate or free the width run cache, as requested by the
143 current state of current_buffer's cache_long_scans variable. */
145 static struct region_cache
*
146 width_run_cache_on_off (void)
148 struct buffer
*cache_buffer
= current_buffer
;
149 bool indirect_p
= false;
151 if (cache_buffer
->base_buffer
)
153 cache_buffer
= cache_buffer
->base_buffer
;
157 if (NILP (BVAR (current_buffer
, cache_long_scans
))
158 /* And, for the moment, this feature doesn't work on multibyte
160 || !NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
163 || NILP (BVAR (cache_buffer
, cache_long_scans
))
164 || !NILP (BVAR (cache_buffer
, enable_multibyte_characters
)))
166 /* It should be off. */
167 if (cache_buffer
->width_run_cache
)
169 free_region_cache (cache_buffer
->width_run_cache
);
170 cache_buffer
->width_run_cache
= 0;
171 bset_width_table (current_buffer
, Qnil
);
179 || (!NILP (BVAR (cache_buffer
, cache_long_scans
))
180 && NILP (BVAR (cache_buffer
, enable_multibyte_characters
))))
182 /* It should be on. */
183 if (cache_buffer
->width_run_cache
== 0)
185 cache_buffer
->width_run_cache
= new_region_cache ();
186 recompute_width_table (current_buffer
, buffer_display_table ());
189 return cache_buffer
->width_run_cache
;
194 /* Skip some invisible characters starting from POS.
195 This includes characters invisible because of text properties
196 and characters invisible because of overlays.
198 If position POS is followed by invisible characters,
199 skip some of them and return the position after them.
200 Otherwise return POS itself.
202 Set *NEXT_BOUNDARY_P to the next position at which
203 it will be necessary to call this function again.
205 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
206 to a value greater than TO.
208 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
209 take account of overlays that apply only in WINDOW.
211 We don't necessarily skip all the invisible characters after POS
212 because that could take a long time. We skip a reasonable number
213 which can be skipped quickly. If there might be more invisible
214 characters immediately following, then *NEXT_BOUNDARY_P
215 will equal the return value. */
218 skip_invisible (ptrdiff_t pos
, ptrdiff_t *next_boundary_p
, ptrdiff_t to
, Lisp_Object window
)
220 Lisp_Object prop
, position
, overlay_limit
, proplimit
;
221 Lisp_Object buffer
, tmp
;
225 XSETFASTINT (position
, pos
);
226 XSETBUFFER (buffer
, current_buffer
);
228 /* Give faster response for overlay lookup near POS. */
229 recenter_overlay_lists (current_buffer
, pos
);
231 /* We must not advance farther than the next overlay change.
232 The overlay change might change the invisible property;
233 or there might be overlay strings to be displayed there. */
234 overlay_limit
= Fnext_overlay_change (position
);
235 /* As for text properties, this gives a lower bound
236 for where the invisible text property could change. */
237 proplimit
= Fnext_property_change (position
, buffer
, Qt
);
238 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
239 proplimit
= overlay_limit
;
240 /* PROPLIMIT is now a lower bound for the next change
241 in invisible status. If that is plenty far away,
242 use that lower bound. */
243 if (XFASTINT (proplimit
) > pos
+ 100 || XFASTINT (proplimit
) >= to
)
244 *next_boundary_p
= XFASTINT (proplimit
);
245 /* Otherwise, scan for the next `invisible' property change. */
248 /* Don't scan terribly far. */
249 XSETFASTINT (proplimit
, min (pos
+ 100, to
));
250 /* No matter what, don't go past next overlay change. */
251 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
252 proplimit
= overlay_limit
;
253 tmp
= Fnext_single_property_change (position
, Qinvisible
,
255 end
= XFASTINT (tmp
);
257 /* Don't put the boundary in the middle of multibyte form if
258 there is no actual property change. */
260 && !NILP (current_buffer
->enable_multibyte_characters
)
262 while (pos
< end
&& !CHAR_HEAD_P (POS_ADDR (end
)))
265 *next_boundary_p
= end
;
267 /* if the `invisible' property is set, we can skip to
268 the next property change */
269 prop
= Fget_char_property (position
, Qinvisible
,
271 && EQ (XWINDOW (window
)->contents
, buffer
))
273 inv_p
= TEXT_PROP_MEANS_INVISIBLE (prop
);
274 /* When counting columns (window == nil), don't skip over ellipsis text. */
275 if (NILP (window
) ? inv_p
== 1 : inv_p
)
276 return *next_boundary_p
;
280 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
282 DP is a display table or NULL.
284 This macro is used in scan_for_column and in
287 #define MULTIBYTE_BYTES_WIDTH(p, dp, bytes, width) \
291 ch = STRING_CHAR_AND_LENGTH (p, bytes); \
292 if (BYTES_BY_CHAR_HEAD (*p) != bytes) \
296 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, ch))) \
297 width = sanitize_char_width (ASIZE (DISP_CHAR_VECTOR (dp, ch))); \
299 width = CHAR_WIDTH (ch); \
304 DEFUN ("current-column", Fcurrent_column
, Scurrent_column
, 0, 0, 0,
305 doc
: /* Return the horizontal position of point. Beginning of line is column 0.
306 This is calculated by adding together the widths of all the displayed
307 representations of the character between the start of the previous line
308 and point (e.g., control characters will have a width of 2 or 4, tabs
309 will have a variable width).
310 Ignores finite width of frame, which means that this function may return
311 values greater than (frame-width).
312 Whether the line is visible (if `selective-display' is t) has no effect;
313 however, ^M is treated as end of line when `selective-display' is t.
314 Text that has an invisible property is considered as having width 0, unless
315 `buffer-invisibility-spec' specifies that it is replaced by an ellipsis. */)
319 XSETFASTINT (temp
, current_column ());
323 /* Cancel any recorded value of the horizontal position. */
326 invalidate_current_column (void)
328 last_known_column_point
= 0;
332 current_column (void)
335 unsigned char *ptr
, *stop
;
339 int tab_width
= SANE_TAB_WIDTH (current_buffer
);
340 bool ctl_arrow
= !NILP (BVAR (current_buffer
, ctl_arrow
));
341 struct Lisp_Char_Table
*dp
= buffer_display_table ();
343 if (PT
== last_known_column_point
344 && MODIFF
== last_known_column_modified
)
345 return last_known_column
;
347 /* If the buffer has overlays, text properties,
348 or multibyte characters, use a more general algorithm. */
349 if (buffer_intervals (current_buffer
)
350 || buffer_has_overlays ()
352 return current_column_1 ();
354 /* Scan backwards from point to the previous newline,
355 counting width. Tab characters are the only complicated case. */
357 /* Make a pointer for decrementing through the chars before point. */
358 ptr
= BYTE_POS_ADDR (PT_BYTE
- 1) + 1;
359 /* Make a pointer to where consecutive chars leave off,
360 going backwards from point. */
363 else if (PT
<= GPT
|| BEGV
> GPT
)
368 col
= 0, tab_seen
= 0, post_tab
= 0;
377 /* We stopped either for the beginning of the buffer
379 if (ptr
== BEGV_ADDR
)
382 /* It was the gap. Jump back over it. */
386 /* Check whether that brings us to beginning of buffer. */
393 if (dp
&& VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
395 charvec
= DISP_CHAR_VECTOR (dp
, c
);
404 for (i
= n
- 1; i
>= 0; --i
)
406 if (VECTORP (charvec
))
408 /* This should be handled the same as
409 next_element_from_display_vector does it. */
410 Lisp_Object entry
= AREF (charvec
, i
);
412 if (GLYPH_CODE_P (entry
))
413 c
= GLYPH_CODE_CHAR (entry
);
418 if (c
>= 040 && c
< 0177)
422 && EQ (BVAR (current_buffer
, selective_display
), Qt
)))
425 goto start_of_line_found
;
430 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
436 else if (VECTORP (charvec
))
437 /* With a display table entry, C is displayed as is, and
438 not displayed as \NNN or as ^N. If C is a single-byte
439 character, it takes one column. If C is multi-byte in
440 a unibyte buffer, it's translated to unibyte, so it
441 also takes one column. */
444 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
452 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
456 last_known_column
= col
;
457 last_known_column_point
= PT
;
458 last_known_column_modified
= MODIFF
;
464 /* Check the presence of a display property and compute its width.
465 If a property was found and its width was found as well, return
466 its width (>= 0) and set the position of the end of the property
468 Otherwise just return -1. */
470 check_display_width (ptrdiff_t pos
, ptrdiff_t col
, ptrdiff_t *endpos
)
472 Lisp_Object val
, overlay
;
474 if (CONSP (val
= get_char_property_and_overlay
475 (make_number (pos
), Qdisplay
, Qnil
, &overlay
))
476 && EQ (Qspace
, XCAR (val
)))
477 { /* FIXME: Use calc_pixel_width_or_height. */
478 Lisp_Object plist
= XCDR (val
), prop
;
480 EMACS_INT align_to_max
=
481 (col
< MOST_POSITIVE_FIXNUM
- INT_MAX
482 ? (EMACS_INT
) INT_MAX
+ col
483 : MOST_POSITIVE_FIXNUM
);
485 if ((prop
= Fplist_get (plist
, QCwidth
),
486 RANGED_INTEGERP (0, prop
, INT_MAX
))
487 || (prop
= Fplist_get (plist
, QCrelative_width
),
488 RANGED_INTEGERP (0, prop
, INT_MAX
)))
490 else if (FLOATP (prop
) && 0 <= XFLOAT_DATA (prop
)
491 && XFLOAT_DATA (prop
) <= INT_MAX
)
492 width
= (int)(XFLOAT_DATA (prop
) + 0.5);
493 else if ((prop
= Fplist_get (plist
, QCalign_to
),
494 RANGED_INTEGERP (col
, prop
, align_to_max
)))
495 width
= XINT (prop
) - col
;
496 else if (FLOATP (prop
) && col
<= XFLOAT_DATA (prop
)
497 && (XFLOAT_DATA (prop
) <= align_to_max
))
498 width
= (int)(XFLOAT_DATA (prop
) + 0.5) - col
;
503 if (OVERLAYP (overlay
))
504 *endpos
= OVERLAY_POSITION (OVERLAY_END (overlay
));
506 get_property_and_range (pos
, Qdisplay
, &val
, &start
, endpos
, Qnil
);
508 /* For :relative-width, we need to multiply by the column
509 width of the character at POS, if it is greater than 1. */
510 if (!NILP (Fplist_get (plist
, QCrelative_width
))
511 && !NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
514 unsigned char *p
= BYTE_POS_ADDR (CHAR_TO_BYTE (pos
));
516 MULTIBYTE_BYTES_WIDTH (p
, buffer_display_table (), b
, wd
);
525 /* Scanning from the beginning of the current line, stop at the buffer
526 position ENDPOS or at the column GOALCOL or at the end of line, whichever
528 Return the resulting buffer position and column in ENDPOS and GOALCOL.
529 PREVCOL gets set to the column of the previous position (it's always
530 strictly smaller than the goal column). */
532 scan_for_column (ptrdiff_t *endpos
, EMACS_INT
*goalcol
, ptrdiff_t *prevcol
)
534 int tab_width
= SANE_TAB_WIDTH (current_buffer
);
535 bool ctl_arrow
= !NILP (BVAR (current_buffer
, ctl_arrow
));
536 struct Lisp_Char_Table
*dp
= buffer_display_table ();
537 bool multibyte
= !NILP (BVAR (current_buffer
, enable_multibyte_characters
));
538 struct composition_it cmp_it
;
542 /* Start the scan at the beginning of this line with column number 0. */
543 register ptrdiff_t col
= 0, prev_col
= 0;
544 EMACS_INT goal
= goalcol
? *goalcol
: MOST_POSITIVE_FIXNUM
;
545 ptrdiff_t end
= endpos
? *endpos
: PT
;
546 ptrdiff_t scan
, scan_byte
, next_boundary
;
548 scan
= find_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, NULL
, &scan_byte
, 1);
549 next_boundary
= scan
;
551 window
= Fget_buffer_window (Fcurrent_buffer (), Qnil
);
552 w
= ! NILP (window
) ? XWINDOW (window
) : NULL
;
554 memset (&cmp_it
, 0, sizeof cmp_it
);
556 composition_compute_stop_pos (&cmp_it
, scan
, scan_byte
, end
, Qnil
);
558 /* Scan forward to the target position. */
563 /* Occasionally we may need to skip invisible text. */
564 while (scan
== next_boundary
)
566 ptrdiff_t old_scan
= scan
;
567 /* This updates NEXT_BOUNDARY to the next place
568 where we might need to skip more invisible text. */
569 scan
= skip_invisible (scan
, &next_boundary
, end
, Qnil
);
570 if (scan
!= old_scan
)
571 scan_byte
= CHAR_TO_BYTE (scan
);
576 /* Test reaching the goal column. We do this after skipping
577 invisible characters, so that we put point before the
578 character on which the cursor will appear. */
583 { /* Check display property. */
585 int width
= check_display_width (scan
, col
, &endp
);
589 if (endp
> scan
) /* Avoid infinite loops with 0-width overlays. */
592 scan_byte
= CHAR_TO_BYTE (scan
);
598 /* Check composition sequence. */
600 || (scan
== cmp_it
.stop_pos
601 && composition_reseat_it (&cmp_it
, scan
, scan_byte
, end
,
603 composition_update_it (&cmp_it
, scan
, scan_byte
, Qnil
);
606 scan
+= cmp_it
.nchars
;
607 scan_byte
+= cmp_it
.nbytes
;
610 if (cmp_it
.to
== cmp_it
.nglyphs
)
613 composition_compute_stop_pos (&cmp_it
, scan
, scan_byte
, end
,
617 cmp_it
.from
= cmp_it
.to
;
621 c
= FETCH_BYTE (scan_byte
);
623 /* See if there is a display table and it relates
624 to this character. */
627 && ! (multibyte
&& LEADING_CODE_P (c
))
628 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
633 /* This character is displayed using a vector of glyphs.
634 Update the column/position based on those glyphs. */
636 charvec
= DISP_CHAR_VECTOR (dp
, c
);
639 for (i
= 0; i
< n
; i
++)
641 /* This should be handled the same as
642 next_element_from_display_vector does it. */
643 Lisp_Object entry
= AREF (charvec
, i
);
645 if (GLYPH_CODE_P (entry
))
646 c
= GLYPH_CODE_CHAR (entry
);
652 if (c
== '\r' && EQ (BVAR (current_buffer
, selective_display
), Qt
))
657 col
= col
/ tab_width
* tab_width
;
665 /* The display table doesn't affect this character;
666 it displays as itself. */
670 if (c
== '\r' && EQ (BVAR (current_buffer
, selective_display
), Qt
))
675 col
= col
/ tab_width
* tab_width
;
677 else if (multibyte
&& LEADING_CODE_P (c
))
679 /* Start of multi-byte form. */
683 ptr
= BYTE_POS_ADDR (scan_byte
);
684 MULTIBYTE_BYTES_WIDTH (ptr
, dp
, bytes
, width
);
685 /* Subtract one to compensate for the increment
686 that is going to happen below. */
687 scan_byte
+= bytes
- 1;
690 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
692 else if (c
< 040 || c
>= 0177)
703 last_known_column
= col
;
704 last_known_column_point
= PT
;
705 last_known_column_modified
= MODIFF
;
715 /* Return the column number of point
716 by scanning forward from the beginning of the line.
717 This function handles characters that are invisible
718 due to text properties or overlays. */
721 current_column_1 (void)
723 EMACS_INT col
= MOST_POSITIVE_FIXNUM
;
724 ptrdiff_t opoint
= PT
;
726 scan_for_column (&opoint
, &col
, NULL
);
731 #if 0 /* Not used. */
733 /* Return the width in columns of the part of STRING from BEG to END.
734 If BEG is nil, that stands for the beginning of STRING.
735 If END is nil, that stands for the end of STRING. */
738 string_display_width (Lisp_Object string
, Lisp_Object beg
, Lisp_Object end
)
741 unsigned char *ptr
, *stop
;
745 int tab_width
= SANE_TAB_WIDTH (current_buffer
);
746 bool ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
747 struct Lisp_Char_Table
*dp
= buffer_display_table ();
766 /* Make a pointer for decrementing through the chars before point. */
767 ptr
= SDATA (string
) + e
;
768 /* Make a pointer to where consecutive chars leave off,
769 going backwards from point. */
770 stop
= SDATA (string
) + b
;
772 col
= 0, tab_seen
= 0, post_tab
= 0;
780 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
781 col
+= ASIZE (DISP_CHAR_VECTOR (dp
, c
));
782 else if (c
>= 040 && c
< 0177)
789 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
796 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
801 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
811 DEFUN ("indent-to", Findent_to
, Sindent_to
, 1, 2, "NIndent to column: ",
812 doc
: /* Indent from point with tabs and spaces until COLUMN is reached.
813 Optional second argument MINIMUM says always do at least MINIMUM spaces
814 even if that goes past COLUMN; by default, MINIMUM is zero.
816 The return value is COLUMN. */)
817 (Lisp_Object column
, Lisp_Object minimum
)
820 register ptrdiff_t fromcol
;
821 int tab_width
= SANE_TAB_WIDTH (current_buffer
);
823 CHECK_NUMBER (column
);
825 XSETFASTINT (minimum
, 0);
826 CHECK_NUMBER (minimum
);
828 fromcol
= current_column ();
829 mincol
= fromcol
+ XINT (minimum
);
830 if (mincol
< XINT (column
)) mincol
= XINT (column
);
832 if (fromcol
== mincol
)
833 return make_number (mincol
);
835 if (indent_tabs_mode
)
838 XSETFASTINT (n
, mincol
/ tab_width
- fromcol
/ tab_width
);
839 if (XFASTINT (n
) != 0)
841 Finsert_char (make_number ('\t'), n
, Qt
);
843 fromcol
= (mincol
/ tab_width
) * tab_width
;
847 XSETFASTINT (column
, mincol
- fromcol
);
848 Finsert_char (make_number (' '), column
, Qt
);
850 last_known_column
= mincol
;
851 last_known_column_point
= PT
;
852 last_known_column_modified
= MODIFF
;
854 XSETINT (column
, mincol
);
859 DEFUN ("current-indentation", Fcurrent_indentation
, Scurrent_indentation
,
861 doc
: /* Return the indentation of the current line.
862 This is the horizontal position of the character
863 following any initial whitespace. */)
868 find_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, NULL
, &posbyte
, 1);
869 return make_number (position_indentation (posbyte
));
873 position_indentation (ptrdiff_t pos_byte
)
875 register ptrdiff_t column
= 0;
876 int tab_width
= SANE_TAB_WIDTH (current_buffer
);
877 register unsigned char *p
;
878 register unsigned char *stop
;
879 unsigned char *start
;
880 ptrdiff_t next_boundary_byte
= pos_byte
;
881 ptrdiff_t ceiling
= next_boundary_byte
;
883 p
= BYTE_POS_ADDR (pos_byte
);
884 /* STOP records the value of P at which we will need
885 to think about the gap, or about invisible text,
886 or about the end of the buffer. */
888 /* START records the starting value of P. */
894 ptrdiff_t stop_pos_byte
;
896 /* If we have updated P, set POS_BYTE to match.
897 The first time we enter the loop, POS_BYTE is already right. */
899 pos_byte
= PTR_BYTE_POS (p
);
900 /* Consider the various reasons STOP might have been set here. */
901 if (pos_byte
== ZV_BYTE
)
903 if (pos_byte
== next_boundary_byte
)
905 ptrdiff_t next_boundary
;
906 ptrdiff_t pos
= BYTE_TO_CHAR (pos_byte
);
907 pos
= skip_invisible (pos
, &next_boundary
, ZV
, Qnil
);
908 pos_byte
= CHAR_TO_BYTE (pos
);
909 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
911 if (pos_byte
>= ceiling
)
912 ceiling
= BUFFER_CEILING_OF (pos_byte
) + 1;
913 /* Compute the next place we need to stop and think,
914 and set STOP accordingly. */
915 stop_pos_byte
= min (ceiling
, next_boundary_byte
);
916 /* The -1 and +1 arrange to point at the first byte of gap
917 (if STOP_POS_BYTE is the position of the gap)
918 rather than at the data after the gap. */
920 stop
= BYTE_POS_ADDR (stop_pos_byte
- 1) + 1;
921 p
= BYTE_POS_ADDR (pos_byte
);
926 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
932 column
+= tab_width
- column
% tab_width
;
935 if (ASCII_CHAR_P (p
[-1])
936 || NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
940 pos_byte
= PTR_BYTE_POS (p
- 1);
941 c
= FETCH_MULTIBYTE_CHAR (pos_byte
);
942 if (CHAR_HAS_CATEGORY (c
, ' '))
946 p
= BYTE_POS_ADDR (pos_byte
);
955 /* Test whether the line beginning at POS is indented beyond COLUMN.
956 Blank lines are treated as if they had the same indentation as the
960 indented_beyond_p (ptrdiff_t pos
, ptrdiff_t pos_byte
, EMACS_INT column
)
962 while (pos
> BEGV
&& FETCH_BYTE (pos_byte
) == '\n')
964 DEC_BOTH (pos
, pos_byte
);
965 pos
= find_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
,
966 -1, NULL
, &pos_byte
, 0);
968 return position_indentation (pos_byte
) >= column
;
971 DEFUN ("move-to-column", Fmove_to_column
, Smove_to_column
, 1, 2,
973 doc
: /* Move point to column COLUMN in the current line.
974 Interactively, COLUMN is the value of prefix numeric argument.
975 The column of a character is calculated by adding together the widths
976 as displayed of the previous characters in the line.
977 This function ignores line-continuation;
978 there is no upper limit on the column number a character can have
979 and horizontal scrolling has no effect.
981 If specified column is within a character, point goes after that character.
982 If it's past end of line, point goes to end of line.
984 Optional second argument FORCE non-nil means if COLUMN is in the
985 middle of a tab character, change it to spaces.
986 In addition, if FORCE is t, and the line is too short to reach
987 COLUMN, add spaces/tabs to get there.
989 The return value is the current column. */)
990 (Lisp_Object column
, Lisp_Object force
)
992 ptrdiff_t pos
, prev_col
;
996 CHECK_NATNUM (column
);
997 goal
= XINT (column
);
1001 scan_for_column (&pos
, &col
, &prev_col
);
1005 /* If a tab char made us overshoot, change it to spaces
1006 and scan through it again. */
1007 if (!NILP (force
) && col
> goal
)
1010 ptrdiff_t pos_byte
= PT_BYTE
;
1013 c
= FETCH_CHAR (pos_byte
);
1014 if (c
== '\t' && prev_col
< goal
)
1016 ptrdiff_t goal_pt
, goal_pt_byte
;
1018 /* Insert spaces in front of the tab to reach GOAL. Do this
1019 first so that a marker at the end of the tab gets
1021 SET_PT_BOTH (PT
- 1, PT_BYTE
- 1);
1022 Finsert_char (make_number (' '), make_number (goal
- prev_col
), Qt
);
1024 /* Now delete the tab, and indent to COL. */
1025 del_range (PT
, PT
+ 1);
1027 goal_pt_byte
= PT_BYTE
;
1028 Findent_to (make_number (col
), Qnil
);
1029 SET_PT_BOTH (goal_pt
, goal_pt_byte
);
1031 /* Set the last_known... vars consistently. */
1036 /* If line ends prematurely, add space to the end. */
1037 if (col
< goal
&& EQ (force
, Qt
))
1038 Findent_to (make_number (col
= goal
), Qnil
);
1040 last_known_column
= col
;
1041 last_known_column_point
= PT
;
1042 last_known_column_modified
= MODIFF
;
1044 return make_number (col
);
1047 /* compute_motion: compute buffer posn given screen posn and vice versa */
1049 static struct position val_compute_motion
;
1051 /* Scan the current buffer forward from offset FROM, pretending that
1052 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1053 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1054 and return the ending buffer position and screen location. If we
1055 can't hit the requested column exactly (because of a tab or other
1056 multi-column character), overshoot.
1058 DID_MOTION is true if FROMHPOS has already accounted for overlay strings
1059 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1060 earlier call to compute_motion. The other common case is that FROMHPOS
1061 is zero and FROM is a position that "belongs" at column zero, but might
1062 be shifted by overlay strings; in this case DID_MOTION should be false.
1064 WIDTH is the number of columns available to display text;
1065 compute_motion uses this to handle continuation lines and such.
1066 If WIDTH is -1, use width of window's text area adjusted for
1067 continuation glyph when needed.
1069 HSCROLL is the number of columns not being displayed at the left
1070 margin; this is usually taken from a window's hscroll member.
1071 TAB_OFFSET is the number of columns of the first tab that aren't
1072 being displayed, perhaps because of a continuation line or
1075 compute_motion returns a pointer to a struct position. The bufpos
1076 member gives the buffer position at the end of the scan, and hpos
1077 and vpos give its cartesian location. prevhpos is the column at
1078 which the character before bufpos started, and contin is non-zero
1079 if we reached the current line by continuing the previous.
1081 Note that FROMHPOS and TOHPOS should be expressed in real screen
1082 columns, taking HSCROLL and the truncation glyph at the left margin
1083 into account. That is, beginning-of-line moves you to the hpos
1084 -HSCROLL + (HSCROLL > 0).
1086 For example, to find the buffer position of column COL of line LINE
1087 of a certain window, pass the window's starting location as FROM
1088 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1089 Pass the buffer's ZV as TO, to limit the scan to the end of the
1090 visible section of the buffer, and pass LINE and COL as TOVPOS and
1093 When displaying in window w, a typical formula for WIDTH is:
1096 - (has_vertical_scroll_bars
1097 ? WINDOW_CONFIG_SCROLL_BAR_COLS (window)
1098 : (window_width + window_left != frame_cols))
1101 window_width is w->total_cols,
1102 window_left is w->left_col,
1103 has_vertical_scroll_bars is
1104 WINDOW_HAS_VERTICAL_SCROLL_BAR (window)
1105 and frame_cols = FRAME_COLS (XFRAME (window->frame))
1107 Or you can let window_body_cols do this all for you, and write:
1108 window_body_cols (w) - 1
1110 The `-1' accounts for the continuation-line backslashes; the rest
1111 accounts for window borders if the window is split horizontally, and
1112 the scroll bars if they are turned on. */
1115 compute_motion (ptrdiff_t from
, ptrdiff_t frombyte
, EMACS_INT fromvpos
,
1116 EMACS_INT fromhpos
, bool did_motion
, ptrdiff_t to
,
1117 EMACS_INT tovpos
, EMACS_INT tohpos
, EMACS_INT width
,
1118 ptrdiff_t hscroll
, int tab_offset
, struct window
*win
)
1120 EMACS_INT hpos
= fromhpos
;
1121 EMACS_INT vpos
= fromvpos
;
1126 int tab_width
= SANE_TAB_WIDTH (current_buffer
);
1127 bool ctl_arrow
= !NILP (BVAR (current_buffer
, ctl_arrow
));
1128 struct Lisp_Char_Table
*dp
= window_display_table (win
);
1130 = (INTEGERP (BVAR (current_buffer
, selective_display
))
1131 ? XINT (BVAR (current_buffer
, selective_display
))
1132 : !NILP (BVAR (current_buffer
, selective_display
)) ? -1 : 0);
1133 ptrdiff_t selective_rlen
1134 = (selective
&& dp
&& VECTORP (DISP_INVIS_VECTOR (dp
))
1135 ? ASIZE (DISP_INVIS_VECTOR (dp
)) : 0);
1136 /* The next location where the `invisible' property changes, or an
1137 overlay starts or ends. */
1138 ptrdiff_t next_boundary
= from
;
1140 /* For computing runs of characters with similar widths.
1141 Invariant: width_run_width is zero, or all the characters
1142 from width_run_start to width_run_end have a fixed width of
1144 ptrdiff_t width_run_start
= from
;
1145 ptrdiff_t width_run_end
= from
;
1146 ptrdiff_t width_run_width
= 0;
1147 Lisp_Object
*width_table
;
1149 /* The next buffer pos where we should consult the width run cache. */
1150 ptrdiff_t next_width_run
= from
;
1153 bool multibyte
= !NILP (BVAR (current_buffer
, enable_multibyte_characters
));
1154 /* If previous char scanned was a wide character,
1155 this is the column where it ended. Otherwise, this is 0. */
1156 EMACS_INT wide_column_end_hpos
= 0;
1157 ptrdiff_t prev_pos
; /* Previous buffer position. */
1158 ptrdiff_t prev_pos_byte
; /* Previous buffer position. */
1159 EMACS_INT prev_hpos
= 0;
1160 EMACS_INT prev_vpos
= 0;
1161 EMACS_INT contin_hpos
; /* HPOS of last column of continued line. */
1162 int prev_tab_offset
; /* Previous tab offset. */
1163 int continuation_glyph_width
;
1164 struct buffer
*cache_buffer
= current_buffer
;
1165 struct region_cache
*width_cache
;
1167 struct composition_it cmp_it
;
1169 XSETWINDOW (window
, win
);
1171 if (cache_buffer
->base_buffer
)
1172 cache_buffer
= cache_buffer
->base_buffer
;
1173 width_cache
= width_run_cache_on_off ();
1174 if (dp
== buffer_display_table ())
1175 width_table
= (VECTORP (BVAR (current_buffer
, width_table
))
1176 ? XVECTOR (BVAR (current_buffer
, width_table
))->contents
1179 /* If the window has its own display table, we can't use the width
1180 run cache, because that's based on the buffer's display table. */
1183 /* Negative width means use all available text columns. */
1186 width
= window_body_width (win
, 0);
1187 /* We must make room for continuation marks if we don't have fringes. */
1188 #ifdef HAVE_WINDOW_SYSTEM
1189 if (!FRAME_WINDOW_P (XFRAME (win
->frame
)))
1194 continuation_glyph_width
= 1;
1195 #ifdef HAVE_WINDOW_SYSTEM
1196 if (FRAME_WINDOW_P (XFRAME (win
->frame
)))
1197 continuation_glyph_width
= 0; /* In the fringe. */
1203 /* It's just impossible to be too paranoid here. */
1204 eassert (from
== BYTE_TO_CHAR (frombyte
) && frombyte
== CHAR_TO_BYTE (from
));
1206 pos
= prev_pos
= from
;
1207 pos_byte
= prev_pos_byte
= frombyte
;
1209 prev_tab_offset
= tab_offset
;
1210 memset (&cmp_it
, 0, sizeof cmp_it
);
1212 composition_compute_stop_pos (&cmp_it
, pos
, pos_byte
, to
, Qnil
);
1216 while (pos
== next_boundary
)
1218 ptrdiff_t pos_here
= pos
;
1221 /* Don't skip invisible if we are already at the margin. */
1222 if (vpos
> tovpos
|| (vpos
== tovpos
&& hpos
>= tohpos
))
1224 if (contin_hpos
&& prev_hpos
== 0
1226 && (contin_hpos
== width
|| wide_column_end_hpos
> width
))
1227 { /* Line breaks because we can't put the character at the
1228 previous line any more. It is not the multi-column
1229 character continued in middle. Go back to previous
1230 buffer position, screen position, and set tab offset
1231 to previous value. It's the beginning of the
1234 pos_byte
= prev_pos_byte
;
1237 tab_offset
= prev_tab_offset
;
1242 /* If the caller says that the screen position came from an earlier
1243 call to compute_motion, then we've already accounted for the
1244 overlay strings at point. This is only true the first time
1245 through, so clear the flag after testing it. */
1247 /* We need to skip past the overlay strings. Currently those
1248 strings must not contain TAB;
1249 if we want to relax that restriction, something will have
1250 to be changed here. */
1252 unsigned char *ovstr
;
1253 ptrdiff_t ovlen
= overlay_strings (pos
, win
, &ovstr
);
1254 hpos
+= ((multibyte
&& ovlen
> 0)
1255 ? strwidth ((char *) ovstr
, ovlen
) : ovlen
);
1262 /* Advance POS past invisible characters
1263 (but not necessarily all that there are here),
1264 and store in next_boundary the next position where
1265 we need to call skip_invisible. */
1266 newpos
= skip_invisible (pos
, &next_boundary
, to
, window
);
1270 pos
= min (to
, newpos
);
1271 pos_byte
= CHAR_TO_BYTE (pos
);
1275 if (newpos
!= pos_here
)
1278 pos_byte
= CHAR_TO_BYTE (pos
);
1282 /* Handle right margin. */
1283 /* Note on a wide-column character.
1285 Characters are classified into the following three categories
1286 according to the width (columns occupied on screen).
1288 (1) single-column character: ex. `a'
1289 (2) multi-column character: ex. `^A', TAB, `\033'
1290 (3) wide-column character: ex. Japanese character, Chinese character
1291 (In the following example, `W_' stands for them.)
1293 Multi-column characters can be divided around the right margin,
1294 but wide-column characters cannot.
1298 (*) The cursor is placed on the next character after the point.
1302 j ^---- next after the point
1303 ^--- next char. after the point.
1305 In case of sigle-column character
1309 033 ^---- next after the point, next char. after the point.
1311 In case of multi-column character
1315 W_ ^---- next after the point
1316 ^---- next char. after the point.
1318 In case of wide-column character
1320 The problem here is continuation at a wide-column character.
1321 In this case, the line may shorter less than WIDTH.
1322 And we find the continuation AFTER it occurs.
1328 EMACS_INT total_width
= width
+ continuation_glyph_width
;
1331 if (!NILP (Vtruncate_partial_width_windows
)
1332 && (total_width
< FRAME_COLS (XFRAME (WINDOW_FRAME (win
)))))
1334 if (INTEGERP (Vtruncate_partial_width_windows
))
1336 = total_width
< XFASTINT (Vtruncate_partial_width_windows
);
1341 if (hscroll
|| truncate
1342 || !NILP (BVAR (current_buffer
, truncate_lines
)))
1344 /* Truncating: skip to newline, unless we are already past
1345 TO (we need to go back below). */
1348 pos
= find_before_next_newline (pos
, to
, 1, &pos_byte
);
1350 /* If we just skipped next_boundary,
1351 loop around in the main while
1353 if (pos
>= next_boundary
)
1354 next_boundary
= pos
+ 1;
1357 prev_tab_offset
= tab_offset
;
1363 /* Remember the previous value. */
1364 prev_tab_offset
= tab_offset
;
1366 if (wide_column_end_hpos
> width
)
1369 tab_offset
+= prev_hpos
;
1373 tab_offset
+= width
;
1377 contin_hpos
= prev_hpos
;
1383 /* Stop if past the target buffer position or screen position. */
1386 /* Go back to the previous position. */
1388 pos_byte
= prev_pos_byte
;
1391 tab_offset
= prev_tab_offset
;
1393 /* NOTE on contin_hpos, hpos, and prev_hpos.
1397 W_ ^---- contin_hpos
1403 if (contin_hpos
&& prev_hpos
== 0
1404 && contin_hpos
< width
&& !wide_column_end_hpos
)
1406 /* Line breaking occurs in the middle of multi-column
1407 character. Go back to previous line. */
1414 if (vpos
> tovpos
|| (vpos
== tovpos
&& hpos
>= tohpos
))
1416 if (contin_hpos
&& prev_hpos
== 0
1418 && (contin_hpos
== width
|| wide_column_end_hpos
> width
))
1419 { /* Line breaks because we can't put the character at the
1420 previous line any more. It is not the multi-column
1421 character continued in middle. Go back to previous
1422 buffer position, screen position, and set tab offset
1423 to previous value. It's the beginning of the
1426 pos_byte
= prev_pos_byte
;
1429 tab_offset
= prev_tab_offset
;
1433 if (pos
== ZV
) /* We cannot go beyond ZV. Stop here. */
1439 prev_pos_byte
= pos_byte
;
1440 wide_column_end_hpos
= 0;
1442 /* Consult the width run cache to see if we can avoid inspecting
1443 the text character-by-character. */
1444 if (width_cache
&& pos
>= next_width_run
)
1448 = region_cache_forward (cache_buffer
, width_cache
, pos
, &run_end
);
1450 /* A width of zero means the character's width varies (like
1451 a tab), is meaningless (like a newline), or we just don't
1452 want to skip over it for some other reason. */
1453 if (common_width
!= 0)
1455 ptrdiff_t run_end_hpos
;
1457 /* Don't go past the final buffer posn the user
1462 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1464 /* Don't go past the final horizontal position the user
1466 if (vpos
== tovpos
&& run_end_hpos
> tohpos
)
1468 run_end
= pos
+ (tohpos
- hpos
) / common_width
;
1469 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1472 /* Don't go past the margin. */
1473 if (run_end_hpos
>= width
)
1475 run_end
= pos
+ (width
- hpos
) / common_width
;
1476 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1479 hpos
= run_end_hpos
;
1481 prev_hpos
= hpos
- common_width
;
1485 pos_byte
= CHAR_TO_BYTE (pos
);
1489 next_width_run
= run_end
+ 1;
1492 /* We have to scan the text character-by-character. */
1496 Lisp_Object charvec
;
1498 /* Check composition sequence. */
1500 || (pos
== cmp_it
.stop_pos
1501 && composition_reseat_it (&cmp_it
, pos
, pos_byte
, to
, win
,
1503 composition_update_it (&cmp_it
, pos
, pos_byte
, Qnil
);
1506 pos
+= cmp_it
.nchars
;
1507 pos_byte
+= cmp_it
.nbytes
;
1508 hpos
+= cmp_it
.width
;
1509 if (cmp_it
.to
== cmp_it
.nglyphs
)
1512 composition_compute_stop_pos (&cmp_it
, pos
, pos_byte
, to
,
1516 cmp_it
.from
= cmp_it
.to
;
1520 c
= FETCH_BYTE (pos_byte
);
1523 /* Perhaps add some info to the width_run_cache. */
1526 /* Is this character part of the current run? If so, extend
1528 if (pos
- 1 == width_run_end
1529 && XFASTINT (width_table
[c
]) == width_run_width
)
1530 width_run_end
= pos
;
1532 /* The previous run is over, since this is a character at a
1533 different position, or a different width. */
1536 /* Have we accumulated a run to put in the cache?
1537 (Currently, we only cache runs of width == 1). */
1538 if (width_run_start
< width_run_end
1539 && width_run_width
== 1)
1540 know_region_cache (cache_buffer
, width_cache
,
1541 width_run_start
, width_run_end
);
1543 /* Start recording a new width run. */
1544 width_run_width
= XFASTINT (width_table
[c
]);
1545 width_run_start
= pos
- 1;
1546 width_run_end
= pos
;
1551 && ! (multibyte
&& LEADING_CODE_P (c
))
1552 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
1554 charvec
= DISP_CHAR_VECTOR (dp
, c
);
1555 n
= ASIZE (charvec
);
1563 for (i
= 0; i
< n
; ++i
)
1565 if (VECTORP (charvec
))
1567 /* This should be handled the same as
1568 next_element_from_display_vector does it. */
1569 Lisp_Object entry
= AREF (charvec
, i
);
1571 if (GLYPH_CODE_P (entry
))
1572 c
= GLYPH_CODE_CHAR (entry
);
1577 if (c
>= 040 && c
< 0177)
1581 int tem
= ((hpos
+ tab_offset
+ hscroll
- (hscroll
> 0))
1585 hpos
+= tab_width
- tem
;
1590 && indented_beyond_p (pos
, pos_byte
, selective
))
1592 /* If (pos == to), we don't have to take care of
1593 selective display. */
1596 /* Skip any number of invisible lines all at once */
1599 pos
= find_before_next_newline (pos
, to
, 1, &pos_byte
);
1601 INC_BOTH (pos
, pos_byte
);
1604 && indented_beyond_p (pos
, pos_byte
,
1606 /* Allow for the " ..." that is displayed for them. */
1609 hpos
+= selective_rlen
;
1613 DEC_BOTH (pos
, pos_byte
);
1614 /* We have skipped the invis text, but not the
1620 /* A visible line. */
1624 /* Count the truncation glyph on column 0 */
1626 hpos
+= continuation_glyph_width
;
1631 else if (c
== CR
&& selective
< 0)
1633 /* In selective display mode,
1634 everything from a ^M to the end of the line is invisible.
1635 Stop *before* the real newline. */
1637 pos
= find_before_next_newline (pos
, to
, 1, &pos_byte
);
1638 /* If we just skipped next_boundary,
1639 loop around in the main while
1641 if (pos
> next_boundary
)
1642 next_boundary
= pos
;
1643 /* Allow for the " ..." that is displayed for them. */
1646 hpos
+= selective_rlen
;
1651 else if (multibyte
&& LEADING_CODE_P (c
))
1653 /* Start of multi-byte form. */
1655 int mb_bytes
, mb_width
;
1657 pos_byte
--; /* rewind POS_BYTE */
1658 ptr
= BYTE_POS_ADDR (pos_byte
);
1659 MULTIBYTE_BYTES_WIDTH (ptr
, dp
, mb_bytes
, mb_width
);
1660 pos_byte
+= mb_bytes
;
1661 if (mb_width
> 1 && BYTES_BY_CHAR_HEAD (*ptr
) == mb_bytes
)
1662 wide_column_end_hpos
= hpos
+ mb_width
;
1665 else if (VECTORP (charvec
))
1668 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
1675 /* Remember any final width run in the cache. */
1677 && width_run_width
== 1
1678 && width_run_start
< width_run_end
)
1679 know_region_cache (cache_buffer
, width_cache
,
1680 width_run_start
, width_run_end
);
1682 val_compute_motion
.bufpos
= pos
;
1683 val_compute_motion
.bytepos
= pos_byte
;
1684 val_compute_motion
.hpos
= hpos
;
1685 val_compute_motion
.vpos
= vpos
;
1686 if (contin_hpos
&& prev_hpos
== 0)
1687 val_compute_motion
.prevhpos
= contin_hpos
;
1689 val_compute_motion
.prevhpos
= prev_hpos
;
1691 /* Nonzero if have just continued a line */
1692 val_compute_motion
.contin
= (contin_hpos
&& prev_hpos
== 0);
1695 return &val_compute_motion
;
1699 DEFUN ("compute-motion", Fcompute_motion
, Scompute_motion
, 7, 7, 0,
1700 doc
: /* Scan through the current buffer, calculating screen position.
1701 Scan the current buffer forward from offset FROM,
1702 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1703 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1704 and return the ending buffer position and screen location.
1706 If TOPOS is nil, the actual width and height of the window's
1709 There are three additional arguments:
1711 WIDTH is the number of columns available to display text;
1712 this affects handling of continuation lines. A value of nil
1713 corresponds to the actual number of available text columns.
1715 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1716 HSCROLL is the number of columns not being displayed at the left
1717 margin; this is usually taken from a window's hscroll member.
1718 TAB-OFFSET is the number of columns of the first tab that aren't
1719 being displayed, perhaps because the line was continued within it.
1720 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1722 WINDOW is the window to operate on. It is used to choose the display table;
1723 if it is showing the current buffer, it is used also for
1724 deciding which overlay properties apply.
1725 Note that `compute-motion' always operates on the current buffer.
1727 The value is a list of five elements:
1728 (POS HPOS VPOS PREVHPOS CONTIN)
1729 POS is the buffer position where the scan stopped.
1730 VPOS is the vertical position where the scan stopped.
1731 HPOS is the horizontal position where the scan stopped.
1733 PREVHPOS is the horizontal position one character back from POS.
1734 CONTIN is t if a line was continued after (or within) the previous character.
1736 For example, to find the buffer position of column COL of line LINE
1737 of a certain window, pass the window's starting location as FROM
1738 and the window's upper-left coordinates as FROMPOS.
1739 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1740 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1741 (Lisp_Object from
, Lisp_Object frompos
, Lisp_Object to
, Lisp_Object topos
,
1742 Lisp_Object width
, Lisp_Object offsets
, Lisp_Object window
)
1745 Lisp_Object bufpos
, hpos
, vpos
, prevhpos
;
1746 struct position
*pos
;
1750 CHECK_NUMBER_COERCE_MARKER (from
);
1751 CHECK_CONS (frompos
);
1752 CHECK_NUMBER_CAR (frompos
);
1753 CHECK_NUMBER_CDR (frompos
);
1754 CHECK_NUMBER_COERCE_MARKER (to
);
1758 CHECK_NUMBER_CAR (topos
);
1759 CHECK_NUMBER_CDR (topos
);
1762 CHECK_NUMBER (width
);
1764 if (!NILP (offsets
))
1766 CHECK_CONS (offsets
);
1767 CHECK_NUMBER_CAR (offsets
);
1768 CHECK_NUMBER_CDR (offsets
);
1769 if (! (0 <= XINT (XCAR (offsets
)) && XINT (XCAR (offsets
)) <= PTRDIFF_MAX
1770 && 0 <= XINT (XCDR (offsets
)) && XINT (XCDR (offsets
)) <= INT_MAX
))
1771 args_out_of_range (XCAR (offsets
), XCDR (offsets
));
1772 hscroll
= XINT (XCAR (offsets
));
1773 tab_offset
= XINT (XCDR (offsets
));
1776 hscroll
= tab_offset
= 0;
1778 w
= decode_live_window (window
);
1780 if (XINT (from
) < BEGV
|| XINT (from
) > ZV
)
1781 args_out_of_range_3 (from
, make_number (BEGV
), make_number (ZV
));
1782 if (XINT (to
) < BEGV
|| XINT (to
) > ZV
)
1783 args_out_of_range_3 (to
, make_number (BEGV
), make_number (ZV
));
1785 pos
= compute_motion (XINT (from
), CHAR_TO_BYTE (XINT (from
)),
1786 XINT (XCDR (frompos
)),
1787 XINT (XCAR (frompos
)), 0,
1790 ? window_internal_height (w
)
1791 : XINT (XCDR (topos
))),
1793 ? (window_body_width (w
, 0)
1795 #ifdef HAVE_WINDOW_SYSTEM
1796 FRAME_WINDOW_P (XFRAME (w
->frame
)) ? 0 :
1799 : XINT (XCAR (topos
))),
1800 (NILP (width
) ? -1 : XINT (width
)),
1801 hscroll
, tab_offset
, w
);
1803 XSETFASTINT (bufpos
, pos
->bufpos
);
1804 XSETINT (hpos
, pos
->hpos
);
1805 XSETINT (vpos
, pos
->vpos
);
1806 XSETINT (prevhpos
, pos
->prevhpos
);
1808 return list5 (bufpos
, hpos
, vpos
, prevhpos
, pos
->contin
? Qt
: Qnil
);
1811 /* Fvertical_motion and vmotion. */
1813 static struct position val_vmotion
;
1816 vmotion (register ptrdiff_t from
, register ptrdiff_t from_byte
,
1817 register EMACS_INT vtarget
, struct window
*w
)
1819 ptrdiff_t hscroll
= w
->hscroll
;
1820 struct position pos
;
1821 /* VPOS is cumulative vertical position, changed as from is changed. */
1822 register EMACS_INT vpos
= 0;
1824 register ptrdiff_t first
;
1825 ptrdiff_t lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
1827 = (INTEGERP (BVAR (current_buffer
, selective_display
))
1828 ? clip_to_bounds (-1, XINT (BVAR (current_buffer
, selective_display
)),
1830 : !NILP (BVAR (current_buffer
, selective_display
)) ? -1 : 0);
1833 /* This is the object we use for fetching character properties. */
1834 Lisp_Object text_prop_object
;
1836 XSETWINDOW (window
, w
);
1838 /* If the window contains this buffer, use it for getting text properties.
1839 Otherwise use the current buffer as arg for doing that. */
1840 if (EQ (w
->contents
, Fcurrent_buffer ()))
1841 text_prop_object
= window
;
1843 text_prop_object
= Fcurrent_buffer ();
1845 if (vpos
>= vtarget
)
1847 /* To move upward, go a line at a time until
1848 we have gone at least far enough. */
1852 while ((vpos
> vtarget
|| first
) && from
> BEGV
)
1854 ptrdiff_t bytepos
= from_byte
;
1855 Lisp_Object propval
;
1858 DEC_BOTH (prevline
, bytepos
);
1859 prevline
= find_newline_no_quit (prevline
, bytepos
, -1, &bytepos
);
1861 while (prevline
> BEGV
1863 && indented_beyond_p (prevline
, bytepos
, selective
))
1864 /* Watch out for newlines with `invisible' property.
1865 When moving upward, check the newline before. */
1866 || (propval
= Fget_char_property (make_number (prevline
- 1),
1869 TEXT_PROP_MEANS_INVISIBLE (propval
))))
1871 DEC_BOTH (prevline
, bytepos
);
1872 prevline
= find_newline_no_quit (prevline
, bytepos
, -1, &bytepos
);
1874 pos
= *compute_motion (prevline
, bytepos
, 0, lmargin
, 0, from
,
1875 /* Don't care for VPOS... */
1876 1 << (BITS_PER_SHORT
- 1),
1878 1 << (BITS_PER_SHORT
- 1),
1883 from_byte
= bytepos
;
1886 /* If we made exactly the desired vertical distance, or
1887 if we hit beginning of buffer, return point found. */
1888 if (vpos
>= vtarget
)
1890 val_vmotion
.bufpos
= from
;
1891 val_vmotion
.bytepos
= from_byte
;
1892 val_vmotion
.vpos
= vpos
;
1893 val_vmotion
.hpos
= lmargin
;
1894 val_vmotion
.contin
= 0;
1895 val_vmotion
.prevhpos
= 0;
1896 return &val_vmotion
;
1899 /* Otherwise find the correct spot by moving down. */
1902 /* Moving downward is simple, but must calculate from
1903 beg of line to determine hpos of starting point. */
1905 if (from
> BEGV
&& FETCH_BYTE (from_byte
- 1) != '\n')
1908 Lisp_Object propval
;
1910 prevline
= find_newline_no_quit (from
, from_byte
, -1, &bytepos
);
1911 while (prevline
> BEGV
1913 && indented_beyond_p (prevline
, bytepos
, selective
))
1914 /* Watch out for newlines with `invisible' property.
1915 When moving downward, check the newline after. */
1916 || (propval
= Fget_char_property (make_number (prevline
),
1919 TEXT_PROP_MEANS_INVISIBLE (propval
))))
1921 DEC_BOTH (prevline
, bytepos
);
1922 prevline
= find_newline_no_quit (prevline
, bytepos
, -1, &bytepos
);
1924 pos
= *compute_motion (prevline
, bytepos
, 0, lmargin
, 0, from
,
1925 /* Don't care for VPOS... */
1926 1 << (BITS_PER_SHORT
- 1),
1928 1 << (BITS_PER_SHORT
- 1),
1938 return compute_motion (from
, from_byte
, vpos
, pos
.hpos
, did_motion
,
1939 ZV
, vtarget
, - (1 << (BITS_PER_SHORT
- 1)),
1943 /* In window W (derived from WINDOW), return x coordinate for column
1944 COL (derived from COLUMN). */
1946 window_column_x (struct window
*w
, Lisp_Object window
,
1947 double col
, Lisp_Object column
)
1949 double x
= col
* FRAME_COLUMN_WIDTH (XFRAME (w
->frame
)) + 0.5;
1951 /* FIXME: Should this be limited to W's dimensions? */
1952 if (! (INT_MIN
<= x
&& x
<= INT_MAX
))
1953 args_out_of_range (window
, column
);
1958 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 3, 0,
1959 doc
: /* Move point to start of the screen line LINES lines down.
1960 If LINES is negative, this means moving up.
1962 This function is an ordinary cursor motion function
1963 which calculates the new position based on how text would be displayed.
1964 The new position may be the start of a line,
1965 or just the start of a continuation line.
1966 The function returns number of screen lines moved over;
1967 that usually equals LINES, but may be closer to zero
1968 if beginning or end of buffer was reached.
1970 The optional second argument WINDOW specifies the window to use for
1971 parameters such as width, horizontal scrolling, and so on.
1972 The default is to use the selected window's parameters.
1974 LINES can optionally take the form (COLS . LINES), in which case the
1975 motion will not stop at the start of a screen line but COLS column
1976 from the visual start of the line (if such exists on that line, that
1977 is). If the line is scrolled horizontally, COLS is interpreted
1978 visually, i.e., as addition to the columns of text beyond the left
1981 The optional third argument CUR-COL specifies the horizontal
1982 window-relative coordinate of point, in units of frame's canonical
1983 character width, where the function is invoked. If this argument is
1984 omitted or nil, the function will determine the point coordinate by
1985 going back to the beginning of the line.
1987 `vertical-motion' always uses the current buffer,
1988 regardless of which buffer is displayed in WINDOW.
1989 This is consistent with other cursor motion functions
1990 and makes it possible to use `vertical-motion' in any buffer,
1991 whether or not it is currently displayed in some window. */)
1992 (Lisp_Object lines
, Lisp_Object window
, Lisp_Object cur_col
)
1997 Lisp_Object old_buffer
;
1998 EMACS_INT old_charpos
IF_LINT (= 0), old_bytepos
IF_LINT (= 0);
2000 void *itdata
= NULL
;
2002 /* Allow LINES to be of the form (HPOS . VPOS) aka (COLUMNS . LINES). */
2003 bool lcols_given
= CONSP (lines
);
2006 lcols
= XCAR (lines
);
2007 lines
= XCDR (lines
);
2010 CHECK_NUMBER (lines
);
2011 w
= decode_live_window (window
);
2014 if (XBUFFER (w
->contents
) != current_buffer
)
2016 /* Set the window's buffer temporarily to the current buffer. */
2017 old_buffer
= w
->contents
;
2018 old_charpos
= marker_position (w
->pointm
);
2019 old_bytepos
= marker_byte_position (w
->pointm
);
2020 wset_buffer (w
, Fcurrent_buffer ());
2021 set_marker_both (w
->pointm
, w
->contents
,
2022 BUF_PT (current_buffer
), BUF_PT_BYTE (current_buffer
));
2027 struct position pos
;
2028 pos
= *vmotion (PT
, PT_BYTE
, XINT (lines
), w
);
2029 SET_PT_BOTH (pos
.bufpos
, pos
.bytepos
);
2034 ptrdiff_t it_start
, it_overshoot_count
= 0;
2036 bool overshoot_handled
= 0;
2037 bool disp_string_at_start_p
= 0;
2038 ptrdiff_t nlines
= XINT (lines
);
2040 double start_col
IF_LINT (= 0);
2041 int start_x
IF_LINT (= 0);
2044 bool start_x_given
= !NILP (cur_col
);
2047 start_col
= extract_float (cur_col
);
2048 start_x
= window_column_x (w
, window
, start_col
, cur_col
);
2051 itdata
= bidi_shelve_cache ();
2052 SET_TEXT_POS (pt
, PT
, PT_BYTE
);
2053 start_display (&it
, w
, pt
);
2054 first_x
= it
.first_visible_x
;
2055 it_start
= IT_CHARPOS (it
);
2057 /* See comments below for why we calculate this. */
2058 if (it
.cmp_it
.id
>= 0)
2059 it_overshoot_count
= 0;
2060 else if (it
.method
== GET_FROM_STRING
)
2062 const char *s
= SSDATA (it
.string
);
2063 const char *e
= s
+ SBYTES (it
.string
);
2065 disp_string_at_start_p
=
2066 /* If it.area is anything but TEXT_AREA, we need not bother
2067 about the display string, as it doesn't affect cursor
2069 it
.area
== TEXT_AREA
2070 && it
.string_from_display_prop_p
2071 /* A display string on anything but buffer text (e.g., on
2072 an overlay string) doesn't affect cursor positioning. */
2073 && (it
.sp
> 0 && it
.stack
[it
.sp
- 1].method
== GET_FROM_BUFFER
);
2077 it_overshoot_count
++;
2079 if (!it_overshoot_count
)
2080 it_overshoot_count
= -1;
2083 it_overshoot_count
=
2084 !(it
.method
== GET_FROM_IMAGE
|| it
.method
== GET_FROM_STRETCH
);
2088 it
.hpos
= start_col
;
2089 it
.current_x
= start_x
;
2093 /* Scan from the start of the line containing PT. If we don't
2094 do this, we start moving with IT->current_x == 0, while PT is
2095 really at some x > 0. */
2096 reseat_at_previous_visible_line_start (&it
);
2097 it
.current_x
= it
.hpos
= 0;
2099 if (IT_CHARPOS (it
) != PT
)
2100 /* We used to temporarily disable selective display here; the
2101 comment said this is "so we don't move too far" (2005-01-19
2102 checkin by kfs). But this does nothing useful that I can
2103 tell, and it causes Bug#2694 . -- cyd */
2104 /* When the position we started from is covered by a display
2105 string, move_it_to will overshoot it, while vertical-motion
2106 wants to put the cursor _before_ the display string. So in
2107 that case, we move to buffer position before the display
2108 string, and avoid overshooting. But if the position before
2109 the display string is a newline, we don't do this, because
2110 otherwise we will end up in a screen line that is one too
2113 (!disp_string_at_start_p
2114 || FETCH_BYTE (IT_BYTEPOS (it
)) == '\n')
2117 -1, -1, -1, MOVE_TO_POS
);
2119 /* IT may move too far if truncate-lines is on and PT lies
2120 beyond the right margin. IT may also move too far if the
2121 starting point is on a Lisp string that has embedded
2122 newlines, or spans several screen lines. In these cases,
2124 if (IT_CHARPOS (it
) > it_start
)
2126 /* We need to backtrack also if the Lisp string contains no
2127 newlines, but there is a newline right after it. In this
2128 case, IT overshoots if there is an after-string just
2129 before the newline. */
2130 if (it_overshoot_count
< 0
2131 && it
.method
== GET_FROM_BUFFER
2133 it_overshoot_count
= 1;
2134 else if (it_overshoot_count
== 1 && it
.vpos
== 0
2135 && it
.current_x
< it
.last_visible_x
)
2137 /* If we came to the same screen line as the one where
2138 we started, we didn't overshoot the line, and won't
2139 need to backtrack after all. This happens, for
2140 example, when PT is in the middle of a composition. */
2141 it_overshoot_count
= 0;
2143 else if (disp_string_at_start_p
&& it
.vpos
> 0)
2145 /* This is the case of a display string that spans
2146 several screen lines. In that case, we end up at the
2147 end of the string, and it.vpos tells us how many
2148 screen lines we need to backtrack. */
2149 it_overshoot_count
= it
.vpos
;
2151 /* We will overshoot if lines are truncated and point lies
2152 beyond the right margin of the window. */
2153 if (it
.line_wrap
== TRUNCATE
&& it
.current_x
>= it
.last_visible_x
2154 && it_overshoot_count
== 0)
2155 it_overshoot_count
= 1;
2156 if (it_overshoot_count
> 0)
2157 move_it_by_lines (&it
, -it_overshoot_count
);
2159 overshoot_handled
= 1;
2161 else if (IT_CHARPOS (it
) == PT
- 1
2162 && FETCH_BYTE (PT_BYTE
- 1) == '\n'
2165 /* The position we started from was covered by a display
2166 property, so we moved to position before the string, and
2167 backed up one line, because the character at PT - 1 is
2168 a newline. So we need one less line to go up (or exactly
2169 one line to go down if nlines == 0). */
2171 /* But we still need to record that one line, in order to
2172 return the correct value to the caller. */
2175 overshoot_handled
= 1;
2178 to_x
= window_column_x (w
, window
, extract_float (lcols
), lcols
);
2181 it
.vpos
= vpos_init
;
2182 /* Do this even if LINES is 0, so that we move back to the
2183 beginning of the current line as we ought. */
2184 if ((nlines
< 0 && IT_CHARPOS (it
) > 0)
2185 || (nlines
== 0 && !(start_x_given
&& start_x
<= to_x
)))
2186 move_it_by_lines (&it
, max (PTRDIFF_MIN
, nlines
));
2188 else if (overshoot_handled
)
2190 it
.vpos
= vpos_init
;
2191 move_it_by_lines (&it
, min (PTRDIFF_MAX
, nlines
));
2195 /* Otherwise, we are at the first row occupied by PT, which
2196 might span multiple screen lines (e.g., if it's on a
2197 multi-line display string). We want to start from the
2198 last line that it occupies. */
2201 while (IT_CHARPOS (it
) <= it_start
)
2204 move_it_by_lines (&it
, 1);
2207 move_it_by_lines (&it
, min (PTRDIFF_MAX
, nlines
- 1));
2209 else /* it_start = ZV */
2212 move_it_by_lines (&it
, min (PTRDIFF_MAX
, nlines
));
2213 /* We could have some display or overlay string at ZV,
2214 in which case it.vpos will be nonzero now, while
2215 actually we didn't move vertically at all. */
2216 if (IT_CHARPOS (it
) == CHARPOS (pt
) && CHARPOS (pt
) == it_start
)
2221 /* Move to the goal column, if one was specified. If the window
2222 was originally hscrolled, the goal column is interpreted as
2223 an addition to the hscroll amount. */
2226 move_it_in_display_line (&it
, ZV
, first_x
+ to_x
, MOVE_TO_X
);
2227 /* If we find ourselves in the middle of an overlay string
2228 which includes a newline after current string position,
2229 we need to move by lines until we get out of the string,
2230 and then reposition point at the requested X coordinate;
2231 if we don't, the cursor will be placed just after the
2232 string, which might not be the requested column. */
2233 if (nlines
> 0 && it
.area
== TEXT_AREA
)
2235 while (it
.method
== GET_FROM_STRING
2236 && !it
.string_from_display_prop_p
2237 && memchr (SSDATA (it
.string
) + IT_STRING_BYTEPOS (it
),
2239 SBYTES (it
.string
) - IT_STRING_BYTEPOS (it
)))
2241 move_it_by_lines (&it
, 1);
2242 move_it_in_display_line (&it
, ZV
, first_x
+ to_x
, MOVE_TO_X
);
2247 SET_PT_BOTH (IT_CHARPOS (it
), IT_BYTEPOS (it
));
2248 bidi_unshelve_cache (itdata
, 0);
2251 if (BUFFERP (old_buffer
))
2253 wset_buffer (w
, old_buffer
);
2254 set_marker_both (w
->pointm
, w
->contents
,
2255 old_charpos
, old_bytepos
);
2258 return make_number (it
.vpos
);
2263 /* File's initialization. */
2266 syms_of_indent (void)
2268 DEFVAR_BOOL ("indent-tabs-mode", indent_tabs_mode
,
2269 doc
: /* Indentation can insert tabs if this is non-nil. */);
2270 indent_tabs_mode
= 1;
2272 defsubr (&Scurrent_indentation
);
2273 defsubr (&Sindent_to
);
2274 defsubr (&Scurrent_column
);
2275 defsubr (&Smove_to_column
);
2276 defsubr (&Svertical_motion
);
2277 defsubr (&Scompute_motion
);