Skip to content

Commit

Permalink
Merge pull request #156 from antoineB/feature/mode-line
Browse files Browse the repository at this point in the history
Add mode line
  • Loading branch information
necaris committed Jul 8, 2024
2 parents 61cccbe + c9c4320 commit ce748a5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions conda-projectile.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
;;; conda-projectile.el --- Use projectile mode line to display conda env -*- lexical-binding: t; -*-

;; Copyright (C) 2024 antoine

;; Author: antoine <antoine@antoine-AB350>
;; 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 <https://www.gnu.org/licenses/>.

;;; 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
8 changes: 8 additions & 0 deletions conda.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ce748a5

Please sign in to comment.