GithubHelp home page GithubHelp logo

emacle / emacs-smart-input-source Goto Github PK

View Code? Open in Web Editor NEW

This project forked from laishulu/emacs-smart-input-source

0.0 1.0 0.0 6.37 MB

Switch OS-native or Emacs-native input source smartly.

License: GNU General Public License v3.0

Emacs Lisp 100.00%

emacs-smart-input-source's Introduction

README

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

About

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

  • A global mode: smart-input-source-global-respect-mode to respect buffer/mode by proper input source
    1. Respect start: start Emacs with specified language.
    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 and more.
    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. It also has a global version smart-input-source-global-follow-context-mode to 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: smart-input-source-inline-mode to 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 version smart-input-source-global-inline-mode to enable the mode for all buffers.
  • A global mode: smart-input-source-global-cursor-color-mode to automatically change cursor color according to current input source.

Features

  1. Ease the use of OS-native input source, no need to change use experience.
  2. Ease the use of Emacs-native input source, for further compatibility.
  3. Support GUI Emacs, as well as Terminal Emacs.
  4. Easy to install and configure. No further configuration for GUI Emacs Mac Port with Sogou Input.
  5. Automatically change cursor color according to current input source.
  6. Respect buffer/mode by proper input source:
    1. Respect start: start Emacs with specified language.
    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 and more.
    5. Respect buffer: recover buffer input source when it regain focus.
  7. Support inline English and inline other language region. Features of the inline region (inline english as an example):
    1. Inserting space around none-English characters triggers the region (two consecutive spaces for inline other language 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 (can be configured to one space).
    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 (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.
  8. Switch input source according to context:
    1. Pre-configured for entering evil insert mode.
    2. For native emacs user, you can configure by easily set a variable.
  9. Input source switching rules are designed carefully, for pure English editing, input source will never be switched to other languages.

Install

Just install smart-input-source from melpa.

Prepare input source manager (ISM)

Emacs-native input method

Here uses rime as an example:

(smart-input-source-ism-lazyman-config nil "rime" 'emacs)

MacOS

The default MacOS input sources are shown in the following.

;; Not needed if your input sources are the same with the default values
(smart-input-source-ism-lazyman-config
 "com.apple.keylayout.US"
 "com.sogou.inputmethod.sogou.pinyin")

Note:

  1. Your English input source MAY NOT be the default one. Use command smart-input-source-get in Emacs to get the correct one.
  2. According to your use case, EMP or macism should 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:

brew tap railwaycat/emacsmacport
brew install emacs-mac --with-modules --with-rsvg --with-imagemagick --with-natural-title-bar

macism

If your Emacs is not GUI EMP, the pre-configured macism should be installed.

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.
  • On a slow computer, macism needs sleep time (in micro seconds) longer than default to work with Accessibility. The default can be overwritten by the following codes.
    (setq smart-input-source-do-set
          (lambda(source) (start-process "set-input-source" nil "macism" source "50000")))
        
  • Don’t use Alacritty for TUI Emacs, because it can’t handle deleting as well as Option and Command key properly when input method is on. I just recommend kitty until Alacirtty fixed those long existing bugs.
  • 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.

Microsoft Windows

im-select

im-select can be used as a drop-in replacement of macism in Microsoft Windows.

(smart-input-source-ism-lazyman-config "1033" "2052" 'im-select)

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

Linux

fcitx

(smart-input-source-ism-lazyman-config nil nil 'fcitx)

fcitx5

(smart-input-source-ism-lazyman-config nil nil 'fcitx5)

ibus

(smart-input-source-ism-lazyman-config "xkb:us::eng" "OTHER_INPUT_SOURCE" 'ibus)

Internals for configuring ISM

The core of the configuring of ISM is the two variables :

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

They are provided for EMP and macism by default.

If you have a ISM YOUR_ISM:

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

Then you can simply use YOUR_ISM as a drop in replacement for macism:

(setq smart-input-source-external-ism "YOUR_ISM")

You can configure ISM by yourself, but a convenient command smart-input-source-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.

(use-package smart-input-source
  ;; :hook
  ;; enable the /follow context/ and /inline region/ mode for specific buffers
  ;; (((text-mode prog-mode) . smart-input-source-follow-context-mode)
  ;;  ((text-mode prog-mode) . smart-input-source-inline-mode))

  :config
  (smart-input-source-ism-lazyman-config
   ;; "com.apple.keylayout.ABC"
   "com.apple.keylayout.US"
   ;; "im.rime.inputmethod.Squirrel.Rime"
   "com.sogou.inputmethod.sogou.pinyin")

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

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.

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.

./screenshots/smart-input-source.png

Variables and commands

About input source

VariableDescriptionDefault
smart-input-source-englishInput source for Englishcom.apple.keylayout.US
smart-input-source-otherInput source for other languagecom.sogou.inputmethod.sogou.pinyin
smart-input-source-external-ismExternal input resource managermacism
smart-input-source-do-getFunction to get the current input sourcedetermined from the environment
smart-input-source-do-setFunction to set the input sourcedetermined from the environment
smart-inpu-source-change-hookHook to run after input source changesnil
smart-input-source-auto-refresh-secondsIdle interval to auto refresh input source from OS0.2, nil to disable

Note:

  • To save energy, actual interval to refresh input source from OS in a long idle period is increased automatically.
Command NameDescription
smart-input-source-ism-lazyman-configConfigure input source manager
smart-input-source-getGet the input source
smart-input-source-set-englishSet the input source to English
smart-input-source-set-otherSet the input source to other language
smart-input-source-switchSwitch the input source between English and other language

About cursor color mode

VariableDescriptionDefault
smart-input-source-default-cursor-colorDefault cursor color, also used for Englishnil (from envrionment)
smart-input-source-other-cursor-colorCursor color for other input sourcegreen

About respect mode

VariableDescriptionDefault
smart-input-source-respect-startSwitch to specific input source when mode enabled~’english~
smart-input-source-respect-evil-normal-escapeesc to English from evil normal satet
smart-input-source-respect-prefix-and-bufferHandle prefix key and buffert
smart-input-source-respect-go-english-triggersAdditional trigger to save input source and go Englisht
smart-input-source-respect-restore-triggerAdditional trigger to restore input sourcenil
smart-input-source-prefix-override-keysPrefix keys to be respected~’(“C-c” “C-x” “C-h”)~
smart-input-source-prefix-override-recap-triggersFunctions trigger the recap of the prefix overridesee variable doc
smart-input-source-prefix-override-buffer-disable-predicatesPredicates on buffers to disable prefix overridingsee variable doc

About language pattern

VariableDescriptionDefault
smart-input-source-english-patternPattern to identify a character as English[a-zA-Z]
smart-input-source-other-patternPattern to identify a character as other\cc , see emacs manual
smart-input-source-blank-patternPattern to identify a character as blank[:blank:]

About follow context mode

VariableDescriptionDefault
smart-input-source-follow-context-fixedContext is fixed to a specific languagenil
smart-input-source-follow-context-aggressive-lineAggressively detect context across blank linest
smart-input-source-follow-context-hooksHooks trigger the context followingsee variable doc

About inline mode

Face NameDescriptionDefault
smart-input-source-inline-faceFace for the inline region overlay
smart-input-source-inline-not-max-pointInsert new line when the whole buffer ends with the regiont
smart-input-source-inline-tighten-head-ruleRule to delete the head spaces1
smart-input-source-inline-tighten-tail-ruleRule to delete the tail spaces1
smart-input-source-inline-single-space-close1 space to close the region, default is 2 spaces/returnt
smart-input-source-inline-with-englishenable the inline english region featuret
smart-input-source-inline-with-otherenable the inline other language region featurenil
smart-inpu-source-inline-english-activated-hookHook to run after inline english region activatednil
smart-inpu-source-inline-english-deactivated-hookHook to run after inline english region deactivatednil
smart-inpu-source-inline-other-activated-hookHook to run after inline other language region activatednil
smart-inpu-source-inline-other-deactivated-hookHook to run after inline other language region deactivatednil

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 smart-input-source-get.

How the package know the input source change.

  1. If your input source is switched from smart-input-source, then everything should be OK naturally. And your smart-input-source-auto-refresh-seconds can even be set to nil.
  2. If your input source is switched from OS, to detect the switch timely, smart-input-source-auto-refresh-seconds should not be too large.
  3. 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 smart-input-source-get directly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.