1 ;;;; dired-lisp.el - emulate ls completely in Emacs Lisp. $Revision: 1.2 $
2 ;;;; Copyright (C) 1991 Sebastian Kremer <sk@thp.uni-koeln.de>
4 ;;;; READ THE WARNING BELOW BEFORE USING THIS PROGRAM!
6 ;;;; Useful if you cannot afford to fork Emacs on a real memory UNIX,
7 ;;;; under VMS, or if you don't have the ls program.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 1, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;;;; Sometimes I get an internal Emacs error:
29 ;;;; Signalling: (wrong-type-argument natnump #<EMACS BUG: ILLEGAL
30 ;;;; DATATYPE (#o37777777727) Save your buffers immediately and please
31 ;;;; report this bug>)
33 ;;;; Sometimes emacs just crashes with a fatal error.
36 ;;;; Always sorts by name (ls switches are completely ignored for now)
37 ;;;; Cannot display date of file, displays a fake date "Jan 00 00:00" instead
38 ;;;; Only numeric uid/gid
39 ;;;; Loading ange-ftp breaks it
41 ;;;; It is surprisingly fast, though!
44 ;;;; Recognize at least some ls switches: l R g F i
46 (require 'dired) ; we will redefine this function:
48 (defun dired-ls (file &optional switches wildcard full-directory-p)
49 "dired-lisp.el's version of dired-ls."
50 ; "Insert ls output of FILE, optionally formatted with SWITCHES.
51 ;Optional third arg WILDCARD means treat FILE as shell wildcard.
52 ;Optional fourth arg FULL-DIRECTORY-P means file is a directory and
53 ;switches do not contain `d'.
55 ;SWITCHES default to dired-listing-switches."
56 (or switches (setq switches dired-listing-switches))
58 (error "Cannot handle wildcards in lisp emulation of `ls'."))
60 (let* ((dir (file-name-as-directory file))
63 (insert "total \007\n") ; fill in afterwards
66 (function (lambda (short)
67 (let* ((fil (concat dir short))
68 (attr (file-attributes fil))
71 (setq sum (+ sum size))
73 ;;(file-name-nondirectory fil)
74 ;;(dired-make-relative fil dir)
75 ;;(substring fil start)
82 (search-backward "total \007")
83 (goto-char (match-end 0))
85 (insert (format "%d" sum)))
87 ;; if not full-directory-p, FILE *must not* end in /, as
88 ;; file-attributes will not recognize a symlink to a directory
89 ;; must make it a relative filename as ls does:
90 (setq file (file-name-nondirectory file))
91 (insert (dired-lisp-format file (file-attributes file) switches)))
94 (defun dired-lisp-format (file-name file-attr &optional switches)
95 (let ((file-type (nth 0 file-attr)))
96 (concat (nth 8 file-attr) ; permission bits
98 (dired-lisp-pad (nth 1 file-attr) -3) ; no. of links
99 ;; numeric uid/gid are more confusing than helpful
100 ;; Emacs should be able to make strings of them
101 " " (dired-lisp-pad (nth 2 file-attr) -6) ; uid
102 " " (dired-lisp-pad (nth 3 file-attr) -6) ; gid
104 (dired-lisp-pad (nth 7 file-attr) -8) ; size in bytes
105 ;; file-attributes's time is in a braindead format
106 ;; Emacs should have a ctime function
107 " " "Jan 00 00:00 " ; fake time
109 (if (stringp file-type) ; is a symbolic link
110 (concat " -> " file-type)
115 ;; format should really do anything printf can!!
116 (defun dired-lisp-pad (arg width &optional pad-char)
117 "Pad ARG to WIDTH, from left if WIDTH < 0.
118 Non-nil third arg optional PAD-CHAR defaults to a space."
119 (or pad-char (setq pad-char ?\040))
121 (setq arg (int-to-string arg)))
130 (concat (make-string pad pad-char) arg)
131 (concat arg (make-string pad pad-char)))