i-bash/ibash-scheme/modules/cd
Dale Mellor de19bb2129 ibash: extend bash with guile call-outs.
Bash is modified to call out to Guile code whenever the user enters a
command-line; the code has the opportunity to fix up the command line in
any way.  The Guile code is under the ibash-scheme directory, all is all
newly created in this delta.

   * configure: why does this have to be in here?
   * configure.ac: look for guile and libguile.
   * eval.c: call guile_main and load ~/.bash_guile.scm.
   * shell.c: perform call-outs prior to executing command lines.
   * ibash-scheme/ibash-callout.scm: first call-out.
   * ibash-scheme/remote-sender.scm: second call-out.
   * ibash-scheme/ibash-server.scm: central intelligence service.
   * ibash-scheme/modules/i-bash: transform ib -> i-bash.
   * ibash-scheme/modules/cd: cd command line processor.
2020-08-29 15:35:11 +01:00

71 lines
3 KiB
Text

;; This file is loaded by ibash-server.scm, and will return a function
;; which intelligently changes a 'cd' command to go directly to a
;; directory under a specially designated set of parent directories.
;; Copyright (C) 2020 Dale Mellor
;;
;; This file is part of iBash, the intelligent Bourne Again SHell.
;;
;; iBash is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; iBash is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with iBash. If not, see <http://www.gnu.org/licenses/>.
(set! db-modules (cons '(db server cd) db-modules))
(define-module (db server cd)
#:export (verbs))
;; Return an s-exrpression defining a function which returns a list of
;; (absolute) directory paths to search for some given directory.
(define look-list-maker--s '(lambda ()
(let ((home (getenv "HOME")))
(map (lambda (dir)
(string-append home "/" dir))
'("projects" "documents" "")))))
;; Return an s-expression which evaluates to a function which searches for
;; the requested directory and then modifies the 'cd' command-line
;; (/words/) to go directly to that directory, regardless of the current
;; position in the file system.
;;
;; The return is a cons of remaining /words/ to process, and the modified
;; 'cd' command-line prepended to the existing /stack/ of processed
;; command-lines.
(define (cd words stack)
(cond ((null? (cdr words))
'(lambda (w s) (cons '() (cons (list "cd") s))))
(else
`(lambda (words stack)
(if (access? (cadr words) R_OK)
(cons (cddr words) (cons (list "cd" (cadr words)) stack))
(let loop ((look ((primitive-eval ,look-list-maker--s))))
(cond ((null? look)
(cons (cddr words) (cons (list "cd" (cadr words))
stack)))
(else
(let ((dir (string-append (car look)
"/"
(cadr words))))
(if (access? dir R_OK)
(cons (cddr words) (cons (list "cd" dir)
stack))
(loop (cdr look))))))))))))
;; Register the above processor with lines beginning with 'cd'.
(define (verbs) (list (cons "cd" cd)))