1 /* Indentation functions.
2 Copyright (C) 1985,86,87,88,93,94,95,98, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
34 #include "intervals.h"
35 #include "region-cache.h"
37 /* Indentation can insert tabs if this is non-zero;
38 otherwise always uses spaces. */
44 /* These three values memorize the current column to avoid recalculation. */
46 /* Last value returned by current_column.
47 Some things in set last_known_column_point to -1
48 to mark the memorized value as invalid. */
50 int last_known_column
;
52 /* Value of point when current_column was called. */
54 int last_known_column_point
;
56 /* Value of MODIFF when current_column was called. */
58 int last_known_column_modified
;
60 static int current_column_1
P_ ((void));
61 static int position_indentation
P_ ((int));
63 /* Cache of beginning of line found by the last call of
66 int current_column_bol_cache
;
68 /* Get the display table to use for the current buffer. */
70 struct Lisp_Char_Table
*
71 buffer_display_table ()
75 thisbuf
= current_buffer
->display_table
;
76 if (DISP_TABLE_P (thisbuf
))
77 return XCHAR_TABLE (thisbuf
);
78 if (DISP_TABLE_P (Vstandard_display_table
))
79 return XCHAR_TABLE (Vstandard_display_table
);
83 /* Width run cache considerations. */
85 /* Return the width of character C under display table DP. */
88 character_width (c
, dp
)
90 struct Lisp_Char_Table
*dp
;
94 /* These width computations were determined by examining the cases
95 in display_text_line. */
97 /* Everything can be handled by the display table, if it's
98 present and the element is right. */
99 if (dp
&& (elt
= DISP_CHAR_VECTOR (dp
, c
), VECTORP (elt
)))
100 return XVECTOR (elt
)->size
;
102 /* Some characters are special. */
103 if (c
== '\n' || c
== '\t' || c
== '\015')
106 /* Printing characters have width 1. */
107 else if (c
>= 040 && c
< 0177)
110 /* Everybody else (control characters, metacharacters) has other
111 widths. We could return their actual widths here, but they
112 depend on things like ctl_arrow and crud like that, and they're
113 not very common at all. So we'll just claim we don't know their
119 /* Return true iff the display table DISPTAB specifies the same widths
120 for characters as WIDTHTAB. We use this to decide when to
121 invalidate the buffer's width_run_cache. */
124 disptab_matches_widthtab (disptab
, widthtab
)
125 struct Lisp_Char_Table
*disptab
;
126 struct Lisp_Vector
*widthtab
;
130 if (widthtab
->size
!= 256)
133 for (i
= 0; i
< 256; i
++)
134 if (character_width (i
, disptab
)
135 != XFASTINT (widthtab
->contents
[i
]))
141 /* Recompute BUF's width table, using the display table DISPTAB. */
144 recompute_width_table (buf
, disptab
)
146 struct Lisp_Char_Table
*disptab
;
149 struct Lisp_Vector
*widthtab
;
151 if (!VECTORP (buf
->width_table
))
152 buf
->width_table
= Fmake_vector (make_number (256), make_number (0));
153 widthtab
= XVECTOR (buf
->width_table
);
154 if (widthtab
->size
!= 256)
157 for (i
= 0; i
< 256; i
++)
158 XSETFASTINT (widthtab
->contents
[i
], character_width (i
, disptab
));
161 /* Allocate or free the width run cache, as requested by the current
162 state of current_buffer's cache_long_line_scans variable. */
165 width_run_cache_on_off ()
167 if (NILP (current_buffer
->cache_long_line_scans
)
168 /* And, for the moment, this feature doesn't work on multibyte
170 || !NILP (current_buffer
->enable_multibyte_characters
))
172 /* It should be off. */
173 if (current_buffer
->width_run_cache
)
175 free_region_cache (current_buffer
->width_run_cache
);
176 current_buffer
->width_run_cache
= 0;
177 current_buffer
->width_table
= Qnil
;
182 /* It should be on. */
183 if (current_buffer
->width_run_cache
== 0)
185 current_buffer
->width_run_cache
= new_region_cache ();
186 recompute_width_table (current_buffer
, buffer_display_table ());
192 /* Skip some invisible characters starting from POS.
193 This includes characters invisible because of text properties
194 and characters invisible because of overlays.
196 If position POS is followed by invisible characters,
197 skip some of them and return the position after them.
198 Otherwise return POS itself.
200 Set *NEXT_BOUNDARY_P to the next position at which
201 it will be necessary to call this function again.
203 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
204 to a value greater than TO.
206 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
207 take account of overlays that apply only in WINDOW.
209 We don't necessarily skip all the invisible characters after POS
210 because that could take a long time. We skip a reasonable number
211 which can be skipped quickly. If there might be more invisible
212 characters immediately following, then *NEXT_BOUNDARY_P
213 will equal the return value. */
216 skip_invisible (pos
, next_boundary_p
, to
, window
)
218 int *next_boundary_p
;
222 Lisp_Object prop
, position
, overlay_limit
, proplimit
;
226 XSETFASTINT (position
, pos
);
227 XSETBUFFER (buffer
, current_buffer
);
229 /* Give faster response for overlay lookup near POS. */
230 recenter_overlay_lists (current_buffer
, pos
);
232 /* We must not advance farther than the next overlay change.
233 The overlay change might change the invisible property;
234 or there might be overlay strings to be displayed there. */
235 overlay_limit
= Fnext_overlay_change (position
);
236 /* As for text properties, this gives a lower bound
237 for where the invisible text property could change. */
238 proplimit
= Fnext_property_change (position
, buffer
, Qt
);
239 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
240 proplimit
= overlay_limit
;
241 /* PROPLIMIT is now a lower bound for the next change
242 in invisible status. If that is plenty far away,
243 use that lower bound. */
244 if (XFASTINT (proplimit
) > pos
+ 100 || XFASTINT (proplimit
) >= to
)
245 *next_boundary_p
= XFASTINT (proplimit
);
246 /* Otherwise, scan for the next `invisible' property change. */
249 /* Don't scan terribly far. */
250 XSETFASTINT (proplimit
, min (pos
+ 100, to
));
251 /* No matter what. don't go past next overlay change. */
252 if (XFASTINT (overlay_limit
) < XFASTINT (proplimit
))
253 proplimit
= overlay_limit
;
254 end
= XFASTINT (Fnext_single_property_change (position
, Qinvisible
,
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 if (!NILP (window
) && EQ (XWINDOW (window
)->buffer
, buffer
))
270 prop
= Fget_char_property (position
, Qinvisible
, window
);
272 prop
= Fget_char_property (position
, Qinvisible
, buffer
);
274 ? TEXT_PROP_MEANS_INVISIBLE_NOELLIPSIS (prop
)
275 : TEXT_PROP_MEANS_INVISIBLE (prop
))
276 return *next_boundary_p
;
280 /* If a composition starts at POS/POS_BYTE and it doesn't stride over
281 POINT, set *LEN / *LEN_BYTE to the character and byte lengths, *WIDTH
282 to the width, and return 1. Otherwise, return 0. */
285 check_composition (pos
, pos_byte
, point
, len
, len_byte
, width
)
286 int pos
, pos_byte
, point
;
287 int *len
, *len_byte
, *width
;
293 if (! find_composition (pos
, -1, &start
, &end
, &prop
, Qnil
)
294 || pos
!= start
|| point
< end
295 || !COMPOSITION_VALID_P (start
, end
, prop
))
297 if ((id
= get_composition_id (pos
, pos_byte
, end
- pos
, prop
, Qnil
)) < 0)
300 *len
= COMPOSITION_LENGTH (prop
);
301 *len_byte
= CHAR_TO_BYTE (end
) - pos_byte
;
302 *width
= composition_table
[id
]->width
;
306 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
308 DP is a display table or NULL.
310 This macro is used in current_column_1, Fmove_to_column, and
313 #define MULTIBYTE_BYTES_WIDTH(p, dp) \
318 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, bytes); \
319 if (BYTES_BY_CHAR_HEAD (*p) != bytes) \
323 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))) \
324 width = XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; \
326 width = WIDTH_BY_CHAR_HEAD (*p); \
328 wide_column = width; \
332 DEFUN ("current-column", Fcurrent_column
, Scurrent_column
, 0, 0, 0,
333 "Return the horizontal position of point. Beginning of line is column 0.\n\
334 This is calculated by adding together the widths of all the displayed\n\
335 representations of the character between the start of the previous line\n\
336 and point. (eg control characters will have a width of 2 or 4, tabs\n\
337 will have a variable width)\n\
338 Ignores finite width of frame, which means that this function may return\n\
339 values greater than (frame-width).\n\
340 Whether the line is visible (if `selective-display' is t) has no effect;\n\
341 however, ^M is treated as end of line when `selective-display' is t.")
345 XSETFASTINT (temp
, current_column ());
349 /* Cancel any recorded value of the horizontal position. */
352 invalidate_current_column ()
354 last_known_column_point
= 0;
361 register unsigned char *ptr
, *stop
;
362 register int tab_seen
;
365 register int tab_width
= XINT (current_buffer
->tab_width
);
366 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
367 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
369 if (PT
== last_known_column_point
370 && MODIFF
== last_known_column_modified
)
371 return last_known_column
;
373 /* If the buffer has overlays, text properties,
374 or multibyte characters, use a more general algorithm. */
375 if (BUF_INTERVALS (current_buffer
)
376 || !NILP (current_buffer
->overlays_before
)
377 || !NILP (current_buffer
->overlays_after
)
379 return current_column_1 ();
381 /* Scan backwards from point to the previous newline,
382 counting width. Tab characters are the only complicated case. */
384 /* Make a pointer for decrementing through the chars before point. */
385 ptr
= BYTE_POS_ADDR (PT_BYTE
- 1) + 1;
386 /* Make a pointer to where consecutive chars leave off,
387 going backwards from point. */
390 else if (PT
<= GPT
|| BEGV
> GPT
)
395 if (tab_width
<= 0 || tab_width
> 1000)
398 col
= 0, tab_seen
= 0, post_tab
= 0;
407 /* We stopped either for the beginning of the buffer
409 if (ptr
== BEGV_ADDR
)
412 /* It was the gap. Jump back over it. */
416 /* Check whether that brings us to beginning of buffer. */
423 if (dp
&& VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
425 charvec
= DISP_CHAR_VECTOR (dp
, c
);
434 for (i
= n
- 1; i
>= 0; --i
)
436 if (VECTORP (charvec
))
438 /* This should be handled the same as
439 next_element_from_display_vector does it. */
440 Lisp_Object entry
= AREF (charvec
, i
);
443 && GLYPH_CHAR_VALID_P (XFASTINT (entry
)))
444 c
= FAST_GLYPH_CHAR (XFASTINT (entry
));
449 if (c
>= 040 && c
< 0177)
453 && EQ (current_buffer
->selective_display
, Qt
)))
456 goto start_of_line_found
;
461 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
467 else if (VECTORP (charvec
))
468 /* With a display table entry, C is displayed as is, and
469 not displayed as \NNN or as ^N. If C is a single-byte
470 character, it takes one column. If C is multi-byte in
471 an unibyte buffer, it's translated to unibyte, so it
472 also takes one column. */
475 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
483 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
487 if (ptr
== BEGV_ADDR
)
488 current_column_bol_cache
= BEGV
;
490 current_column_bol_cache
= BYTE_TO_CHAR (PTR_BYTE_POS (ptr
));
492 last_known_column
= col
;
493 last_known_column_point
= PT
;
494 last_known_column_modified
= MODIFF
;
499 /* Return the column number of position POS
500 by scanning forward from the beginning of the line.
501 This function handles characters that are invisible
502 due to text properties or overlays. */
507 register int tab_width
= XINT (current_buffer
->tab_width
);
508 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
509 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
510 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
512 /* Start the scan at the beginning of this line with column number 0. */
513 register int col
= 0;
515 int next_boundary
, next_boundary_byte
;
516 int opoint
= PT
, opoint_byte
= PT_BYTE
;
518 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
519 current_column_bol_cache
= PT
;
520 scan
= PT
, scan_byte
= PT_BYTE
;
521 SET_PT_BOTH (opoint
, opoint_byte
);
522 next_boundary
= scan
;
523 next_boundary_byte
= scan_byte
;
525 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
527 /* Scan forward to the target position. */
528 while (scan
< opoint
)
534 /* Occasionally we may need to skip invisible text. */
535 while (scan
== next_boundary
)
538 /* This updates NEXT_BOUNDARY to the next place
539 where we might need to skip more invisible text. */
540 scan
= skip_invisible (scan
, &next_boundary
, opoint
, Qnil
);
543 if (scan
!= old_scan
)
544 scan_byte
= CHAR_TO_BYTE (scan
);
545 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
548 /* Check composition sequence. */
550 int len
, len_byte
, width
;
552 if (check_composition (scan
, scan_byte
, opoint
,
553 &len
, &len_byte
, &width
))
556 scan_byte
+= len_byte
;
563 c
= FETCH_BYTE (scan_byte
);
566 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
567 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
569 charvec
= DISP_CHAR_VECTOR (dp
, c
);
578 for (i
= n
- 1; i
>= 0; --i
)
580 if (VECTORP (charvec
))
582 /* This should be handled the same as
583 next_element_from_display_vector does it. */
584 Lisp_Object entry
= AREF (charvec
, i
);
587 && GLYPH_CHAR_VALID_P (XFASTINT (entry
)))
588 c
= FAST_GLYPH_CHAR (XFASTINT (entry
));
595 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
603 col
= col
/ tab_width
* tab_width
;
605 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
608 int bytes
, width
, wide_column
;
611 ptr
= BYTE_POS_ADDR (scan_byte
);
612 MULTIBYTE_BYTES_WIDTH (ptr
, dp
);
616 else if (VECTORP (charvec
))
618 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
620 else if (c
< 040 || c
>= 0177)
628 last_known_column
= col
;
629 last_known_column_point
= PT
;
630 last_known_column_modified
= MODIFF
;
636 #if 0 /* Not used. */
638 /* Return the width in columns of the part of STRING from BEG to END.
639 If BEG is nil, that stands for the beginning of STRING.
640 If END is nil, that stands for the end of STRING. */
643 string_display_width (string
, beg
, end
)
644 Lisp_Object string
, beg
, end
;
647 register unsigned char *ptr
, *stop
;
648 register int tab_seen
;
651 register int tab_width
= XINT (current_buffer
->tab_width
);
652 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
653 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
657 e
= XSTRING (string
)->size
;
660 CHECK_NUMBER (end
, 0);
668 CHECK_NUMBER (beg
, 0);
672 /* Make a pointer for decrementing through the chars before point. */
673 ptr
= XSTRING (string
)->data
+ e
;
674 /* Make a pointer to where consecutive chars leave off,
675 going backwards from point. */
676 stop
= XSTRING (string
)->data
+ b
;
678 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
680 col
= 0, tab_seen
= 0, post_tab
= 0;
688 if (dp
!= 0 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
689 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
690 else if (c
>= 040 && c
< 0177)
697 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
704 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
709 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
719 DEFUN ("indent-to", Findent_to
, Sindent_to
, 1, 2, "NIndent to column: ",
720 "Indent from point with tabs and spaces until COLUMN is reached.\n\
721 Optional second argument MININUM says always do at least MININUM spaces\n\
722 even if that goes past COLUMN; by default, MININUM is zero.")
724 Lisp_Object column
, minimum
;
727 register int fromcol
;
728 register int tab_width
= XINT (current_buffer
->tab_width
);
730 CHECK_NUMBER (column
, 0);
732 XSETFASTINT (minimum
, 0);
733 CHECK_NUMBER (minimum
, 1);
735 fromcol
= current_column ();
736 mincol
= fromcol
+ XINT (minimum
);
737 if (mincol
< XINT (column
)) mincol
= XINT (column
);
739 if (fromcol
== mincol
)
740 return make_number (mincol
);
742 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
744 if (indent_tabs_mode
)
747 XSETFASTINT (n
, mincol
/ tab_width
- fromcol
/ tab_width
);
748 if (XFASTINT (n
) != 0)
750 Finsert_char (make_number ('\t'), n
, Qt
);
752 fromcol
= (mincol
/ tab_width
) * tab_width
;
756 XSETFASTINT (column
, mincol
- fromcol
);
757 Finsert_char (make_number (' '), column
, Qt
);
759 last_known_column
= mincol
;
760 last_known_column_point
= PT
;
761 last_known_column_modified
= MODIFF
;
763 XSETINT (column
, mincol
);
768 static int position_indentation
P_ ((int));
770 DEFUN ("current-indentation", Fcurrent_indentation
, Scurrent_indentation
,
772 "Return the indentation of the current line.\n\
773 This is the horizontal position of the character\n\
774 following any initial whitespace.")
778 int opoint
= PT
, opoint_byte
= PT_BYTE
;
780 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -1, 1);
782 XSETFASTINT (val
, position_indentation (PT_BYTE
));
783 SET_PT_BOTH (opoint
, opoint_byte
);
788 position_indentation (pos_byte
)
789 register int pos_byte
;
791 register int column
= 0;
792 register int tab_width
= XINT (current_buffer
->tab_width
);
793 register unsigned char *p
;
794 register unsigned char *stop
;
795 unsigned char *start
;
796 int next_boundary_byte
= pos_byte
;
797 int ceiling
= next_boundary_byte
;
799 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
801 p
= BYTE_POS_ADDR (pos_byte
);
802 /* STOP records the value of P at which we will need
803 to think about the gap, or about invisible text,
804 or about the end of the buffer. */
806 /* START records the starting value of P. */
814 /* If we have updated P, set POS_BYTE to match.
815 The first time we enter the loop, POS_BYTE is already right. */
817 pos_byte
= PTR_BYTE_POS (p
);
818 /* Consider the various reasons STOP might have been set here. */
819 if (pos_byte
== ZV_BYTE
)
821 if (pos_byte
== next_boundary_byte
)
824 int pos
= BYTE_TO_CHAR (pos_byte
);
825 pos
= skip_invisible (pos
, &next_boundary
, ZV
, Qnil
);
826 pos_byte
= CHAR_TO_BYTE (pos
);
827 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
829 if (pos_byte
>= ceiling
)
830 ceiling
= BUFFER_CEILING_OF (pos_byte
) + 1;
831 /* Compute the next place we need to stop and think,
832 and set STOP accordingly. */
833 stop_pos_byte
= min (ceiling
, next_boundary_byte
);
834 /* The -1 and +1 arrange to point at the first byte of gap
835 (if STOP_POS_BYTE is the position of the gap)
836 rather than at the data after the gap. */
838 stop
= BYTE_POS_ADDR (stop_pos_byte
- 1) + 1;
839 p
= BYTE_POS_ADDR (pos_byte
);
844 if (! NILP (current_buffer
->enable_multibyte_characters
))
850 column
+= tab_width
- column
% tab_width
;
853 if (ASCII_BYTE_P (p
[-1])
854 || NILP (current_buffer
->enable_multibyte_characters
))
858 pos_byte
= PTR_BYTE_POS (p
- 1);
859 c
= FETCH_MULTIBYTE_CHAR (pos_byte
);
860 if (CHAR_HAS_CATEGORY (c
, ' '))
864 p
= BYTE_POS_ADDR (pos_byte
);
873 /* Test whether the line beginning at POS is indented beyond COLUMN.
874 Blank lines are treated as if they had the same indentation as the
878 indented_beyond_p (pos
, pos_byte
, column
)
879 int pos
, pos_byte
, column
;
882 int opoint
= PT
, opoint_byte
= PT_BYTE
;
884 SET_PT_BOTH (pos
, pos_byte
);
885 while (PT
> BEGV
&& FETCH_BYTE (PT_BYTE
) == '\n')
886 scan_newline (PT
- 1, PT_BYTE
- 1, BEGV
, BEGV_BYTE
, -1, 0);
888 val
= position_indentation (PT_BYTE
);
889 SET_PT_BOTH (opoint
, opoint_byte
);
890 return val
>= column
;
893 DEFUN ("move-to-column", Fmove_to_column
, Smove_to_column
, 1, 2, "p",
894 "Move point to column COLUMN in the current line.\n\
895 The column of a character is calculated by adding together the widths\n\
896 as displayed of the previous characters in the line.\n\
897 This function ignores line-continuation;\n\
898 there is no upper limit on the column number a character can have\n\
899 and horizontal scrolling has no effect.\n\
901 If specified column is within a character, point goes after that character.\n\
902 If it's past end of line, point goes to end of line.\n\n\
903 A non-nil second (optional) argument FORCE means,\n\
904 if COLUMN is in the middle of a tab character, change it to spaces.\n\
905 In addition, if FORCE is t, and the line is too short\n\
906 to reach column COLUMN, add spaces/tabs to get there.\n\
908 The return value is the current column.")
910 Lisp_Object column
, force
;
913 register int col
= current_column ();
916 register int tab_width
= XINT (current_buffer
->tab_width
);
917 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
918 register struct Lisp_Char_Table
*dp
= buffer_display_table ();
919 register int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
926 int pos_byte
, end_byte
, next_boundary_byte
;
928 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
929 CHECK_NATNUM (column
, 0);
930 goal
= XINT (column
);
937 next_boundary_byte
= PT_BYTE
;
939 /* If we're starting past the desired column,
940 back up to beginning of line and scan from there. */
944 pos
= current_column_bol_cache
;
945 pos_byte
= CHAR_TO_BYTE (pos
);
954 while (pos
== next_boundary
)
957 pos
= skip_invisible (pos
, &next_boundary
, end
, Qnil
);
959 pos_byte
= CHAR_TO_BYTE (pos
);
960 next_boundary_byte
= CHAR_TO_BYTE (next_boundary
);
965 /* Test reaching the goal column. We do this after skipping
966 invisible characters, so that we put point before the
967 character on which the cursor will appear. */
971 /* Check composition sequence. */
973 int len
, len_byte
, width
;
975 if (check_composition (pos
, pos_byte
, Z
, &len
, &len_byte
, &width
))
978 pos_byte
+= len_byte
;
984 c
= FETCH_BYTE (pos_byte
);
987 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
988 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
990 charvec
= DISP_CHAR_VECTOR (dp
, c
);
999 for (i
= n
- 1; i
>= 0; --i
)
1001 if (VECTORP (charvec
))
1003 /* This should be handled the same as
1004 next_element_from_display_vector does it. */
1005 Lisp_Object entry
= AREF (charvec
, i
);
1007 if (INTEGERP (entry
)
1008 && GLYPH_CHAR_VALID_P (XFASTINT (entry
)))
1009 c
= FAST_GLYPH_CHAR (XFASTINT (entry
));
1017 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
1025 col
= col
/ tab_width
* tab_width
;
1027 else if (VECTORP (charvec
))
1029 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
1031 else if (c
< 040 || c
== 0177)
1035 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
1037 /* Start of multi-byte form. */
1039 int bytes
, width
, wide_column
;
1042 ptr
= BYTE_POS_ADDR (pos_byte
);
1043 MULTIBYTE_BYTES_WIDTH (ptr
, dp
);
1053 SET_PT_BOTH (pos
, pos_byte
);
1055 /* If a tab char made us overshoot, change it to spaces
1056 and scan through it again. */
1057 if (!NILP (force
) && col
> goal
&& c
== '\t' && prev_col
< goal
)
1059 int goal_pt
, goal_pt_byte
;
1061 /* Insert spaces in front of the tab to reach GOAL. Do this
1062 first so that a marker at the end of the tab gets
1064 SET_PT_BOTH (PT
- 1, PT_BYTE
- 1);
1065 Finsert_char (make_number (' '), make_number (goal
- prev_col
), Qt
);
1067 /* Now delete the tab, and indent to COL. */
1068 del_range (PT
, PT
+ 1);
1070 goal_pt_byte
= PT_BYTE
;
1071 Findent_to (make_number (col
), Qnil
);
1072 SET_PT_BOTH (goal_pt
, goal_pt_byte
);
1074 /* Set the last_known... vars consistently. */
1078 /* If line ends prematurely, add space to the end. */
1079 if (col
< goal
&& EQ (force
, Qt
))
1080 Findent_to (make_number (col
= goal
), Qnil
);
1082 last_known_column
= col
;
1083 last_known_column_point
= PT
;
1084 last_known_column_modified
= MODIFF
;
1086 XSETFASTINT (val
, col
);
1090 /* compute_motion: compute buffer posn given screen posn and vice versa */
1092 struct position val_compute_motion
;
1094 /* Scan the current buffer forward from offset FROM, pretending that
1095 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1096 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1097 and return the ending buffer position and screen location. If we
1098 can't hit the requested column exactly (because of a tab or other
1099 multi-column character), overshoot.
1101 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
1102 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1103 earlier call to compute_motion. The other common case is that FROMHPOS
1104 is zero and FROM is a position that "belongs" at column zero, but might
1105 be shifted by overlay strings; in this case DID_MOTION should be 0.
1107 WIDTH is the number of columns available to display text;
1108 compute_motion uses this to handle continuation lines and such.
1109 HSCROLL is the number of columns not being displayed at the left
1110 margin; this is usually taken from a window's hscroll member.
1111 TAB_OFFSET is the number of columns of the first tab that aren't
1112 being displayed, perhaps because of a continuation line or
1115 compute_motion returns a pointer to a struct position. The bufpos
1116 member gives the buffer position at the end of the scan, and hpos
1117 and vpos give its cartesian location. prevhpos is the column at
1118 which the character before bufpos started, and contin is non-zero
1119 if we reached the current line by continuing the previous.
1121 Note that FROMHPOS and TOHPOS should be expressed in real screen
1122 columns, taking HSCROLL and the truncation glyph at the left margin
1123 into account. That is, beginning-of-line moves you to the hpos
1124 -HSCROLL + (HSCROLL > 0).
1126 For example, to find the buffer position of column COL of line LINE
1127 of a certain window, pass the window's starting location as FROM
1128 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1129 Pass the buffer's ZV as TO, to limit the scan to the end of the
1130 visible section of the buffer, and pass LINE and COL as TOVPOS and
1133 When displaying in window w, a typical formula for WIDTH is:
1136 - (has_vertical_scroll_bars
1137 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
1138 : (window_width + window_left != frame_width))
1141 window_width is XFASTINT (w->width),
1142 window_left is XFASTINT (w->left),
1143 has_vertical_scroll_bars is
1144 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
1145 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
1147 Or you can let window_internal_width do this all for you, and write:
1148 window_internal_width (w) - 1
1150 The `-1' accounts for the continuation-line backslashes; the rest
1151 accounts for window borders if the window is split horizontally, and
1152 the scroll bars if they are turned on. */
1155 compute_motion (from
, fromvpos
, fromhpos
, did_motion
, to
, tovpos
, tohpos
, width
, hscroll
, tab_offset
, win
)
1156 int from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
;
1159 int hscroll
, tab_offset
;
1162 register int hpos
= fromhpos
;
1163 register int vpos
= fromvpos
;
1168 register int tab_width
= XFASTINT (current_buffer
->tab_width
);
1169 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
1170 register struct Lisp_Char_Table
*dp
= window_display_table (win
);
1172 = (INTEGERP (current_buffer
->selective_display
)
1173 ? XINT (current_buffer
->selective_display
)
1174 : !NILP (current_buffer
->selective_display
) ? -1 : 0);
1177 = (selective
&& dp
&& VECTORP (DISP_INVIS_VECTOR (dp
))
1178 ? XVECTOR (DISP_INVIS_VECTOR (dp
))->size
: 0);
1179 /* The next location where the `invisible' property changes, or an
1180 overlay starts or ends. */
1181 int next_boundary
= from
;
1183 /* For computing runs of characters with similar widths.
1184 Invariant: width_run_width is zero, or all the characters
1185 from width_run_start to width_run_end have a fixed width of
1187 int width_run_start
= from
;
1188 int width_run_end
= from
;
1189 int width_run_width
= 0;
1190 Lisp_Object
*width_table
;
1193 /* The next buffer pos where we should consult the width run cache. */
1194 int next_width_run
= from
;
1197 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1198 /* If previous char scanned was a wide character,
1199 this is the column where it ended. Otherwise, this is 0. */
1200 int wide_column_end_hpos
= 0;
1201 int prev_pos
; /* Previous buffer position. */
1202 int prev_pos_byte
; /* Previous buffer position. */
1203 int contin_hpos
; /* HPOS of last column of continued line. */
1204 int prev_tab_offset
; /* Previous tab offset. */
1206 XSETBUFFER (buffer
, current_buffer
);
1207 XSETWINDOW (window
, win
);
1209 width_run_cache_on_off ();
1210 if (dp
== buffer_display_table ())
1211 width_table
= (VECTORP (current_buffer
->width_table
)
1212 ? XVECTOR (current_buffer
->width_table
)->contents
1215 /* If the window has its own display table, we can't use the width
1216 run cache, because that's based on the buffer's display table. */
1219 if (tab_width
<= 0 || tab_width
> 1000)
1225 pos
= prev_pos
= from
;
1226 pos_byte
= prev_pos_byte
= CHAR_TO_BYTE (from
);
1228 prev_tab_offset
= tab_offset
;
1231 while (pos
== next_boundary
)
1236 /* Don't skip invisible if we are already at the margin. */
1237 if (vpos
> tovpos
|| vpos
== tovpos
&& hpos
>= tohpos
)
1239 if (contin_hpos
&& prev_hpos
== 0
1241 && (contin_hpos
== width
|| wide_column_end_hpos
> width
))
1242 { /* Line breaks because we can't put the character at the
1243 previous line any more. It is not the multi-column
1244 character continued in middle. Go back to previous
1245 buffer position, screen position, and set tab offset
1246 to previous value. It's the beginning of the
1249 pos_byte
= prev_pos_byte
;
1251 tab_offset
= prev_tab_offset
;
1256 /* If the caller says that the screen position came from an earlier
1257 call to compute_motion, then we've already accounted for the
1258 overlay strings at point. This is only true the first time
1259 through, so clear the flag after testing it. */
1261 /* We need to skip past the overlay strings. Currently those
1262 strings must not contain TAB;
1263 if we want to relax that restriction, something will have
1264 to be changed here. */
1266 unsigned char *ovstr
;
1267 int ovlen
= overlay_strings (pos
, win
, &ovstr
);
1268 hpos
+= ((multibyte
&& ovlen
> 0)
1269 ? strwidth (ovstr
, ovlen
) : ovlen
);
1276 /* Advance POS past invisible characters
1277 (but not necessarily all that there are here),
1278 and store in next_boundary the next position where
1279 we need to call skip_invisible. */
1280 newpos
= skip_invisible (pos
, &next_boundary
, to
, window
);
1284 pos
= min (to
, newpos
);
1285 pos_byte
= CHAR_TO_BYTE (pos
);
1289 if (newpos
!= pos_here
)
1292 pos_byte
= CHAR_TO_BYTE (pos
);
1296 /* Handle right margin. */
1297 /* Note on a wide-column character.
1299 Characters are classified into the following three categories
1300 according to the width (columns occupied on screen).
1302 (1) single-column character: ex. `a'
1303 (2) multi-column character: ex. `^A', TAB, `\033'
1304 (3) wide-column character: ex. Japanese character, Chinese character
1305 (In the following example, `W_' stands for them.)
1307 Multi-column characters can be divided around the right margin,
1308 but wide-column characters cannot.
1312 (*) The cursor is placed on the next character after the point.
1316 j ^---- next after the point
1317 ^--- next char. after the point.
1319 In case of sigle-column character
1323 033 ^---- next after the point, next char. after the point.
1325 In case of multi-column character
1329 W_ ^---- next after the point
1330 ^---- next char. after the point.
1332 In case of wide-column character
1334 The problem here is continuation at a wide-column character.
1335 In this case, the line may shorter less than WIDTH.
1336 And we find the continuation AFTER it occurs.
1343 || (truncate_partial_width_windows
1344 && width
+ 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win
))))
1345 || !NILP (current_buffer
->truncate_lines
))
1347 /* Truncating: skip to newline, unless we are already past
1348 TO (we need to go back below). */
1351 pos
= find_before_next_newline (pos
, to
, 1);
1352 pos_byte
= CHAR_TO_BYTE (pos
);
1354 /* If we just skipped next_boundary,
1355 loop around in the main while
1357 if (pos
>= next_boundary
)
1358 next_boundary
= pos
+ 1;
1360 prev_tab_offset
= tab_offset
;
1366 /* Remember the previous value. */
1367 prev_tab_offset
= tab_offset
;
1369 if (wide_column_end_hpos
> width
)
1372 tab_offset
+= prev_hpos
;
1376 tab_offset
+= width
;
1380 contin_hpos
= prev_hpos
;
1385 /* Stop if past the target buffer position or screen position. */
1388 /* Go back to the previous position. */
1390 pos_byte
= prev_pos_byte
;
1392 tab_offset
= prev_tab_offset
;
1394 /* NOTE on contin_hpos, hpos, and prev_hpos.
1398 W_ ^---- contin_hpos
1404 if (contin_hpos
&& prev_hpos
== 0
1405 && contin_hpos
< width
&& !wide_column_end_hpos
)
1407 /* Line breaking occurs in the middle of multi-column
1408 character. Go back to previous line. */
1413 /* If previous character is NEWLINE,
1414 set VPOS back to previous line */
1419 if (vpos
> tovpos
|| vpos
== tovpos
&& hpos
>= tohpos
)
1421 if (contin_hpos
&& prev_hpos
== 0
1423 && (contin_hpos
== width
|| wide_column_end_hpos
> width
))
1424 { /* Line breaks because we can't put the character at the
1425 previous line any more. It is not the multi-column
1426 character continued in middle. Go back to previous
1427 buffer position, screen position, and set tab offset
1428 to previous value. It's the beginning of the
1431 pos_byte
= prev_pos_byte
;
1433 tab_offset
= prev_tab_offset
;
1437 if (pos
== ZV
) /* We cannot go beyond ZV. Stop here. */
1442 prev_pos_byte
= pos_byte
;
1443 wide_column_end_hpos
= 0;
1445 /* Consult the width run cache to see if we can avoid inspecting
1446 the text character-by-character. */
1447 if (current_buffer
->width_run_cache
&& pos
>= next_width_run
)
1451 = region_cache_forward (current_buffer
,
1452 current_buffer
->width_run_cache
,
1455 /* A width of zero means the character's width varies (like
1456 a tab), is meaningless (like a newline), or we just don't
1457 want to skip over it for some other reason. */
1458 if (common_width
!= 0)
1462 /* Don't go past the final buffer posn the user
1467 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1469 /* Don't go past the final horizontal position the user
1471 if (vpos
== tovpos
&& run_end_hpos
> tohpos
)
1473 run_end
= pos
+ (tohpos
- hpos
) / common_width
;
1474 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1477 /* Don't go past the margin. */
1478 if (run_end_hpos
>= width
)
1480 run_end
= pos
+ (width
- hpos
) / common_width
;
1481 run_end_hpos
= hpos
+ (run_end
- pos
) * common_width
;
1484 hpos
= run_end_hpos
;
1486 prev_hpos
= hpos
- common_width
;
1490 pos_byte
= CHAR_TO_BYTE (pos
);
1494 next_width_run
= run_end
+ 1;
1497 /* We have to scan the text character-by-character. */
1501 Lisp_Object charvec
;
1503 c
= FETCH_BYTE (pos_byte
);
1505 /* Check composition sequence. */
1507 int len
, len_byte
, width
;
1509 if (check_composition (pos
, pos_byte
, to
, &len
, &len_byte
, &width
))
1512 pos_byte
+= len_byte
;
1520 /* Perhaps add some info to the width_run_cache. */
1521 if (current_buffer
->width_run_cache
)
1523 /* Is this character part of the current run? If so, extend
1525 if (pos
- 1 == width_run_end
1526 && XFASTINT (width_table
[c
]) == width_run_width
)
1527 width_run_end
= pos
;
1529 /* The previous run is over, since this is a character at a
1530 different position, or a different width. */
1533 /* Have we accumulated a run to put in the cache?
1534 (Currently, we only cache runs of width == 1). */
1535 if (width_run_start
< width_run_end
1536 && width_run_width
== 1)
1537 know_region_cache (current_buffer
,
1538 current_buffer
->width_run_cache
,
1539 width_run_start
, width_run_end
);
1541 /* Start recording a new width run. */
1542 width_run_width
= XFASTINT (width_table
[c
]);
1543 width_run_start
= pos
- 1;
1544 width_run_end
= pos
;
1549 && ! (multibyte
&& BASE_LEADING_CODE_P (c
))
1550 && VECTORP (DISP_CHAR_VECTOR (dp
, c
)))
1552 charvec
= DISP_CHAR_VECTOR (dp
, c
);
1553 n
= ASIZE (charvec
);
1561 for (i
= n
- 1; i
>= 0; --i
)
1563 if (VECTORP (charvec
))
1565 /* This should be handled the same as
1566 next_element_from_display_vector does it. */
1567 Lisp_Object entry
= AREF (charvec
, i
);
1569 if (INTEGERP (entry
)
1570 && GLYPH_CHAR_VALID_P (XFASTINT (entry
)))
1571 c
= FAST_GLYPH_CHAR (XFASTINT (entry
));
1576 if (c
>= 040 && c
< 0177)
1580 int tem
= ((hpos
+ tab_offset
+ hscroll
- (hscroll
> 0))
1584 hpos
+= tab_width
- tem
;
1589 && indented_beyond_p (pos
, pos_byte
, selective
))
1591 /* If (pos == to), we don't have to take care of
1592 selective display. */
1595 /* Skip any number of invisible lines all at once */
1598 pos
= find_before_next_newline (pos
, to
, 1);
1601 pos_byte
= CHAR_TO_BYTE (pos
);
1604 && indented_beyond_p (pos
, pos_byte
, selective
));
1605 /* Allow for the " ..." that is displayed for them. */
1608 hpos
+= selective_rlen
;
1612 DEC_BOTH (pos
, pos_byte
);
1613 /* We have skipped the invis text, but not the
1619 /* A visible line. */
1623 /* Count the truncation glyph on column 0 */
1630 else if (c
== CR
&& selective
< 0)
1632 /* In selective display mode,
1633 everything from a ^M to the end of the line is invisible.
1634 Stop *before* the real newline. */
1637 pos
= find_before_next_newline (pos
, to
, 1);
1638 pos_byte
= CHAR_TO_BYTE (pos
);
1640 /* If we just skipped next_boundary,
1641 loop around in the main while
1643 if (pos
> next_boundary
)
1644 next_boundary
= pos
;
1645 /* Allow for the " ..." that is displayed for them. */
1648 hpos
+= selective_rlen
;
1653 else if (multibyte
&& BASE_LEADING_CODE_P (c
))
1655 /* Start of multi-byte form. */
1657 int bytes
, width
, wide_column
;
1659 pos_byte
--; /* rewind POS_BYTE */
1660 ptr
= BYTE_POS_ADDR (pos_byte
);
1661 MULTIBYTE_BYTES_WIDTH (ptr
, dp
);
1664 wide_column_end_hpos
= hpos
+ wide_column
;
1667 else if (VECTORP (charvec
))
1670 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
1677 /* Remember any final width run in the cache. */
1678 if (current_buffer
->width_run_cache
1679 && width_run_width
== 1
1680 && width_run_start
< width_run_end
)
1681 know_region_cache (current_buffer
, current_buffer
->width_run_cache
,
1682 width_run_start
, width_run_end
);
1684 val_compute_motion
.bufpos
= pos
;
1685 val_compute_motion
.bytepos
= pos_byte
;
1686 val_compute_motion
.hpos
= hpos
;
1687 val_compute_motion
.vpos
= vpos
;
1688 if (contin_hpos
&& prev_hpos
== 0)
1689 val_compute_motion
.prevhpos
= contin_hpos
;
1691 val_compute_motion
.prevhpos
= prev_hpos
;
1692 /* We alalways handle all of them here; none of them remain to do. */
1693 val_compute_motion
.ovstring_chars_done
= 0;
1695 /* Nonzero if have just continued a line */
1696 val_compute_motion
.contin
= (contin_hpos
&& prev_hpos
== 0);
1699 return &val_compute_motion
;
1703 #if 0 /* The doc string is too long for some compilers,
1704 but make-docfile can find it in this comment. */
1705 DEFUN ("compute-motion", Ffoo
, Sfoo
, 7, 7, 0,
1706 "Scan through the current buffer, calculating screen position.\n\
1707 Scan the current buffer forward from offset FROM,\n\
1708 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1709 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1710 and return the ending buffer position and screen location.\n\
1712 There are three additional arguments:\n\
1714 WIDTH is the number of columns available to display text;\n\
1715 this affects handling of continuation lines.\n\
1716 This is usually the value returned by `window-width', less one (to allow\n\
1717 for the continuation glyph).\n\
1719 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1720 HSCROLL is the number of columns not being displayed at the left\n\
1721 margin; this is usually taken from a window's hscroll member.\n\
1722 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1723 being displayed, perhaps because the line was continued within it.\n\
1724 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1726 WINDOW is the window to operate on. It is used to choose the display table;\n\
1727 if it is showing the current buffer, it is used also for\n\
1728 deciding which overlay properties apply.\n\
1729 Note that `compute-motion' always operates on the current buffer.\n\
1731 The value is a list of five elements:\n\
1732 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1733 POS is the buffer position where the scan stopped.\n\
1734 VPOS is the vertical position where the scan stopped.\n\
1735 HPOS is the horizontal position where the scan stopped.\n\
1737 PREVHPOS is the horizontal position one character back from POS.\n\
1738 CONTIN is t if a line was continued after (or within) the previous character.\n\
1740 For example, to find the buffer position of column COL of line LINE\n\
1741 of a certain window, pass the window's starting location as FROM\n\
1742 and the window's upper-left coordinates as FROMPOS.\n\
1743 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1744 visible section of the buffer, and pass LINE and COL as TOPOS.")
1745 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1748 DEFUN ("compute-motion", Fcompute_motion
, Scompute_motion
, 7, 7, 0,
1750 (from
, frompos
, to
, topos
, width
, offsets
, window
)
1751 Lisp_Object from
, frompos
, to
, topos
;
1752 Lisp_Object width
, offsets
, window
;
1754 Lisp_Object bufpos
, hpos
, vpos
, prevhpos
;
1755 struct position
*pos
;
1756 int hscroll
, tab_offset
;
1758 CHECK_NUMBER_COERCE_MARKER (from
, 0);
1759 CHECK_CONS (frompos
, 0);
1760 CHECK_NUMBER (XCAR (frompos
), 0);
1761 CHECK_NUMBER (XCDR (frompos
), 0);
1762 CHECK_NUMBER_COERCE_MARKER (to
, 0);
1763 CHECK_CONS (topos
, 0);
1764 CHECK_NUMBER (XCAR (topos
), 0);
1765 CHECK_NUMBER (XCDR (topos
), 0);
1766 CHECK_NUMBER (width
, 0);
1767 if (!NILP (offsets
))
1769 CHECK_CONS (offsets
, 0);
1770 CHECK_NUMBER (XCAR (offsets
), 0);
1771 CHECK_NUMBER (XCDR (offsets
), 0);
1772 hscroll
= XINT (XCAR (offsets
));
1773 tab_offset
= XINT (XCDR (offsets
));
1776 hscroll
= tab_offset
= 0;
1779 window
= Fselected_window ();
1781 CHECK_LIVE_WINDOW (window
, 0);
1783 if (XINT (from
) < BEGV
|| XINT (from
) > ZV
)
1784 args_out_of_range_3 (from
, make_number (BEGV
), make_number (ZV
));
1785 if (XINT (to
) < BEGV
|| XINT (to
) > ZV
)
1786 args_out_of_range_3 (to
, make_number (BEGV
), make_number (ZV
));
1788 pos
= compute_motion (XINT (from
), XINT (XCDR (frompos
)),
1789 XINT (XCAR (frompos
)), 0,
1790 XINT (to
), XINT (XCDR (topos
)),
1791 XINT (XCAR (topos
)),
1792 XINT (width
), hscroll
, tab_offset
,
1795 XSETFASTINT (bufpos
, pos
->bufpos
);
1796 XSETINT (hpos
, pos
->hpos
);
1797 XSETINT (vpos
, pos
->vpos
);
1798 XSETINT (prevhpos
, pos
->prevhpos
);
1800 return Fcons (bufpos
,
1804 Fcons (pos
->contin
? Qt
: Qnil
, Qnil
)))));
1808 /* Fvertical_motion and vmotion */
1810 struct position val_vmotion
;
1813 vmotion (from
, vtarget
, w
)
1814 register int from
, vtarget
;
1817 int width
= window_internal_width (w
) - 1;
1818 int hscroll
= XINT (w
->hscroll
);
1819 struct position pos
;
1820 /* vpos is cumulative vertical position, changed as from is changed */
1821 register int vpos
= 0;
1822 Lisp_Object prevline
;
1825 int lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
1827 = (INTEGERP (current_buffer
->selective_display
)
1828 ? XINT (current_buffer
->selective_display
)
1829 : !NILP (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
->buffer
, 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 Lisp_Object propval
;
1856 XSETFASTINT (prevline
, find_next_newline_no_quit (from
- 1, -1));
1857 while (XFASTINT (prevline
) > BEGV
1859 && indented_beyond_p (XFASTINT (prevline
),
1860 CHAR_TO_BYTE (XFASTINT (prevline
)),
1862 /* watch out for newlines with `invisible' property */
1863 || (propval
= Fget_char_property (prevline
,
1866 TEXT_PROP_MEANS_INVISIBLE (propval
))))
1867 XSETFASTINT (prevline
,
1868 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1870 pos
= *compute_motion (XFASTINT (prevline
), 0,
1871 lmargin
+ (XFASTINT (prevline
) == BEG
1875 /* Don't care for VPOS... */
1876 1 << (BITS_PER_SHORT
- 1),
1878 1 << (BITS_PER_SHORT
- 1),
1880 /* This compensates for start_hpos
1881 so that a tab as first character
1882 still occupies 8 columns. */
1883 (XFASTINT (prevline
) == BEG
1888 from
= XFASTINT (prevline
);
1891 /* If we made exactly the desired vertical distance,
1892 or if we hit beginning of buffer,
1893 return point found */
1894 if (vpos
>= vtarget
)
1896 val_vmotion
.bufpos
= from
;
1897 val_vmotion
.bytepos
= CHAR_TO_BYTE (from
);
1898 val_vmotion
.vpos
= vpos
;
1899 val_vmotion
.hpos
= lmargin
;
1900 val_vmotion
.contin
= 0;
1901 val_vmotion
.prevhpos
= 0;
1902 val_vmotion
.ovstring_chars_done
= 0;
1903 val_vmotion
.tab_offset
= 0; /* For accumulating tab offset. */
1904 return &val_vmotion
;
1907 /* Otherwise find the correct spot by moving down */
1909 /* Moving downward is simple, but must calculate from beg of line
1910 to determine hpos of starting point */
1911 from_byte
= CHAR_TO_BYTE (from
);
1912 if (from
> BEGV
&& FETCH_BYTE (from_byte
- 1) != '\n')
1914 Lisp_Object propval
;
1916 XSETFASTINT (prevline
, find_next_newline_no_quit (from
, -1));
1917 while (XFASTINT (prevline
) > BEGV
1919 && indented_beyond_p (XFASTINT (prevline
),
1920 CHAR_TO_BYTE (XFASTINT (prevline
)),
1922 /* watch out for newlines with `invisible' property */
1923 || (propval
= Fget_char_property (prevline
, Qinvisible
,
1925 TEXT_PROP_MEANS_INVISIBLE (propval
))))
1926 XSETFASTINT (prevline
,
1927 find_next_newline_no_quit (XFASTINT (prevline
) - 1,
1929 pos
= *compute_motion (XFASTINT (prevline
), 0,
1930 lmargin
+ (XFASTINT (prevline
) == BEG
1934 /* Don't care for VPOS... */
1935 1 << (BITS_PER_SHORT
- 1),
1937 1 << (BITS_PER_SHORT
- 1),
1939 (XFASTINT (prevline
) == BEG
? -start_hpos
: 0),
1945 pos
.hpos
= lmargin
+ (from
== BEG
? start_hpos
: 0);
1950 return compute_motion (from
, vpos
, pos
.hpos
, did_motion
,
1951 ZV
, vtarget
, - (1 << (BITS_PER_SHORT
- 1)),
1953 pos
.tab_offset
- (from
== BEG
? start_hpos
: 0),
1957 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 2, 0,
1958 "Move point to start of the screen line LINES lines down.\n\
1959 If LINES is negative, this means moving up.\n\
1961 This function is an ordinary cursor motion function\n\
1962 which calculates the new position based on how text would be displayed.\n\
1963 The new position may be the start of a line,\n\
1964 or just the start of a continuation line.\n\
1965 The function returns number of screen lines moved over;\n\
1966 that usually equals LINES, but may be closer to zero\n\
1967 if beginning or end of buffer was reached.\n\
1969 The optional second argument WINDOW specifies the window to use for\n\
1970 parameters such as width, horizontal scrolling, and so on.\n\
1971 The default is to use the selected window's parameters.\n\
1973 `vertical-motion' always uses the current buffer,\n\
1974 regardless of which buffer is displayed in WINDOW.\n\
1975 This is consistent with other cursor motion functions\n\
1976 and makes it possible to use `vertical-motion' in any buffer,\n\
1977 whether or not it is currently displayed in some window.")
1979 Lisp_Object lines
, window
;
1984 Lisp_Object old_buffer
;
1985 struct gcpro gcpro1
;
1987 CHECK_NUMBER (lines
, 0);
1988 if (! NILP (window
))
1989 CHECK_WINDOW (window
, 0);
1991 window
= selected_window
;
1992 w
= XWINDOW (window
);
1995 GCPRO1 (old_buffer
);
1996 if (XBUFFER (w
->buffer
) != current_buffer
)
1998 /* Set the window's buffer temporarily to the current buffer. */
1999 old_buffer
= w
->buffer
;
2000 XSETBUFFER (w
->buffer
, current_buffer
);
2003 SET_TEXT_POS (pt
, PT
, PT_BYTE
);
2004 start_display (&it
, w
, pt
);
2005 move_it_by_lines (&it
, XINT (lines
), 0);
2006 SET_PT_BOTH (IT_CHARPOS (it
), IT_BYTEPOS (it
));
2008 if (BUFFERP (old_buffer
))
2009 w
->buffer
= old_buffer
;
2011 RETURN_UNGCPRO (make_number (it
.vpos
));
2016 /* File's initialization. */
2021 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode
,
2022 "*Indentation can insert tabs if this is non-nil.\n\
2023 Setting this variable automatically makes it local to the current buffer.");
2024 indent_tabs_mode
= 1;
2026 defsubr (&Scurrent_indentation
);
2027 defsubr (&Sindent_to
);
2028 defsubr (&Scurrent_column
);
2029 defsubr (&Smove_to_column
);
2030 defsubr (&Svertical_motion
);
2031 defsubr (&Scompute_motion
);