From dc9f328c2238562886fad8993ba671e6f2f972db Mon Sep 17 00:00:00 2001
From: Liu Miao <liumiaogemini@foxmail.com>
Date: Tue, 15 Jun 2021 02:34:48 +0800
Subject: [PATCH] more complete python configurations

---
 lisp/prog-python.el | 54 +++++++++++++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 19 deletions(-)

diff --git a/lisp/prog-python.el b/lisp/prog-python.el
index ad7f785..3fb5bbf 100644
--- a/lisp/prog-python.el
+++ b/lisp/prog-python.el
@@ -1,35 +1,51 @@
+;;; prog-python.el --- Configuration for python.
+;;; Commentary:
 
 ;;; Code:
+(defvar python/pyvenv-modes nil)
 
-(defun python/pyenv-executable-find (command)
-  "Find executable taking pyenv shims into account."
-  ())
-(defun python/execute-file (args)
-  "Execute a python script in a shell."
-  (interactive "P")
-  (let ((universal-argument t)
-        (compile-command (format "%s %s")))))
+(defun python/pyvenv-set-local-virtualenv ()
+  "Set pyvenv virtualenv from \".venv\" by looking in parent directories."
+  (interactive)
+  (let ((root-path (locate-dominating-file default-directory ".venv")))
+    (when root-path
+      (let ((file-path (expand-file-name ".venv" root-path)))
+        (cond ((file-directory-p file-path)
+               (pyvenv-activate file-path)
+               (message "Activated local virtualenv"))
+              (t (message ".venv is not a directory")))))))
 
-(use-package importmagic
+(use-package python
   :defer t
-  :ensure t
-  :diminish importmagic-mode
-  :init
-  (add-hook 'python-mode-hook 'importmagic-mode))
+  :ensure nil
+  :after flycheck
+  :mode "\\.py\\'"
+  :custom
+  (python-indent-offset 4)
+  (flycheck-python-pycompile-executable "python3")
+  :config
+  (setq python-shell-interpreter "python3"))
 
-(use-package pipenv
-  :defer t
+(use-package lsp-pyright
   :ensure t
-  :init
-  (add-hook 'python-mode-hook #'pyvenv-tracking-mode))
+  :hook (python-mode . (lambda () (require 'lsp-pyright)))
+  :init (when (executable-find "python3")
+          (setq lsp-pyright-python-executable-cmd "python3")))
 
 (use-package yapfify
   :ensure t
   :defer t
   :hook (python-mode . yapf-mode))
 
-(use-package python
-  :defer t)
+(use-package pyvenv
+  :ensure t
+  :init
+  (add-hook 'python-mode-hook #'pyvenv-tracking-mode)
+  (add-to-list 'python/pyvenv-modes 'python-mode)
+  ;; Set for auto active virtual env
+  (dolist (m python/pyvenv-modes)
+    (add-hook (intern (format "%s-hook" m))
+              'python/pyvenv-set-local-virtualenv())))
 
 (provide 'prog-python)
 ;;; prog-python.el ends here
-- 
GitLab