;;; ravi-init-python.el --- python support ;; Copyright (C) 2013 ;; Author: ;; 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 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; 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: ;; Python programming setup ;;; Code: (use-package python :mode ("\\.py\\'" . python-mode) :commands python-shell-switch-to-shell :config (progn (add-hook 'python-mode-hook 'ravi/python-mode-hook) (defun ravi/python-mode-hook() ;; Set ipython as our interpreter (setq python-shell-interpreter "ipython" python-shell-interpreter-args "--pylab" python-shell-prompt-regexp "In \\[[0-9]+\\]: " python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: " python-shell-completion-setup-code "from IPython.core.completerlib import module_completion" python-shell-completion-module-string-code "';'.join(module_completion('''%s'''))\n" python-shell-completion-string-code "';'.join(get_ipython().Completer.all_completions('''%s'''))\n" ) ;; For ipython 0.10, uncomment the following: ;; (setq python-shell-completion-string-code ;; "';'.join(__IP.complete('''%s'''))\n" ;; python-shell-completion-module-string-code "" ;; ) ;; I'd really prefer indentation by 2 spaces, but have too much existing ;; python code with indentation at 4 spaces. (setq python-indent-offset 4) ) (add-to-list 'smart-tab-disabled-major-modes 'inferior-python-mode) (define-auto-insert "\\.py\\'" 'ravi/auto-insert-py) (defun ravi/auto-insert-py () (progn (insert "#!/usr/bin/env python\n\n") ) ) (use-package virtualenvwrapper :config (progn (venv-initialize-interactive-shells) (venv-initialize-eshell) (setq venv-location (expand-file-name "~/usr/local/venv/")) ) :ensure t ) ;; Use pymacs+ropemacs for code completion plus documentation browsing. ;; The main issue is that this requires both python-side and emacs-side ;; support, and hence cannot be installed as a package from MELPA. (use-package pymacs :disabled t :init (progn ;; Many bindings provide same functionality as from other packages. (setq ropemacs-global-prefix nil) ;(setq ropemancs-local-prefix nil) ;(setq ropemacs-enable-shortcuts nil) ) :config (progn (pymacs-load "ropemacs" "rope-") (bind-key "C-c C-d" 'rope-show-calltip python-mode-map) ;; Auto-complete sources; see ;; http://www.cx4a.org/pub/auto-complete-python.el (defvar ac-ropemacs-completions-cache nil) (defvar ac-source-ropemacs '((init . (lambda () (setq ac-ropemacs-completions-cache (mapcar (lambda (completion) (concat ac-prefix completion)) (ignore-errors (rope-completions)))))) (candidates . (lambda () (all-completions ac-prefix ac-ropemacs-completions-cache))))) (defun ac-ropemacs-setup () (ac-ropemacs-require) ;(setq ac-sources (append (list 'ac-source-ropemacs) ac-sources)) (setq ac-omni-completion-sources '(("\\." ac-source-ropemacs)))) (add-hook 'python-mode-hook 'ac-ropemacs-setup) ) ) ;; Use jedi.el for code completion plus documentation browsing. ;; The main issue is that this requires both python-side and emacs-side ;; support, but the python-side support cannot be installed as a package ;; from MELPA. Fedora does not provide python-epc as an RPM either. We ;; work around it by adding sexpdata, jedi and python-epc as git submodules. (use-package jedi :config (progn (defun ravi/jedi-setup () (let ((basic-server-args (list "--sys-path" (ravi/emacs-file "site-lisp/python-epc") "--sys-path" (ravi/emacs-file "site-lisp/sexpdata") "--sys-path" (ravi/emacs-file "site-lisp/jedi")) )) (set (make-local-variable 'jedi:server-args) (if (and (boundp 'extra-jedi-args) (listp extra-jedi-args)) (append basic-server-args extra-jedi-args) basic-server-args)) (jedi:setup))) (defun ravi/python-jedi-hook-installer () (setq jedi:complete-on-dot t) (add-hook 'hack-local-variables-hook 'ravi/jedi-setup nil t)) (add-hook 'python-mode-hook 'ravi/python-jedi-hook-installer) ) :ensure t :disabled nil ) (use-package sphinx-doc :config (progn (defun ravi/sphinx-doc-setup () (sphinx-doc-mode 1)) (add-hook 'python-mode-hook 'ravi/sphinx-doc-setup) ) :diminish t :ensure t) ) ) (provide 'ravi-init-python)