aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorNoam Postavsky2017-06-14 00:08:15 -0400
committerNoam Postavsky2017-07-05 22:52:35 -0400
commite832febfb4089418e0152c805e24dee977a7590d (patch)
treee57e94068965c89352d735e48d827b6cba83db41 /lisp
parent018600f896fbed768e583f6b8ee7fbae713367d1 (diff)
downloademacs-e832febfb4089418e0152c805e24dee977a7590d.tar.gz
emacs-e832febfb4089418e0152c805e24dee977a7590d.zip
Allow comment-indent-functions to specify exact indentation (Bug#385)
* lisp/newcomment.el (comment-choose-indent): Interpret a cons of two integers as indicating a range of acceptable indentation. (comment-indent): Don't apply `comment-inline-offset', `comment-choose-indent' already does that. (comment-indent-function): * doc/emacs/programs.texi (Options for Comments): Document new acceptable return values. * etc/NEWS: Announce it.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/newcomment.el35
1 files changed, 18 insertions, 17 deletions
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 118549f421c..8772b52376d 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -142,9 +142,10 @@ Should be an empty string if comments are terminated by end-of-line.")
142;;;###autoload 142;;;###autoload
143(defvar comment-indent-function 'comment-indent-default 143(defvar comment-indent-function 'comment-indent-default
144 "Function to compute desired indentation for a comment. 144 "Function to compute desired indentation for a comment.
145This function is called with no args with point at the beginning of 145This function is called with no args with point at the beginning
146the comment's starting delimiter and should return either the desired 146of the comment's starting delimiter and should return either the
147column indentation or nil. 147desired column indentation, a range of acceptable
148indentation (MIN . MAX), or nil.
148If nil is returned, indentation is delegated to `indent-according-to-mode'.") 149If nil is returned, indentation is delegated to `indent-according-to-mode'.")
149 150
150;;;###autoload 151;;;###autoload
@@ -649,13 +650,20 @@ The criteria are (in this order):
649- prefer INDENT (or `comment-column' if nil). 650- prefer INDENT (or `comment-column' if nil).
650Point is expected to be at the start of the comment." 651Point is expected to be at the start of the comment."
651 (unless indent (setq indent comment-column)) 652 (unless indent (setq indent comment-column))
652 ;; Avoid moving comments past the fill-column. 653 (let ((other nil)
653 (let ((max (+ (current-column) 654 min max)
654 (- (or comment-fill-column fill-column) 655 (pcase indent
655 (save-excursion (end-of-line) (current-column))))) 656 (`(,lo . ,hi) (setq min lo) (setq max hi)
656 (other nil) 657 (setq indent comment-column))
657 (min (save-excursion (skip-chars-backward " \t") 658 (_ ;; Avoid moving comments past the fill-column.
658 (if (bolp) 0 (+ comment-inline-offset (current-column)))))) 659 (setq max (+ (current-column)
660 (- (or comment-fill-column fill-column)
661 (save-excursion (end-of-line) (current-column)))))
662 (setq min (save-excursion
663 (skip-chars-backward " \t")
664 ;; Leave at least `comment-inline-offset' space after
665 ;; other nonwhite text on the line.
666 (if (bolp) 0 (+ comment-inline-offset (current-column)))))))
659 ;; Fix up the range. 667 ;; Fix up the range.
660 (if (< max min) (setq max min)) 668 (if (< max min) (setq max min))
661 ;; Don't move past the fill column. 669 ;; Don't move past the fill column.
@@ -750,13 +758,6 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
750 ;; If the comment is at the right of code, adjust the indentation. 758 ;; If the comment is at the right of code, adjust the indentation.
751 (unless (save-excursion (skip-chars-backward " \t") (bolp)) 759 (unless (save-excursion (skip-chars-backward " \t") (bolp))
752 (setq indent (comment-choose-indent indent))) 760 (setq indent (comment-choose-indent indent)))
753 ;; Update INDENT to leave at least one space
754 ;; after other nonwhite text on the line.
755 (save-excursion
756 (skip-chars-backward " \t")
757 (unless (bolp)
758 (setq indent (max indent
759 (+ (current-column) comment-inline-offset)))))
760 ;; If that's different from comment's current position, change it. 761 ;; If that's different from comment's current position, change it.
761 (unless (= (current-column) indent) 762 (unless (= (current-column) indent)
762 (delete-region (point) (progn (skip-chars-backward " \t") (point))) 763 (delete-region (point) (progn (skip-chars-backward " \t") (point)))