aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2004-02-16 12:37:21 +0000
committerEli Zaretskii2004-02-16 12:37:21 +0000
commita71b3805cd48d60dc80ace3da006195235152d31 (patch)
treeb984c59f405fbb13bea4144225d481587f3569f1
parent062a9fce996bb58d9b23e1f7ce2f3de201e6f416 (diff)
downloademacs-a71b3805cd48d60dc80ace3da006195235152d31.tar.gz
emacs-a71b3805cd48d60dc80ace3da006195235152d31.zip
(comment-insert-comment-function, comment-region-function,
uncomment-region-function): New functions. (comment-indent): Use comment-insert-comment-function. (uncomment-region): Use uncomment-region-function. (comment-region): Use comment-region-function.
-rw-r--r--lisp/ChangeLog6
-rw-r--r--lisp/newcomment.el267
2 files changed, 156 insertions, 117 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 3f63f8e9ecb..4176661ca90 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,11 @@
12004-02-16 Dave Love <fx@gnu.org> 12004-02-16 Dave Love <fx@gnu.org>
2 2
3 * newcomment.el (comment-insert-comment-function)
4 (comment-region-function, uncomment-region-function): New.
5 (comment-indent): Use comment-insert-comment-function.
6 (uncomment-region): Use uncomment-region-function.
7 (comment-region): Use comment-region-function.
8
3 * emacs-lisp/rx.el (rx-not): Bind case-fold-search to nil. 9 * emacs-lisp/rx.el (rx-not): Bind case-fold-search to nil.
4 10
52004-02-16 Richard Stallman <rms@gnu.org> 112004-02-16 Richard Stallman <rms@gnu.org>
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 7bd4465c9f2..cf5a815fe95 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -129,6 +129,31 @@ the comment's starting delimiter and should return either the desired
129column indentation or nil. 129column indentation or nil.
130If nil is returned, indentation is delegated to `indent-according-to-mode'.") 130If nil is returned, indentation is delegated to `indent-according-to-mode'.")
131 131
132;;;###autoload
133(defvar comment-insert-comment-function nil
134 "Function to insert a comment when a line doesn't contain one.
135The function has no args.
136
137Applicable at least in modes for languages like fixed-format Fortran where
138comments always start in column zero.")
139
140(defvar comment-region-function nil
141 "Function to comment a region.
142Its args are the same as those of `comment-region', but BEG and END are
143guaranteed to be correctly ordered. It is called within `save-excursion'.
144
145Applicable at least in modes for languages like fixed-format Fortran where
146comments always start in column zero.")
147
148(defvar uncomment-region-function nil
149 "Function to uncomment a region.
150Its args are the same as those of `uncomment-region', but BEG and END are
151guaranteed to be correctly ordered. It is called within `save-excursion'.
152
153Applicable at least in modes for languages like fixed-format Fortran where
154comments always start in column zero.")
155
156;; ?? never set
132(defvar block-comment-start nil) 157(defvar block-comment-start nil)
133(defvar block-comment-end nil) 158(defvar block-comment-end nil)
134 159
@@ -460,7 +485,7 @@ Point is assumed to be just at the end of a comment."
460 485
461;;;###autoload 486;;;###autoload
462(defun comment-indent (&optional continue) 487(defun comment-indent (&optional continue)
463 "Indent this line's comment to comment column, or insert an empty comment. 488 "Indent this line's comment to `comment-column', or insert an empty comment.
464If CONTINUE is non-nil, use the `comment-continue' markers if any." 489If CONTINUE is non-nil, use the `comment-continue' markers if any."
465 (interactive "*") 490 (interactive "*")
466 (comment-normalize-vars) 491 (comment-normalize-vars)
@@ -486,9 +511,12 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
486 (forward-char (/ (skip-chars-backward " \t") -2))) 511 (forward-char (/ (skip-chars-backward " \t") -2)))
487 (setq cpos (point-marker))) 512 (setq cpos (point-marker)))
488 ;; If none, insert one. 513 ;; If none, insert one.
514 (if comment-insert-comment-function
515 (funcall comment-insert-comment-function)
489 (save-excursion 516 (save-excursion
490 ;; Some comment-indent-function insist on not moving comments that 517 ;; Some `comment-indent-function's insist on not moving
491 ;; are in column 0, so we first go to the likely target column. 518 ;; comments that are in column 0, so we first go to the
519 ;; likely target column.
492 (indent-to comment-column) 520 (indent-to comment-column)
493 ;; Ensure there's a space before the comment for things 521 ;; Ensure there's a space before the comment for things
494 ;; like sh where it matters (as well as being neater). 522 ;; like sh where it matters (as well as being neater).
@@ -497,7 +525,7 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
497 (setq begpos (point)) 525 (setq begpos (point))
498 (insert starter) 526 (insert starter)
499 (setq cpos (point-marker)) 527 (setq cpos (point-marker))
500 (insert ender))) 528 (insert ender))))
501 (goto-char begpos) 529 (goto-char begpos)
502 ;; Compute desired indent. 530 ;; Compute desired indent.
503 (setq indent (save-excursion (funcall comment-indent-function))) 531 (setq indent (save-excursion (funcall comment-indent-function)))
@@ -672,30 +700,32 @@ comment markers."
672 (comment-normalize-vars) 700 (comment-normalize-vars)
673 (if (> beg end) (let (mid) (setq mid beg beg end end mid))) 701 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
674 (save-excursion 702 (save-excursion
675 (goto-char beg) 703 (if uncomment-region-function
676 (setq end (copy-marker end)) 704 (funcall uncomment-region-function beg end arg)
677 (let* ((numarg (prefix-numeric-value arg)) 705 (goto-char beg)
678 (ccs comment-continue) 706 (setq end (copy-marker end))
679 (srei (comment-padright ccs 're)) 707 (let* ((numarg (prefix-numeric-value arg))
680 (csre (comment-padright comment-start 're)) 708 (ccs comment-continue)
681 (sre (and srei (concat "^\\s-*?\\(" srei "\\)"))) 709 (srei (comment-padright ccs 're))
682 spt) 710 (csre (comment-padright comment-start 're))
683 (while (and (< (point) end) 711 (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
684 (setq spt (comment-search-forward end t))) 712 spt)
685 (let ((ipt (point)) 713 (while (and (< (point) end)
686 ;; Find the end of the comment. 714 (setq spt (comment-search-forward end t)))
687 (ept (progn 715 (let ((ipt (point))
688 (goto-char spt) 716 ;; Find the end of the comment.
689 (unless (comment-forward) 717 (ept (progn
690 (error "Can't find the comment end")) 718 (goto-char spt)
691 (point))) 719 (unless (comment-forward)
692 (box nil) 720 (error "Can't find the comment end"))
693 (box-equal nil)) ;Whether we might be using `=' for boxes. 721 (point)))
694 (save-restriction 722 (box nil)
695 (narrow-to-region spt ept) 723 (box-equal nil)) ;Whether we might be using `=' for boxes.
696 724 (save-restriction
697 ;; Remove the comment-start. 725 (narrow-to-region spt ept)
698 (goto-char ipt) 726
727 ;; Remove the comment-start.
728 (goto-char ipt)
699 (skip-syntax-backward " ") 729 (skip-syntax-backward " ")
700 ;; A box-comment starts with a looong comment-start marker. 730 ;; A box-comment starts with a looong comment-start marker.
701 (when (and (or (and (= (- (point) (point-min)) 1) 731 (when (and (or (and (= (- (point) (point-min)) 1)
@@ -715,52 +745,52 @@ comment markers."
715 (goto-char (match-end 0))) 745 (goto-char (match-end 0)))
716 (if (null arg) (delete-region (point-min) (point)) 746 (if (null arg) (delete-region (point-min) (point))
717 (skip-syntax-backward " ") 747 (skip-syntax-backward " ")
718 (delete-char (- numarg)) 748 (delete-char (- numarg))
719 (unless (or (bobp) 749 (unless (or (bobp)
720 (save-excursion (goto-char (point-min)) 750 (save-excursion (goto-char (point-min))
721 (looking-at comment-start-skip))) 751 (looking-at comment-start-skip)))
722 ;; If there's something left but it doesn't look like 752 ;; If there's something left but it doesn't look like
723 ;; a comment-start any more, just remove it. 753 ;; a comment-start any more, just remove it.
724 (delete-region (point-min) (point)))) 754 (delete-region (point-min) (point))))
725 755
726 ;; Remove the end-comment (and leading padding and such). 756 ;; Remove the end-comment (and leading padding and such).
727 (goto-char (point-max)) (comment-enter-backward) 757 (goto-char (point-max)) (comment-enter-backward)
728 ;; Check for special `=' used sometimes in comment-box. 758 ;; Check for special `=' used sometimes in comment-box.
729 (when (and box-equal (not (eq (char-before (point-max)) ?\n))) 759 (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
730 (let ((pos (point))) 760 (let ((pos (point)))
731 ;; skip `=' but only if there are at least 7. 761 ;; skip `=' but only if there are at least 7.
732 (when (> (skip-chars-backward "=") -7) (goto-char pos)))) 762 (when (> (skip-chars-backward "=") -7) (goto-char pos))))
733 (unless (looking-at "\\(\n\\|\\s-\\)*\\'") 763 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
734 (when (and (bolp) (not (bobp))) (backward-char)) 764 (when (and (bolp) (not (bobp))) (backward-char))
735 (if (null arg) (delete-region (point) (point-max)) 765 (if (null arg) (delete-region (point) (point-max))
736 (skip-syntax-forward " ") 766 (skip-syntax-forward " ")
737 (delete-char numarg) 767 (delete-char numarg)
738 (unless (or (eobp) (looking-at comment-end-skip)) 768 (unless (or (eobp) (looking-at comment-end-skip))
739 ;; If there's something left but it doesn't look like 769 ;; If there's something left but it doesn't look like
740 ;; a comment-end any more, just remove it. 770 ;; a comment-end any more, just remove it.
741 (delete-region (point) (point-max))))) 771 (delete-region (point) (point-max)))))
742 772
743 ;; Unquote any nested end-comment. 773 ;; Unquote any nested end-comment.
744 (comment-quote-nested comment-start comment-end t) 774 (comment-quote-nested comment-start comment-end t)
745 775
746 ;; Eliminate continuation markers as well. 776 ;; Eliminate continuation markers as well.
747 (when sre 777 (when sre
748 (let* ((cce (comment-string-reverse (or comment-continue 778 (let* ((cce (comment-string-reverse (or comment-continue
749 comment-start))) 779 comment-start)))
750 (erei (and box (comment-padleft cce 're))) 780 (erei (and box (comment-padleft cce 're)))
751 (ere (and erei (concat "\\(" erei "\\)\\s-*$")))) 781 (ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
752 (goto-char (point-min)) 782 (goto-char (point-min))
753 (while (progn 783 (while (progn
754 (if (and ere (re-search-forward 784 (if (and ere (re-search-forward
755 ere (line-end-position) t)) 785 ere (line-end-position) t))
756 (replace-match "" t t nil (if (match-end 2) 2 1)) 786 (replace-match "" t t nil (if (match-end 2) 2 1))
757 (setq ere nil)) 787 (setq ere nil))
758 (forward-line 1) 788 (forward-line 1)
759 (re-search-forward sre (line-end-position) t)) 789 (re-search-forward sre (line-end-position) t))
760 (replace-match "" t t nil (if (match-end 2) 2 1))))) 790 (replace-match "" t t nil (if (match-end 2) 2 1)))))
761 ;; Go to the end for the next comment. 791 ;; Go to the end for the next comment.
762 (goto-char (point-max))))) 792 (goto-char (point-max)))))))
763 (set-marker end nil)))) 793 (set-marker end nil)))
764 794
765(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block) 795(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
766 "Make the leading and trailing extra lines. 796 "Make the leading and trailing extra lines.
@@ -930,49 +960,52 @@ The strings used as comment starts are built from
930 (block (nth 1 style)) 960 (block (nth 1 style))
931 (multi (nth 0 style))) 961 (multi (nth 0 style)))
932 (save-excursion 962 (save-excursion
933 ;; we use `chars' instead of `syntax' because `\n' might be 963 (if comment-region-function
934 ;; of end-comment syntax rather than of whitespace syntax. 964 (funcall comment-region-function beg end arg)
935 ;; sanitize BEG and END 965 ;; we use `chars' instead of `syntax' because `\n' might be
936 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line) 966 ;; of end-comment syntax rather than of whitespace syntax.
937 (setq beg (max beg (point))) 967 ;; sanitize BEG and END
938 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line) 968 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
939 (setq end (min end (point))) 969 (setq beg (max beg (point)))
940 (if (>= beg end) (error "Nothing to comment")) 970 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
941 971 (setq end (min end (point)))
942 ;; sanitize LINES 972 (if (>= beg end) (error "Nothing to comment"))
943 (setq lines 973
944 (and 974 ;; sanitize LINES
945 lines ;; multi 975 (setq lines
946 (progn (goto-char beg) (beginning-of-line) 976 (and
947 (skip-syntax-forward " ") 977 lines ;; multi
948 (>= (point) beg)) 978 (progn (goto-char beg) (beginning-of-line)
949 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ") 979 (skip-syntax-forward " ")
950 (<= (point) end)) 980 (>= (point) beg))
951 (or block (not (string= "" comment-end))) 981 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
952 (or block (progn (goto-char beg) (search-forward "\n" end t)))))) 982 (<= (point) end))
953 983 (or block (not (string= "" comment-end)))
954 ;; don't add end-markers just because the user asked for `block' 984 (or block (progn (goto-char beg) (search-forward "\n" end t))))))
955 (unless (or lines (string= "" comment-end)) (setq block nil)) 985
956 986 ;; don't add end-markers just because the user asked for `block'
957 (cond 987 (unless (or lines (string= "" comment-end)) (setq block nil))
958 ((consp arg) (uncomment-region beg end)) 988
959 ((< numarg 0) (uncomment-region beg end (- numarg))) 989 (cond
960 (t 990 ((consp arg) (uncomment-region beg end))
961 (setq numarg (if (and (null arg) (= (length comment-start) 1)) 991 ((< numarg 0) (uncomment-region beg end (- numarg)))
962 add (1- numarg))) 992 (t
963 (comment-region-internal 993 (setq numarg (if (and (null arg) (= (length comment-start) 1))
964 beg end 994 add (1- numarg)))
965 (let ((s (comment-padright comment-start numarg))) 995 (comment-region-internal
966 (if (string-match comment-start-skip s) s 996 beg end
967 (comment-padright comment-start))) 997 (let ((s (comment-padright comment-start numarg)))
968 (let ((s (comment-padleft comment-end numarg))) 998 (if (string-match comment-start-skip s) s
969 (and s (if (string-match comment-end-skip s) s 999 (comment-padright comment-start)))
970 (comment-padright comment-end)))) 1000 (let ((s (comment-padleft comment-end numarg)))
971 (if multi (comment-padright comment-continue numarg)) 1001 (and s (if (string-match comment-end-skip s) s
972 (if multi (comment-padleft (comment-string-reverse comment-continue) numarg)) 1002 (comment-padright comment-end))))
973 block 1003 (if multi (comment-padright comment-continue numarg))
974 lines 1004 (if multi
975 (nth 3 style)))))) 1005 (comment-padleft (comment-string-reverse comment-continue) numarg))
1006 block
1007 lines
1008 (nth 3 style)))))))
976 1009
977(defun comment-box (beg end &optional arg) 1010(defun comment-box (beg end &optional arg)
978 "Comment out the BEG .. END region, putting it inside a box. 1011 "Comment out the BEG .. END region, putting it inside a box.