riscy / shx-for-emacs

An Emacs shell-mode (and comint-mode) extension that enables displaying small plots and graphics and lets users write shell commands in Emacs Lisp.
GNU General Public License v3.0
218 stars 10 forks source link

shx aliases? #7

Closed CeleritasCelery closed 6 years ago

CeleritasCelery commented 6 years ago

Is there a way to create an shx-cmd-* that will allow me to send input to the shell afterwards? Essentially replacing the need to shell functions and aliases.

For example, If I want to set a tool path I could call shx-cmd-setpath with :setpath and then use Emacs superior functionality to find the tool path I want. after that I could send the proper environment variables to the shell.

I was looking into it, but couldn't seem to find a way to do this.

riscy commented 6 years ago

The best way is probably to use comint-simple-send:

(defun shx-cmd-setvar (value)
  "Sets the environment variable VAR to VALUE."
  (comint-simple-send nil (format "export VAR=%s" value)))
riscy commented 6 years ago

Also I'd be curious to see some examples of what you come up with, if you'd like. :)

CeleritasCelery commented 6 years ago

this seems to work for me

(defun shx-cmd-setmodel (_input)
  (let* ((root (getenv "HDK_MODELS"))
         (models (s-lines (shell-command-to-string (format "find %s/ -maxdepth 1 2>/dev/null" root))))
         (model (completing-read "Select Model: " (mapcar 'file-name-nondirectory models))))
    (setenv "MODEL_ROOT" (concat root "/" model))
    (comint-simple-send nil (format "export MODEL_ROOT=%s" (getenv "MODEL_ROOT")))))

Though it echos the prompt an extra time. I will have to look into it. Probably need to use the output filter functions.

Thanks!