1 /* Indentation functions.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1998, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #include "character.h"
28 #include "composite.h"
36 #include "intervals.h"
37 #include "dispextern.h"
38 #include "region-cache.h"
40 /* Indentation can insert tabs if this is non-zero;
41 otherwise always uses spaces. */
43 static int indent_tabs_mode
;
47 /* These three values memorize 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 memorized value as invalid. */
53 static double last_known_column
;
55 /* Value of point when current_column was called. */
57 EMACS_INT last_known_column_point
;
59 /* Value of MODIFF when current_column was called. */
61 static int last_known_column_modified
;
63 static double current_column_1
P_ ((void));
64 static double position_indentation
P_ ((int));
66 /* Cache of beginning of line found by the last call of
69 static EMACS_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 if 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 EMACS_INT
*next_boundary_p
;
225 Lisp_Object prop
, position
, overlay_limit
, proplimit
;
226 Lisp_Object buffer
, tmp
;
230 XSETFASTINT (position
, pos
);
231 XSETBUFFER (buffer
, current_buffer
);
233 /* Give faster response for overlay lookup near POS. */
234 recenter_overlay_lists (current_buffer
, pos
);
236 /* We must not advance farther than the next overlay change.
237 The overlay change might change the invisible property;
238 or there might be overlay strings to be displayed there. */
239 overlay_limit
= Fnext_overlay_change (position
);
240 /* As for text properties, this gives a lower bound
241 for where the invisible text property could change. */
242 proplimit
= Fnext_property_change (position
, buffer
, Qt
);
243 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
244 proplimit
= overlay_limit
;
245 /* PROPLIMIT is now a lower bound for the next change
246 in invisible status. If that is plenty far away,
247 use that lower bound. */
248 if (XFASTINT (proplimit
) > pos
+ 100 || XFASTINT (proplimit
) >= to
)
249 *next_boundary_p
= XFASTINT (proplimit
);
250 /* Otherwise, scan for the next `invisible' property change. */
253 /* Don't scan terribly far. */
254 XSETFASTINT (proplimit
, min (pos
+ 100, to
));
255 /* No matter what. don't go past next overlay change. */
256 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
257 proplimit
= overlay_limit
;
258 tmp
= Fnext_single_property_change (position
, Qinvisible
,
260 end
= XFASTINT (tmp
);
262 /* Don't put the boundary in the middle of multibyte form if
263 there is no actual property change. */
265 && !NILP (current_buffer
->enable_multibyte_characters
)
267 while (pos
< end
&& !CHAR_HEAD_P (POS_ADDR (end
)))
270 *next_boundary_p
= end
;
272 /* if the `invisible' property is set, we can skip to
273 the next property change */
274 prop
= Fget_char_property (position
, Qinvisible
,
276 && EQ (XWINDOW (window
)->buffer
, buffer
))
278 inv_p
= TEXT_PROP_MEANS_INVISIBLE (prop
);
279 /* When counting columns (window == nil), don't skip over ellipsis text. */
280 if (NILP (window
) ? inv_p
== 1 : inv_p
)
281 return *next_boundary_p
;
285 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
287 DP is a display table or NULL.
289 This macro is used in current_column_1, Fmove_to_column, and
292 #define MULTIBYTE_BYTES_WIDTH(p, dp) \
297 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, bytes); \
298 if (BYTES_BY_CHAR_HEAD (*p) != bytes) \
302 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))) \
303 width = XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; \
305 width = CHAR_WIDTH (c); \
307 wide_column = width; \
312 DEFUN ("current-column", Fcurrent_column
, Scurrent_column
, 0, 0, 0,
313 doc
: /* Return the horizontal position of point. Beginning of line is column 0.
314 This is calculated by adding together the widths of all the displayed
315 representations of the character between the start of the previous line
316 and point (eg. control characters will have a width of 2 or 4, tabs
317 will have a variable width).
318 Ignores finite width of frame, which means that this function may return
319 values greater than (frame-width).
320 Whether the line is visible (if `selective-display' is t) has no effect;
321 however, ^M is treated as end of line when `selective-display' is t.
322 Text that has an invisible property is considered as having width 0, unless
323 `buffer-invisibility-spec' specifies that it is replaced by an ellipsis. */)
327 XSETFASTINT (temp
, (int) current_column ()); /* iftc */
331 /* Cancel any recorded value of the horizontal position. */
334 invalidate_current_column ()
336 last_known_column_point
= 0;
343 register unsigned char *ptr
, *stop
;
344 register int tab_seen
;
347 register int tab_width
= XINT (current_buffer
->tab_width
);
348 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
349 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
351 if (PT
== last_known_column_point
352 && MODIFF
== last_known_column_modified
)
353 return last_known_column
;
355 /* If the buffer has overlays, text properties,
356 or multibyte characters, use a more general algorithm. */
357 if (BUF_INTERVALS (current_buffer
)
358 || current_buffer
->overlays_before
359 || current_buffer
->overlays_after
361 return current_column_1 ();
363 /* Scan backwards from point to the previous newline,
364 counting width. Tab characters are the only complicated case. */
366 /* Make a pointer for decrementing through the chars before point. */
367 ptr
= BYTE_POS_ADDR (PT_BYTE
- 1) + 1;
368 /* Make a pointer to where consecutive chars leave off,
369 going backwards from point. */
372 else if (PT
<= GPT
|| BEGV
> GPT
)
377 if (tab_width
<= 0 || tab_width
> 1000)
380 col
= 0, tab_seen
= 0, post_tab
= 0;
389 /* We stopped either for the beginning of the buffer
391 if (ptr
== BEGV_ADDR
)
394 /* It was the gap. Jump back over it. */
398 /* Check whether that brings us to beginning of buffer. */
405 if (dp
&& VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
407 charvec
= DISP_CHAR_VECTOR (dp
, c
);
416 for (i
= n
- 1; i
>= 0; --i
)
418 if (VECTORP (charvec
))
420 /* This should be handled the same as
421 next_element_from_display_vector does it. */
422 Lisp_Object entry
= AREF (charvec
, i
);
424 if (GLYPH_CODE_P (entry
)
425 && GLYPH_CODE_CHAR_VALID_P (entry
))
426 c
= GLYPH_CODE_CHAR (entry
);
431 if (c
>= 040 && c
< 0177)
435 && EQ (current_buffer
->selective_display
, Qt
)))
438 goto start_of_line_found
;
443 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
449 else if (VECTORP (charvec
))
450 /* With a display table entry, C is displayed as is, and
451 not displayed as \NNN or as ^N. If C is a single-byte
452 character, it takes one column. If C is multi-byte in
453 an unibyte buffer, it's translated to unibyte, so it
454 also takes one column. */
457 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
465 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
469 if (ptr
== BEGV_ADDR
)
470 current_column_bol_cache
= BEGV
;
472 current_column_bol_cache
= BYTE_TO_CHAR (PTR_BYTE_POS (ptr
));
474 last_known_column
= col
;
475 last_known_column_point
= PT
;
476 last_known_column_modified
= MODIFF
;
481 extern Lisp_Object Qspace
, QCwidth
, QCalign_to
;
483 /* Check the presence of a display property and compute its width.
484 If a property was found and its width was found as well, return
485 its width (>= 0) and set the position of the end of the property
487 Otherwise just return -1. */
489 check_display_width (EMACS_INT pos
, EMACS_INT col
, EMACS_INT
*endpos
)
491 Lisp_Object val
, overlay
;
493 if (CONSP (val
= get_char_property_and_overlay
494 (make_number (pos
), Qdisplay
, Qnil
, &overlay
))
495 && EQ (Qspace
, XCAR (val
)))
496 { /* FIXME: Use calc_pixel_width_or_height, as in term.c. */
497 Lisp_Object plist
= XCDR (val
), prop
;
500 if ((prop
= Fplist_get (plist
, QCwidth
), NATNUMP (prop
)))
502 else if (FLOATP (prop
))
503 width
= (int)(XFLOAT_DATA (prop
) + 0.5);
504 else if ((prop
= Fplist_get (plist
, QCalign_to
), NATNUMP (prop
)))
505 width
= XINT (prop
) - col
;
506 else if (FLOATP (prop
))
507 width
= (int)(XFLOAT_DATA (prop
) + 0.5) - col
;
512 if (OVERLAYP (overlay
))
513 *endpos
= OVERLAY_POSITION (OVERLAY_END (overlay
));
515 get_property_and_range (pos
, Qdisplay
, &val
, &start
, endpos
, Qnil
);
522 /* Scanning from the beginning of the current line, stop at the buffer
523 position ENDPOS or at the column GOALCOL or at the end of line, whichever
525 Return the resulting buffer position and column in ENDPOS and GOALCOL.
526 PREVCOL gets set to the column of the previous position (it's always
527 strictly smaller than the goal column). */
529 scan_for_column (EMACS_INT
*endpos
, EMACS_INT
*goalcol
, EMACS_INT
*prevcol
)
531 register EMACS_INT tab_width
= XINT (current_buffer
->tab_width
);
532 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
533 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
534 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
535 struct composition_it cmp_it
;
537 /* Start the scan at the beginning of this line with column number 0. */
538 register EMACS_INT col
= 0, prev_col
= 0;
539 EMACS_INT goal
= goalcol
? *goalcol
: MOST_POSITIVE_FIXNUM
;
540 EMACS_INT end
= endpos
? *endpos
: PT
;
541 EMACS_INT scan
, scan_byte
;
542 EMACS_INT next_boundary
;
544 EMACS_INT opoint
= PT
, opoint_byte
= PT_BYTE
;
545 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
546 current_column_bol_cache
= PT
;
547 scan
= PT
, scan_byte
= PT_BYTE
;
548 SET_PT_BOTH (opoint
, opoint_byte
);
549 next_boundary
= scan
;
552 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
553 bzero (&cmp_it
, sizeof cmp_it
);
555 composition_compute_stop_pos (&cmp_it
, scan
, scan_byte
, end
, Qnil
);
557 /* Scan forward to the target position. */
562 /* Occasionally we may need to skip invisible text. */
563 while (scan
== next_boundary
)
565 EMACS_INT old_scan
= scan
;
566 /* This updates NEXT_BOUNDARY to the next place
567 where we might need to skip more invisible text. */
568 scan
= skip_invisible (scan
, &next_boundary
, end
, Qnil
);
569 if (scan
!= old_scan
)
570 scan_byte
= CHAR_TO_BYTE (scan
);
575 /* Test reaching the goal column. We do this after skipping
576 invisible characters, so that we put point before the
577 character on which the cursor will appear. */
582 { /* Check display property. */
584 int width
= check_display_width (scan
, col
, &end
);
588 if (end
> scan
) /* Avoid infinite loops with 0-width overlays. */
590 scan
= end
; scan_byte
= charpos_to_bytepos (scan
);
596 /* Check composition sequence. */
598 || (scan
== cmp_it
.stop_pos
599 && composition_reseat_it (&cmp_it
, scan
, scan_byte
, end
,
600 XWINDOW (selected_window
), NULL
, Qnil
)))
601 composition_update_it (&cmp_it
, scan
, scan_byte
, Qnil
);
604 scan
+= cmp_it
.nchars
;
605 scan_byte
+= cmp_it
.nbytes
;
608 if (cmp_it
.to
== cmp_it
.nglyphs
)
611 composition_compute_stop_pos (&cmp_it
, scan
, scan_byte
, end
,
615 cmp_it
.from
= cmp_it
.to
;
619 c
= FETCH_BYTE (scan_byte
);
621 /* See if there is a display table and it relates
622 to this character. */
625 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
626 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
631 /* This character is displayed using a vector of glyphs.
632 Update the column/position based on those glyphs. */
634 charvec
= DISP_CHAR_VECTOR (dp
, c
);
637 for (i
= 0; i
< n
; i
++)
639 /* This should be handled the same as
640 next_element_from_display_vector does it. */
641 Lisp_Object entry
= AREF (charvec
, i
);
643 if (GLYPH_CODE_P (entry
)
644 && GLYPH_CODE_CHAR_VALID_P (entry
))
645 c
= GLYPH_CODE_CHAR (entry
);
651 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
656 col
= col
/ tab_width
* tab_width
;
664 /* The display table doesn't affect this character;
665 it displays as itself. */
669 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
674 col
= col
/ tab_width
* tab_width
;
676 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
678 /* Start of multi-byte form. */
680 int bytes
, width
, wide_column
;
682 ptr
= BYTE_POS_ADDR (scan_byte
);
683 MULTIBYTE_BYTES_WIDTH (ptr
, dp
);
684 /* Subtract one to compensate for the increment
685 that is going to happen below. */
686 scan_byte
+= bytes
- 1;
689 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
691 else if (c
< 040 || c
>= 0177)
702 last_known_column
= col
;
703 last_known_column_point
= PT
;
704 last_known_column_modified
= MODIFF
;
714 /* Return the column number of position POS
715 by scanning forward from the beginning of the line.
716 This function handles characters that are invisible
717 due to text properties or overlays. */
722 EMACS_INT col
= MOST_POSITIVE_FIXNUM
;
723 EMACS_INT opoint
= PT
;
725 scan_for_column (&opoint
, &col
, NULL
);
730 #if 0 /* Not used. */
732 /* Return the width in columns of the part of STRING from BEG to END.
733 If BEG is nil, that stands for the beginning of STRING.
734 If END is nil, that stands for the end of STRING. */
737 string_display_width (string
, beg
, end
)
738 Lisp_Object string
, beg
, end
;
741 register unsigned char *ptr
, *stop
;
742 register int tab_seen
;
745 register int tab_width
= XINT (current_buffer
->tab_width
);
746 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
747 register 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 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
774 col
= 0, tab_seen
= 0, post_tab
= 0;
782 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
783 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
784 else if (c
>= 040 && c
< 0177)
791 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
798 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
803 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
813 DEFUN ("indent-to", Findent_to
, Sindent_to
, 1, 2, "NIndent to column: ",
814 doc
: /* Indent from point with tabs and spaces until COLUMN is reached.
815 Optional second argument MINIMUM says always do at least MINIMUM spaces
816 even if that goes past COLUMN; by default, MINIMUM is zero.
818 The return value is COLUMN. */)
820 Lisp_Object column
, minimum
;
823 register int fromcol
;
824 register int tab_width
= XINT (current_buffer
->tab_width
);
826 CHECK_NUMBER (column
);
828 XSETFASTINT (minimum
, 0);
829 CHECK_NUMBER (minimum
);
831 fromcol
= current_column ();
832 mincol
= fromcol
+ XINT (minimum
);
833 if (mincol
< XINT (column
)) mincol
= XINT (column
);
835 if (fromcol
== mincol
)
836 return make_number (mincol
);
838 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
840 if (indent_tabs_mode
)
843 XSETFASTINT (n
, mincol
/ tab_width
- fromcol
/ tab_width
);
844 if (XFASTINT (n
) != 0)
846 Finsert_char (make_number ('\t'), n
, Qt
);
848 fromcol
= (mincol
/ tab_width
) * tab_width
;
852 XSETFASTINT (column
, mincol
- fromcol
);
853 Finsert_char (make_number (' '), column
, Qt
);
855 last_known_column
= mincol
;
856 last_known_column_point
= PT
;
857 last_known_column_modified
= MODIFF
;
859 XSETINT (column
, mincol
);
864 static double position_indentation
P_ ((int));
866 DEFUN ("current-indentation", Fcurrent_indentation
, Scurrent_indentation
,
868 doc
: /* Return the indentation of the current line.
869 This is the horizontal position of the character
870 following any initial whitespace. */)
874 int opoint
= PT
, opoint_byte
= PT_BYTE
;
876 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
878 XSETFASTINT (val
, (int) position_indentation (PT_BYTE
)); /* iftc */
879 SET_PT_BOTH (opoint
, opoint_byte
);
884 position_indentation (pos_byte
)
885 register int pos_byte
;
887 register EMACS_INT column
= 0;
888 register EMACS_INT tab_width
= XINT (current_buffer
->tab_width
);
889 register unsigned char *p
;
890 register unsigned char *stop
;
891 unsigned char *start
;
892 EMACS_INT next_boundary_byte
= pos_byte
;
893 EMACS_INT ceiling
= next_boundary_byte
;
895 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
897 p
= BYTE_POS_ADDR (pos_byte
);
898 /* STOP records the value of P at which we will need
899 to think about the gap, or about invisible text,
900 or about the end of the buffer. */
902 /* START records the starting value of P. */
908 EMACS_INT stop_pos_byte
;
910 /* If we have updated P, set POS_BYTE to match.
911 The first time we enter the loop, POS_BYTE is already right. */
913 pos_byte
= PTR_BYTE_POS (p
);
914 /* Consider the various reasons STOP might have been set here. */
915 if (pos_byte
== ZV_BYTE
)
917 if (pos_byte
== next_boundary_byte
)
919 EMACS_INT next_boundary
;
920 EMACS_INT pos
= BYTE_TO_CHAR (pos_byte
);
921 pos
= skip_invisible (pos
, &next_boundary
, ZV
, Qnil
);
922 pos_byte
= CHAR_TO_BYTE (pos
);
923 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
925 if (pos_byte
>= ceiling
)
926 ceiling
= BUFFER_CEILING_OF (pos_byte
) + 1;
927 /* Compute the next place we need to stop and think,
928 and set STOP accordingly. */
929 stop_pos_byte
= min (ceiling
, next_boundary_byte
);
930 /* The -1 and +1 arrange to point at the first byte of gap
931 (if STOP_POS_BYTE is the position of the gap)
932 rather than at the data after the gap. */
934 stop
= BYTE_POS_ADDR (stop_pos_byte
- 1) + 1;
935 p
= BYTE_POS_ADDR (pos_byte
);
940 if (! NILP (current_buffer
->enable_multibyte_characters
))
946 column
+= tab_width
- column
% tab_width
;
949 if (ASCII_BYTE_P (p
[-1])
950 || NILP (current_buffer
->enable_multibyte_characters
))
954 pos_byte
= PTR_BYTE_POS (p
- 1);
955 c
= FETCH_MULTIBYTE_CHAR (pos_byte
);
956 if (CHAR_HAS_CATEGORY (c
, ' '))
960 p
= BYTE_POS_ADDR (pos_byte
);
969 /* Test whether the line beginning at POS is indented beyond COLUMN.
970 Blank lines are treated as if they had the same indentation as the
974 indented_beyond_p (pos
, pos_byte
, column
)
979 int opoint
= PT
, opoint_byte
= PT_BYTE
;
981 SET_PT_BOTH (pos
, pos_byte
);
982 while (PT
> BEGV
&& FETCH_BYTE (PT_BYTE
) == '\n')
983 scan_newline (PT
- 1, PT_BYTE
- 1, BEGV
, BEGV_BYTE
, -1, 0);
985 val
= position_indentation (PT_BYTE
);
986 SET_PT_BOTH (opoint
, opoint_byte
);
987 return val
>= column
; /* hmm, float comparison */
990 DEFUN ("move-to-column", Fmove_to_column
, Smove_to_column
, 1, 2, "p",
991 doc
: /* Move point to column COLUMN in the current line.
992 Interactively, COLUMN is the value of prefix numeric argument.
993 The column of a character is calculated by adding together the widths
994 as displayed of the previous characters in the line.
995 This function ignores line-continuation;
996 there is no upper limit on the column number a character can have
997 and horizontal scrolling has no effect.
999 If specified column is within a character, point goes after that character.
1000 If it's past end of line, point goes to end of line.
1002 Optional second argument FORCE non-nil means if COLUMN is in the
1003 middle of a tab character, change it to spaces.
1004 In addition, if FORCE is t, and the line is too short to reach
1005 COLUMN, add spaces/tabs to get there.
1007 The return value is the current column. */)
1009 Lisp_Object column
, force
;
1012 EMACS_INT col
, prev_col
;
1015 CHECK_NATNUM (column
);
1016 goal
= XINT (column
);
1020 scan_for_column (&pos
, &col
, &prev_col
);
1024 /* If a tab char made us overshoot, change it to spaces
1025 and scan through it again. */
1026 if (!NILP (force
) && col
> goal
)
1029 EMACS_INT pos_byte
= PT_BYTE
;
1032 c
= FETCH_CHAR (pos_byte
);
1033 if (c
== '\t' && prev_col
< goal
)
1035 EMACS_INT goal_pt
, goal_pt_byte
;
1037 /* Insert spaces in front of the tab to reach GOAL. Do this
1038 first so that a marker at the end of the tab gets
1040 SET_PT_BOTH (PT
- 1, PT_BYTE
- 1);
1041 Finsert_char (make_number (' '), make_number (goal
- prev_col
), Qt
);
1043 /* Now delete the tab, and indent to COL. */
1044 del_range (PT
, PT
+ 1);
1046 goal_pt_byte
= PT_BYTE
;
1047 Findent_to (make_number (col
), Qnil
);
1048 SET_PT_BOTH (goal_pt
, goal_pt_byte
);
1050 /* Set the last_known... vars consistently. */
1055 /* If line ends prematurely, add space to the end. */
1056 if (col
< goal
&& EQ (force
, Qt
))
1057 Findent_to (make_number (col
= goal
), Qnil
);
1059 last_known_column
= col
;
1060 last_known_column_point
= PT
;
1061 last_known_column_modified
= MODIFF
;
1063 return make_number (col
);
1066 /* compute_motion: compute buffer posn given screen posn and vice versa */
1068 struct position val_compute_motion
;
1070 /* Scan the current buffer forward from offset FROM, pretending that
1071 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1072 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1073 and return the ending buffer position and screen location. If we
1074 can't hit the requested column exactly (because of a tab or other
1075 multi-column character), overshoot.
1077 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
1078 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1079 earlier call to compute_motion. The other common case is that FROMHPOS
1080 is zero and FROM is a position that "belongs" at column zero, but might
1081 be shifted by overlay strings; in this case DID_MOTION should be 0.
1083 WIDTH is the number of columns available to display text;
1084 compute_motion uses this to handle continuation lines and such.
1085 If WIDTH is -1, use width of window's text area adjusted for
1086 continuation glyph when needed.
1088 HSCROLL is the number of columns not being displayed at the left
1089 margin; this is usually taken from a window's hscroll member.
1090 TAB_OFFSET is the number of columns of the first tab that aren't
1091 being displayed, perhaps because of a continuation line or
1094 compute_motion returns a pointer to a struct position. The bufpos
1095 member gives the buffer position at the end of the scan, and hpos
1096 and vpos give its cartesian location. prevhpos is the column at
1097 which the character before bufpos started, and contin is non-zero
1098 if we reached the current line by continuing the previous.
1100 Note that FROMHPOS and TOHPOS should be expressed in real screen
1101 columns, taking HSCROLL and the truncation glyph at the left margin
1102 into account. That is, beginning-of-line moves you to the hpos
1103 -HSCROLL + (HSCROLL > 0).
1105 For example, to find the buffer position of column COL of line LINE
1106 of a certain window, pass the window's starting location as FROM
1107 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1108 Pass the buffer's ZV as TO, to limit the scan to the end of the
1109 visible section of the buffer, and pass LINE and COL as TOVPOS and
1112 When displaying in window w, a typical formula for WIDTH is:
1115 - (has_vertical_scroll_bars
1116 ? WINDOW_CONFIG_SCROLL_BAR_COLS (window)
1117 : (window_width + window_left != frame_cols))
1120 window_width is XFASTINT (w->total_cols),
1121 window_left is XFASTINT (w->left_col),
1122 has_vertical_scroll_bars is
1123 WINDOW_HAS_VERTICAL_SCROLL_BAR (window)
1124 and frame_cols = FRAME_COLS (XFRAME (window->frame))
1126 Or you can let window_box_text_cols do this all for you, and write:
1127 window_box_text_cols (w) - 1
1129 The `-1' accounts for the continuation-line backslashes; the rest
1130 accounts for window borders if the window is split horizontally, and
1131 the scroll bars if they are turned on. */
1134 compute_motion (from
, fromvpos
, fromhpos
, did_motion
, to
, tovpos
, tohpos
, width
, hscroll
, tab_offset
, win
)
1135 EMACS_INT from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
;
1138 EMACS_INT hscroll
, tab_offset
;
1141 register EMACS_INT hpos
= fromhpos
;
1142 register EMACS_INT vpos
= fromvpos
;
1144 register EMACS_INT pos
;
1147 register EMACS_INT tab_width
= XFASTINT (current_buffer
->tab_width
);
1148 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
1149 register struct Lisp_Char_Table
*dp
= window_display_table (win
);
1151 = (INTEGERP (current_buffer
->selective_display
)
1152 ? XINT (current_buffer
->selective_display
)
1153 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1155 = (selective
&& dp
&& VECTORP (DISP_INVIS_VECTOR (dp
))
1156 ? XVECTOR (DISP_INVIS_VECTOR (dp
))->size
: 0);
1157 /* The next location where the `invisible' property changes, or an
1158 overlay starts or ends. */
1159 EMACS_INT next_boundary
= from
;
1161 /* For computing runs of characters with similar widths.
1162 Invariant: width_run_width is zero, or all the characters
1163 from width_run_start to width_run_end have a fixed width of
1165 EMACS_INT width_run_start
= from
;
1166 EMACS_INT width_run_end
= from
;
1167 EMACS_INT width_run_width
= 0;
1168 Lisp_Object
*width_table
;
1171 /* The next buffer pos where we should consult the width run cache. */
1172 EMACS_INT next_width_run
= from
;
1175 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1176 /* If previous char scanned was a wide character,
1177 this is the column where it ended. Otherwise, this is 0. */
1178 EMACS_INT wide_column_end_hpos
= 0;
1179 EMACS_INT prev_pos
; /* Previous buffer position. */
1180 EMACS_INT prev_pos_byte
; /* Previous buffer position. */
1181 EMACS_INT prev_hpos
= 0;
1182 EMACS_INT prev_vpos
= 0;
1183 EMACS_INT contin_hpos
; /* HPOS of last column of continued line. */
1184 EMACS_INT prev_tab_offset
; /* Previous tab offset. */
1185 EMACS_INT continuation_glyph_width
;
1187 struct composition_it cmp_it
;
1189 XSETBUFFER (buffer
, current_buffer
);
1190 XSETWINDOW (window
, win
);
1192 width_run_cache_on_off ();
1193 if (dp
== buffer_display_table ())
1194 width_table
= (VECTORP (current_buffer
->width_table
)
1195 ? XVECTOR (current_buffer
->width_table
)->contents
1198 /* If the window has its own display table, we can't use the width
1199 run cache, because that's based on the buffer's display table. */
1202 if (tab_width
<= 0 || tab_width
> 1000)
1205 /* Negative width means use all available text columns. */
1208 width
= window_box_text_cols (win
);
1209 /* We must make room for continuation marks if we don't have fringes. */
1210 #ifdef HAVE_WINDOW_SYSTEM
1211 if (!FRAME_WINDOW_P (XFRAME (win
->frame
)))
1216 continuation_glyph_width
= 1;
1217 #ifdef HAVE_WINDOW_SYSTEM
1218 if (FRAME_WINDOW_P (XFRAME (win
->frame
)))
1219 continuation_glyph_width
= 0; /* In the fringe. */
1225 pos
= prev_pos
= from
;
1226 pos_byte
= prev_pos_byte
= CHAR_TO_BYTE (from
);
1228 prev_tab_offset
= tab_offset
;
1229 bzero (&cmp_it
, sizeof cmp_it
);
1231 composition_compute_stop_pos (&cmp_it
, pos
, pos_byte
, to
, Qnil
);
1235 while (pos
== next_boundary
)
1237 EMACS_INT pos_here
= pos
;
1240 /* Don't skip invisible if we are already at the margin. */
1241 if (vpos
> tovpos
|| (vpos
== tovpos
&& hpos
>= tohpos
))
1243 if (contin_hpos
&& prev_hpos
== 0
1245 && (contin_hpos
== width
|| wide_column_end_hpos
> width
))
1246 { /* Line breaks because we can't put the character at the
1247 previous line any more. It is not the multi-column
1248 character continued in middle. Go back to previous
1249 buffer position, screen position, and set tab offset
1250 to previous value. It's the beginning of the
1253 pos_byte
= prev_pos_byte
;
1256 tab_offset
= prev_tab_offset
;
1261 /* If the caller says that the screen position came from an earlier
1262 call to compute_motion, then we've already accounted for the
1263 overlay strings at point. This is only true the first time
1264 through, so clear the flag after testing it. */
1266 /* We need to skip past the overlay strings. Currently those
1267 strings must not contain TAB;
1268 if we want to relax that restriction, something will have
1269 to be changed here. */
1271 unsigned char *ovstr
;
1272 int ovlen
= overlay_strings (pos
, win
, &ovstr
);
1273 hpos
+= ((multibyte
&& ovlen
> 0)
1274 ? strwidth (ovstr
, ovlen
) : ovlen
);
1281 /* Advance POS past invisible characters
1282 (but not necessarily all that there are here),
1283 and store in next_boundary the next position where
1284 we need to call skip_invisible. */
1285 newpos
= skip_invisible (pos
, &next_boundary
, to
, window
);
1289 pos
= min (to
, newpos
);
1290 pos_byte
= CHAR_TO_BYTE (pos
);
1294 if (newpos
!= pos_here
)
1297 pos_byte
= CHAR_TO_BYTE (pos
);
1301 /* Handle right margin. */
1302 /* Note on a wide-column character.
1304 Characters are classified into the following three categories
1305 according to the width (columns occupied on screen).
1307 (1) single-column character: ex. `a'
1308 (2) multi-column character: ex. `^A', TAB, `\033'
1309 (3) wide-column character: ex. Japanese character, Chinese character
1310 (In the following example, `W_' stands for them.)
1312 Multi-column characters can be divided around the right margin,
1313 but wide-column characters cannot.
1317 (*) The cursor is placed on the next character after the point.
1321 j ^---- next after the point
1322 ^--- next char. after the point.
1324 In case of sigle-column character
1328 033 ^---- next after the point, next char. after the point.
1330 In case of multi-column character
1334 W_ ^---- next after the point
1335 ^---- next char. after the point.
1337 In case of wide-column character
1339 The problem here is continuation at a wide-column character.
1340 In this case, the line may shorter less than WIDTH.
1341 And we find the continuation AFTER it occurs.
1347 int total_width
= width
+ continuation_glyph_width
;
1350 if (!NILP (Vtruncate_partial_width_windows
)
1351 && (total_width
< FRAME_COLS (XFRAME (WINDOW_FRAME (win
)))))
1353 if (INTEGERP (Vtruncate_partial_width_windows
))
1355 = total_width
< XFASTINT (Vtruncate_partial_width_windows
);
1360 if (hscroll
|| truncate
1361 || !NILP (current_buffer
->truncate_lines
))
1363 /* Truncating: skip to newline, unless we are already past
1364 TO (we need to go back below). */
1367 pos
= find_before_next_newline (pos
, to
, 1);
1368 pos_byte
= CHAR_TO_BYTE (pos
);
1370 /* If we just skipped next_boundary,
1371 loop around in the main while
1373 if (pos
>= next_boundary
)
1374 next_boundary
= pos
+ 1;
1377 prev_tab_offset
= tab_offset
;
1383 /* Remember the previous value. */
1384 prev_tab_offset
= tab_offset
;
1386 if (wide_column_end_hpos
> width
)
1389 tab_offset
+= prev_hpos
;
1393 tab_offset
+= width
;
1397 contin_hpos
= prev_hpos
;
1403 /* Stop if past the target buffer position or screen position. */
1406 /* Go back to the previous position. */
1408 pos_byte
= prev_pos_byte
;
1411 tab_offset
= prev_tab_offset
;
1413 /* NOTE on contin_hpos, hpos, and prev_hpos.
1417 W_ ^---- contin_hpos
1423 if (contin_hpos
&& prev_hpos
== 0
1424 && contin_hpos
< width
&& !wide_column_end_hpos
)
1426 /* Line breaking occurs in the middle of multi-column
1427 character. Go back to previous line. */
1434 if (vpos
> tovpos
|| (vpos
== tovpos
&& hpos
>= tohpos
))
1436 if (contin_hpos
&& prev_hpos
== 0
1438 && (contin_hpos
== width
|| wide_column_end_hpos
> width
))
1439 { /* Line breaks because we can't put the character at the
1440 previous line any more. It is not the multi-column
1441 character continued in middle. Go back to previous
1442 buffer position, screen position, and set tab offset
1443 to previous value. It's the beginning of the
1446 pos_byte
= prev_pos_byte
;
1449 tab_offset
= prev_tab_offset
;
1453 if (pos
== ZV
) /* We cannot go beyond ZV. Stop here. */
1459 prev_pos_byte
= pos_byte
;
1460 wide_column_end_hpos
= 0;
1462 /* Consult the width run cache to see if we can avoid inspecting
1463 the text character-by-character. */
1464 if (current_buffer
->width_run_cache
&& pos
>= next_width_run
)
1468 = region_cache_forward (current_buffer
,
1469 current_buffer
->width_run_cache
,
1472 /* A width of zero means the character's width varies (like
1473 a tab), is meaningless (like a newline), or we just don't
1474 want to skip over it for some other reason. */
1475 if (common_width
!= 0)
1479 /* Don't go past the final buffer posn the user
1484 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1486 /* Don't go past the final horizontal position the user
1488 if (vpos
== tovpos
&& run_end_hpos
> tohpos
)
1490 run_end
= pos
+ (tohpos
- hpos
) / common_width
;
1491 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1494 /* Don't go past the margin. */
1495 if (run_end_hpos
>= width
)
1497 run_end
= pos
+ (width
- hpos
) / common_width
;
1498 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1501 hpos
= run_end_hpos
;
1503 prev_hpos
= hpos
- common_width
;
1507 pos_byte
= CHAR_TO_BYTE (pos
);
1511 next_width_run
= run_end
+ 1;
1514 /* We have to scan the text character-by-character. */
1518 Lisp_Object charvec
;
1520 /* Check composition sequence. */
1522 || (pos
== cmp_it
.stop_pos
1523 && composition_reseat_it (&cmp_it
, pos
, pos_byte
, to
, win
,
1525 composition_update_it (&cmp_it
, pos
, pos_byte
, Qnil
);
1528 pos
+= cmp_it
.nchars
;
1529 pos_byte
+= cmp_it
.nbytes
;
1530 hpos
+= cmp_it
.width
;
1531 if (cmp_it
.to
== cmp_it
.nglyphs
)
1534 composition_compute_stop_pos (&cmp_it
, pos
, pos_byte
, to
,
1538 cmp_it
.from
= cmp_it
.to
;
1542 c
= FETCH_BYTE (pos_byte
);
1545 /* Perhaps add some info to the width_run_cache. */
1546 if (current_buffer
->width_run_cache
)
1548 /* Is this character part of the current run? If so, extend
1550 if (pos
- 1 == width_run_end
1551 && XFASTINT (width_table
[c
]) == width_run_width
)
1552 width_run_end
= pos
;
1554 /* The previous run is over, since this is a character at a
1555 different position, or a different width. */
1558 /* Have we accumulated a run to put in the cache?
1559 (Currently, we only cache runs of width == 1). */
1560 if (width_run_start
< width_run_end
1561 && width_run_width
== 1)
1562 know_region_cache (current_buffer
,
1563 current_buffer
->width_run_cache
,
1564 width_run_start
, width_run_end
);
1566 /* Start recording a new width run. */
1567 width_run_width
= XFASTINT (width_table
[c
]);
1568 width_run_start
= pos
- 1;
1569 width_run_end
= pos
;
1574 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
1575 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
1577 charvec
= DISP_CHAR_VECTOR (dp
, c
);
1578 n
= ASIZE (charvec
);
1586 for (i
= n
- 1; i
>= 0; --i
)
1588 if (VECTORP (charvec
))
1590 /* This should be handled the same as
1591 next_element_from_display_vector does it. */
1592 Lisp_Object entry
= AREF (charvec
, i
);
1594 if (GLYPH_CODE_P (entry
)
1595 && GLYPH_CODE_CHAR_VALID_P (entry
))
1596 c
= GLYPH_CODE_CHAR (entry
);
1601 if (c
>= 040 && c
< 0177)
1605 int tem
= ((hpos
+ tab_offset
+ hscroll
- (hscroll
> 0))
1609 hpos
+= tab_width
- tem
;
1614 && indented_beyond_p (pos
, pos_byte
,
1615 (double) selective
)) /* iftc */
1617 /* If (pos == to), we don't have to take care of
1618 selective display. */
1621 /* Skip any number of invisible lines all at once */
1624 pos
= find_before_next_newline (pos
, to
, 1);
1627 pos_byte
= CHAR_TO_BYTE (pos
);
1630 && indented_beyond_p (pos
, pos_byte
,
1631 (double) selective
)); /* iftc */
1632 /* Allow for the " ..." that is displayed for them. */
1635 hpos
+= selective_rlen
;
1639 DEC_BOTH (pos
, pos_byte
);
1640 /* We have skipped the invis text, but not the
1646 /* A visible line. */
1650 /* Count the truncation glyph on column 0 */
1652 hpos
+= continuation_glyph_width
;
1657 else if (c
== CR
&& selective
< 0)
1659 /* In selective display mode,
1660 everything from a ^M to the end of the line is invisible.
1661 Stop *before* the real newline. */
1664 pos
= find_before_next_newline (pos
, to
, 1);
1665 pos_byte
= CHAR_TO_BYTE (pos
);
1667 /* If we just skipped next_boundary,
1668 loop around in the main while
1670 if (pos
> next_boundary
)
1671 next_boundary
= pos
;
1672 /* Allow for the " ..." that is displayed for them. */
1675 hpos
+= selective_rlen
;
1680 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
1682 /* Start of multi-byte form. */
1684 int bytes
, width
, wide_column
;
1686 pos_byte
--; /* rewind POS_BYTE */
1687 ptr
= BYTE_POS_ADDR (pos_byte
);
1688 MULTIBYTE_BYTES_WIDTH (ptr
, dp
);
1691 wide_column_end_hpos
= hpos
+ wide_column
;
1694 else if (VECTORP (charvec
))
1697 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
1704 /* Remember any final width run in the cache. */
1705 if (current_buffer
->width_run_cache
1706 && width_run_width
== 1
1707 && width_run_start
< width_run_end
)
1708 know_region_cache (current_buffer
, current_buffer
->width_run_cache
,
1709 width_run_start
, width_run_end
);
1711 val_compute_motion
.bufpos
= pos
;
1712 val_compute_motion
.bytepos
= pos_byte
;
1713 val_compute_motion
.hpos
= hpos
;
1714 val_compute_motion
.vpos
= vpos
;
1715 if (contin_hpos
&& prev_hpos
== 0)
1716 val_compute_motion
.prevhpos
= contin_hpos
;
1718 val_compute_motion
.prevhpos
= prev_hpos
;
1719 /* We alalways handle all of them here; none of them remain to do. */
1720 val_compute_motion
.ovstring_chars_done
= 0;
1722 /* Nonzero if have just continued a line */
1723 val_compute_motion
.contin
= (contin_hpos
&& prev_hpos
== 0);
1726 return &val_compute_motion
;
1730 DEFUN ("compute-motion", Fcompute_motion
, Scompute_motion
, 7, 7, 0,
1731 doc
: /* Scan through the current buffer, calculating screen position.
1732 Scan the current buffer forward from offset FROM,
1733 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1734 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1735 and return the ending buffer position and screen location.
1737 If TOPOS is nil, the actual width and height of the window's
1740 There are three additional arguments:
1742 WIDTH is the number of columns available to display text;
1743 this affects handling of continuation lines. A value of nil
1744 corresponds to the actual number of available text columns.
1746 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1747 HSCROLL is the number of columns not being displayed at the left
1748 margin; this is usually taken from a window's hscroll member.
1749 TAB-OFFSET is the number of columns of the first tab that aren't
1750 being displayed, perhaps because the line was continued within it.
1751 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1753 WINDOW is the window to operate on. It is used to choose the display table;
1754 if it is showing the current buffer, it is used also for
1755 deciding which overlay properties apply.
1756 Note that `compute-motion' always operates on the current buffer.
1758 The value is a list of five elements:
1759 (POS HPOS VPOS PREVHPOS CONTIN)
1760 POS is the buffer position where the scan stopped.
1761 VPOS is the vertical position where the scan stopped.
1762 HPOS is the horizontal position where the scan stopped.
1764 PREVHPOS is the horizontal position one character back from POS.
1765 CONTIN is t if a line was continued after (or within) the previous character.
1767 For example, to find the buffer position of column COL of line LINE
1768 of a certain window, pass the window's starting location as FROM
1769 and the window's upper-left coordinates as FROMPOS.
1770 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1771 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1772 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1773 Lisp_Object from
, frompos
, to
, topos
;
1774 Lisp_Object width
, offsets
, window
;
1777 Lisp_Object bufpos
, hpos
, vpos
, prevhpos
;
1778 struct position
*pos
;
1779 int hscroll
, tab_offset
;
1781 CHECK_NUMBER_COERCE_MARKER (from
);
1782 CHECK_CONS (frompos
);
1783 CHECK_NUMBER_CAR (frompos
);
1784 CHECK_NUMBER_CDR (frompos
);
1785 CHECK_NUMBER_COERCE_MARKER (to
);
1789 CHECK_NUMBER_CAR (topos
);
1790 CHECK_NUMBER_CDR (topos
);
1793 CHECK_NUMBER (width
);
1795 if (!NILP (offsets
))
1797 CHECK_CONS (offsets
);
1798 CHECK_NUMBER_CAR (offsets
);
1799 CHECK_NUMBER_CDR (offsets
);
1800 hscroll
= XINT (XCAR (offsets
));
1801 tab_offset
= XINT (XCDR (offsets
));
1804 hscroll
= tab_offset
= 0;
1807 window
= Fselected_window ();
1809 CHECK_LIVE_WINDOW (window
);
1810 w
= XWINDOW (window
);
1812 if (XINT (from
) < BEGV
|| XINT (from
) > ZV
)
1813 args_out_of_range_3 (from
, make_number (BEGV
), make_number (ZV
));
1814 if (XINT (to
) < BEGV
|| XINT (to
) > ZV
)
1815 args_out_of_range_3 (to
, make_number (BEGV
), make_number (ZV
));
1817 pos
= compute_motion (XINT (from
), XINT (XCDR (frompos
)),
1818 XINT (XCAR (frompos
)), 0,
1821 ? window_internal_height (w
)
1822 : XINT (XCDR (topos
))),
1824 ? (window_box_text_cols (w
)
1826 #ifdef HAVE_WINDOW_SYSTEM
1827 FRAME_WINDOW_P (XFRAME (w
->frame
)) ? 0 :
1830 : XINT (XCAR (topos
))),
1831 (NILP (width
) ? -1 : XINT (width
)),
1832 hscroll
, tab_offset
,
1835 XSETFASTINT (bufpos
, pos
->bufpos
);
1836 XSETINT (hpos
, pos
->hpos
);
1837 XSETINT (vpos
, pos
->vpos
);
1838 XSETINT (prevhpos
, pos
->prevhpos
);
1840 return Fcons (bufpos
,
1844 Fcons (pos
->contin
? Qt
: Qnil
, Qnil
)))));
1848 /* Fvertical_motion and vmotion */
1850 struct position val_vmotion
;
1853 vmotion (from
, vtarget
, w
)
1854 register EMACS_INT from
, vtarget
;
1857 EMACS_INT hscroll
= XINT (w
->hscroll
);
1858 struct position pos
;
1859 /* vpos is cumulative vertical position, changed as from is changed */
1860 register int vpos
= 0;
1862 register EMACS_INT first
;
1863 EMACS_INT from_byte
;
1864 EMACS_INT lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
1866 = (INTEGERP (current_buffer
->selective_display
)
1867 ? XINT (current_buffer
->selective_display
)
1868 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1870 EMACS_INT start_hpos
= 0;
1872 /* This is the object we use for fetching character properties. */
1873 Lisp_Object text_prop_object
;
1875 XSETWINDOW (window
, w
);
1877 /* If the window contains this buffer, use it for getting text properties.
1878 Otherwise use the current buffer as arg for doing that. */
1879 if (EQ (w
->buffer
, Fcurrent_buffer ()))
1880 text_prop_object
= window
;
1882 text_prop_object
= Fcurrent_buffer ();
1884 if (vpos
>= vtarget
)
1886 /* To move upward, go a line at a time until
1887 we have gone at least far enough. */
1891 while ((vpos
> vtarget
|| first
) && from
> BEGV
)
1893 Lisp_Object propval
;
1895 prevline
= find_next_newline_no_quit (from
- 1, -1);
1896 while (prevline
> BEGV
1898 && indented_beyond_p (prevline
,
1899 CHAR_TO_BYTE (prevline
),
1900 (double) selective
)) /* iftc */
1901 /* Watch out for newlines with `invisible' property.
1902 When moving upward, check the newline before. */
1903 || (propval
= Fget_char_property (make_number (prevline
- 1),
1906 TEXT_PROP_MEANS_INVISIBLE (propval
))))
1907 prevline
= find_next_newline_no_quit (prevline
- 1, -1);
1908 pos
= *compute_motion (prevline
, 0,
1909 lmargin
+ (prevline
== BEG
? start_hpos
: 0),
1912 /* Don't care for VPOS... */
1913 1 << (BITS_PER_SHORT
- 1),
1915 1 << (BITS_PER_SHORT
- 1),
1917 /* This compensates for start_hpos
1918 so that a tab as first character
1919 still occupies 8 columns. */
1920 (prevline
== BEG
? -start_hpos
: 0),
1927 /* If we made exactly the desired vertical distance,
1928 or if we hit beginning of buffer,
1929 return point found */
1930 if (vpos
>= vtarget
)
1932 val_vmotion
.bufpos
= from
;
1933 val_vmotion
.bytepos
= CHAR_TO_BYTE (from
);
1934 val_vmotion
.vpos
= vpos
;
1935 val_vmotion
.hpos
= lmargin
;
1936 val_vmotion
.contin
= 0;
1937 val_vmotion
.prevhpos
= 0;
1938 val_vmotion
.ovstring_chars_done
= 0;
1939 val_vmotion
.tab_offset
= 0; /* For accumulating tab offset. */
1940 return &val_vmotion
;
1943 /* Otherwise find the correct spot by moving down */
1945 /* Moving downward is simple, but must calculate from beg of line
1946 to determine hpos of starting point */
1947 from_byte
= CHAR_TO_BYTE (from
);
1948 if (from
> BEGV
&& FETCH_BYTE (from_byte
- 1) != '\n')
1950 Lisp_Object propval
;
1952 prevline
= find_next_newline_no_quit (from
, -1);
1953 while (prevline
> BEGV
1955 && indented_beyond_p (prevline
,
1956 CHAR_TO_BYTE (prevline
),
1957 (double) selective
)) /* iftc */
1958 /* Watch out for newlines with `invisible' property.
1959 When moving downward, check the newline after. */
1960 || (propval
= Fget_char_property (make_number (prevline
),
1963 TEXT_PROP_MEANS_INVISIBLE (propval
))))
1964 prevline
= find_next_newline_no_quit (prevline
- 1, -1);
1965 pos
= *compute_motion (prevline
, 0,
1966 lmargin
+ (prevline
== BEG
1970 /* Don't care for VPOS... */
1971 1 << (BITS_PER_SHORT
- 1),
1973 1 << (BITS_PER_SHORT
- 1),
1975 (prevline
== BEG
? -start_hpos
: 0),
1981 pos
.hpos
= lmargin
+ (from
== BEG
? start_hpos
: 0);
1986 return compute_motion (from
, vpos
, pos
.hpos
, did_motion
,
1987 ZV
, vtarget
, - (1 << (BITS_PER_SHORT
- 1)),
1989 pos
.tab_offset
- (from
== BEG
? start_hpos
: 0),
1993 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 2, 0,
1994 doc
: /* Move point to start of the screen line LINES lines down.
1995 If LINES is negative, this means moving up.
1997 This function is an ordinary cursor motion function
1998 which calculates the new position based on how text would be displayed.
1999 The new position may be the start of a line,
2000 or just the start of a continuation line.
2001 The function returns number of screen lines moved over;
2002 that usually equals LINES, but may be closer to zero
2003 if beginning or end of buffer was reached.
2005 The optional second argument WINDOW specifies the window to use for
2006 parameters such as width, horizontal scrolling, and so on.
2007 The default is to use the selected window's parameters.
2009 LINES can optionally take the form (COLS . LINES), in which case
2010 the motion will not stop at the start of a screen line but on
2011 its column COLS (if such exists on that line, that is).
2013 `vertical-motion' always uses the current buffer,
2014 regardless of which buffer is displayed in WINDOW.
2015 This is consistent with other cursor motion functions
2016 and makes it possible to use `vertical-motion' in any buffer,
2017 whether or not it is currently displayed in some window. */)
2019 Lisp_Object lines
, window
;
2024 Lisp_Object old_buffer
;
2025 struct gcpro gcpro1
;
2026 Lisp_Object lcols
= Qnil
;
2029 /* Allow LINES to be of the form (HPOS . VPOS) aka (COLUMNS . LINES). */
2030 if (CONSP (lines
) && (NUMBERP (XCAR (lines
))))
2032 lcols
= XCAR (lines
);
2033 cols
= INTEGERP (lcols
) ? (double) XINT (lcols
) : XFLOAT_DATA (lcols
);
2034 lines
= XCDR (lines
);
2037 CHECK_NUMBER (lines
);
2038 if (! NILP (window
))
2039 CHECK_WINDOW (window
);
2041 window
= selected_window
;
2042 w
= XWINDOW (window
);
2045 GCPRO1 (old_buffer
);
2046 if (XBUFFER (w
->buffer
) != current_buffer
)
2048 /* Set the window's buffer temporarily to the current buffer. */
2049 old_buffer
= w
->buffer
;
2050 XSETBUFFER (w
->buffer
, current_buffer
);
2055 struct position pos
;
2056 pos
= *vmotion (PT
, XINT (lines
), w
);
2057 SET_PT_BOTH (pos
.bufpos
, pos
.bytepos
);
2061 int it_start
, oselective
, first_x
, it_overshoot_expected
;
2063 SET_TEXT_POS (pt
, PT
, PT_BYTE
);
2064 start_display (&it
, w
, pt
);
2065 first_x
= it
.first_visible_x
;
2066 it_start
= IT_CHARPOS (it
);
2068 /* See comments below for why we calculate this. */
2069 if (XINT (lines
) > 0)
2071 if (it
.cmp_it
.id
>= 0)
2072 it_overshoot_expected
= 1;
2073 if (it
.method
== GET_FROM_STRING
)
2075 const char *s
= SDATA (it
.string
);
2076 const char *e
= s
+ SBYTES (it
.string
);
2077 while (s
< e
&& *s
!= '\n')
2079 it_overshoot_expected
= (s
== e
) ? -1 : 0;
2082 it_overshoot_expected
= (it
.method
== GET_FROM_IMAGE
2083 || it
.method
== GET_FROM_STRETCH
);
2086 /* Scan from the start of the line containing PT. If we don't
2087 do this, we start moving with IT->current_x == 0, while PT is
2088 really at some x > 0. */
2089 reseat_at_previous_visible_line_start (&it
);
2090 it
.current_x
= it
.hpos
= 0;
2091 /* Temporarily disable selective display so we don't move too far */
2092 oselective
= it
.selective
;
2094 move_it_to (&it
, PT
, -1, -1, -1, MOVE_TO_POS
);
2095 it
.selective
= oselective
;
2097 if (XINT (lines
) <= 0)
2100 /* Do this even if LINES is 0, so that we move back to the
2101 beginning of the current line as we ought. */
2102 if (XINT (lines
) == 0 || IT_CHARPOS (it
) > 0)
2103 move_it_by_lines (&it
, XINT (lines
), 0);
2107 if (IT_CHARPOS (it
) > PT
)
2109 /* IT may move too far if truncate-lines is on and PT
2110 lies beyond the right margin. In that case,
2111 backtrack unless the starting point is on an image,
2112 stretch glyph, composition, or Lisp string. */
2113 if (!it_overshoot_expected
2114 /* Also, backtrack if the Lisp string contains no
2115 newline, but there is a newline right after it.
2116 In this case, IT overshoots if there is an
2117 after-string just before the newline. */
2118 || (it_overshoot_expected
< 0
2119 && it
.method
== GET_FROM_BUFFER
2121 move_it_by_lines (&it
, -1, 0);
2123 move_it_by_lines (&it
, XINT (lines
), 0);
2127 /* Otherwise, we are at the first row occupied by PT,
2128 which might span multiple screen lines (e.g., if it's
2129 on a multi-line display string). We want to start
2130 from the last line that it occupies. */
2134 while (IT_CHARPOS (it
) <= PT
)
2135 move_it_by_lines (&it
, 1, 0);
2136 move_it_by_lines (&it
, XINT (lines
) - 1, 0);
2139 move_it_by_lines (&it
, XINT (lines
), 0);
2143 /* Move to the goal column, if one was specified. */
2146 /* If the window was originally hscrolled, move forward by
2147 the hscrolled amount first. */
2150 move_it_in_display_line (&it
, ZV
, first_x
, MOVE_TO_X
);
2153 move_it_in_display_line
2155 (int)(cols
* FRAME_COLUMN_WIDTH (XFRAME (w
->frame
)) + 0.5),
2159 SET_PT_BOTH (IT_CHARPOS (it
), IT_BYTEPOS (it
));
2162 if (BUFFERP (old_buffer
))
2163 w
->buffer
= old_buffer
;
2165 RETURN_UNGCPRO (make_number (it
.vpos
));
2170 /* File's initialization. */
2175 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode
,
2176 doc
: /* *Indentation can insert tabs if this is non-nil. */);
2177 indent_tabs_mode
= 1;
2179 defsubr (&Scurrent_indentation
);
2180 defsubr (&Sindent_to
);
2181 defsubr (&Scurrent_column
);
2182 defsubr (&Smove_to_column
);
2183 defsubr (&Svertical_motion
);
2184 defsubr (&Scompute_motion
);
2187 /* arch-tag: 9adfea44-71f7-4988-8ee3-96da15c502cc
2188 (do not change this comment) */