aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2004-09-07 05:18:49 +0000
committerStefan Monnier2004-09-07 05:18:49 +0000
commitb70dd952a7d597ce9a9c8e42f2ce25762f97f11e (patch)
treebc03a9daa1ecbb6d2a9a1b4592c539cdaff58f99
parentac62b9e443a436d11dba361f940dbe11bacf352a (diff)
downloademacs-b70dd952a7d597ce9a9c8e42f2ce25762f97f11e.tar.gz
emacs-b70dd952a7d597ce9a9c8e42f2ce25762f97f11e.zip
(uncomment-region-default, comment-region-default):
New functions extracted from uncomment-region and comment-region. (comment-region, comment-region-function, uncomment-region) (uncomment-region-function): Use them.
-rw-r--r--lisp/newcomment.el307
1 files changed, 156 insertions, 151 deletions
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index f4d4dc860d5..3a63636b757 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -1,6 +1,6 @@
1;;; newcomment.el --- (un)comment regions of buffers 1;;; newcomment.el --- (un)comment regions of buffers
2 2
3;; Copyright (C) 1999,2000,2003,2004 Free Software Foundation Inc. 3;; Copyright (C) 1999, 2000, 2003, 2004 Free Software Foundation Inc.
4 4
5;; Author: code extracted from Emacs-20's simple.el 5;; Author: code extracted from Emacs-20's simple.el
6;; Maintainer: Stefan Monnier <monnier@cs.yale.edu> 6;; Maintainer: Stefan Monnier <monnier@cs.yale.edu>
@@ -137,7 +137,7 @@ The function has no args.
137Applicable at least in modes for languages like fixed-format Fortran where 137Applicable at least in modes for languages like fixed-format Fortran where
138comments always start in column zero.") 138comments always start in column zero.")
139 139
140(defvar comment-region-function nil 140(defvar comment-region-function 'comment-region-default
141 "Function to comment a region. 141 "Function to comment a region.
142Its args are the same as those of `comment-region', but BEG and END are 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'. 143guaranteed to be correctly ordered. It is called within `save-excursion'.
@@ -145,7 +145,7 @@ guaranteed to be correctly ordered. It is called within `save-excursion'.
145Applicable at least in modes for languages like fixed-format Fortran where 145Applicable at least in modes for languages like fixed-format Fortran where
146comments always start in column zero.") 146comments always start in column zero.")
147 147
148(defvar uncomment-region-function nil 148(defvar uncomment-region-function 'uncomment-region-default
149 "Function to uncomment a region. 149 "Function to uncomment a region.
150Its args are the same as those of `uncomment-region', but BEG and END are 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'. 151guaranteed to be correctly ordered. It is called within `save-excursion'.
@@ -368,12 +368,12 @@ and raises an error or returns nil if NOERROR is non-nil."
368 (if comment-use-global-state (syntax-ppss pt)) 368 (if comment-use-global-state (syntax-ppss pt))
369 t))) 369 t)))
370 (when (and (nth 8 s) (nth 3 s) (not comment-use-global-state)) 370 (when (and (nth 8 s) (nth 3 s) (not comment-use-global-state))
371 ;; The search ended inside a string. Try to see if it 371 ;; The search ended at eol inside a string. Try to see if it
372 ;; works better when we assume that pt is inside a string. 372 ;; works better when we assume that pt is inside a string.
373 (setq s (parse-partial-sexp 373 (setq s (parse-partial-sexp
374 pt (or limit (point-max)) nil nil 374 pt (or limit (point-max)) nil nil
375 (list nil nil nil (nth 3 s) nil nil nil nil) 375 (list nil nil nil (nth 3 s) nil nil nil nil)
376 t))) 376 t)))
377 (if (not (and (nth 8 s) (not (nth 3 s)))) 377 (if (not (and (nth 8 s) (not (nth 3 s))))
378 (unless noerror (error "No comment")) 378 (unless noerror (error "No comment"))
379 ;; We found the comment. 379 ;; We found the comment.
@@ -710,105 +710,108 @@ comment markers."
710 (interactive "*r\nP") 710 (interactive "*r\nP")
711 (comment-normalize-vars) 711 (comment-normalize-vars)
712 (when (> beg end) (setq beg (prog1 end (setq end beg)))) 712 (when (> beg end) (setq beg (prog1 end (setq end beg))))
713 713 ;; Bind `comment-use-global-state' to nil. While uncommenting a region
714 ;; Bind `comment-use-global-state' to nil. While uncommenting a 714 ;; (which works a line at a time), a comment can appear to be
715 ;; (which works a line at a time) region a comment can appear to be
716 ;; included in a mult-line string, but it is actually not. 715 ;; included in a mult-line string, but it is actually not.
717 (let ((comment-use-global-state nil)) 716 (let ((comment-use-global-state nil))
718 (save-excursion 717 (save-excursion
719 (if uncomment-region-function 718 (funcall uncomment-region-function beg end arg))))
720 (funcall uncomment-region-function beg end arg) 719
721 (goto-char beg) 720(defun uncomment-region-default (beg end &optional arg)
722 (setq end (copy-marker end)) 721 "Uncomment each line in the BEG .. END region.
723 (let* ((numarg (prefix-numeric-value arg)) 722The numeric prefix ARG can specify a number of chars to remove from the
724 (ccs comment-continue) 723comment markers."
725 (srei (comment-padright ccs 're)) 724 (goto-char beg)
726 (csre (comment-padright comment-start 're)) 725 (setq end (copy-marker end))
727 (sre (and srei (concat "^\\s-*?\\(" srei "\\)"))) 726 (let* ((numarg (prefix-numeric-value arg))
728 spt) 727 (ccs comment-continue)
729 (while (and (< (point) end) 728 (srei (comment-padright ccs 're))
730 (setq spt (comment-search-forward end t))) 729 (csre (comment-padright comment-start 're))
731 (let ((ipt (point)) 730 (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
732 ;; Find the end of the comment. 731 spt)
733 (ept (progn 732 (while (and (< (point) end)
734 (goto-char spt) 733 (setq spt (comment-search-forward end t)))
735 (unless (or (comment-forward) 734 (let ((ipt (point))
736 ;; Allow non-terminated comments. 735 ;; Find the end of the comment.
737 (eobp)) 736 (ept (progn
738 (error "Can't find the comment end")) 737 (goto-char spt)
739 (point))) 738 (unless (or (comment-forward)
740 (box nil) 739 ;; Allow non-terminated comments.
741 (box-equal nil)) ;Whether we might be using `=' for boxes. 740 (eobp))
742 (save-restriction 741 (error "Can't find the comment end"))
743 (narrow-to-region spt ept) 742 (point)))
743 (box nil)
744 (box-equal nil)) ;Whether we might be using `=' for boxes.
745 (save-restriction
746 (narrow-to-region spt ept)
744 747
745 ;; Remove the comment-start. 748 ;; Remove the comment-start.
746 (goto-char ipt) 749 (goto-char ipt)
747 (skip-syntax-backward " ") 750 (skip-syntax-backward " ")
748 ;; A box-comment starts with a looong comment-start marker. 751 ;; A box-comment starts with a looong comment-start marker.
749 (when (and (or (and (= (- (point) (point-min)) 1) 752 (when (and (or (and (= (- (point) (point-min)) 1)
750 (setq box-equal t) 753 (setq box-equal t)
751 (looking-at "=\\{7\\}") 754 (looking-at "=\\{7\\}")
752 (not (eq (char-before (point-max)) ?\n)) 755 (not (eq (char-before (point-max)) ?\n))
753 (skip-chars-forward "=")) 756 (skip-chars-forward "="))
754 (> (- (point) (point-min) (length comment-start)) 7)) 757 (> (- (point) (point-min) (length comment-start)) 7))
755 (> (count-lines (point-min) (point-max)) 2)) 758 (> (count-lines (point-min) (point-max)) 2))
756 (setq box t)) 759 (setq box t))
757 ;; Skip the padding. Padding can come from comment-padding and/or 760 ;; Skip the padding. Padding can come from comment-padding and/or
758 ;; from comment-start, so we first check comment-start. 761 ;; from comment-start, so we first check comment-start.
759 (if (or (save-excursion (goto-char (point-min)) (looking-at csre)) 762 (if (or (save-excursion (goto-char (point-min)) (looking-at csre))
760 (looking-at (regexp-quote comment-padding))) 763 (looking-at (regexp-quote comment-padding)))
761 (goto-char (match-end 0))) 764 (goto-char (match-end 0)))
762 (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei))) 765 (when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
763 (goto-char (match-end 0))) 766 (goto-char (match-end 0)))
764 (if (null arg) (delete-region (point-min) (point)) 767 (if (null arg) (delete-region (point-min) (point))
765 (skip-syntax-backward " ") 768 (skip-syntax-backward " ")
766 (delete-char (- numarg)) 769 (delete-char (- numarg))
767 (unless (or (bobp) 770 (unless (or (bobp)
768 (save-excursion (goto-char (point-min)) 771 (save-excursion (goto-char (point-min))
769 (looking-at comment-start-skip))) 772 (looking-at comment-start-skip)))
770 ;; If there's something left but it doesn't look like 773 ;; If there's something left but it doesn't look like
771 ;; a comment-start any more, just remove it. 774 ;; a comment-start any more, just remove it.
772 (delete-region (point-min) (point)))) 775 (delete-region (point-min) (point))))
773 776
774 ;; Remove the end-comment (and leading padding and such). 777 ;; Remove the end-comment (and leading padding and such).
775 (goto-char (point-max)) (comment-enter-backward) 778 (goto-char (point-max)) (comment-enter-backward)
776 ;; Check for special `=' used sometimes in comment-box. 779 ;; Check for special `=' used sometimes in comment-box.
777 (when (and box-equal (not (eq (char-before (point-max)) ?\n))) 780 (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
778 (let ((pos (point))) 781 (let ((pos (point)))
779 ;; skip `=' but only if there are at least 7. 782 ;; skip `=' but only if there are at least 7.
780 (when (> (skip-chars-backward "=") -7) (goto-char pos)))) 783 (when (> (skip-chars-backward "=") -7) (goto-char pos))))
781 (unless (looking-at "\\(\n\\|\\s-\\)*\\'") 784 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
782 (when (and (bolp) (not (bobp))) (backward-char)) 785 (when (and (bolp) (not (bobp))) (backward-char))
783 (if (null arg) (delete-region (point) (point-max)) 786 (if (null arg) (delete-region (point) (point-max))
784 (skip-syntax-forward " ") 787 (skip-syntax-forward " ")
785 (delete-char numarg) 788 (delete-char numarg)
786 (unless (or (eobp) (looking-at comment-end-skip)) 789 (unless (or (eobp) (looking-at comment-end-skip))
787 ;; If there's something left but it doesn't look like 790 ;; If there's something left but it doesn't look like
788 ;; a comment-end any more, just remove it. 791 ;; a comment-end any more, just remove it.
789 (delete-region (point) (point-max))))) 792 (delete-region (point) (point-max)))))
790 793
791 ;; Unquote any nested end-comment. 794 ;; Unquote any nested end-comment.
792 (comment-quote-nested comment-start comment-end t) 795 (comment-quote-nested comment-start comment-end t)
793 796
794 ;; Eliminate continuation markers as well. 797 ;; Eliminate continuation markers as well.
795 (when sre 798 (when sre
796 (let* ((cce (comment-string-reverse (or comment-continue 799 (let* ((cce (comment-string-reverse (or comment-continue
797 comment-start))) 800 comment-start)))
798 (erei (and box (comment-padleft cce 're))) 801 (erei (and box (comment-padleft cce 're)))
799 (ere (and erei (concat "\\(" erei "\\)\\s-*$")))) 802 (ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
800 (goto-char (point-min)) 803 (goto-char (point-min))
801 (while (progn 804 (while (progn
802 (if (and ere (re-search-forward 805 (if (and ere (re-search-forward
803 ere (line-end-position) t)) 806 ere (line-end-position) t))
804 (replace-match "" t t nil (if (match-end 2) 2 1)) 807 (replace-match "" t t nil (if (match-end 2) 2 1))
805 (setq ere nil)) 808 (setq ere nil))
806 (forward-line 1) 809 (forward-line 1)
807 (re-search-forward sre (line-end-position) t)) 810 (re-search-forward sre (line-end-position) t))
808 (replace-match "" t t nil (if (match-end 2) 2 1))))) 811 (replace-match "" t t nil (if (match-end 2) 2 1)))))
809 ;; Go to the end for the next comment. 812 ;; Go to the end for the next comment.
810 (goto-char (point-max))))))) 813 (goto-char (point-max))))))
811 (set-marker end nil)))) 814 (set-marker end nil))
812 815
813(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block) 816(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
814 "Make the leading and trailing extra lines. 817 "Make the leading and trailing extra lines.
@@ -971,59 +974,61 @@ The strings used as comment starts are built from
971 (interactive "*r\nP") 974 (interactive "*r\nP")
972 (comment-normalize-vars) 975 (comment-normalize-vars)
973 (if (> beg end) (let (mid) (setq mid beg beg end end mid))) 976 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
977 (save-excursion
978 ;; FIXME: maybe we should call uncomment depending on ARG.
979 (funcall comment-region-function beg end arg)))
980
981(defun comment-region-default (beg end &optional arg)
974 (let* ((numarg (prefix-numeric-value arg)) 982 (let* ((numarg (prefix-numeric-value arg))
975 (add comment-add) 983 (add comment-add)
976 (style (cdr (assoc comment-style comment-styles))) 984 (style (cdr (assoc comment-style comment-styles)))
977 (lines (nth 2 style)) 985 (lines (nth 2 style))
978 (block (nth 1 style)) 986 (block (nth 1 style))
979 (multi (nth 0 style))) 987 (multi (nth 0 style)))
980 (save-excursion 988 ;; we use `chars' instead of `syntax' because `\n' might be
981 (if comment-region-function 989 ;; of end-comment syntax rather than of whitespace syntax.
982 (funcall comment-region-function beg end arg) 990 ;; sanitize BEG and END
983 ;; we use `chars' instead of `syntax' because `\n' might be 991 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
984 ;; of end-comment syntax rather than of whitespace syntax. 992 (setq beg (max beg (point)))
985 ;; sanitize BEG and END 993 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
986 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line) 994 (setq end (min end (point)))
987 (setq beg (max beg (point))) 995 (if (>= beg end) (error "Nothing to comment"))
988 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line) 996
989 (setq end (min end (point))) 997 ;; sanitize LINES
990 (if (>= beg end) (error "Nothing to comment")) 998 (setq lines
991 999 (and
992 ;; sanitize LINES 1000 lines ;; multi
993 (setq lines 1001 (progn (goto-char beg) (beginning-of-line)
994 (and 1002 (skip-syntax-forward " ")
995 lines ;; multi 1003 (>= (point) beg))
996 (progn (goto-char beg) (beginning-of-line) 1004 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
997 (skip-syntax-forward " ") 1005 (<= (point) end))
998 (>= (point) beg)) 1006 (or block (not (string= "" comment-end)))
999 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ") 1007 (or block (progn (goto-char beg) (search-forward "\n" end t)))))
1000 (<= (point) end)) 1008
1001 (or block (not (string= "" comment-end))) 1009 ;; don't add end-markers just because the user asked for `block'
1002 (or block (progn (goto-char beg) (search-forward "\n" end t)))))) 1010 (unless (or lines (string= "" comment-end)) (setq block nil))
1003 1011
1004 ;; don't add end-markers just because the user asked for `block' 1012 (cond
1005 (unless (or lines (string= "" comment-end)) (setq block nil)) 1013 ((consp arg) (uncomment-region beg end))
1006 1014 ((< numarg 0) (uncomment-region beg end (- numarg)))
1007 (cond 1015 (t
1008 ((consp arg) (uncomment-region beg end)) 1016 (setq numarg (if (and (null arg) (= (length comment-start) 1))
1009 ((< numarg 0) (uncomment-region beg end (- numarg))) 1017 add (1- numarg)))
1010 (t 1018 (comment-region-internal
1011 (setq numarg (if (and (null arg) (= (length comment-start) 1)) 1019 beg end
1012 add (1- numarg))) 1020 (let ((s (comment-padright comment-start numarg)))
1013 (comment-region-internal 1021 (if (string-match comment-start-skip s) s
1014 beg end 1022 (comment-padright comment-start)))
1015 (let ((s (comment-padright comment-start numarg))) 1023 (let ((s (comment-padleft comment-end numarg)))
1016 (if (string-match comment-start-skip s) s 1024 (and s (if (string-match comment-end-skip s) s
1017 (comment-padright comment-start))) 1025 (comment-padright comment-end))))
1018 (let ((s (comment-padleft comment-end numarg))) 1026 (if multi (comment-padright comment-continue numarg))
1019 (and s (if (string-match comment-end-skip s) s 1027 (if multi
1020 (comment-padright comment-end)))) 1028 (comment-padleft (comment-string-reverse comment-continue) numarg))
1021 (if multi (comment-padright comment-continue numarg)) 1029 block
1022 (if multi 1030 lines
1023 (comment-padleft (comment-string-reverse comment-continue) numarg)) 1031 (nth 3 style))))))
1024 block
1025 lines
1026 (nth 3 style)))))))
1027 1032
1028(defun comment-box (beg end &optional arg) 1033(defun comment-box (beg end &optional arg)
1029 "Comment out the BEG .. END region, putting it inside a box. 1034 "Comment out the BEG .. END region, putting it inside a box.
@@ -1198,5 +1203,5 @@ unless optional argument SOFT is non-nil."
1198 1203
1199(provide 'newcomment) 1204(provide 'newcomment)
1200 1205
1201;;; arch-tag: 01e3320a-00c8-44ea-a696-8f8e7354c858 1206;; arch-tag: 01e3320a-00c8-44ea-a696-8f8e7354c858
1202;;; newcomment.el ends here 1207;;; newcomment.el ends here