有一个 userscript,元信息[1]没有严格对齐,可以工作,但是不美观,怎么办呢?
// ==UserScript==
// @name Github Commented Issues
// @namespace https://github.com/jiacai2050
// @description Show your commented issues on github easily.
// @match https://github.com/issues*
// @author jiacai2050
// @version 0.2.3
// @icon https://github.githubassets.com/favicons/favicon.svg
// @grant none
// ==/UserScript==
align-regexp
to the resuce!
C-u M-x align-regexp @[a-z]+\(\s-*\) RET 1 RET 1 RET n
解释上面的命令前,先看下其签名:
(align-regexp BEG END REGEXP &optional GROUP SPACING REPEAT)
•REGEXP 用以对齐的正则,至少包含一个 group 即 ()
,通过修改这个 group 来达到对齐的效果
• @[a-z]+\(\s-*\)
这个正则表示用 @name
后的零或多个空格作为修改的 group
• \s-
表示空格,具体语法可参考 Regexp-Backslash[2]
• GROUP 标明正则式中哪个 group 用做修改来达到对齐效果
• SPACING 不同列之间的空格数
• REPEAT 是否作用在这一整行上,在本示例无所谓,因为最终只有两列
// ==UserScript==
// @name Github Commented Issues
// @namespace https://github.com/jiacai2050
// @description Show your commented issues on github easily.
// @match https://github.com/issues*
// @author jiacai2050
// @version 0.2.3
// @icon https://github.githubassets.com/favicons/favicon.svg
// @grant none
// ==/UserScript==
最后,封装成一个函数
(defun my/align-userscript-metablock (start end)
(interactive "r")
(align-regexp start end
"@[a-z]+\\(\\s-*\\)" 1 1 t))
the quick brown fox
jumped over the lazy
dogs the quick brown
C-u M-x align-regexp \(\s-*\)\s- RET 1 RET 0 RET y
the quick brown fox
jumped over the lazy
dogs the quick brown
• Aligning text[3]
• Aligning columns in Emacs[4]
• Emacs: Align Text[5]
[1]
元信息: https://violentmonkey.github.io/api/metadata-block/
[2]
Regexp-Backslash: https://www.gnu.org/software/emacs/manual/html_node/elisp/Regexp-Backslash.html
[3]
Aligning text: http://pragmaticemacs.com/emacs/aligning-text/
[4]
Aligning columns in Emacs: https://blog.lambda.cx/posts/emacs-align-columns/
[5]
Emacs: Align Text: http://ergoemacs.org/emacs/emacs_align_and_sort.html