Skip to content

Commit

Permalink
add customizable shell commands (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
riscy committed Apr 7, 2020
1 parent 879348b commit 72c92ca
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions shx.el
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
"Triggers of the form: (regexp . function)."
:type '(alist :key-type regexp :value-type function))

(defcustom shx-customized-shell-commands '()
"Map from hostnames/remote identifiers to preferred shells/interpreters.
Example: ((\"my-host.ca\" . \"/bin/zsh\") (\"^/docker:.*\" . \"/bin/sh\"))"

This comment has been minimized.

Copy link
@p3r7

p3r7 Apr 7, 2020

"my-host.ca" should be "my-host\.ca" as the string gets interpreted as a regexp.

But sadly I don't know how to properly quote this as part of the docstring:

  • "[...] "\"my-host\\.ca\" [...]" produces [...] "my-host\\.ca" [...] -> double \
  • "[...] "\"my-host\.ca\" [...]" produces [...] "my-host.ca" [...] -> missing \

This comment has been minimized.

Copy link
@riscy

riscy Apr 8, 2020

Author Owner

It seems like the first one works -

(insert
  "Map from hostnames/remote identifiers to preferred shells/interpreters.
Example: ((\"my-host\\.ca\" . \"/bin/zsh\") (\"^/docker:.*\" . \"/bin/sh\"))")

->

 Map from hostnames/remote identifiers to preferred shells/interpreters.
Example: (("my-host\.ca"  . "/bin/zsh") ("^/docker:.*" . "/bin/sh")) 

and it appears correctly in the Help buffer.

This comment has been minimized.

Copy link
@p3r7

p3r7 Apr 8, 2020

Oh, nice to her.

I was testing with helpful which must be misbehaving for this use-case.

:link '(function-link shx--customized-shell-command)
:type '(alist :key-type regexp :value-type string))

(defcustom shx-directory-tracker-regexp nil
"Input regexp that triggers the `shell-resync-dirs' command."
:link '(function-link shx--directory-tracker)
Expand Down Expand Up @@ -399,7 +405,7 @@ If optional NEW-DIRECTORY is set, use that for `default-directory'."
(let ((default-directory (or new-directory default-directory)))
(when (file-remote-p default-directory)
(message "Restarting shell at %s (C-g to stop)" default-directory))
(let ((cmd (shx--validate-shell-file-name)))
(let ((cmd (shx--shell-command)))
(shx-insert 'font-lock-doc-face "\n" cmd " at " default-directory "\n")
;; manually align comint-file-name-prefix with the default-directory:
(setq-local comint-file-name-prefix (or (file-remote-p default-directory) ""))
Expand All @@ -410,14 +416,30 @@ If optional NEW-DIRECTORY is set, use that for `default-directory'."
;; if all that was successful, commit to the new default directory:
(when new-directory (setq default-directory new-directory)))

(defun shx--validate-shell-file-name ()
"Guess which shell command to run, even if on a remote host or container."
(defun shx--shell-command ()
"Get the shell command, even if on a remote host or container."
(declare (side-effect-free t))
(let ((remote-id (or (file-remote-p default-directory) ""))
;; guess which shell command to run per `shell' convention:
(cmd (or explicit-shell-file-name (getenv "ESHELL") shell-file-name)))
(let* ((remote-id (or (file-remote-p default-directory) ""))
;; guess which shell command to run per `shell' convention:
(cmd (or explicit-shell-file-name
(shx--customized-shell-command remote-id)
(getenv "ESHELL")
shell-file-name)))
(cond ((file-exists-p (concat remote-id cmd)) cmd)
(t (read-file-name "Shell: " nil nil t (concat remote-id "/bin/sh"))))))
(t (completing-read "Shell command: " nil)))))

(defun shx--customized-shell-command (host &optional options)
"Return user's preferred interpreter for HOST, or nil.
If OPTIONS is nil, choose from among `shx-customized-shell-commands'."
(declare (side-effect-free t))
(setq options (or options shx-customized-shell-commands))
(when options
(let ((regexp (caar options))
(interpreter (cdar options))
(host (or host "localhost")))
(cond
((string-match regexp host) interpreter)
((cdr options) (shx--customized-shell-command host (cdr options)))))))

(defun shx--match-last-line (regexp)
"Return a form to find REGEXP on the last line of the buffer."
Expand Down Expand Up @@ -1019,7 +1041,7 @@ See the function `shx-mode' for details."
(default-directory (or directory default-directory)))
;; `switch-to-buffer' first (`shell' uses the unpredictable `pop-to-buffer')
(switch-to-buffer name)
(let ((explicit-shell-file-name (shx--validate-shell-file-name)))
(let ((explicit-shell-file-name (shx--shell-command)))
(shell name))
;; shx might already be active due to shx-global-mode:
(unless shx-mode (shx-mode))))
Expand Down

0 comments on commit 72c92ca

Please sign in to comment.