From c64187b460e8458566c1aa87a701daed2781db7f Mon Sep 17 00:00:00 2001 From: Andreas Gerler Date: Fri, 12 Jan 2018 15:28:20 +0100 Subject: [PATCH] Update avy. Signed-off-by: Andreas Gerler --- bba.org | 2 +- elpa/avy-20160202.2357/avy-autoloads.el | 127 -- elpa/avy-20160202.2357/avy-pkg.el | 1 - elpa/avy-20171230.220/avy-autoloads.el | 246 +++ elpa/avy-20171230.220/avy-pkg.el | 2 + .../avy.el | 861 +++++++-- .../helm-swoop-autoloads.el | 81 + .../helm-swoop-pkg.el | 2 + elpa/helm-swoop-20170923.2127/helm-swoop.el | 1695 +++++++++++++++++ 9 files changed, 2775 insertions(+), 242 deletions(-) delete mode 100644 elpa/avy-20160202.2357/avy-autoloads.el delete mode 100644 elpa/avy-20160202.2357/avy-pkg.el create mode 100644 elpa/avy-20171230.220/avy-autoloads.el create mode 100644 elpa/avy-20171230.220/avy-pkg.el rename elpa/{avy-20160202.2357 => avy-20171230.220}/avy.el (57%) create mode 100644 elpa/helm-swoop-20170923.2127/helm-swoop-autoloads.el create mode 100644 elpa/helm-swoop-20170923.2127/helm-swoop-pkg.el create mode 100644 elpa/helm-swoop-20170923.2127/helm-swoop.el diff --git a/bba.org b/bba.org index 24744b90..ce80cf88 100644 --- a/bba.org +++ b/bba.org @@ -743,7 +743,7 @@ Replaced the deprecated [[https://github.com/winterTTr/ace-jump-mode][ace-jump-m 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) diff --git a/elpa/avy-20160202.2357/avy-autoloads.el b/elpa/avy-20160202.2357/avy-autoloads.el deleted file mode 100644 index bd42f86d..00000000 --- a/elpa/avy-20160202.2357/avy-autoloads.el +++ /dev/null @@ -1,127 +0,0 @@ -;;; avy-autoloads.el --- automatically extracted autoloads -;; -;;; Code: -(add-to-list 'load-path (or (file-name-directory #$) (car load-path))) - -;;;### (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) - -;;;*** - -;; Local Variables: -;; version-control: never -;; no-byte-compile: t -;; no-update-autoloads: t -;; End: -;;; avy-autoloads.el ends here diff --git a/elpa/avy-20160202.2357/avy-pkg.el b/elpa/avy-20160202.2357/avy-pkg.el deleted file mode 100644 index 9d5d4290..00000000 --- a/elpa/avy-20160202.2357/avy-pkg.el +++ /dev/null @@ -1 +0,0 @@ -(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")) diff --git a/elpa/avy-20171230.220/avy-autoloads.el b/elpa/avy-20171230.220/avy-autoloads.el new file mode 100644 index 00000000..adef5c90 --- /dev/null +++ b/elpa/avy-20171230.220/avy-autoloads.el @@ -0,0 +1,246 @@ +;;; avy-autoloads.el --- automatically extracted autoloads +;; +;;; Code: +(add-to-list 'load-path (directory-file-name (or (file-name-directory #$) (car load-path)))) + +;;;### (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) + +;;;*** + +;; Local Variables: +;; version-control: never +;; no-byte-compile: t +;; no-update-autoloads: t +;; End: +;;; avy-autoloads.el ends here diff --git a/elpa/avy-20171230.220/avy-pkg.el b/elpa/avy-20171230.220/avy-pkg.el new file mode 100644 index 00000000..e509bb5c --- /dev/null +++ b/elpa/avy-20171230.220/avy-pkg.el @@ -0,0 +1,2 @@ +;;; -*- 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")) diff --git a/elpa/avy-20160202.2357/avy.el b/elpa/avy-20171230.220/avy.el similarity index 57% rename from elpa/avy-20160202.2357/avy.el rename to elpa/avy-20171230.220/avy.el index ebbe9bad..9ddf2399 100644 --- a/elpa/avy-20160202.2357/avy.el +++ b/elpa/avy-20171230.220/avy.el @@ -1,10 +1,10 @@ -;;; avy.el --- tree-based completion -*- lexical-binding: t -*- +;;; 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 ;; URL: https://github.com/abo-abo/avy -;; Package-Version: 20160202.2357 +;; Package-Version: 20171230.220 ;; Version: 0.4.0 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5")) ;; Keywords: point, location @@ -26,15 +26,23 @@ ;;; 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. +;; 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. ;; -;; 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: +;; 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 @@ -63,8 +71,12 @@ 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") +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 @@ -81,9 +93,48 @@ in the avy overlays." (const avy-goto-word-1) (const avy-copy-line) (const avy-copy-region) - (const avy-move-line)) + (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." @@ -92,7 +143,8 @@ Use `avy-styles-alist' to customize this per-command." (const :tag "At" at) (const :tag "At Full" at-full) (const :tag "Post" post) - (const :tag "De Bruijn" de-bruijn))) + (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. @@ -109,18 +161,29 @@ If the commands isn't on the list, `avy-style' is used." (const avy-goto-word-1) (const avy-copy-line) (const avy-copy-region) - (const avy-move-line)) + (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 "De Bruijn" de-bruijn) + (const :tag "Words" words)))) (defcustom avy-dispatch-alist - '((?x . avy-action-kill) + '((?x . avy-action-kill-move) + (?X . avy-action-kill-stay) + (?t . avy-action-teleport) (?m . avy-action-mark) - (?n . avy-action-copy)) + (?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 @@ -132,7 +195,8 @@ pressed during the dispatch, ACTION is set to replace the default :value-type (choice (const :tag "Mark" avy-action-mark) (const :tag "Copy" avy-action-copy) - (const :tag "Kill" avy-action-kill)))) + (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." @@ -157,9 +221,18 @@ 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.") +Typically, these modes don't use the text representation." + :type 'list) (defvar avy-ring (make-ring 20) "Hold the window and point history.") @@ -229,16 +302,16 @@ self-inserting keys and thus aren't read as characters.") (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))))) + (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)) @@ -272,9 +345,10 @@ SEQ-LEN is how many elements of KEYS it takes to identify a match." (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))))) + (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)))) @@ -345,12 +419,18 @@ KEYS is the path from the root of `avy-tree' to LEAF." (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)))) + (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'.") @@ -358,6 +438,16 @@ KEYS is the path from the root of `avy-tree' to LEAF." (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'. @@ -366,7 +456,7 @@ 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." +multiple DISPLAY-FN invocations." (catch 'done (setq avy-current-path "") (while tree @@ -377,14 +467,19 @@ multiple DISPLAY-FN invokations." (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 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)))))) + (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." @@ -421,6 +516,56 @@ multiple DISPLAY-FN invokations." (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'." @@ -436,9 +581,10 @@ multiple DISPLAY-FN invokations." (t (error "Unrecognized option: %S" avy-all-windows)))) -(defcustom avy-all-windows-alt t +(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))) @@ -454,6 +600,15 @@ multiple DISPLAY-FN invokations." (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." @@ -462,26 +617,41 @@ Set `avy-style' according to COMMMAND as well." `(let ((avy-keys (or (cdr (assq ',command avy-keys-alist)) avy-keys)) (avy-style (or (cdr (assq ',command avy-styles-alist)) - avy-style))) + 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." - (goto-char 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)) - (forward-sexp)) + (avy-forward-item)) (defun avy-action-copy (pt) "Copy sexp starting on PT." (save-excursion (let (str) (goto-char pt) - (forward-sexp) + (avy-forward-item) (setq str (buffer-substring pt (point))) (kill-new str) (message "Copied: %s" str))) @@ -491,12 +661,63 @@ Set `avy-style' according to COMMMAND as well." (select-window (cdr dat)) (goto-char (car dat)))) -(defun avy-action-kill (pt) - "Kill sexp at PT." +(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) - (forward-sexp) + (avy-forward-item) (kill-region pt (point)) - (message "Killed: %s" (current-kill 0))) + (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'. @@ -517,12 +738,16 @@ Use OVERLAY-FN to visualize the decision overlay." (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)))) + (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)) @@ -570,14 +795,14 @@ Use OVERLAY-FN to visualize the decision overlay." (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)))) + (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-overlay-change s)))) + (while (and (not (= (point-max) (setq s (next-char-property-change s)))) (not (get-char-property s 'invisible)))) s)) @@ -678,10 +903,11 @@ Do this even when the char is terminating." (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)))) + (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." @@ -776,6 +1002,10 @@ LEAF is normally ((BEG . END) . WND)." (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)) @@ -880,13 +1110,15 @@ exist." (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 delimit the area where candidates are searched." +BEG and END narrow the scope where candidates are searched." (let ((avy-all-windows (if window-flip (not avy-all-windows) @@ -923,17 +1155,55 @@ The window scope is determined by `avy-all-windows' (ARG negates it)." (line-end-position)))) ;;;###autoload -(defun avy-goto-char-2 (char1 char2 &optional arg) +(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' (ARG negates it)." +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)) + current-prefix-arg + nil nil)) + (when (eq char1 ? ) + (setq char1 ?\n)) + (when (eq char2 ? ) + (setq char2 ?\n)) (avy-with avy-goto-char-2 (avy--generic-jump (regexp-quote (string char1 char2)) arg - avy-style))) + 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 () @@ -942,22 +1212,45 @@ The window scope is determined by `avy-all-windows' (ARG negates it)." (avy-with avy-isearch (let ((avy-background nil)) (avy--process - (avy--regex-candidates isearch-string) + (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) +(defun avy-goto-word-0 (arg &optional beg end) "Jump to a word start. -The window scope is determined by `avy-all-windows' (ARG negates it)." +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 "\\b\\sw" arg avy-style))) + (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) +(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' (ARG negates it)." +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 @@ -967,11 +1260,71 @@ The window scope is determined by `avy-all-windows' (ARG negates it)." ((and avy-word-punc-regexp (string-match avy-word-punc-regexp str)) (regexp-quote str)) + ((<= char 26) + str) (t (concat - "\\b" + (if symbol "\\_<" "\\b") str))))) - (avy--generic-jump regex arg avy-style)))) + (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) @@ -1011,7 +1364,12 @@ should return true." (and predicate (funcall predicate))) (unless (get-char-property (point) 'invisible) (push (cons (point) (selected-window)) window-cands))) - (subword-backward))) + (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))))) @@ -1025,7 +1383,9 @@ The case of CHAR is ignored." (avy-with avy-goto-subword-1 (let ((char (downcase char))) (avy-goto-subword-0 - arg (lambda () (eq (downcase (char-after)) char)))))) + arg (lambda () + (and (char-after) + (eq (downcase (char-after)) char))))))) ;;;###autoload (defun avy-goto-word-or-subword-1 () @@ -1038,10 +1398,11 @@ Which one depends on variable `subword-mode'." (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." +(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)))) @@ -1062,8 +1423,103 @@ Narrow the scope to BEG END." (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))))) + (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) @@ -1083,11 +1539,12 @@ Otherwise, forward to `goto-line' with ARG." (goto-char (point-min)) (forward-line (1- arg))) (avy-with avy-goto-line - (let* ((avy-handler-function + (let* ((avy-handler-old avy-handler-function) + (avy-handler-function (lambda (char) (if (or (< char ?0) (> char ?9)) - (avy-handler-default char) + (funcall avy-handler-old char) (let ((line (read-from-minibuffer "Goto line: " (string char)))) (when line @@ -1209,6 +1666,140 @@ The window scope is determined by `avy-all-windows' or (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." @@ -1216,18 +1807,29 @@ The window scope is determined by `avy-all-windows' or '(define-key isearch-mode-map (kbd "C-'") 'avy-isearch))) (defcustom avy-timeout-seconds 0.5 - "How many seconds to wait for the second char.") + "How many seconds to wait for the second char." + :type 'float) -(defun avy--read-candidates () - "Read as many chars as possible and return their occurences. +(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'. `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'. +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." - (let ((str "") char break overlays regex) +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) @@ -1246,9 +1848,11 @@ This function obeys `avy-all-windows' setting." (cond ;; Handle RET ((= char 13) - (setq break t)) - ;; Handle DEL - ((= char 127) + (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)))))) @@ -1265,16 +1869,18 @@ This function obeys `avy-all-windows' setting." (window-end (selected-window) t))) (save-excursion (goto-char (car pair)) - (setq regex (regexp-quote str)) + (setq regex (funcall re-builder 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)))) + (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))))))) + (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) @@ -1323,12 +1929,41 @@ The window scope is determined by `avy-all-windows' (ARG negates it)." (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.") +;; ** Org-mode +(defvar org-reverse-note-order) +(declare-function org-refile "org") +(declare-function org-back-to-heading "org") -(define-obsolete-function-alias 'avy--with-avy-keys 'avy-with "0.3.0") +(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) diff --git a/elpa/helm-swoop-20170923.2127/helm-swoop-autoloads.el b/elpa/helm-swoop-20170923.2127/helm-swoop-autoloads.el new file mode 100644 index 00000000..7fa74c74 --- /dev/null +++ b/elpa/helm-swoop-20170923.2127/helm-swoop-autoloads.el @@ -0,0 +1,81 @@ +;;; helm-swoop-autoloads.el --- automatically extracted autoloads +;; +;;; Code: +(add-to-list 'load-path (directory-file-name (or (file-name-directory #$) (car load-path)))) + +;;;### (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) + +;;;*** + +;; Local Variables: +;; version-control: never +;; no-byte-compile: t +;; no-update-autoloads: t +;; End: +;;; helm-swoop-autoloads.el ends here diff --git a/elpa/helm-swoop-20170923.2127/helm-swoop-pkg.el b/elpa/helm-swoop-20170923.2127/helm-swoop-pkg.el new file mode 100644 index 00000000..353ee92a --- /dev/null +++ b/elpa/helm-swoop-20170923.2127/helm-swoop-pkg.el @@ -0,0 +1,2 @@ +;;; -*- 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")) diff --git a/elpa/helm-swoop-20170923.2127/helm-swoop.el b/elpa/helm-swoop-20170923.2127/helm-swoop.el new file mode 100644 index 00000000..ba070617 --- /dev/null +++ b/elpa/helm-swoop-20170923.2127/helm-swoop.el @@ -0,0 +1,1695 @@ +;;; 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-edit]] Edit mode, \ +[\\\\[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-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-complete]] Complete" + ", [\\\\[helm-swoop--edit-cancel]] Cancel" + ", [\\\\[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-complete]] Complete" + ", [\\\\[helm-swoop--edit-cancel]] Cancel" + ", [\\\\[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 -- 2.47.3