1 ;;; swiper.el --- Isearch with an overview. Oh, man! -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
8 ;; Package-Requires: ((emacs "24.1") (ivy "0.1.0"))
11 ;; This file is 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. The search regex can be split into groups with a
30 ;; space. Each group is highlighted with a different face.
32 ;; The overview back end is `ivy'.
34 ;; It can double as a quick `regex-builder', although only single
35 ;; lines will be matched.
41 "`isearch' with an overview."
45 (defface swiper-match-face-1
46 '((t (:inherit isearch-lazy-highlight-face)))
47 "Face for `swiper' matches.")
49 (defface swiper-match-face-2
50 '((t (:inherit isearch)))
51 "Face for `swiper' matches.")
53 (defface swiper-match-face-3
54 '((t (:inherit match)))
55 "Face for `swiper' matches.")
57 (defface swiper-match-face-4
58 '((t (:inherit isearch)))
59 "Face for `swiper' matches.")
61 (defface swiper-line-face
62 '((t (:inherit highlight)))
63 "Face for current `swiper' line.")
65 (defcustom swiper-faces '(swiper-match-face-1
69 "List of `swiper' faces for group matches.")
71 (defcustom swiper-min-highlight 2
72 "Only highlight matches for regexps at least this long."
75 (defvar swiper--window nil
76 "Store the current window.")
78 (defun swiper-font-lock-ensure ()
79 "Ensure the entired buffer is highlighted."
80 (unless (or (derived-mode-p 'magit-mode)
81 (memq major-mode '(package-menu-mode emms-playlist-mode)))
82 (if (fboundp 'font-lock-ensure)
84 (font-lock-fontify-buffer))))
86 (defvar swiper--format-spec ""
87 "Store the current candidates format spec.")
89 (defun swiper--candidates ()
90 "Return a list of this buffer lines."
91 (let ((n-lines (count-lines (point-min) (point-max))))
92 (unless (zerop n-lines)
93 (setq swiper--format-spec
94 (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
98 (goto-char (point-min))
99 (swiper-font-lock-ensure)
100 (while (< (point) (point-max))
101 (push (format swiper--format-spec
102 (cl-incf line-number)
104 (line-beginning-position)
105 (line-end-position)))
107 (zerop (forward-line 1)))
108 (nreverse candidates))))))
110 (defvar swiper--opoint 1
111 "The point when `swiper' starts.")
114 (defun swiper (&optional initial-input)
115 "`isearch' with an overview.
116 When non-nil, INITIAL-INPUT is the initial search pattern."
118 (swiper--ivy initial-input))
120 (defun swiper--init ()
121 "Perform initialization common to both completion methods."
123 (setq swiper--opoint (point))
125 (setq swiper--anchor (line-number-at-pos))
126 (setq swiper--window (selected-window)))
128 (defun swiper--ivy (&optional initial-input)
129 "`isearch' with an overview using `ivy'.
130 When non-nil, INITIAL-INPUT is the initial search pattern."
134 (let ((candidates (swiper--candidates))
139 (buffer-substring-no-properties
140 (line-beginning-position)
141 (line-end-position)))))
145 (replace-regexp-in-string
146 "%s" "pattern: " swiper--format-spec)
149 #'swiper--update-input-ivy
154 (goto-char swiper--opoint)
155 (swiper--action res ivy-text)))))
157 (defun swiper--ensure-visible ()
158 "Remove overlays hiding point."
159 (let ((overlays (overlays-at (point)))
161 (while (setq ov (pop overlays))
162 (if (and (invisible-p (overlay-get ov 'invisible))
163 (setq expose (overlay-get ov 'isearch-open-invisible)))
164 (funcall expose ov)))))
166 (defun swiper--cleanup ()
167 "Clean up the overlays."
168 (while swiper--overlays
169 (delete-overlay (pop swiper--overlays)))
171 (goto-char (point-min))
172 (isearch-clean-overlays)))
174 (defvar swiper--overlays nil
177 (defvar swiper--anchor nil
178 "A line number to which the search should be anchored.")
180 (defvar swiper--len 0
181 "The last length of input for which an anchoring was made.")
183 (defun swiper--update-input-ivy ()
184 "Called when `ivy' input is updated."
186 (let* ((re (ivy--regex ivy-text))
188 (num (if (string-match "^[0-9]+" str)
189 (string-to-number (match-string 0 str))
191 (with-selected-window swiper--window
192 (goto-char (point-min))
194 (goto-char (point-min))
195 (forward-line (1- num))
196 (isearch-range-invisible (line-beginning-position)
198 (unless (and (> (point) (window-start))
199 (< (point) (window-end swiper--window t)))
201 (let ((ov (make-overlay
202 (line-beginning-position)
203 (1+ (line-end-position)))))
204 (overlay-put ov 'face 'swiper-line-face)
205 (overlay-put ov 'window swiper--window)
206 (push ov swiper--overlays))
207 (swiper--add-overlays
209 (window-start swiper--window)
210 (window-end swiper--window t)))))
212 (defun swiper--add-overlays (re beg end)
213 "Add overlays for RE regexp in current buffer between BEG and END."
214 (when (>= (length re) swiper-min-highlight)
217 ;; RE can become an invalid regexp
218 (while (and (ignore-errors (re-search-forward re end t))
219 (> (- (match-end 0) (match-beginning 0)) 0))
221 (while (<= i ivy--subexps)
222 (when (match-beginning i)
223 (let ((overlay (make-overlay (match-beginning i)
226 (cond ((zerop ivy--subexps)
227 (cl-caddr swiper-faces))
231 (nth (1+ (mod (1- i) (1- (length swiper-faces))))
233 (push overlay swiper--overlays)
234 (overlay-put overlay 'face face)
235 (overlay-put overlay 'window swiper--window)
236 (overlay-put overlay 'priority i)))
239 (defun swiper--action (x input)
240 "Goto line X and search for INPUT."
242 (user-error "No candidates")
243 (goto-char (point-min))
244 (forward-line (1- (read x)))
246 (ivy--regex input) (line-end-position) t)
247 (swiper--ensure-visible)
248 (when (/= (point) swiper--opoint)
249 (unless (and transient-mark-mode mark-active)
250 (push-mark swiper--opoint t)
251 (message "Mark saved where search started")))))
255 ;;; swiper.el ends here