1

I use Emacs devanagari-itrans input method to type in Devanagari. But I prefer using the usual Arabic symbols for numbers (1..9) rather than the Devanagari symbols. It is cumbersome to keep switching languages every time I have to type a number.

Is it possible to have a composite input system so that I get Arabic numbers with Devanagari characters?

1 Answer 1

2

Investigation

First, we need to find out where the devanagari-itrans method is defined. I used the brute-force method of grepping the Emacs source code. (C-h I devanagari-itrans RET will describe the input method, for example telling you what key sequences insert which characters, but it doesn't tell you where in the sources the method was defined.)

zrgrepping

Go to the directory containing your Emacs's core (i.e. not those coming from packages) elisp files. On GNU/Linux it should be something like /usr/local/share/emacs/EMACS_VERSION/lisp/ or /usr/share/emacs/EMACS_VERSION/lisp/ depending on whether you installed it via your package manager or manually. (You can get it by inspecting the values near the end of load-path (C-h v load-path) or just going to the definition of a random elisp function (e.g. C-h f quail-package and click on the hyperlinked el file (xxx is a compiled Lisp function in yyy.el)).)

We're using zrgrep because the files are compressed (.gz). If they were not, we could just use rgrep (recursive grep).

M-x zrgrep RET devanagari-itrans RET *.el.gz RET the_top_directory_containing_the_core_elisp_files.

In our case, we get two results both in ./leim/quail/indian.el.gz, which somehow must define the devanagari-itrans input method. The most interesting bit is:

(quail-define-indian-trans-package
 indian-dev-itrans-v5-hash "devanagari-itrans" "Devanagari" "DevIT"
 "Devanagari transliteration by ITRANS method.")

If we inspect the function quail-define-indian-trans-package (C-h f quail-define-indian-trans-package) we can see that its first argument is a set of hashtables (HASHTBLS) — presumably the hashtables that define the mapping between input ascii and output Devanagari. Let's look at it!

indian-dev-itrans-v5-hash

We inspect the variable indian-dev-itrans-v5-hash (C-h v indian-dev-itrans-v5-hash). Its value is not very interesting (a listing of hashtables), but we can see that it's defined in ind-util.el:

(defvar indian-dev-itrans-v5-hash
  (indian-make-hash indian-dev-base-table
              indian-itrans-v5-table))

The variable indian-itrans-v5-table is set to the result of (indian-make-hash indian-dev-base-table indian-itrans-v5-table). The function indian-make-hash might be interesting. We can go to its definition with M-. if the mark is over it or use C-h f as before.

indian-make-hash

It's rather long and (hopefully) we don't need to digest all of it. The general idea is that it's setting out the mapping between the output Devanagari (defined in table) and the input ascii (defined in trans-table), in the hash table. We're interested in digits, or more precisely we're interesting in not setting up the mapping for digits, so we can try commenting out the line putting the "digit componenents" in the hash table:

(defun indian-make-hash (table trans-table)
  "Indian Transliteration Hash for decode/encode"
  (let* ((encode-hash (make-hash-table :test 'equal))
     (decode-hash (make-hash-table :test 'equal))
     (hashtbls (cons encode-hash decode-hash))
     (vowels     (elt table 0))
     (consonants (elt table 1))
     (misc       (elt table 2))
     (digits     (elt table 3))
     (halant     (char-to-string (elt misc  4)))
     (trans-vowels     (elt trans-table 0))
     (trans-consonants (elt trans-table 1))
     (trans-misc       (elt trans-table 2))
     (trans-digits  '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9")))
    (indian--puthash-v vowels trans-vowels hashtbls)
    (indian--puthash-c consonants trans-consonants halant hashtbls)
    (indian--puthash-cv consonants trans-consonants
                  vowels trans-vowels hashtbls)
    (indian--puthash-m misc trans-misc hashtbls)
;;    (indian--puthash-m digits trans-digits hashtbls)
    hashtbls))

If you were to test this, by either over-riding the indian-make-hash function and re-evaluating everything relevant depending on it, or by just modifying the source (by commenting out that one line) and restarting Emacs, you could see that this would have the desired effect. However, modifying the sources is not, in general, very convenient, robust or future-proof, so we'll just define our own function and input method.

Solution (if you don't want to read the above)

Add the following to your init.el (or another file that's loaded by default):

(require 'ind-util)

;; based on:
;; https://github.com/emacs-mirror/emacs/blob/30c09955f64815201ae9b86215fed8ea2af0ba66/lisp/language/ind-util.el#L464
(defun indian-make-hash-without-digits (table trans-table)
  "Indian Transliteration Hash for decode/encode"
  (let* ((encode-hash (make-hash-table :test 'equal))
     (decode-hash (make-hash-table :test 'equal))
     (hashtbls (cons encode-hash decode-hash))
     (vowels     (elt table 0))
     (consonants (elt table 1))
     (misc       (elt table 2))
     (digits     (elt table 3))
     (halant     (char-to-string (elt misc  4)))
     (trans-vowels     (elt trans-table 0))
     (trans-consonants (elt trans-table 1))
     (trans-misc       (elt trans-table 2))
     (trans-digits  '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9")))
    (indian--puthash-v vowels trans-vowels hashtbls)
    (indian--puthash-c consonants trans-consonants halant hashtbls)
    (indian--puthash-cv consonants trans-consonants
                  vowels trans-vowels hashtbls)
    (indian--puthash-m misc trans-misc hashtbls)
;;    (indian--puthash-m digits trans-digits hashtbls)
    hashtbls))


;; based on:
;; https://github.com/emacs-mirror/emacs/blob/30c09955f64815201ae9b86215fed8ea2af0ba66/lisp/language/ind-util.el#L486
(setq indian-dev-itrans-v5-hash-without-digits
  (indian-make-hash-without-digits indian-dev-base-table
            indian-itrans-v5-table))

;; needed for the quail-define-indian-trans-package function:
(load "quail/indian.el.gz")

;; based on:
;; https://github.com/emacs-mirror/emacs/blob/30c09955f64815201ae9b86215fed8ea2af0ba66/lisp/leim/quail/indian.el#L56
(quail-define-indian-trans-package
 indian-dev-itrans-v5-hash-without-digits "devanagari-itrans-without-digits" "Devanagari" "DevIT"
 "Devanagari transliteration by ITRANS method, with Arabic numerals.")

The new input method is called devanagari-itrans-without-digits (though obviously you could change it to whatever you want).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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