[feat] add trace-python=virtualenv func

This commit is contained in:
SouthFox 2024-07-06 22:12:45 +08:00
parent 54c44551e8
commit 20eba0d77d

View file

@ -577,3 +577,29 @@
(add-hook 'emacs-lisp-mode-hook 'enable-paredit-mode) (add-hook 'emacs-lisp-mode-hook 'enable-paredit-mode)
(add-hook 'cider-repl-mode-hook 'enable-paredit-mode)) (add-hook 'cider-repl-mode-hook 'enable-paredit-mode))
#+end_src #+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