/* Take the word before point (or Vabbrev_start_location, if non-nil), and look it up in OBARRAY, and return the symbol (or zero). This used to be the default method of searching, with the obvious limitation that the abbrevs may consist only of word characters. It is an order of magnitude faster than the proper abbrev_match(), but then again, vi is an order of magnitude faster than Emacs. This speed difference should be unnoticeable, though. I have tested the degenerated cases of thousands of abbrevs being defined, and abbrev_match() was still fast enough for normal operation. */ static Lisp_Symbol *abbrev_oblookup(struct buffer *buf, Lisp_Object obarray) { Bufpos wordstart, wordend; Bufbyte *word, *p; Bytecount idx; Lisp_Object lookup; CHECK_VECTOR(obarray); if (!NILP(Vabbrev_start_location)) { wordstart = get_buffer_pos_char(buf, Vabbrev_start_location, GB_COERCE_RANGE); Vabbrev_start_location = Qnil; #if 0 /* Previously, abbrev-prefix-mark crockishly inserted a dash to indicate the abbrev start point. It now uses an extent with a begin glyph so there's no dash to remove. */ if (wordstart != BUF_ZV(buf) && BUF_FETCH_CHAR(buf, wordstart) == '-') { buffer_delete_range(buf, wordstart, wordstart + 1, 0); } #endif wordend = BUF_PT(buf); } else { Bufpos point = BUF_PT(buf); wordstart = scan_words(buf, point, -1); if (!wordstart) return 0; wordend = scan_words(buf, wordstart, 1); if (!wordend) return 0; if (wordend > BUF_ZV(buf)) wordend = BUF_ZV(buf); if (wordend > point) wordend = point; /* Unlike the original function, we allow expansion only after the abbrev, not preceded by a number of spaces. This is because of consistency with abbrev_match. */ if (wordend < point) return 0; } if (wordend <= wordstart) return 0; p = word = (Bufbyte *) alloca(MAX_EMCHAR_LEN * (wordend - wordstart)); for (idx = wordstart; idx < wordend; idx++) { Emchar c = BUF_FETCH_CHAR(buf, idx); if (UPPERCASEP(buf, c)) c = DOWNCASE(buf, c); p += set_charptr_emchar(p, c); } lookup = oblookup(obarray, word, p - word); if (SYMBOLP(lookup) && !NILP(symbol_value(XSYMBOL(lookup)))) return XSYMBOL(lookup); else return NULL; }
/* For use by abbrev_match(): Match SYMBOL's name against buffer text before point, case-insensitively. When found, return non-zero, so that map_obarray terminates mapping. */ static int abbrev_match_mapper(Lisp_Object symbol, void *arg) { struct abbrev_match_mapper_closure *closure = (struct abbrev_match_mapper_closure *)arg; Charcount abbrev_length; Lisp_Symbol *sym = XSYMBOL(symbol); Lisp_String *abbrev; /* symbol_value should be OK here, because abbrevs are not expected to contain any SYMBOL_MAGIC stuff. */ if (UNBOUNDP(symbol_value(sym)) || NILP(symbol_value(sym))) { /* The symbol value of nil means that abbrev got undefined. */ return 0; } abbrev = symbol_name(sym); abbrev_length = string_char_length(abbrev); if (abbrev_length > closure->maxlen) { /* This abbrev is too large -- it wouldn't fit. */ return 0; } /* If `bar' is an abbrev, and a user presses `fubar<SPC>', we don't normally want to expand it. OTOH, if the abbrev begins with non-word syntax (e.g. `#if'), it is OK to abbreviate it anywhere. */ if (abbrev_length < closure->maxlen && abbrev_length > 0 && (WORD_SYNTAX_P(closure->chartab, string_char(abbrev, 0))) && (WORD_SYNTAX_P(closure->chartab, BUF_FETCH_CHAR(closure->buf, closure->point - (abbrev_length + 1))))) { return 0; } /* Match abbreviation string against buffer text. */ { Bufbyte *ptr = string_data(abbrev); Charcount idx; for (idx = 0; idx < abbrev_length; idx++) { if (DOWNCASE(closure->buf, BUF_FETCH_CHAR(closure->buf, closure->point - abbrev_length + idx)) != DOWNCASE(closure->buf, charptr_emchar(ptr))) { break; } INC_CHARPTR(ptr); } if (idx == abbrev_length) { /* This is the one. */ closure->found = sym; return 1; } } return 0; }
/* Analyze case in the buffer substring, and report it. */ static void abbrev_count_case(struct buffer *buf, Bufpos pos, Charcount length, int *lccount, int *uccount) { *lccount = *uccount = 0; while (length--) { Emchar c = BUF_FETCH_CHAR(buf, pos); if (UPPERCASEP(buf, c)) ++ * uccount; else if (LOWERCASEP(buf, c)) ++ * lccount; ++pos; } }
static void find_context (struct buffer *buf, Bufpos pt) { /* This function can GC */ #ifndef emacs #ifdef UTF2000 Lisp_Char_Table *mirrortab = XCHAR_TABLE (buf->syntax_table); #else Lisp_Char_Table *mirrortab = XCHAR_TABLE (buf->mirror_syntax_table); #endif Lisp_Object syntaxtab = buf->syntax_table; #endif Emchar prev_c, c; int prev_syncode, syncode; Bufpos target = pt; setup_context_cache (buf, pt); pt = context_cache.cur_point; SCS_STATISTICS_SET_FUNCTION (scs_find_context); SETUP_SYNTAX_CACHE (pt - 1, 1); if (pt > BUF_BEGV (buf)) { c = BUF_FETCH_CHAR (buf, pt - 1); syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c); } else { c = '\n'; /* to get bol_context_cache at point-min */ syncode = Swhitespace; } for (; pt < target; pt++, context_cache.cur_point = pt) { if (context_cache.needs_its_head_reexamined) { if (context_cache.depth == 0 && context_cache.context == context_none) { /* We've found an anchor spot. Try to put the start of defun within 6000 chars of the target, and the end of defun as close as possible. 6000 is also arbitrary but tries to strike a balance between two conflicting pulls when dealing with a file that has lots of stuff sitting outside of a top- level form: a) If you move past the start of defun, you will have to recompute defun, which in this case means that start of defun goes all the way back to the beginning of the file; so you want to set start of defun a ways back from the current point. b) If you move a line backwards but within start of defun, you have to move back to start of defun; so you don't want start of defun too far from the current point. */ if (target - context_cache.start_point > 6000) context_cache.start_point = pt; context_cache.end_point = pt; bol_context_cache = context_cache; } } UPDATE_SYNTAX_CACHE_FORWARD (pt); prev_c = c; prev_syncode = syncode; c = BUF_FETCH_CHAR (buf, pt); syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c); if (prev_c == '\n') bol_context_cache = context_cache; if (context_cache.backslash_p) { context_cache.backslash_p = 0; continue; } switch (SYNTAX_FROM_CACHE (mirrortab, c)) { case Sescape: context_cache.backslash_p = 1; break; case Sopen: if (context_cache.context == context_none) context_cache.depth++; break; case Sclose: if (context_cache.context == context_none) context_cache.depth--; break; case Scomment: if (context_cache.context == context_none) { context_cache.context = context_comment; context_cache.ccontext = ccontext_none; context_cache.style = SINGLE_SYNTAX_STYLE (syncode); if (context_cache.style == comment_style_none) ABORT (); } break; case Sendcomment: if (context_cache.style != SINGLE_SYNTAX_STYLE (syncode)) ; else if (context_cache.context == context_comment) { context_cache.context = context_none; context_cache.style = comment_style_none; } else if (context_cache.context == context_block_comment && (context_cache.ccontext == ccontext_start2 || context_cache.ccontext == ccontext_end1)) { context_cache.context = context_none; context_cache.ccontext = ccontext_none; context_cache.style = comment_style_none; } break; case Sstring: { if (context_cache.context == context_string && context_cache.scontext == c) { context_cache.context = context_none; context_cache.scontext = '\000'; } else if (context_cache.context == context_none) { Lisp_Object stringtermobj = syntax_match (syntax_cache.current_syntax_table, c); Emchar stringterm; if (CHARP (stringtermobj)) stringterm = XCHAR (stringtermobj); else stringterm = c; context_cache.context = context_string; context_cache.scontext = stringterm; context_cache.ccontext = ccontext_none; } break; } case Scomment_fence: { if (context_cache.context == context_generic_comment) { context_cache.context = context_none; } else if (context_cache.context == context_none) { context_cache.context = context_generic_comment; context_cache.ccontext = ccontext_none; } break; } case Sstring_fence: { if (context_cache.context == context_generic_string) { context_cache.context = context_none; } else if (context_cache.context == context_none) { context_cache.context = context_generic_string; context_cache.ccontext = ccontext_none; } break; } default: ; } /* That takes care of the characters with manifest syntax. Now we've got to hack multi-char sequences that start and end block comments. */ if ((SYNTAX_CODE_COMMENT_BITS (syncode) & SYNTAX_SECOND_CHAR_START) && context_cache.context == context_none && context_cache.ccontext == ccontext_start1 && SYNTAX_CODES_START_P (prev_syncode, syncode) /* the two chars match */ ) { context_cache.ccontext = ccontext_start2; context_cache.style = SYNTAX_START_STYLE (prev_syncode, syncode); if (context_cache.style == comment_style_none) ABORT (); } else if ((SYNTAX_CODE_COMMENT_BITS (syncode) & SYNTAX_FIRST_CHAR_START) && context_cache.context == context_none && (context_cache.ccontext == ccontext_none || context_cache.ccontext == ccontext_start1)) { context_cache.ccontext = ccontext_start1; context_cache.style = comment_style_none; /* should be this already*/ } else if ((SYNTAX_CODE_COMMENT_BITS (syncode) & SYNTAX_SECOND_CHAR_END) && context_cache.context == context_block_comment && context_cache.ccontext == ccontext_end1 && SYNTAX_CODES_END_P (prev_syncode, syncode) && /* the two chars match */ context_cache.style == SYNTAX_END_STYLE (prev_syncode, syncode) ) { context_cache.context = context_none; context_cache.ccontext = ccontext_none; context_cache.style = comment_style_none; } else if ((SYNTAX_CODE_COMMENT_BITS (syncode) & SYNTAX_FIRST_CHAR_END) && context_cache.context == context_block_comment && #if 0 /* #### pre-Matt code had: */ (context_cache.style == SYNTAX_END_STYLE (c, BUF_FETCH_CHAR (buf, pt+1))) && /* why do these differ here?! */ #endif context_cache.style == SINGLE_SYNTAX_STYLE (syncode) && (context_cache.ccontext == ccontext_start2 || context_cache.ccontext == ccontext_end1)) /* check end1, to detect a repetition of the first char of a comment-end sequence. ie, '/xxx foo xxx/' or '/xxx foo x/', where 'x' = '*' -- mct */ { if (context_cache.style == comment_style_none) ABORT (); context_cache.ccontext = ccontext_end1; } else if (context_cache.ccontext == ccontext_start1) { if (context_cache.context != context_none) ABORT (); context_cache.ccontext = ccontext_none; } else if (context_cache.ccontext == ccontext_end1) { if (context_cache.context != context_block_comment) ABORT (); context_cache.context = context_none; context_cache.ccontext = ccontext_start2; } if (context_cache.ccontext == ccontext_start2 && context_cache.context == context_none) { context_cache.context = context_block_comment; if (context_cache.style == comment_style_none) ABORT (); } else if (context_cache.ccontext == ccontext_none && context_cache.context == context_block_comment) { context_cache.context = context_none; } } context_cache.needs_its_head_reexamined = 0; }
static void setup_context_cache (struct buffer *buf, Bufpos pt) { int recomputed_start_point = 0; /* This function can GC */ if (context_cache.buffer != buf || pt < context_cache.start_point) { start_over: if (font_lock_debug) stderr_out ("reset context cache\n"); /* OK, completely invalid. */ reset_context_cache (&context_cache); reset_context_cache (&bol_context_cache); } if (!context_cache.buffer) { /* Need to recompute the start point. */ if (font_lock_debug) stderr_out ("recompute start\n"); context_cache.start_point = beginning_of_defun (buf, pt); recomputed_start_point = 1; bol_context_cache.start_point = context_cache.start_point; bol_context_cache.buffer = context_cache.buffer = buf; } if (context_cache.end_point < context_cache.start_point) { /* Need to recompute the end point. */ if (font_lock_debug) stderr_out ("recompute end\n"); context_cache.end_point = end_of_defun (buf, context_cache.start_point); bol_context_cache.end_point = context_cache.end_point; } if (bol_context_cache.cur_point == 0 || pt < bol_context_cache.cur_point) { if (font_lock_debug) stderr_out ("reset to start\n"); if (pt > context_cache.end_point /* 3000 is some arbitrary delta but seems reasonable; about the size of a reasonable function */ && pt - context_cache.end_point > 3000) /* If we're far past the end of the top level form, don't trust it; recompute it. */ { /* But don't get in an infinite loop doing this. If we're really far past the end of the top level form, try to compute a pseudo-top-level form. */ if (recomputed_start_point) context_cache.needs_its_head_reexamined = 1; else /* force recomputation */ goto start_over; } /* Go to the nearest end of the top-level form that's before us. */ if (pt > context_cache.end_point) pt = context_cache.end_point; else pt = context_cache.start_point; /* Reset current point to start of buffer. */ context_cache.cur_point = pt; context_cache.context = context_none; context_cache.ccontext = ccontext_none; context_cache.style = comment_style_none; context_cache.scontext = '\000'; context_cache.depth = 0; /* #### shouldn't this be checking the character's syntax instead of explicitly testing for backslash characters? */ context_cache.backslash_p = ((pt > 1) && (BUF_FETCH_CHAR (buf, pt - 1) == '\\')); /* Note that the BOL context cache may not be at the beginning of the line, but that should be OK, nobody's checking. */ bol_context_cache = context_cache; return; } else if (pt < context_cache.cur_point) { if (font_lock_debug) stderr_out ("reset to bol\n"); /* bol cache is OK but current_cache is not. */ context_cache = bol_context_cache; return; } else if (pt <= context_cache.end_point) { if (font_lock_debug) stderr_out ("everything is OK\n"); /* in same top-level form. */ return; } { /* OK, we're past the end of the top-level form. */ Bufpos maxpt = max (context_cache.end_point, context_cache.cur_point); #if 0 int shortage; #endif if (font_lock_debug) stderr_out ("past end\n"); if (pt <= maxpt) /* OK, fine. */ return; #if 0 /* This appears to cause huge slowdowns in files which have no top-level forms. In any case, it's not really necessary that we know for sure the top-level form we're in; if we're in a form but the form we have recorded is the previous one, it will be OK. */ scan_buffer (buf, '\n', maxpt, pt, 1, &shortage, 1); if (!shortage) /* If there was a newline in the region past the known universe, we might be inside another top-level form, so start over. Otherwise, we're outside of any top-level forms and we know the one directly before us, so it's OK. */ goto start_over; #endif } }
int column_at_point (struct buffer *buf, Bufpos init_pos, int cur_col) { int col; int tab_seen; int tab_width = XINT (buf->tab_width); int post_tab; Bufpos pos = init_pos; Emchar c; if (tab_width <= 0 || tab_width > 1000) tab_width = 8; col = tab_seen = post_tab = 0; while (1) { if (pos <= BUF_BEGV (buf)) break; pos--; c = BUF_FETCH_CHAR (buf, pos); if (c == '\t') { if (tab_seen) col = ((col + tab_width) / tab_width) * tab_width; post_tab += col; col = 0; tab_seen = 1; } else if (c == '\n' || (EQ (buf->selective_display, Qt) && c == '\r')) break; else { /* #### This needs updating to handle the new redisplay. */ /* #### FSFmacs looks at ctl_arrow, display tables. We need to do similar. */ #if 0 displayed_glyphs = glyphs_from_bufpos (sel_frame, buf, XWINDOW (selected_window), pos, dp, 0, col, 0, 0, 0); col += (displayed_glyphs->columns - (displayed_glyphs->begin_columns + displayed_glyphs->end_columns)); #else /* XEmacs */ #ifdef MULE col += CHAR_COLUMNS (c); #else col ++; #endif /* MULE */ #endif /* XEmacs */ } } if (tab_seen) { col = ((col + tab_width) / tab_width) * tab_width; col += post_tab; } if (cur_col) { last_known_column_buffer = buf; last_known_column = col; last_known_column_point = init_pos; last_known_column_modified = BUF_MODIFF (buf); } return col; }