(list-islamic-diary-entries): Doc fix.
[emacs.git] / src / indent.c
blob5480cae2489510168a7eeb29e833a0dde2aeab00
1 /* Indentation functions.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1998, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include <config.h>
24 #include <stdio.h>
26 #include "lisp.h"
27 #include "buffer.h"
28 #include "character.h"
29 #include "category.h"
30 #include "indent.h"
31 #include "keyboard.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "termchar.h"
35 #include "termopts.h"
36 #include "disptab.h"
37 #include "intervals.h"
38 #include "region-cache.h"
40 /* Indentation can insert tabs if this is non-zero;
41 otherwise always uses spaces. */
43 static int indent_tabs_mode;
45 #define CR 015
47 /* These three values memorize the current column to avoid recalculation. */
49 /* Last value returned by current_column.
50 Some things in set last_known_column_point to -1
51 to mark the memorized value as invalid. */
53 static double last_known_column;
55 /* Value of point when current_column was called. */
57 EMACS_INT last_known_column_point;
59 /* Value of MODIFF when current_column was called. */
61 static int last_known_column_modified;
63 static double current_column_1 P_ ((void));
64 static double position_indentation P_ ((int));
66 /* Cache of beginning of line found by the last call of
67 current_column. */
69 static EMACS_INT current_column_bol_cache;
71 /* Get the display table to use for the current buffer. */
73 struct Lisp_Char_Table *
74 buffer_display_table ()
76 Lisp_Object thisbuf;
78 thisbuf = current_buffer->display_table;
79 if (DISP_TABLE_P (thisbuf))
80 return XCHAR_TABLE (thisbuf);
81 if (DISP_TABLE_P (Vstandard_display_table))
82 return XCHAR_TABLE (Vstandard_display_table);
83 return 0;
86 /* Width run cache considerations. */
88 /* Return the width of character C under display table DP. */
90 static int
91 character_width (c, dp)
92 int c;
93 struct Lisp_Char_Table *dp;
95 Lisp_Object elt;
97 /* These width computations were determined by examining the cases
98 in display_text_line. */
100 /* Everything can be handled by the display table, if it's
101 present and the element is right. */
102 if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
103 return XVECTOR (elt)->size;
105 /* Some characters are special. */
106 if (c == '\n' || c == '\t' || c == '\015')
107 return 0;
109 /* Printing characters have width 1. */
110 else if (c >= 040 && c < 0177)
111 return 1;
113 /* Everybody else (control characters, metacharacters) has other
114 widths. We could return their actual widths here, but they
115 depend on things like ctl_arrow and crud like that, and they're
116 not very common at all. So we'll just claim we don't know their
117 widths. */
118 else
119 return 0;
122 /* Return true if the display table DISPTAB specifies the same widths
123 for characters as WIDTHTAB. We use this to decide when to
124 invalidate the buffer's width_run_cache. */
127 disptab_matches_widthtab (disptab, widthtab)
128 struct Lisp_Char_Table *disptab;
129 struct Lisp_Vector *widthtab;
131 int i;
133 if (widthtab->size != 256)
134 abort ();
136 for (i = 0; i < 256; i++)
137 if (character_width (i, disptab)
138 != XFASTINT (widthtab->contents[i]))
139 return 0;
141 return 1;
144 /* Recompute BUF's width table, using the display table DISPTAB. */
146 void
147 recompute_width_table (buf, disptab)
148 struct buffer *buf;
149 struct Lisp_Char_Table *disptab;
151 int i;
152 struct Lisp_Vector *widthtab;
154 if (!VECTORP (buf->width_table))
155 buf->width_table = Fmake_vector (make_number (256), make_number (0));
156 widthtab = XVECTOR (buf->width_table);
157 if (widthtab->size != 256)
158 abort ();
160 for (i = 0; i < 256; i++)
161 XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
164 /* Allocate or free the width run cache, as requested by the current
165 state of current_buffer's cache_long_line_scans variable. */
167 static void
168 width_run_cache_on_off ()
170 if (NILP (current_buffer->cache_long_line_scans)
171 /* And, for the moment, this feature doesn't work on multibyte
172 characters. */
173 || !NILP (current_buffer->enable_multibyte_characters))
175 /* It should be off. */
176 if (current_buffer->width_run_cache)
178 free_region_cache (current_buffer->width_run_cache);
179 current_buffer->width_run_cache = 0;
180 current_buffer->width_table = Qnil;
183 else
185 /* It should be on. */
186 if (current_buffer->width_run_cache == 0)
188 current_buffer->width_run_cache = new_region_cache ();
189 recompute_width_table (current_buffer, buffer_display_table ());
195 /* Skip some invisible characters starting from POS.
196 This includes characters invisible because of text properties
197 and characters invisible because of overlays.
199 If position POS is followed by invisible characters,
200 skip some of them and return the position after them.
201 Otherwise return POS itself.
203 Set *NEXT_BOUNDARY_P to the next position at which
204 it will be necessary to call this function again.
206 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
207 to a value greater than TO.
209 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
210 take account of overlays that apply only in WINDOW.
212 We don't necessarily skip all the invisible characters after POS
213 because that could take a long time. We skip a reasonable number
214 which can be skipped quickly. If there might be more invisible
215 characters immediately following, then *NEXT_BOUNDARY_P
216 will equal the return value. */
218 EMACS_INT
219 skip_invisible (pos, next_boundary_p, to, window)
220 EMACS_INT pos;
221 EMACS_INT *next_boundary_p;
222 EMACS_INT to;
223 Lisp_Object window;
225 Lisp_Object prop, position, overlay_limit, proplimit;
226 Lisp_Object buffer, tmp;
227 EMACS_INT end;
228 int inv_p;
230 XSETFASTINT (position, pos);
231 XSETBUFFER (buffer, current_buffer);
233 /* Give faster response for overlay lookup near POS. */
234 recenter_overlay_lists (current_buffer, pos);
236 /* We must not advance farther than the next overlay change.
237 The overlay change might change the invisible property;
238 or there might be overlay strings to be displayed there. */
239 overlay_limit = Fnext_overlay_change (position);
240 /* As for text properties, this gives a lower bound
241 for where the invisible text property could change. */
242 proplimit = Fnext_property_change (position, buffer, Qt);
243 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
244 proplimit = overlay_limit;
245 /* PROPLIMIT is now a lower bound for the next change
246 in invisible status. If that is plenty far away,
247 use that lower bound. */
248 if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
249 *next_boundary_p = XFASTINT (proplimit);
250 /* Otherwise, scan for the next `invisible' property change. */
251 else
253 /* Don't scan terribly far. */
254 XSETFASTINT (proplimit, min (pos + 100, to));
255 /* No matter what. don't go past next overlay change. */
256 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
257 proplimit = overlay_limit;
258 tmp = Fnext_single_property_change (position, Qinvisible,
259 buffer, proplimit);
260 end = XFASTINT (tmp);
261 #if 0
262 /* Don't put the boundary in the middle of multibyte form if
263 there is no actual property change. */
264 if (end == pos + 100
265 && !NILP (current_buffer->enable_multibyte_characters)
266 && end < ZV)
267 while (pos < end && !CHAR_HEAD_P (POS_ADDR (end)))
268 end--;
269 #endif
270 *next_boundary_p = end;
272 /* if the `invisible' property is set, we can skip to
273 the next property change */
274 prop = Fget_char_property (position, Qinvisible,
275 (!NILP (window)
276 && EQ (XWINDOW (window)->buffer, buffer))
277 ? window : buffer);
278 inv_p = TEXT_PROP_MEANS_INVISIBLE (prop);
279 /* When counting columns (window == nil), don't skip over ellipsis text. */
280 if (NILP (window) ? inv_p == 1 : inv_p)
281 return *next_boundary_p;
282 return pos;
285 /* If a composition starts at POS/POS_BYTE and it doesn't stride over
286 POINT, set *LEN / *LEN_BYTE to the character and byte lengths, *WIDTH
287 to the width, and return 1. Otherwise, return 0. */
289 static int
290 check_composition (pos, pos_byte, point, len, len_byte, width)
291 int pos, pos_byte, point;
292 int *len, *len_byte, *width;
294 Lisp_Object prop;
295 EMACS_INT start, end;
296 int id;
298 if (! find_composition (pos, -1, &start, &end, &prop, Qnil)
299 || pos != start || point < end
300 || !COMPOSITION_VALID_P (start, end, prop))
301 return 0;
302 if ((id = get_composition_id (pos, pos_byte, end - pos, prop, Qnil)) < 0)
303 return 0;
305 *len = COMPOSITION_LENGTH (prop);
306 *len_byte = CHAR_TO_BYTE (end) - pos_byte;
307 *width = composition_table[id]->width;
308 return 1;
311 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
313 DP is a display table or NULL.
315 This macro is used in current_column_1, Fmove_to_column, and
316 compute_motion. */
318 #define MULTIBYTE_BYTES_WIDTH(p, dp) \
319 do { \
320 int c; \
322 wide_column = 0; \
323 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, bytes); \
324 if (BYTES_BY_CHAR_HEAD (*p) != bytes) \
325 width = bytes * 4; \
326 else \
328 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))) \
329 width = XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; \
330 else \
331 width = CHAR_WIDTH (c); \
332 if (width > 1) \
333 wide_column = width; \
335 } while (0)
338 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
339 doc: /* Return the horizontal position of point. Beginning of line is column 0.
340 This is calculated by adding together the widths of all the displayed
341 representations of the character between the start of the previous line
342 and point (eg. control characters will have a width of 2 or 4, tabs
343 will have a variable width).
344 Ignores finite width of frame, which means that this function may return
345 values greater than (frame-width).
346 Whether the line is visible (if `selective-display' is t) has no effect;
347 however, ^M is treated as end of line when `selective-display' is t.
348 Text that has an invisible property is considered as having width 0, unless
349 `buffer-invisibility-spec' specifies that it is replaced by an ellipsis. */)
352 Lisp_Object temp;
353 XSETFASTINT (temp, (int) current_column ()); /* iftc */
354 return temp;
357 /* Cancel any recorded value of the horizontal position. */
359 void
360 invalidate_current_column ()
362 last_known_column_point = 0;
365 double
366 current_column ()
368 register int col;
369 register unsigned char *ptr, *stop;
370 register int tab_seen;
371 int post_tab;
372 register int c;
373 register int tab_width = XINT (current_buffer->tab_width);
374 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
375 register struct Lisp_Char_Table *dp = buffer_display_table ();
377 if (PT == last_known_column_point
378 && MODIFF == last_known_column_modified)
379 return last_known_column;
381 /* If the buffer has overlays, text properties,
382 or multibyte characters, use a more general algorithm. */
383 if (BUF_INTERVALS (current_buffer)
384 || current_buffer->overlays_before
385 || current_buffer->overlays_after
386 || Z != Z_BYTE)
387 return current_column_1 ();
389 /* Scan backwards from point to the previous newline,
390 counting width. Tab characters are the only complicated case. */
392 /* Make a pointer for decrementing through the chars before point. */
393 ptr = BYTE_POS_ADDR (PT_BYTE - 1) + 1;
394 /* Make a pointer to where consecutive chars leave off,
395 going backwards from point. */
396 if (PT == BEGV)
397 stop = ptr;
398 else if (PT <= GPT || BEGV > GPT)
399 stop = BEGV_ADDR;
400 else
401 stop = GAP_END_ADDR;
403 if (tab_width <= 0 || tab_width > 1000)
404 tab_width = 8;
406 col = 0, tab_seen = 0, post_tab = 0;
408 while (1)
410 EMACS_INT i, n;
411 Lisp_Object charvec;
413 if (ptr == stop)
415 /* We stopped either for the beginning of the buffer
416 or for the gap. */
417 if (ptr == BEGV_ADDR)
418 break;
420 /* It was the gap. Jump back over it. */
421 stop = BEGV_ADDR;
422 ptr = GPT_ADDR;
424 /* Check whether that brings us to beginning of buffer. */
425 if (BEGV >= GPT)
426 break;
429 c = *--ptr;
431 if (dp && VECTORP (DISP_CHAR_VECTOR (dp, c)))
433 charvec = DISP_CHAR_VECTOR (dp, c);
434 n = ASIZE (charvec);
436 else
438 charvec = Qnil;
439 n = 1;
442 for (i = n - 1; i >= 0; --i)
444 if (VECTORP (charvec))
446 /* This should be handled the same as
447 next_element_from_display_vector does it. */
448 Lisp_Object entry = AREF (charvec, i);
450 if (GLYPH_CODE_P (entry)
451 && GLYPH_CODE_CHAR_VALID_P (entry))
452 c = GLYPH_CODE_CHAR (entry);
453 else
454 c = ' ';
457 if (c >= 040 && c < 0177)
458 col++;
459 else if (c == '\n'
460 || (c == '\r'
461 && EQ (current_buffer->selective_display, Qt)))
463 ptr++;
464 goto start_of_line_found;
466 else if (c == '\t')
468 if (tab_seen)
469 col = ((col + tab_width) / tab_width) * tab_width;
471 post_tab += col;
472 col = 0;
473 tab_seen = 1;
475 else if (VECTORP (charvec))
476 /* With a display table entry, C is displayed as is, and
477 not displayed as \NNN or as ^N. If C is a single-byte
478 character, it takes one column. If C is multi-byte in
479 an unibyte buffer, it's translated to unibyte, so it
480 also takes one column. */
481 ++col;
482 else
483 col += (ctl_arrow && c < 0200) ? 2 : 4;
487 start_of_line_found:
489 if (tab_seen)
491 col = ((col + tab_width) / tab_width) * tab_width;
492 col += post_tab;
495 if (ptr == BEGV_ADDR)
496 current_column_bol_cache = BEGV;
497 else
498 current_column_bol_cache = BYTE_TO_CHAR (PTR_BYTE_POS (ptr));
500 last_known_column = col;
501 last_known_column_point = PT;
502 last_known_column_modified = MODIFF;
504 return col;
507 extern Lisp_Object Qspace, QCwidth, QCalign_to;
509 /* Check the presence of a display property and compute its width.
510 If a property was found and its width was found as well, return
511 its width (>= 0) and set the position of the end of the property
512 in ENDPOS.
513 Otherwise just return -1. */
514 static int
515 check_display_width (EMACS_INT pos, EMACS_INT col, EMACS_INT *endpos)
517 Lisp_Object val, overlay;
519 if (CONSP (val = get_char_property_and_overlay
520 (make_number (pos), Qdisplay, Qnil, &overlay))
521 && EQ (Qspace, XCAR (val)))
522 { /* FIXME: Use calc_pixel_width_or_height, as in term.c. */
523 Lisp_Object plist = XCDR (val), prop;
524 int width = -1;
526 if ((prop = Fplist_get (plist, QCwidth), NATNUMP (prop)))
527 width = XINT (prop);
528 else if (FLOATP (prop))
529 width = (int)(XFLOAT_DATA (prop) + 0.5);
530 else if ((prop = Fplist_get (plist, QCalign_to), NATNUMP (prop)))
531 width = XINT (prop) - col;
532 else if (FLOATP (prop))
533 width = (int)(XFLOAT_DATA (prop) + 0.5) - col;
535 if (width >= 0)
537 EMACS_INT start;
538 if (OVERLAYP (overlay))
539 *endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
540 else
541 get_property_and_range (pos, Qdisplay, &val, &start, endpos, Qnil);
542 return width;
545 return -1;
548 /* Scanning from the beginning of the current line, stop at the buffer
549 position ENDPOS or at the column GOALCOL or at the end of line, whichever
550 comes first.
551 Return the resulting buffer position and column in ENDPOS and GOALCOL.
552 PREVCOL gets set to the column of the previous position (it's always
553 strictly smaller than the goal column). */
554 static void
555 scan_for_column (EMACS_INT *endpos, EMACS_INT *goalcol, EMACS_INT *prevcol)
557 register EMACS_INT tab_width = XINT (current_buffer->tab_width);
558 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
559 register struct Lisp_Char_Table *dp = buffer_display_table ();
560 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
562 /* Start the scan at the beginning of this line with column number 0. */
563 register EMACS_INT col = 0, prev_col = 0;
564 EMACS_INT goal = goalcol ? *goalcol : MOST_POSITIVE_FIXNUM;
565 EMACS_INT end = endpos ? *endpos : PT;
566 EMACS_INT scan, scan_byte;
567 EMACS_INT next_boundary;
569 EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
570 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
571 current_column_bol_cache = PT;
572 scan = PT, scan_byte = PT_BYTE;
573 SET_PT_BOTH (opoint, opoint_byte);
574 next_boundary = scan;
577 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
579 /* Scan forward to the target position. */
580 while (scan < end)
582 int c;
584 /* Occasionally we may need to skip invisible text. */
585 while (scan == next_boundary)
587 EMACS_INT old_scan = scan;
588 /* This updates NEXT_BOUNDARY to the next place
589 where we might need to skip more invisible text. */
590 scan = skip_invisible (scan, &next_boundary, end, Qnil);
591 if (scan != old_scan)
592 scan_byte = CHAR_TO_BYTE (scan);
593 if (scan >= end)
594 goto endloop;
597 /* Test reaching the goal column. We do this after skipping
598 invisible characters, so that we put point before the
599 character on which the cursor will appear. */
600 if (col >= goal)
601 break;
602 prev_col = col;
604 { /* Check composition sequence. */
605 int len, len_byte, width;
607 if (check_composition (scan, scan_byte, end,
608 &len, &len_byte, &width))
610 scan += len;
611 scan_byte += len_byte;
612 if (scan <= end)
613 col += width;
614 continue;
618 { /* Check display property. */
619 EMACS_INT end;
620 int width = check_display_width (scan, col, &end);
621 if (width >= 0)
623 col += width;
624 if (end > scan) /* Avoid infinite loops with 0-width overlays. */
626 scan = end; scan_byte = charpos_to_bytepos (scan);
627 continue;
632 c = FETCH_BYTE (scan_byte);
634 /* See if there is a display table and it relates
635 to this character. */
637 if (dp != 0
638 && ! (multibyte && BASE_LEADING_CODE_P (c))
639 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
641 Lisp_Object charvec;
642 EMACS_INT i, n;
644 /* This character is displayed using a vector of glyphs.
645 Update the column/position based on those glyphs. */
647 charvec = DISP_CHAR_VECTOR (dp, c);
648 n = ASIZE (charvec);
650 for (i = 0; i < n; i++)
652 /* This should be handled the same as
653 next_element_from_display_vector does it. */
654 Lisp_Object entry = AREF (charvec, i);
656 if (GLYPH_CODE_P (entry)
657 && GLYPH_CODE_CHAR_VALID_P (entry))
658 c = GLYPH_CODE_CHAR (entry);
659 else
660 c = ' ';
662 if (c == '\n')
663 goto endloop;
664 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
665 goto endloop;
666 if (c == '\t')
668 col += tab_width;
669 col = col / tab_width * tab_width;
671 else
672 ++col;
675 else
677 /* The display table doesn't affect this character;
678 it displays as itself. */
680 if (c == '\n')
681 goto endloop;
682 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
683 goto endloop;
684 if (c == '\t')
686 col += tab_width;
687 col = col / tab_width * tab_width;
689 else if (multibyte && BASE_LEADING_CODE_P (c))
691 /* Start of multi-byte form. */
692 unsigned char *ptr;
693 int bytes, width, wide_column;
695 ptr = BYTE_POS_ADDR (scan_byte);
696 MULTIBYTE_BYTES_WIDTH (ptr, dp);
697 /* Subtract one to compensate for the increment
698 that is going to happen below. */
699 scan_byte += bytes - 1;
700 col += width;
702 else if (ctl_arrow && (c < 040 || c == 0177))
703 col += 2;
704 else if (c < 040 || c >= 0177)
705 col += 4;
706 else
707 col++;
709 scan++;
710 scan_byte++;
713 endloop:
715 last_known_column = col;
716 last_known_column_point = PT;
717 last_known_column_modified = MODIFF;
719 if (goalcol)
720 *goalcol = col;
721 if (endpos)
722 *endpos = scan;
723 if (prevcol)
724 *prevcol = prev_col;
727 /* Return the column number of position POS
728 by scanning forward from the beginning of the line.
729 This function handles characters that are invisible
730 due to text properties or overlays. */
732 static double
733 current_column_1 ()
735 EMACS_INT col = MOST_POSITIVE_FIXNUM;
736 EMACS_INT opoint = PT;
738 scan_for_column (&opoint, &col, NULL);
739 return col;
743 #if 0 /* Not used. */
745 /* Return the width in columns of the part of STRING from BEG to END.
746 If BEG is nil, that stands for the beginning of STRING.
747 If END is nil, that stands for the end of STRING. */
749 static double
750 string_display_width (string, beg, end)
751 Lisp_Object string, beg, end;
753 register int col;
754 register unsigned char *ptr, *stop;
755 register int tab_seen;
756 int post_tab;
757 register int c;
758 register int tab_width = XINT (current_buffer->tab_width);
759 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
760 register struct Lisp_Char_Table *dp = buffer_display_table ();
761 int b, e;
763 if (NILP (end))
764 e = SCHARS (string);
765 else
767 CHECK_NUMBER (end);
768 e = XINT (end);
771 if (NILP (beg))
772 b = 0;
773 else
775 CHECK_NUMBER (beg);
776 b = XINT (beg);
779 /* Make a pointer for decrementing through the chars before point. */
780 ptr = SDATA (string) + e;
781 /* Make a pointer to where consecutive chars leave off,
782 going backwards from point. */
783 stop = SDATA (string) + b;
785 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
787 col = 0, tab_seen = 0, post_tab = 0;
789 while (1)
791 if (ptr == stop)
792 break;
794 c = *--ptr;
795 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
796 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
797 else if (c >= 040 && c < 0177)
798 col++;
799 else if (c == '\n')
800 break;
801 else if (c == '\t')
803 if (tab_seen)
804 col = ((col + tab_width) / tab_width) * tab_width;
806 post_tab += col;
807 col = 0;
808 tab_seen = 1;
810 else
811 col += (ctl_arrow && c < 0200) ? 2 : 4;
814 if (tab_seen)
816 col = ((col + tab_width) / tab_width) * tab_width;
817 col += post_tab;
820 return col;
823 #endif /* 0 */
826 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
827 doc: /* Indent from point with tabs and spaces until COLUMN is reached.
828 Optional second argument MINIMUM says always do at least MINIMUM spaces
829 even if that goes past COLUMN; by default, MINIMUM is zero.
831 The return value is COLUMN. */)
832 (column, minimum)
833 Lisp_Object column, minimum;
835 int mincol;
836 register int fromcol;
837 register int tab_width = XINT (current_buffer->tab_width);
839 CHECK_NUMBER (column);
840 if (NILP (minimum))
841 XSETFASTINT (minimum, 0);
842 CHECK_NUMBER (minimum);
844 fromcol = current_column ();
845 mincol = fromcol + XINT (minimum);
846 if (mincol < XINT (column)) mincol = XINT (column);
848 if (fromcol == mincol)
849 return make_number (mincol);
851 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
853 if (indent_tabs_mode)
855 Lisp_Object n;
856 XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
857 if (XFASTINT (n) != 0)
859 Finsert_char (make_number ('\t'), n, Qt);
861 fromcol = (mincol / tab_width) * tab_width;
865 XSETFASTINT (column, mincol - fromcol);
866 Finsert_char (make_number (' '), column, Qt);
868 last_known_column = mincol;
869 last_known_column_point = PT;
870 last_known_column_modified = MODIFF;
872 XSETINT (column, mincol);
873 return column;
877 static double position_indentation P_ ((int));
879 DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
880 0, 0, 0,
881 doc: /* Return the indentation of the current line.
882 This is the horizontal position of the character
883 following any initial whitespace. */)
886 Lisp_Object val;
887 int opoint = PT, opoint_byte = PT_BYTE;
889 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
891 XSETFASTINT (val, (int) position_indentation (PT_BYTE)); /* iftc */
892 SET_PT_BOTH (opoint, opoint_byte);
893 return val;
896 static double
897 position_indentation (pos_byte)
898 register int pos_byte;
900 register EMACS_INT column = 0;
901 register EMACS_INT tab_width = XINT (current_buffer->tab_width);
902 register unsigned char *p;
903 register unsigned char *stop;
904 unsigned char *start;
905 EMACS_INT next_boundary_byte = pos_byte;
906 EMACS_INT ceiling = next_boundary_byte;
908 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
910 p = BYTE_POS_ADDR (pos_byte);
911 /* STOP records the value of P at which we will need
912 to think about the gap, or about invisible text,
913 or about the end of the buffer. */
914 stop = p;
915 /* START records the starting value of P. */
916 start = p;
917 while (1)
919 while (p == stop)
921 EMACS_INT stop_pos_byte;
923 /* If we have updated P, set POS_BYTE to match.
924 The first time we enter the loop, POS_BYTE is already right. */
925 if (p != start)
926 pos_byte = PTR_BYTE_POS (p);
927 /* Consider the various reasons STOP might have been set here. */
928 if (pos_byte == ZV_BYTE)
929 return column;
930 if (pos_byte == next_boundary_byte)
932 EMACS_INT next_boundary;
933 EMACS_INT pos = BYTE_TO_CHAR (pos_byte);
934 pos = skip_invisible (pos, &next_boundary, ZV, Qnil);
935 pos_byte = CHAR_TO_BYTE (pos);
936 next_boundary_byte = CHAR_TO_BYTE (next_boundary);
938 if (pos_byte >= ceiling)
939 ceiling = BUFFER_CEILING_OF (pos_byte) + 1;
940 /* Compute the next place we need to stop and think,
941 and set STOP accordingly. */
942 stop_pos_byte = min (ceiling, next_boundary_byte);
943 /* The -1 and +1 arrange to point at the first byte of gap
944 (if STOP_POS_BYTE is the position of the gap)
945 rather than at the data after the gap. */
947 stop = BYTE_POS_ADDR (stop_pos_byte - 1) + 1;
948 p = BYTE_POS_ADDR (pos_byte);
950 switch (*p++)
952 case 0240:
953 if (! NILP (current_buffer->enable_multibyte_characters))
954 return column;
955 case ' ':
956 column++;
957 break;
958 case '\t':
959 column += tab_width - column % tab_width;
960 break;
961 default:
962 if (ASCII_BYTE_P (p[-1])
963 || NILP (current_buffer->enable_multibyte_characters))
964 return column;
966 int c;
967 pos_byte = PTR_BYTE_POS (p - 1);
968 c = FETCH_MULTIBYTE_CHAR (pos_byte);
969 if (CHAR_HAS_CATEGORY (c, ' '))
971 column++;
972 INC_POS (pos_byte);
973 p = BYTE_POS_ADDR (pos_byte);
975 else
976 return column;
982 /* Test whether the line beginning at POS is indented beyond COLUMN.
983 Blank lines are treated as if they had the same indentation as the
984 preceding line. */
987 indented_beyond_p (pos, pos_byte, column)
988 int pos, pos_byte;
989 double column;
991 double val;
992 int opoint = PT, opoint_byte = PT_BYTE;
994 SET_PT_BOTH (pos, pos_byte);
995 while (PT > BEGV && FETCH_BYTE (PT_BYTE) == '\n')
996 scan_newline (PT - 1, PT_BYTE - 1, BEGV, BEGV_BYTE, -1, 0);
998 val = position_indentation (PT_BYTE);
999 SET_PT_BOTH (opoint, opoint_byte);
1000 return val >= column; /* hmm, float comparison */
1003 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
1004 doc: /* Move point to column COLUMN in the current line.
1005 Interactively, COLUMN is the value of prefix numeric argument.
1006 The column of a character is calculated by adding together the widths
1007 as displayed of the previous characters in the line.
1008 This function ignores line-continuation;
1009 there is no upper limit on the column number a character can have
1010 and horizontal scrolling has no effect.
1012 If specified column is within a character, point goes after that character.
1013 If it's past end of line, point goes to end of line.
1015 Optional second argument FORCE non-nil means if COLUMN is in the
1016 middle of a tab character, change it to spaces.
1017 In addition, if FORCE is t, and the line is too short to reach
1018 COLUMN, add spaces/tabs to get there.
1020 The return value is the current column. */)
1021 (column, force)
1022 Lisp_Object column, force;
1024 EMACS_INT pos;
1025 EMACS_INT col, prev_col;
1026 EMACS_INT goal;
1028 CHECK_NATNUM (column);
1029 goal = XINT (column);
1031 col = goal;
1032 pos = ZV;
1033 scan_for_column (&pos, &col, &prev_col);
1035 SET_PT (pos);
1037 /* If a tab char made us overshoot, change it to spaces
1038 and scan through it again. */
1039 if (!NILP (force) && col > goal)
1041 EMACS_INT pos_byte = PT_BYTE;
1042 DEC_POS (pos_byte);
1043 int c = FETCH_CHAR (pos_byte);
1045 if (c == '\t' && prev_col < goal)
1047 EMACS_INT goal_pt, goal_pt_byte;
1049 /* Insert spaces in front of the tab to reach GOAL. Do this
1050 first so that a marker at the end of the tab gets
1051 adjusted. */
1052 SET_PT_BOTH (PT - 1, PT_BYTE - 1);
1053 Finsert_char (make_number (' '), make_number (goal - prev_col), Qt);
1055 /* Now delete the tab, and indent to COL. */
1056 del_range (PT, PT + 1);
1057 goal_pt = PT;
1058 goal_pt_byte = PT_BYTE;
1059 Findent_to (make_number (col), Qnil);
1060 SET_PT_BOTH (goal_pt, goal_pt_byte);
1062 /* Set the last_known... vars consistently. */
1063 col = goal;
1067 /* If line ends prematurely, add space to the end. */
1068 if (col < goal && EQ (force, Qt))
1069 Findent_to (make_number (col = goal), Qnil);
1071 last_known_column = col;
1072 last_known_column_point = PT;
1073 last_known_column_modified = MODIFF;
1075 return make_number (col);
1078 /* compute_motion: compute buffer posn given screen posn and vice versa */
1080 struct position val_compute_motion;
1082 /* Scan the current buffer forward from offset FROM, pretending that
1083 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1084 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1085 and return the ending buffer position and screen location. If we
1086 can't hit the requested column exactly (because of a tab or other
1087 multi-column character), overshoot.
1089 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
1090 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1091 earlier call to compute_motion. The other common case is that FROMHPOS
1092 is zero and FROM is a position that "belongs" at column zero, but might
1093 be shifted by overlay strings; in this case DID_MOTION should be 0.
1095 WIDTH is the number of columns available to display text;
1096 compute_motion uses this to handle continuation lines and such.
1097 If WIDTH is -1, use width of window's text area adjusted for
1098 continuation glyph when needed.
1100 HSCROLL is the number of columns not being displayed at the left
1101 margin; this is usually taken from a window's hscroll member.
1102 TAB_OFFSET is the number of columns of the first tab that aren't
1103 being displayed, perhaps because of a continuation line or
1104 something.
1106 compute_motion returns a pointer to a struct position. The bufpos
1107 member gives the buffer position at the end of the scan, and hpos
1108 and vpos give its cartesian location. prevhpos is the column at
1109 which the character before bufpos started, and contin is non-zero
1110 if we reached the current line by continuing the previous.
1112 Note that FROMHPOS and TOHPOS should be expressed in real screen
1113 columns, taking HSCROLL and the truncation glyph at the left margin
1114 into account. That is, beginning-of-line moves you to the hpos
1115 -HSCROLL + (HSCROLL > 0).
1117 For example, to find the buffer position of column COL of line LINE
1118 of a certain window, pass the window's starting location as FROM
1119 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1120 Pass the buffer's ZV as TO, to limit the scan to the end of the
1121 visible section of the buffer, and pass LINE and COL as TOVPOS and
1122 TOHPOS.
1124 When displaying in window w, a typical formula for WIDTH is:
1126 window_width - 1
1127 - (has_vertical_scroll_bars
1128 ? WINDOW_CONFIG_SCROLL_BAR_COLS (window)
1129 : (window_width + window_left != frame_cols))
1131 where
1132 window_width is XFASTINT (w->total_cols),
1133 window_left is XFASTINT (w->left_col),
1134 has_vertical_scroll_bars is
1135 WINDOW_HAS_VERTICAL_SCROLL_BAR (window)
1136 and frame_cols = FRAME_COLS (XFRAME (window->frame))
1138 Or you can let window_box_text_cols do this all for you, and write:
1139 window_box_text_cols (w) - 1
1141 The `-1' accounts for the continuation-line backslashes; the rest
1142 accounts for window borders if the window is split horizontally, and
1143 the scroll bars if they are turned on. */
1145 struct position *
1146 compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win)
1147 EMACS_INT from, fromvpos, fromhpos, to, tovpos, tohpos;
1148 int did_motion;
1149 EMACS_INT width;
1150 EMACS_INT hscroll, tab_offset;
1151 struct window *win;
1153 register EMACS_INT hpos = fromhpos;
1154 register EMACS_INT vpos = fromvpos;
1156 register EMACS_INT pos;
1157 EMACS_INT pos_byte;
1158 register int c = 0;
1159 register EMACS_INT tab_width = XFASTINT (current_buffer->tab_width);
1160 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
1161 register struct Lisp_Char_Table *dp = window_display_table (win);
1162 int selective
1163 = (INTEGERP (current_buffer->selective_display)
1164 ? XINT (current_buffer->selective_display)
1165 : !NILP (current_buffer->selective_display) ? -1 : 0);
1166 int selective_rlen
1167 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
1168 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0);
1169 /* The next location where the `invisible' property changes, or an
1170 overlay starts or ends. */
1171 EMACS_INT next_boundary = from;
1173 /* For computing runs of characters with similar widths.
1174 Invariant: width_run_width is zero, or all the characters
1175 from width_run_start to width_run_end have a fixed width of
1176 width_run_width. */
1177 EMACS_INT width_run_start = from;
1178 EMACS_INT width_run_end = from;
1179 EMACS_INT width_run_width = 0;
1180 Lisp_Object *width_table;
1181 Lisp_Object buffer;
1183 /* The next buffer pos where we should consult the width run cache. */
1184 EMACS_INT next_width_run = from;
1185 Lisp_Object window;
1187 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1188 /* If previous char scanned was a wide character,
1189 this is the column where it ended. Otherwise, this is 0. */
1190 EMACS_INT wide_column_end_hpos = 0;
1191 EMACS_INT prev_pos; /* Previous buffer position. */
1192 EMACS_INT prev_pos_byte; /* Previous buffer position. */
1193 EMACS_INT prev_hpos = 0;
1194 EMACS_INT prev_vpos = 0;
1195 EMACS_INT contin_hpos; /* HPOS of last column of continued line. */
1196 EMACS_INT prev_tab_offset; /* Previous tab offset. */
1197 EMACS_INT continuation_glyph_width;
1199 XSETBUFFER (buffer, current_buffer);
1200 XSETWINDOW (window, win);
1202 width_run_cache_on_off ();
1203 if (dp == buffer_display_table ())
1204 width_table = (VECTORP (current_buffer->width_table)
1205 ? XVECTOR (current_buffer->width_table)->contents
1206 : 0);
1207 else
1208 /* If the window has its own display table, we can't use the width
1209 run cache, because that's based on the buffer's display table. */
1210 width_table = 0;
1212 if (tab_width <= 0 || tab_width > 1000)
1213 tab_width = 8;
1215 /* Negative width means use all available text columns. */
1216 if (width < 0)
1218 width = window_box_text_cols (win);
1219 /* We must make room for continuation marks if we don't have fringes. */
1220 #ifdef HAVE_WINDOW_SYSTEM
1221 if (!FRAME_WINDOW_P (XFRAME (win->frame)))
1222 #endif
1223 width -= 1;
1226 continuation_glyph_width = 1;
1227 #ifdef HAVE_WINDOW_SYSTEM
1228 if (FRAME_WINDOW_P (XFRAME (win->frame)))
1229 continuation_glyph_width = 0; /* In the fringe. */
1230 #endif
1232 immediate_quit = 1;
1233 QUIT;
1235 pos = prev_pos = from;
1236 pos_byte = prev_pos_byte = CHAR_TO_BYTE (from);
1237 contin_hpos = 0;
1238 prev_tab_offset = tab_offset;
1239 while (1)
1241 while (pos == next_boundary)
1243 EMACS_INT pos_here = pos;
1244 EMACS_INT newpos;
1246 /* Don't skip invisible if we are already at the margin. */
1247 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1249 if (contin_hpos && prev_hpos == 0
1250 && hpos > tohpos
1251 && (contin_hpos == width || wide_column_end_hpos > width))
1252 { /* Line breaks because we can't put the character at the
1253 previous line any more. It is not the multi-column
1254 character continued in middle. Go back to previous
1255 buffer position, screen position, and set tab offset
1256 to previous value. It's the beginning of the
1257 line. */
1258 pos = prev_pos;
1259 pos_byte = prev_pos_byte;
1260 hpos = prev_hpos;
1261 vpos = prev_vpos;
1262 tab_offset = prev_tab_offset;
1264 break;
1267 /* If the caller says that the screen position came from an earlier
1268 call to compute_motion, then we've already accounted for the
1269 overlay strings at point. This is only true the first time
1270 through, so clear the flag after testing it. */
1271 if (!did_motion)
1272 /* We need to skip past the overlay strings. Currently those
1273 strings must not contain TAB;
1274 if we want to relax that restriction, something will have
1275 to be changed here. */
1277 unsigned char *ovstr;
1278 int ovlen = overlay_strings (pos, win, &ovstr);
1279 hpos += ((multibyte && ovlen > 0)
1280 ? strwidth (ovstr, ovlen) : ovlen);
1282 did_motion = 0;
1284 if (pos >= to)
1285 break;
1287 /* Advance POS past invisible characters
1288 (but not necessarily all that there are here),
1289 and store in next_boundary the next position where
1290 we need to call skip_invisible. */
1291 newpos = skip_invisible (pos, &next_boundary, to, window);
1293 if (newpos >= to)
1295 pos = min (to, newpos);
1296 pos_byte = CHAR_TO_BYTE (pos);
1297 goto after_loop;
1300 if (newpos != pos_here)
1302 pos = newpos;
1303 pos_byte = CHAR_TO_BYTE (pos);
1307 /* Handle right margin. */
1308 /* Note on a wide-column character.
1310 Characters are classified into the following three categories
1311 according to the width (columns occupied on screen).
1313 (1) single-column character: ex. `a'
1314 (2) multi-column character: ex. `^A', TAB, `\033'
1315 (3) wide-column character: ex. Japanese character, Chinese character
1316 (In the following example, `W_' stands for them.)
1318 Multi-column characters can be divided around the right margin,
1319 but wide-column characters cannot.
1321 NOTE:
1323 (*) The cursor is placed on the next character after the point.
1325 ----------
1326 abcdefghi\
1327 j ^---- next after the point
1328 ^--- next char. after the point.
1329 ----------
1330 In case of sigle-column character
1332 ----------
1333 abcdefgh\\
1334 033 ^---- next after the point, next char. after the point.
1335 ----------
1336 In case of multi-column character
1338 ----------
1339 abcdefgh\\
1340 W_ ^---- next after the point
1341 ^---- next char. after the point.
1342 ----------
1343 In case of wide-column character
1345 The problem here is continuation at a wide-column character.
1346 In this case, the line may shorter less than WIDTH.
1347 And we find the continuation AFTER it occurs.
1351 if (hpos > width)
1353 if (hscroll
1354 || (truncate_partial_width_windows
1355 && ((width + continuation_glyph_width)
1356 < FRAME_COLS (XFRAME (WINDOW_FRAME (win)))))
1357 || !NILP (current_buffer->truncate_lines))
1359 /* Truncating: skip to newline, unless we are already past
1360 TO (we need to go back below). */
1361 if (pos <= to)
1363 pos = find_before_next_newline (pos, to, 1);
1364 pos_byte = CHAR_TO_BYTE (pos);
1365 hpos = width;
1366 /* If we just skipped next_boundary,
1367 loop around in the main while
1368 and handle it. */
1369 if (pos >= next_boundary)
1370 next_boundary = pos + 1;
1371 prev_hpos = width;
1372 prev_vpos = vpos;
1373 prev_tab_offset = tab_offset;
1376 else
1378 /* Continuing. */
1379 /* Remember the previous value. */
1380 prev_tab_offset = tab_offset;
1382 if (wide_column_end_hpos > width)
1384 hpos -= prev_hpos;
1385 tab_offset += prev_hpos;
1387 else
1389 tab_offset += width;
1390 hpos -= width;
1392 vpos++;
1393 contin_hpos = prev_hpos;
1394 prev_hpos = 0;
1395 prev_vpos = vpos;
1399 /* Stop if past the target buffer position or screen position. */
1400 if (pos > to)
1402 /* Go back to the previous position. */
1403 pos = prev_pos;
1404 pos_byte = prev_pos_byte;
1405 hpos = prev_hpos;
1406 vpos = prev_vpos;
1407 tab_offset = prev_tab_offset;
1409 /* NOTE on contin_hpos, hpos, and prev_hpos.
1411 ----------
1412 abcdefgh\\
1413 W_ ^---- contin_hpos
1414 | ^----- hpos
1415 \---- prev_hpos
1416 ----------
1419 if (contin_hpos && prev_hpos == 0
1420 && contin_hpos < width && !wide_column_end_hpos)
1422 /* Line breaking occurs in the middle of multi-column
1423 character. Go back to previous line. */
1424 hpos = contin_hpos;
1425 vpos = vpos - 1;
1427 break;
1430 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1432 if (contin_hpos && prev_hpos == 0
1433 && hpos > tohpos
1434 && (contin_hpos == width || wide_column_end_hpos > width))
1435 { /* Line breaks because we can't put the character at the
1436 previous line any more. It is not the multi-column
1437 character continued in middle. Go back to previous
1438 buffer position, screen position, and set tab offset
1439 to previous value. It's the beginning of the
1440 line. */
1441 pos = prev_pos;
1442 pos_byte = prev_pos_byte;
1443 hpos = prev_hpos;
1444 vpos = prev_vpos;
1445 tab_offset = prev_tab_offset;
1447 break;
1449 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1450 break;
1452 prev_hpos = hpos;
1453 prev_vpos = vpos;
1454 prev_pos = pos;
1455 prev_pos_byte = pos_byte;
1456 wide_column_end_hpos = 0;
1458 /* Consult the width run cache to see if we can avoid inspecting
1459 the text character-by-character. */
1460 if (current_buffer->width_run_cache && pos >= next_width_run)
1462 int run_end;
1463 int common_width
1464 = region_cache_forward (current_buffer,
1465 current_buffer->width_run_cache,
1466 pos, &run_end);
1468 /* A width of zero means the character's width varies (like
1469 a tab), is meaningless (like a newline), or we just don't
1470 want to skip over it for some other reason. */
1471 if (common_width != 0)
1473 int run_end_hpos;
1475 /* Don't go past the final buffer posn the user
1476 requested. */
1477 if (run_end > to)
1478 run_end = to;
1480 run_end_hpos = hpos + (run_end - pos) * common_width;
1482 /* Don't go past the final horizontal position the user
1483 requested. */
1484 if (vpos == tovpos && run_end_hpos > tohpos)
1486 run_end = pos + (tohpos - hpos) / common_width;
1487 run_end_hpos = hpos + (run_end - pos) * common_width;
1490 /* Don't go past the margin. */
1491 if (run_end_hpos >= width)
1493 run_end = pos + (width - hpos) / common_width;
1494 run_end_hpos = hpos + (run_end - pos) * common_width;
1497 hpos = run_end_hpos;
1498 if (run_end > pos)
1499 prev_hpos = hpos - common_width;
1500 if (pos != run_end)
1502 pos = run_end;
1503 pos_byte = CHAR_TO_BYTE (pos);
1507 next_width_run = run_end + 1;
1510 /* We have to scan the text character-by-character. */
1511 else
1513 EMACS_INT i, n;
1514 Lisp_Object charvec;
1516 c = FETCH_BYTE (pos_byte);
1518 /* Check composition sequence. */
1520 int len, len_byte, width;
1522 if (check_composition (pos, pos_byte, to, &len, &len_byte, &width))
1524 pos += len;
1525 pos_byte += len_byte;
1526 hpos += width;
1527 continue;
1531 pos++, pos_byte++;
1533 /* Perhaps add some info to the width_run_cache. */
1534 if (current_buffer->width_run_cache)
1536 /* Is this character part of the current run? If so, extend
1537 the run. */
1538 if (pos - 1 == width_run_end
1539 && XFASTINT (width_table[c]) == width_run_width)
1540 width_run_end = pos;
1542 /* The previous run is over, since this is a character at a
1543 different position, or a different width. */
1544 else
1546 /* Have we accumulated a run to put in the cache?
1547 (Currently, we only cache runs of width == 1). */
1548 if (width_run_start < width_run_end
1549 && width_run_width == 1)
1550 know_region_cache (current_buffer,
1551 current_buffer->width_run_cache,
1552 width_run_start, width_run_end);
1554 /* Start recording a new width run. */
1555 width_run_width = XFASTINT (width_table[c]);
1556 width_run_start = pos - 1;
1557 width_run_end = pos;
1561 if (dp != 0
1562 && ! (multibyte && BASE_LEADING_CODE_P (c))
1563 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1565 charvec = DISP_CHAR_VECTOR (dp, c);
1566 n = ASIZE (charvec);
1568 else
1570 charvec = Qnil;
1571 n = 1;
1574 for (i = n - 1; i >= 0; --i)
1576 if (VECTORP (charvec))
1578 /* This should be handled the same as
1579 next_element_from_display_vector does it. */
1580 Lisp_Object entry = AREF (charvec, i);
1582 if (GLYPH_CODE_P (entry)
1583 && GLYPH_CODE_CHAR_VALID_P (entry))
1584 c = GLYPH_CODE_CHAR (entry);
1585 else
1586 c = ' ';
1589 if (c >= 040 && c < 0177)
1590 hpos++;
1591 else if (c == '\t')
1593 int tem = ((hpos + tab_offset + hscroll - (hscroll > 0))
1594 % tab_width);
1595 if (tem < 0)
1596 tem += tab_width;
1597 hpos += tab_width - tem;
1599 else if (c == '\n')
1601 if (selective > 0
1602 && indented_beyond_p (pos, pos_byte,
1603 (double) selective)) /* iftc */
1605 /* If (pos == to), we don't have to take care of
1606 selective display. */
1607 if (pos < to)
1609 /* Skip any number of invisible lines all at once */
1612 pos = find_before_next_newline (pos, to, 1);
1613 if (pos < to)
1614 pos++;
1615 pos_byte = CHAR_TO_BYTE (pos);
1617 while (pos < to
1618 && indented_beyond_p (pos, pos_byte,
1619 (double) selective)); /* iftc */
1620 /* Allow for the " ..." that is displayed for them. */
1621 if (selective_rlen)
1623 hpos += selective_rlen;
1624 if (hpos >= width)
1625 hpos = width;
1627 DEC_BOTH (pos, pos_byte);
1628 /* We have skipped the invis text, but not the
1629 newline after. */
1632 else
1634 /* A visible line. */
1635 vpos++;
1636 hpos = 0;
1637 hpos -= hscroll;
1638 /* Count the truncation glyph on column 0 */
1639 if (hscroll > 0)
1640 hpos += continuation_glyph_width;
1641 tab_offset = 0;
1643 contin_hpos = 0;
1645 else if (c == CR && selective < 0)
1647 /* In selective display mode,
1648 everything from a ^M to the end of the line is invisible.
1649 Stop *before* the real newline. */
1650 if (pos < to)
1652 pos = find_before_next_newline (pos, to, 1);
1653 pos_byte = CHAR_TO_BYTE (pos);
1655 /* If we just skipped next_boundary,
1656 loop around in the main while
1657 and handle it. */
1658 if (pos > next_boundary)
1659 next_boundary = pos;
1660 /* Allow for the " ..." that is displayed for them. */
1661 if (selective_rlen)
1663 hpos += selective_rlen;
1664 if (hpos >= width)
1665 hpos = width;
1668 else if (multibyte && BASE_LEADING_CODE_P (c))
1670 /* Start of multi-byte form. */
1671 unsigned char *ptr;
1672 int bytes, width, wide_column;
1674 pos_byte--; /* rewind POS_BYTE */
1675 ptr = BYTE_POS_ADDR (pos_byte);
1676 MULTIBYTE_BYTES_WIDTH (ptr, dp);
1677 pos_byte += bytes;
1678 if (wide_column)
1679 wide_column_end_hpos = hpos + wide_column;
1680 hpos += width;
1682 else if (VECTORP (charvec))
1683 ++hpos;
1684 else
1685 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1690 after_loop:
1692 /* Remember any final width run in the cache. */
1693 if (current_buffer->width_run_cache
1694 && width_run_width == 1
1695 && width_run_start < width_run_end)
1696 know_region_cache (current_buffer, current_buffer->width_run_cache,
1697 width_run_start, width_run_end);
1699 val_compute_motion.bufpos = pos;
1700 val_compute_motion.bytepos = pos_byte;
1701 val_compute_motion.hpos = hpos;
1702 val_compute_motion.vpos = vpos;
1703 if (contin_hpos && prev_hpos == 0)
1704 val_compute_motion.prevhpos = contin_hpos;
1705 else
1706 val_compute_motion.prevhpos = prev_hpos;
1707 /* We alalways handle all of them here; none of them remain to do. */
1708 val_compute_motion.ovstring_chars_done = 0;
1710 /* Nonzero if have just continued a line */
1711 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1713 immediate_quit = 0;
1714 return &val_compute_motion;
1718 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1719 doc: /* Scan through the current buffer, calculating screen position.
1720 Scan the current buffer forward from offset FROM,
1721 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1722 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1723 and return the ending buffer position and screen location.
1725 If TOPOS is nil, the actual width and height of the window's
1726 text area are used.
1728 There are three additional arguments:
1730 WIDTH is the number of columns available to display text;
1731 this affects handling of continuation lines. A value of nil
1732 corresponds to the actual number of available text columns.
1734 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1735 HSCROLL is the number of columns not being displayed at the left
1736 margin; this is usually taken from a window's hscroll member.
1737 TAB-OFFSET is the number of columns of the first tab that aren't
1738 being displayed, perhaps because the line was continued within it.
1739 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1741 WINDOW is the window to operate on. It is used to choose the display table;
1742 if it is showing the current buffer, it is used also for
1743 deciding which overlay properties apply.
1744 Note that `compute-motion' always operates on the current buffer.
1746 The value is a list of five elements:
1747 (POS HPOS VPOS PREVHPOS CONTIN)
1748 POS is the buffer position where the scan stopped.
1749 VPOS is the vertical position where the scan stopped.
1750 HPOS is the horizontal position where the scan stopped.
1752 PREVHPOS is the horizontal position one character back from POS.
1753 CONTIN is t if a line was continued after (or within) the previous character.
1755 For example, to find the buffer position of column COL of line LINE
1756 of a certain window, pass the window's starting location as FROM
1757 and the window's upper-left coordinates as FROMPOS.
1758 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1759 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1760 (from, frompos, to, topos, width, offsets, window)
1761 Lisp_Object from, frompos, to, topos;
1762 Lisp_Object width, offsets, window;
1764 struct window *w;
1765 Lisp_Object bufpos, hpos, vpos, prevhpos;
1766 struct position *pos;
1767 int hscroll, tab_offset;
1769 CHECK_NUMBER_COERCE_MARKER (from);
1770 CHECK_CONS (frompos);
1771 CHECK_NUMBER_CAR (frompos);
1772 CHECK_NUMBER_CDR (frompos);
1773 CHECK_NUMBER_COERCE_MARKER (to);
1774 if (!NILP (topos))
1776 CHECK_CONS (topos);
1777 CHECK_NUMBER_CAR (topos);
1778 CHECK_NUMBER_CDR (topos);
1780 if (!NILP (width))
1781 CHECK_NUMBER (width);
1783 if (!NILP (offsets))
1785 CHECK_CONS (offsets);
1786 CHECK_NUMBER_CAR (offsets);
1787 CHECK_NUMBER_CDR (offsets);
1788 hscroll = XINT (XCAR (offsets));
1789 tab_offset = XINT (XCDR (offsets));
1791 else
1792 hscroll = tab_offset = 0;
1794 if (NILP (window))
1795 window = Fselected_window ();
1796 else
1797 CHECK_LIVE_WINDOW (window);
1798 w = XWINDOW (window);
1800 if (XINT (from) < BEGV || XINT (from) > ZV)
1801 args_out_of_range_3 (from, make_number (BEGV), make_number (ZV));
1802 if (XINT (to) < BEGV || XINT (to) > ZV)
1803 args_out_of_range_3 (to, make_number (BEGV), make_number (ZV));
1805 pos = compute_motion (XINT (from), XINT (XCDR (frompos)),
1806 XINT (XCAR (frompos)), 0,
1807 XINT (to),
1808 (NILP (topos)
1809 ? window_internal_height (w)
1810 : XINT (XCDR (topos))),
1811 (NILP (topos)
1812 ? (window_box_text_cols (w)
1814 #ifdef HAVE_WINDOW_SYSTEM
1815 FRAME_WINDOW_P (XFRAME (w->frame)) ? 0 :
1816 #endif
1818 : XINT (XCAR (topos))),
1819 (NILP (width) ? -1 : XINT (width)),
1820 hscroll, tab_offset,
1821 XWINDOW (window));
1823 XSETFASTINT (bufpos, pos->bufpos);
1824 XSETINT (hpos, pos->hpos);
1825 XSETINT (vpos, pos->vpos);
1826 XSETINT (prevhpos, pos->prevhpos);
1828 return Fcons (bufpos,
1829 Fcons (hpos,
1830 Fcons (vpos,
1831 Fcons (prevhpos,
1832 Fcons (pos->contin ? Qt : Qnil, Qnil)))));
1836 /* Fvertical_motion and vmotion */
1838 struct position val_vmotion;
1840 struct position *
1841 vmotion (from, vtarget, w)
1842 register EMACS_INT from, vtarget;
1843 struct window *w;
1845 EMACS_INT hscroll = XINT (w->hscroll);
1846 struct position pos;
1847 /* vpos is cumulative vertical position, changed as from is changed */
1848 register int vpos = 0;
1849 EMACS_INT prevline;
1850 register EMACS_INT first;
1851 EMACS_INT from_byte;
1852 EMACS_INT lmargin = hscroll > 0 ? 1 - hscroll : 0;
1853 int selective
1854 = (INTEGERP (current_buffer->selective_display)
1855 ? XINT (current_buffer->selective_display)
1856 : !NILP (current_buffer->selective_display) ? -1 : 0);
1857 Lisp_Object window;
1858 EMACS_INT start_hpos = 0;
1859 int did_motion;
1860 /* This is the object we use for fetching character properties. */
1861 Lisp_Object text_prop_object;
1863 XSETWINDOW (window, w);
1865 /* If the window contains this buffer, use it for getting text properties.
1866 Otherwise use the current buffer as arg for doing that. */
1867 if (EQ (w->buffer, Fcurrent_buffer ()))
1868 text_prop_object = window;
1869 else
1870 text_prop_object = Fcurrent_buffer ();
1872 if (vpos >= vtarget)
1874 /* To move upward, go a line at a time until
1875 we have gone at least far enough. */
1877 first = 1;
1879 while ((vpos > vtarget || first) && from > BEGV)
1881 Lisp_Object propval;
1883 prevline = find_next_newline_no_quit (from - 1, -1);
1884 while (prevline > BEGV
1885 && ((selective > 0
1886 && indented_beyond_p (prevline,
1887 CHAR_TO_BYTE (prevline),
1888 (double) selective)) /* iftc */
1889 /* Watch out for newlines with `invisible' property.
1890 When moving upward, check the newline before. */
1891 || (propval = Fget_char_property (make_number (prevline - 1),
1892 Qinvisible,
1893 text_prop_object),
1894 TEXT_PROP_MEANS_INVISIBLE (propval))))
1895 prevline = find_next_newline_no_quit (prevline - 1, -1);
1896 pos = *compute_motion (prevline, 0,
1897 lmargin + (prevline == BEG ? start_hpos : 0),
1899 from,
1900 /* Don't care for VPOS... */
1901 1 << (BITS_PER_SHORT - 1),
1902 /* ... nor HPOS. */
1903 1 << (BITS_PER_SHORT - 1),
1904 -1, hscroll,
1905 /* This compensates for start_hpos
1906 so that a tab as first character
1907 still occupies 8 columns. */
1908 (prevline == BEG ? -start_hpos : 0),
1910 vpos -= pos.vpos;
1911 first = 0;
1912 from = prevline;
1915 /* If we made exactly the desired vertical distance,
1916 or if we hit beginning of buffer,
1917 return point found */
1918 if (vpos >= vtarget)
1920 val_vmotion.bufpos = from;
1921 val_vmotion.bytepos = CHAR_TO_BYTE (from);
1922 val_vmotion.vpos = vpos;
1923 val_vmotion.hpos = lmargin;
1924 val_vmotion.contin = 0;
1925 val_vmotion.prevhpos = 0;
1926 val_vmotion.ovstring_chars_done = 0;
1927 val_vmotion.tab_offset = 0; /* For accumulating tab offset. */
1928 return &val_vmotion;
1931 /* Otherwise find the correct spot by moving down */
1933 /* Moving downward is simple, but must calculate from beg of line
1934 to determine hpos of starting point */
1935 from_byte = CHAR_TO_BYTE (from);
1936 if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n')
1938 Lisp_Object propval;
1940 prevline = find_next_newline_no_quit (from, -1);
1941 while (prevline > BEGV
1942 && ((selective > 0
1943 && indented_beyond_p (prevline,
1944 CHAR_TO_BYTE (prevline),
1945 (double) selective)) /* iftc */
1946 /* Watch out for newlines with `invisible' property.
1947 When moving downward, check the newline after. */
1948 || (propval = Fget_char_property (make_number (prevline),
1949 Qinvisible,
1950 text_prop_object),
1951 TEXT_PROP_MEANS_INVISIBLE (propval))))
1952 prevline = find_next_newline_no_quit (prevline - 1, -1);
1953 pos = *compute_motion (prevline, 0,
1954 lmargin + (prevline == BEG
1955 ? start_hpos : 0),
1957 from,
1958 /* Don't care for VPOS... */
1959 1 << (BITS_PER_SHORT - 1),
1960 /* ... nor HPOS. */
1961 1 << (BITS_PER_SHORT - 1),
1962 -1, hscroll,
1963 (prevline == BEG ? -start_hpos : 0),
1965 did_motion = 1;
1967 else
1969 pos.hpos = lmargin + (from == BEG ? start_hpos : 0);
1970 pos.vpos = 0;
1971 pos.tab_offset = 0;
1972 did_motion = 0;
1974 return compute_motion (from, vpos, pos.hpos, did_motion,
1975 ZV, vtarget, - (1 << (BITS_PER_SHORT - 1)),
1976 -1, hscroll,
1977 pos.tab_offset - (from == BEG ? start_hpos : 0),
1981 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
1982 doc: /* Move point to start of the screen line LINES lines down.
1983 If LINES is negative, this means moving up.
1985 This function is an ordinary cursor motion function
1986 which calculates the new position based on how text would be displayed.
1987 The new position may be the start of a line,
1988 or just the start of a continuation line.
1989 The function returns number of screen lines moved over;
1990 that usually equals LINES, but may be closer to zero
1991 if beginning or end of buffer was reached.
1993 The optional second argument WINDOW specifies the window to use for
1994 parameters such as width, horizontal scrolling, and so on.
1995 The default is to use the selected window's parameters.
1997 `vertical-motion' always uses the current buffer,
1998 regardless of which buffer is displayed in WINDOW.
1999 This is consistent with other cursor motion functions
2000 and makes it possible to use `vertical-motion' in any buffer,
2001 whether or not it is currently displayed in some window. */)
2002 (lines, window)
2003 Lisp_Object lines, window;
2005 struct it it;
2006 struct text_pos pt;
2007 struct window *w;
2008 Lisp_Object old_buffer;
2009 struct gcpro gcpro1;
2011 CHECK_NUMBER (lines);
2012 if (! NILP (window))
2013 CHECK_WINDOW (window);
2014 else
2015 window = selected_window;
2016 w = XWINDOW (window);
2018 old_buffer = Qnil;
2019 GCPRO1 (old_buffer);
2020 if (XBUFFER (w->buffer) != current_buffer)
2022 /* Set the window's buffer temporarily to the current buffer. */
2023 old_buffer = w->buffer;
2024 XSETBUFFER (w->buffer, current_buffer);
2027 if (noninteractive)
2029 struct position pos;
2030 pos = *vmotion (PT, XINT (lines), w);
2031 SET_PT_BOTH (pos.bufpos, pos.bytepos);
2033 else
2035 int it_start;
2036 int oselective;
2037 int it_overshoot_expected;
2039 SET_TEXT_POS (pt, PT, PT_BYTE);
2040 start_display (&it, w, pt);
2042 /* Scan from the start of the line containing PT. If we don't
2043 do this, we start moving with IT->current_x == 0, while PT is
2044 really at some x > 0. The effect is, in continuation lines, that
2045 we end up with the iterator placed at where it thinks X is 0,
2046 while the end position is really at some X > 0, the same X that
2047 PT had. */
2048 it_start = IT_CHARPOS (it);
2050 /* We expect the call to move_it_to, further down, to overshoot
2051 if the starting point is on an image, stretch glyph,
2052 composition, or Lisp string. We won't need to backtrack in
2053 this situation, except for one corner case: when the Lisp
2054 string contains a newline. */
2055 if (it.method == GET_FROM_STRING)
2057 const char *s = SDATA (it.string);
2058 const char *e = s + SBYTES (it.string);
2060 while (s < e && *s != '\n')
2061 ++s;
2063 /* If there is no newline in the string, we need to check
2064 whether there is a newline immediately after the string
2065 in move_it_to below. This may happen if there is an
2066 overlay with an after-string just before the newline. */
2067 it_overshoot_expected = (s == e) ? -1 : 0;
2069 else
2070 it_overshoot_expected = (it.method == GET_FROM_IMAGE
2071 || it.method == GET_FROM_STRETCH
2072 || it.method == GET_FROM_COMPOSITION);
2074 reseat_at_previous_visible_line_start (&it);
2075 it.current_x = it.hpos = 0;
2076 /* Temporarily disable selective display so we don't move too far */
2077 oselective = it.selective;
2078 it.selective = 0;
2079 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
2080 it.selective = oselective;
2082 /* Move back if we got too far. This may happen if
2083 truncate-lines is on and PT is beyond right margin.
2084 Don't go back if the overshoot is expected (see above). */
2085 if (IT_CHARPOS (it) > it_start && XINT (lines) > 0
2086 && (!it_overshoot_expected
2087 || (it_overshoot_expected < 0
2088 && it.method == GET_FROM_BUFFER
2089 && it.c == '\n')))
2090 move_it_by_lines (&it, -1, 0);
2092 it.vpos = 0;
2093 /* Do this even if LINES is 0, so that we move back
2094 to the beginning of the current line as we ought. */
2095 if (XINT (lines) >= 0 || IT_CHARPOS (it) > 0)
2096 move_it_by_lines (&it, XINT (lines), 0);
2098 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
2101 if (BUFFERP (old_buffer))
2102 w->buffer = old_buffer;
2104 RETURN_UNGCPRO (make_number (it.vpos));
2109 /* File's initialization. */
2111 void
2112 syms_of_indent ()
2114 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
2115 doc: /* *Indentation can insert tabs if this is non-nil. */);
2116 indent_tabs_mode = 1;
2118 defsubr (&Scurrent_indentation);
2119 defsubr (&Sindent_to);
2120 defsubr (&Scurrent_column);
2121 defsubr (&Smove_to_column);
2122 defsubr (&Svertical_motion);
2123 defsubr (&Scompute_motion);
2126 /* arch-tag: 9adfea44-71f7-4988-8ee3-96da15c502cc
2127 (do not change this comment) */