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