]> code.delx.au - gnu-emacs-elpa/blob - packages/avy/avy.el
Merge commit 'b114cf8a93224c85c51e95db52bf359131130476' from ace-window
[gnu-emacs-elpa] / packages / avy / avy.el
1 ;;; avy.el --- set-based completion -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; Version: 0
7
8 ;; This file is part of GNU Emacs.
9
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; For a full copy of the GNU General Public License
21 ;; see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;;
25 ;; This package provides a generic completion method based on building
26 ;; a balanced decision tree with each candidate being a leaf. To
27 ;; traverse the tree from the root to a desired leaf, typically a
28 ;; sequence of `read-char' can be used.
29 ;;
30 ;; In order for `read-char' to make sense, the tree needs to be
31 ;; visualized appropriately, with a character at each branch node. So
32 ;; this completion method works only for things that you can see on
33 ;; your screen, all at once:
34 ;;
35 ;; * character positions
36 ;; * word or subword start positions
37 ;; * line beginning positions
38 ;; * link positions
39 ;; * window positions
40 ;;
41 ;; If you're familiar with the popular `ace-jump-mode' package, this
42 ;; package does all that and more, without the implementation
43 ;; headache.
44
45 ;;; Code:
46 (require 'cl-lib)
47
48 (defmacro avy-multipop (lst n)
49 "Remove LST's first N elements and return them."
50 `(if (<= (length ,lst) ,n)
51 (prog1 ,lst
52 (setq ,lst nil))
53 (prog1 ,lst
54 (setcdr
55 (nthcdr (1- ,n) (prog1 ,lst (setq ,lst (nthcdr ,n ,lst))))
56 nil))))
57
58 (defun avy-tree (lst keys)
59 "Coerce LST into a balanced tree.
60 The degree of the tree is the length of KEYS.
61 KEYS are placed appropriately on internal nodes."
62 (let ((len (length keys)))
63 (cl-labels
64 ((rd (ls)
65 (let ((ln (length ls)))
66 (if (< ln len)
67 (cl-pairlis keys
68 (mapcar (lambda (x) (cons 'leaf x)) ls))
69 (let ((ks (copy-sequence keys))
70 res)
71 (dolist (s (avy-subdiv ln len))
72 (push (cons (pop ks)
73 (if (eq s 1)
74 (cons 'leaf (pop ls))
75 (rd (avy-multipop ls s))))
76 res))
77 (nreverse res))))))
78 (rd lst))))
79
80 (defun avy-subdiv (n b)
81 "Distribute N in B terms in a balanced way."
82 (let* ((p (1- (floor (+ (log n b) 1e-6))))
83 (x1 (expt b p))
84 (x2 (* b x1))
85 (delta (- n x2))
86 (n2 (/ delta (- x2 x1)))
87 (n1 (- b n2 1)))
88 (append
89 (make-list n1 x1)
90 (list
91 (- n (* n1 x1) (* n2 x2)))
92 (make-list n2 x2))))
93
94 (defun avy-traverse (tree walker &optional recur-key)
95 "Traverse TREE generated by `avy-tree'.
96 WALKER is a function that takes KEYS and LEAF.
97
98 RECUR-KEY is used in recursion.
99
100 LEAF is a member of LST argument of `avy-tree'.
101
102 KEYS is the path from the root of `avy-tree' to LEAF."
103 (dolist (br tree)
104 (let ((key (cons (car br) recur-key)))
105 (if (eq (cadr br) 'leaf)
106 (funcall walker key (cddr br))
107 (avy-traverse (cdr br) walker key)))))
108
109 (defun avy-read (tree display-fn cleanup-fn)
110 "Select a leaf from TREE using consecutive `read-char'.
111
112 DISPLAY-FN should take CHAR and LEAF and signify that LEAFs
113 associated with CHAR will be selected if CHAR is pressed. This is
114 commonly done by adding a CHAR overlay at LEAF position.
115
116 CLEANUP-FN should take no arguments and remove the effects of
117 multiple DISPLAY-FN invokations."
118 (catch 'done
119 (while tree
120 (avy-traverse tree display-fn)
121 (let ((char (read-char))
122 branch)
123 (funcall cleanup-fn)
124 (if (setq branch (assoc char tree))
125 (if (eq (car (setq tree (cdr branch))) 'leaf)
126 (throw 'done (cdr tree)))
127 (signal 'user-error (list "No such candidate" char))
128 (throw 'done nil))))))
129
130 (provide 'avy)
131
132 ;;; avy.el ends here