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