gpt4 book ai didi

emacs - 如何使用align.el和 `align-current`轻松对齐let表单

转载 作者:行者123 更新时间:2023-12-01 18:14:29 25 4
gpt4 key购买 nike

我要对齐

(let ((blah  foo)
(asdfasdf asdasdfafd))
(message "foo"))

成为

(let ((blah     foo)
(asdfasdf asdasdfafd))
(message "foo"))

换句话说,let 边界值应该正确对齐。

如果我选择前两行,则此功能有效:

(defun align-try-1 (beg end)
(interactive "r")
(align-regexp beg end "^\\s-*+\\(?:(let\\S-*\\|\\)\\s-*(+\\S-+\\(\\s-+\\)" 1 1 nil))

但是,我想通过 Hook 来使当前对齐工作机械。即,对于前两行中任意位置的点,应该进行正确的对齐,而不会影响第三行。

最佳答案

您无法扫描正则表达式中的 sexp,因为它们是递归扫描的。

我在没有对齐的情况下写了这个东西(我不太确定是否可以使用对齐功能来完成,但坚持使用 sexp 移动、goto 和插入对我来说更容易)。

(defun align-try-1 (beg end)
(interactive "r")
(goto-char beg)
(let ((endmarker (move-marker (make-marker) end)))
(catch :break
(while t
(catch :continue
(let ((actual-begin (re-search-forward "(\\([[:space:]\n]*\\)let\\([[:space:]\n]*\\)(" endmarker t))
(max-column 0))
(unless actual-begin (throw :break nil))
(when (or (in-string-p)
(eq (get-char-property actual-begin 'face) 'font-lock-comment-face))
(throw :continue nil))
(while (and (< (point) endmarker) (ignore-errors (down-list) t) (ignore-errors (forward-sexp) t))
(when (looking-at "[[:space:]\n]+") (delete-region (match-beginning 0) (match-end 0)))
(insert " ")
(when (< max-column (current-column))
(setq max-column (current-column)))
(backward-up-list)
(forward-sexp))
(goto-char actual-begin)
(while (and (< (point) endmarker) (ignore-errors (down-list) t) (ignore-errors (forward-sexp) t))
(when (looking-at "[[:space:]\n]+") (delete-region (match-beginning 0) (match-end 0)))
(dotimes (i (- max-column (current-column))) (insert " "))
(backward-up-list)
(forward-sexp))
(goto-char actual-begin)
))))))

顺便问一下,你在哪里使用它?

更新:添加了对评论或字符串的检查。

更新:删除了强制格式化的东西,因为它有问题并且在原始问题中不需要。

关于emacs - 如何使用align.el和 `align-current`轻松对齐let表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10784588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com