summaryrefslogtreecommitdiff
path: root/lisp/newcomment.el
diff options
context:
space:
mode:
authorNoam Postavsky <npostavs@gmail.com>2017-06-14 00:08:15 -0400
committerNoam Postavsky <npostavs@gmail.com>2017-07-05 22:52:35 -0400
commite832febfb4089418e0152c805e24dee977a7590d (patch)
treee57e94068965c89352d735e48d827b6cba83db41 /lisp/newcomment.el
parent018600f896fbed768e583f6b8ee7fbae713367d1 (diff)
downloademacs-e832febfb4089418e0152c805e24dee977a7590d.tar.gz
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/newcomment.el')
-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.")
;;;###autoload
(defvar comment-indent-function 'comment-indent-default
"Function to compute desired indentation for a comment.
-This function is called with no args with point at the beginning of
-the comment's starting delimiter and should return either the desired
-column indentation or nil.
+This function is called with no args with point at the beginning
+of the comment's starting delimiter and should return either the
+desired column indentation, a range of acceptable
+indentation (MIN . MAX), or nil.
If nil is returned, indentation is delegated to `indent-according-to-mode'.")
;;;###autoload
@@ -649,13 +650,20 @@ The criteria are (in this order):
- prefer INDENT (or `comment-column' if nil).
Point is expected to be at the start of the comment."
(unless indent (setq indent comment-column))
- ;; Avoid moving comments past the fill-column.
- (let ((max (+ (current-column)
- (- (or comment-fill-column fill-column)
- (save-excursion (end-of-line) (current-column)))))
- (other nil)
- (min (save-excursion (skip-chars-backward " \t")
- (if (bolp) 0 (+ comment-inline-offset (current-column))))))
+ (let ((other nil)
+ min max)
+ (pcase indent
+ (`(,lo . ,hi) (setq min lo) (setq max hi)
+ (setq indent comment-column))
+ (_ ;; Avoid moving comments past the fill-column.
+ (setq max (+ (current-column)
+ (- (or comment-fill-column fill-column)
+ (save-excursion (end-of-line) (current-column)))))
+ (setq min (save-excursion
+ (skip-chars-backward " \t")
+ ;; Leave at least `comment-inline-offset' space after
+ ;; other nonwhite text on the line.
+ (if (bolp) 0 (+ comment-inline-offset (current-column)))))))
;; Fix up the range.
(if (< max min) (setq max min))
;; Don't move past the fill column.
@@ -750,13 +758,6 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
;; If the comment is at the right of code, adjust the indentation.
(unless (save-excursion (skip-chars-backward " \t") (bolp))
(setq indent (comment-choose-indent indent)))
- ;; Update INDENT to leave at least one space
- ;; after other nonwhite text on the line.
- (save-excursion
- (skip-chars-backward " \t")
- (unless (bolp)
- (setq indent (max indent
- (+ (current-column) comment-inline-offset)))))
;; If that's different from comment's current position, change it.
(unless (= (current-column) indent)
(delete-region (point) (progn (skip-chars-backward " \t") (point)))