[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/jinx f401da2c50 1/3: Add jinx-languages command to swit
From: |
ELPA Syncer |
Subject: |
[elpa] externals/jinx f401da2c50 1/3: Add jinx-languages command to switch languages (Fix #13) |
Date: |
Tue, 28 Mar 2023 07:58:50 -0400 (EDT) |
branch: externals/jinx
commit f401da2c5031e992985d90769728f714d6e7f01c
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>
Add jinx-languages command to switch languages (Fix #13)
---
CHANGELOG.org | 4 ++++
README.org | 16 ++++++++--------
jinx-mod.c | 31 ++++++++++++++++++++++++++-----
jinx.el | 43 +++++++++++++++++++++++++++++++++----------
4 files changed, 71 insertions(+), 23 deletions(-)
diff --git a/CHANGELOG.org b/CHANGELOG.org
index 9fab145472..7d9b6d2191 100644
--- a/CHANGELOG.org
+++ b/CHANGELOG.org
@@ -2,6 +2,10 @@
#+author: Daniel Mendler
#+language: en
+* Development
+
+- Add =jinx-languages= command to switch languages locally or globally.
+
* Version 0.4 (2023-03-27)
- =jinx-languages=: Fall back to =$LANG= environment variable, since
diff --git a/README.org b/README.org
index 509845f50c..1e7c0a0c6e 100644
--- a/README.org
+++ b/README.org
@@ -41,9 +41,8 @@ Ubuntu, install the packages =libenchant-2-2=,
=libenchant-2-dev= and =pkg-confi
* Usage
-Jinx offers three auto-loaded entry points , the modes =global-jinx-mode=,
-=jinx-mode= and the command =jinx-correct=. You can either enable
=global-jinx-mode=
-or add =jinx-mode= to the hooks of the modes.
+Jinx offers two modes =global-jinx-mode= and =jinx-mode=. You can either enable
+=global-jinx-mode= or add =jinx-mode= to the hooks of the modes.
#+begin_src emacs-lisp
(add-hook 'emacs-startup-hook #'global-jinx-mode)
@@ -52,11 +51,12 @@ or add =jinx-mode= to the hooks of the modes.
(add-hook hook #'jinx-mode))
#+end_src
-In order to correct misspellings bind =jinx-correct= to a convenient key in
your
-configuration. Jinx is independent of the Ispell package, so you can reuse the
-binding =M-$= which is bound to =ispell-word= by default. When pressing =M-$=,
Jinx
-offers correction suggestions for the misspelling next to point. If the prefix
-key =C-u= is pressed, the entire buffer is spell-checked.
+Furthermore Jinx brings two auto-loaded commands =jinx-correct= and
+=jinx-languages=. In order to correct misspellings bind =jinx-correct= to a
+convenient key in your configuration. Jinx is independent of the Ispell
package,
+so you can reuse the binding =M-$= which is bound to =ispell-word= by default.
When
+pressing =M-$=, Jinx offers correction suggestions for the misspelling next to
+point. If the prefix key =C-u= is pressed, the entire buffer is spell-checked.
#+begin_src emacs-lisp
(keymap-global-set "<remap> <ispell-word>" #'jinx-correct)
diff --git a/jinx-mod.c b/jinx-mod.c
index 69d1958fc8..a73acd3c12 100644
--- a/jinx-mod.c
+++ b/jinx-mod.c
@@ -20,12 +20,16 @@ along with GNU Emacs. If not, see
<https://www.gnu.org/licenses/>. */
#include <string.h>
#include <stdlib.h>
+#define jinx_unused(var) _##var __attribute__((unused))
+#define jinx_autofree __attribute__((cleanup(jinx_autofree_cleanup)))
+
int plugin_is_GPL_compatible;
static EnchantBroker* broker = 0;
-#define jinx_unused(var) _##var __attribute__((unused))
-#define jinx_autofree __attribute__((cleanup(jinx_autofree_cleanup)))
+static EnchantBroker* jinx_broker(void) {
+ return broker ? broker : (broker = enchant_broker_init());
+}
static void jinx_autofree_cleanup(char **p) {
free(*(void**)p);
@@ -72,10 +76,8 @@ static void jinx_free_dict(void* dict) {
static emacs_value jinx_dict(emacs_env* env, ptrdiff_t jinx_unused(nargs),
emacs_value args[], void* jinx_unused(data)) {
- if (!broker)
- broker = enchant_broker_init();
jinx_autofree char* str = jinx_cstr(env, args[0]);
- EnchantDict* dict = str ? enchant_broker_request_dict(broker, str) : 0;
+ EnchantDict* dict = str ? enchant_broker_request_dict(jinx_broker(), str)
: 0;
return dict
? env->make_user_ptr(env, jinx_free_dict, dict)
: env->intern(env, "nil");
@@ -102,6 +104,24 @@ static emacs_value jinx_describe(emacs_env* env, ptrdiff_t
jinx_unused(nargs),
return data[1];
}
+static void jinx_langs_cb(const char* const lang_tag,
+ const char* const jinx_unused(provider_name),
+ const char* const jinx_unused(provider_desc),
+ const char* const jinx_unused(provider_file),
+ void* data) {
+ emacs_env* env = ((emacs_env**)data)[0];
+ ((emacs_value*)data)[1] = jinx_cons(env,
+ jinx_str(env, lang_tag),
+ ((emacs_value*)data)[1]);
+}
+
+static emacs_value jinx_langs(emacs_env* env, ptrdiff_t jinx_unused(nargs),
+ emacs_value args[], void* jinx_unused(data)) {
+ void* data[] = { env, env->intern(env, "nil") };
+ enchant_broker_list_dicts(jinx_broker(), jinx_langs_cb, data);
+ return data[1];
+}
+
static emacs_value jinx_check(emacs_env* env, ptrdiff_t jinx_unused(nargs),
emacs_value args[], void* jinx_unused(data)) {
EnchantDict* dict = env->get_user_ptr(env, args[0]);
@@ -158,6 +178,7 @@ int emacs_module_init(struct emacs_runtime *runtime) {
jinx_defun(env, "jinx--mod-check", 2, 2, jinx_check);
jinx_defun(env, "jinx--mod-add", 2, 2, jinx_add);
jinx_defun(env, "jinx--mod-dict", 1, 1, jinx_dict);
+ jinx_defun(env, "jinx--mod-langs", 0, 0, jinx_langs);
jinx_defun(env, "jinx--mod-describe", 1, 1, jinx_describe);
jinx_defun(env, "jinx--mod-wordchars", 1, 1, jinx_wordchars);
return 0;
diff --git a/jinx.el b/jinx.el
index c5a751ed88..ac4fd92cd6 100644
--- a/jinx.el
+++ b/jinx.el
@@ -49,22 +49,23 @@
;; and programmable predicates. Jinx comes preconfigured for the most
;; important major modes.
;;
-;; Jinx offers three auto-loaded entry points , the modes
-;; `global-jinx-mode', `jinx-mode' and the command `jinx-correct'.
-;; You can either enable `global-jinx-mode' or add `jinx-mode' to the
-;; hooks of the modes.
+;; Jinx offers the auto-loaded modes `global-jinx-mode' and
+;; `jinx-mode'. You can either enable `global-jinx-mode' or add
+;; `jinx-mode' to the hooks of the modes.
;;
;; (add-hook 'emacs-startup-hook #'global-jinx-mode)
;;
;; (dolist (hook '(text-mode-hook prog-mode-hook conf-mode-hook))
;; (add-hook hook #'jinx-mode))
;;
-;; In order to correct misspellings bind `jinx-correct' to a
-;; convenient key in your configuration. Jinx is independent of the
-;; Ispell package, so you can reuse the binding M-$ which is bound to
-;; `ispell-word' by default. When pressing M-$, Jinx offers
-;; correction suggestions for the misspelling next to point. If the
-;; prefix key C-u is pressed, the entire buffer is spell-checked.
+;; Furthermore Jinx brings two auto-loaded commands `jinx-correct' and
+;; `jinx-languages'. In order to correct misspellings bind
+;; `jinx-correct' to a convenient key in your configuration. Jinx is
+;; independent of the Ispell package, so you can reuse the binding M-$
+;; which is bound to `ispell-word' by default. When pressing M-$,
+;; Jinx offers correction suggestions for the misspelling next to
+;; point. If the prefix key C-u is pressed, the entire buffer is
+;; spell-checked.
;;
;; (keymap-global-set "<remap> <ispell-word>" #'jinx-correct)
;;
@@ -242,6 +243,7 @@ Predicate may return a position to skip forward.")
(declare-function jinx--mod-suggest nil)
(declare-function jinx--mod-dict nil)
(declare-function jinx--mod-describe nil)
+(declare-function jinx--mod-langs nil)
(declare-function jinx--mod-wordchars nil)
(declare-function org-fold-core-region "org-fold-core")
(declare-function org-fold-core-get-regions "org-fold-core")
@@ -613,6 +615,27 @@ Return list of overlays, see `jinx--get-overlays'."
;;;; Public commands
+;;;###autoload
+(defun jinx-languages (&optional global)
+ "Change languages locally.
+If predicate argument GLOBAL is given, change the languages globally."
+ (interactive "*P")
+ (jinx--load-module)
+ (when-let ((langs
+ (completing-read-multiple
+ (format "Change languages (%s): "
+ (string-join (ensure-list jinx-languages) ", "))
+ (delete-dups (jinx--mod-langs)) nil t)))
+ (when (length= langs 1)
+ (setq langs (car langs)))
+ (if (not global)
+ (setq-local jinx-languages langs)
+ (when (local-variable-p 'jinx-languages)
+ (kill-local-variable 'jinx-languages))
+ (setq-default jinx-languages langs))
+ (jinx-mode -1)
+ (jinx-mode 1)))
+
;;;###autoload
(defun jinx-correct (&optional all)
"Correct nearest misspelled word.