1 ;;; swiper.el --- Isearch with a helm overview. Oh, man! -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Oleh Krehel
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
8 ;; Package-Requires: ((helm "1.6.7") (emacs "24.1"))
11 ;; This file is not part of GNU Emacs
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
28 ;; This package gives an overview of the current regex search
29 ;; candidates in a `helm' buffer. The search regex can be split into
30 ;; groups with a space. Each group is highlighted with a different
33 ;; It can double as a quick `regex-builder', although only single
34 ;; lines will be matched.
40 "Interactive `occur' using `helm'."
44 (defface swiper-match-face-1
45 '((t (:background "#FEEA89")))
46 "Face for `swiper' matches.")
48 (defface swiper-match-face-2
49 '((t (:background "#F9A35A")))
50 "Face for `swiper' matches.")
52 (defface swiper-match-face-3
53 '((t (:background "#fb7905")))
54 "Face for `swiper' matches.")
56 (defface swiper-match-face-4
57 '((t (:background "#F15C79")))
58 "Face for `swiper' matches.")
60 (defface swiper-line-face
61 '((t (:background "#f3d3d3")))
62 "Face for current `swiper' line.")
64 (defcustom swiper-faces '(swiper-match-face-1
68 "List of `swiper' faces for group matches.")
70 (defvar swiper--buffer nil
71 "Store current buffer.")
73 (defalias 'swiper-font-lock-ensure
74 (if (fboundp 'font-lock-ensure)
76 'font-lock-fontify-buffer))
78 (defun swiper--candidates ()
79 "Return a list of this buffer lines."
80 (let* ((line-width (1+ (floor (log (count-lines
81 (point-min) (point-max))
83 (fspec (format "%%-%dd %%s" line-width))
87 (goto-char (point-min))
88 (swiper-font-lock-ensure)
89 (while (< (point) (point-max))
93 (line-beginning-position)
96 (zerop (forward-line 1)))
97 (nreverse candidates))))
101 "Interactive `occur' using `helm'."
105 (setq swiper--anchor (line-number-at-pos))
107 (let ((helm-display-function
110 (split-window-vertically))
112 (switch-to-buffer buf)))
113 helm-candidate-number-limit)
115 `((name . ,(buffer-name))
117 (setq swiper--buffer (current-buffer))
118 (add-hook 'helm-move-selection-after-hook
119 #'swiper--update-sel)
120 (add-hook 'helm-update-hook
121 #'swiper--update-input)
122 (add-hook 'helm-after-update-hook
123 #'swiper--reanchor)))
124 (match-strict . (lambda (x) (ignore-errors
125 (string-match (swiper--regex) x))))
126 (candidates . ,(swiper--candidates))
127 (filtered-candidate-transformer
128 helm-fuzzy-highlight-matches)
129 (action . swiper--action))
131 (format "^%d " swiper--anchor)
134 (remove-hook 'helm-move-selection-after-hook
135 #'swiper--update-sel)
136 (remove-hook 'helm-update-hook
137 #'swiper--update-input)
138 (remove-hook 'helm-after-update-hook
140 (while swiper--overlays
141 (delete-overlay (pop swiper--overlays)))))
143 (defvar swiper--overlays nil
146 (defvar swiper--anchor nil
147 "A line number to which the search should be anchored.")
149 (defvar swiper--len 0
150 "The last length of `helm-input' for which an anchoring was made.")
152 (defun swiper--update-input ()
154 (with-current-buffer swiper--buffer
155 (let ((re (swiper--regex))
156 (we (window-end nil t)))
157 (while swiper--overlays
158 (delete-overlay (pop swiper--overlays)))
159 (when (> (length helm-input) 1)
161 (goto-char (window-start))
162 (while (ignore-errors (re-search-forward re we t))
164 (while (<= i swiper--subexps)
165 (when (match-beginning i)
166 (let ((overlay (make-overlay (match-beginning i)
169 (cond ((zerop swiper--subexps)
170 (cl-caddr swiper-faces))
174 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
176 (push overlay swiper--overlays)
177 (overlay-put overlay 'face face)
178 (overlay-put overlay 'priority i)
180 (when (/= (length helm-input) swiper--len)
181 (setq swiper--len (length helm-input))
184 (defun swiper--binary (beg end)
185 "Find anchor between BEG and END."
186 (if (<= (- end beg) 10)
191 (goto-char (point-min))
192 (forward-line (1- beg))
195 (setq n (read (current-buffer)))
196 (when (< (setq d (abs (- n swiper--anchor))) min)
201 (goto-char (point-min))
203 (forward-line (1- ln))))
204 (let ((mid (+ beg (/ (- end beg) 2))))
205 (goto-char (point-min))
208 (let ((n (read (current-buffer))))
209 (if (> n swiper--anchor)
210 (swiper--binary beg mid)
211 (swiper--binary mid end))))))
213 (defun swiper--update-sel ()
215 (let* ((re (swiper--regex))
216 (str (buffer-substring-no-properties
217 (line-beginning-position)
218 (line-end-position)))
219 (num (if (string-match "^[0-9]+" str)
220 (string-to-number (match-string 0 str))
223 (when (> (length re) 0)
224 (with-current-buffer swiper--buffer
225 (goto-char (point-min))
226 (forward-line (1- num))
227 (when (re-search-forward re (point-max) t)
228 (setq pt (match-beginning 0))))
230 (with-selected-window
231 (helm-persistent-action-display-window)
234 (swiper--update-input))))
235 (with-current-buffer swiper--buffer
236 (let ((ov (make-overlay
237 (line-beginning-position)
238 (1+ (line-end-position)))))
239 (overlay-put ov 'face 'swiper-line-face)
240 (push ov swiper--overlays)))))
242 (defun swiper--reanchor ()
243 "Move to a valid match closest to `swiper--anchor'."
245 (goto-char (point-min))
246 (if (re-search-forward (format "^%d " swiper--anchor) nil t)
249 (swiper--binary 2 (1+ (count-lines (point) (point-max)))))
250 (when (> (count-lines (point-min) (point-max)) 1)
252 (helm-next-line 1))))
254 (defvar swiper--subexps 1
255 "Number of groups in `(swiper--regex)'.")
257 (defvar swiper--regex-hash
258 (make-hash-table :test 'equal)
259 "Store pre-computed regex.")
261 (defun swiper--regex ()
262 "Re-build regex in case it has a space."
263 (let ((hashed (gethash helm-input swiper--regex-hash)))
266 (setq swiper--subexps (car hashed)))
267 (cdr (puthash helm-input
268 (let ((subs (split-string helm-input " +" t)))
269 (if (= (length subs) 1)
271 (setq swiper--subexps 0)
274 (setq swiper--subexps (length subs))
276 (lambda (x) (format "\\(%s\\)" x))
279 swiper--regex-hash)))))
281 (defun swiper--action (x)
283 (goto-char (point-min))
284 (forward-line (1- (read x)))
286 (swiper--regex) (line-end-position) t))
290 ;;; swiper.el ends here