doomemacs/modules/lang/python/config.el

207 lines
6.8 KiB
EmacsLisp
Raw Normal View History

;;; lang/python/config.el -*- lexical-binding: t; -*-
2015-06-15 09:06:10 +02:00
(defconst +python-mode-line-indicator '("" +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.")
;;
;; Packages
(def-package! python
: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
python-indent-guess-indent-offset-verbose nil)
:config
(set-env! "PYTHONPATH" "PYENV_ROOT" "ANACONDA_HOME")
(set-electric! 'python-mode :chars '(?:))
(set-repl-handler! 'python-mode #'+python/repl)
(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")
(define-key python-mode-map (kbd "DEL") nil) ; interferes with smartparens
2018-10-06 00:27:52 -04:00
(sp-local-pair 'python-mode "'" nil
:unless '(sp-point-before-word-p
sp-point-after-word-p
sp-point-before-same-p))
(setq-hook! 'python-mode-hook tab-width python-indent-offset)
;; 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)
(add-hook 'python-mode-hook #'+python|update-version))
2015-06-15 09:06:10 +02:00
2017-02-19 18:57:16 -05:00
(def-package! anaconda-mode
:hook python-mode
:init
(setq anaconda-mode-installation-directory (concat doom-etc-dir "anaconda/")
anaconda-mode-eldoc-as-single-line t)
:config
(add-hook 'anaconda-mode-hook #'anaconda-eldoc-mode)
(set-company-backend! 'anaconda-mode '(company-anaconda))
(set-lookup-handlers! 'anaconda-mode
:definition #'anaconda-mode-find-definitions
:references #'anaconda-mode-find-references
:documentation #'anaconda-mode-show-doc)
(set-popup-rule! "^\\*anaconda-mode" :select nil)
(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
(add-hook 'kill-buffer-hook #'+python|auto-kill-anaconda-processes nil t))
2015-06-15 09:06:10 +02:00
(when (featurep 'evil)
(add-hook 'anaconda-mode-hook #'evil-normalize-keymaps))
(map! :map anaconda-mode-map
:localleader
:prefix "f"
: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
(def-package! nose
2016-04-04 12:06:47 -04:00
:commands nose-mode
: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
(set-popup-rule! "^\\*nosetests" :size 0.4 :select nil)
(set-yas-minor-mode! 'nose-mode)
(when (featurep 'evil)
(add-hook 'nose-mode-hook #'evil-normalize-keymaps))
(map! :map nose-mode-map
2017-02-19 18:57:16 -05:00
:localleader
:prefix "t"
: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-11-22 12:53:08 -06:00
(def-package! python-pytest
2018-12-03 23:15:40 -05:00
:init
(map! :after python
:map python-mode-map
2018-11-22 12:53:08 -06:00
:localleader
:prefix "t"
2018-11-23 11:14:25 -06:00
:nv "f" #'python-pytest-file
:nv "k" #'python-pytest-file-dwim
:nv "m" #'python-pytest-repeat
:nv "p" #'python-pytest-popup))
;;
;; Environment management
(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
(set-eval-handler! 'python-mode
'((:command . (lambda () python-shell-interpreter))
(:exec (lambda ()
(if-let* ((bin (executable-find "pipenv"))
(_ (pipenv-project-p)))
(format "PIPENV_MAX_DEPTH=9999 %s run %%c %%o %%s %%a" bin)
"%c %o %s %a")))
(:description . "Run Python script")))
(advice-add #'pipenv-activate :after-while #'+python|update-version-in-all-buffers)
(advice-add #'pipenv-deactivate :after-while #'+python|update-version-in-all-buffers))
(def-package! pyenv-mode
:when (featurep! +pyenv)
:after python
:config
(pyenv-mode +1)
(when (executable-find "pyenv")
(add-to-list 'exec-path (expand-file-name "shims" (or (getenv "PYENV_ROOT") "~/.pyenv"))))
(advice-add #'pyenv-mode-set :after #'+python|update-version-in-all-buffers)
(advice-add #'pyenv-mode-unset :after #'+python|update-version-in-all-buffers))
(def-package! pyvenv
:when (featurep! +pyvenv)
:after python
:config
(defun +python-current-pyvenv () pyvenv-virtual-env-name)
(add-hook 'pyvenv-post-activate-hooks #'+python|update-version-in-all-buffers)
(add-hook 'pyvenv-post-deactivate-hooks #'+python|update-version-in-all-buffers)
2018-08-12 02:46:55 +02:00
(add-to-list '+python-mode-line-indicator
'(pyvenv-virtual-env-name (" venv:" pyvenv-virtual-env-name))
'append))
(def-package! conda
:when (featurep! +conda)
:after python
:config
;; The location of your anaconda home will be guessed from the following:
;;
;; + `conda-anaconda-home's default value:
;; + ANACONDA_HOME
;; + ~/.anaconda3
;; + ~/.anaconda
;; + ~/.miniconda
;; + ~/usr/bin/anaconda3
;; + ~/usr/local/anaconda3
;; + ~/usr/local/miniconda3
;;
;; 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
(unless (cl-loop for dir in (list conda-anaconda-home
"~/.anaconda"
"~/.miniconda"
2018-11-14 22:33:18 -08:00
"~/.miniconda3"
"/usr/bin/anaconda3"
"/usr/local/anaconda3"
"/usr/local/miniconda3")
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))
(add-hook 'conda-postactivate-hook #'+python|update-version-in-all-buffers)
(add-hook 'conda-postdeactivate-hook #'+python|update-version-in-all-buffers)
(add-to-list '+python-mode-line-indicator
2018-08-12 02:46:55 +02:00
'(conda-env-current-name (" conda:" conda-env-current-name))
'append))