Function: eshell-apply-indices

eshell-apply-indices is a byte-compiled function defined in esh-var.el.gz.

Signature

(eshell-apply-indices VALUE INDICES)

Documentation

Apply to VALUE all of the given INDICES, returning the sub-result.

The format of INDICES is:

  ((INT-OR-NAME-OR-OTHER INT-OR-NAME INT-OR-NAME ...)
   ...)

Each member of INDICES represents a level of nesting. If the first member of a sublist is not an integer or name, and the value it's reference is a string, that will be used as the regexp with which is to divide the string into sub-parts. The default is whitespace. Otherwise, each INT-OR-NAME refers to an element of the list value. Integers imply a direct index, and names, an associate lookup using assoc.

For example, to retrieve the second element of a user's record in
'/etc/passwd', the variable reference would look like:

  ${grep johnw /etc/passwd}[: 2]

Source Code

;; Defined in /usr/src/emacs/lisp/eshell/esh-var.el.gz
(defun eshell-apply-indices (value indices)
  "Apply to VALUE all of the given INDICES, returning the sub-result.
The format of INDICES is:

  ((INT-OR-NAME-OR-OTHER INT-OR-NAME INT-OR-NAME ...)
   ...)

Each member of INDICES represents a level of nesting.  If the first
member of a sublist is not an integer or name, and the value it's
reference is a string, that will be used as the regexp with which is
to divide the string into sub-parts.  The default is whitespace.
Otherwise, each INT-OR-NAME refers to an element of the list value.
Integers imply a direct index, and names, an associate lookup using
`assoc'.

For example, to retrieve the second element of a user's record in
'/etc/passwd', the variable reference would look like:

  ${grep johnw /etc/passwd}[: 2]"
  (while indices
    (let ((refs (car indices)))
      (when (stringp value)
	(let (separator)
	  (if (not (or (not (stringp (caar indices)))
		       (string-match
			(concat "^" eshell-variable-name-regexp "$")
			(caar indices))))
	      (setq separator (caar indices)
		    refs (cdr refs)))
	  (setq value
		(mapcar #'eshell-convert
			(split-string value separator)))))
      (cond
       ((< (length refs) 0)
	(error "Invalid array variable index: %s"
	       (eshell-stringify refs)))
       ((= (length refs) 1)
	(setq value (eshell-index-value value (car refs))))
       (t
	(let ((new-value (list t)))
	  (while refs
	    (nconc new-value
		   (list (eshell-index-value value
					     (car refs))))
	    (setq refs (cdr refs)))
	  (setq value (cdr new-value))))))
    (setq indices (cdr indices)))
  value)