2017-06-08 11:47:56 +02:00
|
|
|
;;; lang/python/config.el -*- lexical-binding: t; -*-
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2018-08-01 01:13:49 +02:00
|
|
|
(defvar +python-mode-line-indicator
|
|
|
|
'("Python" (+python-version (" " +python-version)))
|
|
|
|
"Format for the python version/env indicator in the mode-line.")
|
|
|
|
|
|
|
|
(defvar-local +python-version nil
|
|
|
|
"The python version in the current buffer.")
|
2017-09-26 01:02:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; 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
|
2018-08-01 22:30:30 +02:00
|
|
|
(set-env! "PYTHONPATH" "PYENV_ROOT" "ANACONDA_HOME")
|
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
|
|
|
|
2018-06-16 19:32:25 +02:00
|
|
|
(set-pretty-symbols! 'python-mode
|
|
|
|
;; Functional
|
|
|
|
:def "def"
|
|
|
|
:lambda "lambda"
|
|
|
|
;; Types
|
|
|
|
:null "None"
|
|
|
|
:true "True" :false "False"
|
|
|
|
:int "int" :str "str"
|
|
|
|
:float "float"
|
|
|
|
:bool "bool"
|
|
|
|
:tuple "tuple"
|
|
|
|
;; Flow
|
|
|
|
:not "not"
|
|
|
|
:in "in" :not-in "not in"
|
|
|
|
:and "and" :or "or"
|
|
|
|
:for "for"
|
|
|
|
:return "return" :yield "yield")
|
|
|
|
|
2018-07-31 14:02:34 +02:00
|
|
|
(define-key python-mode-map (kbd "DEL") nil) ; interferes with smartparens
|
|
|
|
(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)))
|
|
|
|
|
|
|
|
(when (featurep! +ipython)
|
2017-04-22 17:08:26 -04:00
|
|
|
(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"))
|
|
|
|
|
2018-08-01 15:16:04 +02:00
|
|
|
;; Add python/pipenv version string to the major mode in the modeline
|
|
|
|
(defun +python|adjust-mode-line ()
|
|
|
|
(setq mode-name +python-mode-line-indicator))
|
|
|
|
(add-hook 'python-mode-hook #'+python|adjust-mode-line)
|
2018-08-01 01:13:49 +02:00
|
|
|
|
2018-08-01 15:16:04 +02:00
|
|
|
(defun +python|update-version (&rest _)
|
|
|
|
(setq +python-version (+python-version)))
|
|
|
|
(+python|update-version)
|
|
|
|
(add-hook 'python-mode-hook #'+python|update-version))
|
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-07-31 14:02:34 +02:00
|
|
|
:hook python-mode
|
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
|
2017-12-08 22:33:12 -05:00
|
|
|
(add-hook 'anaconda-mode-hook #'anaconda-eldoc-mode)
|
2018-07-31 19:20:58 +02:00
|
|
|
(set-company-backend! 'anaconda-mode '(company-anaconda))
|
|
|
|
(set-lookup-handlers! 'anaconda-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-07-31 19:20:58 +02:00
|
|
|
(set-popup-rule! "^\\*anaconda-mode" :select nil)
|
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-07-31 14:02:34 +02:00
|
|
|
(when (featurep 'evil)
|
|
|
|
(add-hook 'anaconda-mode-hook #'evil-normalize-keymaps))
|
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
|
2018-07-31 14:02:34 +02:00
|
|
|
:preface (defvar nose-mode-map (make-sparse-keymap))
|
|
|
|
:init (associate! nose-mode :match "/test_.+\\.py$" :modes (python-mode))
|
2016-04-04 12:06:47 -04:00
|
|
|
:config
|
2018-06-18 02:26:05 +02:00
|
|
|
(set-popup-rule! "^\\*nosetests" :size 0.4 :select nil)
|
2018-06-15 18:03:11 +02:00
|
|
|
(set-yas-minor-mode! 'nose-mode)
|
2018-07-31 14:02:34 +02:00
|
|
|
(when (featurep 'evil)
|
|
|
|
(add-hook 'nose-mode-hook #'evil-normalize-keymaps))
|
|
|
|
|
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
|
|
|
|
|
|
|
;;
|
2018-07-31 14:02:34 +02:00
|
|
|
;; Environment management
|
2018-05-31 11:32:26 +02:00
|
|
|
;;
|
|
|
|
|
2018-07-31 14:02:34 +02:00
|
|
|
(def-package! pipenv
|
|
|
|
:commands pipenv-project-p
|
2018-08-11 21:18:21 +02:00
|
|
|
:hook (python-mode . pipenv-mode)
|
|
|
|
:init (setq pipenv-with-projectile nil)
|
|
|
|
:config
|
|
|
|
(advice-add #'pipenv-activate :after-while #'+python|update-version)
|
|
|
|
(advice-add #'pipenv-deactivate :after-while #'+python|update-version))
|
2018-07-31 14:02:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
(def-package! pyenv-mode
|
|
|
|
:when (featurep! +pyenv)
|
|
|
|
:after python
|
|
|
|
:config
|
|
|
|
(pyenv-mode +1)
|
2018-08-01 01:13:49 +02:00
|
|
|
(advice-add #'pyenv-mode-set :after #'+python|update-version)
|
|
|
|
(advice-add #'pyenv-mode-unset :after #'+python|update-version)
|
2018-08-01 15:16:04 +02:00
|
|
|
(add-to-list '+python-mode-line-indicator
|
|
|
|
'(:eval (if (pyenv-mode-version) (concat " pyenv:" (pyenv-mode-version))))
|
|
|
|
'append))
|
2018-07-31 14:02:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
(def-package! pyvenv
|
|
|
|
:when (featurep! +pyvenv)
|
|
|
|
:after python
|
|
|
|
:config
|
|
|
|
(defun +python-current-pyvenv () pyvenv-virtual-env-name)
|
2018-07-31 23:07:22 +02:00
|
|
|
(add-hook 'pyvenv-post-activate-hooks #'+python|update-version)
|
|
|
|
(add-hook 'pyvenv-post-deactivate-hooks #'+python|update-version)
|
2018-08-01 01:31:12 +02:00
|
|
|
(add-to-list '+python-mode-line-indicator '(pyvenv-virtual-env-name (" venv:" pyvenv-virtual-env-name)) 'append))
|
2018-07-31 14:02:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
(def-package! conda
|
|
|
|
:when (featurep! +conda)
|
|
|
|
:after python
|
|
|
|
:config
|
2018-07-31 23:07:22 +02:00
|
|
|
;; The location of your anaconda home will be guessed from the following:
|
2018-07-31 14:02:34 +02:00
|
|
|
;;
|
2018-07-31 23:07:22 +02:00
|
|
|
;; + ANACONDA_HOME
|
2018-07-31 14:02:34 +02:00
|
|
|
;; + ~/.anaconda3
|
|
|
|
;; + ~/.anaconda
|
|
|
|
;; + ~/.miniconda
|
|
|
|
;; + ~/usr/bin/anaconda3
|
|
|
|
;;
|
2018-07-31 23:07:22 +02:00
|
|
|
;; If none of these work for you, you must set `conda-anaconda-home'
|
|
|
|
;; explicitly. Once set, run M-x `conda-env-activate' to switch between
|
|
|
|
;; environments
|
2018-07-31 14:02:34 +02:00
|
|
|
(unless (cl-loop for dir in (list conda-anaconda-home
|
|
|
|
"~/.anaconda"
|
|
|
|
"~/.miniconda"
|
|
|
|
"/usr/bin/anaconda3")
|
|
|
|
if (file-directory-p dir)
|
|
|
|
return (setq conda-anaconda-home dir
|
|
|
|
conda-env-home-directory dir))
|
|
|
|
(message "Cannot find Anaconda installation"))
|
|
|
|
|
|
|
|
;; integration with term/eshell
|
|
|
|
(conda-env-initialize-interactive-shells)
|
|
|
|
(after! eshell (conda-env-initialize-eshell))
|
|
|
|
|
2018-07-31 23:07:22 +02:00
|
|
|
(add-hook 'conda-postactivate-hook #'+python|update-version)
|
|
|
|
(add-hook 'conda-postdeactivate-hook #'+python|update-version)
|
2018-08-01 15:16:04 +02:00
|
|
|
(add-to-list '+python-mode-line-indicator
|
|
|
|
'(conda-env-current-name
|
|
|
|
(" conda:" conda-env-current-name))
|
2018-08-01 22:29:51 +02:00
|
|
|
'append))
|