GNU Emacs 24.1: forcing the TAB key to insert a <tab> character in ESS mode

隐身守侯 提交于 2019-12-10 11:43:22

问题


I almost never use the TAB key for anything except inserting a <tab> (\t) character. It is unacceptable to continue typing C-q TAB to do this, because I mostly use tabs for comments. Most of my code indentation is accomplished by Emacs automatically matching my first line of manual indentation, and by binding indent-region to C-TAB. I mostly use the tab key to indent my comments. I prefer to set my tab width to 8 characters and indent each of my comments by two tab characters for a total of 16 characters of white space. I would be OK living with the existing system for managing indentation if ESS were not prone to failure as described here. The file that I am currently working on has broken lines which cause exactly the problem described. At present, in that file, pressing the tab key produces NO result.

How does one force the tab key to insert a tab character in ESS[S] and ESS[SAS] modes?

I have tried adding

(global-set-key "\C-i" 'self-insert-command)

and

(setq-default tab-always-indent nil)

to my .emacs file, but this has not changed behavior in ESS. I cannot figure out what Emacs calls ESS[S] mode internally, i.e.,

(define-key ess-mode-map "\C-i" 'self-insert-command)

produces the error

"Variable ess-mode-map is void."

I tried ess-S-mode, ess-s-mode, ESS-S-mode, and a few others with the same result.

I have also confirmed that ess-mode-hook and ess-mode-map are the expected hook and map variables. I then tried

(add-hook 'ess-mode-hook (lambda () (define-key "\t" ess-mode-map 'self-insert-command)))

which did not work.

I tried editing the ess-mode.el file and changing the keymap for "\t" to 'self-insert-command which produced no effect.


回答1:


Sounds like the map variable is not defined before the code defining the mode is loaded, or perhaps not even before the mode is enabled.

  • If the first, then require the library before trying to bind the key. Or use eval-after-load.

  • If the second, then put the binding on the mode hook:

    (add-hook 'THE-MODE-HOOK (lambda () (define-key "\t" THE-MODE-MAP 'self-insert-command)))
    

You might have to peek into the source code to find out what THE-MODE-HOOK and THE-MODE-MAP are. Or you might get what you need from C-h f THE-MODE, where THE-MODE is the mode name.




回答2:


M-i is bound to tab-to-tab-stop in Emacs, which continuously adds 8 spaces but i'm sure that's customizable. There are few more commands that might help you at Indentation Commands.




回答3:


The practical solution was simple, just add:

(add-hook 'ess-mode-hook (lambda () (local-set-key "\t" 'self-insert-command)))

to the .emacs file.




回答4:


I don't quite understand your problem but these are the two alternative options:

  1. Rebind the key locally:

    (define-key ess-mode-map "\C-i" 'self-insert-command)
    
  2. Change the behavior of ess tab, which will make the TAB to indent only at the begining of the line, otherwise it will always insert plain tab:

    (setq ess-tab-always-indent nil)
    


来源:https://stackoverflow.com/questions/23748046/gnu-emacs-24-1-forcing-the-tab-key-to-insert-a-tab-character-in-ess-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!