[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

sh-proxy experiment

From: joakim
Subject: sh-proxy experiment
Date: Wed, 05 Jan 2011 19:53:25 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Any thougts on this little experiment?

;; sh-proxy.el -- experiment with elisp proxies for shell commands

;; (sh-proxy-create "df") defines a function sh/df
;; which takes keyword args corresponding to the flags of df.
;; eldoc works and function docstring

;; motivation: rather than a bunch of ugly notes like these:

;; export X=zen-mode
;; export USR=jave
;; export PASSWD=notmyactualpwd
;; ;mkdir $X
;; cd $X
;; ln -s *.el README
;; git init
;; git add .
;; git commit -m"initial commit"
;; curl -u "$USR:$PASSWD" -F "name=$X"  
;; git remote add origin address@hidden:jave/$X.git
;; git push origin master

;; Id like nice lisp:

;; (defun github-init-proj (x usr passwd)
;;   (cd x)
;;   (sh/ln :s "*.el" "README")
;;   (sh/git-init)
;;   (sh/git-add ".")
;;   (sh/git-commit :m "initial commit")
;;   (sh/curl :u (concat usr ":" passwd) :F (concat "name=" x)  
;;   (sh/git-remote :add "origin" (format "address@hidden:jave/%s.git" x))
;;   (sh/git-push "origin" "master"))

;; sh-proxy currently works somewhat with gnu style help output,
;; but not gits man style help output.

;; sh-proxy.el was inspired by dbus-proxy.el

(defun sh-proxy-parse-help (command)
  "Run COMMAND --help, parse the output and return a list of flags."
  ;;look in col 2 and 6 for -
  (with-current-buffer "*sh-proxy*" ;;todo create this buffer
    (process-file-shell-command command nil "*sh-proxy*" nil "--help")
    (goto-char (point-min))
      ;;this regexp whas briefly tested on "ls --help" with re-builder
      (while (re-search-forward "^\\(  -\\(.\\). \\|      \\)\\(--\\([^ 
=[\n]*\\)\\|                \\)" nil t)
        (sh-proxy-parse-help-2 2)
        (sh-proxy-parse-help-2 4)
      (cons (buffer-string) args))))

(defun sh-proxy-parse-help-2 (match-id)
  ;;fiddle out info from help matches
      ((flag (match-string match-id)))
    (if flag
;          (if (equal "t" flag)
;              (setq flag (concat "_" flag))) ;;see discussion above about "t"
          (setq args (append args (list (intern  flag))))))))

(defun sh-proxy-argstr (args)
  (mapconcat 'sh-proxy-sym2flag args " ") )
;;todo handle --, numbers

(defun sh-proxy-sym2flag  (x)
  (if (symbolp x)
      (concat (if (>  (length (symbol-name x)) 2) "--" "-") (substring 
(symbol-name x) 1))

(defun sh-proxy-create (command)
  "makes a sh proxy, which will have the name sh/COMMAND."
;;  (eval `(defun* ,(intern (concat "sh/" command)) (&rest rest &key 
,@(sh-proxy-parse-help command))
  (eval `(defun ,(intern (concat "sh/" command)) (&rest rest)
           ,(concat (car (sh-proxy-parse-help command)) "\n\n(fn &key" 
(mapconcat  'symbol-name (cdr (sh-proxy-parse-help command)) " ") ")" )
        (apply 'eshell-command (concat ,command " " (sh-proxy-argstr rest)) 

;;yeah, so far so good, but what about:
;; - piping? we would like the lisp syntax to be functional still (have a look 
at reusing eshell)
;; - stdin stdout? again, eshell
;; - other types help output?

;;(sh-proxy-parse-help "ls")

Joakim Verona

reply via email to

[Prev in Thread] Current Thread [Next in Thread]