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