From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Paul Pogonyshev Newsgroups: gmane.emacs.devel Subject: Re: Which Function mode and Python mode Date: Sun, 1 Jul 2007 14:09:25 +0300 Message-ID: <200707011409.26249.pogonyshev@gmx.net> References: <200706300110.12506.pogonyshev@gmx.net> <200707010011.17127.pogonyshev@gmx.net> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1183287414 8587 80.91.229.12 (1 Jul 2007 10:56:54 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 1 Jul 2007 10:56:54 +0000 (UTC) Cc: Stefan Monnier To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Jul 01 12:56:51 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1I4x6l-0005yI-6S for ged-emacs-devel@m.gmane.org; Sun, 01 Jul 2007 12:56:49 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1I4x6i-0005jj-B6 for ged-emacs-devel@m.gmane.org; Sun, 01 Jul 2007 06:56:44 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1I4x6J-0005dv-VK for emacs-devel@gnu.org; Sun, 01 Jul 2007 06:56:20 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1I4x6H-0005cy-Gm for emacs-devel@gnu.org; Sun, 01 Jul 2007 06:56:19 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1I4x6H-0005cd-2J for emacs-devel@gnu.org; Sun, 01 Jul 2007 06:56:17 -0400 Original-Received: from mail.gmx.net ([213.165.64.20]) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1I4x69-0006AB-F7 for emacs-devel@gnu.org; Sun, 01 Jul 2007 06:56:10 -0400 Original-Received: (qmail invoked by alias); 01 Jul 2007 10:55:57 -0000 Original-Received: from unknown (EHLO [80.94.234.57]) [80.94.234.57] by mail.gmx.net (mp002) with SMTP; 01 Jul 2007 12:55:57 +0200 X-Authenticated: #16844820 X-Provags-ID: V01U2FsdGVkX1+tBlh2AEetEovRgu8vsFBjWAFrQEFO84O6fuqg1Z I5/4oIv3L+/CuI User-Agent: KMail/1.7.2 In-Reply-To: Content-Disposition: inline X-Y-GMX-Trusted: 0 X-detected-kernel: Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:74095 Archived-At: 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 * 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."