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