简体   繁体   English

Emacs:如何在cc模式导数中自动插入空格后的空格

[英]Emacs: How to automatically insert space after colon in cc-mode derivatives

Suppose I have this code: 假设我有这个代码:

{
  "type"  : "home",
  "number":"212 555-1234"
}

I want my emacs to automatically insert space after colon in some modes. 我希望我的emacs在某些模式下自动在冒号后插入空格。 Particularly I'm using javascript-mode based on cc-mode. 特别是我正在使用基于cc模式的javascript模式。 Can it help? 它有帮助吗?

Thank in advance. 预先感谢。

The simplest way to do this would be something like this (in your .emacs): 最简单的方法就是这样(在你的.emacs中):

(defun my-js-hook ()
  (local-set-key ":" '(lambda () (interactive) (insert ": "))))

(add-hook 'js-mode-hook 'my-js-hook)

More sophisticated alternatives include yasnippet or skeleton mode . 更复杂的替代方案包括yasnippet骨架模式 They are probably overkill for something this simple, but are useful tools if you want more sophisticated templating. 对于这么简单的事情来说,它们可能有些过分,但如果你想要更复杂的模板,它们是很有用的工具。

EDIT: I'm not aware of any cc-mode magic that allows for different behaviour inside comments. 编辑:我不知道任何cc模式魔术允许在评论中的不同行为。 I don't use cc-mode much, but I don't see anything obvious in the manual. 我不太多使用cc模式,但我在手册中看不到任何明显的内容。 Here's a bit of code that may do what you want though: 这里有一些代码可以做你想要的但是:

(defun my-js-hook ()
  (local-set-key ":" 
             '(lambda () 
                (interactive)
                (let ((in-comment-p))
                  (save-excursion
                    (setq in-comment-p (comment-beginning)))
                  (if in-comment-p 
                      (insert ":")
                    (insert ": "))))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM