From 97cb255360172980e7b79ed6a8cb35abbc58f897 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sun, 8 Feb 2015 19:03:17 -0200 Subject: [PATCH] newcomment.el (comment-line): New command on C-x C-;. --- lisp/ChangeLog | 6 ++++++ lisp/bindings.el | 1 + lisp/newcomment.el | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index b862d42b961..64424b74c95 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,9 @@ +2015-02-08 Artur Malabarba + + * newcomment.el (comment-line): New command. + + * bindings.el (ctl-x-map): Bind to `C-x C-;'. + 2015-02-08 Oleh Krehel * outline.el (outline-show-entry): Fix one invisible char for the diff --git a/lisp/bindings.el b/lisp/bindings.el index 883914ecdc2..4cc9f6ad368 100644 --- a/lisp/bindings.el +++ b/lisp/bindings.el @@ -1130,6 +1130,7 @@ if `inhibit-field-text-motion' is non-nil." (define-key esc-map "j" 'indent-new-comment-line) (define-key esc-map "\C-j" 'indent-new-comment-line) (define-key ctl-x-map ";" 'comment-set-column) +(define-key ctl-x-map "C-;" 'comment-line) (define-key ctl-x-map "f" 'set-fill-column) (define-key ctl-x-map "$" 'set-selective-display) diff --git a/lisp/newcomment.el b/lisp/newcomment.el index e307eac94eb..aabafc76b9a 100644 --- a/lisp/newcomment.el +++ b/lisp/newcomment.el @@ -1451,6 +1451,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 -- 2.11.4.GIT