pydoc-info / pydoc-info.el

;;; pydoc-info.el --- Search and browse the Python documentation in Info

;; Copyright (C) 2011  Jonathan Waltman

;; Author: Jonathan Waltman <>
;; Keywords:

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <>.

;;; Commentary:
;; This package provides routines to better search and browse the
;; Python documentation in Info.
;;; Features:
;; - Enable `info-lookup-symbol' to work with the new Python docs
;; - Improve browsing of Python Info docs by hiding *note: references.

;;; Install:
;; To use this package, you will need to have a recent version of the
;; Python documentation in the Info format on your machine.  It can be
;; downloaded at < ... >.
;; Then add this file to your load-path and put the following
;; expression into your ~/.emacs.d/init.el.
;;     (require 'pydoc-info)

;;; Usage:
;;   C-h S : info-lookup-symbol

;;; Code:

(require 'python)
(require 'info-look)

(defcustom pydoc-info-hide-note-references t
  "*Non-nil if note references should be hidden in Python Info
  :group 'pydoc-info)

;; Is there a better way to do this?
(defadvice info-insert-file-contents (after
  "Hack to make `Info-hide-note-references' buffer-local and
automatically set to `hide' iff it can be determined that this
file was created from a Texinfo file generated by Docutils or

This is only performed if `pydoc-info-hide-note-references' is
  (when pydoc-info-hide-note-references
    (set (make-local-variable 'Info-hide-note-references)
         (default-value 'Info-hide-note-references))
        (widen) (goto-char (point-min))
        (when (re-search-forward
               "^Generated by \\(Sphinx\\|Docutils\\)"
               (save-excursion (search-forward "\x1f" nil t)) t)
          (set (make-local-variable 'Info-hide-note-references)

;; This replaces the Info-look support in `python.el' which is based
;; on the old Python docs and no longer works.
 :mode 'python-mode
 :parse-rule 'pydoc-info-python-symbol-at-point
 '(("(python)Index" pydoc-info-lookup-transform-entry)))

(defun pydoc-info-python-symbol-at-point ()
  (with-syntax-table python-dotty-syntax-table

(defun pydoc-info-lookup-transform-entry (item)
  "Transform a Python index entry to a help item."
   ;; keyword; foo --> foo
   ;; statement; foo --> foo
   ((string-match "\\`\\(keyword\\|statement\\);? \\([[:alnum:]_]+\\)" item)
    (replace-regexp-in-string " " "." (match-string 2 item)))

   ;; foo [built-in ...] --> foo
   ((string-match "\\`\\([[:alnum:]_ ]+\\) \\[built-in .+\\]" item)
    (replace-regexp-in-string " " "." (match-string 1 item)))

   ;; foo bar [module] -->
   ((string-match "\\`\\([[:alnum:]_ ]+\\) \\[module\\]" item)
    (replace-regexp-in-string " " "." (match-string 1 item)))

   ;; baz [in module foo bar] -->
   ((string-match "\\`\\([[:alnum:]_ ]+\\) \\[in module \\(.+\\)\\]" item)
    (replace-regexp-in-string " " "." (concat (match-string 2 item) " "
                                              (match-string 1 item))))
   ;; Bar [class in foo bar] -->
   ((string-match "\\`\\([[:alnum:]_ ]+\\) \\[class in \\(.+\\)\\]" item)
    (replace-regexp-in-string " " "." (concat (match-string 2 item) " "
                                              (match-string 1 item))))
   ;; bar [foo Foo method] -->
     "\\`\\([[:alnum:]_ ]+\\) \\[\\(.+\\) \\(method\\|attribute\\)\\]" item)
    (replace-regexp-in-string " " "." (concat (match-string 2 item) " "
                                              (match-string 1 item))))

(provide 'pydoc-info)
;;; pydoc-info.el ends here