+ :type '(choice (const nil) regexp)
+ :version "24.1")
+
+(defun shell-parse-pcomplete-arguments ()
+ "Parse whitespace separated arguments in the current region."
+ (let ((begin (save-excursion (shell-backward-command 1) (point)))
+ (end (point))
+ begins args)
+ (save-excursion
+ (goto-char begin)
+ (while (< (point) end)
+ (skip-chars-forward " \t\n")
+ (push (point) begins)
+ (let ((arg ()))
+ (while (looking-at
+ (eval-when-compile
+ (concat
+ "\\(?:[^\s\t\n\\\"']+"
+ "\\|'\\([^']*\\)'?"
+ "\\|\"\\(\\(?:[^\"\\]\\|\\\\.\\)*\\)\"?"
+ "\\|\\\\\\(\\(?:.\\|\n\\)?\\)\\)")))
+ (goto-char (match-end 0))
+ (cond
+ ((match-beginning 3) ;Backslash escape.
+ (push (if (= (match-beginning 3) (match-end 3))
+ "\\" (match-string 3))
+ arg))
+ ((match-beginning 2) ;Double quote.
+ (push (replace-regexp-in-string
+ "\\\\\\(.\\)" "\\1" (match-string 2))
+ arg))
+ ((match-beginning 1) ;Single quote.
+ (push (match-string 1) arg))
+ (t (push (match-string 0) arg))))
+ (push (mapconcat #'identity (nreverse arg) "") args)))
+ (cons (nreverse args) (nreverse begins)))))
+
+(defun shell-completion-vars ()
+ "Setup completion vars for `shell-mode' and `read-shell-command'."
+ (set (make-local-variable 'comint-completion-fignore)
+ shell-completion-fignore)
+ (set (make-local-variable 'comint-delimiter-argument-list)
+ shell-delimiter-argument-list)
+ (set (make-local-variable 'comint-file-name-chars) shell-file-name-chars)
+ (set (make-local-variable 'comint-file-name-quote-list)
+ shell-file-name-quote-list)
+ (set (make-local-variable 'comint-dynamic-complete-functions)
+ shell-dynamic-complete-functions)
+ (set (make-local-variable 'pcomplete-parse-arguments-function)
+ #'shell-parse-pcomplete-arguments)
+ (set (make-local-variable 'pcomplete-arg-quote-list)
+ (append "\\ \t\n\r\"'`$|&;(){}[]<>#" nil))
+ (set (make-local-variable 'pcomplete-termination-string)
+ (cond ((not comint-completion-addsuffix) "")
+ ((stringp comint-completion-addsuffix)
+ comint-completion-addsuffix)
+ ((not (consp comint-completion-addsuffix)) " ")
+ (t (cdr comint-completion-addsuffix))))
+ ;; Don't use pcomplete's defaulting mechanism, rely on
+ ;; shell-dynamic-complete-functions instead.
+ (set (make-local-variable 'pcomplete-default-completion-function) #'ignore)
+ (setq comint-input-autoexpand shell-input-autoexpand)
+ ;; Not needed in shell-mode because it's inherited from comint-mode, but
+ ;; placed here for read-shell-command.
+ (add-hook 'completion-at-point-functions 'comint-completion-at-point nil t))