annotate lisp/newcomment.el @ 5067:7d7ae8db0341

add functions `stable-union' and `stable-intersection' to do stable set operations -------------------- ChangeLog entries follow: -------------------- lisp/ChangeLog addition: 2010-02-22 Ben Wing <ben@xemacs.org> * cl-seq.el: * cl-seq.el (stable-union): New. * cl-seq.el (stable-intersection): New. New functions to do stable set operations, i.e. preserve the order of the elements in the argument lists, and prefer LIST1 over LIST2 when ordering the combined result. The result looks as much like LIST1 as possible, followed (in the case of `stable-union') by any necessary elements from LIST2, in order. This is contrary to `union' and `intersection', which are not required to be order- preserving and are not -- they prefer LIST2 and output results in backwards order.
author Ben Wing <ben@xemacs.org>
date Mon, 22 Feb 2010 21:23:02 -0600
parents 1e2fc51563a5
children f00192e1cd49 308d34e9f07d
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)
4435
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
243 ;; (kill-local-variable 'comment-continue))
2511
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))))
4435
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
446 ; (and comment-start-skip
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
447 ; (re-search-forward comment-start-skip eol t)
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
448 ; (setq eol (match-beginning 0)))
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
449 ; (goto-char eol)
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
450 ; (skip-chars-backward " \t")
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
451 ; (max comment-column (1+ (current-column))))))
1333
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).
4435
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
501 ;; ... but unless we're at the beginning of a line -- dvl
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
502 (unless (or (bolp) (eq ?\ (char-syntax (char-before))))
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
503 (insert ?\ ))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
504 (insert starter)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
505 (setq cpos (point-marker))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
506 (insert ender)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
507 (goto-char begpos)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
508 ;; Compute desired indent.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
509 (setq indent (save-excursion (funcall comment-indent-function)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
510 (if (not indent)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
511 ;; comment-indent-function refuses: delegate to indent.
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
512 (indent-according-to-mode)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
513 ;; Avoid moving comments past the fill-column.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
514 (unless (save-excursion (skip-chars-backward " \t") (bolp))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
515 (setq indent
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
516 (min indent
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
517 (+ (current-column)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
518 (- (or comment-fill-column fill-column)
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
519 (save-excursion (end-of-line) (current-column)))))))
4435
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
520 ;; XEmacs change: Preserve indentation of comments starting in
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
521 ;; column 0, as documented.
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
522 (unless (or (= (current-column) 0) (= (current-column) indent))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
523 ;; 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
524 (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
525 (indent-to (if (bolp) indent
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
526 (max indent (1+ (current-column)))))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
527 (goto-char cpos)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
528 (set-marker cpos nil))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
529
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
530 ;;;###autoload
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
531 (defun comment-set-column (arg)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
532 "Set the comment column based on point.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
533 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
534 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
535 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
536 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
537 (interactive "P")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
538 (cond
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
539 ((eq arg '-) (comment-kill nil))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
540 (arg
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
541 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
542 (beginning-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
543 (comment-search-backward)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
544 (beginning-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
545 (goto-char (comment-search-forward (line-end-position)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
546 (setq comment-column (current-column))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
547 (lmessage 'command "Comment column set to %d" comment-column))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
548 (comment-indent))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
549 (t (setq comment-column (current-column))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
550 (lmessage 'command "Comment column set to %d" comment-column))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
551
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
552 ;;;###autoload
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
553 (defun comment-kill (arg)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
554 "Kill the comment on this line, if any.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
555 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
556 ;; XEmacs change: add *
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
557 (interactive "*P")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
558 (dotimes (_ (prefix-numeric-value arg))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
559 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
560 (beginning-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
561 (let ((cs (comment-search-forward (line-end-position) t)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
562 (when cs
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
563 (goto-char cs)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
564 (skip-syntax-backward " ")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
565 (setq cs (point))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
566 (comment-forward)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
567 (kill-region cs (if (bolp) (1- (point)) (point)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
568 (indent-according-to-mode))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
569 (if arg (forward-line 1))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
570
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
571 (defun comment-padright (str &optional n)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
572 "Construct a string composed of STR plus `comment-padding'.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
573 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
574 If STR already contains padding, the corresponding amount is
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
575 ignored from `comment-padding'.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
576 N defaults to 0.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
577 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
578 the string for any N."
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
579 (setq n (or n 0))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
580 (when (and (stringp str) (not (string= "" str)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
581 ;; Separate the actual string from any leading/trailing padding
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
582 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
583 (let ((s (match-string 1 str)) ;actual string
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
584 (lpad (substring str 0 (match-beginning 1))) ;left padding
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
585 (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
586 (substring comment-padding ;additional right padding
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
587 (min (- (match-end 0) (match-end 1))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
588 (length comment-padding)))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
589 ;; 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
590 ;; 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
591 ;; 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
592 ;; and the rest becomes bogus junk.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
593 (multi (not (and comment-quote-nested
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
594 ;; comment-end is a single char
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
595 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
596 (if (not (symbolp n))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
597 (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
598 ;; 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
599 ;; 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
600 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
601 lpad "") ;padding is not required
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
602 (regexp-quote s)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
603 (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
604 (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
605 rpad "")))))) ;padding is not required
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
606
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
607 (defun comment-padleft (str &optional n)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
608 "Construct a string composed of `comment-padding' plus STR.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
609 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
610 If STR already contains padding, the corresponding amount is
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
611 ignored from `comment-padding'.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
612 N defaults to 0.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
613 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
614 the string for any N."
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
615 (setq n (or n 0))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
616 (when (and (stringp str) (not (string= "" str)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
617 ;; 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
618 (string-match "\\`\\s-*" str)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
619 (let ((s (substring str (match-end 0)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
620 (pad (concat (substring comment-padding
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
621 (min (- (match-end 0) (match-beginning 0))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
622 (length comment-padding)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
623 (match-string 0 str)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
624 (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
625 ;; 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
626 ;; 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
627 ;; 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
628 ;; and the rest becomes bogus junk.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
629 (multi (not (and comment-quote-nested
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
630 ;; comment-end is a single char
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
631 (string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
632 (if (not (symbolp n))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
633 (concat pad (when multi (make-string n c)) s)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
634 ;; 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
635 ;; 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
636 ;; 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
637 ;; 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
638 ;; uncomment markers with arbitrary leading space (because
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
639 ;; they were aligned).
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
640 (concat "\\s-*"
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
641 (if multi (concat (regexp-quote (string c)) "*"))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
642 (regexp-quote s))))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
643
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
644 ;;;###autoload
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
645 (defun uncomment-region (beg end &optional arg)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
646 "Uncomment each line in the BEG .. END region.
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
647 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
648 comment markers."
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
649 (interactive "*r\nP")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
650 (comment-normalize-vars)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
651 (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
652 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
653 (goto-char beg)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
654 (setq end (copy-marker end))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
655
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
656 (let* ((numarg (prefix-numeric-value arg))
4435
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
657 (ccs comment-continue)
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
658 (srei (comment-padright ccs 're))
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
659 (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
660 spt)
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
661 (while (and (< (point) end)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
662 (setq spt (comment-search-forward end t)))
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
663 (let ((ipt (point))
4435
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
664 ;; Find the end of the comment.
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
665 (ept (progn
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
666 (goto-char spt)
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
667 (unless (comment-forward)
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
668 (error "Can't find the comment end"))
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
669 (point)))
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
670 (box nil)
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
671 (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
672 (save-restriction
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
673 (narrow-to-region spt ept)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
674
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
675 ;; Remove the comment-start.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
676 (goto-char ipt)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
677 (skip-syntax-backward " ")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
678 ;; 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
679 (when (and (or (and (= (- (point) (point-min)) 1)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
680 (setq box-equal t)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
681 (looking-at "=\\{7\\}")
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
682 (not (eq (char-before (point-max)) ?\n))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
683 (skip-chars-forward "="))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
684 (> (- (point) (point-min) (length comment-start)) 7))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
685 (> (count-lines (point-min) (point-max)) 2))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
686 (setq box t))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
687 (when (looking-at (regexp-quote comment-padding))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
688 (goto-char (match-end 0)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
689 (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
690 (goto-char (match-end 0)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
691 (if (null arg) (delete-region (point-min) (point))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
692 (skip-syntax-backward " ")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
693 (delete-char (- numarg)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
694
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
695 ;; Remove the end-comment (and leading padding and such).
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
696 (goto-char (point-max)) (comment-enter-backward)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
697 ;; 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
698 (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
699 (let ((pos (point)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
700 ;; skip `=' but only if there are at least 7.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
701 (when (> (skip-chars-backward "=") -7) (goto-char pos))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
702 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
703 (when (and (bolp) (not (bobp))) (backward-char))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
704 (if (null arg) (delete-region (point) (point-max))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
705 (skip-syntax-forward " ")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
706 (delete-char numarg)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
707
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
708 ;; Unquote any nested end-comment.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
709 (comment-quote-nested comment-start comment-end t)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
710
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
711 ;; Eliminate continuation markers as well.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
712 (when sre
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
713 (let* ((cce (comment-string-reverse (or comment-continue
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
714 comment-start)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
715 (erei (and box (comment-padleft cce 're)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
716 (ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
717 (goto-char (point-min))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
718 (while (progn
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
719 (if (and ere (re-search-forward
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
720 ere (line-end-position) t))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
721 (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
722 (setq ere nil))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
723 (forward-line 1)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
724 (re-search-forward sre (line-end-position) t))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
725 (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
726 ;; Go to the end for the next comment.
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
727 (goto-char (point-max)))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
728 (set-marker end nil))))
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 (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
731 "Make the leading and trailing extra lines.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
732 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
733 (let ((eindent 0))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
734 (if (not block)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
735 ;; 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
736 (progn
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
737 (setq ce (comment-string-strip ce t t))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
738 (when (string-match "\\(.+\\).*\n\\(.*?\\)\\1" (concat ce "\n" cs))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
739 (setq eindent
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
740 (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
741 0))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
742 ;; box comment
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
743 (let* ((width (- max-indent min-indent))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
744 (s (concat cs "a=m" cce))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
745 (e (concat ccs "a=m" ce))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
746 (c (if (string-match ".*\\S-\\S-" cs)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
747 (aref cs (1- (match-end 0)))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
748 (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
749 (aref cs (1- (match-end 0))) ?=)))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
750 (re "\\s-*a=m\\s-*")
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
751 ; Huh? (_ (string-match re s))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
752 (lcs (length cs))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
753 (fill
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
754 (make-string (+ width (- (match-end 0)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
755 (match-beginning 0) lcs 3)) c)))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
756 (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
757 (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
758 (string-match "a=m" s))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
759 ;; 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
760 (setq re "a=m")
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
761 (setq fill (make-string (- width lcs) c))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
762 (setq cs (replace-match fill t t s)))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
763 (string-match re e)
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
764 (setq ce (replace-match fill t t e))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
765 (cons (concat cs "\n" (make-string min-indent ? ) ccs)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
766 (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
767
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
768 (defmacro comment-with-narrowing (beg end &rest body)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
769 "Execute BODY with BEG..END narrowing.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
770 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
771 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
772 (declare (debug t) (indent 2))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
773 (let ((bindent (make-symbol "bindent")))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
774 `(let ((,bindent (save-excursion (goto-char beg) (current-column))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
775 (save-restriction
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
776 (narrow-to-region beg end)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
777 (goto-char (point-min))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
778 (insert (make-string ,bindent ? ))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
779 (prog1
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
780 (progn ,@body)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
781 ;; remove the bindent
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
782 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
783 (goto-char (point-min))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
784 (when (looking-at " *")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
785 (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
786 (delete-char n)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
787 (setq ,bindent (- ,bindent n))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
788 (end-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
789 (let ((e (point)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
790 (beginning-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
791 (while (and (> ,bindent 0) (re-search-forward " *" e t))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
792 (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
793 (goto-char (match-beginning 0))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
794 (delete-char n)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
795 (setq ,bindent (- ,bindent n)))))))))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
796
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
797 (defun comment-region-internal (beg end cs ce
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
798 &optional ccs cce block lines indent)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
799 "Comment region BEG .. END.
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
800 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
801 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
802 of lines (default to CS and CE).
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
803 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
804 \(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
805 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
806 of the region for CE and CS.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
807 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
808 rather than at left margin."
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
809 ;;(assert (< beg end))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
810 (let ((no-empty t))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
811 ;; Sanitize CE and CCE.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
812 (if (and (stringp ce) (string= "" ce)) (setq ce nil))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
813 (if (and (stringp cce) (string= "" cce)) (setq cce nil))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
814 ;; If CE is empty, multiline cannot be used.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
815 (unless ce (setq ccs nil cce nil))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
816 ;; Should we mark empty lines as well ?
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
817 (if (or ccs block lines) (setq no-empty nil))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
818 ;; Make sure we have end-markers for BLOCK mode.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
819 (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
820 ;; 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
821 (unless block (setq cce nil))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
822 ;; Continuation defaults to the same as CS and CE.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
823 (unless ccs (setq ccs cs cce ce))
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
824
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
825 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
826 (goto-char end)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
827 ;; 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
828 ;; 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
829 (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
830 (comment-with-narrowing beg end
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
831 (let ((min-indent (point-max))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
832 (max-indent 0))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
833 (goto-char (point-min))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
834 ;; Quote any nested comment marker
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
835 (comment-quote-nested comment-start comment-end nil)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
836
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
837 ;; Loop over all lines to find the needed indentations.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
838 (goto-char (point-min))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
839 (while
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
840 (progn
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
841 (unless (looking-at "[ \t]*$")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
842 (setq min-indent (min min-indent (current-indentation))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
843 (end-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
844 (setq max-indent (max max-indent (current-column)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
845 (not (or (eobp) (progn (forward-line) nil)))))
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
846
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
847 ;; 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
848 (setq max-indent
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
849 (+ 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
850 (unless indent (setq min-indent 0))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
851
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
852 ;; make the leading and trailing lines if requested
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
853 (when lines
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
854 (let ((csce
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
855 (comment-make-extra-lines
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
856 cs ce ccs cce min-indent max-indent block)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
857 (setq cs (car csce))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
858 (setq ce (cdr csce))))
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
859
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
860 (goto-char (point-min))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
861 ;; Loop over all lines from BEG to END.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
862 (while
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
863 (progn
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
864 (unless (and no-empty (looking-at "[ \t]*$"))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
865 (move-to-column min-indent t)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
866 (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
867 (end-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
868 (if (eobp) (setq cce ce))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
869 (when cce
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
870 (when block (move-to-column max-indent t))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
871 (insert cce)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
872 (end-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
873 (not (or (eobp) (progn (forward-line) nil))))))))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
874
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
875 ;;;###autoload
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
876 (defun comment-region (beg end &optional arg)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
877 "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
878 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
879 Numeric prefix arg ARG means use ARG comment characters.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
880 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
881 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
882 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
883 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
884
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
885 The strings used as comment starts are built from
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
886 `comment-start' without trailing spaces and `comment-padding'."
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
887 (interactive "*r\nP")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
888 (comment-normalize-vars)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
889 (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
890 (let* ((numarg (prefix-numeric-value arg))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
891 (add comment-add)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
892 (style (cdr (assoc comment-style comment-styles)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
893 (lines (nth 2 style))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
894 (block (nth 1 style))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
895 (multi (nth 0 style)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
896 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
897 ;; 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
898 ;; of end-comment syntax rather than of whitespace syntax.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
899 ;; sanitize BEG and END
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
900 (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
901 (setq beg (max beg (point)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
902 (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
903 (setq end (min end (point)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
904 (if (>= beg end) (error "Nothing to comment"))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
905
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
906 ;; sanitize LINES
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
907 (setq lines
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
908 (and
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
909 lines ;; multi
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
910 (progn (goto-char beg) (beginning-of-line)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
911 (skip-syntax-forward " ")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
912 (>= (point) beg))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
913 (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
914 (<= (point) end))
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
915 (or block (not (string= "" comment-end)))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
916 (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
917
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
918 ;; 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
919 (unless (or lines (string= "" comment-end)) (setq block nil))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
920
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
921 (cond
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
922 ((consp arg) (uncomment-region beg end))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
923 ((< numarg 0) (uncomment-region beg end (- numarg)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
924 (t
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
925 (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
926 add (1- numarg)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
927 (comment-region-internal
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
928 beg end
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
929 (let ((s (comment-padright comment-start numarg)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
930 (if (string-match comment-start-skip s) s
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
931 (comment-padright comment-start)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
932 (let ((s (comment-padleft comment-end numarg)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
933 (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
934 (comment-padright comment-end))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
935 (if multi (comment-padright comment-continue numarg))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
936 (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
937 block
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
938 lines
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
939 (nth 3 style))))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
940
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
941 (defun comment-box (beg end &optional arg)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
942 "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
943 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
944 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
945 (interactive "*r\np")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
946 (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
947 'box-multi 'box)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
948 (comment-region beg end (+ comment-add arg))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
949
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
950
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
951 ;;;###autoload
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
952 (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
953 "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
954 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
955 is passed on to the respective function."
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
956 (interactive "*r\nP")
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
957 (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
958 (goto-char beg)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
959 (comment-forward (point-max))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
960 (<= end (point)))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
961 'uncomment-region 'comment-region)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
962 beg end arg))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
963
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
964 ;;;###autoload
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
965 (defun comment-dwim (arg)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
966 "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
967 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
968 `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
969 case it calls `uncomment-region').
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
970 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
971 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
972 Else, call `comment-indent'."
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
973 (interactive "*P")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
974 (comment-normalize-vars)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
975 (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
976 (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
977 (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
978 ;; 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
979 ;; specified, calling comment-kill is not very clever.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
980 (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
981 (let ((add (if arg (prefix-numeric-value arg)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
982 (if (= (length comment-start) 1) comment-add 0))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
983 ;; 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
984 ;; 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
985 (indent-according-to-mode)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
986 (insert (comment-padright comment-start add))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
987 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
988 (unless (string= "" comment-end)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
989 (insert (comment-padleft comment-end add)))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
990 (indent-according-to-mode))))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
991
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
992 (defcustom comment-auto-fill-only-comments nil
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
993 "Non-nil means to only auto-fill inside comments.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
994 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
995 :type 'boolean)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
996
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
997 (defun comment-valid-prefix (prefix compos)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
998 (or
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
999 ;; 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
1000 (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
1001 ;; 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
1002 (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)")
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1003 fill-prefix)))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1004
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1005 ;;;###autoload
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1006 (defun comment-indent-new-line (&optional soft)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1007 "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
1008 This indents the body of the continued comment
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1009 under the previous comment line.
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 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
1012 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
1013 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
1014
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1015 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
1016 or comment indentation.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1017
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1018 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
1019 unless optional argument SOFT is non-nil."
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1020 (interactive)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1021 (comment-normalize-vars t)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1022 (let (compos comin)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1023 ;; 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
1024 ;; 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
1025 (unless (and comment-start
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1026 comment-auto-fill-only-comments
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1027 (not (interactive-p))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1028 (not (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1029 (prog1 (setq compos (comment-beginning))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1030 (setq comin (point))))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1031
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1032 ;; XEmacs: next 3 lines from old version.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1033 (skip-chars-backward " \t")
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1034 (if (featurep 'mule)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1035 (declare-fboundp (kinsoku-process)))
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1036
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1037 ;; Now we know we should auto-fill.
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1038 ;; 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
1039 ;; get preserved better.
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1040 (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
1041 (save-excursion (forward-char -1) (delete-horizontal-space))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1042 (delete-horizontal-space)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1043
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1044 (if (and fill-prefix (not adaptive-fill-mode))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1045 ;; Blindly trust a non-adaptive fill-prefix.
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1046 (progn
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1047 (indent-to-left-margin)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1048 (insert-before-markers-and-inherit fill-prefix))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1049
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1050 ;;#### jhod: probably need to fix this for kinsoku processing
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1051 ;; 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
1052 (unless (or compos (null comment-start))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1053 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1054 (backward-char)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1055 (setq compos (comment-beginning))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1056 (setq comin (point))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1057
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1058 (cond
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1059 ;; 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
1060 ;; 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
1061 ((and fill-prefix
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1062 (or (not compos)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1063 (comment-valid-prefix fill-prefix compos)))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1064 (indent-to-left-margin)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1065 (insert-and-inherit fill-prefix))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1066 ;; 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
1067 ;; #### 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
1068 ;; 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
1069 ;; --ben
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1070 ;; (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
1071 ((not compos) (indent-according-to-mode))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1072 (t
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1073 (let* ((comment-column
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1074 ;; The continuation indentation should be somewhere between
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1075 ;; 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
1076 ;; and the current comment's indentation, with a preference
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1077 ;; for comment-column.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1078 (save-excursion
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1079 ;; 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
1080 (goto-char compos)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1081 (min (current-column) (max comment-column
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1082 (+ 2 (current-indentation))))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1083 (comstart (buffer-substring compos comin))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1084 (normalp
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1085 (string-match (regexp-quote (comment-string-strip
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1086 comment-start t t))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1087 comstart))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1088 (comment-end
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1089 (if normalp comment-end
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1090 ;; The comment starter is not the normal comment-start
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1091 ;; so we can't just use comment-end.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1092 (save-excursion
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1093 (goto-char compos)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1094 (if (not (comment-forward)) comment-end
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1095 (comment-string-strip
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1096 (buffer-substring
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1097 (save-excursion (comment-enter-backward) (point))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1098 (point))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1099 nil t)))))
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1100 (comment-start comstart)
4435
1e2fc51563a5 Fix auto-formatting of comments in auto-fill-mode
Didier Verna <didier@xemacs.org>
parents: 3289
diff changeset
1101 (block-comment-start comment-start)
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1102 (continuep (or comment-multi-line
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1103 (cadr (assoc comment-style comment-styles))))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1104 ;; Force comment-continue to be recreated from comment-start.
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1105 ;; 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
1106 ;; FIXME: use prev line's continuation if available.
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1107 (comment-continue nil))
2511
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1108 (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
1109 (indent-according-to-mode)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1110 (insert-and-inherit ?\n)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1111 (forward-char -1)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1112 (comment-indent continuep)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1113 (save-excursion
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1114 (let ((pt (point)))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1115 (end-of-line)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1116 (let ((comend (buffer-substring pt (point))))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1117 ;; 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
1118 (delete-region pt (1+ (point)))
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1119 (end-of-line 0)
b9a1074dc6bf [xemacs-hg @ 2005-01-26 09:57:14 by ben]
ben
parents: 2116
diff changeset
1120 (insert comend))))))))))))
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1121
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1122 (provide 'newcomment)
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1123
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents:
diff changeset
1124 ;;; newcomment.el ends here