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.

377 lines
14 KiB

5 years ago
  1. ;;; helm-multi-match.el --- Multiple regexp matching methods for helm -*- lexical-binding: t -*-
  2. ;; Original Author: rubikitch
  3. ;; Copyright (C) 2008 ~ 2011 rubikitch
  4. ;; Copyright (C) 2011 ~ 2019 Thierry Volpiatto <thierry.volpiatto@gmail.com>
  5. ;; Author: Thierry Volpiatto <thierry.volpiatto@gmail.com>
  6. ;; URL: http://github.com/emacs-helm/helm
  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. ;;; Code:
  18. (require 'cl-lib)
  19. (require 'helm-lib)
  20. (defgroup helm-multi-match nil
  21. "Helm multi match."
  22. :group 'helm)
  23. (defcustom helm-mm-matching-method 'multi3
  24. "Matching method for helm match plugin.
  25. You can set here different methods to match candidates in helm.
  26. Here are the possible value of this symbol and their meaning:
  27. - multi1: Respect order, prefix of pattern must match.
  28. - multi2: Same but with partial match.
  29. - multi3: The best, multiple regexp match, allow negation.
  30. - multi3p: Same but prefix must match.
  31. Default is multi3, you should keep this for a better experience.
  32. Note that multi1 and multi3p are incompatible with fuzzy matching
  33. in file completion and by the way fuzzy matching will be disabled there
  34. when these options are used."
  35. :type '(radio :tag "Matching methods for helm"
  36. (const :tag "Multiple regexp 1 ordered with prefix match" multi1)
  37. (const :tag "Multiple regexp 2 ordered with partial match" multi2)
  38. (const :tag "Multiple regexp 3 matching no order, partial, best." multi3)
  39. (const :tag "Multiple regexp 3p matching with prefix match" multi3p))
  40. :group 'helm-multi-match)
  41. ;; Internal
  42. (defvar helm-mm-default-match-functions
  43. '(helm-mm-exact-match helm-mm-match))
  44. (defvar helm-mm-default-search-functions
  45. '(helm-mm-exact-search helm-mm-search))
  46. ;;; Build regexps
  47. ;;
  48. ;;
  49. (defconst helm-mm-space-regexp "\\s\\\\s-"
  50. "Regexp to represent space itself in multiple regexp match.")
  51. (defun helm-mm-split-pattern (pattern &optional grep-space)
  52. "Split PATTERN if it contain spaces and return resulting list.
  53. If spaces in PATTERN are escaped, don't split at this place.
  54. i.e \"foo bar baz\"=> (\"foo\" \"bar\" \"baz\")
  55. but \"foo\\ bar baz\"=> (\"foo\\s-bar\" \"baz\").
  56. If GREP-SPACE is used translate escaped space to \"\\s\" instead of \"\\s-\"."
  57. (split-string
  58. ;; Match spaces litteraly because candidate buffer syntax-table
  59. ;; doesn't understand "\s-" properly.
  60. (replace-regexp-in-string
  61. helm-mm-space-regexp
  62. (if grep-space "\\s" "\\s-") pattern nil t)))
  63. (defun helm-mm-1-make-regexp (pattern)
  64. "Replace spaces in PATTERN with \"\.*\"."
  65. (mapconcat 'identity (helm-mm-split-pattern pattern) ".*"))
  66. ;;; Exact match.
  67. ;;
  68. ;;
  69. ;; Internal.
  70. (defvar helm-mm-exact-pattern-str nil)
  71. (defvar helm-mm-exact-pattern-real nil)
  72. (defun helm-mm-exact-get-pattern (pattern)
  73. (unless (equal pattern helm-mm-exact-pattern-str)
  74. (setq helm-mm-exact-pattern-str pattern
  75. helm-mm-exact-pattern-real (concat "\n" pattern "\n")))
  76. helm-mm-exact-pattern-real)
  77. (cl-defun helm-mm-exact-match (candidate &optional (pattern helm-pattern))
  78. (if case-fold-search
  79. (progn
  80. (setq candidate (downcase candidate)
  81. pattern (downcase pattern))
  82. (string= candidate pattern))
  83. (string= candidate pattern)))
  84. (defun helm-mm-exact-search (pattern &rest _ignore)
  85. (and (search-forward (helm-mm-exact-get-pattern pattern) nil t)
  86. (forward-line -1)))
  87. ;;; Prefix match
  88. ;;
  89. ;;
  90. ;; Internal
  91. (defvar helm-mm-prefix-pattern-str nil)
  92. (defvar helm-mm-prefix-pattern-real nil)
  93. (defun helm-mm-prefix-get-pattern (pattern)
  94. (unless (equal pattern helm-mm-prefix-pattern-str)
  95. (setq helm-mm-prefix-pattern-str pattern
  96. helm-mm-prefix-pattern-real (concat "\n" pattern)))
  97. helm-mm-prefix-pattern-real)
  98. (defun helm-mm-prefix-match (candidate &optional pattern)
  99. ;; In filename completion basename and basedir may be
  100. ;; quoted, unquote them for string comparison (Issue #1283).
  101. (setq pattern (replace-regexp-in-string
  102. "\\\\" "" (or pattern helm-pattern)))
  103. (let ((len (length pattern)))
  104. (and (<= len (length candidate))
  105. (string= (substring candidate 0 len) pattern ))))
  106. (defun helm-mm-prefix-search (pattern &rest _ignore)
  107. (search-forward (helm-mm-prefix-get-pattern pattern) nil t))
  108. ;;; Multiple regexp patterns 1 (order is preserved / prefix).
  109. ;;
  110. ;;
  111. ;; Internal
  112. (defvar helm-mm-1-pattern-str nil)
  113. (defvar helm-mm-1-pattern-real nil)
  114. (defun helm-mm-1-get-pattern (pattern)
  115. (unless (equal pattern helm-mm-1-pattern-str)
  116. (setq helm-mm-1-pattern-str pattern
  117. helm-mm-1-pattern-real
  118. (concat "^" (helm-mm-1-make-regexp pattern))))
  119. helm-mm-1-pattern-real)
  120. (cl-defun helm-mm-1-match (candidate &optional (pattern helm-pattern))
  121. (string-match (helm-mm-1-get-pattern pattern) candidate))
  122. (defun helm-mm-1-search (pattern &rest _ignore)
  123. (re-search-forward (helm-mm-1-get-pattern pattern) nil t))
  124. ;;; Multiple regexp patterns 2 (order is preserved / partial).
  125. ;;
  126. ;;
  127. ;; Internal
  128. (defvar helm-mm-2-pattern-str nil)
  129. (defvar helm-mm-2-pattern-real nil)
  130. (defun helm-mm-2-get-pattern (pattern)
  131. (unless (equal pattern helm-mm-2-pattern-str)
  132. (setq helm-mm-2-pattern-str pattern
  133. helm-mm-2-pattern-real
  134. (concat "^.*" (helm-mm-1-make-regexp pattern))))
  135. helm-mm-2-pattern-real)
  136. (cl-defun helm-mm-2-match (candidate &optional (pattern helm-pattern))
  137. (string-match (helm-mm-2-get-pattern pattern) candidate))
  138. (defun helm-mm-2-search (pattern &rest _ignore)
  139. (re-search-forward (helm-mm-2-get-pattern pattern) nil t))
  140. ;;; Multiple regexp patterns 3 (permutation).
  141. ;;
  142. ;;
  143. ;; Internal
  144. (defvar helm-mm--3-pattern-str nil)
  145. (defvar helm-mm--3-pattern-list nil)
  146. (defun helm-mm-3-get-patterns (pattern)
  147. "Returns a list of predicate/regexp cons cells.
  148. e.g. ((identity . \"foo\") (not . \"bar\")).
  149. If PATTERN is inchanged, don't recompute PATTERN and return the
  150. previous value stored in `helm-mm--3-pattern-list'."
  151. (unless (equal pattern helm-mm--3-pattern-str)
  152. (setq helm-mm--3-pattern-str pattern
  153. helm-mm--3-pattern-list
  154. (helm-mm-3-get-patterns-internal pattern)))
  155. helm-mm--3-pattern-list)
  156. (defun helm-mm-3-get-patterns-internal (pattern)
  157. "Return a list of predicate/regexp cons cells.
  158. e.g. ((identity . \"foo\") (not . \"bar\"))."
  159. (unless (string= pattern "")
  160. (cl-loop for pat in (helm-mm-split-pattern pattern)
  161. collect (if (string= "!" (substring pat 0 1))
  162. (cons 'not (substring pat 1))
  163. (cons 'identity pat)))))
  164. (cl-defun helm-mm-3-match (candidate &optional (pattern helm-pattern))
  165. "Check if PATTERN match CANDIDATE.
  166. When PATTERN contain a space, it is splitted and matching is done
  167. with the several resulting regexps against CANDIDATE.
  168. e.g \"bar foo\" will match \"foobar\" and \"barfoo\".
  169. Argument PATTERN, a string, is transformed in a list of
  170. cons cell with `helm-mm-3-get-patterns' if it contain a space.
  171. e.g \"foo bar\"=>((identity . \"foo\") (identity . \"bar\")).
  172. Then each predicate of cons cell(s) is called with regexp of same
  173. cons cell against CANDIDATE.
  174. i.e (identity (string-match \"foo\" \"foo bar\")) => t."
  175. (let ((pat (helm-mm-3-get-patterns pattern)))
  176. (cl-loop for (predicate . regexp) in pat
  177. always (funcall predicate
  178. (condition-case _err
  179. ;; FIXME: Probably do nothing when
  180. ;; using fuzzy leaving the job
  181. ;; to the fuzzy fn.
  182. (string-match regexp candidate)
  183. (invalid-regexp nil))))))
  184. (defun helm-mm-3-search-base (pattern searchfn1 searchfn2)
  185. "Try to find PATTERN in `helm-buffer' with SEARCHFN1 and SEARCHFN2.
  186. This is the search function for `candidates-in-buffer' enabled sources.
  187. Use the same method as `helm-mm-3-match' except it search in buffer
  188. instead of matching on a string.
  189. i.e (identity (re-search-forward \"foo\" (point-at-eol) t)) => t."
  190. (cl-loop with pat = (if (stringp pattern)
  191. (helm-mm-3-get-patterns pattern)
  192. pattern)
  193. when (eq (caar pat) 'not) return
  194. ;; Pass the job to `helm-search-match-part'.
  195. (prog1 (list (point-at-bol) (point-at-eol))
  196. (forward-line 1))
  197. while (condition-case _err
  198. (funcall searchfn1 (or (cdar pat) "") nil t)
  199. (invalid-regexp nil))
  200. for bol = (point-at-bol)
  201. for eol = (point-at-eol)
  202. if (cl-loop for (pred . str) in (cdr pat) always
  203. (progn (goto-char bol)
  204. (funcall pred (condition-case _err
  205. (funcall searchfn2 str eol t)
  206. (invalid-regexp nil)))))
  207. do (goto-char eol) and return t
  208. else do (goto-char eol)
  209. finally return nil))
  210. (defun helm-mm-3-search (pattern &rest _ignore)
  211. (when (stringp pattern)
  212. (setq pattern (helm-mm-3-get-patterns pattern)))
  213. (helm-mm-3-search-base
  214. pattern 're-search-forward 're-search-forward))
  215. ;;; mp-3 with migemo
  216. ;; Needs https://github.com/emacs-jp/migemo
  217. ;;
  218. (defvar helm-mm--previous-migemo-info nil
  219. "[Internal] Cache previous migemo query.")
  220. (make-local-variable 'helm-mm--previous-migemo-info)
  221. (declare-function migemo-get-pattern "ext:migemo.el")
  222. (declare-function migemo-search-pattern-get "ext:migemo.el")
  223. (define-minor-mode helm-migemo-mode
  224. "Enable migemo in helm.
  225. It will be available in the sources handling it,
  226. i.e the sources which have the slot :migemo with non--nil value."
  227. :lighter " Hmio"
  228. :group 'helm
  229. :global t
  230. (cl-assert (featurep 'migemo)
  231. nil "No feature called migemo found, install migemo.el."))
  232. (defun helm-mm-migemo-get-pattern (pattern)
  233. (let ((regex (migemo-get-pattern pattern)))
  234. (if (ignore-errors (string-match regex "") t)
  235. (concat regex "\\|" pattern) pattern)))
  236. (defun helm-mm-migemo-search-pattern-get (pattern)
  237. (let ((regex (migemo-search-pattern-get pattern)))
  238. (if (ignore-errors (string-match regex "") t)
  239. (concat regex "\\|" pattern) pattern)))
  240. (defun helm-mm-migemo-string-match (pattern str)
  241. "Migemo version of `string-match'."
  242. (unless (assoc pattern helm-mm--previous-migemo-info)
  243. (with-helm-buffer
  244. (setq helm-mm--previous-migemo-info
  245. (push (cons pattern (helm-mm-migemo-get-pattern pattern))
  246. helm-mm--previous-migemo-info))))
  247. (string-match (assoc-default pattern helm-mm--previous-migemo-info) str))
  248. (cl-defun helm-mm-3-migemo-match (candidate &optional (pattern helm-pattern))
  249. (and helm-migemo-mode
  250. (cl-loop for (pred . re) in (helm-mm-3-get-patterns pattern)
  251. always (funcall pred (helm-mm-migemo-string-match re candidate)))))
  252. (defun helm-mm-migemo-forward (word &optional bound noerror count)
  253. (with-helm-buffer
  254. (unless (assoc word helm-mm--previous-migemo-info)
  255. (setq helm-mm--previous-migemo-info
  256. (push (cons word (if (delq 'ascii (find-charset-string word))
  257. word
  258. (helm-mm-migemo-search-pattern-get word)))
  259. helm-mm--previous-migemo-info))))
  260. (re-search-forward
  261. (assoc-default word helm-mm--previous-migemo-info) bound noerror count))
  262. (defun helm-mm-3-migemo-search (pattern &rest _ignore)
  263. (and helm-migemo-mode
  264. (helm-mm-3-search-base
  265. pattern 'helm-mm-migemo-forward 'helm-mm-migemo-forward)))
  266. ;;; mp-3p- (multiple regexp pattern 3 with prefix search)
  267. ;;
  268. ;;
  269. (defun helm-mm-3p-match (candidate &optional pattern)
  270. "Check if PATTERN match CANDIDATE.
  271. Same as `helm-mm-3-match' but only for the cdr of patterns, the car of
  272. patterns must always match CANDIDATE prefix.
  273. e.g \"bar foo baz\" will match \"barfoobaz\" or \"barbazfoo\" but not
  274. \"foobarbaz\" whereas `helm-mm-3-match' would match all."
  275. (let* ((pat (helm-mm-3-get-patterns (or pattern helm-pattern)))
  276. (first (car pat)))
  277. (and (funcall (car first) (helm-mm-prefix-match candidate (cdr first)))
  278. (cl-loop for (predicate . regexp) in (cdr pat)
  279. always (funcall predicate (string-match regexp candidate))))))
  280. (defun helm-mm-3p-search (pattern &rest _ignore)
  281. (when (stringp pattern)
  282. (setq pattern (helm-mm-3-get-patterns pattern)))
  283. (helm-mm-3-search-base
  284. pattern 'helm-mm-prefix-search 're-search-forward))
  285. ;;; Generic multi-match/search functions
  286. ;;
  287. ;;
  288. (cl-defun helm-mm-match (candidate &optional (pattern helm-pattern))
  289. "Call `helm-mm-matching-method' function against CANDIDATE."
  290. (let ((fun (cl-ecase helm-mm-matching-method
  291. (multi1 #'helm-mm-1-match)
  292. (multi2 #'helm-mm-2-match)
  293. (multi3 #'helm-mm-3-match)
  294. (multi3p #'helm-mm-3p-match))))
  295. (funcall fun candidate pattern)))
  296. (defun helm-mm-search (pattern &rest _ignore)
  297. "Search for PATTERN with `helm-mm-matching-method' function."
  298. (let ((fun (cl-ecase helm-mm-matching-method
  299. (multi1 #'helm-mm-1-search)
  300. (multi2 #'helm-mm-2-search)
  301. (multi3 #'helm-mm-3-search)
  302. (multi3p #'helm-mm-3p-search))))
  303. (funcall fun pattern)))
  304. (provide 'helm-multi-match)
  305. ;; Local Variables:
  306. ;; byte-compile-warnings: (not obsolete)
  307. ;; coding: utf-8
  308. ;; indent-tabs-mode: nil
  309. ;; End:
  310. ;;; helm-multi-match.el ends here