Subject: bug#19665: 25.0.50; python.el: mark defun bug when point is in class statement

From: Carlos Pita <[email-masked]>
Date: Mon, 9 Feb 2015 14:36:33 -0300

Here is a variation of the last patch that behaves better when the
point is in empty lines above a defun.

Another entirely different approach to solve this issue without
redefining mark-defun could be to differentiate:

class M:
     def m():
          pass*

and

class M:
   def m():
      pass
*

(where * stands for the point)

In the second case, C-M-h will select the class, while in the first it
will select the method. Then, there would be no need to hack
mark-defun.

What do you think?
diff --git a/.emacs.d/lisp/python.el b/.emacs.d/lisp/python.el
index 30b62f5..0337a17 100644
--- a/.emacs.d/lisp/python.el
+++ b/.emacs.d/lisp/python.el
@@ -284,6 +284,7 @@
     (define-key map [remap backward-sentence] 'python-nav-backward-block)
     (define-key map [remap forward-sentence] 'python-nav-forward-block)
     (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)
     ;; Indent specific
     (define-key map "\177" 'python-indent-dedent-line-backspace)
@@ -1285,7 +1286,7 @@ With positive ARG search backwards, else search forwards."
                      0))))
          (found
           (progn
-            (when (and (< arg 0)
+            (when (and (> arg 0)
                        (python-info-looking-at-beginning-of-defun))
               (end-of-line 1))
             (while (and (funcall re-search-fn
@@ -4460,6 +4461,12 @@ default to utf-8."
 
 ;;; Utility functions
 
+(defun python-mark-defun ()
+  (interactive)
+  (when (python-info-looking-at-beginning-of-defun)
+    (end-of-line 1))
+  (mark-defun))
+
 (defun python-util-goto-line (line-number)
   "Move point to LINE-NUMBER."
   (goto-char (point-min))