annotate lisp/newcomment.el @ 5402:308d34e9f07d

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