From 20eba0d77d34c4b25a729408c54899a98e7c5d10 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Sat, 6 Jul 2024 22:12:45 +0800 Subject: [PATCH] [feat] add trace-python=virtualenv func --- config.org | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/config.org b/config.org index e7dbd4f..4f6dc4b 100644 --- a/config.org +++ b/config.org @@ -577,3 +577,29 @@ (add-hook 'emacs-lisp-mode-hook 'enable-paredit-mode) (add-hook 'cider-repl-mode-hook 'enable-paredit-mode)) #+end_src + +* Python +自动切换虚拟环境, poetry 包可能在处理远端机器的项目可能会有问题所以从那边抠出来放到这里。 + +#+begin_src emacs-lisp +(defvar poetry-venv-list '() + "List of known poetry virtualenvs.") + +(defun my/track-python-virtualenv (&optional _) + (interactive) + (when (and buffer-file-name + (string= nil (file-remote-p default-directory))) + (cond ((locate-dominating-file default-directory "pyproject.toml") + (if-let ((poetry-current-project-venv (cdr (assq '(locate-dominating-file default-directory "pyproject.toml") poetry-venv-list)))) + (when (not (equal python-shell-virtualenv-path poetry-current-project-venv)) + (pyvenv-activate poetry-current-project-venv)) + (let ((poetry-project-path (locate-dominating-file default-directory "pyproject.toml")) + (poetry-venv-path (s-trim (shell-command-to-string "env -u VIRTUAL_ENV poetry env info -p")))) + (pyvenv-activate poetry-venv-path) + (add-to-list 'poetry-venv-list (cons poetry-project-path poetry-venv-path))))) + ((locate-dominating-file default-directory ".venv") + (pyvenv-activate (concat (locate-dominating-file default-directory ".venv") ".venv"))) + (t (pyvenv-deactivate))))) + +(add-to-list 'window-buffer-change-functions 'my/track-python-virtualenv) +#+end_src