38131c7e725db8b717c9ee60771b76111281e897
[emacs-init.git] / python / init_python.el
1 (autoload 'python-mode "python-mode" "Python Mode." t)
2 (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
3 (add-to-list 'interpreter-mode-alist '("python" . python-mode))
4 (require 'python-mode)
5 (add-hook 'python-mode-hook
6       (lambda ()
7         (set-variable 'py-indent-offset 4)
8         ;(set-variable 'py-smart-indentation nil)
9         (set-variable 'indent-tabs-mode nil)
10         (define-key py-mode-map (kbd "RET") 'newline-and-indent)
11         ;(define-key py-mode-map [tab] 'yas/expand)
12         ;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)
13         (smart-operator-mode-on)
14         ))
15 ;; pymacs
16 (autoload 'pymacs-apply "pymacs")
17 (autoload 'pymacs-call "pymacs")
18 (autoload 'pymacs-eval "pymacs" nil t)
19 (autoload 'pymacs-exec "pymacs" nil t)
20 (autoload 'pymacs-load "pymacs" nil t)
21 ;;(eval-after-load "pymacs"
22 ;;  '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY"))
23 (pymacs-load "ropemacs" "rope-")
24 (setq ropemacs-enable-autoimport t)
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Auto-completion
27 ;;;  Integrates:
28 ;;;   1) Rope
29 ;;;   2) Yasnippet
30 ;;;   all with AutoComplete.el
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 (defun prefix-list-elements (list prefix)
33   (let (value)
34     (nreverse
35      (dolist (element list value)
36       (setq value (cons (format "%s%s" prefix element) value))))))
37 (defvar ac-source-rope
38   '((candidates
39      . (lambda ()
40          (prefix-list-elements (rope-completions) ac-target))))
41   "Source for Rope")
42 (defun ac-python-find ()
43   "Python `ac-find-function'."
44   (require 'thingatpt)
45   (let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))
46     (if (null symbol)
47         (if (string= "." (buffer-substring (- (point) 1) (point)))
48             (point)
49           nil)
50       symbol)))
51 (defun ac-python-candidate ()
52   "Python `ac-candidates-function'"
53   (let (candidates)
54     (dolist (source ac-sources)
55       (if (symbolp source)
56           (setq source (symbol-value source)))
57       (let* ((ac-limit (or (cdr-safe (assq 'limit source)) ac-limit))
58              (requires (cdr-safe (assq 'requires source)))
59              cand)
60         (if (or (null requires)
61                 (>= (length ac-target) requires))
62             (setq cand
63                   (delq nil
64                         (mapcar (lambda (candidate)
65                                   (propertize candidate 'source source))
66                                 (funcall (cdr (assq 'candidates source)))))))
67         (if (and (> ac-limit 1)
68                  (> (length cand) ac-limit))
69             (setcdr (nthcdr (1- ac-limit) cand) nil))
70         (setq candidates (append candidates cand))))
71     (delete-dups candidates)))
72 (add-hook 'python-mode-hook
73           (lambda ()
74                  (auto-complete-mode 1)
75                  (set (make-local-variable 'ac-sources)
76                       (append ac-sources '(ac-source-rope) '(ac-source-yasnippet)))
77                  (set (make-local-variable 'ac-find-function) 'ac-python-find)
78                  (set (make-local-variable 'ac-candidate-function) 'ac-python-candidate)
79                  (set (make-local-variable 'ac-auto-start) nil)))
80 ;;Ryan's python specific tab completion
81 (defun ryan-python-tab ()
82   ; Try the following:
83   ; 1) Do a yasnippet expansion
84   ; 2) Do a Rope code completion
85   ; 3) Do an indent
86   (interactive)
87   (if (eql (ac-start) 0)
88       (indent-for-tab-command)))
89 (defadvice ac-start (before advice-turn-on-auto-start activate)
90   (set (make-local-variable 'ac-auto-start) t))
91 (defadvice ac-cleanup (after advice-turn-off-auto-start activate)
92   (set (make-local-variable 'ac-auto-start) nil))
93 ;(define-key py-mode-map "\t" 'ryan-python-tab)
94 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 ;;; End Auto Completion
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 ;; Auto Syntax Error Hightlight
98 (when (load "flymake" t)
99   (defun flymake-pyflakes-init ()
100     (let* ((temp-file (flymake-init-create-temp-buffer-copy
101                        'flymake-create-temp-inplace))
102            (local-file (file-relative-name
103                         temp-file
104                         (file-name-directory buffer-file-name))))
105       (list "pyflakes" (list local-file))))
106   (add-to-list 'flymake-allowed-file-name-masks
107                '("\\.py\\'" flymake-pyflakes-init)))
108 (add-hook 'find-file-hook 'flymake-find-file-hook)
109
110 (defun py-cleanup-imports ()
111   (interactive)
112   (let (beg end)
113     (if (region-active-p)
114         (setq beg (region-beginning)
115               end (region-end))
116       (save-excursion
117         (goto-char (point-min))
118         (while (and (re-search-forward "^\\(import\\s-+\\|from\\s-+\\)" nil t)
119                     (looking-at "__future__")))
120         (beginning-of-line)
121         (if (not (looking-at "\\(import\\s-+\\|from\\s-+\\)"))
122             (error "No imports found"))
123         (setq beg (point))
124         (while (looking-at "\\(import\\s-+\\|from\\s-+\\)")
125           (forward-line 1))
126         (setq end (point))))
127     (sort-lines t beg end)
128     (goto-char beg)
129     (let ((end-marker (make-marker))
130           (doing-imports t)
131           (b beg))
132       (set-marker end-marker end)
133       (while (< (point) end-marker)
134         (let ((is-import (looking-at "import\\s-+"))
135               (eol-marker (save-excursion (end-of-line) (point-marker)))
136               prefix)
137           (if (and doing-imports (not is-import))
138               (progn
139                 (if (> (point) b)
140                     (progn
141                       (sort-lines nil beg (point))
142                       (setq b (point))))
143                 (setq doing-imports nil)))
144           (setq prefix (if is-import "import "
145                          (buffer-substring-no-properties
146                           (point) (re-search-forward "\\s-+import\\s-+" eol-marker t))))
147           (while (search-forward "," eol-marker t)
148             (delete-char -1)
149             (delete-horizontal-space)
150             (insert "\n" prefix))
151           (forward-line 1)))
152       (sort-lines nil b (point))
153       (if (boundp flymake-is-running)
154           (progn
155             (while flymake-is-running (sit-for .1))
156             (flymake-start-syntax-check)
157             (while flymake-is-running (sit-for .1))
158             (goto-char beg)
159             (while (< (point) end-marker)
160               (if (and (loop for ov in (overlays-at (point))
161                              thereis (flymake-overlay-p ov))
162                        (not (save-excursion (search-forward "unused"
163                                                             (save-excursion (end-of-line) (point))
164                                                             t ))))
165                   (delete-region (point)
166                                  (progn (forward-line 1) (point)))
167                 (forward-line 1))))))))
168
169 (defun flyspell-py-progmode-verify ()
170   "Replacement for standard flyspell-generic-progmode-verify which
171 checks for C/C++ preproc directives. Additionally, anything after ^L
172 is ignored (Those are the file local variables and local words)."
173   (let ((f (get-text-property (point) 'face)))
174     (and (memq f flyspell-prog-text-faces)
175          (not (save-excursion
176                 (beginning-of-line)
177                 (looking-at "#!")))
178          (not (let ((l (max (point-min) (- (point-max) 4096))))
179                 (and (< l (point))
180                      (save-excursion (search-backward "\f" l t)))))
181          (not (let* ((pos (python-in-string/comment))
182                      (c (and pos (char-after pos))))
183                 (and pos (not (save-excursion
184                                 (goto-char pos)
185                                 (beginning-of-line)
186                                 (looking-at (concat "^\\s-*[uUrR]?" (regexp-quote (make-string 3 c))))))))))))
187
188 (defun flyspell-py-mode ()
189   "Turn on `flyspell-mode` for comments and multi-line strings"
190   (interactive)
191   (setq flyspell-generic-check-word-p 'flyspell-py-progmode-verify)
192   (flyspell-mode t))
193
194 (provide 'init_python)
195
196