diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2020-07-14 11:25:27 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2020-07-14 11:25:54 -0700 |
commit | 462e785258720862a4c91c28342a24087bfb15c8 (patch) | |
tree | acab641842bfa3b8ddfb16ca0f98c31e71d7df7f /.emacs.d/initlibs | |
parent | 363f4d690663f5ce05e1601838219c5b464a2105 (diff) | |
download | dotfiles-462e785258720862a4c91c28342a24087bfb15c8.tar.gz |
drop some other embedded copies of Emacs libs
Diffstat (limited to '.emacs.d/initlibs')
-rw-r--r-- | .emacs.d/initlibs/ace-link.el | 401 | ||||
-rw-r--r-- | .emacs.d/initlibs/avy.el | 1976 | ||||
-rw-r--r-- | .emacs.d/initlibs/key-chord.el | 372 | ||||
-rw-r--r-- | .emacs.d/initlibs/smex.el | 446 |
4 files changed, 0 insertions, 3195 deletions
diff --git a/.emacs.d/initlibs/ace-link.el b/.emacs.d/initlibs/ace-link.el deleted file mode 100644 index e5a9e1c5..00000000 --- a/.emacs.d/initlibs/ace-link.el +++ /dev/null @@ -1,401 +0,0 @@ -;;; ace-link.el --- Quickly follow links - -;; Copyright (C) 2014-2015 Oleh Krehel - -;; Author: Oleh Krehel <ohwoeowho@gmail.com> -;; URL: https://github.com/abo-abo/ace-link -;; Version: 0.4.0 -;; Package-Requires: ((avy "0.2.0")) -;; Keywords: convenience, links - -;; This file is not part of GNU Emacs - -;; This file is free software; you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation; either version 3, or (at your option) -;; any later version. - -;; This program is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;; GNU General Public License for more details. - -;; For a full copy of the GNU General Public License -;; see <http://www.gnu.org/licenses/>. - -;;; Commentary: -;; -;; This package offers an alternative to tabbing through links in -;; buffers, for instance, in an Info buffer. `avy' is used to turn -;; opening a link from an O(N) operation into an O(1). -;; -;; Use `ace-link-setup-default' to set up the default bindings, which currently -;; bind e.g. `ace-link-info' to "o", which was previously unbound and is -;; close to "l" (which by default goes back). -;; -;; Supported modes: `Info-mode', `help-mode', `org-mode', `eww-mode', -;; `gnus-article-mode', `Custom-mode', `woman-mode', `goto-address-mode'. - -;;; Code: -(require 'avy) - -;;* `ace-link' -;;;###autoload -(defun ace-link () - "Call the ace link function for the current `major-mode'" - (interactive) - (cl-case major-mode - (Info-mode - (ace-link-info)) - ((help-mode package-menu-mode) - (ace-link-help)) - (woman-mode - (ace-link-woman)) - (eww-mode - (ace-link-eww)) - ((compilation-mode grep-mode) - (ace-link-compilation)) - (gnus-mode - (ace-link-gnus)) - (org-mode - (ace-link-org)) - (Custom-mode - (ace-link-org)) - (t - (error "%S isn't supported" major-mode)))) - -;;* `ace-link-info' -;;;###autoload -(defun ace-link-info () - "Open a visible link in an `Info-mode' buffer." - (interactive) - (let ((pt (avy-with ace-link-info - (avy--process - (mapcar #'cdr - (ace-link--info-collect)) - #'avy--overlay-post)))) - (ace-link--info-action pt))) - -(defun ace-link--info-action (pt) - (when (numberp pt) - (push-mark) - (goto-char pt) - (let ((we (window-end))) - (while (not (ignore-errors - (Info-follow-nearest-node))) - (forward-char 1) - (when (> (point) we) - (error "Could not follow link")))))) - -(declare-function Info-follow-nearest-node "info") -(declare-function Info-next-reference "info") -(declare-function Info-try-follow-nearest-node "info") -(declare-function Info-goto-node "info") - -(defun ace-link--info-current () - "Return the node at point." - (cons (cl-letf (((symbol-function #'Info-goto-node) - (lambda (node _) node))) - (Info-try-follow-nearest-node)) - (point))) - -(defun ace-link--info-collect () - "Collect the positions of visible links in the current `Info-mode' buffer." - (let ((end (window-end)) - points) - (save-excursion - (goto-char (window-start)) - (when (ignore-errors (Info-next-reference) t) - (push (ace-link--info-current) points) - (Info-next-reference) - (while (and (< (point) end) - (> (point) (cdar points))) - (push (ace-link--info-current) points) - (Info-next-reference)) - (nreverse points))))) - -;;* `ace-link-help' -;;;###autoload -(defun ace-link-help () - "Open a visible link in a `help-mode' buffer." - (interactive) - (let ((pt (avy-with ace-link-help - (avy--process - (mapcar #'cdr (ace-link--help-collect)) - #'avy--overlay-post)))) - (ace-link--help-action pt))) - -(defun ace-link--help-action (pt) - (when (numberp pt) - (goto-char (1+ pt)) - (push-button))) - -(defun ace-link--help-collect () - "Collect the positions of visible links in the current `help-mode' buffer." - (let ((skip (text-property-any - (window-start) (window-end) 'button nil)) - candidates) - (save-excursion - (while (setq skip (text-property-not-all - skip (window-end) 'button nil)) - (goto-char skip) - (push (cons (button-label (button-at skip)) skip) candidates) - (setq skip (text-property-any (point) (window-end) - 'button nil)))) - (nreverse candidates))) - -;;* `ace-link-woman' -;;;###autoload -(defun ace-link-woman () - "Open a visible link in a `woman-mode' buffer." - (interactive) - (let ((pt (avy-with ace-link-woman - (avy--process - (mapcar #'cdr (ace-link--woman-collect)) - #'avy--overlay-post)))) - (ace-link--woman-action pt))) - -(defun ace-link--woman-action (pt) - (when (number-or-marker-p pt) - (goto-char (1+ pt)) - (push-button))) - -(defun ace-link--woman-collect () - "Collect all links visible in the current `woman-mode' buffer." - (let ((end (window-end)) - candidates) - (save-excursion - (goto-char (window-start)) - (while (and (condition-case nil (forward-button 1) - (error nil)) - (< (point) end)) - (push (cons (button-label (button-at (point))) (point)) - candidates)) - (nreverse candidates)))) - -;;* `ace-link-eww' -;;;###autoload -(defun ace-link-eww () - "Open a visible link in an `eww-mode' buffer." - (interactive) - (let ((pt (avy-with ace-link-eww - (avy--process - (mapcar #'cdr (ace-link--eww-collect)) - #'avy--overlay-post)))) - (ace-link--eww-action pt))) - -(declare-function eww-follow-link "eww") - -(defun ace-link--eww-action (pt) - (when (number-or-marker-p pt) - (goto-char (1+ pt)) - (eww-follow-link))) - -(defun ace-link--eww-collect () - "Collect the positions of visible links in the current `eww' buffer." - (save-excursion - (save-restriction - (narrow-to-region - (window-start) - (window-end)) - (goto-char (point-min)) - (let (beg end candidates) - (setq end - (if (get-text-property (point) 'help-echo) - (point) - (text-property-any - (point) (point-max) 'help-echo nil))) - (while (setq beg (text-property-not-all - end (point-max) 'help-echo nil)) - (goto-char beg) - (setq end (text-property-any - (point) (point-max) 'help-echo nil)) - (push (cons (buffer-substring-no-properties beg end) beg) - candidates)) - (nreverse candidates))))) - -;;* `ace-link-compilation' -;;;###autoload -(defun ace-link-compilation () - "Open a visible link in a `compilation-mode' buffer." - (interactive) - (let ((pt (avy-with ace-link-compilation - (avy--process - (mapcar #'cdr (ace-link--eww-collect)) - #'avy--overlay-post)))) - (ace-link--compilation-action pt))) - -(defun ace-link--compilation-action (pt) - (when (number-or-marker-p pt) - (goto-char (1+ pt)) - (compile-goto-error))) - -(declare-function compile-goto-error "compile") - -;;* `ace-link-gnus' -;;;###autoload -(defun ace-link-gnus () - "Open a visible link in a `gnus-article-mode' buffer." - (interactive) - (when (eq major-mode 'gnus-summary-mode) - (gnus-summary-widget-forward 1)) - (let ((pt (avy-with ace-link-gnus - (avy--process - (ace-link--gnus-collect) - #'avy--overlay-post)))) - (ace-link--gnus-action pt))) - -(defun ace-link--gnus-action (pt) - (when (number-or-marker-p pt) - (goto-char (1+ pt)) - (widget-button-press (point)))) - -(declare-function widget-forward "wid-edit") -(declare-function gnus-summary-widget-forward "gnus-sum") -(declare-function widget-button-press "wid-edit") - -(defun ace-link--gnus-collect () - "Collect the positions of visible links in the current gnus buffer." - (require 'wid-edit) - (let (candidates pt) - (save-excursion - (save-restriction - (narrow-to-region - (window-start) - (window-end)) - (goto-char (point-min)) - (setq pt (point)) - (while (progn (widget-forward 1) - (> (point) pt)) - (setq pt (point)) - (when (or (plist-get (text-properties-at (point)) 'gnus-string) - (plist-get (text-properties-at (point)) 'shr-url)) - (push (point) candidates))) - (nreverse candidates))))) - -;;* `ace-link-org' -;;;###autoload -(defun ace-link-org () - "Open a visible link in an `org-mode' buffer." - (interactive) - (require 'org) - (let ((pt (avy-with ace-link-org - (avy--process - (mapcar #'cdr (ace-link--org-collect)) - #'avy--overlay-pre)))) - (ace-link--org-action pt))) - -(declare-function org-open-at-point "org") -(declare-function outline-invisible-p "outline") -(defvar org-any-link-re) - -(defun ace-link--org-action (pt) - (when (numberp pt) - (goto-char pt) - (org-open-at-point))) - -(defun ace-link--org-collect () - (let ((end (window-end)) - res) - (save-excursion - (goto-char (window-start)) - (while (re-search-forward org-any-link-re end t) - ;; Check that the link is visible. Look at the last character - ;; position in the link ("...X]]") to cover links with and - ;; without a description. - (when (not (outline-invisible-p (- (match-end 0) 3))) - (push - (cons - (buffer-substring-no-properties - (match-beginning 0) - (match-end 0)) - (match-beginning 0)) - res))) - (nreverse res)))) - -;;* `ace-link-custom' -;;;###autoload -(defun ace-link-custom () - "Open a visible link in an `Custom-mode' buffer." - (interactive) - (let ((pt (avy-with ace-link-custom - (avy--process - (ace-link--custom-collect) - #'avy--overlay-pre)))) - (ace-link--custom-action pt))) - -(declare-function Custom-newline "cus-edit") - -(defun ace-link--custom-action (pt) - (when (number-or-marker-p pt) - (goto-char pt) - (Custom-newline (point)))) - -(defun ace-link--custom-collect () - "Collect the positions of visible links in the current `Custom-mode' buffer." - (let (candidates pt) - (save-excursion - (save-restriction - (narrow-to-region - (window-start) - (window-end)) - (goto-char (point-min)) - (setq pt (point)) - (while (progn (widget-forward 1) - (> (point) pt)) - (setq pt (point)) - (when (get-char-property (point) 'button) - (push (point) candidates))))) - (nreverse candidates))) - -;;* `ace-link-addr' -;;;###autoload -(defun ace-link-addr () - "Open a visible link in a goto-address buffer." - (interactive) - (let ((pt (avy-with ace-link-addr - (avy--process - (ace-link--addr-collect) - #'avy--overlay-pre)))) - (ace-link--addr-action pt))) - -(defun ace-link--addr-action (pt) - (when (number-or-marker-p pt) - (goto-char (1+ pt)) - (goto-address-at-point))) - -(defun ace-link--addr-collect () - (let (candidates) - (dolist (overlay (overlays-in (window-start) (window-end))) - (if (overlay-get overlay 'goto-address) - (push (overlay-start overlay) candidates))) - (nreverse candidates))) - -;;* Bindings -(defvar eww-link-keymap) -(defvar eww-mode-map) -(defvar custom-mode-map) - -;;;###autoload -(defun ace-link-setup-default (&optional key) - "Bind KEY to appropriate functions in appropriate keymaps." - (setq key (or key "o")) - (eval-after-load "info" - `(define-key Info-mode-map ,key 'ace-link-info)) - (eval-after-load "compile" - `(define-key compilation-mode-map ,key 'ace-link-compilation)) - (eval-after-load "help-mode" - `(define-key help-mode-map ,key 'ace-link-help)) - (eval-after-load "woman" - `(define-key woman-mode-map ,key 'ace-link-woman)) - (eval-after-load "eww" - `(progn - (define-key eww-link-keymap ,key 'ace-link-eww) - (define-key eww-mode-map ,key 'ace-link-eww))) - (eval-after-load 'cus-edit - `(progn - (define-key custom-mode-map ,key 'ace-link-custom)))) - -(provide 'ace-link) - -;;; ace-link.el ends here diff --git a/.emacs.d/initlibs/avy.el b/.emacs.d/initlibs/avy.el deleted file mode 100644 index fe4fe707..00000000 --- a/.emacs.d/initlibs/avy.el +++ /dev/null @@ -1,1976 +0,0 @@ -;;; 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 <ohwoeowho@gmail.com> -;; URL: https://github.com/abo-abo/avy -;; Version: 0.4.0+git20180322.2d613eb0 -;; Package-Requires: ((emacs "24.1") (cl-lib "0.5")) -;; Keywords: point, location - -;; This file is part of GNU Emacs. - -;; This file is free software; you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation; either version 3, or (at your option) -;; any later version. - -;; This program is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;; GNU General Public License for more details. - -;; For a full copy of the GNU General Public License -;; see <http://www.gnu.org/licenses/>. - -;;; Commentary: -;; -;; With Avy, you can move point to any position in Emacs – even in a -;; different window – using very few keystrokes. For this, you look at -;; the position where you want point to be, invoke Avy, and then enter -;; the sequence of characters displayed at that position. -;; -;; If the position you want to jump to can be determined after only -;; issuing a single keystroke, point is moved to the desired position -;; immediately after that keystroke. In case this isn't possible, the -;; sequence of keystrokes you need to enter is comprised of more than -;; one character. Avy uses a decision tree where each candidate position -;; is a leaf and each edge is described by a character which is distinct -;; per level of the tree. By entering those characters, you navigate the -;; tree, quickly arriving at the desired candidate position, such that -;; Avy can move point to it. -;; -;; Note that this only makes sense for positions you are able to see -;; when invoking Avy. These kinds of positions are supported: -;; -;; * character positions -;; * word or subword start positions -;; * line beginning positions -;; * link positions -;; * window positions -;; -;; If you're familiar with the popular `ace-jump-mode' package, this -;; package does all that and more, without the implementation -;; headache. - -;;; Code: -(require 'cl-lib) -(require 'ring) - -;;* Customization -(defgroup avy nil - "Jump to things tree-style." - :group 'convenience - :prefix "avy-") - -(defcustom avy-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l) - "Default keys for jumping. -Any key is either a character representing a self-inserting -key (letters, digits, punctuation, etc.) or a symbol denoting a -non-printing key like an arrow key (left, right, up, down). For -non-printing keys, a corresponding entry in -`avy-key-to-char-alist' must exist in order to visualize the key -in the avy overlays. - -If `avy-style' is set to words, make sure there are at least three -keys different than the following: a, e, i, o, u, y" - :type '(repeat :tag "Keys" (choice - (character :tag "char") - (symbol :tag "non-printing key")))) - -(defcustom avy-keys-alist nil - "Alist of avy-jump commands to `avy-keys' overriding the default `avy-keys'." - :type '(alist - :key-type (choice :tag "Command" - (const avy-goto-char) - (const avy-goto-char-2) - (const avy-isearch) - (const avy-goto-line) - (const avy-goto-subword-0) - (const avy-goto-subword-1) - (const avy-goto-word-0) - (const avy-goto-word-1) - (const avy-copy-line) - (const avy-copy-region) - (const avy-move-line) - (const avy-move-region) - (const avy-kill-whole-line) - (const avy-kill-region) - (const avy-kill-ring-save-whole-line) - (const avy-kill-ring-save-region) - (function :tag "Other command")) - :value-type (repeat :tag "Keys" character))) - -(defcustom avy-words - '("am" "by" "if" "is" "it" "my" "ox" "up" - "ace" "act" "add" "age" "ago" "aim" "air" "ale" "all" "and" "ant" "any" - "ape" "apt" "arc" "are" "arm" "art" "ash" "ate" "awe" "axe" "bad" "bag" - "ban" "bar" "bat" "bay" "bed" "bee" "beg" "bet" "bid" "big" "bit" "bob" - "bot" "bow" "box" "boy" "but" "cab" "can" "cap" "car" "cat" "cog" "cop" - "cow" "cry" "cup" "cut" "day" "dew" "did" "die" "dig" "dim" "dip" "dog" - "dot" "dry" "dub" "dug" "dye" "ear" "eat" "eel" "egg" "ego" "elf" "eve" - "eye" "fan" "far" "fat" "fax" "fee" "few" "fin" "fit" "fix" "flu" "fly" - "foe" "fog" "for" "fox" "fry" "fun" "fur" "gag" "gap" "gas" "gel" "gem" - "get" "gig" "gin" "gnu" "god" "got" "gum" "gun" "gut" "guy" "gym" "had" - "hag" "ham" "has" "hat" "her" "hid" "him" "hip" "his" "hit" "hop" "hot" - "how" "hub" "hue" "hug" "hut" "ice" "icy" "imp" "ink" "inn" "ion" "ire" - "ivy" "jab" "jam" "jar" "jaw" "jet" "job" "jog" "joy" "key" "kid" "kit" - "lag" "lap" "lay" "let" "lid" "lie" "lip" "lit" "lob" "log" "lot" "low" - "mad" "man" "map" "mat" "may" "men" "met" "mix" "mob" "mop" "mud" "mug" - "nag" "nap" "new" "nil" "nod" "nor" "not" "now" "nun" "oak" "odd" "off" - "oil" "old" "one" "orb" "ore" "ork" "our" "out" "owl" "own" "pad" "pan" - "par" "pat" "paw" "pay" "pea" "pen" "pet" "pig" "pin" "pit" "pod" "pot" - "pry" "pub" "pun" "put" "rag" "ram" "ran" "rat" "raw" "ray" "red" "rib" - "rim" "rip" "rob" "rod" "rot" "row" "rub" "rug" "rum" "run" "sad" "sat" - "saw" "say" "sea" "see" "sew" "she" "shy" "sin" "sip" "sit" "six" "ski" - "sky" "sly" "sob" "son" "soy" "spy" "sum" "sun" "tab" "tad" "tag" "tan" - "tap" "tar" "tax" "tea" "the" "tie" "tin" "tip" "toe" "ton" "too" "top" - "toy" "try" "tub" "two" "urn" "use" "van" "war" "was" "wax" "way" "web" - "wed" "wet" "who" "why" "wig" "win" "wit" "woe" "won" "wry" "you" "zap" - "zip" "zoo") - "Words to use in case `avy-style' is set to `words'. -Every word should contain at least one vowel i.e. one of the following -characters: a, e, i, o, u, y -They do not have to be sorted but no word should be a prefix of another one." - :type '(repeat string)) - -(defcustom avy-style 'at-full - "The default method of displaying the overlays. -Use `avy-styles-alist' to customize this per-command." - :type '(choice - (const :tag "Pre" pre) - (const :tag "At" at) - (const :tag "At Full" at-full) - (const :tag "Post" post) - (const :tag "De Bruijn" de-bruijn) - (const :tag "Words" words))) - -(defcustom avy-styles-alist nil - "Alist of avy-jump commands to the style for each command. -If the commands isn't on the list, `avy-style' is used." - :type '(alist - :key-type (choice :tag "Command" - (const avy-goto-char) - (const avy-goto-char-2) - (const avy-isearch) - (const avy-goto-line) - (const avy-goto-subword-0) - (const avy-goto-subword-1) - (const avy-goto-word-0) - (const avy-goto-word-1) - (const avy-copy-line) - (const avy-copy-region) - (const avy-move-line) - (const avy-move-region) - (const avy-kill-whole-line) - (const avy-kill-region) - (const avy-kill-ring-save-whole-line) - (const avy-kill-ring-save-region) - (function :tag "Other command")) - :value-type (choice - (const :tag "Pre" pre) - (const :tag "At" at) - (const :tag "At Full" at-full) - (const :tag "Post" post) - (const :tag "De Bruijn" de-bruijn) - (const :tag "Words" words)))) - -(defcustom avy-dispatch-alist - '((?x . avy-action-kill-move) - (?X . avy-action-kill-stay) - (?t . avy-action-teleport) - (?m . avy-action-mark) - (?n . avy-action-copy) - (?y . avy-action-yank) - (?i . avy-action-ispell) - (?z . avy-action-zap-to-char)) - "List of actions for `avy-handler-default'. - -Each item is (KEY . ACTION). When KEY not on `avy-keys' is -pressed during the dispatch, ACTION is set to replace the default -`avy-action-goto' once a candidate is finally selected." - :type - '(alist - :key-type (choice (character :tag "Char")) - :value-type (choice - (const :tag "Mark" avy-action-mark) - (const :tag "Copy" avy-action-copy) - (const :tag "Kill and move point" avy-action-kill-move) - (const :tag "Kill" avy-action-kill-stay)))) - -(defcustom avy-background nil - "When non-nil, a gray background will be added during the selection." - :type 'boolean) - -(defcustom avy-all-windows t - "Determine the list of windows to consider in search of candidates." - :type - '(choice - (const :tag "All Frames" all-frames) - (const :tag "This Frame" t) - (const :tag "This Window" nil))) - -(defcustom avy-case-fold-search t - "Non-nil if searches should ignore case." - :type 'boolean) - -(defcustom avy-word-punc-regexp "[!-/:-@[-`{-~]" - "Regexp of punctuation chars that count as word starts for `avy-goto-word-1. -When nil, punctuation chars will not be matched. - -\"[!-/:-@[-`{-~]\" will match all printable punctuation chars." - :type 'regexp) - -(defcustom avy-goto-word-0-regexp "\\b\\sw" - "Regexp that determines positions for `avy-goto-word-0'." - :type '(choice - (const :tag "Default" "\\b\\sw") - (const :tag "Symbol" "\\_<\\(\\sw\\|\\s_\\)") - (const :tag "Not whitespace" "[^ \r\n\t]+") - (regexp :tag "Regex"))) - -(defcustom avy-ignored-modes '(image-mode doc-view-mode pdf-view-mode) - "List of modes to ignore when searching for candidates. -Typically, these modes don't use the text representation." - :type 'list) - -(defvar avy-ring (make-ring 20) - "Hold the window and point history.") - -(defvar avy-translate-char-function #'identity - "Function to translate user input key into another key. -For example, to make SPC do the same as ?a, use -\(lambda (c) (if (= c 32) ?a c)).") - -(defface avy-lead-face-0 - '((t (:foreground "white" :background "#4f57f9"))) - "Face used for first non-terminating leading chars.") - -(defface avy-lead-face-1 - '((t (:foreground "white" :background "gray"))) - "Face used for matched leading chars.") - -(defface avy-lead-face-2 - '((t (:foreground "white" :background "#f86bf3"))) - "Face used for leading chars.") - -(defface avy-lead-face - '((t (:foreground "white" :background "#e52b50"))) - "Face used for the leading chars.") - -(defface avy-background-face - '((t (:foreground "gray40"))) - "Face for whole window background during selection.") - -(defface avy-goto-char-timer-face - '((t (:inherit highlight))) - "Face for matches during reading chars using `avy-goto-char-timer'.") - -(defconst avy-lead-faces '(avy-lead-face - avy-lead-face-0 - avy-lead-face-2 - avy-lead-face - avy-lead-face-0 - avy-lead-face-2) - "Face sequence for `avy--overlay-at-full'.") - -(defvar avy-key-to-char-alist '((left . ?◀) - (right . ?▶) - (up . ?▲) - (down . ?▼) - (prior . ?△) - (next . ?▽)) - "An alist from non-character keys to printable chars used in avy overlays. -This alist must contain all keys used in `avy-keys' which are not -self-inserting keys and thus aren't read as characters.") - -;;* Internals -;;** Tree -(defmacro avy-multipop (lst n) - "Remove LST's first N elements and return them." - `(if (<= (length ,lst) ,n) - (prog1 ,lst - (setq ,lst nil)) - (prog1 ,lst - (setcdr - (nthcdr (1- ,n) (prog1 ,lst (setq ,lst (nthcdr ,n ,lst)))) - nil)))) - -(defun avy--de-bruijn (keys n) - "De Bruijn sequence for alphabet KEYS and subsequences of length N." - (let* ((k (length keys)) - (a (make-list (* n k) 0)) - sequence) - (cl-labels ((db (T p) - (if (> T n) - (if (eq (% n p) 0) - (setq sequence - (append sequence - (cl-subseq a 1 (1+ p))))) - (setf (nth T a) (nth (- T p) a)) - (db (1+ T) p) - (cl-loop for j from (1+ (nth (- T p) a)) to (1- k) do - (setf (nth T a) j) - (db (1+ T) T))))) - (db 1 1) - (mapcar (lambda (n) - (nth n keys)) - sequence)))) - -(defun avy--path-alist-1 (lst seq-len keys) - "Build a De Bruin sequence from LST. -SEQ-LEN is how many elements of KEYS it takes to identify a match." - (let ((db-seq (avy--de-bruijn keys seq-len)) - prev-pos prev-seq prev-win path-alist) - ;; The De Bruijn seq is cyclic, so append the seq-len - 1 first chars to - ;; the end. - (setq db-seq (nconc db-seq (cl-subseq db-seq 0 (1- seq-len)))) - (cl-labels ((subseq-and-pop () - (when (nth (1- seq-len) db-seq) - (prog1 (cl-subseq db-seq 0 seq-len) - (pop db-seq))))) - (while lst - (let* ((cur (car lst)) - (pos (cond - ;; ace-window has matches of the form (pos . wnd) - ((integerp (car cur)) (car cur)) - ;; avy-jump have form ((start . end) . wnd) - ((consp (car cur)) (caar cur)) - (t (error "Unexpected match representation: %s" cur)))) - (win (cdr cur)) - (path (if prev-pos - (let ((diff (if (eq win prev-win) - (- pos prev-pos) - 0))) - (when (and (> diff 0) (< diff seq-len)) - (while (and (nth (1- seq-len) db-seq) - (not - (eq 0 - (cl-search - (cl-subseq prev-seq diff) - (cl-subseq db-seq 0 seq-len))))) - (pop db-seq))) - (subseq-and-pop)) - (subseq-and-pop)))) - (if (not path) - (setq lst nil - path-alist nil) - (push (cons path (car lst)) path-alist) - (setq prev-pos pos - prev-seq path - prev-win win - lst (cdr lst)))))) - (nreverse path-alist))) - -(defun avy-tree (lst keys) - "Coerce LST into a balanced tree. -The degree of the tree is the length of KEYS. -KEYS are placed appropriately on internal nodes." - (let ((len (length keys))) - (cl-labels - ((rd (ls) - (let ((ln (length ls))) - (if (< ln len) - (cl-pairlis keys - (mapcar (lambda (x) (cons 'leaf x)) ls)) - (let ((ks (copy-sequence keys)) - res) - (dolist (s (avy-subdiv ln len)) - (push (cons (pop ks) - (if (eq s 1) - (cons 'leaf (pop ls)) - (rd (avy-multipop ls s)))) - res)) - (nreverse res)))))) - (rd lst)))) - -(defun avy-subdiv (n b) - "Distribute N in B terms in a balanced way." - (let* ((p (1- (floor (+ (log n b) 1e-6)))) - (x1 (expt b p)) - (x2 (* b x1)) - (delta (- n x2)) - (n2 (/ delta (- x2 x1))) - (n1 (- b n2 1))) - (append - (make-list n1 x1) - (list - (- n (* n1 x1) (* n2 x2))) - (make-list n2 x2)))) - -(defun avy-traverse (tree walker &optional recur-key) - "Traverse TREE generated by `avy-tree'. -WALKER is a function that takes KEYS and LEAF. - -RECUR-KEY is used in recursion. - -LEAF is a member of LST argument of `avy-tree'. - -KEYS is the path from the root of `avy-tree' to LEAF." - (dolist (br tree) - (let ((key (cons (car br) recur-key))) - (if (eq (cadr br) 'leaf) - (funcall walker key (cddr br)) - (avy-traverse (cdr br) walker key))))) - -(defvar avy-action nil - "Function to call at the end of select.") - -(defun avy-handler-default (char) - "The default handler for a bad CHAR." - (let (dispatch) - (cond ((setq dispatch (assoc char avy-dispatch-alist)) - (setq avy-action (cdr dispatch)) - (throw 'done 'restart)) - ((memq char '(27 ?\C-g)) - ;; exit silently - (throw 'done 'exit)) - ((mouse-event-p char) - (signal 'user-error (list "Mouse event not handled" char))) - (t - (signal 'user-error (list "No such candidate" - (if (characterp char) (string char) char))) - (throw 'done nil))))) - -(defvar avy-handler-function 'avy-handler-default - "A function to call for a bad `read-key' in `avy-read'.") - -(defvar avy-current-path "" - "Store the current incomplete path during `avy-read'.") - -(defun avy-mouse-event-window (char) - "If CHAR is a mouse event, return the window of the event if any or the selected window. -Return nil if not a mouse event." - (when (mouse-event-p char) - (cond ((windowp (posn-window (event-start char))) - (posn-window (event-start char))) - ((framep (posn-window (event-start char))) - (frame-selected-window (posn-window (event-start char)))) - (t (selected-window))))) - -(defun avy-read (tree display-fn cleanup-fn) - "Select a leaf from TREE using consecutive `read-char'. - -DISPLAY-FN should take CHAR and LEAF and signify that LEAFs -associated with CHAR will be selected if CHAR is pressed. This is -commonly done by adding a CHAR overlay at LEAF position. - -CLEANUP-FN should take no arguments and remove the effects of -multiple DISPLAY-FN invocations." - (catch 'done - (setq avy-current-path "") - (while tree - (let ((avy--leafs nil)) - (avy-traverse tree - (lambda (path leaf) - (push (cons path leaf) avy--leafs))) - (dolist (x avy--leafs) - (funcall display-fn (car x) (cdr x)))) - (let ((char (funcall avy-translate-char-function (read-key))) - window - branch) - (funcall cleanup-fn) - (if (setq window (avy-mouse-event-window char)) - (throw 'done (cons char window)) - ;; Ensure avy-current-path stores the full path prior to - ;; exit so other packages can utilize its value. - (setq avy-current-path - (concat avy-current-path (string (avy--key-to-char char)))) - (if (setq branch (assoc char tree)) - (if (eq (car (setq tree (cdr branch))) 'leaf) - (throw 'done (cdr tree))) - (funcall avy-handler-function char))))))) - -(defun avy-read-de-bruijn (lst keys) - "Select from LST dispatching on KEYS." - ;; In theory, the De Bruijn sequence B(k,n) has k^n subsequences of length n - ;; (the path length) usable as paths, thus that's the lower bound. Due to - ;; partially overlapping matches, not all subsequences may be usable, so it's - ;; possible that the path-len must be incremented, e.g., if we're matching - ;; for x and a buffer contains xaxbxcx only every second subsequence is - ;; usable for the four matches. - (catch 'done - (let* ((path-len (ceiling (log (length lst) (length keys)))) - (alist (avy--path-alist-1 lst path-len keys))) - (while (not alist) - (cl-incf path-len) - (setq alist (avy--path-alist-1 lst path-len keys))) - (let* ((len (length (caar alist))) - (i 0)) - (setq avy-current-path "") - (while (< i len) - (dolist (x (reverse alist)) - (avy--overlay-at-full (reverse (car x)) (cdr x))) - (let ((char (funcall avy-translate-char-function (read-key)))) - (avy--remove-leading-chars) - (setq alist - (delq nil - (mapcar (lambda (x) - (when (eq (caar x) char) - (cons (cdr (car x)) (cdr x)))) - alist))) - (setq avy-current-path - (concat avy-current-path (string (avy--key-to-char char)))) - (cl-incf i) - (unless alist - (funcall avy-handler-function char)))) - (cdar alist))))) - -(defun avy-read-words (lst words) - "Select from LST using WORDS." - (catch 'done - (let ((num-words (length words)) - (num-entries (length lst)) - alist) - ;; If there are not enough words to cover all the candidates, - ;; we use a De Bruijn sequence to generate the remaining ones. - (when (< num-words num-entries) - (let ((keys avy-keys) - (bad-keys '(?a ?e ?i ?o ?u ?y)) - (path-len 1) - (num-remaining (- num-entries num-words)) - tmp-alist) - ;; Delete all keys which could lead to duplicates. - ;; We want at least three keys left to work with. - (dolist (x bad-keys) - (when (memq x keys) - (setq keys (delq ?a keys)))) - (when (< (length keys) 3) - (signal 'user-error - '("Please add more keys to the variable `avy-keys'."))) - ;; Generate the sequence and add the keys to the existing words. - (while (not tmp-alist) - (cl-incf path-len) - (setq tmp-alist (avy--path-alist-1 lst path-len keys))) - (while (>= (cl-decf num-remaining) 0) - (push (mapconcat 'string (caar tmp-alist) nil) (cdr (last words))) - (setq tmp-alist (cdr tmp-alist))))) - (dolist (x lst) - (push (cons (string-to-list (pop words)) x) alist)) - (setq avy-current-path "") - (while (or (> (length alist) 1) - (caar alist)) - (dolist (x (reverse alist)) - (avy--overlay-at-full (reverse (car x)) (cdr x))) - (let ((char (funcall avy-translate-char-function (read-key)))) - (avy--remove-leading-chars) - (setq alist - (delq nil - (mapcar (lambda (x) - (when (eq (caar x) char) - (cons (cdr (car x)) (cdr x)))) - alist))) - (setq avy-current-path - (concat avy-current-path (string (avy--key-to-char char)))) - (unless alist - (funcall avy-handler-function char)))) - (cdar alist)))) - -;;** Rest -(defun avy-window-list () - "Return a list of windows depending on `avy-all-windows'." - (cond ((eq avy-all-windows 'all-frames) - (cl-mapcan #'window-list (frame-list))) - - ((eq avy-all-windows t) - (window-list)) - - ((null avy-all-windows) - (list (selected-window))) - - (t - (error "Unrecognized option: %S" avy-all-windows)))) - -(defcustom avy-all-windows-alt nil - "The alternative `avy-all-windows' for use with \\[universal-argument]." - :type '(choice - (const :tag "Current window" nil) - (const :tag "All windows on the current frame" t) - (const :tag "All windows on all frames" all-frames))) - -(defmacro avy-dowindows (flip &rest body) - "Depending on FLIP and `avy-all-windows' run BODY in each or selected window." - (declare (indent 1) - (debug (form body))) - `(let ((avy-all-windows (if ,flip - avy-all-windows-alt - avy-all-windows))) - (dolist (wnd (avy-window-list)) - (with-selected-window wnd - (unless (memq major-mode avy-ignored-modes) - ,@body))))) - -(defun avy-resume () - "Stub to hold last avy command. -Commands using `avy-with' macro can be resumed." - (interactive)) - -(defvar avy-command nil - "Store the current command symbol. -E.g. 'avy-goto-line or 'avy-goto-char.") - -(defmacro avy-with (command &rest body) - "Set `avy-keys' according to COMMAND and execute BODY. -Set `avy-style' according to COMMMAND as well." - (declare (indent 1) - (debug (form body))) - `(let ((avy-keys (or (cdr (assq ',command avy-keys-alist)) - avy-keys)) - (avy-style (or (cdr (assq ',command avy-styles-alist)) - avy-style)) - (avy-command ',command)) - (setq avy-action nil) - (setf (symbol-function 'avy-resume) - (lambda () - (interactive) - ,@body)) - ,@body)) - -(defun avy-action-goto (pt) - "Goto PT." - (let ((frame (window-frame (selected-window)))) - (unless (equal frame (selected-frame)) - (select-frame-set-input-focus frame) - (raise-frame frame)) - (goto-char pt))) - -(defun avy-forward-item () - (if (eq avy-command 'avy-goto-line) - (end-of-line) - (forward-sexp)) - (point)) - -(defun avy-action-mark (pt) - "Mark sexp at PT." - (goto-char pt) - (set-mark (point)) - (avy-forward-item)) - -(defun avy-action-copy (pt) - "Copy sexp starting on PT." - (save-excursion - (let (str) - (goto-char pt) - (avy-forward-item) - (setq str (buffer-substring pt (point))) - (kill-new str) - (message "Copied: %s" str))) - (let ((dat (ring-ref avy-ring 0))) - (select-frame-set-input-focus - (window-frame (cdr dat))) - (select-window (cdr dat)) - (goto-char (car dat)))) - -(defun avy-action-yank (pt) - "Yank sexp starting at PT at the current point." - (avy-action-copy pt) - (yank) - t) - -(defun avy-action-kill-move (pt) - "Kill sexp at PT and move there." - (goto-char pt) - (avy-forward-item) - (kill-region pt (point)) - (message "Killed: %s" (current-kill 0)) - (point)) - -(defun avy-action-kill-stay (pt) - "Kill sexp at PT." - (save-excursion - (goto-char pt) - (avy-forward-item) - (kill-region pt (point)) - (just-one-space)) - (message "Killed: %s" (current-kill 0)) - (select-window - (cdr - (ring-ref avy-ring 0))) - t) - -(defun avy-action-zap-to-char (pt) - "Kill from point up to PT." - (if (> pt (point)) - (kill-region (point) (1+ pt)) - (kill-region pt (1+ (point))))) - -(defun avy-action-teleport (pt) - "Kill sexp starting on PT and yank into the current location." - (avy-action-kill-stay pt) - (select-window - (cdr - (ring-ref avy-ring 0))) - (save-excursion - (yank)) - t) - -(declare-function flyspell-correct-word-before-point "flyspell") - -(defun avy-action-ispell (pt) - "Auto correct word at PT." - (save-excursion - (goto-char pt) - (cond - ((eq avy-command 'avy-goto-line) - (ispell-region - (line-beginning-position) - (line-end-position))) - ((bound-and-true-p flyspell-mode) - (flyspell-correct-word-before-point)) - ((looking-at-p "\\b") - (ispell-word)) - (t - (progn - (backward-word) - (when (looking-at-p "\\b") - (ispell-word))))))) - -(defun avy--process (candidates overlay-fn) - "Select one of CANDIDATES using `avy-read'. -Use OVERLAY-FN to visualize the decision overlay." - (unless (and (consp (car candidates)) - (windowp (cdar candidates))) - (setq candidates - (mapcar (lambda (x) (cons x (selected-window))) - candidates))) - (let ((len (length candidates)) - (cands (copy-sequence candidates)) - res) - (if (= len 0) - (message "zero candidates") - (if (= len 1) - (setq res (car candidates)) - (unwind-protect - (progn - (avy--make-backgrounds - (avy-window-list)) - (setq res (cond ((eq avy-style 'de-bruijn) - (avy-read-de-bruijn - candidates avy-keys)) - ((eq avy-style 'words) - (avy-read-words - candidates avy-words)) - (t - (avy-read (avy-tree candidates avy-keys) - overlay-fn - #'avy--remove-leading-chars))))) - (avy--done))) - (cond ((eq res 'restart) - (avy--process cands overlay-fn)) - ;; ignore exit from `avy-handler-function' - ((eq res 'exit)) - (t - (avy-push-mark) - (when (and (consp res) - (windowp (cdr res))) - (let* ((window (cdr res)) - (frame (window-frame window))) - (unless (equal frame (selected-frame)) - (select-frame-set-input-focus frame)) - (select-window window)) - (setq res (car res))) - - (funcall (or avy-action 'avy-action-goto) - (if (consp res) - (car res) - res))))))) - -(defvar avy--overlays-back nil - "Hold overlays for when `avy-background' is t.") - -(defun avy--make-backgrounds (wnd-list) - "Create a dim background overlay for each window on WND-LIST." - (when avy-background - (setq avy--overlays-back - (mapcar (lambda (w) - (let ((ol (make-overlay - (window-start w) - (window-end w) - (window-buffer w)))) - (overlay-put ol 'face 'avy-background-face) - (overlay-put ol 'window w) - ol)) - wnd-list)))) - -(defun avy--done () - "Clean up overlays." - (mapc #'delete-overlay avy--overlays-back) - (setq avy--overlays-back nil) - (avy--remove-leading-chars)) - -(defun avy--next-visible-point () - "Return the next closest point without 'invisible property." - (let ((s (point))) - (while (and (not (= (point-max) (setq s (next-char-property-change s)))) - (get-char-property s 'invisible))) - s)) - -(defun avy--next-invisible-point () - "Return the next closest point with 'invisible property." - (let ((s (point))) - (while (and (not (= (point-max) (setq s (next-char-property-change s)))) - (not (get-char-property s 'invisible)))) - s)) - -(defun avy--find-visible-regions (rbeg rend) - "Return a list of all visible regions between RBEG and REND." - (setq rbeg (max rbeg (point-min))) - (setq rend (min rend (point-max))) - (when (< rbeg rend) - (let (visibles beg) - (save-excursion - (save-restriction - (narrow-to-region rbeg rend) - (setq beg (goto-char (point-min))) - (while (not (= (point) (point-max))) - (goto-char (avy--next-invisible-point)) - (push (cons beg (point)) visibles) - (setq beg (goto-char (avy--next-visible-point)))) - (nreverse visibles)))))) - -(defun avy--regex-candidates (regex &optional beg end pred group) - "Return all elements that match REGEX. -Each element of the list is ((BEG . END) . WND) -When PRED is non-nil, it's a filter for matching point positions. -When GROUP is non-nil, (BEG . END) should delimit that regex group." - (setq group (or group 0)) - (let ((case-fold-search (or avy-case-fold-search - (string= regex (downcase regex)))) - candidates) - (avy-dowindows current-prefix-arg - (dolist (pair (avy--find-visible-regions - (or beg (window-start)) - (or end (window-end (selected-window) t)))) - (save-excursion - (goto-char (car pair)) - (while (re-search-forward regex (cdr pair) t) - (unless (get-char-property (1- (point)) 'invisible) - (when (or (null pred) - (funcall pred)) - (push (cons (cons (match-beginning group) - (match-end group)) - wnd) candidates))))))) - (nreverse candidates))) - -(defvar avy--overlay-offset 0 - "The offset to apply in `avy--overlay'.") - -(defvar avy--overlays-lead nil - "Hold overlays for leading chars.") - -(defun avy--remove-leading-chars () - "Remove leading char overlays." - (mapc #'delete-overlay avy--overlays-lead) - (setq avy--overlays-lead nil)) - -(defun avy--old-str (pt wnd) - "Return a one-char string at PT in WND." - (let ((old-str (with-selected-window wnd - (buffer-substring pt (1+ pt))))) - (if avy-background - (propertize old-str 'face 'avy-background-face) - old-str))) - -(defun avy--overlay (str beg end wnd &optional compose-fn) - "Create an overlay with STR from BEG to END in WND. -COMPOSE-FN is a lambda that concatenates the old string at BEG with STR." - (let ((eob (with-selected-window wnd (point-max)))) - (when (<= beg eob) - (let* ((beg (+ beg avy--overlay-offset)) - (ol (make-overlay beg (or end (1+ beg)) (window-buffer wnd))) - (old-str (if (eq beg eob) "" (avy--old-str beg wnd))) - (os-line-prefix (get-text-property 0 'line-prefix old-str)) - (os-wrap-prefix (get-text-property 0 'wrap-prefix old-str)) - other-ol) - (when os-line-prefix - (add-text-properties 0 1 `(line-prefix ,os-line-prefix) str)) - (when os-wrap-prefix - (add-text-properties 0 1 `(wrap-prefix ,os-wrap-prefix) str)) - (when (setq other-ol (cl-find-if - (lambda (o) (overlay-get o 'goto-address)) - (overlays-at beg))) - (add-text-properties - 0 (length old-str) - `(face ,(overlay-get other-ol 'face)) old-str)) - (overlay-put ol 'window wnd) - (overlay-put ol 'category 'avy) - (overlay-put ol (if (eq beg eob) - 'after-string - 'display) - (funcall - (or compose-fn #'concat) - str old-str)) - (push ol avy--overlays-lead))))) - -(defcustom avy-highlight-first nil - "When non-nil highlight the first decision char with `avy-lead-face-0'. -Do this even when the char is terminating." - :type 'boolean) - -(defun avy--key-to-char (c) - "If C is no character, translate it using `avy-key-to-char-alist'." - (cond ((characterp c) c) - ((cdr (assoc c avy-key-to-char-alist))) - ((mouse-event-p c) c) - (t - (error "Unknown key %s" c)))) - -(defun avy-candidate-beg (leaf) - "Return the start position for LEAF." - (cond ((numberp leaf) - leaf) - ((consp (car leaf)) - (caar leaf)) - (t - (car leaf)))) - -(defun avy-candidate-end (leaf) - "Return the end position for LEAF." - (cond ((numberp leaf) - leaf) - ((consp (car leaf)) - (cdar leaf)) - (t - (car leaf)))) - -(defun avy-candidate-wnd (leaf) - "Return the window for LEAF." - (if (consp leaf) - (cdr leaf) - (selected-window))) - -(defun avy--overlay-pre (path leaf) - "Create an overlay with PATH at LEAF. -PATH is a list of keys from tree root to LEAF. -LEAF is normally ((BEG . END) . WND)." - (let* ((path (mapcar #'avy--key-to-char path)) - (str (propertize (apply #'string (reverse path)) - 'face 'avy-lead-face))) - (when (or avy-highlight-first (> (length str) 1)) - (set-text-properties 0 1 '(face avy-lead-face-0) str)) - (setq str (concat - (propertize avy-current-path - 'face 'avy-lead-face-1) - str)) - (avy--overlay - str - (avy-candidate-beg leaf) nil - (avy-candidate-wnd leaf)))) - -(defun avy--overlay-at (path leaf) - "Create an overlay with PATH at LEAF. -PATH is a list of keys from tree root to LEAF. -LEAF is normally ((BEG . END) . WND)." - (let* ((path (mapcar #'avy--key-to-char path)) - (str (propertize - (string (car (last path))) - 'face 'avy-lead-face))) - (avy--overlay - str - (avy-candidate-beg leaf) nil - (avy-candidate-wnd leaf) - (lambda (str old-str) - (cond ((string= old-str "\n") - (concat str "\n")) - ;; add padding for wide-width character - ((eq (string-width old-str) 2) - (concat str " ")) - (t - str)))))) - -(defun avy--overlay-at-full (path leaf) - "Create an overlay with PATH at LEAF. -PATH is a list of keys from tree root to LEAF. -LEAF is normally ((BEG . END) . WND)." - (let* ((path (mapcar #'avy--key-to-char path)) - (str (propertize - (apply #'string (reverse path)) - 'face 'avy-lead-face)) - (len (length path)) - (beg (avy-candidate-beg leaf)) - (wnd (cdr leaf)) - end) - (dotimes (i len) - (set-text-properties (- len i 1) (- len i) - `(face ,(nth i avy-lead-faces)) - str)) - (when (eq avy-style 'de-bruijn) - (setq str (concat - (propertize avy-current-path - 'face 'avy-lead-face-1) - str)) - (setq len (length str))) - (with-selected-window wnd - (save-excursion - (goto-char beg) - (let* ((lep (if (bound-and-true-p visual-line-mode) - (save-excursion - (end-of-visual-line) - (point)) - (line-end-position))) - ;; `end-of-visual-line' is bugged sometimes - (lep (if (< lep beg) - (line-end-position) - lep)) - (len-and-str (avy--update-offset-and-str len str lep))) - (setq len (car len-and-str)) - (setq str (cdr len-and-str)) - (setq end (if (= beg lep) - (1+ beg) - (min (+ beg - (if (eq (char-after) ?\t) - 1 - len)) - lep))) - (when (and (bound-and-true-p visual-line-mode) - (> len (- end beg)) - (not (eq lep beg))) - (setq len (- end beg)) - (let ((old-str (apply #'string (reverse path)))) - (setq str - (substring - (propertize - old-str - 'face - (if (= (length old-str) 1) - 'avy-lead-face - 'avy-lead-face-0)) - 0 len))))))) - (avy--overlay - str beg end wnd - (lambda (str old-str) - (cond ((string= old-str "\n") - (concat str "\n")) - ((string= old-str "\t") - (concat str (make-string (max (- tab-width len) 0) ?\ ))) - (t - ;; add padding for wide-width character - (if (eq (string-width old-str) 2) - (concat str " ") - str))))))) - -(defun avy--overlay-post (path leaf) - "Create an overlay with PATH at LEAF. -PATH is a list of keys from tree root to LEAF. -LEAF is normally ((BEG . END) . WND)." - (let* ((path (mapcar #'avy--key-to-char path)) - (str (propertize (apply #'string (reverse path)) - 'face 'avy-lead-face))) - (when (or avy-highlight-first (> (length str) 1)) - (set-text-properties 0 1 '(face avy-lead-face-0) str)) - (setq str (concat - (propertize avy-current-path - 'face 'avy-lead-face-1) - str)) - (avy--overlay - str - (avy-candidate-end leaf) nil - (avy-candidate-wnd leaf)))) - -(defun avy--update-offset-and-str (offset str lep) - "Recalculate the length of the new overlay at point. - -OFFSET is the previous overlay length. -STR is the overlay string that we wish to add. -LEP is the line end position. - -We want to add an overlay between point and END=point+OFFSET. -When other overlays already exist between point and END, set -OFFSET to be the difference between the start of the first -overlay and point. This is equivalent to truncating our new -overlay, so that it doesn't intersect with overlays that already -exist." - (let* ((wnd (selected-window)) - (beg (point)) - (oov (delq nil - (mapcar - (lambda (o) - (and (eq (overlay-get o 'category) 'avy) - (eq (overlay-get o 'window) wnd) - (overlay-start o))) - (overlays-in beg (min (+ beg offset) lep)))))) - (when oov - (setq offset (- (apply #'min oov) beg)) - (setq str (substring str 0 offset))) - (let ((other-ov (cl-find-if - (lambda (o) - (and (eq (overlay-get o 'category) 'avy) - (eq (overlay-start o) beg) - (not (eq (overlay-get o 'window) wnd)))) - (overlays-in (point) (min (+ (point) offset) lep))))) - (when (and other-ov - (> (overlay-end other-ov) - (+ beg offset))) - (setq str (concat str (buffer-substring - (+ beg offset) - (overlay-end other-ov)))) - (setq offset (- (overlay-end other-ov) - beg)))) - (cons offset str))) - -(defun avy--style-fn (style) - "Transform STYLE symbol to a style function." - (cl-case style - (pre #'avy--overlay-pre) - (at #'avy--overlay-at) - (at-full 'avy--overlay-at-full) - (post #'avy--overlay-post) - (de-bruijn #'avy--overlay-at-full) - (words #'avy--overlay-at-full) - (t (error "Unexpected style %S" style)))) - -(defun avy--generic-jump (regex window-flip style &optional beg end) - "Jump to REGEX. -The window scope is determined by `avy-all-windows'. -When WINDOW-FLIP is non-nil, do the opposite of `avy-all-windows'. -STYLE determines the leading char overlay style. -BEG and END narrow the scope where candidates are searched." - (let ((avy-all-windows - (if window-flip - (not avy-all-windows) - avy-all-windows))) - (avy--process - (avy--regex-candidates regex beg end) - (avy--style-fn style)))) - -;;* Commands -;;;###autoload -(defun avy-goto-char (char &optional arg) - "Jump to the currently visible CHAR. -The window scope is determined by `avy-all-windows' (ARG negates it)." - (interactive (list (read-char "char: " t) - current-prefix-arg)) - (avy-with avy-goto-char - (avy--generic-jump - (if (= 13 char) - "\n" - (regexp-quote (string char))) - arg - avy-style))) - -;;;###autoload -(defun avy-goto-char-in-line (char) - "Jump to the currently visible CHAR in the current line." - (interactive (list (read-char "char: " t))) - (avy-with avy-goto-char - (avy--generic-jump - (regexp-quote (string char)) - avy-all-windows - avy-style - (line-beginning-position) - (line-end-position)))) - -;;;###autoload -(defun avy-goto-char-2 (char1 char2 &optional arg beg end) - "Jump to the currently visible CHAR1 followed by CHAR2. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'. -BEG and END narrow the scope where candidates are searched." - (interactive (list (read-char "char 1: " t) - (read-char "char 2: " t) - current-prefix-arg - nil nil)) - (when (eq char1 ?
) - (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 - beg end))) - -;;;###autoload -(defun avy-goto-char-2-above (char1 char2 &optional arg) - "Jump to the currently visible CHAR1 followed by CHAR2. -This is a scoped version of `avy-goto-char-2', where the scope is -the visible part of the current buffer up to point. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive (list (read-char "char 1: " t) - (read-char "char 2: " t) - current-prefix-arg)) - (avy-with avy-goto-char-2-above - (avy-goto-char-2 - char1 char2 arg - (window-start) (point)))) - -;;;###autoload -(defun avy-goto-char-2-below (char1 char2 &optional arg) - "Jump to the currently visible CHAR1 followed by CHAR2. -This is a scoped version of `avy-goto-char-2', where the scope is -the visible part of the current buffer following point. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive (list (read-char "char 1: " t) - (read-char "char 2: " t) - current-prefix-arg)) - (avy-with avy-goto-char-2-below - (avy-goto-char-2 - char1 char2 arg - (point) (window-end (selected-window) t)))) - -;;;###autoload -(defun avy-isearch () - "Jump to one of the current isearch candidates." - (interactive) - (avy-with avy-isearch - (let ((avy-background nil)) - (avy--process - (avy--regex-candidates (if isearch-regexp - isearch-string - (regexp-quote isearch-string))) - (avy--style-fn avy-style)) - (isearch-done)))) - -;;;###autoload -(defun avy-goto-word-0 (arg &optional beg end) - "Jump to a word start. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'. -BEG and END narrow the scope where candidates are searched." - (interactive "P") - (avy-with avy-goto-word-0 - (avy--generic-jump avy-goto-word-0-regexp arg avy-style beg end))) - -(defun avy-goto-word-0-above (arg) - "Jump to a word start between window start and point. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive "P") - (avy-with avy-goto-word-0 - (avy-goto-word-0 arg (window-start) (point)))) - -(defun avy-goto-word-0-below (arg) - "Jump to a word start between point and window end. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive "P") - (avy-with avy-goto-word-0 - (avy-goto-word-0 arg (point) (window-end (selected-window) t)))) - -;;;###autoload -(defun avy-goto-word-1 (char &optional arg beg end symbol) - "Jump to the currently visible CHAR at a word start. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'. -BEG and END narrow the scope where candidates are searched. -When SYMBOL is non-nil, jump to symbol start instead of word start." - (interactive (list (read-char "char: " t) - current-prefix-arg)) - (avy-with avy-goto-word-1 - (let* ((str (string char)) - (regex (cond ((string= str ".") - "\\.") - ((and avy-word-punc-regexp - (string-match avy-word-punc-regexp str)) - (regexp-quote str)) - ((<= char 26) - str) - (t - (concat - (if symbol "\\_<" "\\b") - str))))) - (avy--generic-jump regex arg avy-style beg end)))) - -;;;###autoload -(defun avy-goto-word-1-above (char &optional arg) - "Jump to the currently visible CHAR at a word start. -This is a scoped version of `avy-goto-word-1', where the scope is -the visible part of the current buffer up to point. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive (list (read-char "char: " t) - current-prefix-arg)) - (avy-with avy-goto-word-1 - (avy-goto-word-1 char arg (window-start) (point)))) - -;;;###autoload -(defun avy-goto-word-1-below (char &optional arg) - "Jump to the currently visible CHAR at a word start. -This is a scoped version of `avy-goto-word-1', where the scope is -the visible part of the current buffer following point. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive (list (read-char "char: " t) - current-prefix-arg)) - (avy-with avy-goto-word-1 - (avy-goto-word-1 char arg (point) (window-end (selected-window) t)))) - -;;;###autoload -(defun avy-goto-symbol-1 (char &optional arg) - "Jump to the currently visible CHAR at a symbol start. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive (list (read-char "char: " t) - current-prefix-arg)) - (avy-with avy-goto-symbol-1 - (avy-goto-word-1 char arg nil nil t))) - -;;;###autoload -(defun avy-goto-symbol-1-above (char &optional arg) - "Jump to the currently visible CHAR at a symbol start. -This is a scoped version of `avy-goto-symbol-1', where the scope is -the visible part of the current buffer up to point. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive (list (read-char "char: " t) - current-prefix-arg)) - (avy-with avy-goto-symbol-1-above - (avy-goto-word-1 char arg (window-start) (point) t))) - -;;;###autoload -(defun avy-goto-symbol-1-below (char &optional arg) - "Jump to the currently visible CHAR at a symbol start. -This is a scoped version of `avy-goto-symbol-1', where the scope is -the visible part of the current buffer following point. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive (list (read-char "char: " t) - current-prefix-arg)) - (avy-with avy-goto-symbol-1-below - (avy-goto-word-1 char arg (point) (window-end (selected-window) t) t))) - -(declare-function subword-backward "subword") -(defvar subword-backward-regexp) - -(defcustom avy-subword-extra-word-chars '(?{ ?= ?} ?* ?: ?> ?<) - "A list of characters that should temporarily match \"\\w\". -This variable is used by `avy-goto-subword-0' and `avy-goto-subword-1'." - :type '(repeat character)) - -;;;###autoload -(defun avy-goto-subword-0 (&optional arg predicate) - "Jump to a word or subword start. - -The window scope is determined by `avy-all-windows' (ARG negates it). - -When PREDICATE is non-nil it's a function of zero parameters that -should return true." - (interactive "P") - (require 'subword) - (avy-with avy-goto-subword-0 - (let ((case-fold-search nil) - (subword-backward-regexp - "\\(\\(\\W\\|[[:lower:][:digit:]]\\)\\([!-/:@`~[:upper:]]+\\W*\\)\\|\\W\\w+\\)") - candidates) - (avy-dowindows arg - (let ((syn-tbl (copy-syntax-table))) - (dolist (char avy-subword-extra-word-chars) - (modify-syntax-entry char "w" syn-tbl)) - (with-syntax-table syn-tbl - (let ((ws (window-start)) - window-cands) - (save-excursion - (goto-char (window-end (selected-window) t)) - (subword-backward) - (while (> (point) ws) - (when (or (null predicate) - (and predicate (funcall predicate))) - (unless (get-char-property (point) 'invisible) - (push (cons (point) (selected-window)) window-cands))) - (subword-backward)) - (and (= (point) ws) - (or (null predicate) - (and predicate (funcall predicate))) - (not (get-char-property (point) 'invisible)) - (push (cons (point) (selected-window)) window-cands))) - (setq candidates (nconc candidates window-cands)))))) - (avy--process candidates (avy--style-fn avy-style))))) - -;;;###autoload -(defun avy-goto-subword-1 (char &optional arg) - "Jump to the currently visible CHAR at a subword start. -The window scope is determined by `avy-all-windows' (ARG negates it). -The case of CHAR is ignored." - (interactive (list (read-char "char: " t) - current-prefix-arg)) - (avy-with avy-goto-subword-1 - (let ((char (downcase char))) - (avy-goto-subword-0 - arg (lambda () - (and (char-after) - (eq (downcase (char-after)) char))))))) - -;;;###autoload -(defun avy-goto-word-or-subword-1 () - "Forward to `avy-goto-subword-1' or `avy-goto-word-1'. -Which one depends on variable `subword-mode'." - (interactive) - (if (bound-and-true-p subword-mode) - (call-interactively #'avy-goto-subword-1) - (call-interactively #'avy-goto-word-1))) - -(defvar visual-line-mode) - -(defun avy--line-cands (&optional arg beg end) - "Get candidates for selecting a line. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'. -BEG and END narrow the scope where candidates are searched." - (let (candidates) - (avy-dowindows arg - (let ((ws (or beg (window-start)))) - (save-excursion - (save-restriction - (narrow-to-region ws (or end (window-end (selected-window) t))) - (goto-char (point-min)) - (while (< (point) (point-max)) - (unless (get-char-property - (max (1- (point)) ws) 'invisible) - (push (cons - (if (eq avy-style 'post) - (line-end-position) - (point)) - (selected-window)) candidates)) - (if visual-line-mode - (progn - (setq temporary-goal-column 0) - (line-move-visual 1 t)) - (forward-line 1))))))) - (nreverse candidates))) - -(defun avy--linum-strings () - "Get strings for `avy-linum-mode'." - (let* ((lines (mapcar #'car (avy--line-cands))) - (line-tree (avy-tree lines avy-keys)) - (line-list nil)) - (avy-traverse - line-tree - (lambda (path _leaf) - (let ((str (propertize (apply #'string (reverse path)) - 'face 'avy-lead-face))) - (when (> (length str) 1) - (set-text-properties 0 1 '(face avy-lead-face-0) str)) - (push str line-list)))) - (nreverse line-list))) - -(defvar linum-available) -(defvar linum-overlays) -(defvar linum-format) -(declare-function linum--face-width "linum") - -(define-minor-mode avy-linum-mode - "Minor mode that uses avy hints for `linum-mode'." - :group 'avy - (if avy-linum-mode - (progn - (require 'linum) - (advice-add 'linum-update-window :around 'avy--linum-update-window) - (linum-mode 1)) - (advice-remove 'linum-update-window 'avy--linum-update-window) - (linum-mode -1))) - -(defun avy--linum-update-window (_ win) - "Update line numbers for the portion visible in window WIN." - (goto-char (window-start win)) - (let ((line (line-number-at-pos)) - (limit (window-end win t)) - (fmt (cond ((stringp linum-format) linum-format) - ((eq linum-format 'dynamic) - (let ((w (length (number-to-string - (count-lines (point-min) (point-max)))))) - (concat "%" (number-to-string w) "d"))))) - (width 0) - (avy-strs (when avy-linum-mode - (avy--linum-strings)))) - (run-hooks 'linum-before-numbering-hook) - ;; Create an overlay (or reuse an existing one) for each - ;; line visible in this window, if necessary. - (while (and (not (eobp)) (< (point) limit)) - (let* ((str - (cond (avy-linum-mode - (pop avy-strs)) - (fmt - (propertize (format fmt line) 'face 'linum)) - (t - (funcall linum-format line)))) - (visited (catch 'visited - (dolist (o (overlays-in (point) (point))) - (when (equal-including-properties - (overlay-get o 'linum-str) str) - (unless (memq o linum-overlays) - (push o linum-overlays)) - (setq linum-available (delq o linum-available)) - (throw 'visited t)))))) - (setq width (max width (length str))) - (unless visited - (let ((ov (if (null linum-available) - (make-overlay (point) (point)) - (move-overlay (pop linum-available) (point) (point))))) - (push ov linum-overlays) - (overlay-put ov 'before-string - (propertize " " 'display `((margin left-margin) ,str))) - (overlay-put ov 'linum-str str)))) - ;; Text may contain those nasty intangible properties, but that - ;; shouldn't prevent us from counting those lines. - (let ((inhibit-point-motion-hooks t)) - (forward-line)) - (setq line (1+ line))) - (when (display-graphic-p) - (setq width (ceiling - (/ (* width 1.0 (linum--face-width 'linum)) - (frame-char-width))))) - (set-window-margins win width (cdr (window-margins win))))) - -(defun avy--line (&optional arg beg end) - "Select a line. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'. -BEG and END narrow the scope where candidates are searched." - (let ((avy-action #'identity)) - (avy--process - (avy--line-cands arg beg end) - (if avy-linum-mode - (progn (message "Goto line:") - 'ignore) - (avy--style-fn avy-style))))) - -;;;###autoload -(defun avy-goto-line (&optional arg) - "Jump to a line start in current buffer. - -When ARG is 1, jump to lines currently visible, with the option -to cancel to `goto-line' by entering a number. - -When ARG is 4, negate the window scope determined by -`avy-all-windows'. - -Otherwise, forward to `goto-line' with ARG." - (interactive "p") - (setq arg (or arg 1)) - (if (not (memq arg '(1 4))) - (progn - (goto-char (point-min)) - (forward-line (1- arg))) - (avy-with avy-goto-line - (let* ((avy-handler-old avy-handler-function) - (avy-handler-function - (lambda (char) - (if (or (< char ?0) - (> char ?9)) - (funcall avy-handler-old char) - (let ((line (read-from-minibuffer - "Goto line: " (string char)))) - (when line - (avy-push-mark) - (save-restriction - (widen) - (goto-char (point-min)) - (forward-line (1- (string-to-number line)))) - (throw 'done 'exit)))))) - (r (avy--line (eq arg 4)))) - (unless (eq r t) - (avy-action-goto r)))))) - -;;;###autoload -(defun avy-goto-line-above () - "Goto visible line above the cursor." - (interactive) - (let* ((avy-all-windows nil) - (r (avy--line nil (window-start) - (line-beginning-position)))) - (unless (eq r t) - (avy-action-goto r)))) - -;;;###autoload -(defun avy-goto-line-below () - "Goto visible line below the cursor." - (interactive) - (let* ((avy-all-windows nil) - (r (avy--line - nil (line-beginning-position 2) - (window-end (selected-window) t)))) - (unless (eq r t) - (avy-action-goto r)))) - -(defcustom avy-line-insert-style 'above - "How to insert the newly copied/cut line." - :type '(choice - (const :tag "Above" above) - (const :tag "Below" below))) - -;;;###autoload -(defun avy-copy-line (arg) - "Copy a selected line above the current line. -ARG lines can be used." - (interactive "p") - (let ((initial-window (selected-window))) - (avy-with avy-copy-line - (let* ((start (avy--line)) - (str (buffer-substring-no-properties - start - (save-excursion - (goto-char start) - (move-end-of-line arg) - (point))))) - (select-window initial-window) - (cond ((eq avy-line-insert-style 'above) - (beginning-of-line) - (save-excursion - (insert str "\n"))) - ((eq avy-line-insert-style 'below) - (end-of-line) - (insert "\n" str) - (beginning-of-line)) - (t - (user-error "Unexpected `avy-line-insert-style'"))))))) - -;;;###autoload -(defun avy-move-line (arg) - "Move a selected line above the current line. -ARG lines can be used." - (interactive "p") - (let ((initial-window (selected-window))) - (avy-with avy-move-line - (let ((start (avy--line))) - (save-excursion - (goto-char start) - (kill-whole-line arg)) - (select-window initial-window) - (cond ((eq avy-line-insert-style 'above) - (beginning-of-line) - (save-excursion - (insert - (current-kill 0)))) - ((eq avy-line-insert-style 'below) - (end-of-line) - (newline) - (save-excursion - (insert (substring (current-kill 0) 0 -1)))) - (t - (user-error "Unexpected `avy-line-insert-style'"))))))) - -;;;###autoload -(defun avy-copy-region (arg) - "Select two lines and copy the text between them to point. - -The window scope is determined by `avy-all-windows' or -`avy-all-windows-alt' when ARG is non-nil." - (interactive "P") - (let ((initial-window (selected-window))) - (avy-with avy-copy-region - (let* ((beg (save-selected-window - (avy--line arg))) - (end (avy--line arg)) - (str (buffer-substring-no-properties - beg - (save-excursion - (goto-char end) - (line-end-position))))) - (select-window initial-window) - (cond ((eq avy-line-insert-style 'above) - (beginning-of-line) - (save-excursion - (insert str "\n"))) - ((eq avy-line-insert-style 'below) - (end-of-line) - (newline) - (save-excursion - (insert str))) - (t - (user-error "Unexpected `avy-line-insert-style'"))))))) - -;;;###autoload -(defun avy-move-region () - "Select two lines and move the text between them above the current line." - (interactive) - (avy-with avy-move-region - (let* ((initial-window (selected-window)) - (beg (avy--line)) - (end (avy--line)) - text) - (when (> beg end) - (cl-rotatef beg end)) - (setq end (save-excursion - (goto-char end) - (1+ (line-end-position)))) - (setq text (buffer-substring beg end)) - (move-beginning-of-line nil) - (delete-region beg end) - (select-window initial-window) - (insert text)))) - -;;;###autoload -(defun avy-kill-region (arg) - "Select two lines and kill the region between them. - -The window scope is determined by `avy-all-windows' or -`avy-all-windows-alt' when ARG is non-nil." - (interactive "P") - (let ((initial-window (selected-window))) - (avy-with avy-kill-region - (let* ((beg (save-selected-window - (list (avy--line arg) (selected-window)))) - (end (list (avy--line arg) (selected-window)))) - (cond - ((not (numberp (car beg))) - (user-error "Fail to select the beginning of region")) - ((not (numberp (car end))) - (user-error "Fail to select the end of region")) - ;; Restrict operation to same window. It's better if it can be - ;; different windows but same buffer; however, then the cloned - ;; buffers with different narrowed regions might cause problem. - ((not (equal (cdr beg) (cdr end))) - (user-error "Selected points are not in the same window")) - ((< (car beg) (car end)) - (save-excursion - (kill-region - (car beg) - (progn (goto-char (car end)) (forward-visible-line 1) (point))))) - (t - (save-excursion - (kill-region - (progn (goto-char (car beg)) (forward-visible-line 1) (point)) - (car end))))))) - (select-window initial-window))) - -;;;###autoload -(defun avy-kill-ring-save-region (arg) - "Select two lines and save the region between them to the kill ring. -The window scope is determined by `avy-all-windows'. -When ARG is non-nil, do the opposite of `avy-all-windows'." - (interactive "P") - (let ((initial-window (selected-window))) - (avy-with avy-kill-ring-save-region - (let* ((beg (save-selected-window - (list (avy--line arg) (selected-window)))) - (end (list (avy--line arg) (selected-window)))) - (cond - ((not (numberp (car beg))) - (user-error "Fail to select the beginning of region")) - ((not (numberp (car end))) - (user-error "Fail to select the end of region")) - ((not (equal (cdr beg) (cdr end))) - (user-error "Selected points are not in the same window")) - ((< (car beg) (car end)) - (save-excursion - (kill-ring-save - (car beg) - (progn (goto-char (car end)) (forward-visible-line 1) (point))))) - (t - (save-excursion - (kill-ring-save - (progn (goto-char (car beg)) (forward-visible-line 1) (point)) - (car end))))))) - (select-window initial-window))) - -;;;###autoload -(defun avy-kill-whole-line (arg) - "Select line and kill the whole selected line. - -With a numerical prefix ARG, kill ARG line(s) starting from the -selected line. If ARG is negative, kill backward. - -If ARG is zero, kill the selected line but exclude the trailing -newline. - -\\[universal-argument] 3 \\[avy-kil-whole-line] kill three lines -starting from the selected line. \\[universal-argument] -3 - -\\[avy-kill-whole-line] kill three lines backward including the -selected line." - (interactive "P") - (let ((initial-window (selected-window))) - (avy-with avy-kill-whole-line - (let* ((start (avy--line))) - (if (not (numberp start)) - (user-error "Fail to select the line to kill") - (save-excursion (goto-char start) - (kill-whole-line arg))))) - (select-window initial-window))) - -;;;###autoload -(defun avy-kill-ring-save-whole-line (arg) - "Select line and save the whole selected line as if killed, but don’t kill it. - -This command is similar to `avy-kill-whole-line', except that it -saves the line(s) as if killed, but does not kill it(them). - -With a numerical prefix ARG, kill ARG line(s) starting from the -selected line. If ARG is negative, kill backward. - -If ARG is zero, kill the selected line but exclude the trailing -newline." - (interactive "P") - (let ((initial-window (selected-window))) - (avy-with avy-kill-ring-save-whole-line - (let* ((start (avy--line))) - (if (not (numberp start)) - (user-error "Fail to select the line to kill") - (save-excursion - (let ((kill-read-only-ok t) - (buffer-read-only t)) - (goto-char start) - (kill-whole-line arg)))))) - (select-window initial-window))) - -;;;###autoload -(defun avy-setup-default () - "Setup the default shortcuts." - (eval-after-load "isearch" - '(define-key isearch-mode-map (kbd "C-'") 'avy-isearch))) - -(defcustom avy-timeout-seconds 0.5 - "How many seconds to wait for the second char." - :type 'float) - -(defcustom avy-enter-times-out t - "Whether enter exits avy-goto-char-timer early. If nil it matches newline" - :type 'boolean) - -(defun avy--read-candidates (&optional re-builder) - "Read as many chars as possible and return their occurrences. -At least one char must be read, and then repeatedly one next char -may be read if it is entered before `avy-timeout-seconds'. `C-h' -or `DEL' deletes the last char entered, and `RET' exits with the -currently read string immediately instead of waiting for another -char for `avy-timeout-seconds'. -The format of the result is the same as that of `avy--regex-candidates'. -This function obeys `avy-all-windows' setting. -RE-BUILDER is a function that takes a string and returns a regex. -When nil, `regexp-quote' is used. -If a group is captured, the first group is highlighted. -Otherwise, the whole regex is highlighted." - (let ((str "") - (re-builder (or re-builder #'regexp-quote)) - char break overlays regex) - (unwind-protect - (progn - (while (and (not break) - (setq char - (read-char (format "char%s: " - (if (string= str "") - str - (format " (%s)" str))) - t - (and (not (string= str "")) - avy-timeout-seconds)))) - ;; Unhighlight - (dolist (ov overlays) - (delete-overlay ov)) - (setq overlays nil) - (cond - ;; Handle RET - ((= char 13) - (if avy-enter-times-out - (setq break t) - (setq str (concat str (list ?\n))))) - ;; Handle C-h, DEL - ((memq char '(8 127)) - (let ((l (length str))) - (when (>= l 1) - (setq str (substring str 0 (1- l)))))) - (t - (setq str (concat str (list char))))) - ;; Highlight - (when (>= (length str) 1) - (let ((case-fold-search - (or avy-case-fold-search (string= str (downcase str)))) - found) - (avy-dowindows current-prefix-arg - (dolist (pair (avy--find-visible-regions - (window-start) - (window-end (selected-window) t))) - (save-excursion - (goto-char (car pair)) - (setq regex (funcall re-builder str)) - (while (re-search-forward regex (cdr pair) t) - (unless (get-char-property (1- (point)) 'invisible) - (let* ((idx (if (= (length (match-data)) 4) 1 0)) - (ov (make-overlay - (match-beginning idx) (match-end idx)))) - (setq found t) - (push ov overlays) - (overlay-put - ov 'window (selected-window)) - (overlay-put - ov 'face 'avy-goto-char-timer-face))))))) - ;; No matches at all, so there's surely a typo in the input. - (unless found (beep))))) - (nreverse (mapcar (lambda (ov) - (cons (cons (overlay-start ov) - (overlay-end ov)) - (overlay-get ov 'window))) - overlays))) - (dolist (ov overlays) - (delete-overlay ov))))) - -;;;###autoload -(defun avy-goto-char-timer (&optional arg) - "Read one or many consecutive chars and jump to the first one. -The window scope is determined by `avy-all-windows' (ARG negates it)." - (interactive "P") - (let ((avy-all-windows (if arg - (not avy-all-windows) - avy-all-windows))) - (avy-with avy-goto-char-timer - (avy--process - (avy--read-candidates) - (avy--style-fn avy-style))))) - -(defun avy-push-mark () - "Store the current point and window." - (ring-insert avy-ring - (cons (point) (selected-window))) - (unless (region-active-p) - (push-mark))) - -(defun avy-pop-mark () - "Jump back to the last location of `avy-push-mark'." - (interactive) - (let (res) - (condition-case nil - (progn - (while (not (window-live-p - (cdr (setq res (ring-remove avy-ring 0)))))) - (let* ((window (cdr res)) - (frame (window-frame window))) - (when (and (frame-live-p frame) - (not (eq frame (selected-frame)))) - (select-frame-set-input-focus frame)) - (select-window window) - (goto-char (car res)))) - (error - (set-mark-command 4))))) - -;; ** Org-mode -(defvar org-reverse-note-order) -(declare-function org-refile "org") -(declare-function org-back-to-heading "org") - -(defun avy-org-refile-as-child () - "Refile current heading as first child of heading selected with `avy.'" - ;; Inspired by `org-teleport': http://kitchingroup.cheme.cmu.edu/blog/2016/03/18/Org-teleport-headlines/ - (interactive) - (let ((rfloc (save-excursion - (let* ((org-reverse-note-order t) - (pos (avy-with avy-goto-line - (avy--generic-jump (rx bol (1+ "*") (1+ space)) - nil avy-style) - (point))) - (filename (buffer-file-name (or (buffer-base-buffer (current-buffer)) - (current-buffer))))) - (list nil filename nil pos))))) - ;; org-refile must be called outside of the excursion - (org-refile nil nil rfloc))) - -(defun avy-org-goto-heading-timer (&optional arg) - "Read one or many characters and jump to matching Org headings. -The window scope is determined by `avy-all-windows' (ARG negates it)." - (interactive "P") - (let ((avy-all-windows (if arg - (not avy-all-windows) - avy-all-windows))) - (avy-with avy-goto-char-timer - (avy--process - (avy--read-candidates - (lambda (input) - (format "^\\*+ .*\\(%s\\)" input))) - (avy--style-fn avy-style)) - (org-back-to-heading)))) - -(provide 'avy) - -;;; avy.el ends here diff --git a/.emacs.d/initlibs/key-chord.el b/.emacs.d/initlibs/key-chord.el deleted file mode 100644 index ed3d15f5..00000000 --- a/.emacs.d/initlibs/key-chord.el +++ /dev/null @@ -1,372 +0,0 @@ -;;; key-chord.el --- map pairs of simultaneously pressed keys to commands -;;------------------------------------------------------------------- -;; -;; Copyright (C) 2003,2005,2008,2012 David Andersson -;; -;; This file is NOT part of Emacs. -;; -;; 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. -;; -;; You should have received a copy of the GNU General Public -;; License along with this program; if not, write to the Free -;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, -;; MA 02111-1307 USA -;; -;;------------------------------------------------------------------- - -;; Author: David Andersson <l.david.andersson(at)sverige.nu> -;; Created: 27 April 2003 -;; Version: 0.6 (2012-10-23) -;; Keywords: keyboard chord input - -;;; Commentary: - -;; ######## Compatibility ######################################## -;; -;; Works with Emacs-20.3, 20.6, 20.7, 21.2, 21.4, 22.1 and 23.1 -;; Does not work with Emacs-19.31 nor XEmacs-20.4 and 21.4. - -;; ######## Quick start ######################################## -;; -;; Add to your ~/.emacs -;; -;; (require 'key-chord) -;; (key-chord-mode 1) -;; -;; and some chords, for example -;; -;; (key-chord-define-global "hj" 'undo) -;; (key-chord-define-global ",." "<>\C-b") - -;; ######## Terminology ######################################## -;; -;; In this package, a "key chord" is two keys pressed simultaneously, -;; or a single key quickly pressed twice. -;; -;; (Sometimes pressing SHIFT and/or META plus another key is call a chord, -;; but not here. However SHIFT plus two normal keys can be a "key chord".) - -;; ######## Description ######################################## -;; -;; Key chord mode acts like a global minor mode controlled by the function -;; `key-chord-mode'. -;; -;; Key chord definitions are stored in ordinary key-maps. -;; The function `key-chord-define-global' defines a chord in the global -;; key-map and `key-chord-define' defines a chord in a specified key-map, -;; for example for a specific mode. -;; -;; A TWO-key chord is two distinct keys pressed simultaneously (within -;; one tenth of a second, or so). -;; -;; Examples: -;; -;; (key-chord-define-global ",." "<>\C-b") -;; (key-chord-define-global "hj" 'undo) -;; (key-chord-define-global [?h ?j] 'undo) ; the same -;; (key-chord-define-global "jk" 'dabbrev-expand) -;; (key-chord-define-global "cv" 'reindent-then-newline-and-indent) -;; (key-chord-define-global "4r" "$") -;; -;; Comma and dot pressed together insert a pair of angle brackets. -;; `h' and `j' pressed together invoke the undo command. -;; `j' and `k' pressed together invoke the dabbrev-expand command. -;; 'c' and 'v' pressed together insert a newline. -;; `4' and `r' pressed together insert a dollar sign. -;; -;; A ONE-key chord is a single key quickly pressed twice (within one third -;; of a second or so). -;; -;; Examples: -;; -;; (key-chord-define-global "''" "`'\C-b") -;; (key-chord-define-global ",," 'indent-for-comment) -;; (key-chord-define-global "qq" "the ") -;; (key-chord-define-global "QQ" "The ") -;; -;; Tick (') pressed twice inserts a back-tick and a tick (`'). -;; Comma (,) pressed twice indents for and/or inserts a comment. -;; `q' pressed twice inserts the word "the ". -;; -;; Examples: Mode specific chords -;; -;; (key-chord-define c++-mode-map ";;" "\C-e;") -;; (key-chord-define c++-mode-map "{}" "{\n\n}\C-p\t") -;; -;; The command `key-chord-describe' lists currently defined key chords. -;; The standard command `describe-bindings' (C-h b) will also show key chords. -;; -;; The standard command `describe-key' (C-h k) will accept a key chord and -;; show its definition. (Isn't that amazing. There is no explicit code to -;; carry out this functionality.) - -;; ######## Tips ######################################## -;; -;; Don't chord key combinations that exists in the languages you typically -;; write. Otherwise, if you are typing fast, two key intended to be separate -;; letters might instead trig a chord. -;; E.g. "uu" would be a good chord in spanish but not in finnish, and -;; "hj" would be a good chord in english but not in swedish. -;; -;; Don't rely solely on /usr/dict/words to find unusual combination. -;; For example "cv" or "fg" can be quite common in certain kinds of -;; programming. Grep your own texts to verify that a combination is unusual. -;; And don't forget to check both permutations: "fg" and "gf". -;; -;; Choose two keys that are close to each other on the keyboard, so they -;; can be quickly typed without effort. Chords involving two hands (as -;; opposed to two fingers on one hand) are harder to type (quickly). -;; The idea is that key chords are to replace function keys for functions -;; that are frequently performed while the hands are in writing position. -;; -;; Key chords might not work well over a slow network. - -;; ######## Limitations ######################################## -;; -;; When recording keyboard macros, the time between keyboard inputs are not -;; recorded. Thus, the key-chord-input-method cannot know for sure if two keys -;; in a macro was a chord or not. The current solution remembers the first key -;; of the chords typed during macro recording, and keys that match those (and -;; are defined as chords) are considered key-chords during macro execution. -;; This knowledge is not saved with `name-last-kbd-macro', so they may -;; execute wrong if they contain pair of keys that match defined chords. -;; -;; Emacs will not call input-method-function for keys that have non numeric -;; codes or whos code is outside the range 32..126. Thus you cannot define -;; key chords involving function keys, control keys, or even your non-english -;; letters (on national keyboards) that otherwise are well positioned for -;; chording on your keyboard. -;; (I think chording left and right arrow keys would be useful, but cannot do. -;; I consider this a bug in Emacs. Input methods could happily return -;; unmodified *any* key they don't know about.) -;; -;; Key chords longer that 2 keys are not supported. It could be done, but I -;; don't think it is worth the trubbel since most keyboards will not reliably -;; send all key codes when 3 or more keys are pressed simultaneously. -;; It might also be a bit trickier to maintain performance. -;; -;; Key chord mode uses input-method-function. And so do internationalisation -;; packages (mule, quail, etc). Do not expect them to work well together. -;; The last one that gets the input-method-function rules. - -;; ######## Implementation ######################################## -;; -;; Key chords piggy back in ordinary key maps, so they can be defined -;; per mode without having to add hooks to all modes. -;; -;; Key chord key codes are vectors beginning with the atom `key-chord'. -;; A two key chord, e.g. "hj", will add two entries in the key-map. -;; E.g. [key-chord ?h ?j] and [key-chord ?j ?h]. -;; -;; When key-chord-mode is enabled input-method-function is set to -;; key-chord-input-method. - -;; ######## To do ######################################## -;; -;; * Find a way to save key-chord info in keyboard macros. -;; -;; * Save previous value of input-method-function? And call it? -;; -;; * input-method-function is reset in *info* buffers! What to do? -;; -;; * How to enter interactively command OR string in key-chord-define-global? -;; -;; * Customize public vars (defcustom). - -;; ######## History ######################################## -;; -;; 0.6 (2012-10-23) l.david.andersson(at)sverige.nu -;; Add key-chord-define-local, key-chord-unset-local, key-chord-unset-global -;; 0.5 (2008-09-15) david(at)symsoft.se -;; Bugfix sit-for; Improved examples; New E-mail in comment -;; 0.4 (2005-05-07) david(at)symsoft.se -;; Slightly better macro heuristics; Added option key-chord-in-macros -;; 0.3 (2005-04-14) david(at)symsoft.se -;; Require advice; More examples -;; 0.2 (2003-09-13) david(at)symsoft.se -;; Quick and dirty fix for keyboard macros -;; 0.1 (2003-04-27) david(at)symsoft.se -;; First release - -;;; Code: - -(defvar key-chord-two-keys-delay 0.1 ; 0.05 or 0.1 - "Max time delay between two key press to be considered a key chord.") - -(defvar key-chord-one-key-delay 0.2 ; 0.2 or 0.3 to avoid first autorepeat - "Max time delay between two press of the same key to be considered a key chord. -This should normally be a little longer than `key-chord-two-keys-delay'.") - -(defvar key-chord-in-macros t - "If nil, don't expand key chords when executing keyboard macros. -If non-nil, expand chord sequenses in macros, but only if a similar chord was -entered during the last interactive macro recording. (This carries a bit of -guesswork. We can't know for sure when executing whether two keys were -typed quickly or slowly when recorded.)") - -;; Internal vars -(defvar key-chord-mode nil) - -;; Shortcut for key-chord-input-method: no need to test a key again if it -;; didn't matched a chord the last time. Improves feedback during autorepeat. -(defvar key-chord-last-unmatched nil) - -;; Macro heuristics: Keep track of which chords was used when the last macro -;; was defined. Or rather, only the first-char of the chords. Only expand -;; matching chords during macro execution. -(defvar key-chord-in-last-kbd-macro nil) -(defvar key-chord-defining-kbd-macro nil) - -;;;###autoload -(defun key-chord-mode (arg) - "Toggle key chord mode. -With positive ARG enable the mode. With zero or negative arg disable the mode. -A key chord is two keys that are pressed simultaneously, or one key quickly -pressed twice. -\nSee functions `key-chord-define-global', `key-chord-define-local', and -`key-chord-define' and variables `key-chord-two-keys-delay' and -`key-chord-one-key-delay'." - - (interactive "P") - (setq key-chord-mode (if arg - (> (prefix-numeric-value arg) 0) - (not key-chord-mode))) - (cond (key-chord-mode - (setq input-method-function 'key-chord-input-method) - (message "Key Chord mode on")) - (t - (setq input-method-function nil) - (message "Key Chord mode off")))) - -;;;###autoload -(defun key-chord-define-global (keys command) - "Define a key-chord of the two keys in KEYS starting a COMMAND. -\nKEYS can be a string or a vector of two elements. Currently only elements -that corresponds to ascii codes in the range 32 to 126 can be used. -\nCOMMAND can be an interactive function, a string, or nil. -If COMMAND is nil, the key-chord is removed. -\nNote that KEYS defined locally in the current buffer will have precedence." - (interactive "sSet key chord globally (2 keys): \nCSet chord \"%s\" to command: ") - (key-chord-define (current-global-map) keys command)) - -;;;###autoload -(defun key-chord-define-local (keys command) - "Locally define a key-chord of the two keys in KEYS starting a COMMAND. -\nKEYS can be a string or a vector of two elements. Currently only elements -that corresponds to ascii codes in the range 32 to 126 can be used. -\nCOMMAND can be an interactive function, a string, or nil. -If COMMAND is nil, the key-chord is removed. -\nThe binding goes in the current buffer's local map, -which in most cases is shared with all other buffers in the same major mode." - (interactive "sSet key chord locally (2 keys): \nCSet chord \"%s\" to command: ") - (key-chord-define (current-local-map) keys command)) - -(defun key-chord-unset-global (keys) - "Remove global key-chord of the two keys in KEYS." - (interactive "sUnset key chord globally (2 keys): ") - (key-chord-define (current-global-map) keys nil)) - -(defun key-chord-unset-local (keys) - "Remove local key-chord of the two keys in KEYS." - (interactive "sUnset key chord locally (2 keys): ") - (key-chord-define (current-local-map) keys nil)) - -;;;###autoload -(defun key-chord-define (keymap keys command) - "Define in KEYMAP, a key-chord of the two keys in KEYS starting a COMMAND. -\nKEYS can be a string or a vector of two elements. Currently only elements -that corresponds to ascii codes in the range 32 to 126 can be used. -\nCOMMAND can be an interactive function, a string, or nil. -If COMMAND is nil, the key-chord is removed." - (if (/= 2 (length keys)) - (error "Key-chord keys must have two elements")) - ;; Exotic chars in a string are >255 but define-key wants 128..255 for those - (let ((key1 (logand 255 (aref keys 0))) - (key2 (logand 255 (aref keys 1)))) - (if (eq key1 key2) - (define-key keymap (vector 'key-chord key1 key2) command) - ;; else - (define-key keymap (vector 'key-chord key1 key2) command) - (define-key keymap (vector 'key-chord key2 key1) command)))) - -(defun key-chord-lookup-key1 (keymap key) - "Like lookup-key but no third arg and no numeric return value." - (let ((res (lookup-key keymap key))) - (if (numberp res) - nil - ;; else - res))) - -(defun key-chord-lookup-key (key) - "Lookup KEY in all current key maps." - (let ((maps (current-minor-mode-maps)) - res) - (while (and maps (not res)) - (setq res (key-chord-lookup-key1 (car maps) key) - maps (cdr maps))) - (or res - (if (current-local-map) - (key-chord-lookup-key1 (current-local-map) key)) - (key-chord-lookup-key1 (current-global-map) key)))) - -(defun key-chord-describe () - "List key chord bindings in a help buffer. -\nTwo key chords will be listed twice and there will be Prefix Commands. -Please ignore that." - (interactive) - (describe-bindings [key-chord])) - -(defun key-chord-input-method (first-char) - "Input method controlled by key bindings with the prefix `key-chord'." - (if (and (not (eq first-char key-chord-last-unmatched)) - (key-chord-lookup-key (vector 'key-chord first-char))) - (let ((delay (if (key-chord-lookup-key (vector 'key-chord first-char first-char)) - key-chord-one-key-delay - ;; else - key-chord-two-keys-delay))) - (if (if executing-kbd-macro - (not (memq first-char key-chord-in-last-kbd-macro)) - (when (bound-and-true-p eldoc-mode) - (eldoc-pre-command-refresh-echo-area)) - - (sit-for delay 0 'no-redisplay)) - (progn - (setq key-chord-last-unmatched nil) - (list first-char)) - ;; else input-pending-p - (let* ((input-method-function nil) - (next-char (read-event)) - (res (vector 'key-chord first-char next-char))) - (if (key-chord-lookup-key res) - (progn - (setq key-chord-defining-kbd-macro - (cons first-char key-chord-defining-kbd-macro)) - (list 'key-chord first-char next-char)) - ;; else put back next-char and return first-char - (setq unread-command-events (cons next-char unread-command-events)) - (if (eq first-char next-char) - (setq key-chord-last-unmatched first-char)) - (list first-char))))) - ;; else no key-chord keymap - (setq key-chord-last-unmatched first-char) - (list first-char))) - -(require 'advice) - -(defadvice start-kbd-macro (after key-chord activate) - (setq key-chord-defining-kbd-macro nil)) - -(defadvice end-kbd-macro (after key-chord activate) - (setq key-chord-in-last-kbd-macro key-chord-defining-kbd-macro)) - -(provide 'key-chord) - -;;; key-chord.el ends here diff --git a/.emacs.d/initlibs/smex.el b/.emacs.d/initlibs/smex.el deleted file mode 100644 index 74d3b8c0..00000000 --- a/.emacs.d/initlibs/smex.el +++ /dev/null @@ -1,446 +0,0 @@ -;;; smex.el --- M-x interface with Ido-style fuzzy matching. - -;; Copyright (C) 2009-2014 Cornelius Mika and contributors -;; -;; Author: Cornelius Mika <cornelius.mika@gmail.com> and contributors -;; URL: http://github.com/nonsequitur/smex/ -;; Version: 3.0 -;; Keywords: convenience, usability - -;; This file is not part of GNU Emacs. - -;;; License: - -;; Licensed under the same terms as Emacs. - -;;; Commentary: - -;; Quick start: -;; run (smex-initialize) -;; -;; Bind the following commands: -;; smex, smex-major-mode-commands -;; -;; For a detailed introduction see: -;; http://github.com/nonsequitur/smex/blob/master/README.markdown - -;;; Code: - -(require 'ido) - -(defgroup smex nil - "M-x interface with Ido-style fuzzy matching and ranking heuristics." - :group 'extensions - :group 'convenience - :link '(emacs-library-link :tag "Lisp File" "smex.el")) - -(defcustom smex-auto-update t - "If non-nil, `Smex' checks for new commands each time it is run. -Turn it off for minor speed improvements on older systems." - :type 'boolean - :group 'smex) - -(defcustom smex-save-file "~/.smex-items" - "File in which the smex state is saved between Emacs sessions. -Variables stored are: `smex-data', `smex-history'. -Must be set before initializing Smex." - :type 'string - :group 'smex) - -(defcustom smex-history-length 7 - "Determines on how many recently executed commands -Smex should keep a record. -Must be set before initializing Smex." - :type 'integer - :group 'smex) - -(defcustom smex-prompt-string "M-x " - "String to display in the Smex prompt." - :type 'string - :group 'smex) - -(defcustom smex-flex-matching t - "Enables Ido flex matching. On by default. -Set this to nil to disable fuzzy matching." - :type 'boolean - :group 'smex) - -(defvar smex-initialized-p nil) -(defvar smex-cache) -(defvar smex-ido-cache) -(defvar smex-data) -(defvar smex-history) -(defvar smex-command-count 0) -(defvar smex-custom-action nil) - -;;-------------------------------------------------------------------------------- -;; Smex Interface - -;;;###autoload -(defun smex () - (interactive) - (unless smex-initialized-p - (smex-initialize)) - (if (smex-already-running) - (smex-update-and-rerun) - (and smex-auto-update - (smex-detect-new-commands) - (smex-update)) - (smex-read-and-run smex-ido-cache))) - -(defsubst smex-already-running () - (and (boundp 'ido-choice-list) (eql ido-choice-list smex-ido-cache))) - -(defsubst smex-update-and-rerun () - (smex-do-with-selected-item - (lambda (ignore) (smex-update) (smex-read-and-run smex-ido-cache ido-text)))) - -(defun smex-read-and-run (commands &optional initial-input) - (let* ((chosen-item-name (smex-completing-read commands initial-input)) - (chosen-item (intern chosen-item-name))) - (if smex-custom-action - (let ((action smex-custom-action)) - (setq smex-custom-action nil) - (funcall action chosen-item)) - (unwind-protect - (execute-extended-command current-prefix-arg chosen-item-name) - (smex-rank chosen-item))))) - -(defun smex-major-mode-commands () - "Like `smex', but limited to commands that are relevant to the active major mode." - (interactive) - (let ((commands (delete-dups (append (smex-extract-commands-from-keymap (current-local-map)) - (smex-extract-commands-from-features major-mode))))) - (setq commands (smex-sort-according-to-cache commands)) - (setq commands (mapcar #'symbol-name commands)) - (smex-read-and-run commands))) - -(defun smex-completing-read (choices initial-input) - (let ((ido-completion-map ido-completion-map) - (ido-setup-hook (cons 'smex-prepare-ido-bindings ido-setup-hook)) - (ido-enable-prefix nil) - (ido-enable-flex-matching smex-flex-matching) - (ido-max-prospects 10) - (minibuffer-completion-table choices)) - (ido-completing-read (smex-prompt-with-prefix-arg) choices nil nil - initial-input 'extended-command-history (car choices)))) - -(defun smex-prompt-with-prefix-arg () - (if (not current-prefix-arg) - smex-prompt-string - (concat - (if (eq current-prefix-arg '-) - "- " - (if (integerp current-prefix-arg) - (format "%d " current-prefix-arg) - (if (= (car current-prefix-arg) 4) - "C-u " - (format "%d " (car current-prefix-arg))))) - smex-prompt-string))) - -(defun smex-prepare-ido-bindings () - (define-key ido-completion-map (kbd "TAB") 'minibuffer-complete) - (define-key ido-completion-map (kbd "C-h f") 'smex-describe-function) - (define-key ido-completion-map (kbd "C-h w") 'smex-where-is) - (define-key ido-completion-map (kbd "M-.") 'smex-find-function) - (define-key ido-completion-map (kbd "C-a") 'move-beginning-of-line)) - -;;-------------------------------------------------------------------------------- -;; Cache and Maintenance - -(defun smex-rebuild-cache () - (interactive) - (setq smex-cache nil) - - ;; Build up list 'new-commands' and later put it at the end of 'smex-cache'. - ;; This speeds up sorting. - (let (new-commands) - (mapatoms (lambda (symbol) - (when (commandp symbol) - (let ((known-command (assq symbol smex-data))) - (if known-command - (setq smex-cache (cons known-command smex-cache)) - (setq new-commands (cons (list symbol) new-commands))))))) - (if (eq (length smex-cache) 0) - (setq smex-cache new-commands) - (setcdr (last smex-cache) new-commands))) - - (setq smex-cache (sort smex-cache 'smex-sorting-rules)) - (smex-restore-history) - (setq smex-ido-cache (smex-convert-for-ido smex-cache))) - -(defun smex-convert-for-ido (command-items) - (mapcar (lambda (command-item) (symbol-name (car command-item))) command-items)) - -(defun smex-restore-history () - "Rearranges `smex-cache' according to `smex-history'" - (if (> (length smex-history) smex-history-length) - (setcdr (nthcdr (- smex-history-length 1) smex-history) nil)) - (mapc (lambda (command) - (unless (eq command (caar smex-cache)) - (let ((command-cell-position (smex-detect-position smex-cache (lambda (cell) - (eq command (caar cell)))))) - (if command-cell-position - (let ((command-cell (smex-remove-nth-cell command-cell-position smex-cache))) - (setcdr command-cell smex-cache) - (setq smex-cache command-cell)))))) - (reverse smex-history))) - -(defun smex-sort-according-to-cache (list) - "Sorts a list of commands by their order in `smex-cache'" - (let (sorted) - (dolist (command-item smex-cache) - (let ((command (car command-item))) - (when (memq command list) - (setq sorted (cons command sorted)) - (setq list (delq command list))))) - (nreverse (append list sorted)))) - -(defun smex-update () - (interactive) - (smex-save-history) - (smex-rebuild-cache)) - -(defun smex-detect-new-commands () - (let ((i 0)) - (mapatoms (lambda (symbol) (if (commandp symbol) (setq i (1+ i))))) - (unless (= i smex-command-count) - (setq smex-command-count i)))) - -(defun smex-auto-update (&optional idle-time) - "Update Smex when Emacs has been idle for IDLE-TIME." - (unless idle-time (setq idle-time 60)) - (run-with-idle-timer idle-time t - '(lambda () (if (smex-detect-new-commands) (smex-update))))) - -;;;###autoload -(defun smex-initialize () - (interactive) - (unless ido-mode (smex-initialize-ido)) - (smex-load-save-file) - (smex-detect-new-commands) - (smex-rebuild-cache) - (add-hook 'kill-emacs-hook 'smex-save-to-file) - (setq smex-initialized-p t)) - -(defun smex-initialize-ido () - "Sets up a minimal Ido environment for `ido-completing-read'." - (ido-init-completion-maps) - (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)) - -(defun smex-load-save-file () - "Loads `smex-history' and `smex-data' from `smex-save-file'" - (let ((save-file (expand-file-name smex-save-file))) - (if (file-readable-p save-file) - (with-temp-buffer - (insert-file-contents save-file) - (condition-case nil - (setq smex-history (read (current-buffer)) - smex-data (read (current-buffer))) - (error (if (smex-save-file-not-empty-p) - (error "Invalid data in smex-save-file (%s). Can't restore history." - smex-save-file) - (unless (boundp 'smex-history) (setq smex-history nil)) - (unless (boundp 'smex-data) (setq smex-data nil)))))) - (setq smex-history nil smex-data nil)))) - -(defsubst smex-save-file-not-empty-p () - (string-match-p "\[^[:space:]\]" (buffer-string))) - -(defun smex-save-history () - "Updates `smex-history'" - (setq smex-history nil) - (let ((cell smex-cache)) - (dotimes (i smex-history-length) - (setq smex-history (cons (caar cell) smex-history)) - (setq cell (cdr cell)))) - (setq smex-history (nreverse smex-history))) - -(defun smex-save-to-file () - (interactive) - (smex-save-history) - (with-temp-file (expand-file-name smex-save-file) - (ido-pp 'smex-history) - (ido-pp 'smex-data))) - -;;-------------------------------------------------------------------------------- -;; Ranking - -(defun smex-sorting-rules (command-item other-command-item) - "Returns true if COMMAND-ITEM should sort before OTHER-COMMAND-ITEM." - (let* ((count (or (cdr command-item ) 0)) - (other-count (or (cdr other-command-item) 0)) - (name (car command-item)) - (other-name (car other-command-item)) - (length (length (symbol-name name))) - (other-length (length (symbol-name other-name)))) - (or (> count other-count) ; 1. Frequency of use - (and (= count other-count) - (or (< length other-length) ; 2. Command length - (and (= length other-length) - (string< name other-name))))))) ; 3. Alphabetical order - -(defun smex-rank (command) - (let ((command-item (or (assq command smex-cache) - ;; Update caches and try again if not found. - (progn (smex-update) - (assq command smex-cache))))) - (when command-item - (smex-update-counter command-item) - - ;; Don't touch the cache order if the chosen command - ;; has just been execucted previously. - (unless (eq command-item (car smex-cache)) - (let (command-cell - (pos (smex-detect-position smex-cache (lambda (cell) - (eq command-item (car cell)))))) - ;; Remove the just executed command. - (setq command-cell (smex-remove-nth-cell pos smex-cache)) - ;; And put it on top of the cache. - (setcdr command-cell smex-cache) - (setq smex-cache command-cell) - - ;; Repeat the same for the ido cache. Should this be DRYed? - (setq command-cell (smex-remove-nth-cell pos smex-ido-cache)) - (setcdr command-cell smex-ido-cache) - (setq smex-ido-cache command-cell) - - ;; Now put the last history item back to its normal place. - (smex-sort-item-at smex-history-length)))))) - -(defun smex-update-counter (command-item) - (let ((count (cdr command-item))) - (setcdr command-item - (if count - (1+ count) - ;; Else: Command has just been executed for the first time. - ;; Add it to `smex-data'. - (if smex-data - (setcdr (last smex-data) (list command-item)) - (setq smex-data (list command-item))) - 1)))) - -(defun smex-sort-item-at (n) - "Sorts item at position N in `smex-cache'." - (let* ((command-cell (nthcdr n smex-cache)) - (command-item (car command-cell)) - (command-count (cdr command-item))) - (let ((insert-at (smex-detect-position command-cell (lambda (cell) - (smex-sorting-rules command-item (car cell)))))) - ;; TODO: Should we handle the case of 'insert-at' being nil? - ;; This will never happen in practice. - (when (> insert-at 1) - (setq command-cell (smex-remove-nth-cell n smex-cache)) - ;; smex-cache just got shorter by one element, so subtract '1' from insert-at. - (setq insert-at (+ n (- insert-at 1))) - (smex-insert-cell command-cell insert-at smex-cache) - - ;; Repeat the same for the ido cache. DRY? - (setq command-cell (smex-remove-nth-cell n smex-ido-cache)) - (smex-insert-cell command-cell insert-at smex-ido-cache))))) - -(defun smex-detect-position (cell function) - "Detects, relatively to CELL, the position of the cell -on which FUNCTION returns true. -Only checks cells after CELL, starting with the cell right after CELL. -Returns nil when reaching the end of the list." - (let ((pos 1)) - (catch 'break - (while t - (setq cell (cdr cell)) - (if (not cell) - (throw 'break nil) - (if (funcall function cell) (throw 'break pos)) - (setq pos (1+ pos))))))) - -(defun smex-remove-nth-cell (n list) - "Removes and returns the Nth cell in LIST." - (let* ((previous-cell (nthcdr (- n 1) list)) - (result (cdr previous-cell))) - (setcdr previous-cell (cdr result)) - result)) - -(defun smex-insert-cell (new-cell n list) - "Inserts cell at position N in LIST." - (let* ((cell (nthcdr (- n 1) list)) - (next-cell (cdr cell))) - (setcdr (setcdr cell new-cell) next-cell))) - -;;-------------------------------------------------------------------------------- -;; Help and Reference - -(defun smex-do-with-selected-item (fn) - (setq smex-custom-action fn) - (ido-exit-minibuffer)) - -(defun smex-describe-function () - (interactive) - (smex-do-with-selected-item (lambda (chosen) - (describe-function chosen) - (pop-to-buffer "*Help*")))) - -(defun smex-where-is () - (interactive) - (smex-do-with-selected-item 'where-is)) - -(defun smex-find-function () - (interactive) - (smex-do-with-selected-item 'find-function)) - -(defun smex-extract-commands-from-keymap (map) - (let (commands) - (smex-parse-keymap map commands) - commands)) - -(defun smex-parse-keymap (map commands) - (map-keymap (lambda (binding element) - (if (and (listp element) (eq 'keymap (car element))) - (smex-parse-keymap element commands) - ; Strings are commands, too. Reject them. - (if (and (symbolp element) (commandp element)) - (push element commands)))) - map)) - -(defun smex-extract-commands-from-features (mode) - (let ((library-path (symbol-file mode)) - (mode-name (symbol-name mode)) - commands) - - (string-match "\\(.+?\\)\\(-mode\\)?$" mode-name) - ;; 'lisp-mode' -> 'lisp' - (setq mode-name (match-string 1 mode-name)) - (if (string= mode-name "c") (setq mode-name "cc")) - (setq mode-name (regexp-quote mode-name)) - - (dolist (feature load-history) - (let ((feature-path (car feature))) - (when (and feature-path (or (equal feature-path library-path) - (string-match mode-name (file-name-nondirectory - feature-path)))) - (dolist (item (cdr feature)) - (if (and (listp item) (eq 'defun (car item))) - (let ((function (cdr item))) - (when (commandp function) - (setq commands (append commands (list function)))))))))) - commands)) - -(defun smex-show-unbound-commands () - "Shows unbound commands in a new buffer, -sorted by frequency of use." - (interactive) - (setq smex-data (sort smex-data 'smex-sorting-rules)) - (let ((unbound-commands (delq nil - (mapcar (lambda (command-item) - (unless (where-is-internal (car command-item)) - command-item)) - smex-data)))) - (view-buffer-other-window "*Smex: Unbound Commands*") - (setq buffer-read-only t) - (let ((inhibit-read-only t)) - (erase-buffer) - (ido-pp 'unbound-commands)) - (set-buffer-modified-p nil) - (goto-char (point-min)))) - -(provide 'smex) -;;; smex.el ends here |