(ls-lisp-insert-directory): Make -B work
[emacs.git] / src / indent.c
blobee2e9c6fb71c99e1b6801546a6f287f512fc0d8f
1 /* Indentation functions.
2 Copyright (C) 1985-1988, 1993-1995, 1998, 2000-2016 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
10 (at 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 = CHAR_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;
1167 struct composition_it cmp_it;
1169 XSETWINDOW (window, win);
1171 if (cache_buffer->base_buffer)
1172 cache_buffer = cache_buffer->base_buffer;
1173 width_cache = width_run_cache_on_off ();
1174 if (dp == buffer_display_table ())
1175 width_table = (VECTORP (BVAR (current_buffer, width_table))
1176 ? XVECTOR (BVAR (current_buffer, width_table))->contents
1177 : 0);
1178 else
1179 /* If the window has its own display table, we can't use the width
1180 run cache, because that's based on the buffer's display table. */
1181 width_table = 0;
1183 /* Negative width means use all available text columns. */
1184 if (width < 0)
1186 width = window_body_width (win, 0);
1187 /* We must make room for continuation marks if we don't have fringes. */
1188 #ifdef HAVE_WINDOW_SYSTEM
1189 if (!FRAME_WINDOW_P (XFRAME (win->frame)))
1190 #endif
1191 width -= 1;
1194 continuation_glyph_width = 1;
1195 #ifdef HAVE_WINDOW_SYSTEM
1196 if (FRAME_WINDOW_P (XFRAME (win->frame)))
1197 continuation_glyph_width = 0; /* In the fringe. */
1198 #endif
1200 immediate_quit = 1;
1201 QUIT;
1203 /* It's just impossible to be too paranoid here. */
1204 eassert (from == BYTE_TO_CHAR (frombyte) && frombyte == CHAR_TO_BYTE (from));
1206 pos = prev_pos = from;
1207 pos_byte = prev_pos_byte = frombyte;
1208 contin_hpos = 0;
1209 prev_tab_offset = tab_offset;
1210 memset (&cmp_it, 0, sizeof cmp_it);
1211 cmp_it.id = -1;
1212 composition_compute_stop_pos (&cmp_it, pos, pos_byte, to, Qnil);
1214 while (1)
1216 while (pos == next_boundary)
1218 ptrdiff_t pos_here = pos;
1219 ptrdiff_t newpos;
1221 /* Don't skip invisible if we are already at the margin. */
1222 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1224 if (contin_hpos && prev_hpos == 0
1225 && hpos > tohpos
1226 && (contin_hpos == width || wide_column_end_hpos > width))
1227 { /* Line breaks because we can't put the character at the
1228 previous line any more. It is not the multi-column
1229 character continued in middle. Go back to previous
1230 buffer position, screen position, and set tab offset
1231 to previous value. It's the beginning of the
1232 line. */
1233 pos = prev_pos;
1234 pos_byte = prev_pos_byte;
1235 hpos = prev_hpos;
1236 vpos = prev_vpos;
1237 tab_offset = prev_tab_offset;
1239 break;
1242 /* If the caller says that the screen position came from an earlier
1243 call to compute_motion, then we've already accounted for the
1244 overlay strings at point. This is only true the first time
1245 through, so clear the flag after testing it. */
1246 if (!did_motion)
1247 /* We need to skip past the overlay strings. Currently those
1248 strings must not contain TAB;
1249 if we want to relax that restriction, something will have
1250 to be changed here. */
1252 unsigned char *ovstr;
1253 ptrdiff_t ovlen = overlay_strings (pos, win, &ovstr);
1254 hpos += ((multibyte && ovlen > 0)
1255 ? strwidth ((char *) ovstr, ovlen) : ovlen);
1257 did_motion = 0;
1259 if (pos >= to)
1260 break;
1262 /* Advance POS past invisible characters
1263 (but not necessarily all that there are here),
1264 and store in next_boundary the next position where
1265 we need to call skip_invisible. */
1266 newpos = skip_invisible (pos, &next_boundary, to, window);
1268 if (newpos >= to)
1270 pos = min (to, newpos);
1271 pos_byte = CHAR_TO_BYTE (pos);
1272 goto after_loop;
1275 if (newpos != pos_here)
1277 pos = newpos;
1278 pos_byte = CHAR_TO_BYTE (pos);
1282 /* Handle right margin. */
1283 /* Note on a wide-column character.
1285 Characters are classified into the following three categories
1286 according to the width (columns occupied on screen).
1288 (1) single-column character: ex. `a'
1289 (2) multi-column character: ex. `^A', TAB, `\033'
1290 (3) wide-column character: ex. Japanese character, Chinese character
1291 (In the following example, `W_' stands for them.)
1293 Multi-column characters can be divided around the right margin,
1294 but wide-column characters cannot.
1296 NOTE:
1298 (*) The cursor is placed on the next character after the point.
1300 ----------
1301 abcdefghi\
1302 j ^---- next after the point
1303 ^--- next char. after the point.
1304 ----------
1305 In case of sigle-column character
1307 ----------
1308 abcdefgh\\
1309 033 ^---- next after the point, next char. after the point.
1310 ----------
1311 In case of multi-column character
1313 ----------
1314 abcdefgh\\
1315 W_ ^---- next after the point
1316 ^---- next char. after the point.
1317 ----------
1318 In case of wide-column character
1320 The problem here is continuation at a wide-column character.
1321 In this case, the line may shorter less than WIDTH.
1322 And we find the continuation AFTER it occurs.
1326 if (hpos > width)
1328 EMACS_INT total_width = width + continuation_glyph_width;
1329 bool truncate = 0;
1331 if (!NILP (Vtruncate_partial_width_windows)
1332 && (total_width < FRAME_COLS (XFRAME (WINDOW_FRAME (win)))))
1334 if (INTEGERP (Vtruncate_partial_width_windows))
1335 truncate
1336 = total_width < XFASTINT (Vtruncate_partial_width_windows);
1337 else
1338 truncate = 1;
1341 if (hscroll || truncate
1342 || !NILP (BVAR (current_buffer, truncate_lines)))
1344 /* Truncating: skip to newline, unless we are already past
1345 TO (we need to go back below). */
1346 if (pos <= to)
1348 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1349 hpos = width;
1350 /* If we just skipped next_boundary,
1351 loop around in the main while
1352 and handle it. */
1353 if (pos >= next_boundary)
1354 next_boundary = pos + 1;
1355 prev_hpos = width;
1356 prev_vpos = vpos;
1357 prev_tab_offset = tab_offset;
1360 else
1362 /* Continuing. */
1363 /* Remember the previous value. */
1364 prev_tab_offset = tab_offset;
1366 if (wide_column_end_hpos > width)
1368 hpos -= prev_hpos;
1369 tab_offset += prev_hpos;
1371 else
1373 tab_offset += width;
1374 hpos -= width;
1376 vpos++;
1377 contin_hpos = prev_hpos;
1378 prev_hpos = 0;
1379 prev_vpos = vpos;
1383 /* Stop if past the target buffer position or screen position. */
1384 if (pos > to)
1386 /* Go back to the previous position. */
1387 pos = prev_pos;
1388 pos_byte = prev_pos_byte;
1389 hpos = prev_hpos;
1390 vpos = prev_vpos;
1391 tab_offset = prev_tab_offset;
1393 /* NOTE on contin_hpos, hpos, and prev_hpos.
1395 ----------
1396 abcdefgh\\
1397 W_ ^---- contin_hpos
1398 | ^----- hpos
1399 \---- prev_hpos
1400 ----------
1403 if (contin_hpos && prev_hpos == 0
1404 && contin_hpos < width && !wide_column_end_hpos)
1406 /* Line breaking occurs in the middle of multi-column
1407 character. Go back to previous line. */
1408 hpos = contin_hpos;
1409 vpos = vpos - 1;
1411 break;
1414 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1416 if (contin_hpos && prev_hpos == 0
1417 && hpos > tohpos
1418 && (contin_hpos == width || wide_column_end_hpos > width))
1419 { /* Line breaks because we can't put the character at the
1420 previous line any more. It is not the multi-column
1421 character continued in middle. Go back to previous
1422 buffer position, screen position, and set tab offset
1423 to previous value. It's the beginning of the
1424 line. */
1425 pos = prev_pos;
1426 pos_byte = prev_pos_byte;
1427 hpos = prev_hpos;
1428 vpos = prev_vpos;
1429 tab_offset = prev_tab_offset;
1431 break;
1433 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1434 break;
1436 prev_hpos = hpos;
1437 prev_vpos = vpos;
1438 prev_pos = pos;
1439 prev_pos_byte = pos_byte;
1440 wide_column_end_hpos = 0;
1442 /* Consult the width run cache to see if we can avoid inspecting
1443 the text character-by-character. */
1444 if (width_cache && pos >= next_width_run)
1446 ptrdiff_t run_end;
1447 int common_width
1448 = region_cache_forward (cache_buffer, width_cache, pos, &run_end);
1450 /* A width of zero means the character's width varies (like
1451 a tab), is meaningless (like a newline), or we just don't
1452 want to skip over it for some other reason. */
1453 if (common_width != 0)
1455 ptrdiff_t run_end_hpos;
1457 /* Don't go past the final buffer posn the user
1458 requested. */
1459 if (run_end > to)
1460 run_end = to;
1462 run_end_hpos = hpos + (run_end - pos) * common_width;
1464 /* Don't go past the final horizontal position the user
1465 requested. */
1466 if (vpos == tovpos && run_end_hpos > tohpos)
1468 run_end = pos + (tohpos - hpos) / common_width;
1469 run_end_hpos = hpos + (run_end - pos) * common_width;
1472 /* Don't go past the margin. */
1473 if (run_end_hpos >= width)
1475 run_end = pos + (width - hpos) / common_width;
1476 run_end_hpos = hpos + (run_end - pos) * common_width;
1479 hpos = run_end_hpos;
1480 if (run_end > pos)
1481 prev_hpos = hpos - common_width;
1482 if (pos != run_end)
1484 pos = run_end;
1485 pos_byte = CHAR_TO_BYTE (pos);
1489 next_width_run = run_end + 1;
1492 /* We have to scan the text character-by-character. */
1493 else
1495 ptrdiff_t i, n;
1496 Lisp_Object charvec;
1498 /* Check composition sequence. */
1499 if (cmp_it.id >= 0
1500 || (pos == cmp_it.stop_pos
1501 && composition_reseat_it (&cmp_it, pos, pos_byte, to, win,
1502 NULL, Qnil)))
1503 composition_update_it (&cmp_it, pos, pos_byte, Qnil);
1504 if (cmp_it.id >= 0)
1506 pos += cmp_it.nchars;
1507 pos_byte += cmp_it.nbytes;
1508 hpos += cmp_it.width;
1509 if (cmp_it.to == cmp_it.nglyphs)
1511 cmp_it.id = -1;
1512 composition_compute_stop_pos (&cmp_it, pos, pos_byte, to,
1513 Qnil);
1515 else
1516 cmp_it.from = cmp_it.to;
1517 continue;
1520 c = FETCH_BYTE (pos_byte);
1521 pos++, pos_byte++;
1523 /* Perhaps add some info to the width_run_cache. */
1524 if (width_cache)
1526 /* Is this character part of the current run? If so, extend
1527 the run. */
1528 if (pos - 1 == width_run_end
1529 && XFASTINT (width_table[c]) == width_run_width)
1530 width_run_end = pos;
1532 /* The previous run is over, since this is a character at a
1533 different position, or a different width. */
1534 else
1536 /* Have we accumulated a run to put in the cache?
1537 (Currently, we only cache runs of width == 1). */
1538 if (width_run_start < width_run_end
1539 && width_run_width == 1)
1540 know_region_cache (cache_buffer, width_cache,
1541 width_run_start, width_run_end);
1543 /* Start recording a new width run. */
1544 width_run_width = XFASTINT (width_table[c]);
1545 width_run_start = pos - 1;
1546 width_run_end = pos;
1550 if (dp != 0
1551 && ! (multibyte && LEADING_CODE_P (c))
1552 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1554 charvec = DISP_CHAR_VECTOR (dp, c);
1555 n = ASIZE (charvec);
1557 else
1559 charvec = Qnil;
1560 n = 1;
1563 for (i = 0; i < n; ++i)
1565 if (VECTORP (charvec))
1567 /* This should be handled the same as
1568 next_element_from_display_vector does it. */
1569 Lisp_Object entry = AREF (charvec, i);
1571 if (GLYPH_CODE_P (entry))
1572 c = GLYPH_CODE_CHAR (entry);
1573 else
1574 c = ' ';
1577 if (c >= 040 && c < 0177)
1578 hpos++;
1579 else if (c == '\t')
1581 int tem = ((hpos + tab_offset + hscroll - (hscroll > 0))
1582 % tab_width);
1583 if (tem < 0)
1584 tem += tab_width;
1585 hpos += tab_width - tem;
1587 else if (c == '\n')
1589 if (selective > 0
1590 && indented_beyond_p (pos, pos_byte, selective))
1592 /* If (pos == to), we don't have to take care of
1593 selective display. */
1594 if (pos < to)
1596 /* Skip any number of invisible lines all at once */
1599 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1600 if (pos < to)
1601 INC_BOTH (pos, pos_byte);
1603 while (pos < to
1604 && indented_beyond_p (pos, pos_byte,
1605 selective));
1606 /* Allow for the " ..." that is displayed for them. */
1607 if (selective_rlen)
1609 hpos += selective_rlen;
1610 if (hpos >= width)
1611 hpos = width;
1613 DEC_BOTH (pos, pos_byte);
1614 /* We have skipped the invis text, but not the
1615 newline after. */
1618 else
1620 /* A visible line. */
1621 vpos++;
1622 hpos = 0;
1623 hpos -= hscroll;
1624 /* Count the truncation glyph on column 0 */
1625 if (hscroll > 0)
1626 hpos += continuation_glyph_width;
1627 tab_offset = 0;
1629 contin_hpos = 0;
1631 else if (c == CR && selective < 0)
1633 /* In selective display mode,
1634 everything from a ^M to the end of the line is invisible.
1635 Stop *before* the real newline. */
1636 if (pos < to)
1637 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1638 /* If we just skipped next_boundary,
1639 loop around in the main while
1640 and handle it. */
1641 if (pos > next_boundary)
1642 next_boundary = pos;
1643 /* Allow for the " ..." that is displayed for them. */
1644 if (selective_rlen)
1646 hpos += selective_rlen;
1647 if (hpos >= width)
1648 hpos = width;
1651 else if (multibyte && LEADING_CODE_P (c))
1653 /* Start of multi-byte form. */
1654 unsigned char *ptr;
1655 int mb_bytes, mb_width;
1657 pos_byte--; /* rewind POS_BYTE */
1658 ptr = BYTE_POS_ADDR (pos_byte);
1659 MULTIBYTE_BYTES_WIDTH (ptr, dp, mb_bytes, mb_width);
1660 pos_byte += mb_bytes;
1661 if (mb_width > 1 && BYTES_BY_CHAR_HEAD (*ptr) == mb_bytes)
1662 wide_column_end_hpos = hpos + mb_width;
1663 hpos += mb_width;
1665 else if (VECTORP (charvec))
1666 ++hpos;
1667 else
1668 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1673 after_loop:
1675 /* Remember any final width run in the cache. */
1676 if (width_cache
1677 && width_run_width == 1
1678 && width_run_start < width_run_end)
1679 know_region_cache (cache_buffer, width_cache,
1680 width_run_start, width_run_end);
1682 val_compute_motion.bufpos = pos;
1683 val_compute_motion.bytepos = pos_byte;
1684 val_compute_motion.hpos = hpos;
1685 val_compute_motion.vpos = vpos;
1686 if (contin_hpos && prev_hpos == 0)
1687 val_compute_motion.prevhpos = contin_hpos;
1688 else
1689 val_compute_motion.prevhpos = prev_hpos;
1691 /* Nonzero if have just continued a line */
1692 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1694 immediate_quit = 0;
1695 return &val_compute_motion;
1699 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1700 doc: /* Scan through the current buffer, calculating screen position.
1701 Scan the current buffer forward from offset FROM,
1702 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1703 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1704 and return the ending buffer position and screen location.
1706 If TOPOS is nil, the actual width and height of the window's
1707 text area are used.
1709 There are three additional arguments:
1711 WIDTH is the number of columns available to display text;
1712 this affects handling of continuation lines. A value of nil
1713 corresponds to the actual number of available text columns.
1715 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1716 HSCROLL is the number of columns not being displayed at the left
1717 margin; this is usually taken from a window's hscroll member.
1718 TAB-OFFSET is the number of columns of the first tab that aren't
1719 being displayed, perhaps because the line was continued within it.
1720 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1722 WINDOW is the window to operate on. It is used to choose the display table;
1723 if it is showing the current buffer, it is used also for
1724 deciding which overlay properties apply.
1725 Note that `compute-motion' always operates on the current buffer.
1727 The value is a list of five elements:
1728 (POS HPOS VPOS PREVHPOS CONTIN)
1729 POS is the buffer position where the scan stopped.
1730 VPOS is the vertical position where the scan stopped.
1731 HPOS is the horizontal position where the scan stopped.
1733 PREVHPOS is the horizontal position one character back from POS.
1734 CONTIN is t if a line was continued after (or within) the previous character.
1736 For example, to find the buffer position of column COL of line LINE
1737 of a certain window, pass the window's starting location as FROM
1738 and the window's upper-left coordinates as FROMPOS.
1739 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1740 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1741 (Lisp_Object from, Lisp_Object frompos, Lisp_Object to, Lisp_Object topos,
1742 Lisp_Object width, Lisp_Object offsets, Lisp_Object window)
1744 struct window *w;
1745 Lisp_Object bufpos, hpos, vpos, prevhpos;
1746 struct position *pos;
1747 ptrdiff_t hscroll;
1748 int tab_offset;
1750 CHECK_NUMBER_COERCE_MARKER (from);
1751 CHECK_CONS (frompos);
1752 CHECK_NUMBER_CAR (frompos);
1753 CHECK_NUMBER_CDR (frompos);
1754 CHECK_NUMBER_COERCE_MARKER (to);
1755 if (!NILP (topos))
1757 CHECK_CONS (topos);
1758 CHECK_NUMBER_CAR (topos);
1759 CHECK_NUMBER_CDR (topos);
1761 if (!NILP (width))
1762 CHECK_NUMBER (width);
1764 if (!NILP (offsets))
1766 CHECK_CONS (offsets);
1767 CHECK_NUMBER_CAR (offsets);
1768 CHECK_NUMBER_CDR (offsets);
1769 if (! (0 <= XINT (XCAR (offsets)) && XINT (XCAR (offsets)) <= PTRDIFF_MAX
1770 && 0 <= XINT (XCDR (offsets)) && XINT (XCDR (offsets)) <= INT_MAX))
1771 args_out_of_range (XCAR (offsets), XCDR (offsets));
1772 hscroll = XINT (XCAR (offsets));
1773 tab_offset = XINT (XCDR (offsets));
1775 else
1776 hscroll = tab_offset = 0;
1778 w = decode_live_window (window);
1780 if (XINT (from) < BEGV || XINT (from) > ZV)
1781 args_out_of_range_3 (from, make_number (BEGV), make_number (ZV));
1782 if (XINT (to) < BEGV || XINT (to) > ZV)
1783 args_out_of_range_3 (to, make_number (BEGV), make_number (ZV));
1785 pos = compute_motion (XINT (from), CHAR_TO_BYTE (XINT (from)),
1786 XINT (XCDR (frompos)),
1787 XINT (XCAR (frompos)), 0,
1788 XINT (to),
1789 (NILP (topos)
1790 ? window_internal_height (w)
1791 : XINT (XCDR (topos))),
1792 (NILP (topos)
1793 ? (window_body_width (w, 0)
1795 #ifdef HAVE_WINDOW_SYSTEM
1796 FRAME_WINDOW_P (XFRAME (w->frame)) ? 0 :
1797 #endif
1799 : XINT (XCAR (topos))),
1800 (NILP (width) ? -1 : XINT (width)),
1801 hscroll, tab_offset, w);
1803 XSETFASTINT (bufpos, pos->bufpos);
1804 XSETINT (hpos, pos->hpos);
1805 XSETINT (vpos, pos->vpos);
1806 XSETINT (prevhpos, pos->prevhpos);
1808 return list5 (bufpos, hpos, vpos, prevhpos, pos->contin ? Qt : Qnil);
1811 /* Fvertical_motion and vmotion. */
1813 static struct position val_vmotion;
1815 struct position *
1816 vmotion (register ptrdiff_t from, register ptrdiff_t from_byte,
1817 register EMACS_INT vtarget, struct window *w)
1819 ptrdiff_t hscroll = w->hscroll;
1820 struct position pos;
1821 /* VPOS is cumulative vertical position, changed as from is changed. */
1822 register EMACS_INT vpos = 0;
1823 ptrdiff_t prevline;
1824 register ptrdiff_t first;
1825 ptrdiff_t lmargin = hscroll > 0 ? 1 - hscroll : 0;
1826 ptrdiff_t selective
1827 = (INTEGERP (BVAR (current_buffer, selective_display))
1828 ? clip_to_bounds (-1, XINT (BVAR (current_buffer, selective_display)),
1829 PTRDIFF_MAX)
1830 : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
1831 Lisp_Object window;
1832 bool did_motion;
1833 /* This is the object we use for fetching character properties. */
1834 Lisp_Object text_prop_object;
1836 XSETWINDOW (window, w);
1838 /* If the window contains this buffer, use it for getting text properties.
1839 Otherwise use the current buffer as arg for doing that. */
1840 if (EQ (w->contents, Fcurrent_buffer ()))
1841 text_prop_object = window;
1842 else
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. */
1850 first = 1;
1852 while ((vpos > vtarget || first) && from > BEGV)
1854 ptrdiff_t bytepos = from_byte;
1855 Lisp_Object propval;
1857 prevline = from;
1858 DEC_BOTH (prevline, bytepos);
1859 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1861 while (prevline > BEGV
1862 && ((selective > 0
1863 && indented_beyond_p (prevline, bytepos, selective))
1864 /* Watch out for newlines with `invisible' property.
1865 When moving upward, check the newline before. */
1866 || (propval = Fget_char_property (make_number (prevline - 1),
1867 Qinvisible,
1868 text_prop_object),
1869 TEXT_PROP_MEANS_INVISIBLE (propval))))
1871 DEC_BOTH (prevline, bytepos);
1872 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1874 pos = *compute_motion (prevline, bytepos, 0, lmargin, 0, from,
1875 /* Don't care for VPOS... */
1876 1 << (BITS_PER_SHORT - 1),
1877 /* ... nor HPOS. */
1878 1 << (BITS_PER_SHORT - 1),
1879 -1, hscroll, 0, w);
1880 vpos -= pos.vpos;
1881 first = 0;
1882 from = prevline;
1883 from_byte = bytepos;
1886 /* If we made exactly the desired vertical distance, or
1887 if we hit beginning of buffer, return point found. */
1888 if (vpos >= vtarget)
1890 val_vmotion.bufpos = from;
1891 val_vmotion.bytepos = from_byte;
1892 val_vmotion.vpos = vpos;
1893 val_vmotion.hpos = lmargin;
1894 val_vmotion.contin = 0;
1895 val_vmotion.prevhpos = 0;
1896 return &val_vmotion;
1899 /* Otherwise find the correct spot by moving down. */
1902 /* Moving downward is simple, but must calculate from
1903 beg of line to determine hpos of starting point. */
1905 if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n')
1907 ptrdiff_t bytepos;
1908 Lisp_Object propval;
1910 prevline = find_newline_no_quit (from, from_byte, -1, &bytepos);
1911 while (prevline > BEGV
1912 && ((selective > 0
1913 && indented_beyond_p (prevline, bytepos, selective))
1914 /* Watch out for newlines with `invisible' property.
1915 When moving downward, check the newline after. */
1916 || (propval = Fget_char_property (make_number (prevline),
1917 Qinvisible,
1918 text_prop_object),
1919 TEXT_PROP_MEANS_INVISIBLE (propval))))
1921 DEC_BOTH (prevline, bytepos);
1922 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1924 pos = *compute_motion (prevline, bytepos, 0, lmargin, 0, from,
1925 /* Don't care for VPOS... */
1926 1 << (BITS_PER_SHORT - 1),
1927 /* ... nor HPOS. */
1928 1 << (BITS_PER_SHORT - 1),
1929 -1, hscroll, 0, w);
1930 did_motion = 1;
1932 else
1934 pos.hpos = lmargin;
1935 pos.vpos = 0;
1936 did_motion = 0;
1938 return compute_motion (from, from_byte, vpos, pos.hpos, did_motion,
1939 ZV, vtarget, - (1 << (BITS_PER_SHORT - 1)),
1940 -1, hscroll, 0, w);
1943 /* In window W (derived from WINDOW), return x coordinate for column
1944 COL (derived from COLUMN). */
1945 static int
1946 window_column_x (struct window *w, Lisp_Object window,
1947 double col, Lisp_Object column)
1949 double x = col * FRAME_COLUMN_WIDTH (XFRAME (w->frame)) + 0.5;
1951 /* FIXME: Should this be limited to W's dimensions? */
1952 if (! (INT_MIN <= x && x <= INT_MAX))
1953 args_out_of_range (window, column);
1955 return x;
1958 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 3, 0,
1959 doc: /* Move point to start of the screen line LINES lines down.
1960 If LINES is negative, this means moving up.
1962 This function is an ordinary cursor motion function
1963 which calculates the new position based on how text would be displayed.
1964 The new position may be the start of a line,
1965 or just the start of a continuation line.
1966 The function returns number of screen lines moved over;
1967 that usually equals LINES, but may be closer to zero
1968 if beginning or end of buffer was reached.
1970 The optional second argument WINDOW specifies the window to use for
1971 parameters such as width, horizontal scrolling, and so on.
1972 The default is to use the selected window's parameters.
1974 LINES can optionally take the form (COLS . LINES), in which case the
1975 motion will not stop at the start of a screen line but COLS column
1976 from the visual start of the line (if such exists on that line, that
1977 is). If the line is scrolled horizontally, COLS is interpreted
1978 visually, i.e., as addition to the columns of text beyond the left
1979 edge of the window.
1981 The optional third argument CUR-COL specifies the horizontal
1982 window-relative coordinate of point, in units of frame's canonical
1983 character width, where the function is invoked. If this argument is
1984 omitted or nil, the function will determine the point coordinate by
1985 going back to the beginning of the line.
1987 `vertical-motion' always uses the current buffer,
1988 regardless of which buffer is displayed in WINDOW.
1989 This is consistent with other cursor motion functions
1990 and makes it possible to use `vertical-motion' in any buffer,
1991 whether or not it is currently displayed in some window. */)
1992 (Lisp_Object lines, Lisp_Object window, Lisp_Object cur_col)
1994 struct it it;
1995 struct text_pos pt;
1996 struct window *w;
1997 Lisp_Object old_buffer;
1998 EMACS_INT old_charpos IF_LINT (= 0), old_bytepos IF_LINT (= 0);
1999 Lisp_Object lcols;
2000 void *itdata = NULL;
2002 /* Allow LINES to be of the form (HPOS . VPOS) aka (COLUMNS . LINES). */
2003 bool lcols_given = CONSP (lines);
2004 if (lcols_given)
2006 lcols = XCAR (lines);
2007 lines = XCDR (lines);
2010 CHECK_NUMBER (lines);
2011 w = decode_live_window (window);
2013 old_buffer = Qnil;
2014 if (XBUFFER (w->contents) != current_buffer)
2016 /* Set the window's buffer temporarily to the current buffer. */
2017 old_buffer = w->contents;
2018 old_charpos = marker_position (w->pointm);
2019 old_bytepos = marker_byte_position (w->pointm);
2020 wset_buffer (w, Fcurrent_buffer ());
2021 set_marker_both (w->pointm, w->contents,
2022 BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
2025 if (noninteractive)
2027 struct position pos;
2028 pos = *vmotion (PT, PT_BYTE, XINT (lines), w);
2029 SET_PT_BOTH (pos.bufpos, pos.bytepos);
2031 else
2033 ptrdiff_t it_start, it_overshoot_count = 0;
2034 int first_x;
2035 bool overshoot_handled = 0;
2036 bool disp_string_at_start_p = 0;
2037 ptrdiff_t nlines = XINT (lines);
2038 int vpos_init = 0;
2039 double start_col;
2040 int start_x IF_LINT (= 0);
2041 int to_x = -1;
2043 bool start_x_given = !NILP (cur_col);
2044 if (start_x_given)
2046 start_col = extract_float (cur_col);
2047 start_x = window_column_x (w, window, start_col, cur_col);
2050 itdata = bidi_shelve_cache ();
2051 SET_TEXT_POS (pt, PT, PT_BYTE);
2052 start_display (&it, w, pt);
2053 first_x = it.first_visible_x;
2054 it_start = IT_CHARPOS (it);
2056 /* See comments below for why we calculate this. */
2057 if (it.cmp_it.id >= 0)
2058 it_overshoot_count = 0;
2059 else if (it.method == GET_FROM_STRING)
2061 const char *s = SSDATA (it.string);
2062 const char *e = s + SBYTES (it.string);
2064 disp_string_at_start_p =
2065 /* If it.area is anything but TEXT_AREA, we need not bother
2066 about the display string, as it doesn't affect cursor
2067 positioning. */
2068 it.area == TEXT_AREA
2069 && it.string_from_display_prop_p
2070 /* A display string on anything but buffer text (e.g., on
2071 an overlay string) doesn't affect cursor positioning. */
2072 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER);
2073 while (s < e)
2075 if (*s++ == '\n')
2076 it_overshoot_count++;
2078 if (!it_overshoot_count)
2079 it_overshoot_count = -1;
2081 else
2082 it_overshoot_count =
2083 !(it.method == GET_FROM_IMAGE || it.method == GET_FROM_STRETCH);
2085 if (start_x_given)
2087 it.hpos = start_col;
2088 it.current_x = start_x;
2090 else
2092 /* Scan from the start of the line containing PT. If we don't
2093 do this, we start moving with IT->current_x == 0, while PT is
2094 really at some x > 0. */
2095 reseat_at_previous_visible_line_start (&it);
2096 it.current_x = it.hpos = 0;
2098 if (IT_CHARPOS (it) != PT)
2099 /* We used to temporarily disable selective display here; the
2100 comment said this is "so we don't move too far" (2005-01-19
2101 checkin by kfs). But this does nothing useful that I can
2102 tell, and it causes Bug#2694 . -- cyd */
2103 /* When the position we started from is covered by a display
2104 string, move_it_to will overshoot it, while vertical-motion
2105 wants to put the cursor _before_ the display string. So in
2106 that case, we move to buffer position before the display
2107 string, and avoid overshooting. But if the position before
2108 the display string is a newline, we don't do this, because
2109 otherwise we will end up in a screen line that is one too
2110 far back. */
2111 move_it_to (&it,
2112 (!disp_string_at_start_p
2113 || FETCH_BYTE (IT_BYTEPOS (it)) == '\n')
2114 ? PT
2115 : PT - 1,
2116 -1, -1, -1, MOVE_TO_POS);
2118 /* IT may move too far if truncate-lines is on and PT lies
2119 beyond the right margin. IT may also move too far if the
2120 starting point is on a Lisp string that has embedded
2121 newlines, or spans several screen lines. In these cases,
2122 backtrack. */
2123 if (IT_CHARPOS (it) > it_start)
2125 /* We need to backtrack also if the Lisp string contains no
2126 newlines, but there is a newline right after it. In this
2127 case, IT overshoots if there is an after-string just
2128 before the newline. */
2129 if (it_overshoot_count < 0
2130 && it.method == GET_FROM_BUFFER
2131 && it.c == '\n')
2132 it_overshoot_count = 1;
2133 else if (it_overshoot_count == 1 && it.vpos == 0
2134 && it.current_x < it.last_visible_x)
2136 /* If we came to the same screen line as the one where
2137 we started, we didn't overshoot the line, and won't
2138 need to backtrack after all. This happens, for
2139 example, when PT is in the middle of a composition. */
2140 it_overshoot_count = 0;
2142 else if (disp_string_at_start_p && it.vpos > 0)
2144 /* This is the case of a display string that spans
2145 several screen lines. In that case, we end up at the
2146 end of the string, and it.vpos tells us how many
2147 screen lines we need to backtrack. */
2148 it_overshoot_count = it.vpos;
2150 /* We will overshoot if lines are truncated and point lies
2151 beyond the right margin of the window. */
2152 if (it.line_wrap == TRUNCATE && it.current_x >= it.last_visible_x
2153 && it_overshoot_count == 0)
2154 it_overshoot_count = 1;
2155 if (it_overshoot_count > 0)
2156 move_it_by_lines (&it, -it_overshoot_count);
2158 overshoot_handled = 1;
2160 else if (IT_CHARPOS (it) == PT - 1
2161 && FETCH_BYTE (PT_BYTE - 1) == '\n'
2162 && nlines <= 0)
2164 /* The position we started from was covered by a display
2165 property, so we moved to position before the string, and
2166 backed up one line, because the character at PT - 1 is
2167 a newline. So we need one less line to go up (or exactly
2168 one line to go down if nlines == 0). */
2169 nlines++;
2170 /* But we still need to record that one line, in order to
2171 return the correct value to the caller. */
2172 vpos_init = -1;
2174 overshoot_handled = 1;
2176 if (lcols_given)
2177 to_x = window_column_x (w, window, extract_float (lcols), lcols);
2178 if (nlines <= 0)
2180 it.vpos = vpos_init;
2181 /* Do this even if LINES is 0, so that we move back to the
2182 beginning of the current line as we ought. */
2183 if ((nlines < 0 && IT_CHARPOS (it) > 0)
2184 || (nlines == 0 && !(start_x_given && start_x <= to_x)))
2185 move_it_by_lines (&it, max (PTRDIFF_MIN, nlines));
2187 else if (overshoot_handled)
2189 it.vpos = vpos_init;
2190 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines));
2192 else
2194 /* Otherwise, we are at the first row occupied by PT, which
2195 might span multiple screen lines (e.g., if it's on a
2196 multi-line display string). We want to start from the
2197 last line that it occupies. */
2198 if (it_start < ZV)
2200 while (IT_CHARPOS (it) <= it_start)
2202 it.vpos = 0;
2203 move_it_by_lines (&it, 1);
2205 if (nlines > 1)
2206 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines - 1));
2208 else /* it_start = ZV */
2210 it.vpos = 0;
2211 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines));
2212 /* We could have some display or overlay string at ZV,
2213 in which case it.vpos will be nonzero now, while
2214 actually we didn't move vertically at all. */
2215 if (IT_CHARPOS (it) == CHARPOS (pt) && CHARPOS (pt) == it_start)
2216 it.vpos = 0;
2220 /* Move to the goal column, if one was specified. If the window
2221 was originally hscrolled, the goal column is interpreted as
2222 an addition to the hscroll amount. */
2223 if (lcols_given)
2225 move_it_in_display_line (&it, ZV, first_x + to_x, MOVE_TO_X);
2226 /* If we find ourselves in the middle of an overlay string
2227 which includes a newline after current string position,
2228 we need to move by lines until we get out of the string,
2229 and then reposition point at the requested X coordinate;
2230 if we don't, the cursor will be placed just after the
2231 string, which might not be the requested column. */
2232 if (nlines > 0 && it.area == TEXT_AREA)
2234 while (it.method == GET_FROM_STRING
2235 && !it.string_from_display_prop_p
2236 && memchr (SSDATA (it.string) + IT_STRING_BYTEPOS (it),
2237 '\n',
2238 SBYTES (it.string) - IT_STRING_BYTEPOS (it)))
2240 move_it_by_lines (&it, 1);
2241 move_it_in_display_line (&it, ZV, first_x + to_x, MOVE_TO_X);
2246 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
2247 bidi_unshelve_cache (itdata, 0);
2250 if (BUFFERP (old_buffer))
2252 wset_buffer (w, old_buffer);
2253 set_marker_both (w->pointm, w->contents,
2254 old_charpos, old_bytepos);
2257 return make_number (it.vpos);
2262 /* File's initialization. */
2264 void
2265 syms_of_indent (void)
2267 DEFVAR_BOOL ("indent-tabs-mode", indent_tabs_mode,
2268 doc: /* Indentation can insert tabs if this is non-nil. */);
2269 indent_tabs_mode = 1;
2271 defsubr (&Scurrent_indentation);
2272 defsubr (&Sindent_to);
2273 defsubr (&Scurrent_column);
2274 defsubr (&Smove_to_column);
2275 defsubr (&Svertical_motion);
2276 defsubr (&Scompute_motion);