Add 2008 to copyright years.
[bpt/emacs.git] / src / indent.c
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.
5
6 This file is part of GNU Emacs.
7
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.
12
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.
17
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. */
22
23 #include <config.h>
24 #include "lisp.h"
25 #include "buffer.h"
26 #include "charset.h"
27 #include "category.h"
28 #include "indent.h"
29 #include "keyboard.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "termchar.h"
33 #include "termopts.h"
34 #include "disptab.h"
35 #include "intervals.h"
36 #include "region-cache.h"
37
38 /* Indentation can insert tabs if this is non-zero;
39 otherwise always uses spaces. */
40
41 int indent_tabs_mode;
42
43 #define CR 015
44
45 /* These three values memorize the current column to avoid recalculation. */
46
47 /* Last value returned by current_column.
48 Some things in set last_known_column_point to -1
49 to mark the memorized value as invalid. */
50
51 double last_known_column;
52
53 /* Value of point when current_column was called. */
54
55 int last_known_column_point;
56
57 /* Value of MODIFF when current_column was called. */
58
59 int last_known_column_modified;
60
61 static double current_column_1 P_ ((void));
62 static double position_indentation P_ ((int));
63
64 /* Cache of beginning of line found by the last call of
65 current_column. */
66
67 int current_column_bol_cache;
68
69 /* Get the display table to use for the current buffer. */
70
71 struct Lisp_Char_Table *
72 buffer_display_table ()
73 {
74 Lisp_Object thisbuf;
75
76 thisbuf = current_buffer->display_table;
77 if (DISP_TABLE_P (thisbuf))
78 return XCHAR_TABLE (thisbuf);
79 if (DISP_TABLE_P (Vstandard_display_table))
80 return XCHAR_TABLE (Vstandard_display_table);
81 return 0;
82 }
83 \f
84 /* Width run cache considerations. */
85
86 /* Return the width of character C under display table DP. */
87
88 static int
89 character_width (c, dp)
90 int c;
91 struct Lisp_Char_Table *dp;
92 {
93 Lisp_Object elt;
94
95 /* These width computations were determined by examining the cases
96 in display_text_line. */
97
98 /* Everything can be handled by the display table, if it's
99 present and the element is right. */
100 if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
101 return XVECTOR (elt)->size;
102
103 /* Some characters are special. */
104 if (c == '\n' || c == '\t' || c == '\015')
105 return 0;
106
107 /* Printing characters have width 1. */
108 else if (c >= 040 && c < 0177)
109 return 1;
110
111 /* Everybody else (control characters, metacharacters) has other
112 widths. We could return their actual widths here, but they
113 depend on things like ctl_arrow and crud like that, and they're
114 not very common at all. So we'll just claim we don't know their
115 widths. */
116 else
117 return 0;
118 }
119
120 /* Return true if the display table DISPTAB specifies the same widths
121 for characters as WIDTHTAB. We use this to decide when to
122 invalidate the buffer's width_run_cache. */
123
124 int
125 disptab_matches_widthtab (disptab, widthtab)
126 struct Lisp_Char_Table *disptab;
127 struct Lisp_Vector *widthtab;
128 {
129 int i;
130
131 if (widthtab->size != 256)
132 abort ();
133
134 for (i = 0; i < 256; i++)
135 if (character_width (i, disptab)
136 != XFASTINT (widthtab->contents[i]))
137 return 0;
138
139 return 1;
140 }
141
142 /* Recompute BUF's width table, using the display table DISPTAB. */
143
144 void
145 recompute_width_table (buf, disptab)
146 struct buffer *buf;
147 struct Lisp_Char_Table *disptab;
148 {
149 int i;
150 struct Lisp_Vector *widthtab;
151
152 if (!VECTORP (buf->width_table))
153 buf->width_table = Fmake_vector (make_number (256), make_number (0));
154 widthtab = XVECTOR (buf->width_table);
155 if (widthtab->size != 256)
156 abort ();
157
158 for (i = 0; i < 256; i++)
159 XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
160 }
161
162 /* Allocate or free the width run cache, as requested by the current
163 state of current_buffer's cache_long_line_scans variable. */
164
165 static void
166 width_run_cache_on_off ()
167 {
168 if (NILP (current_buffer->cache_long_line_scans)
169 /* And, for the moment, this feature doesn't work on multibyte
170 characters. */
171 || !NILP (current_buffer->enable_multibyte_characters))
172 {
173 /* It should be off. */
174 if (current_buffer->width_run_cache)
175 {
176 free_region_cache (current_buffer->width_run_cache);
177 current_buffer->width_run_cache = 0;
178 current_buffer->width_table = Qnil;
179 }
180 }
181 else
182 {
183 /* It should be on. */
184 if (current_buffer->width_run_cache == 0)
185 {
186 current_buffer->width_run_cache = new_region_cache ();
187 recompute_width_table (current_buffer, buffer_display_table ());
188 }
189 }
190 }
191
192 \f
193 /* Skip some invisible characters starting from POS.
194 This includes characters invisible because of text properties
195 and characters invisible because of overlays.
196
197 If position POS is followed by invisible characters,
198 skip some of them and return the position after them.
199 Otherwise return POS itself.
200
201 Set *NEXT_BOUNDARY_P to the next position at which
202 it will be necessary to call this function again.
203
204 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
205 to a value greater than TO.
206
207 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
208 take account of overlays that apply only in WINDOW.
209
210 We don't necessarily skip all the invisible characters after POS
211 because that could take a long time. We skip a reasonable number
212 which can be skipped quickly. If there might be more invisible
213 characters immediately following, then *NEXT_BOUNDARY_P
214 will equal the return value. */
215
216 int
217 skip_invisible (pos, next_boundary_p, to, window)
218 int pos;
219 int *next_boundary_p;
220 int to;
221 Lisp_Object window;
222 {
223 Lisp_Object prop, position, overlay_limit, proplimit;
224 Lisp_Object buffer, tmp;
225 int end, inv_p;
226
227 XSETFASTINT (position, pos);
228 XSETBUFFER (buffer, current_buffer);
229
230 /* Give faster response for overlay lookup near POS. */
231 recenter_overlay_lists (current_buffer, pos);
232
233 /* We must not advance farther than the next overlay change.
234 The overlay change might change the invisible property;
235 or there might be overlay strings to be displayed there. */
236 overlay_limit = Fnext_overlay_change (position);
237 /* As for text properties, this gives a lower bound
238 for where the invisible text property could change. */
239 proplimit = Fnext_property_change (position, buffer, Qt);
240 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
241 proplimit = overlay_limit;
242 /* PROPLIMIT is now a lower bound for the next change
243 in invisible status. If that is plenty far away,
244 use that lower bound. */
245 if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
246 *next_boundary_p = XFASTINT (proplimit);
247 /* Otherwise, scan for the next `invisible' property change. */
248 else
249 {
250 /* Don't scan terribly far. */
251 XSETFASTINT (proplimit, min (pos + 100, to));
252 /* No matter what. don't go past next overlay change. */
253 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
254 proplimit = overlay_limit;
255 tmp = Fnext_single_property_change (position, Qinvisible,
256 buffer, proplimit);
257 end = XFASTINT (tmp);
258 #if 0
259 /* Don't put the boundary in the middle of multibyte form if
260 there is no actual property change. */
261 if (end == pos + 100
262 && !NILP (current_buffer->enable_multibyte_characters)
263 && end < ZV)
264 while (pos < end && !CHAR_HEAD_P (POS_ADDR (end)))
265 end--;
266 #endif
267 *next_boundary_p = end;
268 }
269 /* if the `invisible' property is set, we can skip to
270 the next property change */
271 prop = Fget_char_property (position, Qinvisible,
272 (!NILP (window)
273 && EQ (XWINDOW (window)->buffer, buffer))
274 ? window : buffer);
275 inv_p = TEXT_PROP_MEANS_INVISIBLE (prop);
276 /* When counting columns (window == nil), don't skip over ellipsis text. */
277 if (NILP (window) ? inv_p == 1 : inv_p)
278 return *next_boundary_p;
279 return pos;
280 }
281 \f
282 /* If a composition starts at POS/POS_BYTE and it doesn't stride over
283 POINT, set *LEN / *LEN_BYTE to the character and byte lengths, *WIDTH
284 to the width, and return 1. Otherwise, return 0. */
285
286 static int
287 check_composition (pos, pos_byte, point, len, len_byte, width)
288 int pos, pos_byte, point;
289 int *len, *len_byte, *width;
290 {
291 Lisp_Object prop;
292 int start, end;
293 int id;
294
295 if (! find_composition (pos, -1, &start, &end, &prop, Qnil)
296 || pos != start || point < end
297 || !COMPOSITION_VALID_P (start, end, prop))
298 return 0;
299 if ((id = get_composition_id (pos, pos_byte, end - pos, prop, Qnil)) < 0)
300 return 0;
301
302 *len = COMPOSITION_LENGTH (prop);
303 *len_byte = CHAR_TO_BYTE (end) - pos_byte;
304 *width = composition_table[id]->width;
305 return 1;
306 }
307 \f
308 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
309
310 DP is a display table or NULL.
311
312 This macro is used in current_column_1, Fmove_to_column, and
313 compute_motion. */
314
315 #define MULTIBYTE_BYTES_WIDTH(p, dp) \
316 do { \
317 int c; \
318 \
319 wide_column = 0; \
320 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, bytes); \
321 if (BYTES_BY_CHAR_HEAD (*p) != bytes) \
322 width = bytes * 4; \
323 else \
324 { \
325 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))) \
326 width = XVECTOR (DISP_CHAR_VECTOR (dp, c))->size; \
327 else \
328 width = WIDTH_BY_CHAR_HEAD (*p); \
329 if (width > 1) \
330 wide_column = width; \
331 } \
332 } while (0)
333
334
335 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
336 doc: /* Return the horizontal position of point. Beginning of line is column 0.
337 This is calculated by adding together the widths of all the displayed
338 representations of the character between the start of the previous line
339 and point (eg. control characters will have a width of 2 or 4, tabs
340 will have a variable width).
341 Ignores finite width of frame, which means that this function may return
342 values greater than (frame-width).
343 Whether the line is visible (if `selective-display' is t) has no effect;
344 however, ^M is treated as end of line when `selective-display' is t.
345 Text that has an invisible property is considered as having width 0, unless
346 `buffer-invisibility-spec' specifies that it is replaced by an ellipsis. */)
347 ()
348 {
349 Lisp_Object temp;
350 XSETFASTINT (temp, (int) current_column ()); /* iftc */
351 return temp;
352 }
353
354 /* Cancel any recorded value of the horizontal position. */
355
356 void
357 invalidate_current_column ()
358 {
359 last_known_column_point = 0;
360 }
361
362 double
363 current_column ()
364 {
365 register int col;
366 register unsigned char *ptr, *stop;
367 register int tab_seen;
368 int post_tab;
369 register int c;
370 register int tab_width = XINT (current_buffer->tab_width);
371 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
372 register struct Lisp_Char_Table *dp = buffer_display_table ();
373
374 if (PT == last_known_column_point
375 && MODIFF == last_known_column_modified)
376 return last_known_column;
377
378 /* If the buffer has overlays, text properties,
379 or multibyte characters, use a more general algorithm. */
380 if (BUF_INTERVALS (current_buffer)
381 || current_buffer->overlays_before
382 || current_buffer->overlays_after
383 || Z != Z_BYTE)
384 return current_column_1 ();
385
386 /* Scan backwards from point to the previous newline,
387 counting width. Tab characters are the only complicated case. */
388
389 /* Make a pointer for decrementing through the chars before point. */
390 ptr = BYTE_POS_ADDR (PT_BYTE - 1) + 1;
391 /* Make a pointer to where consecutive chars leave off,
392 going backwards from point. */
393 if (PT == BEGV)
394 stop = ptr;
395 else if (PT <= GPT || BEGV > GPT)
396 stop = BEGV_ADDR;
397 else
398 stop = GAP_END_ADDR;
399
400 if (tab_width <= 0 || tab_width > 1000)
401 tab_width = 8;
402
403 col = 0, tab_seen = 0, post_tab = 0;
404
405 while (1)
406 {
407 EMACS_INT i, n;
408 Lisp_Object charvec;
409
410 if (ptr == stop)
411 {
412 /* We stopped either for the beginning of the buffer
413 or for the gap. */
414 if (ptr == BEGV_ADDR)
415 break;
416
417 /* It was the gap. Jump back over it. */
418 stop = BEGV_ADDR;
419 ptr = GPT_ADDR;
420
421 /* Check whether that brings us to beginning of buffer. */
422 if (BEGV >= GPT)
423 break;
424 }
425
426 c = *--ptr;
427
428 if (dp && VECTORP (DISP_CHAR_VECTOR (dp, c)))
429 {
430 charvec = DISP_CHAR_VECTOR (dp, c);
431 n = ASIZE (charvec);
432 }
433 else
434 {
435 charvec = Qnil;
436 n = 1;
437 }
438
439 for (i = n - 1; i >= 0; --i)
440 {
441 if (VECTORP (charvec))
442 {
443 /* This should be handled the same as
444 next_element_from_display_vector does it. */
445 Lisp_Object entry = AREF (charvec, i);
446
447 if (INTEGERP (entry)
448 && GLYPH_CHAR_VALID_P (XFASTINT (entry)))
449 c = FAST_GLYPH_CHAR (XFASTINT (entry));
450 else
451 c = ' ';
452 }
453
454 if (c >= 040 && c < 0177)
455 col++;
456 else if (c == '\n'
457 || (c == '\r'
458 && EQ (current_buffer->selective_display, Qt)))
459 {
460 ptr++;
461 goto start_of_line_found;
462 }
463 else if (c == '\t')
464 {
465 if (tab_seen)
466 col = ((col + tab_width) / tab_width) * tab_width;
467
468 post_tab += col;
469 col = 0;
470 tab_seen = 1;
471 }
472 else if (VECTORP (charvec))
473 /* With a display table entry, C is displayed as is, and
474 not displayed as \NNN or as ^N. If C is a single-byte
475 character, it takes one column. If C is multi-byte in
476 an unibyte buffer, it's translated to unibyte, so it
477 also takes one column. */
478 ++col;
479 else
480 col += (ctl_arrow && c < 0200) ? 2 : 4;
481 }
482 }
483
484 start_of_line_found:
485
486 if (tab_seen)
487 {
488 col = ((col + tab_width) / tab_width) * tab_width;
489 col += post_tab;
490 }
491
492 if (ptr == BEGV_ADDR)
493 current_column_bol_cache = BEGV;
494 else
495 current_column_bol_cache = BYTE_TO_CHAR (PTR_BYTE_POS (ptr));
496
497 last_known_column = col;
498 last_known_column_point = PT;
499 last_known_column_modified = MODIFF;
500
501 return col;
502 }
503 \f
504 /* Return the column number of position POS
505 by scanning forward from the beginning of the line.
506 This function handles characters that are invisible
507 due to text properties or overlays. */
508
509 static double
510 current_column_1 ()
511 {
512 register int tab_width = XINT (current_buffer->tab_width);
513 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
514 register struct Lisp_Char_Table *dp = buffer_display_table ();
515 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
516
517 /* Start the scan at the beginning of this line with column number 0. */
518 register int col = 0;
519 int scan, scan_byte;
520 int next_boundary;
521 int opoint = PT, opoint_byte = PT_BYTE;
522
523 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
524 current_column_bol_cache = PT;
525 scan = PT, scan_byte = PT_BYTE;
526 SET_PT_BOTH (opoint, opoint_byte);
527 next_boundary = scan;
528
529 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
530
531 /* Scan forward to the target position. */
532 while (scan < opoint)
533 {
534 int c;
535
536 /* Occasionally we may need to skip invisible text. */
537 while (scan == next_boundary)
538 {
539 int old_scan = scan;
540 /* This updates NEXT_BOUNDARY to the next place
541 where we might need to skip more invisible text. */
542 scan = skip_invisible (scan, &next_boundary, opoint, Qnil);
543 if (scan >= opoint)
544 goto endloop;
545 if (scan != old_scan)
546 scan_byte = CHAR_TO_BYTE (scan);
547 }
548
549 /* Check composition sequence. */
550 {
551 int len, len_byte, width;
552
553 if (check_composition (scan, scan_byte, opoint,
554 &len, &len_byte, &width))
555 {
556 scan += len;
557 scan_byte += len_byte;
558 if (scan <= opoint)
559 col += width;
560 continue;
561 }
562 }
563
564 c = FETCH_BYTE (scan_byte);
565
566 if (dp != 0
567 && ! (multibyte && BASE_LEADING_CODE_P (c))
568 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
569 {
570 Lisp_Object charvec;
571 EMACS_INT i, n;
572
573 /* This character is displayed using a vector of glyphs.
574 Update the column based on those glyphs. */
575
576 charvec = DISP_CHAR_VECTOR (dp, c);
577 n = ASIZE (charvec);
578
579 for (i = 0; i < n; i++)
580 {
581 /* This should be handled the same as
582 next_element_from_display_vector does it. */
583 Lisp_Object entry;
584 entry = AREF (charvec, i);
585
586 if (INTEGERP (entry)
587 && GLYPH_CHAR_VALID_P (XFASTINT (entry)))
588 c = FAST_GLYPH_CHAR (XFASTINT (entry));
589 else
590 c = ' ';
591
592 if (c == '\n')
593 goto endloop;
594 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
595 goto endloop;
596 if (c == '\t')
597 {
598 col += tab_width;
599 col = col / tab_width * tab_width;
600 }
601 else
602 ++col;
603 }
604 }
605 else
606 {
607 /* The display table says nothing for this character.
608 Display it as itself. */
609
610 if (c == '\n')
611 goto endloop;
612 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
613 goto endloop;
614 if (c == '\t')
615 {
616 col += tab_width;
617 col = col / tab_width * tab_width;
618 }
619 else if (multibyte && BASE_LEADING_CODE_P (c))
620 {
621 unsigned char *ptr;
622 int bytes, width, wide_column;
623
624 ptr = BYTE_POS_ADDR (scan_byte);
625 MULTIBYTE_BYTES_WIDTH (ptr, dp);
626 scan_byte += bytes;
627 /* Subtract one to compensate for the increment
628 that is going to happen below. */
629 scan_byte--;
630 col += width;
631 }
632 else if (ctl_arrow && (c < 040 || c == 0177))
633 col += 2;
634 else if (c < 040 || c >= 0177)
635 col += 4;
636 else
637 col++;
638 }
639 scan++;
640 scan_byte++;
641
642 }
643 endloop:
644
645 last_known_column = col;
646 last_known_column_point = PT;
647 last_known_column_modified = MODIFF;
648
649 return col;
650 }
651 \f
652
653 #if 0 /* Not used. */
654
655 /* Return the width in columns of the part of STRING from BEG to END.
656 If BEG is nil, that stands for the beginning of STRING.
657 If END is nil, that stands for the end of STRING. */
658
659 static double
660 string_display_width (string, beg, end)
661 Lisp_Object string, beg, end;
662 {
663 register int col;
664 register unsigned char *ptr, *stop;
665 register int tab_seen;
666 int post_tab;
667 register int c;
668 register int tab_width = XINT (current_buffer->tab_width);
669 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
670 register struct Lisp_Char_Table *dp = buffer_display_table ();
671 int b, e;
672
673 if (NILP (end))
674 e = SCHARS (string);
675 else
676 {
677 CHECK_NUMBER (end);
678 e = XINT (end);
679 }
680
681 if (NILP (beg))
682 b = 0;
683 else
684 {
685 CHECK_NUMBER (beg);
686 b = XINT (beg);
687 }
688
689 /* Make a pointer for decrementing through the chars before point. */
690 ptr = SDATA (string) + e;
691 /* Make a pointer to where consecutive chars leave off,
692 going backwards from point. */
693 stop = SDATA (string) + b;
694
695 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
696
697 col = 0, tab_seen = 0, post_tab = 0;
698
699 while (1)
700 {
701 if (ptr == stop)
702 break;
703
704 c = *--ptr;
705 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
706 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
707 else if (c >= 040 && c < 0177)
708 col++;
709 else if (c == '\n')
710 break;
711 else if (c == '\t')
712 {
713 if (tab_seen)
714 col = ((col + tab_width) / tab_width) * tab_width;
715
716 post_tab += col;
717 col = 0;
718 tab_seen = 1;
719 }
720 else
721 col += (ctl_arrow && c < 0200) ? 2 : 4;
722 }
723
724 if (tab_seen)
725 {
726 col = ((col + tab_width) / tab_width) * tab_width;
727 col += post_tab;
728 }
729
730 return col;
731 }
732
733 #endif /* 0 */
734
735 \f
736 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
737 doc: /* Indent from point with tabs and spaces until COLUMN is reached.
738 Optional second argument MINIMUM says always do at least MINIMUM spaces
739 even if that goes past COLUMN; by default, MINIMUM is zero.
740
741 The return value is COLUMN. */)
742 (column, minimum)
743 Lisp_Object column, minimum;
744 {
745 int mincol;
746 register int fromcol;
747 register int tab_width = XINT (current_buffer->tab_width);
748
749 CHECK_NUMBER (column);
750 if (NILP (minimum))
751 XSETFASTINT (minimum, 0);
752 CHECK_NUMBER (minimum);
753
754 fromcol = current_column ();
755 mincol = fromcol + XINT (minimum);
756 if (mincol < XINT (column)) mincol = XINT (column);
757
758 if (fromcol == mincol)
759 return make_number (mincol);
760
761 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
762
763 if (indent_tabs_mode)
764 {
765 Lisp_Object n;
766 XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
767 if (XFASTINT (n) != 0)
768 {
769 Finsert_char (make_number ('\t'), n, Qt);
770
771 fromcol = (mincol / tab_width) * tab_width;
772 }
773 }
774
775 XSETFASTINT (column, mincol - fromcol);
776 Finsert_char (make_number (' '), column, Qt);
777
778 last_known_column = mincol;
779 last_known_column_point = PT;
780 last_known_column_modified = MODIFF;
781
782 XSETINT (column, mincol);
783 return column;
784 }
785
786 \f
787 static double position_indentation P_ ((int));
788
789 DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
790 0, 0, 0,
791 doc: /* Return the indentation of the current line.
792 This is the horizontal position of the character
793 following any initial whitespace. */)
794 ()
795 {
796 Lisp_Object val;
797 int opoint = PT, opoint_byte = PT_BYTE;
798
799 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
800
801 XSETFASTINT (val, (int) position_indentation (PT_BYTE)); /* iftc */
802 SET_PT_BOTH (opoint, opoint_byte);
803 return val;
804 }
805
806 static double
807 position_indentation (pos_byte)
808 register int pos_byte;
809 {
810 register int column = 0;
811 register int tab_width = XINT (current_buffer->tab_width);
812 register unsigned char *p;
813 register unsigned char *stop;
814 unsigned char *start;
815 int next_boundary_byte = pos_byte;
816 int ceiling = next_boundary_byte;
817
818 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
819
820 p = BYTE_POS_ADDR (pos_byte);
821 /* STOP records the value of P at which we will need
822 to think about the gap, or about invisible text,
823 or about the end of the buffer. */
824 stop = p;
825 /* START records the starting value of P. */
826 start = p;
827 while (1)
828 {
829 while (p == stop)
830 {
831 int stop_pos_byte;
832
833 /* If we have updated P, set POS_BYTE to match.
834 The first time we enter the loop, POS_BYTE is already right. */
835 if (p != start)
836 pos_byte = PTR_BYTE_POS (p);
837 /* Consider the various reasons STOP might have been set here. */
838 if (pos_byte == ZV_BYTE)
839 return column;
840 if (pos_byte == next_boundary_byte)
841 {
842 int next_boundary;
843 int pos = BYTE_TO_CHAR (pos_byte);
844 pos = skip_invisible (pos, &next_boundary, ZV, Qnil);
845 pos_byte = CHAR_TO_BYTE (pos);
846 next_boundary_byte = CHAR_TO_BYTE (next_boundary);
847 }
848 if (pos_byte >= ceiling)
849 ceiling = BUFFER_CEILING_OF (pos_byte) + 1;
850 /* Compute the next place we need to stop and think,
851 and set STOP accordingly. */
852 stop_pos_byte = min (ceiling, next_boundary_byte);
853 /* The -1 and +1 arrange to point at the first byte of gap
854 (if STOP_POS_BYTE is the position of the gap)
855 rather than at the data after the gap. */
856
857 stop = BYTE_POS_ADDR (stop_pos_byte - 1) + 1;
858 p = BYTE_POS_ADDR (pos_byte);
859 }
860 switch (*p++)
861 {
862 case 0240:
863 if (! NILP (current_buffer->enable_multibyte_characters))
864 return column;
865 case ' ':
866 column++;
867 break;
868 case '\t':
869 column += tab_width - column % tab_width;
870 break;
871 default:
872 if (ASCII_BYTE_P (p[-1])
873 || NILP (current_buffer->enable_multibyte_characters))
874 return column;
875 {
876 int c;
877 pos_byte = PTR_BYTE_POS (p - 1);
878 c = FETCH_MULTIBYTE_CHAR (pos_byte);
879 if (CHAR_HAS_CATEGORY (c, ' '))
880 {
881 column++;
882 INC_POS (pos_byte);
883 p = BYTE_POS_ADDR (pos_byte);
884 }
885 else
886 return column;
887 }
888 }
889 }
890 }
891
892 /* Test whether the line beginning at POS is indented beyond COLUMN.
893 Blank lines are treated as if they had the same indentation as the
894 preceding line. */
895
896 int
897 indented_beyond_p (pos, pos_byte, column)
898 int pos, pos_byte;
899 double column;
900 {
901 double val;
902 int opoint = PT, opoint_byte = PT_BYTE;
903
904 SET_PT_BOTH (pos, pos_byte);
905 while (PT > BEGV && FETCH_BYTE (PT_BYTE) == '\n')
906 scan_newline (PT - 1, PT_BYTE - 1, BEGV, BEGV_BYTE, -1, 0);
907
908 val = position_indentation (PT_BYTE);
909 SET_PT_BOTH (opoint, opoint_byte);
910 return val >= column; /* hmm, float comparison */
911 }
912 \f
913 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
914 doc: /* Move point to column COLUMN in the current line.
915 Interactively, COLUMN is the value of prefix numeric argument.
916 The column of a character is calculated by adding together the widths
917 as displayed of the previous characters in the line.
918 This function ignores line-continuation;
919 there is no upper limit on the column number a character can have
920 and horizontal scrolling has no effect.
921
922 If specified column is within a character, point goes after that character.
923 If it's past end of line, point goes to end of line.
924
925 Optional second argument FORCE non-nil means if COLUMN is in the
926 middle of a tab character, change it to spaces.
927 In addition, if FORCE is t, and the line is too short to reach
928 COLUMN, add spaces/tabs to get there.
929
930 The return value is the current column. */)
931 (column, force)
932 Lisp_Object column, force;
933 {
934 register int pos;
935 register int col = current_column ();
936 register int goal;
937 register int end;
938 register int tab_width = XINT (current_buffer->tab_width);
939 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
940 register struct Lisp_Char_Table *dp = buffer_display_table ();
941 register int multibyte = !NILP (current_buffer->enable_multibyte_characters);
942
943 Lisp_Object val;
944 int prev_col = 0;
945 int c = 0;
946 int next_boundary, pos_byte;
947
948 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
949 CHECK_NATNUM (column);
950 goal = XINT (column);
951
952 pos = PT;
953 pos_byte = PT_BYTE;
954 end = ZV;
955
956 /* If we're starting past the desired column,
957 back up to beginning of line and scan from there. */
958 if (col > goal)
959 {
960 end = pos;
961 pos = current_column_bol_cache;
962 pos_byte = CHAR_TO_BYTE (pos);
963 col = 0;
964 }
965
966 next_boundary = pos;
967
968 while (pos < end)
969 {
970 while (pos == next_boundary)
971 {
972 int prev = pos;
973 pos = skip_invisible (pos, &next_boundary, end, Qnil);
974 if (pos != prev)
975 pos_byte = CHAR_TO_BYTE (pos);
976 if (pos >= end)
977 goto endloop;
978 }
979
980 /* Test reaching the goal column. We do this after skipping
981 invisible characters, so that we put point before the
982 character on which the cursor will appear. */
983 if (col >= goal)
984 break;
985
986 /* Check composition sequence. */
987 {
988 int len, len_byte, width;
989
990 if (check_composition (pos, pos_byte, Z, &len, &len_byte, &width))
991 {
992 pos += len;
993 pos_byte += len_byte;
994 col += width;
995 continue;
996 }
997 }
998
999 c = FETCH_BYTE (pos_byte);
1000
1001 /* See if there is a display table and it relates
1002 to this character. */
1003
1004 if (dp != 0
1005 && ! (multibyte && BASE_LEADING_CODE_P (c))
1006 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1007 {
1008 Lisp_Object charvec;
1009 EMACS_INT i, n;
1010
1011 /* This character is displayed using a vector of glyphs.
1012 Update the position based on those glyphs. */
1013
1014 charvec = DISP_CHAR_VECTOR (dp, c);
1015 n = ASIZE (charvec);
1016
1017 for (i = 0; i < n; i++)
1018 {
1019 /* This should be handled the same as
1020 next_element_from_display_vector does it. */
1021
1022 Lisp_Object entry;
1023 entry = AREF (charvec, i);
1024
1025 if (INTEGERP (entry)
1026 && GLYPH_CHAR_VALID_P (XFASTINT (entry)))
1027 c = FAST_GLYPH_CHAR (XFASTINT (entry));
1028 else
1029 c = ' ';
1030
1031 if (c == '\n')
1032 goto endloop;
1033 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
1034 goto endloop;
1035 if (c == '\t')
1036 {
1037 prev_col = col;
1038 col += tab_width;
1039 col = col / tab_width * tab_width;
1040 }
1041 else
1042 ++col;
1043 }
1044 }
1045 else
1046 {
1047 /* The display table doesn't affect this character;
1048 it displays as itself. */
1049
1050 if (c == '\n')
1051 goto endloop;
1052 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
1053 goto endloop;
1054 if (c == '\t')
1055 {
1056 prev_col = col;
1057 col += tab_width;
1058 col = col / tab_width * tab_width;
1059 }
1060 else if (ctl_arrow && (c < 040 || c == 0177))
1061 col += 2;
1062 else if (c < 040 || c == 0177)
1063 col += 4;
1064 else if (c < 0177)
1065 col++;
1066 else if (multibyte && BASE_LEADING_CODE_P (c))
1067 {
1068 /* Start of multi-byte form. */
1069 unsigned char *ptr;
1070 int bytes, width, wide_column;
1071
1072 ptr = BYTE_POS_ADDR (pos_byte);
1073 MULTIBYTE_BYTES_WIDTH (ptr, dp);
1074 pos_byte += bytes - 1;
1075 col += width;
1076 }
1077 else
1078 col += 4;
1079 }
1080
1081 pos++;
1082 pos_byte++;
1083 }
1084 endloop:
1085
1086 SET_PT_BOTH (pos, pos_byte);
1087
1088 /* If a tab char made us overshoot, change it to spaces
1089 and scan through it again. */
1090 if (!NILP (force) && col > goal && c == '\t' && prev_col < goal)
1091 {
1092 int goal_pt, goal_pt_byte;
1093
1094 /* Insert spaces in front of the tab to reach GOAL. Do this
1095 first so that a marker at the end of the tab gets
1096 adjusted. */
1097 SET_PT_BOTH (PT - 1, PT_BYTE - 1);
1098 Finsert_char (make_number (' '), make_number (goal - prev_col), Qt);
1099
1100 /* Now delete the tab, and indent to COL. */
1101 del_range (PT, PT + 1);
1102 goal_pt = PT;
1103 goal_pt_byte = PT_BYTE;
1104 Findent_to (make_number (col), Qnil);
1105 SET_PT_BOTH (goal_pt, goal_pt_byte);
1106
1107 /* Set the last_known... vars consistently. */
1108 col = goal;
1109 }
1110
1111 /* If line ends prematurely, add space to the end. */
1112 if (col < goal && EQ (force, Qt))
1113 Findent_to (make_number (col = goal), Qnil);
1114
1115 last_known_column = col;
1116 last_known_column_point = PT;
1117 last_known_column_modified = MODIFF;
1118
1119 XSETFASTINT (val, col);
1120 return val;
1121 }
1122 \f
1123 /* compute_motion: compute buffer posn given screen posn and vice versa */
1124
1125 struct position val_compute_motion;
1126
1127 /* Scan the current buffer forward from offset FROM, pretending that
1128 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
1129 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
1130 and return the ending buffer position and screen location. If we
1131 can't hit the requested column exactly (because of a tab or other
1132 multi-column character), overshoot.
1133
1134 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
1135 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
1136 earlier call to compute_motion. The other common case is that FROMHPOS
1137 is zero and FROM is a position that "belongs" at column zero, but might
1138 be shifted by overlay strings; in this case DID_MOTION should be 0.
1139
1140 WIDTH is the number of columns available to display text;
1141 compute_motion uses this to handle continuation lines and such.
1142 If WIDTH is -1, use width of window's text area adjusted for
1143 continuation glyph when needed.
1144
1145 HSCROLL is the number of columns not being displayed at the left
1146 margin; this is usually taken from a window's hscroll member.
1147 TAB_OFFSET is the number of columns of the first tab that aren't
1148 being displayed, perhaps because of a continuation line or
1149 something.
1150
1151 compute_motion returns a pointer to a struct position. The bufpos
1152 member gives the buffer position at the end of the scan, and hpos
1153 and vpos give its cartesian location. prevhpos is the column at
1154 which the character before bufpos started, and contin is non-zero
1155 if we reached the current line by continuing the previous.
1156
1157 Note that FROMHPOS and TOHPOS should be expressed in real screen
1158 columns, taking HSCROLL and the truncation glyph at the left margin
1159 into account. That is, beginning-of-line moves you to the hpos
1160 -HSCROLL + (HSCROLL > 0).
1161
1162 For example, to find the buffer position of column COL of line LINE
1163 of a certain window, pass the window's starting location as FROM
1164 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
1165 Pass the buffer's ZV as TO, to limit the scan to the end of the
1166 visible section of the buffer, and pass LINE and COL as TOVPOS and
1167 TOHPOS.
1168
1169 When displaying in window w, a typical formula for WIDTH is:
1170
1171 window_width - 1
1172 - (has_vertical_scroll_bars
1173 ? WINDOW_CONFIG_SCROLL_BAR_COLS (window)
1174 : (window_width + window_left != frame_cols))
1175
1176 where
1177 window_width is XFASTINT (w->total_cols),
1178 window_left is XFASTINT (w->left_col),
1179 has_vertical_scroll_bars is
1180 WINDOW_HAS_VERTICAL_SCROLL_BAR (window)
1181 and frame_cols = FRAME_COLS (XFRAME (window->frame))
1182
1183 Or you can let window_box_text_cols do this all for you, and write:
1184 window_box_text_cols (w) - 1
1185
1186 The `-1' accounts for the continuation-line backslashes; the rest
1187 accounts for window borders if the window is split horizontally, and
1188 the scroll bars if they are turned on. */
1189
1190 struct position *
1191 compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win)
1192 int from, fromvpos, fromhpos, to, tovpos, tohpos;
1193 int did_motion;
1194 register int width;
1195 int hscroll, tab_offset;
1196 struct window *win;
1197 {
1198 register int hpos = fromhpos;
1199 register int vpos = fromvpos;
1200
1201 register int pos;
1202 int pos_byte;
1203 register int c = 0;
1204 register int tab_width = XFASTINT (current_buffer->tab_width);
1205 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
1206 register struct Lisp_Char_Table *dp = window_display_table (win);
1207 int selective
1208 = (INTEGERP (current_buffer->selective_display)
1209 ? XINT (current_buffer->selective_display)
1210 : !NILP (current_buffer->selective_display) ? -1 : 0);
1211 int selective_rlen
1212 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
1213 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0);
1214 /* The next location where the `invisible' property changes, or an
1215 overlay starts or ends. */
1216 int next_boundary = from;
1217
1218 /* For computing runs of characters with similar widths.
1219 Invariant: width_run_width is zero, or all the characters
1220 from width_run_start to width_run_end have a fixed width of
1221 width_run_width. */
1222 int width_run_start = from;
1223 int width_run_end = from;
1224 int width_run_width = 0;
1225 Lisp_Object *width_table;
1226 Lisp_Object buffer;
1227
1228 /* The next buffer pos where we should consult the width run cache. */
1229 int next_width_run = from;
1230 Lisp_Object window;
1231
1232 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1233 /* If previous char scanned was a wide character,
1234 this is the column where it ended. Otherwise, this is 0. */
1235 int wide_column_end_hpos = 0;
1236 int prev_pos; /* Previous buffer position. */
1237 int prev_pos_byte; /* Previous buffer position. */
1238 int prev_hpos = 0;
1239 int prev_vpos = 0;
1240 int contin_hpos; /* HPOS of last column of continued line. */
1241 int prev_tab_offset; /* Previous tab offset. */
1242 int continuation_glyph_width;
1243
1244 XSETBUFFER (buffer, current_buffer);
1245 XSETWINDOW (window, win);
1246
1247 width_run_cache_on_off ();
1248 if (dp == buffer_display_table ())
1249 width_table = (VECTORP (current_buffer->width_table)
1250 ? XVECTOR (current_buffer->width_table)->contents
1251 : 0);
1252 else
1253 /* If the window has its own display table, we can't use the width
1254 run cache, because that's based on the buffer's display table. */
1255 width_table = 0;
1256
1257 if (tab_width <= 0 || tab_width > 1000)
1258 tab_width = 8;
1259
1260 /* Negative width means use all available text columns. */
1261 if (width < 0)
1262 {
1263 width = window_box_text_cols (win);
1264 /* We must make room for continuation marks if we don't have fringes. */
1265 #ifdef HAVE_WINDOW_SYSTEM
1266 if (!FRAME_WINDOW_P (XFRAME (win->frame)))
1267 #endif
1268 width -= 1;
1269 }
1270
1271 continuation_glyph_width = 1;
1272 #ifdef HAVE_WINDOW_SYSTEM
1273 if (FRAME_WINDOW_P (XFRAME (win->frame)))
1274 continuation_glyph_width = 0; /* In the fringe. */
1275 #endif
1276
1277 immediate_quit = 1;
1278 QUIT;
1279
1280 pos = prev_pos = from;
1281 pos_byte = prev_pos_byte = CHAR_TO_BYTE (from);
1282 contin_hpos = 0;
1283 prev_tab_offset = tab_offset;
1284 while (1)
1285 {
1286 while (pos == next_boundary)
1287 {
1288 int pos_here = pos;
1289 int newpos;
1290
1291 /* Don't skip invisible if we are already at the margin. */
1292 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1293 {
1294 if (contin_hpos && prev_hpos == 0
1295 && hpos > tohpos
1296 && (contin_hpos == width || wide_column_end_hpos > width))
1297 { /* Line breaks because we can't put the character at the
1298 previous line any more. It is not the multi-column
1299 character continued in middle. Go back to previous
1300 buffer position, screen position, and set tab offset
1301 to previous value. It's the beginning of the
1302 line. */
1303 pos = prev_pos;
1304 pos_byte = prev_pos_byte;
1305 hpos = prev_hpos;
1306 vpos = prev_vpos;
1307 tab_offset = prev_tab_offset;
1308 }
1309 break;
1310 }
1311
1312 /* If the caller says that the screen position came from an earlier
1313 call to compute_motion, then we've already accounted for the
1314 overlay strings at point. This is only true the first time
1315 through, so clear the flag after testing it. */
1316 if (!did_motion)
1317 /* We need to skip past the overlay strings. Currently those
1318 strings must not contain TAB;
1319 if we want to relax that restriction, something will have
1320 to be changed here. */
1321 {
1322 unsigned char *ovstr;
1323 int ovlen = overlay_strings (pos, win, &ovstr);
1324 hpos += ((multibyte && ovlen > 0)
1325 ? strwidth (ovstr, ovlen) : ovlen);
1326 }
1327 did_motion = 0;
1328
1329 if (pos >= to)
1330 break;
1331
1332 /* Advance POS past invisible characters
1333 (but not necessarily all that there are here),
1334 and store in next_boundary the next position where
1335 we need to call skip_invisible. */
1336 newpos = skip_invisible (pos, &next_boundary, to, window);
1337
1338 if (newpos >= to)
1339 {
1340 pos = min (to, newpos);
1341 pos_byte = CHAR_TO_BYTE (pos);
1342 goto after_loop;
1343 }
1344
1345 if (newpos != pos_here)
1346 {
1347 pos = newpos;
1348 pos_byte = CHAR_TO_BYTE (pos);
1349 }
1350 }
1351
1352 /* Handle right margin. */
1353 /* Note on a wide-column character.
1354
1355 Characters are classified into the following three categories
1356 according to the width (columns occupied on screen).
1357
1358 (1) single-column character: ex. `a'
1359 (2) multi-column character: ex. `^A', TAB, `\033'
1360 (3) wide-column character: ex. Japanese character, Chinese character
1361 (In the following example, `W_' stands for them.)
1362
1363 Multi-column characters can be divided around the right margin,
1364 but wide-column characters cannot.
1365
1366 NOTE:
1367
1368 (*) The cursor is placed on the next character after the point.
1369
1370 ----------
1371 abcdefghi\
1372 j ^---- next after the point
1373 ^--- next char. after the point.
1374 ----------
1375 In case of sigle-column character
1376
1377 ----------
1378 abcdefgh\\
1379 033 ^---- next after the point, next char. after the point.
1380 ----------
1381 In case of multi-column character
1382
1383 ----------
1384 abcdefgh\\
1385 W_ ^---- next after the point
1386 ^---- next char. after the point.
1387 ----------
1388 In case of wide-column character
1389
1390 The problem here is continuation at a wide-column character.
1391 In this case, the line may shorter less than WIDTH.
1392 And we find the continuation AFTER it occurs.
1393
1394 */
1395
1396 if (hpos > width)
1397 {
1398 if (hscroll
1399 || (truncate_partial_width_windows
1400 && ((width + continuation_glyph_width)
1401 < FRAME_COLS (XFRAME (WINDOW_FRAME (win)))))
1402 || !NILP (current_buffer->truncate_lines))
1403 {
1404 /* Truncating: skip to newline, unless we are already past
1405 TO (we need to go back below). */
1406 if (pos <= to)
1407 {
1408 pos = find_before_next_newline (pos, to, 1);
1409 pos_byte = CHAR_TO_BYTE (pos);
1410 hpos = width;
1411 /* If we just skipped next_boundary,
1412 loop around in the main while
1413 and handle it. */
1414 if (pos >= next_boundary)
1415 next_boundary = pos + 1;
1416 prev_hpos = width;
1417 prev_vpos = vpos;
1418 prev_tab_offset = tab_offset;
1419 }
1420 }
1421 else
1422 {
1423 /* Continuing. */
1424 /* Remember the previous value. */
1425 prev_tab_offset = tab_offset;
1426
1427 if (wide_column_end_hpos > width)
1428 {
1429 hpos -= prev_hpos;
1430 tab_offset += prev_hpos;
1431 }
1432 else
1433 {
1434 tab_offset += width;
1435 hpos -= width;
1436 }
1437 vpos++;
1438 contin_hpos = prev_hpos;
1439 prev_hpos = 0;
1440 prev_vpos = vpos;
1441 }
1442 }
1443
1444 /* Stop if past the target buffer position or screen position. */
1445 if (pos > to)
1446 {
1447 /* Go back to the previous position. */
1448 pos = prev_pos;
1449 pos_byte = prev_pos_byte;
1450 hpos = prev_hpos;
1451 vpos = prev_vpos;
1452 tab_offset = prev_tab_offset;
1453
1454 /* NOTE on contin_hpos, hpos, and prev_hpos.
1455
1456 ----------
1457 abcdefgh\\
1458 W_ ^---- contin_hpos
1459 | ^----- hpos
1460 \---- prev_hpos
1461 ----------
1462 */
1463
1464 if (contin_hpos && prev_hpos == 0
1465 && contin_hpos < width && !wide_column_end_hpos)
1466 {
1467 /* Line breaking occurs in the middle of multi-column
1468 character. Go back to previous line. */
1469 hpos = contin_hpos;
1470 vpos = vpos - 1;
1471 }
1472 break;
1473 }
1474
1475 if (vpos > tovpos || (vpos == tovpos && hpos >= tohpos))
1476 {
1477 if (contin_hpos && prev_hpos == 0
1478 && hpos > tohpos
1479 && (contin_hpos == width || wide_column_end_hpos > width))
1480 { /* Line breaks because we can't put the character at the
1481 previous line any more. It is not the multi-column
1482 character continued in middle. Go back to previous
1483 buffer position, screen position, and set tab offset
1484 to previous value. It's the beginning of the
1485 line. */
1486 pos = prev_pos;
1487 pos_byte = prev_pos_byte;
1488 hpos = prev_hpos;
1489 vpos = prev_vpos;
1490 tab_offset = prev_tab_offset;
1491 }
1492 break;
1493 }
1494 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1495 break;
1496
1497 prev_hpos = hpos;
1498 prev_vpos = vpos;
1499 prev_pos = pos;
1500 prev_pos_byte = pos_byte;
1501 wide_column_end_hpos = 0;
1502
1503 /* Consult the width run cache to see if we can avoid inspecting
1504 the text character-by-character. */
1505 if (current_buffer->width_run_cache && pos >= next_width_run)
1506 {
1507 int run_end;
1508 int common_width
1509 = region_cache_forward (current_buffer,
1510 current_buffer->width_run_cache,
1511 pos, &run_end);
1512
1513 /* A width of zero means the character's width varies (like
1514 a tab), is meaningless (like a newline), or we just don't
1515 want to skip over it for some other reason. */
1516 if (common_width != 0)
1517 {
1518 int run_end_hpos;
1519
1520 /* Don't go past the final buffer posn the user
1521 requested. */
1522 if (run_end > to)
1523 run_end = to;
1524
1525 run_end_hpos = hpos + (run_end - pos) * common_width;
1526
1527 /* Don't go past the final horizontal position the user
1528 requested. */
1529 if (vpos == tovpos && run_end_hpos > tohpos)
1530 {
1531 run_end = pos + (tohpos - hpos) / common_width;
1532 run_end_hpos = hpos + (run_end - pos) * common_width;
1533 }
1534
1535 /* Don't go past the margin. */
1536 if (run_end_hpos >= width)
1537 {
1538 run_end = pos + (width - hpos) / common_width;
1539 run_end_hpos = hpos + (run_end - pos) * common_width;
1540 }
1541
1542 hpos = run_end_hpos;
1543 if (run_end > pos)
1544 prev_hpos = hpos - common_width;
1545 if (pos != run_end)
1546 {
1547 pos = run_end;
1548 pos_byte = CHAR_TO_BYTE (pos);
1549 }
1550 }
1551
1552 next_width_run = run_end + 1;
1553 }
1554
1555 /* We have to scan the text character-by-character. */
1556 else
1557 {
1558 EMACS_INT i, n;
1559 Lisp_Object charvec;
1560
1561 c = FETCH_BYTE (pos_byte);
1562
1563 /* Check composition sequence. */
1564 {
1565 int len, len_byte, width;
1566
1567 if (check_composition (pos, pos_byte, to, &len, &len_byte, &width))
1568 {
1569 pos += len;
1570 pos_byte += len_byte;
1571 hpos += width;
1572 continue;
1573 }
1574 }
1575
1576 pos++, pos_byte++;
1577
1578 /* Perhaps add some info to the width_run_cache. */
1579 if (current_buffer->width_run_cache)
1580 {
1581 /* Is this character part of the current run? If so, extend
1582 the run. */
1583 if (pos - 1 == width_run_end
1584 && XFASTINT (width_table[c]) == width_run_width)
1585 width_run_end = pos;
1586
1587 /* The previous run is over, since this is a character at a
1588 different position, or a different width. */
1589 else
1590 {
1591 /* Have we accumulated a run to put in the cache?
1592 (Currently, we only cache runs of width == 1). */
1593 if (width_run_start < width_run_end
1594 && width_run_width == 1)
1595 know_region_cache (current_buffer,
1596 current_buffer->width_run_cache,
1597 width_run_start, width_run_end);
1598
1599 /* Start recording a new width run. */
1600 width_run_width = XFASTINT (width_table[c]);
1601 width_run_start = pos - 1;
1602 width_run_end = pos;
1603 }
1604 }
1605
1606 if (dp != 0
1607 && ! (multibyte && BASE_LEADING_CODE_P (c))
1608 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1609 {
1610 charvec = DISP_CHAR_VECTOR (dp, c);
1611 n = ASIZE (charvec);
1612 }
1613 else
1614 {
1615 charvec = Qnil;
1616 n = 1;
1617 }
1618
1619 for (i = n - 1; i >= 0; --i)
1620 {
1621 if (VECTORP (charvec))
1622 {
1623 /* This should be handled the same as
1624 next_element_from_display_vector does it. */
1625 Lisp_Object entry = AREF (charvec, i);
1626
1627 if (INTEGERP (entry)
1628 && GLYPH_CHAR_VALID_P (XFASTINT (entry)))
1629 c = FAST_GLYPH_CHAR (XFASTINT (entry));
1630 else
1631 c = ' ';
1632 }
1633
1634 if (c >= 040 && c < 0177)
1635 hpos++;
1636 else if (c == '\t')
1637 {
1638 int tem = ((hpos + tab_offset + hscroll - (hscroll > 0))
1639 % tab_width);
1640 if (tem < 0)
1641 tem += tab_width;
1642 hpos += tab_width - tem;
1643 }
1644 else if (c == '\n')
1645 {
1646 if (selective > 0
1647 && indented_beyond_p (pos, pos_byte,
1648 (double) selective)) /* iftc */
1649 {
1650 /* If (pos == to), we don't have to take care of
1651 selective display. */
1652 if (pos < to)
1653 {
1654 /* Skip any number of invisible lines all at once */
1655 do
1656 {
1657 pos = find_before_next_newline (pos, to, 1);
1658 if (pos < to)
1659 pos++;
1660 pos_byte = CHAR_TO_BYTE (pos);
1661 }
1662 while (pos < to
1663 && indented_beyond_p (pos, pos_byte,
1664 (double) selective)); /* iftc */
1665 /* Allow for the " ..." that is displayed for them. */
1666 if (selective_rlen)
1667 {
1668 hpos += selective_rlen;
1669 if (hpos >= width)
1670 hpos = width;
1671 }
1672 DEC_BOTH (pos, pos_byte);
1673 /* We have skipped the invis text, but not the
1674 newline after. */
1675 }
1676 }
1677 else
1678 {
1679 /* A visible line. */
1680 vpos++;
1681 hpos = 0;
1682 hpos -= hscroll;
1683 /* Count the truncation glyph on column 0 */
1684 if (hscroll > 0)
1685 hpos += continuation_glyph_width;
1686 tab_offset = 0;
1687 }
1688 contin_hpos = 0;
1689 }
1690 else if (c == CR && selective < 0)
1691 {
1692 /* In selective display mode,
1693 everything from a ^M to the end of the line is invisible.
1694 Stop *before* the real newline. */
1695 if (pos < to)
1696 {
1697 pos = find_before_next_newline (pos, to, 1);
1698 pos_byte = CHAR_TO_BYTE (pos);
1699 }
1700 /* If we just skipped next_boundary,
1701 loop around in the main while
1702 and handle it. */
1703 if (pos > next_boundary)
1704 next_boundary = pos;
1705 /* Allow for the " ..." that is displayed for them. */
1706 if (selective_rlen)
1707 {
1708 hpos += selective_rlen;
1709 if (hpos >= width)
1710 hpos = width;
1711 }
1712 }
1713 else if (multibyte && BASE_LEADING_CODE_P (c))
1714 {
1715 /* Start of multi-byte form. */
1716 unsigned char *ptr;
1717 int bytes, width, wide_column;
1718
1719 pos_byte--; /* rewind POS_BYTE */
1720 ptr = BYTE_POS_ADDR (pos_byte);
1721 MULTIBYTE_BYTES_WIDTH (ptr, dp);
1722 pos_byte += bytes;
1723 if (wide_column)
1724 wide_column_end_hpos = hpos + wide_column;
1725 hpos += width;
1726 }
1727 else if (VECTORP (charvec))
1728 ++hpos;
1729 else
1730 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1731 }
1732 }
1733 }
1734
1735 after_loop:
1736
1737 /* Remember any final width run in the cache. */
1738 if (current_buffer->width_run_cache
1739 && width_run_width == 1
1740 && width_run_start < width_run_end)
1741 know_region_cache (current_buffer, current_buffer->width_run_cache,
1742 width_run_start, width_run_end);
1743
1744 val_compute_motion.bufpos = pos;
1745 val_compute_motion.bytepos = pos_byte;
1746 val_compute_motion.hpos = hpos;
1747 val_compute_motion.vpos = vpos;
1748 if (contin_hpos && prev_hpos == 0)
1749 val_compute_motion.prevhpos = contin_hpos;
1750 else
1751 val_compute_motion.prevhpos = prev_hpos;
1752 /* We alalways handle all of them here; none of them remain to do. */
1753 val_compute_motion.ovstring_chars_done = 0;
1754
1755 /* Nonzero if have just continued a line */
1756 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1757
1758 immediate_quit = 0;
1759 return &val_compute_motion;
1760 }
1761
1762
1763 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1764 doc: /* Scan through the current buffer, calculating screen position.
1765 Scan the current buffer forward from offset FROM,
1766 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
1767 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
1768 and return the ending buffer position and screen location.
1769
1770 If TOPOS is nil, the actual width and height of the window's
1771 text area are used.
1772
1773 There are three additional arguments:
1774
1775 WIDTH is the number of columns available to display text;
1776 this affects handling of continuation lines. A value of nil
1777 corresponds to the actual number of available text columns.
1778
1779 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
1780 HSCROLL is the number of columns not being displayed at the left
1781 margin; this is usually taken from a window's hscroll member.
1782 TAB-OFFSET is the number of columns of the first tab that aren't
1783 being displayed, perhaps because the line was continued within it.
1784 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
1785
1786 WINDOW is the window to operate on. It is used to choose the display table;
1787 if it is showing the current buffer, it is used also for
1788 deciding which overlay properties apply.
1789 Note that `compute-motion' always operates on the current buffer.
1790
1791 The value is a list of five elements:
1792 (POS HPOS VPOS PREVHPOS CONTIN)
1793 POS is the buffer position where the scan stopped.
1794 VPOS is the vertical position where the scan stopped.
1795 HPOS is the horizontal position where the scan stopped.
1796
1797 PREVHPOS is the horizontal position one character back from POS.
1798 CONTIN is t if a line was continued after (or within) the previous character.
1799
1800 For example, to find the buffer position of column COL of line LINE
1801 of a certain window, pass the window's starting location as FROM
1802 and the window's upper-left coordinates as FROMPOS.
1803 Pass the buffer's (point-max) as TO, to limit the scan to the end of the
1804 visible section of the buffer, and pass LINE and COL as TOPOS. */)
1805 (from, frompos, to, topos, width, offsets, window)
1806 Lisp_Object from, frompos, to, topos;
1807 Lisp_Object width, offsets, window;
1808 {
1809 struct window *w;
1810 Lisp_Object bufpos, hpos, vpos, prevhpos;
1811 struct position *pos;
1812 int hscroll, tab_offset;
1813
1814 CHECK_NUMBER_COERCE_MARKER (from);
1815 CHECK_CONS (frompos);
1816 CHECK_NUMBER_CAR (frompos);
1817 CHECK_NUMBER_CDR (frompos);
1818 CHECK_NUMBER_COERCE_MARKER (to);
1819 if (!NILP (topos))
1820 {
1821 CHECK_CONS (topos);
1822 CHECK_NUMBER_CAR (topos);
1823 CHECK_NUMBER_CDR (topos);
1824 }
1825 if (!NILP (width))
1826 CHECK_NUMBER (width);
1827
1828 if (!NILP (offsets))
1829 {
1830 CHECK_CONS (offsets);
1831 CHECK_NUMBER_CAR (offsets);
1832 CHECK_NUMBER_CDR (offsets);
1833 hscroll = XINT (XCAR (offsets));
1834 tab_offset = XINT (XCDR (offsets));
1835 }
1836 else
1837 hscroll = tab_offset = 0;
1838
1839 if (NILP (window))
1840 window = Fselected_window ();
1841 else
1842 CHECK_LIVE_WINDOW (window);
1843 w = XWINDOW (window);
1844
1845 if (XINT (from) < BEGV || XINT (from) > ZV)
1846 args_out_of_range_3 (from, make_number (BEGV), make_number (ZV));
1847 if (XINT (to) < BEGV || XINT (to) > ZV)
1848 args_out_of_range_3 (to, make_number (BEGV), make_number (ZV));
1849
1850 pos = compute_motion (XINT (from), XINT (XCDR (frompos)),
1851 XINT (XCAR (frompos)), 0,
1852 XINT (to),
1853 (NILP (topos)
1854 ? window_internal_height (w)
1855 : XINT (XCDR (topos))),
1856 (NILP (topos)
1857 ? (window_box_text_cols (w)
1858 - (
1859 #ifdef HAVE_WINDOW_SYSTEM
1860 FRAME_WINDOW_P (XFRAME (w->frame)) ? 0 :
1861 #endif
1862 1))
1863 : XINT (XCAR (topos))),
1864 (NILP (width) ? -1 : XINT (width)),
1865 hscroll, tab_offset,
1866 XWINDOW (window));
1867
1868 XSETFASTINT (bufpos, pos->bufpos);
1869 XSETINT (hpos, pos->hpos);
1870 XSETINT (vpos, pos->vpos);
1871 XSETINT (prevhpos, pos->prevhpos);
1872
1873 return Fcons (bufpos,
1874 Fcons (hpos,
1875 Fcons (vpos,
1876 Fcons (prevhpos,
1877 Fcons (pos->contin ? Qt : Qnil, Qnil)))));
1878
1879 }
1880 \f
1881 /* Fvertical_motion and vmotion */
1882
1883 struct position val_vmotion;
1884
1885 struct position *
1886 vmotion (from, vtarget, w)
1887 register int from, vtarget;
1888 struct window *w;
1889 {
1890 int hscroll = XINT (w->hscroll);
1891 struct position pos;
1892 /* vpos is cumulative vertical position, changed as from is changed */
1893 register int vpos = 0;
1894 int prevline;
1895 register int first;
1896 int from_byte;
1897 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
1898 int selective
1899 = (INTEGERP (current_buffer->selective_display)
1900 ? XINT (current_buffer->selective_display)
1901 : !NILP (current_buffer->selective_display) ? -1 : 0);
1902 Lisp_Object window;
1903 int start_hpos = 0;
1904 int did_motion;
1905 /* This is the object we use for fetching character properties. */
1906 Lisp_Object text_prop_object;
1907
1908 XSETWINDOW (window, w);
1909
1910 /* If the window contains this buffer, use it for getting text properties.
1911 Otherwise use the current buffer as arg for doing that. */
1912 if (EQ (w->buffer, Fcurrent_buffer ()))
1913 text_prop_object = window;
1914 else
1915 text_prop_object = Fcurrent_buffer ();
1916
1917 if (vpos >= vtarget)
1918 {
1919 /* To move upward, go a line at a time until
1920 we have gone at least far enough. */
1921
1922 first = 1;
1923
1924 while ((vpos > vtarget || first) && from > BEGV)
1925 {
1926 Lisp_Object propval;
1927
1928 prevline = find_next_newline_no_quit (from - 1, -1);
1929 while (prevline > BEGV
1930 && ((selective > 0
1931 && indented_beyond_p (prevline,
1932 CHAR_TO_BYTE (prevline),
1933 (double) selective)) /* iftc */
1934 /* Watch out for newlines with `invisible' property.
1935 When moving upward, check the newline before. */
1936 || (propval = Fget_char_property (make_number (prevline - 1),
1937 Qinvisible,
1938 text_prop_object),
1939 TEXT_PROP_MEANS_INVISIBLE (propval))))
1940 prevline = find_next_newline_no_quit (prevline - 1, -1);
1941 pos = *compute_motion (prevline, 0,
1942 lmargin + (prevline == BEG ? start_hpos : 0),
1943 0,
1944 from,
1945 /* Don't care for VPOS... */
1946 1 << (BITS_PER_SHORT - 1),
1947 /* ... nor HPOS. */
1948 1 << (BITS_PER_SHORT - 1),
1949 -1, hscroll,
1950 /* This compensates for start_hpos
1951 so that a tab as first character
1952 still occupies 8 columns. */
1953 (prevline == BEG ? -start_hpos : 0),
1954 w);
1955 vpos -= pos.vpos;
1956 first = 0;
1957 from = prevline;
1958 }
1959
1960 /* If we made exactly the desired vertical distance,
1961 or if we hit beginning of buffer,
1962 return point found */
1963 if (vpos >= vtarget)
1964 {
1965 val_vmotion.bufpos = from;
1966 val_vmotion.bytepos = CHAR_TO_BYTE (from);
1967 val_vmotion.vpos = vpos;
1968 val_vmotion.hpos = lmargin;
1969 val_vmotion.contin = 0;
1970 val_vmotion.prevhpos = 0;
1971 val_vmotion.ovstring_chars_done = 0;
1972 val_vmotion.tab_offset = 0; /* For accumulating tab offset. */
1973 return &val_vmotion;
1974 }
1975
1976 /* Otherwise find the correct spot by moving down */
1977 }
1978 /* Moving downward is simple, but must calculate from beg of line
1979 to determine hpos of starting point */
1980 from_byte = CHAR_TO_BYTE (from);
1981 if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n')
1982 {
1983 Lisp_Object propval;
1984
1985 prevline = find_next_newline_no_quit (from, -1);
1986 while (prevline > BEGV
1987 && ((selective > 0
1988 && indented_beyond_p (prevline,
1989 CHAR_TO_BYTE (prevline),
1990 (double) selective)) /* iftc */
1991 /* Watch out for newlines with `invisible' property.
1992 When moving downward, check the newline after. */
1993 || (propval = Fget_char_property (make_number (prevline),
1994 Qinvisible,
1995 text_prop_object),
1996 TEXT_PROP_MEANS_INVISIBLE (propval))))
1997 prevline = find_next_newline_no_quit (prevline - 1, -1);
1998 pos = *compute_motion (prevline, 0,
1999 lmargin + (prevline == BEG
2000 ? start_hpos : 0),
2001 0,
2002 from,
2003 /* Don't care for VPOS... */
2004 1 << (BITS_PER_SHORT - 1),
2005 /* ... nor HPOS. */
2006 1 << (BITS_PER_SHORT - 1),
2007 -1, hscroll,
2008 (prevline == BEG ? -start_hpos : 0),
2009 w);
2010 did_motion = 1;
2011 }
2012 else
2013 {
2014 pos.hpos = lmargin + (from == BEG ? start_hpos : 0);
2015 pos.vpos = 0;
2016 pos.tab_offset = 0;
2017 did_motion = 0;
2018 }
2019 return compute_motion (from, vpos, pos.hpos, did_motion,
2020 ZV, vtarget, - (1 << (BITS_PER_SHORT - 1)),
2021 -1, hscroll,
2022 pos.tab_offset - (from == BEG ? start_hpos : 0),
2023 w);
2024 }
2025
2026 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
2027 doc: /* Move point to start of the screen line LINES lines down.
2028 If LINES is negative, this means moving up.
2029
2030 This function is an ordinary cursor motion function
2031 which calculates the new position based on how text would be displayed.
2032 The new position may be the start of a line,
2033 or just the start of a continuation line.
2034 The function returns number of screen lines moved over;
2035 that usually equals LINES, but may be closer to zero
2036 if beginning or end of buffer was reached.
2037
2038 The optional second argument WINDOW specifies the window to use for
2039 parameters such as width, horizontal scrolling, and so on.
2040 The default is to use the selected window's parameters.
2041
2042 `vertical-motion' always uses the current buffer,
2043 regardless of which buffer is displayed in WINDOW.
2044 This is consistent with other cursor motion functions
2045 and makes it possible to use `vertical-motion' in any buffer,
2046 whether or not it is currently displayed in some window. */)
2047 (lines, window)
2048 Lisp_Object lines, window;
2049 {
2050 struct it it;
2051 struct text_pos pt;
2052 struct window *w;
2053 Lisp_Object old_buffer;
2054 struct gcpro gcpro1;
2055
2056 CHECK_NUMBER (lines);
2057 if (! NILP (window))
2058 CHECK_WINDOW (window);
2059 else
2060 window = selected_window;
2061 w = XWINDOW (window);
2062
2063 old_buffer = Qnil;
2064 GCPRO1 (old_buffer);
2065 if (XBUFFER (w->buffer) != current_buffer)
2066 {
2067 /* Set the window's buffer temporarily to the current buffer. */
2068 old_buffer = w->buffer;
2069 XSETBUFFER (w->buffer, current_buffer);
2070 }
2071
2072 if (noninteractive)
2073 {
2074 struct position pos;
2075 pos = *vmotion (PT, XINT (lines), w);
2076 SET_PT_BOTH (pos.bufpos, pos.bytepos);
2077 }
2078 else
2079 {
2080 int it_start;
2081 int oselective;
2082 int it_overshoot_expected;
2083
2084 SET_TEXT_POS (pt, PT, PT_BYTE);
2085 start_display (&it, w, pt);
2086
2087 /* Scan from the start of the line containing PT. If we don't
2088 do this, we start moving with IT->current_x == 0, while PT is
2089 really at some x > 0. The effect is, in continuation lines, that
2090 we end up with the iterator placed at where it thinks X is 0,
2091 while the end position is really at some X > 0, the same X that
2092 PT had. */
2093 it_start = IT_CHARPOS (it);
2094
2095 /* We expect the call to move_it_to, further down, to overshoot
2096 if the starting point is on an image, stretch glyph,
2097 composition, or Lisp string. We won't need to backtrack in
2098 this situation, except for one corner case: when the Lisp
2099 string contains a newline. */
2100 if (it.method == GET_FROM_STRING)
2101 {
2102 const char *s = SDATA (it.string);
2103 const char *e = s + SBYTES (it.string);
2104
2105 while (s < e && *s != '\n')
2106 ++s;
2107
2108 /* If there is no newline in the string, we need to check
2109 whether there is a newline immediately after the string
2110 in move_it_to below. This may happen if there is an
2111 overlay with an after-string just before the newline. */
2112 it_overshoot_expected = (s == e) ? -1 : 0;
2113 }
2114 else
2115 it_overshoot_expected = (it.method == GET_FROM_IMAGE
2116 || it.method == GET_FROM_STRETCH
2117 || it.method == GET_FROM_COMPOSITION);
2118
2119 reseat_at_previous_visible_line_start (&it);
2120 it.current_x = it.hpos = 0;
2121 /* Temporarily disable selective display so we don't move too far */
2122 oselective = it.selective;
2123 it.selective = 0;
2124 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
2125 it.selective = oselective;
2126
2127 /* Move back if we got too far. This may happen if
2128 truncate-lines is on and PT is beyond right margin.
2129 Don't go back if the overshoot is expected (see above). */
2130 if (IT_CHARPOS (it) > it_start && XINT (lines) > 0
2131 && (!it_overshoot_expected
2132 || (it_overshoot_expected < 0
2133 && it.method == GET_FROM_BUFFER
2134 && it.c == '\n')))
2135 move_it_by_lines (&it, -1, 0);
2136
2137 it.vpos = 0;
2138 /* Do this even if LINES is 0, so that we move back
2139 to the beginning of the current line as we ought. */
2140 if (XINT (lines) >= 0 || IT_CHARPOS (it) > 0)
2141 move_it_by_lines (&it, XINT (lines), 0);
2142
2143 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
2144 }
2145
2146 if (BUFFERP (old_buffer))
2147 w->buffer = old_buffer;
2148
2149 RETURN_UNGCPRO (make_number (it.vpos));
2150 }
2151
2152
2153 \f
2154 /* File's initialization. */
2155
2156 void
2157 syms_of_indent ()
2158 {
2159 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
2160 doc: /* *Indentation can insert tabs if this is non-nil.
2161 Setting this variable automatically makes it local to the current buffer. */);
2162 indent_tabs_mode = 1;
2163
2164 defsubr (&Scurrent_indentation);
2165 defsubr (&Sindent_to);
2166 defsubr (&Scurrent_column);
2167 defsubr (&Smove_to_column);
2168 defsubr (&Svertical_motion);
2169 defsubr (&Scompute_motion);
2170 }
2171
2172 /* arch-tag: 9adfea44-71f7-4988-8ee3-96da15c502cc
2173 (do not change this comment) */