我使用ViewSourceWith和Emacs编辑我的StackOverflow答案和问题。经常我会包含代码,而StackOverflow格式规则要求必须缩进四个空格才能识别为代码块。手动缩进或者使用宏都很麻烦。
我在SO的以前的帖子中进行了搜索,但没有找到任何有用的信息。
从Python模式开始,我编写了:
(defun text-shift-region (start end count)
"Indent lines from START to END by COUNT spaces."
(save-excursion
(goto-char end)
(beginning-of-line)
(setq end (point))
(goto-char start)
(beginning-of-line)
(setq start (point))
(indent-rigidly start end count)))
(defun text-shift-region-right (start end &optional count)
"Shift region of code to the right
Stolen from python-mode.
The lines from the line containing the start of the current region up
to (but not including) the line containing the end of the region are
shifted to the right, by `text-indent-offset' columns.
If a prefix argument is given, the region is instead shifted by that
many columns. With no active region, indent only the current line."
(interactive
(let ((p (point))
(m (mark))
(arg current-prefix-arg))
(if m
(list (min p m) (max p m) arg)
(list p (save-excursion (forward-line 1) (point)) arg))))
(text-shift-region start end (prefix-numeric-value
(or count text-indent-offset)))
)
;; Code in StackOverflow must be marked by four spaces at the
;; beginning of the line
(setq text-indent-offset 4)
(global-set-key "\C-c>" 'text-shift-region-right)
看起来它可以工作,但我欢迎建议、替代方案、错误报告等。
M-x indent-region
。 - Ryan C. Thompson