In emacs, can I get a comment pane to use a block comment instead of commenting out each line?

I have an XML file; opens in nxml mode in emacs 23.2.

I would like to comment-region comment on the region as a block, rather than comment on each line in the region. In my opinion, this makes a more readable comment.

Before:

enter image description here

After the `comment-region ':

enter image description here

Required after:

enter image description here

In italic-sliding languages, such as JavaScript and Java, comments on the region of comments of each line, but I think this is normal, because it uses the comment prefix on one line, // , which keeps the readability of the next. For XML, I would like things to be different.


Edit

I just saw Trey's old answer regarding a similar question for c-mode: the Emacs comment area in C mode. Basically there was a new module called newcomment.el that defines a bunch of comment styles.

This looks promising, but it is not completely sorted with nxml-mode. For example, when I tried box-multi as a style, the commented section looked nice, but Cu comment-region did not change what was added.: / Similar to the box style. I will play with him a little more.


Edit # 2

Here is the code I used, 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 Cu 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-->")))))) 

Then in my nxml-mode-fn I did the following:

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

Actual behavior: enter image description here

Be careful: this is completely naive and does not try to "run away" from comments that start and stop comments in the region. If someone feels the desire to add this to the code above, I would appreciate it, but I will no longer worry about it.

+4
source share
2 answers

It could be something like:

 (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 " -->")))) 

associated it with any key in xml-mode hook ...

+1
source

You can get some mileage from something like

 (add-hook 'nxml-mode-hook (lambda () (set (make-local-variable 'comment-style) 'multi-line) (set (make-local-variable 'comment-continue) " "))) 
+1
source

Source: https://habr.com/ru/post/1485476/


All Articles