-
Notifications
You must be signed in to change notification settings - Fork 1
/
uv-mode.el
117 lines (101 loc) · 3.77 KB
/
uv-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;;; uv-mode.el --- Integrate uv with python-mode -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2024 z80
;;
;; Author: z80 <[email protected]>
;; Maintainer: z80 <[email protected]>
;; Created: December 13, 2024
;; Modified: December 13, 2024
;; Version: 0.0.1
;; Homepage: https://github.com/z80/uv-mode
;; Package-Requires: ((emacs "25.1") (pythonic "0.1.0"))
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; UV mode provides integration between uv (the Python package installer)
;; and Emacs python-mode. Unlike pyenv which manages multiple Python versions,
;; this mode focuses on managing project-specific virtual environments created by uv.
;;; Code:
(require 'pythonic)
(defgroup uv nil
"UV virtualenv integration with python mode."
:group 'languages)
(defcustom uv-mode-mode-line-format
'(:eval
(when (uv-mode-version)
(concat "UV:" (uv-mode-version) " ")))
"How `uv-mode' will indicate the current virtual environment in the mode line."
:type 'string
:group 'uv)
(defun uv-mode-version ()
"Return currently active virtual environment name.
This looks for a .venv directory in the project root."
(let ((project-root (locate-dominating-file default-directory ".venv")))
(when project-root
(file-name-nondirectory (directory-file-name project-root)))))
(defun uv-mode-root ()
"Return the root directory of the current project with a .venv."
(locate-dominating-file default-directory ".venv"))
(defun uv-mode-full-path (project-dir)
"Return full path for virtualenv in PROJECT-DIR."
(when project-dir
(expand-file-name ".venv" project-dir)))
;;;###autoload
(defun uv-mode-set ()
"Set python shell virtualenv for PROJECT."
(interactive)
(let ((venv-path (uv-mode-full-path (uv-mode-root))))
(when venv-path
(pythonic-activate venv-path)
(setenv "VIRTUAL_ENV" venv-path)
(force-mode-line-update))))
;;;###autoload
(defun uv-mode-unset ()
"Unset python shell virtualenv."
(interactive)
(pythonic-deactivate)
(setenv "VIRTUAL_ENV")
(force-mode-line-update))
(defvar uv-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-s") 'uv-mode-set)
(define-key map (kbd "C-c C-u") 'uv-mode-unset)
map)
"Keymap for `uv-mode'.")
;;;###autoload
(define-minor-mode uv-mode
"Minor mode for uv virtualenv interaction.
\\{uv-mode-map}"
:global t
:lighter ""
:keymap uv-mode-map
(if uv-mode
(if (executable-find "uv")
(add-to-list 'mode-line-misc-info uv-mode-mode-line-format)
(error "Uv-mode: uv executable not found"))
(setq mode-line-misc-info
(delete uv-mode-mode-line-format mode-line-misc-info))))
;;;###autoload
(defun uv-mode-auto-activate-hook ()
"Automatically activate UV mode and the project's virtualenv if available.
This function checks for a .venv directory in the project root. If found,
it activates that virtualenv for the current buffer. This is especially
useful when automatically run via `python-mode-hook'."
(when (derived-mode-p 'python-mode)
(let ((project-root (uv-mode-root)))
(when project-root
(uv-mode 1) ; Enable uv-mode
(uv-mode-set)))))
(provide 'uv-mode)
;;; uv-mode.el ends here