1 |
;;; newcomment.el --- (un)comment regions of buffers |
;;; newcomment.el --- (un)comment regions of buffers |
2 |
|
|
3 |
;; Copyright (C) 1999, 2000 Free Software Foundation Inc. |
;; Copyright (C) 1999,2000,2003 Free Software Foundation Inc. |
4 |
|
|
5 |
;; Author: code extracted from Emacs-20's simple.el |
;; Author: code extracted from Emacs-20's simple.el |
6 |
;; Maintainer: Stefan Monnier <monnier@cs.yale.edu> |
;; Maintainer: Stefan Monnier <monnier@cs.yale.edu> |
493 |
;; Compute desired indent. |
;; Compute desired indent. |
494 |
(setq indent (save-excursion (funcall comment-indent-function))) |
(setq indent (save-excursion (funcall comment-indent-function))) |
495 |
(if (not indent) |
(if (not indent) |
496 |
;; comment-indent-function refuses: delegate to indent. |
;; comment-indent-function refuses: delegate to line-indent. |
497 |
(indent-according-to-mode) |
(indent-according-to-mode) |
498 |
;; Avoid moving comments past the fill-column. |
;; Avoid moving comments past the fill-column. |
499 |
(unless (save-excursion (skip-chars-backward " \t") (bolp)) |
(unless (save-excursion (skip-chars-backward " \t") (bolp)) |
500 |
(setq indent |
(let ((max (+ (current-column) |
|
(min indent |
|
|
(+ (current-column) |
|
501 |
(- (or comment-fill-column fill-column) |
(- (or comment-fill-column fill-column) |
502 |
(save-excursion (end-of-line) (current-column))))))) |
(save-excursion (end-of-line) (current-column)))))) |
503 |
|
(if (<= max indent) |
504 |
|
(setq indent max) ;Don't move past the fill column. |
505 |
|
;; We can choose anywhere between indent..max. |
506 |
|
;; Let's try to align to a comment on the previous line. |
507 |
|
(let ((other nil)) |
508 |
|
(save-excursion |
509 |
|
(when (and (zerop (forward-line -1)) |
510 |
|
(setq other (comment-search-forward |
511 |
|
(line-end-position) t))) |
512 |
|
(goto-char other) (setq other (current-column)))) |
513 |
|
(if (and other (<= other max) (> other indent)) |
514 |
|
;; There is a comment and it's in the range: bingo. |
515 |
|
(setq indent other) |
516 |
|
;; Let's try to align to a comment on the next line, then. |
517 |
|
(let ((other nil)) |
518 |
|
(save-excursion |
519 |
|
(when (and (zerop (forward-line 1)) |
520 |
|
(setq other (comment-search-forward |
521 |
|
(line-end-position) t))) |
522 |
|
(goto-char other) (setq other (current-column)))) |
523 |
|
(if (and other (<= other max) (> other indent)) |
524 |
|
;; There is a comment and it's in the range: bingo. |
525 |
|
(setq indent other)))))))) |
526 |
(unless (= (current-column) indent) |
(unless (= (current-column) indent) |
527 |
;; If that's different from current, change it. |
;; If that's different from current, change it. |
528 |
(delete-region (point) (progn (skip-chars-backward " \t") (point))) |
(delete-region (point) (progn (skip-chars-backward " \t") (point))) |
785 |
indentation to be kept as it was before narrowing." |
indentation to be kept as it was before narrowing." |
786 |
(declare (debug t) (indent 2)) |
(declare (debug t) (indent 2)) |
787 |
(let ((bindent (make-symbol "bindent"))) |
(let ((bindent (make-symbol "bindent"))) |
788 |
`(let ((,bindent (save-excursion (goto-char beg) (current-column)))) |
`(let ((,bindent (save-excursion (goto-char ,beg) (current-column)))) |
789 |
(save-restriction |
(save-restriction |
790 |
(narrow-to-region beg end) |
(narrow-to-region ,beg ,end) |
791 |
(goto-char (point-min)) |
(goto-char (point-min)) |
792 |
(insert (make-string ,bindent ? )) |
(insert (make-string ,bindent ? )) |
793 |
(prog1 |
(prog1 |
1009 |
This has no effect in modes that do not define a comment syntax." |
This has no effect in modes that do not define a comment syntax." |
1010 |
:type 'boolean) |
:type 'boolean) |
1011 |
|
|
1012 |
(defun comment-valid-prefix (prefix compos) |
(defun comment-valid-prefix-p (prefix compos) |
1013 |
(or |
(or |
1014 |
;; Accept any prefix if the current comment is not EOL-terminated. |
;; Accept any prefix if the current comment is not EOL-terminated. |
1015 |
(save-excursion (goto-char compos) (comment-forward) (not (bolp))) |
(save-excursion (goto-char compos) (comment-forward) (not (bolp))) |
1016 |
;; Accept any prefix that starts with a comment-start marker. |
;; Accept any prefix that starts with a comment-start marker. |
1017 |
(string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)") |
(string-match (concat "\\`[ \t]*\\(?:" comment-start-skip "\\)") |
1018 |
fill-prefix))) |
prefix))) |
1019 |
|
|
1020 |
;;;###autoload |
;;;###autoload |
1021 |
(defun comment-indent-new-line (&optional soft) |
(defun comment-indent-new-line (&optional soft) |
1069 |
;; a comment and the prefix is not a comment starter. |
;; a comment and the prefix is not a comment starter. |
1070 |
((and fill-prefix |
((and fill-prefix |
1071 |
(or (not compos) |
(or (not compos) |
1072 |
(comment-valid-prefix fill-prefix compos))) |
(comment-valid-prefix-p fill-prefix compos))) |
1073 |
(indent-to-left-margin) |
(indent-to-left-margin) |
1074 |
(insert-and-inherit fill-prefix)) |
(insert-and-inherit fill-prefix)) |
1075 |
;; If we're not inside a comment, just try to indent. |
;; If we're not inside a comment, just try to indent. |