From 79fbaac739ee3401245d90fe0f4c2ce7de155e27 Mon Sep 17 00:00:00 2001 From: Taylor Bockman Date: Sat, 22 Aug 2020 21:28:33 -0700 Subject: [PATCH] Make python play nice with emacs --- dotfiles/emacs.d/init.el | 64 ++++++++++++++++++++++++++++++++++++++++++--- dotfiles/emacs.d/package.el | 7 +++-- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/dotfiles/emacs.d/init.el b/dotfiles/emacs.d/init.el index 1976eb5..3903f38 100644 --- a/dotfiles/emacs.d/init.el +++ b/dotfiles/emacs.d/init.el @@ -70,6 +70,13 @@ There are two things you can do about this warning: (set-buffer-file-coding-system 'unix) ;;-------------------------------------------------------------------------------------- +;; Populate emacs path to $PATH when on *nix or OS X +;;-------------------------------------------------------------------------------------- + +(when (memq window-system '(mac ns x)) + (exec-path-from-shell-initialize)) + +;;-------------------------------------------------------------------------------------- ;; Enable auto-refresh to keep buffers up to date when git or another program ;; modifies them ;;-------------------------------------------------------------------------------------- @@ -184,19 +191,70 @@ There are two things you can do about this warning: ;; Enable semantic for language-aware editing commands (setq semantic-mode 't) + +;; Enable company mode +(add-hook 'after-init-hook 'global-company-mode) + +;; Globally enable flycheck +(add-hook 'after-init-hook #'global-flycheck-mode) + +;;;;; PYTHON ;;;;;;;;;;;;;;;;;; +;; +;; NOTES: +;; +;; - For each project you will need to install flake8/autopep8 into the virtualenv +;; - For each project you will need to install jedi into the virtualenv +;; - To set your pyenv use M-x pyenv-mode and select your local virtualenv +;; +;; + +;; thx rakan.me +(defun pyenv-activate-current-project () + "Automatically activates pyenv version if .python-version file exists." + (interactive) + (f-traverse-upwards + (lambda (path) + (message path) + (let ((pyenv-version-path (f-expand ".python-version" path))) + (if (f-exists? pyenv-version-path) + (let ((pyenv-current-version (s-trim (f-read-text pyenv-version-path 'utf-8)))) + (pyenv-mode-set pyenv-current-version) + (message (concat "Setting virtualenv to " pyenv-current-version)))))))) + ;; For Python enable Elpy. (use-package elpy - :ensure t - :init (elpy-enable)) + :ensure t + :init (elpy-enable)) + +(setq elpy-rpc-backend "jedi") ;; Enable pyenv integration. (use-package pyenv-mode - :config + :config (pyenv-mode)) ;; Disable elpy default virtualenv (use the pyenv one) (setq elpy-rpc-virtualenv-path 'current) +;; Enable autopep8 + +(require 'py-autopep8) + +(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save) + +;; Enable flake8 - requires flake8 in your path so use pyenv/global pip install +(add-hook 'python-mode-hook '(lambda () + (setq flycheck-python-flake8-executable "flake8") + (flycheck-select-checker 'python-flake8) + (flycheck-mode t))) + +;; Activate pyenv automatically if a .python-version is supplied +(add-hook 'python-mode-hook 'pyenv-activate-current-project) + +;; Add company-jedi hook to python mode +(add-hook 'python-mode-hook (lambda () + (add-to-list 'company-backends 'company-jedi))) + ;;-------------------------------------------------------------------------------------- ;; Save hooks ;;-------------------------------------------------------------------------------------- diff --git a/dotfiles/emacs.d/package.el b/dotfiles/emacs.d/package.el index 35aecf0..0db17c4 100644 --- a/dotfiles/emacs.d/package.el +++ b/dotfiles/emacs.d/package.el @@ -82,6 +82,9 @@ ;; Environments pyenv-mode + ;; Python specific stuff + py-autopep8 + ;;General use-package @@ -92,8 +95,8 @@ (defun my-packages-installed-p () (loop for p in my-packages - when (not (package-installed-p p)) do (return nil) - finally (return t))) + when (not (package-installed-p p)) do (return nil) + finally (return t))) (unless (my-packages-installed-p) ;; check for new packages (package versions)