]> code.delx.au - gnu-emacs-elpa/blob - packages/ioccur/ioccur.el
Clean up copyright notices.
[gnu-emacs-elpa] / packages / ioccur / ioccur.el
1 ;;; ioccur.el --- Incremental occur
2
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
4
5 ;; Author: Thierry Volpiatto <thierry dot volpiatto at gmail dot com>
6 ;; X-URL: https://github.com/thierryvolpiatto/ioccur
7 ;; Version: 2.4
8 ;; Compatibility: GNU Emacs >=22.3
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 3, or
13 ;; (at your option) any later version.
14 ;;
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
19 ;;
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Install:
24 ;;
25 ;; Add this file to your `load-path', BYTE-COMPILE it and
26 ;; add (require 'ioccur) in your .emacs.
27 ;;
28 ;; Start with (C-u) M-x ioccur
29 ;; or
30 ;; (C-u) M-x ioccur-find-buffer-matching
31 ;;
32 ;; Do C-h f ioccur or ioccur-find-buffer-matching for more info.
33
34 ;;; Commentary:
35 ;;
36 ;; This package provides the command M-x ioccur, which is similar to
37 ;; M-x occur, except that it is incremental.
38 ;;
39 ;; You can jump and quit to an occurrence, or jump and save the search
40 ;; buffer (ioccur-buffer) for further use. You can toggle literal and
41 ;; regexp searching while running. It is auto documented both in
42 ;; mode-line and tooltip. It has its own history, `ioccur-history',
43 ;; which is a real ring.
44 ;;
45 ;; To save `ioccur-history' via the Desktop package, add this to your
46 ;; init file (see (info "(emacs) Saving Emacs Sessions") for details):
47 ;;
48 ;; (add-to-list 'desktop-globals-to-save 'ioccur-history)
49
50 ;;; Code:
51 (require 'derived)
52 (eval-when-compile (require 'cl))
53 (require 'outline)
54 (eval-when-compile (require 'wdired))
55
56 (defvar ioccur-mode-map
57 (let ((map (make-sparse-keymap)))
58 (define-key map (kbd "q") 'ioccur-quit)
59 (define-key map (kbd "RET") 'ioccur-jump-and-quit)
60 (define-key map (kbd "<left>") 'ioccur-jump-and-quit)
61 (define-key map (kbd "<right>") 'ioccur-jump-without-quit)
62 (define-key map (kbd "C-z") 'ioccur-jump-without-quit)
63 (define-key map (kbd "<C-down>") 'ioccur-scroll-down)
64 (define-key map (kbd "<C-up>") 'ioccur-scroll-up)
65 (define-key map (kbd "C-v") 'ioccur-scroll-other-window-up)
66 (define-key map (kbd "M-v") 'ioccur-scroll-other-window-down)
67 (define-key map (kbd "<down>") 'ioccur-next-line)
68 (define-key map (kbd "<up>") 'ioccur-precedent-line)
69 (define-key map (kbd "C-n") 'ioccur-next-line)
70 (define-key map (kbd "C-p") 'ioccur-precedent-line)
71 (define-key map (kbd "R") 'ioccur-restart)
72 (define-key map (kbd "C-|") 'ioccur-split-window)
73 (define-key map (kbd "M-<") 'ioccur-beginning-of-buffer)
74 (define-key map (kbd "M->") 'ioccur-end-of-buffer)
75 map)
76 "Keymap used for ioccur commands.")
77
78
79 (defgroup ioccur nil
80 "Mode that provide incremental searching in buffer."
81 :prefix "ioccur-"
82 :group 'text)
83
84 ;;; User variables.
85 (defcustom ioccur-search-delay 0.5
86 "During incremental searching, display is updated all these seconds."
87 :group 'ioccur
88 :type 'integer)
89
90 (defcustom ioccur-search-prompt "Pattern: "
91 "Prompt used for `ioccur-occur'."
92 :group 'ioccur
93 :type 'string)
94
95 (defcustom ioccur-mode-line-string
96 (if (window-system)
97 " RET:Exit,C-g:Quit,C-j/left:Jump&quit,C-z/right:Jump,\
98 C-k/x:Kill(as sexp),M-p/n:Hist,C/M-v:Scroll,C-down/up:Follow,C-w:Yank tap"
99
100 " RET:Exit,C-g:Quit,C-j:Jump&quit,C-z:Jump,C-k/x:Kill(as sexp),\
101 S-/Tab:Hist,C-v/t:Scroll,C-d/u:Follow,C-w:Yank tap")
102
103 "Minimal documentation of `ioccur' commands displayed in mode-line.
104 Set it to nil to remove doc in mode-line."
105 :group 'ioccur
106 :type 'string)
107
108 (defcustom ioccur-length-line 80
109 "Length of the line displayed in ioccur buffer.
110 When set to nil lines displayed in `ioccur-buffer' will not be modified.
111 See `ioccur-truncate-line'."
112 :group 'ioccur
113 :type 'integer)
114
115 (defcustom ioccur-max-length-history 100
116 "Maximum number of element stored in `ioccur-history'."
117 :group 'ioccur
118 :type 'integer)
119
120 (defcustom ioccur-buffer-completion-use-ido nil
121 "Use ido to choose buffers in `ioccur-find-buffer-matching'."
122 :group 'ioccur
123 :type 'symbol)
124
125 (defcustom ioccur-default-search-function 're-search-forward
126 "Default search function.
127 Use here one of `re-search-forward' or `search-forward'."
128 :group 'ioccur
129 :type 'symbol)
130
131 (defcustom ioccur-highlight-match-p t
132 "Highlight matchs in `ioccur-buffer' when non--nil."
133 :group 'ioccur
134 :type 'boolean)
135
136 (defcustom ioccur-fontify-buffer-p nil
137 "Fontify `ioccur-current-buffer' when non--nil.
138 This allow to have syntactic coloration in `ioccur-buffer' but
139 it slow down the start of ioccur at first time on large buffers."
140 :group 'ioccur
141 :type 'boolean)
142
143 (defcustom ioccur-case-fold-search 'smart
144 "Add 'smart' option to `case-fold-search'.
145 When smart is enabled, Ignore case in the search strings
146 if pattern contains no uppercase characters.
147 Otherwise, with a nil or t value, the behavior is same as
148 `case-fold-search'.
149 Default value is smart, other possible values are nil and t."
150 :group 'ioccur
151 :type 'symbol)
152
153 (defvar ioccur-read-char-or-event-skip-read-key nil
154 "Force not using `read-key' to read input in minibuffer even if bounded.
155 Set it to non--nil if menu disapear or if keys are echoing in minibuffer.
156 Deprecated, should be used only in old Emacs versions.")
157
158 ;;; Faces.
159 (defface ioccur-overlay-face
160 '((t (:background "Green4" :underline t)))
161 "Face for highlight line in ioccur buffer."
162 :group 'ioccur-faces)
163
164 (defface ioccur-match-overlay-face
165 '((t (:background "Indianred4" :underline t)))
166 "Face for highlight line in matched buffer."
167 :group 'ioccur-faces)
168
169 (defface ioccur-title-face
170 '((t (:background "Dodgerblue4")))
171 "Face for highlight incremental buffer title."
172 :group 'ioccur-faces)
173
174 (defface ioccur-regexp-face
175 '((t (:background "DeepSkyBlue" :underline t)))
176 "Face for highlight found regexp in `ioccur-buffer'."
177 :group 'ioccur-faces)
178
179 (defface ioccur-match-face
180 '((t (:background "DeepSkyBlue")))
181 "Face for highlight matches in `ioccur-buffer'."
182 :group 'ioccur-faces)
183
184 (defface ioccur-num-line-face
185 '((t (:foreground "OrangeRed")))
186 "Face for highlight number line in ioccur buffer."
187 :group 'ioccur-faces)
188
189 (defface ioccur-invalid-regexp
190 '((t (:foreground "Goldenrod")))
191 "Face for highlight wrong regexp message in ioccur buffer."
192 :group 'ioccur-faces)
193
194 (defface ioccur-cursor
195 '((t (:foreground "green")))
196 "Face for cursor color in minibuffer."
197 :group 'ioccur-faces)
198
199 ;;; Internal variables.
200 ;; String entered in prompt.
201 (defvar ioccur-pattern "")
202 ;; The ioccur timer.
203 (defvar ioccur-search-timer nil)
204 ;; Signal C-g hit.
205 (defvar ioccur-quit-flag nil)
206 ;; The buffer we search in.
207 (defvar ioccur-current-buffer nil)
208 ;; The overlay in `ioccur-buffer'.
209 (defvar ioccur-occur-overlay nil)
210 (make-variable-buffer-local 'ioccur-occur-overlay)
211 ;; Signal we quit and kill `ioccur-buffer'.
212 (defvar ioccur-exit-and-quit-p nil)
213 ;; A list to store history.
214 (defvar ioccur-history nil)
215 ;; The overlay in `ioccur-current-buffer'.
216 (defvar ioccur-match-overlay nil)
217 ;; Number of occurences found.
218 (defvar ioccur-count-occurences 0)
219 ;;The buffer where we send results.
220 (defvar ioccur-buffer nil)
221 (make-variable-buffer-local 'ioccur-buffer)
222 ;; True when jumping to a founded occurence.
223 (defvar ioccur-success nil)
224 ;; Search function actually in use.
225 (defvar ioccur-search-function ioccur-default-search-function)
226 ;; Message to send when ioccur exit
227 (defvar ioccur-message nil)
228 ;; Store last window-configuration
229 (defvar ioccur-last-window-configuration nil)
230
231
232 (define-derived-mode ioccur-mode
233 text-mode "ioccur"
234 "Major mode to search occurences of regexp in current buffer.
235
236 Special commands:
237 \\{ioccur-mode-map}"
238 (if ioccur-mode-line-string
239 (setq mode-line-format
240 '(" " mode-line-buffer-identification " "
241 (line-number-mode "%l") " "
242 ioccur-mode-line-string "-%-"))
243 (kill-local-variable 'mode-line-format)))
244
245 (defsubst* ioccur-position (item seq &key (test 'eq))
246 "A simple replacement of CL `position'."
247 (loop for i in seq for index from 0
248 when (funcall test i item) return index))
249
250 ;; Compatibility
251 (unless (fboundp 'window-system)
252 (defun window-system (&optional arg)
253 window-system))
254
255 ;;; Iterators.
256 (defmacro ioccur-iter-list (list-obj)
257 "Return an iterator from list LIST-OBJ."
258 `(lexical-let ((lis ,list-obj))
259 (lambda ()
260 (let ((elm (car lis)))
261 (setq lis (cdr lis))
262 elm))))
263
264 (defun ioccur-iter-next (iterator)
265 "Return next elm of ITERATOR."
266 (funcall iterator))
267
268 (defun ioccur-iter-circular (seq)
269 "Infinite iteration on SEQ."
270 (lexical-let ((it (ioccur-iter-list seq))
271 (lis seq))
272 (lambda ()
273 (let ((elm (ioccur-iter-next it)))
274 (or elm
275 (progn (setq it (ioccur-iter-list lis))
276 (ioccur-iter-next it)))))))
277
278 (defun ioccur-butlast (seq pos)
279 "Return SEQ from index 0 to POS."
280 (butlast seq (- (length seq) pos)))
281
282 (defun* ioccur-sub-prec-circular (seq elm &key (test 'eq))
283 "Infinite reverse iteration of SEQ starting at ELM."
284 (lexical-let* ((rev-seq (reverse seq))
285 (pos (ioccur-position elm rev-seq :test test))
286 (sub (append (nthcdr (1+ pos) rev-seq)
287 (ioccur-butlast rev-seq pos)))
288 (iterator (ioccur-iter-list sub)))
289 (lambda ()
290 (let ((elm (ioccur-iter-next iterator)))
291 (or elm
292 (progn (setq iterator (ioccur-iter-list sub))
293 (ioccur-iter-next iterator)))))))
294
295 (defun* ioccur-sub-next-circular (seq elm &key (test 'eq))
296 "Infinite iteration of SEQ starting at ELM."
297 (lexical-let* ((pos (ioccur-position elm seq :test test))
298 (sub (append (nthcdr (1+ pos) seq)
299 (ioccur-butlast seq pos)))
300 (iterator (ioccur-iter-list sub)))
301 (lambda ()
302 (let ((elm (ioccur-iter-next iterator)))
303 (or elm (progn
304 (setq iterator (ioccur-iter-list sub))
305 (ioccur-iter-next iterator)))))))
306
307 (defun ioccur-print-results (regexp)
308 "Print in `ioccur-buffer' lines matching REGEXP in `ioccur-current-buffer'."
309 (setq ioccur-count-occurences 0)
310 (with-current-buffer ioccur-current-buffer
311 (let ((case-fold-search (case ioccur-case-fold-search
312 (smart (let ((case-fold-search nil))
313 (if (string-match "[A-Z]" regexp) nil t)))
314 (t ioccur-case-fold-search))))
315 (save-excursion
316 (goto-char (point-min))
317 (loop
318 while (not (eobp))
319 ;; We need to read also C-g from here
320 ;; Because when loop is started `ioccur-read-search-input'
321 ;; will read key only when loop is finished
322 ;; and we have no chance to exit loop.
323 when quit-flag do (setq ioccur-quit-flag t) and return nil
324 for count from 0
325 when (funcall ioccur-search-function regexp (point-at-eol) t)
326 do (ioccur-print-line
327 (buffer-substring (point-at-bol) (point-at-eol))
328 count (match-string 0) regexp)
329 do (forward-line 1))))))
330
331 (defun ioccur-print-match (str &optional all)
332 "Highlight in string STR all occurences matching `ioccur-pattern'.
333 If ALL is non--nil highlight the whole string STR."
334 (condition-case nil
335 (with-temp-buffer
336 (insert str)
337 (goto-char (point-min))
338 (if all
339 (add-text-properties
340 (point) (point-at-eol)
341 '(face ioccur-match-face))
342 (while (and (funcall ioccur-search-function ioccur-pattern nil t)
343 ;; Don't try to highlight line with a length <= 0.
344 (> (- (match-end 0) (match-beginning 0)) 0))
345 (add-text-properties
346 (match-beginning 0) (match-end 0)
347 '(face ioccur-match-face))))
348 (buffer-string))
349 (error nil)))
350
351 (defun ioccur-print-line (line nline match regexp)
352 "Prepare and insert a matched LINE at line number NLINE in `ioccur-buffer'."
353 (with-current-buffer ioccur-buffer
354 (let* ((lineno (int-to-string (1+ nline)))
355 (whole-line-matched (string= match line))
356 (hightline (if ioccur-highlight-match-p
357 (ioccur-print-match
358 line
359 whole-line-matched)
360 line))
361 (trunc-line (ioccur-truncate-line hightline)))
362 (incf ioccur-count-occurences)
363 (insert " " (propertize lineno 'face 'ioccur-num-line-face
364 'help-echo line)
365 ":" trunc-line "\n"))))
366
367 (defun* ioccur-truncate-line (line &optional (columns ioccur-length-line))
368 "Remove indentation in LINE and truncate modified LINE of num COLUMNS.
369 COLUMNS default value is `ioccur-length-line'.
370 If COLUMNS is nil return original indented LINE.
371 If COLUMNS is 0 only remove indentation in LINE.
372 So just set `ioccur-length-line' to nil if you don't want lines truncated."
373 (let ((old-line line))
374 (when (string-match "^[[:blank:]]*" line)
375 ;; Remove tab and spaces at beginning of LINE.
376 (setq line (replace-match "" nil nil line)))
377 (if (and columns (> columns 0) (> (length line) columns))
378 (substring line 0 columns)
379 (if columns line old-line))))
380
381 (defun ioccur-buffer-contain (buffer regexp)
382 "Return BUFFER if it contain an occurence of REGEXP."
383 (with-current-buffer buffer
384 (save-excursion
385 (goto-char (point-min))
386 (when (re-search-forward regexp nil t) buffer))))
387
388 (defun ioccur-list-buffers-matching (buffer-match regexp buffer-list)
389 "Collect all buffers in BUFFER-LIST whose names match BUFFER-MATCH and \
390 contain lines matching REGEXP."
391 (loop
392 with ini-buf-list = (loop for buf in buffer-list
393 unless (rassq buf dired-buffers)
394 collect buf)
395 for buf in ini-buf-list
396 for bname = (buffer-name buf)
397 when (and (string-match buffer-match bname)
398 (ioccur-buffer-contain buf regexp))
399 collect bname))
400
401 (defun ioccur-list-buffers-containing (regexp buffer-list)
402 "Collect all buffers in BUFFER-LIST containing lines matching REGEXP."
403 (loop with buf-list = (loop for i in buffer-list
404 when (buffer-file-name (get-buffer i))
405 collect i)
406 for buf in buf-list
407 when (ioccur-buffer-contain buf regexp)
408 collect (buffer-name buf)))
409
410 (defun* ioccur-find-buffer-matching1 (regexp
411 &optional
412 match-buf-name
413 (buffer-list (buffer-list)))
414 "Find all buffers containing a text matching REGEXP \
415 and connect `ioccur' to the selected one.
416
417 If MATCH-BUF-NAME is non--nil search is performed only in buffers
418 with name matching specified expression (prompt).
419
420 Hitting C-g in a `ioccur' session will return to completion list.
421 Hitting C-g in the completion list will jump back to initial buffer.
422
423 The buffer completion list is provided by one of:
424 `ido-completing-read', `completing-read'
425 depending on which `ioccur-buffer-completion-use-ido' you have choosen."
426 ;; Remove doublons maybe added by minibuffer in `ioccur-history'.
427 (setq ioccur-history
428 (loop with hist for i in ioccur-history
429 when (not (member i hist)) collect i into hist
430 finally return hist))
431
432 (let ((prompt (format "Search (%s) in Buffer: " regexp))
433 (win-conf (current-window-configuration))
434 (buf-list (if match-buf-name
435 (ioccur-list-buffers-matching
436 (read-string "In Buffer names matching: ")
437 regexp buffer-list)
438 (ioccur-list-buffers-containing regexp buffer-list))))
439
440 (labels
441 ((find-buffer ()
442 (let ((buf (if (and ido-mode
443 (eq ioccur-buffer-completion-use-ido 'ido))
444 (ido-completing-read prompt buf-list nil t)
445 (completing-read prompt buf-list nil t))))
446 (unwind-protect
447 (progn
448 (switch-to-buffer buf)
449 (ioccur regexp)
450 ;; Exit if we jump to this `ioccur-current-buffer',
451 ;; otherwise, if C-g is hitten,
452 ;; go back to buffer completion list.
453 (unless ioccur-success
454 (find-buffer)))
455 ;; C-g hit in buffer completion restore window config.
456 (unless ioccur-success
457 (set-window-configuration win-conf))))))
458
459 (find-buffer))))
460
461 ;;;###autoload
462 (defun ioccur-find-buffer-matching (regexp)
463 "Find all buffers containing a text matching REGEXP.
464 See `ioccur-find-buffer-matching1'."
465 (interactive (list (let ((savehist-save-minibuffer-history nil))
466 (read-from-minibuffer "Search for Pattern: "
467 nil nil nil '(ioccur-history . 0)
468 (thing-at-point 'symbol)))))
469 (ioccur-find-buffer-matching1 regexp current-prefix-arg))
470
471 ;;; Ioccur dired
472 ;;;###autoload
473 (defun ioccur-dired (regexp)
474 (interactive (list (let ((savehist-save-minibuffer-history nil))
475 (read-from-minibuffer "Search for Pattern: "
476 nil nil nil '(ioccur-history . 0)
477 (thing-at-point 'symbol)))))
478 (let ((buf-list (loop for f in (dired-get-marked-files)
479 do (find-file-noselect f)
480 unless (file-directory-p f)
481 collect (get-buffer (file-name-nondirectory f)))))
482 (ioccur-find-buffer-matching1 regexp nil buf-list)))
483
484 ;;;###autoload
485 (defun ioccur-restart ()
486 "Restart `ioccur' from `ioccur-buffer'.
487 `ioccur-buffer' is erased and a new search is started."
488 (interactive)
489 (when (eq major-mode 'ioccur-mode)
490 (pop-to-buffer ioccur-current-buffer)
491 (kill-buffer ioccur-buffer)
492 (set-window-configuration ioccur-last-window-configuration)
493 (ioccur)))
494
495 ;;;###autoload
496 (defun ioccur-quit ()
497 "Quit `ioccur-buffer'."
498 (interactive)
499 (let ((pos (with-current-buffer ioccur-current-buffer (point))))
500 (when ioccur-match-overlay
501 (delete-overlay ioccur-match-overlay))
502 (quit-window)
503 (set-window-configuration ioccur-last-window-configuration)
504 (pop-to-buffer ioccur-current-buffer)
505 (goto-char pos)))
506
507 (defun ioccur-goto-line (lineno)
508 "Goto LINENO without modifying outline visibility if needed."
509 (flet ((gotoline (numline)
510 (goto-char (point-min)) (forward-line (1- numline))))
511 (if (or (eq major-mode 'org-mode)
512 outline-minor-mode)
513 (progn
514 (gotoline lineno)
515 (org-reveal))
516 (gotoline lineno))))
517
518 (defun ioccur-forward-line (n)
519 "Forward N lines but empty one's."
520 (let (pos)
521 (save-excursion
522 (forward-line n) (forward-line 0)
523 (when (looking-at "^\\s-[0-9]+:")
524 (forward-line 0) (setq pos (point))))
525 (when pos (goto-char pos) (ioccur-color-current-line))))
526
527 ;;;###autoload
528 (defun ioccur-next-line ()
529 "Goto next line if it is not an empty line."
530 (interactive)
531 (ioccur-forward-line 1))
532
533 ;;;###autoload
534 (defun ioccur-precedent-line ()
535 "Goto precedent line if it is not an empty line."
536 (interactive)
537 (ioccur-forward-line -1))
538
539 ;;;###autoload
540 (defun ioccur-beginning-of-buffer ()
541 "Goto beginning of `ioccur-buffer'."
542 (interactive)
543 (when (looking-at "^\\s-[0-9]+:")
544 (goto-char (point-min))
545 (re-search-forward "^\\s-[0-9]+:" nil t)
546 (forward-line 0)
547 (ioccur-color-current-line)))
548
549 ;;;###autoload
550 (defun ioccur-end-of-buffer ()
551 "Go to end of `ioccur-buffer'."
552 (interactive)
553 (when (looking-at "^\\s-[0-9]+:")
554 (goto-char (point-max))
555 (forward-line -1)
556 (ioccur-color-current-line)))
557
558 (defun ioccur-jump (&optional win-conf)
559 "Jump to line in other buffer and put an overlay on it.
560 Move point to first occurence of `ioccur-pattern'."
561 (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
562 (pos (string-to-number line))
563 (back-search-fn (if (eq ioccur-search-function 're-search-forward)
564 're-search-backward 'search-backward)))
565 (unless (string= line "")
566 (if win-conf
567 (set-window-configuration win-conf)
568 (pop-to-buffer ioccur-current-buffer))
569 (ioccur-goto-line pos)
570 (recenter)
571 ;; Go to beginning of first occurence in this line
572 ;; of what match `ioccur-pattern'.
573 (when (funcall ioccur-search-function
574 ioccur-pattern (point-at-eol) t)
575 (goto-char (match-beginning 0)))
576 (ioccur-color-matched-line))))
577
578 ;;;###autoload
579 (defun ioccur-jump-and-quit ()
580 "Jump to line in other buffer and quit search buffer."
581 (interactive)
582 (when (ioccur-jump ioccur-last-window-configuration)
583 (sit-for 0.3)
584 (when ioccur-match-overlay
585 (delete-overlay ioccur-match-overlay))))
586
587 ;;;###autoload
588 (defun ioccur-jump-without-quit (&optional mark)
589 "Jump to line in `ioccur-current-buffer' without quitting."
590 (interactive)
591 (when (ioccur-jump ioccur-last-window-configuration)
592 (and mark (set-marker (mark-marker) (point))
593 (push-mark (point) 'nomsg))
594 (switch-to-buffer-other-window ioccur-buffer t)))
595
596 ;;;###autoload
597 (defun ioccur-scroll-other-window-down ()
598 "Scroll other window down."
599 (interactive)
600 (let ((other-window-scroll-buffer ioccur-current-buffer))
601 (scroll-other-window 1)))
602
603 ;;;###autoload
604 (defun ioccur-scroll-other-window-up ()
605 "Scroll other window up."
606 (interactive)
607 (let ((other-window-scroll-buffer ioccur-current-buffer))
608 (scroll-other-window -1)))
609
610 (defun ioccur-scroll (n)
611 "Scroll `ioccur-buffer' and `ioccur-current-buffer' simultaneously."
612 (ioccur-forward-line n)
613 (ioccur-color-current-line)
614 (and (ioccur-jump ioccur-last-window-configuration)
615 (switch-to-buffer-other-window ioccur-buffer t)))
616
617 ;;;###autoload
618 (defun ioccur-scroll-down ()
619 "Scroll down `ioccur-buffer' and `ioccur-current-buffer' simultaneously."
620 (interactive)
621 (ioccur-scroll 1))
622
623 ;;;###autoload
624 (defun ioccur-scroll-up ()
625 "Scroll up `ioccur-buffer' and `ioccur-current-buffer' simultaneously."
626 (interactive)
627 (ioccur-scroll -1))
628
629 ;;;###autoload
630 (defun ioccur-split-window ()
631 "Toggle split window, vertically or horizontally."
632 (interactive)
633 (with-current-buffer ioccur-current-buffer
634 (let ((old-size (window-height)))
635 (delete-window)
636 (set-window-buffer
637 (select-window (if (= (window-height) old-size)
638 (split-window-vertically)
639 (split-window-horizontally)))
640 (get-buffer ioccur-buffer)))))
641
642 (defun ioccur-read-char-or-event (prompt)
643 "Replace `read-key' when not available using PROMPT."
644 (if (and (fboundp 'read-key)
645 (not ioccur-read-char-or-event-skip-read-key))
646 (read-key prompt)
647 (let* ((chr (condition-case nil (read-char prompt) (error nil)))
648 (evt (unless chr (read-event prompt))))
649 (or chr evt))))
650
651 (defun ioccur-read-search-input (initial-input start-point)
652 "Read each keyboard input and add it to `ioccur-pattern'.
653 INITIAL-INPUT is a string given as default input, generally thing at point.
654 START-POINT is the point where we start searching in buffer."
655 (let* ((prompt (propertize ioccur-search-prompt
656 'face 'minibuffer-prompt))
657 (inhibit-quit (or (eq system-type 'windows-nt)
658 (not (fboundp 'read-key))
659 ioccur-read-char-or-event-skip-read-key))
660 (tmp-list ())
661 (it-prec nil)
662 (it-next nil)
663 (cur-hist-elm (car ioccur-history))
664 (start-hist nil) ; Flag to notify if cycling history started.
665 yank-point
666 (index 0))
667 (unless (string= initial-input "")
668 (loop for char across initial-input do (push char tmp-list)))
669 (setq ioccur-pattern initial-input)
670 ;; Cycle history function.
671 ;;
672 (flet ((cycle-hist (arg)
673 ;; ARG can be positive or negative depending we call M-p or M-n.
674 (if ioccur-history
675 (progn
676 ;; Cycle history will start at second call,
677 ;; at first call just use the car of hist ring.
678 ;; We build a new iterator based on a sublist
679 ;; starting at the current element of history.
680 ;; This is a circular iterator. (no end)
681 (if start-hist ; At first call, start-hist is nil.
682 (progn
683 (if (< arg 0)
684 ;; M-p (move from left to right in hist ring).
685 (unless it-prec ; Don't rebuild iterator if exists.
686 (setq it-prec (ioccur-sub-next-circular
687 ioccur-history
688 cur-hist-elm :test 'equal))
689 (setq it-next nil)) ; Kill forward iterator.
690 ;; M-n (move from right to left in hist ring).
691 (unless it-next ; Don't rebuild iterator if exists.
692 (setq it-next (ioccur-sub-prec-circular
693 ioccur-history
694 cur-hist-elm :test 'equal))
695 (setq it-prec nil))) ; kill backward iterator.
696 (let ((it (or it-prec it-next)))
697 (setq cur-hist-elm (ioccur-iter-next it))
698 (setq tmp-list nil)
699 (loop for char across cur-hist-elm
700 do (push char tmp-list))
701 (setq ioccur-pattern cur-hist-elm)))
702 ;; First call use car of history ring.
703 (setq tmp-list nil)
704 (loop for char across cur-hist-elm
705 do (push char tmp-list))
706 (setq ioccur-pattern cur-hist-elm)
707 (setq start-hist t)))
708 (message "No history available.") (sit-for 2) t))
709 ;; Insert INITIAL-INPUT.
710 ;;
711 (insert-initial-input ()
712 (unless (string= initial-input "")
713 (loop for char across initial-input
714 do (push char (nthcdr index tmp-list)))))
715 ;; Maybe start timer.
716 ;;
717 (start-timer ()
718 (unless ioccur-search-timer
719 (ioccur-start-timer)))
720 ;; Maybe stop timer.
721 ;;
722 (stop-timer ()
723 (when ioccur-search-timer
724 (ioccur-cancel-search)))
725 ;; Kill pattern
726 ;;
727 (kill (str)
728 (with-current-buffer ioccur-current-buffer
729 (goto-char start-point)
730 (setq yank-point start-point))
731 (kill-new (substring str (- (length tmp-list) index)))
732 (setq tmp-list (nthcdr index tmp-list)))
733 ;; Add cursor in minibuffer
734 ;;
735 (set-cursor (str pos)
736 (setq pos (min index (1- (length tmp-list))))
737 (when (not (string= str ""))
738 (let* ((real-index (- (1- (length tmp-list)) pos))
739 (cur-str (substring str real-index (1+ real-index))))
740 (concat (substring str 0 real-index)
741 (propertize cur-str 'display
742 (if (= index (length tmp-list))
743 (concat
744 (propertize "|" 'face 'ioccur-cursor)
745 cur-str)
746 (concat
747 cur-str
748 (propertize "|" 'face 'ioccur-cursor))))
749 (substring str (1+ real-index)))))))
750
751 ;; Start incremental loop.
752 (while (let ((char (ioccur-read-char-or-event
753 (concat prompt (set-cursor ioccur-pattern index)))))
754 (message nil)
755 (case char
756 ((not (?\M-p ?\M-n ?\t C-tab)) ; Reset history
757 (setq start-hist nil)
758 (setq cur-hist-elm (car ioccur-history)) t)
759 ((down ?\C-n) ; Next line.
760 (stop-timer) (ioccur-next-line)
761 (ioccur-color-current-line) t)
762 ((up ?\C-p) ; Precedent line.
763 (stop-timer) (ioccur-precedent-line)
764 (ioccur-color-current-line) t)
765 (?\M-< ; Beginning of buffer.
766 (when (ioccur-beginning-of-buffer)
767 (stop-timer)) t)
768 (?\M-> ; End of buffer.
769 (when (ioccur-end-of-buffer)
770 (stop-timer)) t)
771 ((?\C-d C-down) ; Scroll both windows down.
772 (stop-timer) (ioccur-scroll-down) t)
773 ((?\C-u C-up) ; Scroll both windows up.
774 (stop-timer) (ioccur-scroll-up) t)
775 (?\r ; RET break and exit code.
776 nil)
777 (?\d ; Delete backward with DEL.
778 (start-timer)
779 (with-current-buffer ioccur-current-buffer
780 (goto-char start-point)
781 (setq yank-point start-point))
782 (with-no-warnings (pop (nthcdr index tmp-list)))
783 t)
784 (?\C-g ; Quit and restore buffers.
785 (setq ioccur-quit-flag t) nil)
786 ((right ?\C-z) ; Persistent action.
787 (ioccur-jump-without-quit) t)
788 ((?\C- ) ; Persistent action save mark.
789 (ioccur-jump-without-quit t) t)
790 ((left ?\C-j) ; Jump and kill search buffer.
791 (setq ioccur-exit-and-quit-p t) nil)
792 ((next ?\C-v) ; Scroll down.
793 (ioccur-scroll-other-window-down) t)
794 ((?\C-t ?\M-v prior) ; Scroll up.
795 (ioccur-scroll-other-window-up) t)
796 (?\C-s ; Toggle split window.
797 (ioccur-split-window) t)
798 ((?\C-: ?\C-l) ; Toggle regexp/litteral search.
799 (start-timer)
800 (if (eq ioccur-search-function 're-search-forward)
801 (setq ioccur-search-function 'search-forward)
802 (setq ioccur-search-function 're-search-forward)) t)
803 (?\C-k ; Kill input.
804 (start-timer)
805 (kill ioccur-pattern) (setq index 0) t)
806 ((?\M-k ?\C-x) ; Kill input as sexp.
807 (start-timer)
808 (let ((sexp (prin1-to-string ioccur-pattern)))
809 (kill sexp)
810 (setq ioccur-quit-flag t)
811 (setq ioccur-message (format "Killed: %s" sexp)))
812 nil)
813 (?\C-y ; Yank from `kill-ring'.
814 (setq initial-input (car kill-ring))
815 (insert-initial-input) t)
816 (?\C-w ; Yank stuff at point.
817 (start-timer)
818 (with-current-buffer ioccur-current-buffer
819 ;; Start to initial point if C-w have never been hit.
820 (unless yank-point (setq yank-point start-point))
821 ;; After a search `ioccur-print-results' have put point
822 ;; to point-max, so reset position.
823 (when yank-point (goto-char yank-point))
824 (let ((pmax (point-at-eol))
825 (eoword (save-excursion (forward-word 1) (point))))
826 ;; Don't yank further than eol.
827 (unless (> eoword pmax)
828 (goto-char eoword)
829 (setq initial-input (buffer-substring-no-properties
830 yank-point (point)))
831 (setq yank-point (point)) ; End of last forward-word
832 (insert-initial-input)))) t)
833 ((?\t ?\M-p) ; Precedent history elm.
834 (start-timer)
835 (setq index 0)
836 (cycle-hist -1))
837 ((backtab ?\M-n) ; Next history elm.
838 (start-timer)
839 (setq index 0)
840 (cycle-hist 1))
841 (?\C-q ; quoted-insert.
842 (stop-timer)
843 (let ((char (with-temp-buffer
844 (call-interactively 'quoted-insert)
845 (buffer-string))))
846 (push (string-to-char char) tmp-list))
847 (start-timer)
848 t)
849 ;; Movements in minibuffer
850 (?\C-b ; backward-char.
851 (setq index (min (1+ index) (length tmp-list))) t)
852 (?\C-f ; forward-char.
853 (setq index (max (1- index) 0)) t)
854 (?\C-a ; move bol.
855 (setq index (length tmp-list)) t)
856 (?\C-e ; move eol.
857 (setq index 0) t)
858 (t ; Store character.
859 (start-timer)
860 (if (characterp char)
861 (push char (nthcdr index tmp-list))
862 (setq unread-command-events
863 (nconc (mapcar 'identity
864 (this-single-command-raw-keys))
865 unread-command-events))
866 nil))))
867 (setq ioccur-pattern (apply 'string (reverse tmp-list)))))))
868
869 (defun ioccur-print-buffer (regexp)
870 "Pretty Print results matching REGEXP in `ioccur-buffer'."
871 (unless (window-system) (setq tooltip-use-echo-area t) (tooltip-mode 1))
872 (let* ((cur-method (if (eq ioccur-search-function 're-search-forward)
873 "Regexp" "Literal"))
874 (title (propertize
875 (format
876 "* Ioccur %s searching %s"
877 cur-method
878 (if (window-system)
879 "* (`C-:' to Toggle Method, Mouse over for help.)"
880 "* (`C-l' to Toggle Method.)"))
881 'face 'ioccur-title-face
882 'help-echo
883 " Ioccur map:\n
884 C-n or <down> Next line.\n
885 C-p or <up> Precedent line.\n
886 C-v and M-v/C-t Scroll up and down.\n
887 C-z or <right> Jump without quitting loop.\n
888 C-TAB Jump without quitting and save to mark-ring.\n
889 C-j or <left> Jump and kill `ioccur-buffer'.\n
890 RET Exit keeping `ioccur-buffer'.\n
891 DEL Remove last character entered.\n
892 C-k Kill current input.\n
893 C-a/e/b/f Movements in minibuffer.\n
894 M-k/C-x Kill current input as sexp.\n
895 C-w Yank stuff at point.\n
896 C-g Quit and restore buffer.\n
897 C-s Toggle split window.\n
898 C-:/l Toggle regexp/litteral search.\n
899 C-down or C-u Follow in other buffer.\n
900 C-up/d or C-d Follow in other buffer.\n
901 M-<, M-> Beginning and end of buffer.\n
902 M-p/n or tab/S-tab History."))
903 wrong-regexp)
904 (if (string= regexp "")
905 (progn (erase-buffer) (insert title "\n\n"))
906 (erase-buffer)
907 (condition-case err
908 (ioccur-print-results regexp)
909 (error (setq wrong-regexp t)))
910 (goto-char (point-min))
911 (if wrong-regexp
912 (insert
913 title "\n\n"
914 (propertize "Invalid Regexp: "
915 'face 'ioccur-invalid-regexp)
916 (format "No match for `%s'" regexp) "\n\n")
917 (insert title "\n\n"
918 (propertize (format "Found %s occurences matching "
919 ioccur-count-occurences)
920 'face 'underline)
921 (propertize regexp 'face 'ioccur-regexp-face)
922 (propertize
923 (format " in %s" ioccur-current-buffer)
924 'face 'underline) "\n\n")
925 (ioccur-color-current-line)))))
926
927 (defun ioccur-start-timer ()
928 "Start ioccur incremental timer."
929 (setq ioccur-search-timer
930 (run-with-idle-timer
931 ioccur-search-delay 'repeat
932 #'(lambda ()
933 (ioccur-print-buffer
934 ioccur-pattern)))))
935
936 (defun ioccur-send-message ()
937 "Send message defined in `ioccur-message'."
938 (message ioccur-message))
939
940 ;;;###autoload
941 (defun ioccur (&optional initial-input)
942 "Incremental search of lines in current buffer matching input.
943 With a prefix arg search symbol at point (INITIAL-INPUT).
944
945 While you are incremental searching, commands provided are:
946
947 C-n or <down> next line.
948 C-p or <up> precedent line.
949 C-v and M-v scroll up and down.
950 C-z or <right> jump without quitting loop.
951 C-j or <left> jump and kill `ioccur-buffer'.
952 RET exit keeping `ioccur-buffer'.
953 DEL remove last character entered.
954 C-k Kill current input from cursor to eol.
955 C-a/e/b/f Movements in minibuffer.
956 M-k Kill current input as sexp.
957 C-w Yank stuff at point.
958 C-g quit and restore buffer.
959 C-s Toggle split window.
960 C-: Toggle regexp/litteral search.
961 C-down Follow in other buffer.
962 C-up Follow in other buffer.
963 M-p/n Precedent and next `ioccur-history' element.
964 M-<, M-> Beginning and end of buffer.
965
966 Unlike minibuffer history, cycling in ioccur history have no end:
967
968 M-p ,-->A B C D E F G H I---,
969 | |
970 `---I H G F E D C B A<--'
971
972 M-n ,-->I H G F E D C B A---,
973 | |
974 `---A B C D E F G H I<--'
975
976
977 Special NOTE for terms:
978 =======================
979 tab/S-tab are bound to history.
980 C-d/u are for following in other buffer.
981 Use C-t to Scroll up.
982
983 When you quit incremental search with RET, see `ioccur-mode'
984 for commands provided in the `ioccur-buffer'."
985 (interactive "P")
986 (let (pop-up-frames)
987 (setq ioccur-exit-and-quit-p nil)
988 (setq ioccur-success nil)
989 (setq ioccur-current-buffer (buffer-name (current-buffer)))
990 (when ioccur-fontify-buffer-p
991 (message "Fontifying buffer...Please wait it could be long.")
992 (jit-lock-fontify-now) (message nil))
993 (setq ioccur-buffer (concat "*ioccur-" ioccur-current-buffer "*"))
994 (setq ioccur-last-window-configuration (current-window-configuration))
995 (if (and (not initial-input)
996 (get-buffer ioccur-buffer)
997 (not (get-buffer-window ioccur-buffer)))
998 ;; An hidden `ioccur-buffer' exists jump to it and reuse it.
999 (switch-to-buffer-other-window ioccur-buffer t)
1000 ;; `ioccur-buffer' doesn't exists or is visible, start searching
1001 ;; Creating a new `ioccur-buffer' or reusing the visible one after
1002 ;; erasing it.
1003 (let* ((init-str (if initial-input
1004 (if (stringp initial-input)
1005 initial-input (thing-at-point 'symbol))
1006 ""))
1007 (len (length init-str))
1008 (curpos (point))
1009 (inhibit-read-only t)
1010 (cur-mode (with-current-buffer ioccur-current-buffer
1011 (prog1
1012 major-mode
1013 ;; If current `major-mode' is wdired
1014 ;; Turn it off.
1015 (when (eq major-mode 'wdired-mode)
1016 (wdired-change-to-dired-mode)))))
1017 str-no-prop)
1018 (set-text-properties 0 len nil init-str)
1019 (setq str-no-prop init-str)
1020 (pop-to-buffer (get-buffer-create ioccur-buffer))
1021 (ioccur-mode)
1022 (unwind-protect
1023 ;; Start incremental search.
1024 (progn
1025 (ioccur-start-timer)
1026 (ioccur-read-search-input str-no-prop curpos))
1027 ;; At this point incremental search loop is exited.
1028 (progn
1029 (ioccur-cancel-search)
1030 (kill-local-variable 'mode-line-format)
1031 (when (equal (buffer-substring (point-at-bol) (point-at-eol)) "")
1032 (setq ioccur-quit-flag t))
1033 (cond (ioccur-quit-flag ; C-g hit or empty `ioccur-buffer'.
1034 (kill-buffer ioccur-buffer)
1035 (pop-to-buffer ioccur-current-buffer)
1036 (when ioccur-match-overlay
1037 (delete-overlay ioccur-match-overlay))
1038 (set-window-configuration ioccur-last-window-configuration)
1039 (goto-char curpos)
1040 (ioccur-send-message)
1041 ;; If `ioccur-message' is non--nil, thats mean we exit
1042 ;; with a specific action other than `C-g',
1043 ;; e.g kill-as-sexp, so we save history.
1044 (when ioccur-message (ioccur-save-history)))
1045 (ioccur-exit-and-quit-p ; Jump and kill `ioccur-buffer'.
1046 (ioccur-jump-and-quit)
1047 (kill-buffer ioccur-buffer)
1048 (ioccur-send-message) (ioccur-save-history))
1049 (t ; Jump keeping `ioccur-buffer'.
1050 (ioccur-jump)
1051 (pop-to-buffer ioccur-buffer)
1052 (setq buffer-read-only t)
1053 (ioccur-save-history)))
1054 ;; Maybe reenable `wdired-mode'.
1055 (when (eq cur-mode 'wdired-mode) (wdired-change-to-wdired-mode))
1056 (setq ioccur-count-occurences 0)
1057 (setq ioccur-quit-flag nil)
1058 (setq ioccur-message nil)
1059 (setq ioccur-search-function ioccur-default-search-function)))))))
1060
1061 (defun ioccur-save-history ()
1062 "Save last ioccur element found in `ioccur-history'."
1063 (unless (string= ioccur-pattern "")
1064 (setq ioccur-history
1065 (cons ioccur-pattern (delete ioccur-pattern ioccur-history)))
1066 (when (> (length ioccur-history) ioccur-max-length-history)
1067 (setq ioccur-history (delete (car (last ioccur-history))
1068 ioccur-history)))
1069 (setq ioccur-success t)))
1070
1071 (defun ioccur-cancel-search ()
1072 "Cancel timer used for ioccur searching."
1073 (when ioccur-search-timer
1074 (cancel-timer ioccur-search-timer)
1075 (setq ioccur-search-timer nil)))
1076
1077 (defun ioccur-color-current-line ()
1078 "Highlight and underline current line in `ioccur-buffer'."
1079 (if ioccur-occur-overlay
1080 (move-overlay ioccur-occur-overlay
1081 (point-at-bol) (1+ (point-at-eol)) ioccur-buffer)
1082 (setq ioccur-occur-overlay
1083 (make-overlay (point-at-bol) (1+ (point-at-eol)) ioccur-buffer)))
1084 (overlay-put ioccur-occur-overlay 'face 'ioccur-overlay-face))
1085
1086 (defun ioccur-color-matched-line ()
1087 "Highlight and underline current position \
1088 of matched line in `ioccur-current-buffer'."
1089 (if ioccur-match-overlay
1090 (move-overlay ioccur-match-overlay
1091 (point-at-bol) (1+ (point-at-eol)))
1092 (setq ioccur-match-overlay
1093 (make-overlay (point-at-bol) (1+ (point-at-eol)))))
1094 (overlay-put ioccur-match-overlay 'face 'ioccur-match-overlay-face))
1095
1096
1097 (provide 'ioccur)
1098
1099 ;;; ioccur.el ends here