25

GitHub - laishulu/emacs-smart-input-source: Switch OS native input source smartl...

 3 years ago
source link: https://github.com/laishulu/emacs-smart-input-source
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.org

README

https://melpa.org/packages/smart-input-source-badge.svg

About

This package provide several modes to switch OS native input source smartly:

  • A global mode: smart-input-source-global-auto-english-mode to automatically switch to English when leaving evil insert mode. It can also be used regardless the presence of evil, to optionally switched to English immediately when the mode is turned on.
  • A buffer local mode: smart-input-source-remember-input-source-mode to remember the input source of a buffer even after buffer switch and losing focus.
  • A buffer local mode: smart-input-source-follow-context-mode to switch input source smarted according to context when entering evil insert mode. A function smart-input-source-follow-context is also provided to hook as your wish, regardless the presence of evil.
  • A buffer local mode: smart-input-source-inline-english-mode to enable the automatic triggerring of a temporary overlay to input English and then back to none-English without switching input sources manually.

Features

In English

  1. Use OS native input source, no need to change use experience.
  2. Support GUI Emacs, as well as Terminial Emacs.
  3. Easy to install and config. No further config for GUI Emacs Mac Port with Sogou Input.
  4. Automatically switch to English when Emacs start.
  5. Respect Evil (also works without evil):
    1. When leave insert mode, switch to English input source automatically.
    2. when enter insert model, switch input source automatically according to context.
  6. Remember input source for individual buffer, so when you switch back from other buffer or fucos again from other window, the input source will be restored.
  7. Support inline English region:
    1. Inserting space around none-English characters triggers the region.
    2. In this mode, Engligh will be sticked.
    3. Region is closed when: a. Cursor leaves the region b. Return is pressed. c. Two consequtive space are input.
    4. If the region ends with space, none-English input source will be selected, otherwise English input source is retained.
    5. After the close of the region, one space in the head and one space in the tail (if exists) of the region will be deleted.
  8. Switch input source according to context: When entering evil insert mode, input source will be determined according to the context.
  9. Input source switching rules are designed carefully, for pure English editing, input source will never be switched to other languages.

In Chinese

  1. 使用操作系统的输入法,不改变使用习惯。
  2. 支持 GUI Emacs, 也支持 Terminal Emacs.
  3. 安装配置简单, GUI Emacs Mac Port 下使用搜狗拼音免配置。
  4. Emacs 启动时自动切换到英文输入法。
  5. 如果在 Evil 下(没有 evil 也可以正常工作):
    1. 从 insert 模式离开时,自动切换到英文输入法。
    2. 进入 insert 模式时, 根据上下文自动切换输入法。
  6. 记得缓冲区自己的输入法。从其他缓冲区切换回来,或者重新获得窗口焦点时, 会恢复缓冲区自己的输入法
  7. 支持 inline English 区域:
    1. 在汉字前后插入空格触发 inline English 区域。
    2. 在 inline English 下,一直使用英文输入法。
    3. 关闭 inline English 区域 a. 光标离开区域。 b. 按回车键。 c. 输入了连续两个空格。
    4. 区域如果是空格结尾,输入法切回非其它语言,否则输入法保持英文。
    5. 区域关闭后,首尾各删除一个空格(如果有的话)。
  8. 根据上下文切换输入法:当进入 evil 的 insert 模式时,根据上下文确定输入法。
  9. 可以安全地默认开启mode:编辑英文时,输入法永远不会自动切换到其它语言。

Install

Just install this package from melpa, and then install input source manager by any of the following ways:

  • On MacOS, use GUI Emacs Mac Port (EMP), no need to configure input source manager. You can install EMP by the following commands:
    brew tap railwaycat/emacsmacport
    brew install emacs-mac --with-modules --with-rsvg --with-imagemagick --with-natural-title-bar
        
  • On MacOS, use Emacs other than GUI EMP, with macism, no need to configure input source manager. You can install macism by the following commands:
    brew tap laishulu/macism
    brew install macism
        

    Note: The first time when you use macism SOME_INPUT_SOURCE_ID in your app, MacOS will popup a window asking you to grant permission of Accessibility, you can also grant the permission manually following the instructions in macism.

    If you have problems on granting permission of Accessibility for Emacs, see below:

    Some Emacs “distributions” package together a number of Emacs binaries for various macOS versions in a single folder, and dynamically at runtime chooses the right one for your system. This means that the icon you click to start the program is really a “placeholder” that is not Emacs itself, but rather just starts up Emacs. This “placeholder” often takes the form of a Ruby script. If this is the case, you’ll need to drag the Ruby program on to the list. Ruby comes with macOS by default. You can find the program by opening Finder, and then from the “Go” menu choose “Go to Folder”. Enter “/usr/bin” and you’ll get that folder opened in Finder. In the folder, you’ll find the ruby program that you can drag on to the Accessibility list.

  • Any other case (eg. on Linux or Windows), input source manager should be configured:
    (setq smart-input-source-external-ism "YOUR_ISM")
        

    If YOUR_ISM CAN’T be used in the following way:

    • Run YOUR_ISM will output the current input source
    • Run YOUR_ISM INPUT_SOURCE_ID will select INPUT_SOURCE_ID.

    you should also configure the following:

    (setq smart-input-source-do-get-input-source
          #'YOUR_DO_GET_INPUT_SOURCE_FUNCTION)
    (setq smart-input-source-do-set-input-source
          #'YOUR_DO_SET_INPUT_SOURCE_FUNCTION)
        

    Refer to the section of Configurations for other input source managers at the end of this documentation.

Usage

The mode is designed carefully, so it’s safe to enable for all buffers even its all in English.

(use-package smart-input-source
  :config
  (setq smart-input-source-english-input-source
        "com.apple.keylayout.US")
  (setq-default smart-input-source-other-input-source
        "com.sogou.inputmethod.sogou.pinyin")
  (smart-input-source-global-auto-english-mode t)
  (add-hook 'text-mode-hook #'smart-input-source-remember-input-source-mode)
  (add-hook 'prog-mode-hook #'smart-input-source-remember-input-source-mode)
  (add-hook 'text-mode-hook #'smart-input-source-follow-context-mode)
  (add-hook 'prog-mode-hook #'smart-input-source-follow-context-mode)
  (add-hook 'text-mode-hook #'smart-input-source-inline-english-mode)
  (add-hook 'prog-mode-hook #'smart-input-source-inline-english-mode))

Tips:

  1. For spacemacs user, if you config it with the hybrid mode, Some of the evil related features may not work. Change it to vim mode instead.
  2. If you want to change the default value of a buffer local variable for all buffers, you should use setq-default rather than setq.

How to get the input source id

After input source manager is configured, you can get your current input source id by runing the following command in Emacs:

(smart-input-source--get-input-source)

Input examples

./screenshots/smart-input-source.png

Configure

VariableBufferDescriptionDefaultsmart-input-source-english-input-sourcenoInput source for Englishcom.apple.keylayout.USsmart-input-source-other-input-sourceyesInput source for other languagecom.sogou.inputmethod.sogou.pinyinsmart-input-source-fixed-contextyesContext is fixed to a specific languagenilsmart-input-source-english-patternnoPattern to identify a character as English[a-zA-Z]smart-input-source-other-patternyesPattern to identify a character as other\cc , see emacs manualsmart-input-source-blank-patternyesPattern to identify a character as blank[:blank:]smart-input-source-aggressive-lineyesAggressively detect context across blank linestsmart-input-source-external-ismnoinput resource managermacismsmart-input-source-do-get-input-sourcenoFunction to get the current input sourcedetermined from the environmentsmart-input-source-do-set-input-sourcenoFunction to set the input sourcedetermined from the environmentsmart-input-source-start-with-englishnoSwitch to english for `auto-english-mode’tsmart-input-source-remember-input-source-triggersnoCommands that trigger the save/restore.see variable docsmart-input-source-save-input-source-hook-triggersnoHooks which trigger the savesee variable docsmart-input-source-restore-input-source-hook-triggersnoHooks which trigger the restoresee variable docFace NameDescriptionsmart-input-source-inline-english-faceFace for the online english region overlay

Configurations for other input source managers

Example: fcitx

fcitx is a input method framework popular among Chinese Linux users. fcitx-remote can serve as an input source manager for `fcitx`, and you can configure as following:

(require 'subr-x)
(setq smart-input-source-external-ism "fcitx-remote")
(setq smart-input-source-english-input-source "1")
(setq-default smart-input-source-other-input-source "2")
(setq smart-input-source-do-get-input-source
      (lambda()
        (string-trim
         (shell-command-to-string
          smart-input-source-external-ism))))
(setq smart-input-source-do-set-input-source
      (lambda(source)
        (pcase source
          ("1" (string-trim (shell-command-to-string
                             (concat smart-input-source-external-ism " -c"))))
          ("2" (string-trim (shell-command-to-string
                             (concat smart-input-source-external-ism " -o")))))))

Example: ibus

ibus is another popular input method framework in the Linux world. You can configure as following:

(require 'subr-x)
(setq smart-input-source-external-ism "ibus")
(setq smart-input-source-english-input-source "xkb:us::eng")
(setq-default smart-input-source-other-input-source "OTHER_INPUT_SOURCE")
(setq smart-input-source-do-get-input-source
      (lambda()
        (string-trim (shell-command-to-string
          (concat smart-input-source-external-ism " engine")))))
(setq smart-input-source-do-set-input-source
      (lambda(source)
        (string-trim (shell-command-to-string
          (concat smart-input-source-external-ism " engine " source)))))

Example: im-select

im-select can be used as input source manager in Microsoft Windows. It fulfills the requirements as a drop-in replacement of macism, thus its configuration is simpler than other input source managers.

(setq smart-input-source-external-ism "im-select.exe")
(setq smart-input-source-english-input-source "ENGLISH_INPUT_SOURCE")
(setq-default smart-input-source-other-input-source "OTHER_INPUT_SOURCE")

However, because even though im-select supports switching different input languages, it does not support multiple input methods in the same lanuage, thus you should ensure that in each input language there is only one input method, just like the following screenshot.

./screenshots/windows-im-select.jpg


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK