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