-This version of the function comes from `ls-lisp.el'. It doesn not
-run any external programs or shells. It supports ordinary shell
-wildcards if `ls-lisp-support-shell-wildcards' variable is non-nil;
-otherwise, it interprets wildcards as regular expressions to match
-file names.
-
-Not all `ls' switches are supported. The switches that work
-are: A a c i r S s t u"
- (let ((handler (find-file-name-handler file 'insert-directory)))
- (if handler
- (funcall handler 'insert-directory file switches
- wildcard full-directory-p)
- ;; Sometimes we get ".../foo*/" as FILE. While the shell and
- ;; `ls' don't mind, we certainly do, because it makes us think
- ;; there is no wildcard, only a directory name.
- (if (and ls-lisp-support-shell-wildcards
- (string-match "[[?*]" file))
- (progn
- (or (not (eq (aref file (1- (length file))) ?/))
- (setq file (substring file 0 (1- (length file)))))
- (setq wildcard t)))
- ;; Convert SWITCHES to a list of characters.
- (setq switches (append switches nil))
- (if wildcard
- (setq wildcard
- (if ls-lisp-support-shell-wildcards
- (wildcard-to-regexp (file-name-nondirectory file))
- (file-name-nondirectory file))
- file (file-name-directory file)))
- (if (or wildcard
- full-directory-p)
- (let* ((dir (file-name-as-directory file))
- (default-directory dir);; so that file-attributes works
- (sum 0)
- elt
- short
- (file-list (directory-files dir nil wildcard))
- file-alist
- ;; do all bindings here for speed
- fil attr)
- (cond ((memq ?A switches)
- (setq file-list
- (ls-lisp-delete-matching "^\\.\\.?$" file-list)))
- ((not (memq ?a switches))
- ;; if neither -A nor -a, flush . files
- (setq file-list
- (ls-lisp-delete-matching "^\\." file-list))))
- (setq file-alist
- (mapcar
- (function
- (lambda (x)
- ;; file-attributes("~bogus") bombs
- (cons x (file-attributes (expand-file-name x)))))
- ;; inserting the call to directory-files right here
- ;; seems to stimulate an Emacs bug
- ;; ILLEGAL DATATYPE (#o37777777727) or #o67
- file-list))
- ;; ``Total'' line (filled in afterwards).
- (insert (if (car-safe file-alist)
- "total \007\n"
- ;; Shell says ``No match'' if no files match
- ;; the wildcard; let's say something similar.
- "(No match)\ntotal \007\n"))
- (setq file-alist
- (ls-lisp-handle-switches file-alist switches))
+This version of the function comes from `ls-lisp.el'.
+If the value of `ls-lisp-use-insert-directory-program' is non-nil then
+it works exactly like the version from `files.el' and runs a directory
+listing program whose name is in the variable
+`insert-directory-program'; if also WILDCARD is non-nil then it runs
+the shell specified by `shell-file-name'. If the value of
+`ls-lisp-use-insert-directory-program' is nil then it runs a Lisp
+emulation.
+
+The Lisp emulation does not run any external programs or shells. It
+supports ordinary shell wildcards if `ls-lisp-support-shell-wildcards'
+is non-nil; otherwise, it interprets wildcards as regular expressions
+to match file names. It does not support all `ls' switches -- those
+that work are: A a c i r S s t u U X g G B C R and F partly."
+ (if ls-lisp-use-insert-directory-program
+ (funcall original-insert-directory
+ file switches wildcard full-directory-p)
+ ;; We need the directory in order to find the right handler.
+ (let ((handler (find-file-name-handler (expand-file-name file)
+ 'insert-directory))
+ wildcard-regexp)
+ (if handler
+ (funcall handler 'insert-directory file switches
+ wildcard full-directory-p)
+ ;; Remove --dired switch
+ (if (string-match "--dired " switches)
+ (setq switches (replace-match "" nil nil switches)))
+ ;; Convert SWITCHES to a list of characters.
+ (setq switches (delete ?- (append switches nil)))
+ ;; Sometimes we get ".../foo*/" as FILE. While the shell and
+ ;; `ls' don't mind, we certainly do, because it makes us think
+ ;; there is no wildcard, only a directory name.
+ (if (and ls-lisp-support-shell-wildcards
+ (string-match "[[?*]" file))
+ (progn
+ (or (not (eq (aref file (1- (length file))) ?/))
+ (setq file (substring file 0 (1- (length file)))))
+ (setq wildcard t)))
+ (if wildcard
+ (setq wildcard-regexp
+ (if ls-lisp-support-shell-wildcards
+ (wildcard-to-regexp (file-name-nondirectory file))
+ (file-name-nondirectory file))
+ file (file-name-directory file))
+ (if (memq ?B switches) (setq wildcard-regexp "[^~]\\'")))
+ (ls-lisp-insert-directory
+ file switches (ls-lisp-time-index switches)
+ wildcard-regexp full-directory-p)
+ ;; Try to insert the amount of free space.
+ (save-excursion
+ (goto-char (point-min))
+ ;; First find the line to put it on.
+ (when (re-search-forward "^total" nil t)
+ (let ((available (get-free-disk-space ".")))
+ (when available
+ ;; Replace "total" with "total used", to avoid confusion.
+ (replace-match "total used in directory")
+ (end-of-line)
+ (insert " available " available)))))))))
+
+(defun ls-lisp-insert-directory
+ (file switches time-index wildcard-regexp full-directory-p)
+ "Insert directory listing for FILE, formatted according to SWITCHES.
+Leaves point after the inserted text. This is an internal function
+optionally called by the `ls-lisp.el' version of `insert-directory'.
+It is called recursively if the -R switch is used.
+SWITCHES is a *list* of characters. TIME-INDEX is the time index into
+file-attributes according to SWITCHES. WILDCARD-REGEXP is nil or an *Emacs
+regexp*. FULL-DIRECTORY-P means file is a directory and SWITCHES does
+not contain `d', so that a full listing is expected."
+ (if (or wildcard-regexp full-directory-p)
+ (let* ((dir (file-name-as-directory file))
+ (default-directory dir) ; so that file-attributes works
+ (file-alist
+ (directory-files-and-attributes dir nil wildcard-regexp t 'string))
+ (now (current-time))
+ (sum 0)
+ ;; do all bindings here for speed
+ total-line files elt short file-size fil attr)
+ (cond ((memq ?A switches)
+ (setq file-alist
+ (ls-lisp-delete-matching "^\\.\\.?$" file-alist)))
+ ((not (memq ?a switches))
+ ;; if neither -A nor -a, flush . files
+ (setq file-alist
+ (ls-lisp-delete-matching "^\\." file-alist))))
+ (setq file-alist
+ (ls-lisp-handle-switches file-alist switches))
+ (if (memq ?C switches) ; column (-C) format
+ (ls-lisp-column-format file-alist)
+ (setq total-line (cons (point) (car-safe file-alist)))
+ (setq files file-alist)
+ (while files ; long (-l) format
+ (setq elt (car files)
+ files (cdr files)
+ short (car elt)
+ attr (cdr elt)
+ file-size (nth 7 attr))
+ (and attr
+ (setq sum (+ file-size
+ ;; Even if neither SUM nor file's size
+ ;; overflow, their sum could.
+ (if (or (< sum (- 134217727 file-size))
+ (floatp sum)
+ (floatp file-size))
+ sum
+ (float sum))))
+ (insert (ls-lisp-format short attr file-size
+ switches time-index now))))
+ ;; Insert total size of all files:
+ (save-excursion
+ (goto-char (car total-line))
+ (or (cdr total-line)
+ ;; Shell says ``No match'' if no files match
+ ;; the wildcard; let's say something similar.
+ (insert "(No match)\n"))
+ (insert (format "total %.0f\n" (fceiling (/ sum 1024.0))))))
+ (if (memq ?R switches)
+ ;; List the contents of all directories recursively.
+ ;; cadr of each element of `file-alist' is t for
+ ;; directory, string (name linked to) for symbolic
+ ;; link, or nil.