2013-06-12 23:40:20 +02:00
|
|
|
;; Python
|
|
|
|
|
2013-07-04 08:39:35 +02:00
|
|
|
(defun python-version ()
|
2014-02-26 18:22:47 +01:00
|
|
|
"Returns \"python2\" or \"python3\" according to the shabang.
|
|
|
|
`python-shell-interpreter' is assumed by default."
|
2014-02-12 16:15:13 +01:00
|
|
|
(let ((firstline
|
|
|
|
(car
|
|
|
|
(split-string (buffer-substring-no-properties 1 (point-max)) "\n"))))
|
2013-07-04 08:39:35 +02:00
|
|
|
(if (not (string-match "^#!" firstline))
|
|
|
|
"python"
|
|
|
|
(cond
|
|
|
|
((string-match "python2" firstline) "python2")
|
|
|
|
((string-match "python3" firstline) "python3")
|
2014-02-26 18:22:47 +01:00
|
|
|
(t python-shell-interpreter)))))
|
2013-07-03 20:14:06 +02:00
|
|
|
|
2013-07-04 10:37:18 +02:00
|
|
|
(defun python-set-interpreter ()
|
2013-07-03 20:14:06 +02:00
|
|
|
"Use compile to run python programs."
|
|
|
|
(interactive)
|
2014-02-26 18:22:47 +01:00
|
|
|
(set (make-local-variable 'compile-command)
|
2015-04-19 13:25:29 +02:00
|
|
|
(concat (python-version) " " (shell-quote-argument buffer-file-name))))
|
2013-07-03 20:14:06 +02:00
|
|
|
|
2014-02-26 18:22:47 +01:00
|
|
|
(add-hook-and-eval
|
2013-06-12 23:40:20 +02:00
|
|
|
'python-mode-hook
|
|
|
|
(lambda ()
|
2013-07-03 20:14:06 +02:00
|
|
|
(set (make-local-variable 'compilation-scroll-output) t)
|
2014-03-15 15:08:44 +01:00
|
|
|
(add-hook 'compilation-before-hook 'python-set-interpreter nil t)))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
|
|
|
;; Doc lookup. Requires the python.info file to be installed. See
|
|
|
|
;; https://bitbucket.org/jonwaltman/pydoc-info/.
|
2014-02-12 17:37:43 +01:00
|
|
|
;; (add-to-list 'load-path "~/path/to/pydoc-info")
|
|
|
|
;; (require 'pydoc-info nil t)
|
2014-02-12 17:48:29 +01:00
|
|
|
|
2014-02-26 18:22:47 +01:00
|
|
|
(provide 'mode-python)
|