-1

I split the emacs into 2 windows, one window is used to edit the python code, say test.py, and another shell window to run shell command "python test.py", so each time after I edit the python, I need to 1: save the buffer 2: switch from python window to shell window 3: run the last command in shell window 4: switch back to python window to edit again

That's boring, so I used the following code to automate the process,

  (defun run-shell-command-again ()
(interactive)
(save-buffer (buffer-name))
(other-window)
(comint-previous-input)
(other-window))

But when I run the command using M-x run-shell-command-again in the python window, it throws wierd message as below:

run-shell-command-again: Wrong number of arguments: #[(count &optional all-frames)

  • please fix formatting of your function and error message – sds Dec 21 '16 at 14:12
1

The other-window function takes an argument COUNT.

(other-window COUNT &optional ALL-FRAMES)

Select another window in cyclic ordering of windows.
COUNT specifies the number of windows to skip, starting with the
selected window, before making the selection.  If COUNT is
positive, skip COUNT windows forwards.  If COUNT is negative,
skip -COUNT windows backwards.  COUNT zero means do not skip any
window, so select the selected window.

So you would call other window like so: (other-window 1) to get to the next window and (other-window -1) to get to the previous.

You will also have to add an argument for the comint-previous-input since you are not calling it interactively. You will also need to send the previous-input to the shell with comint-send-input. Something like this works for me.

(defun run-shell-command-again ()
  (interactive)
  (save-buffer (buffer-name))
  (other-window 1)
  (comint-previous-input 1)
  (comint-send-input)
  (other-window -1))

But as @sds said there are some better (built-in) function in emacs that can take care of this for you!

1

Your question

If you insist on playing with your function, you should

  • start with setting debug-on-error to t and examining the stack trace,
  • then do C-h f on all your functions
  • then avoid interactive commands in your function, prefer lower level function

However, you are wasting your time.

The Right Way

  • Use Python REPL to debug each function in your file separately.
  • To execute your whole file, do C-u M-x compile RET python your-file-name.py RET.

To make it shorter, do

(define-key mode-specific-map "c" 'compile)

And then C-u C-c c python your-file-name.py RET (first time) after which all you need to do to run your file is C-c c.

  • when I do what you suggested, C-c c, it show me the correct command to compile the python file, but it requires me to press Enter to run the command, anyway to just run the command instead of waiting to press enter? – Daniel Wu Dec 22 '16 at 2:13
  • @DanielWu: set compilation-read-command to nil – sds Dec 22 '16 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.