我使用ViewSourceWith 和Emacs 编辑StackOverflow的答案和问题.通常,我包含代码和StackOverflow格式规则 ,说它必须缩进四个空格才能被识别.手动或甚至使用宏来做这件事都很痛苦.
我在之前的帖子中搜索过,但一无所获.
从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)
它似乎工作,但我欢迎建议,替代方案,错误报告等.
kmk*_*lan 14
Cx TAB运行indent-rigidly.给出四个数字参数,它会做你想要的.或者使用<pre> <code>来介绍您的代码(请参阅Markdown Editing帮助的第一段).
编辑:最好写下您的互动声明:
(interactive "r
p")
pol*_*lot 12
另一种简单的方法是使用emacs的强大矩形编辑能力:设置你的区域从第一行的起点开始,在要缩进(注意最后一行的开头结尾:在它有是在年初这行,因为你不想替换现有的文本!),然后做
C-x r t (string-rectangle)
然后根据提示输入4个空格.瞧!不需要额外的lisp黑客攻击.此外,您还可以灵活地将空格旁边的其他内容插入到一堆行的开头或中间的任何位置.