misearch.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. ;;; misearch.el --- isearch extensions for multi-buffer search
  2. ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. ;; Author: Juri Linkov <juri@jurta.org>
  4. ;; Keywords: matching
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file adds more dimensions to the search space. It implements
  18. ;; various features that extend isearch. One of them is an ability to
  19. ;; search through multiple buffers.
  20. ;;; Code:
  21. ;;; Search multiple buffers
  22. ;;;###autoload (add-hook 'isearch-mode-hook 'multi-isearch-setup)
  23. (defgroup multi-isearch nil
  24. "Using isearch to search through multiple buffers."
  25. :version "23.1"
  26. :group 'isearch)
  27. (defcustom multi-isearch-search t
  28. "Non-nil enables searching multiple related buffers, in certain modes."
  29. :type 'boolean
  30. :version "23.1"
  31. :group 'multi-isearch)
  32. (defcustom multi-isearch-pause t
  33. "A choice defining where to pause the search.
  34. If the value is nil, don't pause before going to the next buffer.
  35. If the value is `initial', pause only after a failing search in the
  36. initial buffer.
  37. If t, pause in all buffers that contain the search string."
  38. :type '(choice
  39. (const :tag "Don't pause" nil)
  40. (const :tag "Only in initial buffer" initial)
  41. (const :tag "All buffers" t))
  42. :version "23.1"
  43. :group 'multi-isearch)
  44. ;;;###autoload
  45. (defvar multi-isearch-next-buffer-function nil
  46. "Function to call to get the next buffer to search.
  47. When this variable is set to a function that returns a buffer, then
  48. after typing another \\[isearch-forward] or \\[isearch-backward] \
  49. at a failing search, the search goes
  50. to the next buffer in the series and continues searching for the
  51. next occurrence.
  52. This function should return the next buffer (it doesn't need to switch
  53. to it), or nil if it can't find the next buffer (when it reaches the
  54. end of the search space).
  55. The first argument of this function is the current buffer where the
  56. search is currently searching. It defines the base buffer relative to
  57. which this function should find the next buffer. When the isearch
  58. direction is backward (when option `isearch-forward' is nil), this function
  59. should return the previous buffer to search.
  60. If the second argument of this function WRAP is non-nil, then it
  61. should return the first buffer in the series; and for the backward
  62. search, it should return the last buffer in the series.")
  63. ;;;###autoload
  64. (defvar multi-isearch-next-buffer-current-function nil
  65. "The currently active function to get the next buffer to search.
  66. Initialized from `multi-isearch-next-buffer-function' when
  67. Isearch starts.")
  68. ;;;###autoload
  69. (defvar multi-isearch-current-buffer nil
  70. "The buffer where the search is currently searching.
  71. The value is nil when the search still is in the initial buffer.")
  72. ;;;###autoload
  73. (defvar multi-isearch-buffer-list nil
  74. "Sequence of buffers visited by multiple buffers Isearch.
  75. This is nil if Isearch is not currently searching more than one buffer.")
  76. ;;;###autoload
  77. (defvar multi-isearch-file-list nil
  78. "Sequence of files visited by multiple file buffers Isearch.")
  79. (defvar multi-isearch-orig-search-fun nil)
  80. (defvar multi-isearch-orig-wrap nil)
  81. (defvar multi-isearch-orig-push-state nil)
  82. ;;;###autoload
  83. (defun multi-isearch-setup ()
  84. "Set up isearch to search multiple buffers.
  85. Intended to be added to `isearch-mode-hook'."
  86. (when (and multi-isearch-search
  87. multi-isearch-next-buffer-function)
  88. (setq multi-isearch-current-buffer nil
  89. multi-isearch-next-buffer-current-function
  90. multi-isearch-next-buffer-function
  91. multi-isearch-orig-search-fun
  92. (default-value 'isearch-search-fun-function)
  93. multi-isearch-orig-wrap
  94. (default-value 'isearch-wrap-function)
  95. multi-isearch-orig-push-state
  96. (default-value 'isearch-push-state-function))
  97. (setq-default isearch-search-fun-function 'multi-isearch-search-fun
  98. isearch-wrap-function 'multi-isearch-wrap
  99. isearch-push-state-function 'multi-isearch-push-state)
  100. (add-hook 'isearch-mode-end-hook 'multi-isearch-end)))
  101. (defun multi-isearch-end ()
  102. "Clean up the multi-buffer search after terminating isearch."
  103. (setq multi-isearch-current-buffer nil
  104. multi-isearch-next-buffer-current-function nil
  105. multi-isearch-buffer-list nil
  106. multi-isearch-file-list nil)
  107. (setq-default isearch-search-fun-function multi-isearch-orig-search-fun
  108. isearch-wrap-function multi-isearch-orig-wrap
  109. isearch-push-state-function multi-isearch-orig-push-state)
  110. (remove-hook 'isearch-mode-end-hook 'multi-isearch-end))
  111. (defun multi-isearch-search-fun ()
  112. "Return the proper search function, for isearch in multiple buffers."
  113. (lambda (string bound noerror)
  114. (let ((search-fun
  115. ;; Use standard functions to search within one buffer
  116. (isearch-search-fun-default))
  117. found buffer)
  118. (or
  119. ;; 1. First try searching in the initial buffer
  120. (let ((res (funcall search-fun string bound noerror)))
  121. ;; Reset wrapping for all-buffers pause after successful search
  122. (if (and res (not bound) (eq multi-isearch-pause t))
  123. (setq multi-isearch-current-buffer nil))
  124. res)
  125. ;; 2. If the above search fails, start visiting next/prev buffers
  126. ;; successively, and search the string in them. Do this only
  127. ;; when bound is nil (i.e. not while lazy-highlighting search
  128. ;; strings in the current buffer).
  129. (when (and (not bound) multi-isearch-search)
  130. ;; If no-pause or there was one attempt to leave the current buffer
  131. (if (or (null multi-isearch-pause)
  132. (and multi-isearch-pause multi-isearch-current-buffer))
  133. (condition-case nil
  134. (progn
  135. (while (not found)
  136. ;; Find the next buffer to search
  137. (setq buffer (funcall multi-isearch-next-buffer-current-function
  138. (or buffer (current-buffer)) nil))
  139. (with-current-buffer buffer
  140. (goto-char (if isearch-forward (point-min) (point-max)))
  141. (setq isearch-barrier (point) isearch-opoint (point))
  142. ;; After visiting the next/prev buffer search the
  143. ;; string in it again, until the function in
  144. ;; multi-isearch-next-buffer-current-function raises
  145. ;; an error at the beginning/end of the buffer sequence.
  146. (setq found (funcall search-fun string bound noerror))))
  147. ;; Set buffer for isearch-search-string to switch
  148. (if buffer (setq multi-isearch-current-buffer buffer))
  149. ;; Return point of the new search result
  150. found)
  151. ;; Return nil when multi-isearch-next-buffer-current-function fails
  152. ;; (`with-current-buffer' raises an error for nil returned from it).
  153. (error (signal 'search-failed (list string "end of multi"))))
  154. (signal 'search-failed (list string "repeat for next buffer"))))))))
  155. (defun multi-isearch-wrap ()
  156. "Wrap the multiple buffers search when search is failed.
  157. Switch buffer to the first buffer for a forward search,
  158. or to the last buffer for a backward search.
  159. Set `multi-isearch-current-buffer' to the current buffer to display
  160. the isearch suffix message [initial buffer] only when isearch leaves
  161. the initial buffer."
  162. (if (or (null multi-isearch-pause)
  163. (and multi-isearch-pause multi-isearch-current-buffer))
  164. (progn
  165. (switch-to-buffer
  166. (setq multi-isearch-current-buffer
  167. (funcall multi-isearch-next-buffer-current-function
  168. (current-buffer) t)))
  169. (goto-char (if isearch-forward (point-min) (point-max))))
  170. (setq multi-isearch-current-buffer (current-buffer))
  171. (setq isearch-wrapped nil)))
  172. (defun multi-isearch-push-state ()
  173. "Save a function restoring the state of multiple buffers search.
  174. Save the current buffer to the additional state parameter in the
  175. search status stack."
  176. `(lambda (cmd)
  177. (multi-isearch-pop-state cmd ,(current-buffer))))
  178. (defun multi-isearch-pop-state (_cmd buffer)
  179. "Restore the multiple buffers search state.
  180. Switch to the buffer restored from the search status stack."
  181. (unless (equal buffer (current-buffer))
  182. (switch-to-buffer (setq multi-isearch-current-buffer buffer))))
  183. ;;; Global multi-buffer search invocations
  184. (defun multi-isearch-next-buffer-from-list (&optional buffer wrap)
  185. "Return the next buffer in the series of buffers.
  186. This function is used for multiple buffers Isearch. A sequence of
  187. buffers is defined by the variable `multi-isearch-buffer-list'
  188. set in `multi-isearch-buffers' or `multi-isearch-buffers-regexp'."
  189. (let ((buffers (if isearch-forward
  190. multi-isearch-buffer-list
  191. (reverse multi-isearch-buffer-list))))
  192. (if wrap
  193. (car buffers)
  194. (cadr (member buffer buffers)))))
  195. (defvar ido-ignore-item-temp-list) ; from ido.el
  196. (defun multi-isearch-read-buffers ()
  197. "Return a list of buffers specified interactively, one by one."
  198. ;; Most code from `multi-occur'.
  199. (let* ((bufs (list (read-buffer "First buffer to search: "
  200. (current-buffer) t)))
  201. (buf nil)
  202. (ido-ignore-item-temp-list bufs))
  203. (while (not (string-equal
  204. (setq buf (read-buffer
  205. (if (eq read-buffer-function #'ido-read-buffer)
  206. "Next buffer to search (C-j to end): "
  207. "Next buffer to search (RET to end): ")
  208. nil t))
  209. ""))
  210. (add-to-list 'bufs buf)
  211. (setq ido-ignore-item-temp-list bufs))
  212. (nreverse bufs)))
  213. (defun multi-isearch-read-matching-buffers ()
  214. "Return a list of buffers whose names match specified regexp.
  215. Uses `read-regexp' to read the regexp."
  216. ;; Most code from `multi-occur-in-matching-buffers'
  217. ;; and `kill-matching-buffers'.
  218. (let ((bufregexp
  219. (read-regexp "Search in buffers whose names match regexp")))
  220. (when bufregexp
  221. (delq nil (mapcar (lambda (buf)
  222. (when (string-match bufregexp (buffer-name buf))
  223. buf))
  224. (buffer-list))))))
  225. ;;;###autoload
  226. (defun multi-isearch-buffers (buffers)
  227. "Start multi-buffer Isearch on a list of BUFFERS.
  228. This list can contain live buffers or their names.
  229. Interactively read buffer names to search, one by one, ended with RET.
  230. With a prefix argument, ask for a regexp, and search in buffers
  231. whose names match the specified regexp."
  232. (interactive
  233. (list (if current-prefix-arg
  234. (multi-isearch-read-matching-buffers)
  235. (multi-isearch-read-buffers))))
  236. (let ((multi-isearch-next-buffer-function
  237. 'multi-isearch-next-buffer-from-list))
  238. (setq multi-isearch-buffer-list (mapcar #'get-buffer buffers))
  239. (switch-to-buffer (car multi-isearch-buffer-list))
  240. (goto-char (if isearch-forward (point-min) (point-max)))
  241. (isearch-forward nil t)))
  242. ;;;###autoload
  243. (defun multi-isearch-buffers-regexp (buffers)
  244. "Start multi-buffer regexp Isearch on a list of BUFFERS.
  245. This list can contain live buffers or their names.
  246. Interactively read buffer names to search, one by one, ended with RET.
  247. With a prefix argument, ask for a regexp, and search in buffers
  248. whose names match the specified regexp."
  249. (interactive
  250. (list (if current-prefix-arg
  251. (multi-isearch-read-matching-buffers)
  252. (multi-isearch-read-buffers))))
  253. (let ((multi-isearch-next-buffer-function
  254. 'multi-isearch-next-buffer-from-list))
  255. (setq multi-isearch-buffer-list (mapcar #'get-buffer buffers))
  256. (switch-to-buffer (car multi-isearch-buffer-list))
  257. (goto-char (if isearch-forward (point-min) (point-max)))
  258. (isearch-forward-regexp nil t)))
  259. ;;; Global multi-file search invocations
  260. (defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap)
  261. "Return the next buffer in the series of file buffers.
  262. This function is used for multiple file buffers Isearch. A sequence
  263. of files is defined by the variable `multi-isearch-file-list' set in
  264. `multi-isearch-files' or `multi-isearch-files-regexp'.
  265. Every next/previous file in the defined sequence is visited by
  266. `find-file-noselect' that returns the corresponding file buffer."
  267. (let ((files (if isearch-forward
  268. multi-isearch-file-list
  269. (reverse multi-isearch-file-list))))
  270. (find-file-noselect
  271. (if wrap
  272. (car files)
  273. (cadr (member (buffer-file-name buffer) files))))))
  274. (defun multi-isearch-read-files ()
  275. "Return a list of files specified interactively, one by one."
  276. ;; Most code from `multi-occur'.
  277. (let* ((files (list (read-file-name "First file to search: "
  278. default-directory
  279. buffer-file-name)))
  280. (file nil))
  281. (while (not (string-equal
  282. (setq file (read-file-name
  283. "Next file to search (RET to end): "
  284. default-directory
  285. default-directory))
  286. default-directory))
  287. (add-to-list 'files file))
  288. (nreverse files)))
  289. ;; A regexp is not the same thing as a file glob - does this matter?
  290. (defun multi-isearch-read-matching-files ()
  291. "Return a list of files whose names match specified wildcard.
  292. Uses `read-regexp' to read the wildcard."
  293. ;; Most wildcard code from `find-file-noselect'.
  294. (let ((filename (read-regexp "Search in files whose names match wildcard")))
  295. (when (and filename
  296. (not (string-match "\\`/:" filename))
  297. (string-match "[[*?]" filename))
  298. (condition-case nil
  299. (file-expand-wildcards filename t)
  300. (error (list filename))))))
  301. ;;;###autoload
  302. (defun multi-isearch-files (files)
  303. "Start multi-buffer Isearch on a list of FILES.
  304. Relative file names in this list are expanded to absolute
  305. file names using the current buffer's value of `default-directory'.
  306. Interactively read file names to search, one by one, ended with RET.
  307. With a prefix argument, ask for a wildcard, and search in file buffers
  308. whose file names match the specified wildcard."
  309. (interactive
  310. (list (if current-prefix-arg
  311. (multi-isearch-read-matching-files)
  312. (multi-isearch-read-files))))
  313. (let ((multi-isearch-next-buffer-function
  314. 'multi-isearch-next-file-buffer-from-list))
  315. (setq multi-isearch-file-list (mapcar #'expand-file-name files))
  316. (find-file (car multi-isearch-file-list))
  317. (goto-char (if isearch-forward (point-min) (point-max)))
  318. (isearch-forward nil t)))
  319. ;;;###autoload
  320. (defun multi-isearch-files-regexp (files)
  321. "Start multi-buffer regexp Isearch on a list of FILES.
  322. Relative file names in this list are expanded to absolute
  323. file names using the current buffer's value of `default-directory'.
  324. Interactively read file names to search, one by one, ended with RET.
  325. With a prefix argument, ask for a wildcard, and search in file buffers
  326. whose file names match the specified wildcard."
  327. (interactive
  328. (list (if current-prefix-arg
  329. (multi-isearch-read-matching-files)
  330. (multi-isearch-read-files))))
  331. (let ((multi-isearch-next-buffer-function
  332. 'multi-isearch-next-file-buffer-from-list))
  333. (setq multi-isearch-file-list (mapcar #'expand-file-name files))
  334. (find-file (car multi-isearch-file-list))
  335. (goto-char (if isearch-forward (point-min) (point-max)))
  336. (isearch-forward-regexp nil t)))
  337. (defvar unload-function-defs-list)
  338. (defun multi-isearch-unload-function ()
  339. "Remove autoloaded variables from `unload-function-defs-list'.
  340. Also prevent the feature from being reloaded via `isearch-mode-hook'."
  341. (remove-hook 'isearch-mode-hook 'multi-isearch-setup)
  342. (let ((defs (list (car unload-function-defs-list)))
  343. (auto '(multi-isearch-next-buffer-function
  344. multi-isearch-next-buffer-current-function
  345. multi-isearch-current-buffer
  346. multi-isearch-buffer-list multi-isearch-file-list)))
  347. (dolist (def (cdr unload-function-defs-list))
  348. (unless (and (symbolp def)
  349. (memq def auto))
  350. (push def defs)))
  351. (setq unload-function-defs-list (nreverse defs))
  352. ;; .
  353. nil))
  354. (defalias 'misearch-unload-function 'multi-isearch-unload-function)
  355. (provide 'multi-isearch)
  356. (provide 'misearch)
  357. ;;; misearch.el ends here