2005-05-17 05:56:11 -04:00
|
|
|
(define key-m 109)
|
|
|
|
(define key-u 117)
|
2005-05-26 07:35:17 -04:00
|
|
|
(define key-return 10)
|
|
|
|
|
2005-05-27 17:32:21 -04:00
|
|
|
(define (add-marks-to-special-file file-name fs-object)
|
|
|
|
(let ((info (fs-object-info fs-object)))
|
2005-05-26 07:35:17 -04:00
|
|
|
(cond
|
2005-05-27 05:53:06 -04:00
|
|
|
((not info)
|
2005-05-27 17:32:21 -04:00
|
|
|
(string-append " " file-name ": error during file-info!"))
|
2005-05-26 07:35:17 -04:00
|
|
|
((file-info-directory? info)
|
2005-05-27 17:32:21 -04:00
|
|
|
(string-append " " file-name "/"))
|
2005-05-26 07:35:17 -04:00
|
|
|
((file-info-executable? info)
|
2005-05-27 17:32:21 -04:00
|
|
|
(string-append "*" file-name))
|
2005-05-26 07:35:17 -04:00
|
|
|
((file-info-symlink? info)
|
2005-05-27 17:32:21 -04:00
|
|
|
(string-append "@" file-name))
|
2005-05-26 07:35:17 -04:00
|
|
|
(else
|
2005-05-27 17:32:21 -04:00
|
|
|
(string-append " " file-name)))))
|
2005-05-26 07:35:17 -04:00
|
|
|
|
2005-05-31 13:28:06 -04:00
|
|
|
(define (have-permission? mode perm-mask)
|
|
|
|
(not (zero? (bitwise-and mode perm-mask))))
|
|
|
|
|
|
|
|
(define (format-permissions mode)
|
|
|
|
(apply string-append
|
|
|
|
(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
|
|
|
|
|
|
|
|
(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)))))
|
|
|
|
|
|
|
|
(define (format-user/group fi)
|
|
|
|
(fill-up-string 17
|
|
|
|
(string-append
|
|
|
|
(cut-to-size 8 (->username (file-info:uid fi)))
|
|
|
|
":"
|
|
|
|
(cut-to-size 8 (group-info:name
|
|
|
|
(group-info (file-info:gid fi)))))))
|
|
|
|
|
2005-05-26 07:35:17 -04:00
|
|
|
;; leave one line for the heading
|
|
|
|
(define (calculate-number-of-lines result-buffer)
|
|
|
|
(- (result-buffer-num-lines result-buffer)
|
|
|
|
1))
|
|
|
|
|
2005-05-31 13:28:06 -04:00
|
|
|
(define (layout-fsobject parent-dir-len fsobject num-cols)
|
2005-05-27 17:32:21 -04:00
|
|
|
(let ((file-name (combine-path (string-drop
|
|
|
|
(fs-object-path fsobject)
|
|
|
|
parent-dir-len)
|
2005-05-31 13:28:06 -04:00
|
|
|
(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))
|
|
|
|
" "
|
|
|
|
(fill-up-string
|
|
|
|
7 (format-size (file-info:size fi)))
|
|
|
|
" "
|
|
|
|
(format-user/group fi)
|
|
|
|
" "
|
|
|
|
(format-permissions
|
|
|
|
(file-info:mode fi))))))
|
2005-05-27 17:32:21 -04:00
|
|
|
|
2005-05-31 13:28:06 -04:00
|
|
|
(define (make-file-select-list fsobjects parent-dir num-lines num-cols)
|
2005-05-27 17:32:21 -04:00
|
|
|
(let ((parent-dir-len (string-length parent-dir)))
|
|
|
|
(make-select-list
|
|
|
|
(cons (make-unmarked-element 'parent-dir #f " ..")
|
|
|
|
(map (lambda (fs-object)
|
|
|
|
(make-unmarked-element
|
2005-05-31 13:28:06 -04:00
|
|
|
fs-object #t (layout-fsobject parent-dir-len
|
|
|
|
fs-object num-cols)))
|
2005-05-27 17:32:21 -04:00
|
|
|
fsobjects))
|
|
|
|
num-lines)))
|
2005-05-26 07:35:17 -04:00
|
|
|
|
|
|
|
;;; 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 ")
|
|
|
|
|
2005-05-31 04:01:06 -04:00
|
|
|
(define (make-header-line wdir width)
|
|
|
|
(string-append
|
|
|
|
header-line-path
|
|
|
|
(if wdir
|
|
|
|
(abbrev-path
|
|
|
|
wdir (- width (string-length header-line-path)))
|
|
|
|
"<unknown>")))
|
|
|
|
|
|
|
|
(define (paint-browser select-list wdir win buffer have-focus?)
|
|
|
|
(wattron win (A-BOLD))
|
|
|
|
(mvwaddstr win 0 0
|
|
|
|
(make-header-line
|
|
|
|
wdir (result-buffer-num-cols buffer)))
|
|
|
|
(wattrset win (A-NORMAL))
|
|
|
|
(paint-selection-list-at select-list 1 2 win
|
|
|
|
buffer have-focus?))
|
|
|
|
|
2005-05-27 17:32:21 -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))))))))
|
2005-05-26 07:35:17 -04:00
|
|
|
|
2005-05-31 10:54:48 -04:00
|
|
|
(define (make-browser-for-dir dir buffer)
|
|
|
|
(with-cwd dir
|
|
|
|
(make-fsobjects-viewer (directory-files)
|
|
|
|
buffer
|
|
|
|
(cwd))))
|
2005-05-26 07:35:17 -04:00
|
|
|
|
2005-05-31 10:54:48 -04:00
|
|
|
(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-05-31 13:28:06 -04:00
|
|
|
(make-file-select-list
|
|
|
|
fs-objects working-dir
|
2005-09-27 04:59:55 -04:00
|
|
|
;; we need one line for the header
|
|
|
|
(- (result-buffer-num-lines buffer) 1)
|
2005-05-31 13:28:06 -04:00
|
|
|
(result-buffer-num-cols buffer))))
|
2005-05-31 10:54:48 -04:00
|
|
|
|
2005-06-01 04:48:37 -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)))))
|
|
|
|
|
|
|
|
(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)))
|
|
|
|
(else
|
|
|
|
(set! select-list
|
|
|
|
(select-list-handle-key-press select-list key))
|
|
|
|
self)))
|
|
|
|
|
2005-06-01 05:15:43 -04:00
|
|
|
(define (prepare-selection-for-scheme-mode file-names)
|
2005-09-27 04:46:34 -04:00
|
|
|
(string-append "'" (write-to-string file-names)))
|
2005-06-01 05:15:43 -04:00
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
;; FIXME: quote file names containing space etc
|
2005-06-01 05:15:43 -04:00
|
|
|
(define (prepare-selection-for-command-mode file-names)
|
2005-07-06 04:57:44 -04:00
|
|
|
(string-join file-names))
|
2005-06-01 05:15:43 -04:00
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
(define (get-selection-as-text self for-scheme-mode? focus-object-table)
|
2005-09-27 04:08:15 -04:00
|
|
|
(let* ((marked (select-list-get-marked select-list))
|
2005-06-01 05:15:43 -04:00
|
|
|
(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)))
|
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
(define (get-selection-as-ref self focus-object-table)
|
2005-09-27 04:08:15 -04:00
|
|
|
(let ((marked (select-list-get-marked select-list))
|
2005-06-01 06:04:21 -04:00
|
|
|
(make-reference (lambda (obj)
|
|
|
|
(make-focus-object-reference
|
|
|
|
focus-object-table obj))))
|
|
|
|
(if (null? marked)
|
2005-09-27 04:46:34 -04:00
|
|
|
(write-to-string
|
2005-06-01 06:04:21 -04:00
|
|
|
(make-reference (select-list-selected-entry select-list)))
|
|
|
|
(string-append
|
|
|
|
"(list "
|
2005-09-27 04:46:34 -04:00
|
|
|
(string-join (map write-to-string (map make-reference marked)))
|
2005-06-01 06:04:21 -04:00
|
|
|
")"))))
|
|
|
|
|
2005-06-01 05:15:43 -04:00
|
|
|
(lambda (message)
|
|
|
|
(cond
|
|
|
|
((eq? message 'paint)
|
|
|
|
(lambda (self . args)
|
|
|
|
(apply paint-browser
|
|
|
|
(append (list select-list working-dir) args))))
|
|
|
|
|
|
|
|
((eq? message 'key-press)
|
|
|
|
(lambda (self key control-x-pressed?)
|
|
|
|
(handle-key-press self key)))
|
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
((eq? message 'get-selection-as-text)
|
|
|
|
get-selection-as-text)
|
2005-06-01 06:04:21 -04:00
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
((eq? message 'get-selection-as-ref)
|
|
|
|
get-selection-as-ref)
|
2005-05-31 04:01:06 -04:00
|
|
|
|
2005-06-01 05:15:43 -04:00
|
|
|
(else
|
|
|
|
(error "fsobjects-viewer unknown message" message)))))))
|
2004-10-14 07:58:20 -04:00
|
|
|
|
2005-05-22 11:05:25 -04:00
|
|
|
(define (list-of-fs-objects? thing)
|
|
|
|
(and (proper-list? thing)
|
|
|
|
(every fs-object? thing)))
|
2004-10-14 07:58:20 -04:00
|
|
|
|
2005-05-23 08:47:41 -04:00
|
|
|
(register-plugin!
|
2005-05-31 04:01:06 -04:00
|
|
|
(make-view-plugin make-fsobjects-viewer
|
2005-07-06 04:57:44 -04:00
|
|
|
(lambda (thing)
|
|
|
|
(or (fs-object? thing)
|
|
|
|
(list-of-fs-objects? thing)))))
|
|
|
|
|