blob: 100b448ff4265c81570d475a2c965d38c2853306 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
;;; ravi-init-python.el --- python support
;; Copyright (C) 2013
;; Author: <ravi@nero.lan>
;; 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 <http://www.gnu.org/licenses/>.
;;; 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/old-ipython-setup ()
"Set up ipython interpreter for version less than 4, which works with readline"
(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"
))
(defun ravi/ipython-5p4plus-setup ()
"Set up ipython interpreter for version 5.4+ which requires rlipython to be installed"
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "--pylab --TerminalIPythonApp.interactive_shell_class=rlipython.TerminalInteractiveShell"
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"
))
(defun ravi/python-mode-hook()
;; Set ipython as our interpreter
(ravi/old-ipython-setup)
(when (functionp 'helm-dash)
(setq-local helm-dash-docsets '("Python 2" "NumPy")))
;; 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)
)
(bind-key "<return>" 'newline-and-indent python-mode-map)
;; We no longer use smart-tab-mode
;(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 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-core
: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"))
))
(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)))
;; Override pos-tip support to automatically delete tooltip
(defun ravi/jedi:tooltip-show (string)
(cond
((and (memq 'pos-tip jedi:tooltip-method) window-system
(featurep 'pos-tip))
(pos-tip-show (jedi:string-fill-paragraph string)
'popup-tip-face nil nil 5))
((and (memq 'popup jedi:tooltip-method)
(featurep 'popup))
(popup-tip string))
(t (when (stringp string)
(let ((message-log-max nil))
(message string))))))
(fset 'jedi:tooltip-show 'ravi/jedi:tooltip-show)
(defun ravi/python-jedi-hook-installer ()
;; (setq jedi:complete-on-dot t) ; needs auto-complete
(add-hook 'hack-local-variables-hook 'ravi/jedi-setup nil t))
(add-hook 'python-mode-hook 'ravi/python-jedi-hook-installer)
)
:ensure t
)
(use-package company-jedi
:config
(progn
(add-to-list 'company-backends 'company-jedi))
:ensure t)
(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 sphinx-doc-mode
:ensure t)
(use-package python-docstring
:config
(progn
(defun ravi/python-docstring-mode-setup ()
(python-docstring-mode 1))
(add-hook 'python-mode-hook 'ravi/python-docstring-mode-setup))
:diminish python-docstring-mode
:ensure t)
(use-package electric-operator
:config
(progn
(add-hook 'python-mode-hook #'electric-operator-mode))
;; :diminish electric-operator
:ensure t)
)
)
(provide 'ravi-init-python)
|