Function: eshell-glob-regexp

eshell-glob-regexp is a byte-compiled function defined in em-glob.el.gz.

Signature

(eshell-glob-regexp PATTERN)

Documentation

Convert glob-pattern PATTERN to a regular expression.

The basic syntax is:

  glob regexp meaning
  ---- ------ -------
  ? . matches any single character
  * .* matches any group of characters (or none)
  # * matches zero or more occurrences of preceding
  ## + matches one or more occurrences of preceding
  (x) \(x\) makes x a regular expression group
  | \| boolean OR within an expression group
  [a-b] [a-b] matches a character or range
  [^a] [^a] excludes a character or range

If any characters in PATTERN have the text property eshell-escaped set to true, then these characters will match themselves in the resulting regular expression.

Source Code

;; Defined in /usr/src/emacs/lisp/eshell/em-glob.el.gz
(defun eshell-glob-regexp (pattern)
  "Convert glob-pattern PATTERN to a regular expression.
The basic syntax is:

  glob  regexp   meaning
  ----  ------   -------
  ?      .       matches any single character
  *      .*      matches any group of characters (or none)
  #      *       matches zero or more occurrences of preceding
  ##     +       matches one or more occurrences of preceding
  (x)    \\(x\\)   makes `x' a regular expression group
  |      \\|      boolean OR within an expression group
  [a-b]  [a-b]   matches a character or range
  [^a]   [^a]    excludes a character or range

If any characters in PATTERN have the text property `eshell-escaped'
set to true, then these characters will match themselves in the
resulting regular expression."
  (let ((matched-in-pattern 0)          ; How much of PATTERN handled
	regexp)
    (while (string-match
	    (or eshell-glob-chars-regexp
                (setq-local eshell-glob-chars-regexp
		     (format "[%s]+" (apply 'string eshell-glob-chars-list))))
	    pattern matched-in-pattern)
      (let* ((op-begin (match-beginning 0))
	     (op-char (aref pattern op-begin)))
	(setq regexp
	      (concat regexp
		      (regexp-quote
		       (substring pattern matched-in-pattern op-begin))))
	(if (get-text-property op-begin 'escaped pattern)
	    (setq regexp (concat regexp
				 (regexp-quote (char-to-string op-char)))
		  matched-in-pattern (1+ op-begin))
	  (let ((xlat (assq op-char eshell-glob-translate-alist)))
	    (if (not xlat)
		(error "Unrecognized globbing character `%c'" op-char)
	      (if (stringp (cdr xlat))
		  (setq regexp (concat regexp (cdr xlat))
			matched-in-pattern (1+ op-begin))
		(let ((result (funcall (cdr xlat) pattern op-begin)))
		  (setq regexp (concat regexp (car result))
			matched-in-pattern (cdr result)))))))))
    (concat "\\`"
	    regexp
	    (regexp-quote (substring pattern matched-in-pattern))
	    "\\'")))