2017-06-08 11:47:56 +02:00
|
|
|
;;; lang/python/config.el -*- lexical-binding: t; -*-
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2017-09-26 01:02:42 +02:00
|
|
|
(defvar +python-pyenv-root nil
|
|
|
|
"The path to pyenv's root directory. This is automatically set when `python'
|
|
|
|
is loaded.")
|
|
|
|
|
|
|
|
(defvar +python-pyenv-versions nil
|
|
|
|
"Available versions of python in pyenv.")
|
|
|
|
|
|
|
|
(defvar-local +python-current-version nil
|
|
|
|
"The currently active pyenv version.")
|
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Plugins
|
|
|
|
;;
|
|
|
|
|
2017-02-23 00:06:12 -05:00
|
|
|
(def-package! python
|
2018-05-25 00:46:11 +02:00
|
|
|
:defer t
|
2015-06-15 09:06:10 +02:00
|
|
|
:init
|
2017-02-19 18:57:16 -05:00
|
|
|
(setq python-environment-directory doom-cache-dir
|
2017-02-27 20:52:50 -05:00
|
|
|
python-indent-guess-indent-offset-verbose nil
|
2017-04-22 17:08:26 -04:00
|
|
|
python-shell-interpreter "python")
|
2016-05-01 01:10:30 -04:00
|
|
|
:config
|
2017-06-07 14:36:24 +02:00
|
|
|
(add-hook! 'python-mode-hook #'(flycheck-mode highlight-numbers-mode))
|
|
|
|
|
2018-06-15 03:15:34 +02:00
|
|
|
(set-env! "PYTHONPATH" "PYENV_ROOT")
|
2018-06-15 12:30:56 +02:00
|
|
|
(set-electric! 'python-mode :chars '(?:))
|
2018-06-15 16:07:24 +02:00
|
|
|
(set-repl-handler! 'python-mode #'+python/repl)
|
2017-03-04 20:51:35 -05:00
|
|
|
|
2017-04-22 17:08:26 -04:00
|
|
|
(when (executable-find "ipython")
|
|
|
|
(setq python-shell-interpreter "ipython"
|
|
|
|
python-shell-interpreter-args "-i --simple-prompt --no-color-info"
|
|
|
|
python-shell-prompt-regexp "In \\[[0-9]+\\]: "
|
|
|
|
python-shell-prompt-block-regexp "\\.\\.\\.\\.: "
|
|
|
|
python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
|
|
|
|
python-shell-completion-setup-code
|
|
|
|
"from IPython.core.completerlib import module_completion"
|
|
|
|
python-shell-completion-string-code
|
|
|
|
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n"))
|
|
|
|
|
2017-09-26 01:02:42 +02:00
|
|
|
;; Version management with pyenv
|
|
|
|
(defun +python|add-version-to-modeline ()
|
|
|
|
"Add version string to the major mode in the modeline."
|
|
|
|
(setq mode-name
|
|
|
|
(if +python-current-version
|
|
|
|
(format "Python %s" +python-current-version)
|
|
|
|
"Python")))
|
|
|
|
(add-hook 'python-mode-hook #'+python|add-version-to-modeline)
|
|
|
|
|
|
|
|
(if (not (executable-find "pyenv"))
|
|
|
|
(setq +python-current-version (string-trim (shell-command-to-string "python --version 2>&1 | cut -d' ' -f2")))
|
|
|
|
(setq +python-pyenv-root (string-trim (shell-command-to-string "pyenv root"))
|
|
|
|
+python-pyenv-versions (split-string (shell-command-to-string "pyenv versions --bare") "\n" t))
|
|
|
|
|
|
|
|
(defun +python|detect-pyenv-version ()
|
|
|
|
"Detect the pyenv version for the current project and set the relevant
|
|
|
|
environment variables."
|
2017-12-10 14:49:52 -05:00
|
|
|
(when-let* ((version-str (shell-command-to-string "python --version 2>&1 | cut -d' ' -f2")))
|
2017-09-26 01:02:42 +02:00
|
|
|
(setq version-str (string-trim version-str)
|
|
|
|
+python-current-version version-str)
|
|
|
|
(let ((pyenv-current-path (concat +python-pyenv-root "/versions/" version-str)))
|
|
|
|
(when (file-directory-p pyenv-current-path)
|
|
|
|
(setq pythonic-environment pyenv-current-path)))
|
|
|
|
(when (member version-str +python-pyenv-versions)
|
|
|
|
(setenv "PYENV_VERSION" version-str))))
|
|
|
|
(add-hook 'python-mode-hook #'+python|detect-pyenv-version))
|
|
|
|
|
2017-08-08 14:24:48 +02:00
|
|
|
(define-key python-mode-map (kbd "DEL") nil) ; interferes with smartparens
|
2017-03-04 20:51:35 -05:00
|
|
|
(sp-with-modes 'python-mode
|
|
|
|
(sp-local-pair "'" nil :unless '(sp-point-before-word-p sp-point-after-word-p sp-point-before-same-p))))
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2017-02-19 18:57:16 -05:00
|
|
|
|
2017-02-23 00:06:12 -05:00
|
|
|
(def-package! anaconda-mode
|
2018-06-01 12:19:11 +02:00
|
|
|
:after python
|
2016-04-23 22:08:46 -04:00
|
|
|
:init
|
2017-03-16 23:38:22 -04:00
|
|
|
(setq anaconda-mode-installation-directory (concat doom-etc-dir "anaconda/")
|
2016-04-23 22:08:46 -04:00
|
|
|
anaconda-mode-eldoc-as-single-line t)
|
|
|
|
:config
|
2018-06-01 12:19:11 +02:00
|
|
|
(add-hook 'python-mode-hook #'anaconda-mode)
|
2017-12-08 22:33:12 -05:00
|
|
|
(add-hook 'anaconda-mode-hook #'anaconda-eldoc-mode)
|
2018-06-15 02:58:12 +02:00
|
|
|
(set-company-backend! 'python-mode '(company-anaconda))
|
|
|
|
(set-popup-rule! "^\\*anaconda-mode" nil '((select)))
|
2018-06-15 17:27:48 +02:00
|
|
|
(set-lookup-handlers! 'python-mode
|
2018-01-24 03:16:36 -05:00
|
|
|
:definition #'anaconda-mode-find-definitions
|
|
|
|
:references #'anaconda-mode-find-references
|
|
|
|
:documentation #'anaconda-mode-show-doc)
|
2018-01-31 05:32:05 -05:00
|
|
|
|
|
|
|
(defun +python|auto-kill-anaconda-processes ()
|
|
|
|
"Kill anaconda processes if this buffer is the last python buffer."
|
|
|
|
(when (and (eq major-mode 'python-mode)
|
|
|
|
(not (delq (current-buffer)
|
|
|
|
(doom-buffers-in-mode 'python-mode (buffer-list)))))
|
|
|
|
(anaconda-mode-stop)))
|
|
|
|
(add-hook! 'python-mode-hook
|
2018-05-31 11:31:32 +02:00
|
|
|
(add-hook 'kill-buffer-hook #'+python|auto-kill-anaconda-processes nil t))
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2018-03-23 15:18:52 -04:00
|
|
|
(map! :map anaconda-mode-map
|
2017-02-27 20:52:31 -05:00
|
|
|
:localleader
|
2017-02-28 12:11:18 -05:00
|
|
|
:prefix "f"
|
2017-04-17 02:17:10 -04:00
|
|
|
:nv "d" #'anaconda-mode-find-definitions
|
|
|
|
:nv "h" #'anaconda-mode-show-doc
|
|
|
|
:nv "a" #'anaconda-mode-find-assignments
|
|
|
|
:nv "f" #'anaconda-mode-find-file
|
|
|
|
:nv "u" #'anaconda-mode-find-references))
|
2017-02-19 18:57:16 -05:00
|
|
|
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2017-02-23 00:06:12 -05:00
|
|
|
(def-package! nose
|
2016-04-04 12:06:47 -04:00
|
|
|
:commands nose-mode
|
2017-02-19 18:57:16 -05:00
|
|
|
:preface
|
|
|
|
(defvar nose-mode-map (make-sparse-keymap))
|
|
|
|
:init
|
2017-03-02 18:14:52 -05:00
|
|
|
(associate! nose-mode :match "/test_.+\\.py$" :modes (python-mode))
|
2016-04-04 12:06:47 -04:00
|
|
|
:config
|
2018-06-15 02:58:12 +02:00
|
|
|
(set-popup-rule! "^\\*nosetests" '((size . 0.4)) '((select)))
|
2018-06-15 18:03:11 +02:00
|
|
|
(set-yas-minor-mode! 'nose-mode)
|
2017-02-23 00:06:12 -05:00
|
|
|
(map! :map nose-mode-map
|
2017-02-19 18:57:16 -05:00
|
|
|
:localleader
|
2017-02-28 12:11:18 -05:00
|
|
|
:prefix "t"
|
2017-04-17 02:17:10 -04:00
|
|
|
:n "r" #'nosetests-again
|
|
|
|
:n "a" #'nosetests-all
|
|
|
|
:n "s" #'nosetests-one
|
|
|
|
:n "v" #'nosetests-module
|
|
|
|
:n "A" #'nosetests-pdb-all
|
|
|
|
:n "O" #'nosetests-pdb-one
|
|
|
|
:n "V" #'nosetests-pdb-module))
|
2016-04-04 12:06:47 -04:00
|
|
|
|
2018-05-31 11:32:26 +02:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Evil integration
|
|
|
|
;;
|
|
|
|
|
|
|
|
(when (featurep! :feature evil +everywhere)
|
2018-06-03 15:51:59 +02:00
|
|
|
(add-hook! '(anaconda-mode-hook nose-mode-hook)
|
|
|
|
#'evil-normalize-keymaps))
|