Mercurial > hg > xemacs-beta
annotate src/indent.c @ 5435:aa729daae5e2
Added GPLv3 or later license and copyright notice to GTK ChangeLogs.
author | Mats Lidell <matsl@xemacs.org> |
---|---|
date | Sat, 13 Nov 2010 00:15:17 +0100 |
parents | 308d34e9f07d |
children | 8d29f1c4bb98 |
rev | line source |
---|---|
428 | 1 /* Indentation functions. |
2 Copyright (C) 1995 Board of Trustees, University of Illinois. | |
3 Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995 | |
826 | 4 Free Software Foundation, Inc. |
3025 | 5 Copyright (C) 2002, 2005 Ben Wing. |
428 | 6 |
7 This file is part of XEmacs. | |
8 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5047
diff
changeset
|
9 XEmacs is free software: you can redistribute it and/or modify it |
428 | 10 under the terms of the GNU General Public License as published by the |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5047
diff
changeset
|
11 Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5047
diff
changeset
|
12 option) any later version. |
428 | 13 |
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5047
diff
changeset
|
20 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
428 | 21 |
22 /* This file has been Mule-ized. */ | |
23 | |
24 /* Synched up with: 19.30. Diverges significantly from FSF. */ | |
25 | |
26 | |
27 #include <config.h> | |
28 #include "lisp.h" | |
29 | |
30 #include "buffer.h" | |
31 #include "device.h" | |
32 #include "extents.h" | |
33 #include "faces.h" | |
34 #include "frame.h" | |
35 #include "glyphs.h" | |
36 #include "insdel.h" | |
37 #ifdef REGION_CACHE_NEEDS_WORK | |
38 #include "region-cache.h" | |
39 #endif | |
40 #include "window.h" | |
41 | |
42 /* Indentation can insert tabs if this is non-zero; | |
43 otherwise always uses spaces */ | |
44 int indent_tabs_mode; | |
45 | |
46 /* Avoid recalculation by remembering things in these variables. */ | |
47 | |
48 /* Last value returned by current_column. | |
49 | |
50 Some things set last_known_column_point to -1 | |
51 to mark the memoized value as invalid */ | |
52 static int last_known_column; | |
53 | |
54 /* Last buffer searched by current_column */ | |
55 static struct buffer *last_known_column_buffer; | |
56 | |
57 /* Value of point when current_column was called */ | |
665 | 58 static Charbpos last_known_column_point; |
428 | 59 |
60 /* Value of MODIFF when current_column was called */ | |
61 static int last_known_column_modified; | |
62 | |
665 | 63 static Charbpos |
64 last_visible_position (Charbpos pos, struct buffer *buf) | |
428 | 65 { |
66 Lisp_Object buffer; | |
67 Lisp_Object value; | |
68 | |
793 | 69 buffer = wrap_buffer (buf); |
2506 | 70 value = Fprevious_single_char_property_change (make_int (pos), Qinvisible, |
71 buffer, Qnil); | |
428 | 72 if (NILP (value)) |
73 return 0; /* no visible position found */ | |
74 else | |
75 /* #### bug bug bug!!! This will return the position of the beginning | |
76 of an invisible extent; this extent is very likely to be start-closed, | |
77 and thus the spaces inserted in `indent-to' will go inside the | |
78 invisible extent. | |
79 | |
80 Not sure what the correct solution is here. Rethink indent-to? */ | |
81 return XINT (value); | |
82 } | |
83 | |
84 #ifdef REGION_CACHE_NEEDS_WORK | |
85 | |
86 /* Allocate or free the width run cache, as requested by the current | |
87 state of current_buffer's cache_long_line_scans variable. */ | |
88 static void | |
89 width_run_cache_on_off (struct buffer *buf) | |
90 { | |
91 if (NILP (buf->cache_long_line_scans)) | |
92 { | |
93 /* It should be off. */ | |
94 if (buf->width_run_cache) | |
95 { | |
96 free_region_cache (buf->width_run_cache); | |
97 buf->width_run_cache = 0; | |
98 buf->width_table = Qnil; | |
99 } | |
100 } | |
101 else | |
102 { | |
103 /* It should be on. */ | |
104 if (buf->width_run_cache == 0) | |
105 { | |
106 buf->width_run_cache = new_region_cache (); | |
107 recompute_width_table (buf, buffer_display_table ()); | |
108 } | |
109 } | |
110 } | |
111 | |
112 #endif /* REGION_CACHE_NEEDS_WORK */ | |
113 | |
114 | |
115 /* Cancel any recorded value of the horizontal position. */ | |
116 | |
117 void | |
118 invalidate_current_column (void) | |
119 { | |
120 last_known_column_point = -1; | |
121 } | |
122 | |
123 int | |
665 | 124 column_at_point (struct buffer *buf, Charbpos init_pos, int cur_col) |
428 | 125 { |
126 int col; | |
127 int tab_seen; | |
128 int tab_width = XINT (buf->tab_width); | |
129 int post_tab; | |
665 | 130 Charbpos pos = init_pos; |
867 | 131 Ichar c; |
428 | 132 |
133 if (tab_width <= 0 || tab_width > 1000) tab_width = 8; | |
134 col = tab_seen = post_tab = 0; | |
135 | |
136 while (1) | |
137 { | |
138 if (pos <= BUF_BEGV (buf)) | |
139 break; | |
140 | |
141 pos--; | |
142 c = BUF_FETCH_CHAR (buf, pos); | |
143 if (c == '\t') | |
144 { | |
145 if (tab_seen) | |
146 col = ((col + tab_width) / tab_width) * tab_width; | |
147 | |
148 post_tab += col; | |
149 col = 0; | |
150 tab_seen = 1; | |
151 } | |
152 else if (c == '\n' || | |
153 (EQ (buf->selective_display, Qt) && c == '\r')) | |
154 break; | |
155 else | |
156 { | |
157 /* #### This needs updating to handle the new redisplay. */ | |
158 /* #### FSFmacs looks at ctl_arrow, display tables. | |
159 We need to do similar. */ | |
160 #if 0 | |
665 | 161 displayed_glyphs = glyphs_from_charbpos (sel_frame, buf, |
428 | 162 XWINDOW (selected_window), |
163 pos, dp, 0, col, 0, 0, 0); | |
164 col += (displayed_glyphs->columns | |
165 - (displayed_glyphs->begin_columns | |
166 + displayed_glyphs->end_columns)); | |
167 #else /* XEmacs */ | |
168 #ifdef MULE | |
867 | 169 col += XCHARSET_COLUMNS (ichar_charset (c)); |
428 | 170 #else |
171 col ++; | |
172 #endif /* MULE */ | |
173 #endif /* XEmacs */ | |
174 } | |
175 } | |
176 | |
177 if (tab_seen) | |
178 { | |
179 col = ((col + tab_width) / tab_width) * tab_width; | |
180 col += post_tab; | |
181 } | |
182 | |
183 if (cur_col) | |
184 { | |
185 last_known_column_buffer = buf; | |
186 last_known_column = col; | |
187 last_known_column_point = init_pos; | |
188 last_known_column_modified = BUF_MODIFF (buf); | |
189 } | |
190 | |
191 return col; | |
192 } | |
193 | |
194 int | |
793 | 195 string_column_at_point (Lisp_Object s, Charbpos init_pos, int tab_width) |
428 | 196 { |
197 int col; | |
198 int tab_seen; | |
199 int post_tab; | |
665 | 200 Charbpos pos = init_pos; |
867 | 201 Ichar c; |
428 | 202 |
203 if (tab_width <= 0 || tab_width > 1000) tab_width = 8; | |
204 col = tab_seen = post_tab = 0; | |
205 | |
206 while (1) | |
207 { | |
208 if (pos <= 0) | |
209 break; | |
210 | |
211 pos--; | |
867 | 212 c = string_ichar (s, pos); |
428 | 213 if (c == '\t') |
214 { | |
215 if (tab_seen) | |
216 col = ((col + tab_width) / tab_width) * tab_width; | |
217 | |
218 post_tab += col; | |
219 col = 0; | |
220 tab_seen = 1; | |
221 } | |
222 else if (c == '\n') | |
223 break; | |
224 else | |
225 #ifdef MULE | |
867 | 226 col += XCHARSET_COLUMNS (ichar_charset (c)); |
428 | 227 #else |
228 col ++; | |
229 #endif /* MULE */ | |
230 } | |
231 | |
232 if (tab_seen) | |
233 { | |
234 col = ((col + tab_width) / tab_width) * tab_width; | |
235 col += post_tab; | |
236 } | |
237 | |
238 return col; | |
239 } | |
240 | |
241 int | |
242 current_column (struct buffer *buf) | |
243 { | |
244 if (buf == last_known_column_buffer | |
245 && BUF_PT (buf) == last_known_column_point | |
246 && BUF_MODIFF (buf) == last_known_column_modified) | |
247 return last_known_column; | |
248 | |
249 return column_at_point (buf, BUF_PT (buf), 1); | |
250 } | |
251 | |
252 DEFUN ("current-column", Fcurrent_column, 0, 1, 0, /* | |
253 Return the horizontal position of point. Beginning of line is column 0. | |
254 This is calculated by adding together the widths of all the displayed | |
255 representations of the character between the start of the previous line | |
256 and point. (e.g. control characters will have a width of 2 or 4, tabs | |
257 will have a variable width.) | |
258 Ignores finite width of frame, which means that this function may return | |
259 values greater than (frame-width). | |
260 Whether the line is visible (if `selective-display' is t) has no effect; | |
261 however, ^M is treated as end of line when `selective-display' is t. | |
262 If BUFFER is nil, the current buffer is assumed. | |
263 */ | |
264 (buffer)) | |
265 { | |
266 return make_int (current_column (decode_buffer (buffer, 0))); | |
267 } | |
268 | |
269 | |
270 DEFUN ("indent-to", Findent_to, 1, 3, "NIndent to column: ", /* | |
271 Indent from point with tabs and spaces until COLUMN is reached. | |
444 | 272 Optional second argument MINIMUM says always do at least MINIMUM spaces |
273 even if that goes past COLUMN; by default, MINIMUM is zero. | |
428 | 274 If BUFFER is nil, the current buffer is assumed. |
275 */ | |
444 | 276 (column, minimum, buffer)) |
428 | 277 { |
278 /* This function can GC */ | |
279 int mincol; | |
280 int fromcol; | |
281 struct buffer *buf = decode_buffer (buffer, 0); | |
282 int tab_width = XINT (buf->tab_width); | |
665 | 283 Charbpos opoint = 0; |
428 | 284 |
444 | 285 CHECK_INT (column); |
428 | 286 if (NILP (minimum)) |
287 minimum = Qzero; | |
288 else | |
289 CHECK_INT (minimum); | |
290 | |
793 | 291 buffer = wrap_buffer (buf); |
428 | 292 |
293 fromcol = current_column (buf); | |
294 mincol = fromcol + XINT (minimum); | |
444 | 295 if (mincol < XINT (column)) mincol = XINT (column); |
428 | 296 |
297 if (fromcol == mincol) | |
298 return make_int (mincol); | |
299 | |
300 if (tab_width <= 0 || tab_width > 1000) tab_width = 8; | |
301 | |
302 if (!NILP (Fextent_at (make_int (BUF_PT (buf)), buffer, Qinvisible, | |
303 Qnil, Qnil))) | |
304 { | |
665 | 305 Charbpos last_visible = last_visible_position (BUF_PT (buf), buf); |
428 | 306 |
307 opoint = BUF_PT (buf); | |
308 if (last_visible >= BUF_BEGV (buf)) | |
309 BUF_SET_PT (buf, last_visible); | |
310 else | |
563 | 311 invalid_operation ("Visible portion of buffer not modifiable", Qunbound); |
428 | 312 } |
313 | |
314 if (indent_tabs_mode) | |
315 { | |
316 int n = mincol / tab_width - fromcol / tab_width; | |
317 if (n != 0) | |
318 { | |
319 Finsert_char (make_char ('\t'), make_int (n), Qnil, buffer); | |
320 | |
321 fromcol = (mincol / tab_width) * tab_width; | |
322 } | |
323 } | |
324 | |
325 Finsert_char (make_char (' '), make_int (mincol - fromcol), Qnil, buffer); | |
326 | |
327 last_known_column_buffer = buf; | |
328 last_known_column = mincol; | |
329 last_known_column_point = BUF_PT (buf); | |
330 last_known_column_modified = BUF_MODIFF (buf); | |
331 | |
332 /* Not in FSF: */ | |
333 if (opoint > 0) | |
334 BUF_SET_PT (buf, opoint); | |
335 | |
336 return make_int (mincol); | |
337 } | |
338 | |
339 int | |
826 | 340 byte_spaces_at_point (struct buffer *b, Bytebpos byte_pos) |
428 | 341 { |
826 | 342 Bytebpos byte_end = BYTE_BUF_ZV (b); |
428 | 343 int col = 0; |
867 | 344 Ichar c; |
428 | 345 int tab_width = XINT (b->tab_width); |
346 | |
347 if (tab_width <= 0 || tab_width > 1000) | |
348 tab_width = 8; | |
349 | |
826 | 350 while (byte_pos < byte_end && |
351 (c = BYTE_BUF_FETCH_CHAR (b, byte_pos), | |
428 | 352 (c == '\t' |
353 ? (col += tab_width - col % tab_width) | |
354 : (c == ' ' ? ++col : 0)))) | |
826 | 355 INC_BYTEBPOS (b, byte_pos); |
428 | 356 |
357 return col; | |
358 } | |
359 | |
360 | |
361 DEFUN ("current-indentation", Fcurrent_indentation, 0, 1, 0, /* | |
362 Return the indentation of the current line. | |
363 This is the horizontal position of the character | |
364 following any initial whitespace. | |
365 */ | |
366 (buffer)) | |
367 { | |
368 struct buffer *buf = decode_buffer (buffer, 0); | |
665 | 369 Charbpos pos = find_next_newline (buf, BUF_PT (buf), -1); |
428 | 370 |
793 | 371 buffer = wrap_buffer (buf); |
428 | 372 |
373 if (!NILP (Fextent_at (make_int (pos), buffer, Qinvisible, Qnil, Qnil))) | |
374 return Qzero; | |
375 | |
826 | 376 return make_int (byte_spaces_at_point (buf, charbpos_to_bytebpos (buf, pos))); |
428 | 377 } |
378 | |
379 | |
380 DEFUN ("move-to-column", Fmove_to_column, 1, 3, 0, /* | |
381 Move point to column COLUMN in the current line. | |
382 The column of a character is calculated by adding together the widths | |
383 as displayed of the previous characters in the line. | |
384 This function ignores line-continuation; | |
385 there is no upper limit on the column number a character can have | |
386 and horizontal scrolling has no effect. | |
387 | |
388 If specified column is within a character, point goes after that character. | |
389 If it's past end of line, point goes to end of line. | |
390 | |
3025 | 391 A value of `coerce' for the second (optional) argument FORCE means if |
428 | 392 COLUMN is in the middle of a tab character, change it to spaces. |
393 Any other non-nil value means the same, plus if the line is too short to | |
394 reach column COLUMN, then add spaces/tabs to get there. | |
395 | |
396 Returns the actual column that it moved to. | |
397 */ | |
398 (column, force, buffer)) | |
399 { | |
400 /* This function can GC */ | |
665 | 401 Charbpos pos; |
428 | 402 struct buffer *buf = decode_buffer (buffer, 0); |
403 int col = current_column (buf); | |
404 int goal; | |
665 | 405 Charbpos end; |
428 | 406 int tab_width = XINT (buf->tab_width); |
407 | |
408 int prev_col = 0; | |
867 | 409 Ichar c = 0; |
428 | 410 |
793 | 411 buffer = wrap_buffer (buf); |
428 | 412 if (tab_width <= 0 || tab_width > 1000) tab_width = 8; |
413 CHECK_NATNUM (column); | |
414 goal = XINT (column); | |
415 | |
416 retry: | |
417 pos = BUF_PT (buf); | |
418 end = BUF_ZV (buf); | |
419 | |
420 /* If we're starting past the desired column, | |
421 back up to beginning of line and scan from there. */ | |
422 if (col > goal) | |
423 { | |
424 pos = find_next_newline (buf, pos, -1); | |
425 col = 0; | |
426 } | |
427 | |
428 while (col < goal && pos < end) | |
429 { | |
430 c = BUF_FETCH_CHAR (buf, pos); | |
431 if (c == '\n') | |
432 break; | |
433 if (c == '\r' && EQ (buf->selective_display, Qt)) | |
434 break; | |
435 if (c == '\t') | |
436 { | |
437 prev_col = col; | |
438 col += tab_width; | |
439 col = col / tab_width * tab_width; | |
440 } | |
441 else | |
442 { | |
443 /* #### oh for the days of the complete new redisplay */ | |
444 /* #### FSFmacs looks at ctl_arrow, display tables. | |
445 We need to do similar. */ | |
446 #if 0 | |
665 | 447 displayed_glyphs = glyphs_from_charbpos (selected_frame (), |
428 | 448 buf, |
449 XWINDOW (Fselected_window (Qnil)), | |
450 pos, dp, 0, col, 0, 0, 0); | |
451 col += (displayed_glyphs->columns | |
452 - (displayed_glyphs->begin_columns | |
453 + displayed_glyphs->end_columns)); | |
454 #else /* XEmacs */ | |
455 #ifdef MULE | |
867 | 456 col += XCHARSET_COLUMNS (ichar_charset (c)); |
428 | 457 #else |
458 col ++; | |
459 #endif /* MULE */ | |
460 #endif /* XEmacs */ | |
461 } | |
462 | |
463 pos++; | |
464 } | |
465 | |
466 BUF_SET_PT (buf, pos); | |
467 | |
468 /* If a tab char made us overshoot, change it to spaces | |
469 and scan through it again. */ | |
470 if (!NILP (force) && col > goal && c == '\t' && prev_col < goal) | |
471 { | |
472 buffer_delete_range (buf, BUF_PT (buf) - 1, BUF_PT (buf), 0); | |
473 Findent_to (make_int (col - 1), Qzero, buffer); | |
474 buffer_insert_emacs_char (buf, ' '); | |
475 goto retry; | |
476 } | |
477 | |
478 /* If line ends prematurely, add space to the end. */ | |
479 if (col < goal && !NILP (force) && !EQ (force, Qcoerce)) | |
480 { | |
481 col = goal; | |
482 Findent_to (make_int (col), Qzero, buffer); | |
483 } | |
484 | |
485 last_known_column_buffer = buf; | |
486 last_known_column = col; | |
487 last_known_column_point = BUF_PT (buf); | |
488 last_known_column_modified = BUF_MODIFF (buf); | |
489 | |
490 return make_int (col); | |
491 } | |
492 | |
493 #if 0 /* #### OK boys, this function needs to be present, I think. | |
494 It was there before the 19.12 redisplay rewrite. */ | |
495 | |
826 | 496 DEFUN ("compute-motion", Fcompute_motion, 7, 7, 0, /* |
428 | 497 "Scan through the current buffer, calculating screen position. |
498 Scan the current buffer forward from offset FROM, | |
499 assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)-- | |
500 to position TO or position TOPOS--another cons of the form (HPOS . VPOS)-- | |
501 and return the ending buffer position and screen location. | |
502 | |
503 There are three additional arguments: | |
504 | |
505 WIDTH is the number of columns available to display text; | |
506 this affects handling of continuation lines. | |
507 This is usually the value returned by `window-width', less one (to allow | |
508 for the continuation glyph). | |
509 | |
510 OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET). | |
511 HSCROLL is the number of columns not being displayed at the left | |
512 margin; this is usually taken from a window's hscroll member. | |
513 TAB-OFFSET is the number of columns of the first tab that aren't | |
514 being displayed, perhaps because the line was continued within it. | |
515 If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero. | |
516 | |
517 WINDOW is the window to operate on. Currently this is used only to | |
518 find the display table. It does not matter what buffer WINDOW displays; | |
519 `compute-motion' always operates on the current buffer. | |
520 | |
521 The value is a list of five elements: | |
522 (POS HPOS VPOS PREVHPOS CONTIN) | |
523 POS is the buffer position where the scan stopped. | |
524 VPOS is the vertical position where the scan stopped. | |
525 HPOS is the horizontal position where the scan stopped. | |
526 | |
527 PREVHPOS is the horizontal position one character back from POS. | |
528 CONTIN is t if a line was continued after (or within) the previous character. | |
529 | |
530 For example, to find the buffer position of column COL of line LINE | |
531 of a certain window, pass the window's starting location as FROM | |
532 and the window's upper-left coordinates as FROMPOS. | |
533 Pass the buffer's (point-max) as TO, to limit the scan to the end of the | |
534 visible section of the buffer, and pass LINE and COL as TOPOS. | |
535 */ | |
536 (from, frompos, to, topos, width, offsets, window)) | |
537 { | |
665 | 538 Lisp_Object charbpos, hpos, vpos, prevhpos, contin; |
428 | 539 struct position *pos; |
540 int hscroll, tab_offset; | |
541 struct window *w = decode_window (window); | |
542 | |
543 CHECK_INT_COERCE_MARKER (from); | |
544 CHECK_CONS (frompos); | |
545 CHECK_INT (XCAR (frompos)); | |
546 CHECK_INT (XCDR (frompos)); | |
547 CHECK_INT_COERCE_MARKER (to); | |
548 CHECK_CONS (topos); | |
549 CHECK_INT (XCAR (topos)); | |
550 CHECK_INT (XCDR (topos)); | |
551 CHECK_INT (width); | |
552 if (!NILP (offsets)) | |
553 { | |
554 CHECK_CONS (offsets); | |
555 CHECK_INT (XCAR (offsets)); | |
556 CHECK_INT (XCDR (offsets)); | |
557 hscroll = XINT (XCAR (offsets)); | |
558 tab_offset = XINT (XCDR (offsets)); | |
559 } | |
560 else | |
561 hscroll = tab_offset = 0; | |
562 | |
563 pos = compute_motion (XINT (from), XINT (XCDR (frompos)), | |
564 XINT (XCAR (frompos)), | |
565 XINT (to), XINT (XCDR (topos)), | |
566 XINT (XCAR (topos)), | |
567 XINT (width), hscroll, tab_offset, w); | |
568 | |
793 | 569 charbpos = make_int (pos->charbpos); |
570 hpos = make_int (pos->hpos); | |
571 vpos = make_int (pos->vpos); | |
572 prevhpos = make_int (pos->prevhpos); | |
428 | 573 |
665 | 574 return list5 (charbpos, hpos, vpos, prevhpos, |
428 | 575 pos->contin ? Qt : Qnil); |
576 } | |
577 | |
578 #endif /* 0 */ | |
579 | |
580 /* Helper for vmotion_1 - compute vertical pixel motion between | |
581 START and END in the line start cache CACHE. This just sums | |
582 the line heights, including both the starting and ending lines. | |
583 */ | |
584 static int | |
585 vpix_motion (line_start_cache_dynarr *cache, int start, int end) | |
586 { | |
587 int i, vpix; | |
588 | |
589 assert (start <= end); | |
590 assert (start >= 0); | |
591 assert (end < Dynarr_length (cache)); | |
592 | |
593 vpix = 0; | |
594 for (i = start; i <= end; i++) | |
595 vpix += Dynarr_atp (cache, i)->height; | |
596 | |
597 return vpix; | |
598 } | |
599 | |
600 /***************************************************************************** | |
601 vmotion_1 | |
602 | |
603 Given a starting position ORIG, move point VTARGET lines in WINDOW. | |
604 Returns the new value for point. If the arg ret_vpos is not nil, it is | |
605 taken to be a pointer to an int and the number of lines actually moved is | |
606 returned in it. If the arg ret_vpix is not nil, it is taken to be a | |
607 pointer to an int and the vertical pixel height of the motion which | |
608 took place is returned in it. | |
609 ****************************************************************************/ | |
665 | 610 static Charbpos |
611 vmotion_1 (struct window *w, Charbpos orig, int vtarget, | |
428 | 612 int *ret_vpos, int *ret_vpix) |
613 { | |
614 struct buffer *b = XBUFFER (w->buffer); | |
615 int elt; | |
616 | |
617 elt = point_in_line_start_cache (w, orig, (vtarget < 0 | |
618 ? -vtarget | |
619 : vtarget)); | |
620 | |
621 /* #### This assertion must be true before the if statements are hit | |
622 but may possibly be wrong after the call to | |
623 point_in_line_start_cache if orig is outside of the visible | |
624 region of the buffer. Handle this. */ | |
625 assert (elt >= 0); | |
626 | |
627 /* Moving downward. */ | |
628 if (vtarget > 0) | |
629 { | |
630 int cur_line = Dynarr_length (w->line_start_cache) - 1 - elt; | |
665 | 631 Charbpos ret_pt; |
428 | 632 |
633 if (cur_line > vtarget) | |
634 cur_line = vtarget; | |
635 | |
636 /* The traditional FSF behavior is to return the end of buffer | |
637 position if we couldn't move far enough because we hit it. */ | |
638 if (cur_line < vtarget) | |
639 ret_pt = BUF_ZV (b); | |
640 else | |
641 ret_pt = Dynarr_atp (w->line_start_cache, cur_line + elt)->start; | |
642 | |
643 while (ret_pt > BUF_ZV (b) && cur_line > 0) | |
644 { | |
645 cur_line--; | |
646 ret_pt = Dynarr_atp (w->line_start_cache, cur_line + elt)->start; | |
647 } | |
648 | |
649 if (ret_vpos) *ret_vpos = cur_line; | |
650 if (ret_vpix) | |
651 *ret_vpix = vpix_motion (w->line_start_cache, elt, cur_line + elt); | |
652 return ret_pt; | |
653 } | |
654 else if (vtarget < 0) | |
655 { | |
656 if (elt < -vtarget) | |
657 { | |
658 if (ret_vpos) *ret_vpos = -elt; | |
659 if (ret_vpix) | |
660 *ret_vpix = vpix_motion (w->line_start_cache, 0, elt); | |
661 /* #### This should be BUF_BEGV (b), right? */ | |
4967 | 662 return Dynarr_begin (w->line_start_cache)->start; |
428 | 663 } |
664 else | |
665 { | |
666 if (ret_vpos) *ret_vpos = vtarget; | |
667 if (ret_vpix) | |
668 *ret_vpix = vpix_motion (w->line_start_cache, elt + vtarget, elt); | |
669 return Dynarr_atp (w->line_start_cache, elt + vtarget)->start; | |
670 } | |
671 } | |
672 else | |
673 { | |
674 /* No vertical motion requested so we just return the position | |
675 of the beginning of the current line. */ | |
676 if (ret_vpos) *ret_vpos = 0; | |
677 if (ret_vpix) | |
678 *ret_vpix = vpix_motion (w->line_start_cache, elt, elt); | |
679 | |
680 return Dynarr_atp (w->line_start_cache, elt)->start; | |
681 } | |
682 | |
1204 | 683 RETURN_NOT_REACHED(0); /* shut up compiler */ |
428 | 684 } |
685 | |
686 /***************************************************************************** | |
687 vmotion | |
688 | |
689 Given a starting position ORIG, move point VTARGET lines in WINDOW. | |
690 Returns the new value for point. If the arg ret_vpos is not nil, it is | |
691 taken to be a pointer to an int and the number of lines actually moved is | |
692 returned in it. | |
693 ****************************************************************************/ | |
665 | 694 Charbpos |
695 vmotion (struct window *w, Charbpos orig, int vtarget, int *ret_vpos) | |
428 | 696 { |
697 return vmotion_1 (w, orig, vtarget, ret_vpos, NULL); | |
698 } | |
699 | |
700 /* Helper for Fvertical_motion. | |
701 */ | |
702 static | |
703 Lisp_Object vertical_motion_1 (Lisp_Object lines, Lisp_Object window, | |
704 int pixels) | |
705 { | |
665 | 706 Charbpos charbpos; |
707 Charbpos orig; | |
428 | 708 int selected; |
709 int *vpos, *vpix; | |
710 int value=0; | |
711 struct window *w; | |
712 | |
713 if (NILP (window)) | |
714 window = Fselected_window (Qnil); | |
715 | |
716 CHECK_LIVE_WINDOW (window); | |
717 CHECK_INT (lines); | |
718 | |
719 selected = (EQ (window, Fselected_window (Qnil))); | |
720 | |
721 w = XWINDOW (window); | |
722 | |
723 orig = selected ? BUF_PT (XBUFFER (w->buffer)) | |
724 : marker_position (w->pointm[CURRENT_DISP]); | |
725 | |
726 vpos = pixels ? NULL : &value; | |
727 vpix = pixels ? &value : NULL; | |
728 | |
665 | 729 charbpos = vmotion_1 (w, orig, XINT (lines), vpos, vpix); |
428 | 730 |
731 /* Note that the buffer's point is set, not the window's point. */ | |
732 if (selected) | |
665 | 733 BUF_SET_PT (XBUFFER (w->buffer), charbpos); |
428 | 734 else |
735 set_marker_restricted (w->pointm[CURRENT_DISP], | |
665 | 736 make_int(charbpos), |
428 | 737 w->buffer); |
738 | |
739 return make_int (value); | |
740 } | |
741 | |
742 DEFUN ("vertical-motion", Fvertical_motion, 1, 3, 0, /* | |
743 Move to start of frame line LINES lines down. | |
744 If LINES is negative, this is moving up. | |
745 Optional second argument is WINDOW to move in, | |
746 the default is the selected window. | |
747 | |
748 Sets point to position found; this may be start of line | |
749 or just the start of a continuation line. | |
750 If optional third argument PIXELS is nil, returns number | |
751 of lines moved; may be closer to zero than LINES if beginning | |
752 or end of buffer was reached. If PIXELS is non-nil, the | |
753 vertical pixel height of the motion which took place is | |
754 returned instead of the actual number of lines moved. A | |
755 motion of zero lines returns the height of the current line. | |
756 | |
1268 | 757 NOTE NOTE NOTE: GNU Emacs/XEmacs difference. |
758 | |
759 What `vertical-motion' actually does is set WINDOW's buffer's point | |
760 if WINDOW is the selected window; else, it sets WINDOW's point. | |
761 This is unfortunately somewhat tricky to work with, and different | |
762 from GNU Emacs, which always uses the current buffer, not WINDOW's | |
763 buffer, always sets current buffer's point, and, from the | |
764 perspective of this function, temporarily makes WINDOW display | |
765 the current buffer if it wasn't already. | |
428 | 766 */ |
767 (lines, window, pixels)) | |
768 { | |
769 return vertical_motion_1 (lines, window, !NILP (pixels)); | |
770 } | |
771 | |
772 /* | |
773 * Like vmotion() but requested and returned movement is in pixels. | |
774 * HOW specifies the stopping condition. Positive means move at least | |
775 * PIXELS. Negative means at most. Zero means as close as possible. | |
776 */ | |
665 | 777 Charbpos |
778 vmotion_pixels (Lisp_Object window, Charbpos start, int pixels, int how, | |
428 | 779 int *motion) |
780 { | |
781 struct window *w; | |
665 | 782 Charbpos eobuf, bobuf; |
428 | 783 int defheight; |
784 int needed; | |
785 int line, next; | |
786 int remain, abspix, dirn; | |
787 int elt, nelt; | |
788 int i; | |
789 line_start_cache_dynarr *cache; | |
790 int previous = -1; | |
791 int lines; | |
792 | |
793 if (NILP (window)) | |
794 window = Fselected_window (Qnil); | |
795 | |
796 CHECK_LIVE_WINDOW (window); | |
797 w = XWINDOW (window); | |
798 | |
799 eobuf = BUF_ZV (XBUFFER (w->buffer)); | |
800 bobuf = BUF_BEGV (XBUFFER (w->buffer)); | |
801 | |
5047
07dcc7000bbf
put width before height consistently, fix a real bug found in the process
Ben Wing <ben@xemacs.org>
parents:
4998
diff
changeset
|
802 default_face_width_and_height (window, NULL, &defheight); |
428 | 803 |
804 /* guess num lines needed in line start cache + a few extra */ | |
805 abspix = abs (pixels); | |
806 needed = (abspix + defheight-1)/defheight + 3; | |
807 | |
808 dirn = (pixels >= 0) ? 1 : -1; | |
809 | |
810 while (1) | |
811 { | |
812 elt = point_in_line_start_cache (w, start, needed); | |
813 assert (elt >= 0); /* in the cache */ | |
814 | |
815 cache = w->line_start_cache; | |
816 nelt = Dynarr_length (cache); | |
817 | |
818 *motion = 0; | |
819 | |
820 if (pixels == 0) | |
821 /* No vertical motion requested so we just return the position | |
822 of the beginning of the current display line. */ | |
823 return Dynarr_atp (cache, elt)->start; | |
824 | |
825 if ((dirn < 0 && elt == 0 && | |
826 Dynarr_atp (cache, elt)->start <= bobuf) || | |
827 (dirn > 0 && elt == nelt-1 && | |
828 Dynarr_atp (cache, elt)->end >= eobuf)) | |
829 return Dynarr_atp (cache, elt)->start; | |
830 | |
831 remain = abspix; | |
832 for (i = elt; (dirn > 0) ? (i < nelt) : (i > 0); i += dirn) | |
833 { | |
834 /* cache line we're considering moving over */ | |
835 int ii = (dirn > 0) ? i : i-1; | |
836 | |
837 if (remain < 0) | |
838 return Dynarr_atp (cache, i)->start; | |
839 | |
840 line = Dynarr_atp (cache, ii)->height; | |
841 next = remain - line; | |
842 | |
843 /* is stopping condition satisfied? */ | |
844 if ((how > 0 && remain <= 0) || /* at least */ | |
845 (how < 0 && next < 0) || /* at most */ | |
846 (how == 0 && remain <= abs (next))) /* closest */ | |
847 return Dynarr_atp (cache, i)->start; | |
848 | |
849 /* moving down and nowhere left to go? */ | |
850 if (dirn > 0 && Dynarr_atp (cache, ii)->end >= eobuf) | |
851 return Dynarr_atp (cache, ii)->start; | |
852 | |
853 /* take the step */ | |
854 remain = next; | |
855 *motion += dirn * line; | |
856 | |
857 /* moving up and nowhere left to go? */ | |
858 if (dirn < 0 && Dynarr_atp (cache, ii)->start <= bobuf) | |
859 return Dynarr_atp (cache, ii)->start; | |
860 } | |
861 | |
862 /* get here => need more cache lines. try again. */ | |
863 assert (abs (*motion) > previous); /* progress? */ | |
864 previous = abs (*motion); | |
865 | |
866 lines = (pixels < 0) ? elt : (nelt - elt); | |
867 needed += (remain*lines + abspix-1)/abspix + 3; | |
868 } | |
869 | |
1204 | 870 RETURN_NOT_REACHED(0); /* shut up compiler */ |
428 | 871 } |
872 | |
873 DEFUN ("vertical-motion-pixels", Fvertical_motion_pixels, 1, 3, 0, /* | |
874 Move to start of frame line PIXELS vertical pixels down. | |
875 If PIXELS is negative, this is moving up. | |
876 The actual vertical motion in pixels is returned. | |
877 | |
878 Optional second argument is WINDOW to move in, | |
879 the default is the selected window. | |
880 | |
881 Optional third argument HOW specifies when to stop. A value | |
882 less than zero indicates that the motion should be no more | |
883 than PIXELS. A value greater than zero indicates that the | |
884 motion should be at least PIXELS. Any other value indicates | |
885 that the motion should be as close as possible to PIXELS. | |
886 */ | |
887 (pixels, window, how)) | |
888 { | |
665 | 889 Charbpos charbpos; |
890 Charbpos orig; | |
428 | 891 int selected; |
892 int motion; | |
893 int howto; | |
894 struct window *w; | |
895 | |
896 if (NILP (window)) | |
897 window = Fselected_window (Qnil); | |
898 | |
899 CHECK_LIVE_WINDOW (window); | |
900 CHECK_INT (pixels); | |
901 | |
902 selected = (EQ (window, Fselected_window (Qnil))); | |
903 | |
904 w = XWINDOW (window); | |
905 | |
906 orig = selected ? BUF_PT (XBUFFER (w->buffer)) | |
907 : marker_position (w->pointm[CURRENT_DISP]); | |
908 | |
909 howto = INTP (how) ? XINT (how) : 0; | |
910 | |
665 | 911 charbpos = vmotion_pixels (window, orig, XINT (pixels), howto, &motion); |
428 | 912 |
913 if (selected) | |
665 | 914 BUF_SET_PT (XBUFFER (w->buffer), charbpos); |
428 | 915 else |
916 set_marker_restricted (w->pointm[CURRENT_DISP], | |
665 | 917 make_int(charbpos), |
428 | 918 w->buffer); |
919 | |
920 return make_int (motion); | |
921 } | |
922 | |
923 | |
924 void | |
925 syms_of_indent (void) | |
926 { | |
927 DEFSUBR (Fcurrent_indentation); | |
928 DEFSUBR (Findent_to); | |
929 DEFSUBR (Fcurrent_column); | |
930 DEFSUBR (Fmove_to_column); | |
931 #if 0 /* #### */ | |
932 DEFSUBR (Fcompute_motion); | |
933 #endif | |
934 DEFSUBR (Fvertical_motion); | |
935 DEFSUBR (Fvertical_motion_pixels); | |
936 } | |
937 | |
938 void | |
939 vars_of_indent (void) | |
940 { | |
941 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode /* | |
942 *Indentation can insert tabs if this is non-nil. | |
943 Setting this variable automatically makes it local to the current buffer. | |
944 */ ); | |
945 indent_tabs_mode = 1; | |
946 } |