aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2000-05-16 22:02:37 +0000
committerStefan Monnier2000-05-16 22:02:37 +0000
commitad679e458a9a601ba5d5316b26a569eacd101644 (patch)
tree79b596dd16ae1bb7e5f571d88d2d11bf0717e463
parentdd6b8ea7a87c3597abb9c5ff3e527b30507017ac (diff)
downloademacs-ad679e458a9a601ba5d5316b26a569eacd101644.tar.gz
emacs-ad679e458a9a601ba5d5316b26a569eacd101644.zip
(comment-string-strip): Strip terminating newlines.
(comment-search-forward): Make LIMIT compulsory. If an unterminated string (rather than a comment) is found, try again, assuming that the region starts inside a string. (comment-beginning): Make sure we don't move if we find a comment but it's not the one we're in. (comment-enter-backward): Handle the case where comment-end-skip fails. (comment-indent): Normalize variables and use line-end-position. (comment-kill): Use line-end-position. (comment-spill): Remove. (comment-indent-new-line): Renamed from indent-new-comment-line. Cleaned up old commented-out code. Reset comment-continue and comment-end before calling comment-indent.
-rw-r--r--lisp/newcomment.el212
1 files changed, 127 insertions, 85 deletions
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 075573e4230..a5ff8b83e67 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -6,7 +6,7 @@
6;; Maintainer: Stefan Monnier <monnier@cs.yale.edu> 6;; Maintainer: Stefan Monnier <monnier@cs.yale.edu>
7;; Keywords: comment uncomment 7;; Keywords: comment uncomment
8;; Version: $Name: $ 8;; Version: $Name: $
9;; Revision: $Id: newcomment.el,v 1.7 2000/05/13 19:41:08 monnier Exp $ 9;; Revision: $Id: newcomment.el,v 1.8 2000/05/14 00:56:10 monnier Exp $
10 10
11;; This program is free software; you can redistribute it and/or modify 11;; This program is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by 12;; it under the terms of the GNU General Public License as published by
@@ -31,7 +31,6 @@
31;; - single-char nestable comment-start can only do the "\\s<+" stuff 31;; - single-char nestable comment-start can only do the "\\s<+" stuff
32;; if the corresponding closing marker happens to be right. 32;; if the corresponding closing marker happens to be right.
33;; - comment-box in TeXinfo generate bogus comments @ccccc@ 33;; - comment-box in TeXinfo generate bogus comments @ccccc@
34;; - the code assumes that bol is outside of any comment/string.
35;; - uncomment-region with a numeric argument can render multichar 34;; - uncomment-region with a numeric argument can render multichar
36;; comment markers invalid. 35;; comment markers invalid.
37 36
@@ -47,6 +46,9 @@
47;; - support gnu-style "multi-line with space in continue" 46;; - support gnu-style "multi-line with space in continue"
48;; - somehow allow comment-dwim to use the region even if transient-mark-mode 47;; - somehow allow comment-dwim to use the region even if transient-mark-mode
49;; is not turned on. 48;; is not turned on.
49;; - (from c-comment-line-break-function): c-ignore-auto-fill so that you can
50;; auto-fill comments but not code. This should probably be moved directly
51;; inside do-auto-fill.
50 52
51;;; Code: 53;;; Code:
52 54
@@ -167,7 +169,7 @@ This is obsolete because you might as well use \\[newline-and-indent]."
167(defun comment-string-strip (str beforep afterp) 169(defun comment-string-strip (str beforep afterp)
168 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space." 170 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space."
169 (string-match (concat "\\`" (if beforep "\\s-*") 171 (string-match (concat "\\`" (if beforep "\\s-*")
170 "\\(.*?\\)" (if afterp "\\s-*") 172 "\\(.*?\\)" (if afterp "\\s-*\n?")
171 "\\'") str) 173 "\\'") str)
172 (match-string 1 str)) 174 (match-string 1 str))
173 175
@@ -253,7 +255,7 @@ If UNP is non-nil, unquote nested comment markers."
253;;;; Navigation 255;;;; Navigation
254;;;; 256;;;;
255 257
256(defun comment-search-forward (&optional limit noerror) 258(defun comment-search-forward (limit &optional noerror)
257 "Find a comment start between point and LIMIT. 259 "Find a comment start between point and LIMIT.
258Moves point to inside the comment and returns the position of the 260Moves point to inside the comment and returns the position of the
259comment-starter. If no comment is found, moves point to LIMIT 261comment-starter. If no comment is found, moves point to LIMIT
@@ -261,25 +263,40 @@ and raises an error or returns nil of NOERROR is non-nil."
261 (if (not comment-use-syntax) 263 (if (not comment-use-syntax)
262 (when (re-search-forward comment-start-skip limit noerror) 264 (when (re-search-forward comment-start-skip limit noerror)
263 (or (match-end 1) (match-beginning 0))) 265 (or (match-end 1) (match-beginning 0)))
264 (let ((s (parse-partial-sexp (point) (or limit (point-max)) nil nil nil t))) 266 (let* ((pt (point))
265 (if (and (nth 8 s) (not (nth 3 s))) 267 ;; Assume (at first) that pt is outside of any string.
266 (let ((pt (point)) 268 (s (parse-partial-sexp pt (or limit (point-max)) nil nil nil t)))
267 (start (nth 8 s)) 269 (when (and (nth 8 s) (nth 3 s))
268 (bol (save-excursion (beginning-of-line) (point))) 270 ;; The search ended inside a string. Try to see if it
269 (end nil)) 271 ;; works better when we assume that pt is inside a string.
270 (while (and (null end) (>= (point) bol)) 272 (setq s (parse-partial-sexp
271 (if (looking-at comment-start-skip) 273 pt (or limit (point-max)) nil nil
272 (setq end (min (or limit (point-max)) (match-end 0))) 274 (list nil nil nil (nth 3 s) nil nil nil nil)
273 (backward-char))) 275 t)))
274 (goto-char end) 276 (if (not (and (nth 8 s) (not (nth 3 s))))
275 start) 277 (unless noerror (error "No comment"))
276 (unless noerror (error "No comment")))))) 278 ;; We found the comment.
279 (let ((pt (point))
280 (start (nth 8 s))
281 (bol (save-excursion (beginning-of-line) (point)))
282 (end nil))
283 (while (and (null end) (>= (point) bol))
284 (if (looking-at comment-start-skip)
285 (setq end (min (or limit (point-max)) (match-end 0)))
286 (backward-char)))
287 (goto-char end)
288 start)))))
277 289
278(defun comment-search-backward (&optional limit noerror) 290(defun comment-search-backward (&optional limit noerror)
279 "Find a comment start between LIMIT and point. 291 "Find a comment start between LIMIT and point.
280Moves point to inside the comment and returns the position of the 292Moves point to inside the comment and returns the position of the
281comment-starter. If no comment is found, moves point to LIMIT 293comment-starter. If no comment is found, moves point to LIMIT
282and raises an error or returns nil of NOERROR is non-nil." 294and raises an error or returns nil of NOERROR is non-nil."
295 ;; FIXME: If a comment-start appears inside a comment, we may erroneously
296 ;; stop there. This can be rather bad in general, but since
297 ;; comment-search-backward is only used to find the comment-column (in
298 ;; comment-set-column) and to find the comment-start string (via
299 ;; comment-beginning) in indent-new-comment-line, it should be harmless.
283 (if (not (re-search-backward comment-start-skip limit t)) 300 (if (not (re-search-backward comment-start-skip limit t))
284 (unless noerror (error "No comment")) 301 (unless noerror (error "No comment"))
285 (beginning-of-line) 302 (beginning-of-line)
@@ -303,10 +320,11 @@ Returns nil if not inside a comment, else moves point and returns
303the same as `comment-search-forward'." 320the same as `comment-search-forward'."
304 (let ((pt (point)) 321 (let ((pt (point))
305 (cs (comment-search-backward nil t))) 322 (cs (comment-search-backward nil t)))
306 (save-excursion 323 (when cs
307 (and cs 324 (if (save-excursion (goto-char cs) (comment-forward 1) (> (point) pt))
308 (progn (goto-char cs) (forward-comment 1) (> (point) pt)) 325 cs
309 cs)))) 326 (goto-char pt)
327 nil))))
310 328
311(defun comment-forward (&optional n) 329(defun comment-forward (&optional n)
312 "Skip forward over N comments. 330 "Skip forward over N comments.
@@ -333,8 +351,15 @@ Point is assumed to be just at the end of a comment."
333 (beginning-of-line) 351 (beginning-of-line)
334 (save-restriction 352 (save-restriction
335 (narrow-to-region (point) end) 353 (narrow-to-region (point) end)
336 (re-search-forward (concat comment-end-skip "\\'")) 354 (if (re-search-forward (concat comment-end-skip "\\'") nil t)
337 (goto-char (match-beginning 0)))))) 355 (goto-char (match-beginning 0))
356 ;; comment-end-skip not found probably because it was not set right.
357 ;; Since \\s> should catch the single-char case, we'll blindly
358 ;; assume we're at the end of a two-char comment-end.
359 (goto-char (point-max))
360 (backward-char 2)
361 (skip-chars-backward (string (char-after)))
362 (skip-syntax-backward " "))))))
338 363
339;;;; 364;;;;
340;;;; Commands 365;;;; Commands
@@ -345,6 +370,7 @@ Point is assumed to be just at the end of a comment."
345 "Indent this line's comment to comment column, or insert an empty comment. 370 "Indent this line's comment to comment column, or insert an empty comment.
346If CONTINUE is non-nil, use the `comment-continuation' markers if any." 371If CONTINUE is non-nil, use the `comment-continuation' markers if any."
347 (interactive "*") 372 (interactive "*")
373 (comment-normalize-vars)
348 (let* ((empty (save-excursion (beginning-of-line) 374 (let* ((empty (save-excursion (beginning-of-line)
349 (looking-at "[ \t]*$"))) 375 (looking-at "[ \t]*$")))
350 (starter (or (and continue comment-continue) 376 (starter (or (and continue comment-continue)
@@ -354,7 +380,7 @@ If CONTINUE is non-nil, use the `comment-continuation' markers if any."
354 (cond 380 (cond
355 ((null starter) 381 ((null starter)
356 (error "No comment syntax defined")) 382 (error "No comment syntax defined"))
357 (t (let* ((eolpos (save-excursion (end-of-line) (point))) 383 (t (let* ((eolpos (line-end-position))
358 cpos indent begpos) 384 cpos indent begpos)
359 (beginning-of-line) 385 (beginning-of-line)
360 (when (setq begpos (comment-search-forward eolpos t)) 386 (when (setq begpos (comment-search-forward eolpos t))
@@ -389,16 +415,16 @@ With any other arg, set comment column to indentation of the previous comment
389 and then align or create a comment on this line at that column." 415 and then align or create a comment on this line at that column."
390 (interactive "P") 416 (interactive "P")
391 (cond 417 (cond
392 ((eq arg '-) (kill-comment nil)) 418 ((eq arg '-) (comment-kill nil))
393 (arg 419 (arg
394 (save-excursion 420 (save-excursion
395 (beginning-of-line) 421 (beginning-of-line)
396 (comment-search-backward) 422 (comment-search-backward)
397 (beginning-of-line) 423 (beginning-of-line)
398 (goto-char (comment-search-forward)) 424 (goto-char (comment-search-forward (line-end-position)))
399 (setq comment-column (current-column)) 425 (setq comment-column (current-column))
400 (message "Comment column set to %d" comment-column)) 426 (message "Comment column set to %d" comment-column))
401 (indent-for-comment)) 427 (comment-indent))
402 (t (setq comment-column (current-column)) 428 (t (setq comment-column (current-column))
403 (message "Comment column set to %d" comment-column)))) 429 (message "Comment column set to %d" comment-column))))
404 430
@@ -407,21 +433,18 @@ With any other arg, set comment column to indentation of the previous comment
407 "Kill the comment on this line, if any. 433 "Kill the comment on this line, if any.
408With prefix ARG, kill comments on that many lines starting with this one." 434With prefix ARG, kill comments on that many lines starting with this one."
409 (interactive "P") 435 (interactive "P")
410 (let (endc) 436 (dotimes (_ (prefix-numeric-value arg))
411 (dotimes (_ (prefix-numeric-value arg)) 437 (save-excursion
412 (save-excursion 438 (beginning-of-line)
413 (end-of-line) 439 (let ((cs (comment-search-forward (line-end-position) t)))
414 (setq endc (point)) 440 (when cs
415 (beginning-of-line) 441 (goto-char cs)
416 (let ((cs (comment-search-forward endc t))) 442 (skip-syntax-backward " ")
417 (when cs 443 (setq cs (point))
418 (goto-char cs) 444 (comment-forward)
419 (skip-syntax-backward " ") 445 (kill-region cs (if (bolp) (1- (point)) (point)))
420 (setq cs (point)) 446 (indent-according-to-mode))))
421 (comment-forward) 447 (if arg (forward-line 1))))
422 (kill-region cs (if (bolp) (1- (point)) (point)))
423 (indent-according-to-mode))))
424 (if arg (forward-line 1)))))
425 448
426(defun comment-padright (str &optional n) 449(defun comment-padright (str &optional n)
427 "Construct a string composed of STR plus `comment-padding'. 450 "Construct a string composed of STR plus `comment-padding'.
@@ -561,6 +584,8 @@ comment markers."
561 (set-marker end nil)))) 584 (set-marker end nil))))
562 585
563(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block) 586(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
587 "Make the leading and trailing extra lines.
588This is used for `extra-line' style (or `box' style if BLOCK is specified)."
564 (if block 589 (if block
565 (let* ((s (concat cs "a=m" cce "\n" 590 (let* ((s (concat cs "a=m" cce "\n"
566 (make-string min-indent ? ) ccs)) 591 (make-string min-indent ? ) ccs))
@@ -767,12 +792,13 @@ end- comment markers additionally to what `comment-add' already specifies."
767 (comment-region beg end (+ comment-add arg)))) 792 (comment-region beg end (+ comment-add arg))))
768 793
769(defun comment-dwim (arg) 794(defun comment-dwim (arg)
770 "Call the comment command you want. 795 "Call the comment command you want (Do What I Mean).
771If the region is active and `transient-mark-mode' is on, 796If the region is active and `transient-mark-mode' is on, call
772calls `comment-region' (unless it only consists 797 `comment-region' (unless it only consists in comments, in which
773in comments, in which case it calls `uncomment-region'). 798 case it calls `uncomment-region').
774Else, if the current line is empty, insert a comment and indent it. 799Else, if the current line is empty, insert a comment and indent it.
775Else call `indent-for-comment' or `kill-comment' if a prefix ARG is specified." 800Else if a prefix ARG is specified, call `comment-kill'.
801Else, call `comment-indent'."
776 (interactive "*P") 802 (interactive "*P")
777 (comment-normalize-vars) 803 (comment-normalize-vars)
778 (if (and mark-active transient-mark-mode) 804 (if (and mark-active transient-mark-mode)
@@ -785,7 +811,9 @@ Else call `indent-for-comment' or `kill-comment' if a prefix ARG is specified."
785 (uncomment-region beg end arg) 811 (uncomment-region beg end arg)
786 (comment-region beg end arg))) 812 (comment-region beg end arg)))
787 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$"))) 813 (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
788 (if arg (kill-comment (and (integerp arg) arg)) (indent-for-comment)) 814 ;; FIXME: If there's no comment to kill on this line and ARG is
815 ;; specified, calling comment-kill is not very clever.
816 (if arg (comment-kill (and (integerp arg) arg)) (comment-indent))
789 (let ((add (if arg (prefix-numeric-value arg) 817 (let ((add (if arg (prefix-numeric-value arg)
790 (if (= (length comment-start) 1) comment-add 0)))) 818 (if (= (length comment-start) 1) comment-add 0))))
791 (insert (comment-padright comment-start add)) 819 (insert (comment-padright comment-start add))
@@ -794,10 +822,8 @@ Else call `indent-for-comment' or `kill-comment' if a prefix ARG is specified."
794 (insert (comment-padleft comment-end add))) 822 (insert (comment-padleft comment-end add)))
795 (indent-according-to-mode)))))) 823 (indent-according-to-mode))))))
796 824
797(defcustom comment-spill t 825(defalias 'indent-new-comment-line 'comment-indent-new-line)
798 "") 826(defun comment-indent-new-line (&optional soft)
799
800(defun indent-new-comment-line (&optional soft)
801 "Break line at point and indent, continuing comment if within one. 827 "Break line at point and indent, continuing comment if within one.
802This indents the body of the continued comment 828This indents the body of the continued comment
803under the previous comment line. 829under the previous comment line.
@@ -813,11 +839,9 @@ The inserted newline is marked hard if variable `use-hard-newlines' is true,
813unless optional argument SOFT is non-nil." 839unless optional argument SOFT is non-nil."
814 (interactive) 840 (interactive)
815 (comment-normalize-vars t) 841 (comment-normalize-vars t)
816 (let (comcol comstart) 842 (let (comcol comstart compt)
817 (skip-chars-backward " \t") 843 (delete-region (progn (skip-chars-backward " \t") (point))
818 (delete-region (point) 844 (progn (skip-chars-forward " \t") (point)))
819 (progn (skip-chars-forward " \t")
820 (point)))
821 (if soft (insert-and-inherit ?\n) (newline 1)) 845 (if soft (insert-and-inherit ?\n) (newline 1))
822 (if fill-prefix 846 (if fill-prefix
823 (progn 847 (progn
@@ -826,36 +850,38 @@ unless optional argument SOFT is non-nil."
826 (unless comment-multi-line 850 (unless comment-multi-line
827 (save-excursion 851 (save-excursion
828 (backward-char) 852 (backward-char)
829 (if (and comment-start 853 (when (and comment-start
830 (setq comcol (comment-beginning))) 854 (setq compt (comment-beginning)))
831 ;; The old line has a comment and point was inside the comment. 855 ;; The old line has a comment and point was inside the comment.
832 ;; Set WIN to the pos of the comment-start. 856 ;; Indent this line like what we found.
833 857 (setq comstart (buffer-substring compt (point)))
834 ;; If comment-start-skip contains a \(...\) pair, 858 (goto-char compt)
835 ;; the real comment delimiter starts at the end of that pair. 859 (setq comcol (current-column)))))
836 (let ((win comcol)) 860 (if compt
837 ;; But if the comment is empty, look at preceding lines 861 (let* ((comment-column comcol)
838 ;; to find one that has a nonempty comment. 862 (normal-comment
839 ;; (while (and (eolp) (not (bobp)) 863 (string-match (regexp-quote (comment-string-strip
840 ;; (let (opoint) 864 comment-start t t))
841 ;; (beginning-of-line) 865 comstart))
842 ;; (setq opoint (point)) 866 ;; Force comment-continue to be recreated from comment-start.
843 ;; (forward-line -1) 867 (comment-continue nil) ;(if normal-comment comment-continue)
844 ;; (setq win (comment-search-forward opoint t))))) 868 (comment-end
845 ;; Why do we do that ? -sm 869 (if normal-comment comment-end
846 870 ;; The comment starter is not the normal comment-start
847 ;; Indent this line like what we found. 871 ;; so we can't just use comment-end.
848 (setq comstart (buffer-substring win (point))) 872 (save-excursion
849 (goto-char win) 873 (goto-char compt)
850 (setq comcol (current-column)) 874 (if (not (comment-forward)) comment-end
851 )))) 875 (comment-string-strip
852 (if comcol 876 (buffer-substring
853 (let ((comment-column comcol) 877 (save-excursion (comment-enter-backward) (point))
854 (comment-start comstart)) 878 (point))
879 nil t)))))
880 (comment-start comstart))
855 ;;(if (not (eolp)) (setq comment-end "")) 881 ;;(if (not (eolp)) (setq comment-end ""))
856 (insert-and-inherit ?\n) 882 (insert-and-inherit ?\n)
857 (forward-char -1) 883 (forward-char -1)
858 (indent-for-comment (cadr (assoc comment-style comment-styles))) 884 (comment-indent (cadr (assoc comment-style comment-styles)))
859 (save-excursion 885 (save-excursion
860 (let ((pt (point))) 886 (let ((pt (point)))
861 (end-of-line) 887 (end-of-line)
@@ -872,12 +898,28 @@ unless optional argument SOFT is non-nil."
872 898
873;;; Change Log: 899;;; Change Log:
874;; $Log: newcomment.el,v $ 900;; $Log: newcomment.el,v $
901;; Revision 1.8 2000/05/14 00:56:10 monnier
902;; (comment-start, comment-start-skip, comment-end): Made `defvar'.
903;; (comment-style): Extract the choice out of comment-styles.
904;; (comment-continue): Just a simple string now.
905;; (comment-normalize-vars): Update for the new comment-continue.
906;; (until, comment-end-quote-re): Removed.
907;; (comment-quote-re, comment-quote-nested): New functions for quoting.
908;; These quote both the end and the start and also work for single-chars.
909;; (comment-padright): Added lots of comments.
910;; (comment-padleft): Added more comments. Check comment-end rather than
911;; STR to determine whether N can be applied or not.
912;; (uncomment-region): Rename BLOCK to BOX.
913;; Use the new comment-quote-nested.
914;; Use only one marker and properly set it back to nil.
915;; Be more picky when eliminating continuation markers.
916;;
875;; Revision 1.7 2000/05/13 19:41:08 monnier 917;; Revision 1.7 2000/05/13 19:41:08 monnier
876;; (comment-use-syntax): Change `maybe' to `undecided'. 918;; (comment-use-syntax): Change `maybe' to `undecided'.
877;; (comment-quote-nested): New. Replaces comment-nested. 919;; (comment-quote-nested): New. Replaces comment-nested.
878;; (comment-add): Turn into a mere defvar or a integer. 920;; (comment-add): Turn into a mere defvar or a integer.
879;; (comment-style): Change default to `plain'. 921;; (comment-style): Change default to `plain'.
880;; (comment-styles): Rename `plain' to `indent' and create a new plainer `plain'. 922;; (comment-styles): Rename `plain' to `indent' and create a new plainer `plain'
881;; (comment-string-reverse): Use nreverse. 923;; (comment-string-reverse): Use nreverse.
882;; (comment-normalize-vars): Change `maybe' to `undecided', add comments. 924;; (comment-normalize-vars): Change `maybe' to `undecided', add comments.
883;; Don't infer the setting of comment-nested anymore (the default for 925;; Don't infer the setting of comment-nested anymore (the default for