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