From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.devel Subject: Auto-suggesting Packages Date: Tue, 16 Jan 2024 08:18:55 +0000 Message-ID: <87il3tg07k.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1879"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Jan 16 09:19:42 2024 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 1rPefc-0000GC-KQ for ged-emacs-devel@m.gmane-mx.org; Tue, 16 Jan 2024 09:19:40 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rPef3-0001tK-Vf; Tue, 16 Jan 2024 03:19:05 -0500 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 1rPef1-0001sx-NT for emacs-devel@gnu.org; Tue, 16 Jan 2024 03:19:03 -0500 Original-Received: from mout02.posteo.de ([185.67.36.66]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rPeex-00032D-MM for emacs-devel@gnu.org; Tue, 16 Jan 2024 03:19:03 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 91092240103 for ; Tue, 16 Jan 2024 09:18:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1705393136; bh=JGlFMp5nPD/eJGfLxRi+59O2dtpmj7gcjU1qXtuPs/M=; h=From:To:Subject:Autocrypt:OpenPGP:Date:Message-ID:MIME-Version: From; b=fz6rJ5FrDhBB943ty5nv8B0Zhp8G047HUGm38MZHzGs29H666qSwM/8eaMORlUjed jvTj4fNDPcopIZAm5g8/NbehQ8JLM1Z6mgpHvssPo83q969jC5Wx6nu+yP6xE1Rcky fbJ2JGMHb/frOE1e3jZ9PBCAzAgNQ+AMuND1z6KQLcPBsUY8eJdPYQT4htgwVmkr4g aXo852Z7fjrNl0SOPP29SFGzGGOh865zeKiBtW5mZvuN0I8OXAliAI5NiHSSI1GYWk mxwo7DqY6wJ8e0nG6yLCPa2dqVT4tCt7jXJnvtl+f86YDgtZ1AvyI8ysW51mSM666g 4o91xbwR8FZEg== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4TDhjr0fDNz6twQ for ; Tue, 16 Jan 2024 09:18:56 +0100 (CET) Autocrypt: addr=philipk@posteo.net; keydata= mDMEZBBQQhYJKwYBBAHaRw8BAQdAHJuofBrfqFh12uQu0Yi7mrl525F28eTmwUDflFNmdui0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiWBBMWCAA+FiEEDg7HY17ghYlni8XN8xYDWXahwukFAmQQUEICGwMFCQHhM4AFCwkI BwIGFQoJCAsCBBYCAwECHgECF4AACgkQ8xYDWXahwulikAEA77hloUiSrXgFkUVJhlKBpLCHUjA0 mWZ9j9w5d08+jVwBAK6c4iGP7j+/PhbkxaEKa4V3MzIl7zJkcNNjHCXmvFcEuDgEZBBQQhIKKwYB BAGXVQEFAQEHQI5NLiLRjZy3OfSt1dhCmFyn+fN/QKELUYQetiaoe+MMAwEIB4h+BBgWCAAmFiEE Dg7HY17ghYlni8XN8xYDWXahwukFAmQQUEICGwwFCQHhM4AACgkQ8xYDWXahwukm+wEA8cml4JpK NeAu65rg+auKrPOP6TP/4YWRCTIvuYDm0joBALw98AMz7/qMHvSCeU/hw9PL6u6R2EScxtpKnWof z4oM OpenPGP: id=7126E1DE2F0CE35C770BED01F2C3CC513DB89F66; url="https://keys.openpgp.org/vks/v1/by-fingerprint/7126E1DE2F0CE35C770BED01F2C3CC513DB89F66"; preference=signencrypt Received-SPF: pass client-ip=185.67.36.66; envelope-from=philipk@posteo.net; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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:315008 Archived-At: --=-=-= Content-Type: text/plain I posted a patch like this a few months back and forgot about it, but Sacha's Emacs News this weeks reminded me again. I took the code and changed the default behaviour to indicate a package can be installed in the mode line: --=-=-= Content-Type: text/plain Content-Disposition: inline diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el index 868373f46c2..0c94a1df1cf 100644 --- a/lisp/emacs-lisp/package.el +++ b/lisp/emacs-lisp/package.el @@ -4533,6 +4533,155 @@ package-get-version (or (lm-header "package-version") (lm-header "version"))))))))) + +;;;; Autosuggest + +(defvar package-autosuggest-database + '((sml-mode auto-mode-alist "\\.sml\\'") + (lua-mode auto-mode-alist "\\.lua\\'" ) + (ada-mode auto-mode-alist "\\.ada\\'") + ;; ... + ;; + ;; this is just for testing, in practice I think this data should + ;; be generated and bundled with Emacs, and would ideally be + ;; loaded in at compile-time. When the "archive-contents" file + ;; format is updated, we can include more information in there + ;; that would be added to this database. + ) + "Database of hints for packages to suggest installing.") + +(define-minor-mode package-autosuggest-mode + "Enable the automatic suggestion and installation of packages." + :init-value 'mode-line :global t + :type '(choice (const :tag "Indicate in mode line" mode-line) + (const :tag "Always prompt" always) + (const :tag "Prompt only once" once) + (const :tag "Indicate with message" message) + (const :tag "Do not suggest anything" nil)) + (funcall (if package-autosuggest-mode #'add-hook #'remove-hook) + 'after-change-major-mode-hook + #'package--autosuggest-after-change-mode)) + +(defun package--suggestion-applies-p (pkg-sug) + "Check if a suggestion PKG-SUG is applicable to the current buffer." + (pcase pkg-sug + (`(,(pred package-installed-p) . _) nil) + ((or `(,_ auto-mode-alist ,ext _) + `(,_ auto-mode-alist ,ext)) + (and (string-match-p ext (buffer-name)) t)) + ((or `(,_ magic-mode-alist ,mag _) + `(,_ magic-mode-alist ,mag)) + (save-restriction + (widen) + (save-excursion + (goto-char (point-min)) + (looking-at-p mag)))) + ((or `(,_ interpreter-mode-alist ,magic _) + `(,_ interpreter-mode-alist ,magic)) + (save-restriction + (widen) + (save-excursion + (goto-char (point-min)) + (and (looking-at auto-mode-interpreter-regexp) + (string-match-p + (concat "\\`" (file-name-nondirectory (match-string 2)) "\\'") + magic))))))) + +(defun package--autosuggest-find-candidates () + "Return a list of packages that might be interesting the current buffer." + (and package-autosuggest-mode + (let (suggetions) + (dolist (sug package-autosuggest-database) + (when (package--suggestion-applies-p sug) + (push sug suggetions))) + suggetions))) + +(defun package--autosuggest-install-and-enable (pkg-sug) + "Install and enable a package suggestion PKG-ENT. +PKG-SUG has the same form as an element of +`package-autosuggest-database'." + (package-install (car pkg-sug)) + (with-demoted-errors "Failed to enable: %S" + (dolist (buf (buffer-list)) + (with-current-buffer buf + (when (and (eq major-mode 'fundamental-mode) (buffer-file-name) + (package--suggestion-applies-p pkg-sug)) + (funcall-interactively (or (cadddr pkg-sug) (car pkg-sug)))))))) + +(defvar package--autosuggest-suggested '() + "List of packages that have already been suggested.") + +(defvar package--autosugest-line-format + '(:eval (package--autosugest-line-format))) +(put 'package--autosugest-line-format 'risky-local-variable t) + +(defface package-autosuggest-face + '((t :inherit (success))) + "Face to use in the mode line to highlight suggested packages." + :version "30.1") + +(defun package--autosugest-line-format () + "Generate a mode-line string to indicate a suggested package." + `(,@(and-let* (((eq package-autosuggest-mode 'mode-line)) + (avail (seq-difference (package--autosuggest-find-candidates) + package--autosuggest-suggested))) + (propertize + (format "Install %s?" + (mapconcat + #'symbol-name + (delete-dups (mapcar #'car avail)) + ", ")) + 'face 'package-autosuggest-face + 'mouse-face 'mode-line-highlight + 'help-echo "Click to install suggested package." + 'keymap (let ((map (make-sparse-keymap))) + (define-key map [mode-line down-mouse-1] #'package-autosuggest) + map))))) + +(add-to-list 'mode-line-misc-info + `(package-autosuggest-mode ("" package--autosugest-line-format))) + +(defun package--autosuggest-after-change-mode () + "Hook function to suggest packages for installation." + (when-let ((avail (seq-difference (package--autosuggest-find-candidates) + package--autosuggest-suggested)) + (pkgs (mapconcat #'symbol-name + (delete-dups (mapcar #'car avail)) + ", "))) + (add-to-list 'mode-line-misc-info + `(eglot--managed-mode (" [" eglot--mode-line-format "] "))) + (pcase package-autosuggest-mode + ('mode-line + (force-mode-line-update t)) + ('always + (when (yes-or-no-p (format "Install suggested packages (%s)?" pkgs)) + (mapc #'package--autosuggest-install-and-enable avail))) + ('once + (when (yes-or-no-p (format "Install suggested packages (%s)?" pkgs)) + (mapc #'package--autosuggest-install-and-enable avail)) + (setq package--autosuggest-suggested (append avail package--autosuggest-suggested))) + ('message + (message + (substitute-command-keys + (format "Found suggested packages: %s. Install using \\[package-autosuggest]" + pkgs))))))) + +(defun package-autosuggest () + "Prompt the user for suggested packages." + (interactive) + (let* ((avail (or (package--autosuggest-find-candidates) + (user-error "No suggestions found"))) + (pkgs (completing-read-multiple + "Install suggested packages: " avail + nil t + (mapconcat #'symbol-name + (delete-dups (mapcar #'car avail)) + ","))) + (choice (concat "\\`" (regexp-opt pkgs) "\\'"))) + (dolist (ent avail) + (when (string-match-p choice (symbol-name (car ent))) + (package--autosuggest-install-and-enable ent))))) + ;;;; Quickstart: precompute activation actions for faster start up. --=-=-= Content-Type: text/plain One annoyance I cannot resolve right now is that when I click on the mode line, it calls `package-autosuggest' that attempts to confirm the installation wish using `completing-read-multiple' with an initial input (so everything the user needs to do is to press enter). But due to the mouse-click, it appears the minibuffer is not selected, which can be confusing. Adding a `switch-to-minibuffer' didn't help either. Does anyone know what to do? --=-=-=--