Skip to content

Commit

Permalink
Merge pull request #39 from a13/cache-keymaps
Browse files Browse the repository at this point in the history
Cache keymaps
  • Loading branch information
a13 authored Aug 15, 2024
2 parents bcd70b4 + 481da44 commit dfb169b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 31 additions & 5 deletions reverse-im.el
Original file line number Diff line number Diff line change
Expand Up @@ -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'."

Check warning on line 98 in reverse-im.el

View workflow job for this annotation

GitHub Actions / check (snapshot, false)

Symbols t and nil should not appear in single quotes
:group 'reverse-im
:type 'file)

;;; Storage vars
(defvar reverse-im--default-parent
nil
Expand Down Expand Up @@ -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

Check warning on line 201 in reverse-im.el

View workflow job for this annotation

GitHub Actions / check (snapshot, false)

All variables and subroutines might as well have a documentation string
(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))))
Expand Down Expand Up @@ -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))))
Expand Down

0 comments on commit dfb169b

Please sign in to comment.