📄 subr.el
字号:
(defun split-string (string &optional pattern) "Return a list of substrings of STRING which are separated by PATTERN.If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"." (or pattern (setq pattern "[ \f\t\n\r\v]+")) (let (parts (start 0) (len (length string))) (if (string-match pattern string) (setq parts (cons (substring string 0 (match-beginning 0)) parts) start (match-end 0))) (while (and (< start len) (string-match pattern string (if (> start (match-beginning 0)) start (1+ start)))) (setq parts (cons (substring string start (match-beginning 0)) parts) start (match-end 0))) (nreverse (cons (substring string start) parts))));; #### #### #### AAaargh! Must be in C, because it is used insanely;; early in the bootstrap process.;(defun split-path (path); "Explode a search path into a list of strings.;The path components are separated with the characters specified;with `path-separator'."; (while (or (not stringp path-separator); (/= (length path-separator) 1)); (setq path-separator (signal 'error (list "\;`path-separator' should be set to a single-character string"; path-separator)))); (split-string-by-char path (aref separator 0)))#|(defmacro with-output-to-string (&rest forms) "Collect output to `standard-output' while evaluating FORMS and returnit as a string." ;; by "William G. Dubuque" <wgd@zurich.ai.mit.edu> w/ mods from Stig `(with-current-buffer (get-buffer-create (generate-new-buffer-name " *string-output*")) (setq buffer-read-only nil) (buffer-disable-undo (current-buffer)) (erase-buffer) (let ((standard-output (current-buffer))) ,@forms) (prog1 (buffer-string) (erase-buffer))))(defmacro with-current-buffer (buffer &rest body) "Temporarily make BUFFER the current buffer and execute the forms in BODY.The value returned is the value of the last form in BODY.See also `with-temp-buffer'." `(save-current-buffer (set-buffer ,buffer) ,@body))(defmacro with-temp-file (file &rest forms) "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.The value of the last form in FORMS is returned, like `progn'.See also `with-temp-buffer'." (let ((temp-file (make-symbol "temp-file")) (temp-buffer (make-symbol "temp-buffer"))) `(let ((,temp-file ,file) (,temp-buffer (get-buffer-create (generate-new-buffer-name " *temp file*")))) (unwind-protect (prog1 (with-current-buffer ,temp-buffer ,@forms) (with-current-buffer ,temp-buffer (widen) (write-region (point-min) (point-max) ,temp-file nil 0))) (and (buffer-name ,temp-buffer) (kill-buffer ,temp-buffer))))))(defmacro with-temp-buffer (&rest forms) "Create a temporary buffer, and evaluate FORMS there like `progn'.See also `with-temp-file' and `with-output-to-string'." (let ((temp-buffer (make-symbol "temp-buffer"))) `(let ((,temp-buffer (get-buffer-create (generate-new-buffer-name " *temp*")))) (unwind-protect (with-current-buffer ,temp-buffer ,@forms) (and (buffer-name ,temp-buffer) (kill-buffer ,temp-buffer))))));; Moved from mule-coding.el.(defmacro with-string-as-buffer-contents (str &rest body) "With the contents of the current buffer being STR, run BODY.Returns the new contents of the buffer, as modified by BODY.The original current buffer is restored afterwards." `(with-temp-buffer (insert ,str) ,@body (buffer-string)))|#(defun insert-face (string face) "Insert STRING and highlight with FACE. Return the extent created." (let ((p (point)) ext) (insert string) (setq ext (make-extent p (point))) (set-extent-face ext face) ext));; not obsolete.(define-function 'string= 'string-equal)(define-function 'string< 'string-lessp)(define-function 'int-to-string 'number-to-string)(define-function 'string-to-int 'string-to-number);; These two names are a bit awkward, as they conflict with the normal;; foo-to-bar naming scheme, but CLtL2 has them, so they stay.(define-function 'char-int 'char-to-int)(define-function 'int-char 'int-to-char);; alist/plist functions(defun plist-to-alist (plist) "Convert property list PLIST into the equivalent association-list form.The alist is returned. This converts from\(a 1 b 2 c 3)into\((a . 1) (b . 2) (c . 3))The original plist is not modified. See also `destructive-plist-to-alist'." (let (alist) (while plist (setq alist (cons (cons (car plist) (cadr plist)) alist)) (setq plist (cddr plist))) (nreverse alist)))(defun destructive-plist-to-alist (plist) "Convert property list PLIST into the equivalent association-list form.The alist is returned. This converts from\(a 1 b 2 c 3)into\((a . 1) (b . 2) (c . 3))The original plist is destroyed in the process of constructing the alist.See also `plist-to-alist'." (let ((head plist) next) (while plist ;; remember the next plist pair. (setq next (cddr plist)) ;; make the cons holding the property value into the alist element. (setcdr (cdr plist) (cadr plist)) (setcar (cdr plist) (car plist)) ;; reattach into alist form. (setcar plist (cdr plist)) (setcdr plist next) (setq plist next)) head))(defun alist-to-plist (alist) "Convert association list ALIST into the equivalent property-list form.The plist is returned. This converts from\((a . 1) (b . 2) (c . 3))into\(a 1 b 2 c 3)The original alist is not modified. See also `destructive-alist-to-plist'." (let (plist) (while alist (let ((el (car alist))) (setq plist (cons (cdr el) (cons (car el) plist)))) (setq alist (cdr alist))) (nreverse plist)));; getf, remf in cl*.el.#|(defmacro putf (plist prop val) "Add property PROP to plist PLIST with value VAL.Analogous to (setq PLIST (plist-put PLIST PROP VAL))." `(setq ,plist (plist-put ,plist ,prop ,val)))(defmacro laxputf (lax-plist prop val) "Add property PROP to lax plist LAX-PLIST with value VAL.Analogous to (setq LAX-PLIST (lax-plist-put LAX-PLIST PROP VAL))." `(setq ,lax-plist (lax-plist-put ,lax-plist ,prop ,val)))(defmacro laxremf (lax-plist prop) "Remove property PROP from lax plist LAX-PLIST.Analogous to (setq LAX-PLIST (lax-plist-remprop LAX-PLIST PROP))." `(setq ,lax-plist (lax-plist-remprop ,lax-plist ,prop)))|#;;; Error functions(defun error (&rest args) "Signal an error, making error message by passing all args to `format'.This error is not continuable: you cannot continue execution after theerror using the debugger `r' command. See also `cerror'." (while t (apply 'cerror args)))(defun cerror (&rest args) "Like `error' but signals a continuable error." (signal 'error (list (apply 'format args))))#|(defmacro check-argument-type (predicate argument) "Check that ARGUMENT satisfies PREDICATE.If not, signal a continuable `wrong-type-argument' error until thereturned value satisfies PREDICATE, and assign the returned valueto ARGUMENT." `(if (not (,(eval predicate) ,argument)) (setq ,argument (wrong-type-argument ,predicate ,argument))))|#(defun signal-error (error-symbol data) "Signal a non-continuable error. Args are ERROR-SYMBOL, and associated DATA.An error symbol is a symbol defined using `define-error'.DATA should be a list. Its elements are printed as part of the error message.If the signal is handled, DATA is made available to the handler.See also `signal', and the functions to handle errors: `condition-case'and `call-with-condition-handler'." (while t (signal error-symbol data)))(defun define-error (error-sym doc-string &optional inherits-from) "Define a new error, denoted by ERROR-SYM.DOC-STRING is an informative message explaining the error, and will beprinted out when an unhandled error occurs.ERROR-SYM is a sub-error of INHERITS-FROM (which defaults to `error').\[`define-error' internally works by putting on ERROR-SYM an `error-message'property whose value is DOC-STRING, and an `error-conditions' propertythat is a list of ERROR-SYM followed by each of its super-errors, upto and including `error'. You will sometimes see code that sets this updirectly rather than calling `define-error', but you should *not* do thisyourself.]" (check-argument-type 'symbolp error-sym) (check-argument-type 'stringp doc-string) (put error-sym 'error-message doc-string) (or inherits-from (setq inherits-from 'error)) (let ((conds (get inherits-from 'error-conditions))) (or conds (signal-error 'error (list "Not an error symbol" error-sym))) (put error-sym 'error-conditions (cons error-sym conds))));;;; Miscellanea.;; This is now in C.;(defun buffer-substring-no-properties (beg end); "Return the text from BEG to END, without text properties, as a string."; (let ((string (buffer-substring beg end))); (set-text-properties 0 (length string) nil string); string))(defun get-buffer-window-list (&optional buffer minibuf frame) "Return windows currently displaying BUFFER, or nil if none.BUFFER defaults to the current buffer.See `walk-windows' for the meaning of MINIBUF and FRAME." (cond ((null buffer) (setq buffer (current-buffer))) ((not (bufferp buffer)) (setq buffer (get-buffer buffer)))) (let (windows) (walk-windows (lambda (window) (if (eq (window-buffer window) buffer) (push window windows))) minibuf frame) windows))(defun ignore (&rest ignore) "Do nothing and return nil.This function accepts any number of arguments, but ignores them." (interactive) nil)(define-function 'eval-in-buffer 'with-current-buffer)(make-obsolete 'eval-in-buffer 'with-current-buffer);;; The real defn is in abbrev.el but some early callers;;; (eg lisp-mode-abbrev-table) want this before abbrev.el is loaded...(if (not (fboundp 'define-abbrev-table)) (progn (setq abbrev-table-name-list '()) (fset 'define-abbrev-table (function (lambda (name defs) ;; These are fixed-up when abbrev.el loads. (setq abbrev-table-name-list (cons (cons name defs) abbrev-table-name-list)))))));;; `functionp' has been moved into C.;;(defun functionp (object);; "Non-nil if OBJECT can be called as a function.";; (or (and (symbolp object) (fboundp object));; (subrp object);; (compiled-function-p object);; (eq (car-safe object) 'lambda)))(defun function-interactive (function) "Return the interactive specification of FUNCTION.FUNCTION can be any funcallable object.The specification will be returned as the list of the symbol `interactive' and the specs.If FUNCTION is not interactive, nil will be returned." (setq function (indirect-function function)) (cond ((compiled-function-p function) (compiled-function-interactive function)) ((subrp function) (subr-interactive function)) ((eq (car-safe function) 'lambda) (let ((spec (if (stringp (nth 2 function)) (nth 3 function) (nth 2 function)))) (and (eq (car-safe spec) 'interactive) spec))) (t (error "Non-funcallable object: %s" function))));; This function used to be an alias to `buffer-substring', except;; that FSF Emacs 20.4 added a BUFFER argument in an incompatible way.;; The new FSF's semantics makes more sense, but we try to support;; both for backward compatibility.(defun buffer-string (&optional buffer old-end old-buffer) "Return the contents of the current buffer as a string.If narrowing is in effect, this function returns only the visible partof the buffer.If BUFFER is specified, the contents of that buffer are returned.The arguments OLD-END and OLD-BUFFER are supported for backwardcompatibility with pre-21.2 XEmacsen times when arguments to thisfunction were (buffer-string &optional START END BUFFER)." (cond ((or (stringp buffer) (bufferp buffer)) ;; Most definitely the new way. (buffer-substring nil nil buffer)) ((or (stringp old-buffer) (bufferp old-buffer) (natnump buffer) (natnump old-end)) ;; Definitely the old way. (buffer-substring buffer old-end old-buffer)) (t ;; Probably the old way. (buffer-substring buffer old-end old-buffer))));; This was not present before. I think Jamie had some objections;; to this, so I'm leaving this undefined for now. --ben;;; The objection is this: there is more than one way to load the same file.;;; "foo", "foo.elc", "foo.el", and "/some/path/foo.elc" are all different;;; ways to load the exact same code. `eval-after-load' is too stupid to;;; deal with this sort of thing. If this sort of feature is desired, then;;; it should work off of a hook on `provide'. Features are unique and;;; the arguments to (load) are not. --Stig;; We provide this for FSFmacs compatibility, at least until we devise;; something better.;;;; Specifying things to do after certain files are loaded.(defun eval-after-load (file form) "Arrange that, if FILE is ever loaded, FORM will be run at that time.This makes or adds to an entry on `after-load-alist'.If FILE is already loaded, evaluate FORM right now.It does nothing if FORM is already on the list for FILE.FILE should be the name of a library, with no directory name." ;; Make sure there is an element for FILE. (or (assoc file after-load-alist) (setq after-load-alist (cons (list file) after-load-alist))) ;; Add FORM to the element if it isn't there. (let ((elt (assoc file after-load-alist))) (or (member form (cdr elt)) (progn (nconc elt (list form)) ;; If the file has been loaded already, run FORM right away. (and (assoc file load-history) (eval form))))) form)(make-compatible 'eval-after-load "")(defun eval-next-after-load (file) "Read the following input sexp, and run it whenever FILE is loaded.This makes or adds to an entry on `after-load-alist'.FILE should be the name of a library, with no directory name." (eval-after-load file (read)))(make-compatible 'eval-next-after-load ""); alternate names (not obsolete)(if (not (fboundp 'mod)) (define-function 'mod '%))(define-function 'move-marker 'set-marker)(define-function 'beep 'ding) ; preserve lingual purity(define-function 'indent-to-column 'indent-to)(define-function 'backward-delete-char 'delete-backward-char)(define-function 'search-forward-regexp (symbol-function 're-search-forward))(define-function 'search-backward-regexp (symbol-function 're-search-backward))(define-function 'remove-directory 'delete-directory)(define-function 'set-match-data 'store-match-data)(define-function 'send-string-to-terminal 'external-debugging-output);;; subr.el ends here
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -