我正在为类似Lisp的语言编写一个简单的模式,并且在设置缩进时遇到了问题。我一直在学习emacswiki模式教程。
然而,我不知道如何根据我的需要调整他们的示例缩进,因为他们不进行任何形式的计数。
基本上,每次看到{
或(
时,我只需要在缩进计数中添加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嵌套深度。虽然我使用了"paren"这个词,但它并没有真正计算paren,而是计算语法表定义为paren-like的字符,所以如果您设置语法表,使{和}声明为paren-link,那么这些字符也将被计算在内。
所以你可以从这样的东西开始
(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
中配置)。