我正在为类似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”一词时,它实际上并不计算parens,而是计算语法表定义为类似于paren的那些字符,因此,如果您设置语法表,使得{和}被声明为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
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句