2005-05-23 10:52:03 -04:00
|
|
|
(define (standard-command-plugin-completer command args)
|
|
|
|
#f)
|
|
|
|
|
2005-08-10 14:03:59 -04:00
|
|
|
;; helper functions for globbing
|
|
|
|
|
2005-05-27 13:01:14 -04:00
|
|
|
(define (contains-glob-enumerator? arg)
|
|
|
|
(if-match
|
|
|
|
(regexp-search
|
2005-05-28 05:43:10 -04:00
|
|
|
(rx (: (submatch (* any)) ("{[") (* any) (submatch (* any)) ("]}")))
|
2005-05-27 13:01:14 -04:00
|
|
|
arg)
|
|
|
|
(whole-arg submatch-before submatch-after)
|
|
|
|
(not (or (string-suffix? "\\" submatch-before)
|
|
|
|
(string-suffix? "\\" submatch-after)))
|
|
|
|
#f))
|
|
|
|
|
|
|
|
(define (contains-glob-wildcard? arg)
|
|
|
|
(if-match
|
|
|
|
(regexp-search (rx (: (submatch (* any)) ("*?"))) arg)
|
|
|
|
(whole-arg submatch-before)
|
|
|
|
(not (string-suffix? "\\" submatch-before))
|
|
|
|
#f))
|
|
|
|
|
|
|
|
(define (contains-glob-expression? arg)
|
|
|
|
(or (contains-glob-wildcard? arg)
|
|
|
|
(contains-glob-enumerator? arg)))
|
|
|
|
|
|
|
|
(define (glob-argument arg)
|
|
|
|
(let ((files (glob arg)))
|
|
|
|
(if (null? files)
|
|
|
|
(error "no files match this glob expression" arg (cwd))
|
|
|
|
files)))
|
|
|
|
|
2005-08-10 14:03:59 -04:00
|
|
|
;; expand command list:
|
|
|
|
;; - substiute environment vars in strings with their values
|
|
|
|
;; - globbing
|
|
|
|
|
|
|
|
(define (env-var-name str)
|
|
|
|
(cond
|
|
|
|
((regexp-search
|
|
|
|
(rx (: #\$ (? #\{) (submatch (+ (- ascii #\}))) (? #\}))) str)
|
|
|
|
=> (lambda (matches)
|
|
|
|
(match:substring matches 1)))
|
|
|
|
(else #f)))
|
|
|
|
|
|
|
|
(define (lookup-env-var var)
|
|
|
|
(cond
|
|
|
|
((assoc var (env->alist))
|
|
|
|
=> cdr)
|
|
|
|
(else
|
|
|
|
(error "Undefined environment variable" var))))
|
|
|
|
|
|
|
|
(define (substitute-env-vars str)
|
|
|
|
(cond
|
|
|
|
((env-var-name str)
|
|
|
|
=> lookup-env-var)
|
|
|
|
(else str)))
|
|
|
|
|
2005-08-10 14:29:04 -04:00
|
|
|
(define (expand-string/s-expr v)
|
|
|
|
(if (string? v)
|
|
|
|
(substitute-env-vars v)
|
|
|
|
v))
|
2005-08-10 14:03:59 -04:00
|
|
|
|
|
|
|
(define (expand-redirection redirection)
|
|
|
|
(make-redirection
|
|
|
|
(redirection-op redirection)
|
2005-08-10 14:29:04 -04:00
|
|
|
(expand-string/s-expr (redirection-dest redirection))))
|
2005-08-10 14:03:59 -04:00
|
|
|
|
|
|
|
(define (expand-command command)
|
2005-08-10 14:29:04 -04:00
|
|
|
(let ((expanded (map expand-string/s-expr (command-args command))))
|
2005-08-10 14:03:59 -04:00
|
|
|
(make-command
|
2005-08-10 14:29:04 -04:00
|
|
|
(expand-string/s-expr (command-executable command))
|
2005-08-10 14:03:59 -04:00
|
|
|
(fold-right
|
|
|
|
(lambda (arg args)
|
2005-08-10 14:33:33 -04:00
|
|
|
(if (and (string? arg) (contains-glob-expression? arg))
|
2005-08-10 14:03:59 -04:00
|
|
|
(append (glob-argument arg) args)
|
|
|
|
(cons arg args)))
|
|
|
|
'() expanded)
|
|
|
|
(map expand-redirection (command-redirections command)))))
|
2005-05-27 13:01:14 -04:00
|
|
|
|
2005-08-10 14:03:59 -04:00
|
|
|
(define (expand-command-line command-line)
|
|
|
|
(make-command-line
|
|
|
|
(expand-command (command-line-first-cmd command-line))
|
|
|
|
(map (lambda (combinator.command)
|
|
|
|
(cons (car combinator.command)
|
|
|
|
(expand-command (cdr combinator.command))))
|
|
|
|
(command-line-combinator/cmds command-line))
|
|
|
|
(command-line-job-ctrl command-line)))
|
2005-05-27 13:01:14 -04:00
|
|
|
|
2005-08-10 14:03:59 -04:00
|
|
|
;; #####
|
|
|
|
;; it's a dumb idea to keep command and args separate, merge
|
|
|
|
;; this stuff
|
2005-05-23 10:52:03 -04:00
|
|
|
(define (standard-command-plugin-evaluater command args)
|
2005-08-10 14:03:59 -04:00
|
|
|
(let* ((parsed
|
|
|
|
(parse-command-line
|
|
|
|
(lex-command-line
|
|
|
|
(string-append command " "
|
|
|
|
(string-join args)))))
|
|
|
|
(expanded (expand-command-line parsed))
|
|
|
|
(s-expr (compile-command-line expanded)))
|
|
|
|
(debug-message "Compiled command " s-expr)
|
|
|
|
(eval-s-expr s-expr)))
|
2005-05-23 10:52:03 -04:00
|
|
|
|
|
|
|
(define standard-command-plugin
|
|
|
|
(make-command-plugin #f
|
|
|
|
standard-command-plugin-completer
|
|
|
|
standard-command-plugin-evaluater))
|
2005-05-23 12:03:26 -04:00
|
|
|
|
|
|
|
;; some common commands
|
|
|
|
|
2005-08-19 08:30:37 -04:00
|
|
|
(define no-completer #f)
|
2005-05-23 12:03:26 -04:00
|
|
|
|
2005-07-06 04:57:44 -04:00
|
|
|
;; Parse options for ls command using args-fold (SRFI 37)
|
|
|
|
;; We don't care for options that format the output.
|
|
|
|
|
|
|
|
(define defaults-ls-options
|
|
|
|
'((long . #t) (dot-files? . #t)
|
|
|
|
(sort-by-mtime . #f) (reverse-sort . #f)))
|
|
|
|
|
|
|
|
(define (parse-ls-arguments args)
|
|
|
|
(let* ((on/off-option-processor
|
|
|
|
(lambda (name)
|
|
|
|
(lambda (option arg-name arg ops)
|
|
|
|
(cons (cons name #t) ops))))
|
|
|
|
(long-option
|
|
|
|
(option '(#\l) #f #f
|
|
|
|
(on/off-option-processor 'long)))
|
|
|
|
(dotfiles-option
|
|
|
|
(option '(#\a) #f #f
|
|
|
|
(on/off-option-processor 'dot-files?)))
|
|
|
|
(sort-mtime-option
|
|
|
|
(option '(#\t) #f #f
|
|
|
|
(on/off-option-processor 'sort-by-mtime)))
|
|
|
|
(reverse-sort-option
|
|
|
|
(option '(#\r) #f #f
|
|
|
|
(on/off-option-processor 'reverse-sort))))
|
|
|
|
(let ((given-args
|
|
|
|
(args-fold
|
|
|
|
args
|
|
|
|
(list long-option dotfiles-option
|
|
|
|
sort-mtime-option reverse-sort-option)
|
|
|
|
(lambda (option name args operands)
|
|
|
|
(error "Unknown ls option" name))
|
|
|
|
cons '())))
|
|
|
|
(map (lambda (p)
|
|
|
|
(or (assoc (car p) given-args) p))
|
|
|
|
defaults-ls-options))))
|
|
|
|
|
2005-05-23 12:03:26 -04:00
|
|
|
(register-plugin!
|
2005-05-27 17:32:21 -04:00
|
|
|
(make-command-plugin
|
|
|
|
"ls"
|
|
|
|
no-completer
|
|
|
|
(lambda (command args)
|
2005-07-06 04:57:44 -04:00
|
|
|
(let* ((options (parse-ls-arguments args))
|
|
|
|
(set? (lambda (opt) (cdr (assoc opt options))))
|
|
|
|
(sort
|
|
|
|
(if (set? 'sort-by-mtime)
|
|
|
|
(lambda (lst)
|
|
|
|
(list-sort
|
|
|
|
(lambda (f g)
|
|
|
|
(< (file-info:mtime (fs-object-info f))
|
|
|
|
(file-info:mtime (fs-object-info g))))
|
|
|
|
lst))
|
|
|
|
(lambda (lst)
|
|
|
|
(list-sort
|
|
|
|
(lambda (f g)
|
|
|
|
(string<? (fs-object-name f) (fs-object-name g)))
|
|
|
|
lst))))
|
|
|
|
(reverse
|
|
|
|
(if (set? 'reverse-sort)
|
|
|
|
reverse
|
|
|
|
(lambda (l) l))))
|
|
|
|
(reverse
|
|
|
|
(sort
|
|
|
|
(directory-files (cwd) (set? 'dot-files?))))))))
|
2005-05-23 12:03:26 -04:00
|
|
|
|
2005-05-27 12:02:39 -04:00
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin "ps"
|
|
|
|
no-completer
|
|
|
|
(lambda (command args)
|
|
|
|
(pps))))
|
|
|
|
|
2005-05-23 12:03:26 -04:00
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin "pwd"
|
|
|
|
no-completer
|
|
|
|
(lambda (command args)
|
|
|
|
(cwd))))
|
|
|
|
|
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin "cd"
|
|
|
|
no-completer
|
|
|
|
(lambda (command args)
|
2005-05-26 13:39:20 -04:00
|
|
|
(chdir (resolve-file-name (if (null? args)
|
|
|
|
"~"
|
|
|
|
(car args))))
|
|
|
|
(cwd))))
|
2005-05-23 12:03:26 -04:00
|
|
|
|
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin "setenv"
|
|
|
|
no-completer
|
|
|
|
(lambda (command args)
|
|
|
|
(setenv (car args) (cadr args)))))
|
|
|
|
|
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin "getenv"
|
|
|
|
no-completer
|
|
|
|
(lambda (command args)
|
|
|
|
(getenv (car args)))))
|
|
|
|
|
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin "printenv"
|
|
|
|
no-completer
|
|
|
|
(lambda (command args)
|
|
|
|
(env->alist))))
|
|
|
|
|
2005-05-26 13:39:20 -04:00
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin "exit"
|
|
|
|
no-completer
|
|
|
|
(lambda (command args)
|
2005-06-04 07:22:44 -04:00
|
|
|
(clear)
|
2005-05-26 13:39:20 -04:00
|
|
|
(exit (if (null? args)
|
|
|
|
0
|
2005-06-04 07:22:44 -04:00
|
|
|
(string->number (car args)))))))
|
|
|
|
|
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin "jobs"
|
2005-06-07 14:24:05 -04:00
|
|
|
(lambda (command prefix args arg-pos)
|
2005-07-06 04:57:44 -04:00
|
|
|
'("running" "ready" "stopped" "output" "waiting-for-input"))
|
2005-06-04 07:22:44 -04:00
|
|
|
(lambda (command args)
|
2005-07-06 04:57:44 -04:00
|
|
|
(let ((selectors
|
|
|
|
`(("running" . ,running-jobs)
|
|
|
|
("ready" . ,ready-jobs)
|
|
|
|
("stopped" . ,stopped-jobs)
|
|
|
|
("output" . ,jobs-with-new-output)
|
|
|
|
("input" . ,jobs-waiting-for-input))))
|
|
|
|
(append-map
|
|
|
|
(lambda (arg)
|
|
|
|
(cond
|
|
|
|
((assoc arg selectors)
|
|
|
|
=> (lambda (p)
|
|
|
|
((cdr p))))))
|
|
|
|
(if (null? args)
|
|
|
|
(map car selectors)
|
|
|
|
(delete-duplicates args)))))))
|
2005-06-14 07:20:30 -04:00
|
|
|
|
|
|
|
(register-plugin!
|
|
|
|
(make-command-plugin
|
|
|
|
"ftp"
|
2005-08-18 05:23:59 -04:00
|
|
|
(let* ((hosts '("ftp.gnu.org" "ftp.x.org"))
|
|
|
|
(cs (make-completion-set hosts)))
|
|
|
|
(lambda (command to-complete)
|
|
|
|
(debug-message "ftp completer " command "," to-complete)
|
|
|
|
(completions-for cs (or (to-complete-prefix to-complete) ""))))
|
2005-06-14 07:20:30 -04:00
|
|
|
(lambda (command args)
|
2005-07-06 04:57:44 -04:00
|
|
|
(run/fg (,command ,@args)))))
|
2005-06-14 07:20:30 -04:00
|
|
|
|