标题: 初步学会emacs中的align-regexp的用法

时间: 2014-12-28 19:59:52

正文:

话说咱英语也是六级的水准, 看起 emacs 的帮助来还是相当费劲, elisp 的基本概念都有了, 为了对齐 php 中的多维数组的定义, 知道了基本思路应该是用 align-regexp 函数了.搜索了下中文和使用样例以后, 再翻看其英文说明, 竟是看得一愣一愣, 半明半白的:

align-regexp is an interactive autoloaded compiled Lisp function in
`align.el'.

(align-regexp BEG END REGEXP &optional GROUP SPACING REPEAT)

Align the current region using an ad-hoc rule read from the minibuffer.
BEG and END mark the limits of the region.  This function will prompt
for the REGEXP to align with.  If no prefix arg was specified, you
only need to supply the characters to be lined up and any preceding
whitespace is replaced.  If a prefix arg was specified, the full
regexp with parenthesized whitespace should be supplied; it will also
prompt for which parenthesis GROUP within REGEXP to modify, the amount
of SPACING to use, and whether or not to REPEAT the rule throughout
the line.  See `align-rules-list' for more information about these
options.

For example, let's say you had a list of phone numbers, and wanted to
align them so that the opening parentheses would line up:

    Fred (123) 456-7890
    Alice (123) 456-7890
    Mary-Anne (123) 456-7890
    Joe (123) 456-7890

There is no predefined rule to handle this, but you could easily do it
using a REGEXP like "(".  All you would have to do is to mark the
region, call `align-regexp' and type in that regular expression.

align-regexp 的基本用法是 M-x align-regexp, 然后输入对齐的标准字符比如等于符号 = . 为了对齐下面这样的 php 多维数组定义就必须 C-u M-x align-regexp 来使用差不多全部参数了.

[
    'GodYouList' => array('mod' => 'topic', 'class' => 'YouHick', 'func' => 'getYouList'),
    'GodCompanyList' => array('mod' => 'topic', 'class' => 'YouHick', 'func' => 'getCompanyList'),
    'GadamnCheck' => array('mod' => 'recommend', 'class' => 'GadamnHick', 'func' => 'GadamnCheck'),
    'GadamnJob' => array('mod' => 'recommend','class' => 'GadamnHick','func' => 'GadamnFriend'),
    'RecommedJob' => array('mod' => 'recommend','class' => 'GadamnHick','func' => 'recommendFriend'),
]

一开始都没明白 REGEXP 参数默认提示的 \(\s-*\) 是什么意思, 这也得抱怨下 elisp 或者 emacs 各种场景的正则真操蛋. 后来用了几次感觉出来了, 这是空格的正则. 英语六级也没白费, 看英文的时候, 猜 GROUP 参数大概是当定义多个正则匹配的分组---用 () 定义的正则子表达式---时, 需要填充空格位置的子表达式的编号. 多次尝试以后发现, SPACING 是填充的空格数. REPEAT 是一行内是否重复该规则---英文其实也能理解上去, 竟然没看明白.

        转载请注明出处 http://blog.hickwu.com/posts/334 by Hick

摸索的重点在怎么写正则上. 一开始还分解需要对齐的参考可以是 =>, 俩个符号, 但是 => 是需要在前面填充空格, 而 , 需要在后面填充空格, 不知道该如何写了, 简化成这样写出来是有些难看的: [>,]\(\s-*\) ,

多种尝试中慢慢明白了 REPEAT 的用处, 通过下面这俩个操作能实现比较完美的对齐了, 本来还想自己写个 elisp 函数封装下, 想想这样的场景不会太多, 操作两次就操作两次好了:

C-u M-x align-regexp \(\s-*\)= 1 1 n
C-u M-x align-regexp ,\(\s-*\) 1 1 y

最终效果:

[
   'GodYouList'     => array('mod' => 'topic',     'class' => 'YouHick',    'func' => 'getYouList'),      
   'GodCompanyList' => array('mod' => 'topic',     'class' => 'YouHick',    'func' => 'getCompanyList'),  
   'GadamnCheck'    => array('mod' => 'recommend', 'class' => 'GadamnHick', 'func' => 'GadamnCheck'),     
   'GadamnJob'      => array('mod' => 'recommend', 'class' => 'GadamnHick', 'func' => 'GadamnFriend'),    
   'RecommedJob'    => array('mod' => 'recommend', 'class' => 'GadamnHick', 'func' => 'recommendFriend'), 
]

查看更多文章
分享到:


分享到: