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- Respect self: optional start this mode with English
- Respect
evil
: switch to English when leavingevil
insert
mode. - Respect
minibuffer
: switch to English when enterminibuffer
. - Respect prefix key: switch to English for
C-c
/C-x
/C-h
. - 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 enteringevil
insert mode. A functionsmart-input-source-follow-context
is also provided to hook as your wish, regardless the presence ofevil
. It also has a global versionsmart-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 versionsmart-input-source-global-inline-english-mode
to enable the mode for all buffers.
- Use OS native input source, no need to change use experience.
- Support GUI Emacs, as well as Terminal Emacs.
- Easy to install and configure. No further configuration for
GUI Emacs Mac Port
withSogou Input
. - Respect buffer/mode by proper input source:
- Respect self: optional start this mode with English
- Respect
evil
: switch to English when leavingevil
insert
mode. - Respect
minibuffer
: switch to English when enterminibuffer
. - Respect prefix key: switch to English for ~”C-c”~/ ~”C-x”~/ ~”C-h”~.
- Respect buffer: recover buffer input source when it regain focus.
- Support
inline English
region:- Inserting space around none-English characters triggers the region.
- In this mode, English will be sticked.
- Region is closed when:
a. Cursor leaves the region
b.
Return
is pressed. c. Two consecutivespace
are input. - If the region ends with space, none-English input source will be selected, otherwise English input source is retained.
- 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.
- Switch input source according to context: When entering
evil
insert mode, input source will be determined according to the context. - Input source switching rules are designed carefully, for pure English editing, input source will never be switched to other languages.
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 installmacism
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 selectINPUT_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.
- Run
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:
- For
spacemacs
user, if you config it with thehybrid
mode, Some of theevil
related features may not work. Change it tovim
mode instead. - If you want to change the default value of a buffer local variable for all
buffers, you should use
setq-default
rather thansetq
.
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)
Variable | Buffer | Description | Default |
---|---|---|---|
smart-input-source-english | no | Input source for English | com.apple.keylayout.US |
smart-input-source-other | yes | Input source for other language | com.sogou.inputmethod.sogou.pinyin |
smart-input-source-fixed-context | yes | Context is fixed to a specific language | nil |
smart-input-source-english-pattern | no | Pattern to identify a character as English | [a-zA-Z] |
smart-input-source-other-pattern | yes | Pattern to identify a character as other | \cc , see emacs manual |
smart-input-source-blank-pattern | yes | Pattern to identify a character as blank | [:blank:] |
smart-input-source-aggressive-line | yes | Aggressively detect context across blank lines | t |
smart-input-source-external-ism | no | input resource manager | macism |
smart-input-source-do-get | no | Function to get the current input source | determined from the environment |
smart-input-source-do-set | no | Function to set the input source | determined from the environment |
smart-input-source-start-with-english | no | Switch to English for `respect-mode’ | t |
smart-input-source-preserve-triggers | no | Commands trigger the before-save/after-restore | see variable doc |
smart-input-source-restore-triggers | no | Commands trigger the after-restore | see variable doc |
smart-input-source-restore-hooks | no | Hooks trigger the restore | see variable doc |
Face Name | Description |
---|---|
smart-input-source-inline-english-face | Face for the online English region overlay |
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")))))))
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)))))
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.