prot-emacs.org: define prot-emacs-outline command

This commit is contained in:
Protesilaos Stavrou 2023-12-25 12:23:48 +02:00
parent b20247ec48
commit b513acc022
No known key found for this signature in database
GPG key ID: 99BD6459CD5CA3EA
3 changed files with 70 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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