3

我要对齐

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

成为

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

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

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

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

但是,我想通过连接到 align 机器来使 align-current 工作。即前两行中的任意点,正确对齐应该发生而不影响第三行。

4

1 回答 1

0

您无法扫描正则表达式中的 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)
            ))))))

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

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

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

于 2012-06-13T00:22:31.363 回答