C Comment in Emacs - Linux Kernel Style v2

2019-07-12 00:21发布

问题:

I received an answer about C Comment in Emacs - Linux Kernel Style that works great but

when emacs comments (comment-dwim) it's padding the second * long_function_name_vari and last */ lines with spaces (before the comment) not tabs like I have configured it. How to avoid that?

And how easily to make a comment using this style?

    /* void main()
     * {
     *  int i;
     *  int b;
     *  printf("format string");
     * }
     */

回答1:

Customizable variable comment-style is defined in newcomment.el saying ...

(extra-line t   nil t   t
            "One comment for all lines, end on a line by itself")

...

which should deliver the wanted result IIUC.

Current implementation however is two-extra-lines, the opening newline undocumented so far. Please file a feature request resp. docu-bug report.



回答2:

Well, after seek the newcomment.el I could understand how to tweak that behavior.

(defun my-c-comment-dwim (tabify delete-trailing)      
  (interactive)
  (let (beg end)
    (if (region-active-p)
        (progn
          (setq beg (region-beginning)
                end (region-end))
          (if (comment-only-p beg end)
              (uncomment-region beg end)
            (progn
              (comment-region beg end)
              (when (equal comment-style 'extra-line)
                (save-excursion
                  (goto-char end)
                  (forward-line 2)
                  (setq end (line-end-position))))
              (when tabify
                (tabify beg end))
              (when delete-trailing
                (delete-trailing-whitespace beg end)))))
      (comment-indent))))

(global-set-key [remap comment-dwim] (lambda ()
                                       (interactive)
                                       (my-c-comment-dwim t t)))


标签: emacs elisp