Programming With Python Dot El (original) (raw)

There are two major variants of python.el

YaSnippet

see this post

Documentation

‘python-describe-symbol’ tries to get doc in the context of what the source file imports (be sure to first run the Python interpreter [C-c !]).

ElDoc,

Does it work with python.el? python.el is supposed to have superior support for this, but I can’t get it to work; it worked alright in python-mode.el though! – CH

nevermind, it works now. shows up in the mini-buffer when you type a function name. – CH

It may have stopped working when my ‘imports’ were incomplete. check that the computed value of ‘python-imports’ is sensible! --CH

CEDET

CEDET is now part of emacs (but I still recommend getting the latest version). It includes things like completion and folding, so that some things here may not be required.

Indentation and folding

python.el includes a regexp for outliner-mode, and hs-mode too.

Since indentation is significant in python, correct indent functions are vital, thought these might overload the tab key.

guess indent? or do something! maybe a ‘fix syntax’ key? py-smart? – CH

tab to guess or max indent, backspace to reduce? This appear to be a function already. – CH

See SmartTabs (toc5) or TabCompletion (tabkey2 stuff)

Newline-and-indent

In python.el you can get this with this in your InitFile:

(add-hook 'python-mode-hook '(lambda () (define-key python-mode-map "\C-m" 'newline-and-indent)))

Useful Functions

python-end-of-block instead of py-next-block?

auto-complete

The following will integrate python.el completions with auto-complete. You need to have installed and loaded auto-complete.el first:

(defvar ac-source-python
  '((candidates .
    (lambda ()
      (mapcar '(lambda (completion)
             (first (last (split-string completion "\\." t))))
          (python-symbol-completions (python-partial-symbol)))))))

(add-hook 'python-mode-hook
  (lambda() (setq ac-sources '(ac-source-python))))

Troubleshooting

C-c C-c / C-c C-r doesn't send / evaluate my code!

It seems like point has to be at the very bottom, with a clear prompt, for python-send-region to work. Here’s a quick hack that does that before calling the function, and puts it back the way it was afterwards:

(defadvice python-send-region (around advice-python-send-region-goto-end)
  "Fix a little bug if the point is not at the prompt when you do
C-c C-[rc]"
  (let ((oldpoint (with-current-buffer (process-buffer (python-proc)) (point)))
    (oldinput
     (with-current-buffer (process-buffer (python-proc))
       (goto-char (point-max))
       
       (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
         (when (> (point) (marker-position pmark))
           (let ((ret (buffer-substring pmark (point))))
         (delete-region pmark (point))
         ret))))))
    ad-do-it
    (with-current-buffer (process-buffer (python-proc))
      (when oldinput (insert oldinput))
      (goto-char oldpoint))))
(ad-enable-advice 'python-send-region 'around 'advice-python-send-region-goto-end)
(ad-activate 'python-send-region)

python-complete-symbol

Using python-complete-symbol in the inferior python buffer causes an infinite loop in python.el as of 080117, changing python-imports from the default nil to ‘None’ solves this. 2008-01-17, patch submitted.

py-help-at-point bug

:I’m a little confused - It appears when I set flymake-mode to be automatically on for python mode, py-help-at-point doesn’t™t work. I tried switching from python-mode.el to python.el, but this doesn’t seem to solve it; However, there is no advice here on how to use python.el, so I’m not entirely sure I’m doing it right…

The above refers to python.el as packaged with Emacs 22; loveshack- python.el completion can’t generally work in the inferior buffer because it sends input to that buffer. The loveshack- python.el doesn’t actually define python-complete-symbol. You can use the interpreter rlcompleter module as normal if you want.