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

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

[nongnu] elpa/opam-switch-mode df290bd0e3 21/31: Merge pull request #6 f


From: ELPA Syncer
Subject: [nongnu] elpa/opam-switch-mode df290bd0e3 21/31: Merge pull request #6 from ProofGeneral/prepare-melpa
Date: Mon, 14 Nov 2022 09:00:01 -0500 (EST)

branch: elpa/opam-switch-mode
commit df290bd0e33700258f799f1f2d2e02524c5f291c
Merge: 7c648a0719 7c2118fb9e
Author: Erik Martin-Dorel <erik@martin-dorel.org>
Commit: GitHub <noreply@github.com>

    Merge pull request #6 from ProofGeneral/prepare-melpa
    
    Prepare MELPA packaging
---
 README.md           |   2 +-
 opam-switch-mode.el | 225 +++++++++++++++++++++++++++-------------------------
 2 files changed, 119 insertions(+), 108 deletions(-)

diff --git a/README.md b/README.md
index bc5a44a90a..0b075fe9a7 100644
--- a/README.md
+++ b/README.md
@@ -40,4 +40,4 @@ any other shells outside emacs.
 
 - [Proof General](https://proofgeneral.github.io/)
   can kill the coq background process, when the opam switch changes,
-  see `coq-kill-coq-on-opam-switch`.
+  see 
[`coq-kill-coq-on-opam-switch`](https://proofgeneral.github.io/doc/master/userman/Coq-Proof-General/#index-coq_002dkill_002dcoq_002don_002dopam_002dswitch).
diff --git a/opam-switch-mode.el b/opam-switch-mode.el
index c0543360b5..b7b7ca85ed 100644
--- a/opam-switch-mode.el
+++ b/opam-switch-mode.el
@@ -1,9 +1,12 @@
-;;; opam-switch-mode.el --- select opam switches within emacs  -*- 
lexical-binding: t; -*-
+;;; opam-switch-mode.el --- Select opam switches within Emacs  -*- 
lexical-binding: t; -*-
 ;;
 ;; Copyright (C) 2021 Hendrik Tews
 ;;
 ;; Authors: Hendrik Tews
 ;; Maintainer: Hendrik Tews <hendrik@askra.de>
+;; URL: https://github.com/ProofGeneral/opam-switch-mode
+;; Package-Requires: ((emacs "25.1"))
+;; Version: 1.0-git
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -11,13 +14,13 @@
 ;; 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 file 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 in file COPYING in this or one of the parent
 ;; directories for more details.
-;; 
+;;
 ;; You should have received a copy of the GNU General Public License
 ;; along with "prooftree". If not, see <http://www.gnu.org/licenses/>.
 ;;
@@ -25,20 +28,20 @@
 ;;; Commentary:
 ;;
 ;; Provide command `opam-switch-set-switch' to change the opam switch
-;; of the running emacs session and minor mode `opam-switch-mode' to
+;; of the running Emacs session and minor mode `opam-switch-mode' to
 ;; select the opam switch via a menu bar menu.
 ;;
 ;; `opam-switch-set-switch' reads the name of the switch in the
-;; minibuffer, providing completion with all available switches. With
+;; 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-switch-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
+;; 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.
+;; environment to the state when Emacs was started.
 ;;
 ;; For obvious reasons, `opam-switch-set-switch' does not change the
 ;; switch of any other shell.
@@ -48,23 +51,24 @@
 
 ;;; User options and variables
 
-(defgroup opam-switch-mode ()
+(defgroup opam-switch ()
   "Customization for opam switch support in Emacs"
   :group 'external)
 
-(defcustom opsw--program-name "opam"
+
+(defcustom opam-switch--program-name "opam"
   "Name or path of the opam binary."
-  :group 'opam-switch-mode
+  :group 'opam-switch
   :type 'string)
 
-(defcustom opsw--common-options ()
+(defcustom opam-switch--common-options ()
   "Options to be supplied to every opam invocation.
 This must be a list of strings, each member string an option
 accepted by opam."
-  :group 'opam-switch-mode
+  :group 'opam-switch
   :type '(repeat string))
 
-(defcustom opsw--common-environment
+(defcustom opam-switch--common-environment
   '("OPAMUTF8=never"
     "OPAMCOLOR=never"
     "LC_ALL=C")
@@ -75,26 +79,26 @@ element should have the form of ENVVARNAME=VALUE.
 The process environment must ensure that output is plain ascii
 without color, non-ascii arrow symbols and that it is in English.
 Otherwise parsing the output of opam commands won't work."
-  :group 'opam-switch-mode
+  :group 'opam-switch
   :type '(repeat string))
 
 (defcustom opam-switch-change-opam-switch-hook nil
   "Hook run when the opam switch changes.
 This is used, for instance, to let Proof General kill the coq
 background process when the opam switch changes."
-  :group 'opam-switch-mode
+  :group 'opam-switch
   :type '(repeat function))
 
-;;; Code
+;;; Code:
 
-(defun opsw--run-command-without-stderr (sub-cmd
+(defun opam-switch--run-command-without-stderr (sub-cmd
                                         &optional switch sexp
                                         &rest args)
   "Run opam SUB-CMD, without capturing error output.
 Run opam SUB-CMD with additional arguments and insert the output
-in the current buffer at point. Error output (stderr) is
-discarded. If SWITCH is not nil, an option \"--swith=SWITCH\" is
-added. If SEXP is t, option --sexep is added. All remaining
+in the current buffer at point.  Error output (stderr) is
+discarded.  If SWITCH is not nil, an option \"--swith=SWITCH\" is
+added. If SEXP is t, option --sexp is added. All remaining
 arguments ARGS are added as arguments.
 
 Return exit status of the opam invocation.
@@ -103,38 +107,44 @@ Internally this function uses `process-file' internally 
and will
 therfore respect file-name handlers specified via
 `default-directory'."
   (let ((process-environment
-         (append opsw--common-environment process-environment))
-        (options (append args opsw--common-options)))
+         (append opam-switch--common-environment process-environment))
+        (options (append args opam-switch--common-options)))
     (when switch
       (push (format "--switch=%s" switch) options))
     (when sexp
       (push "--sexp" options))
-    ;; (message "run %s %s %s" opsw--program-name sub-cmd options)
-    (apply 'process-file opsw--program-name
+    ;; (message "run %s %s %s" opam-switch--program-name sub-cmd options)
+    (apply 'process-file opam-switch--program-name
                nil '(t nil) nil sub-cmd options)))
 
-(defun opsw--command-as-string (sub-cmd &optional switch sexp &rest args)
-  "Return output of opam SUB-CMD as string or nil.
-Same as `opsw--run-command-without-stderr' but return all output
-as string. Return nil if opam command fails."
+(defun opam-switch--command-as-string (sub-cmd &optional switch sexp &rest 
args)
+  "Run opam SUB-CMD, with additional arguments, without capturing stderr.
+Return nil if the opam command fails.
+Return all output as string otherwise.
+
+If SWITCH is not nil, an option \"--swith=SWITCH\" is added.
+If SEXP is t, option --sexp is added.
+All remaining arguments ARGS are added as arguments.
+
+This function  `opam-switch--run-command-without-stderr'."
   (with-temp-buffer
     (let ((status
-           (apply 'opsw--run-command-without-stderr sub-cmd switch sexp args)))
+           (apply 'opam-switch--run-command-without-stderr sub-cmd switch sexp 
args)))
       (if (eq status 0)
           (buffer-string)
         nil))))
 
-(defun opsw--get-root ()
+(defun opam-switch--get-root ()
   "Get the opam root directory.
 This is the opam variable 'root'."
-  (let ((root (opsw--command-as-string "var" nil nil "root")))
+  (let ((root (opam-switch--command-as-string "var" nil nil "root")))
     (unless root
-      (error "opam var root failed"))
+      (error "Command 'opam var root' failed"))
     (when (eq (aref root (1- (length root))) ?\n)
       (setq root (substring root 0 -1)))
     root))
 
-(defconst opsw--root (opsw--get-root)
+(defconst opam-switch--root (opam-switch--get-root)
   "The opam root directory.")
 
 ;; Example output of opam switch. The warning is output on stderr.
@@ -153,147 +163,147 @@ This is the opam variable 'root'."
 ;; [WARNING] The environment is not in sync with the current switch.
 ;;           You should run: eval $(opam env)
 
-(defun opsw--get-switches ()
+(defun opam-switch--get-switches ()
   "Return all opam switches as list of strings."
   (let (opam-switches)
     (with-temp-buffer
-      (unless (eq (opsw--run-command-without-stderr "switch") 0)
+      (unless (eq (opam-switch--run-command-without-stderr "switch") 0)
         ;; opam exit status different from 0 -- some error occured
-        (error "opam switch failed"))
+        (error "Command 'opam switch' failed"))
       (goto-char (point-min))
       (forward-line)
       (while (re-search-forward "^.. *\\([^ ]*\\).*$" nil t)
         (push (match-string 1) opam-switches))
       opam-switches)))
 
-(defvar opsw--switch-history nil
-  "Minibuffer history list for `opsw--set-switch'.")
+(defvar opam-switch--switch-history nil
+  "Minibuffer history list for `opam-switch--set-switch'.")
 
-(defvar opsw--saved-env nil
+(defvar opam-switch--saved-env nil
   "Saved environment variables, overwritten by an opam switch.
-This is a list of saved environment variables. Each saved
+This is a list of saved environment variables.  Each saved
 variable is a list of two strings, the variable and the value.
 Set when the first chosen opam switch overwrites the
 environment.")
 
-(defvar opsw--saved-exec-path nil
-  "Saved value of `exec-path'.
-Set when the first chosen opam switch overwrites `exec-path'.")
+(defvar opam-switch--saved-exec-path nil
+  "Saved value of variable `exec-path'.
+Set when the first chosen opam switch overwrites variable `exec-path'.")
 
 
-(defun opsw--save-current-env (opam-env)
+(defun opam-switch--save-current-env (opam-env)
   "Save the current environment values relevant to opam.
 Argument OPAM-ENV, coming from calling `opam env', is only used
-to find the environment variables to save. `exec-path' is saved
-in addition to environment variables."
-  (setq opsw--saved-env
+to find the environment variables to save.
+The variable `exec-path' is saved in addition to environment variables."
+  (setq opam-switch--saved-env
        (mapcar (lambda (x) (list (car x) (getenv (car x)))) opam-env))
-  (setq opsw--saved-exec-path exec-path))
-  
-(defun opsw--set-env (opam-env)
-  "Sets a new opam environment.
+  (setq opam-switch--saved-exec-path exec-path))
+
+(defun opam-switch--set-env (opam-env)
+  "Set a new opam environment.
 Environment variables in OPAM-ENV are put into the environment of
-the current Emacs session. `exec-path' is changed to match the
-environment PATH.
+the current Emacs session.  The variable `exec-path' is changed to
+match the environment PATH.
 
-It is unclear which value in `exec-path' corresponds to a
+It is unclear which value in variable `exec-path' corresponds to a
 previously set opam switch and also which entry in the PATH
 environment variable in OPAM-ENV corresponds to the new switch.
-Therefore this function uses the following heuristic. First all
-entries in `exec-path' that match `opsw--root' are deleted. Then,
-the first entry for PATH that maches `opsw--root' is added at the
-front of `exec-path'."
+Therefore this function uses the following heuristic.  First all
+entries in variable `exec-path' that match `opam-switch--root' are deleted.
+Then, the first entry for PATH that maches `opam-switch--root' is added at the
+front of variable `exec-path'."
   (let ((new-bin-dir
          (seq-find
-          (lambda (dir) (string-prefix-p opsw--root dir))
+          (lambda (dir) (string-prefix-p opam-switch--root dir))
           (parse-colon-path (cadr (assoc "PATH" opam-env))))))
     (unless new-bin-dir
       (error "No opam-root directory in PATH"))
     (mapc (lambda (x) (setenv (car x) (cadr x))) opam-env)
     (setq exec-path
-          (seq-remove (lambda (dir) (string-prefix-p opsw--root dir)) 
exec-path))
+          (seq-remove (lambda (dir) (string-prefix-p opam-switch--root dir)) 
exec-path))
     (push new-bin-dir exec-path)))
-  
-(defun opsw--reset-env ()
+
+(defun opam-switch--reset-env ()
   "Reset process environment to the state before setting the first opam switch.
-Reset all environment variables and `exec-path' to the values
-they had in this emacs session before the first chosen opam
+Reset variable `exec-path' and all environment variables to the values
+they had in this Emacs session before the first chosen opam
 switch overwrote them."
-  (mapc (lambda (x) (setenv (car x) (cadr x))) opsw--saved-env)
-  (setq exec-path opsw--saved-exec-path)
-  (setq opsw--saved-env nil)
-  (setq opsw--saved-exec-path nil))
+  (mapc (lambda (x) (setenv (car x) (cadr x))) opam-switch--saved-env)
+  (setq exec-path opam-switch--saved-exec-path)
+  (setq opam-switch--saved-env nil)
+  (setq opam-switch--saved-exec-path nil))
 
 
-(defun opsw--get-current-switch ()
+(defun opam-switch--get-current-switch ()
   "Return name of current switch or \"<none>\"."
   (let ((current-switch (getenv "OPAM_SWITCH_PREFIX")))
     (if current-switch
          (file-name-nondirectory current-switch)
       "<none>")))
 
-(defun opsw--set-switch (switch-name)
+(defun opam-switch--set-switch (switch-name)
   "Choose and set an opam switch.
 Set opam switch SWITCH-NAME, which must be a valid opam switch
-name. When called interactively, the switch name must be entered
+name.  When called interactively, the switch name must be entered
 in the minibuffer, which forces completion to a valid switch name
 or the empty string.
 
-Setting the opam switch for the first time inside emacs will save
-the current environment. Using the empty string for SWITCH-NAME
+Setting the opam switch for the first time inside Emacs will save
+the current environment.  Using the empty string for SWITCH-NAME
 will reset the environment to the saved values.
 
 The switch is set such that all process invocations from
-emacs respect the newly set opam switch. In addition to setting
+Emacs respect the newly set opam switch.  In addition to setting
 environment variables such as PATH and CAML_LD_LIBRARY_PATH, this
-also sets `exec-path', which controls emacs'
+also sets variable `exec-path', which controls Emacs'
 subprocesses (`call-process', `make-process' and similar
 functions).
 
 When the switch is changed, `opam-switch-change-opam-switch-hook'
-runs. This a can be used to inform other modes that may run
+runs.  This a can be used to inform other modes that may run
 background processes that depend on the currently active opam
 switch.
 
-For obvious reasons, `opsw--set-switch' will only affect emacs and
-not any other shells outside emacs."
+For obvious reasons, `opam-switch--set-switch' will only affect Emacs and
+not any other shells outside Emacs."
   (interactive
-   (let* ((switches (opsw--get-switches))
-          (current-switch (opsw--get-current-switch)))
+   (let* ((switches (opam-switch--get-switches))
+          (current-switch (opam-switch--get-current-switch)))
      (list
       (completing-read
        (format "current switch %s; switch to (empty to reset): " 
current-switch)
-       switches nil t "" 'opsw--switch-history nil))))
-  (when (and (equal switch-name "") (not opsw--saved-env))
-    (error "No saved opam environment, cannot reset."))
+       switches nil t "" 'opam-switch--switch-history nil))))
+  (when (and (equal switch-name "") (not opam-switch--saved-env))
+    (error "No saved opam environment, cannot reset"))
   (if (equal switch-name "")
-      (opsw--reset-env)
-    (let ((output-string (opsw--command-as-string "env" switch-name t))
+      (opam-switch--reset-env)
+    (let ((output-string (opam-switch--command-as-string "env" switch-name t))
           opam-env)
       (unless output-string
         (error
-         "opam env %s failed - probably because of invalid opam switch \"%s\""
+         "Command 'opam env %s' failed - probably because of invalid opam 
switch \"%s\""
          switch-name switch-name))
       (setq opam-env (car (read-from-string output-string)))
-      (unless opsw--saved-env
-        (opsw--save-current-env opam-env))
-      (opsw--set-env opam-env)))
+      (unless opam-switch--saved-env
+        (opam-switch--save-current-env opam-env))
+      (opam-switch--set-env opam-env)))
   (run-hooks 'opam-switch-change-opam-switch-hook))
 
-(defalias 'opam-switch-set-switch #'opsw--set-switch)
+(defalias 'opam-switch-set-switch #'opam-switch--set-switch)
 
 ;;; minor mode, keymap and menu
 
-(defvar opsw--mode-keymap (make-sparse-keymap)
-  "Keymap for `opam-switch-mode'")
+(defvar opam-switch--mode-keymap (make-sparse-keymap)
+  "Keymap for `opam-switch-mode'.")
 
-(defun opsw--menu-items ()
+(defun opam-switch--menu-items ()
   "Create list of opam switches as menu items for `easy-menu'."
   (append
    ;; first the current switch as info with a separator
    '(["current switch: " nil
       :active t
-      :suffix (opsw--get-current-switch)
+      :suffix (opam-switch--get-current-switch)
       :help "Shows the currently selected opam switch"]
      "-------")
    ;; then the list with all the real opam switches
@@ -301,18 +311,17 @@ not any other shells outside emacs."
     (lambda (switch)
       (vconcat
        `(,switch
-         (opsw--set-switch ,switch)
+         (opam-switch--set-switch ,switch)
          :active t
          :help ,(concat "select opam switch \"" switch "\""))))
-    (opsw--get-switches))
+    (opam-switch--get-switches))
    ;; now reset as last element
    '(
-     ["reset" (opsw--set-switch "")
-      :active opsw--saved-env
-      :help "reset to state when emacs was started"]
-     )))
+     ["reset" (opam-switch--set-switch "")
+      :active opam-switch--saved-env
+      :help "reset to state when emacs was started"])))
 
-(defun opsw--setup-opam-switch-mode ()
+(defun opam-switch--setup-opam-switch-mode ()
   "Re-define menu for `opam-switch-mode'.
 This function runs when `opam-switch-mode' is enabled to setup
 `opam-switch-mode'. Currently it only redefines the menu.
@@ -320,11 +329,11 @@ This function runs when `opam-switch-mode' is enabled to 
setup
 Note that the code for setting up the keymap and running the hook
 is automatically created by `define-minor-mode'."
   (easy-menu-define
-    opsw--mode-menu
-    opsw--mode-keymap
+    opam-switch--mode-menu
+    opam-switch--mode-keymap
     "opam mode menu"
     (cons "opam-switch"
-          (opsw--menu-items))))
+          (opam-switch--menu-items))))
 
 (define-minor-mode opam-switch-mode
   "Toggle opam mode"
@@ -333,10 +342,12 @@ is automatically created by `define-minor-mode'."
   ;; lighter
   " OPSW"
   ;; keymap
-  opsw--mode-keymap
-  :group 'opam-switch-mode
+  opam-switch--mode-keymap
+  :group 'opam-switch
   ;; body
   (when opam-switch-mode
-    (opsw--setup-opam-switch-mode)))
+    (opam-switch--setup-opam-switch-mode)))
 
 (provide 'opam-switch-mode)
+
+;;; opam-switch-mode.el ends here



reply via email to

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