annotate lisp/newcomment.el @ 112183:adafd735e9aa

* net/dbus.el (dbus-register-property): Use `dont-register' keyword.
author Michael Albinus <michael.albinus@gmx.de>
date Mon, 10 Jan 2011 11:53:13 +0100
parents e8c77965d85e
children 417b1e4d63cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1 ;;; newcomment.el --- (un)comment regions of buffers
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2
74442
b2e5081b9320 Update copyright years.
Glenn Morris <rgm@gnu.org>
parents: 74239
diff changeset
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
106815
1d1d5d9bd884 Add 2010 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 105957
diff changeset
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
5
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
6 ;; Author: code extracted from Emacs-20's simple.el
64861
7a8556a71193 Address change.
Richard M. Stallman <rms@gnu.org>
parents: 64762
diff changeset
7 ;; Maintainer: Stefan Monnier <monnier@iro.umontreal.ca>
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
8 ;; Keywords: comment uncomment
110015
280c8ae2476d Add "Package:" file headers to denote built-in packages.
Chong Yidong <cyd@stupidchicken.com>
parents: 109817
diff changeset
9 ;; Package: emacs
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
10
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
11 ;; This file is part of GNU Emacs.
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
12
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93622
diff changeset
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
14 ;; it under the terms of the GNU General Public License as published by
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93622
diff changeset
15 ;; the Free Software Foundation, either version 3 of the License, or
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93622
diff changeset
16 ;; (at your option) any later version.
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
17
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
18 ;; GNU Emacs is distributed in the hope that it will be useful,
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
21 ;; GNU General Public License for more details.
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
22
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
23 ;; You should have received a copy of the GNU General Public License
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93622
diff changeset
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
25
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
26 ;;; Commentary:
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
27
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
28 ;; A replacement for simple.el's comment-related functions.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
29
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
30 ;;; Bugs:
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
31
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
32 ;; - boxed comments in Perl are not properly uncommented because they are
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
33 ;; uncommented one-line at a time.
41689
4e6f73fdeab0 New bug.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41286
diff changeset
34 ;; - nested comments in sgml-mode are not properly quoted.
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
35 ;; - single-char nestable comment-start can only do the "\\s<+" stuff
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
36 ;; if the corresponding closing marker happens to be right.
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
37 ;; - uncomment-region with a numeric argument can render multichar
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
38 ;; comment markers invalid.
29119
c411f27154ee (comment-make-extra-lines): Don't use `assert'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29066
diff changeset
39 ;; - comment-indent or comment-region when called inside a comment
c411f27154ee (comment-make-extra-lines): Don't use `assert'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29066
diff changeset
40 ;; will happily break the surrounding comment.
c411f27154ee (comment-make-extra-lines): Don't use `assert'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29066
diff changeset
41 ;; - comment-quote-nested will not (un)quote properly all nested comment
c411f27154ee (comment-make-extra-lines): Don't use `assert'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29066
diff changeset
42 ;; markers if there are more than just comment-start and comment-end.
c411f27154ee (comment-make-extra-lines): Don't use `assert'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29066
diff changeset
43 ;; For example, in Pascal where {...*) and (*...} are possible.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
44
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
45 ;;; Todo:
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
46
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
47 ;; - rebox.el-style refill.
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
48 ;; - quantized steps in comment-alignment.
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
49 ;; - try to align tail comments.
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
50 ;; - check what c-comment-line-break-function has to say.
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
51 ;; - spill auto-fill of comments onto the end of the next line.
26646
0d447856a2f7 (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26645
diff changeset
52 ;; - uncomment-region with a consp (for blocks) or somehow make the
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
53 ;; deletion of continuation markers less dangerous.
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
54 ;; - drop block-comment-<foo> unless it's really used.
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
55 ;; - uncomment-region on a subpart of a comment.
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
56 ;; - support gnu-style "multi-line with space in continue".
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
57 ;; - somehow allow comment-dwim to use the region even if transient-mark-mode
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
58 ;; is not turned on.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
59
32014
13bd1ce1c353 (comment-indent-function): Use 0 for ;;; and %%%.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30091
diff changeset
60 ;; - when auto-filling a comment, try to move the comment to the left
13bd1ce1c353 (comment-indent-function): Use 0 for ;;; and %%%.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30091
diff changeset
61 ;; rather than break it (if possible).
13bd1ce1c353 (comment-indent-function): Use 0 for ;;; and %%%.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30091
diff changeset
62 ;; - sometimes default the comment-column to the same
13bd1ce1c353 (comment-indent-function): Use 0 for ;;; and %%%.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30091
diff changeset
63 ;; one used on the preceding line(s).
13bd1ce1c353 (comment-indent-function): Use 0 for ;;; and %%%.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30091
diff changeset
64
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
65 ;;; Code:
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
66
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
67 ;;;###autoload
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
68 (defalias 'indent-for-comment 'comment-indent)
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
69 ;;;###autoload
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
70 (defalias 'set-comment-column 'comment-set-column)
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
71 ;;;###autoload
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
72 (defalias 'kill-comment 'comment-kill)
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
73 ;;;###autoload
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
74 (defalias 'indent-new-comment-line 'comment-indent-new-line)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
75
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
76 (defgroup comment nil
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
77 "Indenting and filling of comments."
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
78 :prefix "comment-"
29760
c2940b6e267e (comment) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents: 29409
diff changeset
79 :version "21.1"
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
80 :group 'fill)
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
81
50640
8ec8f6706902 (comment-use-syntax): Add autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents: 49768
diff changeset
82 ;; Autoload this to avoid warnings, since some major modes define it.
8ec8f6706902 (comment-use-syntax): Add autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents: 49768
diff changeset
83 ;;;###autoload
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
84 (defvar comment-use-syntax 'undecided
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
85 "Non-nil if syntax-tables can be used instead of regexps.
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
86 Can also be `undecided' which means that a somewhat expensive test will
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
87 be used to try to determine whether syntax-tables should be trusted
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
88 to understand comments or not in the given buffer.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
89 Major modes should set this variable.")
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
90
44999
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
91 (defcustom comment-fill-column nil
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
92 "Column to use for `comment-indent'. If nil, use `fill-column' instead."
62531
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
93 :type '(choice (const nil) integer)
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
94 :group 'comment)
44999
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
95
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
96 ;;;###autoload
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
97 (defcustom comment-column 32
68594
ca4201c7597d Docstring fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 67057
diff changeset
98 "Column to indent right-margin comments to.
78909
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
99 Each mode may establish a different default value for this variable; you
44455
8a30d8f66e1a (comment-column): Docstring improvement.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 43689
diff changeset
100 can set the value for a particular mode using that mode's hook.
78909
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
101 Comments might be indented to a different value in order not to go beyond
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
102 `comment-fill-column' or in order to align them with surrounding comments."
62531
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
103 :type 'integer
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
104 :group 'comment)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
105 (make-variable-buffer-local 'comment-column)
70590
63b772bfba93 Move `safe-local-variable' declarations to the respective files.
Reiner Steib <Reiner.Steib@gmx.de>
parents: 70139
diff changeset
106 ;;;###autoload(put 'comment-column 'safe-local-variable 'integerp)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
107
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
108 ;;;###autoload
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
109 (defvar comment-start nil
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
110 "*String to insert to start a new comment, or nil if no comment syntax.")
70139
fa073d9c1a2b (comment-start, comment-start-skip)
Dan Nicolaescu <dann@ics.uci.edu>
parents: 70043
diff changeset
111 ;;;###autoload(put 'comment-start 'safe-local-variable 'string-or-null-p)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
112
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
113 ;;;###autoload
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
114 (defvar comment-start-skip nil
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
115 "*Regexp to match the start of a comment plus everything up to its body.
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
116 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
117 at the place matched by the close of the first pair.")
70139
fa073d9c1a2b (comment-start, comment-start-skip)
Dan Nicolaescu <dann@ics.uci.edu>
parents: 70043
diff changeset
118 ;;;###autoload(put 'comment-start-skip 'safe-local-variable 'string-or-null-p)
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
119
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
120 ;;;###autoload
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
121 (defvar comment-end-skip nil
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
122 "Regexp to match the end of a comment plus everything up to its body.")
70139
fa073d9c1a2b (comment-start, comment-start-skip)
Dan Nicolaescu <dann@ics.uci.edu>
parents: 70043
diff changeset
123 ;;;###autoload(put 'comment-end-skip 'safe-local-variable 'string-or-null-p)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
124
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
125 ;;;###autoload
105957
25e1d7e37015 * widget.el (define-widget): Purecopy the docstring.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 105372
diff changeset
126 (defvar comment-end (purecopy "")
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
127 "*String to insert to end a new comment.
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
128 Should be an empty string if comments are terminated by end-of-line.")
70139
fa073d9c1a2b (comment-start, comment-start-skip)
Dan Nicolaescu <dann@ics.uci.edu>
parents: 70043
diff changeset
129 ;;;###autoload(put 'comment-end 'safe-local-variable 'string-or-null-p)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
130
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
131 ;;;###autoload
32186
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
132 (defvar comment-indent-function 'comment-indent-default
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
133 "Function to compute desired indentation for a comment.
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
134 This function is called with no args with point at the beginning of
32186
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
135 the comment's starting delimiter and should return either the desired
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
136 column indentation or nil.
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
137 If nil is returned, indentation is delegated to `indent-according-to-mode'.")
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
138
53976
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
139 ;;;###autoload
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
140 (defvar comment-insert-comment-function nil
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
141 "Function to insert a comment when a line doesn't contain one.
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
142 The function has no args.
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
143
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
144 Applicable at least in modes for languages like fixed-format Fortran where
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
145 comments always start in column zero.")
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
146
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
147 (defvar comment-region-function 'comment-region-default
53976
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
148 "Function to comment a region.
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
149 Its args are the same as those of `comment-region', but BEG and END are
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
150 guaranteed to be correctly ordered. It is called within `save-excursion'.
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
151
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
152 Applicable at least in modes for languages like fixed-format Fortran where
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
153 comments always start in column zero.")
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
154
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
155 (defvar uncomment-region-function 'uncomment-region-default
53976
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
156 "Function to uncomment a region.
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
157 Its args are the same as those of `uncomment-region', but BEG and END are
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
158 guaranteed to be correctly ordered. It is called within `save-excursion'.
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
159
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
160 Applicable at least in modes for languages like fixed-format Fortran where
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
161 comments always start in column zero.")
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
162
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
163 ;; ?? never set
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
164 (defvar block-comment-start nil)
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
165 (defvar block-comment-end nil)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
166
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
167 (defvar comment-quote-nested t
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
168 "Non-nil if nested comments should be quoted.
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
169 This should be locally set by each major mode if needed.")
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
170
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
171 (defvar comment-continue nil
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
172 "Continuation string to insert for multiline comments.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
173 This string will be added at the beginning of each line except the very
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
174 first one when commenting a region with a commenting style that allows
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
175 comments to span several lines.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
176 It should generally have the same length as `comment-start' in order to
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
177 preserve indentation.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
178 If it is nil a value will be automatically derived from `comment-start'
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
179 by replacing its first character with a space.")
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
180
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
181 (defvar comment-add 0
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
182 "How many more comment chars should be inserted by `comment-region'.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
183 This determines the default value of the numeric argument of `comment-region'.
85665
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
184 The `plain' comment style doubles this value.
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
185
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
186 This should generally stay 0, except for a few modes like Lisp where
85665
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
187 it is 1 so that regions are commented with two or three semi-colons.")
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
188
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
189 (defconst comment-styles
111821
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
190 '((plain nil nil nil nil
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
191 "Start in column 0 (do not indent), as in Emacs-20")
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
192 (indent-or-triple nil nil nil multi-char
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
193 "Start in column 0, but only for single-char starters")
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
194 (indent nil nil nil t
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
195 "Full comment per line, ends not aligned")
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
196 (aligned nil t nil t
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
197 "Full comment per line, ends aligned")
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
198 (box nil t t t
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
199 "Full comment per line, ends aligned, + top and bottom")
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
200 (extra-line t nil t t
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
201 "One comment for all lines, end on a line by itself")
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
202 (multi-line t nil nil t
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
203 "One comment for all lines, end on last commented line")
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
204 (box-multi t t t t
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
205 "One comment for all lines, + top and bottom"))
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
206 "Comment region style definitions.
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
207 Each style is defined with a form (STYLE . (MULTI ALIGN EXTRA INDENT DOC)).
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
208 DOC should succinctly describe the style.
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
209 STYLE should be a mnemonic symbol.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
210 MULTI specifies that comments are allowed to span multiple lines.
111821
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
211 e.g. in C it comments regions as
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
212 /* blabla
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
213 * bli */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
214 rather than
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
215 /* blabla */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
216 /* bli */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
217 if `comment-end' is empty, this has no effect.
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
218
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
219 ALIGN specifies that the `comment-end' markers should be aligned.
111821
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
220 e.g. in C it comments regions as
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
221 /* blabla */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
222 /* bli */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
223 rather than
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
224 /* blabla */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
225 /* bli */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
226 if `comment-end' is empty, this has no effect, unless EXTRA is also set,
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
227 in which case the comment gets wrapped in a box.
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
228
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
229 EXTRA specifies that an extra line should be used before and after the
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
230 region to comment (to put the `comment-end' and `comment-start').
111821
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
231 e.g. in C it comments regions as
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
232 /*
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
233 * blabla
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
234 * bli
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
235 */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
236 rather than
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
237 /* blabla
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
238 * bli */
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
239 if the comment style is not multi line, this has no effect, unless ALIGN
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
240 is also set, in which case the comment gets wrapped in a box.
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
241
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
242 INDENT specifies that the `comment-start' markers should not be put at the
85665
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
243 left margin but at the current indentation of the region to comment.
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
244 If INDENT is `multi-char', that means indent multi-character
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
245 comment starters, but not one-character comment starters.")
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
246
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
247 ;;;###autoload
100486
1043d1f4aa3e (comment-style): Default to `indent'. (Bug#1589)
Dan Nicolaescu <dann@ics.uci.edu>
parents: 94678
diff changeset
248 (defcustom comment-style 'indent
68594
ca4201c7597d Docstring fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 67057
diff changeset
249 "Style to be used for `comment-region'.
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
250 See `comment-styles' for a list of available styles."
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
251 :type (if (boundp 'comment-styles)
111821
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
252 `(choice
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
253 ,@(mapcar (lambda (s)
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
254 `(const :tag ,(format "%s: %s" (car s) (nth 5 s))
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
255 ,(car s)))
e8c77965d85e * lisp/newcomment.el (comment-styles): Add docs to each style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111081
diff changeset
256 comment-styles))
62531
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
257 'symbol)
100486
1043d1f4aa3e (comment-style): Default to `indent'. (Bug#1589)
Dan Nicolaescu <dann@ics.uci.edu>
parents: 94678
diff changeset
258 :version "23.1"
62531
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
259 :group 'comment)
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
260
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
261 ;;;###autoload
105957
25e1d7e37015 * widget.el (define-widget): Purecopy the docstring.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 105372
diff changeset
262 (defcustom comment-padding (purecopy " ")
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
263 "Padding string that `comment-region' puts between comment chars and text.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
264 Can also be an integer which will be automatically turned into a string
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
265 of the corresponding number of spaces.
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
266
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
267 Extra spacing between the comment characters and the comment text
41286
6b56e476153d (comment-column, comment-style, comment-multi-line)
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41253
diff changeset
268 makes the comment easier to read. Default is 1. nil means 0."
62531
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
269 :type '(choice string integer (const nil))
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
270 :group 'comment)
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
271
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
272 ;;;###autoload
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
273 (defcustom comment-multi-line nil
68594
ca4201c7597d Docstring fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 67057
diff changeset
274 "Non-nil means `comment-indent-new-line' continues comments.
57281
902c984fa20a (comment-multi-line): Doc fix.
Luc Teirlinck <teirllm@auburn.edu>
parents: 56957
diff changeset
275 That is, it inserts no new terminator or starter.
902c984fa20a (comment-multi-line): Doc fix.
Luc Teirlinck <teirllm@auburn.edu>
parents: 56957
diff changeset
276 This affects `auto-fill-mode', which is the main reason to
902c984fa20a (comment-multi-line): Doc fix.
Luc Teirlinck <teirllm@auburn.edu>
parents: 56957
diff changeset
277 customize this variable.
902c984fa20a (comment-multi-line): Doc fix.
Luc Teirlinck <teirllm@auburn.edu>
parents: 56957
diff changeset
278
902c984fa20a (comment-multi-line): Doc fix.
Luc Teirlinck <teirllm@auburn.edu>
parents: 56957
diff changeset
279 It also affects \\[indent-new-comment-line]. However, if you want this
902c984fa20a (comment-multi-line): Doc fix.
Luc Teirlinck <teirllm@auburn.edu>
parents: 56957
diff changeset
280 behavior for explicit filling, you might as well use \\[newline-and-indent]."
62531
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
281 :type 'boolean
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
282 :group 'comment)
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
283
51337
0237e2420453 (comment-empty-lines): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50947
diff changeset
284 (defcustom comment-empty-lines nil
0237e2420453 (comment-empty-lines): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50947
diff changeset
285 "If nil, `comment-region' does not comment out empty lines.
0237e2420453 (comment-empty-lines): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50947
diff changeset
286 If t, it always comments out empty lines.
73317
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
287 If `eol' it only comments out empty lines if comments are
51337
0237e2420453 (comment-empty-lines): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50947
diff changeset
288 terminated by the end of line (i.e. `comment-end' is empty)."
0237e2420453 (comment-empty-lines): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50947
diff changeset
289 :type '(choice (const :tag "Never" nil)
0237e2420453 (comment-empty-lines): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50947
diff changeset
290 (const :tag "Always" t)
62531
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
291 (const :tag "EOl-terminated" 'eol))
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
292 :group 'comment)
51337
0237e2420453 (comment-empty-lines): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50947
diff changeset
293
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
294 ;;;;
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
295 ;;;; Helpers
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
296 ;;;;
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
297
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
298 (defun comment-string-strip (str beforep afterp)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
299 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
300 (string-match (concat "\\`" (if beforep "\\s-*")
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
301 "\\(.*?\\)" (if afterp "\\s-*\n?")
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
302 "\\'") str)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
303 (match-string 1 str))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
304
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
305 (defun comment-string-reverse (s)
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
306 "Return the mirror image of string S, without any trailing space."
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
307 (comment-string-strip (concat (nreverse (string-to-list s))) nil t))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
308
39761
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
309 ;;;###autoload
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
310 (defun comment-normalize-vars (&optional noerror)
50845
ac2120f868b9 (comment-normalize-vars): Add docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50843
diff changeset
311 "Check and setup the variables needed by other commenting functions.
ac2120f868b9 (comment-normalize-vars): Add docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50843
diff changeset
312 Functions autoloaded from newcomment.el, being entry points, should call
ac2120f868b9 (comment-normalize-vars): Add docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50843
diff changeset
313 this function before any other, so the rest of the code can assume that
ac2120f868b9 (comment-normalize-vars): Add docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50843
diff changeset
314 the variables are properly set."
53164
2ea1aaf0d7db (comment-normalize-vars): Initialize properly if comment-start was nil.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52931
diff changeset
315 (unless (and (not comment-start) noerror)
2ea1aaf0d7db (comment-normalize-vars): Initialize properly if comment-start was nil.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52931
diff changeset
316 (unless comment-start
54279
41d93a97701d (comment-normalize-vars): Check the user-specified comment start marker.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54276
diff changeset
317 (let ((cs (read-string "No comment syntax is defined. Use: ")))
41d93a97701d (comment-normalize-vars): Check the user-specified comment start marker.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54276
diff changeset
318 (if (zerop (length cs))
41d93a97701d (comment-normalize-vars): Check the user-specified comment start marker.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54276
diff changeset
319 (error "No comment syntax defined")
41d93a97701d (comment-normalize-vars): Check the user-specified comment start marker.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54276
diff changeset
320 (set (make-local-variable 'comment-start) cs))))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
321 ;; comment-use-syntax
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
322 (when (eq comment-use-syntax 'undecided)
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
323 (set (make-local-variable 'comment-use-syntax)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
324 (let ((st (syntax-table))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
325 (cs comment-start)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
326 (ce (if (string= "" comment-end) "\n" comment-end)))
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
327 ;; Try to skip over a comment using forward-comment
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
328 ;; to see if the syntax tables properly recognize it.
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
329 (with-temp-buffer
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
330 (set-syntax-table st)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
331 (insert cs " hello " ce)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
332 (goto-char (point-min))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
333 (and (forward-comment 1) (eobp))))))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
334 ;; comment-padding
41021
c07719cfdfcd (comment-normalize-vars): Handle a nil comment-padding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39864
diff changeset
335 (unless comment-padding (setq comment-padding 0))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
336 (when (integerp comment-padding)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
337 (setq comment-padding (make-string comment-padding ? )))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
338 ;; comment markers
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
339 ;;(setq comment-start (comment-string-strip comment-start t nil))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
340 ;;(setq comment-end (comment-string-strip comment-end nil t))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
341 ;; comment-continue
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
342 (unless (or comment-continue (string= comment-end ""))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
343 (set (make-local-variable 'comment-continue)
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
344 (concat (if (string-match "\\S-\\S-" comment-start) " " "|")
39761
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
345 (substring comment-start 1)))
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
346 ;; Hasn't been necessary yet.
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
347 ;; (unless (string-match comment-start-skip comment-continue)
72453
204e1538695a (comment-box): Call `comment-normalize-vars'. Add autoload cookie.
Juanma Barranquero <lekktu@gmail.com>
parents: 72410
diff changeset
348 ;; (kill-local-variable 'comment-continue))
39761
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
349 )
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
350 ;; comment-skip regexps
45902
24d34388fd69 (comment-normalize-vars): Refresh the comment-foo-skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 45023
diff changeset
351 (unless (and comment-start-skip
24d34388fd69 (comment-normalize-vars): Refresh the comment-foo-skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 45023
diff changeset
352 ;; In case comment-start has changed since last time.
24d34388fd69 (comment-normalize-vars): Refresh the comment-foo-skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 45023
diff changeset
353 (string-match comment-start-skip comment-start))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
354 (set (make-local-variable 'comment-start-skip)
50673
4b5928c8d588 (comment-normalize-vars): Fix regexp.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50640
diff changeset
355 (concat "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
356 (regexp-quote (comment-string-strip comment-start t t))
36490
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
357 ;; Let's not allow any \s- but only [ \t] since \n
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
358 ;; might be both a comment-end marker and \s-.
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
359 "+\\)[ \t]*")))
45902
24d34388fd69 (comment-normalize-vars): Refresh the comment-foo-skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 45023
diff changeset
360 (unless (and comment-end-skip
24d34388fd69 (comment-normalize-vars): Refresh the comment-foo-skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 45023
diff changeset
361 ;; In case comment-end has changed since last time.
110529
43a857ac456c * lisp/newcomment.el (comment-normalize-vars): Better test validity of
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
362 (string-match comment-end-skip
43a857ac456c * lisp/newcomment.el (comment-normalize-vars): Better test validity of
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
363 (if (string= "" comment-end) "\n" comment-end)))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
364 (let ((ce (if (string= "" comment-end) "\n"
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
365 (comment-string-strip comment-end t t))))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
366 (set (make-local-variable 'comment-end-skip)
39486
54fe59ee8057 (comment-normalize-vars): Use " \t" rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39079
diff changeset
367 ;; We use [ \t] rather than \s- because we don't want to
54fe59ee8057 (comment-normalize-vars): Use " \t" rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39079
diff changeset
368 ;; remove ^L in C mode when uncommenting.
54fe59ee8057 (comment-normalize-vars): Use " \t" rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39079
diff changeset
369 (concat "[ \t]*\\(\\s>" (if comment-quote-nested "" "+")
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
370 "\\|" (regexp-quote (substring ce 0 1))
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
371 (if (and comment-quote-nested (<= (length ce) 1)) "" "+")
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
372 (regexp-quote (substring ce 1))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
373 "\\)"))))))
49597
e88404e8f2cf Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 48257
diff changeset
374
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
375 (defun comment-quote-re (str unp)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
376 (concat (regexp-quote (substring str 0 1))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
377 "\\\\" (if unp "+" "*")
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
378 (regexp-quote (substring str 1))))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
379
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
380 (defun comment-quote-nested (cs ce unp)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
381 "Quote or unquote nested comments.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
382 If UNP is non-nil, unquote nested comment markers."
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
383 (setq cs (comment-string-strip cs t t))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
384 (setq ce (comment-string-strip ce t t))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
385 (when (and comment-quote-nested (> (length ce) 0))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
386 (let ((re (concat (comment-quote-re ce unp)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
387 "\\|" (comment-quote-re cs unp))))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
388 (goto-char (point-min))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
389 (while (re-search-forward re nil t)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
390 (goto-char (match-beginning 0))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
391 (forward-char 1)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
392 (if unp (delete-char 1) (insert "\\"))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
393 (when (= (length ce) 1)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
394 ;; If the comment-end is a single char, adding a \ after that
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
395 ;; "first" char won't deactivate it, so we turn such a CE
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
396 ;; into !CS. I.e. for pascal, we turn } into !{
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
397 (if (not unp)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
398 (when (string= (match-string 0) ce)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
399 (replace-match (concat "!" cs) t t))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
400 (when (and (< (point-min) (match-beginning 0))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
401 (string= (buffer-substring (1- (match-beginning 0))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
402 (1- (match-end 0)))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
403 (concat "!" cs)))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
404 (backward-char 2)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
405 (delete-char (- (match-end 0) (match-beginning 0)))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
406 (insert ce))))))))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
407
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
408 ;;;;
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
409 ;;;; Navigation
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
410 ;;;;
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
411
54314
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
412 (defvar comment-use-global-state nil
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
413 "Non-nil means that the global syntactic context is used.
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
414 More specifically, it means that `syntax-ppss' is used to find out whether
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
415 point is within a string or not. Major modes whose syntax is faithfully
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
416 described by the syntax-tables can set this to non-nil so comment markers
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
417 in strings will not confuse Emacs.")
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
418
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
419 (defun comment-search-forward (limit &optional noerror)
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
420 "Find a comment start between point and LIMIT.
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
421 Moves point to inside the comment and returns the position of the
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
422 comment-starter. If no comment is found, moves point to LIMIT
56019
0517f0b254be (comment-search-forward, comment-search-backward): Fix typos.
Juanma Barranquero <lekktu@gmail.com>
parents: 54314
diff changeset
423 and raises an error or returns nil if NOERROR is non-nil."
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
424 (if (not comment-use-syntax)
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
425 (if (re-search-forward comment-start-skip limit noerror)
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
426 (or (match-end 1) (match-beginning 0))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
427 (goto-char limit)
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
428 (unless noerror (error "No comment")))
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
429 (let* ((pt (point))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
430 ;; Assume (at first) that pt is outside of any string.
54314
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
431 (s (parse-partial-sexp pt (or limit (point-max)) nil nil
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
432 (if comment-use-global-state (syntax-ppss pt))
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
433 t)))
597fb06dcdda (comment-use-global-state): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54279
diff changeset
434 (when (and (nth 8 s) (nth 3 s) (not comment-use-global-state))
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
435 ;; The search ended at eol inside a string. Try to see if it
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
436 ;; works better when we assume that pt is inside a string.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
437 (setq s (parse-partial-sexp
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
438 pt (or limit (point-max)) nil nil
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
439 (list nil nil nil (nth 3 s) nil nil nil nil)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
440 t)))
76879
d703606e395a (comment-search-forward): Discard comment starters before point.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 75347
diff changeset
441 (if (or (not (and (nth 8 s) (not (nth 3 s))))
d703606e395a (comment-search-forward): Discard comment starters before point.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 75347
diff changeset
442 ;; Make sure the comment starts after PT.
d703606e395a (comment-search-forward): Discard comment starters before point.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 75347
diff changeset
443 (< (nth 8 s) pt))
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
444 (unless noerror (error "No comment"))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
445 ;; We found the comment.
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
446 (let ((pos (point))
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
447 (start (nth 8 s))
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
448 (bol (line-beginning-position))
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
449 (end nil))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
450 (while (and (null end) (>= (point) bol))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
451 (if (looking-at comment-start-skip)
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
452 (setq end (min (or limit (point-max)) (match-end 0)))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
453 (backward-char)))
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
454 (goto-char (or end pos))
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
455 start)))))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
456
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
457 (defun comment-search-backward (&optional limit noerror)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
458 "Find a comment start between LIMIT and point.
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
459 Moves point to inside the comment and returns the position of the
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
460 comment-starter. If no comment is found, moves point to LIMIT
56019
0517f0b254be (comment-search-forward, comment-search-backward): Fix typos.
Juanma Barranquero <lekktu@gmail.com>
parents: 54314
diff changeset
461 and raises an error or returns nil if NOERROR is non-nil."
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
462 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
463 ;; stop there. This can be rather bad in general, but since
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
464 ;; comment-search-backward is only used to find the comment-column (in
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
465 ;; comment-set-column) and to find the comment-start string (via
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
466 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
467 (if (not (re-search-backward comment-start-skip limit t))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
468 (unless noerror (error "No comment"))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
469 (beginning-of-line)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
470 (let* ((end (match-end 0))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
471 (cs (comment-search-forward end t))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
472 (pt (point)))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
473 (if (not cs)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
474 (progn (beginning-of-line)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
475 (comment-search-backward limit noerror))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
476 (while (progn (goto-char cs)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
477 (comment-forward)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
478 (and (< (point) end)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
479 (setq cs (comment-search-forward end t))))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
480 (setq pt (point)))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
481 (goto-char pt)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
482 cs))))
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
483
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
484 (defun comment-beginning ()
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
485 "Find the beginning of the enclosing comment.
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
486 Returns nil if not inside a comment, else moves point and returns
57301
800ede9cd8e3 comment-beginning: doc-fix and don't choke on unset comment-end-skip when at beginning of comment
Daniel Pfeiffer <occitan@esperanto.org>
parents: 57281
diff changeset
487 the same as `comment-search-backward'."
36490
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
488 ;; HACK ATTACK!
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
489 ;; We should really test `in-string-p' but that can be expensive.
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
490 (unless (eq (get-text-property (point) 'face) 'font-lock-string-face)
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
491 (let ((pt (point))
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
492 (cs (comment-search-backward nil t)))
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
493 (when cs
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
494 (if (save-excursion
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
495 (goto-char cs)
38898
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
496 (and
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
497 ;; For modes where comment-start and comment-end are the same,
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
498 ;; the search above may have found a `ce' rather than a `cs'.
57301
800ede9cd8e3 comment-beginning: doc-fix and don't choke on unset comment-end-skip when at beginning of comment
Daniel Pfeiffer <occitan@esperanto.org>
parents: 57281
diff changeset
499 (or (if comment-end-skip (not (looking-at comment-end-skip)))
38898
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
500 ;; Maybe font-lock knows that it's a `cs'?
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
501 (eq (get-text-property (match-end 0) 'face)
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
502 'font-lock-comment-face)
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
503 (unless (eq (get-text-property (point) 'face)
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
504 'font-lock-comment-face)
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
505 ;; Let's assume it's a `cs' if we're on the same line.
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
506 (>= (line-end-position) pt)))
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
507 ;; Make sure that PT is not past the end of the comment.
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
508 (if (comment-forward 1) (> (point) pt) (eobp))))
36490
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
509 cs
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
510 (goto-char pt)
e40c1ae4f04b (comment-normalize-vars): Use [ \t] for the trailing
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36271
diff changeset
511 nil)))))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
512
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
513 (defun comment-forward (&optional n)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
514 "Skip forward over N comments.
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
515 Just like `forward-comment' but only for positive N
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
516 and can use regexps instead of syntax."
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
517 (setq n (or n 1))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
518 (if (< n 0) (error "No comment-backward")
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
519 (if comment-use-syntax (forward-comment n)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
520 (while (> n 0)
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
521 (setq n
43689
5e6a5df9973a (comment-forward): Use forward-comment to skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42706
diff changeset
522 (if (or (forward-comment 1)
5e6a5df9973a (comment-forward): Use forward-comment to skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42706
diff changeset
523 (and (looking-at comment-start-skip)
5e6a5df9973a (comment-forward): Use forward-comment to skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42706
diff changeset
524 (goto-char (match-end 0))
5e6a5df9973a (comment-forward): Use forward-comment to skip
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42706
diff changeset
525 (re-search-forward comment-end-skip nil 'move)))
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
526 (1- n) -1)))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
527 (= n 0))))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
528
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
529 (defun comment-enter-backward ()
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
530 "Move from the end of a comment to the end of its content.
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
531 Point is assumed to be just at the end of a comment."
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
532 (if (bolp)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
533 ;; comment-end = ""
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
534 (progn (backward-char) (skip-syntax-backward " "))
67054
b8c0eb74d77e *** empty log message ***
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 66712
diff changeset
535 (cond
93622
5d2b21aad9af (comment-enter-backward): Be careful to restore point changed during narrowing.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 92692
diff changeset
536 ((save-excursion
5d2b21aad9af (comment-enter-backward): Be careful to restore point changed during narrowing.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 92692
diff changeset
537 (save-restriction
5d2b21aad9af (comment-enter-backward): Be careful to restore point changed during narrowing.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 92692
diff changeset
538 (narrow-to-region (line-beginning-position) (point))
5d2b21aad9af (comment-enter-backward): Be careful to restore point changed during narrowing.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 92692
diff changeset
539 (goto-char (point-min))
5d2b21aad9af (comment-enter-backward): Be careful to restore point changed during narrowing.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 92692
diff changeset
540 (re-search-forward (concat comment-end-skip "\\'") nil t)))
67054
b8c0eb74d77e *** empty log message ***
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 66712
diff changeset
541 (goto-char (match-beginning 0)))
80877
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
542 ;; comment-end-skip not found probably because it was not set
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
543 ;; right. Since \\s> should catch the single-char case, let's
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
544 ;; check that we're looking at a two-char comment ender.
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
545 ((not (or (<= (- (point-max) (line-beginning-position)) 1)
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
546 (zerop (logand (car (syntax-after (- (point) 1)))
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
547 ;; Here we take advantage of the fact that
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
548 ;; the syntax class " " is encoded to 0,
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
549 ;; so " 4" gives us just the 4 bit.
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
550 (car (string-to-syntax " 4"))))
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
551 (zerop (logand (car (syntax-after (- (point) 2)))
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
552 (car (string-to-syntax " 3"))))))
67054
b8c0eb74d77e *** empty log message ***
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 66712
diff changeset
553 (backward-char 2)
b8c0eb74d77e *** empty log message ***
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 66712
diff changeset
554 (skip-chars-backward (string (char-after)))
80877
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
555 (skip-syntax-backward " "))
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
556 ;; No clue what's going on: maybe we're really not right after the
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
557 ;; end of a comment. Maybe we're at the "end" because of EOB rather
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
558 ;; than because of a marker.
52034b3003ea (comment-search-forward): Make sure we search forward.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 76879
diff changeset
559 (t (skip-syntax-backward " ")))))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
560
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
561 ;;;;
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
562 ;;;; Commands
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
563 ;;;;
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
564
32307
c3058f7483c4 (comment-indent-default): Autoload.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32186
diff changeset
565 ;;;###autoload
32186
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
566 (defun comment-indent-default ()
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
567 "Default for `comment-indent-function'."
33468
4d15850856e4 (comment-indent-default): Stick \s<\s< to the left
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32307
diff changeset
568 (if (and (looking-at "\\s<\\s<\\(\\s<\\)?")
4d15850856e4 (comment-indent-default): Stick \s<\s< to the left
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32307
diff changeset
569 (or (match-end 1) (/= (current-column) (current-indentation))))
4d15850856e4 (comment-indent-default): Stick \s<\s< to the left
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32307
diff changeset
570 0
32186
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
571 (when (or (/= (current-column) (current-indentation))
63829
5448f90c05f7 (comment-indent-default): Don't get fooled by an early end of buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 62531
diff changeset
572 (and (> comment-add 0) (looking-at "\\s<\\(\\S<\\|\\'\\)")))
32186
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
573 comment-column)))
964f10494360 (comment-indent):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32014
diff changeset
574
78909
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
575 (defun comment-choose-indent (&optional indent)
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
576 "Choose the indentation to use for a right-hand-side comment.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
577 The criteria are (in this order):
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
578 - try to keep the comment's text within `comment-fill-column'.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
579 - try to align with surrounding comments.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
580 - prefer INDENT (or `comment-column' if nil).
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
581 Point is expected to be at the start of the comment."
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
582 (unless indent (setq indent comment-column))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
583 ;; Avoid moving comments past the fill-column.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
584 (let ((max (+ (current-column)
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
585 (- (or comment-fill-column fill-column)
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
586 (save-excursion (end-of-line) (current-column)))))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
587 (other nil)
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
588 (min (save-excursion (skip-chars-backward " \t")
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
589 (1+ (current-column)))))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
590 ;; Fix up the range.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
591 (if (< max min) (setq max min))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
592 ;; Don't move past the fill column.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
593 (if (<= max indent) (setq indent max))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
594 ;; We can choose anywhere between min..max.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
595 ;; Let's try to align to a comment on the previous line.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
596 (save-excursion
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
597 (when (and (zerop (forward-line -1))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
598 (setq other (comment-search-forward
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
599 (line-end-position) t)))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
600 (goto-char other) (setq other (current-column))))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
601 (if (and other (<= other max) (>= other min))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
602 ;; There is a comment and it's in the range: bingo!
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
603 other
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
604 ;; Can't align to a previous comment: let's try to align to comments
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
605 ;; on the following lines, then. These have not been re-indented yet,
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
606 ;; so we can't directly align ourselves with them. All we do is to try
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
607 ;; and choose an indentation point with which they will be able to
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
608 ;; align themselves.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
609 (save-excursion
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
610 (while (and (zerop (forward-line 1))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
611 (setq other (comment-search-forward
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
612 (line-end-position) t)))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
613 (goto-char other)
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
614 (let ((omax (+ (current-column)
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
615 (- (or comment-fill-column fill-column)
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
616 (save-excursion (end-of-line) (current-column)))))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
617 (omin (save-excursion (skip-chars-backward " \t")
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
618 (1+ (current-column)))))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
619 (if (and (>= omax min) (<= omin max))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
620 (progn (setq min (max omin min))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
621 (setq max (min omax max)))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
622 ;; Can't align with this anyway, so exit the loop.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
623 (goto-char (point-max))))))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
624 ;; Return the closest point to indent within min..max.
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
625 (max min (min max indent)))))
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
626
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
627 ;;;###autoload
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
628 (defun comment-indent (&optional continue)
53976
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
629 "Indent this line's comment to `comment-column', or insert an empty comment.
38898
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
630 If CONTINUE is non-nil, use the `comment-continue' markers if any."
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
631 (interactive "*")
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
632 (comment-normalize-vars)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
633 (let* ((empty (save-excursion (beginning-of-line)
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
634 (looking-at "[ \t]*$")))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
635 (starter (or (and continue comment-continue)
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
636 (and empty block-comment-start) comment-start))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
637 (ender (or (and continue comment-continue "")
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
638 (and empty block-comment-end) comment-end)))
33705
273d5b4aba82 (comment-indent): Insert comment before calling
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 33485
diff changeset
639 (unless starter (error "No comment syntax defined"))
273d5b4aba82 (comment-indent): Insert comment before calling
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 33485
diff changeset
640 (beginning-of-line)
273d5b4aba82 (comment-indent): Insert comment before calling
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 33485
diff changeset
641 (let* ((eolpos (line-end-position))
273d5b4aba82 (comment-indent): Insert comment before calling
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 33485
diff changeset
642 (begpos (comment-search-forward eolpos t))
273d5b4aba82 (comment-indent): Insert comment before calling
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 33485
diff changeset
643 cpos indent)
85895
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
644 (if (and comment-insert-comment-function (not begpos))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
645 ;; If no comment and c-i-c-f is set, let it do everything.
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
646 (funcall comment-insert-comment-function)
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
647 ;; An existing comment?
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
648 (if begpos
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
649 (progn
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
650 (if (and (not (looking-at "[\t\n ]"))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
651 (looking-at comment-end-skip))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
652 ;; The comment is empty and we have skipped all its space
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
653 ;; and landed right before the comment-ender:
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
654 ;; Go back to the middle of the space.
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
655 (forward-char (/ (skip-chars-backward " \t") -2)))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
656 (setq cpos (point-marker)))
33705
273d5b4aba82 (comment-indent): Insert comment before calling
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 33485
diff changeset
657 ;; If none, insert one.
273d5b4aba82 (comment-indent): Insert comment before calling
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 33485
diff changeset
658 (save-excursion
53976
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
659 ;; Some `comment-indent-function's insist on not moving
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
660 ;; comments that are in column 0, so we first go to the
b2fbe05650de (comment-insert-comment-function, comment-region-function,
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53164
diff changeset
661 ;; likely target column.
39079
fc69197cb76d (comment-indent): Don't insert a fixed number of spaces.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39078
diff changeset
662 (indent-to comment-column)
49768
ffe55e61c3fc (comment-indent): Ensure space before added comment.
Dave Love <fx@gnu.org>
parents: 49597
diff changeset
663 ;; Ensure there's a space before the comment for things
ffe55e61c3fc (comment-indent): Ensure space before added comment.
Dave Love <fx@gnu.org>
parents: 49597
diff changeset
664 ;; like sh where it matters (as well as being neater).
74239
9343217833e9 (comment-indent): "?\ " -> "?\s".
Juanma Barranquero <lekktu@gmail.com>
parents: 73317
diff changeset
665 (unless (memq (char-before) '(nil ?\n ?\t ?\s))
9343217833e9 (comment-indent): "?\ " -> "?\s".
Juanma Barranquero <lekktu@gmail.com>
parents: 73317
diff changeset
666 (insert ?\s))
50929
068965f379b3 (comment-indent): Be more careful when inserting
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50845
diff changeset
667 (setq begpos (point))
33705
273d5b4aba82 (comment-indent): Insert comment before calling
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 33485
diff changeset
668 (insert starter)
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
669 (setq cpos (point-marker))
85895
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
670 (insert ender)))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
671 (goto-char begpos)
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
672 ;; Compute desired indent.
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
673 (setq indent (save-excursion (funcall comment-indent-function)))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
674 ;; If `indent' is nil and there's code before the comment, we can't
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
675 ;; use `indent-according-to-mode', so we default to comment-column.
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
676 (unless (or indent (save-excursion (skip-chars-backward " \t") (bolp)))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
677 (setq indent comment-column))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
678 (if (not indent)
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
679 ;; comment-indent-function refuses: delegate to line-indent.
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
680 (indent-according-to-mode)
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
681 ;; If the comment is at the right of code, adjust the indentation.
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
682 (unless (save-excursion (skip-chars-backward " \t") (bolp))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
683 (setq indent (comment-choose-indent indent)))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
684 ;; Update INDENT to leave at least one space
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
685 ;; after other nonwhite text on the line.
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
686 (save-excursion
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
687 (skip-chars-backward " \t")
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
688 (unless (bolp)
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
689 (setq indent (max indent (1+ (current-column))))))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
690 ;; If that's different from comment's current position, change it.
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
691 (unless (= (current-column) indent)
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
692 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
693 (indent-to indent)))
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
694 (goto-char cpos)
597274bf7123 (comment-indent): Let comment-insert-comment-function,
Glenn Morris <rgm@gnu.org>
parents: 85665
diff changeset
695 (set-marker cpos nil)))))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
696
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
697 ;;;###autoload
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
698 (defun comment-set-column (arg)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
699 "Set the comment column based on point.
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
700 With no ARG, set the comment column to the current column.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
701 With just minus as arg, kill any comment on this line.
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
702 With any other arg, set comment column to indentation of the previous comment
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
703 and then align or create a comment on this line at that column."
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
704 (interactive "P")
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
705 (cond
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
706 ((eq arg '-) (comment-kill nil))
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
707 (arg
50843
bc3a5a6b96ab (comment-set-column, comment-kill, comment-or-uncomment-region):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50839
diff changeset
708 (comment-normalize-vars)
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
709 (save-excursion
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
710 (beginning-of-line)
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
711 (comment-search-backward)
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
712 (beginning-of-line)
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
713 (goto-char (comment-search-forward (line-end-position)))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
714 (setq comment-column (current-column))
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
715 (message "Comment column set to %d" comment-column))
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
716 (comment-indent))
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
717 (t (setq comment-column (current-column))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
718 (message "Comment column set to %d" comment-column))))
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
719
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
720 ;;;###autoload
26763
5a4671b4895c various fixes and gratuitous movements.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26663
diff changeset
721 (defun comment-kill (arg)
80271
f74b83c18a6a (comment-kill): Docstring improvement.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 79721
diff changeset
722 "Kill the first comment on this line, if any.
26646
0d447856a2f7 (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26645
diff changeset
723 With prefix ARG, kill comments on that many lines starting with this one."
0d447856a2f7 (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26645
diff changeset
724 (interactive "P")
50843
bc3a5a6b96ab (comment-set-column, comment-kill, comment-or-uncomment-region):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50839
diff changeset
725 (comment-normalize-vars)
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
726 (dotimes (_ (prefix-numeric-value arg))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
727 (save-excursion
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
728 (beginning-of-line)
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
729 (let ((cs (comment-search-forward (line-end-position) t)))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
730 (when cs
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
731 (goto-char cs)
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
732 (skip-syntax-backward " ")
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
733 (setq cs (point))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
734 (comment-forward)
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
735 (kill-region cs (if (bolp) (1- (point)) (point)))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
736 (indent-according-to-mode))))
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
737 (if arg (forward-line 1))))
26646
0d447856a2f7 (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26645
diff changeset
738
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
739 (defun comment-padright (str &optional n)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
740 "Construct a string composed of STR plus `comment-padding'.
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
741 It also adds N copies of the last non-whitespace chars of STR.
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
742 If STR already contains padding, the corresponding amount is
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
743 ignored from `comment-padding'.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
744 N defaults to 0.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
745 If N is `re', a regexp is returned instead, that would match
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
746 the string for any N."
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
747 (setq n (or n 0))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
748 (when (and (stringp str) (not (string= "" str)))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
749 ;; Separate the actual string from any leading/trailing padding
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
750 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
751 (let ((s (match-string 1 str)) ;actual string
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
752 (lpad (substring str 0 (match-beginning 1))) ;left padding
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
753 (rpad (concat (substring str (match-end 1)) ;original right padding
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
754 (substring comment-padding ;additional right padding
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
755 (min (- (match-end 0) (match-end 1))
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
756 (length comment-padding)))))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
757 ;; We can only duplicate C if the comment-end has multiple chars
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
758 ;; or if comments can be nested, else the comment-end `}' would
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
759 ;; be turned into `}}}' where only the first ends the comment
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
760 ;; and the rest becomes bogus junk.
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
761 (multi (not (and comment-quote-nested
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
762 ;; comment-end is a single char
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
763 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
764 (if (not (symbolp n))
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
765 (concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
766 ;; construct a regexp that would match anything from just S
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
767 ;; to any possible output of this function for any N.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
768 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
769 lpad "") ;padding is not required
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
770 (regexp-quote s)
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
771 (when multi "+") ;the last char of S might be repeated
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
772 (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
773 rpad "")))))) ;padding is not required
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
774
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
775 (defun comment-padleft (str &optional n)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
776 "Construct a string composed of `comment-padding' plus STR.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
777 It also adds N copies of the first non-whitespace chars of STR.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
778 If STR already contains padding, the corresponding amount is
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
779 ignored from `comment-padding'.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
780 N defaults to 0.
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
781 If N is `re', a regexp is returned instead, that would match
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
782 the string for any N."
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
783 (setq n (or n 0))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
784 (when (and (stringp str) (not (string= "" str)))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
785 ;; Only separate the left pad because we assume there is no right pad.
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
786 (string-match "\\`\\s-*" str)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
787 (let ((s (substring str (match-end 0)))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
788 (pad (concat (substring comment-padding
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
789 (min (- (match-end 0) (match-beginning 0))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
790 (length comment-padding)))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
791 (match-string 0 str)))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
792 (c (aref str (match-end 0))) ;the first non-space char of STR
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
793 ;; We can only duplicate C if the comment-end has multiple chars
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
794 ;; or if comments can be nested, else the comment-end `}' would
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
795 ;; be turned into `}}}' where only the first ends the comment
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
796 ;; and the rest becomes bogus junk.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
797 (multi (not (and comment-quote-nested
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
798 ;; comment-end is a single char
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
799 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
800 (if (not (symbolp n))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
801 (concat pad (when multi (make-string n c)) s)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
802 ;; Construct a regexp that would match anything from just S
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
803 ;; to any possible output of this function for any N.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
804 ;; We match any number of leading spaces because this regexp will
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
805 ;; be used for uncommenting where we might want to remove
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
806 ;; uncomment markers with arbitrary leading space (because
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
807 ;; they were aligned).
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
808 (concat "\\s-*"
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
809 (if multi (concat (regexp-quote (string c)) "*"))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
810 (regexp-quote s))))))
26646
0d447856a2f7 (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26645
diff changeset
811
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
812 ;;;###autoload
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
813 (defun uncomment-region (beg end &optional arg)
47370
408ecff0939b (uncomment-region): Be more careful with `='.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47212
diff changeset
814 "Uncomment each line in the BEG .. END region.
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
815 The numeric prefix ARG can specify a number of chars to remove from the
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
816 comment markers."
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
817 (interactive "*r\nP")
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
818 (comment-normalize-vars)
53988
043054d4d662 (uncomment-region): Allow eob as comment end.
Eli Zaretskii <eliz@is.elta.co.il>
parents: 53976
diff changeset
819 (when (> beg end) (setq beg (prog1 end (setq end beg))))
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
820 ;; Bind `comment-use-global-state' to nil. While uncommenting a region
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
821 ;; (which works a line at a time), a comment can appear to be
56915
12240bc21df6 (uncomment-region): Bind comment-use-global-state to nil.
Eli Zaretskii <eliz@gnu.org>
parents: 56143
diff changeset
822 ;; included in a mult-line string, but it is actually not.
12240bc21df6 (uncomment-region): Bind comment-use-global-state to nil.
Eli Zaretskii <eliz@gnu.org>
parents: 56143
diff changeset
823 (let ((comment-use-global-state nil))
12240bc21df6 (uncomment-region): Bind comment-use-global-state to nil.
Eli Zaretskii <eliz@gnu.org>
parents: 56143
diff changeset
824 (save-excursion
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
825 (funcall uncomment-region-function beg end arg))))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
826
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
827 (defun uncomment-region-default (beg end &optional arg)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
828 "Uncomment each line in the BEG .. END region.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
829 The numeric prefix ARG can specify a number of chars to remove from the
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
830 comment markers."
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
831 (goto-char beg)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
832 (setq end (copy-marker end))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
833 (let* ((numarg (prefix-numeric-value arg))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
834 (ccs comment-continue)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
835 (srei (comment-padright ccs 're))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
836 (csre (comment-padright comment-start 're))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
837 (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
838 spt)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
839 (while (and (< (point) end)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
840 (setq spt (comment-search-forward end t)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
841 (let ((ipt (point))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
842 ;; Find the end of the comment.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
843 (ept (progn
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
844 (goto-char spt)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
845 (unless (or (comment-forward)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
846 ;; Allow non-terminated comments.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
847 (eobp))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
848 (error "Can't find the comment end"))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
849 (point)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
850 (box nil)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
851 (box-equal nil)) ;Whether we might be using `=' for boxes.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
852 (save-restriction
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
853 (narrow-to-region spt ept)
72453
204e1538695a (comment-box): Call `comment-normalize-vars'. Add autoload cookie.
Juanma Barranquero <lekktu@gmail.com>
parents: 72410
diff changeset
854
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
855 ;; Remove the comment-start.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
856 (goto-char ipt)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
857 (skip-syntax-backward " ")
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
858 ;; A box-comment starts with a looong comment-start marker.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
859 (when (and (or (and (= (- (point) (point-min)) 1)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
860 (setq box-equal t)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
861 (looking-at "=\\{7\\}")
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
862 (not (eq (char-before (point-max)) ?\n))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
863 (skip-chars-forward "="))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
864 (> (- (point) (point-min) (length comment-start)) 7))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
865 (> (count-lines (point-min) (point-max)) 2))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
866 (setq box t))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
867 ;; Skip the padding. Padding can come from comment-padding and/or
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
868 ;; from comment-start, so we first check comment-start.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
869 (if (or (save-excursion (goto-char (point-min)) (looking-at csre))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
870 (looking-at (regexp-quote comment-padding)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
871 (goto-char (match-end 0)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
872 (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
873 (goto-char (match-end 0)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
874 (if (null arg) (delete-region (point-min) (point))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
875 (skip-syntax-backward " ")
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
876 (delete-char (- numarg))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
877 (unless (or (bobp)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
878 (save-excursion (goto-char (point-min))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
879 (looking-at comment-start-skip)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
880 ;; If there's something left but it doesn't look like
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
881 ;; a comment-start any more, just remove it.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
882 (delete-region (point-min) (point))))
72453
204e1538695a (comment-box): Call `comment-normalize-vars'. Add autoload cookie.
Juanma Barranquero <lekktu@gmail.com>
parents: 72410
diff changeset
883
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
884 ;; Remove the end-comment (and leading padding and such).
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
885 (goto-char (point-max)) (comment-enter-backward)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
886 ;; Check for special `=' used sometimes in comment-box.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
887 (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
888 (let ((pos (point)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
889 ;; skip `=' but only if there are at least 7.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
890 (when (> (skip-chars-backward "=") -7) (goto-char pos))))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
891 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
892 (when (and (bolp) (not (bobp))) (backward-char))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
893 (if (null arg) (delete-region (point) (point-max))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
894 (skip-syntax-forward " ")
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
895 (delete-char numarg)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
896 (unless (or (eobp) (looking-at comment-end-skip))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
897 ;; If there's something left but it doesn't look like
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
898 ;; a comment-end any more, just remove it.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
899 (delete-region (point) (point-max)))))
26647
b00a81cd0f6e (comment-find): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26646
diff changeset
900
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
901 ;; Unquote any nested end-comment.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
902 (comment-quote-nested comment-start comment-end t)
26646
0d447856a2f7 (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26645
diff changeset
903
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
904 ;; Eliminate continuation markers as well.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
905 (when sre
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
906 (let* ((cce (comment-string-reverse (or comment-continue
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
907 comment-start)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
908 (erei (and box (comment-padleft cce 're)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
909 (ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
910 (goto-char (point-min))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
911 (while (progn
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
912 (if (and ere (re-search-forward
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
913 ere (line-end-position) t))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
914 (replace-match "" t t nil (if (match-end 2) 2 1))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
915 (setq ere nil))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
916 (forward-line 1)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
917 (re-search-forward sre (line-end-position) t))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
918 (replace-match "" t t nil (if (match-end 2) 2 1)))))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
919 ;; Go to the end for the next comment.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
920 (goto-char (point-max))))))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
921 (set-marker end nil))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
922
26645
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
923 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
924 "Make the leading and trailing extra lines.
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
925 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
926 (let ((eindent 0))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
927 (if (not block)
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
928 ;; Try to match CS and CE's content so they align aesthetically.
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
929 (progn
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
930 (setq ce (comment-string-strip ce t t))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
931 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce "\n" cs))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
932 (setq eindent
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
933 (max (- (match-end 2) (match-beginning 2) (match-beginning 0))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
934 0))))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
935 ;; box comment
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
936 (let* ((width (- max-indent min-indent))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
937 (s (concat cs "a=m" cce))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
938 (e (concat ccs "a=m" ce))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
939 (c (if (string-match ".*\\S-\\S-" cs)
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
940 (aref cs (1- (match-end 0)))
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
941 (if (and (equal comment-end "") (string-match ".*\\S-" cs))
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
942 (aref cs (1- (match-end 0))) ?=)))
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
943 (re "\\s-*a=m\\s-*")
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
944 (_ (string-match re s))
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
945 (lcs (length cs))
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
946 (fill
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
947 (make-string (+ width (- (match-end 0)
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
948 (match-beginning 0) lcs 3)) c)))
26645
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
949 (setq cs (replace-match fill t t s))
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
950 (when (and (not (string-match comment-start-skip cs))
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
951 (string-match "a=m" s))
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
952 ;; The whitespace around CS cannot be ignored: put it back.
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
953 (setq re "a=m")
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
954 (setq fill (make-string (- width lcs) c))
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
955 (setq cs (replace-match fill t t s)))
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
956 (string-match re e)
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
957 (setq ce (replace-match fill t t e))))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
958 (cons (concat cs "\n" (make-string min-indent ? ) ccs)
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
959 (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce))))
26645
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
960
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
961 (defmacro comment-with-narrowing (beg end &rest body)
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
962 "Execute BODY with BEG..END narrowing.
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
963 Space is added (and then removed) at the beginning for the text's
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
964 indentation to be kept as it was before narrowing."
47467
c5035e184eba (comment-with-narrowing): Use the `declare' thingy.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47400
diff changeset
965 (declare (debug t) (indent 2))
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
966 (let ((bindent (make-symbol "bindent")))
50947
15c3c47aca50 (comment-indent): Try to align to adjacent comments.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50929
diff changeset
967 `(let ((,bindent (save-excursion (goto-char ,beg) (current-column))))
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
968 (save-restriction
50947
15c3c47aca50 (comment-indent): Try to align to adjacent comments.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50929
diff changeset
969 (narrow-to-region ,beg ,end)
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
970 (goto-char (point-min))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
971 (insert (make-string ,bindent ? ))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
972 (prog1
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
973 (progn ,@body)
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
974 ;; remove the bindent
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
975 (save-excursion
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
976 (goto-char (point-min))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
977 (when (looking-at " *")
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
978 (let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent)))
26645
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
979 (delete-char n)
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
980 (setq ,bindent (- ,bindent n))))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
981 (end-of-line)
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
982 (let ((e (point)))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
983 (beginning-of-line)
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
984 (while (and (> ,bindent 0) (re-search-forward " *" e t))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
985 (let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1))))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
986 (goto-char (match-beginning 0))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
987 (delete-char n)
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
988 (setq ,bindent (- ,bindent n)))))))))))
26645
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
989
70043
cec61a0fff33 (comment-add): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 68594
diff changeset
990 (defun comment-add (arg)
109817
012ee86d25c7 * lisp/newcomment.el (comment-add): Move comment to docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
991 "Compute the number of extra comment starter characters
012ee86d25c7 * lisp/newcomment.el (comment-add): Move comment to docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
992 \(extra semicolons in Lisp mode, extra stars in C mode, etc.)
012ee86d25c7 * lisp/newcomment.el (comment-add): Move comment to docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
993 If ARG is non-nil, just follow ARG.
012ee86d25c7 * lisp/newcomment.el (comment-add): Move comment to docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
994 If the comment starter is multi-char, just follow ARG.
012ee86d25c7 * lisp/newcomment.el (comment-add): Move comment to docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
995 Otherwise obey `comment-add'."
70043
cec61a0fff33 (comment-add): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 68594
diff changeset
996 (if (and (null arg) (= (string-match "[ \t]*\\'" comment-start) 1))
85665
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
997 (* comment-add 1)
70043
cec61a0fff33 (comment-add): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 68594
diff changeset
998 (1- (prefix-numeric-value arg))))
cec61a0fff33 (comment-add): New function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 68594
diff changeset
999
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1000 (defun comment-region-internal (beg end cs ce
56143
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1001 &optional ccs cce block lines indent)
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
1002 "Comment region BEG .. END.
56143
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1003 CS and CE are the comment start string and comment end string,
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1004 respectively. CCS and CCE are the comment continuation strings
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1005 for the start and end of lines, respectively (default to CS and CE).
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1006 BLOCK indicates that end of lines should be marked with either CCE,
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1007 CE or CS \(if CE is empty) and that those markers should be aligned.
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1008 LINES indicates that an extra lines will be used at the beginning
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1009 and end of the region for CE and CS.
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1010 INDENT indicates to put CS and CCS at the current indentation of
94e9e6f1f0b3 (comment-region-internal): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 56019
diff changeset
1011 the region rather than at left margin."
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1012 ;;(assert (< beg end))
51337
0237e2420453 (comment-empty-lines): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50947
diff changeset
1013 (let ((no-empty (not (or (eq comment-empty-lines t)
86771
e947df5d708b Martin Rudalics <rudalics at gmx.at>
Glenn Morris <rgm@gnu.org>
parents: 85905
diff changeset
1014 (and comment-empty-lines (zerop (length ce))))))
e947df5d708b Martin Rudalics <rudalics at gmx.at>
Glenn Morris <rgm@gnu.org>
parents: 85905
diff changeset
1015 ce-sanitized)
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1016 ;; Sanitize CE and CCE.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1017 (if (and (stringp ce) (string= "" ce)) (setq ce nil))
86771
e947df5d708b Martin Rudalics <rudalics at gmx.at>
Glenn Morris <rgm@gnu.org>
parents: 85905
diff changeset
1018 (setq ce-sanitized ce)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1019 (if (and (stringp cce) (string= "" cce)) (setq cce nil))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1020 ;; If CE is empty, multiline cannot be used.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1021 (unless ce (setq ccs nil cce nil))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1022 ;; Should we mark empty lines as well ?
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1023 (if (or ccs block lines) (setq no-empty nil))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1024 ;; Make sure we have end-markers for BLOCK mode.
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1025 (when block (unless ce (setq ce (comment-string-reverse cs))))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1026 ;; If BLOCK is not requested, we don't need CCE.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1027 (unless block (setq cce nil))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1028 ;; Continuation defaults to the same as CS and CE.
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1029 (unless ccs (setq ccs cs cce ce))
49597
e88404e8f2cf Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 48257
diff changeset
1030
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1031 (save-excursion
26645
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
1032 (goto-char end)
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1033 ;; If the end is not at the end of a line and the comment-end
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1034 ;; is implicit (i.e. a newline), explicitly insert a newline.
86771
e947df5d708b Martin Rudalics <rudalics at gmx.at>
Glenn Morris <rgm@gnu.org>
parents: 85905
diff changeset
1035 (unless (or ce-sanitized (eolp)) (insert "\n") (indent-according-to-mode))
26645
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
1036 (comment-with-narrowing beg end
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1037 (let ((min-indent (point-max))
39864
71f93e410115 (comment-region-internal): Don't rebind
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39761
diff changeset
1038 (max-indent 0))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1039 (goto-char (point-min))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1040 ;; Quote any nested comment marker
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1041 (comment-quote-nested comment-start comment-end nil)
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1042
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1043 ;; Loop over all lines to find the needed indentations.
29066
720287a2312f (comment-region-internal): Go back to BEG after quoting
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29037
diff changeset
1044 (goto-char (point-min))
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1045 (while
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1046 (progn
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1047 (unless (looking-at "[ \t]*$")
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1048 (setq min-indent (min min-indent (current-indentation))))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1049 (end-of-line)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1050 (setq max-indent (max max-indent (current-column)))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1051 (not (or (eobp) (progn (forward-line) nil)))))
49597
e88404e8f2cf Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 48257
diff changeset
1052
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1053 (setq max-indent
66712
13b5bb686732 (comment-region-internal): Box more tightly in the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 64861
diff changeset
1054 (+ max-indent (max (length cs) (length ccs))
13b5bb686732 (comment-region-internal): Box more tightly in the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 64861
diff changeset
1055 ;; Inserting ccs can change max-indent by (1- tab-width)
13b5bb686732 (comment-region-internal): Box more tightly in the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 64861
diff changeset
1056 ;; but only if there are TABs in the boxed text, of course.
13b5bb686732 (comment-region-internal): Box more tightly in the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 64861
diff changeset
1057 (if (save-excursion (goto-char beg)
13b5bb686732 (comment-region-internal): Box more tightly in the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 64861
diff changeset
1058 (search-forward "\t" end t))
13b5bb686732 (comment-region-internal): Box more tightly in the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 64861
diff changeset
1059 (1- tab-width) 0)))
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1060 (unless indent (setq min-indent 0))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1061
26645
39f87d842e57 (comment-make-extra-lines): Moved out of comment-region-internal.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26640
diff changeset
1062 ;; make the leading and trailing lines if requested
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1063 (when lines
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1064 (let ((csce
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1065 (comment-make-extra-lines
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1066 cs ce ccs cce min-indent max-indent block)))
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1067 (setq cs (car csce))
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1068 (setq ce (cdr csce))))
49597
e88404e8f2cf Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 48257
diff changeset
1069
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1070 (goto-char (point-min))
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1071 ;; Loop over all lines from BEG to END.
28904
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1072 (while
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1073 (progn
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1074 (unless (and no-empty (looking-at "[ \t]*$"))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1075 (move-to-column min-indent t)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1076 (insert cs) (setq cs ccs) ;switch to CCS after the first line
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1077 (end-of-line)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1078 (if (eobp) (setq cce ce))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1079 (when cce
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1080 (when block (move-to-column max-indent t))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1081 (insert cce)))
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1082 (end-of-line)
8d91ded5215c (comment-start, comment-start-skip, comment-end): Made `defvar'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28901
diff changeset
1083 (not (or (eobp) (progn (forward-line) nil))))))))))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1084
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
1085 ;;;###autoload
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1086 (defun comment-region (beg end &optional arg)
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1087 "Comment or uncomment each line in the region.
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
1088 With just \\[universal-argument] prefix arg, uncomment each line in region BEG .. END.
56019
0517f0b254be (comment-search-forward, comment-search-backward): Fix typos.
Juanma Barranquero <lekktu@gmail.com>
parents: 54314
diff changeset
1089 Numeric prefix ARG means use ARG comment characters.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1090 If ARG is negative, delete that many comment characters instead.
103292
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1091
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1092 The strings used as comment starts are built from `comment-start'
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1093 and `comment-padding'; the strings used as comment ends are built
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1094 from `comment-end' and `comment-padding'.
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1095
103292
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1096 By default, the `comment-start' markers are inserted at the
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1097 current indentation of the region, and comments are terminated on
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1098 each line (even for syntaxes in which newline does not end the
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1099 comment and blank lines do not get comments). This can be
0d056af7a0af * newcomment.el (comment-region): Doc fix (Bug#3370).
Chong Yidong <cyd@stupidchicken.com>
parents: 100908
diff changeset
1100 changed with `comment-style'."
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1101 (interactive "*r\nP")
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1102 (comment-normalize-vars)
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1103 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1104 (save-excursion
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1105 ;; FIXME: maybe we should call uncomment depending on ARG.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1106 (funcall comment-region-function beg end arg)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1107
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1108 (defun comment-region-default (beg end &optional arg)
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1109 (let* ((numarg (prefix-numeric-value arg))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1110 (style (cdr (assoc comment-style comment-styles)))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1111 (lines (nth 2 style))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1112 (block (nth 1 style))
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1113 (multi (nth 0 style)))
78909
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
1114
545574ac320d (comment-choose-indent): New function extracted
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 78236
diff changeset
1115 ;; We use `chars' instead of `syntax' because `\n' might be
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1116 ;; of end-comment syntax rather than of whitespace syntax.
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1117 ;; sanitize BEG and END
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1118 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1119 (setq beg (max beg (point)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1120 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1121 (setq end (min end (point)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1122 (if (>= beg end) (error "Nothing to comment"))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1123
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1124 ;; sanitize LINES
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1125 (setq lines
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1126 (and
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1127 lines ;; multi
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1128 (progn (goto-char beg) (beginning-of-line)
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1129 (skip-syntax-forward " ")
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1130 (>= (point) beg))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1131 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1132 (<= (point) end))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1133 (or block (not (string= "" comment-end)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1134 (or block (progn (goto-char beg) (search-forward "\n" end t)))))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1135
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1136 ;; don't add end-markers just because the user asked for `block'
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1137 (unless (or lines (string= "" comment-end)) (setq block nil))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1138
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1139 (cond
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1140 ((consp arg) (uncomment-region beg end))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1141 ((< numarg 0) (uncomment-region beg end (- numarg)))
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1142 (t
85665
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1143 (let ((multi-char (/= (string-match "[ \t]*\\'" comment-start) 1))
87366
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1144 indent triple)
85665
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1145 (if (eq (nth 3 style) 'multi-char)
87366
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1146 (save-excursion
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1147 (goto-char beg)
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1148 (setq indent multi-char
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1149 ;; Triple if we will put the comment starter at the margin
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1150 ;; and the first line of the region isn't indented
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1151 ;; at least two spaces.
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1152 triple (and (not multi-char) (looking-at "\t\\| "))))
85665
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1153 (setq indent (nth 3 style)))
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1154
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1155 ;; In Lisp and similar modes with one-character comment starters,
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1156 ;; double it by default if `comment-add' says so.
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1157 ;; If it isn't indented, triple it.
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1158 (if (and (null arg) (not multi-char))
87366
682c0e86fe6b (comment-region-default): Don't triple the
Richard M. Stallman <rms@gnu.org>
parents: 86771
diff changeset
1159 (setq numarg (* comment-add (if triple 2 1)))
85665
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1160 (setq numarg (1- (prefix-numeric-value arg))))
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1161
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1162 (comment-region-internal
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1163 beg end
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1164 (let ((s (comment-padright comment-start numarg)))
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1165 (if (string-match comment-start-skip s) s
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1166 (comment-padright comment-start)))
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1167 (let ((s (comment-padleft comment-end numarg)))
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1168 (and s (if (string-match comment-end-skip s) s
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1169 (comment-padright comment-end))))
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1170 (if multi (comment-padright comment-continue numarg))
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1171 (if multi
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1172 (comment-padleft (comment-string-reverse comment-continue) numarg))
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1173 block
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1174 lines
2a0d98e5c9f2 (comment-styles): New style indent-or-triple.
Richard M. Stallman <rms@gnu.org>
parents: 84679
diff changeset
1175 indent))))))
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1176
72453
204e1538695a (comment-box): Call `comment-normalize-vars'. Add autoload cookie.
Juanma Barranquero <lekktu@gmail.com>
parents: 72410
diff changeset
1177 ;;;###autoload
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1178 (defun comment-box (beg end &optional arg)
47212
5ddbbad82052 (comment-make-extra-lines): Only use the
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 46870
diff changeset
1179 "Comment out the BEG .. END region, putting it inside a box.
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1180 The numeric prefix ARG specifies how many characters to add to begin- and
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1181 end- comment markers additionally to what `comment-add' already specifies."
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1182 (interactive "*r\np")
72453
204e1538695a (comment-box): Call `comment-normalize-vars'. Add autoload cookie.
Juanma Barranquero <lekktu@gmail.com>
parents: 72410
diff changeset
1183 (comment-normalize-vars)
29037
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
1184 (let ((comment-style (if (cadr (assoc comment-style comment-styles))
238233843fc1 (comment-styles): New `box-multi'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28990
diff changeset
1185 'box-multi 'box)))
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1186 (comment-region beg end (+ comment-add arg))))
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1187
44999
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1188
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1189 ;;;###autoload
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1190 (defun comment-or-uncomment-region (beg end &optional arg)
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1191 "Call `comment-region', unless the region only consists of comments,
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1192 in which case call `uncomment-region'. If a prefix arg is given, it
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1193 is passed on to the respective function."
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1194 (interactive "*r\nP")
50843
bc3a5a6b96ab (comment-set-column, comment-kill, comment-or-uncomment-region):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50839
diff changeset
1195 (comment-normalize-vars)
44999
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1196 (funcall (if (save-excursion ;; check for already commented region
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1197 (goto-char beg)
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1198 (comment-forward (point-max))
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1199 (<= end (point)))
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1200 'uncomment-region 'comment-region)
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1201 beg end arg))
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1202
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
1203 ;;;###autoload
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1204 (defun comment-dwim (arg)
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
1205 "Call the comment command you want (Do What I Mean).
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
1206 If the region is active and `transient-mark-mode' is on, call
111081
26c658c72bf6 * newcomment.el (comment-dwim): Fix the intentation in the doc string.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents: 110533
diff changeset
1207 `comment-region' (unless it only consists of comments, in which
26c658c72bf6 * newcomment.el (comment-dwim): Fix the intentation in the doc string.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents: 110533
diff changeset
1208 case it calls `uncomment-region').
85905
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1209 Else, if the current line is empty, call `comment-insert-comment-function'
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1210 if it is defined, otherwise insert a comment and indent it.
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
1211 Else if a prefix ARG is specified, call `comment-kill'.
68594
ca4201c7597d Docstring fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 67057
diff changeset
1212 Else, call `comment-indent'.
ca4201c7597d Docstring fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 67057
diff changeset
1213 You can configure `comment-style' to change the way regions are commented."
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1214 (interactive "*P")
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1215 (comment-normalize-vars)
28901
0e7bbb764f47 (comment-use-syntax): Change `maybe' to `undecided'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26763
diff changeset
1216 (if (and mark-active transient-mark-mode)
44999
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1217 (comment-or-uncomment-region (region-beginning) (region-end) arg)
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1218 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
1219 ;; FIXME: If there's no comment to kill on this line and ARG is
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
1220 ;; specified, calling comment-kill is not very clever.
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
1221 (if arg (comment-kill (and (integerp arg) arg)) (comment-indent))
85905
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1222 ;; Inserting a comment on a blank line. comment-indent calls
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1223 ;; c-i-c-f if needed in the non-blank case.
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1224 (if comment-insert-comment-function
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1225 (funcall comment-insert-comment-function)
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1226 (let ((add (comment-add arg)))
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1227 ;; Some modes insist on keeping column 0 comment in column 0
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1228 ;; so we need to move away from it before inserting the comment.
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1229 (indent-according-to-mode)
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1230 (insert (comment-padright comment-start add))
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1231 (save-excursion
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1232 (unless (string= "" comment-end)
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1233 (insert (comment-padleft comment-end add)))
7100260421ed (comment-dwim): Call comment-insert-comment-function, if defined, for
Glenn Morris <rgm@gnu.org>
parents: 85895
diff changeset
1234 (indent-according-to-mode)))))))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1235
57526
d0bca414ad07 (comment-auto-fill-only-comments): Add autoload.
Richard M. Stallman <rms@gnu.org>
parents: 57301
diff changeset
1236 ;;;###autoload
28961
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1237 (defcustom comment-auto-fill-only-comments nil
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1238 "Non-nil means to only auto-fill inside comments.
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1239 This has no effect in modes that do not define a comment syntax."
62531
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
1240 :type 'boolean
c905fcf5e3d9 Specify missing group (and type, if simple) in defcustom.
Juanma Barranquero <lekktu@gmail.com>
parents: 57526
diff changeset
1241 :group 'comment)
28961
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1242
50947
15c3c47aca50 (comment-indent): Try to align to adjacent comments.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 50929
diff changeset
1243 (defun comment-valid-prefix-p (prefix compos)
73317
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1244 "Check that the adaptive-fill-prefix is consistent with the context.
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1245 PREFIX is the prefix (presumably guessed by `adaptive-fill-mode').
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1246 COMPOS is the position of the beginning of the comment we're in, or nil
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1247 if we're not inside a comment."
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1248 ;; This consistency checking is mostly needed to workaround the limitation
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1249 ;; of auto-fill-mode whose paragraph-determination doesn't pay attention
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1250 ;; to comment boundaries.
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1251 (if (null compos)
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1252 ;; We're not inside a comment: the prefix shouldn't match
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1253 ;; a comment-starter.
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1254 (not (and comment-start comment-start-skip
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1255 (string-match comment-start-skip prefix)))
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1256 (or
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1257 ;; Accept any prefix if the current comment is not EOL-terminated.
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1258 (save-excursion (goto-char compos) (comment-forward) (not (bolp)))
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1259 ;; Accept any prefix that starts with the same comment-start marker
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1260 ;; as the current one.
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1261 (when (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)")
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1262 prefix)
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1263 (let ((prefix-com (comment-string-strip (match-string 0 prefix) nil t)))
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1264 (string-match "\\`[ \t]*" prefix-com)
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1265 (let* ((prefix-space (match-string 0 prefix-com))
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1266 (prefix-indent (string-width prefix-space))
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1267 (prefix-comstart (substring prefix-com (match-end 0))))
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1268 (save-excursion
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1269 (goto-char compos)
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1270 ;; The comstart marker is the same.
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1271 (and (looking-at (regexp-quote prefix-comstart))
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1272 ;; The indentation as well.
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1273 (or (= prefix-indent
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1274 (- (current-column) (current-left-margin)))
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1275 ;; Check the indentation in two different ways, just
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1276 ;; to try and avoid most of the potential funny cases.
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1277 (equal prefix-space
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1278 (buffer-substring (point)
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1279 (progn (move-to-left-margin)
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1280 (point)))))))))))))
105372
bd2966850aac Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents: 103292
diff changeset
1281
39761
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1282
29215
21c457830b9d Add abundant autoload cookies.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29119
diff changeset
1283 ;;;###autoload
28949
856f1364a955 (comment-string-strip): Strip terminating newlines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28904
diff changeset
1284 (defun comment-indent-new-line (&optional soft)
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1285 "Break line at point and indent, continuing comment if within one.
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1286 This indents the body of the continued comment
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1287 under the previous comment line.
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1288
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1289 This command is intended for styles where you write a comment per line,
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1290 starting a new comment (and terminating it if necessary) on each line.
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1291 If you want to continue one comment across several lines, use \\[newline-and-indent].
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1292
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1293 If a fill column is specified, it overrides the use of the comment column
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1294 or comment indentation.
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1295
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1296 The inserted newline is marked hard if variable `use-hard-newlines' is true,
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1297 unless optional argument SOFT is non-nil."
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1298 (interactive)
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1299 (comment-normalize-vars t)
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1300 (let (compos comin)
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1301 ;; If we are not inside a comment and we only auto-fill comments,
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1302 ;; don't do anything (unless no comment syntax is defined).
28961
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1303 (unless (and comment-start
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1304 comment-auto-fill-only-comments
105372
bd2966850aac Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents: 103292
diff changeset
1305 (not (called-interactively-p 'interactive))
28961
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1306 (not (save-excursion
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1307 (prog1 (setq compos (comment-beginning))
28961
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1308 (setq comin (point))))))
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1309
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1310 ;; Now we know we should auto-fill.
44999
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1311 ;; Insert the newline before removing empty space so that markers
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1312 ;; get preserved better.
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1313 (if soft (insert-and-inherit ?\n) (newline 1))
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1314 (save-excursion (forward-char -1) (delete-horizontal-space))
34459
1007fb1b5df9 (comment-indent-new-line): Use delete-horizontal-space
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 34301
diff changeset
1315 (delete-horizontal-space)
44999
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1316
39761
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1317 (if (and fill-prefix (not adaptive-fill-mode))
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1318 ;; Blindly trust a non-adaptive fill-prefix.
28961
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1319 (progn
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1320 (indent-to-left-margin)
44999
a8d9e35048ff (comment-fill-column): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 44455
diff changeset
1321 (insert-before-markers-and-inherit fill-prefix))
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1322
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1323 ;; If necessary check whether we're inside a comment.
41253
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1324 (unless (or compos (null comment-start))
28961
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1325 (save-excursion
8092e0d9d8b9 (comment-beginning): Handle unclosed comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28949
diff changeset
1326 (backward-char)
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1327 (setq compos (comment-beginning))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1328 (setq comin (point))))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1329
39761
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1330 (cond
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1331 ;; If there's an adaptive prefix, use it unless we're inside
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1332 ;; a comment and the prefix is not a comment starter.
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1333 ((and fill-prefix
73317
a8f3a59e04e0 (comment-valid-prefix-p): Make the check more thorough.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72453
diff changeset
1334 (comment-valid-prefix-p fill-prefix compos))
39761
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1335 (indent-to-left-margin)
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1336 (insert-and-inherit fill-prefix))
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1337 ;; If we're not inside a comment, just try to indent.
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1338 ((not compos) (indent-according-to-mode))
1cf8ddfb831f (comment-indent): Be a little more robust in case
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39486
diff changeset
1339 (t
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1340 (let* ((comment-column
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1341 ;; The continuation indentation should be somewhere between
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1342 ;; the current line's indentation (plus 2 for good measure)
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1343 ;; and the current comment's indentation, with a preference
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1344 ;; for comment-column.
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1345 (save-excursion
41253
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1346 ;; FIXME: use prev line's info rather than first line's.
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1347 (goto-char compos)
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1348 (min (current-column) (max comment-column
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1349 (+ 2 (current-indentation))))))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1350 (comstart (buffer-substring compos comin))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1351 (normalp
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1352 (string-match (regexp-quote (comment-string-strip
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1353 comment-start t t))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1354 comstart))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1355 (comment-end
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1356 (if normalp comment-end
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1357 ;; The comment starter is not the normal comment-start
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1358 ;; so we can't just use comment-end.
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1359 (save-excursion
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1360 (goto-char compos)
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1361 (if (not (comment-forward)) comment-end
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1362 (comment-string-strip
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1363 (buffer-substring
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1364 (save-excursion (comment-enter-backward) (point))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1365 (point))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1366 nil t)))))
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1367 (comment-start comstart)
41253
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1368 (continuep (or comment-multi-line
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1369 (cadr (assoc comment-style comment-styles))))
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1370 ;; Force comment-continue to be recreated from comment-start.
38898
29a1feb5e1fe (comment-beginning): Make sure the comment-start
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 36490
diff changeset
1371 ;; FIXME: wrong if comment-continue was set explicitly!
41253
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1372 ;; FIXME: use prev line's continuation if available.
28990
80e6f0d6eac1 Fix license text and author.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28961
diff changeset
1373 (comment-continue nil))
41253
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1374 (if (and comment-multi-line (> (length comment-end) 0))
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1375 (indent-according-to-mode)
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1376 (insert-and-inherit ?\n)
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1377 (forward-char -1)
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1378 (comment-indent continuep)
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1379 (save-excursion
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1380 (let ((pt (point)))
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1381 (end-of-line)
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1382 (let ((comend (buffer-substring pt (point))))
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1383 ;; The 1+ is to make sure we delete the \n inserted above.
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1384 (delete-region pt (1+ (point)))
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1385 (end-of-line 0)
87ae7a37e076 (comment-indent): Fix misindentation for comment-only lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41021
diff changeset
1386 (insert comend))))))))))))
26663
9979145a424e (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 26647
diff changeset
1387
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1388 (provide 'newcomment)
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1389
56957
56009bee8238 (uncomment-region-default, comment-region-default):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 56915
diff changeset
1390 ;; arch-tag: 01e3320a-00c8-44ea-a696-8f8e7354c858
26640
f95236e53857 First "working" version:
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1391 ;;; newcomment.el ends here