emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/opam-switch-mode 7de138a0e9 07/31: add minor mode with men


From: ELPA Syncer
Subject: [nongnu] elpa/opam-switch-mode 7de138a0e9 07/31: add minor mode with menu bar menu
Date: Mon, 14 Nov 2022 08:59:59 -0500 (EST)

branch: elpa/opam-switch-mode
commit 7de138a0e9ea5ab5853d76b64b365180ea87b26b
Author: Hendrik Tews <Hendrik.Tews@kernkonzept.com>
Commit: Hendrik Tews <Hendrik.Tews@kernkonzept.com>

    add minor mode with menu bar menu
---
 README.md    | 13 ++++++-----
 opam-mode.el | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 73 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index 8707c20673..590ecd24fd 100644
--- a/README.md
+++ b/README.md
@@ -1,12 +1,15 @@
 # opam mode
 
 Provide command `opam-set-switch` to change the opam switch of the
-running emacs session. The command reads the name of the switch in
-the minibuffer, providing completion with all available switches,
-see the documentation of `opam-set-switch` below.
+running emacs session and minor mode `opam-mode` to select the opam
+switch via a menu bar menu.
+
+The menu is generated each time the minor mode is enabled and
+contains the switches that are known at that time. If you create a
+new switch, re-enable the minor mode to get it added to the menu.
+The menu contains an additional entry "reset" to reset the
+environment to the state when emacs was started.
 
-Contrary to the file name, this file does not (yet) provide a minor
-mode.
 
 ## Command `opam-set-switch`
 
diff --git a/opam-mode.el b/opam-mode.el
index 83790d21db..4c2364ec1d 100644
--- a/opam-mode.el
+++ b/opam-mode.el
@@ -25,16 +25,23 @@
 ;;; Commentary:
 ;;
 ;; Provide command `opam-set-switch' to change the opam switch of the
-;; running emacs session. The command reads the name of the switch in
-;; the minibuffer, providing completion with all available switches.
-;; With no input (i.e., leaving the minibuffer empty) the environment
-;; is reset to the state before the first call of `opam-set-switch'.
+;; running emacs session and minor mode `opam-mode' to select the opam
+;; switch via a menu bar menu.
+;;
+;; `opam-set-switch' reads the name of the switch in the minibuffer,
+;; providing completion with all available switches. With no input
+;; (i.e., leaving the minibuffer empty) the environment is reset to
+;; the state before the first call of `opam-set-switch'.
+;;
+;; The menu is generated each time the minor mode is enabled and
+;; contains the switches that are known at that time. If you create a
+;; new switch, re-enable the minor mode to get it added to the menu.
+;; The menu contains an additional entry "reset" to reset the
+;; environment to the state when emacs was started.
 ;;
 ;; For obvious reasons, `opam-set-switch' does not change the switch
 ;; of any other shell.
 ;;
-;; Contrary to the file name, this file does not (yet) provide a minor
-;; mode.
 ;; 
 
 (require 'seq)
@@ -246,4 +253,56 @@ not any other shells outside emacs."
         (opam-save-current-env opam-env))
       (opam-set-env opam-env))))
 
+
+;;; minor mode, keymap and menu
+
+(defvar opam-mode-keymap (make-sparse-keymap)
+  "Keymap for `opam-mode'")
+
+(defun opam-menu-items ()
+  "Create list or opam switches as menu items for `easy-menu'."
+  (nconc
+   ;; first the list with all the real opam switches
+   (mapcar
+    (lambda (switch)
+      (vconcat
+       `(,switch
+         (opam-set-switch ,switch)
+         :active t
+         :help ,(concat "select opam switch \"" switch "\""))))
+    (opam-get-switches))
+   ;; now reset as last element
+   '(
+     ["reset" (opam-set-switch "")
+      :active opam-saved-env
+      :help "reset to state when emacs was started"]
+     )))
+
+(defun opam-setup-opam-mode ()
+  "Re-define menu when for `opam-mode'.
+This function runs when `opam-mode' is enabled to setup
+`opam-mode'. Currently it only redefines the menu.
+
+Note that the code for setting up the keymap and running the hook
+is automatically created by `define-minor-mode'."
+  (easy-menu-define
+    opam-mode-menu
+    opam-mode-keymap
+    "opam mode menu"
+    (cons "opam"
+          (opam-menu-items))))
+
+(define-minor-mode opam-mode
+  "Toggle opam mode"
+  ;; init value - should be nil
+  nil
+  ;; lighter
+  " OP"
+  ;; keymap
+  opam-mode-keymap
+  :group 'opam-mode
+  ;; body
+  (when opam-mode
+    (opam-setup-opam-mode)))
+
 (provide 'opam-mode)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]