unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Which Function mode and Python mode
@ 2007-06-29 22:10 Paul Pogonyshev
  2007-06-30 18:31 ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Paul Pogonyshev @ 2007-06-29 22:10 UTC (permalink / raw)
  To: emacs-devel

Hi,

This short patch adds support for Which Function minor mode to Python
mode.  It also adds Python mode to the default list of modes where
Which Function mode is in effect.

Paul


2007-06-29  Paul Pogonyshev  <pogonyshev@gmx.net>

	* progmodes/which-func.el (which-func-modes): Add `python-mode'.

	* progmodes/python.el (python-def-or-class-regexp): New defconst.
	(python-which-func): New function.
	(python-mode): Hook it up to `which-func-functions'.


Index: lisp/progmodes/python.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/python.el,v
retrieving revision 1.62
diff -u -p -r1.62 python.el
--- lisp/progmodes/python.el	14 Jun 2007 00:11:40 -0000	1.62
+++ lisp/progmodes/python.el	29 Jun 2007 21:52:14 -0000
@@ -996,6 +996,27 @@ don't move and return nil.  Otherwise re
 			  (throw 'done t)))))))
       (setq arg (1- arg)))
     (zerop arg)))
+
+(defconst python-def-or-class-regexp
+  (rx (0+ blank) (or "def" "class") (1+ blank)
+      (group symbol-start (+? nonl) symbol-end))
+  "Regular expression matching function or class definition.")
+
+(defun python-which-func ()
+  (let ((components nil))
+    (save-excursion
+      (beginning-of-line)
+      (when (looking-at python-def-or-class-regexp)
+	(setq components (list (match-string-no-properties 1))))
+      (while (python-beginning-of-block)
+	(if (looking-at python-def-or-class-regexp)
+	    (setq components (cons (match-string-no-properties 1) components)))))
+    (when components
+      (apply 'concat
+	     (car components)
+	     (mapcar (lambda (component)
+		       (concat "." component))
+		     (cdr components))))))
  
 ;;;; Imenu.
 
@@ -2248,6 +2269,7 @@ with skeleton expansions for compound st
   (set (make-local-variable 'beginning-of-defun-function)
        'python-beginning-of-defun)
   (set (make-local-variable 'end-of-defun-function) 'python-end-of-defun)
+  (add-hook 'which-func-functions 'python-which-func nil t)
   (setq imenu-create-index-function #'python-imenu-create-index)
   (set (make-local-variable 'eldoc-documentation-function)
        #'python-eldoc-function)
Index: lisp/progmodes/which-func.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/which-func.el,v
retrieving revision 1.17
diff -u -p -r1.17 which-func.el
--- lisp/progmodes/which-func.el	21 Jan 2007 03:20:44 -0000	1.17
+++ lisp/progmodes/which-func.el	29 Jun 2007 21:52:14 -0000
@@ -76,8 +76,8 @@
   :version "20.3")
 
 (defcustom which-func-modes
-  '(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode makefile-mode
-		    sh-mode fortran-mode f90-mode ada-mode)
+  '(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode python-mode
+		    makefile-mode sh-mode fortran-mode f90-mode ada-mode)
   "List of major modes for which Which Function mode should be used.
 For other modes it is disabled.  If this is equal to t,
 then Which Function mode is enabled in any major mode that supports it."

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

* Re: Which Function mode and Python mode
  2007-06-29 22:10 Which Function mode and Python mode Paul Pogonyshev
@ 2007-06-30 18:31 ` Stefan Monnier
  2007-06-30 21:11   ` Paul Pogonyshev
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2007-06-30 18:31 UTC (permalink / raw)
  To: Paul Pogonyshev; +Cc: emacs-devel

> This short patch adds support for Which Function minor mode to Python
> mode.  It also adds Python mode to the default list of modes where
> Which Function mode is in effect.

How does your code compare to the result you get with the default which-func
support, which relies on imenu's data?


        Stefan

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

* Re: Which Function mode and Python mode
  2007-06-30 18:31 ` Stefan Monnier
@ 2007-06-30 21:11   ` Paul Pogonyshev
  2007-07-01  2:01     ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Paul Pogonyshev @ 2007-06-30 21:11 UTC (permalink / raw)
  To: emacs-devel; +Cc: Stefan Monnier

Stefan Monnier wrote:
> > This short patch adds support for Which Function minor mode to Python
> > mode.  It also adds Python mode to the default list of modes where
> > Which Function mode is in effect.
> 
> How does your code compare to the result you get with the default which-func
> support, which relies on imenu's data?

class SomeClass (object):
    class Nested (object):
        def __init__(self):
            pass

Put the point on `pass' line.  With my code: "SomeClass.Nested.__init__".
With original imenu-dependent code: " class SomeClass".

I personally see this as an improvement.  With imenu approach it is
necessary to regenerate the menu, as far as I understand.  I personally
see little value in having "class X" in mode line, I'm more interested
in which exactly method of that classn the point is.  Finally, "class"
is probably redundant, because at least with standard Python naming
scheme, it is very easy to tell classes and methods/functions just by
their names.

Still, I agree it is somewhat of code duplication.  I was not aware
there was imenu approach.  It never manifested itself on my Python
buffers mode lines...

Paul

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

* Re: Which Function mode and Python mode
  2007-06-30 21:11   ` Paul Pogonyshev
@ 2007-07-01  2:01     ` Stefan Monnier
  2007-07-01 11:09       ` Paul Pogonyshev
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2007-07-01  2:01 UTC (permalink / raw)
  To: Paul Pogonyshev; +Cc: emacs-devel

>> > This short patch adds support for Which Function minor mode to Python
>> > mode.  It also adds Python mode to the default list of modes where
>> > Which Function mode is in effect.
>> 
>> How does your code compare to the result you get with the default which-func
>> support, which relies on imenu's data?

> class SomeClass (object):
>     class Nested (object):
>         def __init__(self):
>             pass

> Put the point on `pass' line.  With my code: "SomeClass.Nested.__init__".
> With original imenu-dependent code: " class SomeClass".

Great.  Does it ever happen that the nesting is high enough that the length
can becomes a problem?
Can you adjust your patch so that it's also used by C-x 4 a ?


        Stefan

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

* Re: Which Function mode and Python mode
  2007-07-01  2:01     ` Stefan Monnier
@ 2007-07-01 11:09       ` Paul Pogonyshev
  2007-07-12  4:37         ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Paul Pogonyshev @ 2007-07-01 11:09 UTC (permalink / raw)
  To: emacs-devel; +Cc: Stefan Monnier

Stefan Monnier wrote:
> >> > This short patch adds support for Which Function minor mode to Python
> >> > mode.  It also adds Python mode to the default list of modes where
> >> > Which Function mode is in effect.
> >> 
> >> How does your code compare to the result you get with the default which-func
> >> support, which relies on imenu's data?
> 
> > class SomeClass (object):
> >     class Nested (object):
> >         def __init__(self):
> >             pass
> 
> > Put the point on `pass' line.  With my code: "SomeClass.Nested.__init__".
> > With original imenu-dependent code: " class SomeClass".
> 
> Great.  Does it ever happen that the nesting is high enough that the length
> can becomes a problem?

Yes.  Revised patch solves this problem.

> Can you adjust your patch so that it's also used by C-x 4 a ?

Actually, C-x 4 a already seems to do that.  I reused the functionality
in the revised patch below.

Note that it is quite slow when you are inside a long toplevel declaration,
like a class of 500 lines.  My proposal for generic parsing cache should
also solve this, if it is adopted.

Paul


2007-07-01  Paul Pogonyshev  <pogonyshev@gmx.net>

        * progmodes/which-func.el (which-func-modes): Add `python-mode'.

	* progmodes/python.el (python-which-func-length-limit): New
	defconst.
	(python-which-func): New function.
	(python-current-defun): Add optional `length-limit' and try to fit
	computed function name to that length.
	(python-mode): Hook `python-which-func' up.


Index: lisp/progmodes/python.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/python.el,v
retrieving revision 1.62
diff -u -p -r1.62 python.el
--- lisp/progmodes/python.el	14 Jun 2007 00:11:40 -0000	1.62
+++ lisp/progmodes/python.el	1 Jul 2007 10:50:42 -0000
@@ -996,6 +996,15 @@ don't move and return nil.  Otherwise re
 			  (throw 'done t)))))))
       (setq arg (1- arg)))
     (zerop arg)))
+
+(defconst python-which-func-length-limit 40
+  "Non-strict length limit for `python-which-func' output.")
+
+(defun python-which-func ()
+  (let ((function-name (python-current-defun python-which-func-length-limit)))
+    (set-text-properties 0 (length function-name) nil function-name)
+    function-name))
+
  
 ;;;; Imenu.
 
@@ -1814,22 +1823,30 @@ of current line."
   (1+ (/ (current-indentation) python-indent)))
 
 ;; Fixme: Consider top-level assignments, imports, &c.
-(defun python-current-defun ()
+(defun python-current-defun (&optional length-limit)
   "`add-log-current-defun-function' for Python."
   (save-excursion
     ;; Move up the tree of nested `class' and `def' blocks until we
     ;; get to zero indentation, accumulating the defined names.
     (let ((start t)
-	  accum)
-      (while (or start (> (current-indentation) 0))
+	  (accum)
+	  (length -1))
+      (while (and (or start (> (current-indentation) 0))
+		  (or (null length-limit)
+		      (null (cdr accum))
+		      (< length length-limit)))
 	(setq start nil)
 	(python-beginning-of-block)
 	(end-of-line)
 	(beginning-of-defun)
-	(if (looking-at (rx (0+ space) (or "def" "class") (1+ space)
-			    (group (1+ (or word (syntax symbol))))))
-	    (push (match-string 1) accum)))
-      (if accum (mapconcat 'identity accum ".")))))
+	(when (looking-at (rx (0+ space) (or "def" "class") (1+ space)
+			      (group (1+ (or word (syntax symbol))))))
+	  (push (match-string 1) accum)
+	  (setq length (+ length 1 (length (car accum))))))
+      (when accum
+	(when (and length-limit (> length length-limit))
+	  (setcar accum ".."))
+	(mapconcat 'identity accum ".")))))
 
 (defun python-mark-block ()
   "Mark the block around point.
@@ -2248,6 +2265,7 @@ with skeleton expansions for compound st
   (set (make-local-variable 'beginning-of-defun-function)
        'python-beginning-of-defun)
   (set (make-local-variable 'end-of-defun-function) 'python-end-of-defun)
+  (add-hook 'which-func-functions 'python-which-func nil t)
   (setq imenu-create-index-function #'python-imenu-create-index)
   (set (make-local-variable 'eldoc-documentation-function)
        #'python-eldoc-function)
Index: lisp/progmodes/which-func.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/which-func.el,v
retrieving revision 1.17
diff -u -p -r1.17 which-func.el
--- lisp/progmodes/which-func.el	21 Jan 2007 03:20:44 -0000	1.17
+++ lisp/progmodes/which-func.el	1 Jul 2007 10:50:42 -0000
@@ -76,8 +76,8 @@
   :version "20.3")
 
 (defcustom which-func-modes
-  '(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode makefile-mode
-		    sh-mode fortran-mode f90-mode ada-mode)
+  '(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode python-mode
+		    makefile-mode sh-mode fortran-mode f90-mode ada-mode)
   "List of major modes for which Which Function mode should be used.
 For other modes it is disabled.  If this is equal to t,
 then Which Function mode is enabled in any major mode that supports it."

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

* Re: Which Function mode and Python mode
  2007-07-01 11:09       ` Paul Pogonyshev
@ 2007-07-12  4:37         ` Stefan Monnier
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2007-07-12  4:37 UTC (permalink / raw)
  To: Paul Pogonyshev; +Cc: emacs-devel

> Yes.  Revised patch solves this problem.

Thanks, installed,


        Stefan

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

end of thread, other threads:[~2007-07-12  4:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-29 22:10 Which Function mode and Python mode Paul Pogonyshev
2007-06-30 18:31 ` Stefan Monnier
2007-06-30 21:11   ` Paul Pogonyshev
2007-07-01  2:01     ` Stefan Monnier
2007-07-01 11:09       ` Paul Pogonyshev
2007-07-12  4:37         ` 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).