]> code.delx.au - gnu-emacs/blob - src/indent.c
(current_column_1): Eliminate argument POS; use PT and PT_BYTE.
[gnu-emacs] / src / indent.c
1 /* Indentation functions.
2 Copyright (C) 1985,86,87,88,93,94,95 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "charset.h"
26 #include "category.h"
27 #include "indent.h"
28 #include "frame.h"
29 #include "window.h"
30 #include "termchar.h"
31 #include "termopts.h"
32 #include "disptab.h"
33 #include "intervals.h"
34 #include "region-cache.h"
35
36 /* Indentation can insert tabs if this is non-zero;
37 otherwise always uses spaces */
38 int indent_tabs_mode;
39
40 #define min(a, b) ((a) < (b) ? (a) : (b))
41 #define max(a, b) ((a) > (b) ? (a) : (b))
42
43 #define CR 015
44
45 /* These three values memoize the current column to avoid recalculation */
46 /* Some things in set last_known_column_point to -1
47 to mark the memoized value as invalid */
48 /* Last value returned by current_column */
49 int last_known_column;
50 /* Value of point when current_column was called */
51 int last_known_column_point;
52 /* Value of MODIFF when current_column was called */
53 int last_known_column_modified;
54
55 static int current_column_1 ();
56 static int position_indentation ();
57
58 /* Cache of beginning of line found by the last call of
59 current_column. */
60 int current_column_bol_cache;
61
62 /* Get the display table to use for the current buffer. */
63
64 struct Lisp_Char_Table *
65 buffer_display_table ()
66 {
67 Lisp_Object thisbuf;
68
69 thisbuf = current_buffer->display_table;
70 if (DISP_TABLE_P (thisbuf))
71 return XCHAR_TABLE (thisbuf);
72 if (DISP_TABLE_P (Vstandard_display_table))
73 return XCHAR_TABLE (Vstandard_display_table);
74 return 0;
75 }
76 \f
77 /* Width run cache considerations. */
78
79 /* Return the width of character C under display table DP. */
80
81 static int
82 character_width (c, dp)
83 int c;
84 struct Lisp_Char_Table *dp;
85 {
86 Lisp_Object elt;
87
88 /* These width computations were determined by examining the cases
89 in display_text_line. */
90
91 /* Everything can be handled by the display table, if it's
92 present and the element is right. */
93 if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
94 return XVECTOR (elt)->size;
95
96 /* Some characters are special. */
97 if (c == '\n' || c == '\t' || c == '\015')
98 return 0;
99
100 /* Printing characters have width 1. */
101 else if (c >= 040 && c < 0177)
102 return 1;
103
104 /* Everybody else (control characters, metacharacters) has other
105 widths. We could return their actual widths here, but they
106 depend on things like ctl_arrow and crud like that, and they're
107 not very common at all. So we'll just claim we don't know their
108 widths. */
109 else
110 return 0;
111 }
112
113 /* Return true iff the display table DISPTAB specifies the same widths
114 for characters as WIDTHTAB. We use this to decide when to
115 invalidate the buffer's width_run_cache. */
116 int
117 disptab_matches_widthtab (disptab, widthtab)
118 struct Lisp_Char_Table *disptab;
119 struct Lisp_Vector *widthtab;
120 {
121 int i;
122
123 if (widthtab->size != 256)
124 abort ();
125
126 for (i = 0; i < 256; i++)
127 if (character_width (i, disptab)
128 != XFASTINT (widthtab->contents[i]))
129 return 0;
130
131 return 1;
132 }
133
134 /* Recompute BUF's width table, using the display table DISPTAB. */
135 void
136 recompute_width_table (buf, disptab)
137 struct buffer *buf;
138 struct Lisp_Char_Table *disptab;
139 {
140 int i;
141 struct Lisp_Vector *widthtab;
142
143 if (!VECTORP (buf->width_table))
144 buf->width_table = Fmake_vector (make_number (256), make_number (0));
145 widthtab = XVECTOR (buf->width_table);
146 if (widthtab->size != 256)
147 abort ();
148
149 for (i = 0; i < 256; i++)
150 XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
151 }
152
153 /* Allocate or free the width run cache, as requested by the current
154 state of current_buffer's cache_long_line_scans variable. */
155 static void
156 width_run_cache_on_off ()
157 {
158 if (NILP (current_buffer->cache_long_line_scans)
159 /* And, for the moment, this feature doesn't work on multibyte
160 characters. */
161 || !NILP (current_buffer->enable_multibyte_characters))
162 {
163 /* It should be off. */
164 if (current_buffer->width_run_cache)
165 {
166 free_region_cache (current_buffer->width_run_cache);
167 current_buffer->width_run_cache = 0;
168 current_buffer->width_table = Qnil;
169 }
170 }
171 else
172 {
173 /* It should be on. */
174 if (current_buffer->width_run_cache == 0)
175 {
176 current_buffer->width_run_cache = new_region_cache ();
177 recompute_width_table (current_buffer, buffer_display_table ());
178 }
179 }
180 }
181
182 \f
183 /* Skip some invisible characters starting from POS.
184 This includes characters invisible because of text properties
185 and characters invisible because of overlays.
186
187 If position POS is followed by invisible characters,
188 skip some of them and return the position after them.
189 Otherwise return POS itself.
190
191 Set *NEXT_BOUNDARY_P to the next position at which
192 it will be necessary to call this function again.
193
194 Don't scan past TO, and don't set *NEXT_BOUNDARY_P
195 to a value greater than TO.
196
197 If WINDOW is non-nil, and this buffer is displayed in WINDOW,
198 take account of overlays that apply only in WINDOW.
199
200 We don't necessarily skip all the invisible characters after POS
201 because that could take a long time. We skip a reasonable number
202 which can be skipped quickly. If there might be more invisible
203 characters immediately following, then *NEXT_BOUNDARY_P
204 will equal the return value. */
205
206 static int
207 skip_invisible (pos, next_boundary_p, to, window)
208 int pos;
209 int *next_boundary_p;
210 int to;
211 Lisp_Object window;
212 {
213 Lisp_Object prop, position, overlay_limit, proplimit;
214 Lisp_Object buffer;
215 int end;
216
217 XSETFASTINT (position, pos);
218 XSETBUFFER (buffer, current_buffer);
219
220 /* Give faster response for overlay lookup near POS. */
221 recenter_overlay_lists (current_buffer, pos);
222
223 /* We must not advance farther than the next overlay change.
224 The overlay change might change the invisible property;
225 or there might be overlay strings to be displayed there. */
226 overlay_limit = Fnext_overlay_change (position);
227 /* As for text properties, this gives a lower bound
228 for where the invisible text property could change. */
229 proplimit = Fnext_property_change (position, buffer, Qt);
230 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
231 proplimit = overlay_limit;
232 /* PROPLIMIT is now a lower bound for the next change
233 in invisible status. If that is plenty far away,
234 use that lower bound. */
235 if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
236 *next_boundary_p = XFASTINT (proplimit);
237 /* Otherwise, scan for the next `invisible' property change. */
238 else
239 {
240 /* Don't scan terribly far. */
241 XSETFASTINT (proplimit, min (pos + 100, to));
242 /* No matter what. don't go past next overlay change. */
243 if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
244 proplimit = overlay_limit;
245 end = XFASTINT (Fnext_single_property_change (position, Qinvisible,
246 buffer, proplimit));
247 #if 0
248 /* Don't put the boundary in the middle of multibyte form if
249 there is no actual property change. */
250 if (end == pos + 100
251 && !NILP (current_buffer->enable_multibyte_characters)
252 && end < ZV)
253 while (pos < end && !CHAR_HEAD_P (POS_ADDR (end)))
254 end--;
255 #endif
256 *next_boundary_p = end;
257 }
258 /* if the `invisible' property is set, we can skip to
259 the next property change */
260 if (!NILP (window) && EQ (XWINDOW (window)->buffer, buffer))
261 prop = Fget_char_property (position, Qinvisible, window);
262 else
263 prop = Fget_char_property (position, Qinvisible, buffer);
264 if (TEXT_PROP_MEANS_INVISIBLE (prop))
265 return *next_boundary_p;
266 return pos;
267 }
268 \f
269 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
270 "Return the horizontal position of point. Beginning of line is column 0.\n\
271 This is calculated by adding together the widths of all the displayed\n\
272 representations of the character between the start of the previous line\n\
273 and point. (eg control characters will have a width of 2 or 4, tabs\n\
274 will have a variable width)\n\
275 Ignores finite width of frame, which means that this function may return\n\
276 values greater than (frame-width).\n\
277 Whether the line is visible (if `selective-display' is t) has no effect;\n\
278 however, ^M is treated as end of line when `selective-display' is t.")
279 ()
280 {
281 Lisp_Object temp;
282 XSETFASTINT (temp, current_column ());
283 return temp;
284 }
285
286 /* Cancel any recorded value of the horizontal position. */
287
288 void
289 invalidate_current_column ()
290 {
291 last_known_column_point = 0;
292 }
293
294 int
295 current_column ()
296 {
297 register int col;
298 register unsigned char *ptr, *stop;
299 register int tab_seen;
300 int post_tab;
301 register int c;
302 register int tab_width = XINT (current_buffer->tab_width);
303 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
304 register struct Lisp_Char_Table *dp = buffer_display_table ();
305 int stopchar;
306
307 if (PT == last_known_column_point
308 && MODIFF == last_known_column_modified)
309 return last_known_column;
310
311 /* If the buffer has overlays, text properties,
312 or multibyte characters, use a more general algorithm. */
313 if (BUF_INTERVALS (current_buffer)
314 || !NILP (current_buffer->overlays_before)
315 || !NILP (current_buffer->overlays_after)
316 || Z != Z_BYTE)
317 return current_column_1 ();
318
319 /* Scan backwards from point to the previous newline,
320 counting width. Tab characters are the only complicated case. */
321
322 /* Make a pointer for decrementing through the chars before point. */
323 ptr = BYTE_POS_ADDR (PT_BYTE - 1) + 1;
324 /* Make a pointer to where consecutive chars leave off,
325 going backwards from point. */
326 if (PT == BEGV)
327 stop = ptr;
328 else if (PT <= GPT || BEGV > GPT)
329 stop = BEGV_ADDR;
330 else
331 stop = GAP_END_ADDR;
332
333 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
334
335 col = 0, tab_seen = 0, post_tab = 0;
336
337 while (1)
338 {
339 if (ptr == stop)
340 {
341 /* We stopped either for the beginning of the buffer
342 or for the gap. */
343 if (ptr == BEGV_ADDR)
344 break;
345 /* It was the gap. Jump back over it. */
346 stop = BEGV_ADDR;
347 ptr = GPT_ADDR;
348 /* Check whether that brings us to beginning of buffer. */
349 if (BEGV >= GPT) break;
350 }
351
352 c = *--ptr;
353 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
354 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
355 else if (c >= 040 && c < 0177)
356 col++;
357 else if (c == '\n'
358 || (c == '\r' && EQ (current_buffer->selective_display, Qt)))
359 {
360 ptr++;
361 break;
362 }
363 else if (c == '\t')
364 {
365 if (tab_seen)
366 col = ((col + tab_width) / tab_width) * tab_width;
367
368 post_tab += col;
369 col = 0;
370 tab_seen = 1;
371 }
372 else
373 col += (ctl_arrow && c < 0200) ? 2 : 4;
374 }
375
376 if (tab_seen)
377 {
378 col = ((col + tab_width) / tab_width) * tab_width;
379 col += post_tab;
380 }
381
382 if (ptr == BEGV_ADDR)
383 current_column_bol_cache = BEGV;
384 else
385 current_column_bol_cache = BYTE_TO_CHAR (PTR_BYTE_POS (ptr));
386
387 last_known_column = col;
388 last_known_column_point = PT;
389 last_known_column_modified = MODIFF;
390
391 return col;
392 }
393 \f
394 /* Return the column number of position POS
395 by scanning forward from the beginning of the line.
396 This function handles characters that are invisible
397 due to text properties or overlays. */
398
399 static int
400 current_column_1 ()
401 {
402 register int tab_width = XINT (current_buffer->tab_width);
403 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
404 register struct Lisp_Char_Table *dp = buffer_display_table ();
405 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
406
407 /* Start the scan at the beginning of this line with column number 0. */
408 register int col = 0;
409 int scan, scan_byte;
410 int next_boundary, next_boundary_byte;
411 int opoint = PT, opoint_byte = PT_BYTE;
412
413 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
414 current_column_bol_cache = PT;
415 scan = PT, scan_byte = PT_BYTE;
416 SET_PT_BOTH (opoint, opoint_byte);
417 next_boundary = scan;
418 next_boundary_byte = scan_byte;
419
420 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
421
422 /* Scan forward to the target position. */
423 while (scan < opoint)
424 {
425 int c;
426
427 /* Occasionally we may need to skip invisible text. */
428 while (scan == next_boundary)
429 {
430 int old_scan = scan;
431 /* This updates NEXT_BOUNDARY to the next place
432 where we might need to skip more invisible text. */
433 scan = skip_invisible (scan, &next_boundary, opoint, Qnil);
434 if (scan >= opoint)
435 goto endloop;
436 if (scan != old_scan)
437 scan_byte = CHAR_TO_BYTE (scan);
438 next_boundary_byte = CHAR_TO_BYTE (next_boundary);
439 }
440
441 c = FETCH_BYTE (scan_byte);
442 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
443 {
444 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
445 scan++;
446 scan_byte++;
447 continue;
448 }
449 if (c == '\n')
450 break;
451 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
452 break;
453 scan++;
454 scan_byte++;
455 if (c == '\t')
456 {
457 int prev_col = col;
458 col += tab_width;
459 col = col / tab_width * tab_width;
460 }
461 else if (multibyte && BASE_LEADING_CODE_P (c))
462 {
463 scan_byte--;
464 /* Start of multi-byte form. */
465 if (c == LEADING_CODE_COMPOSITION)
466 {
467 unsigned char *ptr = BYTE_POS_ADDR (scan_byte);
468
469 int cmpchar_id
470 = str_cmpchar_id (ptr, next_boundary_byte - scan_byte);
471 if (cmpchar_id >= 0)
472 {
473 scan_byte += cmpchar_table[cmpchar_id]->len;
474 col += cmpchar_table[cmpchar_id]->width;
475 }
476 else
477 { /* invalid composite character */
478 scan_byte++;
479 col += 4;
480 }
481 }
482 else
483 {
484 /* Here, we check that the following bytes are valid
485 constituents of multi-byte form. */
486 int len = BYTES_BY_CHAR_HEAD (c), i;
487
488 for (i = 1, scan_byte++; i < len; i++, scan_byte++)
489 /* We don't need range checking for PTR because there
490 are anchors (`\0') at GAP and Z. */
491 if (CHAR_HEAD_P (FETCH_BYTE (scan_byte)))
492 break;
493
494 if (i < len)
495 col += 4, scan_byte -= i - 1;
496 else
497 col += WIDTH_BY_CHAR_HEAD (c);
498 }
499 }
500 else if (ctl_arrow && (c < 040 || c == 0177))
501 col += 2;
502 else if (c < 040 || c >= 0177)
503 col += 4;
504 else
505 col++;
506 }
507 endloop:
508
509 last_known_column = col;
510 last_known_column_point = PT;
511 last_known_column_modified = MODIFF;
512
513 return col;
514 }
515 \f
516 /* Return the width in columns of the part of STRING from BEG to END.
517 If BEG is nil, that stands for the beginning of STRING.
518 If END is nil, that stands for the end of STRING. */
519
520 static int
521 string_display_width (string, beg, end)
522 Lisp_Object string, beg, end;
523 {
524 register int col;
525 register unsigned char *ptr, *stop;
526 register int tab_seen;
527 int post_tab;
528 register int c;
529 register int tab_width = XINT (current_buffer->tab_width);
530 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
531 register struct Lisp_Char_Table *dp = buffer_display_table ();
532 int b, e;
533
534 if (NILP (end))
535 e = XSTRING (string)->size;
536 else
537 {
538 CHECK_NUMBER (end, 0);
539 e = XINT (end);
540 }
541
542 if (NILP (beg))
543 b = 0;
544 else
545 {
546 CHECK_NUMBER (beg, 0);
547 b = XINT (beg);
548 }
549
550 /* Make a pointer for decrementing through the chars before point. */
551 ptr = XSTRING (string)->data + e;
552 /* Make a pointer to where consecutive chars leave off,
553 going backwards from point. */
554 stop = XSTRING (string)->data + b;
555
556 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
557
558 col = 0, tab_seen = 0, post_tab = 0;
559
560 while (1)
561 {
562 if (ptr == stop)
563 break;
564
565 c = *--ptr;
566 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
567 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
568 else if (c >= 040 && c < 0177)
569 col++;
570 else if (c == '\n')
571 break;
572 else if (c == '\t')
573 {
574 if (tab_seen)
575 col = ((col + tab_width) / tab_width) * tab_width;
576
577 post_tab += col;
578 col = 0;
579 tab_seen = 1;
580 }
581 else
582 col += (ctl_arrow && c < 0200) ? 2 : 4;
583 }
584
585 if (tab_seen)
586 {
587 col = ((col + tab_width) / tab_width) * tab_width;
588 col += post_tab;
589 }
590
591 return col;
592 }
593 \f
594 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
595 "Indent from point with tabs and spaces until COLUMN is reached.\n\
596 Optional second argument MININUM says always do at least MININUM spaces\n\
597 even if that goes past COLUMN; by default, MININUM is zero.")
598 (column, minimum)
599 Lisp_Object column, minimum;
600 {
601 int mincol;
602 register int fromcol;
603 register int tab_width = XINT (current_buffer->tab_width);
604
605 CHECK_NUMBER (column, 0);
606 if (NILP (minimum))
607 XSETFASTINT (minimum, 0);
608 CHECK_NUMBER (minimum, 1);
609
610 fromcol = current_column ();
611 mincol = fromcol + XINT (minimum);
612 if (mincol < XINT (column)) mincol = XINT (column);
613
614 if (fromcol == mincol)
615 return make_number (mincol);
616
617 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
618
619 if (indent_tabs_mode)
620 {
621 Lisp_Object n;
622 XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
623 if (XFASTINT (n) != 0)
624 {
625 Finsert_char (make_number ('\t'), n, Qt);
626
627 fromcol = (mincol / tab_width) * tab_width;
628 }
629 }
630
631 XSETFASTINT (column, mincol - fromcol);
632 Finsert_char (make_number (' '), column, Qt);
633
634 last_known_column = mincol;
635 last_known_column_point = PT;
636 last_known_column_modified = MODIFF;
637
638 XSETINT (column, mincol);
639 return column;
640 }
641
642 \f
643 DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
644 0, 0, 0,
645 "Return the indentation of the current line.\n\
646 This is the horizontal position of the character\n\
647 following any initial whitespace.")
648 ()
649 {
650 Lisp_Object val;
651 int opoint = PT, opoint_byte = PT_BYTE;
652
653 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
654
655 XSETFASTINT (val, position_indentation (PT_BYTE));
656 SET_PT_BOTH (opoint, opoint_byte);
657 return val;
658 }
659
660 static int
661 position_indentation (pos_byte)
662 register int pos_byte;
663 {
664 register int column = 0;
665 register int tab_width = XINT (current_buffer->tab_width);
666 register unsigned char *p;
667 register unsigned char *stop;
668 unsigned char *start;
669 int next_boundary_byte = pos_byte;
670 int ceiling = next_boundary_byte;
671
672 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
673
674 p = BYTE_POS_ADDR (pos_byte);
675 /* STOP records the value of P at which we will need
676 to think about the gap, or about invisible text,
677 or about the end of the buffer. */
678 stop = p;
679 /* START records the starting value of P. */
680 start = p;
681 while (1)
682 {
683 while (p == stop)
684 {
685 int stop_pos_byte;
686
687 /* If we have updated P, set POS_BYTE to match.
688 The first time we enter the loop, POS_BYTE is already right. */
689 if (p != start)
690 pos_byte = PTR_BYTE_POS (p);
691 /* Consider the various reasons STOP might have been set here. */
692 if (pos_byte == ZV_BYTE)
693 return column;
694 if (pos_byte == next_boundary_byte)
695 {
696 int next_boundary;
697 int pos = BYTE_TO_CHAR (pos_byte);
698 pos = skip_invisible (pos, &next_boundary, ZV, Qnil);
699 pos_byte = CHAR_TO_BYTE (pos);
700 next_boundary_byte = CHAR_TO_BYTE (next_boundary);
701 }
702 if (pos_byte >= ceiling)
703 ceiling = BUFFER_CEILING_OF (pos_byte) + 1;
704 /* Compute the next place we need to stop and think,
705 and set STOP accordingly. */
706 stop_pos_byte = min (ceiling, next_boundary_byte);
707 /* The -1 and +1 arrange to point at the first byte of gap
708 (if STOP_POS_BYTE is the position of the gap)
709 rather than at the data after the gap. */
710
711 stop = BYTE_POS_ADDR (stop_pos_byte - 1) + 1;
712 p = BYTE_POS_ADDR (pos_byte);
713 }
714 switch (*p++)
715 {
716 case 0240:
717 if (! NILP (current_buffer->enable_multibyte_characters))
718 return column;
719 case ' ':
720 column++;
721 break;
722 case '\t':
723 column += tab_width - column % tab_width;
724 break;
725 default:
726 if (ASCII_BYTE_P (p[-1])
727 || NILP (current_buffer->enable_multibyte_characters))
728 return column;
729 {
730 int c;
731 pos_byte = PTR_BYTE_POS (p - 1);
732 c = FETCH_MULTIBYTE_CHAR (pos_byte);
733 if (CHAR_HAS_CATEGORY (c, ' '))
734 {
735 column++;
736 INC_POS (pos_byte);
737 p = BYTE_POS_ADDR (pos_byte);
738 }
739 else
740 return column;
741 }
742 }
743 }
744 }
745
746 /* Test whether the line beginning at POS is indented beyond COLUMN.
747 Blank lines are treated as if they had the same indentation as the
748 preceding line. */
749
750 int
751 indented_beyond_p (pos, pos_byte, column)
752 int pos, pos_byte, column;
753 {
754 Lisp_Object val;
755 int opoint = PT, opoint_byte = PT_BYTE;
756
757 SET_PT_BOTH (pos, pos_byte);
758 while (PT > BEGV && FETCH_BYTE (PT_BYTE) == '\n')
759 scan_newline (PT - 1, PT_BYTE - 1, BEGV, BEGV_BYTE, -1, 0);
760
761 XSETFASTINT (val, position_indentation (PT_BYTE));
762 SET_PT_BOTH (opoint, opoint_byte);
763 return val;
764 }
765 \f
766 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
767 "Move point to column COLUMN in the current line.\n\
768 The column of a character is calculated by adding together the widths\n\
769 as displayed of the previous characters in the line.\n\
770 This function ignores line-continuation;\n\
771 there is no upper limit on the column number a character can have\n\
772 and horizontal scrolling has no effect.\n\
773 \n\
774 If specified column is within a character, point goes after that character.\n\
775 If it's past end of line, point goes to end of line.\n\n\
776 A non-nil second (optional) argument FORCE means, if the line\n\
777 is too short to reach column COLUMN then add spaces/tabs to get there,\n\
778 and if COLUMN is in the middle of a tab character, change it to spaces.\n\
779 \n\
780 The return value is the current column.")
781 (column, force)
782 Lisp_Object column, force;
783 {
784 register int pos;
785 register int col = current_column ();
786 register int goal;
787 register int end;
788 register int tab_width = XINT (current_buffer->tab_width);
789 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
790 register struct Lisp_Char_Table *dp = buffer_display_table ();
791 register int multibyte = !NILP (current_buffer->enable_multibyte_characters);
792
793 Lisp_Object val;
794 int prev_col;
795 int c;
796 int next_boundary;
797
798 int pos_byte, end_byte, next_boundary_byte;
799
800 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
801 CHECK_NATNUM (column, 0);
802 goal = XINT (column);
803
804 pos = PT;
805 pos_byte = PT_BYTE;
806 end = ZV;
807 end_byte = ZV_BYTE;
808 next_boundary = pos;
809 next_boundary_byte = PT_BYTE;
810
811 /* If we're starting past the desired column,
812 back up to beginning of line and scan from there. */
813 if (col > goal)
814 {
815 end = pos;
816 pos = current_column_bol_cache;
817 pos_byte = CHAR_TO_BYTE (pos);
818 col = 0;
819 }
820
821 while (pos < end)
822 {
823 while (pos == next_boundary)
824 {
825 int prev = pos;
826 pos = skip_invisible (pos, &next_boundary, end, Qnil);
827 if (pos != prev)
828 pos_byte = CHAR_TO_BYTE (pos);
829 next_boundary_byte = CHAR_TO_BYTE (next_boundary);
830 if (pos >= end)
831 goto endloop;
832 }
833
834 /* Test reaching the goal column. We do this after skipping
835 invisible characters, so that we put point before the
836 character on which the cursor will appear. */
837 if (col >= goal)
838 break;
839
840 c = FETCH_BYTE (pos_byte);
841 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
842 {
843 col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
844 pos_byte++;
845 pos++;
846 continue;
847 }
848 if (c == '\n')
849 break;
850 if (c == '\r' && EQ (current_buffer->selective_display, Qt))
851 break;
852 pos++;
853 pos_byte++;
854 if (c == '\t')
855 {
856 prev_col = col;
857 col += tab_width;
858 col = col / tab_width * tab_width;
859 }
860 else if (ctl_arrow && (c < 040 || c == 0177))
861 col += 2;
862 else if (c < 040 || c == 0177)
863 col += 4;
864 else if (c < 0177)
865 col++;
866 else if (multibyte && BASE_LEADING_CODE_P (c))
867 {
868 /* Start of multi-byte form. */
869 unsigned char *ptr;
870
871 pos_byte--; /* rewind to the character head */
872 ptr = BYTE_POS_ADDR (pos_byte);
873 if (c == LEADING_CODE_COMPOSITION)
874 {
875 int cmpchar_id = str_cmpchar_id (ptr, end_byte - pos_byte);
876
877 if (cmpchar_id >= 0)
878 {
879 col += cmpchar_table[cmpchar_id]->width;
880 pos_byte += cmpchar_table[cmpchar_id]->len;
881 }
882 else
883 { /* invalid composite character */
884 col += 4;
885 pos_byte++;
886 }
887 }
888 else
889 {
890 /* Here, we check that the following bytes are valid
891 constituents of multi-byte form. */
892 int len = BYTES_BY_CHAR_HEAD (c), i;
893
894 for (i = 1, ptr++; i < len; i++, ptr++)
895 /* We don't need range checking for PTR because there
896 are anchors (`\0') both at GPT and Z. */
897 if (CHAR_HEAD_P (*ptr))
898 break;
899
900 if (i < len)
901 col += 4, pos_byte++;
902 else
903 col += WIDTH_BY_CHAR_HEAD (c), pos_byte += i;
904 }
905 }
906 else
907 col += 4;
908 }
909 endloop:
910
911 SET_PT_BOTH (pos, pos_byte);
912
913 /* If a tab char made us overshoot, change it to spaces
914 and scan through it again. */
915 if (!NILP (force) && col > goal && c == '\t' && prev_col < goal)
916 {
917 int old_point, old_point_byte;
918
919 del_range (PT - 1, PT);
920 Findent_to (make_number (goal), Qnil);
921 old_point = PT;
922 old_point_byte = PT_BYTE;
923 Findent_to (make_number (col), Qnil);
924 SET_PT_BOTH (old_point, old_point_byte);
925 /* Set the last_known... vars consistently. */
926 col = goal;
927 }
928
929 /* If line ends prematurely, add space to the end. */
930 if (col < goal && !NILP (force))
931 Findent_to (make_number (col = goal), Qnil);
932
933 last_known_column = col;
934 last_known_column_point = PT;
935 last_known_column_modified = MODIFF;
936
937 XSETFASTINT (val, col);
938 return val;
939 }
940 \f
941 /* compute_motion: compute buffer posn given screen posn and vice versa */
942
943 struct position val_compute_motion;
944
945 /* Scan the current buffer forward from offset FROM, pretending that
946 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
947 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
948 and return the ending buffer position and screen location. If we
949 can't hit the requested column exactly (because of a tab or other
950 multi-column character), overshoot.
951
952 DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
953 at FROM. This is the case if FROMVPOS and FROMVPOS came from an
954 earlier call to compute_motion. The other common case is that FROMHPOS
955 is zero and FROM is a position that "belongs" at column zero, but might
956 be shifted by overlay strings; in this case DID_MOTION should be 0.
957
958 WIDTH is the number of columns available to display text;
959 compute_motion uses this to handle continuation lines and such.
960 HSCROLL is the number of columns not being displayed at the left
961 margin; this is usually taken from a window's hscroll member.
962 TAB_OFFSET is the number of columns of the first tab that aren't
963 being displayed, perhaps because of a continuation line or
964 something.
965
966 compute_motion returns a pointer to a struct position. The bufpos
967 member gives the buffer position at the end of the scan, and hpos
968 and vpos give its cartesian location. prevhpos is the column at
969 which the character before bufpos started, and contin is non-zero
970 if we reached the current line by continuing the previous.
971
972 Note that FROMHPOS and TOHPOS should be expressed in real screen
973 columns, taking HSCROLL and the truncation glyph at the left margin
974 into account. That is, beginning-of-line moves you to the hpos
975 -HSCROLL + (HSCROLL > 0).
976
977 For example, to find the buffer position of column COL of line LINE
978 of a certain window, pass the window's starting location as FROM
979 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
980 Pass the buffer's ZV as TO, to limit the scan to the end of the
981 visible section of the buffer, and pass LINE and COL as TOVPOS and
982 TOHPOS.
983
984 When displaying in window w, a typical formula for WIDTH is:
985
986 window_width - 1
987 - (has_vertical_scroll_bars
988 ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
989 : (window_width + window_left != frame_width))
990
991 where
992 window_width is XFASTINT (w->width),
993 window_left is XFASTINT (w->left),
994 has_vertical_scroll_bars is
995 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
996 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
997
998 Or you can let window_internal_width do this all for you, and write:
999 window_internal_width (w) - 1
1000
1001 The `-1' accounts for the continuation-line backslashes; the rest
1002 accounts for window borders if the window is split horizontally, and
1003 the scroll bars if they are turned on. */
1004
1005 struct position *
1006 compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win)
1007 int from, fromvpos, fromhpos, to, tovpos, tohpos;
1008 int did_motion;
1009 register int width;
1010 int hscroll, tab_offset;
1011 struct window *win;
1012 {
1013 register int hpos = fromhpos;
1014 register int vpos = fromvpos;
1015
1016 register int pos;
1017 int pos_byte;
1018 register int c;
1019 register int tab_width = XFASTINT (current_buffer->tab_width);
1020 register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
1021 register struct Lisp_Char_Table *dp = window_display_table (win);
1022 int selective
1023 = (INTEGERP (current_buffer->selective_display)
1024 ? XINT (current_buffer->selective_display)
1025 : !NILP (current_buffer->selective_display) ? -1 : 0);
1026 int prev_hpos = 0;
1027 int selective_rlen
1028 = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
1029 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0);
1030 /* The next location where the `invisible' property changes, or an
1031 overlay starts or ends. */
1032 int next_boundary = from;
1033
1034 /* For computing runs of characters with similar widths.
1035 Invariant: width_run_width is zero, or all the characters
1036 from width_run_start to width_run_end have a fixed width of
1037 width_run_width. */
1038 int width_run_start = from;
1039 int width_run_end = from;
1040 int width_run_width = 0;
1041 Lisp_Object *width_table;
1042 Lisp_Object buffer;
1043
1044 /* The next buffer pos where we should consult the width run cache. */
1045 int next_width_run = from;
1046 Lisp_Object window;
1047
1048 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1049 int wide_column = 0; /* Set to 1 when a previous character
1050 is wide-colomn. */
1051 int prev_pos; /* Previous buffer position. */
1052 int prev_pos_byte; /* Previous buffer position. */
1053 int contin_hpos; /* HPOS of last column of continued line. */
1054 int prev_tab_offset; /* Previous tab offset. */
1055
1056 XSETBUFFER (buffer, current_buffer);
1057 XSETWINDOW (window, win);
1058
1059 width_run_cache_on_off ();
1060 if (dp == buffer_display_table ())
1061 width_table = (VECTORP (current_buffer->width_table)
1062 ? XVECTOR (current_buffer->width_table)->contents
1063 : 0);
1064 else
1065 /* If the window has its own display table, we can't use the width
1066 run cache, because that's based on the buffer's display table. */
1067 width_table = 0;
1068
1069 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
1070
1071 pos = prev_pos = from;
1072 pos_byte = prev_pos_byte = CHAR_TO_BYTE (from);
1073 contin_hpos = 0;
1074 prev_tab_offset = tab_offset;
1075 while (1)
1076 {
1077 while (pos == next_boundary)
1078 {
1079 int pos_here = pos;
1080 int newpos;
1081
1082 /* If the caller says that the screen position came from an earlier
1083 call to compute_motion, then we've already accounted for the
1084 overlay strings at point. This is only true the first time
1085 through, so clear the flag after testing it. */
1086 if (!did_motion)
1087 /* We need to skip past the overlay strings. Currently those
1088 strings must not contain TAB;
1089 if we want to relax that restriction, something will have
1090 to be changed here. */
1091 {
1092 unsigned char *ovstr;
1093 int ovlen = overlay_strings (pos, win, &ovstr);
1094 hpos += (multibyte ? strwidth (ovstr, ovlen) : ovlen);
1095 }
1096 did_motion = 0;
1097
1098 if (pos >= to)
1099 break;
1100
1101 /* Advance POS past invisible characters
1102 (but not necessarily all that there are here),
1103 and store in next_boundary the next position where
1104 we need to call skip_invisible. */
1105 newpos = skip_invisible (pos, &next_boundary, to, window);
1106
1107 if (newpos >= to)
1108 goto after_loop;
1109
1110 if (newpos != pos_here)
1111 {
1112 pos = newpos;
1113 pos_byte = CHAR_TO_BYTE (pos);
1114 }
1115 }
1116
1117 /* Handle right margin. */
1118 /* Note on a wide-column character.
1119
1120 Characters are classified into the following three categories
1121 according to the width (columns occupied on screen).
1122
1123 (1) single-column character: ex. `a'
1124 (2) multi-column character: ex. `^A', TAB, `\033'
1125 (3) wide-column character: ex. Japanese character, Chinese character
1126 (In the following example, `W_' stands for them.)
1127
1128 Multi-column characters can be divided around the right margin,
1129 but wide-column characters cannot.
1130
1131 NOTE:
1132
1133 (*) The cursor is placed on the next character after the point.
1134
1135 ----------
1136 abcdefghi\
1137 j ^---- next after the point
1138 ^--- next char. after the point.
1139 ----------
1140 In case of sigle-column character
1141
1142 ----------
1143 abcdefgh\\
1144 033 ^---- next after the point, next char. after the point.
1145 ----------
1146 In case of multi-column character
1147
1148 ----------
1149 abcdefgh\\
1150 W_ ^---- next after the point
1151 ^---- next char. after the point.
1152 ----------
1153 In case of wide-column character
1154
1155 The problem here is continuation at a wide-column character.
1156 In this case, the line may shorter less than WIDTH.
1157 And we find the continuation AFTER it occurs.
1158
1159 */
1160
1161 if (hpos > width)
1162 {
1163 if (hscroll
1164 || (truncate_partial_width_windows
1165 && width + 1 < FRAME_WIDTH (XFRAME (WINDOW_FRAME (win))))
1166 || !NILP (current_buffer->truncate_lines))
1167 {
1168 /* Truncating: skip to newline. */
1169 if (pos <= to) /* This IF is needed because we may past TO */
1170 {
1171 pos = find_before_next_newline (pos, to, 1);
1172 pos_byte = CHAR_TO_BYTE (pos);
1173 }
1174 hpos = width;
1175 /* If we just skipped next_boundary,
1176 loop around in the main while
1177 and handle it. */
1178 if (pos >= next_boundary)
1179 next_boundary = pos + 1;
1180 prev_hpos = width;
1181 prev_tab_offset = tab_offset;
1182 }
1183 else
1184 {
1185 /* Continuing. */
1186 /* Remember the previous value. */
1187 prev_tab_offset = tab_offset;
1188
1189 if (wide_column)
1190 {
1191 hpos -= prev_hpos;
1192 tab_offset += prev_hpos;
1193 }
1194 else
1195 {
1196 tab_offset += width;
1197 hpos -= width;
1198 }
1199 vpos++;
1200 contin_hpos = prev_hpos;
1201 prev_hpos = 0;
1202 }
1203 }
1204
1205 /* Stop if past the target buffer position or screen position. */
1206 if (pos > to)
1207 {
1208 /* Go back to the previous position. */
1209 pos = prev_pos;
1210 pos_byte = prev_pos_byte;
1211 hpos = prev_hpos;
1212 tab_offset = prev_tab_offset;
1213
1214 /* NOTE on contin_hpos, hpos, and prev_hpos.
1215
1216 ----------
1217 abcdefgh\\
1218 W_ ^---- contin_hpos
1219 | ^----- hpos
1220 \---- prev_hpos
1221 ----------
1222 */
1223
1224 if (contin_hpos && prev_hpos == 0
1225 && contin_hpos < width && !wide_column)
1226 {
1227 /* Line breaking occurs in the middle of multi-column
1228 character. Go back to previous line. */
1229 hpos = contin_hpos;
1230 vpos = vpos - 1;
1231 }
1232 else if (c == '\n')
1233 /* If previous character is NEWLINE,
1234 set VPOS back to previous line */
1235 vpos = vpos - 1;
1236 break;
1237 }
1238
1239 if (vpos > tovpos || vpos == tovpos && hpos >= tohpos)
1240 {
1241 if (contin_hpos && prev_hpos == 0
1242 && ((hpos > tohpos && contin_hpos == width) || wide_column))
1243 { /* Line breaks because we can't put the character at the
1244 previous line any more. It is not the multi-column
1245 character continued in middle. Go back to previous
1246 buffer position, screen position, and set tab offset
1247 to previous value. It's the beginning of the
1248 line. */
1249 pos = prev_pos;
1250 pos_byte = prev_pos_byte;
1251 hpos = prev_hpos;
1252 tab_offset = prev_tab_offset;
1253 }
1254 break;
1255 }
1256 if (pos == ZV) /* We cannot go beyond ZV. Stop here. */
1257 break;
1258
1259 prev_hpos = hpos;
1260 prev_pos = pos;
1261 prev_pos_byte = pos_byte;
1262 wide_column = 0;
1263
1264 /* Consult the width run cache to see if we can avoid inspecting
1265 the text character-by-character. */
1266 if (current_buffer->width_run_cache && pos >= next_width_run)
1267 {
1268 int run_end;
1269 int common_width
1270 = region_cache_forward (current_buffer,
1271 current_buffer->width_run_cache,
1272 pos, &run_end);
1273
1274 /* A width of zero means the character's width varies (like
1275 a tab), is meaningless (like a newline), or we just don't
1276 want to skip over it for some other reason. */
1277 if (common_width != 0)
1278 {
1279 int run_end_hpos;
1280
1281 /* Don't go past the final buffer posn the user
1282 requested. */
1283 if (run_end > to)
1284 run_end = to;
1285
1286 run_end_hpos = hpos + (run_end - pos) * common_width;
1287
1288 /* Don't go past the final horizontal position the user
1289 requested. */
1290 if (vpos == tovpos && run_end_hpos > tohpos)
1291 {
1292 run_end = pos + (tohpos - hpos) / common_width;
1293 run_end_hpos = hpos + (run_end - pos) * common_width;
1294 }
1295
1296 /* Don't go past the margin. */
1297 if (run_end_hpos >= width)
1298 {
1299 run_end = pos + (width - hpos) / common_width;
1300 run_end_hpos = hpos + (run_end - pos) * common_width;
1301 }
1302
1303 hpos = run_end_hpos;
1304 if (run_end > pos)
1305 prev_hpos = hpos - common_width;
1306 if (pos != run_end)
1307 {
1308 pos = run_end;
1309 pos_byte = CHAR_TO_BYTE (pos);
1310 }
1311 }
1312
1313 next_width_run = run_end + 1;
1314 }
1315
1316 /* We have to scan the text character-by-character. */
1317 else
1318 {
1319 c = FETCH_BYTE (pos_byte);
1320 pos++, pos_byte++;
1321
1322 /* Perhaps add some info to the width_run_cache. */
1323 if (current_buffer->width_run_cache)
1324 {
1325 /* Is this character part of the current run? If so, extend
1326 the run. */
1327 if (pos - 1 == width_run_end
1328 && XFASTINT (width_table[c]) == width_run_width)
1329 width_run_end = pos;
1330
1331 /* The previous run is over, since this is a character at a
1332 different position, or a different width. */
1333 else
1334 {
1335 /* Have we accumulated a run to put in the cache?
1336 (Currently, we only cache runs of width == 1). */
1337 if (width_run_start < width_run_end
1338 && width_run_width == 1)
1339 know_region_cache (current_buffer,
1340 current_buffer->width_run_cache,
1341 width_run_start, width_run_end);
1342
1343 /* Start recording a new width run. */
1344 width_run_width = XFASTINT (width_table[c]);
1345 width_run_start = pos - 1;
1346 width_run_end = pos;
1347 }
1348 }
1349
1350 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c))
1351 && ! (multibyte && BASE_LEADING_CODE_P (c)))
1352 hpos += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
1353 else if (c >= 040 && c < 0177)
1354 hpos++;
1355 else if (c == '\t')
1356 {
1357 int tem = (hpos + tab_offset + hscroll - (hscroll > 0)) % tab_width;
1358 if (tem < 0)
1359 tem += tab_width;
1360 hpos += tab_width - tem;
1361 }
1362 else if (c == '\n')
1363 {
1364 if (selective > 0
1365 && indented_beyond_p (pos, pos_byte, selective))
1366 {
1367 /* If (pos == to), we don't have to take care of
1368 selective display. */
1369 if (pos < to)
1370 {
1371 /* Skip any number of invisible lines all at once */
1372 do
1373 {
1374 pos = find_before_next_newline (pos, to, 1) + 1;
1375 pos_byte = CHAR_TO_BYTE (pos);
1376 }
1377 while (pos < to
1378 && indented_beyond_p (pos, pos_byte, selective));
1379 /* Allow for the " ..." that is displayed for them. */
1380 if (selective_rlen)
1381 {
1382 hpos += selective_rlen;
1383 if (hpos >= width)
1384 hpos = width;
1385 }
1386 DEC_BOTH (pos, pos_byte);
1387 /* We have skipped the invis text, but not the
1388 newline after. */
1389 }
1390 }
1391 else
1392 {
1393 /* A visible line. */
1394 vpos++;
1395 hpos = 0;
1396 hpos -= hscroll;
1397 /* Count the truncation glyph on column 0 */
1398 if (hscroll > 0)
1399 hpos++;
1400 tab_offset = 0;
1401 }
1402 contin_hpos = 0;
1403 }
1404 else if (c == CR && selective < 0)
1405 {
1406 /* In selective display mode,
1407 everything from a ^M to the end of the line is invisible.
1408 Stop *before* the real newline. */
1409 if (pos < to)
1410 {
1411 pos = find_before_next_newline (pos, to, 1);
1412 pos_byte = CHAR_TO_BYTE (pos);
1413 }
1414 /* If we just skipped next_boundary,
1415 loop around in the main while
1416 and handle it. */
1417 if (pos > next_boundary)
1418 next_boundary = pos;
1419 /* Allow for the " ..." that is displayed for them. */
1420 if (selective_rlen)
1421 {
1422 hpos += selective_rlen;
1423 if (hpos >= width)
1424 hpos = width;
1425 }
1426 }
1427 else if (multibyte && BASE_LEADING_CODE_P (c))
1428 {
1429 /* Start of multi-byte form. */
1430 unsigned char *ptr;
1431 int len, actual_len;
1432
1433 pos--, pos_byte--; /* rewind POS */
1434
1435 ptr = BYTE_POS_ADDR (pos_byte);
1436 len = BUFFER_CEILING_OF (pos_byte) - pos_byte + 1;
1437
1438 c = STRING_CHAR_AND_LENGTH (ptr, len, actual_len);
1439
1440 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1441 hpos += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
1442 else if (actual_len == 1)
1443 hpos += 4;
1444 else if (COMPOSITE_CHAR_P (c))
1445 {
1446 int id = COMPOSITE_CHAR_ID (c);
1447 int width = (id < n_cmpchars) ? cmpchar_table[id]->width : 0;
1448 hpos += width;
1449 if (width > 1)
1450 wide_column = 1;
1451 }
1452 else
1453 {
1454 int width = WIDTH_BY_CHAR_HEAD (*ptr);
1455 hpos += width;
1456 if (width > 1)
1457 wide_column = 1;
1458 }
1459
1460 pos++;
1461 pos_byte += actual_len;
1462 }
1463 else
1464 hpos += (ctl_arrow && c < 0200) ? 2 : 4;
1465 }
1466 }
1467
1468 after_loop:
1469
1470 /* Remember any final width run in the cache. */
1471 if (current_buffer->width_run_cache
1472 && width_run_width == 1
1473 && width_run_start < width_run_end)
1474 know_region_cache (current_buffer, current_buffer->width_run_cache,
1475 width_run_start, width_run_end);
1476
1477 val_compute_motion.bufpos = pos;
1478 val_compute_motion.bytepos = pos_byte;
1479 val_compute_motion.hpos = hpos;
1480 val_compute_motion.vpos = vpos;
1481 val_compute_motion.prevhpos = prev_hpos;
1482 /* We alalways handle all of them here; none of them remain to do. */
1483 val_compute_motion.ovstring_chars_done = 0;
1484
1485 /* Nonzero if have just continued a line */
1486 val_compute_motion.contin = (contin_hpos && prev_hpos == 0);
1487
1488 return &val_compute_motion;
1489 }
1490
1491 #if 0 /* The doc string is too long for some compilers,
1492 but make-docfile can find it in this comment. */
1493 DEFUN ("compute-motion", Ffoo, Sfoo, 7, 7, 0,
1494 "Scan through the current buffer, calculating screen position.\n\
1495 Scan the current buffer forward from offset FROM,\n\
1496 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--\n\
1497 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--\n\
1498 and return the ending buffer position and screen location.\n\
1499 \n\
1500 There are three additional arguments:\n\
1501 \n\
1502 WIDTH is the number of columns available to display text;\n\
1503 this affects handling of continuation lines.\n\
1504 This is usually the value returned by `window-width', less one (to allow\n\
1505 for the continuation glyph).\n\
1506 \n\
1507 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).\n\
1508 HSCROLL is the number of columns not being displayed at the left\n\
1509 margin; this is usually taken from a window's hscroll member.\n\
1510 TAB-OFFSET is the number of columns of the first tab that aren't\n\
1511 being displayed, perhaps because the line was continued within it.\n\
1512 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.\n\
1513 \n\
1514 WINDOW is the window to operate on. It is used to choose the display table;\n\
1515 if it is showing the current buffer, it is used also for\n\
1516 deciding which overlay properties apply.\n\
1517 Note that `compute-motion' always operates on the current buffer.\n\
1518 \n\
1519 The value is a list of five elements:\n\
1520 (POS HPOS VPOS PREVHPOS CONTIN)\n\
1521 POS is the buffer position where the scan stopped.\n\
1522 VPOS is the vertical position where the scan stopped.\n\
1523 HPOS is the horizontal position where the scan stopped.\n\
1524 \n\
1525 PREVHPOS is the horizontal position one character back from POS.\n\
1526 CONTIN is t if a line was continued after (or within) the previous character.\n\
1527 \n\
1528 For example, to find the buffer position of column COL of line LINE\n\
1529 of a certain window, pass the window's starting location as FROM\n\
1530 and the window's upper-left coordinates as FROMPOS.\n\
1531 Pass the buffer's (point-max) as TO, to limit the scan to the end of the\n\
1532 visible section of the buffer, and pass LINE and COL as TOPOS.")
1533 (from, frompos, to, topos, width, offsets, window)
1534 #endif
1535
1536 DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
1537 0)
1538 (from, frompos, to, topos, width, offsets, window)
1539 Lisp_Object from, frompos, to, topos;
1540 Lisp_Object width, offsets, window;
1541 {
1542 Lisp_Object bufpos, hpos, vpos, prevhpos, contin;
1543 struct position *pos;
1544 int hscroll, tab_offset;
1545
1546 CHECK_NUMBER_COERCE_MARKER (from, 0);
1547 CHECK_CONS (frompos, 0);
1548 CHECK_NUMBER (XCONS (frompos)->car, 0);
1549 CHECK_NUMBER (XCONS (frompos)->cdr, 0);
1550 CHECK_NUMBER_COERCE_MARKER (to, 0);
1551 CHECK_CONS (topos, 0);
1552 CHECK_NUMBER (XCONS (topos)->car, 0);
1553 CHECK_NUMBER (XCONS (topos)->cdr, 0);
1554 CHECK_NUMBER (width, 0);
1555 if (!NILP (offsets))
1556 {
1557 CHECK_CONS (offsets, 0);
1558 CHECK_NUMBER (XCONS (offsets)->car, 0);
1559 CHECK_NUMBER (XCONS (offsets)->cdr, 0);
1560 hscroll = XINT (XCONS (offsets)->car);
1561 tab_offset = XINT (XCONS (offsets)->cdr);
1562 }
1563 else
1564 hscroll = tab_offset = 0;
1565
1566 if (NILP (window))
1567 window = Fselected_window ();
1568 else
1569 CHECK_LIVE_WINDOW (window, 0);
1570
1571 pos = compute_motion (XINT (from), XINT (XCONS (frompos)->cdr),
1572 XINT (XCONS (frompos)->car), 0,
1573 XINT (to), XINT (XCONS (topos)->cdr),
1574 XINT (XCONS (topos)->car),
1575 XINT (width), hscroll, tab_offset,
1576 XWINDOW (window));
1577
1578 XSETFASTINT (bufpos, pos->bufpos);
1579 XSETINT (hpos, pos->hpos);
1580 XSETINT (vpos, pos->vpos);
1581 XSETINT (prevhpos, pos->prevhpos);
1582
1583 return Fcons (bufpos,
1584 Fcons (hpos,
1585 Fcons (vpos,
1586 Fcons (prevhpos,
1587 Fcons (pos->contin ? Qt : Qnil, Qnil)))));
1588
1589 }
1590 \f
1591 /* Fvertical_motion and vmotion */
1592 struct position val_vmotion;
1593
1594 struct position *
1595 vmotion (from, vtarget, w)
1596 register int from, vtarget;
1597 struct window *w;
1598 {
1599 int width = window_internal_width (w) - 1;
1600 int hscroll = XINT (w->hscroll);
1601 struct position pos;
1602 /* vpos is cumulative vertical position, changed as from is changed */
1603 register int vpos = 0;
1604 Lisp_Object prevline;
1605 register int first;
1606 int from_byte;
1607 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
1608 int selective
1609 = (INTEGERP (current_buffer->selective_display)
1610 ? XINT (current_buffer->selective_display)
1611 : !NILP (current_buffer->selective_display) ? -1 : 0);
1612 Lisp_Object window;
1613 int start_hpos = 0;
1614 int did_motion;
1615
1616 XSETWINDOW (window, w);
1617
1618 /* The omission of the clause
1619 && marker_position (w->start) == BEG
1620 here is deliberate; I think we want to measure from the prompt
1621 position even if the minibuffer window has scrolled. */
1622 if (EQ (window, minibuf_window))
1623 {
1624 if (minibuf_prompt_width == 0 && STRINGP (minibuf_prompt))
1625 minibuf_prompt_width
1626 = string_display_width (minibuf_prompt, Qnil, Qnil);
1627
1628 start_hpos = minibuf_prompt_width;
1629 }
1630
1631 if (vpos >= vtarget)
1632 {
1633 /* To move upward, go a line at a time until
1634 we have gone at least far enough. */
1635
1636 first = 1;
1637
1638 while ((vpos > vtarget || first) && from > BEGV)
1639 {
1640 Lisp_Object propval;
1641
1642 XSETFASTINT (prevline, find_next_newline_no_quit (from - 1, -1));
1643 while (XFASTINT (prevline) > BEGV
1644 && ((selective > 0
1645 && indented_beyond_p (XFASTINT (prevline),
1646 CHAR_TO_BYTE (XFASTINT (prevline)),
1647 selective))
1648 #ifdef USE_TEXT_PROPERTIES
1649 /* watch out for newlines with `invisible' property */
1650 || (propval = Fget_char_property (prevline,
1651 Qinvisible,
1652 window),
1653 TEXT_PROP_MEANS_INVISIBLE (propval))
1654 #endif
1655 ))
1656 XSETFASTINT (prevline,
1657 find_next_newline_no_quit (XFASTINT (prevline) - 1,
1658 -1));
1659 pos = *compute_motion (XFASTINT (prevline), 0,
1660 lmargin + (XFASTINT (prevline) == BEG
1661 ? start_hpos : 0),
1662 0,
1663 from,
1664 /* Don't care for VPOS... */
1665 1 << (BITS_PER_SHORT - 1),
1666 /* ... nor HPOS. */
1667 1 << (BITS_PER_SHORT - 1),
1668 width, hscroll,
1669 /* This compensates for start_hpos
1670 so that a tab as first character
1671 still occupies 8 columns. */
1672 (XFASTINT (prevline) == BEG
1673 ? -start_hpos : 0),
1674 w);
1675 vpos -= pos.vpos;
1676 first = 0;
1677 from = XFASTINT (prevline);
1678 }
1679
1680 /* If we made exactly the desired vertical distance,
1681 or if we hit beginning of buffer,
1682 return point found */
1683 if (vpos >= vtarget)
1684 {
1685 val_vmotion.bufpos = from;
1686 val_vmotion.bytepos = CHAR_TO_BYTE (from);
1687 val_vmotion.vpos = vpos;
1688 val_vmotion.hpos = lmargin;
1689 val_vmotion.contin = 0;
1690 val_vmotion.prevhpos = 0;
1691 val_vmotion.ovstring_chars_done = 0;
1692 val_vmotion.tab_offset = 0; /* For accumulating tab offset. */
1693 return &val_vmotion;
1694 }
1695
1696 /* Otherwise find the correct spot by moving down */
1697 }
1698 /* Moving downward is simple, but must calculate from beg of line
1699 to determine hpos of starting point */
1700 from_byte = CHAR_TO_BYTE (from);
1701 if (from > BEGV && FETCH_BYTE (from_byte - 1) != '\n')
1702 {
1703 Lisp_Object propval;
1704
1705 XSETFASTINT (prevline, find_next_newline_no_quit (from, -1));
1706 while (XFASTINT (prevline) > BEGV
1707 && ((selective > 0
1708 && indented_beyond_p (XFASTINT (prevline),
1709 CHAR_TO_BYTE (XFASTINT (prevline)),
1710 selective))
1711 #ifdef USE_TEXT_PROPERTIES
1712 /* watch out for newlines with `invisible' property */
1713 || (propval = Fget_char_property (prevline, Qinvisible,
1714 window),
1715 TEXT_PROP_MEANS_INVISIBLE (propval))
1716 #endif
1717 ))
1718 XSETFASTINT (prevline,
1719 find_next_newline_no_quit (XFASTINT (prevline) - 1,
1720 -1));
1721 pos = *compute_motion (XFASTINT (prevline), 0,
1722 lmargin + (XFASTINT (prevline) == BEG
1723 ? start_hpos : 0),
1724 0,
1725 from,
1726 /* Don't care for VPOS... */
1727 1 << (BITS_PER_SHORT - 1),
1728 /* ... nor HPOS. */
1729 1 << (BITS_PER_SHORT - 1),
1730 width, hscroll,
1731 (XFASTINT (prevline) == BEG ? -start_hpos : 0),
1732 w);
1733 did_motion = 1;
1734 }
1735 else
1736 {
1737 pos.hpos = lmargin + (from == BEG ? start_hpos : 0);
1738 pos.vpos = 0;
1739 pos.tab_offset = 0;
1740 did_motion = 0;
1741 }
1742 return compute_motion (from, vpos, pos.hpos, did_motion,
1743 ZV, vtarget, - (1 << (BITS_PER_SHORT - 1)),
1744 width, hscroll,
1745 pos.tab_offset - (from == BEG ? start_hpos : 0),
1746 w);
1747 }
1748
1749 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
1750 "Move point to start of the screen line LINES lines down.\n\
1751 If LINES is negative, this means moving up.\n\
1752 \n\
1753 This function is an ordinary cursor motion function\n\
1754 which calculates the new position based on how text would be displayed.\n\
1755 The new position may be the start of a line,\n\
1756 or just the start of a continuation line.\n\
1757 The function returns number of screen lines moved over;\n\
1758 that usually equals LINES, but may be closer to zero\n\
1759 if beginning or end of buffer was reached.\n\
1760 \n\
1761 The optional second argument WINDOW specifies the window to use for\n\
1762 parameters such as width, horizontal scrolling, and so on.\n\
1763 The default is to use the selected window's parameters.\n\
1764 \n\
1765 `vertical-motion' always uses the current buffer,\n\
1766 regardless of which buffer is displayed in WINDOW.\n\
1767 This is consistent with other cursor motion functions\n\
1768 and makes it possible to use `vertical-motion' in any buffer,\n\
1769 whether or not it is currently displayed in some window.")
1770 (lines, window)
1771 Lisp_Object lines, window;
1772 {
1773 struct position pos;
1774
1775 CHECK_NUMBER (lines, 0);
1776 if (! NILP (window))
1777 CHECK_WINDOW (window, 0);
1778 else
1779 window = selected_window;
1780
1781 pos = *vmotion (PT, (int) XINT (lines), XWINDOW (window));
1782
1783 SET_PT (pos.bufpos);
1784 return make_number (pos.vpos);
1785 }
1786 \f
1787 /* file's initialization. */
1788
1789 syms_of_indent ()
1790 {
1791 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
1792 "*Indentation can insert tabs if this is non-nil.\n\
1793 Setting this variable automatically makes it local to the current buffer.");
1794 indent_tabs_mode = 1;
1795
1796 defsubr (&Scurrent_indentation);
1797 defsubr (&Sindent_to);
1798 defsubr (&Scurrent_column);
1799 defsubr (&Smove_to_column);
1800 defsubr (&Svertical_motion);
1801 defsubr (&Scompute_motion);
1802 }