text-search.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ;;; text-search.el --- Library for searching text in buffers, files, URLs
  2. ;; Copyright © 2014 Alex Kost
  3. ;; Author: Alex Kost <alezost@gmail.com>
  4. ;; Created: 5 Nov 2014
  5. ;; URL: https://gitlab.com/alezost-emacs/text-search
  6. ;; Keywords: tools
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file provides some functions for unusual searching for strings
  19. ;; in buffers, files and URLs.
  20. ;;; Code:
  21. (require 'cl-lib)
  22. (defun search-buffer-for-matches (buffer regexp
  23. &optional count group reversep)
  24. "Find COUNT unique strings matching REGEXP in BUFFER.
  25. Return a list of strings matching parenthesized GROUP
  26. \(0 by default) in REGEXP.
  27. If COUNT is nil, search the whole buffer (until search is failed).
  28. If REVERSEP is non-nil, return a reverted list."
  29. (or count (setq count -1))
  30. (with-current-buffer buffer
  31. (save-excursion
  32. (goto-char (point-min))
  33. (let ((i 0)
  34. search-res search-list)
  35. (cl-loop do (setq search-res (re-search-forward regexp nil t))
  36. until (or (= i count) (null search-res))
  37. do
  38. (let ((tmp search-list))
  39. (cl-pushnew (match-string-no-properties (or group 0))
  40. search-list
  41. :test #'string=)
  42. (or (eq tmp search-list)
  43. (setq i (+ i 1)))))
  44. (if reversep
  45. search-list
  46. (nreverse search-list))))))
  47. (defun search-url-for-matches (url regexp
  48. &optional count group reversep)
  49. "Find COUNT unique strings matching regexp REGEXP in URL.
  50. See `search-buffer-for-matches' for the meaning of COUNT, GROUP
  51. and REVERSEP."
  52. (search-buffer-for-matches (url-retrieve-synchronously url)
  53. regexp count group reversep))
  54. (defun search-file-for-matches (file regexp
  55. &optional count group reversep)
  56. "Find COUNT unique strings matching regexp REGEXP in FILE.
  57. See `search-buffer-for-matches' for the meaning of COUNT, GROUP
  58. and REVERSEP."
  59. (search-buffer-for-matches (find-file-noselect file t t)
  60. regexp count group reversep))
  61. (defun search-buffer-by-re-list (buffer regexps &optional group noerror)
  62. "Find string in BUFFER by consecutive searching for REGEXPS.
  63. Return a string matching the parenthesized GROUP (0 by default)
  64. in the last regexp from REGEXPS list.
  65. If NOERROR is non-nil, just return nil if fail (no error)."
  66. (with-current-buffer buffer
  67. (save-excursion
  68. (goto-char (point-min))
  69. (let (result)
  70. (dolist (regexp regexps result)
  71. (setq result (re-search-forward regexp nil noerror)))
  72. (and result (match-string-no-properties (or group 0)))))))
  73. (defun search-url-by-re-list (url regexps &optional group noerror)
  74. "Find string in BUFFER by consecutive searching for REGEXPS.
  75. See `search-buffer-by-re-list' for the meaning of GROUP and NOERROR."
  76. (search-buffer-by-re-list (url-retrieve-synchronously url)
  77. regexps group noerror))
  78. (defun search-file-by-re-list (file regexps &optional group noerror)
  79. "Find string in BUFFER by consecutive searching for REGEXPS.
  80. See `search-buffer-by-re-list' for the meaning of GROUP and NOERROR."
  81. (search-buffer-by-re-list (find-file-noselect file t t)
  82. regexps group noerror))
  83. (provide 'text-search)
  84. ;;; text-search.el ends here