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