2015-01-14 04:12:56 +00:00
|
|
|
;;; packages.el --- Python Layer packages File for Spacemacs
|
|
|
|
;;
|
|
|
|
;; Copyright (c) 2012-2014 Sylvain Benner
|
|
|
|
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
|
|
|
|
;;
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
;;
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
2015-04-19 03:40:24 +00:00
|
|
|
(setq python-packages
|
2014-11-10 04:54:10 +00:00
|
|
|
'(
|
|
|
|
anaconda-mode
|
2015-04-09 03:57:22 +00:00
|
|
|
company
|
2014-11-10 04:54:10 +00:00
|
|
|
company-anaconda
|
2015-04-09 03:57:22 +00:00
|
|
|
cython-mode
|
2014-11-10 04:54:10 +00:00
|
|
|
eldoc
|
2014-12-09 03:19:55 +00:00
|
|
|
evil-jumper
|
2014-11-10 04:54:10 +00:00
|
|
|
flycheck
|
2015-07-24 02:20:25 +00:00
|
|
|
helm-cscope
|
2015-03-29 04:57:43 +00:00
|
|
|
helm-pydoc
|
2014-12-14 05:19:20 +00:00
|
|
|
hy-mode
|
2015-04-25 03:54:39 +00:00
|
|
|
pip-requirements
|
2015-01-30 15:30:10 +00:00
|
|
|
pyenv-mode
|
2015-06-08 03:59:23 +00:00
|
|
|
pytest
|
2014-11-10 04:54:10 +00:00
|
|
|
python
|
2015-06-08 03:59:23 +00:00
|
|
|
pyvenv
|
2014-12-09 17:08:30 +00:00
|
|
|
semantic
|
2014-12-12 03:16:09 +00:00
|
|
|
smartparens
|
2015-04-15 22:57:31 +00:00
|
|
|
stickyfunc-enhance
|
2015-07-24 02:20:25 +00:00
|
|
|
xcscope
|
2015-04-19 03:40:24 +00:00
|
|
|
))
|
2014-11-10 04:54:10 +00:00
|
|
|
|
2014-12-09 03:19:55 +00:00
|
|
|
(defun python/init-anaconda-mode ()
|
|
|
|
(use-package anaconda-mode
|
|
|
|
:defer t
|
|
|
|
:init (add-hook 'python-mode-hook 'anaconda-mode)
|
|
|
|
:config
|
|
|
|
(progn
|
|
|
|
(evil-leader/set-key-for-mode 'python-mode
|
2015-02-13 03:21:35 +00:00
|
|
|
"mhh" 'anaconda-mode-view-doc
|
2015-01-21 03:32:13 +00:00
|
|
|
"mgg" 'anaconda-mode-goto)
|
2014-12-09 03:19:55 +00:00
|
|
|
(spacemacs|hide-lighter anaconda-mode))))
|
|
|
|
|
2015-01-22 16:22:19 +00:00
|
|
|
(defun python/init-cython-mode ()
|
|
|
|
(use-package cython-mode
|
|
|
|
:defer t
|
|
|
|
:init
|
|
|
|
(progn
|
|
|
|
(evil-leader/set-key-for-mode 'cython-mode
|
2015-02-13 03:21:35 +00:00
|
|
|
"mhh" 'anaconda-mode-view-doc
|
2015-01-22 16:22:19 +00:00
|
|
|
"mgg" 'anaconda-mode-goto)
|
|
|
|
)))
|
|
|
|
|
2015-08-09 03:35:16 +00:00
|
|
|
(defun python/post-init-eldoc ()
|
2014-12-23 03:05:03 +00:00
|
|
|
(add-hook 'python-mode-hook 'eldoc-mode))
|
2014-12-09 03:19:55 +00:00
|
|
|
|
2015-08-09 03:35:16 +00:00
|
|
|
(defun python/post-init-evil-jumper ()
|
2014-12-09 03:19:55 +00:00
|
|
|
(defadvice anaconda-mode-goto (before python/anaconda-mode-goto activate)
|
|
|
|
(evil-jumper--push)))
|
|
|
|
|
2015-04-25 03:54:39 +00:00
|
|
|
(defun python/init-pip-requirements ()
|
|
|
|
(use-package pip-requirements
|
2015-04-25 05:37:18 +00:00
|
|
|
:defer t
|
2015-04-25 03:54:39 +00:00
|
|
|
:init
|
2015-04-25 05:37:18 +00:00
|
|
|
(progn
|
|
|
|
;; company support
|
2015-04-30 03:54:30 +00:00
|
|
|
(push 'company-capf company-backends-pip-requirements-mode)
|
2015-04-25 03:54:39 +00:00
|
|
|
(spacemacs|add-company-hook pip-requirements-mode))))
|
|
|
|
|
2015-01-30 15:30:10 +00:00
|
|
|
(defun python/init-pyenv-mode ()
|
|
|
|
(use-package pyenv-mode
|
|
|
|
:defer t
|
|
|
|
:init (progn
|
|
|
|
(evil-leader/set-key-for-mode 'python-mode
|
2015-01-31 03:28:47 +00:00
|
|
|
"mvs" 'pyenv-mode-set
|
|
|
|
"mvu" 'pyenv-mode-unset))))
|
2015-01-30 15:30:10 +00:00
|
|
|
|
2014-12-10 16:30:48 +00:00
|
|
|
(defun python/init-pyvenv ()
|
|
|
|
(use-package pyvenv
|
2014-12-09 03:19:55 +00:00
|
|
|
:defer t
|
|
|
|
:init
|
|
|
|
(evil-leader/set-key-for-mode 'python-mode
|
2015-01-31 03:28:47 +00:00
|
|
|
"mV" 'pyvenv-workon)))
|
2014-12-09 03:19:55 +00:00
|
|
|
|
2015-06-04 09:09:56 +00:00
|
|
|
(defun python/init-pytest ()
|
|
|
|
(use-package pytest
|
2015-06-08 03:59:23 +00:00
|
|
|
:if (eq 'pytest python-test-runner)
|
2015-06-04 09:09:56 +00:00
|
|
|
:defer t
|
|
|
|
:commands (pytest-one
|
|
|
|
pytest-pdb-one
|
|
|
|
pytest-all
|
|
|
|
pytest-pdb-all
|
|
|
|
pytest-module
|
|
|
|
pytest-pdb-module)
|
2015-06-08 03:59:23 +00:00
|
|
|
:init (evil-leader/set-key-for-mode 'python-mode
|
|
|
|
"mTa" 'pytest-pdb-all
|
|
|
|
"mta" 'pytest-all
|
|
|
|
"mTb" 'pytest-pdb-module
|
|
|
|
"mtb" 'pytest-module
|
|
|
|
"mTt" 'pytest-pdb-one
|
|
|
|
"mtt" 'pytest-one
|
|
|
|
"mTm" 'pytest-pdb-module
|
|
|
|
"mtm" 'pytest-module)
|
|
|
|
:config (add-to-list 'pytest-project-root-files "setup.cfg")))
|
2015-06-04 09:09:56 +00:00
|
|
|
|
2014-11-10 04:54:10 +00:00
|
|
|
(defun python/init-python ()
|
|
|
|
(use-package python
|
|
|
|
:defer t
|
|
|
|
:init
|
|
|
|
(progn
|
2014-12-09 03:19:55 +00:00
|
|
|
(defun python-default ()
|
|
|
|
(setq mode-name "Python"
|
|
|
|
tab-width 4
|
|
|
|
;; auto-indent on colon doesn't work well with if statement
|
|
|
|
electric-indent-chars (delq ?: electric-indent-chars))
|
|
|
|
(annotate-pdb)
|
|
|
|
(spacemacs/highlight-TODO-words)
|
|
|
|
;; make C-j work the same way as RET
|
|
|
|
(local-set-key (kbd "C-j") 'newline-and-indent))
|
|
|
|
|
2014-11-10 04:54:10 +00:00
|
|
|
(defun python-setup-shell ()
|
|
|
|
(if (executable-find "ipython")
|
|
|
|
(setq python-shell-interpreter "ipython"
|
|
|
|
;; python-shell-interpreter-args (if (system-is-mac)
|
|
|
|
;; "--gui=osx --matplotlib=osx --colors=Linux"
|
|
|
|
;; (if (system-is-linux)
|
|
|
|
;; "--gui=wx --matplotlib=wx --colors=Linux"))
|
|
|
|
python-shell-prompt-regexp "In \\[[0-9]+\\]: "
|
|
|
|
python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
|
|
|
|
python-shell-completion-setup-code "from IPython.core.completerlib import module_completion"
|
|
|
|
python-shell-completion-module-string-code "';'.join(module_completion('''%s'''))\n"
|
|
|
|
python-shell-completion-string-code "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
|
|
|
|
(setq python-shell-interpreter "python")))
|
|
|
|
|
2015-06-02 11:21:34 +00:00
|
|
|
(defun inferior-python-setup-hook ()
|
|
|
|
(setq indent-tabs-mode t))
|
|
|
|
|
|
|
|
(add-hook 'inferior-python-mode-hook #'inferior-python-setup-hook)
|
2014-12-09 03:19:55 +00:00
|
|
|
(add-all-to-hook 'python-mode-hook
|
|
|
|
'python-default
|
|
|
|
'python-setup-shell))
|
2014-11-10 04:54:10 +00:00
|
|
|
:config
|
|
|
|
(progn
|
2015-02-07 16:03:07 +00:00
|
|
|
(add-hook 'inferior-python-mode-hook 'smartparens-mode)
|
2014-11-10 04:54:10 +00:00
|
|
|
;; add support for `ahs-range-beginning-of-defun' for python-mode
|
|
|
|
(eval-after-load 'auto-highlight-symbol
|
|
|
|
'(add-to-list 'ahs-plugin-bod-modes 'python-mode))
|
2014-12-09 03:19:55 +00:00
|
|
|
|
|
|
|
(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))
|
|
|
|
|
2015-02-18 03:00:26 +00:00
|
|
|
(defun python-shell-send-region-switch (start end)
|
2014-12-09 03:19:55 +00:00
|
|
|
"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)
|
|
|
|
(python-shell-switch-to-shell)
|
|
|
|
(evil-insert-state))
|
|
|
|
|
2015-03-14 06:58:11 +00:00
|
|
|
;; reset compile-command (by default it is `make -k')
|
|
|
|
(setq compile-command nil)
|
|
|
|
(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
|
|
|
|
(setq universal-argument t)
|
|
|
|
(if arg
|
|
|
|
(call-interactively 'compile)
|
2015-03-14 11:14:11 +00:00
|
|
|
|
2015-04-13 19:29:46 +00:00
|
|
|
(setq compile-command (format "python %s" (file-name-nondirectory
|
|
|
|
buffer-file-name)))
|
|
|
|
(compile compile-command t)
|
|
|
|
(with-current-buffer (get-buffer "*compilation*")
|
|
|
|
(inferior-python-mode))))
|
2015-03-14 06:58:11 +00:00
|
|
|
|
|
|
|
(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*")
|
2015-03-14 09:41:54 +00:00
|
|
|
(end-of-buffer)
|
2015-03-14 06:58:11 +00:00
|
|
|
(evil-insert-state))
|
|
|
|
|
2014-11-10 04:54:10 +00:00
|
|
|
(evil-leader/set-key-for-mode 'python-mode
|
2015-03-14 06:58:11 +00:00
|
|
|
"mcc" 'spacemacs/python-execute-file
|
|
|
|
"mcC" 'spacemacs/python-execute-file-focus
|
2015-01-14 15:12:09 +00:00
|
|
|
"mdb" 'python-toggle-breakpoint
|
2015-04-25 02:15:02 +00:00
|
|
|
"mri" 'python-remove-unused-imports
|
2015-02-18 03:07:43 +00:00
|
|
|
"msB" 'python-shell-send-buffer-switch
|
|
|
|
"msb" 'python-shell-send-buffer
|
|
|
|
"msF" 'python-shell-send-defun-switch
|
|
|
|
"msf" 'python-shell-send-defun
|
|
|
|
"msi" 'python-start-or-switch-repl
|
|
|
|
"msR" 'python-shell-send-region-switch
|
|
|
|
"msr" 'python-shell-send-region)
|
2014-11-10 04:54:10 +00:00
|
|
|
|
2015-05-23 17:19:48 +00:00
|
|
|
;; Emacs users won't need these key bindings
|
2015-05-28 03:49:19 +00:00
|
|
|
;; TODO: make these key bindings dynamic given the current style
|
|
|
|
;; Doing it only at init time won't update it if the user switches style
|
|
|
|
;; Also find a way to generalize these bindings.
|
2015-05-23 17:19:48 +00:00
|
|
|
(when (eq dotspacemacs-editing-style 'vim)
|
|
|
|
;; the default in Emacs is M-n
|
|
|
|
(define-key inferior-python-mode-map (kbd "C-j") 'comint-next-input)
|
|
|
|
;; the default in Emacs is M-p and this key binding overrides default C-k
|
|
|
|
;; which prevents Emacs users to kill line
|
|
|
|
(define-key inferior-python-mode-map (kbd "C-k") 'comint-previous-input)
|
|
|
|
;; the default in Emacs is M-r; C-r to search backward old output
|
|
|
|
;; and should not be changed
|
|
|
|
(define-key inferior-python-mode-map (kbd "C-r") 'comint-history-isearch-backward)
|
|
|
|
;; this key binding is for recentering buffer in Emacs
|
|
|
|
;; it would be troublesome if Emacs user
|
|
|
|
;; Vim users can use this key since they have other key
|
|
|
|
(define-key inferior-python-mode-map (kbd "C-l") 'comint-clear-buffer))
|
|
|
|
|
|
|
|
;; add this optional key binding for Emacs user, since it is unbound
|
|
|
|
(define-key inferior-python-mode-map (kbd "C-c M-l") 'comint-clear-buffer))))
|
2014-11-10 04:54:10 +00:00
|
|
|
|
2015-04-11 02:23:16 +00:00
|
|
|
(defun python/post-init-flycheck ()
|
2014-11-29 06:20:50 +00:00
|
|
|
(add-hook 'python-mode-hook 'flycheck-mode))
|
2014-12-09 17:08:30 +00:00
|
|
|
|
2015-04-03 21:35:33 +00:00
|
|
|
(defun python/init-hy-mode ()
|
2014-12-14 05:19:20 +00:00
|
|
|
(use-package hy-mode
|
|
|
|
:defer t))
|
|
|
|
|
2015-03-29 04:57:43 +00:00
|
|
|
(defun python/init-helm-pydoc ()
|
|
|
|
(use-package helm-pydoc
|
|
|
|
:defer t
|
|
|
|
:init
|
|
|
|
(evil-leader/set-key-for-mode 'python-mode "mhd" 'helm-pydoc)))
|
|
|
|
|
2015-08-09 03:35:16 +00:00
|
|
|
(defun python/post-init-smartparens ()
|
2014-12-12 03:16:09 +00:00
|
|
|
(defadvice python-indent-dedent-line-backspace
|
|
|
|
(around python/sp-backward-delete-char activate)
|
|
|
|
(let ((pythonp (or (not smartparens-strict-mode)
|
|
|
|
(char-equal (char-before) ?\s))))
|
|
|
|
(if pythonp
|
|
|
|
ad-do-it
|
|
|
|
(call-interactively 'sp-backward-delete-char)))))
|
2015-04-03 21:12:56 +00:00
|
|
|
|
2015-04-03 23:13:52 +00:00
|
|
|
(when (configuration-layer/layer-usedp 'auto-completion)
|
2015-04-09 03:57:22 +00:00
|
|
|
(defun python/post-init-company ()
|
Enable company in Python REPL for code completion
- Company should be enabled in Python REPL for displaying completion
candidates. Note that in Python REPL, we must use company-capf to
display the candidates from Python REPL, since in the REPL it uses
completion-at-point to display candidates from the REPL. Also, we
locally set company-minimum-prefix-length to 0, so that completion
starts immediately after the first character. We also need to set the
idle delay a bit higher (0.5) compared with Spacemacs default (0.2),
since setting it too low may cause company unable to retrieve completion
candidates.
- Anaconda should NOT be enabled in Python REPL for retrieving proper
completion candidates. This can be tested in Python REPL: when
anaconda-mode is enabled and without any prefix string before point, run
the command completion-at-point (bound to C-M-i by default), completion
candidates from Anaconda are displayed but NOT the ones from the real
REPL. So, when you import a module i.e. os and try to access the
functions in the module with "os." prefix, nothing inside the module is
displayed because Anaconda does not work in Python REPL. When
anaconda-mode is disabled, candidates in "os." are displayed properly
with company-capf. company-anaconda backend cannot be used in Python
REPL since it is buggy: it cannot retrieve any prefix before point and
narrow properly.
2015-05-21 05:39:57 +00:00
|
|
|
(spacemacs|add-company-hook python-mode)
|
|
|
|
(spacemacs|add-company-hook inferior-python-mode)
|
|
|
|
(push 'company-capf company-backends-inferior-python-mode)
|
|
|
|
(add-hook 'inferior-python-mode-hook (lambda ()
|
|
|
|
(setq-local company-minimum-prefix-length 0)
|
|
|
|
(setq-local company-idle-delay 0.5))))
|
2015-04-03 21:12:56 +00:00
|
|
|
|
|
|
|
(defun python/init-company-anaconda ()
|
|
|
|
(use-package company-anaconda
|
2015-04-09 03:57:22 +00:00
|
|
|
:if (configuration-layer/package-usedp 'company)
|
2015-04-03 21:12:56 +00:00
|
|
|
:defer t
|
|
|
|
:init
|
2015-04-30 03:54:30 +00:00
|
|
|
(push 'company-anaconda company-backends-python-mode))))
|
2015-04-16 10:43:10 +00:00
|
|
|
|
2015-04-23 14:35:04 +00:00
|
|
|
(defun python/post-init-semantic ()
|
2015-05-21 14:18:02 +00:00
|
|
|
(semantic/enable-semantic-mode 'python-mode)
|
|
|
|
(defadvice semantic-python-get-system-include-path (around semantic-python-skip-error-advice activate)
|
|
|
|
"Don't cause error when Semantic cannot retrieve include
|
|
|
|
paths for Python then prevent the buffer to be switched. This
|
|
|
|
issue might be fixed in Emacs 25. Until then, we need it here to
|
|
|
|
fix this issue."
|
|
|
|
(condition-case nil
|
|
|
|
ad-do-it
|
|
|
|
(error nil))))
|
2015-04-23 14:35:04 +00:00
|
|
|
|
2015-04-16 10:43:10 +00:00
|
|
|
(defun python/post-init-stickyfunc-enhance ()
|
|
|
|
(add-hook 'python-mode-hook 'spacemacs/lazy-load-stickyfunc-enhance))
|
2015-07-24 02:20:25 +00:00
|
|
|
|
|
|
|
(defun python/pre-init-xcscope ()
|
|
|
|
(spacemacs|use-package-add-hook xcscope
|
|
|
|
:post-init
|
|
|
|
(evil-leader/set-key-for-mode 'python-mode "mgi" 'cscope/run-pycscope)))
|
|
|
|
|
|
|
|
(defun python/pre-init-helm-cscope ()
|
|
|
|
(spacemacs|use-package-add-hook xcscope
|
|
|
|
:post-init
|
|
|
|
(spacemacs/setup-helm-cscope 'python-mode)))
|