comparison lisp/ls-lisp.el @ 16098:b463288de0a8

(insert-directory): Use same value of `now' for all files. (ls-lisp-format): New argument NOW. Arguments are no longer optional. (ls-lisp-format-time): New argument NOW. Use same method as `ls' to decide whether to show time-of-day or year.
author Richard M. Stallman <rms@gnu.org>
date Tue, 03 Sep 1996 23:25:46 +0000
parents aaa459c274b9
children 727cf56647a4
comparison
equal deleted inserted replaced
16097:d4fd600475dc 16098:b463288de0a8
105 (sum 0) 105 (sum 0)
106 elt 106 elt
107 short 107 short
108 (file-list (directory-files dir nil wildcard)) 108 (file-list (directory-files dir nil wildcard))
109 file-alist 109 file-alist
110 (now (current-time))
110 ;; do all bindings here for speed 111 ;; do all bindings here for speed
111 fil attr) 112 fil attr)
112 (cond ((memq ?A switches) 113 (cond ((memq ?A switches)
113 (setq file-list 114 (setq file-list
114 (ls-lisp-delete-matching "^\\.\\.?$" file-list))) 115 (ls-lisp-delete-matching "^\\.\\.?$" file-list)))
139 file-alist (cdr file-alist) 140 file-alist (cdr file-alist)
140 short (car elt) 141 short (car elt)
141 attr (cdr elt)) 142 attr (cdr elt))
142 (and attr 143 (and attr
143 (setq sum (+ sum (nth 7 attr))) 144 (setq sum (+ sum (nth 7 attr)))
144 (insert (ls-lisp-format short attr switches)))) 145 (insert (ls-lisp-format short attr switches now))))
145 ;; Fill in total size of all files: 146 ;; Fill in total size of all files:
146 (save-excursion 147 (save-excursion
147 (search-backward "total \007") 148 (search-backward "total \007")
148 (goto-char (match-end 0)) 149 (goto-char (match-end 0))
149 (delete-char -1) 150 (delete-char -1)
150 (insert (format "%d" (if (zerop sum) 0 (1+ (/ sum 1024))))))) 151 (insert (format "%d" (if (zerop sum) 0 (1+ (/ sum 1024)))))))
151 ;; if not full-directory-p, FILE *must not* end in /, as 152 ;; if not full-directory-p, FILE *must not* end in /, as
152 ;; file-attributes will not recognize a symlink to a directory 153 ;; file-attributes will not recognize a symlink to a directory
153 ;; must make it a relative filename as ls does: 154 ;; must make it a relative filename as ls does:
154 (setq file (file-name-nondirectory file)) 155 (setq file (file-name-nondirectory file))
155 (insert (ls-lisp-format file (file-attributes file) switches)))))) 156 (insert (ls-lisp-format file (file-attributes file) switches
157 (current-time)))))))
156 158
157 (defun ls-lisp-delete-matching (regexp list) 159 (defun ls-lisp-delete-matching (regexp list)
158 ;; Delete all elements matching REGEXP from LIST, return new list. 160 ;; Delete all elements matching REGEXP from LIST, return new list.
159 ;; Should perhaps use setcdr for efficiency. 161 ;; Should perhaps use setcdr for efficiency.
160 (let (result) 162 (let (result)
202 (or (< hi0 hi1) 204 (or (< hi0 hi1)
203 (and (= hi0 hi1) 205 (and (= hi0 hi1)
204 (< lo0 lo1))))) 206 (< lo0 lo1)))))
205 207
206 208
207 (defun ls-lisp-format (file-name file-attr &optional switches) 209 (defun ls-lisp-format (file-name file-attr switches now)
208 (let ((file-type (nth 0 file-attr))) 210 (let ((file-type (nth 0 file-attr)))
209 (concat (if (memq ?i switches) ; inode number 211 (concat (if (memq ?i switches) ; inode number
210 (format "%6d " (nth 10 file-attr))) 212 (format "%6d " (nth 10 file-attr)))
211 ;; nil is treated like "" in concat 213 ;; nil is treated like "" in concat
212 (if (memq ?s switches) ; size in K 214 (if (memq ?s switches) ; size in K
224 (if (eq system-type 'ms-dos) 226 (if (eq system-type 'ms-dos)
225 "root" ; everything is root on MSDOS. 227 "root" ; everything is root on MSDOS.
226 (int-to-string (nth 3 file-attr))) ; gid 228 (int-to-string (nth 3 file-attr))) ; gid
227 (nth 7 file-attr) ; size in bytes 229 (nth 7 file-attr) ; size in bytes
228 ) 230 )
229 (ls-lisp-format-time file-attr switches) 231 (ls-lisp-format-time file-attr switches now)
230 " " 232 " "
231 file-name 233 file-name
232 (if (stringp file-type) ; is a symbolic link 234 (if (stringp file-type) ; is a symbolic link
233 (concat " -> " file-type) 235 (concat " -> " file-type)
234 "") 236 "")
241 ((memq ?c switches) 6) ; last mode change 243 ((memq ?c switches) 6) ; last mode change
242 ((memq ?u switches) 4) ; last access 244 ((memq ?u switches) 4) ; last access
243 ;; default is last modtime 245 ;; default is last modtime
244 (t 5))) 246 (t 5)))
245 247
246 (defun ls-lisp-format-time (file-attr switches) 248 (defun ls-lisp-format-time (file-attr switches now)
247 ;; Format time string for file with attributes FILE-ATTR according 249 ;; Format time string for file with attributes FILE-ATTR according
248 ;; to SWITCHES (a list of ls option letters of which c and u are recognized). 250 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
249 ;; file-attributes's time is in a braindead format 251 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
250 ;; Emacs 19 can format it using a new optional argument to 252 ;; depending on distance between file date and NOW.
251 ;; current-time-string, for Emacs 18 we just return the faked fixed 253 (let* ((time (nth (ls-lisp-time-index switches) file-attr))
252 ;; date "Jan 00 00:00 ". 254 (diff16 (- (car time) (car now)))
253 (condition-case error-data 255 (diff (+ (ash diff16 16) (- (car (cdr time)) (car (cdr now)))))
254 (let* ((time (current-time-string 256 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
255 (nth (ls-lisp-time-index switches) file-attr))) 257 (future-cutoff (* 60 60))) ; 1 hour
256 (date (substring time 4 11)) ; "Apr 30 " 258 (format-time-string
257 (clock (substring time 11 16)) ; "11:27" 259 (if (and
258 (year (substring time 19 24)) ; " 1992" 260 (<= past-cutoff diff) (<= diff future-cutoff)
259 (same-year (equal year (substring (current-time-string) 19 24)))) 261 ;; Sanity check in case `diff' computation overflowed.
260 (concat date ; has trailing SPC 262 (<= (1- (ash past-cutoff -16)) diff16)
261 (if same-year 263 (<= diff16 (1+ (ash future-cutoff -16))))
262 ;; this is not exactly the same test used by ls 264 "%b %e %H:%M"
263 ;; ls tests if the file is older than 6 months 265 "%b %e %Y")
264 ;; but we can't do time differences easily 266 time)))
265 clock
266 year)))
267 (error
268 "Jan 00 00:00")))
269 267
270 (provide 'ls-lisp) 268 (provide 'ls-lisp)
271 269
272 ;;; ls-lisp.el ends here 270 ;;; ls-lisp.el ends here