all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Paul Pogonyshev <pogonyshev@gmx.net>
To: emacs-devel@gnu.org
Cc: Stefan Monnier <monnier@iro.umontreal.ca>
Subject: Re: Which Function mode and Python mode
Date: Sun, 1 Jul 2007 14:09:25 +0300	[thread overview]
Message-ID: <200707011409.26249.pogonyshev@gmx.net> (raw)
In-Reply-To: <jwvir94hoin.fsf-monnier+emacs@gnu.org>

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

  reply	other threads:[~2007-07-01 11:09 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2007-07-12  4:37         ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200707011409.26249.pogonyshev@gmx.net \
    --to=pogonyshev@gmx.net \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.