From 5f7ac02ffa5605d38d65c535ce4ec605d7f46510 Mon Sep 17 00:00:00 2001 From: Protesilaos Stavrou Date: Thu, 28 Dec 2023 06:49:37 +0200 Subject: [PATCH] emacs: make prot-search-outline use imenu category with no sorting --- emacs/.emacs.d/prot-emacs.org | 19 ++++++++++++++++++- emacs/.emacs.d/prot-lisp/prot-common.el | 10 ++++++++++ emacs/.emacs.d/prot-lisp/prot-search.el | 9 ++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/emacs/.emacs.d/prot-emacs.org b/emacs/.emacs.d/prot-emacs.org index ce601786..f971cd38 100644 --- a/emacs/.emacs.d/prot-emacs.org +++ b/emacs/.emacs.d/prot-emacs.org @@ -8127,6 +8127,16 @@ tools (e.g. `embark')." `(metadata (category . ,category)) (complete-with-action action candidates string pred)))) +;;;###autoload +(defun prot-common-completion-table-no-sort (category candidates) + "Pass appropriate metadata CATEGORY to completion CANDIDATES. +Like `prot-common-completion-table' but also disable sorting." + (lambda (string pred action) + (if (eq action 'metadata) + `(metadata (category . ,category) + (display-sort-function . ,#'identity)) + (complete-with-action action candidates string pred)))) + ;; Thanks to Igor Lima for the `prot-common-crm-exclude-selected-p': ;; . ;; This is used as a filter predicate in the relevant prompts. @@ -12146,6 +12156,13 @@ Also see `prot-search-grep-todo-keywords'." (nreverse candidates) (user-error "No outline")))) +(defun prot-search--outline-prompt () + "Prompt for outline among headings retrieved by `prot-search--get-outline'." + (completing-read + "Go to outline: " + (prot-common-completion-table-no-sort 'imenu (prot-search--get-outline)) + nil :require-match)) + (defvar prot-search-outline-hook nil "Normal hook to run at the end of `prot-search-outline'.") @@ -12153,7 +12170,7 @@ Also see `prot-search-grep-todo-keywords'." (defun prot-search-outline () "Go to the line of the given outline using completion." (interactive) - (when-let ((selection (completing-read "Go to outline: " (prot-search--get-outline) nil :require-match)) + (when-let ((selection (prot-search--outline-prompt)) (line (string-to-number (car (split-string selection "\t"))))) (goto-line line) (run-hooks 'prot-search-outline-hook))) diff --git a/emacs/.emacs.d/prot-lisp/prot-common.el b/emacs/.emacs.d/prot-lisp/prot-common.el index f6910d1d..1219f095 100644 --- a/emacs/.emacs.d/prot-lisp/prot-common.el +++ b/emacs/.emacs.d/prot-lisp/prot-common.el @@ -244,6 +244,16 @@ tools (e.g. `embark')." `(metadata (category . ,category)) (complete-with-action action candidates string pred)))) +;;;###autoload +(defun prot-common-completion-table-no-sort (category candidates) + "Pass appropriate metadata CATEGORY to completion CANDIDATES. +Like `prot-common-completion-table' but also disable sorting." + (lambda (string pred action) + (if (eq action 'metadata) + `(metadata (category . ,category) + (display-sort-function . ,#'identity)) + (complete-with-action action candidates string pred)))) + ;; Thanks to Igor Lima for the `prot-common-crm-exclude-selected-p': ;; . ;; This is used as a filter predicate in the relevant prompts. diff --git a/emacs/.emacs.d/prot-lisp/prot-search.el b/emacs/.emacs.d/prot-lisp/prot-search.el index d404c70b..88aed5df 100644 --- a/emacs/.emacs.d/prot-lisp/prot-search.el +++ b/emacs/.emacs.d/prot-lisp/prot-search.el @@ -319,6 +319,13 @@ Also see `prot-search-grep-todo-keywords'." (nreverse candidates) (user-error "No outline")))) +(defun prot-search--outline-prompt () + "Prompt for outline among headings retrieved by `prot-search--get-outline'." + (completing-read + "Go to outline: " + (prot-common-completion-table-no-sort 'imenu (prot-search--get-outline)) + nil :require-match)) + (defvar prot-search-outline-hook nil "Normal hook to run at the end of `prot-search-outline'.") @@ -326,7 +333,7 @@ Also see `prot-search-grep-todo-keywords'." (defun prot-search-outline () "Go to the line of the given outline using completion." (interactive) - (when-let ((selection (completing-read "Go to outline: " (prot-search--get-outline) nil :require-match)) + (when-let ((selection (prot-search--outline-prompt)) (line (string-to-number (car (split-string selection "\t"))))) (goto-line line) (run-hooks 'prot-search-outline-hook)))