-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkanji-mode.el
155 lines (124 loc) · 5.35 KB
/
kanji-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
;;; kanji-mode.el --- View stroke order for kanji characters at cursor
;; Copyright 2014-2015 Wojciech Gac
;; Author: Wojciech Gac <[email protected]>
;; URL: http://github.com/wsgac/kanji-mode
;; Version: 1.0
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; You can add a text-mode hook to enable kanji-mode automatically
;; every time you enter text-mode. To do so add the following to
;; your .emacs file:
;; (add-hook 'text-mode-hook 'kanji-mode)
;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;
;; Paths and variables ;;
;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *kanji-svg-path* (concat (file-name-directory load-file-name) "kanji")
"Relative path to stroke order files in SVG format.")
(make-variable-buffer-local '*kanji-svg-path*)
(defvar *km:kakasi-executable* (locate-file "kakasi" exec-path)
"Path to Kakasi binary.")
(defvar *km:kakasi-common-options* "-iutf8 -outf8"
"Kakasi command-line options common to all calls.")
;; Conversion options
(defvar *km:kanji->hiragana* "-JH"
"Kakasi command-line options for converting kanji to hiragana.")
(defvar *km:all->romaji* "-Ja -Ha -Ka -ka -Ea"
"Kakasi command-line options for converting Japanese to romaji.")
;;;;;;;;;;;;;;;;;;;;;;;
;; Utility functions ;;
;;;;;;;;;;;;;;;;;;;;;;;
(defun km:char-to-hex (char)
"Return hex code for character, padded with `0`s to conform with KanjiVG naming convention."
(format "%05x" char))
(cl-defun km:command->string (text conversion &optional
(exec *km:kakasi-executable*)
(common *km:kakasi-common-options*))
"Run conversion command on TEXT using options specified in
CONVERSION. Optionally provide your own Kakasi EXECutable and
COMMON CLI options. TEXT is passed to Kakasi via a pipe."
(unless (and exec (file-exists-p exec) (file-executable-p exec))
(error "You don't seem to have Kakasi installed."))
(string-trim (shell-command-to-string
(format "echo '%s' | %s %s %s" text exec common conversion))))
(defun km:kanji->hiragana (text)
(km:command->string text *km:kanji->hiragana*))
(defun km:all->romaji (text)
(km:command->string text *km:all->romaji*))
(defmacro km:interactive-function (fn)
"Abstract common behavior for transcription functions. When
called without a prefix argument, the function's result will
be printed in the minibuffer. When called with prefix argument
-1, it will place the results in the kill-ring instead. Any
other prefix argument will produce a new buffer containing
results of the transcription."
`(let* ((text (if (= start end)
(thing-at-point 'word)
(buffer-substring start end)))
(transcribed (,fn text)))
(cl-case current-prefix-arg
((nil)
(message "%s" transcribed))
((-)
(kill-new transcribed)
(message "Transcription finished; pushed result to kill ring."))
(t
(with-current-buffer
(generate-new-buffer "kanji-mode transcription")
(insert transcribed)
(local-set-key (kbd "q") 'kill-current-buffer)
(switch-to-buffer (current-buffer))
(message "Press 'q' to kill this buffer."))))))
(defun kanji-mode-kanji-to-hiragana (start end)
(interactive "r")
(km:interactive-function km:kanji->hiragana))
(defun kanji-mode-all-to-romaji (start end)
(interactive "r")
(km:interactive-function km:all->romaji))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stroke order functions ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun km:get-svg-for-kanji-code (code)
"Return an image object for the Unicode code provided."
(let ((image-path (format "%s.svg" (expand-file-name code *kanji-svg-path*))))
(create-image image-path)))
(defun km:create-buffer-with-image (name)
"Create new buffer with relevant image and switch to it.
Buffer can be closed by hitting `q`"
(with-current-buffer (generate-new-buffer name)
(let ((image (km:get-svg-for-kanji-code name)))
(iimage-mode)
(iimage-mode-buffer t)
(insert-image image)
(local-set-key (kbd "q") 'kill-current-buffer)
(switch-to-buffer (current-buffer))
(message "Press 'q' to kill this buffer."))))
(defun kanji-mode-stroke-order (point)
"Take character at point and try to display its stroke order."
(interactive "d")
(let ((char (char-after point)))
(km:create-buffer-with-image (km:char-to-hex char))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Minor mode definition ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;###autoload
(define-minor-mode kanji-mode
"Minor mode for displaying Japanese characters' stroke orders."
:lighter " kanji"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "M-s M-o") 'kanji-mode-stroke-order)
(define-key map (kbd "M-s M-h") 'kanji-mode-kanji-to-hiragana)
(define-key map (kbd "M-s M-r") 'kanji-mode-all-to-romaji)
map)
)
(provide 'kanji-mode)
;;; kanji-mode.el ends here