我正在为类似 Lisp 的语言编写一个简单模式,但在设置缩进时遇到了问题。我一直在关注emacswiki mode tutorial .
但是,我不知道如何使他们的示例缩进适应我的需要,因为他们不进行任何形式的计数。
基本上,每次看到 {
或 (
时,我只需要在缩进计数中添加 2 个空格,即使同一行上有多个空格,并且当我看到上面的闭包时,减去 2 个空格。我是 elisp 的新手;如何调整他们的示例来计算大括号和方括号的数量?
为了方便起见,这里是他们正在使用的代码(对于非括号语言):
(defun wpdl-indent-line ()
"Indent current line as WPDL code"
(interactive)
(beginning-of-line)
(if (bobp) ; Check for rule 1
(indent-line-to 0)
(let ((not-indented t) cur-indent)
(if (looking-at "^[ \t]*END_") ; Check for rule 2
(progn
(save-excursion
(forward-line -1)
(setq cur-indent (- (current-indentation) default-tab-width)))
(if (< cur-indent 0)
(setq cur-indent 0)))
(save-excursion
(while not-indented
(forward-line -1)
(if (looking-at "^[ \t]*END_") ; Check for rule 3
(progn
(setq cur-indent (current-indentation))
(setq not-indented nil))
; Check for rule 4
(if (looking-at "^[ \t]*\\(PARTICIPANT\\|MODEL\\|APPLICATION\\|WORKFLOW\\|ACTIVITY\\|DATA\\|TOOL_LIST\\|TRANSITION\\)")
(progn
(setq cur-indent (+ (current-indentation) default-tab-width))
(setq not-indented nil))
(if (bobp) ; Check for rule 5
(setq not-indented nil)))))))
(if cur-indent
(indent-line-to cur-indent)
(indent-line-to 0))))) ; If we didn't see an indentation hint, then allow no indentation
我怎样才能实现类似 lisp 的缩进(而且还带有大括号)?
最佳答案
如果你想要 Lisp 风格的语言一些简单的东西,我建议你从 (syntax-ppss)
开始,它会返回此时的“解析状态”。该状态的第一个元素是当前的括号嵌套深度。虽然我使用了“paren”这个词,但这并不真正计算括号,而是计算语法表定义为类似括号的那些字符,因此如果您设置语法表,使得 { 和 } 被声明为类似括号,那么这些也将被计算在内。
所以你可以从类似的事情开始
(defun foo-indent-function ()
(save-excursion
(beginning-of-line)
(indent-line-to (* 2 (car (syntax-ppss))))))
不要将其定义为交互式,因为使用它的方法是添加
(set (make-local-variable 'indent-line-function) #'foo-indent-function)
在你的主模式函数中。
但也许更好的选择就是简单地执行以下操作:
(require 'smie)
...
(define-derived-mode foo-mode "Foo"
...
(smie-setup nil #'ignore)
...)
这将使用 4 级缩进(在 smie-indent-basic
中配置)。
关于emacs - 创建 emacs 模式 : defining indentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989800/