mgilson
mgilson

Reputation: 310167

comment mode in emacs

I commonly program in languages that don't have any sort of block/multiline comment (e.g. python [#] and fortran [c or !].). Is there any way to define a minor mode in emacs that would allow me to enter multiline comments? By that I mean, it would cause emacs to wrap text automatically after X lines (say 72) and automatically prepend a comment character (taken from the current major mode) to the beginning of each line?

Sorry if this is a pretty basic question -- my elisp skills are rudimentary at best.

Upvotes: 12

Views: 2229

Answers (2)

Andreas Röhler
Andreas Röhler

Reputation: 4804

M-x ;

comments/uncomments the region

Upvotes: 0

Nicolas Dudebout
Nicolas Dudebout

Reputation: 9262

You can use the following code:

(setq fill-column 72)
(setq comment-auto-fill-only-comments t)
(auto-fill-mode t)

This will wrap the text automatically, only for the comments, and will insert the comment character everytime it does a line break.

I have this set up only for programming modes as follows:

(defun my-prog-mode-hook
  (setq fill-column 72)
  (set (make-local-variable 'comment-auto-fill-only-comments) t)
  (auto-fill-mode t))
(add-hook 'prog-mode-hook 'my-prog-mode-hook)

This makes sure that if I turn auto-fill mode in a non programming mode such as AUCTeX all the text gets wrapped and not only the comments.

Upvotes: 17

Related Questions