diff --git a/README.md b/README.md index 5b48abb..8ce6bf9 100644 --- a/README.md +++ b/README.md @@ -195,16 +195,23 @@ minor mode, which will: ### displaying the currently active environment on the mode line -The name of the currently active conda environment is stored in the variable -`conda-env-current-name`. If you want to have it displayed on your customized -mode line you can just add `(:exec (list conda-env-current-name)))` somewhere -in your `mode-line-format`. If you don't customize your mode line and just want -to have the current virtualenv displayed, you can do: +To add the current env in the mode-line you can run: ```lisp -(setq-default mode-line-format (cons '(:exec conda-env-current-name) mode-line-format)) +(conda-mode-line-setup) ``` +#### displaying with projectile + +To display if the conda env is activated inside the projectile mode-line part. + +```lisp +(require 'conda-projectile) +(conda-projectile-mode-line-setup) +``` + +If the projectile name and conda env name mismatch you can modify `conda-projectile-name-assoc` variable. + ### eshell prompt customization You might also want to have the name of your current conda environment appear on diff --git a/conda-projectile.el b/conda-projectile.el new file mode 100644 index 0000000..6d6e4de --- /dev/null +++ b/conda-projectile.el @@ -0,0 +1,68 @@ +;;; conda-projectile.el --- Use projectile mode line to display conda env -*- lexical-binding: t; -*- + +;; Copyright (C) 2024 antoine + +;; Author: antoine +;; Keywords: extensions + +;; 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 . + +;;; Commentary: + +;; The conda env name and the project name very often are the same so instead of clutter the mode line with the same +;; info just add $ char to projectile mode-line part. +;; +;; If the conda env name and projectile match display : Projectile$[project-name:project-type] +;; else : Projectile$conda-env[project-name:project-type] +;; +;; If the name didn't match exactly you can't modify conda-projectile-name-assoc like +;; '(("some-conda-env-name" . "some-projectile-name")) + +;;; Code: + +(require 'projectile) + +(defun conda-projectile-mode-line-setup () + (add-to-list 'conda-postactivate-hook #'projectile-update-mode-line) + (add-to-list 'conda-postdeactivate-hook #'projectile-update-mode-line) + (setq projectile-mode-line-function #'conda-projectile-mode-line)) + +(defvar conda-projectile-name-assoc '() + "AList between conda env name and projectile project name") + +(defun conda-projectile--match-env-p () + (let ((association (assoc conda-env-current-name conda-projectile-name-assoc)) + (project-name (projectile-project-name))) + (or + (equal project-name conda-env-current-name) + (and association + (equal project-name (cdr association)))))) + +(defun conda-projectile-default-mode-line () + "Close to projectile-default-mode-line format." + (let* ((project-name (projectile-project-name)) + (project-type (projectile-project-type))) + (format "%s%s[%s%s]" + projectile-mode-line-prefix + (cond ((conda-projectile--match-env-p) "$") + (conda-env-current-name (concat "$" conda-env-current-name)) + (t "")) + (or project-name "-") + (if project-type + (format ":%s" project-type) + "")))) + + +(provide 'conda-projectile) +;;; conda-projectile.el ends here diff --git a/conda.el b/conda.el index b056cb0..2d864c6 100644 --- a/conda.el +++ b/conda.el @@ -113,6 +113,10 @@ This should be consistent across platforms.") ;; ensure it's considered safe (put 'conda-env-name-for-buffer 'safe-local-variable 'stringp) +(defvar conda-mode-line '(:propertize + (:eval (when conda-env-current-name (concat "$" conda-env-current-name " "))) + help-echo "Current conda env")) + ;; internal utility functions (defvar conda--executable-path nil @@ -438,6 +442,10 @@ Returns a list of new path elements." (or (car conda-env-history) (car candidates))))) +(defun conda-mode-line-setup () + "Setup a basic mode-line display of current env." + (add-to-list 'mode-line-misc-info conda-mode-line)) + ;; potentially interactive user-exposed functions ;;;###autoload