# HG changeset patch # User Stefan Monnier # Date 1052607275 0 # Node ID 15c3c47aca5025c45d47a3faca9411274afe7b42 # Parent 89a47df6186003321d5d9847785da36086d778eb (comment-indent): Try to align to adjacent comments. (comment-with-narrowing): Actually use the arguments. (comment-valid-prefix-p): Rename from comment-valid-prefix and fix to actually use its argument. diff -r 89a47df61860 -r 15c3c47aca50 lisp/newcomment.el --- a/lisp/newcomment.el Sat May 10 22:46:10 2003 +0000 +++ b/lisp/newcomment.el Sat May 10 22:54:35 2003 +0000 @@ -1,6 +1,6 @@ ;;; newcomment.el --- (un)comment regions of buffers -;; Copyright (C) 1999, 2000 Free Software Foundation Inc. +;; Copyright (C) 1999,2000,2003 Free Software Foundation Inc. ;; Author: code extracted from Emacs-20's simple.el ;; Maintainer: Stefan Monnier @@ -493,15 +493,36 @@ ;; Compute desired indent. (setq indent (save-excursion (funcall comment-indent-function))) (if (not indent) - ;; comment-indent-function refuses: delegate to indent. + ;; comment-indent-function refuses: delegate to line-indent. (indent-according-to-mode) ;; Avoid moving comments past the fill-column. (unless (save-excursion (skip-chars-backward " \t") (bolp)) - (setq indent - (min indent - (+ (current-column) + (let ((max (+ (current-column) (- (or comment-fill-column fill-column) - (save-excursion (end-of-line) (current-column))))))) + (save-excursion (end-of-line) (current-column)))))) + (if (<= max indent) + (setq indent max) ;Don't move past the fill column. + ;; We can choose anywhere between indent..max. + ;; Let's try to align to a comment on the previous line. + (let ((other nil)) + (save-excursion + (when (and (zerop (forward-line -1)) + (setq other (comment-search-forward + (line-end-position) t))) + (goto-char other) (setq other (current-column)))) + (if (and other (<= other max) (> other indent)) + ;; There is a comment and it's in the range: bingo. + (setq indent other) + ;; Let's try to align to a comment on the next line, then. + (let ((other nil)) + (save-excursion + (when (and (zerop (forward-line 1)) + (setq other (comment-search-forward + (line-end-position) t))) + (goto-char other) (setq other (current-column)))) + (if (and other (<= other max) (> other indent)) + ;; There is a comment and it's in the range: bingo. + (setq indent other)))))))) (unless (= (current-column) indent) ;; If that's different from current, change it. (delete-region (point) (progn (skip-chars-backward " \t") (point))) @@ -764,9 +785,9 @@ indentation to be kept as it was before narrowing." (declare (debug t) (indent 2)) (let ((bindent (make-symbol "bindent"))) - `(let ((,bindent (save-excursion (goto-char beg) (current-column)))) + `(let ((,bindent (save-excursion (goto-char ,beg) (current-column)))) (save-restriction - (narrow-to-region beg end) + (narrow-to-region ,beg ,end) (goto-char (point-min)) (insert (make-string ,bindent ? )) (prog1 @@ -988,13 +1009,13 @@ This has no effect in modes that do not define a comment syntax." :type 'boolean) -(defun comment-valid-prefix (prefix compos) +(defun comment-valid-prefix-p (prefix compos) (or ;; Accept any prefix if the current comment is not EOL-terminated. (save-excursion (goto-char compos) (comment-forward) (not (bolp))) ;; Accept any prefix that starts with a comment-start marker. (string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)") - fill-prefix))) + prefix))) ;;;###autoload (defun comment-indent-new-line (&optional soft) @@ -1048,7 +1069,7 @@ ;; a comment and the prefix is not a comment starter. ((and fill-prefix (or (not compos) - (comment-valid-prefix fill-prefix compos))) + (comment-valid-prefix-p fill-prefix compos))) (indent-to-left-margin) (insert-and-inherit fill-prefix)) ;; If we're not inside a comment, just try to indent.