diff --git a/layers/+lang/python/funcs.el b/layers/+lang/python/funcs.el index e8e839da3..f140c91c2 100644 --- a/layers/+lang/python/funcs.el +++ b/layers/+lang/python/funcs.el @@ -9,6 +9,20 @@ ;; ;;; License: GPLv3 +(defun spacemacs//python-default () + "Defaut settings for python buffers" + (setq mode-name "Python" + tab-width python-tab-width + fill-column python-fill-column) + (when (version< emacs-version "24.5") + ;; auto-indent on colon doesn't work well with if statement + ;; should be fixed in 24.5 and above + (setq electric-indent-chars (delq ?: electric-indent-chars))) + (setq-local comment-inline-offset 2) + (spacemacs/python-annotate-pdb) + ;; make C-j work the same way as RET + (local-set-key (kbd "C-j") 'newline-and-indent)) + ;; from http://pedrokroger.net/2010/07/configuring-emacs-as-a-python-ide-2/ (defun spacemacs/python-annotate-pdb () "Highlight break point lines." @@ -237,3 +251,89 @@ to be called for each testrunner. " (interactive) (forward-button 1) (call-interactively #'push-button)) + + +;; REPL + +(defun spacemacs//inferior-python-setup-hook () + "Setup REPL for python inferior process buffer." + (setq indent-tabs-mode t)) + +(defun spacemacs/python-shell-send-buffer-switch () + "Send buffer content to shell and switch to it in insert mode." + (interactive) + (python-shell-send-buffer) + (python-shell-switch-to-shell) + (evil-insert-state)) + +(defun spacemacs/python-shell-send-defun-switch () + "Send function content to shell and switch to it in insert mode." + (interactive) + (python-shell-send-defun nil) + (python-shell-switch-to-shell) + (evil-insert-state)) + +(defun spacemacs/python-shell-send-region-switch (start end) + "Send region content to shell and switch to it in insert mode." + (interactive "r") + (python-shell-send-region start end) + (python-shell-switch-to-shell) + (evil-insert-state)) + +(defun spacemacs/python-start-or-switch-repl () + "Start and/or switch to the REPL." + (interactive) + (let ((shell-process + (or (python-shell-get-process) + ;; `run-python' has different return values and different + ;; errors in different emacs versions. In 24.4, it throws an + ;; error when the process didn't start, but in 25.1 it + ;; doesn't throw an error, so we demote errors here and + ;; check the process later + (with-demoted-errors "Error: %S" + ;; in Emacs 24.5 and 24.4, `run-python' doesn't return the + ;; shell process + (call-interactively #'run-python) + (python-shell-get-process))))) + (unless shell-process + (error "Failed to start python shell properly")) + (pop-to-buffer (process-buffer shell-process)) + (evil-insert-state))) + +(defun spacemacs/python-execute-file (arg) + "Execute a python script in a shell." + (interactive "P") + ;; set compile command to buffer-file-name + ;; universal argument put compile buffer in comint mode + (let ((universal-argument t) + (compile-command (format "%s %s" + (spacemacs/pyenv-executable-find python-shell-interpreter) + (file-name-nondirectory buffer-file-name)))) + (if arg + (call-interactively 'compile) + (compile compile-command t) + (with-current-buffer (get-buffer "*compilation*") + (inferior-python-mode))))) + +(defun spacemacs/python-execute-file-focus (arg) + "Execute a python script in a shell and switch to the shell buffer in + `insert state'." + (interactive "P") + (spacemacs/python-execute-file arg) + (switch-to-buffer-other-window "*compilation*") + (end-of-buffer) + (evil-insert-state)) + +;; fix for issue #2569 (https://github.com/syl20bnr/spacemacs/issues/2569) +(when (version< emacs-version "25") + (advice-add 'wisent-python-default-setup :after + #'spacemacs//python-imenu-create-index-use-semantic-maybe)) + + +;; Eldoc + +(defun spacemacs//init-eldoc-python-mode () + "Initialize elddoc for python buffers" + (eldoc-mode) + (when (configuration-layer/package-used-p 'anaconda-mode) + (anaconda-eldoc-mode))) diff --git a/layers/+lang/python/packages.el b/layers/+lang/python/packages.el index a74425300..b863f7c75 100644 --- a/layers/+lang/python/packages.el +++ b/layers/+lang/python/packages.el @@ -100,10 +100,6 @@ "gu" 'anaconda-mode-usages)))) (defun python/post-init-eldoc () - (defun spacemacs//init-eldoc-python-mode () - (eldoc-mode) - (when (configuration-layer/package-used-p 'anaconda-mode) - (anaconda-eldoc-mode))) (add-hook 'python-mode-hook 'spacemacs//init-eldoc-python-mode)) (defun python/post-init-evil-matchit () @@ -263,26 +259,11 @@ :init (progn (spacemacs/register-repl 'python 'python-start-or-switch-repl "python") - - (defun python-default () - (setq mode-name "Python" - tab-width python-tab-width - fill-column python-fill-column) - (when (version< emacs-version "24.5") - ;; auto-indent on colon doesn't work well with if statement - ;; should be fixed in 24.5 and above - (setq electric-indent-chars (delq ?: electric-indent-chars))) - (setq-local comment-inline-offset 2) - (spacemacs/python-annotate-pdb) - ;; make C-j work the same way as RET - (local-set-key (kbd "C-j") 'newline-and-indent)) - - (defun inferior-python-setup-hook () - (setq indent-tabs-mode t)) - - (add-hook 'inferior-python-mode-hook #'inferior-python-setup-hook) - (add-hook 'python-mode-hook #'python-default) - ;; call `spacemacs//python-setup-shell' once, don't put it in a hook (see issue #5988) + (add-hook 'inferior-python-mode-hook + #'spacemacs//inferior-python-setup-hook) + (add-hook 'python-mode-hook #'spacemacs//python-default) + ;; call `spacemacs//python-setup-shell' once, don't put it in a hook + ;; (see issue #5988) (spacemacs//python-setup-shell)) :config (progn @@ -290,96 +271,26 @@ (with-eval-after-load 'auto-highlight-symbol (add-to-list 'ahs-plugin-bod-modes 'python-mode)) - (defun python-shell-send-buffer-switch () - "Send buffer content to shell and switch to it in insert mode." - (interactive) - (python-shell-send-buffer) - (python-shell-switch-to-shell) - (evil-insert-state)) - - (defun python-shell-send-defun-switch () - "Send function content to shell and switch to it in insert mode." - (interactive) - (python-shell-send-defun nil) - (python-shell-switch-to-shell) - (evil-insert-state)) - - (defun python-shell-send-region-switch (start end) - "Send region content to shell and switch to it in insert mode." - (interactive "r") - (python-shell-send-region start end) - (python-shell-switch-to-shell) - (evil-insert-state)) - - (defun python-start-or-switch-repl () - "Start and/or switch to the REPL." - (interactive) - (let ((shell-process - (or (python-shell-get-process) - ;; `run-python' has different return values and different - ;; errors in different emacs versions. In 24.4, it throws an - ;; error when the process didn't start, but in 25.1 it - ;; doesn't throw an error, so we demote errors here and - ;; check the process later - (with-demoted-errors "Error: %S" - ;; in Emacs 24.5 and 24.4, `run-python' doesn't return the - ;; shell process - (call-interactively #'run-python) - (python-shell-get-process))))) - (unless shell-process - (error "Failed to start python shell properly")) - (pop-to-buffer (process-buffer shell-process)) - (evil-insert-state))) - - (defun spacemacs/python-execute-file (arg) - "Execute a python script in a shell." - (interactive "P") - ;; set compile command to buffer-file-name - ;; universal argument put compile buffer in comint mode - (let ((universal-argument t) - (compile-command (format "%s %s" - (spacemacs/pyenv-executable-find python-shell-interpreter) - (file-name-nondirectory buffer-file-name)))) - (if arg - (call-interactively 'compile) - (compile compile-command t) - (with-current-buffer (get-buffer "*compilation*") - (inferior-python-mode))))) - - (defun spacemacs/python-execute-file-focus (arg) - "Execute a python script in a shell and switch to the shell buffer in -`insert state'." - (interactive "P") - (spacemacs/python-execute-file arg) - (switch-to-buffer-other-window "*compilation*") - (end-of-buffer) - (evil-insert-state)) - - ;; fix for issue #2569 (https://github.com/syl20bnr/spacemacs/issues/2569) - (when (version< emacs-version "25") - (advice-add 'wisent-python-default-setup :after - #'spacemacs//python-imenu-create-index-use-semantic-maybe)) - (spacemacs/declare-prefix-for-mode 'python-mode "mc" "execute") (spacemacs/declare-prefix-for-mode 'python-mode "md" "debug") (spacemacs/declare-prefix-for-mode 'python-mode "mh" "help") (spacemacs/declare-prefix-for-mode 'python-mode "mg" "goto") - (spacemacs/declare-prefix-for-mode 'python-mode "ms" "send to REPL") + (spacemacs/declare-prefix-for-mode 'python-mode "ms" "REPL") (spacemacs/declare-prefix-for-mode 'python-mode "mr" "refactor") (spacemacs/declare-prefix-for-mode 'python-mode "mv" "pyenv") (spacemacs/declare-prefix-for-mode 'python-mode "mV" "pyvenv") (spacemacs/set-leader-keys-for-major-mode 'python-mode - "'" 'python-start-or-switch-repl + "'" 'spacemacs/python-start-or-switch-repl "cc" 'spacemacs/python-execute-file "cC" 'spacemacs/python-execute-file-focus "db" 'spacemacs/python-toggle-breakpoint "ri" 'spacemacs/python-remove-unused-imports - "sB" 'python-shell-send-buffer-switch + "sB" 'spacemacs/python-shell-send-buffer-switch "sb" 'python-shell-send-buffer - "sF" 'python-shell-send-defun-switch + "sF" 'spacemacs/python-shell-send-defun-switch "sf" 'python-shell-send-defun - "si" 'python-start-or-switch-repl - "sR" 'python-shell-send-region-switch + "si" 'spacemacs/python-start-or-switch-repl + "sR" 'spacemacs/python-shell-send-region-switch "sr" 'python-shell-send-region) ;; Emacs users won't need these key bindings