📄 abbrev.c
字号:
/* Primitives for word-abbrev mode. Copyright (C) 1985, 1986 Free Software Foundation, Inc.This file is part of GNU Emacs.GNU Emacs is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 1, or (at your option)any later version.GNU Emacs is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Emacs; see the file COPYING. If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */#include "config.h"#include <stdio.h>#undef NULL#include "lisp.h"#include "commands.h"#include "buffer.h"#include "window.h"/* An abbrev table is an obarray. Each defined abbrev is represented by a symbol in that obarray whose print name is the abbreviation. The symbol's value is a string which is the expansion. If its function definition is non-nil, it is called after the expansion is done. The plist slot of the abbrev symbol is its usage count. *//* List of all abbrev-table name symbols: symbols whose values are abbrev tables. */Lisp_Object Vabbrev_table_name_list;/* The table of global abbrevs. These are in effect in any buffer in which abbrev mode is turned on. */Lisp_Object Vglobal_abbrev_table;/* The local abbrev table used by default (in Fundamental Mode buffers) */Lisp_Object Vfundamental_mode_abbrev_table;/* Set nonzero when an abbrev definition is changed */int abbrevs_changed;int abbrev_all_caps;/* Non-nil => use this location as the start of abbrev to expand (rather than taking the word before point as the abbrev) */Lisp_Object Vabbrev_start_location;/* Buffer that Vabbrev_start_location applies to */Lisp_Object Vabbrev_start_location_buffer;/* The symbol representing the abbrev most recently expanded */Lisp_Object Vlast_abbrev;/* A string for the actual text of the abbrev most recently expanded. This has more info than Vlast_abbrev since case is significant. */Lisp_Object Vlast_abbrev_text;/* Character address of start of last abbrev expanded */int last_abbrev_point;extern Lisp_Object oblookup ();DEFUN ("make-abbrev-table", Fmake_abbrev_table, Smake_abbrev_table, 0, 0, 0, "Create a new, empty abbrev table object.") (){ return Fmake_vector (make_number (59), make_number (0));}DEFUN ("clear-abbrev-table", Fclear_abbrev_table, Sclear_abbrev_table, 1, 1, 0, "Undefine all abbrevs in abbrev table TABLE, leaving it empty.") (table) Lisp_Object table;{ int i, size; CHECK_VECTOR (table, 0); size = XVECTOR (table)->size; abbrevs_changed = 1; for (i = 0; i < size; i++) XVECTOR (table)->contents[i] = make_number (0); return Qnil;}DEFUN ("define-abbrev", Fdefine_abbrev, Sdefine_abbrev, 3, 5, 0, "Define an abbrev in TABLE named NAME, to expand to EXPANSION or call HOOK.\n\NAME and EXPANSION are strings. HOOK is a function or nil.\n\To undefine an abbrev, define it with EXPANSION = nil") (table, name, expansion, hook, count) Lisp_Object table, name, expansion, hook, count;{ Lisp_Object sym, oexp, ohook, tem; CHECK_VECTOR (table, 0); CHECK_STRING (name, 1); CHECK_STRING (expansion, 2); if (NULL (count)) count = make_number (0); else CHECK_NUMBER (count, 0); sym = Fintern (name, table); oexp = XSYMBOL (sym)->value; ohook = XSYMBOL (sym)->function; if (!((EQ (oexp, expansion) || (XTYPE (oexp) == Lisp_String && XTYPE (expansion) == Lisp_String && (tem = Fstring_equal (oexp, expansion), !NULL (tem)))) && (EQ (ohook, hook) || (tem = Fequal (ohook, hook), !NULL (tem))))) abbrevs_changed = 1; Fset (sym, expansion); Ffset (sym, hook); Fsetplist (sym, count); return name;}DEFUN ("define-global-abbrev", Fdefine_global_abbrev, Sdefine_global_abbrev, 2, 2, "sDefine global abbrev: \nsExpansion for %s: ", "Define ABBREV as a global abbreviation for EXPANSION.") (name, expansion) Lisp_Object name, expansion;{ Fdefine_abbrev (Vglobal_abbrev_table, Fdowncase (name), expansion, Qnil, make_number (0)); return name;}DEFUN ("define-mode-abbrev", Fdefine_mode_abbrev, Sdefine_mode_abbrev, 2, 2, "sDefine mode abbrev: \nsExpansion for %s: ", "Define ABBREV as a mode-specific abbreviation for EXPANSION.") (name, expansion) Lisp_Object name, expansion;{ if (NULL (current_buffer->abbrev_table)) error ("Major mode has no abbrev table"); Fdefine_abbrev (current_buffer->abbrev_table, Fdowncase (name), expansion, Qnil, make_number (0)); return name;}DEFUN ("abbrev-symbol", Fabbrev_symbol, Sabbrev_symbol, 1, 2, 0, "Return the symbol representing abbrev named ABBREV.\n\Value is nil if that abbrev is not defined.\n\Optional second arg TABLE is abbrev table to look it up in.\n\Default is try buffer's mode-specific abbrev table, then global table.") (abbrev, table) Lisp_Object abbrev, table;{ Lisp_Object sym; CHECK_STRING (abbrev, 0); if (!NULL (table)) sym = Fintern_soft (abbrev, table); else { sym = Qnil; if (!NULL (current_buffer->abbrev_table)) sym = Fintern_soft (abbrev, current_buffer->abbrev_table); if (NULL (XSYMBOL (sym)->value)) sym = Qnil; if (NULL (sym)) sym = Fintern_soft (abbrev, Vglobal_abbrev_table); } if (NULL (XSYMBOL (sym)->value)) return Qnil; return sym;}DEFUN ("abbrev-expansion", Fabbrev_expansion, Sabbrev_expansion, 1, 2, 0, "Return the string that ABBREV expands into in the current buffer.\n\Optionally specify an abbrev table; then ABBREV is looked up in that table only.") (abbrev, table) Lisp_Object abbrev, table;{ Lisp_Object sym; sym = Fabbrev_symbol (abbrev, table); if (NULL (sym)) return sym; return Fsymbol_value (sym);}/* Expand the word before point, if it is an abbrev. Returns 1 if an expansion is done. */DEFUN ("expand-abbrev", Fexpand_abbrev, Sexpand_abbrev, 0, 0, "", "Expand the abbrev before point, if it is an abbrev.\n\Effective when explicitly called even when abbrev-mode is not enabled.\n\Returns t if expansion took place.") (){ char buffer[200]; register char *p = buffer; register int wordstart, idx; int uccount = 0, lccount = 0; register Lisp_Object sym; Lisp_Object expansion, hook, tem; if (XBUFFER (Vabbrev_start_location_buffer) != current_buffer) Vabbrev_start_location = Qnil; if (!NULL (Vabbrev_start_location)) { tem = Vabbrev_start_location; CHECK_NUMBER_COERCE_MARKER (tem, 0); wordstart = XINT (tem); Vabbrev_start_location = Qnil; if (FETCH_CHAR (wordstart) == '-') del_range (wordstart, wordstart + 1); } else wordstart = scan_words (point, -1); if (!wordstart || point - wordstart >= sizeof buffer || point <= wordstart) return Qnil; for (idx = wordstart; idx < point; idx++) { register int c = FETCH_CHAR (idx); if (UPPERCASEP (c)) c = DOWNCASE (c), uccount++; else if (! NOCASEP (c)) lccount++; *p++ = c; } if (XTYPE (current_buffer->abbrev_table) == Lisp_Vector) sym = oblookup (current_buffer->abbrev_table, buffer, p - buffer); else XFASTINT (sym) = 0; if (XTYPE (sym) == Lisp_Int || NULL (XSYMBOL (sym)->value)) sym = oblookup (Vglobal_abbrev_table, buffer, p - buffer); if (XTYPE (sym) == Lisp_Int || NULL (XSYMBOL (sym)->value)) return Qnil; if (FROM_KBD && !EQ (minibuf_window, selected_window)) { SET_PT (wordstart + p - buffer); Fundo_boundary (); } SET_PT (wordstart); Vlast_abbrev_text = Fbuffer_substring (make_number (point), make_number (point + (p - buffer)));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -