laishulu / Emacs Smart Input Source
Projects that are alternatives of or similar to Emacs Smart Input Source
#+TITLE: README [[https://melpa.org/#/sis][file:https://melpa.org/packages/sis-badge.svg]]
- About
sis(smart input source) enables less manual switch for native or OS input source (input method):
- A global mode:
sis-global-respect-modeto respect buffer/mode by proper input source- Respect start: start Emacs with specified language.
- Respect
evil: switch to English when leavingevilinsertmode. - Respect
minibuffer: switch to English when enterminibuffer. - Respect prefix key: switch to English for
C-c/C-x/C-hand more. - Respect buffer: recover buffer input source when it regain focus.
- A buffer local mode:
sis-context-modeto switch input source smarted according to context. It also has a global versionsis-global-context-modeto enable the mode for all buffers. When to switch input source by following context can be configured easily by a variable, whose default value indicates when entering evil insert mode. - A buffer local mode:
sis-inline-modeto enable the automatically triggering of a temporary overlay to input English/other language and then back to original input source without switching input sources manually. It also has a global versionsis-global-inline-modeto enable the mode for all buffers. - A global mode:
sis-global-cursor-color-modeto automatically change cursor color according to current input source.
- Features
- Ease the use of OS-native input source, no need to change use experience.
- Ease the use of Emacs-native input source, for further compatibility.
- Support GUI Emacs, as well as Terminal Emacs.
- Easy to install and configure. No further configuration for
GUI Emacs Mac PortwithSogou Input. - Automatically change cursor color according to current input source.
- Respect buffer/mode by proper input source:
- Respect start: start Emacs with specified language.
- Respect
evil: switch to English when leavingevilinsertmode. - Respect
minibuffer: switch to English when enterminibuffer. - Respect prefix key: switch to English for
C-c/C-x/C-hand more. - Respect buffer: recover buffer input source when it regain focus.
- Support
inline Englishandinline other languageregion. Features of the inline region (inline englishas an example):- Inserting space around none-English characters triggers the region (two
consecutive spaces for
inline other languageregion). - In this mode, English will be sticked.
- Region is closed when:
a. Cursor leaves the region
b.
Returnis pressed. c. Two consecutivespaceare input (can be configured to onespace). - 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 (can be configured) in the head and tail respectively (if exists) of the region will be deleted. But if the whole region is blank, the no character will be deleted.
- Inserting space around none-English characters triggers the region (two
consecutive spaces for
- Switch input source according to context:
- Pre-configured for entering evil insert mode.
- For native emacs user, you can configure by easily set a variable.
- Input source switching rules are designed carefully, for pure English editing, input source will never be switched to other languages.
-
Install Just install
sisfrommelpa. -
Prepare /input source manager (ISM)/ ** Emacs-native input method Here uses
rimeas an example: #+BEGIN_SRC lisp (sis-ism-lazyman-config nil "rime" 'native) #+END_SRC
** MacOS The default MacOS input sources are shown in the following. #+BEGIN_SRC lisp ;; Not needed if your input sources are the same with the default values (sis-ism-lazyman-config "com.apple.keylayout.US" "com.sogou.inputmethod.sogou.pinyin") #+END_SRC
Note:
- Your English input source MAY NOT be the default one. Use command
sis-getin Emacs to get the correct one. - According to your use case,
EMPormacismshould be installed.
*** GUI Emacs Mac Port (EMP) EMP is a Emacs distribution enhanced for MacOS. It has builtin native API to interact with MacOS input sources efficiently. EMP can be installed by the following commands: #+BEGIN_SRC bash brew tap railwaycat/emacsmacport brew install emacs-mac --with-modules --with-rsvg --with-imagemagick --with-natural-title-bar #+END_SRC
*** macism
If your Emacs is not GUI EMP, the pre-configured macism should be installed.
#+BEGIN_SRC bash
brew tap laishulu/macism
brew install macism
#+END_SRC
Note:
- The first time when you use
macism SOME_INPUT_SOURCE_IDin 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 [[https://github.com/laishulu/macism/][macism]]. - On a slow computer,
macismneeds sleep time (in micro seconds) longer than default to work with Accessibility. The default can be overwritten by the following codes. #+BEGIN_SRC lisp (setq sis-do-set (lambda(source) (start-process "set-input-source" nil "macism" source "50000"))) #+END_SRC - Don't use
Alacrittyfor TUI Emacs, because it can't handle deleting as well asOptionandCommandkey properly when input method is on. I just recommendkittyuntilAlacirttyfixed those long existing bugs. - If you have problems on granting permission of Accessibility for Emacs, see below: #+BEGIN_QUOTE 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. #+END_QUOTE
** Microsoft Windows
*** im-select
[[https://github.com/daipeihust/im-select][im-select]] can be used as a drop-in replacement of macism in Microsoft Windows.
#+BEGIN_SRC lisp
(sis-ism-lazyman-config "1033" "2052" 'im-select)
#+END_SRC
- Even though
im-selectsupports 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. #+CAPTION: Smart input source [[./screenshots/windows-im-select.jpg]] - If you are using
winkey assuperkey, you may also need [[https://github.com/laishulu/winsuper][winsuper]].
** Linux
*** fcitx
#+BEGIN_SRC lisp
(sis-ism-lazyman-config "1" "2" 'fcitx)
#+END_SRC
*** fcitx5
#+BEGIN_SRC lisp
(sis-ism-lazyman-config "1" "2" 'fcitx5)
#+END_SRC
*** ibus
#+BEGIN_SRC lisp
(sis-ism-lazyman-config "xkb🇺🇸:eng" "OTHER_INPUT_SOURCE" 'ibus)
#+END_SRC
** Internals for configuring ISM The core of the configuring of ISM is the two variables : #+BEGIN_SRC lisp (setq sis-do-get #'YOUR_DO_GET_INPUT_SOURCE_FUNCTION) (setq sis-do-set #'YOUR_DO_SET_INPUT_SOURCE_FUNCTION) #+END_SRC
They are provided for EMP and macism by default.
If you have a ISM YOUR_ISM:
- Run
YOUR_ISMwill output the current input source - Run
YOUR_ISM INPUT_SOURCE_IDwill selectINPUT_SOURCE_ID.
Then you can simply use YOUR_ISM as a drop in replacement for macism:
#+BEGIN_SRC lisp
(setq sis-external-ism "YOUR_ISM")
#+END_SRC
You can configure ISM by yourself, but a convenient command
sis-ism-lazyman-config is also provided for common ISMs.
- Configure The mode is designed carefully, so it's safe to enable for all buffers even its all in English.
#+BEGIN_SRC lisp (use-package sis ;; :hook ;; enable the /follow context/ and /inline region/ mode for specific buffers ;; (((text-mode prog-mode) . sis-context-mode) ;; ((text-mode prog-mode) . sis-inline-mode))
:config ;; For MacOS (sis-ism-lazyman-config
;; English input source may be: "ABC", "US" or another one. ;; "com.apple.keylayout.ABC" "com.apple.keylayout.US"
;; Other language input source: "rime", "sogou" or another one. ;; "im.rime.inputmethod.Squirrel.Rime" "com.sogou.inputmethod.sogou.pinyin")
;; enable the /cursor color/ mode (sis-global-cursor-color-mode t) ;; enable the /respect/ mode (sis-global-respect-mode t) ;; enable the /context/ mode for all buffers (sis-global-context-mode t) ;; enable the /inline english/ mode for all buffers (sis-global-inline-mode t) ) #+END_SRC
Tips:
- For
spacemacs, if it works in thehybridmode, some of theevilrelated features may not work. Change tovimmode instead.
** Default behavior for /inline english mode/ and /follow context mode/
You can change some of the behaviors by configuring related variables, note:
inline other language feature is turned off by default.
#+CAPTION: Smart Input Source
[[./screenshots/sis.png]]
- Variables and commands
** About input source
| Variable | Description | Default |
|----------------------------+----------------------------------------------------+--------------------------------------|
|
sis-english-source| Input source for English |com.apple.keylayout.US| |sis-other-source| Input source for other language |com.sogou.inputmethod.sogou.pinyin| |sis-external-ism| External input resource manager |macism| |sis-do-get| Function to get the current input source | determined from the environment | |sis-do-set| Function to set the input source | determined from the environment | |sis-change-hook| Hook to run after input source changes |nil| |sis-auto-refresh-seconds| Idle interval to auto refresh input source from OS |0.2,nilto disable | |----------------------------+----------------------------------------------------+--------------------------------------|
Note:
- To save energy, actual interval to refresh input source from OS in a long idle period is increased automatically.
| Command Name | Description |
|--------------------------+------------------------------------------------------------|
| sis-ism-lazyman-config | Configure input source manager |
| sis-get | Get the input source |
| sis-set-english | Set the input source to English |
| sis-set-other | Set the input source to other language |
| sis-switch | Switch the input source between English and other language |
|--------------------------+------------------------------------------------------------|
** About /cursor color mode/
| Variable | Description | Default |
|----------------------------+---------------------------------------------+--------------------------|
| sis-default-cursor-color | Default cursor color, also used for English | nil (from envrionment) |
| sis-other-cursor-color | Cursor color for other input source | green |
|----------------------------+---------------------------------------------+--------------------------|
** About /respect mode/
| Variable | Description | Default |
|-------------------------------------------------+--------------------------------------------------------+------------------------|
| sis-respect-start | Switch to specific input source when mode enabled | 'english |
| sis-respect-evil-normal-escape | esc to English even in evil normal state | t |
| sis-respect-prefix-and-buffer | Handle prefix key and buffer | t |
| sis-respect-go-english-triggers | Additional trigger to save input source and go English | t |
| sis-respect-restore-triggers | Additional trigger to restore input source | nil |
| sis-respect-minibuffer-triggers | Commands trigger to set input source in minibuffer | see variable doc |
| sis-prefix-override-keys | Prefix keys to be respected | '("C-c" "C-x" "C-h") |
| sis-prefix-override-recap-triggers | Functions trigger the recap of the prefix override | see variable doc |
| sis-prefix-override-buffer-disable-predicates | Predicates on buffers to disable prefix overriding | see variable doc |
|-------------------------------------------------+--------------------------------------------------------+------------------------|
** About language pattern
| Variable | Description | Default |
|-----------------------+--------------------------------------------+--------------------------|
| sis-english-pattern | Pattern to identify a character as English | [a-zA-Z] |
| sis-other-pattern | Pattern to identify a character as other | \cc , see [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Backslash.html][emacs manual]] |
| sis-blank-pattern | Pattern to identify a character as blank | [:blank:] |
|-----------------------+--------------------------------------------+--------------------------|
** About /follow context mode/
| Variable | Description | Default |
|-------------------------------+------------------------------------------------+------------------|
| sis-context-detectors | Detectors to detect the context | see variable doc |
| sis-context-fixed | Context is fixed to a specific language | nil |
| sis-context-aggressive-line | Aggressively detect context across blank lines | t |
| sis-context-hooks | Hooks trigger the context following | see variable doc |
| sis-context-triggers | Commands trigger the context following | see variable doc |
|-------------------------------+------------------------------------------------+------------------|
** About /inline mode/
| Face Name | Description | Default |
|---------------------------------------+------------------------------------------------------------+---------|
| sis-inline-face | Face for the inline region overlay | |
| sis-inline-not-max-point | Insert new line when the whole buffer ends with the region | t |
| sis-inline-tighten-head-rule | Rule to delete the head spaces | 1 |
| sis-inline-tighten-tail-rule | Rule to delete the tail spaces | 1 |
| sis-inline-single-space-close | 1 space to close the region, default is 2 spaces/return | nil |
| sis-inline-with-english | enable the inline english region feature | t |
| sis-inline-with-other | enable the inline other language region feature | nil |
| sis-inline-english-activated-hook | Hook to run after inline english region activated | nil |
| sis-inline-english-deactivated-hook | Hook to run after inline english region deactivated | nil |
| sis-inline-other-activated-hook | Hook to run after inline other language region activated | nil |
| sis-inline-other-deactivated-hook | Hook to run after inline other language region deactivated | nil |
|---------------------------------------+------------------------------------------------------------+---------|
- How to
** How to get the input source id
After /input source manager/ is configured, you can get your /current
input source id/ by the command
sis-get.
** How to inform the package of the input source change
- If your input source is switched from
sis, then everything should be OK naturally. And yoursis-auto-refresh-secondscan even be set tonil. - If your input source is switched from OS, to detect the switch timely,
sis-auto-refresh-secondsshould not be too large. - To save energy, if the input source is switched from OS during a long idle
period of Emacs, the package won't be aware in time. Then you can manually
inform the package by doing anything in Emacs to exit the long idle period,
or invoke the command
sis-getdirectly.
** How to auto set /other/ input source for /org capture/ buffer #+begin_src elisp (add-hook 'org-capture-mode-hook #'sis-set-other) #+end_src
** How to customize my own context detector
Customize sis-context-detectors like the following codes:
#+begin_src elisp
(add-to-list 'sis-context-detectors
(lambda (&rest _)
'other))
#+end_src