comparison lisp/newcomment.el @ 39761:1cf8ddfb831f

(comment-indent): Be a little more robust in case comment-indent-function does funny things. (comment-normalize-vars): Autoload. (comment-region-internal): Fiddle with the visibility spec. (comment-valid-prefix): New function. (comment-indent-new-line): Use it. Ignore adaptive-fill-prefixes that would turn comment-text into non-comment-text.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Thu, 11 Oct 2001 01:44:48 +0000
parents 54fe59ee8057
children 71f93e410115
comparison
equal deleted inserted replaced
39760:5801481e337c 39761:1cf8ddfb831f
3 ;; Copyright (C) 1999, 2000 Free Software Foundation Inc. 3 ;; Copyright (C) 1999, 2000 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>
7 ;; Keywords: comment uncomment 7 ;; Keywords: comment uncomment
8 ;; Revision: $Id: newcomment.el,v 1.34 2001/09/01 21:23:17 monnier Exp $ 8 ;; Revision: $Id: newcomment.el,v 1.35 2001/09/27 21:13:44 monnier Exp $
9 9
10 ;; This file is part of GNU Emacs. 10 ;; This file is part of GNU Emacs.
11 11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify 12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by 13 ;; it under the terms of the GNU General Public License as published by
201 201
202 (defun comment-string-reverse (s) 202 (defun comment-string-reverse (s)
203 "Return the mirror image of string S, without any trailing space." 203 "Return the mirror image of string S, without any trailing space."
204 (comment-string-strip (concat (nreverse (string-to-list s))) nil t)) 204 (comment-string-strip (concat (nreverse (string-to-list s))) nil t))
205 205
206 ;;;###autoload
206 (defun comment-normalize-vars (&optional noerror) 207 (defun comment-normalize-vars (&optional noerror)
207 (if (not comment-start) (or noerror (error "No comment syntax is defined")) 208 (if (not comment-start) (or noerror (error "No comment syntax is defined"))
208 ;; comment-use-syntax 209 ;; comment-use-syntax
209 (when (eq comment-use-syntax 'undecided) 210 (when (eq comment-use-syntax 'undecided)
210 (set (make-local-variable 'comment-use-syntax) 211 (set (make-local-variable 'comment-use-syntax)
226 ;;(setq comment-end (comment-string-strip comment-end nil t)) 227 ;;(setq comment-end (comment-string-strip comment-end nil t))
227 ;; comment-continue 228 ;; comment-continue
228 (unless (or comment-continue (string= comment-end "")) 229 (unless (or comment-continue (string= comment-end ""))
229 (set (make-local-variable 'comment-continue) 230 (set (make-local-variable 'comment-continue)
230 (concat (if (string-match "\\S-\\S-" comment-start) " " "|") 231 (concat (if (string-match "\\S-\\S-" comment-start) " " "|")
231 (substring comment-start 1)))) 232 (substring comment-start 1)))
233 ;; Hasn't been necessary yet.
234 ;; (unless (string-match comment-start-skip comment-continue)
235 ;; (kill-local-variable 'comment-continue))
236 )
232 ;; comment-skip regexps 237 ;; comment-skip regexps
233 (unless comment-start-skip 238 (unless comment-start-skip
234 (set (make-local-variable 'comment-start-skip) 239 (set (make-local-variable 'comment-start-skip)
235 (concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|" 240 (concat "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(\\s<+\\|"
236 (regexp-quote (comment-string-strip comment-start t t)) 241 (regexp-quote (comment-string-strip comment-start t t))
464 (setq indent 469 (setq indent
465 (min indent 470 (min indent
466 (+ (current-column) 471 (+ (current-column)
467 (- fill-column 472 (- fill-column
468 (save-excursion (end-of-line) (current-column)))))) 473 (save-excursion (end-of-line) (current-column))))))
469 (if (= (current-column) indent) 474 (unless (= (current-column) indent)
470 (goto-char begpos)
471 ;; If that's different from current, change it. 475 ;; If that's different from current, change it.
472 (skip-chars-backward " \t") 476 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
473 (delete-region (point) begpos)
474 (indent-to (if (bolp) indent 477 (indent-to (if (bolp) indent
475 (max indent (1+ (current-column))))))) 478 (max indent (1+ (current-column)))))))
476 (goto-char cpos) 479 (goto-char cpos)
477 (set-marker cpos nil)))) 480 (set-marker cpos nil))))
478 481
760 ;; If the end is not at the end of a line and the comment-end 763 ;; If the end is not at the end of a line and the comment-end
761 ;; is implicit (i.e. a newline), explicitly insert a newline. 764 ;; is implicit (i.e. a newline), explicitly insert a newline.
762 (unless (or ce (eolp)) (insert "\n") (indent-according-to-mode)) 765 (unless (or ce (eolp)) (insert "\n") (indent-according-to-mode))
763 (comment-with-narrowing beg end 766 (comment-with-narrowing beg end
764 (let ((min-indent (point-max)) 767 (let ((min-indent (point-max))
765 (max-indent 0)) 768 (max-indent 0)
769 ;; We rebind the invisibility spec because move-to-column
770 ;; skips invisible text. Only reveal ellipses.
771 (buffer-invisibility-spec
772 (if (listp buffer-invisibility-spec)
773 (mapcar (lambda (x) (if (cdr-safe x) t x))
774 buffer-invisibility-spec)
775 buffer-invisibility-spec)))
766 (goto-char (point-min)) 776 (goto-char (point-min))
767 ;; Quote any nested comment marker 777 ;; Quote any nested comment marker
768 (comment-quote-nested comment-start comment-end nil) 778 (comment-quote-nested comment-start comment-end nil)
769 779
770 ;; Loop over all lines to find the needed indentations. 780 ;; Loop over all lines to find the needed indentations.
919 "Non-nil means to only auto-fill inside comments. 929 "Non-nil means to only auto-fill inside comments.
920 This has no effect in modes that do not define a comment syntax." 930 This has no effect in modes that do not define a comment syntax."
921 :type 'boolean 931 :type 'boolean
922 :group 'comment) 932 :group 'comment)
923 933
934 (defun comment-valid-prefix (prefix compos)
935 (or
936 ;; Accept any prefix if the current comment is not EOL-terminated.
937 (save-excursion (goto-char compos) (comment-forward) (not (bolp)))
938 ;; Accept any prefix that starts with a comment-start marker.
939 (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)")
940 fill-prefix)))
941
924 ;;;###autoload 942 ;;;###autoload
925 (defun comment-indent-new-line (&optional soft) 943 (defun comment-indent-new-line (&optional soft)
926 "Break line at point and indent, continuing comment if within one. 944 "Break line at point and indent, continuing comment if within one.
927 This indents the body of the continued comment 945 This indents the body of the continued comment
928 under the previous comment line. 946 under the previous comment line.
948 (setq comin (point)))))) 966 (setq comin (point))))))
949 967
950 ;; Now we know we should auto-fill. 968 ;; Now we know we should auto-fill.
951 (delete-horizontal-space) 969 (delete-horizontal-space)
952 (if soft (insert-and-inherit ?\n) (newline 1)) 970 (if soft (insert-and-inherit ?\n) (newline 1))
953 (if fill-prefix 971 (if (and fill-prefix (not adaptive-fill-mode))
972 ;; Blindly trust a non-adaptive fill-prefix.
954 (progn 973 (progn
955 (indent-to-left-margin) 974 (indent-to-left-margin)
956 (insert-and-inherit fill-prefix)) 975 (insert-and-inherit fill-prefix))
957 976
958 ;; If necessary check whether we're inside a comment. 977 ;; If necessary check whether we're inside a comment.
960 (save-excursion 979 (save-excursion
961 (backward-char) 980 (backward-char)
962 (setq compos (comment-beginning)) 981 (setq compos (comment-beginning))
963 (setq comin (point)))) 982 (setq comin (point))))
964 983
965 ;; If we're not inside a comment, just try to indent. 984 (cond
966 (if (not compos) (indent-according-to-mode) 985 ;; If there's an adaptive prefix, use it unless we're inside
986 ;; a comment and the prefix is not a comment starter.
987 ((and fill-prefix
988 (or (not compos)
989 (comment-valid-prefix fill-prefix compos)))
990 (indent-to-left-margin)
991 (insert-and-inherit fill-prefix))
992 ;; If we're not inside a comment, just try to indent.
993 ((not compos) (indent-according-to-mode))
994 (t
967 (let* ((comment-column 995 (let* ((comment-column
968 ;; The continuation indentation should be somewhere between 996 ;; The continuation indentation should be somewhere between
969 ;; the current line's indentation (plus 2 for good measure) 997 ;; the current line's indentation (plus 2 for good measure)
970 ;; and the current comment's indentation, with a preference 998 ;; and the current comment's indentation, with a preference
971 ;; for comment-column. 999 ;; for comment-column.
1004 ;; The 1+ is to make sure we delete the \n inserted above. 1032 ;; The 1+ is to make sure we delete the \n inserted above.
1005 (delete-region pt (1+ (point))) 1033 (delete-region pt (1+ (point)))
1006 (beginning-of-line) 1034 (beginning-of-line)
1007 (backward-char) 1035 (backward-char)
1008 (insert comend) 1036 (insert comend)
1009 (forward-char)))))))))) 1037 (forward-char)))))))))))
1010 1038
1011 (provide 'newcomment) 1039 (provide 'newcomment)
1012 1040
1013 ;;; newcomment.el ends here 1041 ;;; newcomment.el ends here