When opening new eww buffers, use buffer names based on the host name
[emacs.git] / src / indent.c
blob34449955a6c0376d122e95f53e170aa90b19c581
1 /* Indentation functions.
2 Copyright (C) 1985-1988, 1993-1995, 1998, 2000-2017 Free Software
3 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 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <stdio.h>
23 #include "lisp.h"
24 #include "character.h"
25 #include "buffer.h"
26 #include "category.h"
27 #include "composite.h"
28 #include "indent.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "disptab.h"
32 #include "intervals.h"
33 #include "dispextern.h"
34 #include "region-cache.h"
36 #define CR 015
38 /* These three values memorize the current column to avoid recalculation. */
40 /* Last value returned by current_column.
41 Some things in set last_known_column_point to -1
42 to mark the memorized value as invalid. */
44 static ptrdiff_t last_known_column;
46 /* Value of point when current_column was called. */
48 ptrdiff_t last_known_column_point;
50 /* Value of MODIFF when current_column was called. */
52 static EMACS_INT last_known_column_modified;
54 static ptrdiff_t current_column_1 (void);
55 static ptrdiff_t position_indentation (ptrdiff_t);
57 /* Get the display table to use for the current buffer. */
59 struct Lisp_Char_Table *
60 buffer_display_table (void)
62 Lisp_Object thisbuf;
64 thisbuf = BVAR (current_buffer, display_table);
65 if (DISP_TABLE_P (thisbuf))
66 return XCHAR_TABLE (thisbuf);
67 if (DISP_TABLE_P (Vstandard_display_table))
68 return XCHAR_TABLE (Vstandard_display_table);
69 return 0;
72 /* Width run cache considerations. */
74 /* Return the width of character C under display table DP. */
76 static int
77 character_width (int c, struct Lisp_Char_Table *dp)
79 Lisp_Object elt;
81 /* These width computations were determined by examining the cases
82 in display_text_line. */
84 /* Everything can be handled by the display table, if it's
85 present and the element is right. */
86 if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
87 return ASIZE (elt);
89 /* Some characters are special. */
90 if (c == '\n' || c == '\t' || c == '\015')
91 return 0;
93 /* Printing characters have width 1. */
94 else if (c >= 040 && c < 0177)
95 return 1;
97 /* Everybody else (control characters, metacharacters) has other
98 widths. We could return their actual widths here, but they
99 depend on things like ctl_arrow and crud like that, and they're
100 not very common at all. So we'll just claim we don't know their
101 widths. */
102 else
103 return 0;
106 /* Return true if the display table DISPTAB specifies the same widths
107 for characters as WIDTHTAB. We use this to decide when to
108 invalidate the buffer's width_run_cache. */
110 bool
111 disptab_matches_widthtab (struct Lisp_Char_Table *disptab, struct Lisp_Vector *widthtab)
113 int i;
115 eassert (widthtab->header.size == 256);
117 for (i = 0; i < 256; i++)
118 if (character_width (i, disptab)
119 != XFASTINT (widthtab->contents[i]))
120 return 0;
122 return 1;
125 /* Recompute BUF's width table, using the display table DISPTAB. */
127 void
128 recompute_width_table (struct buffer *buf, struct Lisp_Char_Table *disptab)
130 int i;
131 struct Lisp_Vector *widthtab;
133 if (!VECTORP (BVAR (buf, width_table)))
134 bset_width_table (buf, make_uninit_vector (256));
135 widthtab = XVECTOR (BVAR (buf, width_table));
136 eassert (widthtab->header.size == 256);
138 for (i = 0; i < 256; i++)
139 XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
142 /* Allocate or free the width run cache, as requested by the
143 current state of current_buffer's cache_long_scans variable. */
145 static struct region_cache *
146 width_run_cache_on_off (void)
148 struct buffer *cache_buffer = current_buffer;
149 bool indirect_p = false;
151 if (cache_buffer->base_buffer)
153 cache_buffer = cache_buffer->base_buffer;
154 indirect_p = true;
157 if (NILP (BVAR (current_buffer, cache_long_scans))
158 /* And, for the moment, this feature doesn't work on multibyte
159 characters. */
160 || !NILP (BVAR (current_buffer, enable_multibyte_characters)))
162 if (!indirect_p
163 || NILP (BVAR (cache_buffer, cache_long_scans))
164 || !NILP (BVAR (cache_buffer, enable_multibyte_characters)))
166 /* It should be off. */
167 if (cache_buffer->width_run_cache)
169 free_region_cache (cache_buffer->width_run_cache);
170 cache_buffer->width_run_cache = 0;
171 bset_width_table (current_buffer, Qnil);
174 return NULL;
176 else
178 if (!indirect_p
179 || (!NILP (BVAR (cache_buffer, cache_long_scans))
180 && NILP (BVAR (cache_buffer, enable_multibyte_characters))))
182 /* It should be on. */
183 if (cache_buffer->width_run_cache == 0)
185 cache_buffer->width_run_cache = new_region_cache ();
186 recompute_width_table (current_buffer, buffer_display_table ());
189 return cache_buffer->width_run_cache;
194 /* Skip some invisible characters starting from POS.
195 This includes characters invisible because of text properties
196 and characters invisible because of overlays.
198 If position POS is followed by invisible characters,
199 skip some of them and return the position after them.
200 Otherwise return POS itself.
202 Set *NEXT_BOUNDARY_P to the next position at which
203 it will be necessary to call this function again.
205 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
206 to a value greater than TO.
208 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
209 take account of overlays that apply only in WINDOW.
211 We don't necessarily skip all the invisible characters after POS
212 because that could take a long time. We skip a reasonable number
213 which can be skipped quickly. If there might be more invisible
214 characters immediately following, then *NEXT_BOUNDARY_P
215 will equal the return value. */
217 ptrdiff_t
218 skip_invisible (ptrdiff_t pos, ptrdiff_t *next_boundary_p, ptrdiff_t to, Lisp_Object window)
220 Lisp_Object prop, position, overlay_limit, proplimit;
221 Lisp_Object buffer, tmp;
222 ptrdiff_t end;
223 int inv_p;
225 XSETFASTINT (position, pos);
226 XSETBUFFER (buffer, current_buffer);
228 /* Give faster response for overlay lookup near POS. */
229 recenter_overlay_lists (current_buffer, pos);
231 /* We must not advance farther than the next overlay change.
232 The overlay change might change the invisible property;
233 or there might be overlay strings to be displayed there. */
234 overlay_limit = Fnext_overlay_change (position);
235 /* As for text properties, this gives a lower bound
236 for where the invisible text property could change. */
237 proplimit = Fnext_property_change (position, buffer, Qt);
238 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
239 proplimit = overlay_limit;
240 /* PROPLIMIT is now a lower bound for the next change
241 in invisible status. If that is plenty far away,
242 use that lower bound. */
243 if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
244 *next_boundary_p = XFASTINT (proplimit);
245 /* Otherwise, scan for the next `invisible' property change. */
246 else
248 /* Don't scan terribly far. */
249 XSETFASTINT (proplimit, min (pos + 100, to));
250 /* No matter what, don't go past next overlay change. */
251 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
252 proplimit = overlay_limit;
253 tmp = Fnext_single_property_change (position, Qinvisible,
254 buffer, proplimit);
255 end = XFASTINT (tmp);
256 #if 0
257 /* Don't put the boundary in the middle of multibyte form if
258 there is no actual property change. */
259 if (end == pos + 100
260 && !NILP (current_buffer->enable_multibyte_characters)
261 && end < ZV)
262 while (pos < end && !CHAR_HEAD_P (POS_ADDR (end)))
263 end--;
264 #endif
265 *next_boundary_p = end;
267 /* if the `invisible' property is set, we can skip to
268 the next property change */
269 prop = Fget_char_property (position, Qinvisible,
270 (!NILP (window)
271 && EQ (XWINDOW (window)->contents, buffer))
272 ? window : buffer);
273 inv_p = TEXT_PROP_MEANS_INVISIBLE (prop);
274 /* When counting columns (window == nil), don't skip over ellipsis text. */
275 if (NILP (window) ? inv_p == 1 : inv_p)
276 return *next_boundary_p;
277 return pos;
280 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
282 DP is a display table or NULL.
284 This macro is used in scan_for_column and in
285 compute_motion. */
287 #define MULTIBYTE_BYTES_WIDTH(p, dp, bytes, width) \
288 do { \
289 int ch; \
291 ch = STRING_CHAR_AND_LENGTH (p, bytes); \
292 if (BYTES_BY_CHAR_HEAD (*p) != bytes) \
293 width = bytes * 4; \
294 else \
296 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, ch))) \
297 width = sanitize_char_width (ASIZE (DISP_CHAR_VECTOR (dp, ch))); \
298 else \
299 width = CHARACTER_WIDTH (ch); \
301 } while (0)
304 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
305 doc: /* Return the horizontal position of point. Beginning of line is column 0.
306 This is calculated by adding together the widths of all the displayed
307 representations of the character between the start of the previous line
308 and point (e.g., control characters will have a width of 2 or 4, tabs
309 will have a variable width).
310 Ignores finite width of frame, which means that this function may return
311 values greater than (frame-width).
312 Whether the line is visible (if `selective-display' is t) has no effect;
313 however, ^M is treated as end of line when `selective-display' is t.
314 Text that has an invisible property is considered as having width 0, unless
315 `buffer-invisibility-spec' specifies that it is replaced by an ellipsis. */)
316 (void)
318 Lisp_Object temp;
319 XSETFASTINT (temp, current_column ());
320 return temp;
323 /* Cancel any recorded value of the horizontal position. */
325 void
326 invalidate_current_column (void)
328 last_known_column_point = 0;
331 ptrdiff_t
332 current_column (void)
334 ptrdiff_t col;
335 unsigned char *ptr, *stop;
336 bool tab_seen;
337 ptrdiff_t post_tab;
338 int c;
339 int tab_width = SANE_TAB_WIDTH (current_buffer);
340 bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
341 struct Lisp_Char_Table *dp = buffer_display_table ();
343 if (PT == last_known_column_point
344 && MODIFF == last_known_column_modified)
345 return last_known_column;
347 /* If the buffer has overlays, text properties,
348 or multibyte characters, use a more general algorithm. */
349 if (buffer_intervals (current_buffer)
350 || buffer_has_overlays ()
351 || Z != Z_BYTE)
352 return current_column_1 ();
354 /* Scan backwards from point to the previous newline,
355 counting width. Tab characters are the only complicated case. */
357 /* Make a pointer for decrementing through the chars before point. */
358 ptr = BYTE_POS_ADDR (PT_BYTE - 1) + 1;
359 /* Make a pointer to where consecutive chars leave off,
360 going backwards from point. */
361 if (PT == BEGV)
362 stop = ptr;
363 else if (PT <= GPT || BEGV > GPT)
364 stop = BEGV_ADDR;
365 else
366 stop = GAP_END_ADDR;
368 col = 0, tab_seen = 0, post_tab = 0;
370 while (1)
372 ptrdiff_t i, n;
373 Lisp_Object charvec;
375 if (ptr == stop)
377 /* We stopped either for the beginning of the buffer
378 or for the gap. */
379 if (ptr == BEGV_ADDR)
380 break;
382 /* It was the gap. Jump back over it. */
383 stop = BEGV_ADDR;
384 ptr = GPT_ADDR;
386 /* Check whether that brings us to beginning of buffer. */
387 if (BEGV >= GPT)
388 break;
391 c = *--ptr;
393 if (dp && VECTORP (DISP_CHAR_VECTOR (dp, c)))
395 charvec = DISP_CHAR_VECTOR (dp, c);
396 n = ASIZE (charvec);
398 else
400 charvec = Qnil;
401 n = 1;
404 for (i = n - 1; i >= 0; --i)
406 if (VECTORP (charvec))
408 /* This should be handled the same as
409 next_element_from_display_vector does it. */
410 Lisp_Object entry = AREF (charvec, i);
412 if (GLYPH_CODE_P (entry))
413 c = GLYPH_CODE_CHAR (entry);
414 else
415 c = ' ';
418 if (c >= 040 && c < 0177)
419 col++;
420 else if (c == '\n'
421 || (c == '\r'
422 && EQ (BVAR (current_buffer, selective_display), Qt)))
424 ptr++;
425 goto start_of_line_found;
427 else if (c == '\t')
429 if (tab_seen)
430 col = ((col + tab_width) / tab_width) * tab_width;
432 post_tab += col;
433 col = 0;
434 tab_seen = 1;
436 else if (VECTORP (charvec))
437 /* With a display table entry, C is displayed as is, and
438 not displayed as \NNN or as ^N. If C is a single-byte
439 character, it takes one column. If C is multi-byte in
440 a unibyte buffer, it's translated to unibyte, so it
441 also takes one column. */
442 ++col;
443 else
444 col += (ctl_arrow && c < 0200) ? 2 : 4;
448 start_of_line_found:
450 if (tab_seen)
452 col = ((col + tab_width) / tab_width) * tab_width;
453 col += post_tab;
456 last_known_column = col;
457 last_known_column_point = PT;
458 last_known_column_modified = MODIFF;
460 return col;
464 /* Check the presence of a display property and compute its width.
465 If a property was found and its width was found as well, return
466 its width (>= 0) and set the position of the end of the property
467 in ENDPOS.
468 Otherwise just return -1. */
469 static int
470 check_display_width (ptrdiff_t pos, ptrdiff_t col, ptrdiff_t *endpos)
472 Lisp_Object val, overlay;
474 if (CONSP (val = get_char_property_and_overlay
475 (make_number (pos), Qdisplay, Qnil, &overlay))
476 && EQ (Qspace, XCAR (val)))
477 { /* FIXME: Use calc_pixel_width_or_height. */
478 Lisp_Object plist = XCDR (val), prop;
479 int width = -1;
480 EMACS_INT align_to_max =
481 (col < MOST_POSITIVE_FIXNUM - INT_MAX
482 ? (EMACS_INT) INT_MAX + col
483 : MOST_POSITIVE_FIXNUM);
485 if ((prop = Fplist_get (plist, QCwidth),
486 RANGED_INTEGERP (0, prop, INT_MAX))
487 || (prop = Fplist_get (plist, QCrelative_width),
488 RANGED_INTEGERP (0, prop, INT_MAX)))
489 width = XINT (prop);
490 else if (FLOATP (prop) && 0 <= XFLOAT_DATA (prop)
491 && XFLOAT_DATA (prop) <= INT_MAX)
492 width = (int)(XFLOAT_DATA (prop) + 0.5);
493 else if ((prop = Fplist_get (plist, QCalign_to),
494 RANGED_INTEGERP (col, prop, align_to_max)))
495 width = XINT (prop) - col;
496 else if (FLOATP (prop) && col <= XFLOAT_DATA (prop)
497 && (XFLOAT_DATA (prop) <= align_to_max))
498 width = (int)(XFLOAT_DATA (prop) + 0.5) - col;
500 if (width >= 0)
502 ptrdiff_t start;
503 if (OVERLAYP (overlay))
504 *endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
505 else
506 get_property_and_range (pos, Qdisplay, &val, &start, endpos, Qnil);
508 /* For :relative-width, we need to multiply by the column
509 width of the character at POS, if it is greater than 1. */
510 if (!NILP (Fplist_get (plist, QCrelative_width))
511 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
513 int b, wd;
514 unsigned char *p = BYTE_POS_ADDR (CHAR_TO_BYTE (pos));
516 MULTIBYTE_BYTES_WIDTH (p, buffer_display_table (), b, wd);
517 width *= wd;
519 return width;
522 return -1;
525 /* Scanning from the beginning of the current line, stop at the buffer
526 position ENDPOS or at the column GOALCOL or at the end of line, whichever
527 comes first.
528 Return the resulting buffer position and column in ENDPOS and GOALCOL.
529 PREVCOL gets set to the column of the previous position (it's always
530 strictly smaller than the goal column). */
531 static void
532 scan_for_column (ptrdiff_t *endpos, EMACS_INT *goalcol, ptrdiff_t *prevcol)
534 int tab_width = SANE_TAB_WIDTH (current_buffer);
535 bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
536 struct Lisp_Char_Table *dp = buffer_display_table ();
537 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
538 struct composition_it cmp_it;
539 Lisp_Object window;
540 struct window *w;
542 /* Start the scan at the beginning of this line with column number 0. */
543 register ptrdiff_t col = 0, prev_col = 0;
544 EMACS_INT goal = goalcol ? *goalcol : MOST_POSITIVE_FIXNUM;
545 ptrdiff_t end = endpos ? *endpos : PT;
546 ptrdiff_t scan, scan_byte, next_boundary;
548 scan = find_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, NULL, &scan_byte, 1);
549 next_boundary = scan;
551 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
552 w = ! NILP (window) ? XWINDOW (window) : NULL;
554 memset (&cmp_it, 0, sizeof cmp_it);
555 cmp_it.id = -1;
556 composition_compute_stop_pos (&cmp_it, scan, scan_byte, end, Qnil);
558 /* Scan forward to the target position. */
559 while (scan < end)
561 int c;
563 /* Occasionally we may need to skip invisible text. */
564 while (scan == next_boundary)
566 ptrdiff_t old_scan = scan;
567 /* This updates NEXT_BOUNDARY to the next place
568 where we might need to skip more invisible text. */
569 scan = skip_invisible (scan, &next_boundary, end, Qnil);
570 if (scan != old_scan)
571 scan_byte = CHAR_TO_BYTE (scan);
572 if (scan >= end)
573 goto endloop;
576 /* Test reaching the goal column. We do this after skipping
577 invisible characters, so that we put point before the
578 character on which the cursor will appear. */
579 if (col >= goal)
580 break;
581 prev_col = col;
583 { /* Check display property. */
584 ptrdiff_t endp;
585 int width = check_display_width (scan, col, &endp);
586 if (width >= 0)
588 col += width;
589 if (endp > scan) /* Avoid infinite loops with 0-width overlays. */
591 scan = endp;
592 scan_byte = CHAR_TO_BYTE (scan);
593 continue;
598 /* Check composition sequence. */
599 if (cmp_it.id >= 0
600 || (scan == cmp_it.stop_pos
601 && composition_reseat_it (&cmp_it, scan, scan_byte, end,
602 w, NULL, Qnil)))
603 composition_update_it (&cmp_it, scan, scan_byte, Qnil);
604 if (cmp_it.id >= 0)
606 scan += cmp_it.nchars;
607 scan_byte += cmp_it.nbytes;
608 if (scan <= end)
609 col += cmp_it.width;
610 if (cmp_it.to == cmp_it.nglyphs)
612 cmp_it.id = -1;
613 composition_compute_stop_pos (&cmp_it, scan, scan_byte, end,
614 Qnil);
616 else
617 cmp_it.from = cmp_it.to;
618 continue;
621 c = FETCH_BYTE (scan_byte);
623 /* See if there is a display table and it relates
624 to this character. */
626 if (dp != 0
627 && ! (multibyte && LEADING_CODE_P (c))
628 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
630 Lisp_Object charvec;
631 ptrdiff_t i, n;
633 /* This character is displayed using a vector of glyphs.
634 Update the column/position based on those glyphs. */
636 charvec = DISP_CHAR_VECTOR (dp, c);
637 n = ASIZE (charvec);
639 for (i = 0; i < n; i++)
641 /* This should be handled the same as
642 next_element_from_display_vector does it. */
643 Lisp_Object entry = AREF (charvec, i);
645 if (GLYPH_CODE_P (entry))
646 c = GLYPH_CODE_CHAR (entry);
647 else
648 c = ' ';
650 if (c == '\n')
651 goto endloop;
652 if (c == '\r' && EQ (BVAR (current_buffer, selective_display), Qt))
653 goto endloop;
654 if (c == '\t')
656 col += tab_width;
657 col = col / tab_width * tab_width;
659 else
660 ++col;
663 else
665 /* The display table doesn't affect this character;
666 it displays as itself. */
668 if (c == '\n')
669 goto endloop;
670 if (c == '\r' && EQ (BVAR (current_buffer, selective_display), Qt))
671 goto endloop;
672 if (c == '\t')
674 col += tab_width;
675 col = col / tab_width * tab_width;
677 else if (multibyte && LEADING_CODE_P (c))
679 /* Start of multi-byte form. */
680 unsigned char *ptr;
681 int bytes, width;
683 ptr = BYTE_POS_ADDR (scan_byte);
684 MULTIBYTE_BYTES_WIDTH (ptr, dp, bytes, width);
685 /* Subtract one to compensate for the increment
686 that is going to happen below. */
687 scan_byte += bytes - 1;
688 col += width;
690 else if (ctl_arrow && (c < 040 || c == 0177))
691 col += 2;
692 else if (c < 040 || c >= 0177)
693 col += 4;
694 else
695 col++;
697 scan++;
698 scan_byte++;
701 endloop:
703 last_known_column = col;
704 last_known_column_point = PT;
705 last_known_column_modified = MODIFF;
707 if (goalcol)
708 *goalcol = col;
709 if (endpos)
710 *endpos = scan;
711 if (prevcol)
712 *prevcol = prev_col;
715 /* Return the column number of point
716 by scanning forward from the beginning of the line.
717 This function handles characters that are invisible
718 due to text properties or overlays. */
720 static ptrdiff_t
721 current_column_1 (void)
723 EMACS_INT col = MOST_POSITIVE_FIXNUM;
724 ptrdiff_t opoint = PT;
726 scan_for_column (&opoint, &col, NULL);
727 return col;
731 #if 0 /* Not used. */
733 /* Return the width in columns of the part of STRING from BEG to END.
734 If BEG is nil, that stands for the beginning of STRING.
735 If END is nil, that stands for the end of STRING. */
737 static double
738 string_display_width (Lisp_Object string, Lisp_Object beg, Lisp_Object end)
740 int col;
741 unsigned char *ptr, *stop;
742 bool tab_seen;
743 int post_tab;
744 int c;
745 int tab_width = SANE_TAB_WIDTH (current_buffer);
746 bool ctl_arrow = !NILP (current_buffer->ctl_arrow);
747 struct Lisp_Char_Table *dp = buffer_display_table ();
748 int b, e;
750 if (NILP (end))
751 e = SCHARS (string);
752 else
754 CHECK_NUMBER (end);
755 e = XINT (end);
758 if (NILP (beg))
759 b = 0;
760 else
762 CHECK_NUMBER (beg);
763 b = XINT (beg);
766 /* Make a pointer for decrementing through the chars before point. */
767 ptr = SDATA (string) + e;
768 /* Make a pointer to where consecutive chars leave off,
769 going backwards from point. */
770 stop = SDATA (string) + b;
772 col = 0, tab_seen = 0, post_tab = 0;
774 while (1)
776 if (ptr == stop)
777 break;
779 c = *--ptr;
780 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
781 col += ASIZE (DISP_CHAR_VECTOR (dp, c));
782 else if (c >= 040 && c < 0177)
783 col++;
784 else if (c == '\n')
785 break;
786 else if (c == '\t')
788 if (tab_seen)
789 col = ((col + tab_width) / tab_width) * tab_width;
791 post_tab += col;
792 col = 0;
793 tab_seen = 1;
795 else
796 col += (ctl_arrow && c < 0200) ? 2 : 4;
799 if (tab_seen)
801 col = ((col + tab_width) / tab_width) * tab_width;
802 col += post_tab;
805 return col;
808 #endif /* 0 */
811 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
812 doc: /* Indent from point with tabs and spaces until COLUMN is reached.
813 Optional second argument MINIMUM says always do at least MINIMUM spaces
814 even if that goes past COLUMN; by default, MINIMUM is zero.
816 The return value is COLUMN. */)
817 (Lisp_Object column, Lisp_Object minimum)
819 EMACS_INT mincol;
820 register ptrdiff_t fromcol;
821 int tab_width = SANE_TAB_WIDTH (current_buffer);
823 CHECK_NUMBER (column);
824 if (NILP (minimum))
825 XSETFASTINT (minimum, 0);
826 CHECK_NUMBER (minimum);
828 fromcol = current_column ();
829 mincol = fromcol + XINT (minimum);
830 if (mincol < XINT (column)) mincol = XINT (column);
832 if (fromcol == mincol)
833 return make_number (mincol);
835 if (indent_tabs_mode)
837 Lisp_Object n;
838 XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
839 if (XFASTINT (n) != 0)
841 Finsert_char (make_number ('\t'), n, Qt);
843 fromcol = (mincol / tab_width) * tab_width;
847 XSETFASTINT (column, mincol - fromcol);
848 Finsert_char (make_number (' '), column, Qt);
850 last_known_column = mincol;
851 last_known_column_point = PT;
852 last_known_column_modified = MODIFF;
854 XSETINT (column, mincol);
855 return column;
859 DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
860 0, 0, 0,
861 doc: /* Return the indentation of the current line.
862 This is the horizontal position of the character
863 following any initial whitespace. */)
864 (void)
866 ptrdiff_t posbyte;
868 find_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, NULL, &posbyte, 1);
869 return make_number (position_indentation (posbyte));
872 static ptrdiff_t
873 position_indentation (ptrdiff_t pos_byte)
875 register ptrdiff_t column = 0;
876 int tab_width = SANE_TAB_WIDTH (current_buffer);
877 register unsigned char *p;
878 register unsigned char *stop;
879 unsigned char *start;
880 ptrdiff_t next_boundary_byte = pos_byte;
881 ptrdiff_t ceiling = next_boundary_byte;
883 p = BYTE_POS_ADDR (pos_byte);
884 /* STOP records the value of P at which we will need
885 to think about the gap, or about invisible text,
886 or about the end of the buffer. */
887 stop = p;
888 /* START records the starting value of P. */
889 start = p;
890 while (1)
892 while (p == stop)
894 ptrdiff_t stop_pos_byte;
896 /* If we have updated P, set POS_BYTE to match.
897 The first time we enter the loop, POS_BYTE is already right. */
898 if (p != start)
899 pos_byte = PTR_BYTE_POS (p);
900 /* Consider the various reasons STOP might have been set here. */
901 if (pos_byte == ZV_BYTE)
902 return column;
903 if (pos_byte == next_boundary_byte)
905 ptrdiff_t next_boundary;
906 ptrdiff_t pos = BYTE_TO_CHAR (pos_byte);
907 pos = skip_invisible (pos, &next_boundary, ZV, Qnil);
908 pos_byte = CHAR_TO_BYTE (pos);
909 next_boundary_byte = CHAR_TO_BYTE (next_boundary);
911 if (pos_byte >= ceiling)
912 ceiling = BUFFER_CEILING_OF (pos_byte) + 1;
913 /* Compute the next place we need to stop and think,
914 and set STOP accordingly. */
915 stop_pos_byte = min (ceiling, next_boundary_byte);
916 /* The -1 and +1 arrange to point at the first byte of gap
917 (if STOP_POS_BYTE is the position of the gap)
918 rather than at the data after the gap. */
920 stop = BYTE_POS_ADDR (stop_pos_byte - 1) + 1;
921 p = BYTE_POS_ADDR (pos_byte);
923 switch (*p++)
925 case 0240:
926 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
927 return column;
928 case ' ':
929 column++;
930 break;
931 case '\t':
932 column += tab_width - column % tab_width;
933 break;
934 default:
935 if (ASCII_CHAR_P (p[-1])
936 || NILP (BVAR (current_buffer, enable_multibyte_characters)))
937 return column;
939 int c;
940 pos_byte = PTR_BYTE_POS (p - 1);
941 c = FETCH_MULTIBYTE_CHAR (pos_byte);
942 if (CHAR_HAS_CATEGORY (c, ' '))
944 column++;
945 INC_POS (pos_byte);
946 p = BYTE_POS_ADDR (pos_byte);
948 else
949 return column;
955 /* Test whether the line beginning at POS is indented beyond COLUMN.
956 Blank lines are treated as if they had the same indentation as the
957 preceding line. */
959 bool
960 indented_beyond_p (ptrdiff_t pos, ptrdiff_t pos_byte, EMACS_INT column)
962 while (pos > BEGV && FETCH_BYTE (pos_byte) == '\n')
964 DEC_BOTH (pos, pos_byte);
965 pos = find_newline (pos, pos_byte, BEGV, BEGV_BYTE,
966 -1, NULL, &pos_byte, 0);
968 return position_indentation (pos_byte) >= column;
971 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2,
972 "NMove to column: ",
973 doc: /* Move point to column COLUMN in the current line.
974 Interactively, COLUMN is the value of prefix numeric argument.
975 The column of a character is calculated by adding together the widths
976 as displayed of the previous characters in the line.
977 This function ignores line-continuation;
978 there is no upper limit on the column number a character can have
979 and horizontal scrolling has no effect.
981 If specified column is within a character, point goes after that character.
982 If it's past end of line, point goes to end of line.
984 Optional second argument FORCE non-nil means if COLUMN is in the
985 middle of a tab character, change it to spaces.
986 In addition, if FORCE is t, and the line is too short to reach
987 COLUMN, add spaces/tabs to get there.
989 The return value is the current column. */)
990 (Lisp_Object column, Lisp_Object force)
992 ptrdiff_t pos, prev_col;
993 EMACS_INT col;
994 EMACS_INT goal;
996 CHECK_NATNUM (column);
997 goal = XINT (column);
999 col = goal;
1000 pos = ZV;
1001 scan_for_column (&pos, &col, &prev_col);
1003 SET_PT (pos);
1005 /* If a tab char made us overshoot, change it to spaces
1006 and scan through it again. */
1007 if (!NILP (force) && col > goal)
1009 int c;
1010 ptrdiff_t pos_byte = PT_BYTE;
1012 DEC_POS (pos_byte);
1013 c = FETCH_CHAR (pos_byte);
1014 if (c == '\t' && prev_col < goal)
1016 ptrdiff_t goal_pt, goal_pt_byte;
1018 /* Insert spaces in front of the tab to reach GOAL. Do this
1019 first so that a marker at the end of the tab gets
1020 adjusted. */
1021 SET_PT_BOTH (PT - 1, PT_BYTE - 1);
1022 Finsert_char (make_number (' '), make_number (goal - prev_col), Qt);
1024 /* Now delete the tab, and indent to COL. */
1025 del_range (PT, PT + 1);
1026 goal_pt = PT;
1027 goal_pt_byte = PT_BYTE;
1028 Findent_to (make_number (col), Qnil);
1029 SET_PT_BOTH (goal_pt, goal_pt_byte);
1031 /* Set the last_known... vars consistently. */
1032 col = goal;
1036 /* If line ends prematurely, add space to the end. */
1037 if (col < goal && EQ (force, Qt))
1038 Findent_to (make_number (col = goal), Qnil);
1040 last_known_column = col;
1041 last_known_column_point = PT;
1042 last_known_column_modified = MODIFF;
1044 return make_number (col);
1047 /* compute_motion: compute buffer posn given screen posn and vice versa */
1049 static struct position val_compute_motion;
1051 /* Scan the current buffer forward from offset FROM, pretending that
1052 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1053 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1054 and return the ending buffer position and screen location. If we
1055 can't hit the requested column exactly (because of a tab or other
1056 multi-column character), overshoot.
1058 DID_MOTION is true if FROMHPOS has already accounted for overlay strings
1059 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1060 earlier call to compute_motion. The other common case is that FROMHPOS
1061 is zero and FROM is a position that "belongs" at column zero, but might
1062 be shifted by overlay strings; in this case DID_MOTION should be false.
1064 WIDTH is the number of columns available to display text;
1065 compute_motion uses this to handle continuation lines and such.
1066 If WIDTH is -1, use width of window's text area adjusted for
1067 continuation glyph when needed.
1069 HSCROLL is the number of columns not being displayed at the left
1070 margin; this is usually taken from a window's hscroll member.
1071 TAB_OFFSET is the number of columns of the first tab that aren't
1072 being displayed, perhaps because of a continuation line or
1073 something.
1075 compute_motion returns a pointer to a struct position. The bufpos
1076 member gives the buffer position at the end of the scan, and hpos
1077 and vpos give its cartesian location. prevhpos is the column at
1078 which the character before bufpos started, and contin is non-zero
1079 if we reached the current line by continuing the previous.
1081 Note that FROMHPOS and TOHPOS should be expressed in real screen
1082 columns, taking HSCROLL and the truncation glyph at the left margin
1083 into account. That is, beginning-of-line moves you to the hpos
1084 -HSCROLL + (HSCROLL > 0).
1086 For example, to find the buffer position of column COL of line LINE
1087 of a certain window, pass the window's starting location as FROM
1088 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1089 Pass the buffer's ZV as TO, to limit the scan to the end of the
1090 visible section of the buffer, and pass LINE and COL as TOVPOS and
1091 TOHPOS.
1093 When displaying in window w, a typical formula for WIDTH is:
1095 window_width - 1
1096 - (has_vertical_scroll_bars
1097 ? WINDOW_CONFIG_SCROLL_BAR_COLS (window)
1098 : (window_width + window_left != frame_cols))
1100 where
1101 window_width is w->total_cols,
1102 window_left is w->left_col,
1103 has_vertical_scroll_bars is
1104 WINDOW_HAS_VERTICAL_SCROLL_BAR (window)
1105 and frame_cols = FRAME_COLS (XFRAME (window->frame))
1107 Or you can let window_body_cols do this all for you, and write:
1108 window_body_cols (w) - 1
1110 The `-1' accounts for the continuation-line backslashes; the rest
1111 accounts for window borders if the window is split horizontally, and
1112 the scroll bars if they are turned on. */
1114 struct position *
1115 compute_motion (ptrdiff_t from, ptrdiff_t frombyte, EMACS_INT fromvpos,
1116 EMACS_INT fromhpos, bool did_motion, ptrdiff_t to,
1117 EMACS_INT tovpos, EMACS_INT tohpos, EMACS_INT width,
1118 ptrdiff_t hscroll, int tab_offset, struct window *win)
1120 EMACS_INT hpos = fromhpos;
1121 EMACS_INT vpos = fromvpos;
1123 ptrdiff_t pos;
1124 ptrdiff_t pos_byte;
1125 int c = 0;
1126 int tab_width = SANE_TAB_WIDTH (current_buffer);
1127 bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
1128 struct Lisp_Char_Table *dp = window_display_table (win);
1129 EMACS_INT selective
1130 = (INTEGERP (BVAR (current_buffer, selective_display))
1131 ? XINT (BVAR (current_buffer, selective_display))
1132 : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
1133 ptrdiff_t selective_rlen
1134 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
1135 ? ASIZE (DISP_INVIS_VECTOR (dp)) : 0);
1136 /* The next location where the `invisible' property changes, or an
1137 overlay starts or ends. */
1138 ptrdiff_t next_boundary = from;
1140 /* For computing runs of characters with similar widths.
1141 Invariant: width_run_width is zero, or all the characters
1142 from width_run_start to width_run_end have a fixed width of
1143 width_run_width. */
1144 ptrdiff_t width_run_start = from;
1145 ptrdiff_t width_run_end = from;
1146 ptrdiff_t width_run_width = 0;
1147 Lisp_Object *width_table;
1149 /* The next buffer pos where we should consult the width run cache. */
1150 ptrdiff_t next_width_run = from;
1151 Lisp_Object window;
1153 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
1154 /* If previous char scanned was a wide character,
1155 this is the column where it ended. Otherwise, this is 0. */
1156 EMACS_INT wide_column_end_hpos = 0;
1157 ptrdiff_t prev_pos; /* Previous buffer position. */
1158 ptrdiff_t prev_pos_byte; /* Previous buffer position. */
1159 EMACS_INT prev_hpos = 0;
1160 EMACS_INT prev_vpos = 0;
1161 EMACS_INT contin_hpos; /* HPOS of last column of continued line. */
1162 int prev_tab_offset; /* Previous tab offset. */
1163 int continuation_glyph_width;
1164 struct buffer *cache_buffer = current_buffer;
1165 struct region_cache *width_cache = NULL;
1167 struct composition_it cmp_it;
1169 XSETWINDOW (window, win);
1171 if (cache_buffer->base_buffer)
1172 cache_buffer = cache_buffer->base_buffer;
1173 if (dp == buffer_display_table ())
1175 width_table = (VECTORP (BVAR (current_buffer, width_table))
1176 ? XVECTOR (BVAR (current_buffer, width_table))->contents
1177 : 0);
1178 if (width_table)
1179 width_cache = width_run_cache_on_off ();
1181 else
1182 /* If the window has its own display table, we can't use the width
1183 run cache, because that's based on the buffer's display table. */
1184 width_table = 0;
1186 /* Negative width means use all available text columns. */
1187 if (width < 0)
1189 width = window_body_width (win, 0);
1190 /* We must make room for continuation marks if we don't have fringes. */
1191 #ifdef HAVE_WINDOW_SYSTEM
1192 if (!FRAME_WINDOW_P (XFRAME (win->frame)))
1193 #endif
1194 width -= 1;
1197 continuation_glyph_width = 1;
1198 #ifdef HAVE_WINDOW_SYSTEM
1199 if (FRAME_WINDOW_P (XFRAME (win->frame)))
1200 continuation_glyph_width = 0; /* In the fringe. */
1201 #endif
1203 immediate_quit = 1;
1204 QUIT;
1206 /* It's just impossible to be too paranoid here. */
1207 eassert (from == BYTE_TO_CHAR (frombyte) && frombyte == CHAR_TO_BYTE (from));
1209 pos = prev_pos = from;
1210 pos_byte = prev_pos_byte = frombyte;
1211 contin_hpos = 0;
1212 prev_tab_offset = tab_offset;
1213 memset (&cmp_it, 0, sizeof cmp_it);
1214 cmp_it.id = -1;
1215 composition_compute_stop_pos (&cmp_it, pos, pos_byte, to, Qnil);
1217 while (1)
1219 while (pos == next_boundary)
1221 ptrdiff_t pos_here = pos;
1222 ptrdiff_t newpos;
1224 /* Don't skip invisible if we are already at the margin. */
1225 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1227 if (contin_hpos && prev_hpos == 0
1228 && hpos > tohpos
1229 && (contin_hpos == width || wide_column_end_hpos > width))
1230 { /* Line breaks because we can't put the character at the
1231 previous line any more. It is not the multi-column
1232 character continued in middle. Go back to previous
1233 buffer position, screen position, and set tab offset
1234 to previous value. It's the beginning of the
1235 line. */
1236 pos = prev_pos;
1237 pos_byte = prev_pos_byte;
1238 hpos = prev_hpos;
1239 vpos = prev_vpos;
1240 tab_offset = prev_tab_offset;
1242 break;
1245 /* If the caller says that the screen position came from an earlier
1246 call to compute_motion, then we've already accounted for the
1247 overlay strings at point. This is only true the first time
1248 through, so clear the flag after testing it. */
1249 if (!did_motion)
1250 /* We need to skip past the overlay strings. Currently those
1251 strings must not contain TAB;
1252 if we want to relax that restriction, something will have
1253 to be changed here. */
1255 unsigned char *ovstr;
1256 ptrdiff_t ovlen = overlay_strings (pos, win, &ovstr);
1257 hpos += ((multibyte && ovlen > 0)
1258 ? strwidth ((char *) ovstr, ovlen) : ovlen);
1260 did_motion = 0;
1262 if (pos >= to)
1263 break;
1265 /* Advance POS past invisible characters
1266 (but not necessarily all that there are here),
1267 and store in next_boundary the next position where
1268 we need to call skip_invisible. */
1269 newpos = skip_invisible (pos, &next_boundary, to, window);
1271 if (newpos >= to)
1273 pos = min (to, newpos);
1274 pos_byte = CHAR_TO_BYTE (pos);
1275 goto after_loop;
1278 if (newpos != pos_here)
1280 pos = newpos;
1281 pos_byte = CHAR_TO_BYTE (pos);
1285 /* Handle right margin. */
1286 /* Note on a wide-column character.
1288 Characters are classified into the following three categories
1289 according to the width (columns occupied on screen).
1291 (1) single-column character: ex. `a'
1292 (2) multi-column character: ex. `^A', TAB, `\033'
1293 (3) wide-column character: ex. Japanese character, Chinese character
1294 (In the following example, `W_' stands for them.)
1296 Multi-column characters can be divided around the right margin,
1297 but wide-column characters cannot.
1299 NOTE:
1301 (*) The cursor is placed on the next character after the point.
1303 ----------
1304 abcdefghi\
1305 j ^---- next after the point
1306 ^--- next char. after the point.
1307 ----------
1308 In case of sigle-column character
1310 ----------
1311 abcdefgh\\
1312 033 ^---- next after the point, next char. after the point.
1313 ----------
1314 In case of multi-column character
1316 ----------
1317 abcdefgh\\
1318 W_ ^---- next after the point
1319 ^---- next char. after the point.
1320 ----------
1321 In case of wide-column character
1323 The problem here is continuation at a wide-column character.
1324 In this case, the line may shorter less than WIDTH.
1325 And we find the continuation AFTER it occurs.
1329 if (hpos > width)
1331 EMACS_INT total_width = width + continuation_glyph_width;
1332 bool truncate = 0;
1334 if (!NILP (Vtruncate_partial_width_windows)
1335 && (total_width < FRAME_COLS (XFRAME (WINDOW_FRAME (win)))))
1337 if (INTEGERP (Vtruncate_partial_width_windows))
1338 truncate
1339 = total_width < XFASTINT (Vtruncate_partial_width_windows);
1340 else
1341 truncate = 1;
1344 if (hscroll || truncate
1345 || !NILP (BVAR (current_buffer, truncate_lines)))
1347 /* Truncating: skip to newline, unless we are already past
1348 TO (we need to go back below). */
1349 if (pos <= to)
1351 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1352 hpos = width;
1353 /* If we just skipped next_boundary,
1354 loop around in the main while
1355 and handle it. */
1356 if (pos >= next_boundary)
1357 next_boundary = pos + 1;
1358 prev_hpos = width;
1359 prev_vpos = vpos;
1360 prev_tab_offset = tab_offset;
1363 else
1365 /* Continuing. */
1366 /* Remember the previous value. */
1367 prev_tab_offset = tab_offset;
1369 if (wide_column_end_hpos > width)
1371 hpos -= prev_hpos;
1372 tab_offset += prev_hpos;
1374 else
1376 tab_offset += width;
1377 hpos -= width;
1379 vpos++;
1380 contin_hpos = prev_hpos;
1381 prev_hpos = 0;
1382 prev_vpos = vpos;
1386 /* Stop if past the target buffer position or screen position. */
1387 if (pos > to)
1389 /* Go back to the previous position. */
1390 pos = prev_pos;
1391 pos_byte = prev_pos_byte;
1392 hpos = prev_hpos;
1393 vpos = prev_vpos;
1394 tab_offset = prev_tab_offset;
1396 /* NOTE on contin_hpos, hpos, and prev_hpos.
1398 ----------
1399 abcdefgh\\
1400 W_ ^---- contin_hpos
1401 | ^----- hpos
1402 \---- prev_hpos
1403 ----------
1406 if (contin_hpos && prev_hpos == 0
1407 && contin_hpos < width && !wide_column_end_hpos)
1409 /* Line breaking occurs in the middle of multi-column
1410 character. Go back to previous line. */
1411 hpos = contin_hpos;
1412 vpos = vpos - 1;
1414 break;
1417 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1419 if (contin_hpos && prev_hpos == 0
1420 && hpos > tohpos
1421 && (contin_hpos == width || wide_column_end_hpos > width))
1422 { /* Line breaks because we can't put the character at the
1423 previous line any more. It is not the multi-column
1424 character continued in middle. Go back to previous
1425 buffer position, screen position, and set tab offset
1426 to previous value. It's the beginning of the
1427 line. */
1428 pos = prev_pos;
1429 pos_byte = prev_pos_byte;
1430 hpos = prev_hpos;
1431 vpos = prev_vpos;
1432 tab_offset = prev_tab_offset;
1434 break;
1436 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1437 break;
1439 prev_hpos = hpos;
1440 prev_vpos = vpos;
1441 prev_pos = pos;
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 (width_cache && pos >= next_width_run)
1449 ptrdiff_t run_end;
1450 int common_width
1451 = region_cache_forward (cache_buffer, width_cache, pos, &run_end);
1453 /* A width of zero means the character's width varies (like
1454 a tab), is meaningless (like a newline), or we just don't
1455 want to skip over it for some other reason. */
1456 if (common_width != 0)
1458 ptrdiff_t run_end_hpos;
1460 /* Don't go past the final buffer posn the user
1461 requested. */
1462 if (run_end > to)
1463 run_end = to;
1465 run_end_hpos = hpos + (run_end - pos) * common_width;
1467 /* Don't go past the final horizontal position the user
1468 requested. */
1469 if (vpos == tovpos && run_end_hpos > tohpos)
1471 run_end = pos + (tohpos - hpos) / common_width;
1472 run_end_hpos = hpos + (run_end - pos) * common_width;
1475 /* Don't go past the margin. */
1476 if (run_end_hpos >= width)
1478 run_end = pos + (width - hpos) / common_width;
1479 run_end_hpos = hpos + (run_end - pos) * common_width;
1482 hpos = run_end_hpos;
1483 if (run_end > pos)
1484 prev_hpos = hpos - common_width;
1485 if (pos != run_end)
1487 pos = run_end;
1488 pos_byte = CHAR_TO_BYTE (pos);
1492 next_width_run = run_end + 1;
1495 /* We have to scan the text character-by-character. */
1496 else
1498 ptrdiff_t i, n;
1499 Lisp_Object charvec;
1501 /* Check composition sequence. */
1502 if (cmp_it.id >= 0
1503 || (pos == cmp_it.stop_pos
1504 && composition_reseat_it (&cmp_it, pos, pos_byte, to, win,
1505 NULL, Qnil)))
1506 composition_update_it (&cmp_it, pos, pos_byte, Qnil);
1507 if (cmp_it.id >= 0)
1509 pos += cmp_it.nchars;
1510 pos_byte += cmp_it.nbytes;
1511 hpos += cmp_it.width;
1512 if (cmp_it.to == cmp_it.nglyphs)
1514 cmp_it.id = -1;
1515 composition_compute_stop_pos (&cmp_it, pos, pos_byte, to,
1516 Qnil);
1518 else
1519 cmp_it.from = cmp_it.to;
1520 continue;
1523 c = FETCH_BYTE (pos_byte);
1524 pos++, pos_byte++;
1526 /* Perhaps add some info to the width_run_cache. */
1527 if (width_cache)
1529 /* Is this character part of the current run? If so, extend
1530 the run. */
1531 if (pos - 1 == width_run_end
1532 && XFASTINT (width_table[c]) == width_run_width)
1533 width_run_end = pos;
1535 /* The previous run is over, since this is a character at a
1536 different position, or a different width. */
1537 else
1539 /* Have we accumulated a run to put in the cache?
1540 (Currently, we only cache runs of width == 1). */
1541 if (width_run_start < width_run_end
1542 && width_run_width == 1)
1543 know_region_cache (cache_buffer, width_cache,
1544 width_run_start, width_run_end);
1546 /* Start recording a new width run. */
1547 width_run_width = XFASTINT (width_table[c]);
1548 width_run_start = pos - 1;
1549 width_run_end = pos;
1553 if (dp != 0
1554 && ! (multibyte && LEADING_CODE_P (c))
1555 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1557 charvec = DISP_CHAR_VECTOR (dp, c);
1558 n = ASIZE (charvec);
1560 else
1562 charvec = Qnil;
1563 n = 1;
1566 for (i = 0; i < n; ++i)
1568 if (VECTORP (charvec))
1570 /* This should be handled the same as
1571 next_element_from_display_vector does it. */
1572 Lisp_Object entry = AREF (charvec, i);
1574 if (GLYPH_CODE_P (entry))
1575 c = GLYPH_CODE_CHAR (entry);
1576 else
1577 c = ' ';
1580 if (c >= 040 && c < 0177)
1581 hpos++;
1582 else if (c == '\t')
1584 int tem = ((hpos + tab_offset + hscroll - (hscroll > 0))
1585 % tab_width);
1586 if (tem < 0)
1587 tem += tab_width;
1588 hpos += tab_width - tem;
1590 else if (c == '\n')
1592 if (selective > 0
1593 && indented_beyond_p (pos, pos_byte, selective))
1595 /* If (pos == to), we don't have to take care of
1596 selective display. */
1597 if (pos < to)
1599 /* Skip any number of invisible lines all at once */
1602 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1603 if (pos < to)
1604 INC_BOTH (pos, pos_byte);
1606 while (pos < to
1607 && indented_beyond_p (pos, pos_byte,
1608 selective));
1609 /* Allow for the " ..." that is displayed for them. */
1610 if (selective_rlen)
1612 hpos += selective_rlen;
1613 if (hpos >= width)
1614 hpos = width;
1616 DEC_BOTH (pos, pos_byte);
1617 /* We have skipped the invis text, but not the
1618 newline after. */
1621 else
1623 /* A visible line. */
1624 vpos++;
1625 hpos = 0;
1626 hpos -= hscroll;
1627 /* Count the truncation glyph on column 0 */
1628 if (hscroll > 0)
1629 hpos += continuation_glyph_width;
1630 tab_offset = 0;
1632 contin_hpos = 0;
1634 else if (c == CR && selective < 0)
1636 /* In selective display mode,
1637 everything from a ^M to the end of the line is invisible.
1638 Stop *before* the real newline. */
1639 if (pos < to)
1640 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1641 /* If we just skipped next_boundary,
1642 loop around in the main while
1643 and handle it. */
1644 if (pos > next_boundary)
1645 next_boundary = pos;
1646 /* Allow for the " ..." that is displayed for them. */
1647 if (selective_rlen)
1649 hpos += selective_rlen;
1650 if (hpos >= width)
1651 hpos = width;
1654 else if (multibyte && LEADING_CODE_P (c))
1656 /* Start of multi-byte form. */
1657 unsigned char *ptr;
1658 int mb_bytes, mb_width;
1660 pos_byte--; /* rewind POS_BYTE */
1661 ptr = BYTE_POS_ADDR (pos_byte);
1662 MULTIBYTE_BYTES_WIDTH (ptr, dp, mb_bytes, mb_width);
1663 pos_byte += mb_bytes;
1664 if (mb_width > 1 && BYTES_BY_CHAR_HEAD (*ptr) == mb_bytes)
1665 wide_column_end_hpos = hpos + mb_width;
1666 hpos += mb_width;
1668 else if (VECTORP (charvec))
1669 ++hpos;
1670 else
1671 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1676 after_loop:
1678 /* Remember any final width run in the cache. */
1679 if (width_cache
1680 && width_run_width == 1
1681 && width_run_start < width_run_end)
1682 know_region_cache (cache_buffer, width_cache,
1683 width_run_start, width_run_end);
1685 val_compute_motion.bufpos = pos;
1686 val_compute_motion.bytepos = pos_byte;
1687 val_compute_motion.hpos = hpos;
1688 val_compute_motion.vpos = vpos;
1689 if (contin_hpos && prev_hpos == 0)
1690 val_compute_motion.prevhpos = contin_hpos;
1691 else
1692 val_compute_motion.prevhpos = prev_hpos;
1694 /* Nonzero if have just continued a line */
1695 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1697 immediate_quit = 0;
1698 return &val_compute_motion;
1702 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1703 doc: /* Scan through the current buffer, calculating screen position.
1704 Scan the current buffer forward from offset FROM,
1705 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1706 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1707 and return the ending buffer position and screen location.
1709 If TOPOS is nil, the actual width and height of the window's
1710 text area are used.
1712 There are three additional arguments:
1714 WIDTH is the number of columns available to display text;
1715 this affects handling of continuation lines. A value of nil
1716 corresponds to the actual number of available text columns.
1718 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1719 HSCROLL is the number of columns not being displayed at the left
1720 margin; this is usually taken from a window's hscroll member.
1721 TAB-OFFSET is the number of columns of the first tab that aren't
1722 being displayed, perhaps because the line was continued within it.
1723 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1725 WINDOW is the window to operate on. It is used to choose the display table;
1726 if it is showing the current buffer, it is used also for
1727 deciding which overlay properties apply.
1728 Note that `compute-motion' always operates on the current buffer.
1730 The value is a list of five elements:
1731 (POS HPOS VPOS PREVHPOS CONTIN)
1732 POS is the buffer position where the scan stopped.
1733 VPOS is the vertical position where the scan stopped.
1734 HPOS is the horizontal position where the scan stopped.
1736 PREVHPOS is the horizontal position one character back from POS.
1737 CONTIN is t if a line was continued after (or within) the previous character.
1739 For example, to find the buffer position of column COL of line LINE
1740 of a certain window, pass the window's starting location as FROM
1741 and the window's upper-left coordinates as FROMPOS.
1742 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1743 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1744 (Lisp_Object from, Lisp_Object frompos, Lisp_Object to, Lisp_Object topos,
1745 Lisp_Object width, Lisp_Object offsets, Lisp_Object window)
1747 struct window *w;
1748 Lisp_Object bufpos, hpos, vpos, prevhpos;
1749 struct position *pos;
1750 ptrdiff_t hscroll;
1751 int tab_offset;
1753 CHECK_NUMBER_COERCE_MARKER (from);
1754 CHECK_CONS (frompos);
1755 CHECK_NUMBER_CAR (frompos);
1756 CHECK_NUMBER_CDR (frompos);
1757 CHECK_NUMBER_COERCE_MARKER (to);
1758 if (!NILP (topos))
1760 CHECK_CONS (topos);
1761 CHECK_NUMBER_CAR (topos);
1762 CHECK_NUMBER_CDR (topos);
1764 if (!NILP (width))
1765 CHECK_NUMBER (width);
1767 if (!NILP (offsets))
1769 CHECK_CONS (offsets);
1770 CHECK_NUMBER_CAR (offsets);
1771 CHECK_NUMBER_CDR (offsets);
1772 if (! (0 <= XINT (XCAR (offsets)) && XINT (XCAR (offsets)) <= PTRDIFF_MAX
1773 && 0 <= XINT (XCDR (offsets)) && XINT (XCDR (offsets)) <= INT_MAX))
1774 args_out_of_range (XCAR (offsets), XCDR (offsets));
1775 hscroll = XINT (XCAR (offsets));
1776 tab_offset = XINT (XCDR (offsets));
1778 else
1779 hscroll = tab_offset = 0;
1781 w = decode_live_window (window);
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), CHAR_TO_BYTE (XINT (from)),
1789 XINT (XCDR (frompos)),
1790 XINT (XCAR (frompos)), 0,
1791 XINT (to),
1792 (NILP (topos)
1793 ? window_internal_height (w)
1794 : XINT (XCDR (topos))),
1795 (NILP (topos)
1796 ? (window_body_width (w, 0)
1798 #ifdef HAVE_WINDOW_SYSTEM
1799 FRAME_WINDOW_P (XFRAME (w->frame)) ? 0 :
1800 #endif
1802 : XINT (XCAR (topos))),
1803 (NILP (width) ? -1 : XINT (width)),
1804 hscroll, tab_offset, w);
1806 XSETFASTINT (bufpos, pos->bufpos);
1807 XSETINT (hpos, pos->hpos);
1808 XSETINT (vpos, pos->vpos);
1809 XSETINT (prevhpos, pos->prevhpos);
1811 return list5 (bufpos, hpos, vpos, prevhpos, pos->contin ? Qt : Qnil);
1814 /* Fvertical_motion and vmotion. */
1816 static struct position val_vmotion;
1818 struct position *
1819 vmotion (register ptrdiff_t from, register ptrdiff_t from_byte,
1820 register EMACS_INT vtarget, struct window *w)
1822 ptrdiff_t hscroll = w->hscroll;
1823 struct position pos;
1824 /* VPOS is cumulative vertical position, changed as from is changed. */
1825 register EMACS_INT vpos = 0;
1826 ptrdiff_t prevline;
1827 register ptrdiff_t first;
1828 ptrdiff_t lmargin = hscroll > 0 ? 1 - hscroll : 0;
1829 ptrdiff_t selective
1830 = (INTEGERP (BVAR (current_buffer, selective_display))
1831 ? clip_to_bounds (-1, XINT (BVAR (current_buffer, selective_display)),
1832 PTRDIFF_MAX)
1833 : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
1834 Lisp_Object window;
1835 bool did_motion;
1836 /* This is the object we use for fetching character properties. */
1837 Lisp_Object text_prop_object;
1839 XSETWINDOW (window, w);
1841 /* If the window contains this buffer, use it for getting text properties.
1842 Otherwise use the current buffer as arg for doing that. */
1843 if (EQ (w->contents, Fcurrent_buffer ()))
1844 text_prop_object = window;
1845 else
1846 text_prop_object = Fcurrent_buffer ();
1848 if (vpos >= vtarget)
1850 /* To move upward, go a line at a time until
1851 we have gone at least far enough. */
1853 first = 1;
1855 while ((vpos > vtarget || first) && from > BEGV)
1857 ptrdiff_t bytepos = from_byte;
1858 Lisp_Object propval;
1860 prevline = from;
1861 DEC_BOTH (prevline, bytepos);
1862 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1864 while (prevline > BEGV
1865 && ((selective > 0
1866 && indented_beyond_p (prevline, bytepos, selective))
1867 /* Watch out for newlines with `invisible' property.
1868 When moving upward, check the newline before. */
1869 || (propval = Fget_char_property (make_number (prevline - 1),
1870 Qinvisible,
1871 text_prop_object),
1872 TEXT_PROP_MEANS_INVISIBLE (propval))))
1874 DEC_BOTH (prevline, bytepos);
1875 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1877 pos = *compute_motion (prevline, bytepos, 0, lmargin, 0, from,
1878 /* Don't care for VPOS... */
1879 1 << (SHRT_WIDTH - 1),
1880 /* ... nor HPOS. */
1881 1 << (SHRT_WIDTH - 1),
1882 -1, hscroll, 0, w);
1883 vpos -= pos.vpos;
1884 first = 0;
1885 from = prevline;
1886 from_byte = bytepos;
1889 /* If we made exactly the desired vertical distance, or
1890 if we hit beginning of buffer, return point found. */
1891 if (vpos >= vtarget)
1893 val_vmotion.bufpos = from;
1894 val_vmotion.bytepos = from_byte;
1895 val_vmotion.vpos = vpos;
1896 val_vmotion.hpos = lmargin;
1897 val_vmotion.contin = 0;
1898 val_vmotion.prevhpos = 0;
1899 return &val_vmotion;
1902 /* Otherwise find the correct spot by moving down. */
1905 /* Moving downward is simple, but must calculate from
1906 beg of line to determine hpos of starting point. */
1908 if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n')
1910 ptrdiff_t bytepos;
1911 Lisp_Object propval;
1913 prevline = find_newline_no_quit (from, from_byte, -1, &bytepos);
1914 while (prevline > BEGV
1915 && ((selective > 0
1916 && indented_beyond_p (prevline, bytepos, selective))
1917 /* Watch out for newlines with `invisible' property.
1918 When moving downward, check the newline after. */
1919 || (propval = Fget_char_property (make_number (prevline),
1920 Qinvisible,
1921 text_prop_object),
1922 TEXT_PROP_MEANS_INVISIBLE (propval))))
1924 DEC_BOTH (prevline, bytepos);
1925 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1927 pos = *compute_motion (prevline, bytepos, 0, lmargin, 0, from,
1928 /* Don't care for VPOS... */
1929 1 << (SHRT_WIDTH - 1),
1930 /* ... nor HPOS. */
1931 1 << (SHRT_WIDTH - 1),
1932 -1, hscroll, 0, w);
1933 did_motion = 1;
1935 else
1937 pos.hpos = lmargin;
1938 pos.vpos = 0;
1939 did_motion = 0;
1941 return compute_motion (from, from_byte, vpos, pos.hpos, did_motion,
1942 ZV, vtarget, - (1 << (SHRT_WIDTH - 1)),
1943 -1, hscroll, 0, w);
1946 /* In window W (derived from WINDOW), return x coordinate for column
1947 COL (derived from COLUMN). */
1948 static int
1949 window_column_x (struct window *w, Lisp_Object window,
1950 double col, Lisp_Object column)
1952 double x = col * FRAME_COLUMN_WIDTH (XFRAME (w->frame)) + 0.5;
1954 /* FIXME: Should this be limited to W's dimensions? */
1955 if (! (INT_MIN <= x && x <= INT_MAX))
1956 args_out_of_range (window, column);
1958 return x;
1961 /* Restore window's buffer and point. */
1963 static void
1964 restore_window_buffer (Lisp_Object list)
1966 struct window *w = decode_live_window (XCAR (list));
1967 list = XCDR (list);
1968 wset_buffer (w, XCAR (list));
1969 list = XCDR (list);
1970 set_marker_both (w->pointm, w->contents,
1971 XFASTINT (XCAR (list)),
1972 XFASTINT (XCAR (XCDR (list))));
1975 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 3, 0,
1976 doc: /* Move point to start of the screen line LINES lines down.
1977 If LINES is negative, this means moving up.
1979 This function is an ordinary cursor motion function
1980 which calculates the new position based on how text would be displayed.
1981 The new position may be the start of a line,
1982 or just the start of a continuation line.
1983 The function returns number of screen lines moved over;
1984 that usually equals LINES, but may be closer to zero
1985 if beginning or end of buffer was reached.
1987 The optional second argument WINDOW specifies the window to use for
1988 parameters such as width, horizontal scrolling, and so on.
1989 The default is to use the selected window's parameters.
1991 LINES can optionally take the form (COLS . LINES), in which case the
1992 motion will not stop at the start of a screen line but COLS column
1993 from the visual start of the line (if such exists on that line, that
1994 is). If the line is scrolled horizontally, COLS is interpreted
1995 visually, i.e., as addition to the columns of text beyond the left
1996 edge of the window.
1998 The optional third argument CUR-COL specifies the horizontal
1999 window-relative coordinate of point, in units of frame's canonical
2000 character width, where the function is invoked. If this argument is
2001 omitted or nil, the function will determine the point coordinate by
2002 going back to the beginning of the line.
2004 `vertical-motion' always uses the current buffer,
2005 regardless of which buffer is displayed in WINDOW.
2006 This is consistent with other cursor motion functions
2007 and makes it possible to use `vertical-motion' in any buffer,
2008 whether or not it is currently displayed in some window. */)
2009 (Lisp_Object lines, Lisp_Object window, Lisp_Object cur_col)
2011 struct it it;
2012 struct text_pos pt;
2013 struct window *w;
2014 Lisp_Object lcols;
2015 void *itdata = NULL;
2016 ptrdiff_t count = SPECPDL_INDEX ();
2018 /* Allow LINES to be of the form (HPOS . VPOS) aka (COLUMNS . LINES). */
2019 bool lcols_given = CONSP (lines);
2020 if (lcols_given)
2022 lcols = XCAR (lines);
2023 lines = XCDR (lines);
2026 CHECK_NUMBER (lines);
2027 w = decode_live_window (window);
2029 if (XBUFFER (w->contents) != current_buffer)
2031 /* Set the window's buffer temporarily to the current buffer. */
2032 Lisp_Object old = list4 (window, w->contents,
2033 make_number (marker_position (w->pointm)),
2034 make_number (marker_byte_position (w->pointm)));
2035 record_unwind_protect (restore_window_buffer, old);
2036 wset_buffer (w, Fcurrent_buffer ());
2037 set_marker_both (w->pointm, w->contents,
2038 BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
2041 if (noninteractive)
2043 struct position pos;
2044 pos = *vmotion (PT, PT_BYTE, XINT (lines), w);
2045 SET_PT_BOTH (pos.bufpos, pos.bytepos);
2046 it.vpos = pos.vpos;
2048 else
2050 ptrdiff_t it_start, it_overshoot_count = 0;
2051 int first_x;
2052 bool overshoot_handled = 0;
2053 bool disp_string_at_start_p = 0;
2054 ptrdiff_t nlines = XINT (lines);
2055 int vpos_init = 0;
2056 double start_col UNINIT;
2057 int start_x UNINIT;
2058 int to_x = -1;
2060 bool start_x_given = !NILP (cur_col);
2061 if (start_x_given)
2063 start_col = extract_float (cur_col);
2064 start_x = window_column_x (w, window, start_col, cur_col);
2067 itdata = bidi_shelve_cache ();
2068 SET_TEXT_POS (pt, PT, PT_BYTE);
2069 start_display (&it, w, pt);
2070 first_x = it.first_visible_x;
2071 it_start = IT_CHARPOS (it);
2073 /* See comments below for why we calculate this. */
2074 if (it.cmp_it.id >= 0)
2075 it_overshoot_count = 0;
2076 else if (it.method == GET_FROM_STRING)
2078 const char *s = SSDATA (it.string);
2079 const char *e = s + SBYTES (it.string);
2081 disp_string_at_start_p =
2082 /* If it.area is anything but TEXT_AREA, we need not bother
2083 about the display string, as it doesn't affect cursor
2084 positioning. */
2085 it.area == TEXT_AREA
2086 && it.string_from_display_prop_p
2087 /* A display string on anything but buffer text (e.g., on
2088 an overlay string) doesn't affect cursor positioning. */
2089 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER);
2090 while (s < e)
2092 if (*s++ == '\n')
2093 it_overshoot_count++;
2095 if (!it_overshoot_count)
2096 it_overshoot_count = -1;
2098 else
2099 it_overshoot_count =
2100 !(it.method == GET_FROM_IMAGE || it.method == GET_FROM_STRETCH);
2102 if (start_x_given)
2104 it.hpos = start_col;
2105 it.current_x = start_x;
2107 else
2109 /* Scan from the start of the line containing PT. If we don't
2110 do this, we start moving with IT->current_x == 0, while PT is
2111 really at some x > 0. */
2112 reseat_at_previous_visible_line_start (&it);
2113 it.current_x = it.hpos = 0;
2115 if (IT_CHARPOS (it) != PT)
2116 /* We used to temporarily disable selective display here; the
2117 comment said this is "so we don't move too far" (2005-01-19
2118 checkin by kfs). But this does nothing useful that I can
2119 tell, and it causes Bug#2694 . -- cyd */
2120 /* When the position we started from is covered by a display
2121 string, move_it_to will overshoot it, while vertical-motion
2122 wants to put the cursor _before_ the display string. So in
2123 that case, we move to buffer position before the display
2124 string, and avoid overshooting. But if the position before
2125 the display string is a newline, we don't do this, because
2126 otherwise we will end up in a screen line that is one too
2127 far back. */
2128 move_it_to (&it,
2129 (!disp_string_at_start_p
2130 || FETCH_BYTE (IT_BYTEPOS (it)) == '\n')
2131 ? PT
2132 : PT - 1,
2133 -1, -1, -1, MOVE_TO_POS);
2135 /* IT may move too far if truncate-lines is on and PT lies
2136 beyond the right margin. IT may also move too far if the
2137 starting point is on a Lisp string that has embedded
2138 newlines, or spans several screen lines. In these cases,
2139 backtrack. */
2140 if (IT_CHARPOS (it) > it_start)
2142 /* We need to backtrack also if the Lisp string contains no
2143 newlines, but there is a newline right after it. In this
2144 case, IT overshoots if there is an after-string just
2145 before the newline. */
2146 if (it_overshoot_count < 0
2147 && it.method == GET_FROM_BUFFER
2148 && it.c == '\n')
2149 it_overshoot_count = 1;
2150 else if (it_overshoot_count == 1 && it.vpos == 0
2151 && it.current_x < it.last_visible_x)
2153 /* If we came to the same screen line as the one where
2154 we started, we didn't overshoot the line, and won't
2155 need to backtrack after all. This happens, for
2156 example, when PT is in the middle of a composition. */
2157 it_overshoot_count = 0;
2159 else if (disp_string_at_start_p && it.vpos > 0)
2161 /* This is the case of a display string that spans
2162 several screen lines. In that case, we end up at the
2163 end of the string, and it.vpos tells us how many
2164 screen lines we need to backtrack. */
2165 it_overshoot_count = it.vpos;
2167 /* We will overshoot if lines are truncated and point lies
2168 beyond the right margin of the window. */
2169 if (it.line_wrap == TRUNCATE && it.current_x >= it.last_visible_x
2170 && it_overshoot_count == 0)
2171 it_overshoot_count = 1;
2172 if (it_overshoot_count > 0)
2173 move_it_by_lines (&it, -it_overshoot_count);
2175 overshoot_handled = 1;
2177 else if (IT_CHARPOS (it) == PT - 1
2178 && FETCH_BYTE (PT_BYTE - 1) == '\n'
2179 && nlines <= 0)
2181 /* The position we started from was covered by a display
2182 property, so we moved to position before the string, and
2183 backed up one line, because the character at PT - 1 is
2184 a newline. So we need one less line to go up (or exactly
2185 one line to go down if nlines == 0). */
2186 nlines++;
2187 /* But we still need to record that one line, in order to
2188 return the correct value to the caller. */
2189 vpos_init = -1;
2191 overshoot_handled = 1;
2193 if (lcols_given)
2194 to_x = window_column_x (w, window, extract_float (lcols), lcols);
2195 if (nlines <= 0)
2197 it.vpos = vpos_init;
2198 it.current_y = 0;
2199 /* Do this even if LINES is 0, so that we move back to the
2200 beginning of the current line as we ought. */
2201 if ((nlines < 0 && IT_CHARPOS (it) > 0)
2202 || (nlines == 0 && !(start_x_given && start_x <= to_x)))
2203 move_it_by_lines (&it, max (PTRDIFF_MIN, nlines));
2205 else if (overshoot_handled)
2207 it.vpos = vpos_init;
2208 it.current_y = 0;
2209 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines));
2211 else
2213 /* Otherwise, we are at the first row occupied by PT, which
2214 might span multiple screen lines (e.g., if it's on a
2215 multi-line display string). We want to start from the
2216 last line that it occupies. */
2217 if (it_start < ZV)
2219 while (IT_CHARPOS (it) <= it_start)
2221 it.vpos = 0;
2222 it.current_y = 0;
2223 move_it_by_lines (&it, 1);
2225 if (nlines > 1)
2226 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines - 1));
2228 else /* it_start = ZV */
2230 it.vpos = 0;
2231 it.current_y = 0;
2232 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines));
2233 /* We could have some display or overlay string at ZV,
2234 in which case it.vpos will be nonzero now, while
2235 actually we didn't move vertically at all. */
2236 if (IT_CHARPOS (it) == CHARPOS (pt) && CHARPOS (pt) == it_start)
2237 it.vpos = 0;
2241 /* Move to the goal column, if one was specified. If the window
2242 was originally hscrolled, the goal column is interpreted as
2243 an addition to the hscroll amount. */
2244 if (lcols_given)
2246 move_it_in_display_line (&it, ZV, first_x + to_x, MOVE_TO_X);
2247 /* If we find ourselves in the middle of an overlay string
2248 which includes a newline after current string position,
2249 we need to move by lines until we get out of the string,
2250 and then reposition point at the requested X coordinate;
2251 if we don't, the cursor will be placed just after the
2252 string, which might not be the requested column. */
2253 if (nlines > 0 && it.area == TEXT_AREA)
2255 while (it.method == GET_FROM_STRING
2256 && !it.string_from_display_prop_p
2257 && memchr (SSDATA (it.string) + IT_STRING_BYTEPOS (it),
2258 '\n',
2259 SBYTES (it.string) - IT_STRING_BYTEPOS (it)))
2261 move_it_by_lines (&it, 1);
2262 move_it_in_display_line (&it, ZV, first_x + to_x, MOVE_TO_X);
2267 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
2268 bidi_unshelve_cache (itdata, 0);
2271 unbind_to (count, Qnil);
2273 return make_number (it.vpos);
2278 /* File's initialization. */
2280 void
2281 syms_of_indent (void)
2283 DEFVAR_BOOL ("indent-tabs-mode", indent_tabs_mode,
2284 doc: /* Indentation can insert tabs if this is non-nil. */);
2285 indent_tabs_mode = 1;
2287 defsubr (&Scurrent_indentation);
2288 defsubr (&Sindent_to);
2289 defsubr (&Scurrent_column);
2290 defsubr (&Smove_to_column);
2291 defsubr (&Svertical_motion);
2292 defsubr (&Scompute_motion);