2005-09-27 12:31:28 -04:00
|
|
|
(define-option 'ls 'sort-up-key (char->ascii #\s))
|
|
|
|
(define-option 'ls 'sort-down-key (char->ascii #\S))
|
|
|
|
|
2005-09-27 12:24:06 -04:00
|
|
|
(define key-m 109)
|
|
|
|
(define key-u 117)
|
|
|
|
(define key-return 10)
|
|
|
|
|
|
|
|
(define (add-marks-to-special-file file-name fs-object)
|
|
|
|
(let ((info (fs-object-info fs-object)))
|
|
|
|
(cond
|
|
|
|
((not info)
|
|
|
|
(string-append " " file-name ": error during file-info!"))
|
|
|
|
((file-info-directory? info)
|
|
|
|
(string-append " " file-name "/"))
|
|
|
|
((file-info-executable? info)
|
|
|
|
(string-append "*" file-name))
|
|
|
|
((file-info-symlink? info)
|
|
|
|
(string-append "@" file-name))
|
|
|
|
(else
|
|
|
|
(string-append " " file-name)))))
|
|
|
|
|
|
|
|
(define (have-permission? mode perm-mask)
|
|
|
|
(not (zero? (bitwise-and mode perm-mask))))
|
|
|
|
|
|
|
|
(define (format-permissions mode)
|
|
|
|
(apply string-append
|
2005-09-27 12:31:28 -04:00
|
|
|
(cons
|
|
|
|
(cond
|
|
|
|
((have-permission? mode #o1000) "t") ; sticky
|
|
|
|
((have-permission? mode #o2000) "s") ; setgit
|
|
|
|
((have-permission? mode #o4000) "s") ; setuid
|
|
|
|
(else "-"))
|
|
|
|
(map (lambda (mask.symbol)
|
|
|
|
(if (have-permission? mode (car mask.symbol))
|
|
|
|
(cdr mask.symbol)
|
|
|
|
"-"))
|
|
|
|
'((#o0400 . "r");; owner read
|
|
|
|
(#o0200 . "w");; owner write
|
|
|
|
(#o0100 . "x");; owner exec
|
|
|
|
(#o0040 . "r");; group read
|
|
|
|
(#o0020 . "w");; group write
|
|
|
|
(#o0010 . "x");; group exec
|
|
|
|
(#o0004 . "r");; others read
|
|
|
|
(#o0002 . "w");; others write
|
|
|
|
(#o0001 . "x"))))));; others exec
|
2005-09-27 12:24:06 -04:00
|
|
|
|
|
|
|
(define (digits-left-of-comma-as-string float)
|
|
|
|
(string-drop-right
|
|
|
|
(number->string (truncate float)) 1))
|
|
|
|
|
|
|
|
(define (format-size/unit float unit)
|
|
|
|
(string-append (digits-left-of-comma-as-string float) " " unit))
|
|
|
|
|
|
|
|
(define (format-size bytes)
|
|
|
|
(let* ((kbyte 1024.0)
|
|
|
|
(mbyte (* 1024 kbyte))
|
|
|
|
(gbyte (* 1024 mbyte)))
|
|
|
|
(cond
|
|
|
|
((>= bytes gbyte)
|
|
|
|
(format-size/unit (/ bytes gbyte) "GB"))
|
|
|
|
((>= bytes mbyte)
|
|
|
|
(format-size/unit (/ bytes mbyte) "MB"))
|
|
|
|
((>= bytes kbyte)
|
|
|
|
(format-size/unit (/ bytes kbyte) "KB"))
|
|
|
|
(else
|
|
|
|
(number->string bytes)))))
|
|
|
|
|
2006-04-05 03:38:50 -04:00
|
|
|
(define (->username/save uid/name)
|
|
|
|
(with-fatal-error-handler
|
|
|
|
(lambda (condition more)
|
|
|
|
(if (number? uid/name)
|
|
|
|
(number->string uid/name)
|
|
|
|
uid/name))
|
|
|
|
(->username uid/name)))
|
|
|
|
|
|
|
|
(define (->groupname/save gid/name)
|
|
|
|
(with-fatal-error-handler
|
|
|
|
(lambda (condition more)
|
|
|
|
(if (number? gid/name)
|
|
|
|
(number->string gid/name)
|
|
|
|
gid/name))
|
|
|
|
(->groupname gid/name)))
|
|
|
|
|
2005-09-27 12:24:06 -04:00
|
|
|
(define (format-user/group fi)
|
|
|
|
(fill-up-string 17
|
|
|
|
(string-append
|
2006-04-05 03:38:50 -04:00
|
|
|
(cut-to-size 8 (->username/save (file-info:uid fi)))
|
2005-09-27 12:24:06 -04:00
|
|
|
":"
|
2006-04-05 03:38:50 -04:00
|
|
|
(cut-to-size 8 (->groupname/save (file-info:gid fi))))))
|
2005-09-27 12:24:06 -04:00
|
|
|
|
2006-04-06 10:46:06 -04:00
|
|
|
(define today (date))
|
|
|
|
|
|
|
|
(define (format-time t)
|
|
|
|
(let ((d (date t)))
|
|
|
|
(if (= (date:year today)
|
|
|
|
(date:year d))
|
|
|
|
(format-date/wo-year d)
|
|
|
|
(format-date/w-year d))))
|
|
|
|
|
|
|
|
(define (format-date/w-year d)
|
|
|
|
(format-date "~b ~d ~Y" d))
|
|
|
|
|
|
|
|
(define (format-date/wo-year d)
|
|
|
|
(format-date "~b ~d ~H:~M" d))
|
|
|
|
|
2005-09-27 12:24:06 -04:00
|
|
|
;; leave one line for the heading
|
|
|
|
(define (calculate-number-of-lines result-buffer)
|
|
|
|
(- (result-buffer-num-lines result-buffer)
|
|
|
|
1))
|
|
|
|
|
2005-09-27 12:31:28 -04:00
|
|
|
(define (make-file-select-line)
|
|
|
|
(make-select-line
|
|
|
|
(list
|
|
|
|
(make-unmarked-text-element 'file-name #f
|
|
|
|
(left-align-string 31 "File name"))
|
|
|
|
(make-unmarked-text-element 'size #f
|
|
|
|
(right-align-string 8 "Size "))
|
|
|
|
(make-unmarked-text-element 'user/group #f
|
|
|
|
(left-align-string 18 "User:Group "))
|
2006-04-06 10:46:06 -04:00
|
|
|
(make-unmarked-text-element 'mode #f (left-align-string 10 "Mode "))
|
|
|
|
(make-unmarked-text-element 'mtime #f
|
|
|
|
(left-align-string 13 " Mod time")))))
|
2005-09-27 12:31:28 -04:00
|
|
|
|
|
|
|
|
2005-09-27 12:24:06 -04:00
|
|
|
(define (layout-fsobject parent-dir-len fsobject num-cols)
|
|
|
|
(let ((file-name (combine-path (string-drop
|
|
|
|
(fs-object-path fsobject)
|
|
|
|
parent-dir-len)
|
|
|
|
(fs-object-name fsobject)))
|
|
|
|
(fi (fs-object-info fsobject)))
|
|
|
|
(cut-to-size num-cols
|
|
|
|
(string-append
|
|
|
|
(fill-up-string
|
|
|
|
30 (add-marks-to-special-file file-name fsobject))
|
|
|
|
" "
|
2005-09-27 12:31:28 -04:00
|
|
|
(right-align-string 7 (format-size (file-info:size fi)))
|
2005-09-27 12:24:06 -04:00
|
|
|
" "
|
|
|
|
(format-user/group fi)
|
|
|
|
" "
|
|
|
|
(format-permissions
|
2006-04-06 10:46:06 -04:00
|
|
|
(file-info:mode fi))
|
|
|
|
" "
|
|
|
|
(format-time
|
|
|
|
(file-info:mtime fi))))))
|
2005-09-27 12:24:06 -04:00
|
|
|
|
|
|
|
(define (make-file-select-list fsobjects parent-dir num-lines num-cols)
|
|
|
|
(let ((parent-dir-len (string-length parent-dir)))
|
|
|
|
(make-select-list
|
2005-09-27 12:29:34 -04:00
|
|
|
(cons (make-unmarked-text-element 'parent-dir #f " ..")
|
2005-09-27 12:24:06 -04:00
|
|
|
(map (lambda (fs-object)
|
2005-09-27 12:29:34 -04:00
|
|
|
(make-unmarked-text-element
|
2005-09-27 12:24:06 -04:00
|
|
|
fs-object #t (layout-fsobject parent-dir-len
|
|
|
|
fs-object num-cols)))
|
|
|
|
fsobjects))
|
|
|
|
num-lines)))
|
|
|
|
|
|
|
|
;;; lacks some coolness
|
|
|
|
(define (abbrev-path path length)
|
|
|
|
(if (< (string-length path) length)
|
|
|
|
path
|
|
|
|
(string-copy path
|
|
|
|
(- (string-length path) length))))
|
|
|
|
|
|
|
|
(define header-line-path
|
|
|
|
"Paths relative to ")
|
|
|
|
|
|
|
|
(define (make-header-line wdir width)
|
|
|
|
(string-append
|
|
|
|
header-line-path
|
|
|
|
(if wdir
|
|
|
|
(abbrev-path
|
|
|
|
wdir (- width (string-length header-line-path)))
|
|
|
|
"<unknown>")))
|
|
|
|
|
2005-09-27 12:31:28 -04:00
|
|
|
(define (paint-browser wdir select-line select-list win buffer have-focus?)
|
2005-09-27 12:24:06 -04:00
|
|
|
(wattron win (A-BOLD))
|
|
|
|
(mvwaddstr win 0 0
|
|
|
|
(make-header-line
|
|
|
|
wdir (result-buffer-num-cols buffer)))
|
|
|
|
(wattrset win (A-NORMAL))
|
2005-09-27 12:31:28 -04:00
|
|
|
(paint-select-line-at select-line 1 1 win buffer)
|
2005-09-27 12:24:06 -04:00
|
|
|
(paint-selection-list-at select-list 1 2 win
|
2005-09-27 12:31:54 -04:00
|
|
|
(result-buffer-num-cols buffer) have-focus?))
|
2005-09-27 12:24:06 -04:00
|
|
|
|
|
|
|
(define (find-common-parent paths)
|
|
|
|
(if (null? paths)
|
|
|
|
""
|
|
|
|
(let lp ((paths (cdr paths))
|
|
|
|
(common (car paths))
|
|
|
|
(common-len (string-length (car paths))))
|
|
|
|
(if (null? paths)
|
|
|
|
common
|
|
|
|
(let ((prefix-len (string-prefix-length common (car paths))))
|
|
|
|
(cond
|
|
|
|
((= 0 prefix-len) (error "no prefix??" common (car paths)))
|
|
|
|
((= 1 prefix-len) "/") ; search ends here
|
|
|
|
((= prefix-len common-len) ; short cut
|
|
|
|
(lp (cdr paths)
|
|
|
|
common
|
|
|
|
common-len))
|
|
|
|
(else
|
|
|
|
(lp (cdr paths)
|
|
|
|
(substring common
|
|
|
|
0
|
|
|
|
prefix-len)
|
|
|
|
prefix-len))))))))
|
|
|
|
|
|
|
|
(define (make-browser-for-dir dir buffer)
|
|
|
|
(with-cwd dir
|
|
|
|
(make-fsobjects-viewer (directory-files)
|
|
|
|
buffer
|
|
|
|
(cwd))))
|
|
|
|
|
|
|
|
(define (make-fsobjects-viewer fs-objects buffer . maybe-parent)
|
|
|
|
(let-optionals maybe-parent
|
|
|
|
((working-dir (find-common-parent
|
|
|
|
(map fs-object-path fs-objects))))
|
|
|
|
(let ((fs-objects fs-objects)
|
|
|
|
(buffer buffer)
|
|
|
|
(select-list
|
2005-09-27 12:31:28 -04:00
|
|
|
(make-file-select-list
|
2005-09-27 12:24:06 -04:00
|
|
|
fs-objects working-dir
|
|
|
|
;; we need one line for the header
|
|
|
|
(- (result-buffer-num-lines buffer) 1)
|
2005-09-27 12:31:28 -04:00
|
|
|
(result-buffer-num-cols buffer)))
|
|
|
|
(select-line (make-file-select-line)))
|
2005-09-27 12:24:06 -04:00
|
|
|
|
|
|
|
(define (handle-return-key self selected-entry num-lines)
|
|
|
|
(cond
|
|
|
|
((eq? selected-entry 'parent-dir)
|
|
|
|
(let* ((maybe-parent (file-name-directory working-dir))
|
|
|
|
(parent (if (string=? maybe-parent "") "/" maybe-parent)))
|
|
|
|
(make-browser-for-dir parent buffer)))
|
|
|
|
(else
|
|
|
|
(let ((fi (fs-object-info selected-entry)))
|
|
|
|
(if (and fi (file-info-directory? fi))
|
|
|
|
(with-errno-handler
|
|
|
|
((errno packet)
|
|
|
|
(else
|
|
|
|
(display packet)
|
|
|
|
(newline)
|
|
|
|
self))
|
|
|
|
(make-browser-for-dir (fs-object-complete-path selected-entry)
|
|
|
|
buffer))
|
|
|
|
self)))))
|
|
|
|
|
2005-09-27 12:31:28 -04:00
|
|
|
(define (set-fs-objects! new-fs-objects)
|
|
|
|
(set! fs-objects new-fs-objects)
|
|
|
|
(set! select-list
|
|
|
|
(make-file-select-list
|
|
|
|
fs-objects working-dir
|
|
|
|
;; we need one line for the header
|
|
|
|
(- (result-buffer-num-lines buffer) 1)
|
|
|
|
(result-buffer-num-cols buffer))))
|
|
|
|
|
2005-09-27 12:24:06 -04:00
|
|
|
(define (handle-key-press self key)
|
|
|
|
(cond
|
|
|
|
((= key key-return)
|
|
|
|
(handle-return-key
|
|
|
|
self (select-list-selected-entry select-list)
|
|
|
|
(calculate-number-of-lines buffer)))
|
2005-09-27 12:31:28 -04:00
|
|
|
((or (= key (config 'ls 'sort-up-key))
|
|
|
|
(= key (config 'ls 'sort-down-key)))
|
|
|
|
(let ((column (select-line-selected-entry select-line)))
|
|
|
|
(receive (compare-up compare-down select)
|
|
|
|
(case column
|
|
|
|
((file-name)
|
|
|
|
;; TODO: use path stripped by parent
|
|
|
|
(values string<? string>? fs-object-complete-path))
|
|
|
|
((size)
|
|
|
|
(values <
|
|
|
|
>
|
|
|
|
(lambda (fso)
|
|
|
|
(file-info:size (fs-object-info fso)))))
|
|
|
|
((user/group)
|
|
|
|
(values string<?
|
|
|
|
string>?
|
|
|
|
(lambda (fso)
|
|
|
|
(format-user/group (fs-object-info fso)))))
|
|
|
|
((mode)
|
|
|
|
(values <
|
|
|
|
>
|
|
|
|
(lambda (fso)
|
|
|
|
(file-info:mode (fs-object-info fso)))))
|
2006-04-06 10:46:06 -04:00
|
|
|
((mtime)
|
|
|
|
(values <
|
|
|
|
>
|
|
|
|
(lambda (fso)
|
|
|
|
(file-info:mtime (fs-object-info fso)))))
|
2005-09-27 12:31:28 -04:00
|
|
|
(else
|
|
|
|
(error "unknown column" column)))
|
|
|
|
(let ((compare (if (= key (config 'ls 'sort-up-key))
|
|
|
|
compare-up
|
|
|
|
compare-down)))
|
|
|
|
(set-fs-objects!
|
|
|
|
(list-sort
|
|
|
|
(lambda (p1 p2)
|
|
|
|
(compare (select p1) (select p2)))
|
|
|
|
fs-objects))
|
|
|
|
self))))
|
|
|
|
((select-list-key? key)
|
2005-09-27 12:24:06 -04:00
|
|
|
(set! select-list
|
|
|
|
(select-list-handle-key-press select-list key))
|
2005-09-27 12:31:28 -04:00
|
|
|
self)
|
|
|
|
((select-line-key? key)
|
|
|
|
(select-line-handle-key-press! select-line key)
|
|
|
|
self)
|
|
|
|
(else self)))
|
2005-09-27 12:24:06 -04:00
|
|
|
|
|
|
|
(define (prepare-selection-for-scheme-mode file-names)
|
|
|
|
(string-append "'" (write-to-string file-names)))
|
|
|
|
|
|
|
|
;; FIXME: quote file names containing space etc
|
|
|
|
(define (prepare-selection-for-command-mode file-names)
|
|
|
|
(string-join file-names))
|
|
|
|
|
|
|
|
(define (get-selection-as-text self for-scheme-mode? focus-object-table)
|
|
|
|
(let* ((marked (select-list-get-marked select-list))
|
|
|
|
(file-names
|
|
|
|
(map fs-object-complete-path
|
|
|
|
(if (null? marked)
|
|
|
|
(list (select-list-selected-entry select-list))
|
|
|
|
marked))))
|
|
|
|
((if for-scheme-mode?
|
|
|
|
prepare-selection-for-scheme-mode
|
|
|
|
prepare-selection-for-command-mode)
|
|
|
|
file-names)))
|
|
|
|
|
|
|
|
(define (get-selection-as-ref self focus-object-table)
|
|
|
|
(let ((marked (select-list-get-marked select-list))
|
|
|
|
(make-reference (lambda (obj)
|
|
|
|
(make-focus-object-reference
|
|
|
|
focus-object-table obj))))
|
|
|
|
(if (null? marked)
|
|
|
|
(write-to-string
|
|
|
|
(make-reference (select-list-selected-entry select-list)))
|
|
|
|
(string-append
|
|
|
|
"(list "
|
|
|
|
(string-join (map write-to-string (map make-reference marked)))
|
|
|
|
")"))))
|
|
|
|
|
|
|
|
(lambda (message)
|
|
|
|
(cond
|
|
|
|
((eq? message 'paint)
|
2005-09-27 12:31:28 -04:00
|
|
|
(lambda (self win buffer have-focus?)
|
|
|
|
(paint-browser working-dir select-line select-list
|
|
|
|
win buffer have-focus?)))
|
2005-09-27 12:24:06 -04:00
|
|
|
|
|
|
|
((eq? message 'key-press)
|
|
|
|
(lambda (self key control-x-pressed?)
|
|
|
|
(handle-key-press self key)))
|
|
|
|
|
|
|
|
((eq? message 'get-selection-as-text)
|
|
|
|
get-selection-as-text)
|
|
|
|
|
|
|
|
((eq? message 'get-selection-as-ref)
|
|
|
|
get-selection-as-ref)
|
|
|
|
|
|
|
|
(else
|
|
|
|
(error "fsobjects-viewer unknown message" message)))))))
|
|
|
|
|
|
|
|
(define (list-of-fs-objects? thing)
|
|
|
|
(and (proper-list? thing)
|
|
|
|
(every fs-object? thing)))
|
|
|
|
|
|
|
|
(register-plugin!
|
|
|
|
(make-view-plugin make-fsobjects-viewer
|
|
|
|
(lambda (thing)
|
2005-09-27 12:31:35 -04:00
|
|
|
(list-of-fs-objects? thing))))
|
2005-09-27 12:24:06 -04:00
|
|
|
|