Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.7 KiB

  1. (defun rlbr/get-venv-name (&optional library-root)
  2. "Generate venv name based off of the base-name of the library root"
  3. (file-name-base
  4. (directory-file-name
  5. (if library-root
  6. library-root
  7. (elpy-library-root)))))
  8. (defun rlbr/handle-name-conflicts (venv-name)
  9. "Deal with potential name conflicts in venv"
  10. (let ((venv-conflicts)
  11. (venv-partition-name))
  12. (setq venv-partition-name (rlbr/split-venv-with-number venv-name))
  13. (setq venv-conflicts
  14. (seq-filter
  15. (lambda (item) (string-equal (cdr item) venv-name))
  16. (mapcar #'rlbr/split-venv-with-number (pyvenv-virtualenv-list))))
  17. (when venv-conflicts
  18. (setcar venv-partition-name (1+ (apply 'max (mapcar #'car venv-conflicts)))))
  19. (rlbr/join-venv-with-number venv-partition-name)))
  20. (defun rlbr/setup-python-venv-dirlocals (&optional library-root)
  21. "Setup .dir-locals file inf library root and tell vc system to ignore .dir-locals file"
  22. (let* ((library-root (if library-root
  23. library-root
  24. (elpy-library-root)))
  25. (default-directory library-root)
  26. (dir-locals-path (expand-file-name
  27. ".dir-locals.el"))
  28. (venv-name (rlbr/get-venv-name
  29. library-root)))
  30. (message default-directory)
  31. (find-file dir-locals-path)
  32. (add-dir-local-variable
  33. 'python-mode
  34. 'pyvenv-workon
  35. venv-name)
  36. (save-buffer)
  37. (kill-buffer)))
  38. (defun rlbr/init-python-venv-in-library-root (&optional library-root)
  39. "If no venv is specified in the library root .dir-locals file, prompt to either create one or use default"
  40. (let ((venv-name (rlbr/get-venv-name)))
  41. (setq venv-name (rlbr/handle-name-conflicts venv-name))
  42. (if (y-or-n-p (format "Create venv '%'?" venv-name))
  43. (pyvenv-create venv-name python-command))))