GitHub - laishulu/emacs-smart-input-source: Switch OS native input source smartl...
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
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 leavingevil
insert mode. It can also be used regardless the presence ofevil
, 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 enteringevil
insert mode. A functionsmart-input-source-follow-context
is also provided to hook as your wish, regardless the presence ofevil
. - 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
- Use OS native input source, no need to change use experience.
- Support GUI Emacs, as well as Terminial Emacs.
- Easy to install and config. No further config for
GUI Emacs Mac Port
withSogou Input
. - Automatically switch to English when Emacs start.
- Respect
Evil
(also works without evil):- When leave
insert
mode, switch to English input source automatically. - when enter
insert
model, switch input source automatically according to context.
- When leave
- 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.
- Support
inline English
region:- Inserting space around none-English characters triggers the region.
- In this mode, Engligh will be sticked.
- Region is closed when:
a. Cursor leaves the region
b.
Return
is pressed. c. Two consequtivespace
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.
- 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.
In Chinese
- 使用操作系统的输入法,不改变使用习惯。
- 支持 GUI Emacs, 也支持 Terminal Emacs.
- 安装配置简单, GUI Emacs Mac Port 下使用搜狗拼音免配置。
- Emacs 启动时自动切换到英文输入法。
- 如果在 Evil 下(没有 evil 也可以正常工作):
- 从 insert 模式离开时,自动切换到英文输入法。
- 进入 insert 模式时, 根据上下文自动切换输入法。
- 记得缓冲区自己的输入法。从其他缓冲区切换回来,或者重新获得窗口焦点时, 会恢复缓冲区自己的输入法
- 支持 inline English 区域:
- 在汉字前后插入空格触发 inline English 区域。
- 在 inline English 下,一直使用英文输入法。
- 关闭 inline English 区域 a. 光标离开区域。 b. 按回车键。 c. 输入了连续两个空格。
- 区域如果是空格结尾,输入法切回非其它语言,否则输入法保持英文。
- 区域关闭后,首尾各删除一个空格(如果有的话)。
- 根据上下文切换输入法:当进入
evil
的 insert 模式时,根据上下文确定输入法。 - 可以安全地默认开启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 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-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.
- Run
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:
- 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
.
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
Configure
VariableBufferDescriptionDefaultsmart-input-source-english-input-source
noInput source for Englishcom.apple.keylayout.US
smart-input-source-other-input-source
yesInput source for other languagecom.sogou.inputmethod.sogou.pinyin
smart-input-source-fixed-context
yesContext is fixed to a specific languagenil
smart-input-source-english-pattern
noPattern to identify a character as English[a-zA-Z]
smart-input-source-other-pattern
yesPattern to identify a character as other\cc
, see emacs manualsmart-input-source-blank-pattern
yesPattern to identify a character as blank[:blank:]
smart-input-source-aggressive-line
yesAggressively detect context across blank linest
smart-input-source-external-ism
noinput resource managermacism
smart-input-source-do-get-input-source
noFunction to get the current input sourcedetermined from the environmentsmart-input-source-do-set-input-source
noFunction to set the input sourcedetermined from the environmentsmart-input-source-start-with-english
noSwitch to english for `auto-english-mode’t
smart-input-source-remember-input-source-triggers
noCommands that trigger the save/restore.see variable docsmart-input-source-save-input-source-hook-triggers
noHooks which trigger the savesee variable docsmart-input-source-restore-input-source-hook-triggers
noHooks which trigger the restoresee variable docFace NameDescriptionsmart-input-source-inline-english-face
Face for the online english region overlayConfigurations 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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK