89
89
:group 'python-mode)
91
91
(defcustom py-load-pymacs-p nil
92
"If Pymacs as delivered with python-mode.el shall be loaded.
92
"If Pymacs as delivered with python-mode.el shall be loaded.
95
95
Pymacs has been written by François Pinard and many others.
96
96
See original source: http://pymacs.progiciels-bpi.ca"
101
101
(defcustom py-report-level-p nil
102
102
"If indenting functions should report reached indent level.
299
299
:tag "Jython Command Args")
301
301
(defcustom py-cleanup-temporary t
302
"If temporary buffers and files used by functions executing region should be deleted afterwards. "
302
"If temporary buffers and files used by functions executing region should be deleted afterwards. "
308
308
(defcustom py-lhs-inbound-indent 1
309
309
"When line starts a multiline-assignment: How many colums indent should be more than opening bracket, brace or parenthesis. "
464
464
(make-obsolete-variable 'py-jpython-packages 'py-jython-packages nil)
466
466
(defcustom py-current-defun-show t
467
"If `py-current-defun' should jump to the definition, highlight it while waiting PY-WHICH-FUNC-DELAY seconds, before returning to previous position.
467
"If `py-current-defun' should jump to the definition, highlight it while waiting PY-WHICH-FUNC-DELAY seconds, before returning to previous position.
474
474
(defcustom py-current-defun-delay 2
475
"When called interactively, `py-current-defun' should wait PY-WHICH-FUNC-DELAY seconds at the definition name found, before returning to previous position. "
475
"When called interactively, `py-current-defun' should wait PY-WHICH-FUNC-DELAY seconds at the definition name found, before returning to previous position. "
480
480
(defcustom py-send-receive-delay 5
481
"Seconds to wait for output, used by `python-send-receive'. "
481
"Seconds to wait for output, used by `python-send-receive'. "
486
486
(defvar py-exec-command nil
487
487
"Mode commands will set this. ")
634
634
"A regular expression to match the output prompt of IPython.")
636
636
(defcustom py-cleanup-temporary t
637
"If temporary buffers and files used by functions executing region should be deleted afterwards. "
637
"If temporary buffers and files used by functions executing region should be deleted afterwards. "
643
643
(defcustom py-match-paren-mode nil
644
644
"*Non-nil means, cursor will jump to beginning or end of a block.
1342
1342
(save-excursion
1346
((eq position 'bol) (beginning-of-line))
1347
((eq position 'eol) (end-of-line))
1348
((eq position 'bod) (py-beginning-of-def-or-class 'either))
1349
((eq position 'eod) (py-end-of-def-or-class 'either))
1350
;; Kind of funny, I know, but useful for py-up-exception.
1351
((eq position 'bob) (goto-char (point-min)))
1352
((eq position 'eob) (goto-char (point-max)))
1353
((eq position 'boi) (back-to-indentation))
1354
((eq position 'bos) (py-beginning-of-statement))
1346
((eq position 'bol) (beginning-of-line))
1347
((eq position 'eol) (end-of-line))
1348
((eq position 'bod) (py-beginning-of-def-or-class 'either))
1349
((eq position 'eod) (py-end-of-def-or-class 'either))
1350
;; Kind of funny, I know, but useful for py-up-exception.
1351
((eq position 'bob) (goto-char (point-min)))
1352
((eq position 'eob) (goto-char (point-max)))
1353
((eq position 'boi) (back-to-indentation))
1354
((eq position 'bos) (py-beginning-of-statement))
1355
1355
(t (error "Unknown buffer position requested: %s" position))) (point))))
1715
1715
(delete-char (- remains))
1716
1716
(indent-line-to (- (current-indentation) py-indent-offset))))
1717
1717
(delete-char (- 1))))
1718
(setq erg (current-column))
1719
(when (and (interactive-p) py-report-level-p) (message "%s" erg))
1718
(setq erg (current-column))
1719
(when (and (interactive-p) py-report-level-p) (message "%s" erg))
1722
1722
(defun py-electric-delete (&optional arg)
1876
1876
(interactive "P")
1877
1877
(save-excursion
1878
1878
(goto-char (point-min))
1879
(let ((lastindent (if
1880
(py-beginning-of-statement-p)
1881
(current-indentation)
1879
(let ((lastindent (if
1880
(py-beginning-of-statement-p)
1881
(current-indentation)
1883
1883
(py-down-statement)
1884
1884
(current-indentation)))))
1885
(while (and (eq lastindent (current-indentation))
1885
(while (and (eq lastindent (current-indentation))
1887
1887
(setq erg (point))
1888
1888
(py-down-statement)
1889
1889
(< erg (point))
1890
(not (py-guessed-sanity-check (setq erg (abs (- lastindent (current-indentation))))))))
1890
(not (py-guessed-sanity-check (setq erg (abs (- lastindent (current-indentation))))))))
1891
1891
(if (py-guessed-sanity-check erg)
1893
(funcall (if global 'kill-local-variable 'make-local-variable)
1895
(setq py-indent-offset erg))
1893
(funcall (if global 'kill-local-variable 'make-local-variable)
1895
(setq py-indent-offset erg))
1896
1896
(setq py-indent-offset (default-value 'py-indent-offset)))
1897
(when (interactive-p)
1898
(message "%s value of py-indent-offset: %d"
1899
(if global "Global" "Local")
1897
(when (interactive-p)
1898
(message "%s value of py-indent-offset: %d"
1899
(if global "Global" "Local")
1901
1901
py-indent-offset)))
1903
1903
(defun py-guessed-sanity-check (guessed)
2399
2399
(py-beginning-of-statement))
2400
2400
(line-beginning-position))
2401
2401
(py-beginning-of-statement)
2402
(not (py-beginning-of-block-p))
2402
(not (py-beginning-of-block-p))
2403
2403
(eq (current-indentation) orig-indent))
2404
2404
(setq beg (line-beginning-position)))
2405
2405
(goto-char orig)
2752
2752
(interactive "*")
2753
2753
(let* ((orig (point))
2754
2754
(funcname (progn
2755
(py-beginning-of-def)
2756
(when (looking-at (concat py-def-re " *\\([^(]+\\) *(\\(?:[^),]*\\),? *\\([^)]*\\))"))
2757
(match-string-no-properties 2))))
2755
(py-beginning-of-def)
2756
(when (looking-at (concat py-def-re " *\\([^(]+\\) *(\\(?:[^),]*\\),? *\\([^)]*\\))"))
2757
(match-string-no-properties 2))))
2758
2758
(args (match-string-no-properties 3))
2759
2759
(erg (py-which-python))
2825
2825
(skip-chars-backward " \t\r\n\f")
2826
2826
(if (ignore-errors (< (nth 2 (if (featurep 'xemacs)
2827
(parse-partial-sexp (point-min) (point))
2828
(syntax-ppss))) (line-beginning-position)))
2827
(parse-partial-sexp (point-min) (point))
2828
(syntax-ppss))) (line-beginning-position)))
2829
2829
(current-indentation)
2830
(ignore-errors (goto-char (nth 2 pps)))
2831
(py-line-backward-maybe)
2832
(back-to-indentation)
2830
(ignore-errors (goto-char (nth 2 pps)))
2831
(py-line-backward-maybe)
2832
(back-to-indentation)
2833
2833
(py-compute-indentation orig origline closing line inside repeat)))
2834
2834
(current-indentation)))
2835
2835
((and (looking-at "\"\"\"\\|'''")(not (bobp)))
2858
2858
(cond ((and inside (not line))
2859
(when (and (eq (point) orig) (looking-at "[ \t]*\\()\\)[ \t]*$"))
2860
(setq closing (match-beginning 0)))
2862
(goto-char (nth 1 pps))
2863
(setq this-line (py-count-lines))
2865
((< 0 (- origline this-line))
2866
(if (< 1 (- origline this-line))
2868
(if py-closing-list-dedents-bos
2869
(current-indentation)
2870
(+ (current-indentation) py-indent-offset))
2871
(py-fetch-previous-indent orig))
2872
(cond ((looking-at "\\s([ \t]*$")
2876
(back-to-indentation)
2877
(looking-at py-block-or-clause-re)))
2879
(back-to-indentation)
2880
(+ (current-column) (* 2 py-indent-offset)))
2881
(back-to-indentation)
2882
(+ (current-column) py-indent-offset)))
2883
((looking-at "\\s([ \t]*\\([^ \t]+.*\\)$")
2884
(goto-char (match-beginning 1))
2886
(t (+ (current-column) (* (nth 0 pps)))))))
2887
(t (back-to-indentation)
2888
(py-beginning-of-statement)
2889
(py-compute-indentation orig origline closing line inside repeat)))))
2859
(when (and (eq (point) orig) (looking-at "[ \t]*\\()\\)[ \t]*$"))
2860
(setq closing (match-beginning 0)))
2862
(goto-char (nth 1 pps))
2863
(setq this-line (py-count-lines))
2865
((< 0 (- origline this-line))
2866
(if (< 1 (- origline this-line))
2868
(if py-closing-list-dedents-bos
2869
(current-indentation)
2870
(+ (current-indentation) py-indent-offset))
2871
(py-fetch-previous-indent orig))
2872
(cond ((looking-at "\\s([ \t]*$")
2876
(back-to-indentation)
2877
(looking-at py-block-or-clause-re)))
2879
(back-to-indentation)
2880
(+ (current-column) (* 2 py-indent-offset)))
2881
(back-to-indentation)
2882
(+ (current-column) py-indent-offset)))
2883
((looking-at "\\s([ \t]*\\([^ \t]+.*\\)$")
2884
(goto-char (match-beginning 1))
2886
(t (+ (current-column) (* (nth 0 pps)))))))
2887
(t (back-to-indentation)
2888
(py-beginning-of-statement)
2889
(py-compute-indentation orig origline closing line inside repeat)))))
2890
2890
((and (not inside) line)
2891
(py-beginning-of-statement)
2892
(py-compute-indentation orig origline closing line inside repeat))
2891
(py-beginning-of-statement)
2892
(py-compute-indentation orig origline closing line inside repeat))
2894
(progn (goto-char (+ py-lhs-inbound-indent (nth 1 pps)))
2895
(when (looking-at "[ \t]+")
2896
(goto-char (match-end 0)))
2894
(progn (goto-char (+ py-lhs-inbound-indent (nth 1 pps)))
2895
(when (looking-at "[ \t]+")
2896
(goto-char (match-end 0)))
2899
(goto-char (nth 1 pps))
2899
(goto-char (nth 1 pps))
2900
2900
(py-compute-indentation orig origline closing line inside repeat))))
2901
2901
((py-preceding-line-backslashed-p)
3391
3391
(save-restriction
3393
3393
(let ((beg (cond (start)
3398
(if (featurep 'xemacs)
3399
(setq erg (count-lines beg end))
3400
(setq erg (1+ (count-matches "[\n\C-m]" beg end))))
3401
(when (interactive-p) (message "%s" erg))
3398
(if (featurep 'xemacs)
3399
(setq erg (count-lines beg end))
3400
(setq erg (1+ (count-matches "[\n\C-m]" beg end))))
3401
(when (interactive-p) (message "%s" erg))
3404
3404
(defun py-which-function ()
4014
4014
(goto-char (nth 8 pps))
4015
(when (looking-at "\"\"\"\\|'''")
4016
(goto-char (match-end 0))
4015
(when (looking-at "\"\"\"\\|'''")
4016
(goto-char (match-end 0))
4017
4017
(while (and (re-search-forward (match-string-no-properties 0) nil (quote move) 1)
4018
4018
(setq done nil)
4020
4020
(if (featurep 'xemacs)
4021
4021
(parse-partial-sexp (point-min) (point))
4022
4022
(syntax-ppss)))))
4025
(skip-chars-backward " \t\r\n\f" (line-beginning-position))
4025
(skip-chars-backward " \t\r\n\f" (line-beginning-position))
4027
4027
(py-end-of-statement orig origline done)))
4034
(skip-chars-backward " \t\r\n\f" (line-beginning-position))
4034
(skip-chars-backward " \t\r\n\f" (line-beginning-position))
4037
4037
(py-end-of-statement orig origline done)))))
4038
4038
((and (looking-at "[ \t]*#")(looking-back "^[ \t]*"))
4039
4039
(while (and (looking-at "[ \t]*#") (forward-line 1)(not (eobp))
4063
4063
(goto-char orig))))
4064
4064
((eq (point) orig)
4065
4065
(cond ((not (looking-at "[ \t]*$"))
4067
(py-beginning-of-comment)
4068
(skip-chars-backward " \t")
4069
(if (< orig (point))
4070
(py-end-of-statement orig origline t)
4072
(py-end-of-statement orig origline done)))
4067
(py-beginning-of-comment)
4068
(skip-chars-backward " \t")
4069
(if (< orig (point))
4070
(py-end-of-statement orig origline t)
4072
(py-end-of-statement orig origline done)))
4073
4073
((and (looking-at "[ \t]*$")(not (eobp)))
4076
(py-end-of-statement orig origline done))
4076
(py-end-of-statement orig origline done))
4079
4079
(py-end-of-statement orig origline done))))
4080
4080
((and (bolp) (not (empty-line-p)))
4088
4088
(skip-chars-forward "A-Za-z_0-9")
4089
4089
(forward-char 1)
4090
4090
(py-end-of-statement orig origline done)))
4091
(unless (or (eq (point) orig)(empty-line-p)
4092
(if (featurep 'xemacs)
4093
(nth 4 (parse-partial-sexp (point-min) (point)))
4094
(nth 4 (syntax-ppss)))
4095
(eq 0 (current-column)))
4097
(when (interactive-p) (message "%s" erg))
4091
(unless (or (eq (point) orig)(empty-line-p)
4092
(if (featurep 'xemacs)
4093
(nth 4 (parse-partial-sexp (point-min) (point)))
4094
(nth 4 (syntax-ppss)))
4095
(eq 0 (current-column)))
4097
(when (interactive-p) (message "%s" erg))
4098
4098
;; (message "%s" erg)
5032
5032
(defalias 'py-dedicated-shell 'py-shell-dedicated)
5033
5033
(defun py-shell-dedicated (&optional argprompt)
5034
"Start an interactive Python interpreter in another window.
5034
"Start an interactive Python interpreter in another window.
5036
5036
With optional \\[universal-argument] user is prompted by
5037
5037
`py-choose-shell' for command and options to pass to the Python
5053
5053
(cond ((eq 4 (prefix-numeric-value argprompt))
5054
5054
(py-choose-shell '(4)))
5055
5055
(py-use-local-default
5056
(if (not (string= "" py-shell-local-path))
5057
(expand-file-name py-shell-local-path)
5056
(if (not (string= "" py-shell-local-path))
5057
(expand-file-name py-shell-local-path)
5058
5058
(message "Abort: `py-use-local-default' is set to `t' but `py-shell-local-path' is empty. Maybe call `py-toggle-local-default-use'")))
5059
5059
(pyshellname pyshellname)
5060
5060
((stringp py-shell-name) py-shell-name)
5286
5286
(defun py-execute-intern (strg &optional procbuf proc temp file filebuf name py-execute-directory)
5288
5288
(pec (if (string-match "Python3" name)
5289
(format "exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\n" file file)
5290
(format "execfile(r'%s') # PYTHON-MODE\n" file)))
5289
(format "exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\n" file file)
5290
(format "execfile(r'%s') # PYTHON-MODE\n" file)))
5292
5292
(set-buffer filebuf)
5430
5430
(switch-to-buffer procbuf)
5432
5432
(comint-send-input)
5433
;; (ipython-send-and-indent)
5433
;; (ipython-send-and-indent)
5434
5434
;; (when (< 1 lines)
5435
;; (goto-char (point-max))
5435
;; (goto-char (point-max))
5436
5436
;; (comint-send-input))
5439
5439
(defun ipython-get-indenting-buffer ()
5440
"Return a temporary buffer set in python-mode. Create one if necessary."
5441
(let ((buf (get-buffer-create ipython-indenting-buffer-name)))
5443
(unless (eq major-mode 'python-mode)
5440
"Return a temporary buffer set in python-mode. Create one if necessary."
5441
(let ((buf (get-buffer-create ipython-indenting-buffer-name)))
5443
(unless (eq major-mode 'python-mode)
5447
5447
(defvar ipython-autoindent t
5448
"If non-nil, enable autoindent for IPython shell through python-mode.")
5448
"If non-nil, enable autoindent for IPython shell through python-mode.")
5450
5450
(defun ipython-send-and-indent ()
5451
"Send the current line to IPython, and calculate the indentation for
5451
"Send the current line to IPython, and calculate the indentation for
5452
5452
the next line."
5454
(if ipython-autoindent
5455
(let ((line (buffer-substring (point-at-bol) (point)))
5460
(if (looking-at py-shell-input-prompt-1-regexp)
5461
(setq after-prompt1 t)
5462
(setq after-prompt2 (looking-at py-shell-input-prompt-2-regexp)))
5463
(with-current-buffer (ipython-get-indenting-buffer)
5466
(when (or after-prompt1 after-prompt2)
5467
(delete-region (point-at-bol) (point))
5469
(newline-and-indent))))))
5470
;; send input line to ipython interpreter
5471
(comint-send-input))
5454
(if ipython-autoindent
5455
(let ((line (buffer-substring (point-at-bol) (point)))
5460
(if (looking-at py-shell-input-prompt-1-regexp)
5461
(setq after-prompt1 t)
5462
(setq after-prompt2 (looking-at py-shell-input-prompt-2-regexp)))
5463
(with-current-buffer (ipython-get-indenting-buffer)
5466
(when (or after-prompt1 after-prompt2)
5467
(delete-region (point-at-bol) (point))
5469
(newline-and-indent))))))
5470
;; send input line to ipython interpreter
5471
(comint-send-input))
5473
5473
(defun py-execute-region-in-shell (start end &optional async)
5474
5474
"Execute the region in a Python shell. "
5677
5677
(let ((proc (if dedicated
5678
5678
(get-process (py-shell nil dedicated))
5679
5679
(get-process (py-shell)))))
5680
;; Maybe save some buffers
5681
(save-some-buffers (not py-ask-about-save) nil)
5680
;; Maybe save some buffers
5681
(save-some-buffers (not py-ask-about-save) nil)
5682
5682
(py-execute-file proc file
5683
(if (string-match "\\.py$" file)
5684
(let ((m (py-qualified-module-name (expand-file-name file))))
5685
(format "import sys\nif sys.modules.has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
5687
;; (format "execfile(r'%s')\n" file)
5683
(if (string-match "\\.py$" file)
5684
(let ((m (py-qualified-module-name (expand-file-name file))))
5685
(format "import sys\nif sys.modules.has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
5687
;; (format "execfile(r'%s')\n" file)
5688
5688
(py-which-execute-file-command file)))))
5690
5690
(py-execute-buffer async))))
5699
5699
(if (file-exists-p initpy)
5700
5700
(let ((d2 (directory-file-name d)))
5701
5701
(funcall rec (file-name-directory d2)
5702
(concat (file-name-nondirectory d2) "." f)))
5702
(concat (file-name-nondirectory d2) "." f)))
5704
5704
(funcall rec (file-name-directory file)
5705
5705
(file-name-sans-extension (file-name-nondirectory file)))))
5910
5910
"Send the current defun (class or method) to the inferior Python process."
5912
5912
(save-excursion (py-execute-region (progn (beginning-of-defun) (point))
5913
(progn (end-of-defun) (point)))))
5913
(progn (end-of-defun) (point)))))
5915
5915
(defun py-process-file (filename &optional output-buffer error-buffer)
5916
5916
"Process \"python filename\".
6214
6214
line (string-to-number (match-string-no-properties 3))))
6215
6215
(save-excursion
6216
;; file and line-number are in different lines
6217
(setq line (string-to-number (match-string-no-properties 1))
6220
(when (and (re-search-backward "\\(^IPython\\|^In \\[[0-9]+\\]: *\\|^>>>\\|^[^\t >]+\\)>?[ \t]+in[ \t]+\\([^ \t\n]+\\)" nil t 1)
6221
(not (save-match-data (string-match "<\\|^IPython\\|^In \\[[0-9]+\\]: *\\|^>>>" (match-string-no-properties 1)))))
6216
;; file and line-number are in different lines
6217
(setq line (string-to-number (match-string-no-properties 1))
6220
(when (and (re-search-backward "\\(^IPython\\|^In \\[[0-9]+\\]: *\\|^>>>\\|^[^\t >]+\\)>?[ \t]+in[ \t]+\\([^ \t\n]+\\)" nil t 1)
6221
(not (save-match-data (string-match "<\\|^IPython\\|^In \\[[0-9]+\\]: *\\|^>>>" (match-string-no-properties 1)))))
6222
6222
(match-string-no-properties 1))))))
6224
(when (string-match ".+\.pyc" file)
6225
(setq file (substring file 0 -1)))
6224
(when (string-match ".+\.pyc" file)
6225
(setq file (substring file 0 -1)))
6226
6226
(error "%s of traceback" errwhere))
6227
6227
(if (and file line)
6329
6329
(setq target_buffer (cadr target))
6330
6330
(setq target_fname (buffer-file-name target_buffer))
6331
6331
(switch-to-buffer-other-window target_buffer)
6332
;; (goto-line target_lineno)
6332
;; (goto-line target_lineno)
6333
6333
(goto-char (point-min))
6334
6334
(forward-line target_lineno)
6335
6335
(message "pdbtrack: line %s, file %s" target_lineno target_fname)
6353
6353
(if (and (not (string-match py-pdbtrack-stack-entry-regexp block))
6354
6354
;; (not (string-match py-pydbtrack-stack-entry-regexp block))
6356
6356
"Traceback cue not found"
6357
6357
(let* ((filename (match-string
6358
6358
py-pdbtrack-marker-regexp-file-group block))
6359
6359
(lineno (string-to-number (match-string
6360
py-pdbtrack-marker-regexp-line-group
6360
py-pdbtrack-marker-regexp-line-group
6362
6362
(funcname (match-string py-pdbtrack-marker-regexp-funcname-group
6463
6463
(defun py-find-imports ()
6467
(goto-char (point-min))
6468
(while (re-search-forward
6469
"^import *[A-Za-z_][A-Za-z_0-9].*\\|^from +[A-Za-z_][A-Za-z_0-9]+ +import .*" nil t)
6473
(buffer-substring-no-properties (match-beginning 0) (match-end 0)) "\n"))))))
6467
(goto-char (point-min))
6468
(while (re-search-forward
6469
"^import *[A-Za-z_][A-Za-z_0-9].*\\|^from +[A-Za-z_][A-Za-z_0-9]+ +import .*" nil t)
6473
(buffer-substring-no-properties (match-beginning 0) (match-end 0)) "\n"))))))
6474
6474
(when (interactive-p) (message "%s" erg))
6482
6482
"Find top-level imports, updating `python-imports'."
6484
6484
(save-excursion
6486
(goto-char (point-min))
6487
(while (re-search-forward "^import\\>\\|^from\\>" nil t)
6488
(unless (syntax-ppss-context (syntax-ppss))
6489
(let ((start (line-beginning-position)))
6490
;; Skip over continued lines.
6491
(while (and (eq ?\\ (char-before (line-end-position)))
6492
(= 0 (forward-line 1)))
6494
(push (buffer-substring start (line-beginning-position 2))
6496
(setq python-imports
6502
(set-text-properties 0 (length python-imports) nil python-imports)
6503
;; The output ends up in the wrong place if the string we
6504
;; send contains newlines (from the imports).
6505
(setq python-imports
6506
(replace-regexp-in-string "\n" "\\n"
6507
(format "%S" python-imports) t t)))))
6486
(goto-char (point-min))
6487
(while (re-search-forward "^import\\>\\|^from\\>" nil t)
6488
(unless (syntax-ppss-context (syntax-ppss))
6489
(let ((start (line-beginning-position)))
6490
;; Skip over continued lines.
6491
(while (and (eq ?\\ (char-before (line-end-position)))
6492
(= 0 (forward-line 1)))
6494
(push (buffer-substring start (line-beginning-position 2))
6496
(setq python-imports
6502
(set-text-properties 0 (length python-imports) nil python-imports)
6503
;; The output ends up in the wrong place if the string we
6504
;; send contains newlines (from the imports).
6505
(setq python-imports
6506
(replace-regexp-in-string "\n" "\\n"
6507
(format "%S" python-imports) t t)))))
6508
6508
(when (interactive-p) (message "%s" (car (read-from-string python-imports))))
6509
6509
python-imports)
6855
6855
(with-current-buffer (process-buffer proc)
6856
6856
(when (python-check-comint-prompt proc)
6857
6857
(set (make-local-variable 'python-preoutput-result) nil)
6858
(accept-process-output proc py-send-receive-delay)
6858
(accept-process-output proc py-send-receive-delay)
6859
6859
(if (null python-preoutput-result)
6860
(message "No output from: %s, maybe set `py-send-receive-delay' onto a higher value " string))
6860
(message "No output from: %s, maybe set `py-send-receive-delay' onto a higher value " string))
6861
6861
(prog1 python-preoutput-result
6862
6862
(kill-local-variable 'python-preoutput-result))))))
7478
7478
;; look for an interpreter specified in the first line
7479
7479
;; similar to set-auto-mode (files.el)
7481
(interpreter (save-excursion
7482
(goto-char (point-min))
7483
(when (looking-at py-shebang-regexp)
7484
(setq erg (match-string-no-properties 0))
7485
(substring erg (string-match "[ijp]+ython" erg))))))
7481
(interpreter (save-excursion
7482
(goto-char (point-min))
7483
(when (looking-at py-shebang-regexp)
7484
(setq erg (match-string-no-properties 0))
7485
(substring erg (string-match "[ijp]+ython" erg))))))
7486
7486
(when (interactive-p) (message "%s" interpreter))
7610
7610
(if path (concat path path-separator))
7611
7611
(expand-file-name py-install-directory) "/Pymacs")))
7613
(if (or (not (boundp 'py-install-directory)) (not (stringp py-install-directory)))
7614
(error "`py-install-directory' not set, see INSTALL")
7613
(if (or (not (boundp 'py-install-directory)) (not (stringp py-install-directory)))
7614
(error "`py-install-directory' not set, see INSTALL")
7615
7615
(load (concat py-install-directory "/pymacs.el") nil t)
7616
7616
(setenv "PYMACS_PYTHON" (if (string-match "IP" pyshell)
7619
(autoload 'pymacs-apply "pymacs")
7620
(autoload 'pymacs-call "pymacs")
7621
(autoload 'pymacs-eval "pymacs")
7622
(autoload 'pymacs-exec "pymacs")
7623
(autoload 'pymacs-load "pymacs")
7619
(autoload 'pymacs-apply "pymacs")
7620
(autoload 'pymacs-call "pymacs")
7621
(autoload 'pymacs-eval "pymacs")
7622
(autoload 'pymacs-exec "pymacs")
7623
(autoload 'pymacs-load "pymacs")
7624
7624
(require 'pymacs))
7625
7625
(unwind-protect
7648
7648
(add-to-list 'load-path (concat (expand-file-name py-install-directory) "/test"))
7649
7649
(add-to-list 'load-path (concat (expand-file-name py-install-directory) "/tools")))
7650
7650
((when py-guess-py-install-directory-p
7651
(py-guess-py-install-directory)))
7651
(py-guess-py-install-directory)))
7652
7652
(t (error "Please set `py-install-directory', see INSTALL"))
7653
7653
(when (interactive-p) (message "%s" load-path))))
7828
7828
(when (interactive-p) (message "python-mode loaded from: %s" "python-mode.el")))
7830
7830
(defadvice pdb (before gud-query-cmdline activate)
7831
"Provide a better default command line when called interactively."
7833
(list (gud-query-cmdline pdb-path
7834
(file-name-nondirectory buffer-file-name)))))
7831
"Provide a better default command line when called interactively."
7833
(list (gud-query-cmdline pdb-path
7834
(file-name-nondirectory buffer-file-name)))))
7836
7836
(defalias 'py-hungry-delete-forward 'c-hungry-delete-forward)
7837
7837
(defalias 'py-hungry-delete-backwards 'c-hungry-delete-backwards)
7971
7971
Returns `py-use-local-default'
7973
7973
See also `py-install-local-shells'
7974
Installing named virualenv shells is the preffered way,
7974
Installing named virualenv shells is the preffered way,
7975
7975
as it leaves your system default unchanged."
7976
7976
(setq py-use-local-default (not py-use-local-default))
7977
7977
(when (interactive-p) (message "py-use-local-default set to %s" py-use-local-default))
8822
8822
`kill-output-from-shell' does The Right Thing."
8823
8823
(let ((curbuf (current-buffer))
8824
8824
(procbuf (process-buffer proc))
8825
; (comint-scroll-to-bottom-on-output t)
8825
; (comint-scroll-to-bottom-on-output t)
8826
8826
(msg (format "## working on region in file %s...\n" filename))
8827
8827
;; add some comment, so that we can filter it out of history
8828
8828
(cmd (format "execfile(r'%s') # PYTHON-MODE\n" filename)))
8962
8962
(get-process (py-process-name)))
8963
8963
(cmd (format "exec '''%s''' in {}"
8964
8964
(mapconcat 'identity (split-string string "\n") "\\n")))
8965
(procbuf (process-buffer proc))
8966
(outbuf (get-buffer-create " *pyshellcomplete-output*"))
8967
(lines (reverse py-shell-input-lines)))
8965
(procbuf (process-buffer proc))
8966
(outbuf (get-buffer-create " *pyshellcomplete-output*"))
8967
(lines (reverse py-shell-input-lines)))
8968
8968
(if (and proc (not py-file-queue))
8969
8969
(unwind-protect
8970
8970
(condition-case nil
8990
8990
(while (not comint-redirect-completed) ; wait for output
8991
8991
(accept-process-output proc 1)))
8992
8992
(signal 'quit nil)))
8993
(if (with-current-buffer procbuf comint-redirect-completed)
8995
(with-current-buffer procbuf
8996
(py-shell-redirect-send-command-to-process
8997
(car lines) outbuf proc nil t))
8998
(accept-process-output proc 1)
8999
(setq lines (cdr lines))))))))
8993
(if (with-current-buffer procbuf comint-redirect-completed)
8995
(with-current-buffer procbuf
8996
(py-shell-redirect-send-command-to-process
8997
(car lines) outbuf proc nil t))
8998
(accept-process-output proc 1)
8999
(setq lines (cdr lines))))))))
9001
9001
(defun py-dot-word-before-point ()
9002
9002
(buffer-substring
9103
9103
;; (setq python-process (get-process (ipython-dedicated))))
9104
9104
(if (string= pattern "")
9105
9105
(tab-to-tab-stop)
9106
(process-send-string python-process
9107
(format completion-command-string pattern))
9108
(accept-process-output python-process)
9110
(split-string (substring ugly-return 0 (position ?\n ugly-return)) sep))
9111
(setq completion-table (loop for str in completions
9112
collect (list str nil)))
9113
(setq completion (try-completion pattern completion-table))
9114
(cond ((eq completion t))
9116
(message "Can't find completion for \"%s\"" pattern)
9118
((not (string= pattern completion))
9119
(delete-region beg end)
9120
(insert completion))
9122
(message "Making completion list...")
9123
(with-output-to-temp-buffer "*Python Completions*"
9124
(display-completion-list (all-completions pattern completion-table)))
9106
(process-send-string python-process
9107
(format completion-command-string pattern))
9108
(accept-process-output python-process)
9110
(split-string (substring ugly-return 0 (position ?\n ugly-return)) sep))
9111
(setq completion-table (loop for str in completions
9112
collect (list str nil)))
9113
(setq completion (try-completion pattern completion-table))
9114
(cond ((eq completion t))
9116
(message "Can't find completion for \"%s\"" pattern)
9118
((not (string= pattern completion))
9119
(delete-region beg end)
9120
(insert completion))
9122
(message "Making completion list...")
9123
(with-output-to-temp-buffer "*Python Completions*"
9124
(display-completion-list (all-completions pattern completion-table)))
9125
9125
(message "Making completion list...%s" "done"))))))
9132
9132
(if (buffer-file-name)
9133
(format "%s %s %s" py-pychecker-command
9134
(mapconcat 'identity py-pychecker-command-args " ")
9133
(format "%s %s %s" py-pychecker-command
9134
(mapconcat 'identity py-pychecker-command-args " ")
9135
9135
(buffer-file-name))
9136
9136
(format "%s %s" py-pychecker-command
9137
9137
(mapconcat 'identity py-pychecker-command-args " "))))