Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor python compile command
  • Loading branch information
syl20bnr committed Mar 14, 2015
1 parent ca7048c commit 3098a1a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
19 changes: 14 additions & 5 deletions contrib/lang/python/README.md
Expand Up @@ -10,6 +10,7 @@
- [Install](#install)
- [Key Bindings](#key-bindings)
- [Inferior REPL process](#inferior-repl-process)
- [Running Python Script in shell](#running-python-script-in-shell)
- [Testing in Python](#testing-in-python)
- [Other Python commands](#other-python-commands)
- [Django](#django)
Expand Down Expand Up @@ -67,12 +68,20 @@ Send code to inferior process commands:
<kbd>CTRL+j</kbd> | next item in REPL history
<kbd>CTRL+k</kbd> | previous item in REPL history

### Running Python Script in Comint Mode
### Running Python Script in shell

To run a Python script like you would in the shell
press <kbd>SPC c C RET</kbd> to start the Python script in
Comint mode. This is useful when working with multiple Python files
since the REPL does not reload changes made in other modules.
To run a Python script like you would in the shell press <kbd>SPC m c c</kbd>
to start the Python script in comint mode. This is useful when working with
multiple Python files since the REPL does not reload changes made in other
modules.

Key Binding | Description
----------------------|------------------------------------------------------------
<kbd>SPC m c c</kbd> | Execute current file in a comint shell
<kbd>SPC m c C</kbd> | Execute current file in a comint shell and switch to it in `insert state`

**Note** With the universal argument <kbd>SPC u</kbd> you can enter a new
compilation command.

### Testing in Python

Expand Down
22 changes: 0 additions & 22 deletions contrib/lang/python/extensions.el
Expand Up @@ -67,25 +67,3 @@
(setq pylookup-dir (concat dir "/pylookup")
pylookup-program (concat pylookup-dir "/pylookup.py")
pylookup-db-file (concat pylookup-dir "/pylookup.db"))))))

(defun python/init-python-compile ()
"Initialize Compile command for python buffers"
;; set compile command to buffer-file-name
;; if buffer-file-name exists
;; otherwise error occurs on e.g. org export including python src
(add-hook 'python-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(if buffer-file-name
(format "python %s" (file-name-nondirectory buffer-file-name))))))

(defadvice compile (before ad-compile-smart activate)
"Advises `compile' so it sets the argument COMINT to t
in `python-mode' files"
(when (derived-mode-p major-mode 'python-mode)
(save-excursion
(save-match-data
(goto-char (point-min))
;; set COMINT argument to `t'.
(ad-set-arg 1 t)))))
)
30 changes: 30 additions & 0 deletions contrib/lang/python/packages.el
Expand Up @@ -204,7 +204,37 @@ which require an initialization must be listed explicitly in the list.")
(python-shell-switch-to-shell)
(evil-insert-state))

;; reset compile-command (by default it is `make -k')
(setq compile-command nil)
(defun spacemacs/python-execute-file (arg)
"Execute a python script in a shell."
(interactive "P")
;; set compile command to buffer-file-name
;; if buffer-file-name exists
;; otherwise error occurs on e.g. org export including python src
;; universal argument put compile buffer in comint mode
(setq universal-argument t)
(if arg
(call-interactively 'compile)
(unless compile-command
(setq compile-command (format "python %s" (file-name-nondirectory
buffer-file-name))))
(compile compile-command t)))

(defun spacemacs/python-execute-file-focus (arg)
"Execute a python script in a shell and switch to the shell buffer in
`insert state'."
(interactive "P")
;; set compile command to buffer-file-name
;; if buffer-file-name exists
;; otherwise error occurs on e.g. org export including python src
(spacemacs/python-execute-file arg)
(switch-to-buffer-other-window "*compilation*")
(evil-insert-state))

(evil-leader/set-key-for-mode 'python-mode
"mcc" 'spacemacs/python-execute-file
"mcC" 'spacemacs/python-execute-file-focus
"mdb" 'python-toggle-breakpoint
"msB" 'python-shell-send-buffer-switch
"msb" 'python-shell-send-buffer
Expand Down

0 comments on commit 3098a1a

Please sign in to comment.