unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Adding comment features to Python progmode.
@ 2015-09-12 22:07 Alban Gruin
  2015-09-13 13:16 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Alban Gruin @ 2015-09-12 22:07 UTC (permalink / raw)
  To: emacs-devel; +Cc: Alban Gruin

* 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




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Adding comment features to Python progmode.
  2015-09-12 22:07 [PATCH] Adding comment features to Python progmode Alban Gruin
@ 2015-09-13 13:16 ` Stefan Monnier
  2015-09-14 19:46   ` Alban Gruin
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2015-09-13 13:16 UTC (permalink / raw)
  To: Alban Gruin; +Cc: emacs-devel

> * lisp/progmodes/python.el: adding features to comment and uncomment a line
>   or a region (functions `python-comment-line`,

Could you explain what this does compared to the built-in support
available via M-; (and M-x comment-region, M-x uncomment-region, ...)?


        Stefan



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Adding comment features to Python progmode.
  2015-09-13 13:16 ` Stefan Monnier
@ 2015-09-14 19:46   ` Alban Gruin
  2015-09-15  0:59     ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Alban Gruin @ 2015-09-14 19:46 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

comment-region comments only a part of a line (where the mark is, which is
useless in Python since you just have to put a hash instead invoking a
command), where python-comment-region comments the entire line.

-- 
   Alban Gruin



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Adding comment features to Python progmode.
  2015-09-14 19:46   ` Alban Gruin
@ 2015-09-15  0:59     ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2015-09-15  0:59 UTC (permalink / raw)
  To: Alban Gruin; +Cc: emacs-devel

> comment-region comments only a part of a line (where the mark is, which is
> useless in Python since you just have to put a hash instead invoking a
> command), where python-comment-region comments the entire line.

Python is not special in this regard.  Whether that's useless or not
depends on your habits.

It's easy to get your behavior on top of the existing function, so
there's no need to write all that new code.


        Stefan



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-09-15  0:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-12 22:07 [PATCH] Adding comment features to Python progmode Alban Gruin
2015-09-13 13:16 ` Stefan Monnier
2015-09-14 19:46   ` Alban Gruin
2015-09-15  0:59     ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).