with [[help:avy-goto-line][avy-goto-line]].
#+BEGIN_SRC emacs-lisp :tangle yes
- (add-to-list 'load-path "~/.emacs.d/elpa/avy-20160202.2357")
+ (add-to-list 'load-path "~/.emacs.d/elpa/avy-20171230.220")
(require 'avy)
(define-key global-map (kbd "C-c C-@") 'avy-goto-word-1)
(define-key global-map (kbd "C-c C-SPC") 'avy-goto-word-1)
+++ /dev/null
-;;; avy-autoloads.el --- automatically extracted autoloads
-;;
-;;; Code:
-(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))
-\f
-;;;### (autoloads nil "avy" "avy.el" (22215 9152 0 0))
-;;; Generated autoloads from avy.el
-
-(autoload 'avy-goto-char "avy" "\
-Jump to the currently visible CHAR.
-The window scope is determined by `avy-all-windows' (ARG negates it).
-
-\(fn CHAR &optional ARG)" t nil)
-
-(autoload 'avy-goto-char-in-line "avy" "\
-Jump to the currently visible CHAR in the current line.
-
-\(fn CHAR)" t nil)
-
-(autoload 'avy-goto-char-2 "avy" "\
-Jump to the currently visible CHAR1 followed by CHAR2.
-The window scope is determined by `avy-all-windows' (ARG negates it).
-
-\(fn CHAR1 CHAR2 &optional ARG)" t nil)
-
-(autoload 'avy-isearch "avy" "\
-Jump to one of the current isearch candidates.
-
-\(fn)" t nil)
-
-(autoload 'avy-goto-word-0 "avy" "\
-Jump to a word start.
-The window scope is determined by `avy-all-windows' (ARG negates it).
-
-\(fn ARG)" t nil)
-
-(autoload 'avy-goto-word-1 "avy" "\
-Jump to the currently visible CHAR at a word start.
-The window scope is determined by `avy-all-windows' (ARG negates it).
-
-\(fn CHAR &optional ARG)" t nil)
-
-(autoload 'avy-goto-subword-0 "avy" "\
-Jump to a word or subword start.
-
-The window scope is determined by `avy-all-windows' (ARG negates it).
-
-When PREDICATE is non-nil it's a function of zero parameters that
-should return true.
-
-\(fn &optional ARG PREDICATE)" t nil)
-
-(autoload 'avy-goto-subword-1 "avy" "\
-Jump to the currently visible CHAR at a subword start.
-The window scope is determined by `avy-all-windows' (ARG negates it).
-The case of CHAR is ignored.
-
-\(fn CHAR &optional ARG)" t nil)
-
-(autoload 'avy-goto-word-or-subword-1 "avy" "\
-Forward to `avy-goto-subword-1' or `avy-goto-word-1'.
-Which one depends on variable `subword-mode'.
-
-\(fn)" t nil)
-
-(autoload 'avy-goto-line "avy" "\
-Jump to a line start in current buffer.
-
-When ARG is 1, jump to lines currently visible, with the option
-to cancel to `goto-line' by entering a number.
-
-When ARG is 4, negate the window scope determined by
-`avy-all-windows'.
-
-Otherwise, forward to `goto-line' with ARG.
-
-\(fn &optional ARG)" t nil)
-
-(autoload 'avy-goto-line-above "avy" "\
-Goto visible line above the cursor.
-
-\(fn)" t nil)
-
-(autoload 'avy-goto-line-below "avy" "\
-Goto visible line below the cursor.
-
-\(fn)" t nil)
-
-(autoload 'avy-copy-line "avy" "\
-Copy a selected line above the current line.
-ARG lines can be used.
-
-\(fn ARG)" t nil)
-
-(autoload 'avy-move-line "avy" "\
-Move a selected line above the current line.
-ARG lines can be used.
-
-\(fn ARG)" t nil)
-
-(autoload 'avy-copy-region "avy" "\
-Select two lines and copy the text between them to point.
-
-The window scope is determined by `avy-all-windows' or
-`avy-all-windows-alt' when ARG is non-nil.
-
-\(fn ARG)" t nil)
-
-(autoload 'avy-setup-default "avy" "\
-Setup the default shortcuts.
-
-\(fn)" nil nil)
-
-(autoload 'avy-goto-char-timer "avy" "\
-Read one or many consecutive chars and jump to the first one.
-The window scope is determined by `avy-all-windows' (ARG negates it).
-
-\(fn &optional ARG)" t nil)
-
-;;;***
-\f
-;; Local Variables:
-;; version-control: never
-;; no-byte-compile: t
-;; no-update-autoloads: t
-;; End:
-;;; avy-autoloads.el ends here
+++ /dev/null
-(define-package "avy" "20160202.2357" "tree-based completion" '((emacs "24.1") (cl-lib "0.5")) :url "https://github.com/abo-abo/avy" :keywords '("point" "location"))
+++ /dev/null
-;;; avy.el --- tree-based completion -*- lexical-binding: t -*-
-
-;; Copyright (C) 2015 Free Software Foundation, Inc.
-
-;; Author: Oleh Krehel <ohwoeowhoNOSPAMgmail.com>
-;; URL: https://github.com/abo-abo/avy
-;; Package-Version: 20160202.2357
-;; Version: 0.4.0
-;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
-;; Keywords: point, location
-
-;; This file is part of GNU Emacs.
-
-;; This file is free software; you can redistribute it and/or modify
-;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
-
-;; This program is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-;; GNU General Public License for more details.
-
-;; For a full copy of the GNU General Public License
-;; see <http://www.gnu.org/licenses/>.
-
-;;; Commentary:
-;;
-;; This package provides a generic completion method based on building
-;; a balanced decision tree with each candidate being a leaf. To
-;; traverse the tree from the root to a desired leaf, typically a
-;; sequence of `read-key' can be used.
-;;
-;; In order for `read-key' to make sense, the tree needs to be
-;; visualized appropriately, with a character at each branch node. So
-;; this completion method works only for things that you can see on
-;; your screen, all at once:
-;;
-;; * character positions
-;; * word or subword start positions
-;; * line beginning positions
-;; * link positions
-;; * window positions
-;;
-;; If you're familiar with the popular `ace-jump-mode' package, this
-;; package does all that and more, without the implementation
-;; headache.
-
-;;; Code:
-(require 'cl-lib)
-(require 'ring)
-
-;;* Customization
-(defgroup avy nil
- "Jump to things tree-style."
- :group 'convenience
- :prefix "avy-")
-
-(defcustom avy-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
- "Default keys for jumping.
-Any key is either a character representing a self-inserting
-key (letters, digits, punctuation, etc.) or a symbol denoting a
-non-printing key like an arrow key (left, right, up, down). For
-non-printing keys, a corresponding entry in
-`avy-key-to-char-alist' must exist in order to visualize the key
-in the avy overlays."
- :type '(repeat :tag "Keys" (choice (character :tag "char")
- (symbol :tag "non-printing key"))))
-
-(defcustom avy-keys-alist nil
- "Alist of avy-jump commands to `avy-keys' overriding the default `avy-keys'."
- :type '(alist
- :key-type (choice :tag "Command"
- (const avy-goto-char)
- (const avy-goto-char-2)
- (const avy-isearch)
- (const avy-goto-line)
- (const avy-goto-subword-0)
- (const avy-goto-subword-1)
- (const avy-goto-word-0)
- (const avy-goto-word-1)
- (const avy-copy-line)
- (const avy-copy-region)
- (const avy-move-line))
- :value-type (repeat :tag "Keys" character)))
-
-(defcustom avy-style 'at-full
- "The default method of displaying the overlays.
-Use `avy-styles-alist' to customize this per-command."
- :type '(choice
- (const :tag "Pre" pre)
- (const :tag "At" at)
- (const :tag "At Full" at-full)
- (const :tag "Post" post)
- (const :tag "De Bruijn" de-bruijn)))
-
-(defcustom avy-styles-alist nil
- "Alist of avy-jump commands to the style for each command.
-If the commands isn't on the list, `avy-style' is used."
- :type '(alist
- :key-type (choice :tag "Command"
- (const avy-goto-char)
- (const avy-goto-char-2)
- (const avy-isearch)
- (const avy-goto-line)
- (const avy-goto-subword-0)
- (const avy-goto-subword-1)
- (const avy-goto-word-0)
- (const avy-goto-word-1)
- (const avy-copy-line)
- (const avy-copy-region)
- (const avy-move-line))
- :value-type (choice
- (const :tag "Pre" pre)
- (const :tag "At" at)
- (const :tag "At Full" at-full)
- (const :tag "Post" post)
- (const :tag "De Bruijn" de-bruijn))))
-
-(defcustom avy-dispatch-alist
- '((?x . avy-action-kill)
- (?m . avy-action-mark)
- (?n . avy-action-copy))
- "List of actions for `avy-handler-default'.
-
-Each item is (KEY . ACTION). When KEY not on `avy-keys' is
-pressed during the dispatch, ACTION is set to replace the default
-`avy-action-goto' once a candidate is finally selected."
- :type
- '(alist
- :key-type (choice (character :tag "Char"))
- :value-type (choice
- (const :tag "Mark" avy-action-mark)
- (const :tag "Copy" avy-action-copy)
- (const :tag "Kill" avy-action-kill))))
-
-(defcustom avy-background nil
- "When non-nil, a gray background will be added during the selection."
- :type 'boolean)
-
-(defcustom avy-all-windows t
- "Determine the list of windows to consider in search of candidates."
- :type
- '(choice
- (const :tag "All Frames" all-frames)
- (const :tag "This Frame" t)
- (const :tag "This Window" nil)))
-
-(defcustom avy-case-fold-search t
- "Non-nil if searches should ignore case."
- :type 'boolean)
-
-(defcustom avy-word-punc-regexp "[!-/:-@[-`{-~]"
- "Regexp of punctuation chars that count as word starts for `avy-goto-word-1.
-When nil, punctuation chars will not be matched.
-
-\"[!-/:-@[-`{-~]\" will match all printable punctuation chars."
- :type 'regexp)
-
-(defcustom avy-ignored-modes '(image-mode doc-view-mode pdf-view-mode)
- "List of modes to ignore when searching for candidates.
-Typically, these modes don't use the text representation.")
-
-(defvar avy-ring (make-ring 20)
- "Hold the window and point history.")
-
-(defvar avy-translate-char-function #'identity
- "Function to translate user input key into another key.
-For example, to make SPC do the same as ?a, use
-\(lambda (c) (if (= c 32) ?a c)).")
-
-(defface avy-lead-face-0
- '((t (:foreground "white" :background "#4f57f9")))
- "Face used for first non-terminating leading chars.")
-
-(defface avy-lead-face-1
- '((t (:foreground "white" :background "gray")))
- "Face used for matched leading chars.")
-
-(defface avy-lead-face-2
- '((t (:foreground "white" :background "#f86bf3")))
- "Face used for leading chars.")
-
-(defface avy-lead-face
- '((t (:foreground "white" :background "#e52b50")))
- "Face used for the leading chars.")
-
-(defface avy-background-face
- '((t (:foreground "gray40")))
- "Face for whole window background during selection.")
-
-(defface avy-goto-char-timer-face
- '((t (:inherit highlight)))
- "Face for matches during reading chars using `avy-goto-char-timer'.")
-
-(defconst avy-lead-faces '(avy-lead-face
- avy-lead-face-0
- avy-lead-face-2
- avy-lead-face
- avy-lead-face-0
- avy-lead-face-2)
- "Face sequence for `avy--overlay-at-full'.")
-
-(defvar avy-key-to-char-alist '((left . ?◀)
- (right . ?▶)
- (up . ?▲)
- (down . ?▼)
- (prior . ?△)
- (next . ?▽))
- "An alist from non-character keys to printable chars used in avy overlays.
-This alist must contain all keys used in `avy-keys' which are not
-self-inserting keys and thus aren't read as characters.")
-
-;;* Internals
-;;** Tree
-(defmacro avy-multipop (lst n)
- "Remove LST's first N elements and return them."
- `(if (<= (length ,lst) ,n)
- (prog1 ,lst
- (setq ,lst nil))
- (prog1 ,lst
- (setcdr
- (nthcdr (1- ,n) (prog1 ,lst (setq ,lst (nthcdr ,n ,lst))))
- nil))))
-
-(defun avy--de-bruijn (keys n)
- "De Bruijn sequence for alphabet KEYS and subsequences of length N."
- (let* ((k (length keys))
- (a (make-list (* n k) 0))
- sequence)
- (cl-labels ((db (T p)
- (if (> T n)
- (if (eq (% n p) 0)
- (setq sequence
- (append sequence
- (cl-subseq a 1 (1+ p)))))
- (setf (nth T a) (nth (- T p) a))
- (db (1+ T) p)
- (cl-loop for j from (1+ (nth (- T p) a)) to (1- k) do
- (setf (nth T a) j)
- (db (1+ T) T)))))
- (db 1 1)
- (mapcar (lambda (n)
- (nth n keys))
- sequence))))
-
-(defun avy--path-alist-1 (lst seq-len keys)
- "Build a De Bruin sequence from LST.
-SEQ-LEN is how many elements of KEYS it takes to identify a match."
- (let ((db-seq (avy--de-bruijn keys seq-len))
- prev-pos prev-seq prev-win path-alist)
- ;; The De Bruijn seq is cyclic, so append the seq-len - 1 first chars to
- ;; the end.
- (setq db-seq (nconc db-seq (cl-subseq db-seq 0 (1- seq-len))))
- (cl-labels ((subseq-and-pop ()
- (when (nth (1- seq-len) db-seq)
- (prog1 (cl-subseq db-seq 0 seq-len)
- (pop db-seq)))))
- (while lst
- (let* ((cur (car lst))
- (pos (cond
- ;; ace-window has matches of the form (pos . wnd)
- ((integerp (car cur)) (car cur))
- ;; avy-jump have form ((start . end) . wnd)
- ((consp (car cur)) (caar cur))
- (t (error "Unexpected match representation: %s" cur))))
- (win (cdr cur))
- (path (if prev-pos
- (let ((diff (if (eq win prev-win)
- (- pos prev-pos)
- 0)))
- (when (and (> diff 0) (< diff seq-len))
- (while (and (nth (1- seq-len) db-seq)
- (not
- (eq 0 (cl-search
- (cl-subseq prev-seq diff)
- (cl-subseq db-seq 0 seq-len)))))
- (pop db-seq)))
- (subseq-and-pop))
- (subseq-and-pop))))
- (if (not path)
- (setq lst nil
- path-alist nil)
- (push (cons path (car lst)) path-alist)
- (setq prev-pos pos
- prev-seq path
- prev-win win
- lst (cdr lst))))))
- (nreverse path-alist)))
-
-(defun avy-tree (lst keys)
- "Coerce LST into a balanced tree.
-The degree of the tree is the length of KEYS.
-KEYS are placed appropriately on internal nodes."
- (let ((len (length keys)))
- (cl-labels
- ((rd (ls)
- (let ((ln (length ls)))
- (if (< ln len)
- (cl-pairlis keys
- (mapcar (lambda (x) (cons 'leaf x)) ls))
- (let ((ks (copy-sequence keys))
- res)
- (dolist (s (avy-subdiv ln len))
- (push (cons (pop ks)
- (if (eq s 1)
- (cons 'leaf (pop ls))
- (rd (avy-multipop ls s))))
- res))
- (nreverse res))))))
- (rd lst))))
-
-(defun avy-subdiv (n b)
- "Distribute N in B terms in a balanced way."
- (let* ((p (1- (floor (+ (log n b) 1e-6))))
- (x1 (expt b p))
- (x2 (* b x1))
- (delta (- n x2))
- (n2 (/ delta (- x2 x1)))
- (n1 (- b n2 1)))
- (append
- (make-list n1 x1)
- (list
- (- n (* n1 x1) (* n2 x2)))
- (make-list n2 x2))))
-
-(defun avy-traverse (tree walker &optional recur-key)
- "Traverse TREE generated by `avy-tree'.
-WALKER is a function that takes KEYS and LEAF.
-
-RECUR-KEY is used in recursion.
-
-LEAF is a member of LST argument of `avy-tree'.
-
-KEYS is the path from the root of `avy-tree' to LEAF."
- (dolist (br tree)
- (let ((key (cons (car br) recur-key)))
- (if (eq (cadr br) 'leaf)
- (funcall walker key (cddr br))
- (avy-traverse (cdr br) walker key)))))
-
-(defvar avy-action nil
- "Function to call at the end of select.")
-
-(defun avy-handler-default (char)
- "The default handler for a bad CHAR."
- (let (dispatch)
- (if (setq dispatch (assoc char avy-dispatch-alist))
- (progn
- (setq avy-action (cdr dispatch))
- (throw 'done 'restart))
- (signal 'user-error (list "No such candidate" char))
- (throw 'done nil))))
-
-(defvar avy-handler-function 'avy-handler-default
- "A function to call for a bad `read-key' in `avy-read'.")
-
-(defvar avy-current-path ""
- "Store the current incomplete path during `avy-read'.")
-
-(defun avy-read (tree display-fn cleanup-fn)
- "Select a leaf from TREE using consecutive `read-char'.
-
-DISPLAY-FN should take CHAR and LEAF and signify that LEAFs
-associated with CHAR will be selected if CHAR is pressed. This is
-commonly done by adding a CHAR overlay at LEAF position.
-
-CLEANUP-FN should take no arguments and remove the effects of
-multiple DISPLAY-FN invokations."
- (catch 'done
- (setq avy-current-path "")
- (while tree
- (let ((avy--leafs nil))
- (avy-traverse tree
- (lambda (path leaf)
- (push (cons path leaf) avy--leafs)))
- (dolist (x avy--leafs)
- (funcall display-fn (car x) (cdr x))))
- (let ((char (funcall avy-translate-char-function (read-key)))
- branch)
- (funcall cleanup-fn)
- (if (setq branch (assoc char tree))
- (if (eq (car (setq tree (cdr branch))) 'leaf)
- (throw 'done (cdr tree))
- (setq avy-current-path
- (concat avy-current-path (string (avy--key-to-char char)))))
- (funcall avy-handler-function char))))))
-
-(defun avy-read-de-bruijn (lst keys)
- "Select from LST dispatching on KEYS."
- ;; In theory, the De Bruijn sequence B(k,n) has k^n subsequences of length n
- ;; (the path length) usable as paths, thus that's the lower bound. Due to
- ;; partially overlapping matches, not all subsequences may be usable, so it's
- ;; possible that the path-len must be incremented, e.g., if we're matching
- ;; for x and a buffer contains xaxbxcx only every second subsequence is
- ;; usable for the four matches.
- (catch 'done
- (let* ((path-len (ceiling (log (length lst) (length keys))))
- (alist (avy--path-alist-1 lst path-len keys)))
- (while (not alist)
- (cl-incf path-len)
- (setq alist (avy--path-alist-1 lst path-len keys)))
- (let* ((len (length (caar alist)))
- (i 0))
- (setq avy-current-path "")
- (while (< i len)
- (dolist (x (reverse alist))
- (avy--overlay-at-full (reverse (car x)) (cdr x)))
- (let ((char (funcall avy-translate-char-function (read-key))))
- (avy--remove-leading-chars)
- (setq alist
- (delq nil
- (mapcar (lambda (x)
- (when (eq (caar x) char)
- (cons (cdr (car x)) (cdr x))))
- alist)))
- (setq avy-current-path
- (concat avy-current-path (string (avy--key-to-char char))))
- (cl-incf i)
- (unless alist
- (funcall avy-handler-function char))))
- (cdar alist)))))
-
-;;** Rest
-(defun avy-window-list ()
- "Return a list of windows depending on `avy-all-windows'."
- (cond ((eq avy-all-windows 'all-frames)
- (cl-mapcan #'window-list (frame-list)))
-
- ((eq avy-all-windows t)
- (window-list))
-
- ((null avy-all-windows)
- (list (selected-window)))
-
- (t
- (error "Unrecognized option: %S" avy-all-windows))))
-
-(defcustom avy-all-windows-alt t
- "The alternative `avy-all-windows' for use with \\[universal-argument]."
- :type '(choice
- (const :tag "All windows on the current frame" t)
- (const :tag "All windows on all frames" all-frames)))
-
-(defmacro avy-dowindows (flip &rest body)
- "Depending on FLIP and `avy-all-windows' run BODY in each or selected window."
- (declare (indent 1)
- (debug (form body)))
- `(let ((avy-all-windows (if ,flip
- avy-all-windows-alt
- avy-all-windows)))
- (dolist (wnd (avy-window-list))
- (with-selected-window wnd
- (unless (memq major-mode avy-ignored-modes)
- ,@body)))))
-
-(defmacro avy-with (command &rest body)
- "Set `avy-keys' according to COMMAND and execute BODY.
-Set `avy-style' according to COMMMAND as well."
- (declare (indent 1)
- (debug (form body)))
- `(let ((avy-keys (or (cdr (assq ',command avy-keys-alist))
- avy-keys))
- (avy-style (or (cdr (assq ',command avy-styles-alist))
- avy-style)))
- (setq avy-action nil)
- ,@body))
-
-(defun avy-action-goto (pt)
- "Goto PT."
- (goto-char pt))
-
-(defun avy-action-mark (pt)
- "Mark sexp at PT."
- (goto-char pt)
- (set-mark (point))
- (forward-sexp))
-
-(defun avy-action-copy (pt)
- "Copy sexp starting on PT."
- (save-excursion
- (let (str)
- (goto-char pt)
- (forward-sexp)
- (setq str (buffer-substring pt (point)))
- (kill-new str)
- (message "Copied: %s" str)))
- (let ((dat (ring-ref avy-ring 0)))
- (select-frame-set-input-focus
- (window-frame (cdr dat)))
- (select-window (cdr dat))
- (goto-char (car dat))))
-
-(defun avy-action-kill (pt)
- "Kill sexp at PT."
- (goto-char pt)
- (forward-sexp)
- (kill-region pt (point))
- (message "Killed: %s" (current-kill 0)))
-
-(defun avy--process (candidates overlay-fn)
- "Select one of CANDIDATES using `avy-read'.
-Use OVERLAY-FN to visualize the decision overlay."
- (unless (and (consp (car candidates))
- (windowp (cdar candidates)))
- (setq candidates
- (mapcar (lambda (x) (cons x (selected-window)))
- candidates)))
- (let ((len (length candidates))
- (cands (copy-sequence candidates))
- res)
- (if (= len 0)
- (message "zero candidates")
- (if (= len 1)
- (setq res (car candidates))
- (unwind-protect
- (progn
- (avy--make-backgrounds
- (avy-window-list))
- (setq res (if (eq avy-style 'de-bruijn)
- (avy-read-de-bruijn
- candidates avy-keys)
- (avy-read (avy-tree candidates avy-keys)
- overlay-fn
- #'avy--remove-leading-chars))))
- (avy--done)))
- (cond ((eq res 'restart)
- (avy--process cands overlay-fn))
- ;; ignore exit from `avy-handler-function'
- ((eq res 'exit))
- (t
- (avy-push-mark)
- (when (and (consp res)
- (windowp (cdr res)))
- (let* ((window (cdr res))
- (frame (window-frame window)))
- (unless (equal frame (selected-frame))
- (select-frame-set-input-focus frame))
- (select-window window))
- (setq res (car res)))
-
- (funcall (or avy-action 'avy-action-goto)
- (if (consp res)
- (car res)
- res)))))))
-
-(defvar avy--overlays-back nil
- "Hold overlays for when `avy-background' is t.")
-
-(defun avy--make-backgrounds (wnd-list)
- "Create a dim background overlay for each window on WND-LIST."
- (when avy-background
- (setq avy--overlays-back
- (mapcar (lambda (w)
- (let ((ol (make-overlay
- (window-start w)
- (window-end w)
- (window-buffer w))))
- (overlay-put ol 'face 'avy-background-face)
- (overlay-put ol 'window w)
- ol))
- wnd-list))))
-
-(defun avy--done ()
- "Clean up overlays."
- (mapc #'delete-overlay avy--overlays-back)
- (setq avy--overlays-back nil)
- (avy--remove-leading-chars))
-
-(defun avy--next-visible-point ()
- "Return the next closest point without 'invisible property."
- (let ((s (point)))
- (while (and (not (= (point-max) (setq s (next-overlay-change s))))
- (get-char-property s 'invisible)))
- s))
-
-(defun avy--next-invisible-point ()
- "Return the next closest point with 'invisible property."
- (let ((s (point)))
- (while (and (not (= (point-max) (setq s (next-overlay-change s))))
- (not (get-char-property s 'invisible))))
- s))
-
-(defun avy--find-visible-regions (rbeg rend)
- "Return a list of all visible regions between RBEG and REND."
- (setq rbeg (max rbeg (point-min)))
- (setq rend (min rend (point-max)))
- (when (< rbeg rend)
- (let (visibles beg)
- (save-excursion
- (save-restriction
- (narrow-to-region rbeg rend)
- (setq beg (goto-char (point-min)))
- (while (not (= (point) (point-max)))
- (goto-char (avy--next-invisible-point))
- (push (cons beg (point)) visibles)
- (setq beg (goto-char (avy--next-visible-point))))
- (nreverse visibles))))))
-
-(defun avy--regex-candidates (regex &optional beg end pred group)
- "Return all elements that match REGEX.
-Each element of the list is ((BEG . END) . WND)
-When PRED is non-nil, it's a filter for matching point positions.
-When GROUP is non-nil, (BEG . END) should delimit that regex group."
- (setq group (or group 0))
- (let ((case-fold-search (or avy-case-fold-search
- (string= regex (downcase regex))))
- candidates)
- (avy-dowindows current-prefix-arg
- (dolist (pair (avy--find-visible-regions
- (or beg (window-start))
- (or end (window-end (selected-window) t))))
- (save-excursion
- (goto-char (car pair))
- (while (re-search-forward regex (cdr pair) t)
- (unless (get-char-property (1- (point)) 'invisible)
- (when (or (null pred)
- (funcall pred))
- (push (cons (cons (match-beginning group)
- (match-end group))
- wnd) candidates)))))))
- (nreverse candidates)))
-
-(defvar avy--overlay-offset 0
- "The offset to apply in `avy--overlay'.")
-
-(defvar avy--overlays-lead nil
- "Hold overlays for leading chars.")
-
-(defun avy--remove-leading-chars ()
- "Remove leading char overlays."
- (mapc #'delete-overlay avy--overlays-lead)
- (setq avy--overlays-lead nil))
-
-(defun avy--old-str (pt wnd)
- "Return a one-char string at PT in WND."
- (let ((old-str (with-selected-window wnd
- (buffer-substring pt (1+ pt)))))
- (if avy-background
- (propertize old-str 'face 'avy-background-face)
- old-str)))
-
-(defun avy--overlay (str beg end wnd &optional compose-fn)
- "Create an overlay with STR from BEG to END in WND.
-COMPOSE-FN is a lambda that concatenates the old string at BEG with STR."
- (let ((eob (with-selected-window wnd (point-max))))
- (when (<= beg eob)
- (let* ((beg (+ beg avy--overlay-offset))
- (ol (make-overlay beg (or end (1+ beg)) (window-buffer wnd)))
- (old-str (if (eq beg eob) "" (avy--old-str beg wnd)))
- (os-line-prefix (get-text-property 0 'line-prefix old-str))
- (os-wrap-prefix (get-text-property 0 'wrap-prefix old-str))
- other-ol)
- (when os-line-prefix
- (add-text-properties 0 1 `(line-prefix ,os-line-prefix) str))
- (when os-wrap-prefix
- (add-text-properties 0 1 `(wrap-prefix ,os-wrap-prefix) str))
- (when (setq other-ol (cl-find-if
- (lambda (o) (overlay-get o 'goto-address))
- (overlays-at beg)))
- (add-text-properties
- 0 (length old-str)
- `(face ,(overlay-get other-ol 'face)) old-str))
- (overlay-put ol 'window wnd)
- (overlay-put ol 'category 'avy)
- (overlay-put ol (if (eq beg eob)
- 'after-string
- 'display)
- (funcall
- (or compose-fn #'concat)
- str old-str))
- (push ol avy--overlays-lead)))))
-
-(defcustom avy-highlight-first nil
- "When non-nil highlight the first decision char with `avy-lead-face-0'.
-Do this even when the char is terminating."
- :type 'boolean)
-
-(defun avy--key-to-char (c)
- "If C is no character, translate it using `avy-key-to-char-alist'."
- (if (characterp c)
- c
- (or (cdr (assoc c avy-key-to-char-alist))
- (error "Unknown key %s" c))))
-
-(defun avy-candidate-beg (leaf)
- "Return the start position for LEAF."
- (cond ((numberp leaf)
- leaf)
- ((consp (car leaf))
- (caar leaf))
- (t
- (car leaf))))
-
-(defun avy-candidate-end (leaf)
- "Return the end position for LEAF."
- (cond ((numberp leaf)
- leaf)
- ((consp (car leaf))
- (cdar leaf))
- (t
- (car leaf))))
-
-(defun avy-candidate-wnd (leaf)
- "Return the window for LEAF."
- (if (consp leaf)
- (cdr leaf)
- (selected-window)))
-
-(defun avy--overlay-pre (path leaf)
- "Create an overlay with PATH at LEAF.
-PATH is a list of keys from tree root to LEAF.
-LEAF is normally ((BEG . END) . WND)."
- (let* ((path (mapcar #'avy--key-to-char path))
- (str (propertize (apply #'string (reverse path))
- 'face 'avy-lead-face)))
- (when (or avy-highlight-first (> (length str) 1))
- (set-text-properties 0 1 '(face avy-lead-face-0) str))
- (setq str (concat
- (propertize avy-current-path
- 'face 'avy-lead-face-1)
- str))
- (avy--overlay
- str
- (avy-candidate-beg leaf) nil
- (avy-candidate-wnd leaf))))
-
-(defun avy--overlay-at (path leaf)
- "Create an overlay with PATH at LEAF.
-PATH is a list of keys from tree root to LEAF.
-LEAF is normally ((BEG . END) . WND)."
- (let* ((path (mapcar #'avy--key-to-char path))
- (str (propertize
- (string (car (last path)))
- 'face 'avy-lead-face)))
- (avy--overlay
- str
- (avy-candidate-beg leaf) nil
- (avy-candidate-wnd leaf)
- (lambda (str old-str)
- (cond ((string= old-str "\n")
- (concat str "\n"))
- ;; add padding for wide-width character
- ((eq (string-width old-str) 2)
- (concat str " "))
- (t
- str))))))
-
-(defun avy--overlay-at-full (path leaf)
- "Create an overlay with PATH at LEAF.
-PATH is a list of keys from tree root to LEAF.
-LEAF is normally ((BEG . END) . WND)."
- (let* ((path (mapcar #'avy--key-to-char path))
- (str (propertize
- (apply #'string (reverse path))
- 'face 'avy-lead-face))
- (len (length path))
- (beg (avy-candidate-beg leaf))
- (wnd (cdr leaf))
- end)
- (dotimes (i len)
- (set-text-properties (- len i 1) (- len i)
- `(face ,(nth i avy-lead-faces))
- str))
- (when (eq avy-style 'de-bruijn)
- (setq str (concat
- (propertize avy-current-path
- 'face 'avy-lead-face-1)
- str))
- (setq len (length str)))
- (with-selected-window wnd
- (save-excursion
- (goto-char beg)
- (let* ((lep (if (bound-and-true-p visual-line-mode)
- (save-excursion
- (end-of-visual-line)
- (point))
- (line-end-position)))
- (len-and-str (avy--update-offset-and-str len str lep)))
- (setq len (car len-and-str))
- (setq str (cdr len-and-str))
- (setq end (if (= beg lep)
- (1+ beg)
- (min (+ beg
- (if (eq (char-after) ?\t)
- 1
- len))
- lep)))
- (when (and (bound-and-true-p visual-line-mode)
- (> len (- end beg))
- (not (eq lep beg)))
- (setq len (- end beg))
- (let ((old-str (apply #'string (reverse path))))
- (setq str
- (substring
- (propertize
- old-str
- 'face
- (if (= (length old-str) 1)
- 'avy-lead-face
- 'avy-lead-face-0))
- 0 len)))))))
- (avy--overlay
- str beg end wnd
- (lambda (str old-str)
- (cond ((string= old-str "\n")
- (concat str "\n"))
- ((string= old-str "\t")
- (concat str (make-string (max (- tab-width len) 0) ?\ )))
- (t
- ;; add padding for wide-width character
- (if (eq (string-width old-str) 2)
- (concat str " ")
- str)))))))
-
-(defun avy--overlay-post (path leaf)
- "Create an overlay with PATH at LEAF.
-PATH is a list of keys from tree root to LEAF.
-LEAF is normally ((BEG . END) . WND)."
- (let* ((path (mapcar #'avy--key-to-char path))
- (str (propertize (apply #'string (reverse path))
- 'face 'avy-lead-face)))
- (when (or avy-highlight-first (> (length str) 1))
- (set-text-properties 0 1 '(face avy-lead-face-0) str))
- (setq str (concat
- (propertize avy-current-path
- 'face 'avy-lead-face-1)
- str))
- (avy--overlay
- str
- (avy-candidate-end leaf) nil
- (avy-candidate-wnd leaf))))
-
-(defun avy--update-offset-and-str (offset str lep)
- "Recalculate the length of the new overlay at point.
-
-OFFSET is the previous overlay length.
-STR is the overlay string that we wish to add.
-LEP is the line end position.
-
-We want to add an overlay between point and END=point+OFFSET.
-When other overlays already exist between point and END, set
-OFFSET to be the difference between the start of the first
-overlay and point. This is equivalent to truncating our new
-overlay, so that it doesn't intersect with overlays that already
-exist."
- (let* ((wnd (selected-window))
- (beg (point))
- (oov (delq nil
- (mapcar
- (lambda (o)
- (and (eq (overlay-get o 'category) 'avy)
- (eq (overlay-get o 'window) wnd)
- (overlay-start o)))
- (overlays-in beg (min (+ beg offset) lep))))))
- (when oov
- (setq offset (- (apply #'min oov) beg))
- (setq str (substring str 0 offset)))
- (let ((other-ov (cl-find-if
- (lambda (o)
- (and (eq (overlay-get o 'category) 'avy)
- (eq (overlay-start o) beg)
- (not (eq (overlay-get o 'window) wnd))))
- (overlays-in (point) (min (+ (point) offset) lep)))))
- (when (and other-ov
- (> (overlay-end other-ov)
- (+ beg offset)))
- (setq str (concat str (buffer-substring
- (+ beg offset)
- (overlay-end other-ov))))
- (setq offset (- (overlay-end other-ov)
- beg))))
- (cons offset str)))
-
-(defun avy--style-fn (style)
- "Transform STYLE symbol to a style function."
- (cl-case style
- (pre #'avy--overlay-pre)
- (at #'avy--overlay-at)
- (at-full 'avy--overlay-at-full)
- (post #'avy--overlay-post)
- (de-bruijn #'avy--overlay-at-full)
- (t (error "Unexpected style %S" style))))
-
-(defun avy--generic-jump (regex window-flip style &optional beg end)
- "Jump to REGEX.
-When WINDOW-FLIP is non-nil, do the opposite of `avy-all-windows'.
-STYLE determines the leading char overlay style.
-BEG and END delimit the area where candidates are searched."
- (let ((avy-all-windows
- (if window-flip
- (not avy-all-windows)
- avy-all-windows)))
- (avy--process
- (avy--regex-candidates regex beg end)
- (avy--style-fn style))))
-
-;;* Commands
-;;;###autoload
-(defun avy-goto-char (char &optional arg)
- "Jump to the currently visible CHAR.
-The window scope is determined by `avy-all-windows' (ARG negates it)."
- (interactive (list (read-char "char: " t)
- current-prefix-arg))
- (avy-with avy-goto-char
- (avy--generic-jump
- (if (= 13 char)
- "\n"
- (regexp-quote (string char)))
- arg
- avy-style)))
-
-;;;###autoload
-(defun avy-goto-char-in-line (char)
- "Jump to the currently visible CHAR in the current line."
- (interactive (list (read-char "char: " t)))
- (avy-with avy-goto-char
- (avy--generic-jump
- (regexp-quote (string char))
- avy-all-windows
- avy-style
- (line-beginning-position)
- (line-end-position))))
-
-;;;###autoload
-(defun avy-goto-char-2 (char1 char2 &optional arg)
- "Jump to the currently visible CHAR1 followed by CHAR2.
-The window scope is determined by `avy-all-windows' (ARG negates it)."
- (interactive (list (read-char "char 1: " t)
- (read-char "char 2: " t)
- current-prefix-arg))
- (avy-with avy-goto-char-2
- (avy--generic-jump
- (regexp-quote (string char1 char2))
- arg
- avy-style)))
-
-;;;###autoload
-(defun avy-isearch ()
- "Jump to one of the current isearch candidates."
- (interactive)
- (avy-with avy-isearch
- (let ((avy-background nil))
- (avy--process
- (avy--regex-candidates isearch-string)
- (avy--style-fn avy-style))
- (isearch-done))))
-
-;;;###autoload
-(defun avy-goto-word-0 (arg)
- "Jump to a word start.
-The window scope is determined by `avy-all-windows' (ARG negates it)."
- (interactive "P")
- (avy-with avy-goto-word-0
- (avy--generic-jump "\\b\\sw" arg avy-style)))
-
-;;;###autoload
-(defun avy-goto-word-1 (char &optional arg)
- "Jump to the currently visible CHAR at a word start.
-The window scope is determined by `avy-all-windows' (ARG negates it)."
- (interactive (list (read-char "char: " t)
- current-prefix-arg))
- (avy-with avy-goto-word-1
- (let* ((str (string char))
- (regex (cond ((string= str ".")
- "\\.")
- ((and avy-word-punc-regexp
- (string-match avy-word-punc-regexp str))
- (regexp-quote str))
- (t
- (concat
- "\\b"
- str)))))
- (avy--generic-jump regex arg avy-style))))
-
-(declare-function subword-backward "subword")
-(defvar subword-backward-regexp)
-
-(defcustom avy-subword-extra-word-chars '(?{ ?= ?} ?* ?: ?> ?<)
- "A list of characters that should temporarily match \"\\w\".
-This variable is used by `avy-goto-subword-0' and `avy-goto-subword-1'."
- :type '(repeat character))
-
-;;;###autoload
-(defun avy-goto-subword-0 (&optional arg predicate)
- "Jump to a word or subword start.
-
-The window scope is determined by `avy-all-windows' (ARG negates it).
-
-When PREDICATE is non-nil it's a function of zero parameters that
-should return true."
- (interactive "P")
- (require 'subword)
- (avy-with avy-goto-subword-0
- (let ((case-fold-search nil)
- (subword-backward-regexp
- "\\(\\(\\W\\|[[:lower:][:digit:]]\\)\\([!-/:@`~[:upper:]]+\\W*\\)\\|\\W\\w+\\)")
- candidates)
- (avy-dowindows arg
- (let ((syn-tbl (copy-syntax-table)))
- (dolist (char avy-subword-extra-word-chars)
- (modify-syntax-entry char "w" syn-tbl))
- (with-syntax-table syn-tbl
- (let ((ws (window-start))
- window-cands)
- (save-excursion
- (goto-char (window-end (selected-window) t))
- (subword-backward)
- (while (> (point) ws)
- (when (or (null predicate)
- (and predicate (funcall predicate)))
- (unless (get-char-property (point) 'invisible)
- (push (cons (point) (selected-window)) window-cands)))
- (subword-backward)))
- (setq candidates (nconc candidates window-cands))))))
- (avy--process candidates (avy--style-fn avy-style)))))
-
-;;;###autoload
-(defun avy-goto-subword-1 (char &optional arg)
- "Jump to the currently visible CHAR at a subword start.
-The window scope is determined by `avy-all-windows' (ARG negates it).
-The case of CHAR is ignored."
- (interactive (list (read-char "char: " t)
- current-prefix-arg))
- (avy-with avy-goto-subword-1
- (let ((char (downcase char)))
- (avy-goto-subword-0
- arg (lambda () (eq (downcase (char-after)) char))))))
-
-;;;###autoload
-(defun avy-goto-word-or-subword-1 ()
- "Forward to `avy-goto-subword-1' or `avy-goto-word-1'.
-Which one depends on variable `subword-mode'."
- (interactive)
- (if (bound-and-true-p subword-mode)
- (call-interactively #'avy-goto-subword-1)
- (call-interactively #'avy-goto-word-1)))
-
-(defvar visual-line-mode)
-
-(defun avy--line (&optional arg beg end)
- "Select a line.
-The window scope is determined by `avy-all-windows' (ARG negates it).
-Narrow the scope to BEG END."
- (let (candidates)
- (avy-dowindows arg
- (let ((ws (or beg (window-start))))
- (save-excursion
- (save-restriction
- (narrow-to-region ws (or end (window-end (selected-window) t)))
- (goto-char (point-min))
- (while (< (point) (point-max))
- (unless (get-char-property
- (max (1- (point)) ws) 'invisible)
- (push (cons
- (if (eq avy-style 'post)
- (line-end-position)
- (point))
- (selected-window)) candidates))
- (if visual-line-mode
- (progn
- (setq temporary-goal-column 0)
- (line-move-visual 1 t))
- (forward-line 1)))))))
- (let ((avy-action #'identity))
- (avy--process (nreverse candidates) (avy--style-fn avy-style)))))
-
-;;;###autoload
-(defun avy-goto-line (&optional arg)
- "Jump to a line start in current buffer.
-
-When ARG is 1, jump to lines currently visible, with the option
-to cancel to `goto-line' by entering a number.
-
-When ARG is 4, negate the window scope determined by
-`avy-all-windows'.
-
-Otherwise, forward to `goto-line' with ARG."
- (interactive "p")
- (setq arg (or arg 1))
- (if (not (memq arg '(1 4)))
- (progn
- (goto-char (point-min))
- (forward-line (1- arg)))
- (avy-with avy-goto-line
- (let* ((avy-handler-function
- (lambda (char)
- (if (or (< char ?0)
- (> char ?9))
- (avy-handler-default char)
- (let ((line (read-from-minibuffer
- "Goto line: " (string char))))
- (when line
- (avy-push-mark)
- (save-restriction
- (widen)
- (goto-char (point-min))
- (forward-line (1- (string-to-number line))))
- (throw 'done 'exit))))))
- (r (avy--line (eq arg 4))))
- (unless (eq r t)
- (avy-action-goto r))))))
-
-;;;###autoload
-(defun avy-goto-line-above ()
- "Goto visible line above the cursor."
- (interactive)
- (let* ((avy-all-windows nil)
- (r (avy--line nil (window-start)
- (line-beginning-position))))
- (unless (eq r t)
- (avy-action-goto r))))
-
-;;;###autoload
-(defun avy-goto-line-below ()
- "Goto visible line below the cursor."
- (interactive)
- (let* ((avy-all-windows nil)
- (r (avy--line
- nil (line-beginning-position 2)
- (window-end (selected-window) t))))
- (unless (eq r t)
- (avy-action-goto r))))
-
-(defcustom avy-line-insert-style 'above
- "How to insert the newly copied/cut line."
- :type '(choice
- (const :tag "Above" above)
- (const :tag "Below" below)))
-
-;;;###autoload
-(defun avy-copy-line (arg)
- "Copy a selected line above the current line.
-ARG lines can be used."
- (interactive "p")
- (let ((initial-window (selected-window)))
- (avy-with avy-copy-line
- (let* ((start (avy--line))
- (str (buffer-substring-no-properties
- start
- (save-excursion
- (goto-char start)
- (move-end-of-line arg)
- (point)))))
- (select-window initial-window)
- (cond ((eq avy-line-insert-style 'above)
- (beginning-of-line)
- (save-excursion
- (insert str "\n")))
- ((eq avy-line-insert-style 'below)
- (end-of-line)
- (insert "\n" str)
- (beginning-of-line))
- (t
- (user-error "Unexpected `avy-line-insert-style'")))))))
-
-;;;###autoload
-(defun avy-move-line (arg)
- "Move a selected line above the current line.
-ARG lines can be used."
- (interactive "p")
- (let ((initial-window (selected-window)))
- (avy-with avy-move-line
- (let ((start (avy--line)))
- (save-excursion
- (goto-char start)
- (kill-whole-line arg))
- (select-window initial-window)
- (cond ((eq avy-line-insert-style 'above)
- (beginning-of-line)
- (save-excursion
- (insert
- (current-kill 0))))
- ((eq avy-line-insert-style 'below)
- (end-of-line)
- (newline)
- (save-excursion
- (insert (substring (current-kill 0) 0 -1))))
- (t
- (user-error "Unexpected `avy-line-insert-style'")))))))
-
-;;;###autoload
-(defun avy-copy-region (arg)
- "Select two lines and copy the text between them to point.
-
-The window scope is determined by `avy-all-windows' or
-`avy-all-windows-alt' when ARG is non-nil."
- (interactive "P")
- (let ((initial-window (selected-window)))
- (avy-with avy-copy-region
- (let* ((beg (save-selected-window
- (avy--line arg)))
- (end (avy--line arg))
- (str (buffer-substring-no-properties
- beg
- (save-excursion
- (goto-char end)
- (line-end-position)))))
- (select-window initial-window)
- (cond ((eq avy-line-insert-style 'above)
- (beginning-of-line)
- (save-excursion
- (insert str "\n")))
- ((eq avy-line-insert-style 'below)
- (end-of-line)
- (newline)
- (save-excursion
- (insert str)))
- (t
- (user-error "Unexpected `avy-line-insert-style'")))))))
-
-;;;###autoload
-(defun avy-setup-default ()
- "Setup the default shortcuts."
- (eval-after-load "isearch"
- '(define-key isearch-mode-map (kbd "C-'") 'avy-isearch)))
-
-(defcustom avy-timeout-seconds 0.5
- "How many seconds to wait for the second char.")
-
-(defun avy--read-candidates ()
- "Read as many chars as possible and return their occurences.
-At least one char must be read, and then repeatedly one next char
-may be read if it is entered before `avy-timeout-seconds'. `DEL'
-deletes the last char entered, and `RET' exits with the currently
-read string immediately instead of waiting for another char for
-`avy-timeout-seconds'.
-The format of the result is the same as that of `avy--regex-candidates'.
-This function obeys `avy-all-windows' setting."
- (let ((str "") char break overlays regex)
- (unwind-protect
- (progn
- (while (and (not break)
- (setq char
- (read-char (format "char%s: "
- (if (string= str "")
- str
- (format " (%s)" str)))
- t
- (and (not (string= str ""))
- avy-timeout-seconds))))
- ;; Unhighlight
- (dolist (ov overlays)
- (delete-overlay ov))
- (setq overlays nil)
- (cond
- ;; Handle RET
- ((= char 13)
- (setq break t))
- ;; Handle DEL
- ((= char 127)
- (let ((l (length str)))
- (when (>= l 1)
- (setq str (substring str 0 (1- l))))))
- (t
- (setq str (concat str (list char)))))
- ;; Highlight
- (when (>= (length str) 1)
- (let ((case-fold-search
- (or avy-case-fold-search (string= str (downcase str))))
- found)
- (avy-dowindows current-prefix-arg
- (dolist (pair (avy--find-visible-regions
- (window-start)
- (window-end (selected-window) t)))
- (save-excursion
- (goto-char (car pair))
- (setq regex (regexp-quote str))
- (while (re-search-forward regex (cdr pair) t)
- (unless (get-char-property (1- (point)) 'invisible)
- (let ((ov (make-overlay
- (match-beginning 0)
- (match-end 0))))
- (setq found t)
- (push ov overlays)
- (overlay-put ov 'window (selected-window))
- (overlay-put ov 'face 'avy-goto-char-timer-face)))))))
- ;; No matches at all, so there's surely a typo in the input.
- (unless found (beep)))))
- (nreverse (mapcar (lambda (ov)
- (cons (cons (overlay-start ov)
- (overlay-end ov))
- (overlay-get ov 'window)))
- overlays)))
- (dolist (ov overlays)
- (delete-overlay ov)))))
-
-;;;###autoload
-(defun avy-goto-char-timer (&optional arg)
- "Read one or many consecutive chars and jump to the first one.
-The window scope is determined by `avy-all-windows' (ARG negates it)."
- (interactive "P")
- (let ((avy-all-windows (if arg
- (not avy-all-windows)
- avy-all-windows)))
- (avy-with avy-goto-char-timer
- (avy--process
- (avy--read-candidates)
- (avy--style-fn avy-style)))))
-
-(defun avy-push-mark ()
- "Store the current point and window."
- (ring-insert avy-ring
- (cons (point) (selected-window)))
- (unless (region-active-p)
- (push-mark)))
-
-(defun avy-pop-mark ()
- "Jump back to the last location of `avy-push-mark'."
- (interactive)
- (let (res)
- (condition-case nil
- (progn
- (while (not (window-live-p
- (cdr (setq res (ring-remove avy-ring 0))))))
- (let* ((window (cdr res))
- (frame (window-frame window)))
- (when (and (frame-live-p frame)
- (not (eq frame (selected-frame))))
- (select-frame-set-input-focus frame))
- (select-window window)
- (goto-char (car res))))
- (error
- (set-mark-command 4)))))
-
-(define-obsolete-function-alias
- 'avy--goto 'identity "0.3.0"
- "Don't use this function any more.
-`avy--process' will do the jump all by itself.")
-
-(define-obsolete-function-alias 'avy--with-avy-keys 'avy-with "0.3.0")
-
-(provide 'avy)
-
-;;; avy.el ends here
--- /dev/null
+;;; avy-autoloads.el --- automatically extracted autoloads
+;;
+;;; Code:
+(add-to-list 'load-path (directory-file-name (or (file-name-directory #$) (car load-path))))
+\f
+;;;### (autoloads nil "avy" "avy.el" (23115 25984 207150 608000))
+;;; Generated autoloads from avy.el
+
+(autoload 'avy-goto-char "avy" "\
+Jump to the currently visible CHAR.
+The window scope is determined by `avy-all-windows' (ARG negates it).
+
+\(fn CHAR &optional ARG)" t nil)
+
+(autoload 'avy-goto-char-in-line "avy" "\
+Jump to the currently visible CHAR in the current line.
+
+\(fn CHAR)" t nil)
+
+(autoload 'avy-goto-char-2 "avy" "\
+Jump to the currently visible CHAR1 followed by CHAR2.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+BEG and END narrow the scope where candidates are searched.
+
+\(fn CHAR1 CHAR2 &optional ARG BEG END)" t nil)
+
+(autoload 'avy-goto-char-2-above "avy" "\
+Jump to the currently visible CHAR1 followed by CHAR2.
+This is a scoped version of `avy-goto-char-2', where the scope is
+the visible part of the current buffer up to point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+
+\(fn CHAR1 CHAR2 &optional ARG)" t nil)
+
+(autoload 'avy-goto-char-2-below "avy" "\
+Jump to the currently visible CHAR1 followed by CHAR2.
+This is a scoped version of `avy-goto-char-2', where the scope is
+the visible part of the current buffer following point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+
+\(fn CHAR1 CHAR2 &optional ARG)" t nil)
+
+(autoload 'avy-isearch "avy" "\
+Jump to one of the current isearch candidates.
+
+\(fn)" t nil)
+
+(autoload 'avy-goto-word-0 "avy" "\
+Jump to a word start.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+BEG and END narrow the scope where candidates are searched.
+
+\(fn ARG &optional BEG END)" t nil)
+
+(autoload 'avy-goto-word-1 "avy" "\
+Jump to the currently visible CHAR at a word start.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+BEG and END narrow the scope where candidates are searched.
+When SYMBOL is non-nil, jump to symbol start instead of word start.
+
+\(fn CHAR &optional ARG BEG END SYMBOL)" t nil)
+
+(autoload 'avy-goto-word-1-above "avy" "\
+Jump to the currently visible CHAR at a word start.
+This is a scoped version of `avy-goto-word-1', where the scope is
+the visible part of the current buffer up to point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+
+\(fn CHAR &optional ARG)" t nil)
+
+(autoload 'avy-goto-word-1-below "avy" "\
+Jump to the currently visible CHAR at a word start.
+This is a scoped version of `avy-goto-word-1', where the scope is
+the visible part of the current buffer following point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+
+\(fn CHAR &optional ARG)" t nil)
+
+(autoload 'avy-goto-symbol-1 "avy" "\
+Jump to the currently visible CHAR at a symbol start.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+
+\(fn CHAR &optional ARG)" t nil)
+
+(autoload 'avy-goto-symbol-1-above "avy" "\
+Jump to the currently visible CHAR at a symbol start.
+This is a scoped version of `avy-goto-symbol-1', where the scope is
+the visible part of the current buffer up to point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+
+\(fn CHAR &optional ARG)" t nil)
+
+(autoload 'avy-goto-symbol-1-below "avy" "\
+Jump to the currently visible CHAR at a symbol start.
+This is a scoped version of `avy-goto-symbol-1', where the scope is
+the visible part of the current buffer following point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+
+\(fn CHAR &optional ARG)" t nil)
+
+(autoload 'avy-goto-subword-0 "avy" "\
+Jump to a word or subword start.
+
+The window scope is determined by `avy-all-windows' (ARG negates it).
+
+When PREDICATE is non-nil it's a function of zero parameters that
+should return true.
+
+\(fn &optional ARG PREDICATE)" t nil)
+
+(autoload 'avy-goto-subword-1 "avy" "\
+Jump to the currently visible CHAR at a subword start.
+The window scope is determined by `avy-all-windows' (ARG negates it).
+The case of CHAR is ignored.
+
+\(fn CHAR &optional ARG)" t nil)
+
+(autoload 'avy-goto-word-or-subword-1 "avy" "\
+Forward to `avy-goto-subword-1' or `avy-goto-word-1'.
+Which one depends on variable `subword-mode'.
+
+\(fn)" t nil)
+
+(autoload 'avy-goto-line "avy" "\
+Jump to a line start in current buffer.
+
+When ARG is 1, jump to lines currently visible, with the option
+to cancel to `goto-line' by entering a number.
+
+When ARG is 4, negate the window scope determined by
+`avy-all-windows'.
+
+Otherwise, forward to `goto-line' with ARG.
+
+\(fn &optional ARG)" t nil)
+
+(autoload 'avy-goto-line-above "avy" "\
+Goto visible line above the cursor.
+
+\(fn)" t nil)
+
+(autoload 'avy-goto-line-below "avy" "\
+Goto visible line below the cursor.
+
+\(fn)" t nil)
+
+(autoload 'avy-copy-line "avy" "\
+Copy a selected line above the current line.
+ARG lines can be used.
+
+\(fn ARG)" t nil)
+
+(autoload 'avy-move-line "avy" "\
+Move a selected line above the current line.
+ARG lines can be used.
+
+\(fn ARG)" t nil)
+
+(autoload 'avy-copy-region "avy" "\
+Select two lines and copy the text between them to point.
+
+The window scope is determined by `avy-all-windows' or
+`avy-all-windows-alt' when ARG is non-nil.
+
+\(fn ARG)" t nil)
+
+(autoload 'avy-move-region "avy" "\
+Select two lines and move the text between them above the current line.
+
+\(fn)" t nil)
+
+(autoload 'avy-kill-region "avy" "\
+Select two lines and kill the region between them.
+
+The window scope is determined by `avy-all-windows' or
+`avy-all-windows-alt' when ARG is non-nil.
+
+\(fn ARG)" t nil)
+
+(autoload 'avy-kill-ring-save-region "avy" "\
+Select two lines and save the region between them to the kill ring.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+
+\(fn ARG)" t nil)
+
+(autoload 'avy-kill-whole-line "avy" "\
+Select line and kill the whole selected line.
+
+With a numerical prefix ARG, kill ARG line(s) starting from the
+selected line. If ARG is negative, kill backward.
+
+If ARG is zero, kill the selected line but exclude the trailing
+newline.
+
+\\[universal-argument] 3 \\[avy-kil-whole-line] kill three lines
+starting from the selected line. \\[universal-argument] -3
+
+\\[avy-kill-whole-line] kill three lines backward including the
+selected line.
+
+\(fn ARG)" t nil)
+
+(autoload 'avy-kill-ring-save-whole-line "avy" "\
+Select line and save the whole selected line as if killed, but don’t kill it.
+
+This command is similar to `avy-kill-whole-line', except that it
+saves the line(s) as if killed, but does not kill it(them).
+
+With a numerical prefix ARG, kill ARG line(s) starting from the
+selected line. If ARG is negative, kill backward.
+
+If ARG is zero, kill the selected line but exclude the trailing
+newline.
+
+\(fn ARG)" t nil)
+
+(autoload 'avy-setup-default "avy" "\
+Setup the default shortcuts.
+
+\(fn)" nil nil)
+
+(autoload 'avy-goto-char-timer "avy" "\
+Read one or many consecutive chars and jump to the first one.
+The window scope is determined by `avy-all-windows' (ARG negates it).
+
+\(fn &optional ARG)" t nil)
+
+;;;***
+\f
+;; Local Variables:
+;; version-control: never
+;; no-byte-compile: t
+;; no-update-autoloads: t
+;; End:
+;;; avy-autoloads.el ends here
--- /dev/null
+;;; -*- no-byte-compile: t -*-
+(define-package "avy" "20171230.220" "Jump to arbitrary positions in visible text and select text quickly." '((emacs "24.1") (cl-lib "0.5")) :commit "34d3531a3cd35ae6b9e8dbc37343c027504fdcb0" :url "https://github.com/abo-abo/avy" :keywords '("point" "location"))
--- /dev/null
+;;; avy.el --- Jump to arbitrary positions in visible text and select text quickly. -*- lexical-binding: t -*-
+
+;; Copyright (C) 2015 Free Software Foundation, Inc.
+
+;; Author: Oleh Krehel <ohwoeowhoNOSPAMgmail.com>
+;; URL: https://github.com/abo-abo/avy
+;; Package-Version: 20171230.220
+;; Version: 0.4.0
+;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
+;; Keywords: point, location
+
+;; This file is part of GNU Emacs.
+
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; For a full copy of the GNU General Public License
+;; see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+;; With Avy, you can move point to any position in Emacs – even in a
+;; different window – using very few keystrokes. For this, you look at
+;; the position where you want point to be, invoke Avy, and then enter
+;; the sequence of characters displayed at that position.
+;;
+;; If the position you want to jump to can be determined after only
+;; issuing a single keystroke, point is moved to the desired position
+;; immediately after that keystroke. In case this isn't possible, the
+;; sequence of keystrokes you need to enter is comprised of more than
+;; one character. Avy uses a decision tree where each candidate position
+;; is a leaf and each edge is described by a character which is distinct
+;; per level of the tree. By entering those characters, you navigate the
+;; tree, quickly arriving at the desired candidate position, such that
+;; Avy can move point to it.
+;;
+;; Note that this only makes sense for positions you are able to see
+;; when invoking Avy. These kinds of positions are supported:
+;;
+;; * character positions
+;; * word or subword start positions
+;; * line beginning positions
+;; * link positions
+;; * window positions
+;;
+;; If you're familiar with the popular `ace-jump-mode' package, this
+;; package does all that and more, without the implementation
+;; headache.
+
+;;; Code:
+(require 'cl-lib)
+(require 'ring)
+
+;;* Customization
+(defgroup avy nil
+ "Jump to things tree-style."
+ :group 'convenience
+ :prefix "avy-")
+
+(defcustom avy-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
+ "Default keys for jumping.
+Any key is either a character representing a self-inserting
+key (letters, digits, punctuation, etc.) or a symbol denoting a
+non-printing key like an arrow key (left, right, up, down). For
+non-printing keys, a corresponding entry in
+`avy-key-to-char-alist' must exist in order to visualize the key
+in the avy overlays.
+
+If `avy-style' is set to words, make sure there are at least three
+keys different than the following: a, e, i, o, u, y"
+ :type '(repeat :tag "Keys" (choice
+ (character :tag "char")
+ (symbol :tag "non-printing key"))))
+
+(defcustom avy-keys-alist nil
+ "Alist of avy-jump commands to `avy-keys' overriding the default `avy-keys'."
+ :type '(alist
+ :key-type (choice :tag "Command"
+ (const avy-goto-char)
+ (const avy-goto-char-2)
+ (const avy-isearch)
+ (const avy-goto-line)
+ (const avy-goto-subword-0)
+ (const avy-goto-subword-1)
+ (const avy-goto-word-0)
+ (const avy-goto-word-1)
+ (const avy-copy-line)
+ (const avy-copy-region)
+ (const avy-move-line)
+ (const avy-move-region)
+ (const avy-kill-whole-line)
+ (const avy-kill-region)
+ (const avy-kill-ring-save-whole-line)
+ (const avy-kill-ring-save-region)
+ (function :tag "Other command"))
+ :value-type (repeat :tag "Keys" character)))
+
+(defcustom avy-words
+ '("am" "by" "if" "is" "it" "my" "ox" "up"
+ "ace" "act" "add" "age" "ago" "aim" "air" "ale" "all" "and" "ant" "any"
+ "ape" "apt" "arc" "are" "arm" "art" "ash" "ate" "awe" "axe" "bad" "bag"
+ "ban" "bar" "bat" "bay" "bed" "bee" "beg" "bet" "bid" "big" "bit" "bob"
+ "bot" "bow" "box" "boy" "but" "cab" "can" "cap" "car" "cat" "cog" "cop"
+ "cow" "cry" "cup" "cut" "day" "dew" "did" "die" "dig" "dim" "dip" "dog"
+ "dot" "dry" "dub" "dug" "dye" "ear" "eat" "eel" "egg" "ego" "elf" "eve"
+ "eye" "fan" "far" "fat" "fax" "fee" "few" "fin" "fit" "fix" "flu" "fly"
+ "foe" "fog" "for" "fox" "fry" "fun" "fur" "gag" "gap" "gas" "gel" "gem"
+ "get" "gig" "gin" "gnu" "god" "got" "gum" "gun" "gut" "guy" "gym" "had"
+ "hag" "ham" "has" "hat" "her" "hid" "him" "hip" "his" "hit" "hop" "hot"
+ "how" "hub" "hue" "hug" "hut" "ice" "icy" "imp" "ink" "inn" "ion" "ire"
+ "ivy" "jab" "jam" "jar" "jaw" "jet" "job" "jog" "joy" "key" "kid" "kit"
+ "lag" "lap" "lay" "let" "lid" "lie" "lip" "lit" "lob" "log" "lot" "low"
+ "mad" "man" "map" "mat" "may" "men" "met" "mix" "mob" "mop" "mud" "mug"
+ "nag" "nap" "new" "nil" "nod" "nor" "not" "now" "nun" "oak" "odd" "off"
+ "oil" "old" "one" "orb" "ore" "ork" "our" "out" "owl" "own" "pad" "pan"
+ "par" "pat" "paw" "pay" "pea" "pen" "pet" "pig" "pin" "pit" "pod" "pot"
+ "pry" "pub" "pun" "put" "rag" "ram" "ran" "rat" "raw" "ray" "red" "rib"
+ "rim" "rip" "rob" "rod" "rot" "row" "rub" "rug" "rum" "run" "sad" "sat"
+ "saw" "say" "sea" "see" "sew" "she" "shy" "sin" "sip" "sit" "six" "ski"
+ "sky" "sly" "sob" "son" "soy" "spy" "sum" "sun" "tab" "tad" "tag" "tan"
+ "tap" "tar" "tax" "tea" "the" "tie" "tin" "tip" "toe" "ton" "too" "top"
+ "toy" "try" "tub" "two" "urn" "use" "van" "war" "was" "wax" "way" "web"
+ "wed" "wet" "who" "why" "wig" "win" "wit" "woe" "won" "wry" "you" "zap"
+ "zip" "zoo")
+ "Words to use in case `avy-style' is set to `words'.
+Every word should contain at least one vowel i.e. one of the following
+characters: a, e, i, o, u, y
+They do not have to be sorted but no word should be a prefix of another one."
+ :type '(repeat string))
+
+(defcustom avy-style 'at-full
+ "The default method of displaying the overlays.
+Use `avy-styles-alist' to customize this per-command."
+ :type '(choice
+ (const :tag "Pre" pre)
+ (const :tag "At" at)
+ (const :tag "At Full" at-full)
+ (const :tag "Post" post)
+ (const :tag "De Bruijn" de-bruijn)
+ (const :tag "Words" words)))
+
+(defcustom avy-styles-alist nil
+ "Alist of avy-jump commands to the style for each command.
+If the commands isn't on the list, `avy-style' is used."
+ :type '(alist
+ :key-type (choice :tag "Command"
+ (const avy-goto-char)
+ (const avy-goto-char-2)
+ (const avy-isearch)
+ (const avy-goto-line)
+ (const avy-goto-subword-0)
+ (const avy-goto-subword-1)
+ (const avy-goto-word-0)
+ (const avy-goto-word-1)
+ (const avy-copy-line)
+ (const avy-copy-region)
+ (const avy-move-line)
+ (const avy-move-region)
+ (const avy-kill-whole-line)
+ (const avy-kill-region)
+ (const avy-kill-ring-save-whole-line)
+ (const avy-kill-ring-save-region)
+ (function :tag "Other command"))
+ :value-type (choice
+ (const :tag "Pre" pre)
+ (const :tag "At" at)
+ (const :tag "At Full" at-full)
+ (const :tag "Post" post)
+ (const :tag "De Bruijn" de-bruijn)
+ (const :tag "Words" words))))
+
+(defcustom avy-dispatch-alist
+ '((?x . avy-action-kill-move)
+ (?X . avy-action-kill-stay)
+ (?t . avy-action-teleport)
+ (?m . avy-action-mark)
+ (?n . avy-action-copy)
+ (?y . avy-action-yank)
+ (?i . avy-action-ispell))
+ "List of actions for `avy-handler-default'.
+
+Each item is (KEY . ACTION). When KEY not on `avy-keys' is
+pressed during the dispatch, ACTION is set to replace the default
+`avy-action-goto' once a candidate is finally selected."
+ :type
+ '(alist
+ :key-type (choice (character :tag "Char"))
+ :value-type (choice
+ (const :tag "Mark" avy-action-mark)
+ (const :tag "Copy" avy-action-copy)
+ (const :tag "Kill and move point" avy-action-kill-move)
+ (const :tag "Kill" avy-action-kill-stay))))
+
+(defcustom avy-background nil
+ "When non-nil, a gray background will be added during the selection."
+ :type 'boolean)
+
+(defcustom avy-all-windows t
+ "Determine the list of windows to consider in search of candidates."
+ :type
+ '(choice
+ (const :tag "All Frames" all-frames)
+ (const :tag "This Frame" t)
+ (const :tag "This Window" nil)))
+
+(defcustom avy-case-fold-search t
+ "Non-nil if searches should ignore case."
+ :type 'boolean)
+
+(defcustom avy-word-punc-regexp "[!-/:-@[-`{-~]"
+ "Regexp of punctuation chars that count as word starts for `avy-goto-word-1.
+When nil, punctuation chars will not be matched.
+
+\"[!-/:-@[-`{-~]\" will match all printable punctuation chars."
+ :type 'regexp)
+
+(defcustom avy-goto-word-0-regexp "\\b\\sw"
+ "Regexp that determines positions for `avy-goto-word-0'."
+ :type '(choice
+ (const :tag "Default" "\\b\\sw")
+ (const :tag "Symbol" "\\_<\\(\\sw\\|\\s_\\)")
+ (const :tag "Not whitespace" "[^ \r\n\t]+")
+ (regexp :tag "Regex")))
+
+(defcustom avy-ignored-modes '(image-mode doc-view-mode pdf-view-mode)
+ "List of modes to ignore when searching for candidates.
+Typically, these modes don't use the text representation."
+ :type 'list)
+
+(defvar avy-ring (make-ring 20)
+ "Hold the window and point history.")
+
+(defvar avy-translate-char-function #'identity
+ "Function to translate user input key into another key.
+For example, to make SPC do the same as ?a, use
+\(lambda (c) (if (= c 32) ?a c)).")
+
+(defface avy-lead-face-0
+ '((t (:foreground "white" :background "#4f57f9")))
+ "Face used for first non-terminating leading chars.")
+
+(defface avy-lead-face-1
+ '((t (:foreground "white" :background "gray")))
+ "Face used for matched leading chars.")
+
+(defface avy-lead-face-2
+ '((t (:foreground "white" :background "#f86bf3")))
+ "Face used for leading chars.")
+
+(defface avy-lead-face
+ '((t (:foreground "white" :background "#e52b50")))
+ "Face used for the leading chars.")
+
+(defface avy-background-face
+ '((t (:foreground "gray40")))
+ "Face for whole window background during selection.")
+
+(defface avy-goto-char-timer-face
+ '((t (:inherit highlight)))
+ "Face for matches during reading chars using `avy-goto-char-timer'.")
+
+(defconst avy-lead-faces '(avy-lead-face
+ avy-lead-face-0
+ avy-lead-face-2
+ avy-lead-face
+ avy-lead-face-0
+ avy-lead-face-2)
+ "Face sequence for `avy--overlay-at-full'.")
+
+(defvar avy-key-to-char-alist '((left . ?◀)
+ (right . ?▶)
+ (up . ?▲)
+ (down . ?▼)
+ (prior . ?△)
+ (next . ?▽))
+ "An alist from non-character keys to printable chars used in avy overlays.
+This alist must contain all keys used in `avy-keys' which are not
+self-inserting keys and thus aren't read as characters.")
+
+;;* Internals
+;;** Tree
+(defmacro avy-multipop (lst n)
+ "Remove LST's first N elements and return them."
+ `(if (<= (length ,lst) ,n)
+ (prog1 ,lst
+ (setq ,lst nil))
+ (prog1 ,lst
+ (setcdr
+ (nthcdr (1- ,n) (prog1 ,lst (setq ,lst (nthcdr ,n ,lst))))
+ nil))))
+
+(defun avy--de-bruijn (keys n)
+ "De Bruijn sequence for alphabet KEYS and subsequences of length N."
+ (let* ((k (length keys))
+ (a (make-list (* n k) 0))
+ sequence)
+ (cl-labels ((db (T p)
+ (if (> T n)
+ (if (eq (% n p) 0)
+ (setq sequence
+ (append sequence
+ (cl-subseq a 1 (1+ p)))))
+ (setf (nth T a) (nth (- T p) a))
+ (db (1+ T) p)
+ (cl-loop for j from (1+ (nth (- T p) a)) to (1- k) do
+ (setf (nth T a) j)
+ (db (1+ T) T)))))
+ (db 1 1)
+ (mapcar (lambda (n)
+ (nth n keys))
+ sequence))))
+
+(defun avy--path-alist-1 (lst seq-len keys)
+ "Build a De Bruin sequence from LST.
+SEQ-LEN is how many elements of KEYS it takes to identify a match."
+ (let ((db-seq (avy--de-bruijn keys seq-len))
+ prev-pos prev-seq prev-win path-alist)
+ ;; The De Bruijn seq is cyclic, so append the seq-len - 1 first chars to
+ ;; the end.
+ (setq db-seq (nconc db-seq (cl-subseq db-seq 0 (1- seq-len))))
+ (cl-labels ((subseq-and-pop ()
+ (when (nth (1- seq-len) db-seq)
+ (prog1 (cl-subseq db-seq 0 seq-len)
+ (pop db-seq)))))
+ (while lst
+ (let* ((cur (car lst))
+ (pos (cond
+ ;; ace-window has matches of the form (pos . wnd)
+ ((integerp (car cur)) (car cur))
+ ;; avy-jump have form ((start . end) . wnd)
+ ((consp (car cur)) (caar cur))
+ (t (error "Unexpected match representation: %s" cur))))
+ (win (cdr cur))
+ (path (if prev-pos
+ (let ((diff (if (eq win prev-win)
+ (- pos prev-pos)
+ 0)))
+ (when (and (> diff 0) (< diff seq-len))
+ (while (and (nth (1- seq-len) db-seq)
+ (not
+ (eq 0
+ (cl-search
+ (cl-subseq prev-seq diff)
+ (cl-subseq db-seq 0 seq-len)))))
+ (pop db-seq)))
+ (subseq-and-pop))
+ (subseq-and-pop))))
+ (if (not path)
+ (setq lst nil
+ path-alist nil)
+ (push (cons path (car lst)) path-alist)
+ (setq prev-pos pos
+ prev-seq path
+ prev-win win
+ lst (cdr lst))))))
+ (nreverse path-alist)))
+
+(defun avy-tree (lst keys)
+ "Coerce LST into a balanced tree.
+The degree of the tree is the length of KEYS.
+KEYS are placed appropriately on internal nodes."
+ (let ((len (length keys)))
+ (cl-labels
+ ((rd (ls)
+ (let ((ln (length ls)))
+ (if (< ln len)
+ (cl-pairlis keys
+ (mapcar (lambda (x) (cons 'leaf x)) ls))
+ (let ((ks (copy-sequence keys))
+ res)
+ (dolist (s (avy-subdiv ln len))
+ (push (cons (pop ks)
+ (if (eq s 1)
+ (cons 'leaf (pop ls))
+ (rd (avy-multipop ls s))))
+ res))
+ (nreverse res))))))
+ (rd lst))))
+
+(defun avy-subdiv (n b)
+ "Distribute N in B terms in a balanced way."
+ (let* ((p (1- (floor (+ (log n b) 1e-6))))
+ (x1 (expt b p))
+ (x2 (* b x1))
+ (delta (- n x2))
+ (n2 (/ delta (- x2 x1)))
+ (n1 (- b n2 1)))
+ (append
+ (make-list n1 x1)
+ (list
+ (- n (* n1 x1) (* n2 x2)))
+ (make-list n2 x2))))
+
+(defun avy-traverse (tree walker &optional recur-key)
+ "Traverse TREE generated by `avy-tree'.
+WALKER is a function that takes KEYS and LEAF.
+
+RECUR-KEY is used in recursion.
+
+LEAF is a member of LST argument of `avy-tree'.
+
+KEYS is the path from the root of `avy-tree' to LEAF."
+ (dolist (br tree)
+ (let ((key (cons (car br) recur-key)))
+ (if (eq (cadr br) 'leaf)
+ (funcall walker key (cddr br))
+ (avy-traverse (cdr br) walker key)))))
+
+(defvar avy-action nil
+ "Function to call at the end of select.")
+
+(defun avy-handler-default (char)
+ "The default handler for a bad CHAR."
+ (let (dispatch)
+ (cond ((setq dispatch (assoc char avy-dispatch-alist))
+ (setq avy-action (cdr dispatch))
+ (throw 'done 'restart))
+ ((memq char '(27 ?\C-g))
+ ;; exit silently
+ (throw 'done 'exit))
+ ((mouse-event-p char)
+ (signal 'user-error (list "Mouse event not handled" char)))
+ (t
+ (signal 'user-error (list "No such candidate"
+ (if (characterp char) (string char) char)))
+ (throw 'done nil)))))
+
+(defvar avy-handler-function 'avy-handler-default
+ "A function to call for a bad `read-key' in `avy-read'.")
+
+(defvar avy-current-path ""
+ "Store the current incomplete path during `avy-read'.")
+
+(defun avy-mouse-event-window (char)
+ "If CHAR is a mouse event, return the window of the event if any or the selected window.
+Return nil if not a mouse event."
+ (when (mouse-event-p char)
+ (cond ((windowp (posn-window (event-start char)))
+ (posn-window (event-start char)))
+ ((framep (posn-window (event-start char)))
+ (frame-selected-window (posn-window (event-start char))))
+ (t (selected-window)))))
+
+(defun avy-read (tree display-fn cleanup-fn)
+ "Select a leaf from TREE using consecutive `read-char'.
+
+DISPLAY-FN should take CHAR and LEAF and signify that LEAFs
+associated with CHAR will be selected if CHAR is pressed. This is
+commonly done by adding a CHAR overlay at LEAF position.
+
+CLEANUP-FN should take no arguments and remove the effects of
+multiple DISPLAY-FN invocations."
+ (catch 'done
+ (setq avy-current-path "")
+ (while tree
+ (let ((avy--leafs nil))
+ (avy-traverse tree
+ (lambda (path leaf)
+ (push (cons path leaf) avy--leafs)))
+ (dolist (x avy--leafs)
+ (funcall display-fn (car x) (cdr x))))
+ (let ((char (funcall avy-translate-char-function (read-key)))
+ window
+ branch)
+ (funcall cleanup-fn)
+ (if (setq window (avy-mouse-event-window char))
+ (throw 'done (cons char window))
+ ;; Ensure avy-current-path stores the full path prior to
+ ;; exit so other packages can utilize its value.
+ (setq avy-current-path
+ (concat avy-current-path (string (avy--key-to-char char))))
+ (if (setq branch (assoc char tree))
+ (if (eq (car (setq tree (cdr branch))) 'leaf)
+ (throw 'done (cdr tree)))
+ (funcall avy-handler-function char)))))))
+
+(defun avy-read-de-bruijn (lst keys)
+ "Select from LST dispatching on KEYS."
+ ;; In theory, the De Bruijn sequence B(k,n) has k^n subsequences of length n
+ ;; (the path length) usable as paths, thus that's the lower bound. Due to
+ ;; partially overlapping matches, not all subsequences may be usable, so it's
+ ;; possible that the path-len must be incremented, e.g., if we're matching
+ ;; for x and a buffer contains xaxbxcx only every second subsequence is
+ ;; usable for the four matches.
+ (catch 'done
+ (let* ((path-len (ceiling (log (length lst) (length keys))))
+ (alist (avy--path-alist-1 lst path-len keys)))
+ (while (not alist)
+ (cl-incf path-len)
+ (setq alist (avy--path-alist-1 lst path-len keys)))
+ (let* ((len (length (caar alist)))
+ (i 0))
+ (setq avy-current-path "")
+ (while (< i len)
+ (dolist (x (reverse alist))
+ (avy--overlay-at-full (reverse (car x)) (cdr x)))
+ (let ((char (funcall avy-translate-char-function (read-key))))
+ (avy--remove-leading-chars)
+ (setq alist
+ (delq nil
+ (mapcar (lambda (x)
+ (when (eq (caar x) char)
+ (cons (cdr (car x)) (cdr x))))
+ alist)))
+ (setq avy-current-path
+ (concat avy-current-path (string (avy--key-to-char char))))
+ (cl-incf i)
+ (unless alist
+ (funcall avy-handler-function char))))
+ (cdar alist)))))
+
+(defun avy-read-words (lst words)
+ "Select from LST using WORDS."
+ (catch 'done
+ (let ((num-words (length words))
+ (num-entries (length lst))
+ alist)
+ ;; If there are not enough words to cover all the candidates,
+ ;; we use a De Bruijn sequence to generate the remaining ones.
+ (when (< num-words num-entries)
+ (let ((keys avy-keys)
+ (bad-keys '(?a ?e ?i ?o ?u ?y))
+ (path-len 1)
+ (num-remaining (- num-entries num-words))
+ tmp-alist)
+ ;; Delete all keys which could lead to duplicates.
+ ;; We want at least three keys left to work with.
+ (dolist (x bad-keys)
+ (when (memq x keys)
+ (setq keys (delq ?a keys))))
+ (when (< (length keys) 3)
+ (signal 'user-error
+ '("Please add more keys to the variable `avy-keys'.")))
+ ;; Generate the sequence and add the keys to the existing words.
+ (while (not tmp-alist)
+ (cl-incf path-len)
+ (setq tmp-alist (avy--path-alist-1 lst path-len keys)))
+ (while (>= (cl-decf num-remaining) 0)
+ (push (mapconcat 'string (caar tmp-alist) nil) (cdr (last words)))
+ (setq tmp-alist (cdr tmp-alist)))))
+ (dolist (x lst)
+ (push (cons (string-to-list (pop words)) x) alist))
+ (setq avy-current-path "")
+ (while (or (> (length alist) 1)
+ (caar alist))
+ (dolist (x (reverse alist))
+ (avy--overlay-at-full (reverse (car x)) (cdr x)))
+ (let ((char (funcall avy-translate-char-function (read-key))))
+ (avy--remove-leading-chars)
+ (setq alist
+ (delq nil
+ (mapcar (lambda (x)
+ (when (eq (caar x) char)
+ (cons (cdr (car x)) (cdr x))))
+ alist)))
+ (setq avy-current-path
+ (concat avy-current-path (string (avy--key-to-char char))))
+ (unless alist
+ (funcall avy-handler-function char))))
+ (cdar alist))))
+
+;;** Rest
+(defun avy-window-list ()
+ "Return a list of windows depending on `avy-all-windows'."
+ (cond ((eq avy-all-windows 'all-frames)
+ (cl-mapcan #'window-list (frame-list)))
+
+ ((eq avy-all-windows t)
+ (window-list))
+
+ ((null avy-all-windows)
+ (list (selected-window)))
+
+ (t
+ (error "Unrecognized option: %S" avy-all-windows))))
+
+(defcustom avy-all-windows-alt nil
+ "The alternative `avy-all-windows' for use with \\[universal-argument]."
+ :type '(choice
+ (const :tag "Current window" nil)
+ (const :tag "All windows on the current frame" t)
+ (const :tag "All windows on all frames" all-frames)))
+
+(defmacro avy-dowindows (flip &rest body)
+ "Depending on FLIP and `avy-all-windows' run BODY in each or selected window."
+ (declare (indent 1)
+ (debug (form body)))
+ `(let ((avy-all-windows (if ,flip
+ avy-all-windows-alt
+ avy-all-windows)))
+ (dolist (wnd (avy-window-list))
+ (with-selected-window wnd
+ (unless (memq major-mode avy-ignored-modes)
+ ,@body)))))
+
+(defun avy-resume ()
+ "Stub to hold last avy command.
+Commands using `avy-with' macro can be resumed."
+ (interactive))
+
+(defvar avy-command nil
+ "Store the current command symbol.
+E.g. 'avy-goto-line or 'avy-goto-char.")
+
+(defmacro avy-with (command &rest body)
+ "Set `avy-keys' according to COMMAND and execute BODY.
+Set `avy-style' according to COMMMAND as well."
+ (declare (indent 1)
+ (debug (form body)))
+ `(let ((avy-keys (or (cdr (assq ',command avy-keys-alist))
+ avy-keys))
+ (avy-style (or (cdr (assq ',command avy-styles-alist))
+ avy-style))
+ (avy-command ',command))
+ (setq avy-action nil)
+ (setf (symbol-function 'avy-resume)
+ (lambda ()
+ (interactive)
+ ,@body))
+ ,@body))
+
+(defun avy-action-goto (pt)
+ "Goto PT."
+ (let ((frame (window-frame (selected-window))))
+ (unless (equal frame (selected-frame))
+ (select-frame-set-input-focus frame)
+ (raise-frame frame))
+ (goto-char pt)))
+
+(defun avy-forward-item ()
+ (if (eq avy-command 'avy-goto-line)
+ (end-of-line)
+ (forward-sexp))
+ (point))
+
+(defun avy-action-mark (pt)
+ "Mark sexp at PT."
+ (goto-char pt)
+ (set-mark (point))
+ (avy-forward-item))
+
+(defun avy-action-copy (pt)
+ "Copy sexp starting on PT."
+ (save-excursion
+ (let (str)
+ (goto-char pt)
+ (avy-forward-item)
+ (setq str (buffer-substring pt (point)))
+ (kill-new str)
+ (message "Copied: %s" str)))
+ (let ((dat (ring-ref avy-ring 0)))
+ (select-frame-set-input-focus
+ (window-frame (cdr dat)))
+ (select-window (cdr dat))
+ (goto-char (car dat))))
+
+(defun avy-action-yank (pt)
+ "Yank sexp starting at PT at the current point."
+ (avy-action-copy pt)
+ (yank)
+ t)
+
+(defun avy-action-kill-move (pt)
+ "Kill sexp at PT and move there."
+ (goto-char pt)
+ (avy-forward-item)
+ (kill-region pt (point))
+ (message "Killed: %s" (current-kill 0))
+ (point))
+
+(defun avy-action-kill-stay (pt)
+ "Kill sexp at PT."
+ (save-excursion
+ (goto-char pt)
+ (avy-forward-item)
+ (kill-region pt (point))
+ (just-one-space))
+ (message "Killed: %s" (current-kill 0))
+ (select-window
+ (cdr
+ (ring-ref avy-ring 0)))
+ t)
+
+(defun avy-action-teleport (pt)
+ "Kill sexp starting on PT and yank into the current location."
+ (avy-action-kill-stay pt)
+ (select-window
+ (cdr
+ (ring-ref avy-ring 0)))
+ (save-excursion
+ (yank))
+ t)
+
+(declare-function flyspell-correct-word-before-point "flyspell")
+
+(defun avy-action-ispell (pt)
+ "Auto correct word at PT."
+ (save-excursion
+ (goto-char pt)
+ (cond
+ ((eq avy-command 'avy-goto-line)
+ (ispell-region
+ (line-beginning-position)
+ (line-end-position)))
+ ((bound-and-true-p flyspell-mode)
+ (flyspell-correct-word-before-point))
+ ((looking-at-p "\\b")
+ (ispell-word))
+ (t
+ (progn
+ (backward-word)
+ (when (looking-at-p "\\b")
+ (ispell-word)))))))
+
+(defun avy--process (candidates overlay-fn)
+ "Select one of CANDIDATES using `avy-read'.
+Use OVERLAY-FN to visualize the decision overlay."
+ (unless (and (consp (car candidates))
+ (windowp (cdar candidates)))
+ (setq candidates
+ (mapcar (lambda (x) (cons x (selected-window)))
+ candidates)))
+ (let ((len (length candidates))
+ (cands (copy-sequence candidates))
+ res)
+ (if (= len 0)
+ (message "zero candidates")
+ (if (= len 1)
+ (setq res (car candidates))
+ (unwind-protect
+ (progn
+ (avy--make-backgrounds
+ (avy-window-list))
+ (setq res (cond ((eq avy-style 'de-bruijn)
+ (avy-read-de-bruijn
+ candidates avy-keys))
+ ((eq avy-style 'words)
+ (avy-read-words
+ candidates avy-words))
+ (t
+ (avy-read (avy-tree candidates avy-keys)
+ overlay-fn
+ #'avy--remove-leading-chars)))))
+ (avy--done)))
+ (cond ((eq res 'restart)
+ (avy--process cands overlay-fn))
+ ;; ignore exit from `avy-handler-function'
+ ((eq res 'exit))
+ (t
+ (avy-push-mark)
+ (when (and (consp res)
+ (windowp (cdr res)))
+ (let* ((window (cdr res))
+ (frame (window-frame window)))
+ (unless (equal frame (selected-frame))
+ (select-frame-set-input-focus frame))
+ (select-window window))
+ (setq res (car res)))
+
+ (funcall (or avy-action 'avy-action-goto)
+ (if (consp res)
+ (car res)
+ res)))))))
+
+(defvar avy--overlays-back nil
+ "Hold overlays for when `avy-background' is t.")
+
+(defun avy--make-backgrounds (wnd-list)
+ "Create a dim background overlay for each window on WND-LIST."
+ (when avy-background
+ (setq avy--overlays-back
+ (mapcar (lambda (w)
+ (let ((ol (make-overlay
+ (window-start w)
+ (window-end w)
+ (window-buffer w))))
+ (overlay-put ol 'face 'avy-background-face)
+ (overlay-put ol 'window w)
+ ol))
+ wnd-list))))
+
+(defun avy--done ()
+ "Clean up overlays."
+ (mapc #'delete-overlay avy--overlays-back)
+ (setq avy--overlays-back nil)
+ (avy--remove-leading-chars))
+
+(defun avy--next-visible-point ()
+ "Return the next closest point without 'invisible property."
+ (let ((s (point)))
+ (while (and (not (= (point-max) (setq s (next-char-property-change s))))
+ (get-char-property s 'invisible)))
+ s))
+
+(defun avy--next-invisible-point ()
+ "Return the next closest point with 'invisible property."
+ (let ((s (point)))
+ (while (and (not (= (point-max) (setq s (next-char-property-change s))))
+ (not (get-char-property s 'invisible))))
+ s))
+
+(defun avy--find-visible-regions (rbeg rend)
+ "Return a list of all visible regions between RBEG and REND."
+ (setq rbeg (max rbeg (point-min)))
+ (setq rend (min rend (point-max)))
+ (when (< rbeg rend)
+ (let (visibles beg)
+ (save-excursion
+ (save-restriction
+ (narrow-to-region rbeg rend)
+ (setq beg (goto-char (point-min)))
+ (while (not (= (point) (point-max)))
+ (goto-char (avy--next-invisible-point))
+ (push (cons beg (point)) visibles)
+ (setq beg (goto-char (avy--next-visible-point))))
+ (nreverse visibles))))))
+
+(defun avy--regex-candidates (regex &optional beg end pred group)
+ "Return all elements that match REGEX.
+Each element of the list is ((BEG . END) . WND)
+When PRED is non-nil, it's a filter for matching point positions.
+When GROUP is non-nil, (BEG . END) should delimit that regex group."
+ (setq group (or group 0))
+ (let ((case-fold-search (or avy-case-fold-search
+ (string= regex (downcase regex))))
+ candidates)
+ (avy-dowindows current-prefix-arg
+ (dolist (pair (avy--find-visible-regions
+ (or beg (window-start))
+ (or end (window-end (selected-window) t))))
+ (save-excursion
+ (goto-char (car pair))
+ (while (re-search-forward regex (cdr pair) t)
+ (unless (get-char-property (1- (point)) 'invisible)
+ (when (or (null pred)
+ (funcall pred))
+ (push (cons (cons (match-beginning group)
+ (match-end group))
+ wnd) candidates)))))))
+ (nreverse candidates)))
+
+(defvar avy--overlay-offset 0
+ "The offset to apply in `avy--overlay'.")
+
+(defvar avy--overlays-lead nil
+ "Hold overlays for leading chars.")
+
+(defun avy--remove-leading-chars ()
+ "Remove leading char overlays."
+ (mapc #'delete-overlay avy--overlays-lead)
+ (setq avy--overlays-lead nil))
+
+(defun avy--old-str (pt wnd)
+ "Return a one-char string at PT in WND."
+ (let ((old-str (with-selected-window wnd
+ (buffer-substring pt (1+ pt)))))
+ (if avy-background
+ (propertize old-str 'face 'avy-background-face)
+ old-str)))
+
+(defun avy--overlay (str beg end wnd &optional compose-fn)
+ "Create an overlay with STR from BEG to END in WND.
+COMPOSE-FN is a lambda that concatenates the old string at BEG with STR."
+ (let ((eob (with-selected-window wnd (point-max))))
+ (when (<= beg eob)
+ (let* ((beg (+ beg avy--overlay-offset))
+ (ol (make-overlay beg (or end (1+ beg)) (window-buffer wnd)))
+ (old-str (if (eq beg eob) "" (avy--old-str beg wnd)))
+ (os-line-prefix (get-text-property 0 'line-prefix old-str))
+ (os-wrap-prefix (get-text-property 0 'wrap-prefix old-str))
+ other-ol)
+ (when os-line-prefix
+ (add-text-properties 0 1 `(line-prefix ,os-line-prefix) str))
+ (when os-wrap-prefix
+ (add-text-properties 0 1 `(wrap-prefix ,os-wrap-prefix) str))
+ (when (setq other-ol (cl-find-if
+ (lambda (o) (overlay-get o 'goto-address))
+ (overlays-at beg)))
+ (add-text-properties
+ 0 (length old-str)
+ `(face ,(overlay-get other-ol 'face)) old-str))
+ (overlay-put ol 'window wnd)
+ (overlay-put ol 'category 'avy)
+ (overlay-put ol (if (eq beg eob)
+ 'after-string
+ 'display)
+ (funcall
+ (or compose-fn #'concat)
+ str old-str))
+ (push ol avy--overlays-lead)))))
+
+(defcustom avy-highlight-first nil
+ "When non-nil highlight the first decision char with `avy-lead-face-0'.
+Do this even when the char is terminating."
+ :type 'boolean)
+
+(defun avy--key-to-char (c)
+ "If C is no character, translate it using `avy-key-to-char-alist'."
+ (cond ((characterp c) c)
+ ((cdr (assoc c avy-key-to-char-alist)))
+ ((mouse-event-p c) c)
+ (t
+ (error "Unknown key %s" c))))
+
+(defun avy-candidate-beg (leaf)
+ "Return the start position for LEAF."
+ (cond ((numberp leaf)
+ leaf)
+ ((consp (car leaf))
+ (caar leaf))
+ (t
+ (car leaf))))
+
+(defun avy-candidate-end (leaf)
+ "Return the end position for LEAF."
+ (cond ((numberp leaf)
+ leaf)
+ ((consp (car leaf))
+ (cdar leaf))
+ (t
+ (car leaf))))
+
+(defun avy-candidate-wnd (leaf)
+ "Return the window for LEAF."
+ (if (consp leaf)
+ (cdr leaf)
+ (selected-window)))
+
+(defun avy--overlay-pre (path leaf)
+ "Create an overlay with PATH at LEAF.
+PATH is a list of keys from tree root to LEAF.
+LEAF is normally ((BEG . END) . WND)."
+ (let* ((path (mapcar #'avy--key-to-char path))
+ (str (propertize (apply #'string (reverse path))
+ 'face 'avy-lead-face)))
+ (when (or avy-highlight-first (> (length str) 1))
+ (set-text-properties 0 1 '(face avy-lead-face-0) str))
+ (setq str (concat
+ (propertize avy-current-path
+ 'face 'avy-lead-face-1)
+ str))
+ (avy--overlay
+ str
+ (avy-candidate-beg leaf) nil
+ (avy-candidate-wnd leaf))))
+
+(defun avy--overlay-at (path leaf)
+ "Create an overlay with PATH at LEAF.
+PATH is a list of keys from tree root to LEAF.
+LEAF is normally ((BEG . END) . WND)."
+ (let* ((path (mapcar #'avy--key-to-char path))
+ (str (propertize
+ (string (car (last path)))
+ 'face 'avy-lead-face)))
+ (avy--overlay
+ str
+ (avy-candidate-beg leaf) nil
+ (avy-candidate-wnd leaf)
+ (lambda (str old-str)
+ (cond ((string= old-str "\n")
+ (concat str "\n"))
+ ;; add padding for wide-width character
+ ((eq (string-width old-str) 2)
+ (concat str " "))
+ (t
+ str))))))
+
+(defun avy--overlay-at-full (path leaf)
+ "Create an overlay with PATH at LEAF.
+PATH is a list of keys from tree root to LEAF.
+LEAF is normally ((BEG . END) . WND)."
+ (let* ((path (mapcar #'avy--key-to-char path))
+ (str (propertize
+ (apply #'string (reverse path))
+ 'face 'avy-lead-face))
+ (len (length path))
+ (beg (avy-candidate-beg leaf))
+ (wnd (cdr leaf))
+ end)
+ (dotimes (i len)
+ (set-text-properties (- len i 1) (- len i)
+ `(face ,(nth i avy-lead-faces))
+ str))
+ (when (eq avy-style 'de-bruijn)
+ (setq str (concat
+ (propertize avy-current-path
+ 'face 'avy-lead-face-1)
+ str))
+ (setq len (length str)))
+ (with-selected-window wnd
+ (save-excursion
+ (goto-char beg)
+ (let* ((lep (if (bound-and-true-p visual-line-mode)
+ (save-excursion
+ (end-of-visual-line)
+ (point))
+ (line-end-position)))
+ ;; `end-of-visual-line' is bugged sometimes
+ (lep (if (< lep beg)
+ (line-end-position)
+ lep))
+ (len-and-str (avy--update-offset-and-str len str lep)))
+ (setq len (car len-and-str))
+ (setq str (cdr len-and-str))
+ (setq end (if (= beg lep)
+ (1+ beg)
+ (min (+ beg
+ (if (eq (char-after) ?\t)
+ 1
+ len))
+ lep)))
+ (when (and (bound-and-true-p visual-line-mode)
+ (> len (- end beg))
+ (not (eq lep beg)))
+ (setq len (- end beg))
+ (let ((old-str (apply #'string (reverse path))))
+ (setq str
+ (substring
+ (propertize
+ old-str
+ 'face
+ (if (= (length old-str) 1)
+ 'avy-lead-face
+ 'avy-lead-face-0))
+ 0 len)))))))
+ (avy--overlay
+ str beg end wnd
+ (lambda (str old-str)
+ (cond ((string= old-str "\n")
+ (concat str "\n"))
+ ((string= old-str "\t")
+ (concat str (make-string (max (- tab-width len) 0) ?\ )))
+ (t
+ ;; add padding for wide-width character
+ (if (eq (string-width old-str) 2)
+ (concat str " ")
+ str)))))))
+
+(defun avy--overlay-post (path leaf)
+ "Create an overlay with PATH at LEAF.
+PATH is a list of keys from tree root to LEAF.
+LEAF is normally ((BEG . END) . WND)."
+ (let* ((path (mapcar #'avy--key-to-char path))
+ (str (propertize (apply #'string (reverse path))
+ 'face 'avy-lead-face)))
+ (when (or avy-highlight-first (> (length str) 1))
+ (set-text-properties 0 1 '(face avy-lead-face-0) str))
+ (setq str (concat
+ (propertize avy-current-path
+ 'face 'avy-lead-face-1)
+ str))
+ (avy--overlay
+ str
+ (avy-candidate-end leaf) nil
+ (avy-candidate-wnd leaf))))
+
+(defun avy--update-offset-and-str (offset str lep)
+ "Recalculate the length of the new overlay at point.
+
+OFFSET is the previous overlay length.
+STR is the overlay string that we wish to add.
+LEP is the line end position.
+
+We want to add an overlay between point and END=point+OFFSET.
+When other overlays already exist between point and END, set
+OFFSET to be the difference between the start of the first
+overlay and point. This is equivalent to truncating our new
+overlay, so that it doesn't intersect with overlays that already
+exist."
+ (let* ((wnd (selected-window))
+ (beg (point))
+ (oov (delq nil
+ (mapcar
+ (lambda (o)
+ (and (eq (overlay-get o 'category) 'avy)
+ (eq (overlay-get o 'window) wnd)
+ (overlay-start o)))
+ (overlays-in beg (min (+ beg offset) lep))))))
+ (when oov
+ (setq offset (- (apply #'min oov) beg))
+ (setq str (substring str 0 offset)))
+ (let ((other-ov (cl-find-if
+ (lambda (o)
+ (and (eq (overlay-get o 'category) 'avy)
+ (eq (overlay-start o) beg)
+ (not (eq (overlay-get o 'window) wnd))))
+ (overlays-in (point) (min (+ (point) offset) lep)))))
+ (when (and other-ov
+ (> (overlay-end other-ov)
+ (+ beg offset)))
+ (setq str (concat str (buffer-substring
+ (+ beg offset)
+ (overlay-end other-ov))))
+ (setq offset (- (overlay-end other-ov)
+ beg))))
+ (cons offset str)))
+
+(defun avy--style-fn (style)
+ "Transform STYLE symbol to a style function."
+ (cl-case style
+ (pre #'avy--overlay-pre)
+ (at #'avy--overlay-at)
+ (at-full 'avy--overlay-at-full)
+ (post #'avy--overlay-post)
+ (de-bruijn #'avy--overlay-at-full)
+ (words #'avy--overlay-at-full)
+ (t (error "Unexpected style %S" style))))
+
+(defun avy--generic-jump (regex window-flip style &optional beg end)
+ "Jump to REGEX.
+The window scope is determined by `avy-all-windows'.
+When WINDOW-FLIP is non-nil, do the opposite of `avy-all-windows'.
+STYLE determines the leading char overlay style.
+BEG and END narrow the scope where candidates are searched."
+ (let ((avy-all-windows
+ (if window-flip
+ (not avy-all-windows)
+ avy-all-windows)))
+ (avy--process
+ (avy--regex-candidates regex beg end)
+ (avy--style-fn style))))
+
+;;* Commands
+;;;###autoload
+(defun avy-goto-char (char &optional arg)
+ "Jump to the currently visible CHAR.
+The window scope is determined by `avy-all-windows' (ARG negates it)."
+ (interactive (list (read-char "char: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-char
+ (avy--generic-jump
+ (if (= 13 char)
+ "\n"
+ (regexp-quote (string char)))
+ arg
+ avy-style)))
+
+;;;###autoload
+(defun avy-goto-char-in-line (char)
+ "Jump to the currently visible CHAR in the current line."
+ (interactive (list (read-char "char: " t)))
+ (avy-with avy-goto-char
+ (avy--generic-jump
+ (regexp-quote (string char))
+ avy-all-windows
+ avy-style
+ (line-beginning-position)
+ (line-end-position))))
+
+;;;###autoload
+(defun avy-goto-char-2 (char1 char2 &optional arg beg end)
+ "Jump to the currently visible CHAR1 followed by CHAR2.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+BEG and END narrow the scope where candidates are searched."
+ (interactive (list (read-char "char 1: " t)
+ (read-char "char 2: " t)
+ current-prefix-arg
+ nil nil))
+ (when (eq char1 ?\r)
+ (setq char1 ?\n))
+ (when (eq char2 ?\r)
+ (setq char2 ?\n))
+ (avy-with avy-goto-char-2
+ (avy--generic-jump
+ (regexp-quote (string char1 char2))
+ arg
+ avy-style
+ beg end)))
+
+;;;###autoload
+(defun avy-goto-char-2-above (char1 char2 &optional arg)
+ "Jump to the currently visible CHAR1 followed by CHAR2.
+This is a scoped version of `avy-goto-char-2', where the scope is
+the visible part of the current buffer up to point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive (list (read-char "char 1: " t)
+ (read-char "char 2: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-char-2-above
+ (avy-goto-char-2
+ char1 char2 arg
+ (window-start) (point))))
+
+;;;###autoload
+(defun avy-goto-char-2-below (char1 char2 &optional arg)
+ "Jump to the currently visible CHAR1 followed by CHAR2.
+This is a scoped version of `avy-goto-char-2', where the scope is
+the visible part of the current buffer following point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive (list (read-char "char 1: " t)
+ (read-char "char 2: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-char-2-below
+ (avy-goto-char-2
+ char1 char2 arg
+ (point) (window-end (selected-window) t))))
+
+;;;###autoload
+(defun avy-isearch ()
+ "Jump to one of the current isearch candidates."
+ (interactive)
+ (avy-with avy-isearch
+ (let ((avy-background nil))
+ (avy--process
+ (avy--regex-candidates (if isearch-regexp
+ isearch-string
+ (regexp-quote isearch-string)))
+ (avy--style-fn avy-style))
+ (isearch-done))))
+
+;;;###autoload
+(defun avy-goto-word-0 (arg &optional beg end)
+ "Jump to a word start.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+BEG and END narrow the scope where candidates are searched."
+ (interactive "P")
+ (avy-with avy-goto-word-0
+ (avy--generic-jump avy-goto-word-0-regexp arg avy-style beg end)))
+
+(defun avy-goto-word-0-above (arg)
+ "Jump to a word start between window start and point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive "P")
+ (avy-with avy-goto-word-0
+ (avy-goto-word-0 arg (window-start) (point))))
+
+(defun avy-goto-word-0-below (arg)
+ "Jump to a word start between point and window end.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive "P")
+ (avy-with avy-goto-word-0
+ (avy-goto-word-0 arg (point) (window-end (selected-window) t))))
+
+;;;###autoload
+(defun avy-goto-word-1 (char &optional arg beg end symbol)
+ "Jump to the currently visible CHAR at a word start.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+BEG and END narrow the scope where candidates are searched.
+When SYMBOL is non-nil, jump to symbol start instead of word start."
+ (interactive (list (read-char "char: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-word-1
+ (let* ((str (string char))
+ (regex (cond ((string= str ".")
+ "\\.")
+ ((and avy-word-punc-regexp
+ (string-match avy-word-punc-regexp str))
+ (regexp-quote str))
+ ((<= char 26)
+ str)
+ (t
+ (concat
+ (if symbol "\\_<" "\\b")
+ str)))))
+ (avy--generic-jump regex arg avy-style beg end))))
+
+;;;###autoload
+(defun avy-goto-word-1-above (char &optional arg)
+ "Jump to the currently visible CHAR at a word start.
+This is a scoped version of `avy-goto-word-1', where the scope is
+the visible part of the current buffer up to point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive (list (read-char "char: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-word-1
+ (avy-goto-word-1 char arg (window-start) (point))))
+
+;;;###autoload
+(defun avy-goto-word-1-below (char &optional arg)
+ "Jump to the currently visible CHAR at a word start.
+This is a scoped version of `avy-goto-word-1', where the scope is
+the visible part of the current buffer following point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive (list (read-char "char: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-word-1
+ (avy-goto-word-1 char arg (point) (window-end (selected-window) t))))
+
+;;;###autoload
+(defun avy-goto-symbol-1 (char &optional arg)
+ "Jump to the currently visible CHAR at a symbol start.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive (list (read-char "char: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-symbol-1
+ (avy-goto-word-1 char arg nil nil t)))
+
+;;;###autoload
+(defun avy-goto-symbol-1-above (char &optional arg)
+ "Jump to the currently visible CHAR at a symbol start.
+This is a scoped version of `avy-goto-symbol-1', where the scope is
+the visible part of the current buffer up to point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive (list (read-char "char: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-symbol-1-above
+ (avy-goto-word-1 char arg (window-start) (point) t)))
+
+;;;###autoload
+(defun avy-goto-symbol-1-below (char &optional arg)
+ "Jump to the currently visible CHAR at a symbol start.
+This is a scoped version of `avy-goto-symbol-1', where the scope is
+the visible part of the current buffer following point.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive (list (read-char "char: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-symbol-1-below
+ (avy-goto-word-1 char arg (point) (window-end (selected-window) t) t)))
+
+(declare-function subword-backward "subword")
+(defvar subword-backward-regexp)
+
+(defcustom avy-subword-extra-word-chars '(?{ ?= ?} ?* ?: ?> ?<)
+ "A list of characters that should temporarily match \"\\w\".
+This variable is used by `avy-goto-subword-0' and `avy-goto-subword-1'."
+ :type '(repeat character))
+
+;;;###autoload
+(defun avy-goto-subword-0 (&optional arg predicate)
+ "Jump to a word or subword start.
+
+The window scope is determined by `avy-all-windows' (ARG negates it).
+
+When PREDICATE is non-nil it's a function of zero parameters that
+should return true."
+ (interactive "P")
+ (require 'subword)
+ (avy-with avy-goto-subword-0
+ (let ((case-fold-search nil)
+ (subword-backward-regexp
+ "\\(\\(\\W\\|[[:lower:][:digit:]]\\)\\([!-/:@`~[:upper:]]+\\W*\\)\\|\\W\\w+\\)")
+ candidates)
+ (avy-dowindows arg
+ (let ((syn-tbl (copy-syntax-table)))
+ (dolist (char avy-subword-extra-word-chars)
+ (modify-syntax-entry char "w" syn-tbl))
+ (with-syntax-table syn-tbl
+ (let ((ws (window-start))
+ window-cands)
+ (save-excursion
+ (goto-char (window-end (selected-window) t))
+ (subword-backward)
+ (while (> (point) ws)
+ (when (or (null predicate)
+ (and predicate (funcall predicate)))
+ (unless (get-char-property (point) 'invisible)
+ (push (cons (point) (selected-window)) window-cands)))
+ (subword-backward))
+ (and (= (point) ws)
+ (or (null predicate)
+ (and predicate (funcall predicate)))
+ (not (get-char-property (point) 'invisible))
+ (push (cons (point) (selected-window)) window-cands)))
+ (setq candidates (nconc candidates window-cands))))))
+ (avy--process candidates (avy--style-fn avy-style)))))
+
+;;;###autoload
+(defun avy-goto-subword-1 (char &optional arg)
+ "Jump to the currently visible CHAR at a subword start.
+The window scope is determined by `avy-all-windows' (ARG negates it).
+The case of CHAR is ignored."
+ (interactive (list (read-char "char: " t)
+ current-prefix-arg))
+ (avy-with avy-goto-subword-1
+ (let ((char (downcase char)))
+ (avy-goto-subword-0
+ arg (lambda ()
+ (and (char-after)
+ (eq (downcase (char-after)) char)))))))
+
+;;;###autoload
+(defun avy-goto-word-or-subword-1 ()
+ "Forward to `avy-goto-subword-1' or `avy-goto-word-1'.
+Which one depends on variable `subword-mode'."
+ (interactive)
+ (if (bound-and-true-p subword-mode)
+ (call-interactively #'avy-goto-subword-1)
+ (call-interactively #'avy-goto-word-1)))
+
+(defvar visual-line-mode)
+
+(defun avy--line-cands (&optional arg beg end)
+ "Get candidates for selecting a line.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+BEG and END narrow the scope where candidates are searched."
+ (let (candidates)
+ (avy-dowindows arg
+ (let ((ws (or beg (window-start))))
+ (save-excursion
+ (save-restriction
+ (narrow-to-region ws (or end (window-end (selected-window) t)))
+ (goto-char (point-min))
+ (while (< (point) (point-max))
+ (unless (get-char-property
+ (max (1- (point)) ws) 'invisible)
+ (push (cons
+ (if (eq avy-style 'post)
+ (line-end-position)
+ (point))
+ (selected-window)) candidates))
+ (if visual-line-mode
+ (progn
+ (setq temporary-goal-column 0)
+ (line-move-visual 1 t))
+ (forward-line 1)))))))
+ (nreverse candidates)))
+
+(defun avy--linum-strings ()
+ "Get strings for `avy-linum-mode'."
+ (let* ((lines (mapcar #'car (avy--line-cands)))
+ (line-tree (avy-tree lines avy-keys))
+ (line-list nil))
+ (avy-traverse
+ line-tree
+ (lambda (path _leaf)
+ (let ((str (propertize (apply #'string (reverse path))
+ 'face 'avy-lead-face)))
+ (when (> (length str) 1)
+ (set-text-properties 0 1 '(face avy-lead-face-0) str))
+ (push str line-list))))
+ (nreverse line-list)))
+
+(defvar linum-available)
+(defvar linum-overlays)
+(defvar linum-format)
+(declare-function linum--face-width "linum")
+
+(define-minor-mode avy-linum-mode
+ "Minor mode that uses avy hints for `linum-mode'."
+ :group 'avy
+ (if avy-linum-mode
+ (progn
+ (require 'linum)
+ (advice-add 'linum-update-window :around 'avy--linum-update-window)
+ (linum-mode 1))
+ (advice-remove 'linum-update-window 'avy--linum-update-window)
+ (linum-mode -1)))
+
+(defun avy--linum-update-window (_ win)
+ "Update line numbers for the portion visible in window WIN."
+ (goto-char (window-start win))
+ (let ((line (line-number-at-pos))
+ (limit (window-end win t))
+ (fmt (cond ((stringp linum-format) linum-format)
+ ((eq linum-format 'dynamic)
+ (let ((w (length (number-to-string
+ (count-lines (point-min) (point-max))))))
+ (concat "%" (number-to-string w) "d")))))
+ (width 0)
+ (avy-strs (when avy-linum-mode
+ (avy--linum-strings))))
+ (run-hooks 'linum-before-numbering-hook)
+ ;; Create an overlay (or reuse an existing one) for each
+ ;; line visible in this window, if necessary.
+ (while (and (not (eobp)) (< (point) limit))
+ (let* ((str
+ (cond (avy-linum-mode
+ (pop avy-strs))
+ (fmt
+ (propertize (format fmt line) 'face 'linum))
+ (t
+ (funcall linum-format line))))
+ (visited (catch 'visited
+ (dolist (o (overlays-in (point) (point)))
+ (when (equal-including-properties
+ (overlay-get o 'linum-str) str)
+ (unless (memq o linum-overlays)
+ (push o linum-overlays))
+ (setq linum-available (delq o linum-available))
+ (throw 'visited t))))))
+ (setq width (max width (length str)))
+ (unless visited
+ (let ((ov (if (null linum-available)
+ (make-overlay (point) (point))
+ (move-overlay (pop linum-available) (point) (point)))))
+ (push ov linum-overlays)
+ (overlay-put ov 'before-string
+ (propertize " " 'display `((margin left-margin) ,str)))
+ (overlay-put ov 'linum-str str))))
+ ;; Text may contain those nasty intangible properties, but that
+ ;; shouldn't prevent us from counting those lines.
+ (let ((inhibit-point-motion-hooks t))
+ (forward-line))
+ (setq line (1+ line)))
+ (when (display-graphic-p)
+ (setq width (ceiling
+ (/ (* width 1.0 (linum--face-width 'linum))
+ (frame-char-width)))))
+ (set-window-margins win width (cdr (window-margins win)))))
+
+(defun avy--line (&optional arg beg end)
+ "Select a line.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'.
+BEG and END narrow the scope where candidates are searched."
+ (let ((avy-action #'identity))
+ (avy--process
+ (avy--line-cands arg beg end)
+ (if avy-linum-mode
+ (progn (message "Goto line:")
+ 'ignore)
+ (avy--style-fn avy-style)))))
+
+;;;###autoload
+(defun avy-goto-line (&optional arg)
+ "Jump to a line start in current buffer.
+
+When ARG is 1, jump to lines currently visible, with the option
+to cancel to `goto-line' by entering a number.
+
+When ARG is 4, negate the window scope determined by
+`avy-all-windows'.
+
+Otherwise, forward to `goto-line' with ARG."
+ (interactive "p")
+ (setq arg (or arg 1))
+ (if (not (memq arg '(1 4)))
+ (progn
+ (goto-char (point-min))
+ (forward-line (1- arg)))
+ (avy-with avy-goto-line
+ (let* ((avy-handler-old avy-handler-function)
+ (avy-handler-function
+ (lambda (char)
+ (if (or (< char ?0)
+ (> char ?9))
+ (funcall avy-handler-old char)
+ (let ((line (read-from-minibuffer
+ "Goto line: " (string char))))
+ (when line
+ (avy-push-mark)
+ (save-restriction
+ (widen)
+ (goto-char (point-min))
+ (forward-line (1- (string-to-number line))))
+ (throw 'done 'exit))))))
+ (r (avy--line (eq arg 4))))
+ (unless (eq r t)
+ (avy-action-goto r))))))
+
+;;;###autoload
+(defun avy-goto-line-above ()
+ "Goto visible line above the cursor."
+ (interactive)
+ (let* ((avy-all-windows nil)
+ (r (avy--line nil (window-start)
+ (line-beginning-position))))
+ (unless (eq r t)
+ (avy-action-goto r))))
+
+;;;###autoload
+(defun avy-goto-line-below ()
+ "Goto visible line below the cursor."
+ (interactive)
+ (let* ((avy-all-windows nil)
+ (r (avy--line
+ nil (line-beginning-position 2)
+ (window-end (selected-window) t))))
+ (unless (eq r t)
+ (avy-action-goto r))))
+
+(defcustom avy-line-insert-style 'above
+ "How to insert the newly copied/cut line."
+ :type '(choice
+ (const :tag "Above" above)
+ (const :tag "Below" below)))
+
+;;;###autoload
+(defun avy-copy-line (arg)
+ "Copy a selected line above the current line.
+ARG lines can be used."
+ (interactive "p")
+ (let ((initial-window (selected-window)))
+ (avy-with avy-copy-line
+ (let* ((start (avy--line))
+ (str (buffer-substring-no-properties
+ start
+ (save-excursion
+ (goto-char start)
+ (move-end-of-line arg)
+ (point)))))
+ (select-window initial-window)
+ (cond ((eq avy-line-insert-style 'above)
+ (beginning-of-line)
+ (save-excursion
+ (insert str "\n")))
+ ((eq avy-line-insert-style 'below)
+ (end-of-line)
+ (insert "\n" str)
+ (beginning-of-line))
+ (t
+ (user-error "Unexpected `avy-line-insert-style'")))))))
+
+;;;###autoload
+(defun avy-move-line (arg)
+ "Move a selected line above the current line.
+ARG lines can be used."
+ (interactive "p")
+ (let ((initial-window (selected-window)))
+ (avy-with avy-move-line
+ (let ((start (avy--line)))
+ (save-excursion
+ (goto-char start)
+ (kill-whole-line arg))
+ (select-window initial-window)
+ (cond ((eq avy-line-insert-style 'above)
+ (beginning-of-line)
+ (save-excursion
+ (insert
+ (current-kill 0))))
+ ((eq avy-line-insert-style 'below)
+ (end-of-line)
+ (newline)
+ (save-excursion
+ (insert (substring (current-kill 0) 0 -1))))
+ (t
+ (user-error "Unexpected `avy-line-insert-style'")))))))
+
+;;;###autoload
+(defun avy-copy-region (arg)
+ "Select two lines and copy the text between them to point.
+
+The window scope is determined by `avy-all-windows' or
+`avy-all-windows-alt' when ARG is non-nil."
+ (interactive "P")
+ (let ((initial-window (selected-window)))
+ (avy-with avy-copy-region
+ (let* ((beg (save-selected-window
+ (avy--line arg)))
+ (end (avy--line arg))
+ (str (buffer-substring-no-properties
+ beg
+ (save-excursion
+ (goto-char end)
+ (line-end-position)))))
+ (select-window initial-window)
+ (cond ((eq avy-line-insert-style 'above)
+ (beginning-of-line)
+ (save-excursion
+ (insert str "\n")))
+ ((eq avy-line-insert-style 'below)
+ (end-of-line)
+ (newline)
+ (save-excursion
+ (insert str)))
+ (t
+ (user-error "Unexpected `avy-line-insert-style'")))))))
+
+;;;###autoload
+(defun avy-move-region ()
+ "Select two lines and move the text between them above the current line."
+ (interactive)
+ (avy-with avy-move-region
+ (let* ((initial-window (selected-window))
+ (beg (avy--line))
+ (end (avy--line))
+ text)
+ (when (> beg end)
+ (cl-rotatef beg end))
+ (setq end (save-excursion
+ (goto-char end)
+ (1+ (line-end-position))))
+ (setq text (buffer-substring beg end))
+ (move-beginning-of-line nil)
+ (delete-region beg end)
+ (select-window initial-window)
+ (insert text))))
+
+;;;###autoload
+(defun avy-kill-region (arg)
+ "Select two lines and kill the region between them.
+
+The window scope is determined by `avy-all-windows' or
+`avy-all-windows-alt' when ARG is non-nil."
+ (interactive "P")
+ (let ((initial-window (selected-window)))
+ (avy-with avy-kill-region
+ (let* ((beg (save-selected-window
+ (list (avy--line arg) (selected-window))))
+ (end (list (avy--line arg) (selected-window))))
+ (cond
+ ((not (numberp (car beg)))
+ (user-error "Fail to select the beginning of region"))
+ ((not (numberp (car end)))
+ (user-error "Fail to select the end of region"))
+ ;; Restrict operation to same window. It's better if it can be
+ ;; different windows but same buffer; however, then the cloned
+ ;; buffers with different narrowed regions might cause problem.
+ ((not (equal (cdr beg) (cdr end)))
+ (user-error "Selected points are not in the same window"))
+ ((< (car beg) (car end))
+ (save-excursion
+ (kill-region
+ (car beg)
+ (progn (goto-char (car end)) (forward-visible-line 1) (point)))))
+ (t
+ (save-excursion
+ (kill-region
+ (progn (goto-char (car beg)) (forward-visible-line 1) (point))
+ (car end)))))))
+ (select-window initial-window)))
+
+;;;###autoload
+(defun avy-kill-ring-save-region (arg)
+ "Select two lines and save the region between them to the kill ring.
+The window scope is determined by `avy-all-windows'.
+When ARG is non-nil, do the opposite of `avy-all-windows'."
+ (interactive "P")
+ (let ((initial-window (selected-window)))
+ (avy-with avy-kill-ring-save-region
+ (let* ((beg (save-selected-window
+ (list (avy--line arg) (selected-window))))
+ (end (list (avy--line arg) (selected-window))))
+ (cond
+ ((not (numberp (car beg)))
+ (user-error "Fail to select the beginning of region"))
+ ((not (numberp (car end)))
+ (user-error "Fail to select the end of region"))
+ ((not (equal (cdr beg) (cdr end)))
+ (user-error "Selected points are not in the same window"))
+ ((< (car beg) (car end))
+ (save-excursion
+ (kill-ring-save
+ (car beg)
+ (progn (goto-char (car end)) (forward-visible-line 1) (point)))))
+ (t
+ (save-excursion
+ (kill-ring-save
+ (progn (goto-char (car beg)) (forward-visible-line 1) (point))
+ (car end)))))))
+ (select-window initial-window)))
+
+;;;###autoload
+(defun avy-kill-whole-line (arg)
+ "Select line and kill the whole selected line.
+
+With a numerical prefix ARG, kill ARG line(s) starting from the
+selected line. If ARG is negative, kill backward.
+
+If ARG is zero, kill the selected line but exclude the trailing
+newline.
+
+\\[universal-argument] 3 \\[avy-kil-whole-line] kill three lines
+starting from the selected line. \\[universal-argument] -3
+
+\\[avy-kill-whole-line] kill three lines backward including the
+selected line."
+ (interactive "P")
+ (let ((initial-window (selected-window)))
+ (avy-with avy-kill-whole-line
+ (let* ((start (avy--line)))
+ (if (not (numberp start))
+ (user-error "Fail to select the line to kill")
+ (save-excursion (goto-char start)
+ (kill-whole-line arg)))))
+ (select-window initial-window)))
+
+;;;###autoload
+(defun avy-kill-ring-save-whole-line (arg)
+ "Select line and save the whole selected line as if killed, but don’t kill it.
+
+This command is similar to `avy-kill-whole-line', except that it
+saves the line(s) as if killed, but does not kill it(them).
+
+With a numerical prefix ARG, kill ARG line(s) starting from the
+selected line. If ARG is negative, kill backward.
+
+If ARG is zero, kill the selected line but exclude the trailing
+newline."
+ (interactive "P")
+ (let ((initial-window (selected-window)))
+ (avy-with avy-kill-ring-save-whole-line
+ (let* ((start (avy--line)))
+ (if (not (numberp start))
+ (user-error "Fail to select the line to kill")
+ (save-excursion
+ (let ((kill-read-only-ok t)
+ (buffer-read-only t))
+ (goto-char start)
+ (kill-whole-line arg))))))
+ (select-window initial-window)))
+
+;;;###autoload
+(defun avy-setup-default ()
+ "Setup the default shortcuts."
+ (eval-after-load "isearch"
+ '(define-key isearch-mode-map (kbd "C-'") 'avy-isearch)))
+
+(defcustom avy-timeout-seconds 0.5
+ "How many seconds to wait for the second char."
+ :type 'float)
+
+(defcustom avy-enter-times-out t
+ "Whether enter exits avy-goto-char-timer early. If nil it matches newline"
+ :type 'boolean)
+
+(defun avy--read-candidates (&optional re-builder)
+ "Read as many chars as possible and return their occurrences.
+At least one char must be read, and then repeatedly one next char
+may be read if it is entered before `avy-timeout-seconds'. `C-h'
+or `DEL' deletes the last char entered, and `RET' exits with the
+currently read string immediately instead of waiting for another
+char for `avy-timeout-seconds'.
+The format of the result is the same as that of `avy--regex-candidates'.
+This function obeys `avy-all-windows' setting.
+RE-BUILDER is a function that takes a string and returns a regex.
+When nil, `regexp-quote' is used.
+If a group is captured, the first group is highlighted.
+Otherwise, the whole regex is highlighted."
+ (let ((str "")
+ (re-builder (or re-builder #'regexp-quote))
+ char break overlays regex)
+ (unwind-protect
+ (progn
+ (while (and (not break)
+ (setq char
+ (read-char (format "char%s: "
+ (if (string= str "")
+ str
+ (format " (%s)" str)))
+ t
+ (and (not (string= str ""))
+ avy-timeout-seconds))))
+ ;; Unhighlight
+ (dolist (ov overlays)
+ (delete-overlay ov))
+ (setq overlays nil)
+ (cond
+ ;; Handle RET
+ ((= char 13)
+ (if avy-enter-times-out
+ (setq break t)
+ (setq str (concat str (list ?\n)))))
+ ;; Handle C-h, DEL
+ ((memq char '(8 127))
+ (let ((l (length str)))
+ (when (>= l 1)
+ (setq str (substring str 0 (1- l))))))
+ (t
+ (setq str (concat str (list char)))))
+ ;; Highlight
+ (when (>= (length str) 1)
+ (let ((case-fold-search
+ (or avy-case-fold-search (string= str (downcase str))))
+ found)
+ (avy-dowindows current-prefix-arg
+ (dolist (pair (avy--find-visible-regions
+ (window-start)
+ (window-end (selected-window) t)))
+ (save-excursion
+ (goto-char (car pair))
+ (setq regex (funcall re-builder str))
+ (while (re-search-forward regex (cdr pair) t)
+ (unless (get-char-property (1- (point)) 'invisible)
+ (let* ((idx (if (= (length (match-data)) 4) 1 0))
+ (ov (make-overlay
+ (match-beginning idx) (match-end idx))))
+ (setq found t)
+ (push ov overlays)
+ (overlay-put
+ ov 'window (selected-window))
+ (overlay-put
+ ov 'face 'avy-goto-char-timer-face)))))))
+ ;; No matches at all, so there's surely a typo in the input.
+ (unless found (beep)))))
+ (nreverse (mapcar (lambda (ov)
+ (cons (cons (overlay-start ov)
+ (overlay-end ov))
+ (overlay-get ov 'window)))
+ overlays)))
+ (dolist (ov overlays)
+ (delete-overlay ov)))))
+
+;;;###autoload
+(defun avy-goto-char-timer (&optional arg)
+ "Read one or many consecutive chars and jump to the first one.
+The window scope is determined by `avy-all-windows' (ARG negates it)."
+ (interactive "P")
+ (let ((avy-all-windows (if arg
+ (not avy-all-windows)
+ avy-all-windows)))
+ (avy-with avy-goto-char-timer
+ (avy--process
+ (avy--read-candidates)
+ (avy--style-fn avy-style)))))
+
+(defun avy-push-mark ()
+ "Store the current point and window."
+ (ring-insert avy-ring
+ (cons (point) (selected-window)))
+ (unless (region-active-p)
+ (push-mark)))
+
+(defun avy-pop-mark ()
+ "Jump back to the last location of `avy-push-mark'."
+ (interactive)
+ (let (res)
+ (condition-case nil
+ (progn
+ (while (not (window-live-p
+ (cdr (setq res (ring-remove avy-ring 0))))))
+ (let* ((window (cdr res))
+ (frame (window-frame window)))
+ (when (and (frame-live-p frame)
+ (not (eq frame (selected-frame))))
+ (select-frame-set-input-focus frame))
+ (select-window window)
+ (goto-char (car res))))
+ (error
+ (set-mark-command 4)))))
+
+;; ** Org-mode
+(defvar org-reverse-note-order)
+(declare-function org-refile "org")
+(declare-function org-back-to-heading "org")
+
+(defun avy-org-refile-as-child ()
+ "Refile current heading as first child of heading selected with `avy.'"
+ ;; Inspired by `org-teleport': http://kitchingroup.cheme.cmu.edu/blog/2016/03/18/Org-teleport-headlines/
+ (interactive)
+ (let ((rfloc (save-excursion
+ (let* ((org-reverse-note-order t)
+ (pos (avy-with avy-goto-line
+ (avy--generic-jump (rx bol (1+ "*") (1+ space))
+ nil avy-style)
+ (point)))
+ (filename (buffer-file-name (or (buffer-base-buffer (current-buffer))
+ (current-buffer)))))
+ (list nil filename nil pos)))))
+ ;; org-refile must be called outside of the excursion
+ (org-refile nil nil rfloc)))
+
+(defun avy-org-goto-heading-timer (&optional arg)
+ "Read one or many characters and jump to matching Org headings.
+The window scope is determined by `avy-all-windows' (ARG negates it)."
+ (interactive "P")
+ (let ((avy-all-windows (if arg
+ (not avy-all-windows)
+ avy-all-windows)))
+ (avy-with avy-goto-char-timer
+ (avy--process
+ (avy--read-candidates
+ (lambda (input)
+ (format "^\\*+ .*\\(%s\\)" input)))
+ (avy--style-fn avy-style))
+ (org-back-to-heading))))
+
+(provide 'avy)
+
+;;; avy.el ends here
--- /dev/null
+;;; helm-swoop-autoloads.el --- automatically extracted autoloads
+;;
+;;; Code:
+(add-to-list 'load-path (directory-file-name (or (file-name-directory #$) (car load-path))))
+\f
+;;;### (autoloads nil "helm-swoop" "helm-swoop.el" (23005 64951 0
+;;;;;; 0))
+;;; Generated autoloads from helm-swoop.el
+
+(autoload 'helm-swoop-back-to-last-point "helm-swoop" "\
+Go back to last position where `helm-swoop' was called
+
+\(fn &optional $CANCEL)" t nil)
+
+(autoload 'helm-swoop "helm-swoop" "\
+List the all lines to another buffer, which is able to squeeze by
+ any words you input. At the same time, the original buffer's cursor
+ is jumping line to line according to moving up and down the list.
+
+\(fn &key $QUERY $SOURCE ($MULTILINE current-prefix-arg))" t nil)
+
+(autoload 'helm-swoop-from-isearch "helm-swoop" "\
+Invoke `helm-swoop' from isearch.
+
+\(fn)" t nil)
+
+(autoload 'helm-multi-swoop "helm-swoop" "\
+Usage:
+M-x helm-multi-swoop
+1. Select any buffers by [C-SPC] or [M-SPC]
+2. Press [RET] to start helm-multi-swoop
+
+C-u M-x helm-multi-swoop
+If you have done helm-multi-swoop before, you can skip select buffers step.
+Last selected buffers will be applied to helm-multi-swoop.
+
+\(fn &optional $QUERY $BUFLIST)" t nil)
+
+(autoload 'helm-multi-swoop-all "helm-swoop" "\
+Apply all buffers to helm-multi-swoop
+
+\(fn &optional $QUERY)" t nil)
+
+(autoload 'helm-multi-swoop-org "helm-swoop" "\
+Applies all org-mode buffers to helm-multi-swoop
+
+\(fn &optional $QUERY)" t nil)
+
+(autoload 'helm-multi-swoop-current-mode "helm-swoop" "\
+Applies all buffers of the same mode as the current buffer to helm-multi-swoop
+
+\(fn &optional $QUERY)" t nil)
+
+(autoload 'helm-multi-swoop-projectile "helm-swoop" "\
+Apply all opened buffers of the current project to helm-multi-swoop
+
+\(fn &optional $QUERY)" t nil)
+
+(autoload 'helm-swoop-without-pre-input "helm-swoop" "\
+Start helm-swoop without pre input query.
+
+\(fn)" t nil)
+
+(autoload 'helm-swoop-symble-pre-input "helm-swoop" "\
+Start helm-swoop without pre input query.
+
+\(fn)" t nil)
+
+(autoload 'helm-multi-swoop-edit "helm-swoop" "\
+
+
+\(fn)" t nil)
+
+;;;***
+\f
+;; Local Variables:
+;; version-control: never
+;; no-byte-compile: t
+;; no-update-autoloads: t
+;; End:
+;;; helm-swoop-autoloads.el ends here
--- /dev/null
+;;; -*- no-byte-compile: t -*-
+(define-package "helm-swoop" "20170923.2127" "Efficiently hopping squeezed lines powered by helm interface" '((helm "1.0") (emacs "24.3")) :commit "66a951700e0ef0621cc705791bdd6699488ab121" :url "https://github.com/ShingoFukuyama/helm-swoop" :keywords '("helm" "swoop" "inner" "buffer" "search"))
--- /dev/null
+;;; helm-swoop.el --- Efficiently hopping squeezed lines powered by helm interface -*- coding: utf-8; lexical-binding: t -*-
+
+;; Copyright (C) 2013 - 2017 by Shingo Fukuyama
+
+;; Version: 1.7.3
+;; Package-Version: 20170923.2127
+;; Author: Shingo Fukuyama - http://fukuyama.co
+;; URL: https://github.com/ShingoFukuyama/helm-swoop
+;; Created: Oct 24 2013
+;; Keywords: helm swoop inner buffer search
+;; Package-Requires: ((helm "1.0") (emacs "24.3"))
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2 of
+;; the License, or (at your option) any later version.
+
+;; This program is distributed in the hope that it will be
+;; useful, but WITHOUT ANY WARRANTY; without even the implied
+;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+;; PURPOSE. See the GNU General Public License for more details.
+
+;;; Commentary:
+
+;; List the all lines to another buffer, which is able to squeeze
+;; by any words you input. At the same time, the original buffer's
+;; cursor is jumping line to line according to moving up and down
+;; the list.
+
+;; Example config
+;; ----------------------------------------------------------------
+;; ;; helm from https://github.com/emacs-helm/helm
+;; (require 'helm)
+
+;; ;; Locate the helm-swoop folder to your path
+;; ;; This line is unnecessary if you get this program from MELPA
+;; (add-to-list 'load-path "~/.emacs.d/elisp/helm-swoop")
+
+;; (require 'helm-swoop)
+
+;; ;; Change keybinds to whatever you like :)
+;; (global-set-key (kbd "M-i") 'helm-swoop)
+;; (global-set-key (kbd "M-I") 'helm-swoop-back-to-last-point)
+;; (global-set-key (kbd "C-c M-i") 'helm-multi-swoop)
+;; (global-set-key (kbd "C-x M-i") 'helm-multi-swoop-all)
+
+;; ;; When doing isearch, hand the word over to helm-swoop
+;; (define-key isearch-mode-map (kbd "M-i") 'helm-swoop-from-isearch)
+;; (define-key helm-swoop-map (kbd "M-i") 'helm-multi-swoop-all-from-helm-swoop)
+
+;; ;; Save buffer when helm-multi-swoop-edit complete
+;; (setq helm-multi-swoop-edit-save t)
+
+;; ;; If this value is t, split window inside the current window
+;; (setq helm-swoop-split-with-multiple-windows nil)
+
+;; ;; Split direction. 'split-window-vertically or 'split-window-horizontally
+;; (setq helm-swoop-split-direction 'split-window-vertically)
+
+;; ;; If nil, you can slightly boost invoke speed in exchange for text color
+;; (setq helm-swoop-speed-or-color nil)
+
+;; ;; Go to the opposite side of line from the end or beginning of line
+;; (setq helm-swoop-move-to-line-cycle t)
+
+;; ;; Optional face for line numbers
+;; ;; Face name is `helm-swoop-line-number-face`
+;; (setq helm-swoop-use-line-number-face t)
+
+;; ----------------------------------------------------------------
+
+;; * `M-x helm-swoop` when region active
+;; * `M-x helm-swoop` when the cursor is at any symbol
+;; * `M-x helm-swoop` when the cursor is not at any symbol
+;; * `M-3 M-x helm-swoop` or `C-u 5 M-x helm-swoop` multi separated line culling
+;; * `M-x helm-multi-swoop` multi-occur like feature
+;; * `M-x helm-multi-swoop-all` apply all buffers
+;; * `C-u M-x helm-multi-swoop` apply last selected buffers from the second time
+;; * `M-x helm-swoop-same-face-at-point` list lines have the same face at the cursor is on
+;; * During isearch `M-i` to hand the word over to helm-swoop
+;; * During helm-swoop `M-i` to hand the word over to helm-multi-swoop-all
+;; * While doing `helm-swoop` press `C-c C-e` to edit mode, apply changes to original buffer by `C-x C-s`
+
+;; Helm Swoop Edit
+;; While doing helm-swoop, press keybind [C-c C-e] to move to edit buffer.
+;; Edit the list and apply by [C-x C-s]. If you'd like to cancel, [C-c C-g]
+
+;;; Code:
+
+(require 'cl-lib)
+(require 'helm)
+(require 'helm-utils)
+(require 'helm-grep)
+
+(declare-function migemo-search-pattern-get "migemo")
+(declare-function projectile-buffers-with-file-or-process "projectile")
+(declare-function projectile-project-buffers "projectile")
+(defvar projectile-buffers-filter-function)
+
+;;; @ helm-swoop ----------------------------------------------
+
+(defgroup helm-swoop nil
+ "Open helm-swoop."
+ :prefix "helm-swoop-" :group 'helm)
+
+(defface helm-swoop-target-line-face
+ '((t (:background "#e3e300" :foreground "#222222")))
+ "Face for helm-swoop target line"
+ :group 'helm-swoop)
+(defface helm-swoop-target-line-block-face
+ '((t (:background "#cccc00" :foreground "#222222")))
+ "Face for target line"
+ :group 'helm-swoop)
+(defface helm-swoop-target-word-face
+ '((t (:background "#7700ff" :foreground "#ffffff")))
+ "Face for target word"
+ :group 'helm-swoop)
+(defface helm-swoop-line-number-face
+ '((t (:foreground "#999999")))
+ "Face for line numbers"
+ :group 'helm-swoop)
+
+(defcustom helm-swoop-speed-or-color nil
+ "If nil, you can slightly boost invoke speed in exchange for text color"
+ :group 'helm-swoop :type 'boolean)
+(defcustom helm-swoop-use-line-number-face nil
+ "Use face to line numbers on helm-swoop buffer"
+ :group 'helm-swoop :type 'boolean)
+(defcustom helm-swoop-split-with-multiple-windows nil
+ "Split window when having multiple windows open"
+ :group 'helm-swoop :type 'boolean)
+(defcustom helm-swoop-move-to-line-cycle t
+ "Return to the opposite side of line"
+ :group 'helm-swoop :type 'boolean)
+(defcustom helm-swoop-split-direction 'split-window-vertically
+ "Split direction"
+ :type '(choice (const :tag "vertically" split-window-vertically)
+ (const :tag "horizontally" split-window-horizontally))
+ :group 'helm-swoop)
+(defcustom helm-swoop-use-fuzzy-match nil
+ "If t, use fuzzy matching functions as well as exact matches."
+ :group 'helm-swoop :type 'boolean)
+
+(defvar helm-swoop-split-window-function
+ (lambda ($buf)
+ (if helm-swoop-split-with-multiple-windows
+ (funcall helm-swoop-split-direction)
+ (when (one-window-p)
+ (funcall helm-swoop-split-direction)))
+ (other-window 1)
+ (switch-to-buffer $buf))
+ "Change the way to split window only when `helm-swoop' is calling")
+
+(defcustom helm-swoop-after-goto-line-action-hook nil
+ "hooks run after `helm-swoop--goto-line"
+ :group 'helm-swoop
+ :type 'hook)
+(defcustom helm-swoop-before-goto-line-action-hook nil
+ "hooks run before `helm-swoop--goto-line"
+ :group 'helm-swoop
+ :type 'hook)
+
+(defvar helm-swoop-candidate-number-limit 19999)
+(defvar helm-swoop-buffer "*Helm Swoop*")
+(defvar helm-swoop-prompt "Swoop: ")
+(defvar helm-swoop-last-point nil)
+(defvar helm-swoop-invisible-targets nil)
+(defvar helm-swoop-last-line-info nil)
+
+;; Buffer local variables
+(defvar helm-swoop-list-cache)
+(defvar helm-swoop-pattern) ; Keep helm-pattern value
+(defvar helm-swoop-last-query) ; Last search query for resume
+(defvar-local helm-swoop-last-prefix-number 1) ; For multiline highlight
+
+;; Global variables
+(defvar helm-swoop-synchronizing-window nil
+ "Window object where `helm-swoop' called from")
+(defvar helm-swoop-target-buffer nil
+ "Buffer object where `helm-swoop' called from")
+(defvar helm-swoop-line-overlay nil
+ "Overlay object to indicate other window's line")
+
+(defvar helm-swoop-map
+ (let (($map (make-sparse-keymap)))
+ (set-keymap-parent $map helm-map)
+ (define-key $map (kbd "C-c C-e") 'helm-swoop-edit)
+ (define-key $map (kbd "M-i") 'helm-multi-swoop-all-from-helm-swoop)
+ (define-key $map (kbd "C-w") 'helm-swoop-yank-thing-at-point)
+ (define-key $map (kbd "^") 'helm-swoop-caret-match)
+ (delq nil $map))
+ "Keymap for helm-swoop")
+
+(defvar helm-multi-swoop-map
+ (let (($map (make-sparse-keymap)))
+ (set-keymap-parent $map helm-map)
+ (define-key $map (kbd "C-c C-e") 'helm-multi-swoop-edit)
+ (delq nil $map)))
+
+(defvar helm-c-source-swoop-match-functions
+ '(helm-mm-exact-match
+ helm-mm-match
+ helm-mm-3-migemo-match))
+
+(defvar helm-c-source-swoop-search-functions
+ '(helm-mm-exact-search
+ helm-mm-search
+ helm-candidates-in-buffer-search-default-fn
+ helm-mm-3-migemo-search))
+
+(defun helm-swoop-match-functions ()
+ (if helm-swoop-use-fuzzy-match
+ (append helm-c-source-swoop-match-functions '(helm-fuzzy-match))
+ helm-c-source-swoop-match-functions))
+
+(defun helm-swoop-search-functions ()
+ (if helm-swoop-use-fuzzy-match
+ (append helm-c-source-swoop-search-functions '(helm-fuzzy-search))
+ helm-c-source-swoop-search-functions))
+
+(defcustom helm-swoop-pre-input-function
+ (lambda () (thing-at-point 'symbol))
+ "This function can pre-input keywords when helm-swoop invoked"
+ :group 'helm-swoop :type 'function)
+
+(defun helm-swoop-pre-input-optimize ($query)
+ (when $query
+ (let (($regexp (list '("\+" . "\\\\+")
+ '("\*" . "\\\\*")
+ '("\#" . "\\\\#"))))
+ (mapc (lambda ($r)
+ (setq $query (replace-regexp-in-string (car $r) (cdr $r) $query)))
+ $regexp)
+ $query)))
+
+(defsubst helm-swoop--goto-line ($line)
+ (goto-char (point-min))
+ (forward-line (1- $line)))
+
+(defsubst helm-swoop--recenter ()
+ (recenter (/ (window-height) 2)))
+
+(defsubst helm-swoop--delete-overlay ($identity &optional $beg $end)
+ (or $beg (setq $beg (point-min)))
+ (or $end (setq $end (point-max)))
+ (overlay-recenter $end)
+ (mapc (lambda ($o)
+ (if (overlay-get $o $identity)
+ (delete-overlay $o)))
+ (overlays-in $beg $end)))
+
+(defsubst helm-swoop--get-string-at-line ()
+ (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
+
+(defsubst helm-swoop--buffer-substring ($point-min $point-max)
+ (if helm-swoop-speed-or-color
+ (let (($content (buffer-substring $point-min $point-max)))
+ (with-temp-buffer
+ (let ((inhibit-read-only t))
+ (insert $content)
+ (remove-text-properties (point-min) (point-max) '(read-only t))
+ (setq $content (buffer-substring (point-min) (point-max)))))
+ $content)
+ (buffer-substring-no-properties $point-min $point-max)))
+
+;;;###autoload
+(defun helm-swoop-back-to-last-point (&optional $cancel)
+ "Go back to last position where `helm-swoop' was called"
+ (interactive)
+ (if helm-swoop-last-point
+ (let (($po (point)))
+ (switch-to-buffer (cdr helm-swoop-last-point))
+ (goto-char (car helm-swoop-last-point))
+ (unless $cancel
+ (setq helm-swoop-last-point
+ (cons $po (buffer-name (current-buffer))))))))
+
+(defun helm-swoop--split-lines-by ($string $regexp $step)
+ "split-string by $step for multiline"
+ (or $step (setq $step 1))
+ (let (($from1 0) ;; last match point
+ ($from2 0) ;; last substring point
+ $list
+ ($i 1)) ;; from line 1
+ (while (string-match $regexp $string $from1)
+ (setq $i (1+ $i))
+ (if (eq 0 (% $i $step))
+ (progn
+ (setq $list (cons (substring $string $from2 (match-beginning 0))
+ $list))
+ (setq $from2 (match-end 0))
+ (setq $from1 (match-end 0)))
+ (setq $from1 (match-end 0))))
+ (setq $list (cons (substring $string $from2) $list))
+ (nreverse $list)))
+
+(defun helm-swoop--target-line-overlay-move (&optional $buf)
+ "Add color to the target line"
+ (move-overlay
+ helm-swoop-line-overlay
+ (progn
+ (search-backward
+ "\n" nil t (% (line-number-at-pos) helm-swoop-last-prefix-number))
+ (goto-char (point-at-bol)))
+ ;; For multiline highlight
+ (save-excursion
+ (goto-char (point-at-bol))
+ (or (re-search-forward "\n" nil t helm-swoop-last-prefix-number)
+ ;; For the end of buffer error
+ (point-max)))
+ $buf)
+ (helm-swoop--unveil-invisible-overlay))
+
+(defun helm-swoop--validate-regexp (regexp)
+ (condition-case nil
+ (progn
+ (string-match-p regexp "")
+ t)
+ (invalid-regexp nil)))
+
+(defun helm-swoop--target-word-overlay ($identity &optional $threshold)
+ (interactive)
+ (or $threshold (setq $threshold 2))
+ (save-excursion
+ (let (($pat (split-string helm-pattern " "))
+ $o)
+ (mapc (lambda ($wd)
+ (when (and (helm-swoop--validate-regexp $wd) (< $threshold (length $wd)))
+ (goto-char (point-min))
+ ;; Optional require migemo.el
+ (if (and (featurep 'migemo) helm-migemo-mode)
+ (setq $wd (migemo-search-pattern-get $wd)))
+ ;; For caret begging match
+ (if (string-match "^\\^\\[0\\-9\\]\\+\\.\\(.+\\)" $wd)
+ (setq $wd (concat "^" (match-string 1 $wd))))
+ (overlay-recenter (point-max))
+ (let (finish)
+ (while (and (not finish) (re-search-forward $wd nil t))
+ (if (= (match-beginning 0) (match-end 0))
+ (forward-char 1)
+ (setq $o (make-overlay (match-beginning 0) (match-end 0)))
+ (overlay-put $o 'face 'helm-swoop-target-word-face)
+ (overlay-put $o $identity t))
+ (when (eobp)
+ (setq finish t))))))
+ $pat))))
+
+(defun helm-swoop--restore-unveiled-overlay ()
+ (when helm-swoop-invisible-targets
+ (mapc (lambda ($ov) (overlay-put (car $ov) 'invisible (cdr $ov)))
+ helm-swoop-invisible-targets)
+ (setq helm-swoop-invisible-targets nil)))
+
+(defun helm-swoop--unveil-invisible-overlay ()
+ "Show hidden text temporarily to view it during helm-swoop.
+This function needs to call after latest helm-swoop-line-overlay set."
+ (helm-swoop--restore-unveiled-overlay)
+ (mapc (lambda ($ov)
+ (let (($type (overlay-get $ov 'invisible)))
+ (when $type
+ (overlay-put $ov 'invisible nil)
+ (setq helm-swoop-invisible-targets
+ (cons (cons $ov $type) helm-swoop-invisible-targets)))))
+ (overlays-in (overlay-start helm-swoop-line-overlay)
+ (overlay-end helm-swoop-line-overlay))))
+
+;; helm action ------------------------------------------------
+
+(defadvice helm-next-line (around helm-swoop-next-line disable)
+ (let ((helm-move-to-line-cycle-in-source t))
+ ad-do-it
+ (when (called-interactively-p 'any)
+ (helm-swoop--move-line-action))))
+
+(defadvice helm-previous-line (around helm-swoop-previous-line disable)
+ (let ((helm-move-to-line-cycle-in-source t))
+ ad-do-it
+ (when (called-interactively-p 'any)
+ (helm-swoop--move-line-action))))
+
+(defadvice helm-toggle-visible-mark (around helm-swoop-toggle-visible-mark disable)
+ (let ((helm-move-to-line-cycle-in-source t))
+ ad-do-it
+ (when (called-interactively-p 'any)
+ (helm-swoop--move-line-action))))
+
+(defun helm-swoop--move-line-action ()
+ (with-helm-window
+ (let* (($key (helm-swoop--get-string-at-line))
+ ($num (when (string-match "^[0-9]+" $key)
+ (string-to-number (match-string 0 $key)))))
+ ;; Synchronizing line position
+ (when (and $key $num)
+ (with-selected-window helm-swoop-synchronizing-window
+ (progn
+ (helm-swoop--goto-line $num)
+ (with-current-buffer helm-swoop-target-buffer
+ (delete-overlay helm-swoop-line-overlay)
+ (helm-swoop--target-line-overlay-move))
+ (helm-swoop--recenter)))
+ (setq helm-swoop-last-line-info
+ (cons helm-swoop-target-buffer $num))))))
+
+(defun helm-swoop--nearest-line ($target $list)
+ "Return the nearest number of $target out of $list."
+ (when (and $target $list)
+ (let ($result)
+ (cl-labels ((filter ($fn $elem $list)
+ (let ($r)
+ (mapc (lambda ($e)
+ (if (funcall $fn $elem $e)
+ (setq $r (cons $e $r))))
+ $list) $r)))
+ (if (eq 1 (length $list))
+ (setq $result (car $list))
+ (let* (($lts (filter '> $target $list))
+ ($gts (filter '< $target $list))
+ ($lt (if $lts (apply 'max $lts)))
+ ($gt (if $gts (apply 'min $gts)))
+ ($ltg (if $lt (- $target $lt)))
+ ($gtg (if $gt (- $gt $target))))
+ (setq $result
+ (cond ((memq $target $list) $target)
+ ((and (not $lt) (not $gt)) nil)
+ ((not $gtg) $lt)
+ ((not $ltg) $gt)
+ ((eq $ltg $gtg) $gt)
+ ((< $ltg $gtg) $lt)
+ ((> $ltg $gtg) $gt)
+ (t 1))))))
+ $result)))
+
+(defun helm-swoop--keep-nearest-position ()
+ (with-helm-window
+ (let (($p (point-min)) $list $bound
+ $nearest-line $target-point
+ ($buf (rx-to-string (buffer-name (car helm-swoop-last-line-info)) t)))
+ (save-excursion
+ (goto-char $p)
+ (while (if $p (setq $p (re-search-forward (concat "^" $buf "$") nil t)))
+ (when (get-text-property (point-at-bol) 'helm-header)
+ (forward-char 1)
+ (setq $bound (next-single-property-change (point) 'helm-header))
+ (while (re-search-forward "^[0-9]+" $bound t)
+ (setq $list (cons
+ (string-to-number (match-string 0))
+ $list)))
+ (setq $nearest-line (helm-swoop--nearest-line
+ (cdr helm-swoop-last-line-info)
+ $list))
+ (goto-char $p)
+ (re-search-forward (concat "^"
+ (number-to-string $nearest-line)
+ "\\s-") $bound t)
+ (setq $target-point (point))
+ (setq $p nil))))
+ (when $target-point
+ (goto-char $target-point)
+ (helm-mark-current-line)
+ (if (equal helm-swoop-buffer (buffer-name (current-buffer)))
+ (helm-swoop--move-line-action)
+ (helm-multi-swoop--move-line-action))))))
+
+(defun helm-swoop--pattern-match ()
+ "Overlay target words"
+ (with-helm-window
+ (setq helm-swoop-pattern helm-pattern)
+ (when (< 2 (length helm-pattern))
+ (helm-swoop--delete-overlay 'target-buffer)
+ (helm-swoop--target-word-overlay 'target-buffer)
+ (with-selected-window helm-swoop-synchronizing-window
+ (helm-swoop--delete-overlay 'target-buffer)
+ (helm-swoop--target-word-overlay 'target-buffer)))))
+
+(defun helm-swoop-flash-word ($match-beg $match-end)
+ (interactive)
+ (unwind-protect
+ (let (($o (make-overlay $match-beg $match-end)))
+ (when $o
+ (overlay-put $o 'face 'helm-swoop-target-word-face)
+ (overlay-put $o 'helm-swoop-overlay-word-frash t)))
+ (run-with-idle-timer
+ 0.6 nil (lambda () (helm-swoop--delete-overlay 'helm-swoop-overlay-word-frash)))))
+
+;; core ------------------------------------------------
+
+(defun helm-swoop--get-content ($buffer &optional $linum)
+ "Get the whole content in buffer and add line number at the head.
+If $linum is number, lines are separated by $linum"
+ (let (($buf (get-buffer $buffer)))
+ (when $buf
+ (with-current-buffer $buf
+ (let (($bufstr (helm-swoop--buffer-substring (point-min) (point-max)))
+ $return)
+ (with-temp-buffer
+ (insert $bufstr)
+ (goto-char (point-min))
+ (let (($i 1))
+ (insert (format "%s " $i))
+ (while (re-search-forward "\n" nil t)
+ (cl-incf $i)
+ (if helm-swoop-use-line-number-face
+ (insert (propertize (format "%s" $i) 'font-lock-face 'helm-swoop-line-number-face) " ")
+ (insert (format "%s " $i))))
+ ;; Delete empty lines
+ (unless $linum
+ (goto-char (point-min))
+ (while (re-search-forward "^[0-9]+\\s-*$" nil t)
+ (replace-match ""))))
+ (setq $return (helm-swoop--buffer-substring (point-min) (point-max))))
+ $return)))))
+
+(defun helm-swoop--goto-line-action ($line)
+ (run-hooks 'helm-swoop-before-goto-line-action-hook)
+ (helm-swoop--goto-line
+ (when (string-match "^[0-9]+" $line)
+ (string-to-number (match-string 0 $line))))
+ (let (($regex
+ (mapconcat 'identity
+ (split-string helm-pattern " ")
+ "\\|")))
+ (when (or (and (and (featurep 'migemo) helm-migemo-mode)
+ (migemo-forward $regex nil t))
+ (re-search-forward $regex nil t))
+ (helm-swoop-flash-word (match-beginning 0) (match-end 0))
+ (goto-char (match-beginning 0))
+ (run-hooks 'helm-swoop-after-goto-line-action-hook)))
+ (helm-swoop--recenter))
+
+(defun helm-c-source-swoop ()
+ `((name . ,(buffer-name helm-swoop-target-buffer))
+ (candidates . ,(if helm-swoop-list-cache
+ (progn
+ (helm-swoop--split-lines-by
+ helm-swoop-list-cache "\n" helm-swoop-last-prefix-number))
+ (helm-swoop--split-lines-by
+ (setq helm-swoop-list-cache
+ (helm-swoop--get-content helm-swoop-target-buffer t))
+ "\n" helm-swoop-last-prefix-number)))
+ (get-line . ,(if helm-swoop-speed-or-color
+ 'helm-swoop--buffer-substring
+ 'buffer-substring-no-properties))
+ (keymap . ,helm-swoop-map)
+ (header-line . ,(substitute-command-keys
+ "[\\<helm-swoop-map>\\[helm-swoop-edit]] Edit mode, \
+[\\<helm-swoop-map>\\[helm-multi-swoop-all-from-helm-swoop]] apply all buffers"))
+ (action . (("Go to Line" . helm-swoop--goto-line-action)))
+ ,(if (and helm-swoop-last-prefix-number
+ (> helm-swoop-last-prefix-number 1))
+ '(multiline))
+ (match . ,(helm-swoop-match-functions))
+ (search . ,(helm-swoop-search-functions))))
+
+(defun helm-c-source-multi-swoop ($buf $func $action $multiline)
+ `((name . ,$buf)
+ (candidates . ,(funcall $func))
+ (action . ,$action)
+ (header-line . ,(concat $buf
+ (substitute-command-keys
+ " [\\<helm-multi-swoop-map>\\[helm-multi-swoop-edit]] Edit mode")))
+ (keymap . ,helm-multi-swoop-map)
+ (requires-pattern . 2)
+ ,(if (and $multiline
+ (> $multiline 1))
+ '(multiline))
+ (match . ,(helm-swoop-match-functions))
+ (search . ,(helm-swoop-search-functions))))
+
+(defun helm-swoop--set-prefix ($multiline)
+ ;; Enable scrolling margin
+ (setq helm-swoop-last-prefix-number
+ (or $multiline 1))) ;; $multiline is for resume
+
+;; Delete cache when modified file is saved
+(defun helm-swoop--clear-cache ()
+ (if (boundp 'helm-swoop-list-cache) (setq helm-swoop-list-cache nil)))
+(add-hook 'after-save-hook 'helm-swoop--clear-cache)
+
+(defadvice narrow-to-region (around helm-swoop-advice-narrow-to-region activate)
+ (helm-swoop--clear-cache)
+ ad-do-it)
+(defadvice narrow-to-defun (around helm-swoop-advice-narrow-to-defun activate)
+ (helm-swoop--clear-cache)
+ ad-do-it)
+(defadvice narrow-to-page (around helm-swoop-advice-narrow-to-page activate)
+ (helm-swoop--clear-cache)
+ ad-do-it)
+(defadvice widen (around helm-swoop-advice-widen activate)
+ (helm-swoop--clear-cache)
+ ad-do-it)
+
+(defun helm-swoop--restore ()
+ (when (= 1 helm-exit-status)
+ (helm-swoop-back-to-last-point t)
+ (helm-swoop--restore-unveiled-overlay))
+ (setq helm-swoop-invisible-targets nil)
+ (ad-disable-advice 'helm-next-line 'around 'helm-swoop-next-line)
+ (ad-activate 'helm-next-line)
+ (ad-disable-advice 'helm-previous-line 'around 'helm-swoop-previous-line)
+ (ad-activate 'helm-previous-line)
+ (ad-disable-advice 'helm-toggle-visible-mark 'around 'helm-swoop-toggle-visible-mark)
+ (ad-activate 'helm-toggle-visible-mark)
+ (ad-disable-advice 'helm-move--next-line-fn 'around
+ 'helm-multi-swoop-next-line-cycle)
+ (ad-activate 'helm-move--next-line-fn)
+ (ad-disable-advice 'helm-move--previous-line-fn 'around
+ 'helm-multi-swoop-previous-line-cycle)
+ (ad-activate 'helm-move--previous-line-fn)
+ (remove-hook 'helm-update-hook 'helm-swoop--pattern-match)
+ (remove-hook 'helm-after-update-hook 'helm-swoop--keep-nearest-position)
+ (setq helm-swoop-last-query helm-swoop-pattern)
+ (mapc (lambda ($ov)
+ (when (or (eq 'helm-swoop-target-line-face (overlay-get $ov 'face))
+ (eq 'helm-swoop-target-line-block-face
+ (overlay-get $ov 'face)))
+ (delete-overlay $ov)))
+ (overlays-in (point-min) (point-max)))
+ (helm-swoop--delete-overlay 'target-buffer)
+ (deactivate-mark t))
+
+;;;###autoload
+(cl-defun helm-swoop (&key $query $source ($multiline current-prefix-arg))
+ "List the all lines to another buffer, which is able to squeeze by
+ any words you input. At the same time, the original buffer's cursor
+ is jumping line to line according to moving up and down the list."
+ (interactive)
+ (setq helm-swoop-synchronizing-window (selected-window))
+ (setq helm-swoop-last-point (cons (point) (buffer-name (current-buffer))))
+ (setq helm-swoop-last-line-info
+ (cons (current-buffer) (line-number-at-pos)))
+ (unless (boundp 'helm-swoop-last-query)
+ (set (make-local-variable 'helm-swoop-last-query) ""))
+ (setq helm-swoop-target-buffer (current-buffer))
+ (helm-swoop--set-prefix (prefix-numeric-value $multiline))
+ ;; Overlay
+ (setq helm-swoop-line-overlay (make-overlay (point) (point)))
+ (overlay-put helm-swoop-line-overlay
+ 'face (if (< 1 helm-swoop-last-prefix-number)
+ 'helm-swoop-target-line-block-face
+ 'helm-swoop-target-line-face))
+ ;; Cache
+ (cond ((not (boundp 'helm-swoop-list-cache))
+ (set (make-local-variable 'helm-swoop-list-cache) nil))
+ ((buffer-modified-p)
+ (setq helm-swoop-list-cache nil)))
+ (unwind-protect
+ (progn
+ ;; For synchronizing line position
+ (ad-enable-advice 'helm-next-line 'around 'helm-swoop-next-line)
+ (ad-activate 'helm-next-line)
+ (ad-enable-advice 'helm-previous-line 'around 'helm-swoop-previous-line)
+ (ad-activate 'helm-previous-line)
+ (ad-enable-advice 'helm-toggle-visible-mark 'around 'helm-swoop-toggle-visible-mark)
+ (ad-activate 'helm-toggle-visible-mark)
+ (ad-enable-advice 'helm-move--next-line-fn 'around
+ 'helm-multi-swoop-next-line-cycle)
+ (ad-activate 'helm-move--next-line-fn)
+ (ad-enable-advice 'helm-move--previous-line-fn 'around
+ 'helm-multi-swoop-previous-line-cycle)
+ (ad-activate 'helm-move--previous-line-fn)
+ (add-hook 'helm-update-hook 'helm-swoop--pattern-match)
+ (add-hook 'helm-after-update-hook 'helm-swoop--keep-nearest-position t)
+ (cond ($query
+ (if (string-match
+ "\\(\\^\\[0\\-9\\]\\+\\.\\)\\(.*\\)" $query)
+ $query ;; NEED FIX #1 to appear as a "^"
+ $query))
+ (mark-active
+ (let (($st (buffer-substring-no-properties
+ (region-beginning) (region-end))))
+ (if (string-match "\n" $st)
+ (message "Multi line region is not allowed")
+ (setq $query (helm-swoop-pre-input-optimize $st)))))
+ ((setq $query (helm-swoop-pre-input-optimize
+ (funcall helm-swoop-pre-input-function))))
+ (t (setq $query "")))
+ ;; First behavior
+ (helm-swoop--recenter)
+ (move-beginning-of-line 1)
+ (helm-swoop--target-line-overlay-move)
+ ;; Execute helm
+ (let ((helm-display-function helm-swoop-split-window-function)
+ (helm-display-source-at-screen-top nil)
+ (helm-completion-window-scroll-margin 5))
+ (helm :sources
+ (or $source
+ (helm-c-source-swoop))
+ :buffer helm-swoop-buffer
+ :input $query
+ :prompt helm-swoop-prompt
+ :preselect
+ ;; Get current line has content or else near one
+ (if (string-match "^[\t\n\s]*$" (helm-swoop--get-string-at-line))
+ (save-excursion
+ (if (re-search-forward "[^\t\n\s]" nil t)
+ (format "^%s\s" (line-number-at-pos))
+ (re-search-backward "[^\t\n\s]" nil t)
+ (format "^%s\s" (line-number-at-pos))))
+ (format "^%s\s" (line-number-at-pos)))
+ :candidate-number-limit helm-swoop-candidate-number-limit)))
+ ;; Restore helm's hook and window function etc
+ (helm-swoop--restore)))
+
+;; Receive word from isearch ---------------
+;;;###autoload
+(defun helm-swoop-from-isearch ()
+ "Invoke `helm-swoop' from isearch."
+ (interactive)
+ (let (($query (if isearch-regexp
+ isearch-string
+ (regexp-quote isearch-string))))
+ (let (search-nonincremental-instead)
+ (isearch-exit))
+ (helm-swoop :$query $query)))
+;; When doing isearch, hand the word over to helm-swoop
+(define-key isearch-mode-map (kbd "M-i") 'helm-swoop-from-isearch)
+
+;; Receive word from evil search ---------------
+(defun helm-swoop-from-evil-search ()
+ "Invoke `helm-swoop' from evil isearch"
+ (interactive)
+ (if (string-match "\\(isearch-\\|evil.*search\\)" (symbol-name real-last-command))
+ (helm-swoop :$query (if isearch-regexp
+ isearch-string
+ (regexp-quote isearch-string)))
+ (helm-swoop)))
+;; When doing evil-search, hand the word over to helm-swoop
+;; (define-key evil-motion-state-map (kbd "M-i") 'helm-swoop-from-evil-search)
+
+;; Receive word from evil search ---------------
+(defun helm-swoop-yank-thing-at-point ()
+ "Insert string at which the point helm-swoop started."
+ (interactive)
+ (let ($amend $buf)
+ (with-selected-window helm-swoop-synchronizing-window
+ (setq $buf (get-buffer (cdr helm-swoop-last-point)))
+ (when $buf
+ (with-current-buffer $buf
+ (save-excursion
+ (goto-char (car helm-swoop-last-point))
+ (setq $amend (thing-at-point 'symbol))))))
+ (when $amend
+ (with-selected-window (minibuffer-window)
+ (insert $amend)))))
+
+;; For helm-resume ------------------------
+(defadvice helm-resume-select-buffer
+ (around helm-swoop-if-selected-as-resume activate)
+ "Resume if *Helm Swoop* buffer selected as a resume
+ when helm-resume with prefix"
+ (if (boundp 'helm-swoop-last-query)
+ ad-do-it
+ ;; If the buffer hasn't called helm-swoop, just hide from options
+ (let ((helm-buffers (delete helm-swoop-buffer helm-buffers)))
+ ad-do-it))
+ (when (and (equal ad-return-value helm-swoop-buffer)
+ (boundp 'helm-swoop-last-query))
+ (helm-swoop :$query helm-swoop-last-query
+ :$multiline helm-swoop-last-prefix-number)
+ (setq ad-return-value nil)))
+
+(defadvice helm-resume (around helm-swoop-resume activate)
+ "Resume if the last used helm buffer is helm-swoop-buffer"
+ (if (equal helm-last-buffer helm-swoop-buffer)
+ (if (boundp 'helm-swoop-last-query)
+ (if (not (ad-get-arg 0))
+ (helm-swoop :$query helm-swoop-last-query
+ :$multiline helm-swoop-last-prefix-number))
+ ;; Temporary apply second last buffer
+ (let ((helm-last-buffer (cadr helm-buffers))) ad-do-it))
+ ad-do-it))
+
+;; For caret beginning-match -----------------------------
+(defun helm-swoop--caret-match-delete ($o $aft $beg $end &optional $len)
+ (if $aft
+ (- $end $beg $len) ;; Unused argument? To avoid byte compile error
+ (delete-region (overlay-start $o) (1- (overlay-end $o)))))
+
+(defun helm-swoop-caret-match (&optional _$resume)
+ (interactive)
+ (let* (($prompt helm-swoop-prompt) ;; Accept change of the variable
+ ($line-number-regexp "^[0-9]+.")
+ ($prompt-regexp
+ (funcall `(lambda ()
+ (rx bol ,$prompt))))
+ ($prompt-regexp-with-line-number
+ (funcall `(lambda ()
+ (rx bol ,$prompt (group ,$line-number-regexp)))))
+ ($disguise-caret
+ (lambda ()
+ (save-excursion
+ (re-search-backward $prompt-regexp-with-line-number nil t)
+ (let (($o (make-overlay (match-beginning 1) (match-end 1))))
+ (overlay-put $o 'face 'helm-swoop-target-word-face)
+ (overlay-put $o 'modification-hooks '(helm-swoop--caret-match-delete))
+ (overlay-put $o 'display "^")
+ (overlay-put $o 'evaporate t))))))
+ (if (and (minibufferp)
+ (string-match $prompt-regexp
+ (buffer-substring-no-properties
+ (point-min) (point-max)))
+ (eq (point) (+ 1 (length helm-swoop-prompt))))
+ (progn
+ (insert $line-number-regexp)
+ (funcall $disguise-caret))
+ (insert "^"))))
+
+;;; @ helm-swoop-edit -----------------------------------------
+
+(defvar helm-swoop-edit-target-buffer)
+(defvar helm-swoop-edit-buffer "*Helm Swoop Edit*")
+(defvar helm-swoop-edit-map
+ (let (($map (make-sparse-keymap)))
+ (define-key $map (kbd "C-x C-s") 'helm-swoop--edit-complete)
+ (define-key $map (kbd "C-c C-g") 'helm-swoop--edit-cancel)
+ (define-key $map (kbd "C-c C-q C-k") 'helm-swoop--edit-delete-all-lines)
+ $map))
+
+(defun helm-swoop--clear-edit-buffer ($prop)
+ (let ((inhibit-read-only t))
+ (mapc (lambda ($ov)
+ (when (overlay-get $ov $prop)
+ (delete-overlay $ov)))
+ (overlays-in (point-min) (point-max)))
+ (set-text-properties (point-min) (point-max) nil)
+ (goto-char (point-min))
+ (erase-buffer)))
+
+(defun helm-swoop--collect-edited-lines ()
+ "Create a list of edited lines with each its own line number"
+ (interactive)
+ (let ($list)
+ (goto-char (point-min))
+ (while (re-search-forward "^\\([0-9]+\\)\s" nil t)
+ (setq $list
+ (cons (cons (string-to-number (match-string 1))
+ (buffer-substring-no-properties
+ (point)
+ (save-excursion
+ (if (re-search-forward
+ "^\\([0-9]+\\)\s\\|^\\(\\-+\\)" nil t)
+ (1- (match-beginning 0))
+ (goto-char (point-max))
+ (re-search-backward "\n" nil t)))))
+ $list)))
+ $list))
+
+(defun helm-swoop--edit ($candidate)
+ "This function will only be called from `helm-swoop-edit'"
+ (interactive)
+ (setq helm-swoop-edit-target-buffer helm-swoop-target-buffer)
+ (delete-overlay helm-swoop-line-overlay)
+ (helm-swoop--delete-overlay 'target-buffer)
+ (with-current-buffer (get-buffer-create helm-swoop-edit-buffer)
+
+ (helm-swoop--clear-edit-buffer 'helm-swoop-edit)
+ (let (($bufstr ""))
+ ;; Get target line number to edit
+ (with-current-buffer helm-swoop-buffer
+ ;; Use selected line by [C-SPC] or [M-SPC]
+ (mapc (lambda ($ov)
+ (when (eq 'helm-visible-mark (overlay-get $ov 'face))
+ (setq $bufstr (concat (buffer-substring-no-properties
+ (overlay-start $ov) (overlay-end $ov))
+ $bufstr))))
+ (overlays-in (point-min) (point-max)))
+ (if (equal "" $bufstr)
+ ;; Not found selected line
+ (setq $bufstr (buffer-substring-no-properties
+ (point-min) (point-max)))
+ ;; Attach title
+ (setq $bufstr (concat "Helm Swoop\n" $bufstr))))
+
+ ;; Set for edit buffer
+ (insert $bufstr)
+ (add-text-properties (point-min) (point-max)
+ '(read-only t rear-nonsticky t front-sticky t))
+
+ ;; Set for editable context
+ (let ((inhibit-read-only t))
+ ;; Title and explanation
+ (goto-char (point-min))
+ (let (($o (make-overlay (point) (point-at-eol))))
+ (overlay-put $o 'helm-swoop-edit t)
+ (overlay-put $o 'face 'font-lock-function-name-face)
+ (overlay-put $o 'after-string
+ (propertize
+ (substitute-command-keys
+ (concat
+ " [\\<helm-swoop-edit-map>\\[helm-swoop--edit-complete]] Complete"
+ ", [\\<helm-swoop-edit-map>\\[helm-swoop--edit-cancel]] Cancel"
+ ", [\\<helm-swoop-edit-map>\\[helm-swoop--edit-delete-all-lines]] Delete All"
+ ))
+ 'face 'helm-bookmark-addressbook)))
+ ;; Line number and editable area
+ (while (re-search-forward "^\\([0-9]+\s\\)\\(.*\\)$" nil t)
+ (let* (($bol1 (match-beginning 1))
+ ($eol1 (match-end 1))
+ ($bol2 (match-beginning 2))
+ ($eol2 (match-end 2)))
+ ;; Line number
+ (add-text-properties $bol1 $eol1
+ '(face font-lock-function-name-face
+ intangible t))
+ ;; Editable area
+ (remove-text-properties $bol2 $eol2 '(read-only t))
+ ;; For line tail
+ (set-text-properties $eol2 (or (1+ $eol2) (point-max))
+ '(read-only t rear-nonsticky t))))
+ (helm-swoop--target-word-overlay 'edit-buffer 0))))
+
+ (other-window 1)
+ (switch-to-buffer helm-swoop-edit-buffer)
+ (goto-char (point-min))
+ (if (string-match "^[0-9]+" $candidate)
+ (re-search-forward
+ (concat "^" (match-string 0 $candidate)) nil t))
+ (use-local-map helm-swoop-edit-map))
+
+(defun helm-swoop--edit-complete ()
+ "Apply changes and kill temporary edit buffer"
+ (interactive)
+ (let (($list (helm-swoop--collect-edited-lines)))
+ (with-current-buffer helm-swoop-edit-target-buffer
+ ;; Replace from the end of buffer
+ (save-excursion
+ (mapc (lambda ($cell)
+ (let (($k (car $cell))
+ ($v (cdr $cell)))
+ (goto-char (point-min))
+ (delete-region (point-at-bol $k) (point-at-eol $k))
+ (goto-char (point-at-bol $k))
+ (insert $v)))
+ $list)))
+ (select-window helm-swoop-synchronizing-window)
+ (kill-buffer (get-buffer helm-swoop-edit-buffer)))
+ (message "Successfully helm-swoop-edit applied to original buffer"))
+
+(defun helm-swoop--edit-delete-all-lines ()
+ "Apply changes and kill temporary edit buffer"
+ (interactive)
+ (let (($list (helm-swoop--collect-edited-lines)))
+ (with-current-buffer helm-swoop-edit-target-buffer
+ ;; Replace from the end of buffer
+ (save-excursion
+ (mapc (lambda ($cell)
+ (let (($k (car $cell)))
+ (goto-char (point-min))
+ (delete-region (point-at-bol $k)
+ (if (eq (point-at-eol $k) (point-max))
+ (point-at-eol $k)
+ (1+ (point-at-eol $k))))))
+ $list)))
+ (select-window helm-swoop-synchronizing-window)
+ (kill-buffer (get-buffer helm-swoop-edit-buffer)))
+ (message "Successfully helm-swoop-edit applied to original buffer"))
+
+(defun helm-swoop--edit-cancel ()
+ "Cancel edit and kill temporary buffer"
+ (interactive)
+ (select-window helm-swoop-synchronizing-window)
+ (kill-buffer (get-buffer helm-swoop-edit-buffer))
+ (message "helm-swoop-edit canceled"))
+
+(defun helm-swoop-edit ()
+ (interactive)
+ (helm-exit-and-execute-action 'helm-swoop--edit))
+
+;;; @ helm-multi-swoop ----------------------------------------
+(defvar helm-multi-swoop-buffer-list "*helm-multi-swoop buffers list*"
+ "Buffer name")
+(defvar helm-multi-swoop-ignore-buffers-match "^\\*"
+ "Regexp to eliminate buffers you don't want to see")
+(defvar helm-multi-swoop-candidate-number-limit 250)
+(defvar helm-multi-swoop-last-selected-buffers nil)
+(defvar helm-multi-swoop-last-query nil)
+(defvar helm-multi-swoop-query nil)
+(defvar helm-multi-swoop-buffer "*Helm Multi Swoop*")
+(defvar helm-multi-swoop-all-from-helm-swoop-last-point nil
+ "For the last position, when helm-multi-swoop-all-from-helm-swoop canceled")
+(defvar helm-multi-swoop-move-line-action-last-buffer nil)
+
+(defvar helm-multi-swoop-buffers-map
+ (let (($map (make-sparse-keymap)))
+ (set-keymap-parent $map helm-map)
+ (define-key $map (kbd "RET")
+ (lambda () (interactive)
+ (helm-exit-and-execute-action 'helm-multi-swoop--exec)))
+ (delq nil $map)))
+
+(defvar helm-multi-swoop-projectile-buffers-filter
+ #'projectile-buffers-with-file-or-process)
+;; action -----------------------------------------------------
+
+(defadvice helm-next-line (around helm-multi-swoop-next-line disable)
+ (let ((helm-move-to-line-cycle-in-source nil))
+ ad-do-it
+ (when (called-interactively-p 'any)
+ (helm-multi-swoop--move-line-action))))
+
+(defadvice helm-previous-line (around helm-multi-swoop-previous-line disable)
+ (let ((helm-move-to-line-cycle-in-source nil))
+ ad-do-it
+ (when (called-interactively-p 'any)
+ (helm-multi-swoop--move-line-action))))
+
+(defadvice helm-toggle-visible-mark (around helm-multi-swoop-toggle-visible-mark disable)
+ (let ((helm-move-to-line-cycle-in-source nil))
+ ad-do-it
+ (when (called-interactively-p 'any)
+ (helm-multi-swoop--move-line-action))))
+
+(defadvice helm-move--next-line-fn (around helm-multi-swoop-next-line-cycle disable)
+ (if (not (helm-pos-multiline-p))
+ (if (eq (point-max) (save-excursion (forward-line 1) (point)))
+ (when helm-swoop-move-to-line-cycle
+ (helm-beginning-of-buffer)
+ (helm-swoop--recenter))
+ (forward-line 1))
+ (let ((line-num (line-number-at-pos)))
+ (helm-move--next-multi-line-fn)
+ (when (and helm-swoop-move-to-line-cycle
+ (eq line-num (line-number-at-pos)))
+ (helm-beginning-of-buffer)))))
+
+(defadvice helm-move--previous-line-fn (around helm-multi-swoop-previous-line-cycle disable)
+ (if (not (helm-pos-multiline-p))
+ (forward-line -1)
+ (helm-move--previous-multi-line-fn))
+ (when (and (helm-pos-header-line-p)
+ (eq (point) (save-excursion (forward-line -1) (point))))
+ (when helm-swoop-move-to-line-cycle
+ (helm-end-of-buffer))
+ (when (helm-pos-multiline-p)
+ (helm-move--previous-multi-line-fn))))
+
+(defun helm-multi-swoop--move-line-action ()
+ (with-helm-window
+ (let* (($key (buffer-substring (point-at-bol) (point-at-eol)))
+ ($num (when (string-match "^[0-9]+" $key)
+ (string-to-number (match-string 0 $key))))
+ ($source (helm-get-current-source))
+ ($buf (let (($name (assoc-default 'name $source)))
+ (when $name (get-buffer $name)))))
+ (when $buf
+ ;; Synchronizing line position
+ (with-selected-window helm-swoop-synchronizing-window
+ (with-current-buffer $buf
+ (when (not (eq $buf helm-multi-swoop-move-line-action-last-buffer))
+ (set-window-buffer nil $buf)
+ (helm-swoop--pattern-match))
+ (helm-swoop--goto-line $num)
+ (helm-swoop--target-line-overlay-move $buf)
+ (helm-swoop--recenter))
+ (setq helm-multi-swoop-move-line-action-last-buffer $buf))
+ (setq helm-swoop-last-line-info (cons $buf $num))))))
+
+(defun helm-multi-swoop--get-marked-buffers ()
+ (let ($list
+ ($buf (get-buffer helm-multi-swoop-buffer-list)))
+ (when $buf
+ (with-current-buffer (get-buffer helm-multi-swoop-buffer-list)
+ (mapc (lambda ($ov)
+ (when (eq 'helm-visible-mark (overlay-get $ov 'face))
+ (setq $list (cons
+ (let (($word (buffer-substring-no-properties
+ (overlay-start $ov) (overlay-end $ov))))
+ (mapc (lambda ($r)
+ (setq $word (replace-regexp-in-string
+ (car $r) (cdr $r) $word)))
+ (list '("\\`[ \t\n\r]+" . "")
+ '("[ \t\n\r]+\\'" . "")))
+ $word)
+ $list))))
+ (overlays-in (point-min) (point-max))))
+ (delete "" $list))))
+
+;; core --------------------------------------------------------
+
+(cl-defun helm-multi-swoop--exec (ignored &key $query $buflist $func $action)
+ (interactive)
+ (setq helm-swoop-synchronizing-window (selected-window))
+ (setq helm-swoop-last-point
+ (or helm-multi-swoop-all-from-helm-swoop-last-point
+ (cons (point) (buffer-name (current-buffer)))))
+ (setq helm-swoop-last-line-info
+ (cons (current-buffer) (line-number-at-pos)))
+ (unless (get-buffer helm-multi-swoop-buffer-list)
+ (get-buffer-create helm-multi-swoop-buffer-list))
+ (helm-swoop--set-prefix (prefix-numeric-value current-prefix-arg))
+ (let (($buffs (or $buflist
+ (helm-multi-swoop--get-marked-buffers)
+ `(,(buffer-name helm-swoop-target-buffer))
+ (error "No buffer selected")))
+ $contents
+ $preserve-position
+ ($prefix-arg (prefix-numeric-value
+ (or current-prefix-arg helm-swoop-last-prefix-number 1))))
+ (helm-swoop--set-prefix $prefix-arg)
+ (setq helm-multi-swoop-last-selected-buffers $buffs)
+ ;; Create buffer sources
+ (mapc (lambda ($buf)
+ (when (get-buffer $buf)
+ (with-current-buffer (get-buffer $buf)
+ (let* (($func
+ (or $func
+ (lambda ()
+ (helm-swoop--split-lines-by
+ (helm-swoop--get-content $buf t)
+ "\n" $prefix-arg))))
+ ($action
+ (or $action
+ `(("Go to Line"
+ . (lambda ($line)
+ (switch-to-buffer ,$buf)
+ (helm-swoop--goto-line
+ (when (string-match "^[0-9]+" $line)
+ (string-to-number
+ (match-string 0 $line))))
+ (when (re-search-forward
+ (mapconcat 'identity
+ (split-string
+ helm-pattern " ") "\\|")
+ nil t)
+ (helm-swoop-flash-word (match-beginning 0) (match-end 0))
+ (goto-char (match-beginning 0)))
+ (helm-swoop--recenter)))))))
+ (setq $preserve-position
+ (cons (cons $buf (point)) $preserve-position))
+ (setq
+ $contents
+ (cons
+ (helm-c-source-multi-swoop $buf $func $action $prefix-arg)
+ $contents))))))
+ $buffs)
+ (unwind-protect
+ (progn
+ (ad-enable-advice 'helm-next-line 'around
+ 'helm-multi-swoop-next-line)
+ (ad-activate 'helm-next-line)
+ (ad-enable-advice 'helm-previous-line 'around
+ 'helm-multi-swoop-previous-line)
+ (ad-activate 'helm-previous-line)
+ (ad-enable-advice 'helm-toggle-visible-mark 'around
+ 'helm-multi-swoop-toggle-visible-mark)
+ (ad-activate 'helm-toggle-visible-mark)
+ (ad-enable-advice 'helm-move--next-line-fn 'around
+ 'helm-multi-swoop-next-line-cycle)
+ (ad-activate 'helm-move--next-line-fn)
+ (ad-enable-advice 'helm-move--previous-line-fn 'around
+ 'helm-multi-swoop-previous-line-cycle)
+ (ad-activate 'helm-move--previous-line-fn)
+ (add-hook 'helm-update-hook 'helm-swoop--pattern-match)
+ (add-hook 'helm-after-update-hook 'helm-swoop--keep-nearest-position t)
+ (setq helm-swoop-line-overlay
+ (make-overlay (point) (point)))
+ (overlay-put helm-swoop-line-overlay
+ 'face 'helm-swoop-target-line-face)
+ (helm-swoop--target-line-overlay-move)
+ ;; Execute helm
+ (let ((helm-display-function helm-swoop-split-window-function)
+ (helm-display-source-at-screen-top nil)
+ (helm-completion-window-scroll-margin 5))
+ (helm :sources $contents
+ :buffer helm-multi-swoop-buffer
+ :input (or $query helm-multi-swoop-query "")
+ :prompt helm-swoop-prompt
+ :candidate-number-limit
+ helm-multi-swoop-candidate-number-limit
+ :preselect
+ (regexp-quote
+ (format "%s %s" (line-number-at-pos)
+ (helm-swoop--get-string-at-line))))))
+ ;; Restore
+ (progn
+ (when (= 1 helm-exit-status)
+ (helm-swoop-back-to-last-point t)
+ (helm-swoop--restore-unveiled-overlay))
+ (setq helm-swoop-invisible-targets nil)
+ (ad-disable-advice 'helm-next-line 'around
+ 'helm-multi-swoop-next-line)
+ (ad-activate 'helm-next-line)
+ (ad-disable-advice 'helm-previous-line 'around
+ 'helm-multi-swoop-previous-line)
+ (ad-activate 'helm-previous-line)
+ (ad-disable-advice 'helm-toggle-visible-mark 'around
+ 'helm-multi-swoop-toggle-visible-mark)
+ (ad-activate 'helm-toggle-visible-mark)
+ (ad-disable-advice 'helm-move--next-line-fn 'around
+ 'helm-multi-swoop-next-line-cycle)
+ (ad-activate 'helm-move--next-line-fn)
+ (ad-disable-advice 'helm-move--previous-line-fn 'around
+ 'helm-multi-swoop-previous-line-cycle)
+ (ad-activate 'helm-move--previous-line-fn)
+ (remove-hook 'helm-update-hook 'helm-swoop--pattern-match)
+ (remove-hook 'helm-after-update-hook 'helm-swoop--keep-nearest-position)
+ (setq helm-multi-swoop-last-query helm-swoop-pattern)
+ (helm-swoop--restore-unveiled-overlay)
+ (setq helm-multi-swoop-query nil)
+ (setq helm-multi-swoop-all-from-helm-swoop-last-point nil)
+ (mapc (lambda ($buf)
+ (let (($current-buffer (buffer-name (current-buffer))))
+ (with-current-buffer (car $buf)
+ ;; Delete overlay
+ (delete-overlay helm-swoop-line-overlay)
+ (helm-swoop--delete-overlay 'target-buffer)
+ ;; Restore each buffer's position
+ (unless (equal (car $buf) $current-buffer)
+ (goto-char (cdr $buf))))))
+ $preserve-position)))))
+
+(defun helm-multi-swoop--get-buffer-list ()
+ (let ($buflist1 $buflist2)
+ ;; eliminate buffers start with whitespace and dired buffers
+ (mapc (lambda ($buf)
+ (setq $buf (buffer-name $buf))
+ (unless (string-match "^\\s-" $buf)
+ (unless (eq 'dired-mode (with-current-buffer $buf major-mode))
+ (setq $buflist1 (cons $buf $buflist1)))))
+ (buffer-list))
+ ;; eliminate buffers match pattern
+ (mapc (lambda ($buf)
+ (unless (string-match
+ helm-multi-swoop-ignore-buffers-match
+ $buf)
+ (setq $buflist2 (cons $buf $buflist2))))
+ $buflist1)
+ $buflist2))
+
+(defun helm-c-source-helm-multi-swoop-buffers ()
+ "Show buffer list to select"
+ `((name . "helm-multi-swoop select buffers")
+ (candidates . helm-multi-swoop--get-buffer-list)
+ (header-line . "[C-SPC]/[M-SPC] select, [RET] next step")
+ (keymap . ,helm-multi-swoop-buffers-map)
+ (match . ,(helm-swoop-match-functions))
+ (search . ,(helm-swoop-search-functions))))
+
+(defun helm-multi-swoop--get-query ($query)
+ (cond ($query
+ (setq helm-multi-swoop-query $query))
+ (mark-active
+ (let (($st (buffer-substring-no-properties
+ (region-beginning) (region-end))))
+ (if (string-match "\n" $st)
+ (message "Multi line region is not allowed")
+ (setq helm-multi-swoop-query
+ (helm-swoop-pre-input-optimize $st)))))
+ ((setq helm-multi-swoop-query
+ (helm-swoop-pre-input-optimize
+ (funcall helm-swoop-pre-input-function))))
+ (t (setq helm-multi-swoop-query ""))))
+
+;;;###autoload
+(defun helm-multi-swoop (&optional $query $buflist)
+ "\
+Usage:
+M-x helm-multi-swoop
+1. Select any buffers by [C-SPC] or [M-SPC]
+2. Press [RET] to start helm-multi-swoop
+
+C-u M-x helm-multi-swoop
+If you have done helm-multi-swoop before, you can skip select buffers step.
+Last selected buffers will be applied to helm-multi-swoop.
+"
+ (interactive)
+ (setq helm-multi-swoop-query (helm-multi-swoop--get-query $query))
+ (if (equal current-prefix-arg '(4))
+ (helm-multi-swoop--exec nil
+ :$query helm-multi-swoop-query
+ :$buflist $buflist)
+ (if $buflist
+ (helm-multi-swoop--exec nil
+ :$query $query
+ :$buflist $buflist)
+ (helm :sources (helm-c-source-helm-multi-swoop-buffers)
+ :buffer helm-multi-swoop-buffer-list
+ :prompt "Mark any buffers by [C-SPC] or [M-SPC]: "))))
+
+;;;###autoload
+(defun helm-multi-swoop-all (&optional $query)
+ "Apply all buffers to helm-multi-swoop"
+ (interactive)
+ (setq helm-multi-swoop-query (helm-multi-swoop--get-query $query))
+ (helm-multi-swoop--exec nil
+ :$query helm-multi-swoop-query
+ :$buflist (helm-multi-swoop--get-buffer-list)))
+
+(defun get-buffers-matching-mode ($mode)
+ "Returns a list of buffers where their major-mode is equal to MODE"
+ (let ($buffer-mode-matches)
+ (mapc (lambda ($buf)
+ (when (get-buffer $buf)
+ (with-current-buffer (get-buffer $buf)
+ (if (eq $mode major-mode)
+ (add-to-list '$buffer-mode-matches (buffer-name $buf))))))
+ (buffer-list))
+ $buffer-mode-matches))
+
+(defun helm-multi-swoop-by-mode ($mode &optional $query)
+ "Apply all buffers whose mode is MODE to helm-multi-swoop"
+ (setq helm-multi-swoop-query (helm-multi-swoop--get-query $query))
+ (if (get-buffers-matching-mode $mode)
+ (helm-multi-swoop--exec nil
+ :$query helm-multi-swoop-query
+ :$buflist (get-buffers-matching-mode $mode))
+ (message "there are no buffers in that mode right now")))
+
+;;;###autoload
+(defun helm-multi-swoop-org (&optional $query)
+ "Applies all org-mode buffers to helm-multi-swoop"
+ (interactive)
+ (helm-multi-swoop-by-mode 'org-mode $query))
+
+;;;###autoload
+(defun helm-multi-swoop-current-mode (&optional $query)
+ "Applies all buffers of the same mode as the current buffer to helm-multi-swoop"
+ (interactive)
+ (helm-multi-swoop-by-mode major-mode $query))
+
+;;;###autoload
+(defun helm-multi-swoop-projectile (&optional $query)
+ "Apply all opened buffers of the current project to helm-multi-swoop"
+ (interactive)
+ (setq helm-multi-swoop-query (helm-multi-swoop--get-query $query))
+ (if (require 'projectile nil 'noerror)
+ ;; set filter function that is used in projectile-project-buffers
+ (let ((projectile-buffers-filter-function
+ helm-multi-swoop-projectile-buffers-filter))
+ (helm-multi-swoop--exec nil
+ :$query helm-multi-swoop-query
+ :$buflist (mapcar #'buffer-name
+ (projectile-project-buffers))))
+ (error "Package 'projectile' is not available")))
+
+
+(defun helm-swoop--wrap-function-with-pre-input-function ($target-func $pre-input-func)
+ (let (($restore helm-swoop-pre-input-function))
+ (unwind-protect
+ (progn
+ (setq helm-swoop-pre-input-function $pre-input-func)
+ (funcall $target-func))
+ (setq helm-swoop-pre-input-function $restore))))
+
+;;;###autoload
+(defun helm-swoop-without-pre-input ()
+ "Start helm-swoop without pre input query."
+ (interactive)
+ (helm-swoop--wrap-function-with-pre-input-function
+ 'helm-swoop (lambda () nil)))
+
+;;;###autoload
+(defun helm-swoop-symble-pre-input ()
+ "Start helm-swoop without pre input query."
+ (interactive)
+ (helm-swoop--wrap-function-with-pre-input-function
+ 'helm-swoop (lambda () (format "\\_<%s\\_> " (thing-at-point 'symbol)))))
+
+;; option -------------------------------------------------------
+
+(defun helm-multi-swoop-all-from-isearch ()
+ "Invoke `helm-multi-swoop-all' from isearch."
+ (interactive)
+ (let (($query (if isearch-regexp
+ isearch-string
+ (regexp-quote isearch-string))))
+ (let (search-nonincremental-instead)
+ (isearch-exit))
+ (helm-multi-swoop-all $query)))
+;; When doing isearch, hand the word over to helm-swoop
+;; (define-key isearch-mode-map (kbd "C-x M-i") 'helm-multi-swoop-all-from-isearch)
+
+(defun helm-multi-swoop-all-from-helm-swoop ()
+ "Invoke `helm-multi-swoop-all' from helm-swoop."
+ (interactive)
+ (helm-swoop--restore)
+ (delete-overlay helm-swoop-line-overlay)
+ (setq helm-multi-swoop-all-from-helm-swoop-last-point helm-swoop-last-point)
+ (let (($query helm-pattern))
+ (run-with-timer 0 nil (lambda () (helm-multi-swoop-all $query))))
+ (helm-exit-minibuffer))
+
+(defun helm-multi-swoop-current-mode-from-helm-swoop ()
+ "Invoke `helm-multi-swoop-all' from helm-swoop."
+ (interactive)
+ (helm-swoop--restore)
+ (delete-overlay helm-swoop-line-overlay)
+ (setq helm-multi-swoop-all-from-helm-swoop-last-point helm-swoop-last-point)
+ (let (($query helm-pattern))
+ (run-with-timer 0 nil (lambda () (helm-multi-swoop-current-mode $query))))
+ (helm-exit-minibuffer))
+;; (define-key helm-swoop-map (kbd "M-m") 'helm-multi-swoop-current-mode-from-helm-swoop)
+
+(defadvice helm-resume (around helm-multi-swoop-resume activate)
+ "Resume if the last used helm buffer is *Helm Swoop*"
+ (if (equal helm-last-buffer helm-multi-swoop-buffer)
+
+ (if (boundp 'helm-multi-swoop-last-query)
+ (if (not (ad-get-arg 0))
+ (helm-multi-swoop helm-multi-swoop-last-query
+ helm-multi-swoop-last-selected-buffers))
+ ;; Temporary apply second last buffer
+ (let ((helm-last-buffer (cadr helm-buffers))) ad-do-it))
+ ad-do-it))
+
+;;; @ helm-multi-swoop-edit -----------------------------------
+(defvar helm-multi-swoop-edit-save t
+ "Save each buffer you edit when editing is complete")
+(defvar helm-multi-swoop-edit-buffer "*Helm Multi Swoop Edit*")
+
+(defvar helm-multi-swoop-edit-map
+ (let (($map (make-sparse-keymap)))
+ (define-key $map (kbd "C-x C-s") 'helm-multi-swoop--edit-complete)
+ (define-key $map (kbd "C-c C-g") 'helm-multi-swoop--edit-cancel)
+ (define-key $map (kbd "C-c C-q C-k") 'helm-multi-swoop--edit-delete-all-lines)
+ $map))
+
+(defun helm-multi-swoop--edit ($candidate)
+ "This function will only be called from `helm-swoop-edit'"
+ (interactive)
+ (delete-overlay helm-swoop-line-overlay)
+ (helm-swoop--delete-overlay 'target-buffer)
+ (with-current-buffer (get-buffer-create helm-multi-swoop-edit-buffer)
+ (helm-swoop--clear-edit-buffer 'helm-multi-swoop-edit)
+ (let (($bufstr "")
+ ($mark nil)
+ ($buf (get-buffer helm-multi-swoop-buffer)))
+ ;; Get target line number to edit
+ (when $buf
+ (with-current-buffer $buf
+ ;; Set overlay to helm-source-header for editing marked lines
+ (save-excursion
+ (goto-char (point-min))
+ (let (($beg (point)) $end)
+ (overlay-recenter (point-max))
+ (while (setq $beg (text-property-any $beg (point-max)
+ 'face 'helm-source-header))
+ (setq $end (next-single-property-change $beg 'face))
+ (overlay-put (make-overlay $beg $end) 'source-header t)
+ (setq $beg $end)
+ (goto-char $end))))
+ ;; Use selected line by [C-SPC] or [M-SPC]
+ (mapc (lambda ($ov)
+ (when (overlay-get $ov 'source-header)
+ (setq $bufstr (concat (buffer-substring
+ (overlay-start $ov) (overlay-end $ov))
+ $bufstr)))
+ (when (eq 'helm-visible-mark (overlay-get $ov 'face))
+ (let (($str (buffer-substring (overlay-start $ov) (overlay-end $ov))))
+ (unless (equal "" $str) (setq $mark t))
+ (setq $bufstr (concat (buffer-substring
+ (overlay-start $ov) (overlay-end $ov))
+ $bufstr)))))
+ (overlays-in (point-min) (point-max)))
+ (if $mark
+ (progn (setq $bufstr (concat "Helm Multi Swoop\n" $bufstr))
+ (setq $mark nil))
+ (setq $bufstr (concat "Helm Multi Swoop\n"
+ (buffer-substring
+ (point-min) (point-max)))))))
+
+ ;; Set for edit buffer
+ (insert $bufstr)
+ (add-text-properties (point-min) (point-max)
+ '(read-only t rear-nonsticky t front-sticky t))
+
+ ;; Set for editable context
+ (let ((inhibit-read-only t))
+ ;; Title and explanation
+ (goto-char (point-min))
+ (let (($o (make-overlay (point) (point-at-eol))))
+ (overlay-put $o 'helm-multi-swoop-edit t)
+ (overlay-put $o 'face 'font-lock-function-name-face)
+ (overlay-put $o 'after-string
+ (propertize
+ (substitute-command-keys
+ (concat
+ " [\\<helm-swoop-edit-map>\\[helm-swoop--edit-complete]] Complete"
+ ", [\\<helm-swoop-edit-map>\\[helm-swoop--edit-cancel]] Cancel"
+ ", [\\<helm-swoop-edit-map>\\[helm-swoop--edit-delete-all-lines]] Delete All"
+ ))
+ 'face 'helm-bookmark-addressbook)))
+ ;; Line number and editable area
+ (while (re-search-forward "^\\([0-9]+\s\\)\\(.*\\)$" nil t)
+ (let* (($bol1 (match-beginning 1))
+ ($eol1 (match-end 1))
+ ($bol2 (match-beginning 2))
+ ($eol2 (match-end 2)))
+
+ ;; Line number
+ (add-text-properties $bol1 $eol1
+ '(face font-lock-function-name-face
+ intangible t))
+ ;; Editable area
+ (remove-text-properties $bol2 $eol2 '(read-only t))
+ ;; (add-text-properties $bol2 $eol2 '(font-lock-face helm-match))
+
+ ;; For line tail
+ (set-text-properties $eol2 (or (1+ $eol2) (point-max))
+ '(read-only t rear-nonsticky t))))
+ (helm-swoop--target-word-overlay 'edit-buffer 0))))
+
+ (other-window 1)
+ (switch-to-buffer helm-multi-swoop-edit-buffer)
+ (goto-char (point-min))
+ (if (string-match "^[0-9]+" $candidate)
+ (re-search-forward
+ (concat "^" (match-string 0 $candidate)) nil t))
+ (use-local-map helm-multi-swoop-edit-map))
+
+(defun helm-multi-swoop--separate-text-property-into-list ($property)
+ (interactive)
+ (let ($list $end)
+ (save-excursion
+ (goto-char (point-min))
+ (while (setq $end (next-single-property-change (point) $property))
+ ;; Must eliminate last return because of unexpected edit result
+ (setq $list (cons
+ (let (($str (buffer-substring-no-properties (point) $end)))
+ (if (string-match "\n\n\\'" $str)
+ (replace-regexp-in-string "\n\\'" "" $str)
+ $str))
+ $list))
+ (goto-char $end))
+ (setq $list (cons (buffer-substring-no-properties (point) (point-max))
+ $list)))
+ (nreverse $list)))
+
+(defun helm-multi-swoop--collect-edited-lines ()
+ "Create a list of edited lines with each its own line number"
+ (interactive)
+ (let* (($list
+ (helm-multi-swoop--separate-text-property-into-list 'helm-header))
+ ($length (length $list))
+ ($i 1) ;; 0th $list is header
+ $pairs)
+ (while (<= $i $length)
+ (let ($contents)
+ ;; Make ((number . line) (number . line) (number . line) ...)
+ (with-temp-buffer
+ (insert (format "%s" (nth (1+ $i) $list)))
+ (goto-char (point-min))
+ (while (re-search-forward "^\\([0-9]+\\)\s" nil t)
+ (setq $contents
+ (cons (cons (string-to-number (match-string 1))
+ (buffer-substring-no-properties
+ (point)
+ (save-excursion
+ (if (re-search-forward
+ "^\\([0-9]+\\)\s\\|^\\(\\-+\\)" nil t)
+ (1- (match-beginning 0))
+ (goto-char (point-max))
+ (re-search-backward "\n" nil t)))))
+ $contents))))
+ ;; Make ((buffer-name (number . line) (number . line) ...)
+ ;; (buffer-name (number . line) (number . line) ...) ...)
+ (setq $pairs (cons (cons (nth $i $list) $contents) $pairs)))
+ (setq $i (+ $i 2)))
+ (delete '(nil) $pairs)))
+
+(defun helm-multi-swoop--edit-complete ()
+ "Delete all extracted lines, and apply changes to buffers and kill temporary edit buffer"
+ (interactive)
+ (let (($list (helm-multi-swoop--collect-edited-lines))
+ $read-only)
+ (mapc (lambda ($x)
+ (with-current-buffer (car $x)
+ (unless buffer-read-only
+ (save-excursion
+ (mapc (lambda ($cell)
+ (let (($k (car $cell))
+ ($v (cdr $cell)))
+ (goto-char (point-min))
+ (delete-region (point-at-bol $k) (point-at-eol $k))
+ (goto-char (point-at-bol $k))
+ (insert $v)))
+ (cdr $x))))
+ (if helm-multi-swoop-edit-save
+ (if buffer-read-only
+ (setq $read-only t)
+ (save-buffer)))))
+ $list)
+ (select-window helm-swoop-synchronizing-window)
+ (kill-buffer (get-buffer helm-multi-swoop-edit-buffer))
+ (if $read-only
+ (message "Couldn't save some buffers because of read-only")
+ (message "Successfully helm-multi-swoop-edit applied to original buffer"))))
+
+(defun helm-multi-swoop--edit-delete-all-lines ()
+ "Delete all extracted lines, and apply changes to buffers and kill temporary edit buffer"
+ (interactive)
+ (let (($list (helm-multi-swoop--collect-edited-lines))
+ $read-only)
+ (mapc (lambda ($x)
+ (with-current-buffer (car $x)
+ (unless buffer-read-only
+ (save-excursion
+ (mapc (lambda ($cell)
+ (let (($k (car $cell)))
+ (goto-char (point-min))
+ (delete-region (point-at-bol $k)
+ (if (eq (point-at-eol $k) (point-max))
+ (point-at-eol $k)
+ (1+ (point-at-eol $k))))))
+ (cdr $x))))
+ (if helm-multi-swoop-edit-save
+ (if buffer-read-only
+ (setq $read-only t)
+ (save-buffer)))))
+ $list)
+ (select-window helm-swoop-synchronizing-window)
+ (kill-buffer (get-buffer helm-multi-swoop-edit-buffer))
+ (if $read-only
+ (message "Couldn't save some buffers because of read-only")
+ (message "Successfully helm-multi-swoop-edit applied to original buffer"))))
+
+(defun helm-multi-swoop--edit-cancel ()
+ "Cancel edit and kill temporary buffer"
+ (interactive)
+ (select-window helm-swoop-synchronizing-window)
+ (kill-buffer (get-buffer helm-multi-swoop-edit-buffer))
+ (message "helm-multi-swoop-edit canceled"))
+
+;;;###autoload
+(defun helm-multi-swoop-edit ()
+ (interactive)
+ (helm-exit-and-execute-action 'helm-multi-swoop--edit))
+
+;;; @ helm-swoop-same-face-at-point -----------------------------------
+
+(defsubst helm-swoop--get-at-face (&optional $point)
+ (or $point (setq $point (point)))
+ (let (($face (or (get-char-property $point 'read-face-name)
+ (get-char-property $point 'face))))
+ $face))
+
+(defun helm-swoop--cull-face-include-line ($face)
+ (let (($list) ($po (point-min)))
+ (save-excursion
+ (while (setq $po (next-single-property-change $po 'face))
+ (when (equal $face (helm-swoop--get-at-face $po))
+ (goto-char $po)
+ (setq $list (cons (format "%s %s"
+ (line-number-at-pos $po)
+ (buffer-substring (point-at-bol) (point-at-eol)))
+ $list))
+ (let (($ov (make-overlay $po (or (next-single-property-change $po 'face)
+ (point-max)))))
+ (overlay-put $ov 'face 'helm-swoop-target-word-face)
+ (overlay-put $ov 'target-buffer 'helm-swoop-target-word-face)))))
+ (nreverse (delete-dups $list))))
+
+(defun helm-swoop-same-face-at-point (&optional $face)
+ (interactive)
+ (or $face (setq $face (helm-swoop--get-at-face)))
+ (helm-swoop :$query ""
+ :$source
+ `((name . "helm-swoop-same-face-at-point")
+ (candidates . ,(helm-swoop--cull-face-include-line $face))
+ (header-line . ,(format "%s" $face))
+ (action
+ . (("Go to Line"
+ . (lambda ($line)
+ (helm-swoop--goto-line
+ (when (string-match "^[0-9]+" $line)
+ (string-to-number (match-string 0 $line))))
+ (let (($po (point))
+ ($poe (point-at-eol)))
+ (while (<= (setq $po (next-single-property-change $po 'face)) $poe)
+ (when (eq 'helm-swoop-target-word-face (helm-swoop--get-at-face $po))
+ (goto-char $po))))
+ (helm-swoop--recenter))))))))
+
+(defun helm-multi-swoop-same-face-at-point (&optional $face)
+ (interactive)
+ (or $face (setq $face (helm-swoop--get-at-face)))
+ (helm-multi-swoop--exec
+ nil
+ :$query ""
+ :$func (lambda () (helm-swoop--cull-face-include-line $face))
+ :$action (lambda ($line)
+ (switch-to-buffer (assoc-default 'name (helm-get-current-source)))
+ (helm-swoop--goto-line
+ (when (string-match "^[0-9]+" $line)
+ (string-to-number (match-string 0 $line))))
+ (let (($po (point))
+ ($poe (point-at-eol)))
+ (while (<= (setq $po (next-single-property-change $po 'face)) $poe)
+ (when (eq 'helm-swoop-target-word-face (helm-swoop--get-at-face $po))
+ (goto-char $po))))
+ (helm-swoop--recenter))
+ :$buflist (helm-multi-swoop--get-buffer-list)))
+
+(provide 'helm-swoop)
+;;; helm-swoop.el ends here