From b513acc0224d47fffc1207b4125f28cf6f52d68f Mon Sep 17 00:00:00 2001 From: Protesilaos Stavrou Date: Mon, 25 Dec 2023 12:23:48 +0200 Subject: [PATCH] prot-emacs.org: define prot-emacs-outline command --- .../prot-emacs-modules/prot-emacs-search.el | 1 + emacs/.emacs.d/prot-emacs.org | 35 +++++++++++++++++++ emacs/.emacs.d/prot-lisp/prot-search.el | 34 ++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/emacs/.emacs.d/prot-emacs-modules/prot-emacs-search.el b/emacs/.emacs.d/prot-emacs-modules/prot-emacs-search.el index c2945388..0485e426 100644 --- a/emacs/.emacs.d/prot-emacs-modules/prot-emacs-search.el +++ b/emacs/.emacs.d/prot-emacs-modules/prot-emacs-search.el @@ -49,6 +49,7 @@ "M-s u" #'prot-search-occur-urls "M-s t" #'prot-search-occur-todo-keywords "M-s M-t" #'prot-search-grep-todo-keywords ; With C-u it runs `prot-search-git-grep-todo-keywords' + "M-s M-s" #'prot-search-outline "M-s M-o" #'prot-search-occur-outline "M-s M-u" #'prot-search-occur-browse-url) (prot-emacs-keybind isearch-mode-map diff --git a/emacs/.emacs.d/prot-emacs.org b/emacs/.emacs.d/prot-emacs.org index a0d83e69..bea2a2e0 100644 --- a/emacs/.emacs.d/prot-emacs.org +++ b/emacs/.emacs.d/prot-emacs.org @@ -4313,6 +4313,7 @@ active and the Completions window is on display." "M-s u" #'prot-search-occur-urls "M-s t" #'prot-search-occur-todo-keywords "M-s M-t" #'prot-search-grep-todo-keywords ; With C-u it runs `prot-search-git-grep-todo-keywords' + "M-s M-s" #'prot-search-outline "M-s M-o" #'prot-search-occur-outline "M-s M-u" #'prot-search-occur-browse-url) (prot-emacs-keybind isearch-mode-map @@ -11947,6 +11948,40 @@ Also see `prot-search-grep-todo-keywords'." (buf-name (format "*keywords in <%s>*" (buffer-name)))) (occur-1 prot-search-todo-keywords num (list (current-buffer)) buf-name))) +;;;; Outline + +(defun prot-search--get-outline () + "Return alist of outline outline-regexp and positions." + (let* ((outline-regexp (format "^\\(?:%s\\)" (or (bound-and-true-p outline-regexp) "[*\^L]+"))) + (heading-alist (bound-and-true-p outline-heading-alist)) + (level-fun (or (bound-and-true-p outline-level) + (lambda () ;; as in the default from outline.el + (or (cdr (assoc (match-string 0) heading-alist)) + (- (match-end 0) (match-beginning 0)))))) + candidates) + (save-excursion + (goto-char (point-min)) + (while (if (bound-and-true-p outline-search-function) + (funcall outline-search-function) + (re-search-forward outline-regexp nil t)) + (push + (format "%s %s" + (line-number-at-pos (point)) + (buffer-substring-no-properties (line-beginning-position) (line-end-position))) + candidates) + (goto-char (1+ (line-end-position))))) + (if candidates + (nreverse candidates) + (user-error "No outline")))) + +;;;###autoload +(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)) + (line (string-to-number (car (split-string selection "\t"))))) + (goto-line line))) + ;;;; Grep (defvar prot-search--grep-hist nil diff --git a/emacs/.emacs.d/prot-lisp/prot-search.el b/emacs/.emacs.d/prot-lisp/prot-search.el index 575aadb7..ba41fdc1 100644 --- a/emacs/.emacs.d/prot-lisp/prot-search.el +++ b/emacs/.emacs.d/prot-lisp/prot-search.el @@ -293,6 +293,40 @@ Also see `prot-search-grep-todo-keywords'." (buf-name (format "*keywords in <%s>*" (buffer-name)))) (occur-1 prot-search-todo-keywords num (list (current-buffer)) buf-name))) +;;;; Outline + +(defun prot-search--get-outline () + "Return alist of outline outline-regexp and positions." + (let* ((outline-regexp (format "^\\(?:%s\\)" (or (bound-and-true-p outline-regexp) "[*\^L]+"))) + (heading-alist (bound-and-true-p outline-heading-alist)) + (level-fun (or (bound-and-true-p outline-level) + (lambda () ;; as in the default from outline.el + (or (cdr (assoc (match-string 0) heading-alist)) + (- (match-end 0) (match-beginning 0)))))) + candidates) + (save-excursion + (goto-char (point-min)) + (while (if (bound-and-true-p outline-search-function) + (funcall outline-search-function) + (re-search-forward outline-regexp nil t)) + (push + (format "%s %s" + (line-number-at-pos (point)) + (buffer-substring-no-properties (line-beginning-position) (line-end-position))) + candidates) + (goto-char (1+ (line-end-position))))) + (if candidates + (nreverse candidates) + (user-error "No outline")))) + +;;;###autoload +(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)) + (line (string-to-number (car (split-string selection "\t"))))) + (goto-line line))) + ;;;; Grep (defvar prot-search--grep-hist nil