在Emacs下使用多行表达式对行进行排序

时间:2013-01-18 09:59:59

标签: emacs

我正在寻找可以正确处理多行表达式的M-x sort-lines变体。例如:

this is the first line
this is the second lien
this is the (third 
             line
             spanning multiple lines because of parens)

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

Here是以类似方式解决的类似问题

(defun end-of-chunk ()
  "forward line or to ends of mid-expression."
  (interactive)
  (goto-char (point-at-eol))
  (let ((limit (point-at-bol))
        temp
        expr-beg)
    (while (and (setq temp (nth 1 (syntax-ppss)))
                (<= limit temp))
      (goto-char temp)
      (setq expr-beg (point)))
    (when expr-beg
        (goto-char expr-beg)
      (forward-sexp))))

(defun sort-lines-as-exprs (reverse beg end)
  "sort lines, or whole expression if line ends mid-expression."
  (interactive "P\nr")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (sort-subr reverse
                 'forward-line
                 'end-of-chunk))))