-
Notifications
You must be signed in to change notification settings - Fork 150
/
epy-python.el
168 lines (133 loc) · 5.42 KB
/
epy-python.el
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
;; epy-python.el - setup of python stuff
;; fgallina/python.el
(require 'python (concat epy-install-dir "extensions/python.el"))
;; pymacs
(require 'pymacs (concat epy-install-dir "extensions/pymacs.el"))
(setq epy-enable-ropemacs t)
(defun setup-ropemacs ()
"Setup the ropemacs harness"
(message "****************************")
(if (and (getenv "PYTHONPATH") (not (string= (getenv "PYTHONPATH") "")))
(message "true")
(message "false"))
(message "****************************")
;; If PYTHONPATH is set and not an empty string
(if (and (getenv "PYTHONPATH") (not (string= (getenv "PYTHONPATH") "")))
;; append at the end with separator
(setenv "PYTHONPATH"
(concat
(getenv "PYTHONPATH") path-separator
(concat epy-install-dir "python-libs/")))
;; else set it without separator
(setenv "PYTHONPATH"
(concat epy-install-dir "python-libs/"))
)
(pymacs-load "ropemacs" "rope-")
;; Stops from erroring if there's a syntax err
(setq ropemacs-codeassist-maxfixes 3)
;; Configurations
(setq ropemacs-guess-project t)
(setq ropemacs-enable-autoimport t)
(setq ropemacs-autoimport-modules '("os" "shutil" "sys" "logging"
"django.*"))
;; Adding hook to automatically open a rope project if there is one
;; in the current or in the upper level directory
(add-hook 'python-mode-hook
(lambda ()
(cond ((file-exists-p ".ropeproject")
(rope-open-project default-directory))
((file-exists-p "../.ropeproject")
(rope-open-project (concat default-directory "..")))
)))
)
;; Ipython integration with fgallina/python.el
(defun epy-setup-ipython ()
"Setup ipython integration with python-mode"
(interactive)
(setq
python-shell-interpreter "ipython"
python-shell-interpreter-args ""
python-shell-prompt-regexp "In \[[0-9]+\]: "
python-shell-prompt-output-regexp "Out\[[0-9]+\]: "
python-shell-completion-setup-code ""
python-shell-completion-string-code "';'.join(get_ipython().complete('''%s''')[1])\n")
)
;;=========================================================
;; Flymake additions, I have to put this one somwhere else?
;;=========================================================
(defun flymake-create-temp-in-system-tempdir (filename prefix)
(make-temp-file (or prefix "flymake")))
(defun flymake-create-copy-file ()
"Create a copy local file"
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-in-system-tempdir)))
(file-relative-name
temp-file
(file-name-directory buffer-file-name))
)
)
(defun flymake-command-parse (cmdline)
"Parses the command line CMDLINE in a format compatible
with flymake, as:(list cmd-name arg-list)
The CMDLINE should be something like:
flymake %f python custom.py %f
%f will be substituted with a temporary copy of the file that is
currently being checked.
"
(let ((cmdline-subst (replace-regexp-in-string "%f" (flymake-create-copy-file) cmdline)))
(setq cmdline-subst (split-string-and-unquote cmdline-subst))
(list (first cmdline-subst) (rest cmdline-subst))
))
(when (load-file (concat epy-install-dir "extensions/flymake-patch.el"))
(setq flymake-info-line-regex
(append flymake-info-line-regex '("unused$" "^redefinition" "used$")))
(load-library "flymake-cursor"))
(defun epy-setup-checker (cmdline)
(add-to-list 'flymake-allowed-file-name-masks
(list "\\.py\\'" (apply-partially 'flymake-command-parse cmdline)))
)
;; Python or python mode?
(eval-after-load 'python
'(progn
;;==================================================
;; Ropemacs Configuration
;;==================================================
(add-hook 'python-mode-hook (lambda ()
(when epy-enable-ropemacs
(setup-ropemacs)
(ropemacs-mode t))
))
;;==================================================
;; Virtualenv Commands
;;==================================================
(autoload 'virtualenv-activate "virtualenv"
"Activate a Virtual Environment specified by PATH" t)
(autoload 'virtualenv-workon "virtualenv"
"Activate a Virtual Environment present using virtualenvwrapper" t)
;; Not on all modes, please
;; Be careful of mumamo, buffer file name nil
(add-hook 'python-mode-hook (lambda ()
(if (and (buffer-file-name)
(file-name-directory buffer-file-name))
(flymake-mode))))
;; when we swich on the command line, switch in Emacs
;;(desktop-save-mode 1)
(defun workon-postactivate (virtualenv)
(require 'virtualenv)
(virtualenv-activate virtualenv)
(desktop-change-dir virtualenv))
)
)
;; Cython Mode
(autoload 'cython-mode "cython-mode" "Mode for editing Cython source files")
(add-to-list 'auto-mode-alist '("\\.pyx\\'" . cython-mode))
(add-to-list 'auto-mode-alist '("\\.pxd\\'" . cython-mode))
(add-to-list 'auto-mode-alist '("\\.pxi\\'" . cython-mode))
;; Py3 files
(add-to-list 'auto-mode-alist '("\\.py3\\'" . python-mode))
(add-hook 'python-mode-hook '(lambda ()
(define-key python-mode-map "\C-m" 'newline-and-indent)))
(add-hook 'ein:notebook-python-mode-hook
(lambda ()
(define-key python-mode-map "\C-m" 'newline)))
(provide 'epy-python)