diff --git a/elpy-shell.el b/elpy-shell.el index 09c716048..da362a41e 100644 --- a/elpy-shell.el +++ b/elpy-shell.el @@ -350,7 +350,38 @@ if ARG is negative or 0, disable the use of a dedicated shell." (if (<= arg 0) (kill-local-variable 'python-shell-buffer-name) (setq-local python-shell-buffer-name - (format "Python[%s]" (buffer-name)))))) + (format "Python[%s]L" (buffer-name)))))) + +(defun elpy-shell-set-local-shell (&optional shell-name) + ;; Todo: doc need rewriting + "Associate the current buffer to a given shell. + +This means that the code from the current buffer will be sent to this shell. + +If SHELL-NAME is not specified, ask with completion for a name. + +If SHELL-NAME is \"Global\", associate the current buffer to the main python +shell (often \"*Python*\" shell)." + (interactive) + (let* ((current-shell-name (if (local-variable-p 'python-shell-buffer-name) + (progn + (string-match "Python\\[\\(.*?\\)\\]" + python-shell-buffer-name) + (match-string 1 python-shell-buffer-name)) + "Global")) + (shell-names (cl-loop + for buffer in (buffer-list) + for buffer-name = (substring-no-properties (buffer-name buffer)) + if (string-match "\\*Python\\[\\(.*?\\)\\]\\*" buffer-name) + collect (match-string 1 buffer-name))) + (candidates (remove current-shell-name + (delete-dups + (append (list (buffer-name) "Global") shell-names)))) + (prompt (format "Shell name (current: %s): " current-shell-name)) + (shell-name (or shell-name (completing-read prompt candidates)))) + (if (string= shell-name "Global") + (kill-local-variable 'python-shell-buffer-name) + (setq-local python-shell-buffer-name (format "Python[%s]" shell-name))))) (defun elpy-shell-change-global-shell-name (name) "Tell Elpy to use another python shell named NAME.