annotate lisp/newcomment.el @ 1333:1b0339b048ce

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