misearch.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. ;;; misearch.el --- isearch extensions for multi-buffer search
  2. ;; Copyright (C) 2007-2012 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 `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. (defvar multi-isearch-orig-search-fun nil)
  73. (defvar multi-isearch-orig-wrap nil)
  74. (defvar multi-isearch-orig-push-state nil)
  75. ;;;###autoload
  76. (defun multi-isearch-setup ()
  77. "Set up isearch to search multiple buffers.
  78. Intended to be added to `isearch-mode-hook'."
  79. (when (and multi-isearch-search
  80. multi-isearch-next-buffer-function)
  81. (setq multi-isearch-current-buffer nil
  82. multi-isearch-next-buffer-current-function
  83. multi-isearch-next-buffer-function
  84. multi-isearch-orig-search-fun
  85. (default-value 'isearch-search-fun-function)
  86. multi-isearch-orig-wrap
  87. (default-value 'isearch-wrap-function)
  88. multi-isearch-orig-push-state
  89. (default-value 'isearch-push-state-function))
  90. (setq-default isearch-search-fun-function 'multi-isearch-search-fun
  91. isearch-wrap-function 'multi-isearch-wrap
  92. isearch-push-state-function 'multi-isearch-push-state)
  93. (add-hook 'isearch-mode-end-hook 'multi-isearch-end)))
  94. (defun multi-isearch-end ()
  95. "Clean up the multi-buffer search after terminating isearch."
  96. (setq multi-isearch-current-buffer nil
  97. multi-isearch-next-buffer-current-function nil)
  98. (setq-default isearch-search-fun-function multi-isearch-orig-search-fun
  99. isearch-wrap-function multi-isearch-orig-wrap
  100. isearch-push-state-function multi-isearch-orig-push-state)
  101. (remove-hook 'isearch-mode-end-hook 'multi-isearch-end))
  102. (defun multi-isearch-search-fun ()
  103. "Return the proper search function, for isearch in multiple buffers."
  104. (lambda (string bound noerror)
  105. (let ((search-fun
  106. ;; Use standard functions to search within one buffer
  107. (isearch-search-fun-default))
  108. found buffer)
  109. (or
  110. ;; 1. First try searching in the initial buffer
  111. (let ((res (funcall search-fun string bound noerror)))
  112. ;; Reset wrapping for all-buffers pause after successful search
  113. (if (and res (not bound) (eq multi-isearch-pause t))
  114. (setq multi-isearch-current-buffer nil))
  115. res)
  116. ;; 2. If the above search fails, start visiting next/prev buffers
  117. ;; successively, and search the string in them. Do this only
  118. ;; when bound is nil (i.e. not while lazy-highlighting search
  119. ;; strings in the current buffer).
  120. (when (and (not bound) multi-isearch-search)
  121. ;; If no-pause or there was one attempt to leave the current buffer
  122. (if (or (null multi-isearch-pause)
  123. (and multi-isearch-pause multi-isearch-current-buffer))
  124. (condition-case nil
  125. (progn
  126. (while (not found)
  127. ;; Find the next buffer to search
  128. (setq buffer (funcall multi-isearch-next-buffer-current-function
  129. (or buffer (current-buffer)) nil))
  130. (with-current-buffer buffer
  131. (goto-char (if isearch-forward (point-min) (point-max)))
  132. (setq isearch-barrier (point) isearch-opoint (point))
  133. ;; After visiting the next/prev buffer search the
  134. ;; string in it again, until the function in
  135. ;; multi-isearch-next-buffer-current-function raises
  136. ;; an error at the beginning/end of the buffer sequence.
  137. (setq found (funcall search-fun string bound noerror))))
  138. ;; Set buffer for isearch-search-string to switch
  139. (if buffer (setq multi-isearch-current-buffer buffer))
  140. ;; Return point of the new search result
  141. found)
  142. ;; Return nil when multi-isearch-next-buffer-current-function fails
  143. ;; (`with-current-buffer' raises an error for nil returned from it).
  144. (error (signal 'search-failed (list string "end of multi"))))
  145. (signal 'search-failed (list string "repeat for next buffer"))))))))
  146. (defun multi-isearch-wrap ()
  147. "Wrap the multiple buffers search when search is failed.
  148. Switch buffer to the first buffer for a forward search,
  149. or to the last buffer for a backward search.
  150. Set `multi-isearch-current-buffer' to the current buffer to display
  151. the isearch suffix message [initial buffer] only when isearch leaves
  152. the initial buffer."
  153. (if (or (null multi-isearch-pause)
  154. (and multi-isearch-pause multi-isearch-current-buffer))
  155. (progn
  156. (switch-to-buffer
  157. (setq multi-isearch-current-buffer
  158. (funcall multi-isearch-next-buffer-current-function
  159. (current-buffer) t)))
  160. (goto-char (if isearch-forward (point-min) (point-max))))
  161. (setq multi-isearch-current-buffer (current-buffer))
  162. (setq isearch-wrapped nil)))
  163. (defun multi-isearch-push-state ()
  164. "Save a function restoring the state of multiple buffers search.
  165. Save the current buffer to the additional state parameter in the
  166. search status stack."
  167. `(lambda (cmd)
  168. (multi-isearch-pop-state cmd ,(current-buffer))))
  169. (defun multi-isearch-pop-state (_cmd buffer)
  170. "Restore the multiple buffers search state.
  171. Switch to the buffer restored from the search status stack."
  172. (unless (equal buffer (current-buffer))
  173. (switch-to-buffer (setq multi-isearch-current-buffer buffer))))
  174. ;;; Global multi-buffer search invocations
  175. (defvar multi-isearch-buffer-list nil)
  176. (defun multi-isearch-next-buffer-from-list (&optional buffer wrap)
  177. "Return the next buffer in the series of buffers.
  178. This function is used for multiple buffers Isearch. A sequence of
  179. buffers is defined by the variable `multi-isearch-buffer-list'
  180. set in `multi-isearch-buffers' or `multi-isearch-buffers-regexp'."
  181. (let ((buffers (if isearch-forward
  182. multi-isearch-buffer-list
  183. (reverse multi-isearch-buffer-list))))
  184. (if wrap
  185. (car buffers)
  186. (cadr (member buffer buffers)))))
  187. (defvar ido-ignore-item-temp-list) ; from ido.el
  188. (defun multi-isearch-read-buffers ()
  189. "Return a list of buffers specified interactively, one by one."
  190. ;; Most code from `multi-occur'.
  191. (let* ((bufs (list (read-buffer "First buffer to search: "
  192. (current-buffer) t)))
  193. (buf nil)
  194. (ido-ignore-item-temp-list bufs))
  195. (while (not (string-equal
  196. (setq buf (read-buffer
  197. (if (eq read-buffer-function 'ido-read-buffer)
  198. "Next buffer to search (C-j to end): "
  199. "Next buffer to search (RET to end): ")
  200. nil t))
  201. ""))
  202. (add-to-list 'bufs buf)
  203. (setq ido-ignore-item-temp-list bufs))
  204. (nreverse bufs)))
  205. (defun multi-isearch-read-matching-buffers ()
  206. "Return a list of buffers whose names match specified regexp."
  207. ;; Most code from `multi-occur-in-matching-buffers'
  208. ;; and `kill-matching-buffers'.
  209. (let ((bufregexp
  210. (read-regexp "Search in buffers whose names match regexp")))
  211. (when bufregexp
  212. (delq nil (mapcar (lambda (buf)
  213. (when (string-match bufregexp (buffer-name buf))
  214. buf))
  215. (buffer-list))))))
  216. ;;;###autoload
  217. (defun multi-isearch-buffers (buffers)
  218. "Start multi-buffer Isearch on a list of BUFFERS.
  219. This list can contain live buffers or their names.
  220. Interactively read buffer names to search, one by one, ended with RET.
  221. With a prefix argument, ask for a regexp, and search in buffers
  222. whose names match the specified regexp."
  223. (interactive
  224. (list (if current-prefix-arg
  225. (multi-isearch-read-matching-buffers)
  226. (multi-isearch-read-buffers))))
  227. (let ((multi-isearch-next-buffer-function
  228. 'multi-isearch-next-buffer-from-list)
  229. (multi-isearch-buffer-list (mapcar #'get-buffer buffers)))
  230. (switch-to-buffer (car multi-isearch-buffer-list))
  231. (goto-char (if isearch-forward (point-min) (point-max)))
  232. (isearch-forward)))
  233. ;;;###autoload
  234. (defun multi-isearch-buffers-regexp (buffers)
  235. "Start multi-buffer regexp Isearch on a list of BUFFERS.
  236. This list can contain live buffers or their names.
  237. Interactively read buffer names to search, one by one, ended with RET.
  238. With a prefix argument, ask for a regexp, and search in buffers
  239. whose names match the specified regexp."
  240. (interactive
  241. (list (if current-prefix-arg
  242. (multi-isearch-read-matching-buffers)
  243. (multi-isearch-read-buffers))))
  244. (let ((multi-isearch-next-buffer-function
  245. 'multi-isearch-next-buffer-from-list)
  246. (multi-isearch-buffer-list (mapcar #'get-buffer buffers)))
  247. (switch-to-buffer (car multi-isearch-buffer-list))
  248. (goto-char (if isearch-forward (point-min) (point-max)))
  249. (isearch-forward-regexp)))
  250. ;;; Global multi-file search invocations
  251. (defvar multi-isearch-file-list nil)
  252. (defun multi-isearch-next-file-buffer-from-list (&optional buffer wrap)
  253. "Return the next buffer in the series of file buffers.
  254. This function is used for multiple file buffers Isearch. A sequence
  255. of files is defined by the variable `multi-isearch-file-list' set in
  256. `multi-isearch-files' or `multi-isearch-files-regexp'.
  257. Every next/previous file in the defined sequence is visited by
  258. `find-file-noselect' that returns the corresponding file buffer."
  259. (let ((files (if isearch-forward
  260. multi-isearch-file-list
  261. (reverse multi-isearch-file-list))))
  262. (find-file-noselect
  263. (if wrap
  264. (car files)
  265. (cadr (member (buffer-file-name buffer) files))))))
  266. (defun multi-isearch-read-files ()
  267. "Return a list of files specified interactively, one by one."
  268. ;; Most code from `multi-occur'.
  269. (let* ((files (list (read-file-name "First file to search: "
  270. default-directory
  271. buffer-file-name)))
  272. (file nil))
  273. (while (not (string-equal
  274. (setq file (read-file-name
  275. "Next file to search (RET to end): "
  276. default-directory
  277. default-directory))
  278. default-directory))
  279. (add-to-list 'files file))
  280. (nreverse files)))
  281. (defun multi-isearch-read-matching-files ()
  282. "Return a list of files whose names match specified wildcard."
  283. ;; Most wildcard code from `find-file-noselect'.
  284. (let ((filename (read-regexp "Search in files whose names match wildcard")))
  285. (when (and filename
  286. (not (string-match "\\`/:" filename))
  287. (string-match "[[*?]" filename))
  288. (condition-case nil
  289. (file-expand-wildcards filename t)
  290. (error (list filename))))))
  291. ;;;###autoload
  292. (defun multi-isearch-files (files)
  293. "Start multi-buffer Isearch on a list of FILES.
  294. Relative file names in this list are expanded to absolute
  295. file names using the current buffer's value of `default-directory'.
  296. Interactively read file names to search, one by one, ended with RET.
  297. With a prefix argument, ask for a wildcard, and search in file buffers
  298. whose file names match the specified wildcard."
  299. (interactive
  300. (list (if current-prefix-arg
  301. (multi-isearch-read-matching-files)
  302. (multi-isearch-read-files))))
  303. (let ((multi-isearch-next-buffer-function
  304. 'multi-isearch-next-file-buffer-from-list)
  305. (multi-isearch-file-list (mapcar #'expand-file-name files)))
  306. (find-file (car multi-isearch-file-list))
  307. (goto-char (if isearch-forward (point-min) (point-max)))
  308. (isearch-forward)))
  309. ;;;###autoload
  310. (defun multi-isearch-files-regexp (files)
  311. "Start multi-buffer regexp Isearch on a list of FILES.
  312. Relative file names in this list are expanded to absolute
  313. file names using the current buffer's value of `default-directory'.
  314. Interactively read file names to search, one by one, ended with RET.
  315. With a prefix argument, ask for a wildcard, and search in file buffers
  316. whose file names match the specified wildcard."
  317. (interactive
  318. (list (if current-prefix-arg
  319. (multi-isearch-read-matching-files)
  320. (multi-isearch-read-files))))
  321. (let ((multi-isearch-next-buffer-function
  322. 'multi-isearch-next-file-buffer-from-list)
  323. (multi-isearch-file-list (mapcar #'expand-file-name files)))
  324. (find-file (car multi-isearch-file-list))
  325. (goto-char (if isearch-forward (point-min) (point-max)))
  326. (isearch-forward-regexp)))
  327. (provide 'multi-isearch)
  328. ;;; misearch.el ends here