-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpython-config.el
186 lines (161 loc) · 6.22 KB
/
python-config.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
;;; * Licence
;; Copyright (C) 2017 Samuel Barreto <samuel.barreto8@gmail.com>
;;
;; 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 2 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, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;
;;; * Code
(use-package elpy
:ensure t
:config
(elpy-enable))
;; (use-package anaconda-mode
;; :quelpa (anaconda-mode :fetcher github :repo "proofit404/anaconda-mode")
;; :config
;; (add-hook 'python-mode-hook 'anaconda-mode)
;; (add-hook 'python-mode-hook 'anaconda-eldoc-mode)
;; (bind-keys :map python-mode-map
;; ("C-M-i" . anaconda-mode-complete)
;; ("M-." . anaconda-mode-find-definitions)
;; ("M-?" . anaconda-mode-show-doc)
;; ("M-," . anaconda-mode-go-back)
;; ("M-*" . anaconda-mode-find-assignments)
;; ("M-SPC" . hydra-python/body))
;; (use-package company-anaconda
;; :quelpa (company-anaconda :fetcher github :repo "proofit404/company-anaconda")
;; :config
;; (add-to-list 'company-backends '(company-anaconda :with company-capf))))
;; (use-package py-yapf
;; :quelpa (py-yapf :fetcher github :repo "paetzke/py-yapf.el")
;; :commands py-yapf-buffer)
;; (use-package pyenv-mode :ensure t
;; :commands pyenv-mode
;; :init (add-hook 'python-mode-hook 'pyenv-mode))
;; (use-package pyvenv :ensure t
;; :config
;; (setenv "WORKON_HOME" "/Users/samuelbarreto/")
;; (pyvenv-workon "anaconda"))
;; (use-package lpy
;; :disabled t
;; :quelpa (lpy :fetcher github :repo "abo-abo/lpy")
;; :init
;; (use-package function-args
;; :quelpa (function-args :fetcher github :repo "abo-abo/function-args"))
;; (use-package soap
;; :quelpa (soap :fetcher github :repo "abo-abo/soap"))
;; :config
;; (add-hook 'python-mode-hook (lambda () (lpy-mode 1) (lispy-mode 1))))
;; (use-package py-isort :ensure t
;; :commands (py-isort-buffer
;; py-isort-region))
;; ---------- defaults ----------------------------------------------------
(setq-default indent-tabs-mode nil)
(setq-default python-indent-offset 4)
(if (executable-find "ipython")
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "--simple-prompt -i --pprint")
(setq python-shell-interpreter "python3"))
;; ---------- Function definitions ----------------------------------------
(defun python-shell-send-line (&optional vis)
"send the current line to the inferior python process"
(interactive "P")
(save-excursion
(end-of-line)
(let ((end (point)))
(beginning-of-line)
(python-shell-send-region (point) end vis "eval line"))))
(defun current-line-empty-p ()
(save-excursion
(beginning-of-line)
(looking-at "[[:space:]]*$")))
(defun python-shell-send-block (&optional vis)
"send the current block of text to inferior python process.
If not in a block, send the upper block.
"
(interactive "P")
(save-excursion
(unless (current-line-empty-p) (python-nav-end-of-block))
(let ((end (point)))
(python-nav-beginning-of-block)
(python-shell-send-region (point) end vis "eval line"))))
(defun python-shell-send-block-switch ()
(interactive)
(python-shell-send-block)
(python-shell-switch-to-shell))
(defun python-shell-send-line-switch ()
(interactive)
(python-shell-send-line)
(python-shell-switch-to-shell))
;; from https://github.com/syl20bnr/spacemacs/blob/master/layers/%2Blang/python/packages.el
(defun python-shell-send-buffer-switch ()
"Send buffer content to shell and switch to it in insert mode"
(interactive)
(python-shell-send-buffer)
(python-shell-switch-to-shell))
(defun python-shell-send-defun-switch ()
"send function content to shell and switch to it in insert mode"
(interactive)
(python-shell-send-defun nil)
(python-shell-switch-to-shell))
(defun python-shell-send-region-switch (start end)
"Send region content to shell and switch to it in insert mode."
(interactive "r")
(python-shell-send-region start end)
(python-shell-switch-to-shell))
;; ---------- Keybindings -------------------------------------------------
(general-define-key
:keymaps 'python-mode-map
"s-e" 'python-shell-send-defun
"C-<return>" 'python-shell-send-line
"«" 'python-indent-shift-left
"»" 'python-indent-shift-right
(general-chord ",l") 'python-shell-send-line
(general-chord ";L") 'python-shell-send-line-switch
(general-chord ",b") 'python-shell-send-block
(general-chord ";B") 'python-shell-send-block-switch
(general-chord ",t") 'python-shell-send-defun
(general-chord ";T") 'python-shell-send-defun-switch
(general-chord ",r") 'python-shell-send-region
(general-chord ";R") 'python-shell-send-region-switch
(general-chord "xq") 'hydra-python/body)
(defhydra hydra-python (:hint nil :color teal)
"
^Send^ ^ ^ ^Navigation^ ^Code^ ^Actions^
^----^ ^ ^ ^----------^ ^----^ ^-------^
_sl_: line _st_: function _._: def _>_: indent _y_: yapf
_SL_: line → _ST_: function → _*_: assign _<_: outdent
_sr_: region _sb_: buffer _,_: back
_SR_: region → _SB_: buffer → ^ ^
"
;; shell send
("sl" python-shell-send-line)
("SL" python-shell-send-line-switch)
("sr" python-shell-send-region)
("SR" python-shell-send-region-switch)
("st" python-shell-send-defun)
("ST" python-shell-send-defun-switch)
("sb" python-shell-send-buffer)
("SB" python-shell-send-buffer-switch)
;; code nav
("." anaconda-mode-find-definitions)
("," anaconda-mode-find-assignments :color red)
("*" anaconda-mode-go-back :color red)
;; code editing
("<" python-indent-shift-left)
(">" python-indent-shift-right)
;; test
;; TODO python test via nose
;; actions
("y" py-yapf-buffer)
("q" nil "quit" :color blue))