(frame-parameter) <defsetf>: Make it return the assigned value.
[emacs.git] / src / indent.c
blob9ac4478bf574ec1b4ab1d2fefa2f602c172accc8
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 int c;
1042 EMACS_INT pos_byte = PT_BYTE;
1044 DEC_POS (pos_byte);
1045 c = FETCH_CHAR (pos_byte);
1046 if (c == '\t' && prev_col < goal)
1048 EMACS_INT goal_pt, goal_pt_byte;
1050 /* Insert spaces in front of the tab to reach GOAL. Do this
1051 first so that a marker at the end of the tab gets
1052 adjusted. */
1053 SET_PT_BOTH (PT - 1, PT_BYTE - 1);
1054 Finsert_char (make_number (' '), make_number (goal - prev_col), Qt);
1056 /* Now delete the tab, and indent to COL. */
1057 del_range (PT, PT + 1);
1058 goal_pt = PT;
1059 goal_pt_byte = PT_BYTE;
1060 Findent_to (make_number (col), Qnil);
1061 SET_PT_BOTH (goal_pt, goal_pt_byte);
1063 /* Set the last_known... vars consistently. */
1064 col = goal;
1068 /* If line ends prematurely, add space to the end. */
1069 if (col < goal && EQ (force, Qt))
1070 Findent_to (make_number (col = goal), Qnil);
1072 last_known_column = col;
1073 last_known_column_point = PT;
1074 last_known_column_modified = MODIFF;
1076 return make_number (col);
1079 /* compute_motion: compute buffer posn given screen posn and vice versa */
1081 struct position val_compute_motion;
1083 /* Scan the current buffer forward from offset FROM, pretending that
1084 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1085 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1086 and return the ending buffer position and screen location. If we
1087 can't hit the requested column exactly (because of a tab or other
1088 multi-column character), overshoot.
1090 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
1091 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1092 earlier call to compute_motion. The other common case is that FROMHPOS
1093 is zero and FROM is a position that "belongs" at column zero, but might
1094 be shifted by overlay strings; in this case DID_MOTION should be 0.
1096 WIDTH is the number of columns available to display text;
1097 compute_motion uses this to handle continuation lines and such.
1098 If WIDTH is -1, use width of window's text area adjusted for
1099 continuation glyph when needed.
1101 HSCROLL is the number of columns not being displayed at the left
1102 margin; this is usually taken from a window's hscroll member.
1103 TAB_OFFSET is the number of columns of the first tab that aren't
1104 being displayed, perhaps because of a continuation line or
1105 something.
1107 compute_motion returns a pointer to a struct position. The bufpos
1108 member gives the buffer position at the end of the scan, and hpos
1109 and vpos give its cartesian location. prevhpos is the column at
1110 which the character before bufpos started, and contin is non-zero
1111 if we reached the current line by continuing the previous.
1113 Note that FROMHPOS and TOHPOS should be expressed in real screen
1114 columns, taking HSCROLL and the truncation glyph at the left margin
1115 into account. That is, beginning-of-line moves you to the hpos
1116 -HSCROLL + (HSCROLL > 0).
1118 For example, to find the buffer position of column COL of line LINE
1119 of a certain window, pass the window's starting location as FROM
1120 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1121 Pass the buffer's ZV as TO, to limit the scan to the end of the
1122 visible section of the buffer, and pass LINE and COL as TOVPOS and
1123 TOHPOS.
1125 When displaying in window w, a typical formula for WIDTH is:
1127 window_width - 1
1128 - (has_vertical_scroll_bars
1129 ? WINDOW_CONFIG_SCROLL_BAR_COLS (window)
1130 : (window_width + window_left != frame_cols))
1132 where
1133 window_width is XFASTINT (w->total_cols),
1134 window_left is XFASTINT (w->left_col),
1135 has_vertical_scroll_bars is
1136 WINDOW_HAS_VERTICAL_SCROLL_BAR (window)
1137 and frame_cols = FRAME_COLS (XFRAME (window->frame))
1139 Or you can let window_box_text_cols do this all for you, and write:
1140 window_box_text_cols (w) - 1
1142 The `-1' accounts for the continuation-line backslashes; the rest
1143 accounts for window borders if the window is split horizontally, and
1144 the scroll bars if they are turned on. */
1146 struct position *
1147 compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win)
1148 EMACS_INT from, fromvpos, fromhpos, to, tovpos, tohpos;
1149 int did_motion;
1150 EMACS_INT width;
1151 EMACS_INT hscroll, tab_offset;
1152 struct window *win;
1154 register EMACS_INT hpos = fromhpos;
1155 register EMACS_INT vpos = fromvpos;
1157 register EMACS_INT pos;
1158 EMACS_INT pos_byte;
1159 register int c = 0;
1160 register EMACS_INT tab_width = XFASTINT (current_buffer->tab_width);
1161 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
1162 register struct Lisp_Char_Table *dp = window_display_table (win);
1163 int selective
1164 = (INTEGERP (current_buffer->selective_display)
1165 ? XINT (current_buffer->selective_display)
1166 : !NILP (current_buffer->selective_display) ? -1 : 0);
1167 int selective_rlen
1168 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
1169 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0);
1170 /* The next location where the `invisible' property changes, or an
1171 overlay starts or ends. */
1172 EMACS_INT next_boundary = from;
1174 /* For computing runs of characters with similar widths.
1175 Invariant: width_run_width is zero, or all the characters
1176 from width_run_start to width_run_end have a fixed width of
1177 width_run_width. */
1178 EMACS_INT width_run_start = from;
1179 EMACS_INT width_run_end = from;
1180 EMACS_INT width_run_width = 0;
1181 Lisp_Object *width_table;
1182 Lisp_Object buffer;
1184 /* The next buffer pos where we should consult the width run cache. */
1185 EMACS_INT next_width_run = from;
1186 Lisp_Object window;
1188 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1189 /* If previous char scanned was a wide character,
1190 this is the column where it ended. Otherwise, this is 0. */
1191 EMACS_INT wide_column_end_hpos = 0;
1192 EMACS_INT prev_pos; /* Previous buffer position. */
1193 EMACS_INT prev_pos_byte; /* Previous buffer position. */
1194 EMACS_INT prev_hpos = 0;
1195 EMACS_INT prev_vpos = 0;
1196 EMACS_INT contin_hpos; /* HPOS of last column of continued line. */
1197 EMACS_INT prev_tab_offset; /* Previous tab offset. */
1198 EMACS_INT continuation_glyph_width;
1200 XSETBUFFER (buffer, current_buffer);
1201 XSETWINDOW (window, win);
1203 width_run_cache_on_off ();
1204 if (dp == buffer_display_table ())
1205 width_table = (VECTORP (current_buffer->width_table)
1206 ? XVECTOR (current_buffer->width_table)->contents
1207 : 0);
1208 else
1209 /* If the window has its own display table, we can't use the width
1210 run cache, because that's based on the buffer's display table. */
1211 width_table = 0;
1213 if (tab_width <= 0 || tab_width > 1000)
1214 tab_width = 8;
1216 /* Negative width means use all available text columns. */
1217 if (width < 0)
1219 width = window_box_text_cols (win);
1220 /* We must make room for continuation marks if we don't have fringes. */
1221 #ifdef HAVE_WINDOW_SYSTEM
1222 if (!FRAME_WINDOW_P (XFRAME (win->frame)))
1223 #endif
1224 width -= 1;
1227 continuation_glyph_width = 1;
1228 #ifdef HAVE_WINDOW_SYSTEM
1229 if (FRAME_WINDOW_P (XFRAME (win->frame)))
1230 continuation_glyph_width = 0; /* In the fringe. */
1231 #endif
1233 immediate_quit = 1;
1234 QUIT;
1236 pos = prev_pos = from;
1237 pos_byte = prev_pos_byte = CHAR_TO_BYTE (from);
1238 contin_hpos = 0;
1239 prev_tab_offset = tab_offset;
1240 while (1)
1242 while (pos == next_boundary)
1244 EMACS_INT pos_here = pos;
1245 EMACS_INT newpos;
1247 /* Don't skip invisible if we are already at the margin. */
1248 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1250 if (contin_hpos && prev_hpos == 0
1251 && hpos > tohpos
1252 && (contin_hpos == width || wide_column_end_hpos > width))
1253 { /* Line breaks because we can't put the character at the
1254 previous line any more. It is not the multi-column
1255 character continued in middle. Go back to previous
1256 buffer position, screen position, and set tab offset
1257 to previous value. It's the beginning of the
1258 line. */
1259 pos = prev_pos;
1260 pos_byte = prev_pos_byte;
1261 hpos = prev_hpos;
1262 vpos = prev_vpos;
1263 tab_offset = prev_tab_offset;
1265 break;
1268 /* If the caller says that the screen position came from an earlier
1269 call to compute_motion, then we've already accounted for the
1270 overlay strings at point. This is only true the first time
1271 through, so clear the flag after testing it. */
1272 if (!did_motion)
1273 /* We need to skip past the overlay strings. Currently those
1274 strings must not contain TAB;
1275 if we want to relax that restriction, something will have
1276 to be changed here. */
1278 unsigned char *ovstr;
1279 int ovlen = overlay_strings (pos, win, &ovstr);
1280 hpos += ((multibyte && ovlen > 0)
1281 ? strwidth (ovstr, ovlen) : ovlen);
1283 did_motion = 0;
1285 if (pos >= to)
1286 break;
1288 /* Advance POS past invisible characters
1289 (but not necessarily all that there are here),
1290 and store in next_boundary the next position where
1291 we need to call skip_invisible. */
1292 newpos = skip_invisible (pos, &next_boundary, to, window);
1294 if (newpos >= to)
1296 pos = min (to, newpos);
1297 pos_byte = CHAR_TO_BYTE (pos);
1298 goto after_loop;
1301 if (newpos != pos_here)
1303 pos = newpos;
1304 pos_byte = CHAR_TO_BYTE (pos);
1308 /* Handle right margin. */
1309 /* Note on a wide-column character.
1311 Characters are classified into the following three categories
1312 according to the width (columns occupied on screen).
1314 (1) single-column character: ex. `a'
1315 (2) multi-column character: ex. `^A', TAB, `\033'
1316 (3) wide-column character: ex. Japanese character, Chinese character
1317 (In the following example, `W_' stands for them.)
1319 Multi-column characters can be divided around the right margin,
1320 but wide-column characters cannot.
1322 NOTE:
1324 (*) The cursor is placed on the next character after the point.
1326 ----------
1327 abcdefghi\
1328 j ^---- next after the point
1329 ^--- next char. after the point.
1330 ----------
1331 In case of sigle-column character
1333 ----------
1334 abcdefgh\\
1335 033 ^---- next after the point, next char. after the point.
1336 ----------
1337 In case of multi-column character
1339 ----------
1340 abcdefgh\\
1341 W_ ^---- next after the point
1342 ^---- next char. after the point.
1343 ----------
1344 In case of wide-column character
1346 The problem here is continuation at a wide-column character.
1347 In this case, the line may shorter less than WIDTH.
1348 And we find the continuation AFTER it occurs.
1352 if (hpos > width)
1354 if (hscroll
1355 || (truncate_partial_width_windows
1356 && ((width + continuation_glyph_width)
1357 < FRAME_COLS (XFRAME (WINDOW_FRAME (win)))))
1358 || !NILP (current_buffer->truncate_lines))
1360 /* Truncating: skip to newline, unless we are already past
1361 TO (we need to go back below). */
1362 if (pos <= to)
1364 pos = find_before_next_newline (pos, to, 1);
1365 pos_byte = CHAR_TO_BYTE (pos);
1366 hpos = width;
1367 /* If we just skipped next_boundary,
1368 loop around in the main while
1369 and handle it. */
1370 if (pos >= next_boundary)
1371 next_boundary = pos + 1;
1372 prev_hpos = width;
1373 prev_vpos = vpos;
1374 prev_tab_offset = tab_offset;
1377 else
1379 /* Continuing. */
1380 /* Remember the previous value. */
1381 prev_tab_offset = tab_offset;
1383 if (wide_column_end_hpos > width)
1385 hpos -= prev_hpos;
1386 tab_offset += prev_hpos;
1388 else
1390 tab_offset += width;
1391 hpos -= width;
1393 vpos++;
1394 contin_hpos = prev_hpos;
1395 prev_hpos = 0;
1396 prev_vpos = vpos;
1400 /* Stop if past the target buffer position or screen position. */
1401 if (pos > to)
1403 /* Go back to the previous position. */
1404 pos = prev_pos;
1405 pos_byte = prev_pos_byte;
1406 hpos = prev_hpos;
1407 vpos = prev_vpos;
1408 tab_offset = prev_tab_offset;
1410 /* NOTE on contin_hpos, hpos, and prev_hpos.
1412 ----------
1413 abcdefgh\\
1414 W_ ^---- contin_hpos
1415 | ^----- hpos
1416 \---- prev_hpos
1417 ----------
1420 if (contin_hpos && prev_hpos == 0
1421 && contin_hpos < width && !wide_column_end_hpos)
1423 /* Line breaking occurs in the middle of multi-column
1424 character. Go back to previous line. */
1425 hpos = contin_hpos;
1426 vpos = vpos - 1;
1428 break;
1431 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1433 if (contin_hpos && prev_hpos == 0
1434 && hpos > tohpos
1435 && (contin_hpos == width || wide_column_end_hpos > width))
1436 { /* Line breaks because we can't put the character at the
1437 previous line any more. It is not the multi-column
1438 character continued in middle. Go back to previous
1439 buffer position, screen position, and set tab offset
1440 to previous value. It's the beginning of the
1441 line. */
1442 pos = prev_pos;
1443 pos_byte = prev_pos_byte;
1444 hpos = prev_hpos;
1445 vpos = prev_vpos;
1446 tab_offset = prev_tab_offset;
1448 break;
1450 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1451 break;
1453 prev_hpos = hpos;
1454 prev_vpos = vpos;
1455 prev_pos = pos;
1456 prev_pos_byte = pos_byte;
1457 wide_column_end_hpos = 0;
1459 /* Consult the width run cache to see if we can avoid inspecting
1460 the text character-by-character. */
1461 if (current_buffer->width_run_cache && pos >= next_width_run)
1463 int run_end;
1464 int common_width
1465 = region_cache_forward (current_buffer,
1466 current_buffer->width_run_cache,
1467 pos, &run_end);
1469 /* A width of zero means the character's width varies (like
1470 a tab), is meaningless (like a newline), or we just don't
1471 want to skip over it for some other reason. */
1472 if (common_width != 0)
1474 int run_end_hpos;
1476 /* Don't go past the final buffer posn the user
1477 requested. */
1478 if (run_end > to)
1479 run_end = to;
1481 run_end_hpos = hpos + (run_end - pos) * common_width;
1483 /* Don't go past the final horizontal position the user
1484 requested. */
1485 if (vpos == tovpos && run_end_hpos > tohpos)
1487 run_end = pos + (tohpos - hpos) / common_width;
1488 run_end_hpos = hpos + (run_end - pos) * common_width;
1491 /* Don't go past the margin. */
1492 if (run_end_hpos >= width)
1494 run_end = pos + (width - hpos) / common_width;
1495 run_end_hpos = hpos + (run_end - pos) * common_width;
1498 hpos = run_end_hpos;
1499 if (run_end > pos)
1500 prev_hpos = hpos - common_width;
1501 if (pos != run_end)
1503 pos = run_end;
1504 pos_byte = CHAR_TO_BYTE (pos);
1508 next_width_run = run_end + 1;
1511 /* We have to scan the text character-by-character. */
1512 else
1514 EMACS_INT i, n;
1515 Lisp_Object charvec;
1517 c = FETCH_BYTE (pos_byte);
1519 /* Check composition sequence. */
1521 int len, len_byte, width;
1523 if (check_composition (pos, pos_byte, to, &len, &len_byte, &width))
1525 pos += len;
1526 pos_byte += len_byte;
1527 hpos += width;
1528 continue;
1532 pos++, pos_byte++;
1534 /* Perhaps add some info to the width_run_cache. */
1535 if (current_buffer->width_run_cache)
1537 /* Is this character part of the current run? If so, extend
1538 the run. */
1539 if (pos - 1 == width_run_end
1540 && XFASTINT (width_table[c]) == width_run_width)
1541 width_run_end = pos;
1543 /* The previous run is over, since this is a character at a
1544 different position, or a different width. */
1545 else
1547 /* Have we accumulated a run to put in the cache?
1548 (Currently, we only cache runs of width == 1). */
1549 if (width_run_start < width_run_end
1550 && width_run_width == 1)
1551 know_region_cache (current_buffer,
1552 current_buffer->width_run_cache,
1553 width_run_start, width_run_end);
1555 /* Start recording a new width run. */
1556 width_run_width = XFASTINT (width_table[c]);
1557 width_run_start = pos - 1;
1558 width_run_end = pos;
1562 if (dp != 0
1563 && ! (multibyte && BASE_LEADING_CODE_P (c))
1564 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1566 charvec = DISP_CHAR_VECTOR (dp, c);
1567 n = ASIZE (charvec);
1569 else
1571 charvec = Qnil;
1572 n = 1;
1575 for (i = n - 1; i >= 0; --i)
1577 if (VECTORP (charvec))
1579 /* This should be handled the same as
1580 next_element_from_display_vector does it. */
1581 Lisp_Object entry = AREF (charvec, i);
1583 if (GLYPH_CODE_P (entry)
1584 && GLYPH_CODE_CHAR_VALID_P (entry))
1585 c = GLYPH_CODE_CHAR (entry);
1586 else
1587 c = ' ';
1590 if (c >= 040 && c < 0177)
1591 hpos++;
1592 else if (c == '\t')
1594 int tem = ((hpos + tab_offset + hscroll - (hscroll > 0))
1595 % tab_width);
1596 if (tem < 0)
1597 tem += tab_width;
1598 hpos += tab_width - tem;
1600 else if (c == '\n')
1602 if (selective > 0
1603 && indented_beyond_p (pos, pos_byte,
1604 (double) selective)) /* iftc */
1606 /* If (pos == to), we don't have to take care of
1607 selective display. */
1608 if (pos < to)
1610 /* Skip any number of invisible lines all at once */
1613 pos = find_before_next_newline (pos, to, 1);
1614 if (pos < to)
1615 pos++;
1616 pos_byte = CHAR_TO_BYTE (pos);
1618 while (pos < to
1619 && indented_beyond_p (pos, pos_byte,
1620 (double) selective)); /* iftc */
1621 /* Allow for the " ..." that is displayed for them. */
1622 if (selective_rlen)
1624 hpos += selective_rlen;
1625 if (hpos >= width)
1626 hpos = width;
1628 DEC_BOTH (pos, pos_byte);
1629 /* We have skipped the invis text, but not the
1630 newline after. */
1633 else
1635 /* A visible line. */
1636 vpos++;
1637 hpos = 0;
1638 hpos -= hscroll;
1639 /* Count the truncation glyph on column 0 */
1640 if (hscroll > 0)
1641 hpos += continuation_glyph_width;
1642 tab_offset = 0;
1644 contin_hpos = 0;
1646 else if (c == CR && selective < 0)
1648 /* In selective display mode,
1649 everything from a ^M to the end of the line is invisible.
1650 Stop *before* the real newline. */
1651 if (pos < to)
1653 pos = find_before_next_newline (pos, to, 1);
1654 pos_byte = CHAR_TO_BYTE (pos);
1656 /* If we just skipped next_boundary,
1657 loop around in the main while
1658 and handle it. */
1659 if (pos > next_boundary)
1660 next_boundary = pos;
1661 /* Allow for the " ..." that is displayed for them. */
1662 if (selective_rlen)
1664 hpos += selective_rlen;
1665 if (hpos >= width)
1666 hpos = width;
1669 else if (multibyte && BASE_LEADING_CODE_P (c))
1671 /* Start of multi-byte form. */
1672 unsigned char *ptr;
1673 int bytes, width, wide_column;
1675 pos_byte--; /* rewind POS_BYTE */
1676 ptr = BYTE_POS_ADDR (pos_byte);
1677 MULTIBYTE_BYTES_WIDTH (ptr, dp);
1678 pos_byte += bytes;
1679 if (wide_column)
1680 wide_column_end_hpos = hpos + wide_column;
1681 hpos += width;
1683 else if (VECTORP (charvec))
1684 ++hpos;
1685 else
1686 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1691 after_loop:
1693 /* Remember any final width run in the cache. */
1694 if (current_buffer->width_run_cache
1695 && width_run_width == 1
1696 && width_run_start < width_run_end)
1697 know_region_cache (current_buffer, current_buffer->width_run_cache,
1698 width_run_start, width_run_end);
1700 val_compute_motion.bufpos = pos;
1701 val_compute_motion.bytepos = pos_byte;
1702 val_compute_motion.hpos = hpos;
1703 val_compute_motion.vpos = vpos;
1704 if (contin_hpos && prev_hpos == 0)
1705 val_compute_motion.prevhpos = contin_hpos;
1706 else
1707 val_compute_motion.prevhpos = prev_hpos;
1708 /* We alalways handle all of them here; none of them remain to do. */
1709 val_compute_motion.ovstring_chars_done = 0;
1711 /* Nonzero if have just continued a line */
1712 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1714 immediate_quit = 0;
1715 return &val_compute_motion;
1719 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1720 doc: /* Scan through the current buffer, calculating screen position.
1721 Scan the current buffer forward from offset FROM,
1722 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1723 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1724 and return the ending buffer position and screen location.
1726 If TOPOS is nil, the actual width and height of the window's
1727 text area are used.
1729 There are three additional arguments:
1731 WIDTH is the number of columns available to display text;
1732 this affects handling of continuation lines. A value of nil
1733 corresponds to the actual number of available text columns.
1735 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1736 HSCROLL is the number of columns not being displayed at the left
1737 margin; this is usually taken from a window's hscroll member.
1738 TAB-OFFSET is the number of columns of the first tab that aren't
1739 being displayed, perhaps because the line was continued within it.
1740 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1742 WINDOW is the window to operate on. It is used to choose the display table;
1743 if it is showing the current buffer, it is used also for
1744 deciding which overlay properties apply.
1745 Note that `compute-motion' always operates on the current buffer.
1747 The value is a list of five elements:
1748 (POS HPOS VPOS PREVHPOS CONTIN)
1749 POS is the buffer position where the scan stopped.
1750 VPOS is the vertical position where the scan stopped.
1751 HPOS is the horizontal position where the scan stopped.
1753 PREVHPOS is the horizontal position one character back from POS.
1754 CONTIN is t if a line was continued after (or within) the previous character.
1756 For example, to find the buffer position of column COL of line LINE
1757 of a certain window, pass the window's starting location as FROM
1758 and the window's upper-left coordinates as FROMPOS.
1759 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1760 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1761 (from, frompos, to, topos, width, offsets, window)
1762 Lisp_Object from, frompos, to, topos;
1763 Lisp_Object width, offsets, window;
1765 struct window *w;
1766 Lisp_Object bufpos, hpos, vpos, prevhpos;
1767 struct position *pos;
1768 int hscroll, tab_offset;
1770 CHECK_NUMBER_COERCE_MARKER (from);
1771 CHECK_CONS (frompos);
1772 CHECK_NUMBER_CAR (frompos);
1773 CHECK_NUMBER_CDR (frompos);
1774 CHECK_NUMBER_COERCE_MARKER (to);
1775 if (!NILP (topos))
1777 CHECK_CONS (topos);
1778 CHECK_NUMBER_CAR (topos);
1779 CHECK_NUMBER_CDR (topos);
1781 if (!NILP (width))
1782 CHECK_NUMBER (width);
1784 if (!NILP (offsets))
1786 CHECK_CONS (offsets);
1787 CHECK_NUMBER_CAR (offsets);
1788 CHECK_NUMBER_CDR (offsets);
1789 hscroll = XINT (XCAR (offsets));
1790 tab_offset = XINT (XCDR (offsets));
1792 else
1793 hscroll = tab_offset = 0;
1795 if (NILP (window))
1796 window = Fselected_window ();
1797 else
1798 CHECK_LIVE_WINDOW (window);
1799 w = XWINDOW (window);
1801 if (XINT (from) < BEGV || XINT (from) > ZV)
1802 args_out_of_range_3 (from, make_number (BEGV), make_number (ZV));
1803 if (XINT (to) < BEGV || XINT (to) > ZV)
1804 args_out_of_range_3 (to, make_number (BEGV), make_number (ZV));
1806 pos = compute_motion (XINT (from), XINT (XCDR (frompos)),
1807 XINT (XCAR (frompos)), 0,
1808 XINT (to),
1809 (NILP (topos)
1810 ? window_internal_height (w)
1811 : XINT (XCDR (topos))),
1812 (NILP (topos)
1813 ? (window_box_text_cols (w)
1815 #ifdef HAVE_WINDOW_SYSTEM
1816 FRAME_WINDOW_P (XFRAME (w->frame)) ? 0 :
1817 #endif
1819 : XINT (XCAR (topos))),
1820 (NILP (width) ? -1 : XINT (width)),
1821 hscroll, tab_offset,
1822 XWINDOW (window));
1824 XSETFASTINT (bufpos, pos->bufpos);
1825 XSETINT (hpos, pos->hpos);
1826 XSETINT (vpos, pos->vpos);
1827 XSETINT (prevhpos, pos->prevhpos);
1829 return Fcons (bufpos,
1830 Fcons (hpos,
1831 Fcons (vpos,
1832 Fcons (prevhpos,
1833 Fcons (pos->contin ? Qt : Qnil, Qnil)))));
1837 /* Fvertical_motion and vmotion */
1839 struct position val_vmotion;
1841 struct position *
1842 vmotion (from, vtarget, w)
1843 register EMACS_INT from, vtarget;
1844 struct window *w;
1846 EMACS_INT hscroll = XINT (w->hscroll);
1847 struct position pos;
1848 /* vpos is cumulative vertical position, changed as from is changed */
1849 register int vpos = 0;
1850 EMACS_INT prevline;
1851 register EMACS_INT first;
1852 EMACS_INT from_byte;
1853 EMACS_INT lmargin = hscroll > 0 ? 1 - hscroll : 0;
1854 int selective
1855 = (INTEGERP (current_buffer->selective_display)
1856 ? XINT (current_buffer->selective_display)
1857 : !NILP (current_buffer->selective_display) ? -1 : 0);
1858 Lisp_Object window;
1859 EMACS_INT start_hpos = 0;
1860 int did_motion;
1861 /* This is the object we use for fetching character properties. */
1862 Lisp_Object text_prop_object;
1864 XSETWINDOW (window, w);
1866 /* If the window contains this buffer, use it for getting text properties.
1867 Otherwise use the current buffer as arg for doing that. */
1868 if (EQ (w->buffer, Fcurrent_buffer ()))
1869 text_prop_object = window;
1870 else
1871 text_prop_object = Fcurrent_buffer ();
1873 if (vpos >= vtarget)
1875 /* To move upward, go a line at a time until
1876 we have gone at least far enough. */
1878 first = 1;
1880 while ((vpos > vtarget || first) && from > BEGV)
1882 Lisp_Object propval;
1884 prevline = find_next_newline_no_quit (from - 1, -1);
1885 while (prevline > BEGV
1886 && ((selective > 0
1887 && indented_beyond_p (prevline,
1888 CHAR_TO_BYTE (prevline),
1889 (double) selective)) /* iftc */
1890 /* Watch out for newlines with `invisible' property.
1891 When moving upward, check the newline before. */
1892 || (propval = Fget_char_property (make_number (prevline - 1),
1893 Qinvisible,
1894 text_prop_object),
1895 TEXT_PROP_MEANS_INVISIBLE (propval))))
1896 prevline = find_next_newline_no_quit (prevline - 1, -1);
1897 pos = *compute_motion (prevline, 0,
1898 lmargin + (prevline == BEG ? start_hpos : 0),
1900 from,
1901 /* Don't care for VPOS... */
1902 1 << (BITS_PER_SHORT - 1),
1903 /* ... nor HPOS. */
1904 1 << (BITS_PER_SHORT - 1),
1905 -1, hscroll,
1906 /* This compensates for start_hpos
1907 so that a tab as first character
1908 still occupies 8 columns. */
1909 (prevline == BEG ? -start_hpos : 0),
1911 vpos -= pos.vpos;
1912 first = 0;
1913 from = prevline;
1916 /* If we made exactly the desired vertical distance,
1917 or if we hit beginning of buffer,
1918 return point found */
1919 if (vpos >= vtarget)
1921 val_vmotion.bufpos = from;
1922 val_vmotion.bytepos = CHAR_TO_BYTE (from);
1923 val_vmotion.vpos = vpos;
1924 val_vmotion.hpos = lmargin;
1925 val_vmotion.contin = 0;
1926 val_vmotion.prevhpos = 0;
1927 val_vmotion.ovstring_chars_done = 0;
1928 val_vmotion.tab_offset = 0; /* For accumulating tab offset. */
1929 return &val_vmotion;
1932 /* Otherwise find the correct spot by moving down */
1934 /* Moving downward is simple, but must calculate from beg of line
1935 to determine hpos of starting point */
1936 from_byte = CHAR_TO_BYTE (from);
1937 if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n')
1939 Lisp_Object propval;
1941 prevline = find_next_newline_no_quit (from, -1);
1942 while (prevline > BEGV
1943 && ((selective > 0
1944 && indented_beyond_p (prevline,
1945 CHAR_TO_BYTE (prevline),
1946 (double) selective)) /* iftc */
1947 /* Watch out for newlines with `invisible' property.
1948 When moving downward, check the newline after. */
1949 || (propval = Fget_char_property (make_number (prevline),
1950 Qinvisible,
1951 text_prop_object),
1952 TEXT_PROP_MEANS_INVISIBLE (propval))))
1953 prevline = find_next_newline_no_quit (prevline - 1, -1);
1954 pos = *compute_motion (prevline, 0,
1955 lmargin + (prevline == BEG
1956 ? start_hpos : 0),
1958 from,
1959 /* Don't care for VPOS... */
1960 1 << (BITS_PER_SHORT - 1),
1961 /* ... nor HPOS. */
1962 1 << (BITS_PER_SHORT - 1),
1963 -1, hscroll,
1964 (prevline == BEG ? -start_hpos : 0),
1966 did_motion = 1;
1968 else
1970 pos.hpos = lmargin + (from == BEG ? start_hpos : 0);
1971 pos.vpos = 0;
1972 pos.tab_offset = 0;
1973 did_motion = 0;
1975 return compute_motion (from, vpos, pos.hpos, did_motion,
1976 ZV, vtarget, - (1 << (BITS_PER_SHORT - 1)),
1977 -1, hscroll,
1978 pos.tab_offset - (from == BEG ? start_hpos : 0),
1982 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
1983 doc: /* Move point to start of the screen line LINES lines down.
1984 If LINES is negative, this means moving up.
1986 This function is an ordinary cursor motion function
1987 which calculates the new position based on how text would be displayed.
1988 The new position may be the start of a line,
1989 or just the start of a continuation line.
1990 The function returns number of screen lines moved over;
1991 that usually equals LINES, but may be closer to zero
1992 if beginning or end of buffer was reached.
1994 The optional second argument WINDOW specifies the window to use for
1995 parameters such as width, horizontal scrolling, and so on.
1996 The default is to use the selected window's parameters.
1998 `vertical-motion' always uses the current buffer,
1999 regardless of which buffer is displayed in WINDOW.
2000 This is consistent with other cursor motion functions
2001 and makes it possible to use `vertical-motion' in any buffer,
2002 whether or not it is currently displayed in some window. */)
2003 (lines, window)
2004 Lisp_Object lines, window;
2006 struct it it;
2007 struct text_pos pt;
2008 struct window *w;
2009 Lisp_Object old_buffer;
2010 struct gcpro gcpro1;
2012 CHECK_NUMBER (lines);
2013 if (! NILP (window))
2014 CHECK_WINDOW (window);
2015 else
2016 window = selected_window;
2017 w = XWINDOW (window);
2019 old_buffer = Qnil;
2020 GCPRO1 (old_buffer);
2021 if (XBUFFER (w->buffer) != current_buffer)
2023 /* Set the window's buffer temporarily to the current buffer. */
2024 old_buffer = w->buffer;
2025 XSETBUFFER (w->buffer, current_buffer);
2028 if (noninteractive)
2030 struct position pos;
2031 pos = *vmotion (PT, XINT (lines), w);
2032 SET_PT_BOTH (pos.bufpos, pos.bytepos);
2034 else
2036 int it_start;
2037 int oselective;
2038 int it_overshoot_expected;
2040 SET_TEXT_POS (pt, PT, PT_BYTE);
2041 start_display (&it, w, pt);
2043 /* Scan from the start of the line containing PT. If we don't
2044 do this, we start moving with IT->current_x == 0, while PT is
2045 really at some x > 0. The effect is, in continuation lines, that
2046 we end up with the iterator placed at where it thinks X is 0,
2047 while the end position is really at some X > 0, the same X that
2048 PT had. */
2049 it_start = IT_CHARPOS (it);
2051 /* We expect the call to move_it_to, further down, to overshoot
2052 if the starting point is on an image, stretch glyph,
2053 composition, or Lisp string. We won't need to backtrack in
2054 this situation, except for one corner case: when the Lisp
2055 string contains a newline. */
2056 if (it.method == GET_FROM_STRING)
2058 const char *s = SDATA (it.string);
2059 const char *e = s + SBYTES (it.string);
2061 while (s < e && *s != '\n')
2062 ++s;
2064 /* If there is no newline in the string, we need to check
2065 whether there is a newline immediately after the string
2066 in move_it_to below. This may happen if there is an
2067 overlay with an after-string just before the newline. */
2068 it_overshoot_expected = (s == e) ? -1 : 0;
2070 else
2071 it_overshoot_expected = (it.method == GET_FROM_IMAGE
2072 || it.method == GET_FROM_STRETCH
2073 || it.method == GET_FROM_COMPOSITION);
2075 reseat_at_previous_visible_line_start (&it);
2076 it.current_x = it.hpos = 0;
2077 /* Temporarily disable selective display so we don't move too far */
2078 oselective = it.selective;
2079 it.selective = 0;
2080 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
2081 it.selective = oselective;
2083 /* Move back if we got too far. This may happen if
2084 truncate-lines is on and PT is beyond right margin.
2085 Don't go back if the overshoot is expected (see above). */
2086 if (IT_CHARPOS (it) > it_start && XINT (lines) > 0
2087 && (!it_overshoot_expected
2088 || (it_overshoot_expected < 0
2089 && it.method == GET_FROM_BUFFER
2090 && it.c == '\n')))
2091 move_it_by_lines (&it, -1, 0);
2093 it.vpos = 0;
2094 /* Do this even if LINES is 0, so that we move back
2095 to the beginning of the current line as we ought. */
2096 if (XINT (lines) >= 0 || IT_CHARPOS (it) > 0)
2097 move_it_by_lines (&it, XINT (lines), 0);
2099 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
2102 if (BUFFERP (old_buffer))
2103 w->buffer = old_buffer;
2105 RETURN_UNGCPRO (make_number (it.vpos));
2110 /* File's initialization. */
2112 void
2113 syms_of_indent ()
2115 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
2116 doc: /* *Indentation can insert tabs if this is non-nil. */);
2117 indent_tabs_mode = 1;
2119 defsubr (&Scurrent_indentation);
2120 defsubr (&Sindent_to);
2121 defsubr (&Scurrent_column);
2122 defsubr (&Smove_to_column);
2123 defsubr (&Svertical_motion);
2124 defsubr (&Scompute_motion);
2127 /* arch-tag: 9adfea44-71f7-4988-8ee3-96da15c502cc
2128 (do not change this comment) */