13

On Emacs, while editing a text document of notes for myself (a .txt document, not a .tex document), I am using M-x set-input-method Ret TeX, in order to get easy access to various Unicode characters. So for example, typing \tospace causes a "→" to be inserted into the text, and typing x^2 causes "x2" to be inserted, because the font I am using has support for Unicode codepoints 0x2192 and 0x00B2, respectively.

One of the specially handled characters in the method is for the underscore key, _. However, the font I am using for Emacs does not appear to have support for the codepoints for the various subscript characters, such as subscript zero (codepoint 0x2080), and so when I type _0, I get something rendered as a thin blank in my output. I would prefer to just have the two characters _0 in this case.

I can get _0 by the awkward keystroke sequence _spacedel0, since the space keystroke in the middle of the sequence causes Emacs to abort the TeX input method. But this is awkward.

So, my question: How can I locally customize my Emacs to not remap the _ key at all when I am in the TeX input method? Or how can I create a modified clone (or extension, etc) of the TeX input method that leaves out underscore from its magic?

Things I have tried so far:

  • I have already done M-xdescribe-key on _; but it is just bound to self-insert-command, like many other text characters. I did see a post-self-insert-hook there, but I have not explored trying to use that to subvert the TeX input method.

Things I have not tried so far:

  • I have not tried learning anything about the input method architecture or its source code. From my quick purview of the code and methods. it did not seem like something I could quickly jump into.
pnkfelix
  • 3,770
  • 29
  • 45

4 Answers4

6

So here is the solution I just found: Make a personalized copy of the TeX input method, with all of the undesirable entries removed. Then when using M-x set-input-method, select the personalized version instead of TeX.

I would have tried this earlier, but the built-in documentation for set-input-mode and its ilk does not provide sufficient guidance to the actual source for the input-methods for me to find it. It was only after doing another search on SO and finding this: Emacs: Can't activate input method that I was able to get enough information to do this on my own.

Details:

  1. In Emacs, open /usr/share/emacs/22.1/leim/leim-list.el and find the entry for the input method you want to customize. The entry will be something like the following form:

    (register-input-method
     "TeX" "UTF-8" 'quail-use-package
     "\\" "LaTeX-like input method for many characters."
     "quail/latin-ltx")
    
  2. Note the file name prefix referenced in the last element in the form above. Find the corresponding Elisp source file; in this case, it is a relative path to the file quail/latin-ltx.el[.gz]. Open that file in Emacs, and check it out; it should have the entries for the method remappings, both desired and undesired.

  3. Make a user-local copy of that Elisp source file amongst your other Emacs customizations. Open that local copy in Emacs.

  4. In your local copy, find the (quail-define-package ...) form in the file, and change the name of the package; I used FSK-TeX as my new name, like so:

    (quail-define-package
     "FSK-TeX" "UTF-8" "\\" t   ;; <-- The first argument here is the important bit to change.
     "LaTeX-like input method for many characters but not as many as you might think.
     ...)
    
  5. Go through your local copy, and delete all the S-expressions for mappings that you don't want.

  6. In your .emacs configuration file, register your customized input method, using a form analogous to the one you saw when you looked at leim-list.el in step 1:

    (register-input-method
     "FSK-TeX" "UTF-8" 'quail-use-package
     "\\" "FSK-customized LaTeX-like input method for many characters."
     "~/ConfigFiles/Elisp/leim/latin-ltx")
    
  7. Restart Emacs and test your new input-method; in my case, by doing M-x set-input-method FSK-TeX, typing a_0, and confirming that a_0 shows up in the buffer.


So, there's at least one answer that is less awkward once you have it installed than some of the workarounds listed in the question (and as it turns out, are also officially documented in the Emacs 22 manual as a way to cut off input method processing).

However, I am not really happy with this solution, since I would prefer to inherit future changes to TeX mode, and just have my .emacs remove the undesirable entries on startup.

So I will wait to see if anyone else comes up with a better answer than this.

Community
  • 1
  • 1
pnkfelix
  • 3,770
  • 29
  • 45
  • 1
    You can add sequences with `(quail-defrule "\\sqrt" (decode-char 'ucs #x2713) "TeX")`. But I haven't found an equivalent `quail-undefrule` command for removing them... – Jim Paris Feb 27 '14 at 01:35
  • 2
    You can use `(with-temp-buffer (set-input-method "TeX") (quail-map))` to get the mapping and process it to reconstruct the rules for defining your own input method. – kdb Aug 10 '15 at 13:12
  • @kdb thanks, I will try to adapt that into my solution! – pnkfelix Oct 07 '15 at 18:29
1

I did not test this myself, but this seems to be the exact thing you are looking for:

"How to disable underscore subscript in TeX mode in emacs" - source

Two solutions are given in this blogpot:

  1. By the author of the blogpost: (setq font-lock-maximum-decoration nil) (from maximum)

  2. Mentioned as comment:

    (eval-after-load "tex-mode" '(fset 'tex-font-lock-subscript 'ignore))

Community
  • 1
  • 1
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • Mmm, TeX mode plus `font-lock` minor mode is not the same as `set-input-method` TeX. Note in particular that I am editing a text document (extension: `.txt`), not a TeX one (extension: `.tex`). I will look at the blog post though and see if there is anything else there that might help. – pnkfelix Mar 03 '13 at 00:19
  • Yea, sorry to inform you but indeed, I do not think either of them work now that I checked (one similar variable has been called obsolete). – PascalVKooten Mar 03 '13 at 00:21
  • 1
    could you maybe try `(global-set-key "_" '(lambda () (interactive) (insert "_")))`, I am curious if it is the actual character that has gone wrong. (though maybe indeed, since it is bound to `self-inserting-command`, my bet now would be that some `advice` is involved) – PascalVKooten Mar 03 '13 at 00:23
  • 1
    I just had the same idea, but it does not seem to work: While in the non TeX input-method, the customized function gets called, but while in the TeX input-method, the undesirable behavior occurs. I take this as a sign that my experiment to use `describe-key` to find out what the key-binding is may have misled me, since it may be that `describe-key` was only using the key binding for \_ from the mini-buffer, and not the binding from the buffer with input method TeX turned on. – pnkfelix Mar 03 '13 at 00:37
1

The evil plugin for vim-like modal keybinding allows to map two subsequent presses of the _ key to the insertion of a single _ character:

(set-input-method 'TeX)
(define-key evil-insert-state-local-map (kbd "_ _")
  (lambda () (interactive) (insert "_")))
(define-key evil-insert-state-local-map (kbd "^ ^")
  (lambda () (interactive) (insert "^")))

When _ and then 1 is pressed, we get as before, but

when _ and then _ is pressed, we get _.

Analogous for ^.

m0rphism
  • 103
  • 9
0

As already explained in pnkfelix answer, it seems we have to make a personalized copy of the TeX input method. But here comes a lighter way to do that, without any file tweaking. Simply put the following in your .emacs :

(eval-after-load "quail/latin-ltx"
  '(let ((pkg (copy-tree (quail-package "TeX"))))
     (setcar pkg "MyTeX")
     (assq-delete-all ?_ (nth 2 pkg))
     (quail-add-package pkg)))
(set-input-method 'TeX)
(register-input-method "MyTeX" "UTF-8" 'quail-use-package "\\")
(set-input-method 'MyTeX)

The important part is the assq-delete-all line in the middle that remove all shortcut entries starting with _. It's a bit of a lisp hack but it seems to work. Since I'm also annoyed by the shortcuts starting with - and ^, I also use the following two lines to disable them :

     (assq-delete-all ?- (nth 2 pkg))
     (assq-delete-all ?^ (nth 2 pkg))

Note that afterwards you can M-x set-input-method at any time and indicate TeX or MyTeX to switch between the pristine TeX input method or the customized one.

polio
  • 1