Ron
Ron

Reputation: 8146

lisp on emacs: how to comment a multi-line expression?

For example, if I want to comment this:

(defun noop ()
  nil)

Every time I try to put a semicolon before the "(defun", the defun runs away to the next line. So how is that supposed to be done?

GNU Emacs 23.1.1

Edit: by "running away" I mean when I insert a semicolon before "(defun", a newline is automatically inserted after the semicolon and "(defun" starts on a new line again.

Upvotes: 14

Views: 11467

Answers (8)

Mohsen Banan
Mohsen Banan

Reputation: 143

(defvar orgCmntEnd nil "Org Comment End")

(defun orgCmntBegin (<comment <commentEnd))

(orgCmntBegin "
**  orgCmntBegin. Permits us to include * at the beginning of line as a comment.
Which in turn allows us to switch between emacs-major mode and org-mode for COMEEGA.
Example usage is: 
(orgCmntBegin \"multi-line comment comes here.\" orgCmntEnd)

I wish elisp had a here-document facility. Like common-lisp.
Anybody listening?
" orgCmntEnd)

Upvotes: 0

Rainer Joswig
Rainer Joswig

Reputation: 139251

See the command M-x comment-region and related.

Upvotes: 16

Shlomi
Shlomi

Reputation: 4748

a little late to the party, however, what about:

(defmacro comment (&rest a))

Upvotes: 3

R&#246;rd
R&#246;rd

Reputation: 6681

For the specific task you asked for in the headline (commenting a complete expression that may span multiple lines at once), first press C-M-SPC (bound to mark-sexp) to set the region to the expression following point, and then M-; (bound to comment-dwim which will call comment-region).

Upvotes: 2

Matthias Benkard
Matthias Benkard

Reputation: 15759

If you're talking about Common Lisp (rather than, say, Emacs-Lisp), you can use #+(or):

#+(or)
(defun noop ()
  nil)

See the CLHS for details.

Upvotes: 1

Charlie Martin
Charlie Martin

Reputation: 112346

Ron, do a CTRL-H m and look at the minor modes. You have some "helpful" minor mode active. (Maybe paredit but I dont think that's it.) I remember there was something like that when I tried the EMACS Starter Kit. It lasted maybe thirty seconds before I screamed and found how to kill it.

In any case, that's not default EMACS behavior, it's some init-file or site-emacs addition.

Upvotes: 6

Sergei Lebedev
Sergei Lebedev

Reputation: 2679

M-X comment-dwim or M-;, which is the default key binding for the former — might save you a few key strokes, since it not only comments, but uncomments region, if it's commented already. Anyway, check out Emacs Manual for a proper description.

Upvotes: 8

Victor Deryagin
Victor Deryagin

Reputation: 12215

Mark both lines and call M-x comment-region. Also look at comment-or-uncomment-region and comment-dwim functions.

Upvotes: 5

Related Questions