emacs-devel
[Top][All Lists]
Advanced

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

Which Function mode and Python mode


From: Paul Pogonyshev
Subject: Which Function mode and Python mode
Date: Sat, 30 Jun 2007 01:10:12 +0300
User-agent: KMail/1.7.2

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

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




reply via email to

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