Emacs hack: help() to a window

Beni Cherniavsky cben at techunix.technion.ac.il
Mon Jun 2 05:34:26 EDT 2003


I was annoyed by python-mode's broken recognition of quotes inside
triple quotes or unrelated output in the py-shell window (especially
help() output).  I'm using the following for a long time::

     class q:
         """Easily fix emacs' quoting confusion at prompt"""
         def __repr__(self):
             return "'"
     q = q()

so that I can type q RET to fix the py-shell when it becomes
string-colored.

But now I realized that help() should not mix with the flow of the
py-shell at all!  Here is my hack that redierct it to an emacs window.
In my .emacs::

    (require 'server)
    (require 'man)
    (require 'help-fns)
    (server-start)
    (defun server-handle-pyhelp ()
      "A hook hack for putting Python help in emacs help windows"
      (when (string-match "__pyhelp__$" (buffer-name))
        (let ((text (buffer-string)))
          (setq file-name-history
                (remove (buffer-file-name) file-name-history))
          (server-done)
          (switch-to-buffer "*Python*")
          (with-output-to-temp-buffer "*Python help*"
            (princ text)
            (with-current-buffer standard-output
              (Man-fontify-manpage))
            (print-help-return-message)))))
    (add-hook 'server-switch-hook 'server-handle-pyhelp)

And in my $PYTHONSTARTUP::

    # emacsclient help pager hack
    if os.environ.get('EMACS') == 't':
        import pydoc
        def emacspager(text):
            """Write to a temp. file and show in emacs window."""
            import tempfile
            # The magic suffix is recognized by my emacs
            filename = tempfile.mktemp(suffix='__pyhelp__')
            try:
                try:
                    f = file(filename, 'w')
                    f.write(text)
                finally:
                    f.close()
                os.system('emacsclient -a cat %s > /dev/null' %
                          (filename ,))
          finally:
             os.unlink(filename)
       pydoc.pager = emacspager

Enjoy!  Works for me (on linux, emacs-21.3-derivative).  It confuses
the emacs-server commands (C-x #) a bit, didn't manage to improve
this.

-- 
Beni Cherniavsky <cben at users.sf.net>





More information about the Python-list mailing list