일단 해머스푼을 받아서 설치해준다. https://www.hammerspoon.org/

 

Hammerspoon

Hammerspoon What is Hammerspoon? This is a tool for powerful automation of macOS. At its core, Hammerspoon is just a bridge between the operating system and a Lua scripting engine. What gives Hammerspoon its power is a set of extensions that expose specifi

www.hammerspoon.org

 

태스크바에 있는 해머마크 오른쪽 클릭해서 'Open Config' 클릭해서 다음과 같은 내용을 써서 저장한다. 

 

-- Emacs를 사용하기 전의 입력 소스를 저장하기 위한 변수
local lastInputSourceBeforeEmacs = nil

-- Emacs 애플리케이션 이름 (필요시 "Emacs-pretest" 등으로 변경 가능)
local emacsAppName = "Emacs"

-- 강제로 설정할 영어 입력 소스 ID
local englishInputSource = "com.apple.keylayout.ABC"

-- 애플리케이션 전환 이벤트를 감시하는 Watcher 생성
appWatcher = hs.application.watcher.new(function(appName, eventType, appObject)
  if eventType == hs.application.watcher.activated then
    -- 특정 앱이 활성화되었을 때
    if appName == emacsAppName then
      -- 활성화된 앱이 Emacs라면
      -- 현재 입력 소스를 변수에 저장
      lastInputSourceBeforeEmacs = hs.keycodes.currentSourceID()
      -- 입력 소스를 영어로 변경
      hs.keycodes.currentSourceID(englishInputSource)
    end
  elseif eventType == hs.application.watcher.deactivated then
    -- 특정 앱이 비활성화되었을 때
    if appName == emacsAppName then
      -- 비활성화된 앱이 Emacs라면
      -- 저장해둔 이전 입력 소스가 있을 경우, 그 값으로 복원
      if lastInputSourceBeforeEmacs ~= nil then
        hs.keycodes.currentSourceID(lastInputSourceBeforeEmacs)
        -- 복원 후에는 변수를 초기화
        lastInputSourceBeforeEmacs = nil
      end
    end
  end
end)

-- Watcher 시작
appWatcher:start()

 

안타깝지만 lua 코드블록을 지원하지 않으니 bash 로 저장함. 

주석이 달려있지만 기존에 쓰던 한-영 상태를 지정하고 있다가 Emacs 앱으로 포커스가 가면 영어로 세팅을 하고 Emacs 에서 포커스가 사라지면 원래 쓰고 있던 한-영 상태를 복원하는 코드이다. 

+ Recent posts