From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [nongnu] elpa/opam-switch-mode 8b8670ca8b 1/3: feat(opam-switch-mode-lighter): Show the switch name in the mode-bar Date: Tue, 11 Jul 2023 16:21:33 -0400 Message-ID: References: <168910561415.26377.10324105754705665342@vcs2.savannah.gnu.org> <20230711200015.24AC2C06C6C@vcs2.savannah.gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33726"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: emacs-devel@gnu.org To: Erik Martin-Dorel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Jul 11 22:22:45 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qJJsj-0008TK-6U for ged-emacs-devel@m.gmane-mx.org; Tue, 11 Jul 2023 22:22:45 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qJJrg-0003v9-Pb; Tue, 11 Jul 2023 16:21:40 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qJJrf-0003un-BM for emacs-devel@gnu.org; Tue, 11 Jul 2023 16:21:39 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qJJrd-0004zz-Dx for emacs-devel@gnu.org; Tue, 11 Jul 2023 16:21:39 -0400 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 3EA148060E; Tue, 11 Jul 2023 16:21:35 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id BD65C803DC; Tue, 11 Jul 2023 16:21:33 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1689106893; bh=UrndzI9gsP5LKpseFtpfJ2R+UfUIDzZJw2aeFhDv2mI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=EHjUaqBY/88TCJz+eBoNle1Rwz8S9qW+Ky389zPWREooYXGxU/I1/iZWC1T6O4kX9 xwIwLKDqtzx3ix54PQSyXvbhgYxH0ekVia6UBZ7E1KpF8PKShPBn+mNcse8WlIaISW jH17MLYoCcvEIStSiypTE/z0DLdAITSutLcZSK8gptONrhIXZk63ZEpF1pJQGAA9tn vAXsKFlKWwlcHVnGYX8rsHaMczWp0x0jEWb1Rvjt//WA1MDu4nmJHdQ0GkJpdz1l8w hfZDD5kwZUccvYByPEcwyIQ1DO7juqyTdaJIKVhAcmNGfbPIkJ+UiS8vHsD+eULJj6 pwTN6AG49xBpw== Original-Received: from alfajor (unknown [23.233.149.155]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id A48BE12037C; Tue, 11 Jul 2023 16:21:33 -0400 (EDT) In-Reply-To: <20230711200015.24AC2C06C6C@vcs2.savannah.gnu.org> (ELPA Syncer's message of "Tue, 11 Jul 2023 16:00:14 -0400 (EDT)") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:307761 Archived-At: Salut Erik=C2=B8 > + (progn (opam-switch-set-switch ,switch) > + (redraw-display)) > :active t > :help ,(concat "Select opam switch \"" switch "\"")]) > (opam-switch--get-switches)) > ;; now reset as last element > '( > - ["reset" (opam-switch-set-switch "") > + ["reset" > + (progn (opam-switch-set-switch "") > + (redraw-display)) Instead of `redraw-display` you want `force-mode-line-update`. And you probably want to put it into `opam-switch-set-switch` rather than duplicating it after every call: Also, in some circumstances the mode lines can be updated fairly often (e.g. after every key press, and/or every process output received by a filter), so it might be worthwhile to memoize/precompute the lighter rather than recompute it every time the modeline is updated. So maybe the patch below? Stefan diff --git a/opam-switch-mode.el b/opam-switch-mode.el index 0ec42aa123..b13cbb75d7 100644 --- a/opam-switch-mode.el +++ b/opam-switch-mode.el @@ -93,6 +93,8 @@ background process before the opam switch changes." =20 ;;; Code: =20 +(defvar opam-switch--mode-lighter nil) + (defun opam-switch--run-command-without-stderr (sub-cmd &optional switch sexp &rest args) @@ -321,6 +323,8 @@ not any other shells outside Emacs." (unless opam-switch--saved-env (opam-switch--save-current-env opam-env)) (opam-switch--set-env opam-env prefix))) + (setq opam-switch--mode-lighter nil) + (force-mode-line-update t) (run-hooks 'opam-switch-change-opam-switch-hook)) =20 (define-obsolete-function-alias 'opam-switch--set-switch @@ -343,17 +347,13 @@ not any other shells outside Emacs." ;; then the list with all the real opam switches (mapcar (lambda (switch) - `[,switch - (progn (opam-switch-set-switch ,switch) - (redraw-display)) + `[,switch (opam-switch-set-switch ,switch) :active t :help ,(concat "Select opam switch \"" switch "\"")]) (opam-switch--get-switches)) ;; now reset as last element '( - ["reset" - (progn (opam-switch-set-switch "") - (redraw-display)) + ["reset" (opam-switch-set-switch "") :active opam-switch--saved-env :help "Reset to state when Emacs was started"]))) =20 @@ -376,10 +376,12 @@ is automatically created by `define-minor-mode'." =20 (defun opam-switch-mode-lighter () "Return the lighter for opam-switch-mode which indicates the current swi= tch." - (let* ((current-switch (opam-switch--get-current-switch)) - ;; handle the case of local switches for better UX - (shortened (replace-regexp-in-string ".*/" "=E2=80=A6/" current-s= witch t t))) - (format " OPSW-%s" shortened))) + (or opam-switch--mode-lighter + (let* ((current-switch (opam-switch--get-current-switch)) + ;; handle the case of local switches for better UX + (shortened (replace-regexp-in-string ".*/" "=E2=80=A6/" + current-switch t t))) + (setq opam-switch--mode-lighter (format " OPSW-%s" shortened))))) =20 ;;;###autoload (define-minor-mode opam-switch-mode