I would like to run a command and go to the first matching regex pattern that matches in any open buffer.
Kind of like search-forward-regex.
The requirements are:
- not be limited to forward/backward. It should go to any direction.
- be global and search through all buffers.
- it should not be a list of matching options like (helm-swoop-all), but it should actually jump to what ever matches first
- it should be a regex search, not a word search
Is this possible?
[EDIT] SOLVED
With a few tweaks of the answer below, it now works.
The reason for this was actually part of go-to-item-that-links-to-id-of-current-item
With the below, I can add an elisp link like [[elisp:(my/goto-parent)][parent]]
to the heading of a task (via abbrevations), and then I can navigate to the parent if something links to it.
(require 'cl)
(defcustom search-all-buffers-ignored-files (list (rx-to-string '(and bos (or ".bash_history" "TAGS") eos)))
"Files to ignore when searching buffers via \\[search-all-buffers]."
:type 'editable-list)
(require 'grep)
(defun search-all-buffers (regexp prefix)
"Searches file-visiting buffers for occurence of REGEXP. With
prefix > 1 (i.e., if you type C-u \\[search-all-buffers]),
searches all buffers."
(interactive (list (grep-read-regexp)
current-prefix-arg))
(message "Regexp is %s; prefix is %s" regexp prefix)
(multi-occur
(if (member prefix '(4 (4)))
(buffer-list)
(remove-if
(lambda (b) (some (lambda (rx) (string-match rx (file-name-nondirectory (buffer-file-name b)))) search-all-buffers-ignored-files))
(remove-if-not 'buffer-file-name (buffer-list))))
regexp))
(defun my/search-all-buffers-first-result (regexp prefix)
"Searches file-visiting buffers for occurence of REGEXP. With
prefix > 1 (i.e., if you type C-u \\[search-all-buffers]),
searches all buffers."
;Cache how many windows are open atm.
(setq my-var/multi-window (one-window-p)) ;true if single frame.
(interactive (list (grep-read-regexp)
current-prefix-arg))
(search-all-buffers regexp prefix)
(next-error)
(kill-buffer "*Occur*")
;if there were more than one window open, kill the other once.
(when my-var/multi-window (delete-other-windows))
)
(defun my/goto-parent ()
"Find the parent of this task by looking for what links to this task"
(interactive)
(my/search-all-buffers-first-result (concat "id:" (org-id-get)) nil)
)