forked from technomancy/emacs-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 17
/
starter-kit-python.el
138 lines (112 loc) · 4.98 KB
/
starter-kit-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
;; starter-kit-python.el - setup of python stuff
(require 'pymacs (concat dotfiles-dir "elpa-to-submit/pymacs.el"))
(defun setup-ropemacs ()
"Setup the ropemacs harness"
(setenv "PYTHONPATH"
(concat
(getenv "PYTHONPATH") path-separator
(concat dotfiles-dir "python-libs/")))
(pymacs-load "ropemacs" "rope-")
;; Stops from erroring if there's a syntax err
(setq ropemacs-codeassist-maxfixes 3)
(setq ropemacs-guess-project t)
(setq ropemacs-enable-autoimport t)
;; 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 "..")))
)))
)
;; Customizing
(setq flymake-enable-pyflakes t)
(setq flymake-enable-pylint nil)
(setq flymake-enable-pep8 nil)
;; Python or python mode?
(eval-after-load 'python
'(progn
;;==================================================
;; Ropemacs Configuration
;;==================================================
(setup-ropemacs)
;;==================================================
;; 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)
;;==================================================
;; Flymake for python configuration
;;===================================================
;; TODO: There is some duplication, that can be removed using macros
;; TODO: Implement flymake-remove-checker
;; Instructions to add a new checker based on command:
;;
;; 1) Write an init function, the flymake-command-setup performs some
;; checks and at the end of the option list the filename to process:
;;
;; (defun flymake-newchecker-init ()
;; (flymake-command-setup "command" (list "option1" "option2")))
;;
;; 2) Use the flymake-add-checker function
;;
;; (flymake-add-checker flymake-newchecker-init)
(require 'tramp)
;; Utilities that increase legibility and reduce code duplication
(defun current-file-remotep ()
"Tell if the file is remote"
(subsetp (list (current-buffer)) (tramp-list-remote-buffers)))
(defun flymake-create-copy-file ()
"Create a copy local file"
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace)))
(file-relative-name
temp-file
(file-name-directory buffer-file-name))))
(defun flymake-command-setup (command &optional options)
"Setup the command to be used with flymake, the command
will be called in this way: COMMAND OPTIONS FILE The FILE varible
is passed after the options."
;; Make sure it's not a remote buffer or flymake would not work
(when (not (current-file-remotep))
(list command
(append options (list (flymake-create-copy-file))))))
(when (require 'flymake "flymake-patch" t)
(setq flymake-info-line-regex
(append flymake-info-line-regex '("unused$" "^redefinition" "used$"))))
;; I'm using individual well-defined names to be able to remove them
;; in some way
;; Init functions!
(defun flymake-pyflakes-init ()
(flymake-command-setup "pyflakes"))
(defun flymake-pep8-init ()
(flymake-command-setup "pep8"))
(defun flymake-pylint-init ()
(flymake-command-setup "python" (list (concat dotfiles-dir "scripts/pylint-mod.py"))))
(defun flymake-disable-python-checkers ()
"Disable all python checkers"
(dolist (flymake-checker-init '(flymake-pyflakes-init flymake-pep8-init flymake-pylint-init))
(remove '("\\.py\\'" flymake-checker-init) 'flymake-allowed-file-name-masks)))
(defun flymake-add-checker (command)
"Add the checker specified by the COMMAND list"
(add-to-list 'flymake-allowed-file-name-masks
(list "\\.py\\'" command)))
;; Not on all modes, please
(add-hook 'python-mode-hook 'flymake-find-file-hook)
(when flymake-enable-pyflakes
(flymake-add-checker 'flymake-pyflakes-init))
(when flymake-enable-pylint
(flymake-add-checker 'flymake-pylint-init))
(when flymake-enable-pep8
(flymake-add-checker 'flymake-pep8-init)))
)
;; 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))
(provide 'starter-kit-python)