Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.5 KiB

4 years ago
  1. ;;; helm-regexp.el --- In buffer regexp searching and replacement for helm. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2012 ~ 2019 Thierry Volpiatto <thierry.volpiatto@gmail.com>
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'cl-lib)
  15. (require 'helm)
  16. (require 'helm-help)
  17. (require 'helm-utils)
  18. (declare-function helm-mm-split-pattern "helm-multi-match")
  19. (defgroup helm-regexp nil
  20. "Regexp related Applications and libraries for Helm."
  21. :group 'helm)
  22. ;; History vars
  23. (defvar helm-build-regexp-history nil)
  24. (defun helm-query-replace-regexp (_candidate)
  25. "Query replace regexp from `helm-regexp'.
  26. With a prefix arg replace only matches surrounded by word boundaries,
  27. i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
  28. (let ((regexp helm-input))
  29. (apply 'query-replace-regexp
  30. (helm-query-replace-args regexp))))
  31. (defun helm-kill-regexp-as-sexp (_candidate)
  32. "Kill regexp in a format usable in lisp code."
  33. (helm-regexp-kill-new
  34. (prin1-to-string helm-input)))
  35. (defun helm-kill-regexp (_candidate)
  36. "Kill regexp as it is in `helm-pattern'."
  37. (helm-regexp-kill-new helm-input))
  38. (defun helm-query-replace-args (regexp)
  39. "create arguments of `query-replace-regexp' action in `helm-regexp'."
  40. (let ((region-only (helm-region-active-p)))
  41. (list
  42. regexp
  43. (query-replace-read-to regexp
  44. (format "Query replace %sregexp %s"
  45. (if helm-current-prefix-arg "word " "")
  46. (if region-only "in region " ""))
  47. t)
  48. helm-current-prefix-arg
  49. (when region-only (region-beginning))
  50. (when region-only (region-end)))))
  51. (defvar helm-source-regexp
  52. (helm-build-in-buffer-source "Regexp Builder"
  53. :init (lambda ()
  54. (helm-init-candidates-in-buffer
  55. 'global (with-temp-buffer
  56. (insert-buffer-substring helm-current-buffer)
  57. (buffer-string))))
  58. :get-line #'helm-regexp-get-line
  59. :persistent-action #'helm-regexp-persistent-action
  60. :persistent-help "Show this line"
  61. :multiline t
  62. :multimatch nil
  63. :requires-pattern 2
  64. :group 'helm-regexp
  65. :mode-line "Press TAB to select action."
  66. :action '(("Kill Regexp as sexp" . helm-kill-regexp-as-sexp)
  67. ("Query Replace Regexp (C-u Not inside word.)"
  68. . helm-query-replace-regexp)
  69. ("Kill Regexp" . helm-kill-regexp))))
  70. (defun helm-regexp-get-line (s e)
  71. (let ((matches (match-data))
  72. (line (buffer-substring s e)))
  73. (propertize
  74. (cl-loop with ln = (format "%5d: %s" (1- (line-number-at-pos s)) line)
  75. for i from 0 to (1- (/ (length matches) 2))
  76. if (match-string i)
  77. concat (format "\n%s%s'%s'"
  78. (make-string 10 ? ) (format "Group %d: " i) it)
  79. into ln1
  80. finally return (concat ln ln1))
  81. 'helm-realvalue s)))
  82. (defun helm-regexp-persistent-action (pt)
  83. (helm-goto-char pt)
  84. (helm-highlight-current-line))
  85. (defun helm-regexp-kill-new (input)
  86. (kill-new (substring-no-properties input))
  87. (message "Killed: %s" input))
  88. ;;; Predefined commands
  89. ;;
  90. ;;
  91. ;;;###autoload
  92. (defun helm-regexp ()
  93. "Preconfigured helm to build regexps.
  94. `query-replace-regexp' can be run from there against found regexp."
  95. (interactive)
  96. (save-restriction
  97. (when (and (helm-region-active-p)
  98. ;; Don't narrow to region if buffer is already narrowed.
  99. (not (helm-current-buffer-narrowed-p (current-buffer))))
  100. (narrow-to-region (region-beginning) (region-end)))
  101. (helm :sources helm-source-regexp
  102. :buffer "*helm regexp*"
  103. :prompt "Regexp: "
  104. :history 'helm-build-regexp-history)))
  105. (provide 'helm-regexp)
  106. ;; Local Variables:
  107. ;; byte-compile-warnings: (not obsolete)
  108. ;; coding: utf-8
  109. ;; indent-tabs-mode: nil
  110. ;; End:
  111. ;;; helm-regexp.el ends here