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

时间:2014-04-07 23:41:55

标签: ruby emacs indentation

我一直在尝试配置Emacs,以便它插入一个'标签'而不是一系列的空间'缩进Ruby代码时。

到目前为止,我已尝试将var ruby-indent-tabs-mode设置为t,以便根据文档,如果不是,则会在ruby模式下插入标签零。&#34 ;.但到目前为止,没有骰子。

我还尝试通过Easy customisation进行自定义,并将以下内容插入我的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.
 )

通过 Ch v 检查变量后,它报告变量设置为t,但按 TAB 继续插入空格。

我甚至尝试为ruby-mode编辑.el文件并重新编译它无效。

帮助将不胜感激。

-----编辑-----

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

  

启用次要模式:缩写自动完成自动组合
  自动压缩自动加密文件名称阴影字体锁定
  全局自动完成Global-Font-Lock Inf-Ruby线号菜单栏
  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)

1 个答案:

答案 0 :(得分:2)

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

(setq-default indent-tabs-mode t)

来自indent-tabs-mode的文档:

  

缩进可以插入标签,如果这是非零的。

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


修改

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

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

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

init.el应该解决您的问题。


另一个EDIT(工作解决方案)

(致谢: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和控制台版本。它可能与您的其他自定义功能很好地集成,但需要从您在上面发布的custom-set-variables版本中删除init.el部分及其下方的所有部分。

另请注意,如果您遇到Emacs插入空格而不是标签的情况,您可以随时将其删除,并强制quoting it通过 Cq <插入标签KBD> TAB


结束

enh-ruby-mode设置为{{1}时,enh-ruby-indent-tabs-mode中的bug导致块从第二级开始缩进失败}}。 t的作者/维护者没有修复它的计划,但是错误报告包含一个可以修复问题的补丁。