2013-06-12 23:40:20 +02:00
|
|
|
;;==============================================================================
|
|
|
|
;; Python
|
|
|
|
;;==============================================================================
|
|
|
|
|
2013-07-04 10:37:18 +02:00
|
|
|
(defcustom python-interpreter nil
|
|
|
|
"Python interpreter. If value is nil, the shabang will be
|
|
|
|
checked, and if no shabang is present, `python' will be used."
|
2013-06-12 23:40:20 +02:00
|
|
|
:safe 'stringp)
|
|
|
|
|
2013-07-04 08:39:35 +02:00
|
|
|
(defun python-version ()
|
|
|
|
"Returns whether we're running Python 2 or 3 according to the
|
|
|
|
shebang. System `python' 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")
|
|
|
|
(t "python")))))
|
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)
|
2013-09-17 15:43:45 +02:00
|
|
|
(hack-local-variables)
|
2013-07-04 10:37:18 +02:00
|
|
|
(let ((py-interpreter
|
|
|
|
(if python-interpreter
|
|
|
|
python-interpreter
|
|
|
|
(python-version))))
|
|
|
|
(set (make-local-variable 'compile-command)
|
|
|
|
(concat py-interpreter " " buffer-file-name))))
|
2013-07-03 20:14:06 +02:00
|
|
|
|
2013-06-12 23:40:20 +02:00
|
|
|
(add-hook
|
|
|
|
'python-mode-hook
|
|
|
|
(lambda ()
|
2013-07-03 20:14:06 +02:00
|
|
|
(set (make-local-variable 'compilation-scroll-output) t)
|
2013-07-04 10:37:18 +02:00
|
|
|
(python-set-interpreter)))
|
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
|
|
|
|
|
|
|
(provide 'python)
|