首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在emacs Lua-模式下配置缩进?

如何在emacs Lua-模式下配置缩进?
EN

Stack Overflow用户
提问于 2018-03-28 00:41:05
回答 2查看 0关注 0票数 0

例子:

代码语言:txt
复制
local foo = function()
  print("Hello, world!")
end

自动伸缩:

代码语言:txt
复制
local foo = function()
               print("Hello, world")
end
代码语言:txt
复制
local foo = function()
               print("Hello, world")
        end

是以错误的方式缩进的:

代码语言:txt
复制
local bar = foo(
    "one",
    "two",
   baz(), -- Note three spaces
   "quo"
)  

应该是:

代码语言:txt
复制
local bar = foo(
    "one",
    "two",
    baz(),
    "quo"
  )

第三种错误的缩进:

代码语言:txt
复制
local bar = foo(
    "one",
    "two"
  )

  local t = 5 -- This line should not be indented, 
              -- also note tab between local and t.

以下是我从Thomas获得的最新版本:

代码语言:txt
复制
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"
            )

除了明确指出的地方,我没有为缩进做任何事情,只在代码中输入并按下每一行末尾的返回。我实际上没有输入任何评论。

它应如下所示:

代码语言:txt
复制
local foo = function()
  print("Hello, world")
end

local bar = 5

local zzz = foo(
    "one",
    "two",
    three(),
    "four"
  )

还有一个错误的缩进情况:

代码语言:txt
复制
local foo =
{
bar(); -- Did press a TAB here, but closing brace killed it
baz;
}

应:

代码语言:txt
复制
local foo =
{
  bar();
  baz;
}

为了完整起见,下面是Lua模式

代码语言:txt
复制
local foo = function()
               print("Hello, world!")
            end

local bar = 5

local foo = bar(
bar,
   baz(),
   quo(),
aaa
)

local t =
{
"one",
two(),
}

通过调整:

代码语言:txt
复制
local foo = function()
           print("Hello, world!")
            end

            local bar = 5

            local foo = bar(
            bar,
               baz(),
               quo(),
               aaa
            )

            local t =
            {
            "one",
            two(),
         }

如下所示:

代码语言:txt
复制
local foo = function()
  print("Hello, world!")
end

local bar = 5

local foo = bar(
    bar,
    baz(),
    quo(),
    aaa
  )

local t =
{
  "one",
  two(),
}
EN

Stack Overflow用户

发布于 2018-03-28 09:24:20

在一个新的.lua文件中输入一行,如下所示:

代码语言:txt
复制
local foo = function()

将其保存在“lua-mode.el”所在目录下的名称“my-lua.el”下。

代码语言:txt
复制
;; 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))

将以下行添加到您的~/emacs.d/agladysh.el文件中(假设“agladysh”是您的用户名):

代码语言:txt
复制
(add-hook 'lua-mode-hook 
          (lambda () (unless (fboundp 'lua-calculate-indentation-right-shift-next)
                       (load-file (locate-file "my-lua.el" load-path)))))
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档