📄 subr.el
字号:
;;; subr.el --- basic lisp subroutines for XEmacs;; Copyright (C) 1985, 1986, 1992, 1994-5, 1997 Free Software Foundation, Inc.;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.;; Copyright (C) 1995 Sun Microsystems.;; Copyright (C) 2000 Ben Wing.;; Maintainer: XEmacs Development Team;; Keywords: extensions, dumped;; This file is part of XEmacs.;; XEmacs is free software; you can redistribute it and/or modify it;; under the terms of the GNU General Public License as published by;; the Free Software Foundation; either version 2, or (at your option);; any later version.;; XEmacs is distributed in the hope that it will be useful, but;; WITHOUT ANY WARRANTY; without even the implied warranty of;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU;; General Public License for more details.;; You should have received a copy of the GNU General Public License;; along with XEmacs; see the file COPYING. If not, write to the Free;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA;; 02111-1307, USA.;;; Synched up with: FSF 19.34.;;; Commentary:;; This file is dumped with XEmacs.;; There's not a whole lot in common now with the FSF version,;; be wary when applying differences. I've left in a number of lines;; of commentary just to give diff(1) something to synch itself with to;; provide useful context diffs. -sb;;; Code:;;;; Lisp language features.#|(defmacro lambda (&rest cdr) "Return a lambda expression.A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) isself-quoting; the result of evaluating the lambda expression is theexpression itself. The lambda expression may then be treated as afunction, i.e., stored as the function value of a symbol, passed tofuncall or mapcar, etc.ARGS should take the same form as an argument list for a `defun'.DOCSTRING is an optional documentation string. If present, it should describe how to call the function. But documentation strings are usually not useful in nameless functions.INTERACTIVE should be a call to the function `interactive', which see.It may also be omitted.BODY should be a list of lisp expressions." `(function (lambda ,@cdr)))(defmacro defun-when-void (&rest args) "Define a function, just like `defun', unless it's already defined.Used for compatibility among different emacs variants." `(if (fboundp ',(car args)) nil (defun ,@args)))(defmacro define-function-when-void (&rest args) "Define a function, just like `define-function', unless it's already defined.Used for compatibility among different emacs variants." `(if (fboundp ,(car args)) nil (define-function ,@args)))|#;;;; Keymap support.;; XEmacs: removed to keymap.el;;;; The global keymap tree.;;; global-map, esc-map, and ctl-x-map have their values set up in;;; keymap.c; we just give them docstrings here.;;;; Event manipulation functions.;; XEmacs: This stuff is done in C Code.;;;; Obsolescent names for functions.;; XEmacs: not used.;; XEmacs:(defun local-variable-if-set-p (sym buffer) "Return t if SYM would be local to BUFFER after it is set.A nil value for BUFFER is *not* the same as (current-buffer), butcan be used to determine whether `make-variable-buffer-local' has beencalled on SYM." (local-variable-p sym buffer t));;;; Hook manipulation functions.;; (defconst run-hooks 'run-hooks ...)(defun make-local-hook (hook) "Make the hook HOOK local to the current buffer.When a hook is local, its local and global valueswork in concert: running the hook actually runs all the hookfunctions listed in *either* the local value *or* the global valueof the hook variable.This function works by making `t' a member of the buffer-local value,which acts as a flag to run the hook functions in the default value aswell. This works for all normal hooks, but does not work for mostnon-normal hooks yet. We will be changing the callers of non-normalhooks so that they can handle localness; this has to be done one byone.This function does nothing if HOOK is already local in the currentbuffer.Do not use `make-local-variable' to make a hook variable buffer-local.See also `add-local-hook' and `remove-local-hook'." (if (local-variable-p hook (current-buffer)) ; XEmacs nil (or (boundp hook) (set hook nil)) (make-local-variable hook) (set hook (list t))))(defun add-hook (hook function &optional append local) "Add to the value of HOOK the function FUNCTION.FUNCTION is not added if already present.FUNCTION is added (if necessary) at the beginning of the hook listunless the optional argument APPEND is non-nil, in which caseFUNCTION is added at the end.The optional fourth argument, LOCAL, if non-nil, says to modifythe hook's buffer-local value rather than its default value.This makes no difference if the hook is not buffer-local.To make a hook variable buffer-local, always use`make-local-hook', not `make-local-variable'.HOOK should be a symbol, and FUNCTION may be any valid function. IfHOOK is void, it is first set to nil. If HOOK's value is a singlefunction, it is changed to a list of functions.You can remove this hook yourself using `remove-hook'.See also `add-local-hook' and `add-one-shot-hook'." (or (boundp hook) (set hook nil)) (or (default-boundp hook) (set-default hook nil)) ;; If the hook value is a single function, turn it into a list. (let ((old (symbol-value hook))) (if (or (not (listp old)) (eq (car old) 'lambda)) (set hook (list old)))) (if (or local ;; Detect the case where make-local-variable was used on a hook ;; and do what we used to do. (and (local-variable-if-set-p hook (current-buffer)) ; XEmacs (not (memq t (symbol-value hook))))) ;; Alter the local value only. (or (if (consp function) (member function (symbol-value hook)) (memq function (symbol-value hook))) (set hook (if append (append (symbol-value hook) (list function)) (cons function (symbol-value hook))))) ;; Alter the global value (which is also the only value, ;; if the hook doesn't have a local value). (or (if (consp function) (member function (default-value hook)) (memq function (default-value hook))) (set-default hook (if append (append (default-value hook) (list function)) (cons function (default-value hook)))))))(defun remove-hook (hook function &optional local) "Remove from the value of HOOK the function FUNCTION.HOOK should be a symbol, and FUNCTION may be any valid function. IfFUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in thelist of hooks to run in HOOK, then nothing is done. See `add-hook'.The optional third argument, LOCAL, if non-nil, says to modifythe hook's buffer-local value rather than its default value.This makes no difference if the hook is not buffer-local.To make a hook variable buffer-local, always use`make-local-hook', not `make-local-variable'." (if (or (not (boundp hook)) ;unbound symbol, or (not (default-boundp 'hook)) (null (symbol-value hook)) ;value is nil, or (null function)) ;function is nil, then nil ;Do nothing. (flet ((hook-remove (function hook-value) (flet ((hook-test (fn hel) (or (equal fn hel) (and (symbolp hel) (equal fn (get hel 'one-shot-hook-fun)))))) (if (and (consp hook-value) (not (functionp hook-value))) (if (member* function hook-value :test 'hook-test) (setq hook-value (delete* function (copy-sequence hook-value) :test 'hook-test))) (if (equal hook-value function) (setq hook-value nil))) hook-value))) (if (or local ;; Detect the case where make-local-variable was used on a hook ;; and do what we used to do. (and (local-variable-p hook (current-buffer)) (not (memq t (symbol-value hook))))) (set hook (hook-remove function (symbol-value hook))) (set-default hook (hook-remove function (default-value hook)))))));; XEmacs addition;; #### we need a coherent scheme for indicating compatibility info,;; so that it can be programmatically retrieved.(defun add-local-hook (hook function &optional append) "Add to the local value of HOOK the function FUNCTION.This modifies only the buffer-local value for the hook (which isautomatically make buffer-local, if necessary), not its default value.FUNCTION is not added if already present.FUNCTION is added (if necessary) at the beginning of the hook listunless the optional argument APPEND is non-nil, in which caseFUNCTION is added at the end.HOOK should be a symbol, and FUNCTION may be any valid function. IfHOOK is void, it is first set to nil. If HOOK's value is a singlefunction, it is changed to a list of functions.You can remove this hook yourself using `remove-local-hook'.See also `add-hook' and `make-local-hook'." (make-local-hook hook) (add-hook hook function append t));; XEmacs addition(defun remove-local-hook (hook function) "Remove from the local value of HOOK the function FUNCTION.This modifies only the buffer-local value for the hook, not its defaultvalue. (Nothing happens if the hook is not buffer-local.)HOOK should be a symbol, and FUNCTION may be any valid function. IfFUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in thelist of hooks to run in HOOK, then nothing is done. See `add-hook'.See also `add-local-hook' and `make-local-hook'." (if (local-variable-p hook (current-buffer)) (remove-hook hook function t)))(defun add-one-shot-hook (hook function &optional append local) "Add to the value of HOOK the one-shot function FUNCTION.FUNCTION will automatically be removed from the hook the first timeafter it runs (whether to completion or to an error).FUNCTION is not added if already present.FUNCTION is added (if necessary) at the beginning of the hook listunless the optional argument APPEND is non-nil, in which caseFUNCTION is added at the end.HOOK should be a symbol, and FUNCTION may be any valid function. IfHOOK is void, it is first set to nil. If HOOK's value is a singlefunction, it is changed to a list of functions.You can remove this hook yourself using `remove-hook'.See also `add-hook', `add-local-hook', and `add-local-one-shot-hook'." (let ((sym (gensym))) (fset sym `(lambda (&rest args) (unwind-protect (apply ',function args) (remove-hook ',hook ',sym ',local)))) (put sym 'one-shot-hook-fun function) (add-hook hook sym append local)))(defun add-local-one-shot-hook (hook function &optional append) "Add to the local value of HOOK the one-shot function FUNCTION.FUNCTION will automatically be removed from the hook the first timeafter it runs (whether to completion or to an error).FUNCTION is not added if already present.FUNCTION is added (if necessary) at the beginning of the hook listunless the optional argument APPEND is non-nil, in which caseFUNCTION is added at the end.The optional fourth argument, LOCAL, if non-nil, says to modifythe hook's buffer-local value rather than its default value.This makes no difference if the hook is not buffer-local.To make a hook variable buffer-local, always use`make-local-hook', not `make-local-variable'.HOOK should be a symbol, and FUNCTION may be any valid function. IfHOOK is void, it is first set to nil. If HOOK's value is a singlefunction, it is changed to a list of functions.You can remove this hook yourself using `remove-local-hook'.See also `add-hook', `add-local-hook', and `add-local-one-shot-hook'." (make-local-hook hook) (add-one-shot-hook hook function append t))(defun add-to-list (list-var element) "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.The test for presence of ELEMENT is done with `equal'.If you want to use `add-to-list' on a variable that is not defineduntil a certain package is loaded, you should put the call to `add-to-list'into a hook function that will be run only after loading the package.`eval-after-load' provides one way to do this. In some casesother hooks, such as major mode hooks, can do the job." (or (member element (symbol-value list-var)) (set list-var (cons element (symbol-value list-var)))));; XEmacs additions;; called by Fkill_buffer()(defvar kill-buffer-hook nil "Function or functions to be called when a buffer is killed.The value of this variable may be buffer-local.The buffer about to be killed is current when this hook is run.");; called by Frecord_buffer()(defvar record-buffer-hook nil "Function or functions to be called when a buffer is recorded.The value of this variable may be buffer-local.The buffer being recorded is passed as an argument to the hook.");; in C in FSFmacs(defvar kill-emacs-hook nil "Function or functions to be called when `kill-emacs' is called,just before emacs is actually killed.");; not obsolete.;; #### These are a bad idea, because the CL RPLACA and RPLACD;; return the cons cell, not the new CAR/CDR. -hniksic;; The proper definition would be:;; (defun rplaca (conscell newcar);; (setcar conscell newcar);; conscell);; ...and analogously for RPLACD.(define-function 'rplaca 'setcar)(define-function 'rplacd 'setcdr)(defun copy-symbol (symbol &optional copy-properties) "Return a new uninterned symbol with the same name as SYMBOL.If COPY-PROPERTIES is non-nil, the new symbol will have a copy ofSYMBOL's value, function, and property lists." (let ((new (make-symbol (symbol-name symbol)))) (when copy-properties ;; This will not copy SYMBOL's chain of forwarding objects, but ;; I think that's OK. Callers should not expect such magic to ;; keep working in the copy in the first place. (and (boundp symbol) (set new (symbol-value symbol))) (and (fboundp symbol) (fset new (symbol-function symbol))) (setplist new (copy-list (symbol-plist symbol)))) new));;;; String functions.;; XEmacs(defun replace-in-string (str regexp newtext &optional literal) "Replace all matches in STR for REGEXP with NEWTEXT string, and returns the new string.Optional LITERAL non-nil means do a literal replacement.Otherwise treat \\ in NEWTEXT string as special: \\& means substitute original matched text, \\N means substitute match for \(...\) number N, \\\\ means insert one \\." (check-argument-type 'stringp str) (check-argument-type 'stringp newtext) (let ((rtn-str "") (start 0) (special) match prev-start) (while (setq match (string-match regexp str start)) (setq prev-start start start (match-end 0) rtn-str (concat rtn-str (substring str prev-start match) (cond (literal newtext) (t (mapconcat (lambda (c) (if special (progn (setq special nil) (cond ((eq c ?\\) "\\") ((eq c ?&) (substring str (match-beginning 0) (match-end 0))) ((and (>= c ?0) (<= c ?9)) (if (> c (+ ?0 (length (match-data)))) ;; Invalid match num (error "Invalid match num: %c" c) (setq c (- c ?0)) (substring str (match-beginning c) (match-end c)))) (t (char-to-string c)))) (if (eq c ?\\) (progn (setq special t) nil) (char-to-string c)))) newtext "")))))) (concat rtn-str (substring str start))))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -