comparison lisp/newcomment.el @ 84625:f867348019bf

(comment-add): New arg EXTRA. (comment-region-default): Pass EXTRA if not indenting lines.
author Richard M. Stallman <rms@gnu.org>
date Mon, 17 Sep 2007 16:40:26 +0000
parents b98604865ea0
children a5c8194cf0e8
comparison
equal deleted inserted replaced
84624:444c91c33e7f 84625:f867348019bf
192 (aligned . (nil t nil t)) 192 (aligned . (nil t nil t))
193 (multi-line . (t nil nil t)) 193 (multi-line . (t nil nil t))
194 (extra-line . (t nil t t)) 194 (extra-line . (t nil t t))
195 (box . (nil t t t)) 195 (box . (nil t t t))
196 (box-multi . (t t t t))) 196 (box-multi . (t t t t)))
197 "Possible comment styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)). 197 "Comment region styles of the form (STYLE . (MULTI ALIGN EXTRA INDENT)).
198 STYLE should be a mnemonic symbol. 198 STYLE should be a mnemonic symbol.
199 MULTI specifies that comments are allowed to span multiple lines. 199 MULTI specifies that comments are allowed to span multiple lines.
200 ALIGN specifies that the `comment-end' markers should be aligned. 200 ALIGN specifies that the `comment-end' markers should be aligned.
201 EXTRA specifies that an extra line should be used before and after the 201 EXTRA specifies that an extra line should be used before and after the
202 region to comment (to put the `comment-end' and `comment-start'). 202 region to comment (to put the `comment-end' and `comment-start').
206 ;;;###autoload 206 ;;;###autoload
207 (defcustom comment-style 'plain 207 (defcustom comment-style 'plain
208 "Style to be used for `comment-region'. 208 "Style to be used for `comment-region'.
209 See `comment-styles' for a list of available styles." 209 See `comment-styles' for a list of available styles."
210 :type (if (boundp 'comment-styles) 210 :type (if (boundp 'comment-styles)
211 `(choice ,@(mapcar (lambda (s) `(const ,(car s))) comment-styles)) 211 `(choice ,@(mapcar (lambda (s) `(const ,(car s)))
212 comment-styles))
212 'symbol) 213 'symbol)
213 :group 'comment) 214 :group 'comment)
214 215
215 ;;;###autoload 216 ;;;###autoload
216 (defcustom comment-padding " " 217 (defcustom comment-padding " "
936 (let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1)))) 937 (let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1))))
937 (goto-char (match-beginning 0)) 938 (goto-char (match-beginning 0))
938 (delete-char n) 939 (delete-char n)
939 (setq ,bindent (- ,bindent n))))))))))) 940 (setq ,bindent (- ,bindent n)))))))))))
940 941
941 (defun comment-add (arg) 942 ;; Compute the number of extra semicolons to add to the comment starter
943 ;; in Lisp mode, extra stars in C mode, etc.
944 ;; If ARG is non-nil, just follow ARG.
945 ;; If the comment-starter is mult-char, just follow ARG.
946 ;; Otherwise obey comment-add, and add one more if EXTRA is non-nil.
947 (defun comment-add (arg &optional extra)
942 (if (and (null arg) (= (string-match "[ \t]*\\'" comment-start) 1)) 948 (if (and (null arg) (= (string-match "[ \t]*\\'" comment-start) 1))
943 comment-add 949 (+ comment-add (if extra 1 0))
944 (1- (prefix-numeric-value arg)))) 950 (1- (prefix-numeric-value arg))))
945 951
946 (defun comment-region-internal (beg end cs ce 952 (defun comment-region-internal (beg end cs ce
947 &optional ccs cce block lines indent) 953 &optional ccs cce block lines indent)
948 "Comment region BEG .. END. 954 "Comment region BEG .. END.
1049 (let* ((numarg (prefix-numeric-value arg)) 1055 (let* ((numarg (prefix-numeric-value arg))
1050 (style (cdr (assoc comment-style comment-styles))) 1056 (style (cdr (assoc comment-style comment-styles)))
1051 (lines (nth 2 style)) 1057 (lines (nth 2 style))
1052 (block (nth 1 style)) 1058 (block (nth 1 style))
1053 (multi (nth 0 style))) 1059 (multi (nth 0 style)))
1054 ;; we use `chars' instead of `syntax' because `\n' might be 1060
1061 ;; We use `chars' instead of `syntax' because `\n' might be
1055 ;; of end-comment syntax rather than of whitespace syntax. 1062 ;; of end-comment syntax rather than of whitespace syntax.
1056 ;; sanitize BEG and END 1063 ;; sanitize BEG and END
1057 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line) 1064 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
1058 (setq beg (max beg (point))) 1065 (setq beg (max beg (point)))
1059 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line) 1066 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
1077 1084
1078 (cond 1085 (cond
1079 ((consp arg) (uncomment-region beg end)) 1086 ((consp arg) (uncomment-region beg end))
1080 ((< numarg 0) (uncomment-region beg end (- numarg))) 1087 ((< numarg 0) (uncomment-region beg end (- numarg)))
1081 (t 1088 (t
1082 (setq numarg (comment-add arg)) 1089 ;; Add an extra semicolon in Lisp and similar modes.
1090 ;; If STYLE doesn't specify indenting the comments,
1091 ;; then add yet one more semicolon.
1092 (setq numarg (comment-add arg (null (nth 3 style))))
1083 (comment-region-internal 1093 (comment-region-internal
1084 beg end 1094 beg end
1085 (let ((s (comment-padright comment-start numarg))) 1095 (let ((s (comment-padright comment-start numarg)))
1086 (if (string-match comment-start-skip s) s 1096 (if (string-match comment-start-skip s) s
1087 (comment-padright comment-start))) 1097 (comment-padright comment-start)))