使用 Emacs 缩进(移位 4)代码 [英] Using Emacs to indent (shift 4) code

查看:19
本文介绍了使用 Emacs 缩进(移位 4)代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ViewSourceWith 编辑我的 StackOverflow 答案和问题和 Emacs.通常,我包含代码和 StackOverflow 格式规则说它必须缩进四个空格才能被识别为这样的.手动或什至使用宏进行操作都很痛苦.

I edit my StackOverflow answers and questions with ViewSourceWith and Emacs. Often, I include code and StackOverflow formatting rules say that it must be indented by four spaces to be recognized as such. Doing it by hand or even with macros is painful.

我在 SO 之前的帖子中搜索过,但一无所获.

I searched in SO's previous postings but found nothing.

从Python模式开始,我写道:

Starting from the Python mode, I wrote:

(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)

它似乎有效,但我欢迎建议、替代方案、错误报告,等

It seems to work but I welcome advices, alternatives, bug reports, etc.

推荐答案

C-x TAB 运行 indent-rigidly.给定一个数字参数 4,它会做你想做的.或者使用 <pre><code> 来介绍您的代码(请参阅Markdown 编辑帮助的第一段).

C-x TAB runs indent-rigidly. Given a numerical argument of four it will do what you want. Alternatively use <pre><code> to introduce your code (see the first paragraph of Markdown Editing Help).

你的交互式声明最好写成:

your interactive declaration would better be written:

(interactive "r
p")

这篇关于使用 Emacs 缩进(移位 4)代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆