[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Adding comment features to Python progmode.
From: |
Alban Gruin |
Subject: |
[PATCH] Adding comment features to Python progmode. |
Date: |
Sun, 13 Sep 2015 00:07:52 +0200 |
* lisp/progmodes/python.el: adding features to comment and uncomment a line
or a region (functions `python-comment-line`,
`python-comment-uncomment-line`, `python-comment-region` and
`python-comment-region-uncomment`).
`python-comment-line` and `python-comment-uncomment-line` functions are
non-interactive and can only comment (or uncomment) the current line.
`python-comment-region` and `python-comment-uncomment-region` functions
are interactive and can comment (or uncomment) the current line or region.
`python-comment-region` is bound to C-c C-a, and
`python-comment-uncomment-region` is bound to C-c C-q. Theses functions
are available in the Python menu.
---
lisp/progmodes/python.el | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 9528ffe..aad7462 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -306,6 +306,9 @@
(define-key map [remap backward-up-list] 'python-nav-backward-up-list)
(define-key map [remap mark-defun] 'python-mark-defun)
(define-key map "\C-c\C-j" 'imenu)
+ ;; Comments
+ (define-key map "\C-c\C-a" 'python-comment-region)
+ (define-key map "\C-c\C-q" 'python-comment-uncomment-region)
;; Indent specific
(define-key map "\177" 'python-indent-dedent-line-backspace)
(define-key map (kbd "<backtab>") 'python-indent-dedent-line)
@@ -341,6 +344,11 @@
["Shift region right" python-indent-shift-right :active mark-active
:help "Shift region right by a single indentation step"]
"-"
+ ["Comment region" python-comment-region
+ :help "Comment the selected region or the current line if not
commented"]
+ ["Uncomment region" python-comment-uncomment-region
+ :help "Uncomment the selected region or the current line if
commented"]
+ "-"
["Start of def/class" beginning-of-defun
:help "Go to start of outermost definition around point"]
["End of def/class" end-of-defun
@@ -750,6 +758,52 @@ variable for indentation customizations, refactor your
code to
work on `python-indent-calculate-indentation' instead."
"24.5")
+(defun python-comment-line ()
+ "Comment the current line if not commented"
+ (save-excursion
+ (when (not (python-info-current-line-comment-p))
+ (goto-char (+ (line-beginning-position) (current-indentation)))
+ (insert "#"))))
+
+(defun python-comment-uncomment-line ()
+ "Uncomment the current line if commented"
+ (save-excursion
+ (when (python-info-current-line-comment-p)
+ (goto-char (+ (line-beginning-position) (current-indentation)))
+ (delete-char 1))))
+
+(defun python-comment-region ()
+ "Comment the selected region or the current line"
+ (interactive)
+ (save-excursion
+ (let ((start)
+ (end))
+ (if mark-active
+ (setq start (min (point) (mark))
+ end (max (point) (mark)))
+ (setq start (point)
+ end (point)))
+ (goto-char start)
+ (while (<= (point) end)
+ (python-comment-line)
+ (forward-line)))))
+
+(defun python-comment-uncomment-region ()
+ "Uncomment the selected region or the current line"
+ (interactive)
+ (save-excursion
+ (let ((start)
+ (end))
+ (if mark-active
+ (setq start (min (point) (mark))
+ end (max (point) (mark)))
+ (setq start (point)
+ end (point)))
+ (goto-char start)
+ (while (<= (point) end)
+ (python-comment-uncomment-line)
+ (forward-line)))))
+
(defun python-indent-guess-indent-offset ()
"Guess and set `python-indent-offset' for the current buffer."
(interactive)
--
2.5.1
- [PATCH] Adding comment features to Python progmode.,
Alban Gruin <=