(define-derived-mode): Properly ignore unknown args.
[bpt/emacs.git] / lisp / newcomment.el
CommitLineData
83b96b22
SM
1;;; newcomment.el --- (un)comment regions of buffers
2
39cb51e7 3;; Copyright (C) 1999, 2000 Free Software Foundation Inc.
83b96b22 4
59a1ce8d 5;; Author: code extracted from Emacs-20's simple.el
771c9b97 6;; Maintainer: Stefan Monnier <monnier@cs.yale.edu>
83b96b22 7;; Keywords: comment uncomment
83b96b22 8
59a1ce8d
SM
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
83b96b22 12;; it under the terms of the GNU General Public License as published by
59a1ce8d
SM
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
83b96b22
SM
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
59a1ce8d 20
83b96b22 21;; You should have received a copy of the GNU General Public License
59a1ce8d
SM
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
83b96b22
SM
25
26;;; Commentary:
27
771c9b97 28;; A replacement for simple.el's comment-related functions.
83b96b22
SM
29
30;;; Bugs:
31
3674a4a9
SM
32;; - boxed comments in Perl are not properly uncommented because they are
33;; uncommented one-line at a time.
75e95a97 34;; - nested comments in sgml-mode are not properly quoted.
2ab98065
SM
35;; - single-char nestable comment-start can only do the "\\s<+" stuff
36;; if the corresponding closing marker happens to be right.
3e569d22
SM
37;; - uncomment-region with a numeric argument can render multichar
38;; comment markers invalid.
3fc5b4e2
SM
39;; - comment-indent or comment-region when called inside a comment
40;; will happily break the surrounding comment.
41;; - comment-quote-nested will not (un)quote properly all nested comment
42;; markers if there are more than just comment-start and comment-end.
43;; For example, in Pascal where {...*) and (*...} are possible.
83b96b22
SM
44
45;;; Todo:
46
3674a4a9
SM
47;; - rebox.el-style refill.
48;; - quantized steps in comment-alignment.
49;; - try to align tail comments.
50;; - check what c-comment-line-break-function has to say.
51;; - spill auto-fill of comments onto the end of the next line.
7a0a180a 52;; - uncomment-region with a consp (for blocks) or somehow make the
3674a4a9
SM
53;; deletion of continuation markers less dangerous.
54;; - drop block-comment-<foo> unless it's really used.
55;; - uncomment-region on a subpart of a comment.
56;; - support gnu-style "multi-line with space in continue".
771c9b97
SM
57;; - somehow allow comment-dwim to use the region even if transient-mark-mode
58;; is not turned on.
83b96b22 59
bdbe3a89
SM
60;; - when auto-filling a comment, try to move the comment to the left
61;; rather than break it (if possible).
62;; - sometimes default the comment-column to the same
63;; one used on the preceding line(s).
64
83b96b22
SM
65;;; Code:
66
be83ecb2 67;;;###autoload
59a1ce8d 68(defalias 'indent-for-comment 'comment-indent)
be83ecb2 69;;;###autoload
59a1ce8d 70(defalias 'set-comment-column 'comment-set-column)
be83ecb2 71;;;###autoload
59a1ce8d 72(defalias 'kill-comment 'comment-kill)
be83ecb2 73;;;###autoload
59a1ce8d 74(defalias 'indent-new-comment-line 'comment-indent-new-line)
83b96b22 75
be83ecb2 76;;;###autoload
3e569d22
SM
77(defgroup comment nil
78 "Indenting and filling of comments."
79 :prefix "comment-"
ac87b3a9 80 :version "21.1"
3e569d22
SM
81 :group 'fill)
82
771c9b97
SM
83(defvar comment-use-syntax 'undecided
84 "Non-nil if syntax-tables can be used instead of regexps.
85Can also be `undecided' which means that a somewhat expensive test will
86be used to try to determine whether syntax-tables should be trusted
f5215400
SM
87to understand comments or not in the given buffer.
88Major modes should set this variable.")
2ab98065 89
88fe06af
SM
90(defcustom comment-fill-column nil
91 "Column to use for `comment-indent'. If nil, use `fill-column' instead."
41dfdd71 92 :type '(choice (const nil) integer))
88fe06af 93
be83ecb2 94;;;###autoload
83b96b22
SM
95(defcustom comment-column 32
96 "*Column to indent right-margin comments to.
83b96b22 97Each mode establishes a different default value for this variable; you
a704fec0
SM
98can set the value for a particular mode using that mode's hook.
99Comments might be indented to a value smaller than this in order
88fe06af 100not to go beyond `comment-fill-column'."
0603917d 101 :type 'integer)
83b96b22
SM
102(make-variable-buffer-local 'comment-column)
103
be83ecb2 104;;;###autoload
f5215400
SM
105(defvar comment-start nil
106 "*String to insert to start a new comment, or nil if no comment syntax.")
83b96b22 107
be83ecb2 108;;;###autoload
f5215400 109(defvar comment-start-skip nil
83b96b22
SM
110 "*Regexp to match the start of a comment plus everything up to its body.
111If there are any \\(...\\) pairs, the comment delimiter text is held to begin
f5215400 112at the place matched by the close of the first pair.")
771c9b97 113
be83ecb2 114;;;###autoload
3e569d22
SM
115(defvar comment-end-skip nil
116 "Regexp to match the end of a comment plus everything up to its body.")
83b96b22 117
be83ecb2 118;;;###autoload
f5215400 119(defvar comment-end ""
83b96b22 120 "*String to insert to end a new comment.
f5215400 121Should be an empty string if comments are terminated by end-of-line.")
83b96b22 122
be83ecb2 123;;;###autoload
87cf8421 124(defvar comment-indent-function 'comment-indent-default
83b96b22
SM
125 "Function to compute desired indentation for a comment.
126This function is called with no args with point at the beginning of
87cf8421
SM
127the comment's starting delimiter and should return either the desired
128column indentation or nil.
129If nil is returned, indentation is delegated to `indent-according-to-mode'.")
83b96b22 130
3e569d22
SM
131(defvar block-comment-start nil)
132(defvar block-comment-end nil)
83b96b22 133
771c9b97
SM
134(defvar comment-quote-nested t
135 "Non-nil if nested comments should be quoted.
136This should be locally set by each major mode if needed.")
137
138(defvar comment-continue nil
f5215400
SM
139 "Continuation string to insert for multiline comments.
140This string will be added at the beginning of each line except the very
141first one when commenting a region with a commenting style that allows
142comments to span several lines.
143It should generally have the same length as `comment-start' in order to
144preserve indentation.
145If it is nil a value will be automatically derived from `comment-start'
146by replacing its first character with a space.")
147
771c9b97 148(defvar comment-add 0
f5215400
SM
149 "How many more comment chars should be inserted by `comment-region'.
150This determines the default value of the numeric argument of `comment-region'.
151This should generally stay 0, except for a few modes like Lisp where
152it can be convenient to set it to 1 so that regions are commented with
153two semi-colons.")
2ab98065 154
2ab98065 155(defconst comment-styles
771c9b97
SM
156 '((plain . (nil nil nil nil))
157 (indent . (nil nil nil t))
158 (aligned . (nil t nil t))
159 (multi-line . (t nil nil t))
160 (extra-line . (t nil t t))
9b0d1d6e
SM
161 (box . (nil t t t))
162 (box-multi . (t t t t)))
f5215400
SM
163 "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
164STYLE should be a mnemonic symbol.
165MULTI specifies that comments are allowed to span multiple lines.
166ALIGN specifies that the `comment-end' markers should be aligned.
167EXTRA specifies that an extra line should be used before and after the
168 region to comment (to put the `comment-end' and `comment-start').
169INDENT specifies that the `comment-start' markers should not be put at the
170 left margin but at the current indentation of the region to comment.")
171
be83ecb2 172;;;###autoload
f5215400
SM
173(defcustom comment-style 'plain
174 "*Style to be used for `comment-region'.
175See `comment-styles' for a list of available styles."
be83ecb2
SM
176 :type (if (boundp 'comment-styles)
177 `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles))
178 'symbol))
2ab98065 179
be83ecb2
SM
180;;;###autoload
181(defcustom comment-padding " "
f5215400
SM
182 "Padding string that `comment-region' puts between comment chars and text.
183Can also be an integer which will be automatically turned into a string
184of the corresponding number of spaces.
2ab98065
SM
185
186Extra spacing between the comment characters and the comment text
0603917d
SM
187makes the comment easier to read. Default is 1. nil means 0."
188 :type '(choice string integer (const nil)))
2ab98065 189
be83ecb2 190;;;###autoload
3e569d22 191(defcustom comment-multi-line nil
ac87b3a9 192 "*Non-nil means \\[comment-indent-new-line] continues comments, with no new terminator or starter.
3e569d22 193This is obsolete because you might as well use \\[newline-and-indent]."
0603917d 194 :type 'boolean)
3e569d22 195
2ab98065
SM
196;;;;
197;;;; Helpers
198;;;;
199
f5215400
SM
200(defun comment-string-strip (str beforep afterp)
201 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
202 (string-match (concat "\\`" (if beforep "\\s-*")
ad679e45 203 "\\(.*?\\)" (if afterp "\\s-*\n?")
2ab98065
SM
204 "\\'") str)
205 (match-string 1 str))
206
207(defun comment-string-reverse (s)
f5215400 208 "Return the mirror image of string S, without any trailing space."
771c9b97 209 (comment-string-strip (concat (nreverse (string-to-list s))) nil t))
2ab98065 210
7164ef13 211;;;###autoload
2ab98065
SM
212(defun comment-normalize-vars (&optional noerror)
213 (if (not comment-start) (or noerror (error "No comment syntax is defined"))
214 ;; comment-use-syntax
771c9b97 215 (when (eq comment-use-syntax 'undecided)
2ab98065
SM
216 (set (make-local-variable 'comment-use-syntax)
217 (let ((st (syntax-table))
218 (cs comment-start)
219 (ce (if (string= "" comment-end) "\n" comment-end)))
771c9b97
SM
220 ;; Try to skip over a comment using forward-comment
221 ;; to see if the syntax tables properly recognize it.
2ab98065
SM
222 (with-temp-buffer
223 (set-syntax-table st)
224 (insert cs " hello " ce)
225 (goto-char (point-min))
226 (and (forward-comment 1) (eobp))))))
2ab98065 227 ;; comment-padding
9145f1c2 228 (unless comment-padding (setq comment-padding 0))
2ab98065
SM
229 (when (integerp comment-padding)
230 (setq comment-padding (make-string comment-padding ? )))
231 ;; comment markers
232 ;;(setq comment-start (comment-string-strip comment-start t nil))
233 ;;(setq comment-end (comment-string-strip comment-end nil t))
234 ;; comment-continue
f5215400 235 (unless (or comment-continue (string= comment-end ""))
2ab98065 236 (set (make-local-variable 'comment-continue)
9b0d1d6e 237 (concat (if (string-match "\\S-\\S-" comment-start) " " "|")
7164ef13
SM
238 (substring comment-start 1)))
239 ;; Hasn't been necessary yet.
240 ;; (unless (string-match comment-start-skip comment-continue)
241 ;; (kill-local-variable 'comment-continue))
242 )
2ab98065 243 ;; comment-skip regexps
f23a0f3a
SM
244 (unless (and comment-start-skip
245 ;; In case comment-start has changed since last time.
246 (string-match comment-start-skip comment-start))
2ab98065
SM
247 (set (make-local-variable 'comment-start-skip)
248 (concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
249 (regexp-quote (comment-string-strip comment-start t t))
75ed43a6
SM
250 ;; Let's not allow any \s- but only [ \t] since \n
251 ;; might be both a comment-end marker and \s-.
252 "+\\)[ \t]*")))
f23a0f3a
SM
253 (unless (and comment-end-skip
254 ;; In case comment-end has changed since last time.
255 (string-match comment-end-skip comment-end))
2ab98065
SM
256 (let ((ce (if (string= "" comment-end) "\n"
257 (comment-string-strip comment-end t t))))
258 (set (make-local-variable 'comment-end-skip)
37dbd369
SM
259 ;; We use [ \t] rather than \s- because we don't want to
260 ;; remove ^L in C mode when uncommenting.
261 (concat "[ \t]*\\(\\s>" (if comment-quote-nested "" "+")
2ab98065 262 "\\|" (regexp-quote (substring ce 0 1))
771c9b97 263 (if (and comment-quote-nested (<= (length ce) 1)) "" "+")
2ab98065
SM
264 (regexp-quote (substring ce 1))
265 "\\)"))))))
266
f5215400
SM
267(defun comment-quote-re (str unp)
268 (concat (regexp-quote (substring str 0 1))
269 "\\\\" (if unp "+" "*")
270 (regexp-quote (substring str 1))))
271
272(defun comment-quote-nested (cs ce unp)
273 "Quote or unquote nested comments.
274If UNP is non-nil, unquote nested comment markers."
275 (setq cs (comment-string-strip cs t t))
276 (setq ce (comment-string-strip ce t t))
277 (when (and comment-quote-nested (> (length ce) 0))
278 (let ((re (concat (comment-quote-re ce unp)
279 "\\|" (comment-quote-re cs unp))))
280 (goto-char (point-min))
281 (while (re-search-forward re nil t)
282 (goto-char (match-beginning 0))
283 (forward-char 1)
284 (if unp (delete-char 1) (insert "\\"))
285 (when (= (length ce) 1)
286 ;; If the comment-end is a single char, adding a \ after that
287 ;; "first" char won't deactivate it, so we turn such a CE
288 ;; into !CS. I.e. for pascal, we turn } into !{
289 (if (not unp)
290 (when (string= (match-string 0) ce)
291 (replace-match (concat "!" cs) t t))
292 (when (and (< (point-min) (match-beginning 0))
293 (string= (buffer-substring (1- (match-beginning 0))
294 (1- (match-end 0)))
295 (concat "!" cs)))
296 (backward-char 2)
297 (delete-char (- (match-end 0) (match-beginning 0)))
298 (insert ce))))))))
2ab98065
SM
299
300;;;;
301;;;; Navigation
302;;;;
303
ad679e45 304(defun comment-search-forward (limit &optional noerror)
771c9b97
SM
305 "Find a comment start between point and LIMIT.
306Moves point to inside the comment and returns the position of the
307comment-starter. If no comment is found, moves point to LIMIT
e8fe7d39 308and raises an error or returns nil of NOERROR is non-nil."
2ab98065 309 (if (not comment-use-syntax)
9b0d1d6e
SM
310 (if (re-search-forward comment-start-skip limit noerror)
311 (or (match-end 1) (match-beginning 0))
312 (goto-char limit)
313 (unless noerror (error "No comment")))
ad679e45
SM
314 (let* ((pt (point))
315 ;; Assume (at first) that pt is outside of any string.
316 (s (parse-partial-sexp pt (or limit (point-max)) nil nil nil t)))
317 (when (and (nth 8 s) (nth 3 s))
318 ;; The search ended inside a string. Try to see if it
319 ;; works better when we assume that pt is inside a string.
320 (setq s (parse-partial-sexp
321 pt (or limit (point-max)) nil nil
322 (list nil nil nil (nth 3 s) nil nil nil nil)
323 t)))
324 (if (not (and (nth 8 s) (not (nth 3 s))))
325 (unless noerror (error "No comment"))
326 ;; We found the comment.
9b0d1d6e 327 (let ((pos (point))
ad679e45 328 (start (nth 8 s))
9b0d1d6e 329 (bol (line-beginning-position))
ad679e45
SM
330 (end nil))
331 (while (and (null end) (>= (point) bol))
332 (if (looking-at comment-start-skip)
333 (setq end (min (or limit (point-max)) (match-end 0)))
334 (backward-char)))
9b0d1d6e 335 (goto-char (or end pos))
ad679e45 336 start)))))
2ab98065
SM
337
338(defun comment-search-backward (&optional limit noerror)
339 "Find a comment start between LIMIT and point.
771c9b97
SM
340Moves point to inside the comment and returns the position of the
341comment-starter. If no comment is found, moves point to LIMIT
2ab98065 342and raises an error or returns nil of NOERROR is non-nil."
ad679e45
SM
343 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
344 ;; stop there. This can be rather bad in general, but since
345 ;; comment-search-backward is only used to find the comment-column (in
346 ;; comment-set-column) and to find the comment-start string (via
347 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
2ab98065
SM
348 (if (not (re-search-backward comment-start-skip limit t))
349 (unless noerror (error "No comment"))
350 (beginning-of-line)
351 (let* ((end (match-end 0))
352 (cs (comment-search-forward end t))
353 (pt (point)))
354 (if (not cs)
355 (progn (beginning-of-line)
356 (comment-search-backward limit noerror))
357 (while (progn (goto-char cs)
358 (comment-forward)
359 (and (< (point) end)
360 (setq cs (comment-search-forward end t))))
361 (setq pt (point)))
362 (goto-char pt)
363 cs))))
364
365(defun comment-beginning ()
771c9b97
SM
366 "Find the beginning of the enclosing comment.
367Returns nil if not inside a comment, else moves point and returns
2ab98065 368the same as `comment-search-forward'."
75ed43a6
SM
369 ;; HACK ATTACK!
370 ;; We should really test `in-string-p' but that can be expensive.
371 (unless (eq (get-text-property (point) 'face) 'font-lock-string-face)
372 (let ((pt (point))
373 (cs (comment-search-backward nil t)))
374 (when cs
375 (if (save-excursion
376 (goto-char cs)
dde6824c
SM
377 (and
378 ;; For modes where comment-start and comment-end are the same,
379 ;; the search above may have found a `ce' rather than a `cs'.
380 (or (not (looking-at comment-end-skip))
381 ;; Maybe font-lock knows that it's a `cs'?
382 (eq (get-text-property (match-end 0) 'face)
383 'font-lock-comment-face)
384 (unless (eq (get-text-property (point) 'face)
385 'font-lock-comment-face)
386 ;; Let's assume it's a `cs' if we're on the same line.
387 (>= (line-end-position) pt)))
388 ;; Make sure that PT is not past the end of the comment.
389 (if (comment-forward 1) (> (point) pt) (eobp))))
75ed43a6
SM
390 cs
391 (goto-char pt)
392 nil)))))
2ab98065
SM
393
394(defun comment-forward (&optional n)
395 "Skip forward over N comments.
396Just like `forward-comment' but only for positive N
397and can use regexps instead of syntax."
398 (setq n (or n 1))
399 (if (< n 0) (error "No comment-backward")
400 (if comment-use-syntax (forward-comment n)
401 (while (> n 0)
59a1ce8d 402 (setq n
0f38a885
SM
403 (if (or (forward-comment 1)
404 (and (looking-at comment-start-skip)
405 (goto-char (match-end 0))
406 (re-search-forward comment-end-skip nil 'move)))
59a1ce8d 407 (1- n) -1)))
2ab98065
SM
408 (= n 0))))
409
410(defun comment-enter-backward ()
411 "Move from the end of a comment to the end of its content.
771c9b97 412Point is assumed to be just at the end of a comment."
2ab98065
SM
413 (if (bolp)
414 ;; comment-end = ""
415 (progn (backward-char) (skip-syntax-backward " "))
416 (let ((end (point)))
417 (beginning-of-line)
418 (save-restriction
419 (narrow-to-region (point) end)
ad679e45
SM
420 (if (re-search-forward (concat comment-end-skip "\\'") nil t)
421 (goto-char (match-beginning 0))
422 ;; comment-end-skip not found probably because it was not set right.
423 ;; Since \\s> should catch the single-char case, we'll blindly
424 ;; assume we're at the end of a two-char comment-end.
425 (goto-char (point-max))
426 (backward-char 2)
427 (skip-chars-backward (string (char-after)))
428 (skip-syntax-backward " "))))))
2ab98065
SM
429
430;;;;
431;;;; Commands
432;;;;
433
fae99944 434;;;###autoload
87cf8421
SM
435(defun comment-indent-default ()
436 "Default for `comment-indent-function'."
d3fcda22
SM
437 (if (and (looking-at "\\s<\\s<\\(\\s<\\)?")
438 (or (match-end 1) (/= (current-column) (current-indentation))))
439 0
87cf8421
SM
440 (when (or (/= (current-column) (current-indentation))
441 (and (> comment-add 0) (looking-at "\\s<\\S<")))
442 comment-column)))
443
be83ecb2 444;;;###autoload
3e569d22 445(defun comment-indent (&optional continue)
2ab98065 446 "Indent this line's comment to comment column, or insert an empty comment.
dde6824c 447If CONTINUE is non-nil, use the `comment-continue' markers if any."
83b96b22 448 (interactive "*")
ad679e45 449 (comment-normalize-vars)
83b96b22
SM
450 (let* ((empty (save-excursion (beginning-of-line)
451 (looking-at "[ \t]*$")))
f5215400 452 (starter (or (and continue comment-continue)
2ab98065 453 (and empty block-comment-start) comment-start))
f5215400 454 (ender (or (and continue comment-continue "")
2ab98065 455 (and empty block-comment-end) comment-end)))
2e1fbba4
SM
456 (unless starter (error "No comment syntax defined"))
457 (beginning-of-line)
458 (let* ((eolpos (line-end-position))
459 (begpos (comment-search-forward eolpos t))
460 cpos indent)
461 ;; An existing comment?
7a06b250
SM
462 (if begpos
463 (progn
464 (if (and (not (looking-at "[\t\n ]"))
465 (looking-at comment-end-skip))
466 ;; The comment is empty and we have skipped all its space
467 ;; and landed right before the comment-ender:
468 ;; Go back to the middle of the space.
469 (forward-char (/ (skip-chars-backward " \t") -2)))
470 (setq cpos (point-marker)))
2e1fbba4
SM
471 ;; If none, insert one.
472 (save-excursion
473 ;; Some comment-indent-function insist on not moving comments that
567e961e
SM
474 ;; are in column 0, so we first go to the likely target column.
475 (indent-to comment-column)
2e1fbba4 476 (setq begpos (point))
bb304a7a 477 (insert starter)
2e1fbba4
SM
478 (setq cpos (point-marker))
479 (insert ender)))
480 (goto-char begpos)
481 ;; Compute desired indent.
053b8d35 482 (setq indent (save-excursion (funcall comment-indent-function)))
2e1fbba4 483 (if (not indent)
a764440a 484 ;; comment-indent-function refuses: delegate to indent.
2e1fbba4
SM
485 (indent-according-to-mode)
486 ;; Avoid moving comments past the fill-column.
a764440a
SM
487 (unless (save-excursion (skip-chars-backward " \t") (bolp))
488 (setq indent
489 (min indent
490 (+ (current-column)
88fe06af 491 (- (or comment-fill-column fill-column)
a764440a 492 (save-excursion (end-of-line) (current-column)))))))
7164ef13 493 (unless (= (current-column) indent)
2e1fbba4 494 ;; If that's different from current, change it.
7164ef13 495 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
2e1fbba4
SM
496 (indent-to (if (bolp) indent
497 (max indent (1+ (current-column)))))))
498 (goto-char cpos)
499 (set-marker cpos nil))))
83b96b22 500
be83ecb2 501;;;###autoload
3e569d22 502(defun comment-set-column (arg)
83b96b22 503 "Set the comment column based on point.
2ab98065 504With no ARG, set the comment column to the current column.
83b96b22
SM
505With just minus as arg, kill any comment on this line.
506With any other arg, set comment column to indentation of the previous comment
507 and then align or create a comment on this line at that column."
508 (interactive "P")
e8fe7d39 509 (cond
ad679e45 510 ((eq arg '-) (comment-kill nil))
e8fe7d39
SM
511 (arg
512 (save-excursion
513 (beginning-of-line)
2ab98065 514 (comment-search-backward)
e8fe7d39 515 (beginning-of-line)
ad679e45 516 (goto-char (comment-search-forward (line-end-position)))
83b96b22 517 (setq comment-column (current-column))
e8fe7d39 518 (message "Comment column set to %d" comment-column))
ad679e45 519 (comment-indent))
e8fe7d39 520 (t (setq comment-column (current-column))
83b96b22
SM
521 (message "Comment column set to %d" comment-column))))
522
be83ecb2 523;;;###autoload
3e569d22 524(defun comment-kill (arg)
7a0a180a
SM
525 "Kill the comment on this line, if any.
526With prefix ARG, kill comments on that many lines starting with this one."
527 (interactive "P")
ad679e45
SM
528 (dotimes (_ (prefix-numeric-value arg))
529 (save-excursion
530 (beginning-of-line)
531 (let ((cs (comment-search-forward (line-end-position) t)))
532 (when cs
533 (goto-char cs)
534 (skip-syntax-backward " ")
535 (setq cs (point))
536 (comment-forward)
537 (kill-region cs (if (bolp) (1- (point)) (point)))
538 (indent-according-to-mode))))
539 (if arg (forward-line 1))))
7a0a180a 540
2ab98065
SM
541(defun comment-padright (str &optional n)
542 "Construct a string composed of STR plus `comment-padding'.
f5215400 543It also adds N copies of the last non-whitespace chars of STR.
2ab98065 544If STR already contains padding, the corresponding amount is
f5215400
SM
545ignored from `comment-padding'.
546N defaults to 0.
771c9b97 547If N is `re', a regexp is returned instead, that would match
f5215400 548the string for any N."
2ab98065
SM
549 (setq n (or n 0))
550 (when (and (stringp str) (not (string= "" str)))
f5215400 551 ;; Separate the actual string from any leading/trailing padding
3e569d22 552 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
f5215400
SM
553 (let ((s (match-string 1 str)) ;actual string
554 (lpad (substring str 0 (match-beginning 1))) ;left padding
555 (rpad (concat (substring str (match-end 1)) ;original right padding
556 (substring comment-padding ;additional right padding
3e569d22 557 (min (- (match-end 0) (match-end 1))
9b0d1d6e
SM
558 (length comment-padding)))))
559 ;; We can only duplicate C if the comment-end has multiple chars
560 ;; or if comments can be nested, else the comment-end `}' would
561 ;; be turned into `}}}' where only the first ends the comment
562 ;; and the rest becomes bogus junk.
563 (multi (not (and comment-quote-nested
564 ;; comment-end is a single char
565 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
f5215400 566 (if (not (symbolp n))
9b0d1d6e 567 (concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
f5215400
SM
568 ;; construct a regexp that would match anything from just S
569 ;; to any possible output of this function for any N.
570 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
571 lpad "") ;padding is not required
9b0d1d6e
SM
572 (regexp-quote s)
573 (when multi "+") ;the last char of S might be repeated
f5215400
SM
574 (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
575 rpad "")))))) ;padding is not required
2ab98065
SM
576
577(defun comment-padleft (str &optional n)
578 "Construct a string composed of `comment-padding' plus STR.
f5215400 579It also adds N copies of the first non-whitespace chars of STR.
2ab98065 580If STR already contains padding, the corresponding amount is
f5215400
SM
581ignored from `comment-padding'.
582N defaults to 0.
771c9b97 583If N is `re', a regexp is returned instead, that would match
2ab98065
SM
584 the string for any N."
585 (setq n (or n 0))
586 (when (and (stringp str) (not (string= "" str)))
f5215400 587 ;; Only separate the left pad because we assume there is no right pad.
2ab98065
SM
588 (string-match "\\`\\s-*" str)
589 (let ((s (substring str (match-end 0)))
590 (pad (concat (substring comment-padding
591 (min (- (match-end 0) (match-beginning 0))
592 (length comment-padding)))
593 (match-string 0 str)))
f5215400
SM
594 (c (aref str (match-end 0))) ;the first non-space char of STR
595 ;; We can only duplicate C if the comment-end has multiple chars
596 ;; or if comments can be nested, else the comment-end `}' would
597 ;; be turned into `}}}' where only the first ends the comment
598 ;; and the rest becomes bogus junk.
599 (multi (not (and comment-quote-nested
600 ;; comment-end is a single char
601 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
602 (if (not (symbolp n))
603 (concat pad (when multi (make-string n c)) s)
604 ;; Construct a regexp that would match anything from just S
605 ;; to any possible output of this function for any N.
606 ;; We match any number of leading spaces because this regexp will
607 ;; be used for uncommenting where we might want to remove
608 ;; uncomment markers with arbitrary leading space (because
609 ;; they were aligned).
610 (concat "\\s-*"
611 (if multi (concat (regexp-quote (string c)) "*"))
612 (regexp-quote s))))))
83b96b22 613
be83ecb2 614;;;###autoload
7a0a180a 615(defun uncomment-region (beg end &optional arg)
eb0b51f8 616 "Uncomment each line in the BEG .. END region.
f5215400
SM
617The numeric prefix ARG can specify a number of chars to remove from the
618comment markers."
83b96b22
SM
619 (interactive "*r\nP")
620 (comment-normalize-vars)
621 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
622 (save-excursion
7a0a180a 623 (goto-char beg)
f5215400 624 (setq end (copy-marker end))
4679af47
JB
625 (let* ((numarg (prefix-numeric-value arg))
626 (ccs comment-continue)
627 (srei (comment-padright ccs 're))
628 (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
629 spt)
7a0a180a 630 (while (and (< (point) end)
2ab98065 631 (setq spt (comment-search-forward end t)))
4679af47
JB
632 (let ((ipt (point))
633 ;; Find the end of the comment.
634 (ept (progn
635 (goto-char spt)
636 (unless (comment-forward)
637 (error "Can't find the comment end"))
638 (point)))
639 (box nil)
640 (box-equal nil)) ;Whether we might be using `=' for boxes.
e8fe7d39
SM
641 (save-restriction
642 (narrow-to-region spt ept)
eb0b51f8 643
f5215400 644 ;; Remove the comment-start.
2ab98065
SM
645 (goto-char ipt)
646 (skip-syntax-backward " ")
f5215400 647 ;; A box-comment starts with a looong comment-start marker.
eb0b51f8
SM
648 (when (and (or (and (= (- (point) (point-min)) 1)
649 (setq box-equal t)
650 (looking-at "=\\{7\\}")
651 (not (eq (char-before (point-max)) ?\n))
652 (skip-chars-forward "="))
653 (> (- (point) (point-min) (length comment-start)) 7))
654 (> (count-lines (point-min) (point-max)) 2))
f5215400 655 (setq box t))
2ab98065
SM
656 (when (looking-at (regexp-quote comment-padding))
657 (goto-char (match-end 0)))
658 (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
659 (goto-char (match-end 0)))
3e569d22
SM
660 (if (null arg) (delete-region (point-min) (point))
661 (skip-syntax-backward " ")
662 (delete-char (- numarg)))
2ab98065 663
f5215400 664 ;; Remove the end-comment (and leading padding and such).
2ab98065 665 (goto-char (point-max)) (comment-enter-backward)
9b0d1d6e 666 ;; Check for special `=' used sometimes in comment-box.
eb0b51f8 667 (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
6dcd3806
SM
668 (let ((pos (point)))
669 ;; skip `=' but only if there are at least 7.
670 (when (> (skip-chars-backward "=") -7) (goto-char pos))))
9b0d1d6e 671 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
2ab98065 672 (when (and (bolp) (not (bobp))) (backward-char))
f5215400 673 (if (null arg) (delete-region (point) (point-max))
3e569d22
SM
674 (skip-syntax-forward " ")
675 (delete-char numarg)))
e8fe7d39 676
f5215400
SM
677 ;; Unquote any nested end-comment.
678 (comment-quote-nested comment-start comment-end t)
679
680 ;; Eliminate continuation markers as well.
681 (when sre
682 (let* ((cce (comment-string-reverse (or comment-continue
683 comment-start)))
684 (erei (and box (comment-padleft cce 're)))
685 (ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
7a0a180a 686 (goto-char (point-min))
f5215400
SM
687 (while (progn
688 (if (and ere (re-search-forward
689 ere (line-end-position) t))
690 (replace-match "" t t nil (if (match-end 2) 2 1))
691 (setq ere nil))
692 (forward-line 1)
693 (re-search-forward sre (line-end-position) t))
2ab98065 694 (replace-match "" t t nil (if (match-end 2) 2 1)))))
110c171f 695 ;; Go to the end for the next comment.
f5215400
SM
696 (goto-char (point-max)))))
697 (set-marker end nil))))
83b96b22 698
aac88001 699(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
ad679e45
SM
700 "Make the leading and trailing extra lines.
701This is used for `extra-line' style (or `box' style if BLOCK is specified)."
9b0d1d6e
SM
702 (let ((eindent 0))
703 (if (not block)
704 ;; Try to match CS and CE's content so they align aesthetically.
705 (progn
706 (setq ce (comment-string-strip ce t t))
707 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce "\n" cs))
708 (setq eindent
709 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
710 0))))
711 ;; box comment
712 (let* ((width (- max-indent min-indent))
713 (s (concat cs "a=m" cce))
714 (e (concat ccs "a=m" ce))
715 (c (if (string-match ".*\\S-\\S-" cs)
3674a4a9
SM
716 (aref cs (1- (match-end 0)))
717 (if (and (equal comment-end "") (string-match ".*\\S-" cs))
718 (aref cs (1- (match-end 0))) ?=)))
719 (re "\\s-*a=m\\s-*")
720 (_ (string-match re s))
721 (lcs (length cs))
9b0d1d6e
SM
722 (fill
723 (make-string (+ width (- (match-end 0)
3674a4a9 724 (match-beginning 0) lcs 3)) c)))
aac88001 725 (setq cs (replace-match fill t t s))
3674a4a9
SM
726 (when (and (not (string-match comment-start-skip cs))
727 (string-match "a=m" s))
728 ;; The whitespace around CS cannot be ignored: put it back.
729 (setq re "a=m")
730 (setq fill (make-string (- width lcs) c))
731 (setq cs (replace-match fill t t s)))
732 (string-match re e)
9b0d1d6e
SM
733 (setq ce (replace-match fill t t e))))
734 (cons (concat cs "\n" (make-string min-indent ? ) ccs)
735 (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce))))
aac88001
SM
736
737(def-edebug-spec comment-with-narrowing t)
738(put 'comment-with-narrowing 'lisp-indent-function 2)
739(defmacro comment-with-narrowing (beg end &rest body)
740 "Execute BODY with BEG..END narrowing.
741Space is added (and then removed) at the beginning for the text's
742indentation to be kept as it was before narrowing."
59a1ce8d
SM
743 (let ((bindent (make-symbol "bindent")))
744 `(let ((,bindent (save-excursion (goto-char beg) (current-column))))
745 (save-restriction
746 (narrow-to-region beg end)
747 (goto-char (point-min))
748 (insert (make-string ,bindent ? ))
749 (prog1
750 (progn ,@body)
751 ;; remove the bindent
752 (save-excursion
753 (goto-char (point-min))
754 (when (looking-at " *")
755 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent)))
aac88001 756 (delete-char n)
59a1ce8d
SM
757 (setq ,bindent (- ,bindent n))))
758 (end-of-line)
759 (let ((e (point)))
760 (beginning-of-line)
761 (while (and (> ,bindent 0) (re-search-forward " *" e t))
762 (let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1))))
763 (goto-char (match-beginning 0))
764 (delete-char n)
765 (setq ,bindent (- ,bindent n)))))))))))
aac88001 766
771c9b97
SM
767(defun comment-region-internal (beg end cs ce
768 &optional ccs cce block lines indent)
3674a4a9 769 "Comment region BEG .. END.
4125ec7e
SM
770CS and CE are the comment start resp end string.
771CCS and CCE are the comment continuation strings for the start resp end
f5215400
SM
772of lines (default to CS and CE).
773BLOCK indicates that end of lines should be marked with either CCE, CE or CS
774\(if CE is empty) and that those markers should be aligned.
775LINES indicates that an extra lines will be used at the beginning and end
776of the region for CE and CS.
777INDENT indicates to put CS and CCS at the current indentation of the region
778rather than at left margin."
59a1ce8d 779 ;;(assert (< beg end))
83b96b22 780 (let ((no-empty t))
f5215400 781 ;; Sanitize CE and CCE.
83b96b22
SM
782 (if (and (stringp ce) (string= "" ce)) (setq ce nil))
783 (if (and (stringp cce) (string= "" cce)) (setq cce nil))
f5215400
SM
784 ;; If CE is empty, multiline cannot be used.
785 (unless ce (setq ccs nil cce nil))
786 ;; Should we mark empty lines as well ?
83b96b22 787 (if (or ccs block lines) (setq no-empty nil))
f5215400 788 ;; Make sure we have end-markers for BLOCK mode.
2ab98065 789 (when block (unless ce (setq ce (comment-string-reverse cs))))
f5215400
SM
790 ;; If BLOCK is not requested, we don't need CCE.
791 (unless block (setq cce nil))
792 ;; Continuation defaults to the same as CS and CE.
793 (unless ccs (setq ccs cs cce ce))
7a0a180a 794
83b96b22 795 (save-excursion
aac88001 796 (goto-char end)
f5215400
SM
797 ;; If the end is not at the end of a line and the comment-end
798 ;; is implicit (i.e. a newline), explicitly insert a newline.
aac88001
SM
799 (unless (or ce (eolp)) (insert "\n") (indent-according-to-mode))
800 (comment-with-narrowing beg end
f5215400 801 (let ((min-indent (point-max))
9d5240d2 802 (max-indent 0))
83b96b22 803 (goto-char (point-min))
f5215400
SM
804 ;; Quote any nested comment marker
805 (comment-quote-nested comment-start comment-end nil)
806
807 ;; Loop over all lines to find the needed indentations.
4125ec7e 808 (goto-char (point-min))
f5215400
SM
809 (while
810 (progn
811 (unless (looking-at "[ \t]*$")
812 (setq min-indent (min min-indent (current-indentation))))
813 (end-of-line)
814 (setq max-indent (max max-indent (current-column)))
815 (not (or (eobp) (progn (forward-line) nil)))))
816
817 ;; Inserting ccs can change max-indent by (1- tab-width).
59a1ce8d
SM
818 (setq max-indent
819 (+ max-indent (max (length cs) (length ccs)) tab-width -1))
771c9b97 820 (unless indent (setq min-indent 0))
83b96b22 821
aac88001 822 ;; make the leading and trailing lines if requested
83b96b22 823 (when lines
771c9b97
SM
824 (let ((csce
825 (comment-make-extra-lines
826 cs ce ccs cce min-indent max-indent block)))
827 (setq cs (car csce))
828 (setq ce (cdr csce))))
83b96b22
SM
829
830 (goto-char (point-min))
831 ;; Loop over all lines from BEG to END.
f5215400
SM
832 (while
833 (progn
834 (unless (and no-empty (looking-at "[ \t]*$"))
835 (move-to-column min-indent t)
836 (insert cs) (setq cs ccs) ;switch to CCS after the first line
837 (end-of-line)
838 (if (eobp) (setq cce ce))
839 (when cce
840 (when block (move-to-column max-indent t))
841 (insert cce)))
842 (end-of-line)
843 (not (or (eobp) (progn (forward-line) nil))))))))))
83b96b22 844
be83ecb2 845;;;###autoload
83b96b22
SM
846(defun comment-region (beg end &optional arg)
847 "Comment or uncomment each line in the region.
3674a4a9 848With just \\[universal-argument] prefix arg, uncomment each line in region BEG .. END.
83b96b22
SM
849Numeric prefix arg ARG means use ARG comment characters.
850If ARG is negative, delete that many comment characters instead.
be83ecb2
SM
851By default, comments start at the left margin, are terminated on each line,
852even for syntax in which newline does not end the comment and blank lines
853do not get comments. This can be changed with `comment-style'.
83b96b22
SM
854
855The strings used as comment starts are built from
856`comment-start' without trailing spaces and `comment-padding'."
857 (interactive "*r\nP")
858 (comment-normalize-vars)
859 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
2ab98065 860 (let* ((numarg (prefix-numeric-value arg))
771c9b97 861 (add comment-add)
2ab98065
SM
862 (style (cdr (assoc comment-style comment-styles)))
863 (lines (nth 2 style))
864 (block (nth 1 style))
865 (multi (nth 0 style)))
83b96b22
SM
866 (save-excursion
867 ;; we use `chars' instead of `syntax' because `\n' might be
868 ;; of end-comment syntax rather than of whitespace syntax.
869 ;; sanitize BEG and END
870 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
871 (setq beg (max beg (point)))
872 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
873 (setq end (min end (point)))
874 (if (>= beg end) (error "Nothing to comment"))
875
83b96b22
SM
876 ;; sanitize LINES
877 (setq lines
878 (and
9b0d1d6e 879 lines ;; multi
83b96b22
SM
880 (progn (goto-char beg) (beginning-of-line)
881 (skip-syntax-forward " ")
882 (>= (point) beg))
883 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
884 (<= (point) end))
90a44957
SM
885 (or block (not (string= "" comment-end)))
886 (or block (progn (goto-char beg) (search-forward "\n" end t))))))
83b96b22 887
2ab98065
SM
888 ;; don't add end-markers just because the user asked for `block'
889 (unless (or lines (string= "" comment-end)) (setq block nil))
890
83b96b22
SM
891 (cond
892 ((consp arg) (uncomment-region beg end))
893 ((< numarg 0) (uncomment-region beg end (- numarg)))
894 (t
59a1ce8d
SM
895 (setq numarg (if (and (null arg) (= (length comment-start) 1))
896 add (1- numarg)))
83b96b22
SM
897 (comment-region-internal
898 beg end
3e569d22
SM
899 (let ((s (comment-padright comment-start numarg)))
900 (if (string-match comment-start-skip s) s
901 (comment-padright comment-start)))
902 (let ((s (comment-padleft comment-end numarg)))
903 (and s (if (string-match comment-end-skip s) s
904 (comment-padright comment-end))))
f5215400
SM
905 (if multi (comment-padright comment-continue numarg))
906 (if multi (comment-padleft (comment-string-reverse comment-continue) numarg))
83b96b22 907 block
771c9b97
SM
908 lines
909 (nth 3 style))))))
910
911(defun comment-box (beg end &optional arg)
3674a4a9 912 "Comment out the BEG .. END region, putting it inside a box.
771c9b97
SM
913The numeric prefix ARG specifies how many characters to add to begin- and
914end- comment markers additionally to what `comment-add' already specifies."
915 (interactive "*r\np")
9b0d1d6e
SM
916 (let ((comment-style (if (cadr (assoc comment-style comment-styles))
917 'box-multi 'box)))
771c9b97 918 (comment-region beg end (+ comment-add arg))))
83b96b22 919
88fe06af
SM
920
921;;;###autoload
922(defun comment-or-uncomment-region (beg end &optional arg)
923 "Call `comment-region', unless the region only consists of comments,
924in which case call `uncomment-region'. If a prefix arg is given, it
925is passed on to the respective function."
926 (interactive "*r\nP")
927 (funcall (if (save-excursion ;; check for already commented region
928 (goto-char beg)
929 (comment-forward (point-max))
930 (<= end (point)))
931 'uncomment-region 'comment-region)
932 beg end arg))
933
be83ecb2 934;;;###autoload
2ab98065 935(defun comment-dwim (arg)
ad679e45
SM
936 "Call the comment command you want (Do What I Mean).
937If the region is active and `transient-mark-mode' is on, call
39cb51e7 938 `comment-region' (unless it only consists of comments, in which
ad679e45 939 case it calls `uncomment-region').
2ab98065 940Else, if the current line is empty, insert a comment and indent it.
ad679e45
SM
941Else if a prefix ARG is specified, call `comment-kill'.
942Else, call `comment-indent'."
2ab98065
SM
943 (interactive "*P")
944 (comment-normalize-vars)
771c9b97 945 (if (and mark-active transient-mark-mode)
88fe06af 946 (comment-or-uncomment-region (region-beginning) (region-end) arg)
2ab98065 947 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
ad679e45
SM
948 ;; FIXME: If there's no comment to kill on this line and ARG is
949 ;; specified, calling comment-kill is not very clever.
950 (if arg (comment-kill (and (integerp arg) arg)) (comment-indent))
2ab98065 951 (let ((add (if arg (prefix-numeric-value arg)
771c9b97 952 (if (= (length comment-start) 1) comment-add 0))))
2e1fbba4
SM
953 ;; Some modes insist on keeping column 0 comment in column 0
954 ;; so we need to move away from it before inserting the comment.
955 (indent-according-to-mode)
2ab98065
SM
956 (insert (comment-padright comment-start add))
957 (save-excursion
958 (unless (string= "" comment-end)
959 (insert (comment-padleft comment-end add)))
960 (indent-according-to-mode))))))
961
392f1ef5
SM
962(defcustom comment-auto-fill-only-comments nil
963 "Non-nil means to only auto-fill inside comments.
964This has no effect in modes that do not define a comment syntax."
0603917d 965 :type 'boolean)
392f1ef5 966
7164ef13
SM
967(defun comment-valid-prefix (prefix compos)
968 (or
969 ;; Accept any prefix if the current comment is not EOL-terminated.
970 (save-excursion (goto-char compos) (comment-forward) (not (bolp)))
971 ;; Accept any prefix that starts with a comment-start marker.
972 (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)")
973 fill-prefix)))
974
be83ecb2 975;;;###autoload
ad679e45 976(defun comment-indent-new-line (&optional soft)
2ab98065
SM
977 "Break line at point and indent, continuing comment if within one.
978This indents the body of the continued comment
979under the previous comment line.
980
981This command is intended for styles where you write a comment per line,
982starting a new comment (and terminating it if necessary) on each line.
983If you want to continue one comment across several lines, use \\[newline-and-indent].
984
985If a fill column is specified, it overrides the use of the comment column
986or comment indentation.
987
988The inserted newline is marked hard if variable `use-hard-newlines' is true,
989unless optional argument SOFT is non-nil."
990 (interactive)
991 (comment-normalize-vars t)
59a1ce8d
SM
992 (let (compos comin)
993 ;; If we are not inside a comment and we only auto-fill comments,
994 ;; don't do anything (unless no comment syntax is defined).
392f1ef5
SM
995 (unless (and comment-start
996 comment-auto-fill-only-comments
0603917d 997 (not (interactive-p))
392f1ef5 998 (not (save-excursion
59a1ce8d 999 (prog1 (setq compos (comment-beginning))
392f1ef5 1000 (setq comin (point))))))
59a1ce8d
SM
1001
1002 ;; Now we know we should auto-fill.
88fe06af
SM
1003 ;; Insert the newline before removing empty space so that markers
1004 ;; get preserved better.
392f1ef5 1005 (if soft (insert-and-inherit ?\n) (newline 1))
88fe06af
SM
1006 (save-excursion (forward-char -1) (delete-horizontal-space))
1007 (delete-horizontal-space)
1008
7164ef13
SM
1009 (if (and fill-prefix (not adaptive-fill-mode))
1010 ;; Blindly trust a non-adaptive fill-prefix.
392f1ef5
SM
1011 (progn
1012 (indent-to-left-margin)
88fe06af 1013 (insert-before-markers-and-inherit fill-prefix))
59a1ce8d
SM
1014
1015 ;; If necessary check whether we're inside a comment.
a764440a 1016 (unless (or compos (null comment-start))
392f1ef5
SM
1017 (save-excursion
1018 (backward-char)
59a1ce8d
SM
1019 (setq compos (comment-beginning))
1020 (setq comin (point))))
1021
7164ef13
SM
1022 (cond
1023 ;; If there's an adaptive prefix, use it unless we're inside
1024 ;; a comment and the prefix is not a comment starter.
1025 ((and fill-prefix
1026 (or (not compos)
1027 (comment-valid-prefix fill-prefix compos)))
1028 (indent-to-left-margin)
1029 (insert-and-inherit fill-prefix))
1030 ;; If we're not inside a comment, just try to indent.
1031 ((not compos) (indent-according-to-mode))
1032 (t
59a1ce8d
SM
1033 (let* ((comment-column
1034 ;; The continuation indentation should be somewhere between
1035 ;; the current line's indentation (plus 2 for good measure)
1036 ;; and the current comment's indentation, with a preference
1037 ;; for comment-column.
1038 (save-excursion
a764440a 1039 ;; FIXME: use prev line's info rather than first line's.
59a1ce8d
SM
1040 (goto-char compos)
1041 (min (current-column) (max comment-column
1042 (+ 2 (current-indentation))))))
1043 (comstart (buffer-substring compos comin))
1044 (normalp
1045 (string-match (regexp-quote (comment-string-strip
1046 comment-start t t))
1047 comstart))
1048 (comment-end
1049 (if normalp comment-end
1050 ;; The comment starter is not the normal comment-start
1051 ;; so we can't just use comment-end.
1052 (save-excursion
1053 (goto-char compos)
1054 (if (not (comment-forward)) comment-end
1055 (comment-string-strip
1056 (buffer-substring
1057 (save-excursion (comment-enter-backward) (point))
1058 (point))
1059 nil t)))))
1060 (comment-start comstart)
a764440a
SM
1061 (continuep (or comment-multi-line
1062 (cadr (assoc comment-style comment-styles))))
59a1ce8d 1063 ;; Force comment-continue to be recreated from comment-start.
dde6824c 1064 ;; FIXME: wrong if comment-continue was set explicitly!
a764440a 1065 ;; FIXME: use prev line's continuation if available.
59a1ce8d 1066 (comment-continue nil))
a764440a
SM
1067 (if (and comment-multi-line (> (length comment-end) 0))
1068 (indent-according-to-mode)
1069 (insert-and-inherit ?\n)
1070 (forward-char -1)
1071 (comment-indent continuep)
1072 (save-excursion
1073 (let ((pt (point)))
1074 (end-of-line)
1075 (let ((comend (buffer-substring pt (point))))
1076 ;; The 1+ is to make sure we delete the \n inserted above.
1077 (delete-region pt (1+ (point)))
1078 (end-of-line 0)
1079 (insert comend))))))))))))
2ab98065 1080
83b96b22
SM
1081(provide 'newcomment)
1082
83b96b22 1083;;; newcomment.el ends here