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