Mercurial > emacs
view lisp/eshell/esh-arg.el @ 89943:4c90ffeb71c5
Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-15
Merge from emacs--cvs-trunk--0
Patches applied:
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-218
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-220
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-221
Restore deleted tagline in etc/TUTORIAL.ru
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-222
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-228
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-229
Remove TeX output files from the archive
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-230
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-247
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-248
src/lisp.h (CYCLE_CHECK): Macro moved from xfaces.c
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-249
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-256
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-258
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-263
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-264
Update from CVS: lispref/display.texi: emacs -> Emacs.
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-265
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-274
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-275
Update from CVS: man/makefile.w32-in: Revert last change
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-276
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-295
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-296
Allow restarting an existing debugger session that's exited
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-297
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-299
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-300
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-327
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-328
Update from CVS: src/.gdbinit (xsymbol): Fix last change.
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-329
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-344
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-345
Tweak source regexps so that building in place won't cause problems
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-346
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-351
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-352
Update from CVS: lisp/flymake.el: New file.
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-353
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-361
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-362
Support " [...]" style defaults in minibuffer-electric-default-mode
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-363
(read-number): Use canonical format for default in prompt.
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-364
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-367
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-368
Improve display-supports-face-attributes-p on non-ttys
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-369
Rewrite face-differs-from-default-p
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-370
Move `display-supports-face-attributes-p' entirely into C code
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-371
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-372
Simplify face-differs-from-default-p; don't consider :stipple.
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-373
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-374
(tty_supports_face_attributes_p): Ensure attributes differ from default
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-375
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-376
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-377
(Fdisplay_supports_face_attributes_p): Work around bootstrapping problem
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-378
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-380
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-381
Face merging cleanups
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-382
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-384
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-385
src/xfaces.c (push_named_merge_point): Return 0 if a cycle is detected
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-386
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-395
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-396
Tweak arch tagging to make build/install-in-place less annoying
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-397
Work around vc-arch problems when building eshell
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-398
Tweak permissions
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-399
Tweak directory permissions
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-400
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-401
More build-in-place tweaking of arch tagging
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-402
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-403
Yet more build-in-place tweaking of arch tagging
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-404
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-409
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-410
Make sure image types are initialized for lookup too
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-411
- miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-416
Update from CVS
author | Miles Bader <miles@gnu.org> |
---|---|
date | Mon, 28 Jun 2004 07:56:49 +0000 |
parents | 68c22ea6027c |
children | f9a65d7ebd29 |
line wrap: on
line source
;;; esh-arg.el --- argument processing ;; Copyright (C) 1999, 2000 Free Software Foundation ;; Author: John Wiegley <johnw@gnu.org> ;; This file is part of GNU Emacs. ;; GNU Emacs 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. ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. (provide 'esh-arg) (eval-when-compile (require 'esh-maint)) (defgroup eshell-arg nil "Argument parsing involves transforming the arguments passed on the command line into equivalent Lisp forms that, when evaluated, will yield the values intended." :tag "Argument parsing" :group 'eshell) ;;; Commentary: ;; Parsing of arguments can be extended by adding functions to the ;; hook `eshell-parse-argument-hook'. For a good example of this, see ;; `eshell-parse-drive-letter', defined in eshell-dirs.el. (defcustom eshell-parse-argument-hook (list ;; a term such as #<buffer NAME>, or #<process NAME> is a buffer ;; or process reference 'eshell-parse-special-reference ;; numbers convert to numbers if they stand alone (function (lambda () (when (and (not eshell-current-argument) (not eshell-current-quoted) (looking-at eshell-number-regexp) (eshell-arg-delimiter (match-end 0))) (goto-char (match-end 0)) (let ((str (match-string 0))) (if (> (length str) 0) (add-text-properties 0 1 '(number t) str)) str)))) ;; parse any non-special characters, based on the current context (function (lambda () (unless eshell-inside-quote-regexp (setq eshell-inside-quote-regexp (format "[^%s]+" (apply 'string eshell-special-chars-inside-quoting)))) (unless eshell-outside-quote-regexp (setq eshell-outside-quote-regexp (format "[^%s]+" (apply 'string eshell-special-chars-outside-quoting)))) (when (looking-at (if eshell-current-quoted eshell-inside-quote-regexp eshell-outside-quote-regexp)) (goto-char (match-end 0)) (let ((str (match-string 0))) (if str (set-text-properties 0 (length str) nil str)) str)))) ;; whitespace or a comment is an argument delimiter (function (lambda () (let (comment-p) (when (or (looking-at "[ \t]+") (and (not eshell-current-argument) (looking-at "#\\([^<'].*\\|$\\)") (setq comment-p t))) (if comment-p (add-text-properties (match-beginning 0) (match-end 0) '(comment t))) (goto-char (match-end 0)) (eshell-finish-arg))))) ;; backslash before a special character means escape it 'eshell-parse-backslash ;; text beginning with ' is a literally quoted 'eshell-parse-literal-quote ;; text beginning with " is interpolably quoted 'eshell-parse-double-quote ;; argument delimiter 'eshell-parse-delimiter) "*Define how to process Eshell command line arguments. When each function on this hook is called, point will be at the current position within the argument list. The function should either return nil, meaning that it did no argument parsing, or it should return the result of the parse as a sexp. It is also responsible for moving the point forward to reflect the amount of input text that was parsed. If no function handles the current character at point, it will be treated as a literal character." :type 'hook :group 'eshell-arg) ;;; Code: ;;; User Variables: (defcustom eshell-arg-load-hook '(eshell-arg-initialize) "*A hook that gets run when `eshell-arg' is loaded." :type 'hook :group 'eshell-arg) (defcustom eshell-delimiter-argument-list '(?\; ?& ?\| ?\> ? ?\t ?\n) "List of characters to recognize as argument separators." :type '(repeat character) :group 'eshell-arg) (defcustom eshell-special-chars-inside-quoting '(?\\ ?\") "*Characters which are still special inside double quotes." :type '(repeat character) :group 'eshell-arg) (defcustom eshell-special-chars-outside-quoting (append eshell-delimiter-argument-list '(?# ?! ?\\ ?\" ?\')) "*Characters that require escaping outside of double quotes. Without escaping them, they will introduce a change in the argument." :type '(repeat character) :group 'eshell-arg) ;;; Internal Variables: (defvar eshell-current-argument nil) (defvar eshell-current-modifiers nil) (defvar eshell-arg-listified nil) (defvar eshell-nested-argument nil) (defvar eshell-current-quoted nil) (defvar eshell-inside-quote-regexp nil) (defvar eshell-outside-quote-regexp nil) ;;; Functions: (defun eshell-arg-initialize () "Initialize the argument parsing code." (define-key eshell-command-map [(meta ?b)] 'eshell-insert-buffer-name) (set (make-local-variable 'eshell-inside-quote-regexp) nil) (set (make-local-variable 'eshell-outside-quote-regexp) nil)) (defun eshell-insert-buffer-name (buffer-name) "Insert BUFFER-NAME into the current buffer at point." (interactive "BName of buffer: ") (insert-and-inherit "#<buffer " buffer-name ">")) (defsubst eshell-escape-arg (string) "Return STRING with the `escaped' property on it." (if (stringp string) (add-text-properties 0 (length string) '(escaped t) string)) string) (defun eshell-resolve-current-argument () "If there are pending modifications to be made, make them now." (when eshell-current-argument (when eshell-arg-listified (let ((parts eshell-current-argument)) (while parts (unless (stringp (car parts)) (setcar parts (list 'eshell-to-flat-string (car parts)))) (setq parts (cdr parts))) (setq eshell-current-argument (list 'eshell-convert (append (list 'concat) eshell-current-argument)))) (setq eshell-arg-listified nil)) (while eshell-current-modifiers (setq eshell-current-argument (list (car eshell-current-modifiers) eshell-current-argument) eshell-current-modifiers (cdr eshell-current-modifiers)))) (setq eshell-current-modifiers nil)) (defun eshell-finish-arg (&optional argument) "Finish the current argument being processed." (if argument (setq eshell-current-argument argument)) (throw 'eshell-arg-done t)) (defsubst eshell-arg-delimiter (&optional pos) "Return non-nil if POS is an argument delimiter. If POS is nil, the location of point is checked." (let ((pos (or pos (point)))) (or (= pos (point-max)) (memq (char-after pos) eshell-delimiter-argument-list)))) ;; Argument parsing (defun eshell-parse-arguments (beg end) "Parse all of the arguments at point from BEG to END. Returns the list of arguments in their raw form. Point is left at the end of the arguments." (save-excursion (save-restriction (goto-char beg) (narrow-to-region beg end) (let ((inhibit-point-motion-hooks t) (args (list t)) after-change-functions delim) (remove-text-properties (point-min) (point-max) '(arg-begin nil arg-end nil)) (if (setq delim (catch 'eshell-incomplete (while (not (eobp)) (let* ((here (point)) (arg (eshell-parse-argument))) (if (= (point) here) (error "Failed to parse argument '%s'" (buffer-substring here (point-max)))) (and arg (nconc args (list arg))))))) (if (listp delim) (throw 'eshell-incomplete delim) (throw 'eshell-incomplete (list delim (point) (cdr args))))) (cdr args))))) (defun eshell-parse-argument () "Get the next argument. Leave point after it." (let* ((outer (null eshell-nested-argument)) (arg-begin (and outer (point))) (eshell-nested-argument t) eshell-current-argument eshell-current-modifiers eshell-arg-listified) (catch 'eshell-arg-done (while (not (eobp)) (let ((result (or (run-hook-with-args-until-success 'eshell-parse-argument-hook) (prog1 (char-to-string (char-after)) (forward-char))))) (if (not eshell-current-argument) (setq eshell-current-argument result) (unless eshell-arg-listified (setq eshell-current-argument (list eshell-current-argument) eshell-arg-listified t)) (nconc eshell-current-argument (list result)))))) (when (and outer eshell-current-argument) (add-text-properties arg-begin (1+ arg-begin) '(arg-begin t rear-nonsticky (arg-begin arg-end))) (add-text-properties (1- (point)) (point) '(arg-end t rear-nonsticky (arg-end arg-begin)))) (eshell-resolve-current-argument) eshell-current-argument)) (defsubst eshell-operator (&rest args) "A stub function that generates an error if a floating operator is found." (error "Unhandled operator in input text")) (defsubst eshell-looking-at-backslash-return (pos) "Test whether a backslash-return sequence occurs at POS." (and (eq (char-after pos) ?\\) (or (= (1+ pos) (point-max)) (and (eq (char-after (1+ pos)) ?\n) (= (+ pos 2) (point-max)))))) (defun eshell-quote-backslash (string &optional index) "Intelligently backslash the character occuring in STRING at INDEX. If the character is itself a backslash, it needs no escaping." (let ((char (aref string index))) (if (eq char ?\\) (char-to-string char) (if (memq char eshell-special-chars-outside-quoting) (string ?\\ char))))) (defun eshell-parse-backslash () "Parse a single backslash (\) character, which might mean escape. It only means escape if the character immediately following is a special character that is not itself a backslash." (when (eq (char-after) ?\\) (if (eshell-looking-at-backslash-return (point)) (throw 'eshell-incomplete ?\\) (if (and (not (eq (char-after (1+ (point))) ?\\)) (if eshell-current-quoted (memq (char-after (1+ (point))) eshell-special-chars-inside-quoting) (memq (char-after (1+ (point))) eshell-special-chars-outside-quoting))) (progn (forward-char 2) (list 'eshell-escape-arg (char-to-string (char-before)))) ;; allow \\<RET> to mean a literal "\" character followed by a ;; normal return, rather than a backslash followed by a line ;; continuator (i.e., "\\ + \n" rather than "\ + \\n"). This ;; is necessary because backslashes in Eshell are not special ;; unless they either precede something special, or precede a ;; backslash that precedes something special. (Mainly this is ;; done to make using backslash on Windows systems more ;; natural-feeling). (if (eshell-looking-at-backslash-return (1+ (point))) (forward-char)) (forward-char) "\\")))) (defun eshell-parse-literal-quote () "Parse a literally quoted string. Nothing has special meaning!" (if (eq (char-after) ?\') (let ((end (eshell-find-delimiter ?\' ?\'))) (if (not end) (throw 'eshell-incomplete ?\') (let ((string (buffer-substring-no-properties (1+ (point)) end))) (goto-char (1+ end)) (while (string-match "''" string) (setq string (replace-match "'" t t string))) (list 'eshell-escape-arg string)))))) (defun eshell-parse-double-quote () "Parse a double quoted string, which allows for variable interpolation." (when (eq (char-after) ?\") (let* ((end (eshell-find-delimiter ?\" ?\" nil nil t)) (eshell-current-quoted t)) (if (not end) (throw 'eshell-incomplete ?\") (prog1 (save-restriction (forward-char) (narrow-to-region (point) end) (list 'eshell-escape-arg (eshell-parse-argument))) (goto-char (1+ end))))))) (defun eshell-parse-special-reference () "Parse a special syntax reference, of the form '#<type arg>'." (if (and (not eshell-current-argument) (not eshell-current-quoted) (looking-at "#<\\(buffer\\|process\\)\\s-")) (let ((here (point))) (goto-char (match-end 0)) (let* ((buffer-p (string= (match-string 1) "buffer")) (end (eshell-find-delimiter ?\< ?\>))) (if (not end) (throw 'eshell-incomplete ?\<) (if (eshell-arg-delimiter (1+ end)) (prog1 (list (if buffer-p 'get-buffer-create 'get-process) (buffer-substring-no-properties (point) end)) (goto-char (1+ end))) (ignore (goto-char here)))))))) (defun eshell-parse-delimiter () "Parse an argument delimiter, which is essentially a command operator." ;; this `eshell-operator' keyword gets parsed out by ;; `eshell-separate-commands'. Right now the only possibility for ;; error is an incorrect output redirection specifier. (when (looking-at "[&|;\n]\\s-*") (let ((end (match-end 0))) (if eshell-current-argument (eshell-finish-arg) (eshell-finish-arg (prog1 (list 'eshell-operator (cond ((eq (char-after end) ?\&) (setq end (1+ end)) "&&") ((eq (char-after end) ?\|) (setq end (1+ end)) "||") ((eq (char-after) ?\n) ";") (t (char-to-string (char-after))))) (goto-char end))))))) ;;; arch-tag: 7f593a2b-8fc1-4def-8f84-8f51ed0198d6 ;;; esh-arg.el ends here