BlindingDark / emacs-smart-input-source

Switch OS native input source smartly.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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-respect-mode to respect buffer/mode by proper input source
    1. Respect self: optional start this mode with English
    2. Respect evil: switch to English when leaving evil insert mode.
    3. Respect minibuffer: switch to English when enter minibuffer.
    4. Respect prefix key: switch to English for C-c / C-x / C-h.
    5. Respect buffer: recover buffer input source when it regain 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. It also has a global version smart-input-source-global-follow-context-mode to enable the mode for all buffers.
  • A buffer local mode: smart-input-source-inline-english-mode to enable the automatic triggering of a temporary overlay to input English and then back to none-English without switching input sources manually. It also has a global version smart-input-source-global-inline-english-mode to enable the mode for all buffers.

Features

In English

  1. Use OS native input source, no need to change use experience.
  2. Support GUI Emacs, as well as Terminal Emacs.
  3. Easy to install and configure. No further configuration for GUI Emacs Mac Port with Sogou Input.
  4. Respect buffer/mode by proper input source:
    1. Respect self: optional start this mode with English
    2. Respect evil: switch to English when leaving evil insert mode.
    3. Respect minibuffer: switch to English when enter minibuffer.
    4. Respect prefix key: switch to English for ~”C-c”~/ ~”C-x”~/ ~”C-h”~.
    5. Respect buffer: recover buffer input source when it regain focus.
  5. Support inline English region:
    1. Inserting space around none-English characters triggers the region.
    2. In this mode, English will be sticked.
    3. Region is closed when: a. Cursor leaves the region b. Return is pressed. c. Two consecutive 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. But if the whole region is blank, the no character will be deleted.
  6. Switch input source according to context: When entering evil insert mode, input source will be determined according to the context.
  7. Input source switching rules are designed carefully, for pure English editing, input source will never be switched to other languages.

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
          #'YOUR_DO_GET_INPUT_SOURCE_FUNCTION)
    (setq smart-input-source-do-set
          #'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
  :init
  ;; set the english input source
  (setq smart-input-source-english
        "com.apple.keylayout.US")

  ;; set the default other language input source for all buffer
  (setq-default smart-input-source-other
        "com.sogou.inputmethod.sogou.pinyin")

  :config
  ;; enable the /respect/ mode
  (smart-input-source-global-respect-mode t)

  ;; enable the /follow context/ and /inline english/ mode for all buffers
  (smart-input-source-global-follow-context-mode t)
  (smart-input-source-global-inline-english-mode t)

  ;; enable the /follow context/ and /inline english/ mode for specific buffers
  ;; :hook
  ;; (((text-mode prog-mode) . smart-input-source-follow-context-mode)
  ;;  ((text-mode prog-mode) . 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 examples

./screenshots/smart-input-source.png

Configure

VariableBufferDescriptionDefault
smart-input-source-englishnoInput source for Englishcom.apple.keylayout.US
smart-input-source-otheryesInput source for other languagecom.sogou.inputmethod.sogou.pinyin
smart-input-source-fixed-contextyesContext is fixed to a specific languagenil
smart-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 manual
smart-input-source-blank-patternyesPattern to identify a character as blank[:blank:]
smart-input-source-aggressive-lineyesAggressively detect context across blank linest
smart-input-source-external-ismnoinput resource managermacism
smart-input-source-do-getnoFunction to get the current input sourcedetermined from the environment
smart-input-source-do-setnoFunction to set the input sourcedetermined from the environment
smart-input-source-start-with-englishnoSwitch to English for `respect-mode’t
smart-input-source-preserve-triggersnoCommands trigger the before-save/after-restoresee variable doc
smart-input-source-restore-triggersnoCommands trigger the after-restoresee variable doc
smart-input-source-restore-hooksnoHooks trigger the restoresee variable doc
Face NameDescription
smart-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 "1")
(setq-default smart-input-source-other "2")
(setq smart-input-source-do-get
      (lambda()
        (string-trim
         (shell-command-to-string
          smart-input-source-external-ism))))
(setq smart-input-source-do-set
      (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 "xkb:us::eng")
(setq-default smart-input-source-other "OTHER_INPUT_SOURCE")
(setq smart-input-source-do-get
      (lambda()
        (string-trim (shell-command-to-string
          (concat smart-input-source-external-ism " engine")))))
(setq smart-input-source-do-set
      (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 "ENGLISH_INPUT_SOURCE")
(setq-default smart-input-source-other "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

Switch OS native input source smartly.

License:GNU General Public License v3.0


Languages

Language:Emacs Lisp 100.0%