【发布时间】:2011-06-01 14:48:47
【问题描述】:
我正在寻找一个yasnippet 模板,它允许我将许可证头添加到 Emacs 中的脚本缓冲区。有点像this,但改进了一点:
- 标题需要包含每个用户的数据,例如版权所有者的日期名称和电子邮件, 可以使用来自 yasn-p 的embedded elisp expansion 获得。
- 需要根据文件当前所处的编程模式使用语法注释标题。已经有a gist of a snippet that does all that。基本上它相当于在你的 sn-p 末尾嵌入
(comment-region (point-min) (point))。 - 现在,我想将评论样式更改为方框。请参阅
comment-style变量的 emacs 文档,或者,如果您想查看框式注释的外观,只需在活动区域上调用M-x comment-box:它使用正确的选项调用comment-region。
第一种方法是通过修改前一个 sn-p 的结尾来设置样式:
(let ((comment-style 'box))
(comment-region (point-min) (point)))
不幸的是,压痕搞砸了,我的盒子不是矩形。如果我从sn-p开始:
Copyright (c) ${1:`(nth 5 (decode-time))`}
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted`
(let ((comment-style 'box))
(comment-region (point-min) (point)))`
那个sn-p的扩展“打破了盒子”(我正在用ocaml注释语法调试这个sn-p,这并不重要):
(**************************************************************)
(* Copyright (c) 2010 *)
(* All rights reserved. *)
(* *)
(* Redistribution and use in source and binary forms, with or *)
(* without modification, are permitted *)
(**************************************************************)
- 起初我以为第二行是根据扩展前嵌入代码的大小缩进的,但在这种情况下,它应该使该行的最终
*)过早地出现 25 个空格,而不是 4 个。 - 如果它基于嵌入点存在的 no 文本进行缩进,则最终的
*)应该到达 4 个空格 late,而不是太 很快时间>。 - 最后,我不明白最后一行发生了什么,其中 没有嵌入式代码扩展:通常我从段落中获取方形注释框没有任何问题最后一行很短(使用
comment-box,或此问题的第一个评论块中的 elisp 函数。
我尝试在 sn-p 扩展之后进行注释,以避免任何副作用,将其添加到 yas/after-exit-snippet-hook,将上面 sn-p 的最后一个函数替换为:
(add-hook 'yas/after-exit-snippet-hook
(let ((comment-style 'box))
(comment-region (point-min) (point))) nil t)
但这并没有帮助。即使是这样,它也会给我留下一个扩展钩子,它会注释 all 我想在该缓冲区中使用的 sn-ps,这是我当然不想要的。
我还应该补充一点,我尝试通过添加将yas/indent-line 设置为fixed
# expand-env: ((yas/indent-line 'fixed))
在我的 sn-p 开头,但这并没有改变任何东西。关于如何获得矩形框的任何想法?
编辑:我们有 a very nice answer, along with a proposed fix,(感谢 Seiji !),但仍然存在一个问题,即如何使其适应想要重用字段的情况,比如重用$1 在:
Copyright (c) ${1:`(nth 5 (decode-time))`}
All rights reserved.
Redistribution and use in $1, in source and binary forms
`(let ((comment-style 'box))
(comment-region (point-min) (point-at-eol 0)))`
在这种情况下,模板引擎将在模板展开时(缩进后)将字段$1(即2011)获得的(可变长度)值复制到最后一行,给注释行2个字符太宽了。在编写模板时,很难预测应该在这一行删除 4 个字符。也许字段重用和正确的缩进太多了,不能同时要求。不过,有没有人看到一种方法来做到这一点?
【问题讨论】:
标签: emacs elisp code-snippets