emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Which Function mode and Python mode


From: Paul Pogonyshev
Subject: Re: Which Function mode and Python mode
Date: Sun, 1 Jul 2007 14:09:25 +0300
User-agent: KMail/1.7.2

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  <address@hidden>

        * 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."




reply via email to

[Prev in Thread] Current Thread [Next in Thread]