Function: eshell-complete-parse-arguments

eshell-complete-parse-arguments is a byte-compiled function defined in em-cmpl.el.gz.

Signature

(eshell-complete-parse-arguments)

Documentation

Parse the command line arguments for pcomplete-argument.

Source Code

;; Defined in /usr/src/emacs/lisp/eshell/em-cmpl.el.gz
(defun eshell-complete-parse-arguments ()
  "Parse the command line arguments for `pcomplete-argument'."
  (when (and eshell-no-completion-during-jobs
             eshell-foreground-command)
    (eshell--pcomplete-insert-tab))
  (let ((end (point-marker))
	(begin (save-excursion (beginning-of-line) (point)))
	args posns delim incomplete-arg)
    (when (and pcomplete-allow-modifications
	       (memq this-command '(pcomplete-expand
                                    pcomplete-expand-and-complete)))
      (run-hook-with-args 'eshell-expand-input-functions begin end)
      (if (= begin end)
	  (end-of-line))
      (setq end (point-marker)))
    ;; Don't expand globs when parsing arguments; we want to pass any
    ;; globs to Pcomplete unaltered.
    (declare-function eshell-parse-glob-chars "em-glob" ())
    (let ((eshell-parse-argument-hook (remq #'eshell-parse-glob-chars
                                            eshell-parse-argument-hook)))
      (if (setq delim
                (catch 'eshell-incomplete
		  (ignore
		   (setq args (eshell-parse-arguments begin end)))))
          (cond ((member (car delim) '("{" "${" "$<"))
                 (setq begin (1+ (cadr delim))
		       args (eshell-parse-arguments begin end)))
                ((member (car delim) '("$'" "$\"" "#<"))
                 ;; Add the (incomplete) argument to our arguments, and
                 ;; note its position.
                 (setq args (append (nth 2 delim) (list (car delim)))
                       incomplete-arg t)
                 (push (- (nth 1 delim) 2) posns))
                ((member (car delim) '("(" "$("))
                 (throw 'pcompleted (elisp-completion-at-point)))
                (t
                 (eshell--pcomplete-insert-tab)))))
    (when (and (< begin end)
               (get-text-property (1- end) 'comment))
      (eshell--pcomplete-insert-tab))
    (let ((pos (1- end)))
      (while (>= pos begin)
        (when (get-text-property pos 'arg-begin)
          (push pos posns))
        (setq pos (1- pos))))
    (cl-assert (= (length args) (length posns)))
    (let ((a args) (i 0) new-start)
      (while a
        ;; If there's an unreplaced `eshell-operator' sigil, consider
        ;; the token after it the new start of our arguments.
        (when (and (consp (car a))
                   (eq (caar a) 'eshell-operator))
          (setq new-start i))
        (setq a (cdr a)
              i (1+ i)))
      (when new-start
	(setq args (nthcdr (1+ new-start) args)
	      posns (nthcdr (1+ new-start) posns))))
    (cl-assert (= (length args) (length posns)))
    (when (and args (not incomplete-arg)
               (eq (char-syntax (char-before end)) ? )
	       (not (eq (char-before (1- end)) ?\\)))
      (nconc args (list ""))
      (nconc posns (list (point))))
    ;; Evaluate and expand Eshell forms.
    (let (evaled-args evaled-posns)
      (cl-mapc
       (lambda (arg posn)
         (pcase arg
           (`(eshell-splice-args ,val)
            (dolist (subarg (eshell-complete--eval-argument-form val))
              (push subarg evaled-args)
              (push posn evaled-posns)))
           ((pred listp)
            (push (eshell-complete--eval-argument-form arg) evaled-args)
            (push posn evaled-posns))
           (_
            (push arg evaled-args)
            (push posn evaled-posns))))
       args posns)
      (setq args (nreverse evaled-args)
            posns (nreverse evaled-posns)))
    ;; Determine, whether remote file names shall be completed.  They
    ;; shouldn't for external commands, or when in a pipe.  Respect
    ;; also `eshell-cmpl-remote-file-ignore', which could be set by
    ;; the user.
    (setq-local pcomplete-remote-file-ignore
                (or eshell-cmpl-remote-file-ignore
                    eshell-in-pipeline-p ; does not work
                    (eshell-external-command-p (car args))))
    ;; Convert arguments to forms that Pcomplete can understand.
    (cons (mapcar
           (lambda (arg)
             (pcase arg
               ;; Expand ".../" etc that only Eshell understands to
               ;; the standard "../../".
               ((rx ".." (+ ".") "/")
                (propertize (eshell-expand-multiple-dots arg)
                            'pcomplete-arg-value arg))
               ((pred stringp)
                arg)
               ('nil
                (propertize "" 'pcomplete-arg-value arg))
               (_
                (propertize (eshell-stringify arg t)
                            'pcomplete-arg-value arg))))
	   args)
	  posns)))