X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/fca389d1d3253c58e198efaa28d175106ce5022a..eaa1fd6dbff8346eb38485de5ebf0fbfacf374d9:/lisp/newcomment.el diff --git a/lisp/newcomment.el b/lisp/newcomment.el index b607eb95df..0c49211869 100644 --- a/lisp/newcomment.el +++ b/lisp/newcomment.el @@ -1,6 +1,6 @@ ;;; newcomment.el --- (un)comment regions of buffers -*- lexical-binding: t -*- -;; Copyright (C) 1999-2014 Free Software Foundation, Inc. +;; Copyright (C) 1999-2015 Free Software Foundation, Inc. ;; Author: code extracted from Emacs-20's simple.el ;; Maintainer: Stefan Monnier @@ -179,6 +179,11 @@ comments always start in column zero.") "Non-nil if nested comments should be quoted. This should be locally set by each major mode if needed.") +(defvar comment-quote-nested-function #'comment-quote-nested-default + "Function to quote nested comments in a region. +It takes the same arguments as `comment-quote-nested-default', +and is called with the buffer narrowed to a single comment.") + (defvar comment-continue nil "Continuation string to insert for multiline comments. This string will be added at the beginning of each line except the very @@ -286,8 +291,8 @@ makes the comment easier to read. Default is 1. nil means 0." This is useful when style-conventions require a certain minimal offset. Python's PEP8 for example recommends two spaces, so you could do: -\(add-hook 'python-mode-hook - (lambda () (set (make-local-variable 'comment-inline-offset) 2))) +\(add-hook \\='python-mode-hook + (lambda () (set (make-local-variable \\='comment-inline-offset) 2))) See `comment-padding' for whole-line comments." :version "24.3" @@ -382,7 +387,7 @@ function should first call this function explicitly." (concat (unless (eq comment-use-syntax t) ;; `syntax-ppss' will detect escaping. "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)") - "\\(\\s<+\\|" + "\\(?:\\s<+\\|" (regexp-quote (comment-string-strip comment-start t t)) ;; Let's not allow any \s- but only [ \t] since \n ;; might be both a comment-end marker and \s-. @@ -412,28 +417,44 @@ function should first call this function explicitly." If UNP is non-nil, unquote nested comment markers." (setq cs (comment-string-strip cs t t)) (setq ce (comment-string-strip ce t t)) - (when (and comment-quote-nested (> (length ce) 0)) - (let ((re (concat (comment-quote-re ce unp) - "\\|" (comment-quote-re cs unp)))) - (goto-char (point-min)) - (while (re-search-forward re nil t) - (goto-char (match-beginning 0)) - (forward-char 1) - (if unp (delete-char 1) (insert "\\")) - (when (= (length ce) 1) - ;; If the comment-end is a single char, adding a \ after that - ;; "first" char won't deactivate it, so we turn such a CE - ;; into !CS. I.e. for pascal, we turn } into !{ - (if (not unp) - (when (string= (match-string 0) ce) - (replace-match (concat "!" cs) t t)) - (when (and (< (point-min) (match-beginning 0)) - (string= (buffer-substring (1- (match-beginning 0)) - (1- (match-end 0))) - (concat "!" cs))) - (backward-char 2) - (delete-char (- (match-end 0) (match-beginning 0))) - (insert ce)))))))) + (when (and comment-quote-nested + (> (length ce) 0)) + (funcall comment-quote-nested-function cs ce unp))) + +(defun comment-quote-nested-default (cs ce unp) + "Quote comment delimiters in the buffer. +It expects to be called with the buffer narrowed to a single comment. +It is used as a default for `comment-quote-nested-function'. + +The arguments CS and CE are strings matching comment starting and +ending delimiters respectively. + +If UNP is non-nil, comments are unquoted instead. + +To quote the delimiters, a \\ is inserted after the first +character of CS or CE. If CE is a single character it will +change CE into !CS." + (let ((re (concat (comment-quote-re ce unp) + "\\|" (comment-quote-re cs unp)))) + (goto-char (point-min)) + (while (re-search-forward re nil t) + (goto-char (match-beginning 0)) + (forward-char 1) + (if unp (delete-char 1) (insert "\\")) + (when (= (length ce) 1) + ;; If the comment-end is a single char, adding a \ after that + ;; "first" char won't deactivate it, so we turn such a CE + ;; into !CS. I.e. for pascal, we turn } into !{ + (if (not unp) + (when (string= (match-string 0) ce) + (replace-match (concat "!" cs) t t)) + (when (and (< (point-min) (match-beginning 0)) + (string= (buffer-substring (1- (match-beginning 0)) + (1- (match-end 0))) + (concat "!" cs))) + (backward-char 2) + (delete-char (- (match-end 0) (match-beginning 0))) + (insert ce))))))) ;;;; ;;;; Navigation @@ -1266,7 +1287,7 @@ Else, call `comment-indent'. You can configure `comment-style' to change the way regions are commented." (interactive "*P") (comment-normalize-vars) - (if (and mark-active transient-mark-mode) + (if (use-region-p) (comment-or-uncomment-region (region-beginning) (region-end) arg) (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$"))) ;; FIXME: If there's no comment to kill on this line and ARG is @@ -1451,6 +1472,38 @@ unless optional argument SOFT is non-nil." (end-of-line 0) (insert comend)))))))))))) +;;;###autoload +(defun comment-line (n) + "Comment or uncomment current line and leave point after it. +With positive prefix, apply to N lines including current one. +With negative prefix, apply to -N lines above. Also, further +consecutive invocations of this command will inherit the negative +argument. + +If region is active, comment lines in active region instead. +Unlike `comment-dwim', this always comments whole lines." + (interactive "p") + (if (use-region-p) + (comment-or-uncomment-region + (save-excursion + (goto-char (region-beginning)) + (line-beginning-position)) + (save-excursion + (goto-char (region-end)) + (line-end-position))) + (when (and (eq last-command 'comment-line-backward) + (natnump n)) + (setq n (- n))) + (let ((range + (list (line-beginning-position) + (goto-char (line-end-position n))))) + (comment-or-uncomment-region + (apply #'min range) + (apply #'max range))) + (forward-line 1) + (back-to-indentation) + (unless (natnump n) (setq this-command 'comment-line-backward)))) + (provide 'newcomment) ;;; newcomment.el ends here