syllables->jamo on save

This commit is contained in:
2026-02-12 14:37:17 -07:00
parent fd36692419
commit 109fc3547e
2 changed files with 59 additions and 0 deletions

4
.dir-locals.el Normal file
View File

@@ -0,0 +1,4 @@
((nil
. ((eval . (progn (add-to-list 'load-path (project-root (project-current)))
(require 'helpers)
(add-hook 'before-save-hook #'hangul-convert-buffer-to-jamo))))))

55
helpers.el Normal file
View File

@@ -0,0 +1,55 @@
(defun hangul-syllables-to-jamo (str)
"Convert HANGUL SYLLABLES characters in STR to their HANGUL JAMO
equivalents."
(let ((result "")
(i 0))
(while (< i (length str))
(let ((char (aref str i)))
(if (and (>= char #xAC00) (<= char #xD7A3))
;; Hangul syllable
(let* ((code (- char #xAC00))
(lead (/ code (* 21 28)))
(medial (/ (% code (* 21 28)) 28))
(final (% code 28))
(lead-jamo (+ #x1100 lead))
(medial-jamo (+ #x1161 medial))
(final-jamo (if (> final 0) (+ #x11A7 final) nil)))
(setq result
(concat result (char-to-string lead-jamo)
(char-to-string medial-jamo)
(if final-jamo (char-to-string final-jamo) ""))))
;; Not a Hangul syllable
(setq result (concat result (char-to-string char)))))
(setq i (1+ i)))
result))
(defun hangul-convert-region-to-jamo (beg end)
(interactive "r")
(replace-region-contents
beg end (lambda ()
(hangul-syllables-to-jamo (buffer-substring (point-min)
(point-max))))))
(defun hangul-convert-buffer-to-jamo ()
(interactive)
(hangul-convert-region-to-jamo (point-min) (point-max)))
(require 'dash)
(defconst gf-hangul/choseong
(cl-loop for i from #x1100 to #x1112 collect i))
(defconst gf-hangul/jungseong
(cl-loop for i from #x1161 to #x1175 collect i))
(defconst gf-hangul/batchim
(cl-loop for i from #x11a8 to #x11c2 collect i))
(defun gf-hangul/make-pattern (name seq)
(format "'%s' : pattern Str = #(%s) ;"
name (->> seq
(--map (concat "\"" it "\""))
(-interpose " | ")
(apply #'concat))))
(provide 'helpers)