annotate lisp/newcomment.el @ 2885:6caa3d30f19f

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