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))
5 (add-hook 'python-mode-hook
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)
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;;; all with AutoComplete.el
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 (defun prefix-list-elements (list prefix)
35 (dolist (element list value)
36 (setq value (cons (format "%s%s" prefix element) value))))))
37 (defvar ac-source-rope
40 (prefix-list-elements (rope-completions) ac-target))))
42 (defun ac-python-find ()
43 "Python `ac-find-function'."
45 (let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))
47 (if (string= "." (buffer-substring (- (point) 1) (point)))
51 (defun ac-python-candidate ()
52 "Python `ac-candidates-function'"
54 (dolist (source ac-sources)
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)))
60 (if (or (null requires)
61 (>= (length ac-target) requires))
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
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 ()
83 ; 1) Do a yasnippet expansion
84 ; 2) Do a Rope code completion
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
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)
110 (defun py-cleanup-imports ()
113 (if (region-active-p)
114 (setq beg (region-beginning)
117 (goto-char (point-min))
118 (while (and (re-search-forward "^\\(import\\s-+\\|from\\s-+\\)" nil t)
119 (looking-at "__future__")))
121 (if (not (looking-at "\\(import\\s-+\\|from\\s-+\\)"))
122 (error "No imports found"))
124 (while (looking-at "\\(import\\s-+\\|from\\s-+\\)")
127 (sort-lines t beg end)
129 (let ((end-marker (make-marker))
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)))
137 (if (and doing-imports (not is-import))
141 (sort-lines nil beg (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)
149 (delete-horizontal-space)
150 (insert "\n" prefix))
152 (sort-lines nil b (point))
153 (if (boundp flymake-is-running)
155 (while flymake-is-running (sit-for .1))
156 (flymake-start-syntax-check)
157 (while flymake-is-running (sit-for .1))
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))
165 (delete-region (point)
166 (progn (forward-line 1) (point)))
167 (forward-line 1))))))))
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)
178 (not (let ((l (max (point-min) (- (point-max) 4096))))
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
186 (looking-at (concat "^\\s-*[uUrR]?" (regexp-quote (make-string 3 c))))))))))))
188 (defun flyspell-py-mode ()
189 "Turn on `flyspell-mode` for comments and multi-line strings"
191 (setq flyspell-generic-check-word-p 'flyspell-py-progmode-verify)
194 (provide 'init_python)