I've written some functions attached to my F5 and F6 keys so that when pressed, these evaluate the buffer contents for perl or python.
;; F5 = perlevaluatie
(defun perl-eval ()
"Run whole buffer as Perl code"
(interactive)
(shell-command-on-region (point-min) (point-max) "perl") ; feeds the region to perl on STDIN
)
(global-set-key (kbd "<f5>") 'perl-eval)
;; F6 = pythonevaluatie
(defun python-eval ()
"Run whole buffer as Python code"
(interactive)
(shell-command-on-region (point-min) (point-max) "python")
)
(global-set-key (kbd "<f6>") 'python-eval)
However, when I use these functions for a script that keeps running for long times, emacs hangs. Appending an & does not help as with the shell-command function. Does anybody know a way to make the shell-command-on-region asynchronous?
Thanks in advance,
Marten
I use compile for this: M-x compile, perl <file-name>. After the first run you can use recompile to rerun it.
Slightly more time to type the file's name but you gain a lot of nice stuff, and it's asynchronous of course.
Edit: Bonus helper function to automatically use compile the first time then recompile:
(defun my-recompile ()
"Recompile if possible, otherwise compile current buffer."
(interactive)
;; If recompile exists do it, else compile
(if (fboundp 'recompile) (recompile)
(compile "make -k")))
and in the appropriate hook bind something like
(local-set-key (kbd "<f5>") 'my-recompile)
(local-set-key (kbd "<C-f5>") 'compile)
Related
For example, I use the command local-set-key to set the key C-c C-n for flymake-goto-next-error in python-mode.
Instead of writing an expression and wrapping it into python-mode-hook, is there a convenient way to store this keybinding directly? Does anyone have ideas about this?
Command
(local-set-key (kbd "C-c C-n") 'flymake-goto-next-error)
works in the current buffer's local map. It is correct when local map is python-mode-map.
Convenient minimum lenght command (without using hook)
(define-key python-mode-map (kbd "C-c C-n") 'flymake-goto-next-error)
works when variable python-mode-map has been created.
Variable python-mode-map is created dynamically (in file python-mode.el) after call
(require 'python-mode)
If python-mode hasn't been loaded, command define-key can't directly use python-mode-map. Using hooks is for safety. Your emacs config should be reliable (it shouldn't depend on command's execution order if possible), and such wrapped (with hooks) commands prevent wrong situation: setting keybind in mode-map without setup mode.
When I am debugging code in Emacs, I like to have two buffers open, the first one with the source code I am debugging, and the second one with the debugger (pdb for Python).
I have the following keyboard shortcuts defined on my .emacs file:
(require 'gud)
(define-key gud-mode-map '[C-f10] 'gud-next)
(define-key gud-mode-map '[C-f11] 'gud-step)
(define-key gud-mode-map '[C-f5] 'gud-cont)
(define-key gud-mode-map '[C-f12] 'gud-break)
With the above, I can trigger the GUD shortcuts for gud-next, gud-step, etc. from the buffer where pdb is running, but I can't trigger them from the buffer that has the python code.
I would like to use keyboard shortcuts on the buffer with the source code to trigger GUD commands for the debugger. Is there any way to do this?
I am using the most recent version of python-mode (6.0.4) and Emacs 23.3.1.
Try using global-set-key instead:
(global-set-key [C-f10] 'gud-next)
(global-set-key [C-f11] 'gud-step)
(global-set-key [C-f5] 'gud-cont)
(global-set-key [C-f12] 'gud-break)
IIRC, this worked for me.
I am a fairly proficient vim user, but friends of mine told me so much good stuff about emacs that I decided to give it a try -- especially after finding about the aptly-named evil mode...
Anyways, I am currently working on a python script that requires user input (a subclass of cmd.Cmd). In vim, if I wanted to try it, I could simply do :!python % and then could interact with my script, until it quits. In emacs, I tried M-! python script.py, which would indeed run the script in a separate buffer, but then RETURNs seems not to be sent back to the script, but are caught by the emacs buffer instead. I also tried to have a look at python-mode's C-c C-c, but this runs the script in some temporary directory, whereas I just want to run it in (pwd).
So, is there any canonical way of doing that?
I don't know about canonical, but if I needed to interact with a script I'd do M-xshellRET and run the script from there.
There's also M-xterminal-emulator for more serious terminal emulation, not just shell stuff.
I like to use the Emacs "compile" command to test/run my python scripts. M-XcompileRET will pull up the default "make -k" but if you delete that and put in the command line for your script (including options), subsequent "compiles" will provide the new "compile" command automatically. All the output from your script will appear in the compile buffer. (As opposed to the shell, this provides a nice clean buffer each time it is invoked. Good for searching and such. If you forget to save your script before your run, compile will ask you if you would like to save the file.)
You will lose your the command line when you restart Emacs. But you can get Emacs to set the compile-command for the buffer holding your script by putting at the bottom of the python script this sort of code (actually a python comment):
# Trigger emacs to run this script using the "compile" command
# ;;; Local Variables: ***
# ;;; compile-command: "my_cool_script.py --complicated_option some_filename.txt" ***
# ;;; end: ***
This is handy for scripts with complicated invocations.
Note: The python comment character '#' protects this from the python interpreter while Emacs knows to set these variables because it looks at the bottom of every file when it opens them.
I'd love to be able to jump to 'compile errors' in my python script the way the compile command does when you use it for compiling C code but I'm too lazy to create the Emacs regular expression to make this work. Perhaps that would make another great question for stack overflow!
I currently use these hook to define my compilation commands:
(defun convert-filename-to-executable (file)
(if (eq system-type 'windows-nt)
(concat (file-name-sans-extension file) ".exe")
;; linux
(concat "./" (file-name-sans-extension file))))
(add-hook 'c++-mode-hook
(lambda ()
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let* ((file (file-name-nondirectory buffer- file-name))
(executable (convert-filename-to-executable file)))
(concat "g++ -g -Wall -o "
(file-name-sans-extension file)
" "
file
" && "
executable))))))
(add-hook 'c-mode-hook
(lambda ()
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let* ((file (file-name-nondirectory buffer-file-name))
(executable (convert-filename-to-executable file)))
(concat "gcc -g -ansi -Wall -Wpedantic -Wextra -Wc++-compat -Wconversion -o "
(file-name-sans-extension file)
" "
file
" && "
executable))))))
(add-hook 'python-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "python " buffer-file-name))))
(add-hook 'perl-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "python " buffer-file-name))))
Together with this lambda set to call the compile function interactively:
(global-set-key (kbd "<f4>") (lambda () (interactive) (setq current-prefix-arg '(4)) (call-interactively 'compile)))
One button to rule them all!
If you press F4 (in my case, you can set the key yourself in the lambda for global-set-key), then a file opened in C++ or C mode will be compiled and a file in python or perl mode will be run (interactively)
The I think ansi-term has the most faithful emulation of a terminal. But I don't see a way to pass arguments to the process. You can of course just launch it from a shell inside the ansi-term buffer.
But I think the best thing to do is to not use python-send-buffer, but instead to use a new function which does it "right", that is by sending the path to the current file instead of making a temp file. There are some slight differences of course in that you have to save the current file first, but the following should at least get you on the right track.
(defun python-send-file ()
(interactive)
(save-buffer)
(python-send-string (concat "execfile('" (buffer-file-name) "')")))
;; This overwrites the `python-send-buffer' binding so you may want to pick another key
(eval-after-load "python"
(define-key python-mode-map "\C-c\C-c" 'python-send-file))
I checked and this allows you to interact. To get tabs you have a few options.
C-qTAB will always give you a literal tab
You can rebind tab to be a literal tab in inferior-python-mode-map:
(define-key inferior-python-mode-map "\C-i" 'self-insert-command)
I'm sure there are others that I can't think of
If you use C-c C-c a buffer is created (look for inferior-python). Try changing to that buffer*, every time you hit C-c C-c the result is shown there, you need to see that buffer to get the results. Use C-x 2 so you can see both buffers at the same time.
Also try C-c C-z (switch to shell).
*I use Ibuffer to manage buffers, is very good.
(btw, this http://tuhdo.github.io/index.html is an excelent place to learn some emacs)
EDIT: Have you tried C-c C-p ?
Yet another option:
Using C-c C-c works nicely with fgallina's python.el -- pwd will be the location of the buffer's file.
I apologize if I use the incorrect terminology here, I've only been using emacs for a few months.
I just reinstalled Aquamacs on a macbook that I reformatted and am having the oddest problem.
I open a .py file, and use C-c ! to open a python shell. So I have (as expected), the .py file in the top window and the python shell in the bottom window.
If I then run C-c C-c (py-execute-buffer) in the .py file, the two windows swap positions. I mean, the .py file buffer opens in the bottom window in a new buffer, while the python shell opens in the top window in a new buffer. So basically, they swap positions. Repeatedly using C-c C-c swaps the windows back again... so they're shuffling positions. Also, both windows (top and bottom) have both buffers (.py file and python shell) in tabs.
I've not made any modifications to the default settings yet, and I've gotten the problem with both 2.3a and 2.3 (2.3 was on the machine previously and didn't have this problem, so I tried rolling back... to no avail).
Does anyone know how to stop this behavior? Thanks in advance!
Add the following to your Emacs init file in Aquamacs to prevent it from swapping the buffers around:
(defadvice py-execute-buffer
(around keep-buffers-same activate)
"Don't swap buffers in Aquamacs."
(save-window-excursion
ad-do-it))
You can also try adding the following to your emacs init file:
(setq py-split-windows-on-execute-p nil)
This will prevent the current window from splitting after you run any py-execute-*. (This also means that the python shell won't show up if it isn't already in one of your windows.)
i don't use Aquamacs and couldn't reproduce your described behaviour, however, try this code to toggle either of the windows as 'dedicated'. locking windows to buffers was the first thing i wanted to do when getting up and running with emacs. maybe this will help you here.
add the code to your '.emacs', then either 'mark' (select) the region 'S-< key-down >' and then 'M-x eval-region' to evaluate it ..or save and restart emacs.
(global-set-key [pause] 'window-dedication-toggle)
(defun window-dedication-toggle (&optional window force quiet)
"toggle or ensure the 'dedicated' state for a window"
(interactive)
(let* ((toggle t) (window (if window window (selected-window))) (dedicated (window-dedicated-p window)))
(cond ((equal force "on") (setq toggle (eq dedicated nil)))
((equal force "off") (setq toggle (eq dedicated t))))
(if toggle (progn
(setq dedicated (not dedicated))
(set-window-dedicated-p window dedicated)
(if (not quiet)
(message "window %sdedicated to %s" (if (not dedicated) "no longer " "") (buffer-name)))))))
I use python debugger pdb. I use emacs for python programming. I use python-mode.el. My idea is to make emacs intuitive. So I need the following help for python programs (.py)
Whenever I press 'F9' key, the emacs should put "import pdb; pdb.set_trace();" statements in the current line and move the current line to one line below.
Sentence to be in same line. smart indentation may help very much.
Wherever "import pdb; pdb.set_trace();" statement presents in the python code, emacs should display left indicator and highlight that line.
When I press 'Alt-F9' keys at the current line and emacs found the "import pdb; pdb.set_trace();" statement then, emacs should remove the "import pdb; pdb.set_trace();" line and move the current line to one up.
Whenever I press "F8" key, emacs to jump to "import pdb; pdb.set_trace();" in the same buffer.
I am trying to learn elisp and catch up lisp soon to customize emacs myself. I will appreciate your answers.
The answer shall be great enough for me and others who find this solution is very useful.
to do 1)
(defun add-py-debug ()
"add debug code and move line down"
(interactive)
(move-beginning-of-line 1)
(insert "import pdb; pdb.set_trace();\n"))
(local-set-key (kbd "<f9>") 'add-py-debug)
to do 2) you probably have to change the syntax highlighting of the python mode, or write you own minor mode. You'd have to look into font-lock to get more. Sorry.
to do 3) though I've set this to be C-c F9 instead of Alt-F9
(defun remove-py-debug ()
"remove py debug code, if found"
(interactive)
(let ((x (line-number-at-pos))
(cur (point)))
(search-forward-regexp "^[ ]*import pdb; pdb.set_trace();")
(if (= x (line-number-at-pos))
(let ()
(move-beginning-of-line 1)
(kill-line 1)
(move-beginning-of-line 1))
(goto-char cur))))
(local-set-key (kbd "C c <f9>") 'remove-py-debug)
and to do 4)
(local-set-key (kbd "<f3>") '(lambda ()
(interactive)
(search-forward-regexp "^[ ]*import pdb; pdb.set_trace();")
(move-beginning-of-line 1)))
Note, this is not the best elisp code in the world, but I've tried to make it clear to you what's going on rather than make it totally idiomatic. The GNU Elsip book is a great place to start if you want to do more with elisp.
HTH
I've found that Xah's Elisp Tutorial is an excellent starting point in figuring out the basics of Emacs Lisp programming. There are also some SteveY articles from a while ago that go through techniques you might find useful for learning the basics.
If you're serious about making an amended Python mode, you'll do well to take a look at Writing GNU Emacs Extensions, which is available as a PDF.
Finally, the most useful resource for me is actually Emacs itself. I make frequent use of M-x apropos and M-x describe-key to figure out how built-in functions work, and whether there's something already in place to do what I want.
The specific things you want to look like they can be done through some simple use of insert, and a few search/replace functions, so that'll be a good starting point.