Mercurial > emacs
annotate src/indent.c @ 1688:c4da55e6cfd3
* make-dist: Don't bother creating an arch-lib directory; that's
only for installation now.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Sat, 12 Dec 1992 15:43:12 +0000 |
parents | fa662930e654 |
children | 4edfaa19c7a7 |
rev | line source |
---|---|
165 | 1 /* Indentation functions. |
573 | 2 Copyright (C) 1985, 1986, 1987, 1988, 1992 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 | |
8 the Free Software Foundation; either version 1, or (at your option) | |
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 | |
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | |
20 | |
21 #include "config.h" | |
22 #include "lisp.h" | |
23 #include "buffer.h" | |
24 #include "indent.h" | |
764 | 25 #include "frame.h" |
165 | 26 #include "window.h" |
27 #include "termchar.h" | |
28 #include "termopts.h" | |
29 #include "disptab.h" | |
30 | |
31 /* Indentation can insert tabs if this is non-zero; | |
32 otherwise always uses spaces */ | |
33 int indent_tabs_mode; | |
34 | |
35 #define min(a, b) ((a) < (b) ? (a) : (b)) | |
36 #define max(a, b) ((a) > (b) ? (a) : (b)) | |
37 | |
38 #define CR 015 | |
39 | |
40 /* These three values memoize the current column to avoid recalculation */ | |
41 /* Some things in set last_known_column_point to -1 | |
42 to mark the memoized value as invalid */ | |
43 /* Last value returned by current_column */ | |
44 int last_known_column; | |
45 /* Value of point when current_column was called */ | |
46 int last_known_column_point; | |
47 /* Value of MODIFF when current_column was called */ | |
48 int last_known_column_modified; | |
49 | |
50 /* Get the display table to use for the current buffer. */ | |
51 | |
52 struct Lisp_Vector * | |
53 buffer_display_table () | |
54 { | |
55 Lisp_Object thisbuf; | |
56 | |
57 thisbuf = current_buffer->display_table; | |
58 if (XTYPE (thisbuf) == Lisp_Vector | |
59 && XVECTOR (thisbuf)->size == DISP_TABLE_SIZE) | |
60 return XVECTOR (thisbuf); | |
61 if (XTYPE (Vstandard_display_table) == Lisp_Vector | |
62 && XVECTOR (Vstandard_display_table)->size == DISP_TABLE_SIZE) | |
63 return XVECTOR (Vstandard_display_table); | |
64 return 0; | |
65 } | |
66 | |
67 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0, | |
68 "Return the horizontal position of point. Beginning of line is column 0.\n\ | |
69 This is calculated by adding together the widths of all the displayed\n\ | |
70 representations of the character between the start of the previous line\n\ | |
71 and point. (eg control characters will have a width of 2 or 4, tabs\n\ | |
72 will have a variable width)\n\ | |
764 | 73 Ignores finite width of frame, which means that this function may return\n\ |
74 values greater than (frame-width).\n\ | |
165 | 75 Whether the line is visible (if `selective-display' is t) has no effect;\n\ |
76 however, ^M is treated as end of line when `selective-display' is t.") | |
77 () | |
78 { | |
79 Lisp_Object temp; | |
80 XFASTINT (temp) = current_column (); | |
81 return temp; | |
82 } | |
83 | |
327 | 84 /* Cancel any recorded value of the horizontal position. */ |
85 | |
86 invalidate_current_column () | |
87 { | |
88 last_known_column_point = 0; | |
89 } | |
90 | |
165 | 91 int |
92 current_column () | |
93 { | |
94 register int col; | |
95 register unsigned char *ptr, *stop; | |
96 register int tab_seen; | |
97 int post_tab; | |
98 register int c; | |
99 register int tab_width = XINT (current_buffer->tab_width); | |
488 | 100 int ctl_arrow = !NILP (current_buffer->ctl_arrow); |
165 | 101 register struct Lisp_Vector *dp = buffer_display_table (); |
102 int stopchar; | |
103 | |
104 if (point == last_known_column_point | |
105 && MODIFF == last_known_column_modified) | |
106 return last_known_column; | |
107 | |
108 /* Make a pointer for decrementing through the chars before point. */ | |
109 ptr = &FETCH_CHAR (point - 1) + 1; | |
110 /* Make a pointer to where consecutive chars leave off, | |
111 going backwards from point. */ | |
112 if (point == BEGV) | |
113 stop = ptr; | |
114 else if (point <= GPT || BEGV > GPT) | |
115 stop = BEGV_ADDR; | |
116 else | |
117 stop = GAP_END_ADDR; | |
118 | |
119 if (tab_width <= 0 || tab_width > 20) tab_width = 8; | |
120 | |
121 col = 0, tab_seen = 0, post_tab = 0; | |
122 | |
123 while (1) | |
124 { | |
125 if (ptr == stop) | |
126 { | |
127 /* We stopped either for the beginning of the buffer | |
128 or for the gap. */ | |
129 if (ptr == BEGV_ADDR) | |
130 break; | |
131 /* It was the gap. Jump back over it. */ | |
132 stop = BEGV_ADDR; | |
133 ptr = GPT_ADDR; | |
134 /* Check whether that brings us to beginning of buffer. */ | |
135 if (BEGV >= GPT) break; | |
136 } | |
137 | |
138 c = *--ptr; | |
139 if (c >= 040 && c < 0177 | |
140 && (dp == 0 || XTYPE (DISP_CHAR_ROPE (dp, c)) != Lisp_String)) | |
141 { | |
142 col++; | |
143 } | |
144 else if (c == '\n') | |
145 break; | |
146 else if (c == '\r' && EQ (current_buffer->selective_display, Qt)) | |
147 break; | |
148 else if (c == '\t') | |
149 { | |
150 if (tab_seen) | |
151 col = ((col + tab_width) / tab_width) * tab_width; | |
152 | |
153 post_tab += col; | |
154 col = 0; | |
155 tab_seen = 1; | |
156 } | |
157 else if (dp != 0 && XTYPE (DISP_CHAR_ROPE (dp, c)) == Lisp_String) | |
158 col += XSTRING (DISP_CHAR_ROPE (dp, c))->size / sizeof (GLYPH); | |
159 else | |
160 col += (ctl_arrow && c < 0200) ? 2 : 4; | |
161 } | |
162 | |
163 if (tab_seen) | |
164 { | |
165 col = ((col + tab_width) / tab_width) * tab_width; | |
166 col += post_tab; | |
167 } | |
168 | |
169 last_known_column = col; | |
170 last_known_column_point = point; | |
171 last_known_column_modified = MODIFF; | |
172 | |
173 return col; | |
174 } | |
175 | |
176 | |
177 DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ", | |
178 "Indent from point with tabs and spaces until COLUMN is reached.\n\ | |
179 Optional second argument MIN says always do at least MIN spaces\n\ | |
180 even if that goes past COLUMN; by default, MIN is zero.") | |
181 (col, minimum) | |
182 Lisp_Object col, minimum; | |
183 { | |
184 int mincol; | |
185 register int fromcol; | |
186 register int tab_width = XINT (current_buffer->tab_width); | |
187 | |
188 CHECK_NUMBER (col, 0); | |
488 | 189 if (NILP (minimum)) |
165 | 190 XFASTINT (minimum) = 0; |
191 CHECK_NUMBER (minimum, 1); | |
192 | |
193 fromcol = current_column (); | |
194 mincol = fromcol + XINT (minimum); | |
195 if (mincol < XINT (col)) mincol = XINT (col); | |
196 | |
197 if (fromcol == mincol) | |
198 return make_number (mincol); | |
199 | |
200 if (tab_width <= 0 || tab_width > 20) tab_width = 8; | |
201 | |
202 if (indent_tabs_mode) | |
203 { | |
204 Lisp_Object n; | |
205 XFASTINT (n) = mincol / tab_width - fromcol / tab_width; | |
206 if (XFASTINT (n) != 0) | |
207 { | |
208 Finsert_char (make_number ('\t'), n); | |
209 | |
210 fromcol = (mincol / tab_width) * tab_width; | |
211 } | |
212 } | |
213 | |
214 XFASTINT (col) = mincol - fromcol; | |
215 Finsert_char (make_number (' '), col); | |
216 | |
217 last_known_column = mincol; | |
218 last_known_column_point = point; | |
219 last_known_column_modified = MODIFF; | |
220 | |
221 XSETINT (col, mincol); | |
222 return col; | |
223 } | |
224 | |
225 DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation, | |
226 0, 0, 0, | |
227 "Return the indentation of the current line.\n\ | |
228 This is the horizontal position of the character\n\ | |
229 following any initial whitespace.") | |
230 () | |
231 { | |
232 Lisp_Object val; | |
233 | |
234 XFASTINT (val) = position_indentation (find_next_newline (point, -1)); | |
235 return val; | |
236 } | |
237 | |
238 position_indentation (pos) | |
239 register int pos; | |
240 { | |
241 register int column = 0; | |
242 register int tab_width = XINT (current_buffer->tab_width); | |
243 register unsigned char *p; | |
244 register unsigned char *stop; | |
245 | |
246 if (tab_width <= 0 || tab_width > 20) tab_width = 8; | |
247 | |
248 stop = &FETCH_CHAR (BUFFER_CEILING_OF (pos)) + 1; | |
249 p = &FETCH_CHAR (pos); | |
250 while (1) | |
251 { | |
252 while (p == stop) | |
253 { | |
254 if (pos == ZV) | |
255 return column; | |
256 pos += p - &FETCH_CHAR (pos); | |
257 p = &FETCH_CHAR (pos); | |
258 stop = &FETCH_CHAR (BUFFER_CEILING_OF (pos)) + 1; | |
259 } | |
260 switch (*p++) | |
261 { | |
262 case ' ': | |
263 column++; | |
264 break; | |
265 case '\t': | |
266 column += tab_width - column % tab_width; | |
267 break; | |
268 default: | |
269 return column; | |
270 } | |
271 } | |
272 } | |
273 | |
274 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, 0, | |
275 "Move point to column COLUMN in the current line.\n\ | |
276 The column of a character is calculated by adding together the widths\n\ | |
277 as displayed of the previous characters in the line.\n\ | |
278 This function ignores line-continuation;\n\ | |
279 there is no upper limit on the column number a character can have\n\ | |
1208
fa662930e654
* indent.c (Fmove_to_column): Pass the right number of arguments
Jim Blandy <jimb@redhat.com>
parents:
764
diff
changeset
|
280 and horizontal scrolling has no effect.\n\ |
fa662930e654
* indent.c (Fmove_to_column): Pass the right number of arguments
Jim Blandy <jimb@redhat.com>
parents:
764
diff
changeset
|
281 \n\ |
165 | 282 If specified column is within a character, point goes after that character.\n\ |
283 If it's past end of line, point goes to end of line.\n\n\ | |
284 A non-nil second (optional) argument FORCE means, if the line\n\ | |
285 is too short to reach column COLUMN then add spaces/tabs to get there,\n\ | |
286 and if COLUMN is in the middle of a tab character, change it to spaces.") | |
287 (column, force) | |
288 Lisp_Object column, force; | |
289 { | |
290 register int pos; | |
291 register int col = current_column (); | |
292 register int goal; | |
293 register int end; | |
294 register int tab_width = XINT (current_buffer->tab_width); | |
488 | 295 register int ctl_arrow = !NILP (current_buffer->ctl_arrow); |
165 | 296 register struct Lisp_Vector *dp = buffer_display_table (); |
297 | |
298 Lisp_Object val; | |
299 int prev_col; | |
300 int c; | |
301 | |
302 if (tab_width <= 0 || tab_width > 20) tab_width = 8; | |
303 CHECK_NATNUM (column, 0); | |
304 goal = XINT (column); | |
305 | |
306 retry: | |
307 pos = point; | |
308 end = ZV; | |
309 | |
310 /* If we're starting past the desired column, | |
311 back up to beginning of line and scan from there. */ | |
312 if (col > goal) | |
313 { | |
314 pos = find_next_newline (pos, -1); | |
315 col = 0; | |
316 } | |
317 | |
318 while (col < goal && pos < end) | |
319 { | |
320 c = FETCH_CHAR (pos); | |
321 if (c == '\n') | |
322 break; | |
323 if (c == '\r' && EQ (current_buffer->selective_display, Qt)) | |
324 break; | |
325 pos++; | |
326 if (c == '\t') | |
327 { | |
328 prev_col = col; | |
329 col += tab_width; | |
330 col = col / tab_width * tab_width; | |
331 } | |
332 else if (dp != 0 && XTYPE (DISP_CHAR_ROPE (dp, c)) == Lisp_String) | |
333 col += XSTRING (DISP_CHAR_ROPE (dp, c))->size / sizeof (GLYPH); | |
334 else if (ctl_arrow && (c < 040 || c == 0177)) | |
335 col++; | |
336 else if (c < 040 || c >= 0177) | |
337 col += 3; | |
338 else | |
339 col++; | |
340 } | |
341 | |
342 SET_PT (pos); | |
343 | |
344 /* If a tab char made us overshoot, change it to spaces | |
345 and scan through it again. */ | |
488 | 346 if (!NILP (force) && col > goal && c == '\t' && prev_col < goal) |
165 | 347 { |
573 | 348 int old_point; |
349 | |
165 | 350 del_range (point - 1, point); |
573 | 351 Findent_to (make_number (goal), Qnil); |
352 old_point = point; | |
353 Findent_to (make_number (col), Qnil); | |
354 SET_PT (old_point); | |
165 | 355 } |
356 | |
357 /* If line ends prematurely, add space to the end. */ | |
488 | 358 if (col < goal && !NILP (force)) |
1208
fa662930e654
* indent.c (Fmove_to_column): Pass the right number of arguments
Jim Blandy <jimb@redhat.com>
parents:
764
diff
changeset
|
359 Findent_to (make_number (col = goal), Qnil); |
165 | 360 |
361 last_known_column = col; | |
362 last_known_column_point = point; | |
363 last_known_column_modified = MODIFF; | |
364 | |
365 XFASTINT (val) = col; | |
366 return val; | |
367 } | |
368 | |
369 struct position val_compute_motion; | |
370 | |
371 /* Scan the current buffer forward from offset FROM, pretending that | |
372 this is at line FROMVPOS, column FROMHPOS, until reaching buffer | |
373 offset TO or line TOVPOS, column TOHPOS (whichever comes first), | |
374 and return the ending buffer position and screen location. | |
375 | |
376 WIDTH is the number of columns available to display text; | |
377 compute_motion uses this to handle continuation lines and such. | |
378 HSCROLL is the number of columns not being displayed at the left | |
379 margin; this is usually taken from a window's hscroll member. | |
543 | 380 TAB_OFFSET is the number of columns of the first tab that aren't |
381 being displayed, perhaps because of a continuation line or | |
382 something. | |
165 | 383 |
384 compute_motion returns a pointer to a struct position. The bufpos | |
385 member gives the buffer position at the end of the scan, and hpos | |
386 and vpos give its cartesian location. I'm not clear on what the | |
387 other members are. | |
388 | |
389 For example, to find the buffer position of column COL of line LINE | |
390 of a certain window, pass the window's starting location as FROM | |
391 and the window's upper-left coordinates as FROMVPOS and FROMHPOS. | |
392 Pass the buffer's ZV as TO, to limit the scan to the end of the | |
393 visible section of the buffer, and pass LINE and COL as TOVPOS and | |
394 TOHPOS. | |
395 | |
396 When displaying in window w, a typical formula for WIDTH is: | |
397 | |
398 window_width - 1 | |
764 | 399 - (window_width + window_left != frame_width) |
165 | 400 |
401 where | |
402 window_width is XFASTINT (w->width), | |
403 window_left is XFASTINT (w->left), | |
764 | 404 and frame_width = FRAME_WIDTH (XFRAME (window->frame)) |
165 | 405 |
406 This accounts for the continuation-line backslashes, and the window | |
407 borders if the window is split vertically. */ | |
408 | |
409 struct position * | |
410 compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, tab_offset) | |
411 int from, fromvpos, fromhpos, to, tovpos, tohpos; | |
412 register int width; | |
413 int hscroll, tab_offset; | |
414 { | |
526 | 415 register int hpos = fromhpos; |
416 register int vpos = fromvpos; | |
165 | 417 |
418 register int pos; | |
419 register int c; | |
420 register int tab_width = XFASTINT (current_buffer->tab_width); | |
488 | 421 register int ctl_arrow = !NILP (current_buffer->ctl_arrow); |
165 | 422 register struct Lisp_Vector *dp = buffer_display_table (); |
423 int selective | |
424 = XTYPE (current_buffer->selective_display) == Lisp_Int | |
425 ? XINT (current_buffer->selective_display) | |
488 | 426 : !NILP (current_buffer->selective_display) ? -1 : 0; |
526 | 427 int prev_vpos, prev_hpos; |
165 | 428 int selective_rlen |
429 = (selective && dp && XTYPE (DISP_INVIS_ROPE (dp)) == Lisp_String | |
430 ? XSTRING (DISP_INVIS_ROPE (dp))->size / sizeof (GLYPH) : 0); | |
431 | |
432 if (tab_width <= 0 || tab_width > 20) tab_width = 8; | |
526 | 433 for (pos = from; pos < to; pos++) |
165 | 434 { |
526 | 435 /* Stop if past the target screen position. */ |
436 if (vpos > tovpos | |
437 || (vpos == tovpos && hpos >= tohpos)) | |
438 break; | |
439 | |
440 prev_vpos = vpos; | |
441 prev_hpos = hpos; | |
442 | |
165 | 443 c = FETCH_CHAR (pos); |
444 if (c >= 040 && c < 0177 | |
445 && (dp == 0 || XTYPE (DISP_CHAR_ROPE (dp, c)) != Lisp_String)) | |
526 | 446 hpos++; |
165 | 447 else if (c == '\t') |
448 { | |
526 | 449 hpos += tab_width - ((hpos + tab_offset + hscroll - (hscroll > 0) |
450 /* Add tab_width here to make sure positive. | |
451 hpos can be negative after continuation | |
452 but can't be less than -tab_width. */ | |
453 + tab_width) | |
454 % tab_width); | |
165 | 455 } |
456 else if (c == '\n') | |
457 { | |
458 if (selective > 0 && position_indentation (pos + 1) >= selective) | |
459 { | |
460 /* Skip any number of invisible lines all at once */ | |
461 do | |
462 { | |
526 | 463 while (++pos < to && FETCH_CHAR (pos) != '\n'); |
165 | 464 } |
614
0085dcbad4bd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
573
diff
changeset
|
465 while (pos < to && position_indentation (pos + 1) >= selective); |
165 | 466 pos--; |
467 /* Allow for the " ..." that is displayed for them. */ | |
468 if (selective_rlen) | |
469 { | |
526 | 470 hpos += selective_rlen; |
471 if (hpos >= width) | |
472 hpos = width; | |
165 | 473 } |
614
0085dcbad4bd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
573
diff
changeset
|
474 /* We have skipped the invis text, but not the newline after. */ |
165 | 475 } |
476 else | |
526 | 477 { |
478 /* A visible line. */ | |
479 vpos++; | |
480 hpos = 0; | |
617 | 481 hpos -= hscroll; |
482 if (hscroll > 0) hpos++; /* Count the ! on column 0 */ | |
483 tab_offset = 0; | |
484 } | |
165 | 485 } |
486 else if (c == CR && selective < 0) | |
487 { | |
488 /* In selective display mode, | |
489 everything from a ^M to the end of the line is invisible */ | |
526 | 490 while (pos < to && FETCH_CHAR (pos) != '\n') pos++; |
491 /* Stop *before* the real newline. */ | |
165 | 492 pos--; |
493 /* Allow for the " ..." that is displayed for them. */ | |
494 if (selective_rlen) | |
495 { | |
526 | 496 hpos += selective_rlen; |
497 if (hpos >= width) | |
498 hpos = width; | |
165 | 499 } |
500 } | |
501 else if (dp != 0 && XTYPE (DISP_CHAR_ROPE (dp, c)) == Lisp_String) | |
526 | 502 hpos += XSTRING (DISP_CHAR_ROPE (dp, c))->size / sizeof (GLYPH); |
165 | 503 else |
526 | 504 hpos += (ctl_arrow && c < 0200) ? 2 : 4; |
165 | 505 |
526 | 506 /* Handle right margin. */ |
507 if (hpos >= width | |
508 && (hpos > width | |
509 || (pos < ZV - 1 | |
165 | 510 && FETCH_CHAR (pos + 1) != '\n'))) |
511 { | |
526 | 512 if (vpos > tovpos |
513 || (vpos == tovpos && hpos >= tohpos)) | |
165 | 514 break; |
515 if (hscroll | |
516 || (truncate_partial_width_windows | |
764 | 517 && width + 1 < FRAME_WIDTH (selected_frame)) |
488 | 518 || !NILP (current_buffer->truncate_lines)) |
165 | 519 { |
526 | 520 /* Truncating: skip to newline. */ |
521 while (pos < to && FETCH_CHAR (pos) != '\n') pos++; | |
165 | 522 pos--; |
614
0085dcbad4bd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
573
diff
changeset
|
523 hpos = width; |
165 | 524 } |
525 else | |
526 { | |
526 | 527 /* Continuing. */ |
528 vpos++; | |
529 hpos -= width; | |
165 | 530 tab_offset += width; |
531 } | |
532 | |
533 } | |
534 } | |
535 | |
536 val_compute_motion.bufpos = pos; | |
526 | 537 val_compute_motion.hpos = hpos; |
538 val_compute_motion.vpos = vpos; | |
539 val_compute_motion.prevhpos = prev_hpos; | |
165 | 540 |
541 /* Nonzero if have just continued a line */ | |
542 val_compute_motion.contin | |
526 | 543 = (pos != from |
544 && (val_compute_motion.vpos != prev_vpos) | |
545 && c != '\n'); | |
165 | 546 |
547 return &val_compute_motion; | |
548 } | |
549 | |
550 | |
551 /* Return the column of position POS in window W's buffer, | |
552 rounded down to a multiple of the internal width of W. | |
553 This is the amount of indentation of position POS | |
554 that is not visible in its horizontal position in the window. */ | |
555 | |
556 int | |
557 pos_tab_offset (w, pos) | |
558 struct window *w; | |
559 register int pos; | |
560 { | |
561 int opoint = point; | |
562 int col; | |
563 int width = XFASTINT (w->width) - 1 | |
564 - (XFASTINT (w->width) + XFASTINT (w->left) | |
764 | 565 != FRAME_WIDTH (XFRAME (w->frame))); |
165 | 566 |
567 if (pos == BEGV || FETCH_CHAR (pos - 1) == '\n') | |
568 return 0; | |
569 SET_PT (pos); | |
570 col = current_column (); | |
571 SET_PT (opoint); | |
572 return col - (col % width); | |
573 } | |
574 | |
575 /* start_hpos is the hpos of the first character of the buffer: | |
576 zero except for the minibuffer window, | |
577 where it is the width of the prompt. */ | |
578 | |
579 struct position val_vmotion; | |
580 | |
581 struct position * | |
582 vmotion (from, vtarget, width, hscroll, window) | |
583 register int from, vtarget, width; | |
584 int hscroll; | |
585 Lisp_Object window; | |
586 { | |
587 struct position pos; | |
588 /* vpos is cumulative vertical position, changed as from is changed */ | |
589 register int vpos = 0; | |
590 register int prevline; | |
591 register int first; | |
592 int lmargin = hscroll > 0 ? 1 - hscroll : 0; | |
593 int selective | |
594 = XTYPE (current_buffer->selective_display) == Lisp_Int | |
595 ? XINT (current_buffer->selective_display) | |
488 | 596 : !NILP (current_buffer->selective_display) ? -1 : 0; |
165 | 597 int start_hpos = (EQ (window, minibuf_window) ? minibuf_prompt_width : 0); |
598 | |
599 retry: | |
600 if (vtarget > vpos) | |
601 { | |
602 /* Moving downward is simple, but must calculate from beg of line | |
603 to determine hpos of starting point */ | |
604 if (from > BEGV && FETCH_CHAR (from - 1) != '\n') | |
605 { | |
606 prevline = find_next_newline (from, -1); | |
607 while (selective > 0 | |
608 && prevline > BEGV | |
609 && position_indentation (prevline) >= selective) | |
610 prevline = find_next_newline (prevline - 1, -1); | |
611 pos = *compute_motion (prevline, 0, | |
612 lmargin + (prevline == 1 ? start_hpos : 0), | |
526 | 613 from, 1 << (INTBITS - 2), 0, |
165 | 614 width, hscroll, 0); |
615 } | |
616 else | |
617 { | |
618 pos.hpos = lmargin + (from == 1 ? start_hpos : 0); | |
619 pos.vpos = 0; | |
620 } | |
621 return compute_motion (from, vpos, pos.hpos, | |
526 | 622 ZV, vtarget, - (1 << (INTBITS - 2)), |
165 | 623 width, hscroll, pos.vpos * width); |
624 } | |
625 | |
626 /* To move upward, go a line at a time until | |
627 we have gone at least far enough */ | |
628 | |
629 first = 1; | |
630 | |
631 while ((vpos > vtarget || first) && from > BEGV) | |
632 { | |
633 prevline = from; | |
634 while (1) | |
635 { | |
636 prevline = find_next_newline (prevline - 1, -1); | |
637 if (prevline == BEGV | |
638 || selective <= 0 | |
639 || position_indentation (prevline) < selective) | |
640 break; | |
641 } | |
642 pos = *compute_motion (prevline, 0, | |
643 lmargin + (prevline == 1 ? start_hpos : 0), | |
526 | 644 from, 1 << (INTBITS - 2), 0, |
165 | 645 width, hscroll, 0); |
646 vpos -= pos.vpos; | |
647 first = 0; | |
648 from = prevline; | |
649 } | |
650 | |
651 /* If we made exactly the desired vertical distance, | |
652 or if we hit beginning of buffer, | |
653 return point found */ | |
654 if (vpos >= vtarget) | |
655 { | |
656 val_vmotion.bufpos = from; | |
657 val_vmotion.vpos = vpos; | |
658 val_vmotion.hpos = lmargin; | |
659 val_vmotion.contin = 0; | |
660 val_vmotion.prevhpos = 0; | |
661 return &val_vmotion; | |
662 } | |
663 | |
664 /* Otherwise find the correct spot by moving down */ | |
665 goto retry; | |
666 } | |
667 | |
668 DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 1, 0, | |
669 "Move to start of screen line LINES lines down.\n\ | |
670 If LINES is negative, this is moving up.\n\ | |
671 Sets point to position found; this may be start of line\n\ | |
672 or just the start of a continuation line.\n\ | |
673 Returns number of lines moved; may be closer to zero than LINES\n\ | |
674 if beginning or end of buffer was reached.") | |
675 (lines) | |
676 Lisp_Object lines; | |
677 { | |
678 struct position pos; | |
679 register struct window *w = XWINDOW (selected_window); | |
680 int width = XFASTINT (w->width) - 1 | |
681 - (XFASTINT (w->width) + XFASTINT (w->left) | |
764 | 682 != FRAME_WIDTH (XFRAME (w->frame))); |
165 | 683 |
684 CHECK_NUMBER (lines, 0); | |
685 | |
686 pos = *vmotion (point, XINT (lines), width, | |
687 /* Not XFASTINT since perhaps could be negative */ | |
688 XINT (w->hscroll), selected_window); | |
689 | |
690 SET_PT (pos.bufpos); | |
691 return make_number (pos.vpos); | |
692 } | |
693 | |
694 syms_of_indent () | |
695 { | |
696 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode, | |
697 "*Indentation can insert tabs if this is non-nil.\n\ | |
698 Setting this variable automatically makes it local to the current buffer."); | |
699 indent_tabs_mode = 1; | |
700 | |
701 defsubr (&Scurrent_indentation); | |
702 defsubr (&Sindent_to); | |
703 defsubr (&Scurrent_column); | |
704 defsubr (&Smove_to_column); | |
705 defsubr (&Svertical_motion); | |
706 } |