您无法扫描正则表达式中的 sexps,因为它们是递归扫描的。
我在没有 align 的情况下编写了这个东西(我不确定是否可以使用 align 函数来完成,但只坚持 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)
))))))
顺便问一下,你在哪里使用它?
更新:添加了在评论或字符串中的检查。
更新:删除了强制格式化的东西,因为它有问题并且在原始问题中不需要。