company-ispell.el 2.51 KB
Newer Older
1
;;; company-ispell.el --- company-mode completion backend using Ispell
2

Dmitry Gutov's avatar
Dmitry Gutov committed
3
;; Copyright (C) 2009-2011, 2013-2016  Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14

;; Author: Nikolaj Schumacher

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs is distributed in the hope that it will be useful,
15 16 17
;; 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.
18

19
;; You should have received a copy of the GNU General Public License
20 21 22 23 24 25 26
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.


;;; Commentary:
;;

;;; Code:
27

28
(require 'company)
Bozhidar Batsov's avatar
Bozhidar Batsov committed
29
(require 'cl-lib)
30 31
(require 'ispell)

32
(defgroup company-ispell nil
33
  "Completion backend using Ispell."
34 35
  :group 'company)

36
(defcustom company-ispell-dictionary nil
37
  "Dictionary to use for `company-ispell'.
38 39 40 41
If nil, use `ispell-complete-word-dict'."
  :type '(choice (const :tag "default (nil)" nil)
                 (file :tag "dictionary" t)))

42 43
(defvar company-ispell-available 'unknown)

44 45 46 47 48
(defalias 'company-ispell--lookup-words
  (if (fboundp 'ispell-lookup-words)
      'ispell-lookup-words
    'lookup-words))

49 50 51 52
(defun company-ispell-available ()
  (when (eq company-ispell-available 'unknown)
    (condition-case err
        (progn
53
          (company-ispell--lookup-words "WHATEVER")
54 55
          (setq company-ispell-available t))
      (error
56
       (message "Company-Ispell: %s" (error-message-string err))
57 58 59
       (setq company-ispell-available nil))))
  company-ispell-available)

60
;;;###autoload
61
(defun company-ispell (command &optional arg &rest ignored)
62
  "`company-mode' completion backend using Ispell."
63
  (interactive (list 'interactive))
Bozhidar Batsov's avatar
Bozhidar Batsov committed
64
  (cl-case command
65 66 67
    (interactive (company-begin-backend 'company-ispell))
    (prefix (when (company-ispell-available)
              (company-grab-word)))
68
    (candidates
69 70 71
     (let ((words (company-ispell--lookup-words
                   arg
                   (or company-ispell-dictionary ispell-complete-word-dict)))
72 73 74 75 76 77
           (completion-ignore-case t))
       (if (string= arg "")
           ;; Small optimization.
           words
         ;; Work around issue #284.
         (all-completions arg words))))
78
    (sorted t)
Dmitry Gutov's avatar
Dmitry Gutov committed
79
    (ignore-case 'keep-prefix)))
80 81 82

(provide 'company-ispell)
;;; company-ispell.el ends here