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