4

C-x RET \ or M-x set-input-method to select the text input method; e.g. I use greek and hebrew.

I must say C-\ is genius, because it brings you back to the default input which you use the most.

I want Emacs to automatically detect the entered text method and accordingly change the default font (font0 for English) as I enter the text input method to either font1 in case of Greek or font2 in case of Hebrew.

How to achieve this in Emacs 24.3.1 by Elisp in .emacs?

Note
Platform: Windows 7-32bit.

This is my trial:

(defun font-for-input ()
  (cond ((equal current-input-method nil)
         (face-remap-add-relative 'default :family "Consolas" :height 110))
        ((string-equal current-input-method "greek")
         (face-remap-add-relative 'default :family "Romans" :height 120))
        ((string-equal current-input-method "hebrew")
         (face-remap-add-relative 'default :family "DejaVu Sans" :height 120))
        )
  )
(font-for-input)

So there are three states:
nil: Consolas 11 or by hitting C-\ to deactivate set-input-method and return to default (English in my case)
Greek: change to Romans 12
Hebrew: change to DejaVu Sans 12

Interesting, this is my first code in Elisp! it works but clumsy. I want Emacs to automagically change these fonts as it detects the current-input-method if any? Any suggestions to improve the code (more consice) are appreciated.

Update
Ok, I admit this seems totally silly and impractical. I think what I need actually is 3 key bindings -- one for each language or input method (English, Greek, and Hebrew). Hitting either one should set-input-method to that language and change font accordingly and hook that to whatever buffer in current use -- be it text, org mode or TeX, etc. Any help?

3

Another option is to use the hooks namely input-method-activate-hook and input-method-deactivate-hook provided by emacs. Since you are new to elisp programming, you will find this section of the emacs manual helpful. Basically hooks are functions that are run when some event occurs.

In our case input-method-activate-hook is run when ever you activate a new input method. We use this hook to set font according to the input method being activated (you can read more about the hook by doing C-hvinput-method-activate-hookRET). You can use the following function to set the font according to you preferences

(defun my-change-font-according-to-input-method ()
  (face-remap-add-relative 'default
                           :family (cond ((string= current-input-method "greek") "Romans")
                                         ((string= current-input-method "hebrew") "DejaVu Sans")
                                         (t "Consolas"))
                           :height 120))

You will need to add it to input-method-activate-hook by adding this to your .emacs file

(add-hook 'input-method-activate-hook #'my-change-font-according-to-input-method)

Now whenever you change input method to greek the font will be set to 'Romans', changing input method to hebrew will change the font to "DejaVu Sans", font will be set to "Consolas" if the input method is set to anything else.

There is one problem with this approach though, reverting back to default input method by doing C-\ (or running the commandtoggle-input-method) does not actually activate any new input method as such the hook we added is not called and the font does not get reset to "Consolas". We can workaround the problem by using input-method-deactivate-hook. The hook is run every time an input method is deactivated (you can read more about the hook by doing C-hvinput-method-deactivate-hookRET).

(defun my-switch-to-default-font ()
  (when (and (eq this-command 'toggle-input-method)
             current-input-method)
    (face-remap-add-relative 'default
                             :family "Ubuntu"
                             :height 120)))

In the above function we check if the command being run is toggle-input-method and the current-input-method is non-nil (it will be nil, if default input method is being used), which means the input method is about to be set to nil. If this condition is true we set the font back to "Consolas". As with previous function you will need to add this function to input-method-deactivate-hook by adding the following to your .emacs file

(add-hook 'input-method-deactivate-hook #'my-switch-to-default-font)

Now you can change your input method as you normally do using C-xRETC-\ and C-\ and emacs will automatically set the font for you.

  • an elegant approach but two setbacks are there: First, number of keystrokes needed are way more than my answer. Second, I need to specify hebrew input method to hebrew-biblical-tiro, and I don't see how can this be explicitly passed in your hook-solution?. I could specify it in my answer (updated). – doctorate Dec 18 '14 at 17:50
  • is adding the hash'#' necessary in (add-hook 'input-method-deactivate-hook #'my-switch-to-default-font)? – doctorate Dec 18 '14 at 17:57
  • First, number of keystrokes needed are way more than my answer True but can always change the keybinding to something shorter, also this method is more general you do not have to write a new function for every input method. That said if hebrew and greek are the only input methods you are going to use then having dedicated bindings for them is not a bad idea. – Iqbal Ansari Dec 19 '14 at 1:32
  • As for changing to hebrew-biblical-intro you can do the same thing you did in your answer (change the function) or even better add another clause to cond – Iqbal Ansari Dec 19 '14 at 1:35
  • can you please care to answer this post about how to write hindi numbers: tex.stackexchange.com/q/191040/26295 – doctorate Dec 22 '14 at 10:59
1

Well I came up with this solution but I look forward to getting more suggestions for improvement.

The following code to be in .emacs:

(defun default-input-font () 

  "changes the set-input-method to nil and selects a default font 
bound to C-c f"
  (interactive)
  (set-input-method nil)
  (face-remap-add-relative 'default :family "Consolas" :height 110)
  )

(defun greek-input-font ()

  "changes the set-input-method to Greek and selects another font
bound to C-c g"
  (interactive)
  (set-input-method "greek")
  (face-remap-add-relative 'default :family "Romans" :height 120)
  )

(defun hebrew-input-font () 

  "changes the set-input-method to Hebrew  and selects another font
bound to C-c h"
  (interactive)
  (set-input-method "hebrew") ; you can specify like `hebrew-biblical-tiro`
  (face-remap-add-relative 'default :family "DejaVu Sans" :height 120)
  )

(global-set-key (kbd "C-c f") 'default-input-font)
(global-set-key (kbd "C-c g") 'greek-input-font)
(global-set-key (kbd "C-c h") 'hebrew-input-font)

Notes:

  • These key bindings are only suggestions you can choose whatever you like based on Key-bindings recommendations according to Emacs-manual.
  • Now when I write a document having two or more languages I can type with the font I like that best fits that language and visualization of its letters. For Hebrew I use SBL Hebrew font which is really helpful to show all diacritics nicely. So this will help you as you type in the language of your like without compromising visualization by the default font of the buffer. Be careful to choose explicitly the type of input method if there are more than one! see M-x list-input-methods to choose one that you need.

  • Advantage of this approach is that you still can make use of all the key bindings of Emacs as a professional text editor as long as your keyboard is not switched to another language from the language bar, making the choice for this approach more favorable than the one suggested by a question posted earlier.

  • I am open to any input or suggestions to improve this approach or may be find a better approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.