2013-05-07 104 views
2

我試圖實現以下目標:emacs的對齊功能參數垂直

void foo(int one, 
int const & two, 
Bar three) 

void foo(int   one, 
      inst const & two, 
      Bar   three) 

這可能使用的align-regex函數來完成(有或沒有前綴)?

更一般地說,正則表達式中的分組意味着什麼(它是否被認爲是'列')?那麼「括號內的小組需要修改(證明是否否定)」?

由於

回答

1

參見C-H˚Falign-regexpRET和,特別地,鏈接C-Hvalign-rules-listRET提供了一些用於對準的最佳文檔。

「要修改的組」表示在對齊時將縮小或擴大的模式中的組。你幾乎總是希望這個小組純粹是空白的,以避免刪除實際內容。

的組參數 - 交互「括號組來修改(如果證明負)」 - 是在正則表達式問題的組的從1

的理由部分計數的數目,是有一點麻煩。如果你提供一個負數,則同一組作爲如果該號碼是積極的,但align-rules-list變量的「證明」的行爲也引發:

`justify' 
It is possible with `regexp' and `group' to identify a 
character group that contains more than just whitespace 
characters. By default, any non-whitespace characters in 
that group will also be deleted while aligning the 
alignment character. However, if the `justify' attribute 
is set to a non-nil value, only the initial whitespace 
characters within that group will be deleted. This has 
the effect of right-justifying the characters that remain, 
and can be used for outdenting or just plain old right- 
justification. 
+0

對不起,延遲,我有emacs正則表達式的問題。我的emacs版本(21.4和22.4)都沒有關於align-rules-list的文檔。因此,如果您有多個括號組,爲了對齊,只有「要修改的組」指示的相關? – Taras 2013-05-14 01:09:23

+0

對於正確的'align-regexp'。另外,Emacs 21和22都很舊。你不妨考慮升級。 – phils 2013-05-14 03:20:49

2

IMO這是一個情況下使用正則表達式的變得複雜。使用語法-ppss的功能更容易:

(defun my-arguments-indent() 
    "When called from inside an arguments list, indent it. " 
    (interactive "*") 
    (save-excursion 
    (let* ((pps (syntax-ppss)) 
      (orig (point)) 
      indent) 
     (while (and (nth 1 pps)(not (eobp))) 
     (setq indent (save-excursion 
         (when (nth 1 pps) 
         (goto-char (nth 1 pps)) 
         (forward-char 1) 
         (skip-chars-forward " \t") 
         (current-column)))) 
     (when (and (< orig (line-beginning-position)) indent) 
      (beginning-of-line) 
      (fixup-whitespace) 
      (indent-to indent)) 
     (forward-line 1) 
     (back-to-indentation) 
     (setq pps (syntax-ppss)))))) 
+0

沒錯,這是'align-regexp'的一個非常棘手的情況。我可能會使用自定義的「align-rules-list」規則(因爲您可以指定多個組來對齊,這可以簡化事情),但這種方法看起來很有趣。我真的需要記住'syntax-ppss'存在。 +1 – phils 2013-05-08 05:19:04

+0

@AndreasRöhler,你能否詳細解釋一下如何使用這個功能?我已經把它放在init.el中,重新運行emacs,選擇函數並按列表並看不到任何效果。 – klm123 2013-11-06 18:59:17

+0

@ klm123這只是一個演示算法,當在第二個參數處使用point來調用時。擴展它,所以它也應該從第一個參數中起作用。 – 2013-11-07 19:38:49