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