avy-migemo-0.3.2/ 0000755 0000000 0000000 00000000000 12673744343 011771 5 ustar 00 0000000 0000000 avy-migemo-0.3.2/avy-migemo-e.g.counsel.el 0000644 0000000 0000000 00000007165 12673744343 016514 0 ustar 00 0000000 0000000 ;;; avy-migemo-e.g.counsel.el --- A setting example of avy-migemo for counsel -*- lexical-binding: t; no-byte-compile: t -*-
;; Copyright (C) 2015-2016 momomo5717
;; Author: momomo5717
;; URL: https://github.com/momomo5717/avy-migemo
;; 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 3 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, see .
;;; Commentary:
;; This file is a setting example of avy-migemo for counsel.
;;
;; (require 'avy-migemo-e.g.swiper)
;; (require 'avy-migemo-e.g.counsel)
;;
;; ;; If you want to remove this config from `avy-migemo-function-names' in a init file,
;; (with-eval-after-load 'avy-migemo-e.g.counsel-loaded
;; (avy-migemo-remove-names 'counsel-grep-function-migemo))
;;; Code:
(with-eval-after-load 'ivy--regex-migemo
(with-eval-after-load "counsel"
(defun counsel-pt-function-migemo (string &optional _pred &rest _unused)
"pt in the current directory for STRING."
(if (< (length string) 3)
(counsel-more-chars 3)
(let ((default-directory counsel--git-grep-dir)
(regex (replace-regexp-in-string
"\\\\|" "|"
(counsel-unquote-regex-parens
(setq ivy--old-re
(ivy--regex-migemo string))))))
(counsel--async-command
(format "pt --nocolor --nogroup -e %S" regex))
nil)))
(byte-compile 'counsel-pt-function-migemo)
(defun counsel-pt-migemo (&optional initial-input initial-directory)
"The same as `counsel-ag' except for using pt and migemo."
(interactive)
(setq counsel--git-grep-dir (or initial-directory default-directory))
(ivy-read "pt: " 'counsel-pt-function-migemo
:initial-input initial-input
:dynamic-collection t
:history 'counsel-git-grep-history
:action #'counsel-git-grep-action
:unwind (lambda ()
(counsel-delete-process)
(swiper--cleanup))))
(byte-compile 'counsel-pt-migemo)
(defun counsel-grep-function-migemo (string &optional _pred &rest _unused)
"The same as `counsel-grep-function' except for using migemo."
(if (< (length string) 3)
(counsel-more-chars 3)
(let ((regex (replace-regexp-in-string ; Adapt for migemo
"\\\\|" "|"
(counsel-unquote-regex-parens
(setq ivy--old-re
(ivy--regex-migemo string))))))
(counsel--async-command
(format "grep -nP --ignore-case '%s' %s" regex counsel--git-grep-dir))
nil)))
(byte-compile 'counsel-grep-function-migemo)
(avy-migemo-add-names 'counsel-grep-function-migemo)
;; `counsel-locate' is incompatible with `avy-migemo-mode'.
(advice-add 'counsel-locate :around #'avy-migemo-disable-around)
;; `counsel-ag' is incompatible with `avy-migemo-mode'.
(advice-add 'counsel-ag :around #'avy-migemo-disable-around)
(provide 'avy-migemo-e.g.counsel-loaded)))
(provide 'avy-migemo-e.g.counsel)
;;; avy-migemo-e.g.counsel.el ends here
avy-migemo-0.3.2/avy-migemo-e.g.swiper.el 0000644 0000000 0000000 00000023450 12673744343 016350 0 ustar 00 0000000 0000000 ;;; avy-migemo-e.g.swiper.el --- A setting example of avy-migemo for swiper -*- lexical-binding: t; no-byte-compile: t -*-
;; Copyright (C) 2015-2016 momomo5717
;; Author: momomo5717
;; URL: https://github.com/momomo5717/avy-migemo
;; 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 3 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, see .
;;; Commentary:
;; This file is a setting example of avy-migemo for swiper
;;
;; (require 'avy-migemo-e.g.swiper)
;;
;; ;; If you want to remove this config from `avy-migemo-function-names' in a init file,
;; (with-eval-after-load 'ivy--regex-migemo
;; (avy-migemo-remove-names 'ivy--regex-migemo
;; 'ivy--format-minibuffer-line-migemo))
;; (with-eval-after-load 'swiper--add-overlays-migemo
;; (avy-migemo-remove-names 'swiper--add-overlays-migemo))
;;; Code:
;; For using swiper ( `ivy--regex' ) with migemo
(with-eval-after-load "avy-migemo"
(with-eval-after-load "ivy"
(defvar avy-migemo--ivy--regex-hash
(make-hash-table :test #'equal)
"avy-migemo's `ivy--regex-hash'.")
(defun avy-migemo--ivy--regex-hash-clear ()
"Clear `avy-migemo--ivy--regex-hash'."
(clrhash avy-migemo--ivy--regex-hash))
(byte-compile 'avy-migemo--ivy--regex-hash-clear)
(defun ivy--regex-migemo (str &optional greedy)
"The same as `ivy--regex' except for using migemo."
(let ((hashed (unless greedy
(gethash str avy-migemo--ivy--regex-hash))))
(if hashed
(prog1 (cdr hashed)
(setq ivy--subexps (car hashed)))
(when (string-match "\\([^\\]\\|^\\)\\\\$" str)
(setq str (substring str 0 -1)))
(cdr (puthash str
(let ((subs
;; Adapt for migemo
(mapcar #'avy-migemo-regex-concat
(ivy--split str))))
(if (= (length subs) 1)
(cons
(setq ivy--subexps 0)
(car subs))
(cons
(setq ivy--subexps (length subs))
(mapconcat
(lambda (x)
(if (string-match "\\`\\\\([^?].*\\\\)\\'" x)
x
(format "\\(%s\\)" x)))
subs
(if greedy
".*"
".*?")))))
avy-migemo--ivy--regex-hash)))))
(byte-compile 'ivy--regex-migemo)
(defvar avy-migemo--ivy-old-re "")
(defvar avy-migemo--ivy-old-re-depth 0)
(defun avy-migemo--ivy-cal-old-re-depth (re)
"Calculate RE (regex) depth."
(if (eq avy-migemo--ivy-old-re re)
avy-migemo--ivy-old-re-depth
(setq avy-migemo--ivy-old-re re
avy-migemo--ivy-old-re-depth
(regexp-opt-depth re))))
(byte-compile 'avy-migemo--ivy-cal-old-re-depth)
(defun ivy--format-minibuffer-line-migemo (str)
"The same as `ivy--format-minibuffer-line' except adapting it for migemo's regexp."
(let ((start 0)
(str (copy-sequence str)))
(cond ((eq ivy--regex-function 'ivy--regex-ignore-order)
(when (consp ivy--old-re)
(let ((i 1))
(dolist (re ivy--old-re)
(when (string-match (car re) str)
(ivy-add-face-text-property
(match-beginning 0) (match-end 0)
(nth (1+ (mod (+ i 2) (1- (length ivy-minibuffer-faces))))
ivy-minibuffer-faces)
str))
(cl-incf i)))))
((and (eq ivy-display-style 'fancy)
(not (eq ivy--regex-function 'ivy--regex-fuzzy)))
(unless ivy--old-re
(setq ivy--old-re (funcall ivy--regex-function ivy-text)))
(while (and (string-match ivy--old-re str start)
(> (- (match-end 0) (match-beginning 0)) 0))
(setq start (match-end 0))
;; Adapt for migemo's regexp
(let ((i 0) (i-face 0)
mbeg mend (l-mend 0)
(re-depth+1 (if (zerop ivy--subexps)
1
(1+ (avy-migemo--ivy-cal-old-re-depth ivy--old-re)))))
(while (and (< i re-depth+1) (< l-mend start))
(setq mbeg (match-beginning i)
mend (match-end i))
(when (and mbeg (<= l-mend mbeg) mend)
(let ((face
(cond ((zerop ivy--subexps)
(cadr ivy-minibuffer-faces))
((zerop i)
(car ivy-minibuffer-faces))
(t
(nth (1+ (mod (+ i-face 2) (1- (length ivy-minibuffer-faces))))
ivy-minibuffer-faces)))))
(ivy-add-face-text-property
mbeg (if (> i 0) (setq l-mend mend) mend) face str)
(cl-incf i-face)))
(cl-incf i))))))
str))
(byte-compile 'ivy--format-minibuffer-line-migemo)
(avy-migemo-add-names 'ivy--regex-migemo
'ivy--format-minibuffer-line-migemo)
(add-hook 'avy-migemo-regex-cache-clear-hook
'avy-migemo--ivy--regex-hash-clear)
(define-obsolete-function-alias 'avy-migemo-clear-ivy--regex-hash
'(lambda (&rest _)) "0.2.9")
(provide 'ivy--regex-migemo))
(with-eval-after-load "swiper"
(defvar avy-migemo--swiper-old-re "")
(defvar avy-migemo--swiper-old-re-depth 0)
(defun avy-migemo--swiper-cal-old-re-depth (re)
"Calculate RE (regex) depth."
(if (eq avy-migemo--swiper-old-re re)
avy-migemo--swiper-old-re-depth
(setq avy-migemo--swiper-old-re re
avy-migemo--swiper-old-re-depth
(regexp-opt-depth re))))
(byte-compile 'avy-migemo--swiper-cal-old-re-depth)
(defun swiper--add-overlays-migemo (re &optional beg end)
"The same as `swiper--add-overlays' except adapting it for migemo's regexp."
(let ((ov (if visual-line-mode
(make-overlay
(save-excursion
(beginning-of-visual-line)
(point))
(save-excursion
(end-of-visual-line)
(point)))
(make-overlay
(line-beginning-position)
(1+ (line-end-position))))))
(overlay-put ov 'face 'swiper-line-face)
(overlay-put ov 'window (ivy-state-window ivy-last))
(push ov swiper--overlays)
(let* ((wh (window-height))
(beg (or beg (save-excursion
(forward-line (- wh))
(point))))
(end (or end (save-excursion
(forward-line wh)
(point)))))
(when (>= (length re) swiper-min-highlight)
(save-excursion
(goto-char beg)
;; RE can become an invalid regexp
(while (and (ignore-errors (re-search-forward re end t))
(> (- (match-end 0) (match-beginning 0)) 0))
;; Adapt for migemo's regexp.
(let ((i 0) (i-face 0)
mbeg mend (l-mend 0) (end-0 (match-end 0))
(re-depth+1 (if (zerop ivy--subexps)
1
(1+ (avy-migemo--swiper-cal-old-re-depth re)))))
(while (and (< i re-depth+1) (< l-mend end-0))
(setq mbeg (match-beginning i)
mend (match-end i))
(when (and mbeg (<= l-mend mbeg) mend)
(let ((overlay (make-overlay
mbeg
(if (> i 0) (setq l-mend mend) mend)))
(face
(cond ((zerop ivy--subexps)
(cadr swiper-faces))
((zerop i)
(car swiper-faces))
(t
(nth (1+ (mod (+ i-face 2) (1- (length swiper-faces))))
swiper-faces)))))
(push overlay swiper--overlays)
(overlay-put overlay 'face face)
(overlay-put overlay 'window (ivy-state-window ivy-last))
(overlay-put overlay 'priority i-face))
(cl-incf i-face))
(cl-incf i)))))))))
(byte-compile 'swiper--add-overlays-migemo)
(avy-migemo-add-names 'swiper--add-overlays-migemo)
(provide 'swiper--add-overlays-migemo)))
(provide 'avy-migemo-e.g.swiper)
;;; avy-migemo-e.g.swiper.el ends here
avy-migemo-0.3.2/avy-migemo-e.g.zzz-to-char.el 0000644 0000000 0000000 00000004607 12673744343 017232 0 ustar 00 0000000 0000000 ;;; avy-migemo-e.g.zzz-to-char.el --- A setting example of avy-migemo for zzz-to-char -*- lexical-binding: t; no-byte-compile: t -*-
;; Copyright (C) 2015-2016 momomo5717
;; Author: momomo5717
;; URL: https://github.com/momomo5717/avy-migemo
;; 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 3 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, see .
;;; Commentary:
;; This file is a setting example of avy-migemo for zzz-to-char.
;;
;; (require 'avy-migemo-e.g.zzz-to-char)
;;
;; ;; If you want to remove this config from `avy-migemo-function-names' in a init file,
;; (with-eval-after-load 'zzz-to-char--base-migemo
;; (avy-migemo-remove-names 'zzz-to-char--base-migemo))
;;; Code:
;; For using zzz-to-char ( `zzz-to-char--base' ) with migemo
(with-eval-after-load "avy-migemo"
(with-eval-after-load "zzz-to-char"
(defun zzz-to-char--base-migemo (char n-shift)
"The same as `zzz-to-char--base' except for using migemo."
(let ((p (point))
(avy-all-windows nil))
(avy-with zzz-to-char
(avy--generic-jump
(if (= 13 char)
"\n"
;; Adapt for migemo
(avy-migemo-regex-quote-concat (string char)))
nil avy-style
(max (- p zzz-to-char-reach)
(window-start))
(min (+ p zzz-to-char-reach)
(window-end (selected-window) t))))
(let ((n (point)))
(when (/= n p)
(cl-destructuring-bind (beg . end)
(if (> n p)
(cons p (- (1+ n) n-shift))
(cons (+ n n-shift) p))
(goto-char end)
(kill-region beg end))))))
(byte-compile 'zzz-to-char--base-migemo)
(avy-migemo-add-names 'zzz-to-char--base-migemo)
(provide 'zzz-to-char--base-migemo)))
(provide 'avy-migemo-e.g.zzz-to-char)
;;; avy-migemo-e.g.zzz-to-char.el ends here
avy-migemo-0.3.2/avy-migemo-pkg.el 0000644 0000000 0000000 00000000612 12673744343 015143 0 ustar 00 0000000 0000000 (define-package "avy-migemo" "0.3.2" "avy with migemo"
'((emacs "24.4")
(avy "0.4.0")
(migemo "1.9"))
:commit "ce87777bea76c45be5f185e9fe356a8efe5c2d16" :authors
'(("momomo5717"))
:maintainers
'(("momomo5717"))
:maintainer
'("momomo5717")
:keywords
'("avy" "migemo")
:url "https://github.com/momomo5717/avy-migemo")
;; Local Variables:
;; no-byte-compile: t
;; End:
avy-migemo-0.3.2/avy-migemo.el 0000644 0000000 0000000 00000054646 12673744343 014404 0 ustar 00 0000000 0000000 ;;; avy-migemo.el --- avy with migemo -*- lexical-binding: t -*-
;; Copyright (C) 2015-2016 momomo5717
;; Keywords: avy, migemo
;; Version: 0.3.2
;; Package-Requires:((emacs "24.4") (avy "0.4.0") (migemo "1.9"))
;; Author: momomo5717
;; URL: https://github.com/momomo5717/avy-migemo
;; 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 3 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.
;; For a full copy of the GNU General Public License
;; see .
;;; Commentary:
;; This package is a minor mode of avy for using migemo.
;;
;; Migemo which is a library for incremental search has Japanese dictionaries.
;; It could be used as abbreviation matching for other languages
;; by preparing user's migemo dictionaries or customizing `avy-migemo-get-function'.
;;
;; For example, if url is defined in a migemo dictionary as ftp, http, and so on,
;; these words also can be added to avy's candidates.
;;
;;
;; The following functions are provided:
;;
;; + avy-migemo-goto-char
;; + avy-migemo-goto-char-2
;; + avy-migemo-goto-char-in-line
;; + avy-migemo-goto-char-timer
;; + avy-migemo-goto-subword-1
;; + avy-migemo-goto-word-1
;; + avy-migemo-isearch
;; + avy-migemo--overlay-at
;; + avy-migemo--overlay-at-full
;; + avy-migemo--read-candidates
;;
;; These are the same as avy's predefined functions
;; except for adding candidates via migemo (simply using migemo instead of `regexp-quote').
;;
;; The following extensions are available:
;;
;; + avy-migemo-e.g.zzz-to-char.el
;; + avy-migemo-e.g.swiper.el
;; + avy-migemo-e.g.counsel.el
;;
;; Further information is available from:
;; https://github.com/momomo5717/avy-migemo (README.org or README.jp.org)
;; Setup:
;; (add-to-list 'load-path "/path/to/avy-migemo")
;; (require 'avy-migemo)
;; ;; `avy-migemo-mode' overrides avy's predefined functions using `advice-add'.
;; (avy-migemo-mode 1)
;; (global-set-key (kbd "M-g m") 'avy-migemo-mode)
;;
;; ;; If you would like to restrict the length of displayed keys within 2
;; ;; for `avy-style' of at-full, `avy-migemo-at-full-max' provides it.
;; (custom-set-variables '(avy-migemo-at-full-max 2))
;;; Code:
(require 'avy)
(require 'migemo)
(require 'cl-lib)
(defgroup avy-migemo nil
"avy with migemo."
:group 'avy
:prefix "avy-migemo-")
(defcustom avy-migemo-get-function 'migemo-search-pattern-get
"Getter function of migemo.
It takes a string and returns a regular expression."
:type 'function)
(defcustom avy-migemo-function-names
'(avy-migemo-goto-char
avy-migemo-goto-char-2
avy-migemo-goto-char-in-line
avy-migemo-goto-char-timer
avy-migemo-goto-subword-1
avy-migemo-goto-word-1
avy-migemo-isearch
avy-migemo--overlay-at
avy-migemo--overlay-at-full)
"Function names for overriding avy's functions."
:type '(list function)
:set (lambda (symbol value)
(if (and (boundp symbol) (boundp 'avy-migemo-mode))
(let ((avy-migemo-mode-p avy-migemo-mode))
(ignore-errors (when avy-migemo-mode-p (avy-migemo-mode -1)))
(set symbol value)
(ignore-errors (when avy-migemo-mode-p (avy-migemo-mode 1))))
(set symbol value))))
(defcustom avy-migemo-regex-cache-clear-hook nil
"Normal hook run at the end of `avy-migemo-regex-cache-clear'."
:type 'hook)
;;;###autoload
(defun avy-migemo-add-names (&rest names)
"Add NAMES to the front of `avy-migemo-function-names'."
(let ((names (nconc (cl-loop for name in (cl-delete-duplicates names)
unless (memq name avy-migemo-function-names)
collect name)
avy-migemo-function-names)))
(custom-set-variables `(avy-migemo-function-names ',names))
avy-migemo-function-names))
;;;###autoload
(defun avy-migemo-remove-names (&rest names)
"Remove NAMES from `avy-migemo-function-names'."
(let ((names (cl-loop for name in avy-migemo-function-names
unless (memq name names)
collect name)))
(custom-set-variables `(avy-migemo-function-names ',names))
avy-migemo-function-names))
;;;###autoload
(define-minor-mode avy-migemo-mode
"Override avy's functions."
:global t
(dolist (name avy-migemo-function-names)
(let ((predefined-name
(intern (replace-regexp-in-string
"-migemo" "" (symbol-name name)))))
(if avy-migemo-mode
(unless (eq predefined-name name)
(advice-add predefined-name :override name))
(advice-remove predefined-name name)))))
;;;###autoload
(defun avy-migemo-disable-around (orig-f &rest orig-args)
"Advice for a function incompatible with `avy-migemo-mode'.
e.g. \(advice-add 'counsel-locate :around #'avy-migemo-disable-around\)"
(if (not avy-migemo-mode)
(apply orig-f orig-args)
(avy-migemo-mode -1)
(unwind-protect
(apply orig-f orig-args)
(avy-migemo-mode 1))))
(defvar avy-migemo--regex-cache
(make-hash-table :test #'equal)
"Migemo's regexp cache.")
;;;###autoload
(defun avy-migemo-regex-cache-clear ()
"Clear `avy-migemo--regex-cache'."
(interactive)
(clrhash avy-migemo--regex-cache)
(run-hooks 'avy-migemo-regex-cache-clear-hook))
;;;###autoload
(defun avy-migemo-regex-p (regex)
"Retrun nil if REGEX is invalid."
(ignore-errors
(string-match regex "")
regex))
;;;###autoload
(defun avy-migemo-regex-concat (pattern)
"Return migemo's regexp which includes PATTERN in last place.
Return PATTERN if migemo's regexp is invalid."
(let ((cache (gethash pattern avy-migemo--regex-cache)))
(if cache cache
(puthash pattern
(let ((regex (funcall avy-migemo-get-function pattern)))
(if (avy-migemo-regex-p regex)
(concat "\\(" regex "\\|" pattern "\\)")
pattern))
avy-migemo--regex-cache))))
;;;###autoload
(defun avy-migemo-regex-quote-concat (pattern)
"Return migemo's regexp which includes quoted PATTERN in last place.
Return quoted PATTERN if migemo's regexp is invalid."
(let* ((quoted-pattern (regexp-quote pattern))
(cache (gethash quoted-pattern avy-migemo--regex-cache)))
(if cache cache
(puthash quoted-pattern
(let ((regex (funcall avy-migemo-get-function pattern)))
(if (avy-migemo-regex-p regex)
(concat "\\(" regex "\\|" quoted-pattern "\\)")
quoted-pattern))
avy-migemo--regex-cache))))
;; avy functions for migemo
(defcustom avy-migemo-at-full-max nil
"Max length of keys."
:type '(choice (integer :tag "Restrict the length of displayed keys for `avy-style' of at-full.")
(const :tag "Disabled" nil)))
(defcustom avy-migemo-pad-char (string-to-char " ")
"Pad char."
:type 'character)
(defcustom avy-migemo-pad-char-visual-line-mode ?_
"Pad char for `visual-line-mode'."
:type 'character)
(defvar avy-migemo--pad-style nil)
(defun avy-migemo--pad-char (&optional style)
"Return a pad character of STYLE."
(cl-case (or style avy-migemo--pad-style)
(visual-line-mode avy-migemo-pad-char-visual-line-mode)
(otherwise avy-migemo-pad-char)))
(defun avy-migemo--pad-string (c n)
"Retrun a pad string of C of length N."
(let ((pad-str (make-string n c)))
(if (or avy-background (not (eq c ? )))
(propertize pad-str 'face 'avy-background-face)
pad-str)))
(defun avy-migemo--rest-old-str (old-str+ len)
"Return the padded string of a part of OLD-STR+.
LEN is compared with string width of OLD-STR+."
(cl-loop
with old-ls = (string-to-list old-str+)
with pre-width = 0
with char-count = 0
until (zerop len) do
(cond
((and (zerop char-count) (car old-ls))
(setq pre-width (max (char-width (pop old-ls)) 1))
(cl-decf len)
(cl-incf char-count))
((> pre-width char-count)
(cl-decf len)
(cl-incf char-count))
((null old-ls)
(setq len 0 pre-width 0 char-count 0))
(t (setq char-count 0)))
finally return
(concat (avy-migemo--pad-string (avy-migemo--pad-char)
(- pre-width char-count))
old-ls)))
(defun avy-migemo--overlay-at (path leaf)
"The same as `avy--overlay-at' except adapting it for migemo."
(let* ((path (mapcar #'avy--key-to-char path))
(str (propertize
(string (car (last path)))
'face 'avy-lead-face))
(avy-migemo--pad-style
(when (with-selected-window (avy-candidate-wnd leaf)
(bound-and-true-p visual-line-mode))
'visual-line-mode)))
(avy--overlay
str
(avy-candidate-beg leaf) nil
(avy-candidate-wnd leaf)
(lambda (str old-str)
(cond ((string= old-str "\n")
(concat str "\n"))
;; Adapt for migemo
((= (max (string-width old-str) 1) 1)
str)
(t
(concat str (avy-migemo--rest-old-str old-str 1))))))))
;; Dynamically bound in `avy-migemo--overlay-at-full'.
(defvar avy-migemo--vbeg)
(defvar avy-migemo--vend)
(defvar avy-migemo--vend-pre)
(defvar avy-migemo--vwbeg)
(defvar avy-migemo--visual-line-mode-p)
(defun avy-migemo--ovl-vbeg (ovl)
"Return the beginning position of the visual line of OVL."
(overlay-get ovl 'avy-migemo--vbeg))
(defun avy-migemo--ovl-vend (ovl)
"Return the end position of the visual line of OVL."
(overlay-get ovl 'avy-migemo--vend))
(defun avy-migemo--ovl-vwnd (ovl)
"Return the window of OVL."
(overlay-get ovl 'window))
(defun avy-migemo--ovl-vwbeg (ovl)
"Return the beginning position of the window of OVL."
(overlay-get ovl 'avy-migemo--vwbeg))
(defun avy-migemo--overlay-at-full-vend-position (beg)
"Return the point of the end of the visual line for `visual-line-mode'.
Set variables for distinguish the beginning position of the visual line."
(let* ((ovl (car-safe avy--overlays-lead))
(vbeg (when ovl (avy-migemo--ovl-vbeg ovl)))
(vend (when ovl (avy-migemo--ovl-vend ovl)))
(vwnd (when ovl (avy-migemo--ovl-vwnd ovl)))
(vwbeg (when ovl (avy-migemo--ovl-vwbeg ovl))))
(setq avy-migemo--visual-line-mode-p t)
(setq avy-migemo--vend-pre nil)
(save-excursion
(unless (eq beg (point)) (goto-char beg))
(prog2
(end-of-visual-line)
(setq avy-migemo--vend (point))
(if (and (eq (selected-window) vwnd)
(eq avy-migemo--vend vend))
(setq avy-migemo--vbeg vbeg
avy-migemo--vwbeg vwbeg)
(goto-char beg)
(beginning-of-visual-line)
(setq avy-migemo--vbeg (point)
avy-migemo--vwbeg (window-start)))
(when (and (eq beg avy-migemo--vbeg)
(> beg avy-migemo--vwbeg))
(goto-char (1- beg))
(end-of-visual-line)
(setq avy-migemo--vend-pre (point)))))))
(defun avy-migemo--overlay-at-full-vpre-space (beg)
"Return one space for `visual-line-mode'.
if BEG is equal to `avy-migemo--vend-pre'."
(if (and (eq avy-style 'at-full)
avy-migemo--vend-pre
(eq beg avy-migemo--vend-pre))
" " ""))
(defun avy-migemo--overlay-at-full-vlen (beg len str)
"Restrict len for `visual-line-mode' via BEG LEN STR.
Also restrict LEN if `avy-migemo-at-full-max' is an integer,"
(if (eq avy-style 'at-full)
(let ((len (cond ((and avy-migemo--visual-line-mode-p
(eq beg avy-migemo--vend))
(char-width (aref str 0)))
((and avy-migemo--visual-line-mode-p
(> beg avy-migemo--vend))
(min len (- beg avy-migemo--vend)))
(t len))))
(if (integerp avy-migemo-at-full-max)
(min len avy-migemo-at-full-max) len))
len))
(defun avy-migemo--overlay-at-full-concat (str old-str+ beg len)
"Return a string.
STR / OLD-STR+ is a string.
BEG / LEN is an integer."
(let* ((other-char-p
(cl-loop for c across str
for i from 0
unless (or (memq c avy-keys) (eq c ? ))
return i))
(str (if other-char-p
(substring str 0 other-char-p) str))
(len (if other-char-p (length str) len))
(vlen (avy-migemo--overlay-at-full-vlen beg len str))
(vstr (if (eq len vlen) str (substring str 0 vlen)))
(avy-migemo--pad-style
(when avy-migemo--visual-line-mode-p 'visual-line-mode)))
(concat vstr (avy-migemo--rest-old-str old-str+ vlen))))
(defun avy-migemo--overlay-at-full-add-vpoint (ovl-list)
"Add point for distinguish the beg/end of visual line."
(when avy-migemo--visual-line-mode-p
(let* ((ovl (car ovl-list))
(pre-space (avy-migemo--overlay-at-full-vpre-space (overlay-start ovl))))
(overlay-put ovl 'avy-migemo--vbeg avy-migemo--vbeg)
(overlay-put ovl 'avy-migemo--vend avy-migemo--vend)
(overlay-put ovl 'avy-migemo--vwbeg avy-migemo--vwbeg)
(unless (string= pre-space "")
;; One space is added at the beginning position of the visual line
;; for keeping layout as much as possible.
(overlay-put ovl 'before-string pre-space))))
ovl-list)
(defun avy-migemo--overlay-at-full (path leaf)
"The same as `avy--overlay-at-full' except adapting it for migemo."
(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
;; Adapt for migemo
avy-migemo--old-str+
avy-migemo--visual-line-mode-p
avy-migemo--vbeg avy-migemo--vend
avy-migemo--vwbeg)
(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)
(avy-migemo--overlay-at-full-vend-position beg)
(line-end-position)))
(len-and-str (avy--update-offset-and-str len str lep)))
(setq len (car len-and-str))
(setq str (cdr len-and-str))
(setq end (if (= beg lep)
(1+ beg)
(min (+ beg
(if (eq (char-after) ?\t)
1
len))
lep)))
(setq avy-migemo--old-str+ (buffer-substring beg (min end (point-max))))
(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-migemo--overlay-at-full-add-vpoint
(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
;; Adapt for migemo
(avy-migemo--overlay-at-full-concat str avy-migemo--old-str+ beg len))))))))
;;;###autoload
(defun avy-migemo-goto-char (char &optional arg)
"The same as `avy-migemo-goto-char' except for the candidates via migemo."
(interactive (list (read-char "char: " t)
current-prefix-arg))
(avy-with avy-goto-char
(avy--generic-jump
(if (= 13 char)
"\n"
;; Adapt for migemo
(avy-migemo-regex-quote-concat (string char)))
arg
avy-style)))
;;;###autoload
(defun avy-migemo-goto-char-2 (char1 char2 &optional arg)
"The same as `avy-goto-char-2' except for the candidates via migemo."
(interactive (list (read-char "char 1: " t)
(read-char "char 2: " t)
current-prefix-arg))
(avy-with avy-goto-char-2
(avy--generic-jump
;; Adapt for migemo
(avy-migemo-regex-quote-concat (string char1 char2))
arg
avy-style)))
;;;###autoload
(defun avy-migemo-goto-char-in-line (char)
"The same as `avy-goto-char-in-line' except for the candidates via migemo."
(interactive (list (read-char "char: " t)))
(avy-with avy-goto-char
(avy--generic-jump
;; Adapt for migemo
(avy-migemo-regex-quote-concat (string char))
avy-all-windows
avy-style
(line-beginning-position)
(line-end-position))))
(defun avy-migemo--read-candidates ()
"The same as `avy--read-candidates' except for the candidates via migemo."
(let ((str "") char break overlays regex)
(unwind-protect
(progn
(while (and (not break)
(setq char
(read-char (format "char%s: "
(if (string= str "")
str
(format " (%s)" str)))
t
(and (not (string= str ""))
avy-timeout-seconds))))
;; Unhighlight
(dolist (ov overlays)
(delete-overlay ov))
(setq overlays nil)
(cond
;; Handle RET
((= char 13)
(setq break t))
;; Handle DEL
((= char 127)
(let ((l (length str)))
(when (>= l 1)
(setq str (substring str 0 (1- l))))))
(t
(setq str (concat str (list char)))))
;; Highlight
(when (>= (length str) 1)
;; Adapt for migemo
(setq regex (avy-migemo-regex-quote-concat str))
(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))
(while (re-search-forward regex (cdr pair) t)
(unless (get-char-property (1- (point)) 'invisible)
(let ((ov (make-overlay
(match-beginning 0)
(match-end 0))))
(setq found t)
(push ov overlays)
(overlay-put ov 'window (selected-window))
(overlay-put ov 'face 'avy-goto-char-timer-face)))))))
;; No matches at all, so there's surely a typo in the input.
(unless found (beep)))))
(nreverse (mapcar (lambda (ov)
(cons (cons (overlay-start ov)
(overlay-end ov))
(overlay-get ov 'window)))
overlays)))
(dolist (ov overlays)
(delete-overlay ov)))))
;;;###autoload
(defun avy-migemo-goto-char-timer (&optional arg)
"The same as `avy-goto-char-timer' except for the candidates via migemo."
(interactive "P")
(let ((avy-all-windows (if arg
(not avy-all-windows)
avy-all-windows)))
(avy-with avy-goto-char-timer
(avy--process
;; Adapt for migemo
(avy-migemo--read-candidates)
(avy--style-fn avy-style)))))
;;;###autoload
(defun avy-migemo-goto-subword-1 (char &optional arg)
"The same as `avy-goto-subword-1' except for the candidates via migemo."
(interactive (list (read-char "char: " t)
current-prefix-arg))
(avy-with avy-goto-subword-1
(let ((char (downcase char))
;; Adapt for migemo
(regex (avy-migemo-regex-quote-concat (string char))))
(avy-goto-subword-0
arg (lambda () (let ((char-after (char-after))) ; Adapt for migemo
(or (eq (downcase char-after) char)
(string-match-p regex (string char-after)))))))))
;;;###autoload
(defun avy-migemo-goto-word-1 (char &optional arg)
"The same as `avy-goto-word-1' except for the candidates via migemo."
(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))
;; Adapt for migemo
(avy-migemo-regex-quote-concat str))
(t ;; Adapt for migemo
(concat
"\\b"
(avy-migemo-regex-concat str))))))
(avy--generic-jump regex arg avy-style))))
;;;###autoload
(defun avy-migemo-isearch ()
"The same as `avy-isearch' except for the candidates via migemo."
(interactive)
(avy-with avy-isearch
(let ((avy-background nil))
(avy--process
(avy--regex-candidates
;; Adapt for migemo
(avy-migemo-regex-concat isearch-string))
(avy--style-fn avy-style))
(isearch-done))))
(define-obsolete-function-alias 'avy-migemo-check-regex
'avy-migemo-regex-p "0.2.5")
(define-obsolete-function-alias 'avy-migemo--read-string-timer
'avy-migemo--read-candidates "0.2.9")
(provide 'avy-migemo)
;;; avy-migemo.el ends here