在Emacs ruby​​-mode中用制表符缩进而不是空格

我一直在尝试配置Emacs,以便在缩进
Ruby代码时插入’tab’而不是一系列’spaces’.

到目前为止,我已经尝试将var ruby​​-indent-tabs-mode设置为t,这样根据文档,如果这是非零的话,它将“以ruby模式插入制表符”.但到目前为止,没有骰子.

我也尝试通过Easy customization自定义它,它将以下内容插入到我的init.el中:

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(ruby-indent-tabs-mode t))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

并且在通过C-h v检查变量之后,它报告变量设置为t,但是按TAB继续插入空格.

我甚至尝试编辑.el文件以获取ruby-mode并重新编译它以使其无效.

帮助将不胜感激.

—–编辑—–

这是通过C-h m报告的次要模式:

Enabled minor modes: Abbrev Auto-Complete Auto-Composition
Auto-Compression Auto-Encryption File-Name-Shadow Font-Lock
Global-Auto-Complete Global-Font-Lock Inf-Ruby Line-Number Menu-Bar
Show-Smartparens Show-Smartparens-Global Smartparens
Smartparens-Global Transient-Mark

init.el文件目前有:

(require 'cask "/Users/snowingheart/.cask/cask.el")
(cask-initialize)
(require 'pallet)

(add-to-list 'load-path "~/elisp")
(load "php-mode")
(add-to-list 'auto-mode-alist
             '("\\.php[34]?\\'\\|\\.phtml\\'" . php-mode))

(global-set-key (kbd "C-x <up>") 'windmove-up)
(global-set-key (kbd "C-x <down>") 'windmove-down)
(global-set-key (kbd "C-x <right>") 'windmove-right)
(global-set-key (kbd "C-x <left>") 'windmove-left)

(require 'package)
(add-to-list 'package-archives
    '("marmalade" .
      "http://marmalade-repo.org/packages/"))
(package-initialize)

(global-set-key (kbd "C-x >") 'mc/mark-next-like-this)
(global-set-key (kbd "C-x <") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)
(global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)

(require 'smartparens-config)
(require 'smartparens-ruby)
(require 'smartparens-erb)
(smartparens-global-mode)
(show-smartparens-global-mode t)

(sp-with-modes '(rhtml-mode)
               (sp-local-pair "<%=" "%>")
               (sp-local-pair "<%-" "%>"))
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories
             "~/.emacs.d/.cask/24.3.50.1/elpa/auto-complete-20130724.1750/dict")
(ac-config-default)
(setq ac-ignore-case nil)
(add-to-list 'ac-modes 'enh-ruby-mode)
(add-to-list 'ac-modes 'web-mode)

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(indent-tabs-mode t)
 '(ruby-indent-tabs-mode t))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

(setq-default indent-tabs-mode t)
(setq enh-ruby-indent-tabs-mode t)

(smart-tabs-insinuate 'ruby)
(smart-tabs-advice ruby-indent-line ruby-indent-level)
(setq ruby-indent-tabs-mode t)

最佳答案 尝试将以下内容添加到init.el(在您已有的自定义项下面):

(setq-default indent-tabs-mode t)

从indent-tabs-mode的文档:

Indentation can insert tabs if this is non-nil.

我不使用ruby-mode,所以我不知道indent-tabs-mode和ruby-indent-tabs-mode之间可能的交互.将indent-tabs-mode设置为t(并删除您对ruby-indent-tabs-mode所做的自定义)可能就足够了.但是当您将上面的代码段添加到配置中时,Emacs的默认行为是插入缩进选项卡.

编辑

可以看到here,enh-ruby-mode定义了一个名为enh-ruby-indent-tabs-mode的可自定义变量,默认值为nil. Later on此变量的值用于覆盖indent-tabs-mode的值,这就是为什么将indent-tabs-mode设置为t对启用了enh-ruby-mode的缓冲区没有影响的原因.

因此除非您启用除ruby-mode和enh-ruby-mode之外的任何其他模式,否则可能正在修改indent-tabs-mode变量,添加

(setq enh-ruby-indent-tabs-mode t)

你的init.el应该解决你的问题.

另一个编辑(工作解决方案)

(致谢:This answer让我走上正轨.)

运用

> Emacs 24.3.1
> ruby​​-mode 1.2版(内置)
> enh-ruby-mode版本20140406.252(通过M-x package-install …安装)

通过将以下内容添加到一个完全空的init.el文件中,我能够使它工作:

(package-initialize)

(setq-default tab-width 2)
(setq enh-ruby-indent-tabs-mode t)
(defvaralias 'enh-ruby-indent-level 'tab-width)
(defvaralias 'enh-ruby-hanging-indent-level 'tab-width)

此解决方案适用于Emacs的GUI和控制台版本.它可能与您的其他自定义集成良好,但您需要从上面发布的init.el版本中删除自定义变量部分及其下面的所有内容.

另请注意,如果您遇到Emacs插入空格而不是选项卡的情况,您可以随时删除它并强制在quoting it之前通过C-q TAB插入选项卡.

包起来

原来在enh-ruby模式下有一个bug,当enh-ruby-indent-tabs-mode设置为t时,从第二级开始的块会导致缩进失败. enh-ruby-mode的作者/维护者没有修复它的计划,但是bug报告包含了一个可以修复问题的补丁.

点赞