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