微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在emacs lua模式下配置缩进?

在这里完成emacs新手

我在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-mode的源代码后,我想出了以下方法.

无可比拟的认缩进的原因是一个名为“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的安装说明,那应该是.

我希望这次对你有用,如果没有,让我知道.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


1.github代码实践源代码是lua脚本语言,下载th之后运行thmain.lua-netTypevgg-savevgg_cifar10/-S0.0001,报错: 试看看安装lua:报错了,参考这篇文章:ubuntu18.04安装lua的步骤以及出现的问题_weixin_41355132的博客-CSDN博客问题解决,安装成功:情况并没有好转,出现相
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/p/5999924.html前言在看了uwa之前发布的《Unity项目常见Lua解决方案性能比较》,决定动手写一篇关于lua+unity方案的性能优化文。整合lua是目前最强大的unity热更新方案,毕竟这是唯一可以支持ios热更新的办法。然而作
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能。1.TIMERime是一款可以高度自定义的输入法,相关教程可以查看往期文章,关于时间获取是指输入一个指定关键字,输出当前时间,效果如下(我定义了time关键字):实现如下:①在用户文件夹中新建一个rime.lua文件加入如下代码 ti
localfunctiongenerate_action(params)localscale_action=cc.ScaleTo:create(params.time,params.scale_x,params.scale_y)localfade_action=cc.FadeIn:create(params.time)returncc.Spawn:create(scale_action,fade_action)end
2022年1月11日13:57:45 官方:https://opm.openresty.org/官方文档:https://opm.openresty.org/docs#table-of-contents为什么建议使用opm不建议使用luarocks?http://openresty.org/cn/using-luarocks.html官方解释:请注意!LuaRocks并不是OpenResty官方推荐的装包方式。LuaRoc
在Lua中的table(表),就像c#中的HashMap(哈希表),key和value一一对应。元表:table的一个操作的拓展,里面包含关联了对应的方法,元方法就是其中一个。元方法:当你通过键来访问table的时候,如果这个键没有值,那么Lua就会寻找该table的metatable(假定有metatable)中的__index键。如果__inde
表排序:table.sort(list[,comp])参数list:指定表,可选参数comp:排序函数,无参数时通常按升序排序。排序函数针对表中连续的序列,其间不可以存在空洞或nil,排序函数需要两个形参(对应表中每次参加比较的两个数据),需要一个比较两个形参表达式的返回值,不能含有等于关系,例如>=,<=,==。do
一、安装lua环境1.1安装依赖包[root@centos7~]#yuminstallgccreadline-devel1.2下线lua源码包并解压[root@centos7~]#wgethttp://www.lua.org/ftp/lua-5.3.5.tar.gz[root@centos7~]#tarxvflua-5.3.5.tar.gz-C/usr/local/src1.3进行编译[root@centos7~]
官网OpenResty® 是一个基于 Nginx 与Lua的高性能Web平台,其内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态Web应用、Web服务和动态网关。OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由
表参考《lua程序设计》可以认为,表是一种动态分配的对象,程序只能操作指向表的引用(或指针)。除此以外,Lua语言不会进行隐藏的拷贝(hiddencopies)或创建新的表--创建表a={}--创建空表k="x"a[k]=10--键“x”值10a[20]="great"--键20值“great”print(a["x"])-->10