cape-jinx-completion/cape-jinx-completion.el

117 lines
4.3 KiB
EmacsLisp
Raw Permalink Normal View History

;;; cape-jinx-completion.el --- Completion At Point Extensions using Jinx spell checking -*- lexical-binding: t -*-
2023-05-17 07:12:49 +00:00
2023-07-03 14:01:07 +00:00
;; Copyright (C) 2021-2023 Adam Kruszewski
2023-05-17 07:12:49 +00:00
;; Author: Adam Kruszewski <adam@kruszewski.name>
;; Maintainer: Adam Kruszewski <adam@kruszewski.name>
;; Created: 2023
;; Version: 1.0
2023-07-03 14:01:07 +00:00
;; Package-Requires: ((emacs "28.1") (compat "29.1.4.0") (cape "0.15") (jinx "0.5"))
2023-05-17 07:12:49 +00:00
;; Homepage: https://code.bsdgeek.org/adam/cape-jinx-completion
2023-07-03 14:01:07 +00:00
;; Keywords: convenience, completion, spell-check
2023-05-17 07:12:49 +00:00
2023-07-03 14:01:07 +00:00
;; This file is not part of GNU Emacs.
2023-05-17 07:12:49 +00:00
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Additional completion backend in the form of Capfs
;; (completion-at-point-functions), using underlying C module provided
;; by Jinx just-in-time spell-checking package
2023-05-17 07:12:49 +00:00
;; (see https://github.com/minad/jinx ).
;;
;; It doesn't need Jinx enabled, but it uses its configuration
;; and C module bridge to enchant library.
2023-05-17 07:12:49 +00:00
;;
;; Having jinx and cape configured you just need to add:
2023-07-03 14:01:07 +00:00
;; (add-to-list 'completion-at-point-functions #'cape-jinx-completion)
;; cape-jinx-completion to your completions-at-point-functions.
2023-05-17 07:12:49 +00:00
2023-07-03 14:01:07 +00:00
;;; Code:
2023-05-17 07:12:49 +00:00
(require 'cape)
(require 'jinx)
2023-07-03 14:16:45 +00:00
(eval-when-compile
(require 'cl-lib)
(require 'jinx))
2023-05-17 07:12:49 +00:00
;; jinx.el version 0.5 is required
2023-07-03 14:01:07 +00:00
(defun cape-jinx-completion-suggest-for-word (word)
"Return suggestions as a list for `WORD'."
2023-05-17 07:12:49 +00:00
(when (not jinx--dicts)
(when (not (fboundp 'jinx--mod-dict))
2023-05-17 07:12:49 +00:00
(jinx--load-module))
(jinx--load-dicts))
(if (fboundp 'jinx--mod-suggest)
2023-05-17 07:12:49 +00:00
(let* ((result '()))
(dolist (dict jinx--dicts)
(dolist (el (jinx--mod-suggest dict word))
(when (string-prefix-p word el)
(cl-pushnew el result))))
(delete-dups result))
;; this should never happen, as jinx--load-module will bind jinx--mod-suggest as well.
'()))
2023-05-17 07:12:49 +00:00
2023-07-03 14:01:07 +00:00
;; cape-jinx-completion (based on cape-dict)
(defgroup cape-jinx-completion nil
"Completion At Point extensions using Jinx spell checking."
2023-07-03 14:01:07 +00:00
:prefix "cape-jinx-completion"
:group 'cape-jinx-completion)
2023-07-03 14:01:07 +00:00
(defcustom cape-jinx-completion-case-replace 'case-replace
2023-05-17 07:12:49 +00:00
"Preserve case of input.
2023-07-03 14:01:07 +00:00
See `dabbrev-case-replace' for details."
2023-05-17 07:12:49 +00:00
:type '(choice (const :tag "off" nil)
(const :tag "use `case-replace'" case-replace)
(other :tag "on" t)))
2023-07-03 14:01:07 +00:00
(defcustom cape-jinx-completion-case-fold 'case-fold-search
2023-05-17 07:12:49 +00:00
"Case fold search during search.
2023-07-03 14:01:07 +00:00
See `dabbrev-case-fold-search' for details."
2023-05-17 07:12:49 +00:00
:type '(choice (const :tag "off" nil)
(const :tag "use `case-fold-search'" case-fold-search)
(other :tag "on" t)))
2023-07-03 14:01:07 +00:00
(defvar cape-jinx-completion--properties
2023-05-17 07:12:49 +00:00
(list :annotation-function (lambda (_) " Jinx")
:company-kind (lambda (_) 'text)
:exclusive 'no)
2023-07-03 14:01:07 +00:00
"Completion extra properties for `cape-jinx-completion'.")
2023-05-17 07:12:49 +00:00
2023-07-03 14:01:07 +00:00
(defun cape-jinx-completion--list (input)
2023-05-17 07:12:49 +00:00
"Return all words from Jinx spell checking mechanism matching INPUT."
(unless (equal input "")
(cape--case-replace-list
2023-07-03 14:01:07 +00:00
cape-jinx-completion-case-replace input
(cape-jinx-completion-suggest-for-word input))))
2023-05-17 07:12:49 +00:00
;;;###autoload
2023-07-03 14:01:07 +00:00
(defun cape-jinx-completion (&optional interactive)
2023-05-17 07:12:49 +00:00
"Complete word from Jinx spell checking at point.
2023-07-03 14:01:07 +00:00
If INTERACTIVE is nil the function acts like a Capf."
2023-05-17 07:12:49 +00:00
(interactive (list t))
(if interactive
2023-07-03 14:01:07 +00:00
(cape-interactive #'cape-jinx-completion)
2023-05-17 07:12:49 +00:00
(pcase-let ((`(,beg . ,end) (cape--bounds 'word)))
`(,beg ,end
,(cape--table-with-properties
(completion-table-case-fold
2023-07-03 14:01:07 +00:00
(cape--cached-table beg end #'cape-jinx-completion--list #'string-search)
(not (cape--case-fold-p cape-jinx-completion-case-fold)))
:category 'cape-jinx-completion)
2023-07-03 14:07:30 +00:00
,@cape-jinx-completion--properties))))
2023-05-17 07:12:49 +00:00
(provide 'cape-jinx-completion)
;;; cape-jinx-completion.el ends here