我在Ubuntu上使用了emacs 23.1.1,其中包括emacs starter kit.我主要使用lua模式(使用package-install lua-mode进行安装).
我需要调整缩进的工作原理,因此它符合我的编码准则.
准则是:
>标签到空格;
>每个缩进两个空格;
>每行最多80个字符,不带尾随空格.
例:
local foo = function() print("Hello,world!") end
如果我不试图用自动缩进来与emacs取得联系:
local foo = function() print("Hello,world") end
更新:
(这属于一个评论,但由于需要额外的格式,我必须把它放在这里.)
如果我尝试托马斯的解决方案,我得到这个:
local foo = function() print("Hello,world") end
请注意,末端缩进了一个制表符和四个空格.
不行吗?
更新2:
这个东西也是以错误的方式缩进:
local bar = foo( "one","two",baz(),-- Note three spaces "quo" )
它应该是:
local bar = foo( "one","quo" )
更新3:
第三例错误缩进:
local bar = foo( "one","two" ) local t = 5 -- This line should not be indented,-- also note tab between local and t.
更新4:
这是我从Thomas获得的当前版本:
local foo = function() print("Hello,world") end local bar = 5 -- Emacs put \t before 5 local zzz = foo( -- Emacs put \t before foo "one",-- Pressed TAB here twice "two",three(),"four" )
除非明确指出,我没有做任何缩进,只输入代码,并在每一行结尾按RETURN.我没有真正输入任何评论.
它应该如下所示:
local foo = function() print("Hello,world") end local bar = 5 local zzz = foo( "one","four" )
更新5:
一个错误的缩进案例:
local foo = { bar(); -- Did press a TAB here,but closing brace killed it baz; }
应该:
local foo = { bar(); baz; }
更新6:
为了完整起见,这里是我所得到的current Git HEAD of lua-mode,没有托马斯的配置调整:
local foo = function() print("Hello,world!") end local bar = 5 local foo = bar( bar,quo(),aaa ) local t = { "one",two(),}
调整:
local foo = function() print("Hello,world!") end local bar = 5 local foo = bar( bar,aaa ) local t = { "one",}
为了符合我的编码准则,它应该如下所示:
local foo = function() print("Hello,world!") end local bar = 5 local foo = bar( bar,aaa ) local t = { "one",}
解决方法
无可比拟的默认缩进的原因是一个名为“lua-calculate-indentation”的函数,它计算要缩进当前行的列.不幸的是,返回的值与您所需的规格不匹配.
例如,如果您将一行输入到这样一个新鲜的.lua文件中:
local foo = function()
并按Enter键将点移动到第二行,可以通过键入M-:(lua-calculate-indentation)来调用上述功能.结果是15,这意味着lua模式将缩进到第二列到第15列.这是你原来的问题中描述和示例的非正统缩进的原因.
现在,为了解决这个问题,我建议重新定义函数“lua-calculate-indentation”,以便返回你想要的缩进.为此,将以下代码放入其他空文件中,并将其保存在“lua-mode.el”所在的同一目录中的“my-lua.el”下.
;; use an indentation width of two spaces (setq lua-indent-level 2) ;; Add dangling '(',remove '=' (setq lua-cont-eol-regexp (eval-when-compile (concat "\\((\\|\\_<" (regexp-opt '("and" "or" "not" "in" "for" "while" "local" "function") t) "\\_>\\|" "\\(^\\|[^" lua-operator-class "]\\)" (regexp-opt '("+" "-" "*" "/" "^" ".." "==" "<" ">" "<=" ">=" "~=") t) "\\)" "\\s *\\="))) (defun lua-calculate-indentation (&optional parse-start) "Overwrites the default lua-mode function that calculates the column to which the current line should be indented to." (save-excursion (when parse-start (goto-char parse-start)) ;; We calculate the indentation column depending on the prevIoUs ;; non-blank,non-comment code line. Also,when the current line ;; is a continuation of that prevIoUs line,we add one additional ;; unit of indentation. (+ (if (lua-is-continuing-statement-p) lua-indent-level 0) (if (lua-goto-nonblank-prevIoUs-line) (+ (current-indentation) (lua-calculate-indentation-right-shift-next)) 0)))) (defun lua-calculate-indentation-right-shift-next (&optional parse-start) "Assuming that the next code line is not a block ending line,this function returns the column offset that line should be indented to with respect to the current line." (let ((eol) (token) (token-info) (shift 0)) (save-excursion (when parse-start (goto-char parse-start)) ; count the balance of block-opening and block-closing tokens ; from the beginning to the end of this line. (setq eol (line-end-position)) (beginning-of-line) (while (and (lua-find-regexp 'forward lua-indentation-modifier-regexp) (<= (point) eol) (setq token (match-string 0)) (setq token-info (assoc token lua-block-token-alist))) ; we found a token. Now,is it an opening or closing token? (if (eq (nth 2 token-info) 'open) (setq shift (+ shift lua-indent-level)) (when (or (> shift 0) (string= token ")")) (setq shift (- shift lua-indent-level)))))) shift))
该代码将缩进级别设置为两个空格(而不是3),修改检测语句是否延伸多行的正则表达式,最后使用辅助方式重新定义缩进函数.
剩下要做的就是确保这段代码实际加载.这必须在原始的lua模式加载之后发生,否则该代码将重新安装原始缩进功能.
我们这样做的方式有点麻烦:我们安装一个回调函数,每次缓冲区将其主模式更改为lua模式时调用.然后它检查是否定义了前面提到的辅助功能 – 如果不是,它加载“my-lua.el”.那有点脆弱,但是只要你不玩lua源代码,你应该没事.
将以下行添加到〜/ emacs.d / agladysh.el文件(假设“agladysh”是您的用户名):
(add-hook 'lua-mode-hook (lambda () (unless (fboundp 'lua-calculate-indentation-right-shift-next) (load-file (locate-file "my-lua.el" load-path)))))
我假设lua模式在你的加载路径上,如果你遵循lua-mode的安装说明,那应该是.
我希望这次对你有用,如果没有,让我知道.