Help us understand the problem. What is going on with this article?

ivyインターフェイスを用いたispell/aspellに基づく英単語補完

かなり前の記事に、ispell / aspell による英単語補完をhelmインターフェイスを使って実装した話があった。

今回はそれをhelmでなくivyで実装したわけである。英語アルファベット3文字以上を入力して、M-x ivy-ispell とすれば補完が走る。適当なキーバインドをアサインするのも一興だろう。ちなみにバイナリはispell/aspell/hunspellのどれかを用意しておく必要がある (cf. ispell-program-name)。

(require 'ispell)
(require 'ivy)
(require 'thingatpt)

(defvar ivy-ispell--word-at-point nil)

(defun ivy-ispell--case-function (input)
  (let ((case-fold-search nil))
    (cond ((string-match-p "\\`[A-Z]\\{2\\}" input) 'upcase)
          ((string-match-p "\\`[A-Z]\\{1\\}" input) 'capitalize)
          (t 'identity))))

(defun ivy-ispell--compare-length (a b)
  (< (length a) (length b)))

(defun ivy-ispell--make-candidate ()
  (let ((input (downcase ivy-ispell--word-at-point))
        (case-func (ivy-ispell--case-function
                    ivy-ispell--word-at-point)))
    (when (string-match-p "\\`[a-z]+\\'" input)
      (mapcar case-func
              (sort (ispell-lookup-words (concat input "*")
                                         ispell-complete-word-dict)
                    'ivy-ispell--compare-length)))))

(defun ivy-ispell ()
  (interactive)
  (setq ivy-ispell--word-at-point (thing-at-point 'word))
  (if (not ivy-ispell--word-at-point)
      (message "Nothing to complete.")
    (let ((candidate (ivy-read "Completion: "
                               (funcall 'ivy-ispell--make-candidate)
                               :initial-input ivy-ispell--word-at-point))
          (curpoint (point)))
      (backward-word 1)
      (delete-region (point) curpoint)
      (insert candidate))))

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした