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

2625 by Andreas Röhler
Some minor fixes WRT lexical scope
1
;; python-components-intern.el --- Part of python-components-mode -*- lexical-binding: t; -*-
362 by Andreas Roehler
towards a Python-IDE
2
3
;; Helper functions
4
2615 by Andreas Röhler
`py--beginning-of-' forms simplified
5
;; Copyright (C) 2015-2016 Andreas Röhler
2396 by Andreas Röhler
email updated
6
2626 by Andreas Röhler
Some more minor fixes WRT lexical scope
7
;; Author: Andreas Röhler <andreas.roehler@online.de>
2396 by Andreas Röhler
email updated
8
362 by Andreas Roehler
towards a Python-IDE
9
;; Keywords: languages, processes
10
11
;; This program is free software; you can redistribute it and/or modify
12
;; it under the terms of the GNU General Public License as published by
13
;; the Free Software Foundation, either version 3 of the License, or
14
;; (at your option) any later version.
15
16
;; This program is distributed in the hope that it will be useful,
17
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
;; GNU General Public License for more details.
20
21
;; You should have received a copy of the GNU General Public License
22
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24
;;; Commentary:
782 by Andreas Roehler
rewrite myrkwid
25
;;
362 by Andreas Roehler
towards a Python-IDE
26
27
;;; Code:
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
28
29
;;  Keymap
2347 by Andreas Röhler
Menu re-organized
30
2618 by Andreas Röhler
`py-up-base' fixed
31
(defun ar--beginning-of-form-intern (regexp &optional iact indent orig lc)
32
  "Go to beginning of FORM.
33
34
With INDENT, go to beginning one level above.
35
Whit IACT, print result in message buffer.
36
37
Returns beginning of FORM if successful, nil otherwise"
38
  (interactive "P")
39
  (let (erg)
40
    (unless (bobp)
41
      (let* ((orig (or orig (point)))
42
             (indent (or indent (progn
43
                                  (back-to-indentation)
44
                                  (or (ar--beginning-of-statement-p)
45
                                      (ar-backward-statement))
46
                                  (current-indentation)))))
47
        (setq erg (cond ((and (< (point) orig) (looking-at (symbol-value regexp)))
48
                         (point))
49
                        ((and (eq 0 (current-column)) (numberp indent) (< 0 indent))
50
                         (when (< 0 (abs (skip-chars-backward " \t\r\n\f")))
51
                           (ar-backward-statement)
52
                           (unless (looking-at (symbol-value regexp))
53
                             (cdr (ar--go-to-keyword (symbol-value regexp) (current-indentation))))))
54
                        ((numberp indent)
55
			 (cdr (ar--go-to-keyword (symbol-value regexp) indent)))
56
                        (t (ignore-errors
57
                             (cdr (ar--go-to-keyword (symbol-value regexp)
2654 by Andreas Röhler
Provide `py-fast-output-buffer' default buffer-name
58
                                                    (- (progn (if (ar--beginning-of-statement-p) (current-indentation) (save-excursion (ar-backward-statement) (current-indentation)))) py-indent-offset)))))))
2618 by Andreas Röhler
`py-up-base' fixed
59
        (when lc (beginning-of-line) (setq erg (point)))))
2622 by Andreas Röhler
beginning-position-forms fixed
60
    ;; (when (and ar-verbose-p iact) (message "%s" erg))
2618 by Andreas Röhler
`py-up-base' fixed
61
    erg))
62
2641 by Andreas Röhler
Avoid circular dependency
63
(defun py--indent-prepare (inter-re)
2630 by Andreas Röhler
New `ar--beginning-of-prepare', minor change
64
  (progn (back-to-indentation)
65
	 (or (ar--beginning-of-statement-p)
66
	     (ar-backward-statement))
67
	 (cond ((eq 0 (current-indentation))
68
		(current-indentation))
69
	       ((looking-at (symbol-value inter-re))
70
		(current-indentation))
71
	       (t
2654 by Andreas Röhler
Provide `py-fast-output-buffer' default buffer-name
72
		(if (<= py-indent-offset (current-indentation))
73
		    (- (current-indentation) (if ar-smart-indentation (ar-guess-indent-offset) py-indent-offset))
74
		  py-indent-offset)))))
2630 by Andreas Röhler
New `ar--beginning-of-prepare', minor change
75
2618 by Andreas Röhler
`py-up-base' fixed
76
(defun ar--beginning-of-prepare (indent final-re &optional inter-re iact lc)
77
  (let ((orig (point))
2641 by Andreas Röhler
Avoid circular dependency
78
        (indent (or indent (py--indent-prepare inter-re)))
2630 by Andreas Röhler
New `ar--beginning-of-prepare', minor change
79
        erg)
80
    (if (and (< (point) orig) (looking-at (symbol-value final-re)))
81
        (progn
82
          (and lc (beginning-of-line))
83
          (setq erg (point))
84
          ;; (when (and ar-verbose-p iact) (message "%s" erg))
85
          erg)
86
      (ar--beginning-of-form-intern final-re iact indent orig lc))))
87
88
(defun ar--end-of-prepare (indent final-re &optional inter-re iact lc)
89
  (let ((orig (point))
2618 by Andreas Röhler
`py-up-base' fixed
90
        (indent
91
         (or indent
92
             (progn (back-to-indentation)
93
                    (or (ar--beginning-of-statement-p)
94
                        (ar-backward-statement))
95
                    (cond ((eq 0 (current-indentation))
96
                           (current-indentation))
97
                          ((looking-at (symbol-value inter-re))
98
                           (current-indentation))
99
                          (t
2654 by Andreas Röhler
Provide `py-fast-output-buffer' default buffer-name
100
                           (if (<= py-indent-offset (current-indentation))
101
                               (- (current-indentation) (if ar-smart-indentation (ar-guess-indent-offset) py-indent-offset))
102
                             py-indent-offset))))))
2618 by Andreas Röhler
`py-up-base' fixed
103
        erg)
2630 by Andreas Röhler
New `ar--beginning-of-prepare', minor change
104
    (if (and (< orig (point)) (looking-at (symbol-value final-re)))
2618 by Andreas Röhler
`py-up-base' fixed
105
        (progn
106
          (and lc (beginning-of-line))
107
          (setq erg (point))
2622 by Andreas Röhler
beginning-position-forms fixed
108
          ;; (when (and ar-verbose-p iact) (message "%s" erg))
2618 by Andreas Röhler
`py-up-base' fixed
109
          erg)
110
      (ar--beginning-of-form-intern final-re iact indent orig lc))))
2615 by Andreas Röhler
`py--beginning-of-' forms simplified
111
112
(defun py-separator-char ()
113
  "Return the file-path separator char from current machine.
114
115
When `py-separator-char' is customized, its taken.
116
Returns char found. "
117
  (let ((erg (cond ((characterp py-separator-char)
118
                    (char-to-string py-separator-char))
119
                   ;; epd hack
120
                   ((and
121
                     (string-match "[Ii][Pp]ython" py-shell-name)
122
                     (string-match "epd\\|EPD" py-shell-name))
123
                    (replace-regexp-in-string "\n" ""
124
                                              (shell-command-to-string (concat py-shell-name " -c \"import os; print(os.sep)\"")))))))
125
    (if (and erg (string-match "^$" erg))
126
        (setq erg (substring erg (string-match "^$" erg)))
127
      (setq erg (replace-regexp-in-string "\n" "" (shell-command-to-string (concat py-shell-name " -W ignore" " -c \"import os; print(os.sep)\"")))))
128
    erg))
129
130
(defun pps-emacs-version ()
131
  "Include the appropriate `parse-partial-sexp' "
132
  (if (featurep 'xemacs)
133
      '(parse-partial-sexp (point-min) (point))
134
    '(parse-partial-sexp (point-min) (point))))
135
136
(defun py-in-comment-p ()
137
  "Return the beginning of current line's comment, if inside. "
138
  (interactive)
139
  (let* ((pps (parse-partial-sexp (point-min) (point)))
140
	 (erg (and (nth 4 pps) (nth 8 pps))))
141
    erg))
2623 by Andreas Röhler
Fixes WRT lexical scope
142
;;
2615 by Andreas Röhler
`py--beginning-of-' forms simplified
143
(defun py-in-string-or-comment-p ()
144
  "Returns beginning position if inside a string or comment, nil otherwise. "
145
  (or (nth 8 (parse-partial-sexp (point-min) (point)))
146
      (when (or (looking-at "\"")(looking-at "[ \t]*#[ \t]*"))
147
        (point))))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
148
149
(when py-org-cycle-p
150
  (define-key python-mode-map (kbd "<backtab>") 'org-cycle))
151
2666 by Andreas Röhler
lp:1654606, Anaconda implies a Python3
152
(defun py--buffer-filename-remote-maybe (&optional file-name buffer)
2678 by Andreas Röhler
`py--buffer-filename-remote-maybe' fixed
153
  (let ((file-name (or file-name (ignore-errors (file-readable-p (buffer-file-name))))))
2641 by Andreas Röhler
Avoid circular dependency
154
    (if (and (featurep 'tramp) (tramp-tramp-file-p file-name))
155
	(tramp-file-name-localname
156
	 (tramp-dissect-file-name file-name))
2678 by Andreas Röhler
`py--buffer-filename-remote-maybe' fixed
157
      file-name)))
2512 by Andreas Röhler
Provide `py-backward-buffer' `py-forward-buffer', lp:1480702
158
159
(defun py-forward-buffer ()
160
  "A complementary form used by auto-generated commands.
161
162
Returns position reached if successful"
163
  (interactive)
164
  (unless (eobp)
165
    (goto-char (point-max))))
166
167
(defun py-backward-buffer ()
168
  "A complementary form used by auto-generated commands.
169
170
Returns position reached if successful"
171
  (interactive)
172
  (unless (bobp)
173
    (goto-char (point-min))))
174
2651 by Andreas Röhler
Arguments fixed
175
(defun py--execute-prepare (form &optional shell dedicated switch beg end file fast proc wholebuf split)
2356 by Andreas Röhler
IPython3, IPython2.7 forms added
176
  "Used by python-extended-executes ."
177
  (save-excursion
2436 by Andreas Röhler
executes reconsidered
178
    (let* ((form (prin1-to-string form))
2498 by Andreas Röhler
Avoid assignment to free variable `line'
179
	   (origline (py-count-lines))
2436 by Andreas Röhler
executes reconsidered
180
	   (beg (unless file
2356 by Andreas Röhler
IPython3, IPython2.7 forms added
181
                  (prog1
182
                      (or beg (funcall (intern-soft (concat "py--beginning-of-" form "-p")))
183
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
184
                          (funcall (intern-soft (concat "py-backward-" form)))
2356 by Andreas Röhler
IPython3, IPython2.7 forms added
185
                          (push-mark)))))
186
           (end (unless file
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
187
                  (or end (funcall (intern-soft (concat "py-forward-" form))))))
2356 by Andreas Röhler
IPython3, IPython2.7 forms added
188
           filename)
189
      (setq py-buffer-name nil)
190
      (if file
191
          (progn
192
            (setq filename (expand-file-name form))
193
            (if (file-readable-p filename)
2638 by Andreas Röhler
Still more fixes WRT lexical-binding
194
                (py--execute-file-base nil filename nil nil origline)
2356 by Andreas Röhler
IPython3, IPython2.7 forms added
195
              (message "%s not readable. %s" file "Do you have write permissions?")))
2651 by Andreas Röhler
Arguments fixed
196
        (py--execute-base beg end shell filename proc file wholebuf fast dedicated split switch)))))
2356 by Andreas Röhler
IPython3, IPython2.7 forms added
197
2304 by Andreas Röhler
Provide skeletons with directory extensions
198
(defun py-load-skeletons ()
199
  "Load skeletons from extensions. "
2312 by Andreas Röhler
TAB-completion in IPyton, lp:1425268
200
  (interactive)
2304 by Andreas Röhler
Provide skeletons with directory extensions
201
  (load (concat py-install-directory "/extensions/python-components-skeletons.el")))
202
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
203
(defun py--kill-emacs-hook ()
204
  "Delete files in `py-file-queue'.
205
These are Python temporary files awaiting execution."
206
  (mapc #'(lambda (filename)
207
            (ignore-errors (delete-file filename)))
208
        py-file-queue))
209
210
;;  Add a designator to the minor mode strings
211
(or (assq 'py-pdbtrack-is-tracking-p minor-mode-alist)
212
    (push '(py-pdbtrack-is-tracking-p py-pdbtrack-minor-mode-string)
213
          minor-mode-alist))
214
215
;;  bottle.py
216
;;  py   = sys.version_info
217
;;  py3k = py >= (3,0,0)
218
;;  py25 = py <  (2,6,0)
219
;;  py31 = (3,1,0) <= py < (3,2,0)
220
221
;;  sys.version_info[0]
222
(defun py-python-version (&optional executable verbose)
223
  "Returns versions number of a Python EXECUTABLE, string.
224
225
If no EXECUTABLE given, `py-shell-name' is used.
226
Interactively output of `--version' is displayed. "
227
  (interactive)
228
  (let* ((executable (or executable py-shell-name))
2312 by Andreas Röhler
TAB-completion in IPyton, lp:1425268
229
         (erg (py--string-strip (shell-command-to-string (concat executable " --version")))))
2410 by Andreas Röhler
Reduce compiler warnings
230
    (when (called-interactively-p 'any) (message "%s" erg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
231
    (unless verbose (setq erg (cadr (split-string erg))))
232
    erg))
233
234
(defun py-version ()
235
  "Echo the current version of `python-mode' in the minibuffer."
236
  (interactive)
237
  (message "Using `python-mode' version %s" py-version)
238
  (py-keep-region-active))
239
240
;;  Utility stuff
241
(declare-function compilation-shell-minor-mode "compile" (&optional arg))
242
243
;; dereived from shipped python.el
244
(defun py-history-input-filter (str)
245
  "`comint-input-filter' function for Python process.
246
Don't save anything for STR matching `py-history-filter-regexp'."
247
  (not (string-match py-history-filter-regexp str)))
248
249
(defun py-load-file (file-name)
250
  "Load a Python file FILE-NAME into the Python process.
251
252
If the file has extension `.py' import or reload it as a module.
253
Treating it as a module keeps the global namespace clean, provides
254
function location information for debugging, and supports users of
255
module-qualified names."
256
  (interactive "f")
257
  (py--execute-file-base (get-buffer-process (get-buffer (py-shell))) file-name))
258
259
(defun py-proc (&optional argprompt)
260
  "Return the current Python process.
261
262
Start a new process if necessary. "
263
  (interactive "P")
264
  (let ((erg
265
         (cond ((comint-check-proc (current-buffer))
266
		(get-buffer-process (buffer-name (current-buffer))))
267
	       (t (py-shell argprompt)))))
2622 by Andreas Röhler
beginning-position-forms fixed
268
    ;; (when (called-interactively-p 'any) (message "%S" erg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
269
    erg))
270
271
;;  Miscellany.
2641 by Andreas Röhler
Avoid circular dependency
272
(defun py--shell-simple-send (proc strg)
273
  (let* ((strg (substring-no-properties strg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
274
         (nln (string-match "\n$" strg)))
275
    ;; (or nln (setq strg (concat strg "\n")))
2641 by Andreas Röhler
Avoid circular dependency
276
    ;; (comint-simple-send proc (substring-no-properties strg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
277
    (process-send-string proc strg)
278
    (or nln (process-send-string proc "\n"))))
279
280
(defalias
281
  'py-shell-redirect-send-command-to-process
282
  'comint-redirect-send-command-to-process)
283
(defalias
284
  'py-shell-dynamic-simple-complete
285
  'comint-dynamic-simple-complete)
286
287
;;  Hooks
288
;;  arrange to kill temp files when Emacs exists
289
(add-hook 'kill-emacs-hook 'py--kill-emacs-hook)
290
291
(when py--warn-tmp-files-left-p
292
  (add-hook 'python-mode-hook 'py--warn-tmp-files-left))
293
294

295
(defun py-guess-pdb-path ()
296
  "If py-pdb-path isn't set, find location of pdb.py. "
297
  (interactive)
298
  (let ((ele (split-string (shell-command-to-string "whereis python")))
299
        erg)
300
    (while (or (not erg)(string= "" erg))
301
      (when (and (string-match "^/" (car ele)) (not (string-match "/man" (car ele))))
302
        (setq erg (shell-command-to-string (concat "find " (car ele) " -type f -name \"pdb.py\""))))
303
      (setq ele (cdr ele)))
304
    (if erg
305
        (message "%s" erg)
2333 by Andreas Röhler
`py-update-gud-pdb-history' fixed
306
      (message "%s" "pdb.py not found, please customize `py-pdb-path'"))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
307
    erg))
308
309
(if py-mode-output-map
310
    nil
311
  (setq py-mode-output-map (make-sparse-keymap))
312
  (define-key py-mode-output-map [button2]  'py-mouseto-exception)
313
  (define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception)
314
  ;; TBD: Disable all self-inserting keys.  This is bogus, we should
315
  ;; really implement this as *Python Output* buffer being read-only
316
  (mapc #' (lambda (key)
317
             (define-key py-mode-output-map key
318
               #'(lambda () (interactive) (beep))))
319
           (where-is-internal 'self-insert-command)))
320
321
;;  backward compatibility
322
(defalias 'py-switch-shells 'py-switch-shell)
323
(defalias 'py-toggle-shell 'py-switch-shell)
324
(defun py-switch-shell (&optional arg)
325
  "Toggles between the interpreter customized in `py-shell-toggle-1' resp. `py-shell-toggle-2'. Was hard-coded CPython and Jython in earlier versions, now starts with Python2 and Python3 by default.
326
327
ARG might be a python-version string to set to.
328
329
\\[universal-argument] `py-toggle-shell' prompts to specify a reachable Python command.
330
\\[universal-argument] followed by numerical arg 2 or 3, `py-toggle-shell' opens a respective Python shell.
331
\\[universal-argument] followed by numerical arg 5 opens a Jython shell.
332
333
Should you need more shells to select, extend this command by adding inside the first cond:
334
335
                    ((eq NUMBER (prefix-numeric-value arg))
336
                     \"MY-PATH-TO-SHELL\")"
337
  (interactive "P")
338
  (let ((name (cond ((eq 2 (prefix-numeric-value arg))
339
                     "python2")
340
                    ((eq 3 (prefix-numeric-value arg))
341
                     "python3")
342
                    ((eq 4 (prefix-numeric-value arg))
2312 by Andreas Röhler
TAB-completion in IPyton, lp:1425268
343
                     (py--string-strip
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
344
                      (read-from-minibuffer "Python Shell: " py-shell-name) "\" " "\" "
345
                      ))
346
                    ((eq 5 (prefix-numeric-value arg))
347
                     "jython")
348
                    (t (if (string-match py-shell-name
349
                                         py-shell-toggle-1)
350
                           py-shell-toggle-2
351
                         py-shell-toggle-1))))
352
        erg msg)
353
    (cond ((or (string= "ipython" name)
354
               (string= "IPython" name))
355
           (setq py-shell-name name
356
                 py-which-bufname "IPython"
357
                 msg "IPython"
358
                 mode-name "IPython"))
359
          ((string-match "python3" name)
360
           (setq py-shell-name name
361
                 py-which-bufname (py--choose-buffer-name)
362
                 msg "CPython"
363
                 mode-name (py--choose-buffer-name)))
364
          ((string-match "jython" name)
365
           (setq py-shell-name name
366
                 py-which-bufname (py--choose-buffer-name)
367
                 msg "Jython"
368
                 mode-name (py--choose-buffer-name)))
369
          ((string-match "python" name)
370
           (setq py-shell-name name
371
                 py-which-bufname (py--choose-buffer-name)
372
                 msg "CPython"
373
                 mode-name py-which-bufname))
374
          (t
375
           (setq py-shell-name name
376
                 py-which-bufname name
377
                 msg name
378
                 mode-name name)))
379
    ;; py-edit-only-p has no interpreter
380
    ;; (if py-edit-only-p
381
    ;; (setq erg py-shell-name)
382
    (setq erg (executable-find py-shell-name))
383
    ;;)
384
    (if erg
385
        (progn
386
          (force-mode-line-update)
2410 by Andreas Röhler
Reduce compiler warnings
387
          (when (called-interactively-p 'any)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
388
            (message "Using the %s shell, %s" msg erg))
389
          (setq py-output-buffer (format "*%s Output*" py-which-bufname)))
390
      (error (concat "Could not detect " py-shell-name " on your sys
391
tem")))))
392
393
(defun py-toggle-local-default-use ()
394
  (interactive)
395
  "Toggle boolean value of `py-use-local-default'.
396
397
Returns `py-use-local-default'
398
399
See also `py-install-local-shells'
400
Installing named virualenv shells is the preffered way,
401
as it leaves your system default unchanged."
402
  (setq py-use-local-default (not py-use-local-default))
2410 by Andreas Röhler
Reduce compiler warnings
403
  (when (called-interactively-p 'any) (message "py-use-local-default set to %s" py-use-local-default))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
404
  py-use-local-default)
405
406
(defalias 'py-hungry-delete-forward 'c-hungry-delete-forward)
407
(defalias 'py-hungry-delete-backwards 'c-hungry-delete-backwards)
408
409
;;  FixMe: for unknown reasons this is not done by mode
410
(if (file-readable-p abbrev-file-name)
411
    (add-hook 'python-mode-hook
412
              (lambda ()
413
                (setq py-this-abbrevs-changed abbrevs-changed)
414
                (load abbrev-file-name nil t)
415
                (setq abbrevs-changed py-this-abbrevs-changed)))
416
  (message "Warning: %s" "no abbrev-file found, customize `abbrev-file-name' in order to make mode-specific abbrevs work. "))
417
418
;; ;
2639 by Andreas Röhler
lexical-binding: don't use add-to-list
419
(push (list
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
420
              'python-mode
421
              ;; start regex
422
              (concat (if py-hide-show-hide-docstrings
423
                          "^\\s-*\"\"\"\\|" "")
424
                      (mapconcat 'identity
425
                                 (mapcar #'(lambda (x) (concat "^\\s-*" x "\\_>"))
426
                                         py-hide-show-keywords)
427
                                 "\\|"))
428
              ;; end regex
429
              nil
430
              ;; comment-start regex
431
              "#"
432
              ;; forward-sexp function
2654 by Andreas Röhler
Provide `py-fast-output-buffer' default buffer-name
433
              (lambda ()
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
434
                (py-forward-block-or-clause))
2639 by Andreas Röhler
lexical-binding: don't use add-to-list
435
              nil) hs-special-modes-alist)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
436
437
;; ;
438
439
(defun py--input-filter (str)
440
  "`comint-input-filter' function for Python.
441
442
Don't save anything for STR matching `py-input-filter-re' "
443
  (not (string-match py-input-filter-re str)))
444
445
(make-obsolete 'jpython-mode 'jython-mode nil)
446
2639 by Andreas Röhler
lexical-binding: don't use add-to-list
447
(push (purecopy "*Python*")  same-window-buffer-names)
448
(push (purecopy "*IPython*")  same-window-buffer-names)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
449
2639 by Andreas Röhler
lexical-binding: don't use add-to-list
450
(push (cons (purecopy "\\.py\\'")  'python-mode)  auto-mode-alist)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
451
2523 by Andreas Röhler
Support some more suffixes
452
;; Python Macro File
2676 by Andreas Röhler
window on top, lp:1579309
453
454
(unless (member '(".pym'" . python-mode) auto-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
455
  (push (cons (purecopy "\\.pym\\'")  'python-mode)  auto-mode-alist))
2676 by Andreas Röhler
window on top, lp:1579309
456
457
(unless (member '(".pyc'" . python-mode)  auto-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
458
  (push (cons (purecopy "\\.pyc\\'")  'python-mode)  auto-mode-alist))
2523 by Andreas Röhler
Support some more suffixes
459
460
;; Pyrex Source
2676 by Andreas Röhler
window on top, lp:1579309
461
(unless (member '(".pyx'" . python-mode)  auto-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
462
  (push (cons (purecopy "\\.pyx\\'")  'python-mode) auto-mode-alist))
2523 by Andreas Röhler
Support some more suffixes
463
464
;; Python Optimized Code
2676 by Andreas Röhler
window on top, lp:1579309
465
(unless (member '(".pyo'" . python-mode)  auto-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
466
  (push (cons (purecopy "\\.pyo\\'")  'python-mode) auto-mode-alist))
2523 by Andreas Röhler
Support some more suffixes
467
468
;; Pyrex Definition File
2676 by Andreas Röhler
window on top, lp:1579309
469
(unless (member '(".pxd'" . python-mode)  auto-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
470
  (push (cons (purecopy "\\.pxd\\'")  'python-mode) auto-mode-alist))
2523 by Andreas Röhler
Support some more suffixes
471
472
;; Python Repository
2676 by Andreas Röhler
window on top, lp:1579309
473
(unless (member '(".pyr'" . python-mode)  auto-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
474
  (push (cons (purecopy "\\.pyr\\'")  'python-mode)  auto-mode-alist))
2523 by Andreas Röhler
Support some more suffixes
475
476
;; Python Path Configuration
2676 by Andreas Röhler
window on top, lp:1579309
477
(unless (member '(".pth'" . python-mode)  auto-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
478
  (push (cons (purecopy "\\.pth\\'")  'python-mode)  auto-mode-alist))
2523 by Andreas Röhler
Support some more suffixes
479
480
;; Python Wheels
2676 by Andreas Röhler
window on top, lp:1579309
481
(unless (member '(".whl'" . python-mode)  auto-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
482
  (push (cons (purecopy "\\.whl\\'")  'python-mode)  auto-mode-alist))
2639 by Andreas Röhler
lexical-binding: don't use add-to-list
483
2676 by Andreas Röhler
window on top, lp:1579309
484
(unless (member '("!#[ 	]*/.*[jp]ython[0-9.]*" . python-mode) magic-mode-alist)
2677 by Andreas Röhler
Update auto-mode-alist, minor fix
485
  (push '("!#[ \\t]*/.*[jp]ython[0-9.]*" . python-mode) magic-mode-alist))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
486
487
;;  lp:1355458, what about using `magic-mode-alist'?
488
2561 by Andreas Röhler
#1, `py-comment-indent', new command
489
(defun py--uncomment-intern (beg end)
490
  (uncomment-region beg end)
491
  (when py-uncomment-indents-p
492
    (py-indent-region beg end)))
493
2654 by Andreas Röhler
Provide `py-fast-output-buffer' default buffer-name
494
(defun py-uncomment (&optional beg)
2561 by Andreas Röhler
#1, `py-comment-indent', new command
495
  "Uncomment commented lines at point.
496
497
If region is active, restrict uncommenting at region "
498
  (interactive "*")
499
  (save-excursion
500
    (save-restriction
501
      (when (use-region-p)
502
        (narrow-to-region (region-beginning) (region-end)))
503
      (let* (last
504
             (beg (or beg (save-excursion
505
                            (while (and (py-beginning-of-comment) (setq last (point))(prog1 (forward-line -1)(end-of-line))))
506
                            last))))
507
        (and (py-forward-comment))
508
        (py--uncomment-intern beg (point))))))
509
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
510
(defun py--set-auto-fill-values ()
511
  "Internal use by `py--run-auto-fill-timer'"
2387 by Andreas Röhler
Some cleanup
512
  (let ((pps (parse-partial-sexp (point-min) (point))))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
513
    (cond ((and (nth 4 pps)(numberp py-comment-fill-column))
514
           (setq fill-column py-comment-fill-column))
515
          ((and (nth 3 pps)(numberp py-docstring-fill-column))
516
           (set (make-local-variable 'fill-column) py-docstring-fill-column))
517
          (t (setq fill-column py-fill-column-orig)))))
518
519
(defun py--run-auto-fill-timer ()
520
  "Set fill-column to values of `py-docstring-fill-column' resp. to `py-comment-fill-column' according to environment. "
521
  (when py-auto-fill-mode
522
    (unless py-autofill-timer
523
      (setq py-autofill-timer
524
            (run-with-idle-timer
525
             py-autofill-timer-delay t
526
             'py--set-auto-fill-values)))))
527
528
;;  unconditional Hooks
529
;;  (orgstruct-mode 1)
530
(add-hook 'python-mode-hook
531
	  (lambda ()
532
	    (setq imenu-create-index-function py--imenu-create-index-function)
533
	    (setq indent-tabs-mode py-indent-tabs-mode)))
534
535
(remove-hook 'python-mode-hook 'python-setup-brm)
536
537
(defun py-complete-auto ()
538
  "Auto-complete function using py-complete. "
539
  ;; disable company
540
  ;; (when company-mode (company-mode))
541
  (let ((modified (buffer-chars-modified-tick)))
542
    ;; don't try completion if buffer wasn't modified
543
    (unless (eq modified py-complete-last-modified)
544
      (if py-auto-completion-mode-p
545
	  (if (string= "*PythonCompletions*" (buffer-name (current-buffer)))
546
	      (sit-for 0.1 t)
547
	    (if
548
		(eq py-auto-completion-buffer (current-buffer))
549
		;; not after whitespace, TAB or newline
550
		(unless (member (char-before) (list 32 9 10))
551
		  (py-complete)
552
		  (setq py-complete-last-modified (buffer-chars-modified-tick)))
553
	      (setq py-auto-completion-mode-p nil
554
		    py-auto-completion-buffer nil)
555
	      (cancel-timer py--auto-complete-timer)))))))
556
1867 by Andreas Röhler
New boolean py-debug-p, default is nil
557
(defun py-set-command-args (arguments)
1874 by Andreas Röhler
`py--narrow-in-comint-modes' new function
558
  "Set Python arguments on the fly, override defaults in this session.
1868 by Andreas Röhler
Redundant `set-default', minor cleanup
559
560
Use `defcustom' to keep value across sessions "
1867 by Andreas Röhler
New boolean py-debug-p, default is nil
561
  (interactive
562
   (list
1868 by Andreas Röhler
Redundant `set-default', minor cleanup
563
    (read-from-minibuffer "Command args: " py-python-command-args)))
564
    (setq py-python-command-args arguments))
949 by Andreas Roehler
stops-backslashed-line lp:802504 and related fixed
565
2011 by Andreas Röhler
Some commands not so useful made common functions, like
566
(defun py---emacs-version-greater-23 ()
1931 by Andreas Röhler
`py--emacs-version-greater-23', internal function
567
  "Return `t' if emacs major version is above 23"
568
  (< 23 (string-to-number (car (split-string emacs-version "\\.")))))
569
2654 by Andreas Röhler
Provide `py-fast-output-buffer' default buffer-name
570
(defun py--empty-arglist-indent (nesting &optional indent-offset indent-offset)
1740 by Andreas Röhler
closing-list, lp:826144, again
571
  "Internally used by `py-compute-indentation'"
572
  (if
573
      (and (eq 1 nesting)
574
           (save-excursion
575
             (back-to-indentation)
576
             (looking-at py-extended-block-or-clause-re)))
577
      (progn
578
        (back-to-indentation)
579
        (+ (current-column) (* 2 (or indent-offset py-indent-offset))))
2654 by Andreas Röhler
Provide `py-fast-output-buffer' default buffer-name
580
    (+ (current-indentation) (or indent-offset py-indent-offset))))
1740 by Andreas Röhler
closing-list, lp:826144, again
581
1908 by Andreas Röhler
info-lookup-symbol, lp:1281950
582
(defun py-symbol-at-point ()
583
  "Return the current Python symbol."
584
  (interactive)
585
  (let ((erg (with-syntax-table
586
                 py-dotted-expression-syntax-table
587
               (current-word))))
2622 by Andreas Röhler
beginning-position-forms fixed
588
    ;; (when (called-interactively-p 'any) (message "%s" erg))
1908 by Andreas Röhler
info-lookup-symbol, lp:1281950
589
    erg))
590
2288 by Andreas Röhler
`py-kill-buffer-unconditional' minor fix
591
(defun py-kill-buffer-unconditional (buffer)
1951 by Andreas Röhler
large output makes Emacs freeze, lp:1253907
592
  "Kill buffer unconditional, kill buffer-process if existing. "
2288 by Andreas Röhler
`py-kill-buffer-unconditional' minor fix
593
  (interactive
594
   (list (current-buffer)))
2314 by Andreas Röhler
New variable `py-ert-test-default-executables'
595
  (let ((buffer (or (and (bufferp buffer) buffer)
2312 by Andreas Röhler
TAB-completion in IPyton, lp:1425268
596
		    (get-buffer buffer)))
597
	proc kill-buffer-query-functions)
598
2071 by Andreas Röhler
`py-shell', `py--buffer-name-prepare' fixed
599
    (ignore-errors
600
      (setq proc (get-buffer-process buffer))
601
      (and proc (kill-process proc))
602
      (set-buffer buffer)
2101 by Andreas Röhler
Completion in IPython shell fixed
603
      (set-buffer-modified-p 'nil)
2071 by Andreas Röhler
`py-shell', `py--buffer-name-prepare' fixed
604
      (kill-buffer (current-buffer)))))
1951 by Andreas Röhler
large output makes Emacs freeze, lp:1253907
605
2011 by Andreas Röhler
Some commands not so useful made common functions, like
606
(defun py--line-backward-maybe ()
2498 by Andreas Röhler
Avoid assignment to free variable `line'
607
  "Return result of (< 0 (abs (skip-chars-backward \" \\t\\r\\n\\f\"))) "
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
608
  (skip-chars-backward " \t\f" (line-beginning-position))
609
  (< 0 (abs (skip-chars-backward " \t\r\n\f"))))
1841 by Andreas Röhler
New boolean `py-empty-line-closes-p', lp:1235324
610
2011 by Andreas Röhler
Some commands not so useful made common functions, like
611
(defun py--after-empty-line ()
1841 by Andreas Röhler
New boolean `py-empty-line-closes-p', lp:1235324
612
  "Return `t' if line before contains only whitespace characters. "
613
  (save-excursion
614
    (beginning-of-line)
615
    (forward-line -1)
616
    (beginning-of-line)
617
    (looking-at "\\s-*$")))
1808 by Andreas Röhler
`py-execute-region' error-messages fixed
618
898 by Andreas Roehler
Allow different indents of multiline-listings
619
(defalias 'py-count-indentation 'py-compute-indentation)
2629 by Andreas Röhler
`py-compute-indentation', honor py-verbose-p and interactive calls
620
(defun py-compute-indentation (&optional iact orig origline closing line nesting repeat indent-offset liep)
891 by Andreas Roehler
yet another triple quoted string fix
621
  "Compute Python indentation.
1223 by Andreas Roehler
several docstrings normalised
622
623
When HONOR-BLOCK-CLOSE-P is non-nil, statements such as `return',
1703 by Andreas Roehler
cascading indent, lp:1101962, fixed
624
`raise', `break', `continue', and `pass' force one level of dedenting.
625
2247 by Andreas Röhler
`py-compute-indentation', docu extended
626
Optional arguments are flags resp. values set and used by `py-compute-indentation' internally:
627
ORIG keeps original position
628
ORIGLINE keeps line where compute started
629
CLOSING is t when started at a char delimiting a list as \"]})\"
630
LINE indicates being not at origline now
2552 by Andreas Röhler
#14, `py-indent-paren-spanned-multilines-p' new default `t'
631
NESTING is currently ignored, if executing from inside a list
2247 by Andreas Röhler
`py-compute-indentation', docu extended
632
REPEAT counter enables checks against `py-max-specpdl-size'
633
INDENT-OFFSET allows calculation of block-local values
2280 by Andreas Röhler
py-shell reuse existing unless called from inside or explicitely
634
LIEP stores line-end-position at point-of-interest
1703 by Andreas Roehler
cascading indent, lp:1101962, fixed
635
"
2629 by Andreas Röhler
`py-compute-indentation', honor py-verbose-p and interactive calls
636
  (interactive "p")
782 by Andreas Roehler
rewrite myrkwid
637
  (save-excursion
638
    (save-restriction
639
      (widen)
1852 by Andreas Röhler
Ipython TAB completion cursor stays in place, lp:1244998
640
      ;; in shell, narrow from previous prompt
1818 by Andreas Röhler
PEP8 alternatives how to close a list implemented, lp:1197521
641
      ;; needed by closing
2209 by Andreas Röhler
Bug in `py-compute-indentation' fixed
642
      (unless orig (unless (bobp) (back-to-indentation)))
1080 by Andreas Roehler
indent-match-import-pkg fixed, lp:852500
643
      (let* ((orig (or orig (point)))
2315 by Andreas Röhler
`py-kill-statements' fixed
644
             (origline (or origline (py-count-lines (point-min) (point))))
1140 by Andreas Roehler
indentation-wrong-after-multi-line-parameter-list lp:871698
645
             ;; closing indicates: when started, looked
646
             ;; at a single closing parenthesis
647
             ;; line: moved already a line backward
1866 by Andreas Röhler
`py-compute-indentation' faster
648
             (liep (or liep (line-end-position)))
1140 by Andreas Roehler
indentation-wrong-after-multi-line-parameter-list lp:871698
649
             (line line)
2629 by Andreas Röhler
`py-compute-indentation', honor py-verbose-p and interactive calls
650
	     (verbose py-verbose-p)
2387 by Andreas Röhler
Some cleanup
651
             (pps (parse-partial-sexp (point-min) (point)))
1818 by Andreas Röhler
PEP8 alternatives how to close a list implemented, lp:1197521
652
             (closing
653
              (or closing
654
                  (and (nth 1 pps)
655
                       (looking-at ".*\\(\\s)\\)")(nth 0 pps)
656
                       ;; char doesn't matter for now, maybe drop
1852 by Andreas Röhler
Ipython TAB completion cursor stays in place, lp:1244998
657
                       (string-to-char (match-string-no-properties 1)))))
1140 by Andreas Roehler
indentation-wrong-after-multi-line-parameter-list lp:871698
658
             ;; in a recursive call already
2209 by Andreas Röhler
Bug in `py-compute-indentation' fixed
659
             (repeat (if repeat
660
			 (setq repeat (1+ repeat))
661
		       0))
1737 by Andreas Röhler
enhance XEmacs compatibility, lp:1177156
662
             ;; nesting: started nesting a list
663
             (nesting nesting)
1816 by Andreas Röhler
`py-restore-window-configuration' reconsidered
664
             (indent-offset (or indent-offset py-indent-offset))
2247 by Andreas Röhler
`py-compute-indentation', docu extended
665
             (cubuf (current-buffer))
922 by Andreas Roehler
honors explicitly indents in multiline tqs and lists
666
             erg indent this-line)
2209 by Andreas Röhler
Bug in `py-compute-indentation' fixed
667
        (if (and (< repeat 1)
1852 by Andreas Röhler
Ipython TAB completion cursor stays in place, lp:1244998
668
                 (and (comint-check-proc (current-buffer))
2331 by Andreas Röhler
Some more variable-name normalizations, prefix "py-"
669
                      (re-search-backward (concat py-shell-prompt-regexp "\\|" py-ipython-output-prompt-re "\\|" py-ipython-input-prompt-re) nil t 1)))
1852 by Andreas Röhler
Ipython TAB completion cursor stays in place, lp:1244998
670
            ;; common recursion not suitable because of prompt
671
            (with-temp-buffer
2247 by Andreas Röhler
`py-compute-indentation', docu extended
672
              (insert-buffer-substring cubuf (match-end 0) orig)
1852 by Andreas Röhler
Ipython TAB completion cursor stays in place, lp:1244998
673
              (setq indent (py-compute-indentation)))
2209 by Andreas Röhler
Bug in `py-compute-indentation' fixed
674
	  (if (< py-max-specpdl-size repeat)
2641 by Andreas Röhler
Avoid circular dependency
675
	      (error "`py-compute-indentation' reached loops max.")	(setq nesting (nth 0 pps))
676
	      (setq indent
677
		    (cond ((bobp)
678
			   (cond ((eq liep (line-end-position))
679
				  0)
680
				 ((looking-at py-outdent-re)
681
				  (+ (if py-smart-indentation (py-guess-indent-offset) indent-offset) (current-indentation)))
682
				 (t
683
				  (current-indentation))))
684
			  ;; in string
685
			  ((and (nth 3 pps)(nth 8 pps))
686
			   (cond
687
			    ((py--docstring-p)
688
			     (save-excursion
689
			       (back-to-indentation)
2514 by Andreas Röhler
`py-compute-indentation' minor cleanup
690
			       (skip-chars-backward " \t\r\n\f")
2641 by Andreas Röhler
Avoid circular dependency
691
			       (back-to-indentation)
692
			       (current-indentation)))
693
			    (t 0)))
694
			  ((and (looking-at "\"\"\"\\|'''")(not (bobp)))
2514 by Andreas Röhler
`py-compute-indentation' minor cleanup
695
			   (py-backward-statement)
2641 by Andreas Röhler
Avoid circular dependency
696
			   (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
697
			  ;; comments
698
			  ((nth 8 pps)
699
			   (if (eq liep (line-end-position))
2514 by Andreas Röhler
`py-compute-indentation' minor cleanup
700
			       (progn
2641 by Andreas Röhler
Avoid circular dependency
701
				 (goto-char (nth 8 pps))
2514 by Andreas Röhler
`py-compute-indentation' minor cleanup
702
				 (when (py--line-backward-maybe) (setq line t))
2641 by Andreas Röhler
Avoid circular dependency
703
				 (skip-chars-backward " \t")
704
				 (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
705
			     (goto-char (nth 8 pps))
706
			     (if
707
				 line
708
				 (if py-indent-honors-inline-comment
709
				     (current-column)
710
				   (if py-indent-comments
711
				       (progn
712
					 (py-backward-comment)
713
					 (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
714
				     0))
715
			       (forward-char -1)
716
			       (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))))
717
			  ((and
718
			    (looking-at (concat "[ \t]*" comment-start))
719
			    (looking-back "^[ \t]*" (line-beginning-position))(not line)
720
			    (eq liep (line-end-position)))
721
			   (if py-indent-comments
722
			       (progn
723
				 (setq line t)
724
				 (skip-chars-backward " \t\r\n\f")
725
				 ;; as previous comment-line might
726
				 ;; be wrongly unindented, travel
727
				 ;; whole commented section
728
				 (py-backward-comment)
729
				 (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
730
			     0))
731
			  ((and
732
			    (looking-at (concat "[ \t]*" comment-start))
733
			    (looking-back "^[ \t]*" (line-beginning-position))
734
			    (not (eq liep (line-end-position))))
735
			   (current-indentation))
736
			  ((and (eq 11 (syntax-after (point))) line py-indent-honors-inline-comment)
737
			   (current-column))
738
			  ;; lists
739
			  ((nth 1 pps)
740
			   (save-excursion
741
			     (goto-char (nth 1 pps))
742
			     (setq this-line (py-count-lines))
743
			     (cond
744
			      ((< 0 (- origline this-line))
745
			       (if (< 1 (- origline this-line))
746
				   (cond
747
				    (closing
748
				     (cond
749
				      (py-closing-list-dedents-bos
750
				       (goto-char (nth 1 pps))
751
				       (current-indentation))
752
				      ((looking-back "^[ \t]*" (line-beginning-position))
753
				       (current-column))
754
				      ((and (looking-at "\\s([ \t]*$") py-closing-list-keeps-space)
755
				       (+ (current-column) py-closing-list-space))
756
				      ((looking-at "\\s([ \t]*$")
757
				       (py--empty-arglist-indent nesting py-indent-offset indent-offset))
758
				      ((looking-at "\\s([ \t]*\\([^ \t]+.*\\)$")
759
				       (goto-char (match-beginning 1))
760
				       (if py-indent-paren-spanned-multilines-p
761
					   (+ (current-column) py-indent-offset)
762
					 (current-column)))
763
				      (t (py--fetch-previous-indent orig))))
764
				    ;; already behind a dedented element in list
765
				    ((<= 2 (- origline this-line))
766
				     (py--fetch-previous-indent orig))
767
				    ((< (current-indentation) (current-column))
768
				     (+ (current-indentation) py-indent-offset))
769
				    (t (py--fetch-previous-indent orig)))
770
				 (cond ((looking-at "\\s([ \t]*$")
771
					(py--empty-arglist-indent nesting py-indent-offset indent-offset))
772
				       ((looking-at "\\s([ \t]*\\([^ \t]+.*\\)$")
773
					(goto-char (match-beginning 1))
774
					(if py-indent-paren-spanned-multilines-p
775
					    (+ (current-column) py-indent-offset)
776
					  (current-column)))
777
				       (t (+ (current-column) (* (nth 0 pps)))))))
778
			      ((nth 1 (parse-partial-sexp (point-min) (point)))
779
			       (goto-char (nth 1 (parse-partial-sexp (point-min) (point))))
780
			       (setq line
781
				     ;; should be faster
782
				     (< (line-end-position) liep))
783
			       (py-compute-indentation orig origline closing line nesting repeat indent-offset liep))
784
			      ((not (py--beginning-of-statement-p))
785
			       (py-backward-statement)
786
			       (py-compute-indentation orig origline closing line nesting repeat indent-offset liep))
787
			      (t (1+ (current-column))))))
788
			  ((and (eq (char-after) (or ?\( ?\{ ?\[)) line)
789
			   (1+ (current-column)))
790
			  ((py-preceding-line-backslashed-p)
791
			   (progn
2514 by Andreas Röhler
`py-compute-indentation' minor cleanup
792
			     (py-backward-statement)
2641 by Andreas Röhler
Avoid circular dependency
793
			     (setq this-line (py-count-lines))
794
			     (if (< 1 (- origline this-line))
795
				 (py--fetch-previous-indent orig)
796
			       (if (looking-at "from +\\([^ \t\n]+\\) +import")
797
				   py-backslashed-lines-indent-offset
798
				 (+ (current-indentation) py-continuation-offset)))))
799
			  ((and (looking-at py-block-closing-keywords-re)
800
				(eq liep (line-end-position)))
801
			   (skip-chars-backward "[ \t\r\n\f]")
802
			   (py-backward-statement)
803
			   (cond ((looking-at py-extended-block-or-clause-re)
804
				  (+
805
				   (if py-smart-indentation (py-guess-indent-offset) indent-offset)
806
				   (current-indentation)))
807
				 ((looking-at py-block-closing-keywords-re)
808
				  (- (current-indentation) py-indent-offset))
809
				 (t (current-column))))
810
			  ((looking-at py-block-closing-keywords-re)
811
			   (if (< (line-end-position) orig)
812
			       (- (current-indentation) py-indent-offset)
813
			     (py-backward-block-or-clause (current-indentation))
814
			     (current-indentation)))
815
			  ((and (looking-at py-elif-re) (eq (py-count-lines) origline))
816
			   (when (py--line-backward-maybe) (setq line t))
817
			   (car (py--clause-lookup-keyword py-elif-re -1 nil origline)))
818
			  ((and (looking-at py-clause-re)(not line)
819
				(eq liep (line-end-position)))
820
			   (cond ((looking-at py-finally-re)
821
				  (car (py--clause-lookup-keyword py-finally-re -1 nil origline)))
822
				 ((looking-at py-except-re)
823
				  (car (py--clause-lookup-keyword py-except-re -1 nil origline)))
824
				 ((looking-at py-else-block-re)
825
				  (car (py--clause-lookup-keyword py-else-block-re -1 nil origline)))
826
				 ((looking-at py-elif-block-re)
827
				  (car (py--clause-lookup-keyword py-elif-re -1 nil origline)))
828
				 ;; maybe at if, try, with
829
				 (t (car (py--clause-lookup-keyword py-block-or-clause-re -1 nil origline)))))
830
			  ((looking-at py-extended-block-or-clause-re)
831
			   (cond ((and (not line)
832
				       (eq liep (line-end-position)))
833
				  (when (py--line-backward-maybe) (setq line t))
834
				  (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
835
				 (t (+
836
				     (cond (indent-offset)
837
					   (py-smart-indentation
838
					    (py-guess-indent-offset))
839
					   (t py-indent-offset))
840
				     (current-indentation)))))
841
			  ((and
842
			    (< (line-end-position) liep)
843
			    (eq (current-column) (current-indentation)))
844
			   (and
845
			    (looking-at py-assignment-re)
846
			    (goto-char (match-end 0)))
847
			   ;; multiline-assignment
848
			   (if (and nesting (looking-at " *[[{(]")(not (looking-at ".+[]})][ \t]*$")))
849
			       (+ (current-indentation) py-indent-offset)
850
			     (current-indentation)))
851
			  ((looking-at py-assignment-re)
852
			   (py-backward-statement)
853
			   (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
854
			  ((and (< (current-indentation) (current-column))(not line))
2514 by Andreas Röhler
`py-compute-indentation' minor cleanup
855
			   (back-to-indentation)
2641 by Andreas Röhler
Avoid circular dependency
856
			   (unless line
857
			     (setq nesting (nth 0 (parse-partial-sexp (point-min) (point)))))
858
			   (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
859
			  ((and (not (py--beginning-of-statement-p)) (not (and line (eq 11 (syntax-after (point))))))
860
			   (if (bobp)
861
			       (current-column)
862
			     (if (eq (point) orig)
863
				 (progn
864
				   (when (py--line-backward-maybe) (setq line t))
865
				   (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
866
			       (py-backward-statement)
867
			       (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))))
868
			  ((or (py--statement-opens-block-p py-extended-block-or-clause-re)(looking-at "@"))
869
			   (if (< (py-count-lines) origline)
870
			       (+ (if py-smart-indentation (py-guess-indent-offset) indent-offset) (current-indentation))
871
			     (skip-chars-backward " \t\r\n\f")
872
			     (setq line t)
873
			     (back-to-indentation)
874
			     (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep)))
875
			  ((and py-empty-line-closes-p (py--after-empty-line))
876
			   (progn (py-backward-statement)
877
				  (- (current-indentation) py-indent-offset)))
878
			  ;; still at orignial line
879
			  ((and (eq liep (line-end-position))
880
				(save-excursion
881
				  (and (setq erg (py--go-to-keyword py-extended-block-or-clause-re))
882
				       (if py-smart-indentation (setq indent-offset (py-guess-indent-offset)) t)
883
				       (ignore-errors (< orig (or (py-forward-block-or-clause)(point)))))))
884
			   (+ (car erg) (if py-smart-indentation
885
					    (or indent (py-guess-indent-offset))
886
					  indent-offset)))
887
			  ((and (not line)
888
				(eq liep (line-end-position))
889
				(py--beginning-of-statement-p))
890
			   (py-backward-statement)
891
			   (py-compute-indentation iact orig origline closing line nesting repeat indent-offset liep))
892
			  (t (current-indentation))))
893
	      (when (and verbose iact) (message "%s" indent))
894
	      indent))))))
362 by Andreas Roehler
towards a Python-IDE
895
2011 by Andreas Röhler
Some commands not so useful made common functions, like
896
(defun py--fetch-previous-indent (orig)
922 by Andreas Roehler
honors explicitly indents in multiline tqs and lists
897
  "Report the preceding indent. "
898
  (save-excursion
1080 by Andreas Roehler
indent-match-import-pkg fixed, lp:852500
899
    (goto-char orig)
922 by Andreas Roehler
honors explicitly indents in multiline tqs and lists
900
    (forward-line -1)
901
    (end-of-line)
902
    (skip-chars-backward " \t\r\n\f")
903
    (current-indentation)))
904
923 by Andreas Roehler
explicit-backslashed-continuation-line-indent lp:785091 fixed
905
(defun py-continuation-offset (&optional arg)
906
  "With numeric ARG different from 1 py-continuation-offset is set to that value; returns py-continuation-offset. "
1080 by Andreas Roehler
indent-match-import-pkg fixed, lp:852500
907
  (interactive "p")
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
908
  (and (numberp arg) (not (eq 1 arg)) (setq py-continuation-offset arg))
909
  (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" py-continuation-offset))
910
  py-continuation-offset)
923 by Andreas Roehler
explicit-backslashed-continuation-line-indent lp:785091 fixed
911
1103 by Andreas Roehler
indentation of from import continuation lines fixed, lp:858041
912
(defalias 'pios 'py-indentation-of-statement)
913
(defalias 'ios 'py-indentation-of-statement)
914
(defun py-indentation-of-statement ()
915
  "Returns the indenation of the statement at point. "
1223 by Andreas Roehler
several docstrings normalised
916
  (interactive)
1103 by Andreas Roehler
indentation of from import continuation lines fixed, lp:858041
917
  (let ((erg (save-excursion
918
               (back-to-indentation)
2011 by Andreas Röhler
Some commands not so useful made common functions, like
919
               (or (py--beginning-of-statement-p)
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
920
                   (py-backward-statement))
1103 by Andreas Roehler
indentation of from import continuation lines fixed, lp:858041
921
               (current-indentation))))
2410 by Andreas Röhler
Reduce compiler warnings
922
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
1103 by Andreas Roehler
indentation of from import continuation lines fixed, lp:858041
923
    erg))
924
873 by Andreas Roehler
end forms refactored
925
(defalias 'py-in-list-p 'py-list-beginning-position)
898 by Andreas Roehler
Allow different indents of multiline-listings
926
(defun py-list-beginning-position (&optional start)
391 by Andreas Roehler
py-beginning-of-list...
927
  "Return lists beginning position, nil if not inside.
1223 by Andreas Roehler
several docstrings normalised
928
391 by Andreas Roehler
py-beginning-of-list...
929
Optional ARG indicates a start-position for `parse-partial-sexp'."
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
930
  (nth 1 (parse-partial-sexp (or start (point-min)) (point))))
362 by Andreas Roehler
towards a Python-IDE
931
391 by Andreas Roehler
py-beginning-of-list...
932
(defun py-end-of-list-position (&optional arg)
933
  "Return end position, nil if not inside.
1223 by Andreas Roehler
several docstrings normalised
934
391 by Andreas Roehler
py-beginning-of-list...
935
Optional ARG indicates a start-position for `parse-partial-sexp'."
936
  (interactive)
937
  (let* ((ppstart (or arg (point-min)))
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
938
         (erg (parse-partial-sexp ppstart (point)))
391 by Andreas Roehler
py-beginning-of-list...
939
         (beg (nth 1 erg))
940
         end)
393 by Andreas Roehler
py-end-of-statement
941
    (when beg
942
      (save-excursion
943
        (goto-char beg)
944
        (forward-list 1)
945
        (setq end (point))))
2410 by Andreas Röhler
Reduce compiler warnings
946
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" end))
782 by Andreas Roehler
rewrite myrkwid
947
    end))
391 by Andreas Roehler
py-beginning-of-list...
948
2011 by Andreas Röhler
Some commands not so useful made common functions, like
949
(defun py--in-comment-p ()
863 by Andreas Roehler
py-in-comment-p, py-in-string-p
950
  "Return the beginning of current line's comment, if inside. "
951
  (save-restriction
952
    (widen)
2387 by Andreas Röhler
Some cleanup
953
    (let* ((pps (parse-partial-sexp (point-min) (point)))
999 by Andreas Roehler
indent-after-assigment, thanks to Glenn Linderman reporting case 6
954
           (erg (when (nth 4 pps) (nth 8 pps))))
863 by Andreas Roehler
py-in-comment-p, py-in-string-p
955
      (unless erg
956
        (when (looking-at (concat "^[ \t]*" comment-start-skip))
957
          (setq erg (point))))
958
      erg)))
959
879 by Andreas Roehler
indent-triplequoted-to-itself bug lp:752252 fixed, test delivered
960
(defun py-in-triplequoted-string-p ()
961
  "Returns character address of start tqs-string, nil if not inside. "
962
  (interactive)
2387 by Andreas Röhler
Some cleanup
963
  (let* ((pps (parse-partial-sexp (point-min) (point)))
879 by Andreas Roehler
indent-triplequoted-to-itself bug lp:752252 fixed, test delivered
964
         (erg (when (and (nth 3 pps) (nth 8 pps))(nth 2 pps))))
965
    (save-excursion
966
      (unless erg (setq erg
967
                        (progn
968
                          (when (looking-at "\"\"\"\\|''''")
890 by Andreas Roehler
py-in-triplequoted-string-p fixed
969
                            (goto-char (match-end 0))
2387 by Andreas Röhler
Some cleanup
970
                            (setq pps (parse-partial-sexp (point-min) (point)))
879 by Andreas Roehler
indent-triplequoted-to-itself bug lp:752252 fixed, test delivered
971
                            (when (and (nth 3 pps) (nth 8 pps)) (nth 2 pps)))))))
2410 by Andreas Röhler
Reduce compiler warnings
972
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
879 by Andreas Roehler
indent-triplequoted-to-itself bug lp:752252 fixed, test delivered
973
    erg))
974
2343 by Andreas Röhler
py-in-string-p re-write
975
(defun py-in-string-p-intern (pps)
976
  (goto-char (nth 8 pps))
977
  (list (point) (char-after)(skip-chars-forward (char-to-string (char-after)))))
978
863 by Andreas Roehler
py-in-comment-p, py-in-string-p
979
(defun py-in-string-p ()
2343 by Andreas Röhler
py-in-string-p re-write
980
  "if inside a double- triple- or singlequoted string,
981
982
If non-nil, return a list composed of
983
- beginning position
984
- the character used as string-delimiter (in decimal)
985
- and length of delimiter, commonly 1 or 3 "
505 by Andreas Roehler
py-in-string/comment doku fixes
986
  (interactive)
2343 by Andreas Röhler
py-in-string-p re-write
987
  (save-excursion
988
    (let* ((pps (parse-partial-sexp (point-min) (point)))
989
	   (erg (when (nth 3 pps)
990
		  (py-in-string-p-intern pps))))
991
      (unless erg
992
	(when (looking-at "\"\\|'")
993
	  (forward-char 1)
994
	  (setq pps (parse-partial-sexp (line-beginning-position) (point)))
995
	  (when (nth 3 pps)
996
	    (setq erg (py-in-string-p-intern pps)))))
997
998
    ;; (list (nth 8 pps) (char-before) (1+ (skip-chars-forward (char-to-string (char-before)))))
2622 by Andreas Röhler
beginning-position-forms fixed
999
      ;; (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
2343 by Andreas Röhler
py-in-string-p re-write
1000
    erg)))
362 by Andreas Roehler
towards a Python-IDE
1001
416 by Andreas Roehler
py-in-statement-p, test adapted
1002
(defun py-in-statement-p ()
1003
  "Returns list of beginning and end-position if inside.
1223 by Andreas Roehler
several docstrings normalised
1004
418 by Andreas Roehler
doku
1005
Result is useful for booleans too: (when (py-in-statement-p)...)
782 by Andreas Roehler
rewrite myrkwid
1006
will work.
428 by Andreas Roehler
py-end-of-statement fix, rewrite
1007
"
782 by Andreas Roehler
rewrite myrkwid
1008
  (interactive)
471 by Andreas Roehler
py-in-statement-p kept standalone
1009
  (let ((orig (point))
690 by Andreas Roehler
py-in-string-p
1010
        beg end erg)
471 by Andreas Roehler
py-in-statement-p kept standalone
1011
    (save-excursion
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1012
      (setq end (py-forward-statement))
1013
      (setq beg (py-backward-statement))
471 by Andreas Roehler
py-in-statement-p kept standalone
1014
      (when (and (<= beg orig)(<= orig end))
1015
        (setq erg (cons beg end))
2410 by Andreas Röhler
Reduce compiler warnings
1016
        (when (called-interactively-p 'any) (message "%s" erg))
471 by Andreas Roehler
py-in-statement-p kept standalone
1017
        erg))))
416 by Andreas Roehler
py-in-statement-p, test adapted
1018
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1019
;;  Beginning-of- p
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1020
(defun py-backward-top-level-p ()
1827 by Andreas Röhler
Keys C-M-a, C-M-e usable for all top-level form, lp:1191078
1021
  "Returns position, if cursor is at the beginning of a top-level, nil otherwise. "
1022
  (interactive)
1023
  (let (erg)
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1024
    (and (py--beginning-of-statement-p)
1827 by Andreas Röhler
Keys C-M-a, C-M-e usable for all top-level form, lp:1191078
1025
         (eq 0 (current-column))
1829 by Andreas Röhler
`py-end-base' simplified
1026
         (setq erg (point))
1827 by Andreas Röhler
Keys C-M-a, C-M-e usable for all top-level form, lp:1191078
1027
      erg)))
1028
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1029
(defun py--beginning-of-buffer-p ()
1414 by Andreas Roehler
PY-SHELL-switch forms fixed
1030
  "Returns position, if cursor is at the beginning of buffer, nil otherwise. "
1031
  (when (bobp)(point)))
1032
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1033
;;  End-of- p
1539 by Andreas Roehler
`py-find-imports' use `py-end-of-statement'
1034
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1035
;;  Opens
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1036
(defun py--statement-opens-block-p (&optional regexp)
782 by Andreas Roehler
rewrite myrkwid
1037
  "Return position if the current statement opens a block
446 by Andreas Roehler
py-beginning-of-statement-p
1038
in stricter or wider sense.
1223 by Andreas Roehler
several docstrings normalised
1039
447 by Andreas Roehler
py-statement-opens-block-p fixed
1040
For stricter sense specify regexp. "
1587 by Andreas Roehler
`py-end-of-block' fixed
1041
  (let* ((regexp (or regexp py-block-or-clause-re))
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1042
         (erg (py--statement-opens-base regexp)))
2410 by Andreas Röhler
Reduce compiler warnings
1043
    (when (called-interactively-p 'any) (message "%s" erg))
797 by Andreas Roehler
py-statement-opens-block-p interactive
1044
    erg))
659 by Andreas Roehler
py-compute-indentation reconsidered resp. nested-indents
1045
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1046
(defun py--statement-opens-base (regexp)
782 by Andreas Roehler
rewrite myrkwid
1047
  (let ((orig (point))
574 by Andreas Roehler
bugfix
1048
        erg)
457 by Andreas Roehler
ar-py-statement-beginning-position-atpt used
1049
    (save-excursion
616 by Andreas Roehler
tqs in beg/end-string fixes
1050
      (back-to-indentation)
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1051
      (py-forward-statement)
1052
      (py-backward-statement)
731 by Andreas Roehler
py-statement-opens-block-p fixed
1053
      (when (and
2613 by Andreas Röhler
`looking-back' use `line-beginning-position'
1054
             (<= (line-beginning-position) orig)(looking-back "^[ \t]*" (line-beginning-position))(looking-at regexp))
457 by Andreas Roehler
ar-py-statement-beginning-position-atpt used
1055
        (setq erg (point))))
2410 by Andreas Röhler
Reduce compiler warnings
1056
    (when (called-interactively-p 'any) (message "%s" erg))
448 by Andreas Roehler
check for py-beginning-of-statement-p
1057
    erg))
362 by Andreas Roehler
towards a Python-IDE
1058
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1059
(defun py--statement-opens-clause-p ()
1060
  "Return position if the current statement opens block or clause. "
1061
  (py--statement-opens-base py-clause-re))
1062
1063
(defun py--statement-opens-block-or-clause-p ()
1064
  "Return position if the current statement opens block or clause. "
1065
  (py--statement-opens-base py-block-or-clause-re))
1066
1067
(defun py--statement-opens-class-p ()
1068
  "Return `t' if the statement opens a functions or class definition, nil otherwise. "
1069
  (py--statement-opens-base py-class-re))
1070
1071
(defun py--statement-opens-def-p ()
1072
  "Return `t' if the statement opens a functions or class definition, nil otherwise. "
1073
  (py--statement-opens-base py-def-re))
1074
1075
(defun py--statement-opens-def-or-class-p ()
1076
  "Return `t' if the statement opens a functions or class definition, nil otherwise. "
1077
  (py--statement-opens-base py-def-or-class-re))
1078
1958 by Andreas Röhler
py-mark-def hangs, lp:1294478
1079
(defun py--record-list-error (pps)
1080
  "When encountering a missing parenthesis, store its line, position. `py-verbose-p'  must be t
1081
1082
Unclosed-string errors are not handled here, as made visible by fontification already.
1083
"
1084
  (let ((this-err
1085
         (save-excursion
1086
           (list
1087
            (nth 1 pps)
1088
            (progn
1089
              (goto-char (nth 1 pps))
1090
              (py-count-lines (point-min) (point)))))))
1091
    this-err))
1092
1093
(defun py--message-error (err)
1094
  "Receives a list (position line) "
1095
  (message "Closing paren missed: line %s pos %s" (cadr err) (car err)))
1096
2494 by Andreas Röhler
py--end-base-look-upward receive regexp
1097
(defun py--end-base-look-upward (thisregexp regexp)
2387 by Andreas Röhler
Some cleanup
1098
  (progn (back-to-indentation)
2483 by Andreas Röhler
`py--end-base-look-upward' minor fix
1099
	 (let ((bofst (py--beginning-of-statement-p)))
1100
	   (cond ((and bofst (eq regexp 'py-clause-re)(looking-at py-extended-block-or-clause-re))
1101
		  (point))
1102
		 ((and bofst (looking-at thisregexp))
1103
		  (point))
1104
		 (t
1105
		  (when
1106
		      (cdr-safe
1107
		       (py--go-to-keyword
1108
			thisregexp))
1109
		    (when (py--statement-opens-block-p py-extended-block-or-clause-re)
1110
		      (point))))))))
2387 by Andreas Röhler
Some cleanup
1111
2482 by Andreas Röhler
`py--go-down-when-found-upward' again
1112
(defun py--go-down-when-found-upward (regexp)
1113
  (let ((thisindent (current-indentation))
1114
	last)
2621 by Andreas Röhler
Navigation - major rewrite
1115
    (back-to-indentation)
2482 by Andreas Röhler
`py--go-down-when-found-upward' again
1116
    (while
1117
	(and (py-down-statement)
1118
	     (or (< thisindent (current-indentation))
1119
		 (and (eq thisindent (current-indentation))
1120
		      (or (eq regexp 'py-minor-block-re)
1121
			  (eq regexp 'py-block-re)
1122
			  (eq regexp 'py-if-block-re))
1123
		      (looking-at py-clause-re)))
1124
	     (py-forward-statement)(setq last (point))))
1125
    (and last (goto-char last))))
2387 by Andreas Röhler
Some cleanup
1126
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1127
(defun py--end-of-paragraph (regexp)
1128
  (let* ((regexp (if (symbolp regexp) (symbol-value regexp)
2621 by Andreas Röhler
Navigation - major rewrite
1129
		       regexp)))
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1130
    (while (and (not (eobp)) (re-search-forward regexp nil 'move 1)(nth 8 (parse-partial-sexp (point-min) (point)))))))
2621 by Andreas Röhler
Navigation - major rewrite
1131
2645 by Andreas Röhler
py--beginning-... forms, `py--end-base' fixed
1132
1133
(defun py--end-base (regexp &optional orig decorator bol indent done)
2621 by Andreas Röhler
Navigation - major rewrite
1134
  "Used internal by functions going to the end forms.
1135
1136
Must find start first "
1605 by Andreas Roehler
py-end-base, py-look-downward-for-beginning, py-guess-indent-offset fixed
1137
  (unless (eobp)
2621 by Andreas Röhler
Navigation - major rewrite
1138
    (if (eq regexp 'py-paragraph-re)
1139
	(py--end-of-paragraph regexp)
2645 by Andreas Röhler
py--beginning-... forms, `py--end-base' fixed
1140
      (let* ((pps (parse-partial-sexp (point-min) (point)))
1141
	     ;; (repeat (or (and repeat (1+ repeat)) 0))
2621 by Andreas Röhler
Navigation - major rewrite
1142
	     (orig (or orig (point)))
1143
	     (regexp (or regexp (symbol-value 'py-extended-block-or-clause-re)))
1144
	     (thisregexp (if (symbolp regexp)
1145
			     (cond ((eq regexp 'py-clause-re)
1146
				    (symbol-value 'py-extended-block-or-clause-re))
1147
				   (t (symbol-value regexp)))
1148
			   regexp))
2645 by Andreas Röhler
py--beginning-... forms, `py--end-base' fixed
1149
	     (indent (or indent (if (py--beginning-of-statement-p)
1150
				    (current-indentation)
1151
				  (save-excursion (py-backward-statement) (current-indentation)))))
1152
2621 by Andreas Röhler
Navigation - major rewrite
1153
	     ;; start of form maybe inside
1154
	     (this
2645 by Andreas Röhler
py--beginning-... forms, `py--end-base' fixed
1155
	      (if (and (looking-at thisregexp)(not (or (nth 1 pps) (nth 8 pps))))
2621 by Andreas Röhler
Navigation - major rewrite
1156
		  (point)
2645 by Andreas Röhler
py--beginning-... forms, `py--end-base' fixed
1157
		(py--go-to-keyword thisregexp indent)))
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1158
	     erg)
2645 by Andreas Röhler
py--beginning-... forms, `py--end-base' fixed
1159
	(cond
1160
	 (this (setq erg (py--go-down-when-found-upward regexp)))
1161
	 (t (goto-char orig)))
1162
	(if (< orig (point))
1163
	    (and erg bol (setq erg (py--beginning-of-line-form erg)))
1164
	  (setq erg nil)
1165
	  (unless (eq done orig)
1166
	    (when
1167
		(py-forward-statement)
1168
	      (py--end-base regexp (point) decorator bol indent (point)))))
1169
	erg))))
950 by Andreas Roehler
py-current-defun lp:789265 fixed
1170
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1171
(defun py--look-downward-for-beginning (regexp)
950 by Andreas Roehler
py-current-defun lp:789265 fixed
1172
  "When above any beginning of FORM, search downward. "
1605 by Andreas Roehler
py-end-base, py-look-downward-for-beginning, py-guess-indent-offset fixed
1173
  (let* ((orig (point))
1174
         (erg orig)
1175
         (last orig)
1176
         pps)
2387 by Andreas Röhler
Some cleanup
1177
    (while (and (setq last (point)) (not (eobp)) (re-search-forward regexp nil t 1)(setq erg (match-beginning 0)) (setq pps (parse-partial-sexp (point-min) (point)))
1605 by Andreas Roehler
py-end-base, py-look-downward-for-beginning, py-guess-indent-offset fixed
1178
                (or (nth 8 pps) (nth 1 pps))))
1179
    (cond ((not (or (nth 8 pps) (nth 1 pps) (or (looking-at comment-start))))
1180
           (when (ignore-errors (< orig erg))
1181
             erg)))))
950 by Andreas Roehler
py-current-defun lp:789265 fixed
1182
1596 by Andreas Roehler
`py-guess-indent-offset', `py-end-base' fixed
1183
(defun py-look-downward-for-clause (&optional ind orig regexp)
1587 by Andreas Roehler
`py-end-of-block' fixed
1184
  "If beginning of other clause exists downward in current block.
1185
1186
If succesful return position. "
1187
  (interactive)
1188
  (unless (eobp)
1189
    (let ((ind (or ind
1593 by Andreas Roehler
New complementary upward commands; fixes lp:1044122
1190
                   (save-excursion
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1191
                     (py-backward-statement)
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1192
                     (if (py--statement-opens-block-p)
1587 by Andreas Roehler
`py-end-of-block' fixed
1193
                         (current-indentation)
1594 by Andreas Roehler
Upward commands; lp:1044122
1194
                       (- (current-indentation) py-indent-offset)))))
1587 by Andreas Roehler
`py-end-of-block' fixed
1195
          (orig (or orig (point)))
1596 by Andreas Roehler
`py-guess-indent-offset', `py-end-base' fixed
1196
          (regexp (or regexp py-extended-block-or-clause-re))
1587 by Andreas Roehler
`py-end-of-block' fixed
1197
          erg last)
1198
      (end-of-line)
1596 by Andreas Roehler
`py-guess-indent-offset', `py-end-base' fixed
1199
      (when (re-search-forward regexp nil t 1)
2387 by Andreas Röhler
Some cleanup
1200
        (when (nth 8 (parse-partial-sexp (point-min) (point)))
1596 by Andreas Roehler
`py-guess-indent-offset', `py-end-base' fixed
1201
          (while (and (re-search-forward regexp nil t 1)
2387 by Andreas Röhler
Some cleanup
1202
                      (nth 8 (parse-partial-sexp (point-min) (point))))))
1596 by Andreas Roehler
`py-guess-indent-offset', `py-end-base' fixed
1203
        (setq last (point))
1204
        (back-to-indentation)
1205
        (unless (and (looking-at py-clause-re)
2387 by Andreas Röhler
Some cleanup
1206
                     (not (nth 8 (parse-partial-sexp (point-min) (point)))) (eq (current-indentation) ind))
1596 by Andreas Roehler
`py-guess-indent-offset', `py-end-base' fixed
1207
          (progn (setq ind (current-indentation))
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1208
                 (while (and (py-forward-statement-bol)(not (looking-at py-clause-re))(<= ind (current-indentation)))))
1596 by Andreas Roehler
`py-guess-indent-offset', `py-end-base' fixed
1209
          (if (and (looking-at py-clause-re)
2387 by Andreas Röhler
Some cleanup
1210
                   (not (nth 8 (parse-partial-sexp (point-min) (point))))
1596 by Andreas Roehler
`py-guess-indent-offset', `py-end-base' fixed
1211
                   (< orig (point)))
1212
              (setq erg (point))
1213
            (goto-char orig))))
2410 by Andreas Röhler
Reduce compiler warnings
1214
      (when (called-interactively-p 'any) (message "%s" erg))
1587 by Andreas Roehler
`py-end-of-block' fixed
1215
      erg)))
1216
950 by Andreas Roehler
py-current-defun lp:789265 fixed
1217
(defun py-current-defun (&optional iact)
1080 by Andreas Roehler
indent-match-import-pkg fixed, lp:852500
1218
  "Go to the outermost method or class definition in current scope.
880 by Andreas Roehler
py-current-defun fixed, test added
1219
1220
Python value for `add-log-current-defun-function'.
1221
This tells add-log.el how to find the current function/method/variable.
1222
Returns name of class or methods definition, if found, nil otherwise.
1223
881 by Andreas Roehler
use py-current-defun-show
1224
See customizable variables `py-current-defun-show' and `py-current-defun-delay'."
880 by Andreas Roehler
py-current-defun fixed, test added
1225
  (interactive "p")
1226
  (save-restriction
1227
    (widen)
1228
    (save-excursion
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1229
      (let ((erg (when (py-backward-def-or-class)
950 by Andreas Roehler
py-current-defun lp:789265 fixed
1230
                   (forward-word 1)
1231
                   (skip-chars-forward " \t")
1232
                   (prin1-to-string (symbol-at-point)))))
2537 by Andreas Röhler
`py-current-defun' highlight name if `py-current-defun-show' is t
1233
        (when (and erg py-current-defun-show)
1234
	  (push-mark (point) t t) (skip-chars-forward "^ (")
1235
	  (exchange-point-and-mark)
1236
	  (sit-for py-current-defun-delay))
950 by Andreas Roehler
py-current-defun lp:789265 fixed
1237
        (when iact (message (prin1-to-string erg)))
880 by Andreas Roehler
py-current-defun fixed, test added
1238
        erg))))
362 by Andreas Roehler
towards a Python-IDE
1239
1240
(defun py-sort-imports ()
1241
  "Sort multiline imports.
1223 by Andreas Roehler
several docstrings normalised
1242
362 by Andreas Roehler
towards a Python-IDE
1243
Put point inside the parentheses of a multiline import and hit
1244
\\[py-sort-imports] to sort the imports lexicographically"
1245
  (interactive)
1246
  (save-excursion
2578 by Andreas Röhler
lp:1545013, scan-error on py-sort-imports
1247
    (let ((open-paren (ignore-errors (save-excursion (progn (up-list -1) (point)))))
1248
          (close-paren (ignore-errors (save-excursion (progn (up-list 1) (point)))))
362 by Andreas Roehler
towards a Python-IDE
1249
          sorted-imports)
2578 by Andreas Röhler
lp:1545013, scan-error on py-sort-imports
1250
      (when (and open-paren close-paren)
1251
	(goto-char (1+ open-paren))
1252
	(skip-chars-forward " \n\t")
1253
	(setq sorted-imports
1254
	      (sort
1255
	       (delete-dups
1256
		(split-string (buffer-substring
1257
			       (point)
1258
			       (save-excursion (goto-char (1- close-paren))
1259
					       (skip-chars-backward " \n\t")
1260
					       (point)))
1261
			      ", *\\(\n *\\)?"))
1262
	       ;; XXX Should this sort case insensitively?
1263
	       'string-lessp))
1264
	;; Remove empty strings.
1265
	(delete-region open-paren close-paren)
1266
	(goto-char open-paren)
1267
	(insert "(\n")
1268
	(insert (py--join-words-wrapping (remove "" sorted-imports) "," "    " 78))
1269
	(insert ")")))))
362 by Andreas Roehler
towards a Python-IDE
1270
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1271
(defun py--in-literal (&optional lim)
362 by Andreas Roehler
towards a Python-IDE
1272
  "Return non-nil if point is in a Python literal (a comment or string).
1273
Optional argument LIM indicates the beginning of the containing form,
1274
i.e. the limit on how far back to scan."
999 by Andreas Roehler
indent-after-assigment, thanks to Glenn Linderman reporting case 6
1275
  (let* ((lim (or lim (point-min)))
2387 by Andreas Röhler
Some cleanup
1276
         (state (parse-partial-sexp (point-min) (point))))
362 by Andreas Roehler
towards a Python-IDE
1277
    (cond
1278
     ((nth 3 state) 'string)
594 by Andreas Roehler
py-count-lines fixed
1279
     ((nth 4 state) 'comment))))
362 by Andreas Roehler
towards a Python-IDE
1280
1281
(defconst py-help-address "python-mode@python.org"
1353 by Andreas Roehler
make sure `py-clause-lookup-keyword' ignores indent of original line
1282
  "List dealing with usage and developing python-mode.
1240 by Andreas Roehler
py-help-address docu updated
1283
1284
Also accepts submission of bug reports, whilst a ticket at
1285
http://launchpad.net/python-mode
1286
is preferable for that. ")
362 by Andreas Roehler
towards a Python-IDE
1287
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1288
;;  Utilities
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1289
(defun py--point (position)
1604 by Andreas Roehler
inconvenient-window-splitting-behavior, lp:1018996, again
1290
  "Returns the value of point at certain commonly referenced POSITIONs.
1291
POSITION can be one of the following symbols:
1292
1293
  bol -- beginning of line
1294
  eol -- end of line
1295
  bod -- beginning of def or class
1296
  eod -- end of def or class
1297
  bob -- beginning of buffer
1298
  eob -- end of buffer
1299
  boi -- back to indentation
1300
  bos -- beginning of statement
1301
1302
This function does not modify point or mark."
1303
  (let (erg)
1304
    (save-excursion
1305
      (setq erg
1306
            (progn
1307
              (cond
1308
               ((eq position 'bol) (beginning-of-line))
1309
               ((eq position 'eol) (end-of-line))
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1310
               ((eq position 'bod) (py-backward-def-or-class))
1311
               ((eq position 'eod) (py-forward-def-or-class))
1604 by Andreas Roehler
inconvenient-window-splitting-behavior, lp:1018996, again
1312
               ;; Kind of funny, I know, but useful for py-up-exception.
1313
               ((eq position 'bob) (goto-char (point-min)))
1314
               ((eq position 'eob) (goto-char (point-max)))
1315
               ((eq position 'boi) (back-to-indentation))
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1316
               ((eq position 'bos) (py-backward-statement))
1604 by Andreas Roehler
inconvenient-window-splitting-behavior, lp:1018996, again
1317
               (t (error "Unknown buffer position requested: %s" position))) (point))))
1318
    erg))
1319
1653 by Andreas Roehler
internal used `python-font-lock-keywords' renamed `py-font-lock-keywords'
1320
(defun py-install-search-local ()
1321
  (interactive)
1322
  (let ((erg (split-string (shell-command-to-string (concat "find " default-directory " -maxdepth 9 -type f -name \"*python\"")))))))
1323
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1324
(defun py-install-local-shells (&optional local)
1653 by Andreas Roehler
internal used `python-font-lock-keywords' renamed `py-font-lock-keywords'
1325
  "Builds Python-shell commands from executable found in LOCAL.
1326
1327
If LOCAL is empty, shell-command `find' searches beneath current directory.
1328
Eval resulting buffer to install it, see customizable `py-extensions'. "
1329
  (interactive)
1330
  (let* ((local-dir (if local
1331
                        (expand-file-name local)
1332
                      (read-from-minibuffer "Virtualenv directory: " default-directory)))
1333
         (path-separator (if (string-match "/" local-dir)
1334
                             "/"
1335
                           "\\" t))
1336
         (shells (split-string (shell-command-to-string (concat "find " local-dir " -maxdepth 9 -type f -executable -name \"*python\""))))
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1337
         prefix end orig curexe aktpath)
1653 by Andreas Roehler
internal used `python-font-lock-keywords' renamed `py-font-lock-keywords'
1338
    (set-buffer (get-buffer-create py-extensions))
1339
    (erase-buffer)
1340
    (dolist (elt shells)
1341
      (setq prefix "")
1342
      (setq curexe (substring elt (1+ (string-match "/[^/]+$" elt))))
1343
      (setq aktpath (substring elt 0 (1+ (string-match "/[^/]+$" elt))))
1344
      (dolist (prf (split-string aktpath (regexp-quote path-separator)))
1345
        (unless (string= "" prf)
1346
          (setq prefix (concat prefix (substring prf 0 1)))))
1347
      (setq orig (point))
1348
      (insert py-shell-template)
1349
      (setq end (point))
1350
      (goto-char orig)
1351
      (when (re-search-forward "\\<NAME\\>" end t 1)
1352
        (replace-match (concat prefix "-" (substring elt (1+ (save-match-data (string-match "/[^/]+$" elt)))))t))
1353
      (goto-char orig)
1354
      (while (search-forward "DOCNAME" end t 1)
1355
        (replace-match (if (string= "ipython" curexe)
1356
                           "IPython"
1357
                         (capitalize curexe)) t))
1358
      (goto-char orig)
1359
      (when (search-forward "FULLNAME" end t 1)
1360
        (replace-match elt t))
1361
      (goto-char (point-max)))
1362
    (emacs-lisp-mode)
1363
    (if (file-readable-p (concat py-install-directory "/" py-extensions))
1364
        (find-file (concat py-install-directory "/" py-extensions)))))
1365
1721 by Andreas Röhler
fill-paragraph in docstring, lp:1161232
1366
(defun py-end-of-string (&optional beginning-of-string-position)
2176 by Andreas Röhler
`py--fast-send-string-intern' fixed
1367
  "Go to end of string at point if any, if successful return position. "
1723 by Andreas Röhler
`which-func-mode' misses class, lp:1160022, fixed
1368
  (interactive)
2208 by Andreas Röhler
`py-indent-line' lp:1382799
1369
  (let ((orig (point))
2179 by Andreas Röhler
Regression of lp:1169687 fixed
1370
	(beginning-of-string-position (or beginning-of-string-position (and (nth 3 (parse-partial-sexp 1 (point)))(nth 8 (parse-partial-sexp 1 (point))))
1842 by Andreas Röhler
which-function hangs w unbalanced quote marks, lp:1235375, fixed
1371
                                          (and (looking-at "\"\"\"\\|'''\\|\"\\|\'")(match-beginning 0))))
1372
        erg)
1373
    (if beginning-of-string-position
1374
        (progn
1375
          (goto-char beginning-of-string-position)
2176 by Andreas Röhler
`py--fast-send-string-intern' fixed
1376
	  (when
1377
	      ;; work around parse-partial-sexp error
1378
	      (and (nth 3 (parse-partial-sexp 1 (point)))(nth 8 (parse-partial-sexp 1 (point))))
1379
	    (goto-char (nth 3 (parse-partial-sexp 1 (point)))))
2179 by Andreas Röhler
Regression of lp:1169687 fixed
1380
          (if (ignore-errors (setq erg (scan-sexps (point) 1)))
1381
			      (goto-char erg)
1382
	    (goto-char orig)))
2176 by Andreas Röhler
`py--fast-send-string-intern' fixed
1383
1842 by Andreas Röhler
which-function hangs w unbalanced quote marks, lp:1235375, fixed
1384
      (error (concat "py-end-of-string: don't see end-of-string at " (buffer-name (current-buffer)) "at pos " (point))))
2410 by Andreas Röhler
Reduce compiler warnings
1385
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
1842 by Andreas Röhler
which-function hangs w unbalanced quote marks, lp:1235375, fixed
1386
    erg))
1387
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1388
;;  (goto-char (match-end 0))
1389
;;  (search-forward (match-string-no-properties 0))))
1699 by Andreas Roehler
More docstring filling woes, lp:1102296
1390
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1391
(defun py--until-found (search-string liste)
1707 by Andreas Roehler
`py-find-definition' jumps to beginning of buffer, lp:1120076
1392
  "Search liste for search-string until found. "
1393
  (let ((liste liste) element)
1394
    (while liste
1395
      (if (member search-string (car liste))
1396
          (setq element (car liste) liste nil))
1397
      (setq liste (cdr liste)))
1398
    (when element
1399
      (while (and element (not (numberp element)))
1400
        (if (member search-string (car element))
1401
            (setq element (car element))
1402
          (setq element (cdr element))))
1403
      element)))
1699 by Andreas Roehler
More docstring filling woes, lp:1102296
1404
2221 by Andreas Röhler
`py--fill-docstring-base' again
1405
(defun py--delay-process-dependent (process)
1406
  "Call a `py-ipython-send-delay' or `py-python-send-delay' according to process"
1407
  (if (string-match "ipython" (prin1-to-string process))
2253 by Andreas Röhler
`py--delay-process-dependent' fixed
1408
      (sit-for py-ipython-send-delay t)
1409
    (sit-for py-python-send-delay t)))
2221 by Andreas Röhler
`py--fill-docstring-base' again
1410
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1411
(defun py--send-string-no-output (strg &optional process msg)
2028 by Andreas Röhler
Readability
1412
  "Send STRING to PROCESS and inhibit output display.
1722 by Andreas Röhler
Avoid name-conflicts with python.el as much as possible
1413
When MSG is non-nil messages the first line of STRING.  Return
1414
the output."
1917 by Andreas Röhler
completion doesn't work, lp:1284141
1415
  (let* (output
1722 by Andreas Röhler
Avoid name-conflicts with python.el as much as possible
1416
         (process (or process (get-buffer-process (py-shell))))
1917 by Andreas Röhler
completion doesn't work, lp:1284141
1417
         (comint-preoutput-filter-functions
1418
          (append comint-preoutput-filter-functions
1920 by Andreas Röhler
ansi-color-filter re-installed
1419
                  '(ansi-color-filter-apply
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1420
                    (lambda (strg)
1421
                      (setq output strg)
1917 by Andreas Röhler
completion doesn't work, lp:1284141
1422
                      "")))))
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1423
    (py-send-string strg process)
2225 by Andreas Röhler
`py--fetch-comint-result', `py--postprocess-comint' rewritten
1424
    (sit-for 0.1 t)
2221 by Andreas Röhler
`py--fill-docstring-base' again
1425
    ;; (py--delay-process-dependent process)
2021 by Andreas Röhler
py--send-string-...
1426
    (when (and output (not (string= "" output)))
2312 by Andreas Röhler
TAB-completion in IPyton, lp:1425268
1427
	    (py--string-strip
1428
	     (format "[ \n]*%s[ \n]*" py-fast-filter-re)))))
1722 by Andreas Röhler
Avoid name-conflicts with python.el as much as possible
1429
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1430
(defun py--send-string-return-output (strg &optional process msg)
1722 by Andreas Röhler
Avoid name-conflicts with python.el as much as possible
1431
  "Send STRING to PROCESS and return output.
1432
1433
When MSG is non-nil messages the first line of STRING.  Return
1434
the output."
2623 by Andreas Röhler
Fixes WRT lexical scope
1435
  (let ((process (or process (get-buffer-process (py-shell)))))
2524 by Andreas Röhler
`py-indent-line' fixed
1436
    (with-current-buffer (process-buffer process)
2623 by Andreas Röhler
Fixes WRT lexical scope
1437
      (let* (erg
1438
	     (comint-preoutput-filter-functions
1439
	      (append comint-preoutput-filter-functions
1440
		      '(ansi-color-filter-apply
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1441
			(lambda (strg)
1442
			  strg)))))
1443
	(py-send-string strg process)
2524 by Andreas Röhler
`py-indent-line' fixed
1444
	(accept-process-output process 5)
1445
	(sit-for 0.1 t)
1446
	(when (and erg (not (string= "" erg)))
1447
	  (setq erg
1448
		(replace-regexp-in-string
1449
		 (format "[ \n]*%s[ \n]*" py-fast-filter-re)
1450
		 "" erg)))
1451
	;; (sit-for 0.1 t)
1452
	erg))))
1722 by Andreas Röhler
Avoid name-conflicts with python.el as much as possible
1453
2545 by Andreas Röhler
#11, which-function-mode sluggish
1454
(defun py-which-def-or-class (&optional orig)
1723 by Andreas Röhler
`which-func-mode' misses class, lp:1160022, fixed
1455
  "Returns concatenated `def' and `class' names in hierarchical order, if cursor is inside.
1456
1457
Returns \"???\" otherwise
1458
Used by variable `which-func-functions' "
1459
  (interactive)
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1460
  (let* ((orig (or orig (point)))
2545 by Andreas Röhler
#11, which-function-mode sluggish
1461
	 (backindent 99999)
2548 by Andreas Röhler
py-which-def-or-class fixed
1462
	 (re (concat py-def-or-class-re "\\([[:alnum:]_]+\\)"))
2545 by Andreas Röhler
#11, which-function-mode sluggish
1463
         erg forward indent backward limit)
1464
    (if
2548 by Andreas Röhler
py-which-def-or-class fixed
1465
	(and (looking-at re)
2545 by Andreas Röhler
#11, which-function-mode sluggish
1466
	     (not (nth 8 (parse-partial-sexp (point-min) (point)))))
1467
	(progn
1468
	  (setq erg (list (match-string-no-properties 2)))
1469
	  (setq backindent (current-indentation)))
1470
      ;; maybe inside a definition's symbol
1471
      (or (eolp) (and (looking-at "[[:alnum:]]")(forward-word 1))))
1472
    (if
1473
	(and (not (and erg (eq 0 (current-indentation))))
1474
	     (setq limit (py-backward-top-level))
2548 by Andreas Röhler
py-which-def-or-class fixed
1475
	     (looking-at re))
2545 by Andreas Röhler
#11, which-function-mode sluggish
1476
	(progn
2639 by Andreas Röhler
lexical-binding: don't use add-to-list
1477
	  (push (match-string-no-properties 2)  erg)
2545 by Andreas Röhler
#11, which-function-mode sluggish
1478
	  (setq indent (current-indentation)))
1479
      (goto-char orig)
1480
      (while (and
1481
	      (re-search-backward py-def-or-class-re limit t 1)
1482
	      (< (current-indentation) backindent)
1483
	      (setq backindent (current-indentation))
1484
	      (setq backward (point))
1485
	      (or (< 0 (current-indentation))
1486
		  (nth 8 (parse-partial-sexp (point-min) (point))))))
1487
      (when (and backward
1488
		 (goto-char backward)
2548 by Andreas Röhler
py-which-def-or-class fixed
1489
		 (looking-at re))
2639 by Andreas Röhler
lexical-binding: don't use add-to-list
1490
	(push (match-string-no-properties 2)  erg)
2545 by Andreas Röhler
#11, which-function-mode sluggish
1491
	(setq indent (current-indentation))))
1492
    ;; (goto-char orig))
1493
    (if erg
1494
	(progn
1495
	  (end-of-line)
1496
	  (while (and (re-search-forward py-def-or-class-re nil t 1)
1497
		      (<= (point) orig)
1498
		      (< indent (current-indentation))
1499
		      (or
1500
		       (nth 8 (parse-partial-sexp (point-min) (point)))
1501
		       (setq forward (point)))))
1502
	  (if forward
1503
	      (progn
1504
		(goto-char forward)
1505
		(save-excursion
1506
		  (back-to-indentation)
2548 by Andreas Röhler
py-which-def-or-class fixed
1507
		  (and (looking-at re)
2545 by Andreas Röhler
#11, which-function-mode sluggish
1508
		       (setq erg (list (car erg) (match-string-no-properties 2)))
2548 by Andreas Röhler
py-which-def-or-class fixed
1509
		       ;; (< (py-forward-def-or-class) orig)
2545 by Andreas Röhler
#11, which-function-mode sluggish
1510
		       ;; if match was beyond definition, nil
2548 by Andreas Röhler
py-which-def-or-class fixed
1511
		       ;; (setq erg nil)
2621 by Andreas Röhler
Navigation - major rewrite
1512
)))
2545 by Andreas Röhler
#11, which-function-mode sluggish
1513
	    (goto-char orig))))
1514
    (if erg
1515
	(if (< 1 (length erg))
1516
	    (setq erg (mapconcat 'identity erg "."))
1517
	  (setq erg (car erg)))
1518
      (setq erg "???"))
1519
    (goto-char orig)
1520
    (when (called-interactively-p 'any) (message "%s" erg))
1521
    erg))
1723 by Andreas Röhler
`which-func-mode' misses class, lp:1160022, fixed
1522
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1523
(defun py--beginning-of-form-intern (final-re &optional inter-re iact indent orig lc decorator)
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1524
  "Go to beginning of FORM.
1525
1526
With INDENT, go to beginning one level above.
1527
Whit IACT, print result in message buffer.
1528
2385 by Andreas Röhler
Docu updated
1529
Returns beginning of FORM if successful, nil otherwise"
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1530
  (interactive "P")
2621 by Andreas Röhler
Navigation - major rewrite
1531
  (let ((regexp
1532
	 ;; (if inter-re
1533
	 ;; (concat (symbol-value inter-re) "\\|" (symbol-value final-re))
1534
		  (symbol-value final-re))
1535
		  ;; ))
1536
	erg)
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1537
    (unless (bobp)
1538
      (let* ((orig (or orig (point)))
1539
             (indent (or indent (progn
1540
                                  (back-to-indentation)
2011 by Andreas Röhler
Some commands not so useful made common functions, like
1541
                                  (or (py--beginning-of-statement-p)
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1542
                                      (py-backward-statement))
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1543
                                  (current-indentation)))))
2621 by Andreas Röhler
Navigation - major rewrite
1544
        (setq erg (cond ((and (< (point) orig) (looking-at regexp))
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1545
                         (point))
1546
                        ((and (eq 0 (current-column)) (numberp indent) (< 0 indent))
1547
                         (when (< 0 (abs (skip-chars-backward " \t\r\n\f")))
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1548
                           (py-backward-statement)
2621 by Andreas Röhler
Navigation - major rewrite
1549
                           (unless (looking-at regexp)
1550
                             (cdr (py--go-to-keyword regexp (current-indentation))))))
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1551
                        ((numberp indent)
2621 by Andreas Röhler
Navigation - major rewrite
1552
			 (or (cdr (py--go-to-keyword regexp indent))
1553
			     (progn
1554
			       (goto-char orig)
1555
			       (cdr (py--go-to-keyword regexp indent)))))
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1556
                        (t (ignore-errors
2621 by Andreas Röhler
Navigation - major rewrite
1557
                             (cdr (py--go-to-keyword regexp
1558
						     (- (progn (if (py--beginning-of-statement-p) (current-indentation) (save-excursion (py-backward-statement) (current-indentation)))) py-indent-offset)))))))
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1559
        (when lc (beginning-of-line) (setq erg (point)))))
1560
    (when (and py-verbose-p iact) (message "%s" erg))
1561
    erg))
1562
2621 by Andreas Röhler
Navigation - major rewrite
1563
(defun py--backward-prepare (&optional indent final-re inter-re iact decorator lc)
1564
  (let* ((orig (point))
1565
	 (indent
1566
	  (or indent
1567
	      (progn
1568
		(or (py--beginning-of-statement-p)
1569
		    (py-backward-statement))
1570
		;; maybe after last statement?
1571
		(when (save-excursion
1572
			(< (py-forward-statement) orig))
1573
		  (goto-char orig))
1574
		(cond ((looking-back "^[ \t]*" (line-beginning-position))
1575
		       (current-indentation))
1576
		      (t (progn (back-to-indentation)
1577
				(cond ((eq 0 (current-indentation))
1578
				       (current-indentation))
1579
				      ((and inter-re (looking-at (symbol-value inter-re)))
1580
				       (current-indentation)))))))))
1581
	 erg)
1582
    ;; def and class need lesser value
1583
    (when (and
1584
	   (member final-re (list 'py-def-or-class-re 'py-class-re 'py-def-re))
1585
	   (<= 0 (- indent (if py-smart-indentation (py-guess-indent-offset) py-indent-offset))))
1586
      (setq indent (- indent (if py-smart-indentation (py-guess-indent-offset) py-indent-offset))))
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1587
    (if (and (< (point) orig) (looking-at (symbol-value final-re)))
1588
        (progn
1589
          (and lc (beginning-of-line))
1590
          (setq erg (point))
1591
          (when (and py-verbose-p iact) (message "%s" erg))
1592
          erg)
2653 by Andreas Röhler
Avoid shadowing and other minor fixes
1593
      (py--beginning-of-form-intern final-re inter-re iact indent orig lc decorator))))
1753 by Andreas Röhler
`py-beginning-of-block-or-clause' fixed
1594
1914 by Andreas Röhler
extended-executes-tests reconsidered
1595
(defun py--fetch-first-python-buffer ()
1596
  "Returns first (I)Python-buffer found in `buffer-list'"
1597
  (let ((buli (buffer-list))
1598
        erg)
1599
    (while (and buli (not erg))
1600
      (if (string-match "Python" (prin1-to-string (car buli)))
1601
          (setq erg (car buli))
1602
        (setq buli (cdr buli))))
1603
    erg))
1604
2211 by Andreas Röhler
`py-split-windows-on-execute-p' renamed `py-split-window-on-execute-p'
1605
(defun py-unload-python-el ()
1606
  "Unloads python-mode delivered by shipped python.el
1607
1608
Removes python-skeleton forms from abbrevs.
1609
These would interfere when inserting forms heading a block"
1610
  (interactive)
2213 by Andreas Röhler
`py--complete-base', `py-choose-shell' fixed
1611
  (let (done)
1612
    (when (featurep 'python) (unload-feature 'python t))
1613
    (when (file-readable-p abbrev-file-name)
1614
      (find-file abbrev-file-name)
1615
      (goto-char (point-min))
1616
      (while (re-search-forward "^.+python-skeleton.+$" nil t 1)
1617
	(setq done t)
1618
	(delete-region (match-beginning 0) (1+ (match-end 0))))
1619
      (when done (write-file abbrev-file-name)
1620
	    ;; now reload
1621
	    (read-abbrev-file abbrev-file-name))
1622
      (kill-buffer (file-name-nondirectory abbrev-file-name)))))
2211 by Andreas Röhler
`py-split-windows-on-execute-p' renamed `py-split-window-on-execute-p'
1623
2280 by Andreas Röhler
py-shell reuse existing unless called from inside or explicitely
1624
(defmacro py--kill-buffer-unconditional (buffer)
1625
  "Kill buffer unconditional, kill buffer-process if existing. "
1626
  `(let ((proc (get-buffer-process ,buffer))
1627
	 kill-buffer-query-functions)
1628
     (ignore-errors
1629
       (and proc (kill-process proc))
1630
       (set-buffer ,buffer)
1631
       (set-buffer-modified-p 'nil)
1632
       (kill-buffer (current-buffer)))))
1633
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1634
(defun py--skip-to-semicolon-backward (&optional limit)
1635
  "Fetch the beginning of statement after a semicolon.
1636
1637
Returns position reached if point was moved. "
2500 by Andreas Röhler
`py--skip-to-semicolon-backward' avoid assignment to free variable `done'
1638
  (prog1
1639
      (< 0 (abs (skip-chars-backward "^;" (or limit (line-beginning-position)))))
1640
    (skip-chars-forward " \t" (line-end-position))))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1641
1642
(defun py--end-of-comment-intern (pos)
1643
  (while (and (not (eobp))
1644
              (forward-comment 99999)))
1645
  ;; forward-comment fails sometimes
1646
  (and (eq pos (point)) (prog1 (forward-line 1) (back-to-indentation))
2626 by Andreas Röhler
Some more minor fixes WRT lexical scope
1647
       (while (member (char-after) (list comment-start 10))(forward-line 1)(back-to-indentation))))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1648
2477 by Andreas Röhler
`py--skip-to-comment-or-semicolon' hand over done
1649
(defun py--skip-to-comment-or-semicolon (done)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1650
  "Returns position if comment or semicolon found. "
1651
  (let ((orig (point)))
1652
    (cond ((and done (< 0 (abs (skip-chars-forward "^#;" (line-end-position))))
1653
		(member (char-after) (list ?# ?\;)))
1654
	   (when (eq ?\; (char-after))
1655
	     (skip-chars-forward ";" (line-end-position))))
1656
	  ((and (< 0 (abs (skip-chars-forward "^#;" (line-end-position))))
1657
		(member (char-after) (list ?# ?\;)))
1658
	   (when (eq ?\; (char-after))
1659
	     (skip-chars-forward ";" (line-end-position))))
1660
	  ((not done)
1661
	   (end-of-line)))
1662
    (skip-chars-backward " \t" (line-beginning-position))
1663
    (and (< orig (point))(setq done t)
1664
	 done)))
1665
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1666
(defun py-backward-top-level ()
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1667
  "Go up to beginning of statments until level of indentation is null.
1668
1669
Returns position if successful, nil otherwise "
1670
  (interactive)
1671
  (let (erg)
1672
    (unless (bobp)
2545 by Andreas Röhler
#11, which-function-mode sluggish
1673
      (while (and (not (bobp))
1674
		  (setq erg (re-search-backward "^[[:alpha:]_'\"]" nil t 1))
1675
		  (nth 8 (parse-partial-sexp (point-min) (point)))
1676
		  (setq erg nil)))
2410 by Andreas Röhler
Reduce compiler warnings
1677
      (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1678
      erg)))
1679
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1680
(defun py-forward-top-level ()
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1681
  "Go to end of top-level form at point.
1682
2385 by Andreas Röhler
Docu updated
1683
Returns position if successful, nil otherwise"
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1684
  (interactive)
1685
  (let ((orig (point))
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1686
	erg)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1687
    (unless (eobp)
1688
      (unless (py--beginning-of-statement-p)
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1689
	(py-backward-statement))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1690
      (unless (eq 0 (current-column))
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1691
	(py-backward-top-level))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1692
      (cond ((looking-at py-def-re)
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1693
	     (setq erg (py-forward-def)))
1694
	    ((looking-at py-class-re)
1695
	     (setq erg (py-forward-class)))
1696
	    ((looking-at py-block-re)
1697
	     (setq erg (py-forward-block)))
1698
	    (t (setq erg (py-forward-statement))))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1699
      (unless (< orig (point))
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1700
	(while (and (not (eobp)) (py-down-statement)(< 0 (current-indentation))))
1701
	(if (looking-at py-block-re)
1702
	    (setq erg (py-forward-block))
1703
	  (setq erg (py-forward-statement))))
2410 by Andreas Röhler
Reduce compiler warnings
1704
      (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1705
      erg)))
1706
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1707
(defun py-down-top-level ()
1708
  "Go to beginning of next top-level form downward.
1709
1710
Returns position if successful, nil otherwise"
1711
  (interactive)
1712
  (let ((orig (point))
1713
        erg)
1714
    (while (and (not (eobp))
1715
		(progn (end-of-line)
2545 by Andreas Röhler
#11, which-function-mode sluggish
1716
		       (re-search-forward "^[[:alpha:]_'\"]" nil 'move 1))
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1717
		(nth 8 (parse-partial-sexp (point-min) (point)))))
1718
    (when (and (not (eobp)) (< orig (point)))
2482 by Andreas Röhler
`py--go-down-when-found-upward' again
1719
      (goto-char (match-beginning 0))
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1720
	(setq erg (point)))
2410 by Andreas Röhler
Reduce compiler warnings
1721
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
2404 by Andreas Röhler
`py-match-paren', tests and fixes
1722
    erg))
1723
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1724
(defun py-forward-top-level-bol ()
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1725
  "Go to end of top-level form at point, stop at next beginning-of-line.
1726
2385 by Andreas Röhler
Docu updated
1727
Returns position successful, nil otherwise"
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1728
  (interactive)
1729
  (let (erg)
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1730
    (py-forward-top-level)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1731
    (unless (or (eobp) (bolp))
1732
      (forward-line 1)
1733
      (beginning-of-line)
1734
      (setq erg (point)))
2410 by Andreas Röhler
Reduce compiler warnings
1735
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1736
    erg))
1737
1738
(defun py-down (&optional indent)
1739
  "Go to beginning one level below of compound statement or definition at point.
1740
1741
If no statement or block below, but a delimited form --string or list-- go to its beginning. Repeated call from there will behave like down-list.
1742
2385 by Andreas Röhler
Docu updated
1743
Returns position if successful, nil otherwise"
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1744
  (interactive "P")
1745
  (let* ((orig (point))
1746
         erg
1747
         (indent (if
1748
                     (py--beginning-of-statement-p)
1749
                     (current-indentation)
1750
                   (progn
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1751
                     (py-backward-statement)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1752
                     (current-indentation))))
1753
         last)
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1754
    (while (and (setq last (point)) (py-forward-statement) (py-forward-statement) (py-backward-statement) (eq (current-indentation) indent)))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1755
    (if (< indent (current-indentation))
1756
        (setq erg (point))
1757
      (goto-char last))
1758
    (when (< (point) orig)
1759
      (goto-char orig))
1760
    (when (and (eq (point) orig)
1761
               (progn (forward-char 1)
1762
                      (skip-chars-forward "^\"'[({" (line-end-position))
1763
                      (member (char-after) (list ?\( ?\" ?\' ?\[ ?\{)))
1764
               (setq erg (point))))
1765
    (unless erg
1766
      (goto-char orig))
2410 by Andreas Röhler
Reduce compiler warnings
1767
    (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1768
    erg))
1769
2458 by Andreas Röhler
`py--beginning-of-line-form', hand over argument erg
1770
(defun py--beginning-of-line-form (erg)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1771
  "Internal use: Go to beginning of line following end of form. "
1772
  (when erg
1773
    (unless (eobp)
1774
      (forward-line 1)
1775
      (beginning-of-line)
1776
      (setq erg (point)))))
1777
2641 by Andreas Röhler
Avoid circular dependency
1778
(defun py--mark-base (form &optional mark-decorators)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1779
  "Returns boundaries of FORM, a cons.
1780
1781
If PY-MARK-DECORATORS, `def'- and `class'-forms include decorators
1782
If BOL is t, mark from beginning-of-line"
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1783
  (let* ((begform (intern-soft (concat "py-backward-" form)))
1784
         (endform (intern-soft (concat "py-forward-" form)))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1785
         (begcheckform (intern-soft (concat "py--beginning-of-" form "-p")))
1786
         (orig (point))
1787
         beg end erg)
1788
    (setq beg (if
1789
                  (setq beg (funcall begcheckform))
1790
                  beg
1791
                (funcall begform)))
2641 by Andreas Röhler
Avoid circular dependency
1792
    (and mark-decorators
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1793
         (and (setq erg (py-backward-decorator))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1794
              (setq beg erg)))
1795
    (push-mark)
1796
    (setq end (funcall endform))
1797
    (unless end (when (< beg (point))
1798
                  (setq end (point))))
1799
    (if (and beg end (<= beg orig) (<= orig end))
2387 by Andreas Röhler
Some cleanup
1800
	(cons beg end)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1801
      nil)))
1802
1803
(defun py--mark-base-bol (form &optional py-mark-decorators)
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1804
  (let* ((begform (intern-soft (concat "py-backward-" form "-bol")))
1805
         (endform (intern-soft (concat "py-forward-" form "-bol")))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1806
         (begcheckform (intern-soft (concat "py--beginning-of-" form "-bol-p")))
1807
         beg end erg)
1808
    (setq beg (if
1809
                  (setq beg (funcall begcheckform))
1810
                  beg
1811
                (funcall begform)))
1812
    (when py-mark-decorators
1813
      (save-excursion
2453 by Andreas Röhler
`py-backward-decorator-bol' aliased `py-backward-decorator'
1814
        (when (setq erg (py-backward-decorator))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1815
          (setq beg erg))))
1816
    (setq end (funcall endform))
1817
    (push-mark beg t t)
1818
    (unless end (when (< beg (point))
1819
                  (setq end (point))))
1820
    (cons beg end)))
1821
2652 by Andreas Röhler
Avoid shadowing `py-mark-decorators'
1822
(defun py-mark-base (form &optional mark-decorators)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1823
  "Calls py--mark-base, returns bounds of form, a cons. "
2652 by Andreas Röhler
Avoid shadowing `py-mark-decorators'
1824
  (let* ((bounds (py--mark-base form mark-decorators))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1825
         (beg (car bounds)))
1826
    (push-mark beg t t)
1827
    bounds))
1828
1829
(defun py-beginning (&optional indent)
1830
 "Go to beginning of compound statement or definition at point.
1831
1832
With \\[universal-argument], go to beginning one level above.
2385 by Andreas Röhler
Docu updated
1833
Returns position if successful, nil otherwise"
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1834
  (interactive "P")
2410 by Andreas Röhler
Reduce compiler warnings
1835
  (py--beginning-of-form-intern py-extended-block-or-clause-re (called-interactively-p 'any) indent))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1836
2654 by Andreas Röhler
Provide `py-fast-output-buffer' default buffer-name
1837
(defun py-end ()
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1838
 "Go to end of of compound statement or definition at point.
1839
2385 by Andreas Röhler
Docu updated
1840
Returns position block if successful, nil otherwise"
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1841
  (interactive "P")
1842
    (let* ((orig (point))
1843
           (erg (py--end-base 'py-extended-block-or-clause-re orig)))
2410 by Andreas Röhler
Reduce compiler warnings
1844
      (when (and py-verbose-p (called-interactively-p 'any)) (message "%s" erg))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1845
      erg))
1846
1847
;;  Buffer
1848
(defun py-beginning-of-buffer ()
1849
  "Go to beginning-of-buffer, return position. "
1850
  (let ((erg (unless (bobp)
1851
               (goto-char (point-min)))))
1852
    erg))
1853
1854
(defun py-end-of-buffer ()
1855
  "Go to end-of-buffer, return position.
1856
1857
  If already at end-of-buffer and not at EOB, go to end of next line. "
1858
  (let ((erg (unless (eobp)
1859
               (goto-char (point-max)))))
1860
    erg))
1861
1862
(defun py-backward-same-level ()
1863
  "Go form backward keeping indent level if possible.
1864
1865
If inside a delimited form --string or list-- go to its beginning.
1866
If not at beginning of a statement or block, go to its beginning.
1867
If at beginning of a statement or block, go to previous beginning of compound statement or definition at point.
2385 by Andreas Röhler
Docu updated
1868
If no further element at same level, go one level up."
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1869
  (interactive)
2387 by Andreas Röhler
Some cleanup
1870
  (let ((pps (parse-partial-sexp (point-min) (point))))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1871
    (cond ((nth 8 pps) (goto-char (nth 8 pps)))
1872
          ((nth 1 pps) (goto-char (nth 1 pps)))
2621 by Andreas Röhler
Navigation - major rewrite
1873
          (t (if (eq (current-column) (current-indentation))
1874
		 (py--beginning-of-form-intern 'py-extended-block-or-clause-re (called-interactively-p 'any))
1875
	       (back-to-indentation)
1876
	       (py-backward-same-level))))))
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1877
1878
(defun py--end-of-buffer-p ()
1879
  "Returns position, if cursor is at the end of buffer, nil otherwise. "
1880
  (when (eobp)(point)))
1881
2384 by Andreas Röhler
py-sectionize-region, new commands
1882
(defun py-sectionize-region (&optional beg end)
1883
  "Markup code in region as section.
1884
1885
Use current region unless optional args BEG END are delivered."
1886
  (interactive "*")
1887
  (let ((beg (or beg (region-beginning)))
1888
	(end (or (and end (copy-marker end)) (copy-marker (region-end)))))
1889
    (save-excursion
1890
      (goto-char beg)
1891
      (unless (empty-line-p) (split-line))
2521 by Andreas Röhler
Indent in docstring, #6
1892
      (beginning-of-line)
2384 by Andreas Röhler
py-sectionize-region, new commands
1893
      (insert py-section-start)
1894
      (goto-char end)
1895
      (unless (empty-line-p) (newline))
1896
      (insert py-section-end))))
1897
2381 by Andreas Röhler
Evaluating chunks of code,, lp:1467217, fixed
1898
(defun py-execute-section-prepare (&optional shell)
1899
  "Execute section at point. "
1900
  (save-excursion
2641 by Andreas Röhler
Avoid circular dependency
1901
    (let ((start (when (or (py--beginning-of-section-p)
2384 by Andreas Röhler
py-sectionize-region, new commands
1902
			   (py-backward-section))
2381 by Andreas Röhler
Evaluating chunks of code,, lp:1467217, fixed
1903
		   (forward-line 1)
1904
		   (beginning-of-line)
1905
		   (point))))
2384 by Andreas Röhler
py-sectionize-region, new commands
1906
      (if (and start (py-forward-section))
2381 by Andreas Röhler
Evaluating chunks of code,, lp:1467217, fixed
1907
	  (progn
1908
	    (beginning-of-line)
1909
	    (skip-chars-backward " \t\r\n\f")
1910
	    (if shell
1911
		(funcall (car (read-from-string (concat "py-execute-region-" shell))) start (point))
1912
	      (py-execute-region start (point))))
1913
	(error "Can't see `py-section-start' resp. `py-section-end'")))))
1914
2383 by Andreas Röhler
Provide py-narrow-to-class and related commands, lp:1467160, fixed
1915
(defun py--narrow-prepare (name)
1916
  "Used internally. "
1917
  (save-excursion
1918
    (let ((start (cond ((string= name "statement")
1919
			(if (py--beginning-of-statement-p)
1920
			    (point)
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1921
			  (py-backward-statement-bol)))
2383 by Andreas Röhler
Provide py-narrow-to-class and related commands, lp:1467160, fixed
1922
		       ((funcall (car (read-from-string (concat "py--statement-opens-" name "-p")))))
2391 by Andreas Röhler
Use `py-backward-' forms instead of `py-beginning-of'
1923
		       (t (funcall (car (read-from-string (concat "py-backward-" name "-bol"))))))))
1924
      (funcall (car (read-from-string (concat "py-forward-" name))))
2383 by Andreas Röhler
Provide py-narrow-to-class and related commands, lp:1467160, fixed
1925
      (narrow-to-region (point) start))))
1926
2594 by Andreas Röhler
Highlight matches when called interactively
1927
(defun py--forms-report-result (erg &optional iact)
2387 by Andreas Röhler
Some cleanup
1928
  (let ((res (ignore-errors (buffer-substring-no-properties (car-safe erg) (cdr-safe erg)))))
2594 by Andreas Röhler
Highlight matches when called interactively
1929
    (when (and res iact)
2387 by Andreas Röhler
Some cleanup
1930
      (goto-char (car-safe erg))
1931
      (set-mark (point))
1932
      (goto-char (cdr-safe erg)))
1933
    res))
1934
2584 by Andreas Röhler
`py-version' updated
1935
(defun py-rotate-shell-fontify-style (msg)
1936
  "Rotates between possible values 'all, 'input and nil. "
1937
  (interactive "p")
1938
  (cond ((eq py-shell-fontify-style 'all)
1939
	 (setq py-shell-fontify-style nil))
1940
	((eq py-shell-fontify-style 'input)
1941
	 (setq py-shell-fontify-style 'all))
1942
	(t (setq py-shell-fontify-style 'input)))
1943
  (py--shell-setup-fontification py-shell-fontify-style)
1944
  (when msg (message "py-shell-fontify-style set to: %s" py-shell-fontify-style)))
1945
2588 by Andreas Röhler
`py--edit-docstring-set-vars', proceed
1946
(defun py-toggle-execute-use-temp-file ()
1947
  (interactive)
1948
  (setq py--execute-use-temp-file-p (not py--execute-use-temp-file-p)))
1949
2626 by Andreas Röhler
Some more minor fixes WRT lexical scope
1950
(defun py--close-intern (regexp)
1951
  "Core function, internal used only. "
1952
  (let ((cui (car (py--go-to-keyword (symbol-value regexp)))))
1953
    (message "%s" cui)
1954
    (py--end-base regexp (point))
1955
    (forward-line 1)
1956
    (if py-close-provides-newline
1957
        (unless (empty-line-p) (split-line))
1958
      (fixup-whitespace))
1959
    (indent-to-column cui)
1960
    cui))
1961
2317 by Andreas Röhler
ipython2.7 ipython3: new versioned python-shell commands
1962
;; /usr/lib/python2.7/pdb.py eyp.py
2584 by Andreas Röhler
`py-version' updated
1963
(defalias 'IPython 'ipython)
1964
(defalias 'Ipython 'ipython)
1965
(defalias 'Python 'python)
1966
(defalias 'Python2 'python2)
1967
(defalias 'Python3 'python3)
1968
(defalias 'ipy 'ipython)
1969
(defalias 'iyp 'ipython)
1970
(defalias 'py-execute-region-default 'py-execute-region)
1971
(defalias 'py-execute-region-default-dedicated 'py-execute-region-dedicated)
1972
(defalias 'py-fast-send-string 'py-execute-string-fast)
2291 by Andreas Röhler
`py-beginning-of-for-block', lp:1367424
1973
(defalias 'py-kill-minor-expression 'py-kill-partial-expression)
2317 by Andreas Röhler
ipython2.7 ipython3: new versioned python-shell commands
1974
(defalias 'pyhotn 'python)
1975
(defalias 'pyhton 'python)
1976
(defalias 'pyt 'python)
2584 by Andreas Röhler
`py-version' updated
1977
362 by Andreas Roehler
towards a Python-IDE
1978
(provide 'python-components-intern)
2588 by Andreas Röhler
`py--edit-docstring-set-vars', proceed
1979
 ;;;  python-components-intern.el ends here