diff options
| author | Noam Postavsky | 2017-06-14 00:08:15 -0400 |
|---|---|---|
| committer | Noam Postavsky | 2017-07-05 22:52:35 -0400 |
| commit | e832febfb4089418e0152c805e24dee977a7590d (patch) | |
| tree | e57e94068965c89352d735e48d827b6cba83db41 | |
| parent | 018600f896fbed768e583f6b8ee7fbae713367d1 (diff) | |
| download | emacs-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.
| -rw-r--r-- | doc/emacs/programs.texi | 9 | ||||
| -rw-r--r-- | etc/NEWS | 4 | ||||
| -rw-r--r-- | lisp/newcomment.el | 35 |
3 files changed, 28 insertions, 20 deletions
diff --git a/doc/emacs/programs.texi b/doc/emacs/programs.texi index 222d1c2a4de..27ac0eb6400 100644 --- a/doc/emacs/programs.texi +++ b/doc/emacs/programs.texi | |||
| @@ -1146,9 +1146,12 @@ comment or for aligning an existing comment. It is set differently by | |||
| 1146 | various major modes. The function is called with no arguments, but with | 1146 | various major modes. The function is called with no arguments, but with |
| 1147 | point at the beginning of the comment, or at the end of a line if a new | 1147 | point at the beginning of the comment, or at the end of a line if a new |
| 1148 | comment is to be inserted. It should return the column in which the | 1148 | comment is to be inserted. It should return the column in which the |
| 1149 | comment ought to start. For example, in Lisp mode, the indent hook | 1149 | comment ought to start. For example, the default hook function bases |
| 1150 | function bases its decision on how many semicolons begin an existing | 1150 | its decision on how many comment characters begin an existing comment. |
| 1151 | comment, and on the code in the preceding lines. | 1151 | |
| 1152 | Emacs also tries to align comments on adjacent lines. To override | ||
| 1153 | this, the function may return a cons of two (possibly equal) integers | ||
| 1154 | to indicate an acceptable range of indentation. | ||
| 1152 | 1155 | ||
| 1153 | @node Documentation | 1156 | @node Documentation |
| 1154 | @section Documentation Lookup | 1157 | @section Documentation Lookup |
| @@ -405,6 +405,10 @@ display of raw bytes from octal to hex. | |||
| 405 | ** You can now provide explicit field numbers in format specifiers. | 405 | ** You can now provide explicit field numbers in format specifiers. |
| 406 | For example, '(format "%2$s %1$s" "X" "Y")' produces "Y X". | 406 | For example, '(format "%2$s %1$s" "X" "Y")' produces "Y X". |
| 407 | 407 | ||
| 408 | +++ | ||
| 409 | ** 'comment-indent-function' values may now return a cons to specify a | ||
| 410 | range of indentation. | ||
| 411 | |||
| 408 | 412 | ||
| 409 | * Editing Changes in Emacs 26.1 | 413 | * Editing Changes in Emacs 26.1 |
| 410 | 414 | ||
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. |
| 145 | This function is called with no args with point at the beginning of | 145 | This function is called with no args with point at the beginning |
| 146 | the comment's starting delimiter and should return either the desired | 146 | of the comment's starting delimiter and should return either the |
| 147 | column indentation or nil. | 147 | desired column indentation, a range of acceptable |
| 148 | indentation (MIN . MAX), or nil. | ||
| 148 | If nil is returned, indentation is delegated to `indent-according-to-mode'.") | 149 | If 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). |
| 650 | Point is expected to be at the start of the comment." | 651 | Point 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))) |