aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2007-09-28 01:59:40 +0000
committerStefan Monnier2007-09-28 01:59:40 +0000
commitb2943616250b3ead895918c53e4f49cb7143b460 (patch)
treee7873b099b97f734c2a3890ff786bbbd3683b949
parent60860eb36d45e00509fd97c167ee4e9fbb5b7ac6 (diff)
downloademacs-b2943616250b3ead895918c53e4f49cb7143b460.tar.gz
emacs-b2943616250b3ead895918c53e4f49cb7143b460.zip
(comment-choose-indent): New function extracted
from comment-indent. Improve the alignment algorithm. (comment-indent): Use it.
-rw-r--r--lisp/ChangeLog25
-rw-r--r--lisp/newcomment.el99
2 files changed, 77 insertions, 47 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 2f59072e23f..e2b68f2b617 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,9 @@
12007-09-28 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * newcomment.el (comment-choose-indent): New function extracted
4 from comment-indent. Improve the alignment algorithm.
5 (comment-indent): Use it.
6
12007-09-27 Juanma Barranquero <lekktu@gmail.com> 72007-09-27 Juanma Barranquero <lekktu@gmail.com>
2 8
3 * emacs-lisp/eldoc.el (eldoc-message-commands-table-size) 9 * emacs-lisp/eldoc.el (eldoc-message-commands-table-size)
@@ -8,7 +14,7 @@
8 14
92007-09-26 Eli Zaretskii <eliz@gnu.org> 152007-09-26 Eli Zaretskii <eliz@gnu.org>
10 16
11 * menu-bar.el (menu-bar-search-documentation-menu): Renamed from 17 * menu-bar.el (menu-bar-search-documentation-menu): Rename from
12 menu-bar-apropos-menu. All users changed. 18 menu-bar-apropos-menu. All users changed.
13 (menu-bar-help-menu): Change menu symbols to better match the text 19 (menu-bar-help-menu): Change menu symbols to better match the text
14 displayed by the menu. 20 displayed by the menu.
@@ -31,8 +37,8 @@
31 37
322007-09-24 Ville Skytt,Ad(B <scop@xemacs.org> (tiny change) 382007-09-24 Ville Skytt,Ad(B <scop@xemacs.org> (tiny change)
33 39
34 * net/tramp-vc.el (tramp-vc-user-login-name): Use 40 * net/tramp-vc.el (tramp-vc-user-login-name):
35 `vc-find-version' instead of `process-file' to check whether we 41 Use `vc-find-version' instead of `process-file' to check whether we
36 have a new enough vc that doesn't need the defadvice. 42 have a new enough vc that doesn't need the defadvice.
37 43
382007-09-23 Dan Nicolaescu <dann@ics.uci.edu> 442007-09-23 Dan Nicolaescu <dann@ics.uci.edu>
@@ -75,16 +81,15 @@
75 81
762007-09-22 Glenn Morris <rgm@gnu.org> 822007-09-22 Glenn Morris <rgm@gnu.org>
77 83
78 * eshell/esh-mode.el (eshell-output-filter-functions): Add 84 * eshell/esh-mode.el (eshell-output-filter-functions):
79 eshell-postoutput-scroll-to-bottom. 85 Add eshell-postoutput-scroll-to-bottom.
80 86
812007-09-21 Dan Nicolaescu <dann@ics.uci.edu> 872007-09-21 Dan Nicolaescu <dann@ics.uci.edu>
82 88
83 * files.el (file-name-sans-versions): Allow - and a-z in version 89 * files.el (file-name-sans-versions): Allow - and a-z in version names.
84 names.
85 90
86 * log-view.el (log-view-mode-map, log-view-mode-menu): Bind 91 * log-view.el (log-view-mode-map, log-view-mode-menu):
87 log-view-annotate-version. 92 Bind log-view-annotate-version.
88 (log-view-beginning-of-defun, log-view-end-of-defun) 93 (log-view-beginning-of-defun, log-view-end-of-defun)
89 (log-view-annotate-version): New functions. 94 (log-view-annotate-version): New functions.
90 (log-view-mode): Use log-view-beginning-of-defun and 95 (log-view-mode): Use log-view-beginning-of-defun and
@@ -92,7 +97,7 @@
92 97
932007-09-21 Stefan Monnier <monnier@iro.umontreal.ca> 982007-09-21 Stefan Monnier <monnier@iro.umontreal.ca>
94 99
95 * emacs-lisp/easy-mmode.el (define-minor-mode): Fix staging brain damage. 100 * emacs-lisp/easy-mmode.el (define-minor-mode): Fix staging.
96 101
972007-09-21 Kevin Ryde <user42@zip.com.au> 1022007-09-21 Kevin Ryde <user42@zip.com.au>
98 103
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 63ceacb0440..9c6be255023 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -97,10 +97,10 @@ Major modes should set this variable.")
97;;;###autoload 97;;;###autoload
98(defcustom comment-column 32 98(defcustom comment-column 32
99 "Column to indent right-margin comments to. 99 "Column to indent right-margin comments to.
100Each mode establishes a different default value for this variable; you 100Each mode may establish a different default value for this variable; you
101can set the value for a particular mode using that mode's hook. 101can set the value for a particular mode using that mode's hook.
102Comments might be indented to a value smaller than this in order 102Comments might be indented to a different value in order not to go beyond
103not to go beyond `comment-fill-column'." 103`comment-fill-column' or in order to align them with surrounding comments."
104 :type 'integer 104 :type 'integer
105 :group 'comment) 105 :group 'comment)
106(make-variable-buffer-local 'comment-column) 106(make-variable-buffer-local 'comment-column)
@@ -194,7 +194,7 @@ two semi-colons.")
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)).
198STYLE should be a mnemonic symbol. 198STYLE should be a mnemonic symbol.
199MULTI specifies that comments are allowed to span multiple lines. 199MULTI specifies that comments are allowed to span multiple lines.
200ALIGN specifies that the `comment-end' markers should be aligned. 200ALIGN specifies that the `comment-end' markers should be aligned.
@@ -208,7 +208,8 @@ INDENT specifies that the `comment-start' markers should not be put at the
208 "Style to be used for `comment-region'. 208 "Style to be used for `comment-region'.
209See `comment-styles' for a list of available styles." 209See `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
@@ -516,6 +517,58 @@ Point is assumed to be just at the end of a comment."
516 (and (> comment-add 0) (looking-at "\\s<\\(\\S<\\|\\'\\)"))) 517 (and (> comment-add 0) (looking-at "\\s<\\(\\S<\\|\\'\\)")))
517 comment-column))) 518 comment-column)))
518 519
520(defun comment-choose-indent (&optional indent)
521 "Choose the indentation to use for a right-hand-side comment.
522The criteria are (in this order):
523- try to keep the comment's text within `comment-fill-column'.
524- try to align with surrounding comments.
525- prefer INDENT (or `comment-column' if nil).
526Point is expected to be at the start of the comment."
527 (unless indent (setq indent comment-column))
528 ;; Avoid moving comments past the fill-column.
529 (let ((max (+ (current-column)
530 (- (or comment-fill-column fill-column)
531 (save-excursion (end-of-line) (current-column)))))
532 (other nil)
533 (min (save-excursion (skip-chars-backward " \t")
534 (1+ (current-column)))))
535 ;; Fix up the range.
536 (if (< max min) (setq max min))
537 ;; Don't move past the fill column.
538 (if (<= max indent) (setq indent max))
539 ;; We can choose anywhere between min..max.
540 ;; Let's try to align to a comment on the previous line.
541 (save-excursion
542 (when (and (zerop (forward-line -1))
543 (setq other (comment-search-forward
544 (line-end-position) t)))
545 (goto-char other) (setq other (current-column))))
546 (if (and other (<= other max) (>= other min))
547 ;; There is a comment and it's in the range: bingo!
548 other
549 ;; Can't align to a previous comment: let's try to align to comments
550 ;; on the following lines, then. These have not been re-indented yet,
551 ;; so we can't directly align ourselves with them. All we do is to try
552 ;; and choose an indentation point with which they will be able to
553 ;; align themselves.
554 (save-excursion
555 (while (and (zerop (forward-line 1))
556 (setq other (comment-search-forward
557 (line-end-position) t)))
558 (goto-char other)
559 (let ((omax (+ (current-column)
560 (- (or comment-fill-column fill-column)
561 (save-excursion (end-of-line) (current-column)))))
562 (omin (save-excursion (skip-chars-backward " \t")
563 (1+ (current-column)))))
564 (if (and (>= omax min) (<= omin max))
565 (progn (setq min (max omin min))
566 (setq max (min omax max)))
567 ;; Can't align with this anyway, so exit the loop.
568 (goto-char (point-max))))))
569 ;; Return the closest point to indent within min..max.
570 (max min (min max indent)))))
571
519;;;###autoload 572;;;###autoload
520(defun comment-indent (&optional continue) 573(defun comment-indent (&optional continue)
521 "Indent this line's comment to `comment-column', or insert an empty comment. 574 "Indent this line's comment to `comment-column', or insert an empty comment.
@@ -569,38 +622,9 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
569 (if (not indent) 622 (if (not indent)
570 ;; comment-indent-function refuses: delegate to line-indent. 623 ;; comment-indent-function refuses: delegate to line-indent.
571 (indent-according-to-mode) 624 (indent-according-to-mode)
572 ;; If the comment is at the left of code, adjust the indentation. 625 ;; If the comment is at the right of code, adjust the indentation.
573 (unless (save-excursion (skip-chars-backward " \t") (bolp)) 626 (unless (save-excursion (skip-chars-backward " \t") (bolp))
574 ;; Avoid moving comments past the fill-column. 627 (setq indent (comment-choose-indent indent)))
575 (let ((max (+ (current-column)
576 (- (or comment-fill-column fill-column)
577 (save-excursion (end-of-line) (current-column))))))
578 (if (<= max indent)
579 (setq indent max) ;Don't move past the fill column.
580 ;; We can choose anywhere between indent..max.
581 ;; Let's try to align to a comment on the previous line.
582 (let ((other nil)
583 (min (max indent
584 (save-excursion (skip-chars-backward " \t")
585 (1+ (current-column))))))
586 (save-excursion
587 (when (and (zerop (forward-line -1))
588 (setq other (comment-search-forward
589 (line-end-position) t)))
590 (goto-char other) (setq other (current-column))))
591 (if (and other (<= other max) (>= other min))
592 ;; There is a comment and it's in the range: bingo.
593 (setq indent other)
594 ;; Let's try to align to a comment on the next line, then.
595 (let ((other nil))
596 (save-excursion
597 (when (and (zerop (forward-line 1))
598 (setq other (comment-search-forward
599 (line-end-position) t)))
600 (goto-char other) (setq other (current-column))))
601 (if (and other (<= other max) (> other min))
602 ;; There is a comment and it's in the range: bingo.
603 (setq indent other))))))))
604 ;; Update INDENT to leave at least one space 628 ;; Update INDENT to leave at least one space
605 ;; after other nonwhite text on the line. 629 ;; after other nonwhite text on the line.
606 (save-excursion 630 (save-excursion
@@ -1020,7 +1044,8 @@ The strings used as comment starts are built from
1020 (lines (nth 2 style)) 1044 (lines (nth 2 style))
1021 (block (nth 1 style)) 1045 (block (nth 1 style))
1022 (multi (nth 0 style))) 1046 (multi (nth 0 style)))
1023 ;; we use `chars' instead of `syntax' because `\n' might be 1047
1048 ;; We use `chars' instead of `syntax' because `\n' might be
1024 ;; of end-comment syntax rather than of whitespace syntax. 1049 ;; of end-comment syntax rather than of whitespace syntax.
1025 ;; sanitize BEG and END 1050 ;; sanitize BEG and END
1026 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line) 1051 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)