Emacs自定义缩进 [英] Emacs custom indentation

查看:135
本文介绍了Emacs自定义缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队使用特殊类型的文件进行配置,我想使用emacs对文件进行自动缩进(块缩进)。

My team uses a special type of file for configuration, and I would like to auto-indent (block indent) the file using emacs.

我想通过开头圆括号的选项卡大小增加缩进 - {或[,并减少右括号的选项卡大小 - }或]。

I would like to increase the indentation by a tab size for an opening parenthesis - { or [, and decrease by a tab size for a closing parenthesis - } or ] .

例如,

files = {
    file1 = first_file.txt
    file2 = second_file.txt
    rules = { 
        skip_header = 1
        fast_process = 1
    }
}

C风格的缩进不起作用,因为一行不以分号结尾。

C-style indentation doesn't work since a line doesn't end with semi-colon.

我已经研究了今天半天的emacs缩进,但仍然不知道该怎么做。

I have studied about emacs indentation for half a day today, but still doesn't know how to do this.

推荐答案

从文本模式或某些东西导出新模式,并创建自己的缩进功能。我知道这比说起来容易一些,所以这可能就够了:

Derive a new mode from text-mode or something and create your own indentation function. I know it's easier said than done, so this might be close enough:

(define-derived-mode foo-mode text-mode "Foo"
  "Mode for editing some kind of config files."
  (make-local-variable 'foo-indent-offset)
  (set (make-local-variable 'indent-line-function) 'foo-indent-line))

(defvar foo-indent-offset 4
  "*Indentation offset for `foo-mode'.")

(defun foo-indent-line ()
  "Indent current line for `foo-mode'."
  (interactive)
  (let ((indent-col 0))
    (save-excursion
      (beginning-of-line)
      (condition-case nil
          (while t
            (backward-up-list 1)
            (when (looking-at "[[{]")
              (setq indent-col (+ indent-col foo-indent-offset))))
        (error nil)))
    (save-excursion
      (back-to-indentation)
      (when (and (looking-at "[]}]") (>= indent-col foo-indent-offset))
        (setq indent-col (- indent-col foo-indent-offset))))
    (indent-line-to indent-col)))

打开文件,然后执行 Mx foo-mode

这篇关于Emacs自定义缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆