From cf5831c00879a68cfb2d3f9e171856bac8d5f0d7 Mon Sep 17 00:00:00 2001 From: DK Date: Thu, 15 Aug 2024 13:51:13 +0300 Subject: [PATCH 1/2] cache keymaps --- reverse-im.el | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/reverse-im.el b/reverse-im.el index 87101a6..6c7780b 100644 --- a/reverse-im.el +++ b/reverse-im.el @@ -93,6 +93,12 @@ :group 'reverse-im :type `(repeat (choice regexp symbol))) +(defcustom reverse-im-cache-file + nil + "File to cache translation keymap. Don't cache if `nil'." + :group 'reverse-im + :type 'file) + ;;; Storage vars (defvar reverse-im--default-parent nil @@ -190,17 +196,37 @@ (add-to-list 'reverse-im--keymaps-alist (cons input-method translation-keymap)) translation-keymap)))) -;;; User-accessible functions +;; https://stackoverflow.com/questions/2321904/elisp-how-to-save-data-in-a-file +(defun reverse-im--print-to-file (filename data) + (with-temp-file filename + (prin1 data (current-buffer)))) + +(defun reverse-im--read-from-file (filename) + (with-temp-buffer + (insert-file-contents filename) + (cl-assert (eq (point) (point-min))) + (read (current-buffer)))) +;;; User-accessible functions (defun reverse-im-activate (input-method) "Activate the reverse mapping for INPUT-METHOD (can be a list). Example usage: (reverse-im-activate \"ukrainian-computer\")" + (when (and reverse-im-cache-file + (file-exists-p reverse-im-cache-file)) + (setq reverse-im--keymaps-alist + (reverse-im--read-from-file reverse-im-cache-file))) + (let* ((input-methods (if (listp input-method) input-method (list input-method))) (new-parent (make-composed-keymap (mapcar #'reverse-im--im-to-keymap input-methods))) (old-parent (keymap-parent function-key-map))) + + (when (and reverse-im-cache-file + (not (file-exists-p reverse-im-cache-file))) + (reverse-im--print-to-file reverse-im-cache-file reverse-im--keymaps-alist)) + (unless (equal new-parent old-parent) (setq reverse-im--default-parent old-parent) (set-keymap-parent function-key-map new-parent)))) @@ -305,15 +331,15 @@ Translate all chars, unless `this-command' is not in `reverse-im-read-char-exclu (reverse-im-activate reverse-im-input-methods) (when (reverse-im--char-fold-p) (setq reverse-im--char-fold-include char-fold-include) - (customize-set-variable 'char-fold-include - (append char-fold-include - (reverse-im-char-fold-include)))) + (setq char-fold-include + (append char-fold-include + (reverse-im-char-fold-include)))) (when reverse-im-read-char-advice-function (advice-add #'read-char :around reverse-im-read-char-advice-function) (advice-add #'read-char-exclusive :around reverse-im-read-char-advice-function))) (reverse-im-deactivate t) (when (reverse-im--char-fold-p) - (customize-set-variable 'char-fold-include reverse-im--char-fold-include)) + (setq char-fold-include reverse-im--char-fold-include)) (when reverse-im-read-char-advice-function (advice-remove 'read-char reverse-im-read-char-advice-function) (advice-remove 'read-char-exclusive reverse-im-read-char-advice-function)))) From 481da44737244f79016359762ad1fa65d7a25d60 Mon Sep 17 00:00:00 2001 From: DK Date: Thu, 15 Aug 2024 13:53:58 +0300 Subject: [PATCH 2/2] fix readme --- README.org | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.org b/README.org index 6cf5a3c..f1cc533 100644 --- a/README.org +++ b/README.org @@ -24,9 +24,13 @@ :bind ("M-T" . reverse-im-translate-word) ; fix a word in wrong layout :custom - (reverse-im-char-fold t) ; use lax matching + ;; cache generated keymaps + (reverse-im-cache-file (locate-user-emacs-file "reverse-im-cache.el")) + ;; use lax matching + (reverse-im-char-fold t) (reverse-im-read-char-advice-function #'reverse-im-read-char-include) - (reverse-im-input-methods '("ukrainian-computer")) ; translate these methods + ;; translate these methods + (reverse-im-input-methods '("ukrainian-computer")) :config (reverse-im-mode t)) ; turn the mode on #+END_SRC