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