Wednesday, June 3, 2015

Emacs Spell-checker English/Korean

Emacs Spell-checker English/Korean

Emacs Spell-checker English/Korean


This post is written with e[ ⎋Esc ]m[ ⌥Meta ]a[ ⌥Alt ]c[ ^Ctrl ]s[ ⇧Shift ] org-mode.

이 문서는 e[ ⎋Esc ]m[ ⌥Meta ]a[ ⌥Alt ]c[ ^Ctrl ]s[ ⇧Shift ] org-mode로 작성된 글입니다.


System Environments

Operating Systems

  • Main- : OS X Yosemite (10.10.3)
  • Sub- : Debian GNU/Linux Wheezy (7.8)
  • Server: Debian GNU/Linux Wheezy || Squeeze
  • Mobile: iOS 7.1.2

Ispell-word and the secondary click within Flyspell on

Needless to say, the GIF below is self-explanatory.

English/Korean spell-checker in Emacs 24.3, OS X

The work-flow was captured under Hunspell with Emacs Cocoa 24.3 in OSX. Hunspell was activated for English/Korean. Shortcuts I set are ⌘d for ispell-word and ⌘2⌘2 for the spell-checking pop-up window. For the former, you can select a word you want by typing a digit. The latter is technically nothing but the secondary click(or right click) at a mouse point on a word underscored by Flyspell On. I made the key-binding for it by ⌘2⌘2. Though the gif above shows way in Emacs Cocoa under OSX, it is completely possible in GNU Emacs under Linux.

FYI) ⌘⎵ in the gif indicates the shortcut I set for ispell-change-dictionary.

Requirements

  • Hunspell for Emacs cocoa 24.3 in OSX or for GNU Emacs 23.4/24.3 in Debian GNU/Linux

It is not dealt with in this post, because I explained it already. You can see the details by clicking one of the links followed.

There are descriptions for a simple way to change both input methods and hunspell dictionaries as well.

Instruction

Typing ispell-word after ⌥x (M-x in typical Emacs notation) would be enough for some users to check spelling. However, what I want to say is how to set up a key-stroke for the spell-checking pop-up window once the flyspell is on.

  1. Set flyspell-mode on for text-mode by adding the line
    '(text-mode-hook (quote (turn-on-flyspell text-mode-hook-identify)))
    

    into your Emacs preference file, i.e. ~/.emacs. For instance,

    (custom-set-variables
    ;; custom-set-variables was added by Custom.
    ;; If you edit it by hand, you could mess it up, so be careful.
    ;; Your init file should contain only one such instance.
    ;; If there is more than one, they won't work right.
    '(cua-mode t nil (cua-base)) ;;<--- let c-x c-c c-v work
    '(text-mode-hook (quote (turn-on-flyspell text-mode-hook-identify))) ;;<--- flyspell
    )
    

    The basic to read an elisp source is that anything after ;; is a comment, so that doesn't affect Emacs at all.

  2. Activate ⌘2⌘2 for the secondary click(or right click) at an underscored word by adding the following elisp code to ~/.emacs
    ;; \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    ;; \\\\\[Body III-a.] For keyboard strokes for spelling check
    ;; \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    (global-set-key [\s-mouse-2] 'flyspell-correct-word)   ;; <--- step 0/2 for s-2 s-2
    ;; ------------------------------
    ;; ---> function 1
    ;; ------------------------------
    (defun make-mouse-event-at-point (base-event)
    (let ((posn (posn-at-point))
      (prefix "")
      (basic-type (event-basic-type base-event))
      (modifiers (event-modifiers base-event)))
    (cond
    ((and (integerp basic-type) (>= basic-type ?0) (<= basic-type ?9))
    ;; click
    (let* ((mouse-type (intern (format "%smouse-%d" prefix (- basic-type ?0))))
           (click-count 1)
           (type (event-convert-list (append modifiers (list mouse-type)))))
      (list type posn click-count)))
    (t
    (error "Unsupported key for mouse event: %s" (event-basic-type base-event)))
    ))
    )
    ;; ------------------------------
    ;; ---> function 2
    ;; ------------------------------
    (defun simulate-mouse-event-at-point ()
    (interactive)
    (let ((event (make-mouse-event-at-point last-input-event)))
    (setq unread-command-events (cons event unread-command-events))))
    ;; ------------------------------
    ;; ---> function 3
    ;; ------------------------------
    (defvar simulate-mouse-event-map (make-sparse-keymap))
    (global-set-key (kbd "s-2") simulate-mouse-event-map)   ;; <--- step 1/2 for s-2 s-2
    ;; ------------------------------
    ;; #####!!! final step, i.e step 2/2 for keyboard stroke for mouse#-click
    ;; ------------------------------
    (define-key simulate-mouse-event-map [t] 'simulate-mouse-event-at-point)
    

    When you use the exactly same elisp above in GNU Emacs under Linux, the shortcut must be ⊞Win-2 ⊞Win-2 in a non-apple keyboard. If you want to change it to C-2 C-2, then it can be done after replacing s- and \s- with C- and \C- respectively.

FYI) In GNU Emacs under Linux, C, M, S, and s stand for Ctrl, Meta, Shift, and super-key (a.k.a. Windows key) respectively.

Acknowledgement

I acknowledge Gilles from StackExchange's debt to almost every elisp code here. You can find his source code explicitly in HERE.

Epilog, 한글 사용자를 위하여

Emacs에서 맞춤법 검사는, ⌥x (M-x Emacs 기본 표기) 후 ispell-word 로 간단히 실행할 수 있다. Flyspell mode(틀린 맞춤법에 밑줄 그어짐)이 켜있는 경우에는, 밑줄 그어진 단어 위에서 우클릭하면 팝업창이 뜨고 화살표키로 선택하는 방법으로도 사용할 수 있다. 영어의 경우는 특별한 사전을 설치하지 않아도 바로 검사할 수 있음은 물론이다. 한글 맞춤법 검사는 현재로는 hunspell이 거의 유일무이한 대안인데, 영어와 함께 이를 활성화 하는 방법은 지난 Posts에서 설명했다.

OSX Yosemite 전까지는 Aquamacs에서 ispell-word (or ispell-region)으로 영어/한글 맞춤법 모두를 해결해 왔었다. 바람입력기로 OSX 차원이 아닌 Aquamacs 자체에서 입력소스를 변경하고, 영어 맞춤법은 Aquamacs에서 기본으로 제공하는 것으로 한글 맞춤법은 OSX 차원에서 설치한 Korean hunspell dictionary로 약간은 변칙을 써왔었다. 당시에도 Aquamacs에서 Hunspell을 동작하게 하는 방법을 많이 시도했으나 다 무위로 돌아갔었고, 이 변칙 방법으로도 잘 작동하니 문제 없이 사용했었다. 물론, OSX 단에서 설치한 이 한글 사전은 Aquamacs는 물론 다른 프로그램에서도 잘 돌았었다.

OSX Yosemite 후, 모든 것이 달라졌다. OSX 단에서 설치한 한글 사전은 어디에서도 제대로 동작하지 않았다. BACK TO THE MAC에서 항상 추천하는 한글 맞춤법 검사기 "단디" 역시 나에게는 해당 사항이 없었다. 왜냐하면, 단축키를 지정해도 Emacs던 Aquamacs던 동작하지 않고, 설혹 가능해도 단순히 확인할 수 있을 뿐 수정은 직접 해주어야 하는 점 때문이다. 나에게는 Hunspell이 필요했다.

모든 문제의 시작은 Hunspell임을 직감하고, 어떻게 해도 이를 활성화 할 수 없었던 Aquamacs를 포기하고 Original GNU Emacs에 가장 가깝다고 하는 Emacs Cocoa로 전환하기로 결정했다. 다행히, OSX Yosemite Emacs Cocoa에서 Hunspell을 쓰는 방법을 찾았다. (Linux에서 GNU Emacs로 hunspell 이용은 사실 자명하다.) 이는 위에서 언급한 지난 두 글에서 자세히 설명했지만, 핵심은 OS 마다 적절한(?) Emacs version을 써야 하는 점이다. 그러면 문제 해결은 비교적 간단하다.

언제나 그렇듯이, 한글이 항상 문제(?)다. 어지간히 Emacs에서 한글을 써본 사람은 다 알겠지만, Linux를 제외한 OS에서 OS 단에서 제공하는 한/영 변환은 항상 문제를 일으키므로, Emacs 자체에서 제공하는 입력 소스 변환을 쓰는 것이 항상 추천된다. 이는 OSX Yosemite에서도 바람입력기를 통해 간단히 해결할 수 있다. Hunspell을 Emacs에서 쓸 수 있게 된다 하더라도 문제는 크게 두 가지가 남아있다.

첫째, ⌥x (M-x Emacs 기본 표기) ispell-word*Choices* buffer 에서 한글이 잘린다

한글 잘림 in *Choices* buffer

이는 한글과 영어를 다른 크기로 지정함으로써, 어느 정도 해결 가능하다. 아래는 영어는 Anonymous Pro 10으로, 한글은 HCR Dotum LVT 16으로 만든 경우이다.

;; Setup different font family and size for English/Korean
(setq default-frame-alist
      '(
	(top . 0) (left . 182) (width . 96) (height . 36)   ---> window position and size
	))
(when (eq system-type 'darwin)
  (set-face-attribute 'default nil :family "Anonymous Pro")
  default font size (point * 10)
  (set-face-attribute 'default nil :height 195)
  (set-fontset-font t 'hangul (font-spec :name "HCR Dotum LVT-16"))
  )

이렇게 하면 한글 폰트 크기가 전반적으로 바뀌면서, *Choices* buffer 에서도 한글이 잘리지 않는다. 두벌식 사용자라면, ispell-word 단축키만 따로 지정해도 적당히 쓸 만한 환경을 구축할 수 있다.

나는 세벌식390 사용자다. 문제가 심각해졌다. Emacs 자체 입력 소스를 설정하면 한글 세벌식 입력 환경에서 *Choices* buffer 안에서 단어 선택이 안된다. Emacs 자체 입력 소스를 끄고, OSX 입력 소스를 쓰면 *Choices* buffer 안에서 단어 선택은 되지만, ⌥x 같은 Emacs 단축키가 안먹는다!!! 어떤 세벌식을 쓰는 지와는 무관하게 세벌식 모두(보통, 390, 최종)에서 다 안된다. 이는 Yosemite 이전에 Aquamacs (Aquamacs 2.5x 이하 버전) 사용 시는 발생하지 않는 문제였다.이 문제를 해결하기 위해서 Flyspell-mode에서 마우스 우클릭하면 팝업창이 뜨는 것에 착안, 이를 키보드로 쓸 수 있는 방법을 여기서 기술했다. 이 방법은 당연하게도 두벌식이든 세벌식이든 상관없다. 게다가 ispell-word 를 쓸 때에도 불만이었던, "화살표키로 단어선택이 안되던 문제"에서 자유롭다. 가능한 한 키보드로 모든 것을 해결하려는 이들에게 숫자키로 가는 손가락은 화살표키보다 불편하다.

지난 글과 여기에서 설명하려한 방법의 목적을 다시 정리하자면, " 어떤 한글 입력 방법을 쓰던 상관 없이 Emacs에서 맞춤법 검사 과정 전부를 키보드로 해결하자! "이다. Emacs 버전을 타는 경향이 있지만, 이는 OSX 전반은 물론 Debian GNU/Linux에서도 큰 수정 없이 바로 쓸 수 있는, 현재로써는 가장 완벽한 해결책이다.


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Created: 2015-06-04 Thu 11:21

Emacs 24.3.1 (Org mode 8.2.10)

No comments:

Post a Comment