M-x beginning-of-defun

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

M-x beginning-of-defun

Andreas Röhler-2

Hi Barry,

`beginning-of-defun-raw' in GNU Emacs has a nice
feature, enabling functions instead of regexps to
determine beginning and end of defun, i.e. function or form.

In python-mode.el I see you solved this by binding the
beginning- and end-of-defun keys seperatly.
(Maybe, because XEmacs lacks this feature?)

However, this lets commands like M-x beginning-of-defun
empty.

My suggestion is to follow GNU conventions here, by
introducing the missing forms in XEmacs lisp.el and
python-mode.el alike.

Herewith a patch against latest python-mode.el, which I
enjoy, thanks BTW.

Also a patch against latest XEmacs lisp.el.

Best wishes

Andreas Röhler



diff -u -b python-mode.el python-mode.el
--- python-mode.el 2008-11-29 17:29:45.000000000 +0100
+++ python-mode.el 2008-11-29 17:37:18.000000000 +0100
@@ -2652,6 +2652,22 @@
       (goto-char start)
       (error "Enclosing block not found"))))
 
+
+;; credits to python.el, defining derived mode
+;; (set (make-local-variable 'beginning-of-defun-function)
+;; 'python-beginning-of-defun)
+;; (set (make-local-variable 'end-of-defun-function) 'python-end-of-defun)
+
+(defun py-beg-of-defun-function ()
+  (set (make-local-variable 'beginning-of-defun-function)
+       'py-beginning-of-def-or-class))
+
+(defun py-end-of-defun-function ()
+  (set (make-local-variable 'end-of-defun-function) 'py-end-of-def-or-class))
+
+(add-hook 'python-mode-hook 'py-beg-of-defun-function)
+(add-hook 'python-mode-hook 'py-end-of-defun-function)
+
 (defun py-beginning-of-def-or-class (&optional class count)
   "Move point to start of `def' or `class'.
 

Diff finished.  Sat Nov 29 17:37:44 2008

diff -u lisp.el lisp.el
--- lisp.el 2008-11-29 17:00:18.000000000 +0100
+++ lisp.el 2008-11-29 17:00:18.000000000 +0100
@@ -155,6 +155,21 @@
   (interactive "p")
   (kill-sexp (- (or arg 1))))
 
+
+;; derived stuff from GNU Emacs
+(defvar beginning-of-defun-function nil
+  "If non-nil, function for `beginning-of-defun-raw' to call.
+This is used to find the beginning of the defun instead of using the
+normal recipe (see `beginning-of-defun').  Modes can define this
+if defining `defun-prompt-regexp' is not sufficient to handle the mode's
+needs.")
+
+(defvar end-of-defun-function nil
+  "If non-nil, function for `end-of-defun' to call.
+This is used to find the end of the defun instead of using the normal
+recipe (see `end-of-defun').  Modes can define this if the
+normal method is not appropriate.")
+
 (defun beginning-of-defun (&optional arg)
   "Move backward to the beginning of a defun.
 With argument, do it that many times.  Negative arg -N
@@ -175,13 +190,19 @@
 This is identical to beginning-of-defun, except that point does not move
 to the beginning of the line when `defun-prompt-regexp' is non-nil."
   (interactive "p")
-  (and arg (< arg 0) (not (eobp)) (forward-char 1))
-  (and (re-search-backward (if defun-prompt-regexp
-       (concat "^\\s(\\|"
-       "\\(" defun-prompt-regexp "\\)\\s(")
-     "^\\s(")
-   nil 'move (or arg 1))
-       (progn (goto-char (1- (match-end 0)))) t))
+   ;; (and arg (< arg 0) (not (eobp)) (forward-char 1))
+  (unless arg (setq arg 1))
+  (cond
+   (beginning-of-defun-function
+    (if (> arg 0)
+ (dotimes (i arg)
+  (funcall beginning-of-defun-function))))
+   (t (re-search-backward (if defun-prompt-regexp
+                              (concat "^\\s(\\|"
+                                      "\\(" defun-prompt-regexp "\\)\\s(")
+                            "^\\s(")
+    nil 'move (or arg 1))
+ (progn (goto-char (1- (match-end 0)))) t)))
 
 ;; XEmacs change (optional buffer parameter)
 (defun buffer-end (arg &optional buffer)
@@ -198,6 +219,10 @@
   ;; XEmacs change (for zmacs regions)
   (interactive "_p")
   (if (or (null arg) (= arg 0)) (setq arg 1))
+  (if end-of-defun-function
+      (if (> arg 0)
+  (dotimes (i arg)
+    (funcall end-of-defun-function)))
   (let ((first t))
     (while (and (> arg 0) (< (point) (point-max)))
       (let ((pos (point))) ; XEmacs -- remove unused npos.
@@ -229,7 +254,7 @@
   (if (looking-at "\\s<\\|\n")
       (forward-line 1)))
       (goto-char (point-min)))))
-      (setq arg (1+ arg)))))
+      (setq arg (1+ arg))))))
 
 (defun mark-defun ()
   "Put mark at end of this defun, point at beginning.

_______________________________________________
Python-mode mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: M-x beginning-of-defun

Barry Warsaw
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 29, 2008, at 12:02 PM, Andreas Roehler wrote:

> `beginning-of-defun-raw' in GNU Emacs has a nice
> feature, enabling functions instead of regexps to
> determine beginning and end of defun, i.e. function or form.
>
> In python-mode.el I see you solved this by binding the
> beginning- and end-of-defun keys seperatly.
> (Maybe, because XEmacs lacks this feature?)
>
> However, this lets commands like M-x beginning-of-defun
> empty.
>
> My suggestion is to follow GNU conventions here, by
> introducing the missing forms in XEmacs lisp.el and
> python-mode.el alike.
>
> Herewith a patch against latest python-mode.el, which I
> enjoy, thanks BTW.
>
> Also a patch against latest XEmacs lisp.el.

Thanks for the contribution Andreas, but I don't think we can accept a  
patch that relies on patching XEmacs.  I don't have the energy for  
that.  ;)

Can you rewrite the patch so that it doesn't rely on a XEmacs change?  
Also, please open a bug on Launchpad and attach the patch to that, or  
create a branch with your changes that would be easy for us to review  
and merge.  Having patches sitting in an inbox is a surefire way to  
have it get lost.

Thanks,
Barry

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Darwin)

iQCVAwUBSTGOgnEjvBPtnXfVAQLlxAP+J7UUfTG6Ph44w5fKw+LdTdgCtPaeui05
2QjxHc13GUivvGR3kFP7seqfGnMLFy2MIpU/O4HBQ+nQHrHmEHy+tYfSOjimbo/m
54azM0BczDi+B7KeQWBxgMpS6SzMPGqdtDJ/PQlwupMGJW8FHtqPm8q28XQ+Ww1s
NjIbF+jc74U=
=lris
-----END PGP SIGNATURE-----
_______________________________________________
Python-mode mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: M-x beginning-of-defun

Andreas Röhler-2
Barry Warsaw wrote:

> On Nov 29, 2008, at 12:02 PM, Andreas Roehler wrote:
>
>> `beginning-of-defun-raw' in GNU Emacs has a nice
>> feature, enabling functions instead of regexps to
>> determine beginning and end of defun, i.e. function or form.
>
>> In python-mode.el I see you solved this by binding the
>> beginning- and end-of-defun keys seperatly.
>> (Maybe, because XEmacs lacks this feature?)
>
>> However, this lets commands like M-x beginning-of-defun
>> empty.
>
>> My suggestion is to follow GNU conventions here, by
>> introducing the missing forms in XEmacs lisp.el and
>> python-mode.el alike.
>
>> Herewith a patch against latest python-mode.el, which I
>> enjoy, thanks BTW.
>
>> Also a patch against latest XEmacs lisp.el.
>
> Thanks for the contribution Andreas, but I don't think we can accept a
> patch that relies on patching XEmacs.  

It doesn't rely on that lisp.el patch. It simply will not work with XEmacs,
i.e. in Xemacs everything will be unchanged,
whilst with GNU Emacs "M-x beginning-of-defun" etc. will work then.


I don't have the energy for
> that.  ;)

As the matter seems important, I'm rather confident to convince them.

As said, there is no restriction or bad thing for python-mode anyway.

OTOH should the lisp.el patch be applied, it offers some more chances to clean up the code,
using defaults instead of peciular solutions.

>
> Can you rewrite the patch so that it doesn't rely on a XEmacs change?

As mentioned above, no rewrite needed.

> Also, please open a bug on Launchpad and attach the patch to that, or
> create a branch with your changes that would be easy for us to review
> and merge.  

Ok, I'll try that.

Having patches sitting in an inbox is a surefire way to have
> it get lost.

Probably you are right, I understand that.

Thanks again.

Andreas

>
> Thanks,
> Barry
>
_______________________________________________
Python-mode mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode