Avoid character to byte conversions in motion subroutines.
[emacs.git] / src / indent.c
blobfd692f0b1491deb29cfb32e985a0bedea587809d
1 /* Indentation functions.
2 Copyright (C) 1985-1988, 1993-1995, 1998, 2000-2013 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 "keyboard.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "termchar.h"
33 #include "disptab.h"
34 #include "intervals.h"
35 #include "dispextern.h"
36 #include "region-cache.h"
38 #define CR 015
40 /* These three values memorize the current column to avoid recalculation. */
42 /* Last value returned by current_column.
43 Some things in set last_known_column_point to -1
44 to mark the memorized value as invalid. */
46 static ptrdiff_t last_known_column;
48 /* Value of point when current_column was called. */
50 ptrdiff_t last_known_column_point;
52 /* Value of MODIFF when current_column was called. */
54 static EMACS_INT last_known_column_modified;
56 static ptrdiff_t current_column_1 (void);
57 static ptrdiff_t position_indentation (ptrdiff_t);
59 /* Cache of beginning of line found by the last call of
60 current_column. */
62 static ptrdiff_t current_column_bol_cache;
64 /* Get the display table to use for the current buffer. */
66 struct Lisp_Char_Table *
67 buffer_display_table (void)
69 Lisp_Object thisbuf;
71 thisbuf = BVAR (current_buffer, display_table);
72 if (DISP_TABLE_P (thisbuf))
73 return XCHAR_TABLE (thisbuf);
74 if (DISP_TABLE_P (Vstandard_display_table))
75 return XCHAR_TABLE (Vstandard_display_table);
76 return 0;
79 /* Width run cache considerations. */
81 /* Return the width of character C under display table DP. */
83 static int
84 character_width (int c, struct Lisp_Char_Table *dp)
86 Lisp_Object elt;
88 /* These width computations were determined by examining the cases
89 in display_text_line. */
91 /* Everything can be handled by the display table, if it's
92 present and the element is right. */
93 if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
94 return ASIZE (elt);
96 /* Some characters are special. */
97 if (c == '\n' || c == '\t' || c == '\015')
98 return 0;
100 /* Printing characters have width 1. */
101 else if (c >= 040 && c < 0177)
102 return 1;
104 /* Everybody else (control characters, metacharacters) has other
105 widths. We could return their actual widths here, but they
106 depend on things like ctl_arrow and crud like that, and they're
107 not very common at all. So we'll just claim we don't know their
108 widths. */
109 else
110 return 0;
113 /* Return true if the display table DISPTAB specifies the same widths
114 for characters as WIDTHTAB. We use this to decide when to
115 invalidate the buffer's width_run_cache. */
117 bool
118 disptab_matches_widthtab (struct Lisp_Char_Table *disptab, struct Lisp_Vector *widthtab)
120 int i;
122 eassert (widthtab->header.size == 256);
124 for (i = 0; i < 256; i++)
125 if (character_width (i, disptab)
126 != XFASTINT (widthtab->contents[i]))
127 return 0;
129 return 1;
132 /* Recompute BUF's width table, using the display table DISPTAB. */
134 void
135 recompute_width_table (struct buffer *buf, struct Lisp_Char_Table *disptab)
137 int i;
138 struct Lisp_Vector *widthtab;
140 if (!VECTORP (BVAR (buf, width_table)))
141 bset_width_table (buf, make_uninit_vector (256));
142 widthtab = XVECTOR (BVAR (buf, width_table));
143 eassert (widthtab->header.size == 256);
145 for (i = 0; i < 256; i++)
146 XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
149 /* Allocate or free the width run cache, as requested by the current
150 state of current_buffer's cache_long_line_scans variable. */
152 static void
153 width_run_cache_on_off (void)
155 if (NILP (BVAR (current_buffer, cache_long_line_scans))
156 /* And, for the moment, this feature doesn't work on multibyte
157 characters. */
158 || !NILP (BVAR (current_buffer, enable_multibyte_characters)))
160 /* It should be off. */
161 if (current_buffer->width_run_cache)
163 free_region_cache (current_buffer->width_run_cache);
164 current_buffer->width_run_cache = 0;
165 bset_width_table (current_buffer, Qnil);
168 else
170 /* It should be on. */
171 if (current_buffer->width_run_cache == 0)
173 current_buffer->width_run_cache = new_region_cache ();
174 recompute_width_table (current_buffer, buffer_display_table ());
180 /* Skip some invisible characters starting from POS.
181 This includes characters invisible because of text properties
182 and characters invisible because of overlays.
184 If position POS is followed by invisible characters,
185 skip some of them and return the position after them.
186 Otherwise return POS itself.
188 Set *NEXT_BOUNDARY_P to the next position at which
189 it will be necessary to call this function again.
191 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
192 to a value greater than TO.
194 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
195 take account of overlays that apply only in WINDOW.
197 We don't necessarily skip all the invisible characters after POS
198 because that could take a long time. We skip a reasonable number
199 which can be skipped quickly. If there might be more invisible
200 characters immediately following, then *NEXT_BOUNDARY_P
201 will equal the return value. */
203 ptrdiff_t
204 skip_invisible (ptrdiff_t pos, ptrdiff_t *next_boundary_p, ptrdiff_t to, Lisp_Object window)
206 Lisp_Object prop, position, overlay_limit, proplimit;
207 Lisp_Object buffer, tmp;
208 ptrdiff_t end;
209 int inv_p;
211 XSETFASTINT (position, pos);
212 XSETBUFFER (buffer, current_buffer);
214 /* Give faster response for overlay lookup near POS. */
215 recenter_overlay_lists (current_buffer, pos);
217 /* We must not advance farther than the next overlay change.
218 The overlay change might change the invisible property;
219 or there might be overlay strings to be displayed there. */
220 overlay_limit = Fnext_overlay_change (position);
221 /* As for text properties, this gives a lower bound
222 for where the invisible text property could change. */
223 proplimit = Fnext_property_change (position, buffer, Qt);
224 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
225 proplimit = overlay_limit;
226 /* PROPLIMIT is now a lower bound for the next change
227 in invisible status. If that is plenty far away,
228 use that lower bound. */
229 if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
230 *next_boundary_p = XFASTINT (proplimit);
231 /* Otherwise, scan for the next `invisible' property change. */
232 else
234 /* Don't scan terribly far. */
235 XSETFASTINT (proplimit, min (pos + 100, to));
236 /* No matter what, don't go past next overlay change. */
237 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
238 proplimit = overlay_limit;
239 tmp = Fnext_single_property_change (position, Qinvisible,
240 buffer, proplimit);
241 end = XFASTINT (tmp);
242 #if 0
243 /* Don't put the boundary in the middle of multibyte form if
244 there is no actual property change. */
245 if (end == pos + 100
246 && !NILP (current_buffer->enable_multibyte_characters)
247 && end < ZV)
248 while (pos < end && !CHAR_HEAD_P (POS_ADDR (end)))
249 end--;
250 #endif
251 *next_boundary_p = end;
253 /* if the `invisible' property is set, we can skip to
254 the next property change */
255 prop = Fget_char_property (position, Qinvisible,
256 (!NILP (window)
257 && EQ (XWINDOW (window)->buffer, buffer))
258 ? window : buffer);
259 inv_p = TEXT_PROP_MEANS_INVISIBLE (prop);
260 /* When counting columns (window == nil), don't skip over ellipsis text. */
261 if (NILP (window) ? inv_p == 1 : inv_p)
262 return *next_boundary_p;
263 return pos;
266 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
268 DP is a display table or NULL.
270 This macro is used in scan_for_column and in
271 compute_motion. */
273 #define MULTIBYTE_BYTES_WIDTH(p, dp, bytes, width) \
274 do { \
275 int ch; \
277 ch = STRING_CHAR_AND_LENGTH (p, bytes); \
278 if (BYTES_BY_CHAR_HEAD (*p) != bytes) \
279 width = bytes * 4; \
280 else \
282 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, ch))) \
283 width = sanitize_char_width (ASIZE (DISP_CHAR_VECTOR (dp, ch))); \
284 else \
285 width = CHAR_WIDTH (ch); \
287 } while (0)
290 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
291 doc: /* Return the horizontal position of point. Beginning of line is column 0.
292 This is calculated by adding together the widths of all the displayed
293 representations of the character between the start of the previous line
294 and point (e.g., control characters will have a width of 2 or 4, tabs
295 will have a variable width).
296 Ignores finite width of frame, which means that this function may return
297 values greater than (frame-width).
298 Whether the line is visible (if `selective-display' is t) has no effect;
299 however, ^M is treated as end of line when `selective-display' is t.
300 Text that has an invisible property is considered as having width 0, unless
301 `buffer-invisibility-spec' specifies that it is replaced by an ellipsis. */)
302 (void)
304 Lisp_Object temp;
305 XSETFASTINT (temp, current_column ());
306 return temp;
309 /* Cancel any recorded value of the horizontal position. */
311 void
312 invalidate_current_column (void)
314 last_known_column_point = 0;
317 ptrdiff_t
318 current_column (void)
320 ptrdiff_t col;
321 unsigned char *ptr, *stop;
322 bool tab_seen;
323 ptrdiff_t post_tab;
324 int c;
325 int tab_width = SANE_TAB_WIDTH (current_buffer);
326 bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
327 struct Lisp_Char_Table *dp = buffer_display_table ();
329 if (PT == last_known_column_point
330 && MODIFF == last_known_column_modified)
331 return last_known_column;
333 /* If the buffer has overlays, text properties,
334 or multibyte characters, use a more general algorithm. */
335 if (buffer_intervals (current_buffer)
336 || buffer_has_overlays ()
337 || Z != Z_BYTE)
338 return current_column_1 ();
340 /* Scan backwards from point to the previous newline,
341 counting width. Tab characters are the only complicated case. */
343 /* Make a pointer for decrementing through the chars before point. */
344 ptr = BYTE_POS_ADDR (PT_BYTE - 1) + 1;
345 /* Make a pointer to where consecutive chars leave off,
346 going backwards from point. */
347 if (PT == BEGV)
348 stop = ptr;
349 else if (PT <= GPT || BEGV > GPT)
350 stop = BEGV_ADDR;
351 else
352 stop = GAP_END_ADDR;
354 col = 0, tab_seen = 0, post_tab = 0;
356 while (1)
358 ptrdiff_t i, n;
359 Lisp_Object charvec;
361 if (ptr == stop)
363 /* We stopped either for the beginning of the buffer
364 or for the gap. */
365 if (ptr == BEGV_ADDR)
366 break;
368 /* It was the gap. Jump back over it. */
369 stop = BEGV_ADDR;
370 ptr = GPT_ADDR;
372 /* Check whether that brings us to beginning of buffer. */
373 if (BEGV >= GPT)
374 break;
377 c = *--ptr;
379 if (dp && VECTORP (DISP_CHAR_VECTOR (dp, c)))
381 charvec = DISP_CHAR_VECTOR (dp, c);
382 n = ASIZE (charvec);
384 else
386 charvec = Qnil;
387 n = 1;
390 for (i = n - 1; i >= 0; --i)
392 if (VECTORP (charvec))
394 /* This should be handled the same as
395 next_element_from_display_vector does it. */
396 Lisp_Object entry = AREF (charvec, i);
398 if (GLYPH_CODE_P (entry))
399 c = GLYPH_CODE_CHAR (entry);
400 else
401 c = ' ';
404 if (c >= 040 && c < 0177)
405 col++;
406 else if (c == '\n'
407 || (c == '\r'
408 && EQ (BVAR (current_buffer, selective_display), Qt)))
410 ptr++;
411 goto start_of_line_found;
413 else if (c == '\t')
415 if (tab_seen)
416 col = ((col + tab_width) / tab_width) * tab_width;
418 post_tab += col;
419 col = 0;
420 tab_seen = 1;
422 else if (VECTORP (charvec))
423 /* With a display table entry, C is displayed as is, and
424 not displayed as \NNN or as ^N. If C is a single-byte
425 character, it takes one column. If C is multi-byte in
426 an unibyte buffer, it's translated to unibyte, so it
427 also takes one column. */
428 ++col;
429 else
430 col += (ctl_arrow && c < 0200) ? 2 : 4;
434 start_of_line_found:
436 if (tab_seen)
438 col = ((col + tab_width) / tab_width) * tab_width;
439 col += post_tab;
442 if (ptr == BEGV_ADDR)
443 current_column_bol_cache = BEGV;
444 else
445 current_column_bol_cache = BYTE_TO_CHAR (PTR_BYTE_POS (ptr));
447 last_known_column = col;
448 last_known_column_point = PT;
449 last_known_column_modified = MODIFF;
451 return col;
455 /* Check the presence of a display property and compute its width.
456 If a property was found and its width was found as well, return
457 its width (>= 0) and set the position of the end of the property
458 in ENDPOS.
459 Otherwise just return -1. */
460 static int
461 check_display_width (ptrdiff_t pos, ptrdiff_t col, ptrdiff_t *endpos)
463 Lisp_Object val, overlay;
465 if (CONSP (val = get_char_property_and_overlay
466 (make_number (pos), Qdisplay, Qnil, &overlay))
467 && EQ (Qspace, XCAR (val)))
468 { /* FIXME: Use calc_pixel_width_or_height. */
469 Lisp_Object plist = XCDR (val), prop;
470 int width = -1;
471 EMACS_INT align_to_max =
472 (col < MOST_POSITIVE_FIXNUM - INT_MAX
473 ? (EMACS_INT) INT_MAX + col
474 : MOST_POSITIVE_FIXNUM);
476 if ((prop = Fplist_get (plist, QCwidth),
477 RANGED_INTEGERP (0, prop, INT_MAX)))
478 width = XINT (prop);
479 else if (FLOATP (prop) && 0 <= XFLOAT_DATA (prop)
480 && XFLOAT_DATA (prop) <= INT_MAX)
481 width = (int)(XFLOAT_DATA (prop) + 0.5);
482 else if ((prop = Fplist_get (plist, QCalign_to),
483 RANGED_INTEGERP (col, prop, align_to_max)))
484 width = XINT (prop) - col;
485 else if (FLOATP (prop) && col <= XFLOAT_DATA (prop)
486 && (XFLOAT_DATA (prop) <= align_to_max))
487 width = (int)(XFLOAT_DATA (prop) + 0.5) - col;
489 if (width >= 0)
491 ptrdiff_t start;
492 if (OVERLAYP (overlay))
493 *endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
494 else
495 get_property_and_range (pos, Qdisplay, &val, &start, endpos, Qnil);
496 return width;
499 return -1;
502 /* Scanning from the beginning of the current line, stop at the buffer
503 position ENDPOS or at the column GOALCOL or at the end of line, whichever
504 comes first.
505 Return the resulting buffer position and column in ENDPOS and GOALCOL.
506 PREVCOL gets set to the column of the previous position (it's always
507 strictly smaller than the goal column). */
508 static void
509 scan_for_column (ptrdiff_t *endpos, EMACS_INT *goalcol, ptrdiff_t *prevcol)
511 int tab_width = SANE_TAB_WIDTH (current_buffer);
512 bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
513 struct Lisp_Char_Table *dp = buffer_display_table ();
514 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
515 struct composition_it cmp_it;
516 Lisp_Object window;
517 struct window *w;
519 /* Start the scan at the beginning of this line with column number 0. */
520 register ptrdiff_t col = 0, prev_col = 0;
521 EMACS_INT goal = goalcol ? *goalcol : MOST_POSITIVE_FIXNUM;
522 ptrdiff_t end = endpos ? *endpos : PT;
523 ptrdiff_t scan, scan_byte;
524 ptrdiff_t next_boundary;
526 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
527 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
528 current_column_bol_cache = PT;
529 scan = PT, scan_byte = PT_BYTE;
530 SET_PT_BOTH (opoint, opoint_byte);
531 next_boundary = scan;
534 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
535 w = ! NILP (window) ? XWINDOW (window) : NULL;
537 memset (&cmp_it, 0, sizeof cmp_it);
538 cmp_it.id = -1;
539 composition_compute_stop_pos (&cmp_it, scan, scan_byte, end, Qnil);
541 /* Scan forward to the target position. */
542 while (scan < end)
544 int c;
546 /* Occasionally we may need to skip invisible text. */
547 while (scan == next_boundary)
549 ptrdiff_t old_scan = scan;
550 /* This updates NEXT_BOUNDARY to the next place
551 where we might need to skip more invisible text. */
552 scan = skip_invisible (scan, &next_boundary, end, Qnil);
553 if (scan != old_scan)
554 scan_byte = CHAR_TO_BYTE (scan);
555 if (scan >= end)
556 goto endloop;
559 /* Test reaching the goal column. We do this after skipping
560 invisible characters, so that we put point before the
561 character on which the cursor will appear. */
562 if (col >= goal)
563 break;
564 prev_col = col;
566 { /* Check display property. */
567 ptrdiff_t endp;
568 int width = check_display_width (scan, col, &endp);
569 if (width >= 0)
571 col += width;
572 if (endp > scan) /* Avoid infinite loops with 0-width overlays. */
574 scan = endp;
575 scan_byte = CHAR_TO_BYTE (scan);
576 continue;
581 /* Check composition sequence. */
582 if (cmp_it.id >= 0
583 || (scan == cmp_it.stop_pos
584 && composition_reseat_it (&cmp_it, scan, scan_byte, end,
585 w, NULL, Qnil)))
586 composition_update_it (&cmp_it, scan, scan_byte, Qnil);
587 if (cmp_it.id >= 0)
589 scan += cmp_it.nchars;
590 scan_byte += cmp_it.nbytes;
591 if (scan <= end)
592 col += cmp_it.width;
593 if (cmp_it.to == cmp_it.nglyphs)
595 cmp_it.id = -1;
596 composition_compute_stop_pos (&cmp_it, scan, scan_byte, end,
597 Qnil);
599 else
600 cmp_it.from = cmp_it.to;
601 continue;
604 c = FETCH_BYTE (scan_byte);
606 /* See if there is a display table and it relates
607 to this character. */
609 if (dp != 0
610 && ! (multibyte && LEADING_CODE_P (c))
611 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
613 Lisp_Object charvec;
614 ptrdiff_t i, n;
616 /* This character is displayed using a vector of glyphs.
617 Update the column/position based on those glyphs. */
619 charvec = DISP_CHAR_VECTOR (dp, c);
620 n = ASIZE (charvec);
622 for (i = 0; i < n; i++)
624 /* This should be handled the same as
625 next_element_from_display_vector does it. */
626 Lisp_Object entry = AREF (charvec, i);
628 if (GLYPH_CODE_P (entry))
629 c = GLYPH_CODE_CHAR (entry);
630 else
631 c = ' ';
633 if (c == '\n')
634 goto endloop;
635 if (c == '\r' && EQ (BVAR (current_buffer, selective_display), Qt))
636 goto endloop;
637 if (c == '\t')
639 col += tab_width;
640 col = col / tab_width * tab_width;
642 else
643 ++col;
646 else
648 /* The display table doesn't affect this character;
649 it displays as itself. */
651 if (c == '\n')
652 goto endloop;
653 if (c == '\r' && EQ (BVAR (current_buffer, selective_display), Qt))
654 goto endloop;
655 if (c == '\t')
657 col += tab_width;
658 col = col / tab_width * tab_width;
660 else if (multibyte && LEADING_CODE_P (c))
662 /* Start of multi-byte form. */
663 unsigned char *ptr;
664 int bytes, width;
666 ptr = BYTE_POS_ADDR (scan_byte);
667 MULTIBYTE_BYTES_WIDTH (ptr, dp, bytes, width);
668 /* Subtract one to compensate for the increment
669 that is going to happen below. */
670 scan_byte += bytes - 1;
671 col += width;
673 else if (ctl_arrow && (c < 040 || c == 0177))
674 col += 2;
675 else if (c < 040 || c >= 0177)
676 col += 4;
677 else
678 col++;
680 scan++;
681 scan_byte++;
684 endloop:
686 last_known_column = col;
687 last_known_column_point = PT;
688 last_known_column_modified = MODIFF;
690 if (goalcol)
691 *goalcol = col;
692 if (endpos)
693 *endpos = scan;
694 if (prevcol)
695 *prevcol = prev_col;
698 /* Return the column number of position POS
699 by scanning forward from the beginning of the line.
700 This function handles characters that are invisible
701 due to text properties or overlays. */
703 static ptrdiff_t
704 current_column_1 (void)
706 EMACS_INT col = MOST_POSITIVE_FIXNUM;
707 ptrdiff_t opoint = PT;
709 scan_for_column (&opoint, &col, NULL);
710 return col;
714 #if 0 /* Not used. */
716 /* Return the width in columns of the part of STRING from BEG to END.
717 If BEG is nil, that stands for the beginning of STRING.
718 If END is nil, that stands for the end of STRING. */
720 static double
721 string_display_width (Lisp_Object string, Lisp_Object beg, Lisp_Object end)
723 int col;
724 unsigned char *ptr, *stop;
725 bool tab_seen;
726 int post_tab;
727 int c;
728 int tab_width = SANE_TAB_WIDTH (current_buffer);
729 bool ctl_arrow = !NILP (current_buffer->ctl_arrow);
730 struct Lisp_Char_Table *dp = buffer_display_table ();
731 int b, e;
733 if (NILP (end))
734 e = SCHARS (string);
735 else
737 CHECK_NUMBER (end);
738 e = XINT (end);
741 if (NILP (beg))
742 b = 0;
743 else
745 CHECK_NUMBER (beg);
746 b = XINT (beg);
749 /* Make a pointer for decrementing through the chars before point. */
750 ptr = SDATA (string) + e;
751 /* Make a pointer to where consecutive chars leave off,
752 going backwards from point. */
753 stop = SDATA (string) + b;
755 col = 0, tab_seen = 0, post_tab = 0;
757 while (1)
759 if (ptr == stop)
760 break;
762 c = *--ptr;
763 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
764 col += ASIZE (DISP_CHAR_VECTOR (dp, c));
765 else if (c >= 040 && c < 0177)
766 col++;
767 else if (c == '\n')
768 break;
769 else if (c == '\t')
771 if (tab_seen)
772 col = ((col + tab_width) / tab_width) * tab_width;
774 post_tab += col;
775 col = 0;
776 tab_seen = 1;
778 else
779 col += (ctl_arrow && c < 0200) ? 2 : 4;
782 if (tab_seen)
784 col = ((col + tab_width) / tab_width) * tab_width;
785 col += post_tab;
788 return col;
791 #endif /* 0 */
794 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
795 doc: /* Indent from point with tabs and spaces until COLUMN is reached.
796 Optional second argument MINIMUM says always do at least MINIMUM spaces
797 even if that goes past COLUMN; by default, MINIMUM is zero.
799 The return value is COLUMN. */)
800 (Lisp_Object column, Lisp_Object minimum)
802 EMACS_INT mincol;
803 register ptrdiff_t fromcol;
804 int tab_width = SANE_TAB_WIDTH (current_buffer);
806 CHECK_NUMBER (column);
807 if (NILP (minimum))
808 XSETFASTINT (minimum, 0);
809 CHECK_NUMBER (minimum);
811 fromcol = current_column ();
812 mincol = fromcol + XINT (minimum);
813 if (mincol < XINT (column)) mincol = XINT (column);
815 if (fromcol == mincol)
816 return make_number (mincol);
818 if (indent_tabs_mode)
820 Lisp_Object n;
821 XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
822 if (XFASTINT (n) != 0)
824 Finsert_char (make_number ('\t'), n, Qt);
826 fromcol = (mincol / tab_width) * tab_width;
830 XSETFASTINT (column, mincol - fromcol);
831 Finsert_char (make_number (' '), column, Qt);
833 last_known_column = mincol;
834 last_known_column_point = PT;
835 last_known_column_modified = MODIFF;
837 XSETINT (column, mincol);
838 return column;
842 DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
843 0, 0, 0,
844 doc: /* Return the indentation of the current line.
845 This is the horizontal position of the character
846 following any initial whitespace. */)
847 (void)
849 Lisp_Object val;
850 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
852 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
854 XSETFASTINT (val, position_indentation (PT_BYTE));
855 SET_PT_BOTH (opoint, opoint_byte);
856 return val;
859 static ptrdiff_t
860 position_indentation (ptrdiff_t pos_byte)
862 register ptrdiff_t column = 0;
863 int tab_width = SANE_TAB_WIDTH (current_buffer);
864 register unsigned char *p;
865 register unsigned char *stop;
866 unsigned char *start;
867 ptrdiff_t next_boundary_byte = pos_byte;
868 ptrdiff_t ceiling = next_boundary_byte;
870 p = BYTE_POS_ADDR (pos_byte);
871 /* STOP records the value of P at which we will need
872 to think about the gap, or about invisible text,
873 or about the end of the buffer. */
874 stop = p;
875 /* START records the starting value of P. */
876 start = p;
877 while (1)
879 while (p == stop)
881 ptrdiff_t stop_pos_byte;
883 /* If we have updated P, set POS_BYTE to match.
884 The first time we enter the loop, POS_BYTE is already right. */
885 if (p != start)
886 pos_byte = PTR_BYTE_POS (p);
887 /* Consider the various reasons STOP might have been set here. */
888 if (pos_byte == ZV_BYTE)
889 return column;
890 if (pos_byte == next_boundary_byte)
892 ptrdiff_t next_boundary;
893 ptrdiff_t pos = BYTE_TO_CHAR (pos_byte);
894 pos = skip_invisible (pos, &next_boundary, ZV, Qnil);
895 pos_byte = CHAR_TO_BYTE (pos);
896 next_boundary_byte = CHAR_TO_BYTE (next_boundary);
898 if (pos_byte >= ceiling)
899 ceiling = BUFFER_CEILING_OF (pos_byte) + 1;
900 /* Compute the next place we need to stop and think,
901 and set STOP accordingly. */
902 stop_pos_byte = min (ceiling, next_boundary_byte);
903 /* The -1 and +1 arrange to point at the first byte of gap
904 (if STOP_POS_BYTE is the position of the gap)
905 rather than at the data after the gap. */
907 stop = BYTE_POS_ADDR (stop_pos_byte - 1) + 1;
908 p = BYTE_POS_ADDR (pos_byte);
910 switch (*p++)
912 case 0240:
913 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
914 return column;
915 case ' ':
916 column++;
917 break;
918 case '\t':
919 column += tab_width - column % tab_width;
920 break;
921 default:
922 if (ASCII_BYTE_P (p[-1])
923 || NILP (BVAR (current_buffer, enable_multibyte_characters)))
924 return column;
926 int c;
927 pos_byte = PTR_BYTE_POS (p - 1);
928 c = FETCH_MULTIBYTE_CHAR (pos_byte);
929 if (CHAR_HAS_CATEGORY (c, ' '))
931 column++;
932 INC_POS (pos_byte);
933 p = BYTE_POS_ADDR (pos_byte);
935 else
936 return column;
942 /* Test whether the line beginning at POS is indented beyond COLUMN.
943 Blank lines are treated as if they had the same indentation as the
944 preceding line. */
946 bool
947 indented_beyond_p (ptrdiff_t pos, ptrdiff_t pos_byte, EMACS_INT column)
949 ptrdiff_t val;
950 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
952 SET_PT_BOTH (pos, pos_byte);
953 while (PT > BEGV && FETCH_BYTE (PT_BYTE) == '\n')
954 scan_newline (PT - 1, PT_BYTE - 1, BEGV, BEGV_BYTE, -1, 0);
956 val = position_indentation (PT_BYTE);
957 SET_PT_BOTH (opoint, opoint_byte);
958 return val >= column;
961 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2,
962 "NMove to column: ",
963 doc: /* Move point to column COLUMN in the current line.
964 Interactively, COLUMN is the value of prefix numeric argument.
965 The column of a character is calculated by adding together the widths
966 as displayed of the previous characters in the line.
967 This function ignores line-continuation;
968 there is no upper limit on the column number a character can have
969 and horizontal scrolling has no effect.
971 If specified column is within a character, point goes after that character.
972 If it's past end of line, point goes to end of line.
974 Optional second argument FORCE non-nil means if COLUMN is in the
975 middle of a tab character, change it to spaces.
976 In addition, if FORCE is t, and the line is too short to reach
977 COLUMN, add spaces/tabs to get there.
979 The return value is the current column. */)
980 (Lisp_Object column, Lisp_Object force)
982 ptrdiff_t pos, prev_col;
983 EMACS_INT col;
984 EMACS_INT goal;
986 CHECK_NATNUM (column);
987 goal = XINT (column);
989 col = goal;
990 pos = ZV;
991 scan_for_column (&pos, &col, &prev_col);
993 SET_PT (pos);
995 /* If a tab char made us overshoot, change it to spaces
996 and scan through it again. */
997 if (!NILP (force) && col > goal)
999 int c;
1000 ptrdiff_t pos_byte = PT_BYTE;
1002 DEC_POS (pos_byte);
1003 c = FETCH_CHAR (pos_byte);
1004 if (c == '\t' && prev_col < goal)
1006 ptrdiff_t goal_pt, goal_pt_byte;
1008 /* Insert spaces in front of the tab to reach GOAL. Do this
1009 first so that a marker at the end of the tab gets
1010 adjusted. */
1011 SET_PT_BOTH (PT - 1, PT_BYTE - 1);
1012 Finsert_char (make_number (' '), make_number (goal - prev_col), Qt);
1014 /* Now delete the tab, and indent to COL. */
1015 del_range (PT, PT + 1);
1016 goal_pt = PT;
1017 goal_pt_byte = PT_BYTE;
1018 Findent_to (make_number (col), Qnil);
1019 SET_PT_BOTH (goal_pt, goal_pt_byte);
1021 /* Set the last_known... vars consistently. */
1022 col = goal;
1026 /* If line ends prematurely, add space to the end. */
1027 if (col < goal && EQ (force, Qt))
1028 Findent_to (make_number (col = goal), Qnil);
1030 last_known_column = col;
1031 last_known_column_point = PT;
1032 last_known_column_modified = MODIFF;
1034 return make_number (col);
1037 /* compute_motion: compute buffer posn given screen posn and vice versa */
1039 static struct position val_compute_motion;
1041 /* Scan the current buffer forward from offset FROM, pretending that
1042 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1043 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1044 and return the ending buffer position and screen location. If we
1045 can't hit the requested column exactly (because of a tab or other
1046 multi-column character), overshoot.
1048 DID_MOTION is true if FROMHPOS has already accounted for overlay strings
1049 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1050 earlier call to compute_motion. The other common case is that FROMHPOS
1051 is zero and FROM is a position that "belongs" at column zero, but might
1052 be shifted by overlay strings; in this case DID_MOTION should be false.
1054 WIDTH is the number of columns available to display text;
1055 compute_motion uses this to handle continuation lines and such.
1056 If WIDTH is -1, use width of window's text area adjusted for
1057 continuation glyph when needed.
1059 HSCROLL is the number of columns not being displayed at the left
1060 margin; this is usually taken from a window's hscroll member.
1061 TAB_OFFSET is the number of columns of the first tab that aren't
1062 being displayed, perhaps because of a continuation line or
1063 something.
1065 compute_motion returns a pointer to a struct position. The bufpos
1066 member gives the buffer position at the end of the scan, and hpos
1067 and vpos give its cartesian location. prevhpos is the column at
1068 which the character before bufpos started, and contin is non-zero
1069 if we reached the current line by continuing the previous.
1071 Note that FROMHPOS and TOHPOS should be expressed in real screen
1072 columns, taking HSCROLL and the truncation glyph at the left margin
1073 into account. That is, beginning-of-line moves you to the hpos
1074 -HSCROLL + (HSCROLL > 0).
1076 For example, to find the buffer position of column COL of line LINE
1077 of a certain window, pass the window's starting location as FROM
1078 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1079 Pass the buffer's ZV as TO, to limit the scan to the end of the
1080 visible section of the buffer, and pass LINE and COL as TOVPOS and
1081 TOHPOS.
1083 When displaying in window w, a typical formula for WIDTH is:
1085 window_width - 1
1086 - (has_vertical_scroll_bars
1087 ? WINDOW_CONFIG_SCROLL_BAR_COLS (window)
1088 : (window_width + window_left != frame_cols))
1090 where
1091 window_width is XFASTINT (w->total_cols),
1092 window_left is XFASTINT (w->left_col),
1093 has_vertical_scroll_bars is
1094 WINDOW_HAS_VERTICAL_SCROLL_BAR (window)
1095 and frame_cols = FRAME_COLS (XFRAME (window->frame))
1097 Or you can let window_body_cols do this all for you, and write:
1098 window_body_cols (w) - 1
1100 The `-1' accounts for the continuation-line backslashes; the rest
1101 accounts for window borders if the window is split horizontally, and
1102 the scroll bars if they are turned on. */
1104 struct position *
1105 compute_motion (ptrdiff_t from, ptrdiff_t frombyte, EMACS_INT fromvpos,
1106 EMACS_INT fromhpos, bool did_motion, ptrdiff_t to,
1107 EMACS_INT tovpos, EMACS_INT tohpos, EMACS_INT width,
1108 ptrdiff_t hscroll, int tab_offset, struct window *win)
1110 EMACS_INT hpos = fromhpos;
1111 EMACS_INT vpos = fromvpos;
1113 ptrdiff_t pos;
1114 ptrdiff_t pos_byte;
1115 int c = 0;
1116 int tab_width = SANE_TAB_WIDTH (current_buffer);
1117 bool ctl_arrow = !NILP (BVAR (current_buffer, ctl_arrow));
1118 struct Lisp_Char_Table *dp = window_display_table (win);
1119 EMACS_INT selective
1120 = (INTEGERP (BVAR (current_buffer, selective_display))
1121 ? XINT (BVAR (current_buffer, selective_display))
1122 : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
1123 ptrdiff_t selective_rlen
1124 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
1125 ? ASIZE (DISP_INVIS_VECTOR (dp)) : 0);
1126 /* The next location where the `invisible' property changes, or an
1127 overlay starts or ends. */
1128 ptrdiff_t next_boundary = from;
1130 /* For computing runs of characters with similar widths.
1131 Invariant: width_run_width is zero, or all the characters
1132 from width_run_start to width_run_end have a fixed width of
1133 width_run_width. */
1134 ptrdiff_t width_run_start = from;
1135 ptrdiff_t width_run_end = from;
1136 ptrdiff_t width_run_width = 0;
1137 Lisp_Object *width_table;
1139 /* The next buffer pos where we should consult the width run cache. */
1140 ptrdiff_t next_width_run = from;
1141 Lisp_Object window;
1143 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
1144 /* If previous char scanned was a wide character,
1145 this is the column where it ended. Otherwise, this is 0. */
1146 EMACS_INT wide_column_end_hpos = 0;
1147 ptrdiff_t prev_pos; /* Previous buffer position. */
1148 ptrdiff_t prev_pos_byte; /* Previous buffer position. */
1149 EMACS_INT prev_hpos = 0;
1150 EMACS_INT prev_vpos = 0;
1151 EMACS_INT contin_hpos; /* HPOS of last column of continued line. */
1152 int prev_tab_offset; /* Previous tab offset. */
1153 int continuation_glyph_width;
1155 struct composition_it cmp_it;
1157 XSETWINDOW (window, win);
1159 width_run_cache_on_off ();
1160 if (dp == buffer_display_table ())
1161 width_table = (VECTORP (BVAR (current_buffer, width_table))
1162 ? XVECTOR (BVAR (current_buffer, width_table))->contents
1163 : 0);
1164 else
1165 /* If the window has its own display table, we can't use the width
1166 run cache, because that's based on the buffer's display table. */
1167 width_table = 0;
1169 /* Negative width means use all available text columns. */
1170 if (width < 0)
1172 width = window_body_cols (win);
1173 /* We must make room for continuation marks if we don't have fringes. */
1174 #ifdef HAVE_WINDOW_SYSTEM
1175 if (!FRAME_WINDOW_P (XFRAME (win->frame)))
1176 #endif
1177 width -= 1;
1180 continuation_glyph_width = 1;
1181 #ifdef HAVE_WINDOW_SYSTEM
1182 if (FRAME_WINDOW_P (XFRAME (win->frame)))
1183 continuation_glyph_width = 0; /* In the fringe. */
1184 #endif
1186 immediate_quit = 1;
1187 QUIT;
1189 /* It's just impossible to be too paranoid here. */
1190 eassert (from == BYTE_TO_CHAR (frombyte) && frombyte == CHAR_TO_BYTE (from));
1192 pos = prev_pos = from;
1193 pos_byte = prev_pos_byte = frombyte;
1194 contin_hpos = 0;
1195 prev_tab_offset = tab_offset;
1196 memset (&cmp_it, 0, sizeof cmp_it);
1197 cmp_it.id = -1;
1198 composition_compute_stop_pos (&cmp_it, pos, pos_byte, to, Qnil);
1200 while (1)
1202 while (pos == next_boundary)
1204 ptrdiff_t pos_here = pos;
1205 ptrdiff_t newpos;
1207 /* Don't skip invisible if we are already at the margin. */
1208 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1210 if (contin_hpos && prev_hpos == 0
1211 && hpos > tohpos
1212 && (contin_hpos == width || wide_column_end_hpos > width))
1213 { /* Line breaks because we can't put the character at the
1214 previous line any more. It is not the multi-column
1215 character continued in middle. Go back to previous
1216 buffer position, screen position, and set tab offset
1217 to previous value. It's the beginning of the
1218 line. */
1219 pos = prev_pos;
1220 pos_byte = prev_pos_byte;
1221 hpos = prev_hpos;
1222 vpos = prev_vpos;
1223 tab_offset = prev_tab_offset;
1225 break;
1228 /* If the caller says that the screen position came from an earlier
1229 call to compute_motion, then we've already accounted for the
1230 overlay strings at point. This is only true the first time
1231 through, so clear the flag after testing it. */
1232 if (!did_motion)
1233 /* We need to skip past the overlay strings. Currently those
1234 strings must not contain TAB;
1235 if we want to relax that restriction, something will have
1236 to be changed here. */
1238 unsigned char *ovstr;
1239 ptrdiff_t ovlen = overlay_strings (pos, win, &ovstr);
1240 hpos += ((multibyte && ovlen > 0)
1241 ? strwidth ((char *) ovstr, ovlen) : ovlen);
1243 did_motion = 0;
1245 if (pos >= to)
1246 break;
1248 /* Advance POS past invisible characters
1249 (but not necessarily all that there are here),
1250 and store in next_boundary the next position where
1251 we need to call skip_invisible. */
1252 newpos = skip_invisible (pos, &next_boundary, to, window);
1254 if (newpos >= to)
1256 pos = min (to, newpos);
1257 pos_byte = CHAR_TO_BYTE (pos);
1258 goto after_loop;
1261 if (newpos != pos_here)
1263 pos = newpos;
1264 pos_byte = CHAR_TO_BYTE (pos);
1268 /* Handle right margin. */
1269 /* Note on a wide-column character.
1271 Characters are classified into the following three categories
1272 according to the width (columns occupied on screen).
1274 (1) single-column character: ex. `a'
1275 (2) multi-column character: ex. `^A', TAB, `\033'
1276 (3) wide-column character: ex. Japanese character, Chinese character
1277 (In the following example, `W_' stands for them.)
1279 Multi-column characters can be divided around the right margin,
1280 but wide-column characters cannot.
1282 NOTE:
1284 (*) The cursor is placed on the next character after the point.
1286 ----------
1287 abcdefghi\
1288 j ^---- next after the point
1289 ^--- next char. after the point.
1290 ----------
1291 In case of sigle-column character
1293 ----------
1294 abcdefgh\\
1295 033 ^---- next after the point, next char. after the point.
1296 ----------
1297 In case of multi-column character
1299 ----------
1300 abcdefgh\\
1301 W_ ^---- next after the point
1302 ^---- next char. after the point.
1303 ----------
1304 In case of wide-column character
1306 The problem here is continuation at a wide-column character.
1307 In this case, the line may shorter less than WIDTH.
1308 And we find the continuation AFTER it occurs.
1312 if (hpos > width)
1314 EMACS_INT total_width = width + continuation_glyph_width;
1315 bool truncate = 0;
1317 if (!NILP (Vtruncate_partial_width_windows)
1318 && (total_width < FRAME_COLS (XFRAME (WINDOW_FRAME (win)))))
1320 if (INTEGERP (Vtruncate_partial_width_windows))
1321 truncate
1322 = total_width < XFASTINT (Vtruncate_partial_width_windows);
1323 else
1324 truncate = 1;
1327 if (hscroll || truncate
1328 || !NILP (BVAR (current_buffer, truncate_lines)))
1330 /* Truncating: skip to newline, unless we are already past
1331 TO (we need to go back below). */
1332 if (pos <= to)
1334 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1335 hpos = width;
1336 /* If we just skipped next_boundary,
1337 loop around in the main while
1338 and handle it. */
1339 if (pos >= next_boundary)
1340 next_boundary = pos + 1;
1341 prev_hpos = width;
1342 prev_vpos = vpos;
1343 prev_tab_offset = tab_offset;
1346 else
1348 /* Continuing. */
1349 /* Remember the previous value. */
1350 prev_tab_offset = tab_offset;
1352 if (wide_column_end_hpos > width)
1354 hpos -= prev_hpos;
1355 tab_offset += prev_hpos;
1357 else
1359 tab_offset += width;
1360 hpos -= width;
1362 vpos++;
1363 contin_hpos = prev_hpos;
1364 prev_hpos = 0;
1365 prev_vpos = vpos;
1369 /* Stop if past the target buffer position or screen position. */
1370 if (pos > to)
1372 /* Go back to the previous position. */
1373 pos = prev_pos;
1374 pos_byte = prev_pos_byte;
1375 hpos = prev_hpos;
1376 vpos = prev_vpos;
1377 tab_offset = prev_tab_offset;
1379 /* NOTE on contin_hpos, hpos, and prev_hpos.
1381 ----------
1382 abcdefgh\\
1383 W_ ^---- contin_hpos
1384 | ^----- hpos
1385 \---- prev_hpos
1386 ----------
1389 if (contin_hpos && prev_hpos == 0
1390 && contin_hpos < width && !wide_column_end_hpos)
1392 /* Line breaking occurs in the middle of multi-column
1393 character. Go back to previous line. */
1394 hpos = contin_hpos;
1395 vpos = vpos - 1;
1397 break;
1400 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1402 if (contin_hpos && prev_hpos == 0
1403 && hpos > tohpos
1404 && (contin_hpos == width || wide_column_end_hpos > width))
1405 { /* Line breaks because we can't put the character at the
1406 previous line any more. It is not the multi-column
1407 character continued in middle. Go back to previous
1408 buffer position, screen position, and set tab offset
1409 to previous value. It's the beginning of the
1410 line. */
1411 pos = prev_pos;
1412 pos_byte = prev_pos_byte;
1413 hpos = prev_hpos;
1414 vpos = prev_vpos;
1415 tab_offset = prev_tab_offset;
1417 break;
1419 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1420 break;
1422 prev_hpos = hpos;
1423 prev_vpos = vpos;
1424 prev_pos = pos;
1425 prev_pos_byte = pos_byte;
1426 wide_column_end_hpos = 0;
1428 /* Consult the width run cache to see if we can avoid inspecting
1429 the text character-by-character. */
1430 if (current_buffer->width_run_cache && pos >= next_width_run)
1432 ptrdiff_t run_end;
1433 int common_width
1434 = region_cache_forward (current_buffer,
1435 current_buffer->width_run_cache,
1436 pos, &run_end);
1438 /* A width of zero means the character's width varies (like
1439 a tab), is meaningless (like a newline), or we just don't
1440 want to skip over it for some other reason. */
1441 if (common_width != 0)
1443 ptrdiff_t run_end_hpos;
1445 /* Don't go past the final buffer posn the user
1446 requested. */
1447 if (run_end > to)
1448 run_end = to;
1450 run_end_hpos = hpos + (run_end - pos) * common_width;
1452 /* Don't go past the final horizontal position the user
1453 requested. */
1454 if (vpos == tovpos && run_end_hpos > tohpos)
1456 run_end = pos + (tohpos - hpos) / common_width;
1457 run_end_hpos = hpos + (run_end - pos) * common_width;
1460 /* Don't go past the margin. */
1461 if (run_end_hpos >= width)
1463 run_end = pos + (width - hpos) / common_width;
1464 run_end_hpos = hpos + (run_end - pos) * common_width;
1467 hpos = run_end_hpos;
1468 if (run_end > pos)
1469 prev_hpos = hpos - common_width;
1470 if (pos != run_end)
1472 pos = run_end;
1473 pos_byte = CHAR_TO_BYTE (pos);
1477 next_width_run = run_end + 1;
1480 /* We have to scan the text character-by-character. */
1481 else
1483 ptrdiff_t i, n;
1484 Lisp_Object charvec;
1486 /* Check composition sequence. */
1487 if (cmp_it.id >= 0
1488 || (pos == cmp_it.stop_pos
1489 && composition_reseat_it (&cmp_it, pos, pos_byte, to, win,
1490 NULL, Qnil)))
1491 composition_update_it (&cmp_it, pos, pos_byte, Qnil);
1492 if (cmp_it.id >= 0)
1494 pos += cmp_it.nchars;
1495 pos_byte += cmp_it.nbytes;
1496 hpos += cmp_it.width;
1497 if (cmp_it.to == cmp_it.nglyphs)
1499 cmp_it.id = -1;
1500 composition_compute_stop_pos (&cmp_it, pos, pos_byte, to,
1501 Qnil);
1503 else
1504 cmp_it.from = cmp_it.to;
1505 continue;
1508 c = FETCH_BYTE (pos_byte);
1509 pos++, pos_byte++;
1511 /* Perhaps add some info to the width_run_cache. */
1512 if (current_buffer->width_run_cache)
1514 /* Is this character part of the current run? If so, extend
1515 the run. */
1516 if (pos - 1 == width_run_end
1517 && XFASTINT (width_table[c]) == width_run_width)
1518 width_run_end = pos;
1520 /* The previous run is over, since this is a character at a
1521 different position, or a different width. */
1522 else
1524 /* Have we accumulated a run to put in the cache?
1525 (Currently, we only cache runs of width == 1). */
1526 if (width_run_start < width_run_end
1527 && width_run_width == 1)
1528 know_region_cache (current_buffer,
1529 current_buffer->width_run_cache,
1530 width_run_start, width_run_end);
1532 /* Start recording a new width run. */
1533 width_run_width = XFASTINT (width_table[c]);
1534 width_run_start = pos - 1;
1535 width_run_end = pos;
1539 if (dp != 0
1540 && ! (multibyte && LEADING_CODE_P (c))
1541 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1543 charvec = DISP_CHAR_VECTOR (dp, c);
1544 n = ASIZE (charvec);
1546 else
1548 charvec = Qnil;
1549 n = 1;
1552 for (i = 0; i < n; ++i)
1554 if (VECTORP (charvec))
1556 /* This should be handled the same as
1557 next_element_from_display_vector does it. */
1558 Lisp_Object entry = AREF (charvec, i);
1560 if (GLYPH_CODE_P (entry))
1561 c = GLYPH_CODE_CHAR (entry);
1562 else
1563 c = ' ';
1566 if (c >= 040 && c < 0177)
1567 hpos++;
1568 else if (c == '\t')
1570 int tem = ((hpos + tab_offset + hscroll - (hscroll > 0))
1571 % tab_width);
1572 if (tem < 0)
1573 tem += tab_width;
1574 hpos += tab_width - tem;
1576 else if (c == '\n')
1578 if (selective > 0
1579 && indented_beyond_p (pos, pos_byte, selective))
1581 /* If (pos == to), we don't have to take care of
1582 selective display. */
1583 if (pos < to)
1585 /* Skip any number of invisible lines all at once */
1588 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1589 if (pos < to)
1590 INC_BOTH (pos, pos_byte);
1592 while (pos < to
1593 && indented_beyond_p (pos, pos_byte,
1594 selective));
1595 /* Allow for the " ..." that is displayed for them. */
1596 if (selective_rlen)
1598 hpos += selective_rlen;
1599 if (hpos >= width)
1600 hpos = width;
1602 DEC_BOTH (pos, pos_byte);
1603 /* We have skipped the invis text, but not the
1604 newline after. */
1607 else
1609 /* A visible line. */
1610 vpos++;
1611 hpos = 0;
1612 hpos -= hscroll;
1613 /* Count the truncation glyph on column 0 */
1614 if (hscroll > 0)
1615 hpos += continuation_glyph_width;
1616 tab_offset = 0;
1618 contin_hpos = 0;
1620 else if (c == CR && selective < 0)
1622 /* In selective display mode,
1623 everything from a ^M to the end of the line is invisible.
1624 Stop *before* the real newline. */
1625 if (pos < to)
1626 pos = find_before_next_newline (pos, to, 1, &pos_byte);
1627 /* If we just skipped next_boundary,
1628 loop around in the main while
1629 and handle it. */
1630 if (pos > next_boundary)
1631 next_boundary = pos;
1632 /* Allow for the " ..." that is displayed for them. */
1633 if (selective_rlen)
1635 hpos += selective_rlen;
1636 if (hpos >= width)
1637 hpos = width;
1640 else if (multibyte && LEADING_CODE_P (c))
1642 /* Start of multi-byte form. */
1643 unsigned char *ptr;
1644 int mb_bytes, mb_width;
1646 pos_byte--; /* rewind POS_BYTE */
1647 ptr = BYTE_POS_ADDR (pos_byte);
1648 MULTIBYTE_BYTES_WIDTH (ptr, dp, mb_bytes, mb_width);
1649 pos_byte += mb_bytes;
1650 if (mb_width > 1 && BYTES_BY_CHAR_HEAD (*ptr) == mb_bytes)
1651 wide_column_end_hpos = hpos + mb_width;
1652 hpos += mb_width;
1654 else if (VECTORP (charvec))
1655 ++hpos;
1656 else
1657 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1662 after_loop:
1664 /* Remember any final width run in the cache. */
1665 if (current_buffer->width_run_cache
1666 && width_run_width == 1
1667 && width_run_start < width_run_end)
1668 know_region_cache (current_buffer, current_buffer->width_run_cache,
1669 width_run_start, width_run_end);
1671 val_compute_motion.bufpos = pos;
1672 val_compute_motion.bytepos = pos_byte;
1673 val_compute_motion.hpos = hpos;
1674 val_compute_motion.vpos = vpos;
1675 if (contin_hpos && prev_hpos == 0)
1676 val_compute_motion.prevhpos = contin_hpos;
1677 else
1678 val_compute_motion.prevhpos = prev_hpos;
1680 /* Nonzero if have just continued a line */
1681 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1683 immediate_quit = 0;
1684 return &val_compute_motion;
1688 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1689 doc: /* Scan through the current buffer, calculating screen position.
1690 Scan the current buffer forward from offset FROM,
1691 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1692 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1693 and return the ending buffer position and screen location.
1695 If TOPOS is nil, the actual width and height of the window's
1696 text area are used.
1698 There are three additional arguments:
1700 WIDTH is the number of columns available to display text;
1701 this affects handling of continuation lines. A value of nil
1702 corresponds to the actual number of available text columns.
1704 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1705 HSCROLL is the number of columns not being displayed at the left
1706 margin; this is usually taken from a window's hscroll member.
1707 TAB-OFFSET is the number of columns of the first tab that aren't
1708 being displayed, perhaps because the line was continued within it.
1709 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1711 WINDOW is the window to operate on. It is used to choose the display table;
1712 if it is showing the current buffer, it is used also for
1713 deciding which overlay properties apply.
1714 Note that `compute-motion' always operates on the current buffer.
1716 The value is a list of five elements:
1717 (POS HPOS VPOS PREVHPOS CONTIN)
1718 POS is the buffer position where the scan stopped.
1719 VPOS is the vertical position where the scan stopped.
1720 HPOS is the horizontal position where the scan stopped.
1722 PREVHPOS is the horizontal position one character back from POS.
1723 CONTIN is t if a line was continued after (or within) the previous character.
1725 For example, to find the buffer position of column COL of line LINE
1726 of a certain window, pass the window's starting location as FROM
1727 and the window's upper-left coordinates as FROMPOS.
1728 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1729 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1730 (Lisp_Object from, Lisp_Object frompos, Lisp_Object to, Lisp_Object topos,
1731 Lisp_Object width, Lisp_Object offsets, Lisp_Object window)
1733 struct window *w;
1734 Lisp_Object bufpos, hpos, vpos, prevhpos;
1735 struct position *pos;
1736 ptrdiff_t hscroll;
1737 int tab_offset;
1739 CHECK_NUMBER_COERCE_MARKER (from);
1740 CHECK_CONS (frompos);
1741 CHECK_NUMBER_CAR (frompos);
1742 CHECK_NUMBER_CDR (frompos);
1743 CHECK_NUMBER_COERCE_MARKER (to);
1744 if (!NILP (topos))
1746 CHECK_CONS (topos);
1747 CHECK_NUMBER_CAR (topos);
1748 CHECK_NUMBER_CDR (topos);
1750 if (!NILP (width))
1751 CHECK_NUMBER (width);
1753 if (!NILP (offsets))
1755 CHECK_CONS (offsets);
1756 CHECK_NUMBER_CAR (offsets);
1757 CHECK_NUMBER_CDR (offsets);
1758 if (! (0 <= XINT (XCAR (offsets)) && XINT (XCAR (offsets)) <= PTRDIFF_MAX
1759 && 0 <= XINT (XCDR (offsets)) && XINT (XCDR (offsets)) <= INT_MAX))
1760 args_out_of_range (XCAR (offsets), XCDR (offsets));
1761 hscroll = XINT (XCAR (offsets));
1762 tab_offset = XINT (XCDR (offsets));
1764 else
1765 hscroll = tab_offset = 0;
1767 w = decode_live_window (window);
1769 if (XINT (from) < BEGV || XINT (from) > ZV)
1770 args_out_of_range_3 (from, make_number (BEGV), make_number (ZV));
1771 if (XINT (to) < BEGV || XINT (to) > ZV)
1772 args_out_of_range_3 (to, make_number (BEGV), make_number (ZV));
1774 pos = compute_motion (XINT (from), CHAR_TO_BYTE (XINT (from)),
1775 XINT (XCDR (frompos)),
1776 XINT (XCAR (frompos)), 0,
1777 XINT (to),
1778 (NILP (topos)
1779 ? window_internal_height (w)
1780 : XINT (XCDR (topos))),
1781 (NILP (topos)
1782 ? (window_body_cols (w)
1784 #ifdef HAVE_WINDOW_SYSTEM
1785 FRAME_WINDOW_P (XFRAME (w->frame)) ? 0 :
1786 #endif
1788 : XINT (XCAR (topos))),
1789 (NILP (width) ? -1 : XINT (width)),
1790 hscroll, tab_offset, w);
1792 XSETFASTINT (bufpos, pos->bufpos);
1793 XSETINT (hpos, pos->hpos);
1794 XSETINT (vpos, pos->vpos);
1795 XSETINT (prevhpos, pos->prevhpos);
1797 return list5 (bufpos, hpos, vpos, prevhpos, pos->contin ? Qt : Qnil);
1800 /* Fvertical_motion and vmotion. */
1802 static struct position val_vmotion;
1804 struct position *
1805 vmotion (register ptrdiff_t from, register ptrdiff_t from_byte,
1806 register EMACS_INT vtarget, struct window *w)
1808 ptrdiff_t hscroll = w->hscroll;
1809 struct position pos;
1810 /* VPOS is cumulative vertical position, changed as from is changed. */
1811 register EMACS_INT vpos = 0;
1812 ptrdiff_t prevline;
1813 register ptrdiff_t first;
1814 ptrdiff_t lmargin = hscroll > 0 ? 1 - hscroll : 0;
1815 ptrdiff_t selective
1816 = (INTEGERP (BVAR (current_buffer, selective_display))
1817 ? clip_to_bounds (-1, XINT (BVAR (current_buffer, selective_display)),
1818 PTRDIFF_MAX)
1819 : !NILP (BVAR (current_buffer, selective_display)) ? -1 : 0);
1820 Lisp_Object window;
1821 bool did_motion;
1822 /* This is the object we use for fetching character properties. */
1823 Lisp_Object text_prop_object;
1825 XSETWINDOW (window, w);
1827 /* If the window contains this buffer, use it for getting text properties.
1828 Otherwise use the current buffer as arg for doing that. */
1829 if (EQ (w->buffer, Fcurrent_buffer ()))
1830 text_prop_object = window;
1831 else
1832 text_prop_object = Fcurrent_buffer ();
1834 if (vpos >= vtarget)
1836 /* To move upward, go a line at a time until
1837 we have gone at least far enough. */
1839 first = 1;
1841 while ((vpos > vtarget || first) && from > BEGV)
1843 ptrdiff_t bytepos;
1844 Lisp_Object propval;
1846 prevline = find_newline_no_quit (from - 1, -1, &bytepos);
1847 while (prevline > BEGV
1848 && ((selective > 0
1849 && indented_beyond_p (prevline, bytepos, selective))
1850 /* Watch out for newlines with `invisible' property.
1851 When moving upward, check the newline before. */
1852 || (propval = Fget_char_property (make_number (prevline - 1),
1853 Qinvisible,
1854 text_prop_object),
1855 TEXT_PROP_MEANS_INVISIBLE (propval))))
1856 prevline = find_newline_no_quit (prevline - 1, -1, &bytepos);
1857 pos = *compute_motion (prevline, bytepos, 0, lmargin, 0, from,
1858 /* Don't care for VPOS... */
1859 1 << (BITS_PER_SHORT - 1),
1860 /* ... nor HPOS. */
1861 1 << (BITS_PER_SHORT - 1),
1862 -1, hscroll, 0, w);
1863 vpos -= pos.vpos;
1864 first = 0;
1865 from = prevline;
1866 from_byte = bytepos;
1869 /* If we made exactly the desired vertical distance, or
1870 if we hit beginning of buffer, return point found. */
1871 if (vpos >= vtarget)
1873 val_vmotion.bufpos = from;
1874 val_vmotion.bytepos = from_byte;
1875 val_vmotion.vpos = vpos;
1876 val_vmotion.hpos = lmargin;
1877 val_vmotion.contin = 0;
1878 val_vmotion.prevhpos = 0;
1879 return &val_vmotion;
1882 /* Otherwise find the correct spot by moving down. */
1885 /* Moving downward is simple, but must calculate from
1886 beg of line to determine hpos of starting point. */
1888 if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n')
1890 ptrdiff_t bytepos;
1891 Lisp_Object propval;
1893 prevline = find_newline_no_quit (from, -1, &bytepos);
1894 while (prevline > BEGV
1895 && ((selective > 0
1896 && indented_beyond_p (prevline, bytepos, selective))
1897 /* Watch out for newlines with `invisible' property.
1898 When moving downward, check the newline after. */
1899 || (propval = Fget_char_property (make_number (prevline),
1900 Qinvisible,
1901 text_prop_object),
1902 TEXT_PROP_MEANS_INVISIBLE (propval))))
1903 prevline = find_newline_no_quit (prevline - 1, -1, &bytepos);
1904 pos = *compute_motion (prevline, bytepos, 0, lmargin, 0, from,
1905 /* Don't care for VPOS... */
1906 1 << (BITS_PER_SHORT - 1),
1907 /* ... nor HPOS. */
1908 1 << (BITS_PER_SHORT - 1),
1909 -1, hscroll, 0, w);
1910 did_motion = 1;
1912 else
1914 pos.hpos = lmargin;
1915 pos.vpos = 0;
1916 did_motion = 0;
1918 return compute_motion (from, from_byte, vpos, pos.hpos, did_motion,
1919 ZV, vtarget, - (1 << (BITS_PER_SHORT - 1)),
1920 -1, hscroll, 0, w);
1923 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
1924 doc: /* Move point to start of the screen line LINES lines down.
1925 If LINES is negative, this means moving up.
1927 This function is an ordinary cursor motion function
1928 which calculates the new position based on how text would be displayed.
1929 The new position may be the start of a line,
1930 or just the start of a continuation line.
1931 The function returns number of screen lines moved over;
1932 that usually equals LINES, but may be closer to zero
1933 if beginning or end of buffer was reached.
1935 The optional second argument WINDOW specifies the window to use for
1936 parameters such as width, horizontal scrolling, and so on.
1937 The default is to use the selected window's parameters.
1939 LINES can optionally take the form (COLS . LINES), in which case
1940 the motion will not stop at the start of a screen line but on
1941 its column COLS (if such exists on that line, that is).
1943 `vertical-motion' always uses the current buffer,
1944 regardless of which buffer is displayed in WINDOW.
1945 This is consistent with other cursor motion functions
1946 and makes it possible to use `vertical-motion' in any buffer,
1947 whether or not it is currently displayed in some window. */)
1948 (Lisp_Object lines, Lisp_Object window)
1950 struct it it;
1951 struct text_pos pt;
1952 struct window *w;
1953 Lisp_Object old_buffer;
1954 EMACS_INT old_charpos IF_LINT (= 0), old_bytepos IF_LINT (= 0);
1955 struct gcpro gcpro1;
1956 Lisp_Object lcols = Qnil;
1957 double cols IF_LINT (= 0);
1958 void *itdata = NULL;
1960 /* Allow LINES to be of the form (HPOS . VPOS) aka (COLUMNS . LINES). */
1961 if (CONSP (lines) && (NUMBERP (XCAR (lines))))
1963 lcols = XCAR (lines);
1964 cols = INTEGERP (lcols) ? (double) XINT (lcols) : XFLOAT_DATA (lcols);
1965 lines = XCDR (lines);
1968 CHECK_NUMBER (lines);
1969 w = decode_live_window (window);
1971 old_buffer = Qnil;
1972 GCPRO1 (old_buffer);
1973 if (XBUFFER (w->buffer) != current_buffer)
1975 /* Set the window's buffer temporarily to the current buffer. */
1976 old_buffer = w->buffer;
1977 old_charpos = marker_position (w->pointm);
1978 old_bytepos = marker_byte_position (w->pointm);
1979 wset_buffer (w, Fcurrent_buffer ());
1980 set_marker_both (w->pointm, w->buffer,
1981 BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
1984 if (noninteractive)
1986 struct position pos;
1987 pos = *vmotion (PT, PT_BYTE, XINT (lines), w);
1988 SET_PT_BOTH (pos.bufpos, pos.bytepos);
1990 else
1992 ptrdiff_t it_start, it_overshoot_count = 0;
1993 int first_x;
1994 bool overshoot_handled = 0;
1995 bool disp_string_at_start_p = 0;
1997 itdata = bidi_shelve_cache ();
1998 SET_TEXT_POS (pt, PT, PT_BYTE);
1999 start_display (&it, w, pt);
2000 first_x = it.first_visible_x;
2001 it_start = IT_CHARPOS (it);
2003 /* See comments below for why we calculate this. */
2004 if (it.cmp_it.id >= 0)
2005 it_overshoot_count = 0;
2006 else if (it.method == GET_FROM_STRING)
2008 const char *s = SSDATA (it.string);
2009 const char *e = s + SBYTES (it.string);
2011 /* If it.area is anything but TEXT_AREA, we need not bother
2012 about the display string, as it doesn't affect cursor
2013 positioning. */
2014 disp_string_at_start_p =
2015 it.string_from_display_prop_p && it.area == TEXT_AREA;
2016 while (s < e)
2018 if (*s++ == '\n')
2019 it_overshoot_count++;
2021 if (!it_overshoot_count)
2022 it_overshoot_count = -1;
2024 else
2025 it_overshoot_count =
2026 !(it.method == GET_FROM_IMAGE || it.method == GET_FROM_STRETCH);
2028 /* Scan from the start of the line containing PT. If we don't
2029 do this, we start moving with IT->current_x == 0, while PT is
2030 really at some x > 0. */
2031 reseat_at_previous_visible_line_start (&it);
2032 it.current_x = it.hpos = 0;
2033 if (IT_CHARPOS (it) != PT)
2034 /* We used to temporarily disable selective display here; the
2035 comment said this is "so we don't move too far" (2005-01-19
2036 checkin by kfs). But this does nothing useful that I can
2037 tell, and it causes Bug#2694 . -- cyd */
2038 /* When the position we started from is covered by a display
2039 string, move_it_to will overshoot it, while vertical-motion
2040 wants to put the cursor _before_ the display string. So in
2041 that case, we move to buffer position before the display
2042 string, and avoid overshooting. */
2043 move_it_to (&it, disp_string_at_start_p ? PT - 1 : PT,
2044 -1, -1, -1, MOVE_TO_POS);
2046 /* IT may move too far if truncate-lines is on and PT lies
2047 beyond the right margin. IT may also move too far if the
2048 starting point is on a Lisp string that has embedded
2049 newlines, or spans several screen lines. In these cases,
2050 backtrack. */
2051 if (IT_CHARPOS (it) > it_start)
2053 /* We need to backtrack also if the Lisp string contains no
2054 newlines, but there is a newline right after it. In this
2055 case, IT overshoots if there is an after-string just
2056 before the newline. */
2057 if (it_overshoot_count < 0
2058 && it.method == GET_FROM_BUFFER
2059 && it.c == '\n')
2060 it_overshoot_count = 1;
2061 else if (disp_string_at_start_p && it.vpos > 0)
2063 /* This is the case of a display string that spans
2064 several screen lines. In that case, we end up at the
2065 end of the string, and it.vpos tells us how many
2066 screen lines we need to backtrack. */
2067 it_overshoot_count = it.vpos;
2069 if (it_overshoot_count > 0)
2070 move_it_by_lines (&it, -it_overshoot_count);
2072 overshoot_handled = 1;
2074 if (XINT (lines) <= 0)
2076 it.vpos = 0;
2077 /* Do this even if LINES is 0, so that we move back to the
2078 beginning of the current line as we ought. */
2079 if (XINT (lines) == 0 || IT_CHARPOS (it) > 0)
2080 move_it_by_lines (&it, max (PTRDIFF_MIN, XINT (lines)));
2082 else if (overshoot_handled)
2084 it.vpos = 0;
2085 move_it_by_lines (&it, min (PTRDIFF_MAX, XINT (lines)));
2087 else
2089 /* Otherwise, we are at the first row occupied by PT, which
2090 might span multiple screen lines (e.g., if it's on a
2091 multi-line display string). We want to start from the
2092 last line that it occupies. */
2093 if (it_start < ZV)
2095 while (IT_CHARPOS (it) <= it_start)
2097 it.vpos = 0;
2098 move_it_by_lines (&it, 1);
2100 if (XINT (lines) > 1)
2101 move_it_by_lines (&it, min (PTRDIFF_MAX, XINT (lines) - 1));
2103 else
2105 it.vpos = 0;
2106 move_it_by_lines (&it, min (PTRDIFF_MAX, XINT (lines)));
2110 /* Move to the goal column, if one was specified. */
2111 if (!NILP (lcols))
2113 /* If the window was originally hscrolled, move forward by
2114 the hscrolled amount first. */
2115 if (first_x > 0)
2117 move_it_in_display_line (&it, ZV, first_x, MOVE_TO_X);
2118 it.current_x = 0;
2120 move_it_in_display_line
2121 (&it, ZV,
2122 (int)(cols * FRAME_COLUMN_WIDTH (XFRAME (w->frame)) + 0.5),
2123 MOVE_TO_X);
2126 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
2127 bidi_unshelve_cache (itdata, 0);
2130 if (BUFFERP (old_buffer))
2132 wset_buffer (w, old_buffer);
2133 set_marker_both (w->pointm, w->buffer,
2134 old_charpos, old_bytepos);
2137 RETURN_UNGCPRO (make_number (it.vpos));
2142 /* File's initialization. */
2144 void
2145 syms_of_indent (void)
2147 DEFVAR_BOOL ("indent-tabs-mode", indent_tabs_mode,
2148 doc: /* Indentation can insert tabs if this is non-nil. */);
2149 indent_tabs_mode = 1;
2151 defsubr (&Scurrent_indentation);
2152 defsubr (&Sindent_to);
2153 defsubr (&Scurrent_column);
2154 defsubr (&Smove_to_column);
2155 defsubr (&Svertical_motion);
2156 defsubr (&Scompute_motion);