在emacs中,我可以获得评论区域来使用块注释而不是评论每一行吗?

时间:2013-06-10 16:25:49

标签: xml emacs

我有一个XML文件;在emacs 23.2中以nxml模式打开。

希望comment-region将该区域评论为一个区块,而不是评论该区域中的每一条线。在我看来,这是一个更具可读性的评论部分。

在:

enter image description here

在“评论区域”之后:

enter image description here

渴望之后:

enter image description here

在JavaScript和Java之类的大括号语言中,注释区域注释每一行,但我发现这很好,因为它使用单行注释前缀//,这保留了后面的可读性。对于XML,我希望它与众不同。


修改

我刚刚看到Trey关于c模式的类似问题的旧答案:Emacs comment-region in C mode基本上有一个名为newcomment.el的新模块,它定义了一堆评论样式。

这看起来很有希望,但它没有用nxml-mode完全排序。例如,当我尝试box-multi作为样式时,评论的部分看起来不错,但C-u comment-region没有反转已添加的内容。 :/同样具有box样式。我会再捣蛋了。


编辑#2

以下是我使用的代码thanks to Alex Ott

(defun dino-xml-comment-region (beg end &optional arg)
  (interactive "*r\nP")
  (if (> beg end)
      (let (tmp) (setq tmp beg beg end end tmp)))
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (cond
       ;; is there a C-u prefix?
       ((and (listp arg) (> (length arg) 0))
        (and (re-search-forward "<!-- *[\n\r]" nil t)
             (goto-char (- (point-max) 1))
             (re-search-backward " *-->" nil t)
             (goto-char (point-min))
             (progn
               (re-search-forward "<!-- *[\n\r]" nil t)
               (replace-match "")
               (goto-char (- (point-max) 1))
               (re-search-backward "[\n\r] *-->" nil t)
               (replace-match ""))))

       (t
        (insert "<!--\n")
        (goto-char (- (point-max) 1))
        (unless (= 10 (following-char))
          (forward-char))
        (insert "\n-->"))))))

然后,在我的nxml-mode-fn中,我这样做了:

(local-set-key "\C-c\C-c"  'dino-xml-comment-region)

实际行为: enter image description here

但是要小心:这是完全天真的,并不试图“逃避”干预评论开始和停止在该地区内。如果有人觉得有必要将其添加到上面的代码中,我会很感激,但我不会再担心它了。

2 个答案:

答案 0 :(得分:1)

可能是这样的:

(defun xml-comment-region (beg end &optional arg)
  (interactive "*r\nP")
  (if (> beg end)
      (let (mid) (setq mid beg beg end end mid)))
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (insert "<!-- ")
      (goto-char (- (point-max) 1))
      (unless (= 10 (following-char))
    (forward-char))
      (insert " -->"))))

将它绑定到xml-mode hook中的任何键...

答案 1 :(得分:1)

您可能会从

之类的东西中获得一些好处
(add-hook 'nxml-mode-hook
          (lambda ()
            (set (make-local-variable 'comment-style) 'multi-line)
            (set (make-local-variable 'comment-continue) " ")))
相关问题