Skip to content

Instantly share code, notes, and snippets.

@tam17aki
Last active January 8, 2024 03:37
Show Gist options
  • Save tam17aki/be6445e3b2b28a70ccce3546d2963680 to your computer and use it in GitHub Desktop.
Save tam17aki/be6445e3b2b28a70ccce3546d2963680 to your computer and use it in GitHub Desktop.
A consult extension for selected.el
;;; consult-selected.el --- Consult extension for selected.el -*- lexical-binding: t; -*-
;; Copyright (C) 2022 ballforest
;; 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:
;; This package provides an `consult' extension for displaying candidates of
;; associated command or action regarding `selected.el'.
;;; Code:
(eval-when-compile (require 'cl-lib))
(require 'consult)
(require 'selected)
(defcustom consult-selected-show-key-binding t
"If non-nil, hide key binding when showing a command list."
:type 'boolean
:group 'consult)
(defun consult-selected--get-commands (keymap)
"Create a list from the specified `KEYMAP'."
(if (keymapp keymap)
(cl-loop for i in (cdr keymap)
when (consp i)
unless (string= "consult-selected" (format "%s" (cdr i)))
collect (concat (format "%s" (cdr i))
(when consult-selected-show-key-binding
(format " (%s)"
(propertize
(let ((key (car i)))
(if (characterp key)
(string key) key))
'face 'consult-key)))))
(error "The argument is NOT keymap")))
;;;###autoload
(defun consult-selected ()
"Find a command for `selected'."
(interactive)
(if (require 'selected nil t)
(let ((pattern
(consult--read (consult-selected--get-commands selected-keymap)
:prompt "Selected: "
:sort nil
:category 'command
:require-match t)))
(with-current-buffer (current-buffer)
(command-execute
(intern (replace-regexp-in-string "\\s-(.+)$" "" pattern))
'record)))
(user-error "`selected' is NOT installed")))
(provide 'consult-selected)
;;; consult-selected.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment