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

时间:2012-05-28 12:21:42

标签: emacs elisp

我想对齐

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

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

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

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

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

但是,我想通过挂钩对齐来使对齐当前工作 机械。即,在前两行中的任意位置,应该在不影响第三行的情况下进行正确的对齐。

1 个答案:

答案 0 :(得分:0)

您无法在regexp中扫描性别,因为它们是递归扫描的。

我写的这个东西没有对齐(我不确定它是否可以使用对齐功能,但只是坚持sexp移动,gotos和插入使我更容易)。

(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)
            ))))))

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

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

更新:已删除强制格式化内容,因为它有错误并且在原始问题中不需要。