misearch.el 15 KB

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