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