* lisp/mail/smtpmail.el (smtpmail-send-data-1): No string-as-multibyte
[emacs.git] / src / indent.c
blob1a8bcebddc916ff20f849ce96b96fb5ff60e6d36
1 /* Indentation functions.
2 Copyright (C) 1985-1988, 1993-1995, 1998, 2000-2018 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 <https://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 FALLTHROUGH;
929 case ' ':
930 column++;
931 break;
932 case '\t':
933 column += tab_width - column % tab_width;
934 break;
935 default:
936 if (ASCII_CHAR_P (p[-1])
937 || NILP (BVAR (current_buffer, enable_multibyte_characters)))
938 return column;
940 int c;
941 pos_byte = PTR_BYTE_POS (p - 1);
942 c = FETCH_MULTIBYTE_CHAR (pos_byte);
943 if (CHAR_HAS_CATEGORY (c, ' '))
945 column++;
946 INC_POS (pos_byte);
947 p = BYTE_POS_ADDR (pos_byte);
949 else
950 return column;
956 /* Test whether the line beginning at POS is indented beyond COLUMN.
957 Blank lines are treated as if they had the same indentation as the
958 preceding line. */
960 bool
961 indented_beyond_p (ptrdiff_t pos, ptrdiff_t pos_byte, EMACS_INT column)
963 while (pos > BEGV && FETCH_BYTE (pos_byte) == '\n')
965 DEC_BOTH (pos, pos_byte);
966 pos = find_newline (pos, pos_byte, BEGV, BEGV_BYTE,
967 -1, NULL, &pos_byte, 0);
969 return position_indentation (pos_byte) >= column;
972 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2,
973 "NMove to column: ",
974 doc: /* Move point to column COLUMN in the current line.
975 Interactively, COLUMN is the value of prefix numeric argument.
976 The column of a character is calculated by adding together the widths
977 as displayed of the previous characters in the line.
978 This function ignores line-continuation;
979 there is no upper limit on the column number a character can have
980 and horizontal scrolling has no effect.
982 If specified column is within a character, point goes after that character.
983 If it's past end of line, point goes to end of line.
985 Optional second argument FORCE non-nil means if COLUMN is in the
986 middle of a tab character, change it to spaces.
987 In addition, if FORCE is t, and the line is too short to reach
988 COLUMN, add spaces/tabs to get there.
990 The return value is the current column. */)
991 (Lisp_Object column, Lisp_Object force)
993 ptrdiff_t pos, prev_col;
994 EMACS_INT col;
995 EMACS_INT goal;
997 CHECK_NATNUM (column);
998 goal = XINT (column);
1000 col = goal;
1001 pos = ZV;
1002 scan_for_column (&pos, &col, &prev_col);
1004 SET_PT (pos);
1006 /* If a tab char made us overshoot, change it to spaces
1007 and scan through it again. */
1008 if (!NILP (force) && col > goal)
1010 int c;
1011 ptrdiff_t pos_byte = PT_BYTE;
1013 DEC_POS (pos_byte);
1014 c = FETCH_CHAR (pos_byte);
1015 if (c == '\t' && prev_col < goal)
1017 ptrdiff_t goal_pt, goal_pt_byte;
1019 /* Insert spaces in front of the tab to reach GOAL. Do this
1020 first so that a marker at the end of the tab gets
1021 adjusted. */
1022 SET_PT_BOTH (PT - 1, PT_BYTE - 1);
1023 Finsert_char (make_number (' '), make_number (goal - prev_col), Qt);
1025 /* Now delete the tab, and indent to COL. */
1026 del_range (PT, PT + 1);
1027 goal_pt = PT;
1028 goal_pt_byte = PT_BYTE;
1029 Findent_to (make_number (col), Qnil);
1030 SET_PT_BOTH (goal_pt, goal_pt_byte);
1032 /* Set the last_known... vars consistently. */
1033 col = goal;
1037 /* If line ends prematurely, add space to the end. */
1038 if (col < goal && EQ (force, Qt))
1039 Findent_to (make_number (col = goal), Qnil);
1041 last_known_column = col;
1042 last_known_column_point = PT;
1043 last_known_column_modified = MODIFF;
1045 return make_number (col);
1048 /* compute_motion: compute buffer posn given screen posn and vice versa */
1050 static struct position val_compute_motion;
1052 /* Scan the current buffer forward from offset FROM, pretending that
1053 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1054 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1055 and return the ending buffer position and screen location. If we
1056 can't hit the requested column exactly (because of a tab or other
1057 multi-column character), overshoot.
1059 DID_MOTION is true if FROMHPOS has already accounted for overlay strings
1060 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1061 earlier call to compute_motion. The other common case is that FROMHPOS
1062 is zero and FROM is a position that "belongs" at column zero, but might
1063 be shifted by overlay strings; in this case DID_MOTION should be false.
1065 WIDTH is the number of columns available to display text;
1066 compute_motion uses this to handle continuation lines and such.
1067 If WIDTH is -1, use width of window's text area adjusted for
1068 continuation glyph when needed.
1070 HSCROLL is the number of columns not being displayed at the left
1071 margin; this is usually taken from a window's hscroll member.
1072 TAB_OFFSET is the number of columns of the first tab that aren't
1073 being displayed, perhaps because of a continuation line or
1074 something.
1076 compute_motion returns a pointer to a struct position. The bufpos
1077 member gives the buffer position at the end of the scan, and hpos
1078 and vpos give its cartesian location. prevhpos is the column at
1079 which the character before bufpos started, and contin is non-zero
1080 if we reached the current line by continuing the previous.
1082 Note that FROMHPOS and TOHPOS should be expressed in real screen
1083 columns, taking HSCROLL and the truncation glyph at the left margin
1084 into account. That is, beginning-of-line moves you to the hpos
1085 -HSCROLL + (HSCROLL > 0).
1087 For example, to find the buffer position of column COL of line LINE
1088 of a certain window, pass the window's starting location as FROM
1089 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1090 Pass the buffer's ZV as TO, to limit the scan to the end of the
1091 visible section of the buffer, and pass LINE and COL as TOVPOS and
1092 TOHPOS.
1094 When displaying in window w, a typical formula for WIDTH is:
1096 window_width - 1
1097 - (has_vertical_scroll_bars
1098 ? WINDOW_CONFIG_SCROLL_BAR_COLS (window)
1099 : (window_width + window_left != frame_cols))
1101 where
1102 window_width is w->total_cols,
1103 window_left is w->left_col,
1104 has_vertical_scroll_bars is
1105 WINDOW_HAS_VERTICAL_SCROLL_BAR (window)
1106 and frame_cols = FRAME_COLS (XFRAME (window->frame))
1108 Or you can let window_body_cols do this all for you, and write:
1109 window_body_cols (w) - 1
1111 The `-1' accounts for the continuation-line backslashes; the rest
1112 accounts for window borders if the window is split horizontally, and
1113 the scroll bars if they are turned on. */
1115 struct position *
1116 compute_motion (ptrdiff_t from, ptrdiff_t frombyte, EMACS_INT fromvpos,
1117 EMACS_INT fromhpos, bool did_motion, ptrdiff_t to,
1118 EMACS_INT tovpos, EMACS_INT tohpos, EMACS_INT width,
1119 ptrdiff_t hscroll, int tab_offset, struct window *win)
1121 EMACS_INT hpos = fromhpos;
1122 EMACS_INT vpos = fromvpos;
1124 ptrdiff_t pos;
1125 ptrdiff_t pos_byte;
1126 int c = 0;
1127 int tab_width = SANE_TAB_WIDTH (current_buffer);
1128 bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
1129 struct Lisp_Char_Table *dp = window_display_table (win);
1130 EMACS_INT selective
1131 = (INTEGERP (BVAR (current_buffer, selective_display))
1132 ? XINT (BVAR (current_buffer, selective_display))
1133 : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
1134 ptrdiff_t selective_rlen
1135 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
1136 ? ASIZE (DISP_INVIS_VECTOR (dp)) : 0);
1137 /* The next location where the `invisible' property changes, or an
1138 overlay starts or ends. */
1139 ptrdiff_t next_boundary = from;
1141 /* For computing runs of characters with similar widths.
1142 Invariant: width_run_width is zero, or all the characters
1143 from width_run_start to width_run_end have a fixed width of
1144 width_run_width. */
1145 ptrdiff_t width_run_start = from;
1146 ptrdiff_t width_run_end = from;
1147 ptrdiff_t width_run_width = 0;
1148 Lisp_Object *width_table;
1150 /* The next buffer pos where we should consult the width run cache. */
1151 ptrdiff_t next_width_run = from;
1152 Lisp_Object window;
1154 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
1155 /* If previous char scanned was a wide character,
1156 this is the column where it ended. Otherwise, this is 0. */
1157 EMACS_INT wide_column_end_hpos = 0;
1158 ptrdiff_t prev_pos; /* Previous buffer position. */
1159 ptrdiff_t prev_pos_byte; /* Previous buffer position. */
1160 EMACS_INT prev_hpos = 0;
1161 EMACS_INT prev_vpos = 0;
1162 EMACS_INT contin_hpos; /* HPOS of last column of continued line. */
1163 int prev_tab_offset; /* Previous tab offset. */
1164 int continuation_glyph_width;
1165 struct buffer *cache_buffer = current_buffer;
1166 struct region_cache *width_cache = NULL;
1168 struct composition_it cmp_it;
1170 XSETWINDOW (window, win);
1172 if (cache_buffer->base_buffer)
1173 cache_buffer = cache_buffer->base_buffer;
1174 if (dp == buffer_display_table ())
1176 width_table = (VECTORP (BVAR (current_buffer, width_table))
1177 ? XVECTOR (BVAR (current_buffer, width_table))->contents
1178 : 0);
1179 if (width_table)
1180 width_cache = width_run_cache_on_off ();
1182 else
1183 /* If the window has its own display table, we can't use the width
1184 run cache, because that's based on the buffer's display table. */
1185 width_table = 0;
1187 /* Negative width means use all available text columns. */
1188 if (width < 0)
1190 width = window_body_width (win, 0);
1191 /* We must make room for continuation marks if we don't have fringes. */
1192 #ifdef HAVE_WINDOW_SYSTEM
1193 if (!FRAME_WINDOW_P (XFRAME (win->frame)))
1194 #endif
1195 width -= 1;
1198 continuation_glyph_width = 1;
1199 #ifdef HAVE_WINDOW_SYSTEM
1200 if (FRAME_WINDOW_P (XFRAME (win->frame)))
1201 continuation_glyph_width = 0; /* In the fringe. */
1202 #endif
1204 /* It's just impossible to be too paranoid here. */
1205 eassert (from == BYTE_TO_CHAR (frombyte) && frombyte == CHAR_TO_BYTE (from));
1207 pos = prev_pos = from;
1208 pos_byte = prev_pos_byte = frombyte;
1209 contin_hpos = 0;
1210 prev_tab_offset = tab_offset;
1211 memset (&cmp_it, 0, sizeof cmp_it);
1212 cmp_it.id = -1;
1213 composition_compute_stop_pos (&cmp_it, pos, pos_byte, to, Qnil);
1215 unsigned short int quit_count = 0;
1217 while (true)
1219 rarely_quit (++quit_count);
1221 while (pos == next_boundary)
1223 ptrdiff_t pos_here = pos;
1224 ptrdiff_t newpos;
1226 /* Don't skip invisible if we are already at the margin. */
1227 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1229 if (contin_hpos && prev_hpos == 0
1230 && hpos > tohpos
1231 && (contin_hpos == width || wide_column_end_hpos > width))
1232 { /* Line breaks because we can't put the character at the
1233 previous line any more. It is not the multi-column
1234 character continued in middle. Go back to previous
1235 buffer position, screen position, and set tab offset
1236 to previous value. It's the beginning of the
1237 line. */
1238 pos = prev_pos;
1239 pos_byte = prev_pos_byte;
1240 hpos = prev_hpos;
1241 vpos = prev_vpos;
1242 tab_offset = prev_tab_offset;
1244 break;
1247 /* If the caller says that the screen position came from an earlier
1248 call to compute_motion, then we've already accounted for the
1249 overlay strings at point. This is only true the first time
1250 through, so clear the flag after testing it. */
1251 if (!did_motion)
1252 /* We need to skip past the overlay strings. Currently those
1253 strings must not contain TAB;
1254 if we want to relax that restriction, something will have
1255 to be changed here. */
1257 unsigned char *ovstr;
1258 ptrdiff_t ovlen = overlay_strings (pos, win, &ovstr);
1259 hpos += ((multibyte && ovlen > 0)
1260 ? strwidth ((char *) ovstr, ovlen) : ovlen);
1262 did_motion = 0;
1264 if (pos >= to)
1265 break;
1267 /* Advance POS past invisible characters
1268 (but not necessarily all that there are here),
1269 and store in next_boundary the next position where
1270 we need to call skip_invisible. */
1271 newpos = skip_invisible (pos, &next_boundary, to, window);
1273 if (newpos >= to)
1275 pos = min (to, newpos);
1276 pos_byte = CHAR_TO_BYTE (pos);
1277 goto after_loop;
1280 if (newpos != pos_here)
1282 pos = newpos;
1283 pos_byte = CHAR_TO_BYTE (pos);
1286 rarely_quit (++quit_count);
1289 /* Handle right margin. */
1290 /* Note on a wide-column character.
1292 Characters are classified into the following three categories
1293 according to the width (columns occupied on screen).
1295 (1) single-column character: ex. `a'
1296 (2) multi-column character: ex. `^A', TAB, `\033'
1297 (3) wide-column character: ex. Japanese character, Chinese character
1298 (In the following example, `W_' stands for them.)
1300 Multi-column characters can be divided around the right margin,
1301 but wide-column characters cannot.
1303 NOTE:
1305 (*) The cursor is placed on the next character after the point.
1307 ----------
1308 abcdefghi\
1309 j ^---- next after the point
1310 ^--- next char. after the point.
1311 ----------
1312 In case of sigle-column character
1314 ----------
1315 abcdefgh\\
1316 033 ^---- next after the point, next char. after the point.
1317 ----------
1318 In case of multi-column character
1320 ----------
1321 abcdefgh\\
1322 W_ ^---- next after the point
1323 ^---- next char. after the point.
1324 ----------
1325 In case of wide-column character
1327 The problem here is continuation at a wide-column character.
1328 In this case, the line may shorter less than WIDTH.
1329 And we find the continuation AFTER it occurs.
1333 if (hpos > width)
1335 EMACS_INT total_width = width + continuation_glyph_width;
1336 bool truncate = 0;
1338 if (!NILP (Vtruncate_partial_width_windows)
1339 && (total_width < FRAME_COLS (XFRAME (WINDOW_FRAME (win)))))
1341 if (INTEGERP (Vtruncate_partial_width_windows))
1342 truncate
1343 = total_width < XFASTINT (Vtruncate_partial_width_windows);
1344 else
1345 truncate = 1;
1348 if (hscroll || truncate
1349 || !NILP (BVAR (current_buffer, truncate_lines)))
1351 /* Truncating: skip to newline, unless we are already past
1352 TO (we need to go back below). */
1353 if (pos <= to)
1355 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1356 hpos = width;
1357 /* If we just skipped next_boundary,
1358 loop around in the main while
1359 and handle it. */
1360 if (pos >= next_boundary)
1361 next_boundary = pos + 1;
1362 prev_hpos = width;
1363 prev_vpos = vpos;
1364 prev_tab_offset = tab_offset;
1367 else
1369 /* Continuing. */
1370 /* Remember the previous value. */
1371 prev_tab_offset = tab_offset;
1373 if (wide_column_end_hpos > width)
1375 hpos -= prev_hpos;
1376 tab_offset += prev_hpos;
1378 else
1380 tab_offset += width;
1381 hpos -= width;
1383 vpos++;
1384 contin_hpos = prev_hpos;
1385 prev_hpos = 0;
1386 prev_vpos = vpos;
1390 /* Stop if past the target buffer position or screen position. */
1391 if (pos > to)
1393 /* Go back to the previous position. */
1394 pos = prev_pos;
1395 pos_byte = prev_pos_byte;
1396 hpos = prev_hpos;
1397 vpos = prev_vpos;
1398 tab_offset = prev_tab_offset;
1400 /* NOTE on contin_hpos, hpos, and prev_hpos.
1402 ----------
1403 abcdefgh\\
1404 W_ ^---- contin_hpos
1405 | ^----- hpos
1406 \---- prev_hpos
1407 ----------
1410 if (contin_hpos && prev_hpos == 0
1411 && contin_hpos < width && !wide_column_end_hpos)
1413 /* Line breaking occurs in the middle of multi-column
1414 character. Go back to previous line. */
1415 hpos = contin_hpos;
1416 vpos = vpos - 1;
1418 break;
1421 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1423 if (contin_hpos && prev_hpos == 0
1424 && hpos > tohpos
1425 && (contin_hpos == width || wide_column_end_hpos > width))
1426 { /* Line breaks because we can't put the character at the
1427 previous line any more. It is not the multi-column
1428 character continued in middle. Go back to previous
1429 buffer position, screen position, and set tab offset
1430 to previous value. It's the beginning of the
1431 line. */
1432 pos = prev_pos;
1433 pos_byte = prev_pos_byte;
1434 hpos = prev_hpos;
1435 vpos = prev_vpos;
1436 tab_offset = prev_tab_offset;
1438 break;
1440 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1441 break;
1443 prev_hpos = hpos;
1444 prev_vpos = vpos;
1445 prev_pos = pos;
1446 prev_pos_byte = pos_byte;
1447 wide_column_end_hpos = 0;
1449 /* Consult the width run cache to see if we can avoid inspecting
1450 the text character-by-character. */
1451 if (width_cache && pos >= next_width_run)
1453 ptrdiff_t run_end;
1454 int common_width
1455 = region_cache_forward (cache_buffer, width_cache, pos, &run_end);
1457 /* A width of zero means the character's width varies (like
1458 a tab), is meaningless (like a newline), or we just don't
1459 want to skip over it for some other reason. */
1460 if (common_width != 0)
1462 ptrdiff_t run_end_hpos;
1464 /* Don't go past the final buffer posn the user
1465 requested. */
1466 if (run_end > to)
1467 run_end = to;
1469 run_end_hpos = hpos + (run_end - pos) * common_width;
1471 /* Don't go past the final horizontal position the user
1472 requested. */
1473 if (vpos == tovpos && run_end_hpos > tohpos)
1475 run_end = pos + (tohpos - hpos) / common_width;
1476 run_end_hpos = hpos + (run_end - pos) * common_width;
1479 /* Don't go past the margin. */
1480 if (run_end_hpos >= width)
1482 run_end = pos + (width - hpos) / common_width;
1483 run_end_hpos = hpos + (run_end - pos) * common_width;
1486 hpos = run_end_hpos;
1487 if (run_end > pos)
1488 prev_hpos = hpos - common_width;
1489 if (pos != run_end)
1491 pos = run_end;
1492 pos_byte = CHAR_TO_BYTE (pos);
1496 next_width_run = run_end + 1;
1499 /* We have to scan the text character-by-character. */
1500 else
1502 ptrdiff_t i, n;
1503 Lisp_Object charvec;
1505 /* Check composition sequence. */
1506 if (cmp_it.id >= 0
1507 || (pos == cmp_it.stop_pos
1508 && composition_reseat_it (&cmp_it, pos, pos_byte, to, win,
1509 NULL, Qnil)))
1510 composition_update_it (&cmp_it, pos, pos_byte, Qnil);
1511 if (cmp_it.id >= 0)
1513 pos += cmp_it.nchars;
1514 pos_byte += cmp_it.nbytes;
1515 hpos += cmp_it.width;
1516 if (cmp_it.to == cmp_it.nglyphs)
1518 cmp_it.id = -1;
1519 composition_compute_stop_pos (&cmp_it, pos, pos_byte, to,
1520 Qnil);
1522 else
1523 cmp_it.from = cmp_it.to;
1524 continue;
1527 c = FETCH_BYTE (pos_byte);
1528 pos++, pos_byte++;
1530 /* Perhaps add some info to the width_run_cache. */
1531 if (width_cache)
1533 /* Is this character part of the current run? If so, extend
1534 the run. */
1535 if (pos - 1 == width_run_end
1536 && XFASTINT (width_table[c]) == width_run_width)
1537 width_run_end = pos;
1539 /* The previous run is over, since this is a character at a
1540 different position, or a different width. */
1541 else
1543 /* Have we accumulated a run to put in the cache?
1544 (Currently, we only cache runs of width == 1). */
1545 if (width_run_start < width_run_end
1546 && width_run_width == 1)
1547 know_region_cache (cache_buffer, width_cache,
1548 width_run_start, width_run_end);
1550 /* Start recording a new width run. */
1551 width_run_width = XFASTINT (width_table[c]);
1552 width_run_start = pos - 1;
1553 width_run_end = pos;
1557 if (dp != 0
1558 && ! (multibyte && LEADING_CODE_P (c))
1559 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1561 charvec = DISP_CHAR_VECTOR (dp, c);
1562 n = ASIZE (charvec);
1564 else
1566 charvec = Qnil;
1567 n = 1;
1570 for (i = 0; i < n; ++i)
1572 if (VECTORP (charvec))
1574 /* This should be handled the same as
1575 next_element_from_display_vector does it. */
1576 Lisp_Object entry = AREF (charvec, i);
1578 if (GLYPH_CODE_P (entry))
1579 c = GLYPH_CODE_CHAR (entry);
1580 else
1581 c = ' ';
1584 if (c >= 040 && c < 0177)
1585 hpos++;
1586 else if (c == '\t')
1588 int tem = ((hpos + tab_offset + hscroll - (hscroll > 0))
1589 % tab_width);
1590 if (tem < 0)
1591 tem += tab_width;
1592 hpos += tab_width - tem;
1594 else if (c == '\n')
1596 if (selective > 0
1597 && indented_beyond_p (pos, pos_byte, selective))
1599 /* If (pos == to), we don't have to take care of
1600 selective display. */
1601 if (pos < to)
1603 /* Skip any number of invisible lines all at once */
1606 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1607 if (pos < to)
1608 INC_BOTH (pos, pos_byte);
1609 rarely_quit (++quit_count);
1611 while (pos < to
1612 && indented_beyond_p (pos, pos_byte,
1613 selective));
1614 /* Allow for the " ..." that is displayed for them. */
1615 if (selective_rlen)
1617 hpos += selective_rlen;
1618 if (hpos >= width)
1619 hpos = width;
1621 DEC_BOTH (pos, pos_byte);
1622 /* We have skipped the invis text, but not the
1623 newline after. */
1626 else
1628 /* A visible line. */
1629 vpos++;
1630 hpos = 0;
1631 hpos -= hscroll;
1632 /* Count the truncation glyph on column 0 */
1633 if (hscroll > 0)
1634 hpos += continuation_glyph_width;
1635 tab_offset = 0;
1637 contin_hpos = 0;
1639 else if (c == CR && selective < 0)
1641 /* In selective display mode,
1642 everything from a ^M to the end of the line is invisible.
1643 Stop *before* the real newline. */
1644 if (pos < to)
1645 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1646 /* If we just skipped next_boundary,
1647 loop around in the main while
1648 and handle it. */
1649 if (pos > next_boundary)
1650 next_boundary = pos;
1651 /* Allow for the " ..." that is displayed for them. */
1652 if (selective_rlen)
1654 hpos += selective_rlen;
1655 if (hpos >= width)
1656 hpos = width;
1659 else if (multibyte && LEADING_CODE_P (c))
1661 /* Start of multi-byte form. */
1662 unsigned char *ptr;
1663 int mb_bytes, mb_width;
1665 pos_byte--; /* rewind POS_BYTE */
1666 ptr = BYTE_POS_ADDR (pos_byte);
1667 MULTIBYTE_BYTES_WIDTH (ptr, dp, mb_bytes, mb_width);
1668 pos_byte += mb_bytes;
1669 if (mb_width > 1 && BYTES_BY_CHAR_HEAD (*ptr) == mb_bytes)
1670 wide_column_end_hpos = hpos + mb_width;
1671 hpos += mb_width;
1673 else if (VECTORP (charvec))
1674 ++hpos;
1675 else
1676 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1681 after_loop:
1683 /* Remember any final width run in the cache. */
1684 if (width_cache
1685 && width_run_width == 1
1686 && width_run_start < width_run_end)
1687 know_region_cache (cache_buffer, width_cache,
1688 width_run_start, width_run_end);
1690 val_compute_motion.bufpos = pos;
1691 val_compute_motion.bytepos = pos_byte;
1692 val_compute_motion.hpos = hpos;
1693 val_compute_motion.vpos = vpos;
1694 if (contin_hpos && prev_hpos == 0)
1695 val_compute_motion.prevhpos = contin_hpos;
1696 else
1697 val_compute_motion.prevhpos = prev_hpos;
1699 /* Nonzero if have just continued a line */
1700 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1702 return &val_compute_motion;
1706 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1707 doc: /* Scan through the current buffer, calculating screen position.
1708 Scan the current buffer forward from offset FROM,
1709 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1710 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1711 and return the ending buffer position and screen location.
1713 If TOPOS is nil, the actual width and height of the window's
1714 text area are used.
1716 There are three additional arguments:
1718 WIDTH is the number of columns available to display text;
1719 this affects handling of continuation lines. A value of nil
1720 corresponds to the actual number of available text columns.
1722 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1723 HSCROLL is the number of columns not being displayed at the left
1724 margin; this is usually taken from a window's hscroll member.
1725 TAB-OFFSET is the number of columns of the first tab that aren't
1726 being displayed, perhaps because the line was continued within it.
1727 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1729 WINDOW is the window to operate on. It is used to choose the display table;
1730 if it is showing the current buffer, it is used also for
1731 deciding which overlay properties apply.
1732 Note that `compute-motion' always operates on the current buffer.
1734 The value is a list of five elements:
1735 (POS HPOS VPOS PREVHPOS CONTIN)
1736 POS is the buffer position where the scan stopped.
1737 VPOS is the vertical position where the scan stopped.
1738 HPOS is the horizontal position where the scan stopped.
1740 PREVHPOS is the horizontal position one character back from POS.
1741 CONTIN is t if a line was continued after (or within) the previous character.
1743 For example, to find the buffer position of column COL of line LINE
1744 of a certain window, pass the window's starting location as FROM
1745 and the window's upper-left coordinates as FROMPOS.
1746 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1747 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1748 (Lisp_Object from, Lisp_Object frompos, Lisp_Object to, Lisp_Object topos,
1749 Lisp_Object width, Lisp_Object offsets, Lisp_Object window)
1751 struct window *w;
1752 Lisp_Object bufpos, hpos, vpos, prevhpos;
1753 struct position *pos;
1754 ptrdiff_t hscroll;
1755 int tab_offset;
1757 CHECK_NUMBER_COERCE_MARKER (from);
1758 CHECK_CONS (frompos);
1759 CHECK_NUMBER_CAR (frompos);
1760 CHECK_NUMBER_CDR (frompos);
1761 CHECK_NUMBER_COERCE_MARKER (to);
1762 if (!NILP (topos))
1764 CHECK_CONS (topos);
1765 CHECK_NUMBER_CAR (topos);
1766 CHECK_NUMBER_CDR (topos);
1768 if (!NILP (width))
1769 CHECK_NUMBER (width);
1771 if (!NILP (offsets))
1773 CHECK_CONS (offsets);
1774 CHECK_NUMBER_CAR (offsets);
1775 CHECK_NUMBER_CDR (offsets);
1776 if (! (0 <= XINT (XCAR (offsets)) && XINT (XCAR (offsets)) <= PTRDIFF_MAX
1777 && 0 <= XINT (XCDR (offsets)) && XINT (XCDR (offsets)) <= INT_MAX))
1778 args_out_of_range (XCAR (offsets), XCDR (offsets));
1779 hscroll = XINT (XCAR (offsets));
1780 tab_offset = XINT (XCDR (offsets));
1782 else
1783 hscroll = tab_offset = 0;
1785 w = decode_live_window (window);
1787 if (XINT (from) < BEGV || XINT (from) > ZV)
1788 args_out_of_range_3 (from, make_number (BEGV), make_number (ZV));
1789 if (XINT (to) < BEGV || XINT (to) > ZV)
1790 args_out_of_range_3 (to, make_number (BEGV), make_number (ZV));
1792 pos = compute_motion (XINT (from), CHAR_TO_BYTE (XINT (from)),
1793 XINT (XCDR (frompos)),
1794 XINT (XCAR (frompos)), 0,
1795 XINT (to),
1796 (NILP (topos)
1797 ? window_internal_height (w)
1798 : XINT (XCDR (topos))),
1799 (NILP (topos)
1800 ? (window_body_width (w, 0)
1802 #ifdef HAVE_WINDOW_SYSTEM
1803 FRAME_WINDOW_P (XFRAME (w->frame)) ? 0 :
1804 #endif
1806 : XINT (XCAR (topos))),
1807 (NILP (width) ? -1 : XINT (width)),
1808 hscroll, tab_offset, w);
1810 XSETFASTINT (bufpos, pos->bufpos);
1811 XSETINT (hpos, pos->hpos);
1812 XSETINT (vpos, pos->vpos);
1813 XSETINT (prevhpos, pos->prevhpos);
1815 return list5 (bufpos, hpos, vpos, prevhpos, pos->contin ? Qt : Qnil);
1818 /* Fvertical_motion and vmotion. */
1820 static struct position val_vmotion;
1822 struct position *
1823 vmotion (register ptrdiff_t from, register ptrdiff_t from_byte,
1824 register EMACS_INT vtarget, struct window *w)
1826 ptrdiff_t hscroll = w->hscroll;
1827 struct position pos;
1828 /* VPOS is cumulative vertical position, changed as from is changed. */
1829 register EMACS_INT vpos = 0;
1830 ptrdiff_t prevline;
1831 register ptrdiff_t first;
1832 ptrdiff_t lmargin = hscroll > 0 ? 1 - hscroll : 0;
1833 ptrdiff_t selective
1834 = (INTEGERP (BVAR (current_buffer, selective_display))
1835 ? clip_to_bounds (-1, XINT (BVAR (current_buffer, selective_display)),
1836 PTRDIFF_MAX)
1837 : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
1838 Lisp_Object window;
1839 bool did_motion;
1840 /* This is the object we use for fetching character properties. */
1841 Lisp_Object text_prop_object;
1843 XSETWINDOW (window, w);
1845 /* If the window contains this buffer, use it for getting text properties.
1846 Otherwise use the current buffer as arg for doing that. */
1847 if (EQ (w->contents, Fcurrent_buffer ()))
1848 text_prop_object = window;
1849 else
1850 text_prop_object = Fcurrent_buffer ();
1852 if (vpos >= vtarget)
1854 /* To move upward, go a line at a time until
1855 we have gone at least far enough. */
1857 first = 1;
1859 while ((vpos > vtarget || first) && from > BEGV)
1861 ptrdiff_t bytepos = from_byte;
1862 Lisp_Object propval;
1864 prevline = from;
1865 DEC_BOTH (prevline, bytepos);
1866 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1868 while (prevline > BEGV
1869 && ((selective > 0
1870 && indented_beyond_p (prevline, bytepos, selective))
1871 /* Watch out for newlines with `invisible' property.
1872 When moving upward, check the newline before. */
1873 || (propval = Fget_char_property (make_number (prevline - 1),
1874 Qinvisible,
1875 text_prop_object),
1876 TEXT_PROP_MEANS_INVISIBLE (propval))))
1878 DEC_BOTH (prevline, bytepos);
1879 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1881 pos = *compute_motion (prevline, bytepos, 0, lmargin, 0, from,
1882 /* Don't care for VPOS... */
1883 1 << (SHRT_WIDTH - 1),
1884 /* ... nor HPOS. */
1885 1 << (SHRT_WIDTH - 1),
1886 -1, hscroll, 0, w);
1887 vpos -= pos.vpos;
1888 first = 0;
1889 from = prevline;
1890 from_byte = bytepos;
1893 /* If we made exactly the desired vertical distance, or
1894 if we hit beginning of buffer, return point found. */
1895 if (vpos >= vtarget)
1897 val_vmotion.bufpos = from;
1898 val_vmotion.bytepos = from_byte;
1899 val_vmotion.vpos = vpos;
1900 val_vmotion.hpos = lmargin;
1901 val_vmotion.contin = 0;
1902 val_vmotion.prevhpos = 0;
1903 return &val_vmotion;
1906 /* Otherwise find the correct spot by moving down. */
1909 /* Moving downward is simple, but must calculate from
1910 beg of line to determine hpos of starting point. */
1912 if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n')
1914 ptrdiff_t bytepos;
1915 Lisp_Object propval;
1917 prevline = find_newline_no_quit (from, from_byte, -1, &bytepos);
1918 while (prevline > BEGV
1919 && ((selective > 0
1920 && indented_beyond_p (prevline, bytepos, selective))
1921 /* Watch out for newlines with `invisible' property.
1922 When moving downward, check the newline after. */
1923 || (propval = Fget_char_property (make_number (prevline),
1924 Qinvisible,
1925 text_prop_object),
1926 TEXT_PROP_MEANS_INVISIBLE (propval))))
1928 DEC_BOTH (prevline, bytepos);
1929 prevline = find_newline_no_quit (prevline, bytepos, -1, &bytepos);
1931 pos = *compute_motion (prevline, bytepos, 0, lmargin, 0, from,
1932 /* Don't care for VPOS... */
1933 1 << (SHRT_WIDTH - 1),
1934 /* ... nor HPOS. */
1935 1 << (SHRT_WIDTH - 1),
1936 -1, hscroll, 0, w);
1937 did_motion = 1;
1939 else
1941 pos.hpos = lmargin;
1942 pos.vpos = 0;
1943 did_motion = 0;
1945 return compute_motion (from, from_byte, vpos, pos.hpos, did_motion,
1946 ZV, vtarget, - (1 << (SHRT_WIDTH - 1)),
1947 -1, hscroll, 0, w);
1950 /* Return the width taken by line-number display in window W. */
1951 static void
1952 line_number_display_width (struct window *w, int *width, int *pixel_width)
1954 if (NILP (Vdisplay_line_numbers))
1956 *width = 0;
1957 *pixel_width = 0;
1959 else
1961 struct it it;
1962 struct text_pos startpos;
1963 bool saved_restriction = false;
1964 ptrdiff_t count = SPECPDL_INDEX ();
1965 SET_TEXT_POS_FROM_MARKER (startpos, w->start);
1966 void *itdata = bidi_shelve_cache ();
1967 /* We want to start from window's start point, but it could be
1968 outside the accessible region, in which case we widen the
1969 buffer temporarily. It could even be beyond the buffer's end
1970 (Org mode's display of source code snippets is known to cause
1971 that), in which case we just punt and start from point instead. */
1972 if (startpos.charpos > Z)
1973 SET_TEXT_POS (startpos, PT, PT_BYTE);
1974 if (startpos.charpos < BEGV || startpos.charpos > ZV)
1976 record_unwind_protect (save_restriction_restore,
1977 save_restriction_save ());
1978 Fwiden ();
1979 saved_restriction = true;
1981 start_display (&it, w, startpos);
1982 /* The call to move_it_by_lines below will not generate a line
1983 number if the first line shown in the window is hscrolled
1984 such that all of its display elements are out of view. So we
1985 pretend the hscroll doesn't exist. */
1986 it.first_visible_x = 0;
1987 move_it_by_lines (&it, 1);
1988 *width = it.lnum_width;
1989 *pixel_width = it.lnum_pixel_width;
1990 if (saved_restriction)
1991 unbind_to (count, Qnil);
1992 bidi_unshelve_cache (itdata, 0);
1996 DEFUN ("line-number-display-width", Fline_number_display_width,
1997 Sline_number_display_width, 0, 1, 0,
1998 doc: /* Return the width used for displaying line numbers in the selected window.
1999 If optional argument PIXELWISE is the symbol `columns', return the width
2000 in units of the frame's canonical character width. In this case, the
2001 value is a float.
2002 If optional argument PIXELWISE is t or any other non-nil value, return
2003 the width as an integer number of pixels.
2004 Otherwise return the value as an integer number of columns of the face
2005 used to display line numbers, `line-number'. Note that in the latter
2006 case, the value doesn't include the 2 columns used for padding the
2007 numbers on display. */)
2008 (Lisp_Object pixelwise)
2010 int width, pixel_width;
2011 struct window *w = XWINDOW (selected_window);
2012 line_number_display_width (XWINDOW (selected_window), &width, &pixel_width);
2013 if (EQ (pixelwise, Qcolumns))
2015 struct frame *f = XFRAME (w->frame);
2016 return make_float ((double) pixel_width / FRAME_COLUMN_WIDTH (f));
2018 else if (!NILP (pixelwise))
2019 return make_number (pixel_width);
2020 return make_number (width);
2023 /* In window W (derived from WINDOW), return x coordinate for column
2024 COL (derived from COLUMN). */
2025 static int
2026 window_column_x (struct window *w, Lisp_Object window,
2027 double col, Lisp_Object column)
2029 double x = col * FRAME_COLUMN_WIDTH (XFRAME (w->frame)) + 0.5;
2031 /* FIXME: Should this be limited to W's dimensions? */
2032 if (! (INT_MIN <= x && x <= INT_MAX))
2033 args_out_of_range (window, column);
2035 return x;
2038 /* Restore window's buffer and point. */
2040 static void
2041 restore_window_buffer (Lisp_Object list)
2043 struct window *w = decode_live_window (XCAR (list));
2044 list = XCDR (list);
2045 wset_buffer (w, XCAR (list));
2046 list = XCDR (list);
2047 set_marker_both (w->pointm, w->contents,
2048 XFASTINT (XCAR (list)),
2049 XFASTINT (XCAR (XCDR (list))));
2052 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 3, 0,
2053 doc: /* Move point to start of the screen line LINES lines down.
2054 If LINES is negative, this means moving up.
2056 This function is an ordinary cursor motion function
2057 which calculates the new position based on how text would be displayed.
2058 The new position may be the start of a line,
2059 or just the start of a continuation line.
2060 The function returns number of screen lines moved over;
2061 that usually equals LINES, but may be closer to zero
2062 if beginning or end of buffer was reached.
2064 The optional second argument WINDOW specifies the window to use for
2065 parameters such as width, horizontal scrolling, and so on.
2066 The default is to use the selected window's parameters.
2068 LINES can optionally take the form (COLS . LINES), in which case the
2069 motion will not stop at the start of a screen line but COLS column
2070 from the visual start of the line (if such exists on that line, that
2071 is). If the line is scrolled horizontally, COLS is interpreted
2072 visually, i.e., as addition to the columns of text beyond the left
2073 edge of the window.
2075 The optional third argument CUR-COL specifies the horizontal
2076 window-relative coordinate of point, in units of frame's canonical
2077 character width, where the function is invoked. If this argument is
2078 omitted or nil, the function will determine the point coordinate by
2079 going back to the beginning of the line.
2081 `vertical-motion' always uses the current buffer,
2082 regardless of which buffer is displayed in WINDOW.
2083 This is consistent with other cursor motion functions
2084 and makes it possible to use `vertical-motion' in any buffer,
2085 whether or not it is currently displayed in some window. */)
2086 (Lisp_Object lines, Lisp_Object window, Lisp_Object cur_col)
2088 struct it it;
2089 struct text_pos pt;
2090 struct window *w;
2091 Lisp_Object lcols;
2092 void *itdata = NULL;
2093 ptrdiff_t count = SPECPDL_INDEX ();
2095 /* Allow LINES to be of the form (HPOS . VPOS) aka (COLUMNS . LINES). */
2096 bool lcols_given = CONSP (lines);
2097 if (lcols_given)
2099 lcols = XCAR (lines);
2100 lines = XCDR (lines);
2103 CHECK_NUMBER (lines);
2104 w = decode_live_window (window);
2106 if (XBUFFER (w->contents) != current_buffer)
2108 /* Set the window's buffer temporarily to the current buffer. */
2109 Lisp_Object old = list4 (window, w->contents,
2110 make_number (marker_position (w->pointm)),
2111 make_number (marker_byte_position (w->pointm)));
2112 record_unwind_protect (restore_window_buffer, old);
2113 wset_buffer (w, Fcurrent_buffer ());
2114 set_marker_both (w->pointm, w->contents,
2115 BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
2118 if (noninteractive)
2120 struct position pos;
2121 pos = *vmotion (PT, PT_BYTE, XINT (lines), w);
2122 SET_PT_BOTH (pos.bufpos, pos.bytepos);
2123 it.vpos = pos.vpos;
2125 else
2127 ptrdiff_t it_start, it_overshoot_count = 0;
2128 int first_x;
2129 bool overshoot_handled = 0;
2130 bool disp_string_at_start_p = 0;
2131 ptrdiff_t nlines = XINT (lines);
2132 int vpos_init = 0;
2133 double start_col UNINIT;
2134 int start_x UNINIT;
2135 int to_x = -1;
2137 bool start_x_given = !NILP (cur_col);
2138 if (start_x_given)
2140 start_col = extract_float (cur_col);
2141 start_x = window_column_x (w, window, start_col, cur_col);
2144 /* When displaying line numbers, we need to prime IT's
2145 lnum_width with the value calculated at window's start, since
2146 that's what normal window redisplay does. Otherwise C-n/C-p
2147 will sometimes err by one column. */
2148 int lnum_width = 0;
2149 int lnum_pixel_width = 0;
2150 if (!NILP (Vdisplay_line_numbers)
2151 && !EQ (Vdisplay_line_numbers, Qvisual))
2152 line_number_display_width (w, &lnum_width, &lnum_pixel_width);
2153 SET_TEXT_POS (pt, PT, PT_BYTE);
2154 itdata = bidi_shelve_cache ();
2155 start_display (&it, w, pt);
2156 it.lnum_width = lnum_width;
2157 first_x = it.first_visible_x;
2158 it_start = IT_CHARPOS (it);
2160 /* See comments below for why we calculate this. */
2161 if (it.cmp_it.id >= 0)
2162 it_overshoot_count = 0;
2163 else if (it.method == GET_FROM_STRING)
2165 const char *s = SSDATA (it.string);
2166 const char *e = s + SBYTES (it.string);
2168 disp_string_at_start_p =
2169 /* If it.area is anything but TEXT_AREA, we need not bother
2170 about the display string, as it doesn't affect cursor
2171 positioning. */
2172 it.area == TEXT_AREA
2173 && it.string_from_display_prop_p
2174 /* A display string on anything but buffer text (e.g., on
2175 an overlay string) doesn't affect cursor positioning. */
2176 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER);
2177 while (s < e)
2179 if (*s++ == '\n')
2180 it_overshoot_count++;
2182 if (!it_overshoot_count)
2183 it_overshoot_count = -1;
2185 else
2186 it_overshoot_count =
2187 !(it.method == GET_FROM_IMAGE || it.method == GET_FROM_STRETCH);
2189 if (start_x_given)
2191 it.hpos = start_col;
2192 it.current_x = start_x;
2194 else
2196 /* Scan from the start of the line containing PT. If we don't
2197 do this, we start moving with IT->current_x == 0, while PT is
2198 really at some x > 0. */
2199 reseat_at_previous_visible_line_start (&it);
2200 it.current_x = it.hpos = 0;
2202 if (IT_CHARPOS (it) != PT)
2203 /* We used to temporarily disable selective display here; the
2204 comment said this is "so we don't move too far" (2005-01-19
2205 checkin by kfs). But this does nothing useful that I can
2206 tell, and it causes Bug#2694 . -- cyd */
2207 /* When the position we started from is covered by a display
2208 string, move_it_to will overshoot it, while vertical-motion
2209 wants to put the cursor _before_ the display string. So in
2210 that case, we move to buffer position before the display
2211 string, and avoid overshooting. But if the position before
2212 the display string is a newline, we don't do this, because
2213 otherwise we will end up in a screen line that is one too
2214 far back. */
2215 move_it_to (&it,
2216 (!disp_string_at_start_p
2217 || FETCH_BYTE (IT_BYTEPOS (it)) == '\n')
2218 ? PT
2219 : PT - 1,
2220 -1, -1, -1, MOVE_TO_POS);
2222 /* IT may move too far if truncate-lines is on and PT lies
2223 beyond the right margin. IT may also move too far if the
2224 starting point is on a Lisp string that has embedded
2225 newlines, or spans several screen lines. In these cases,
2226 backtrack. */
2227 if (IT_CHARPOS (it) > it_start)
2229 /* We need to backtrack also if the Lisp string contains no
2230 newlines, but there is a newline right after it. In this
2231 case, IT overshoots if there is an after-string just
2232 before the newline. */
2233 if (it_overshoot_count < 0
2234 && it.method == GET_FROM_BUFFER
2235 && it.c == '\n')
2236 it_overshoot_count = 1;
2237 else if (it_overshoot_count == 1 && it.vpos == 0
2238 && it.current_x < it.last_visible_x)
2240 /* If we came to the same screen line as the one where
2241 we started, we didn't overshoot the line, and won't
2242 need to backtrack after all. This happens, for
2243 example, when PT is in the middle of a composition. */
2244 it_overshoot_count = 0;
2246 else if (disp_string_at_start_p && it.vpos > 0)
2248 /* This is the case of a display string that spans
2249 several screen lines. In that case, we end up at the
2250 end of the string, and it.vpos tells us how many
2251 screen lines we need to backtrack. */
2252 it_overshoot_count = it.vpos;
2254 /* We might overshoot if lines are truncated and point lies
2255 beyond the right margin of the window. */
2256 if (it.line_wrap == TRUNCATE && it.current_x >= it.last_visible_x
2257 && it_overshoot_count == 0 && it.vpos > 0)
2258 it_overshoot_count = 1;
2259 if (it_overshoot_count > 0)
2260 move_it_by_lines (&it, -it_overshoot_count);
2262 overshoot_handled = 1;
2264 else if (IT_CHARPOS (it) == PT - 1
2265 && FETCH_BYTE (PT_BYTE - 1) == '\n'
2266 && nlines <= 0)
2268 /* The position we started from was covered by a display
2269 property, so we moved to position before the string, and
2270 backed up one line, because the character at PT - 1 is
2271 a newline. So we need one less line to go up (or exactly
2272 one line to go down if nlines == 0). */
2273 nlines++;
2274 /* But we still need to record that one line, in order to
2275 return the correct value to the caller. */
2276 vpos_init = -1;
2278 overshoot_handled = 1;
2280 if (lcols_given)
2281 to_x = window_column_x (w, window, extract_float (lcols), lcols);
2282 if (nlines <= 0)
2284 it.vpos = vpos_init;
2285 it.current_y = 0;
2286 /* Do this even if LINES is 0, so that we move back to the
2287 beginning of the current line as we ought. */
2288 if ((nlines < 0 && IT_CHARPOS (it) > 0)
2289 || (nlines == 0 && !(start_x_given && start_x <= to_x)))
2290 move_it_by_lines (&it, max (PTRDIFF_MIN, nlines));
2292 else if (overshoot_handled)
2294 it.vpos = vpos_init;
2295 it.current_y = 0;
2296 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines));
2298 else
2300 /* Otherwise, we are at the first row occupied by PT, which
2301 might span multiple screen lines (e.g., if it's on a
2302 multi-line display string). We want to start from the
2303 last line that it occupies. */
2304 if (it_start < ZV)
2306 while (IT_CHARPOS (it) <= it_start)
2308 it.vpos = 0;
2309 it.current_y = 0;
2310 move_it_by_lines (&it, 1);
2312 if (nlines > 1)
2313 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines - 1));
2315 else /* it_start = ZV */
2317 it.vpos = 0;
2318 it.current_y = 0;
2319 move_it_by_lines (&it, min (PTRDIFF_MAX, nlines));
2320 /* We could have some display or overlay string at ZV,
2321 in which case it.vpos will be nonzero now, while
2322 actually we didn't move vertically at all. */
2323 if (IT_CHARPOS (it) == CHARPOS (pt) && CHARPOS (pt) == it_start)
2324 it.vpos = 0;
2328 /* Move to the goal column, if one was specified. If the window
2329 was originally hscrolled, the goal column is interpreted as
2330 an addition to the hscroll amount. */
2331 if (lcols_given)
2333 /* If we are displaying line numbers, we could cross the
2334 line where the width of the line-number display changes,
2335 in which case we need to fix up the pixel coordinate
2336 accordingly. */
2337 if (lnum_pixel_width > 0)
2338 to_x += it.lnum_pixel_width - lnum_pixel_width;
2339 move_it_in_display_line (&it, ZV, first_x + to_x, MOVE_TO_X);
2340 /* If we find ourselves in the middle of an overlay string
2341 which includes a newline after current string position,
2342 we need to move by lines until we get out of the string,
2343 and then reposition point at the requested X coordinate;
2344 if we don't, the cursor will be placed just after the
2345 string, which might not be the requested column. */
2346 if (nlines > 0 && it.area == TEXT_AREA)
2348 while (it.method == GET_FROM_STRING
2349 && !it.string_from_display_prop_p
2350 && memchr (SSDATA (it.string) + IT_STRING_BYTEPOS (it),
2351 '\n',
2352 SBYTES (it.string) - IT_STRING_BYTEPOS (it)))
2354 move_it_by_lines (&it, 1);
2355 move_it_in_display_line (&it, ZV, first_x + to_x, MOVE_TO_X);
2360 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
2361 bidi_unshelve_cache (itdata, 0);
2364 unbind_to (count, Qnil);
2366 return make_number (it.vpos);
2371 /* File's initialization. */
2373 void
2374 syms_of_indent (void)
2376 DEFVAR_BOOL ("indent-tabs-mode", indent_tabs_mode,
2377 doc: /* Indentation can insert tabs if this is non-nil. */);
2378 indent_tabs_mode = 1;
2380 DEFSYM (Qcolumns, "columns");
2382 defsubr (&Scurrent_indentation);
2383 defsubr (&Sindent_to);
2384 defsubr (&Scurrent_column);
2385 defsubr (&Smove_to_column);
2386 defsubr (&Sline_number_display_width);
2387 defsubr (&Svertical_motion);
2388 defsubr (&Scompute_motion);