~a-roehler/python-mode/components-python-mode

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
;;; python-components-up-down.el -- Searching up/downwards in buffer -*- lexical-binding: t; -*- 

;; Copyright (C) 2015-2016  Andreas Röhler

;; Author: Andreas Röhler <andreas.roehler@online.de>
;; Keywords: languages, convenience

;; 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 3 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, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This file is generated by function from python-mode-utils.el - see in
;; directory devel. Edits here might not be persistent.

;;; Code:


(defun py-up-statement ()
  "Go to the beginning of next statement upwards in buffer.

Return position if statement found, nil otherwise. "
  (interactive)
  (let (erg)
    (if (py--beginning-of-statement-p)
	(setq erg (py-backward-statement))
      (setq erg (and (py-backward-statement) (py-backward-statement))))
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
    erg))

(defun py-down-statement ()
  "Go to the beginning of next statement downwards in buffer.

Return position if statement found, nil otherwise. "
  (interactive)
  (let* ((orig (point))
	 erg)
    (cond ((py--end-of-statement-p)
	   (setq erg (and (py-forward-statement) (py-backward-statement))))
	  ((setq erg (< orig (progn (py-forward-statement) (py-backward-statement))))
	   (point))
	  (t (setq erg (and (py-forward-statement) (py-forward-statement)(py-backward-statement)))))
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
    erg))

(defun py-up-base (regexp &optional indent orig decorator bol repeat)
  "Go to the beginning of next form upwards in buffer.

Return position if form found, nil otherwise.
REGEXP is a quoted symbol "
  (unless (bobp)
    (let* ((orig (or orig (point)))
	   (repeat (or (and repeat (1+ repeat)) 999))
	   erg name command)
      (if (< py-max-specpdl-size repeat)
	  (error "`py-up-base' reached loops max.")
	(if indent
	    (progn
	      (while (and (re-search-backward (symbol-value regexp) nil 'move 1)
			  (or (nth 8 (parse-partial-sexp (point-min) (point)))
			      (<= indent (current-indentation))))))
	  (unless (py--beginning-of-statement-p)
	    (py-backward-statement))
	  (if (looking-at (symbol-value regexp))
	      (py-up-base regexp (current-indentation) orig decorator bol repeat)
	    (setq name (symbol-name regexp))
	    (setq command (intern-soft (concat "py-backward-" (substring name (string-match "minor\\|block\\|def\\|class" name) (string-match "-re" name)))))
	    (funcall command)
	    (py-up-base regexp (current-indentation) orig decorator bol repeat)))
	(when bol (beginning-of-line))
	(and (looking-at (symbol-value regexp)) (< (point) orig) (setq erg (point)))
	(when py-verbose-p (message "%s" erg))
	erg))))

(defun py-down-base (regexp &optional orig indent decorator bol)
  "Go to the beginning of next form below in buffer.

Return position if form found, nil otherwise.
Expects a quoted symbol 'REGEXP"
  (unless (eobp)
    (let* ((name (substring (symbol-name regexp) 3 -3))
	   (p-command (car (read-from-string (concat "py--beginning-of-" name "-p"))))
	   (backward-command (car (read-from-string (concat "py-backward-" name))))
	   (up-command (car (read-from-string (concat "py-up-" name))))
	   ;; (down-command (car (read-from-string (concat "py-down-" name))))
           (forward-command (car (read-from-string (concat "py-forward-" name))))
           erg done start)
      (if (funcall p-command)
	  (setq indent (current-indentation))
	(save-excursion
	  (cond
	   ((and indent decorator bol)
	    (funcall backward-command indent decorator bol))
	   ((and indent decorator)
	    (funcall backward-command indent decorator))
	   (t (funcall backward-command indent)))
	  (setq indent (current-indentation))
	  (setq start (point))))
      ;; (setq done (funcall forward-command indent decorator bol))
      (while (and (not done)
		  (py-down-statement)
		  (<= indent (current-indentation))
		  (when (looking-at (symbol-value regexp))
		    (setq done (point)))))
      (when done
	(when bol (beginning-of-line))
	(setq erg (point)))
      (unless done
	(goto-char orig)
	(or
	 (if
	     (and
	      (funcall up-command)
	      ;; up should not result to backward
	      (not (eq (point) start))
	      (funcall forward-command decorator bol)
	      (< orig (point))
	      (setq erg (point)))
	     (when bol (setq erg (py--beginning-of-line-form erg)))
	   (goto-char (point-max)))))
      (when py-verbose-p (message "%s" erg))
      erg)))

(defalias 'py-block-up 'py-up-block)
(defun py-up-block (&optional indent decorator bol)
  "Go to the beginning of next block upwards in buffer.

Return position if block found, nil otherwise. "
  (interactive)
  (py-up-base 'py-extended-block-or-clause-re indent (point) decorator bol))

(defalias 'py-class-up 'py-up-class)
(defun py-up-class (&optional indent decorator bol)
  "Go to the beginning of next class upwards in buffer.

Return position if class found, nil otherwise. "
  (interactive)
  (py-up-base 'py-class-re indent (point) decorator bol))

(defalias 'py-def-up 'py-up-def)
(defun py-up-def (&optional indent decorator bol)
  "Go to the beginning of next def upwards in buffer.

Return position if def found, nil otherwise. "
  (interactive)
  (py-up-base 'py-def-re indent (point) decorator bol))

(defalias 'py-def-or-class-up 'py-up-def-or-class)
(defun py-up-def-or-class (&optional indent decorator bol)
  "Go to the beginning of next def-or-class upwards in buffer.

Return position if def-or-class found, nil otherwise. "
  (interactive)
  (py-up-base 'py-def-or-class-re indent (point) decorator bol))

(defalias 'py-minor-block-up 'py-up-minor-block)
(defun py-up-minor-block (&optional indent decorator bol)
  "Go to the beginning of next minor-block upwards in buffer.

Return position if minor-block found, nil otherwise. "
  (interactive)
  (py-up-base 'py-extended-block-or-clause-re indent (point) decorator bol))

(defalias 'py-block-down 'py-down-block)
(defun py-down-block (&optional orig indent decorator bol)
  "Go to the beginning of next block below in buffer.

Return position if block found, nil otherwise. "
  (interactive)
  (py-down-base 'py-block-re (or orig (point)) indent decorator bol))

(defalias 'py-class-down 'py-down-class)
(defun py-down-class (&optional orig indent decorator bol)
  "Go to the beginning of next class below in buffer.

Return position if class found, nil otherwise. "
  (interactive)
  (py-down-base 'py-class-re (or orig (point)) indent decorator bol))

(defalias 'py-def-down 'py-down-def)
(defun py-down-def (&optional orig indent decorator bol)
  "Go to the beginning of next def below in buffer.

Return position if def found, nil otherwise. "
  (interactive)
  (py-down-base 'py-def-re (or orig (point)) indent decorator bol))

(defalias 'py-def-or-class-down 'py-down-def-or-class)
(defun py-down-def-or-class (&optional orig indent decorator bol)
  "Go to the beginning of next def-or-class below in buffer.

Return position if def-or-class found, nil otherwise. "
  (interactive)
  (py-down-base 'py-def-or-class-re (or orig (point)) indent decorator bol))

(defalias 'py-minor-block-down 'py-down-minor-block)
(defun py-down-minor-block (&optional orig indent decorator bol)
  "Go to the beginning of next minor-block below in buffer.

Return position if minor-block found, nil otherwise. "
  (interactive)
  (py-down-base 'py-minor-block-re (or orig (point)) indent decorator bol))

(defun py-up-block-bol (&optional indent decorator)
  "Go to the beginning of next block upwards in buffer.

Go to beginning of line.
Return position if block found, nil otherwise. "
  (interactive)
  (py-up-base 'py-block-re indent (point) decorator t))

(defun py-up-class-bol (&optional indent decorator)
  "Go to the beginning of next class upwards in buffer.

Go to beginning of line.
Return position if class found, nil otherwise. "
  (interactive)
  (py-up-base 'py-class-re indent (point) decorator t))

(defun py-up-def-bol (&optional indent decorator)
  "Go to the beginning of next def upwards in buffer.

Go to beginning of line.
Return position if def found, nil otherwise. "
  (interactive)
  (py-up-base 'py-def-re indent (point) decorator t))

(defun py-up-def-or-class-bol (&optional indent decorator)
  "Go to the beginning of next def-or-class upwards in buffer.

Go to beginning of line.
Return position if def-or-class found, nil otherwise. "
  (interactive)
  (py-up-base 'py-def-or-class-re indent (point) decorator t))

(defun py-up-minor-block-bol (&optional indent decorator)
  "Go to the beginning of next minor-block upwards in buffer.

Go to beginning of line.
Return position if minor-block found, nil otherwise. "
  (interactive)
  (py-up-base 'py-minor-block-re indent (point) decorator t))

(defun py-down-block-bol (&optional orig indent decorator bol)
  "Go to the beginning of next block below in buffer.

Go to beginning of line
Return position if block found, nil otherwise "
  (interactive)
  (py-down-base 'py-block-re (or orig (point)) indent decorator (or bol t)))

(defun py-down-class-bol (&optional orig indent decorator bol)
  "Go to the beginning of next class below in buffer.

Go to beginning of line
Return position if class found, nil otherwise "
  (interactive)
  (py-down-base 'py-class-re (or orig (point)) indent decorator (or bol t)))

(defun py-down-def-bol (&optional orig indent decorator bol)
  "Go to the beginning of next def below in buffer.

Go to beginning of line
Return position if def found, nil otherwise "
  (interactive)
  (py-down-base 'py-def-re (or orig (point)) indent decorator (or bol t)))

(defun py-down-def-or-class-bol (&optional orig indent decorator bol)
  "Go to the beginning of next def-or-class below in buffer.

Go to beginning of line
Return position if def-or-class found, nil otherwise "
  (interactive)
  (py-down-base 'py-def-or-class-re (or orig (point)) indent decorator (or bol t)))

(defun py-down-minor-block-bol (&optional orig indent decorator bol)
  "Go to the beginning of next minor-block below in buffer.

Go to beginning of line
Return position if minor-block found, nil otherwise "
  (interactive)
  (py-down-base 'py-minor-block-re (or orig (point)) indent decorator (or bol t)))

;; python-components-up-down.el ends here
(provide 'python-components-up-down)