aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier1999-12-08 00:19:51 +0000
committerStefan Monnier1999-12-08 00:19:51 +0000
commit3e569d228e68690d53bea93614652dc39d8289fd (patch)
treec048cfea72d13b6269ae4916f92fc6e4ac986919
parent05e71564d829d466aa41cd309b4db2e14e058528 (diff)
downloademacs-3e569d228e68690d53bea93614652dc39d8289fd.tar.gz
emacs-3e569d228e68690d53bea93614652dc39d8289fd.zip
various fixes and gratuitous movements.
-rw-r--r--lisp/newcomment.el133
1 files changed, 69 insertions, 64 deletions
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 097c4666ba0..ed8658fc33b 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -5,7 +5,7 @@
5;; Author: Stefan Monnier <monnier@cs.yale.edu> 5;; Author: Stefan Monnier <monnier@cs.yale.edu>
6;; Keywords: comment uncomment 6;; Keywords: comment uncomment
7;; Version: $Name: $ 7;; Version: $Name: $
8;; Revision: $Id: newcomment.el,v 1.4 1999/11/29 01:31:47 monnier Exp $ 8;; Revision: $Id: newcomment.el,v 1.5 1999/11/30 16:20:55 monnier Exp $
9 9
10;; This program is free software; you can redistribute it and/or modify 10;; This program is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by 11;; it under the terms of the GNU General Public License as published by
@@ -30,32 +30,31 @@
30;; - single-char nestable comment-start can only do the "\\s<+" stuff 30;; - single-char nestable comment-start can only do the "\\s<+" stuff
31;; if the corresponding closing marker happens to be right. 31;; if the corresponding closing marker happens to be right.
32;; - C-u C-u comment-region in TeXinfo generates bogus comments @ccccc@ 32;; - C-u C-u comment-region in TeXinfo generates bogus comments @ccccc@
33;; - removal of comment-continue does not necesarily work because the 33;; - the code assumes that bol is outside of any comment/string.
34;; continuation marker could have a leading space that turned into a tab 34;; - uncomment-region with a numeric argument can render multichar
35;; comment markers invalid.
35 36
36;;; Todo: 37;;; Todo:
37 38
38;; - extract comment data from the syntax-table 39;; - extract comment data from the syntax-table
39;; - maybe do the opposite as well (set the syntax-table from other data) 40;; - maybe do the opposite as well (set the syntax-table from other data)
40;; - customizable auto-fill of comments 41;; - spill auto-fill of comments onto the end of the next line
41;; - uncomment-region with a numeric argument
42;; - uncomment-region with a consp (for blocks) or somehow make the 42;; - uncomment-region with a consp (for blocks) or somehow make the
43;; deletion of continuation markers less dangerous 43;; deletion of continuation markers less dangerous
44;; - drop block-comment-<foo> unless it's really used 44;; - drop block-comment-<foo> unless it's really used
45;; - uncomment-region un a part of a comment 45;; - uncomment-region on a part of a comment
46 46;; - obey the numarg of uncomment-region for continuation markers
47;;; Problems:
48
49;; - comment padding: (= comment-start "[- ") can either mean that
50;; the syntax of a comment-start is "[-" plus " " of padding
51;; (as is the case for C) or that the space is strictly required
52;; as is the case for TeXinfo.
53 47
54;;; Code: 48;;; Code:
55 49
56(eval-when-compile (require 'cl)) 50(eval-when-compile (require 'cl))
57 51
58(defcustom comment-use-syntax 'maybe 52(defgroup comment nil
53 "Indenting and filling of comments."
54 :prefix "comment-"
55 :group 'fill)
56
57(defvar comment-use-syntax 'maybe
59 "Non-nil if syntax-tables can be used instead of regexps.") 58 "Non-nil if syntax-tables can be used instead of regexps.")
60 59
61(defcustom comment-column 32 60(defcustom comment-column 32
@@ -64,14 +63,14 @@ Setting this variable automatically makes it local to the current buffer.
64Each mode establishes a different default value for this variable; you 63Each mode establishes a different default value for this variable; you
65can set the value for a particular mode using that mode's hook." 64can set the value for a particular mode using that mode's hook."
66 :type 'integer 65 :type 'integer
67 :group 'fill-comments) 66 :group 'comment)
68(make-variable-buffer-local 'comment-column) 67(make-variable-buffer-local 'comment-column)
69 68
70(defcustom comment-start nil 69(defcustom comment-start nil
71 "*String to insert to start a new comment, or nil if no comment syntax." 70 "*String to insert to start a new comment, or nil if no comment syntax."
72 :type '(choice (const :tag "None" nil) 71 :type '(choice (const :tag "None" nil)
73 string) 72 string)
74 :group 'fill-comments) 73 :group 'comment)
75 74
76(defcustom comment-start-skip nil 75(defcustom comment-start-skip nil
77 "*Regexp to match the start of a comment plus everything up to its body. 76 "*Regexp to match the start of a comment plus everything up to its body.
@@ -79,18 +78,15 @@ If there are any \\(...\\) pairs, the comment delimiter text is held to begin
79at the place matched by the close of the first pair." 78at the place matched by the close of the first pair."
80 :type '(choice (const :tag "None" nil) 79 :type '(choice (const :tag "None" nil)
81 regexp) 80 regexp)
82 :group 'fill-comments) 81 :group 'comment)
83(defcustom comment-end-skip nil 82(defvar comment-end-skip nil
84 "*Regexp to match the end of a comment plus everything up to its body." 83 "Regexp to match the end of a comment plus everything up to its body.")
85 :type '(choice (const :tag "None" nil)
86 regexp)
87 :group 'fill-comments)
88 84
89(defcustom comment-end "" 85(defcustom comment-end ""
90 "*String to insert to end a new comment. 86 "*String to insert to end a new comment.
91Should be an empty string if comments are terminated by end-of-line." 87Should be an empty string if comments are terminated by end-of-line."
92 :type 'string 88 :type 'string
93 :group 'fill-comments) 89 :group 'comment)
94 90
95(defvar comment-indent-hook nil 91(defvar comment-indent-hook nil
96 "Obsolete variable for function to compute desired indentation for a comment. 92 "Obsolete variable for function to compute desired indentation for a comment.
@@ -103,22 +99,8 @@ the comment's starting delimiter.")
103This function is called with no args with point at the beginning of 99This function is called with no args with point at the beginning of
104the comment's starting delimiter.") 100the comment's starting delimiter.")
105 101
106(defcustom block-comment-start nil 102(defvar block-comment-start nil)
107 "*String to insert to start a new comment on a line by itself. 103(defvar block-comment-end nil)
108If nil, use `comment-start' instead.
109Note that the regular expression `comment-start-skip' should skip this string
110as well as the `comment-start' string."
111 :type '(choice (const :tag "Use comment-start" nil)
112 string)
113 :group 'fill-comments)
114
115(defcustom block-comment-end nil
116 "*String to insert to end a new comment on a line by itself.
117Should be an empty string if comments are terminated by end-of-line.
118If nil, use `comment-end' instead."
119 :type '(choice (const :tag "Use comment-end" nil)
120 string)
121 :group 'fill-comments)
122 104
123(defcustom comment-nested 'maybe 105(defcustom comment-nested 'maybe
124 "Whether the comments can be nested.") 106 "Whether the comments can be nested.")
@@ -141,13 +123,20 @@ If nil, use `comment-end' instead."
141 (extra-line . (t nil t))) 123 (extra-line . (t nil t)))
142 "Possible styles.") 124 "Possible styles.")
143 125
144(defvar comment-padding 1 126(defcustom comment-padding 1
145 "Number of spaces `comment-region' puts between comment chars and text. 127 "Number of spaces `comment-region' puts between comment chars and text.
146Can also be a string instead. 128Can also be a string instead.
147 129
148Extra spacing between the comment characters and the comment text 130Extra spacing between the comment characters and the comment text
149makes the comment easier to read. Default is 1. Nil means 0.") 131makes the comment easier to read. Default is 1. Nil means 0.")
150 132
133(defcustom comment-multi-line nil
134 "*Non-nil means \\[indent-new-comment-line] should continue same comment
135on new line, with no new terminator or starter.
136This is obsolete because you might as well use \\[newline-and-indent]."
137 :type 'boolean
138 :group 'comment)
139
151;;;; 140;;;;
152;;;; Helpers 141;;;; Helpers
153;;;; 142;;;;
@@ -321,7 +310,8 @@ The point is assumed to be right at the end of a comment."
321;;;; Commands 310;;;; Commands
322;;;; 311;;;;
323 312
324(defun indent-for-comment (&optional continue) 313(defalias 'indent-for-comment 'comment-indent)
314(defun comment-indent (&optional continue)
325 "Indent this line's comment to comment column, or insert an empty comment. 315 "Indent this line's comment to comment column, or insert an empty comment.
326If CONTINUE is non-nil, use the `comment-continuation' markers if any." 316If CONTINUE is non-nil, use the `comment-continuation' markers if any."
327 (interactive "*") 317 (interactive "*")
@@ -362,7 +352,8 @@ If CONTINUE is non-nil, use the `comment-continuation' markers if any."
362 (save-excursion 352 (save-excursion
363 (insert ender)))))))) 353 (insert ender))))))))
364 354
365(defun set-comment-column (arg) 355(defalias 'set-comment-column 'comment-set-column)
356(defun comment-set-column (arg)
366 "Set the comment column based on point. 357 "Set the comment column based on point.
367With no ARG, set the comment column to the current column. 358With no ARG, set the comment column to the current column.
368With just minus as arg, kill any comment on this line. 359With just minus as arg, kill any comment on this line.
@@ -383,7 +374,8 @@ With any other arg, set comment column to indentation of the previous comment
383 (t (setq comment-column (current-column)) 374 (t (setq comment-column (current-column))
384 (message "Comment column set to %d" comment-column)))) 375 (message "Comment column set to %d" comment-column))))
385 376
386(defun kill-comment (arg) 377(defalias 'kill-comment 'comment-kill)
378(defun comment-kill (arg)
387 "Kill the comment on this line, if any. 379 "Kill the comment on this line, if any.
388With prefix ARG, kill comments on that many lines starting with this one." 380With prefix ARG, kill comments on that many lines starting with this one."
389 (interactive "P") 381 (interactive "P")
@@ -413,17 +405,20 @@ It N is 're, a regexp is returned instead, that would match
413 the string for any N." 405 the string for any N."
414 (setq n (or n 0)) 406 (setq n (or n 0))
415 (when (and (stringp str) (not (string= "" str))) 407 (when (and (stringp str) (not (string= "" str)))
416 (string-match "\\s-*\\'" str) 408 (string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
417 (let ((s (substring str 0 (match-beginning 0))) 409 (let ((s (match-string 1 str))
418 (pad (concat (match-string 0 str) 410 (lpad (substring str 0 (match-beginning 1)))
419 (substring comment-padding 411 (rpad (concat (substring str (match-end 1))
420 (min (- (match-end 0) (match-beginning 0)) 412 (substring comment-padding
421 (length comment-padding)))))) 413 (min (- (match-end 0) (match-end 1))
414 (length comment-padding))))))
422 (if (symbolp n) 415 (if (symbolp n)
423 (concat (regexp-quote s) "+" 416 (concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
417 lpad "")
418 (regexp-quote s) "+"
424 (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?")) 419 (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
425 pad "")) 420 rpad ""))
426 (concat s (make-string n (aref str (1- (match-beginning 0)))) pad))))) 421 (concat lpad s (make-string n (aref str (1- (match-end 1)))) rpad)))))
427 422
428(defun comment-padleft (str &optional n) 423(defun comment-padleft (str &optional n)
429 "Construct a string composed of `comment-padding' plus STR. 424 "Construct a string composed of `comment-padding' plus STR.
@@ -474,7 +469,7 @@ ARG is currently ignored."
474 (end-quote-re (comment-end-quote-re comment-end "\\\\")) 469 (end-quote-re (comment-end-quote-re comment-end "\\\\"))
475 (ccs (car comment-continue)) 470 (ccs (car comment-continue))
476 (srei (comment-padright ccs 're)) 471 (srei (comment-padright ccs 're))
477 (sre (and srei (concat "^\\s-*\\(" srei "\\)")))) 472 (sre (and srei (concat "^\\s-*?\\(" srei "\\)"))))
478 (save-restriction 473 (save-restriction
479 (narrow-to-region spt ept) 474 (narrow-to-region spt ept)
480 ;; remove the comment-start 475 ;; remove the comment-start
@@ -486,14 +481,18 @@ ARG is currently ignored."
486 (goto-char (match-end 0))) 481 (goto-char (match-end 0)))
487 (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei))) 482 (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
488 (goto-char (match-end 0))) 483 (goto-char (match-end 0)))
489 (delete-region (point-min) (point)) 484 (if (null arg) (delete-region (point-min) (point))
485 (skip-syntax-backward " ")
486 (delete-char (- numarg)))
490 487
491 ;; remove the end-comment (and leading padding and such) 488 ;; remove the end-comment (and leading padding and such)
492 (goto-char (point-max)) (comment-enter-backward) 489 (goto-char (point-max)) (comment-enter-backward)
493 (unless (string-match "\\`\\(\n\\|\\s-\\)*\\'" 490 (unless (string-match "\\`\\(\n\\|\\s-\\)*\\'"
494 (buffer-substring (point) ept)) 491 (buffer-substring (point) ept))
495 (when (and (bolp) (not (bobp))) (backward-char)) 492 (when (and (bolp) (not (bobp))) (backward-char))
496 (delete-region (point) ept)) 493 (if (null arg) (delete-region (point) ept)
494 (skip-syntax-forward " ")
495 (delete-char numarg)))
497 496
498 ;; unquote any nested end-comment 497 ;; unquote any nested end-comment
499 (when end-quote-re 498 (when end-quote-re
@@ -689,8 +688,12 @@ The strings used as comment starts are built from
689 (setq numarg add) (decf numarg)) 688 (setq numarg add) (decf numarg))
690 (comment-region-internal 689 (comment-region-internal
691 beg end 690 beg end
692 (comment-padright comment-start numarg) 691 (let ((s (comment-padright comment-start numarg)))
693 (comment-padleft comment-end numarg) 692 (if (string-match comment-start-skip s) s
693 (comment-padright comment-start)))
694 (let ((s (comment-padleft comment-end numarg)))
695 (and s (if (string-match comment-end-skip s) s
696 (comment-padright comment-end))))
694 (if multi (comment-padright (car comment-continue) numarg)) 697 (if multi (comment-padright (car comment-continue) numarg))
695 (if multi (comment-padleft (cdr comment-continue) numarg)) 698 (if multi (comment-padleft (cdr comment-continue) numarg))
696 block 699 block
@@ -723,12 +726,8 @@ Else call `indent-for-comment' or `kill-comment' if a prefix ARG is specified."
723 (insert (comment-padleft comment-end add))) 726 (insert (comment-padleft comment-end add)))
724 (indent-according-to-mode)))))) 727 (indent-according-to-mode))))))
725 728
726(defcustom comment-multi-line nil 729(defcustom comment-spill t
727 "*Non-nil means \\[indent-new-comment-line] should continue same comment 730 "")
728on new line, with no new terminator or starter.
729This is obsolete because you might as well use \\[newline-and-indent]."
730 :type 'boolean
731 :group 'fill-comments)
732 731
733(defun indent-new-comment-line (&optional soft) 732(defun indent-new-comment-line (&optional soft)
734 "Break line at point and indent, continuing comment if within one. 733 "Break line at point and indent, continuing comment if within one.
@@ -805,6 +804,12 @@ unless optional argument SOFT is non-nil."
805 804
806;;; Change Log: 805;;; Change Log:
807;; $Log: newcomment.el,v $ 806;; $Log: newcomment.el,v $
807;; Revision 1.5 1999/11/30 16:20:55 monnier
808;; (comment-style(s)): Replaces comment-extra-lines (and comment-multi-line).
809;; (comment-use-syntax): Whether to use the syntax-table or just the regexps.
810;; (comment-end-skip): To find the end of the text.
811;; ...
812;;
808;; Revision 1.4 1999/11/29 01:31:47 monnier 813;; Revision 1.4 1999/11/29 01:31:47 monnier
809;; (comment-find): New function. 814;; (comment-find): New function.
810;; (indent-for-comment, set-comment-column, kill-comment): use it. 815;; (indent-for-comment, set-comment-column, kill-comment): use it.
@@ -834,7 +839,7 @@ unless optional argument SOFT is non-nil."
834;; - comment-region on a comment calls uncomment-region 839;; - comment-region on a comment calls uncomment-region
835;; - C-u C-u comment-region aligns comment end markers 840;; - C-u C-u comment-region aligns comment end markers
836;; - C-u C-u C-u comment-region puts the comment inside a rectangle 841;; - C-u C-u C-u comment-region puts the comment inside a rectangle
837;; - comment-region will try to quote coment-end markers inside the region 842;; - comment-region will try to quote comment-end markers inside the region
838;; - comment-start markers are placed at the indentation level 843;; - comment-start markers are placed at the indentation level
839;; 844;;
840 845