Download
(or (and (= emacs-major-version 20)
(load "ls-lisp-20" t nil nil 'MUST-SUFFIX))
(require 'ls-lisp))
(require 'files+)
(defvar original-insert-directory)
(defvar ls-lisp-uid-d-fmt)
(defvar ls-lisp-uid-s-fmt)
(defvar ls-lisp-gid-d-fmt)
(defvar ls-lisp-gid-s-fmt)
(defvar ls-lisp-filesize-d-fmt)
(defvar ls-lisp-filesize-f-fmt)
(defun insert-directory (file switches &optional wildcard full-directory-p)
"Insert directory listing for FILE, formatted according to SWITCHES.
Leaves point after the inserted text.
SWITCHES may be a string of options, or a list of strings.
Optional third arg WILDCARD means treat FILE as shell wildcard.
Optional fourth arg FULL-DIRECTORY-P means file is a directory and
switches do not contain `d', so that a full listing is expected.
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 n and F partly."
(if ls-lisp-use-insert-directory-program
(funcall original-insert-directory
file switches wildcard full-directory-p)
(let ((handler (find-file-name-handler (expand-file-name file)
'insert-directory))
(orig-file file)
wildcard-regexp)
(if handler
(funcall handler 'insert-directory file switches
wildcard full-directory-p)
(if (string-match "--dired " switches)
(setq switches (replace-match "" nil nil switches)))
(setq switches (delete ?\ (delete ?- (append switches nil))))
(if (and ls-lisp-support-shell-wildcards
(string-match "[[?*]" file)
(not (file-exists-p 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 "[^~]\\'")))
(condition-case err
(ls-lisp-insert-directory
file switches (ls-lisp-time-index switches)
wildcard-regexp full-directory-p)
(invalid-regexp
(if (equal (cadr err) "Unmatched [ or [^")
(progn
(setq wildcard-regexp (if (memq ?B switches) "[^~]\\'")
file (file-relative-name orig-file))
(ls-lisp-insert-directory
file switches (ls-lisp-time-index switches)
nil full-directory-p))
(signal (car err) (cdr err)))))
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^total" nil t)
(beginning-of-line)
(let ((counted (save-match-data (count-dired-files))))
(if (zerop counted)
(insert "files 0/0 ")
(insert "files " (number-to-string counted)
"/" (number-to-string
(- (length (directory-files default-directory
nil nil t)) 2))
" ")))
(goto-char (point-min))
(re-search-forward "^files [0-9]+/[0-9]+ \\(total\\)" nil t)
(replace-match "space used" nil nil nil 1)
(let ((available (and (fboundp 'get-free-disk-space)
(get-free-disk-space ".")))
(map (make-sparse-keymap))
(inhibit-field-text-motion t))
(define-key map [mouse-2]
'dired-mouse-describe-listed-directory)
(define-key map "\r" 'dired-describe-listed-directory)
(when available (end-of-line) (insert " available " available))
(add-text-properties
(save-excursion (beginning-of-line) (+ 2 (point)))
(1- (match-beginning 1))
`(mouse-face highlight keymap ,map
help-echo "Files shown / total files in directory \
\[RET, mouse-2: more info]"))
(add-text-properties
(match-beginning 1)
(save-excursion (end-of-line) (point))
`(mouse-face highlight keymap ,map
help-echo "Kbytes used in directory, Kbytes \
available on disk [RET, mouse-2: more info]")))))))))
(cond ((= emacs-major-version 21)
(defun ls-lisp-insert-directory
(file switches time-index wildcard 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 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 (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 (or wildcard full-directory-p)
(let* ((dir (file-name-as-directory file))
(default-directory dir)
(file-alist
(directory-files-and-attributes dir nil wildcard t))
(now (current-time))
(sum 0)
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))
(setq file-alist
(ls-lisp-delete-matching "^\\." file-alist))))
(setq file-alist
(ls-lisp-handle-switches file-alist switches))
(if (memq ?C switches)
(ls-lisp-column-format file-alist)
(setq total-line (cons (point) (car-safe file-alist)))
(setq files file-alist)
(while files
(setq elt (car files)
files (cdr files)
short (car elt)
attr (cdr elt)
file-size (nth 7 attr))
(and attr
(setq sum (+ file-size
(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))))
(save-excursion
(goto-char (car total-line))
(or (cdr total-line)
(insert "(No match)\n"))
(insert (format "total %.0f\n" (fceiling (/ sum 1024.0))))))
(if (memq ?R switches)
(while file-alist
(setq elt (car file-alist)
file-alist (cdr file-alist))
(when (and (eq (cadr elt) t)
(not (string-match "\\`\\.\\.?/?\\'" (car elt))))
(setq elt (expand-file-name (car elt) dir))
(insert "\n" elt ":\n")
(ls-lisp-insert-directory
elt switches time-index wildcard full-directory-p)))))
(if (eq (aref file (1- (length file))) ?/)
(setq file (substring file 0 -1)))
(let ((fattr (file-attributes file)))
(if fattr
(insert (ls-lisp-format file fattr (nth 7 fattr)
switches time-index (current-time)))
(message "%s: doesn't exist or is inaccessible" file)
(ding) (sit-for 2))))))
((= emacs-major-version 22)
(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)
(file-alist
(directory-files-and-attributes
dir nil wildcard-regexp t 'string))
(now (current-time))
(sum 0)
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))
(setq file-alist
(ls-lisp-delete-matching "^\\." file-alist))))
(setq file-alist
(ls-lisp-handle-switches file-alist switches))
(if (memq ?C switches)
(ls-lisp-column-format file-alist)
(setq total-line (cons (point) (car-safe file-alist)))
(setq files file-alist)
(while files
(setq elt (car files)
files (cdr files)
short (car elt)
attr (cdr elt)
file-size (nth 7 attr))
(and attr
(setq sum (+ file-size
(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))))
(save-excursion
(goto-char (car total-line))
(or (cdr total-line)
(insert "(No match)\n"))
(insert (format "total %.0f\n" (fceiling (/ sum 1024.0))))))
(if (memq ?R switches)
(while file-alist
(setq elt (car file-alist)
file-alist (cdr file-alist))
(when (and (eq (cadr elt) t)
(not (string-match "\\`\\.\\.?/?\\'" (car elt))))
(setq elt (expand-file-name (car elt) dir))
(insert "\n" elt ":\n")
(ls-lisp-insert-directory
elt switches time-index wildcard-regexp
full-directory-p)))))
(if (eq (aref file (1- (length file))) ?/)
(setq file (substring file 0 -1)))
(let ((fattr (file-attributes file 'string)))
(if fattr
(insert (ls-lisp-format file fattr (nth 7 fattr)
switches time-index (current-time)))
(message "%s: doesn't exist or is inaccessible" file)
(ding) (sit-for 2)))))))
(when (> emacs-major-version 22)
(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."
(when (> (length file) 0)
(if (or wildcard-regexp full-directory-p)
(let* ((dir (if file (file-name-as-directory file) default-directory))
(default-directory dir)
(file-alist
(directory-files-and-attributes dir nil wildcard-regexp t
(if (memq ?n switches)
'integer
'string)))
(now (current-time))
(sum 0)
(max-uid-len 0)
(max-gid-len 0)
(max-file-size 0)
total-line files elt short file-size fil attr
fuid fgid uid-len gid-len)
(cond ((memq ?A switches)
(setq file-alist
(ls-lisp-delete-matching "^\\.\\.?$" file-alist)))
((not (memq ?a switches))
(setq file-alist
(ls-lisp-delete-matching "^\\." file-alist))))
(setq file-alist
(ls-lisp-handle-switches file-alist switches))
(if (memq ?C switches)
(ls-lisp-column-format file-alist)
(setq total-line (cons (point) (car-safe file-alist)))
(dolist (elt file-alist)
(setq attr (cdr elt)
fuid (nth 2 attr)
uid-len (if (stringp fuid) (string-width fuid)
(length (format "%d" fuid)))
fgid (nth 3 attr)
gid-len (if (stringp fgid) (string-width fgid)
(length (format "%d" fgid)))
file-size (nth 7 attr))
(if (> uid-len max-uid-len)
(setq max-uid-len uid-len))
(if (> gid-len max-gid-len)
(setq max-gid-len gid-len))
(if (> file-size max-file-size)
(setq max-file-size file-size)))
(setq ls-lisp-uid-d-fmt (format " %%-%dd" max-uid-len))
(setq ls-lisp-uid-s-fmt (format " %%-%ds" max-uid-len))
(setq ls-lisp-gid-d-fmt (format " %%-%dd" max-gid-len))
(setq ls-lisp-gid-s-fmt (format " %%-%ds" max-gid-len))
(setq ls-lisp-filesize-d-fmt
(format " %%%dd"
(if (memq ?s switches)
(length (format "%.0f"
(fceiling (/ max-file-size 1024.0))))
(length (format "%.0f" max-file-size)))))
(setq ls-lisp-filesize-f-fmt
(format " %%%d.0f"
(if (memq ?s switches)
(length (format "%.0f"
(fceiling (/ max-file-size 1024.0))))
(length (format "%.0f" max-file-size)))))
(setq files file-alist)
(while files
(setq elt (car files)
files (cdr files)
short (car elt)
attr (cdr elt)
file-size (nth 7 attr))
(and attr
(setq sum (+ file-size
(if (or (< sum (- 134217727 file-size))
(floatp sum)
(floatp file-size))
sum
(float sum))))
(insert
(if (> emacs-major-version 23)
(ls-lisp-format short attr file-size switches time-index)
(ls-lisp-format short attr file-size
switches time-index now)))))
(save-excursion
(goto-char (car total-line))
(or (cdr total-line)
(insert "(No match)\n"))
(insert (format "total %.0f\n" (fceiling (/ sum 1024.0))))))
(if (memq ?R switches)
(while file-alist
(setq elt (car file-alist)
file-alist (cdr file-alist))
(when (and (eq (cadr elt) t)
(not (string-match "\\`\\.\\.?/?\\'" (car elt))))
(setq elt (expand-file-name (car elt) dir))
(insert "\n" elt ":\n")
(ls-lisp-insert-directory
elt switches time-index wildcard-regexp full-directory-p)))))
(if (file-name-absolute-p file) (setq file (expand-file-name file)))
(if (eq (aref file (1- (length file))) ?/)
(setq file (substring file 0 -1)))
(let ((fattr (file-attributes file 'string)))
(if fattr
(insert
(if (> emacs-major-version 23)
(ls-lisp-format (if (memq ?F switches)
(ls-lisp-classify-file file fattr)
file)
fattr (nth 7 fattr) switches time-index)
(ls-lisp-format file fattr (nth 7 fattr) switches time-index (current-time))))
(message "%s: doesn't exist or is inaccessible" file)
(ding) (sit-for 2)))))))
(provide 'ls-lisp+)