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