如何在函数中设置emacs缩进级别

时间:2014-12-11 15:29:37

标签: emacs elisp indentation

我不懂emacs,但我在文件中有以下功能:

;;; File: emacs-format-file
;;; Stan Warford
;;; 17 May 2006

(defun emacs-format-function ()
   "Format the whole buffer."
;;;   (c-set-style "stroustrup")
   (indent-region (point-min) (point-max) nil)
   (untabify (point-min) (point-max))
   (delete-trailing-whitespace)
   (save-buffer)
)

然后我在批处理脚本中运行此函数。这是代码片段:

echo "Indenting $1 with emacs in batch mode"
emacs -batch $1 -l $eprog_format -f emacs-format-function
echo

我使用此代码来格式化我的c / c ++文件和标题。我喜欢更改它,以便我可以将缩进级别硬编码到函数中,这样我就可以运行此代码以符合我目前正在编写代码的公司的任何缩进规则。或者将其作为参数传递?

我只是不知道该怎么做。有办法吗?我目前的.emacs有:

; Suppress tabs.
(setq-default indent-tabs-mode nil)

我不想在我的.emacs中添加缩进级别。我喜欢保留默认的emacs缩进。我希望emacs脚本在我“发送”之前自定义indentaion。

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用变量c-basic-offsetlet绑定来执行此操作。以下是let的工作原理示例:

    (setq original "Hello")

    (message "%s" original)
    "Hello"

    (defun temp-set-var (arg)
      (let ((original arg))
        (message "%s" original)))

    (temp-set-var "Goodbye")
    "Goodbye"

    (message "%s" original)
    "Hello"

即使我拨打了(message "%s" original)三次,它第二次输出了不同的字符串,因为我在original的函数中暂时将arg设置为let

所以也许你的格式功能可能是:

    (defun emacs-format-function (indent)
      "Format the whole buffer."
    ;;;   (c-set-style "stroustrup")
      (let ((c-basic-offset indent))
        (indent-region (point-min) (point-max) nil))
      (untabify (point-min) (point-max))
      (delete-trailing-whitespace)
      (save-buffer))

然后称之为:

emacs -batch $1 -l $eprog_format --eval="(emacs-format-function 8)"