How can I set up two, parallel buffers in emacs to edit Python files in one and execute in an IPython shell in the other? - emacs

I'm trying to setup ipython.el in emacs23. I've successfully installed it (after putting python-mode.el in my load-path to supplant python.el which comes pre-installed with emacs). And I can even get it to run via M-x py-shell, etc.
The interface seems to be pretty poorly setup, and I was wondering if I was doing it wrong, or if I need to customize it to make it work the way I'd like.
In short, the workflow I'd like to have:
in one or more buffers, edit Python code
When I hit C-c C-c in that buffer, either execute the Python code in that buffer in the open IPython shell buffer (if there is one) or open up another buffer to do that.
But what happens right now is:
With the IPython shell in one buffer and the Python file in the other, if I hit C-c C-c in the Python file buffer, the file buffer switches to the IPython buffer (meaning I now have two, duplicated iPython buffers) and the file is executed.
This is annoying.
I'm pretty new to elisp, but my understanding of defadvice is that I could advise around python-execute-buffer to take note of the existing file buffer, run python-execute-buffer, and then switch back to the original file buffer as a workaround.
This seems pretty silly. Any suggestions for better ways to accomplish this would be appreciated!
If it matters: I'm on OS X 10.6.8 with IPython 0.10.1 running Emacs 24.0.50.
Thanks in advance!

Turns out that simply installing python-mode.el and anything-ipython.el and putting
(require 'python-mode)
(require 'ipython)
(require 'anything-ipython)
(add-hook 'python-mode-hook #'(lambda ()
(define-key py-mode-map (kbd "C-<tab>") 'anything-ipython-complete)))
(add-hook 'ipython-shell-hook #'(lambda ()
(define-key py-mode-map (kbd "C-<tab>")
'anything-ipython-complete)))
in my .emacs made everything work just how I wanted if py-shell is executed before py-execute-buffer (so that C-c C-c) executes the code in the shell instead of just opening up the *Python Output* buffer.

Related

perltidy-mode autorun in Emacs

I have a perltidy-mode.el lisp file, which is being loaded - I can call it manually by M-x perltidy-mode.
What would be the proper way to run it automatically after a file is opened (or emacs is loaded)?
(defalias 'perl-mode 'cperl-mode)
(defalias 'perl-mode 'perltidy-mode)
doesn't seem to work.
It seems I've forgotten a lot of lisp/emacs
If you would like to activate it for every opened perl file, you can add it to a hook:
(defun my-perl-hook ()
(perltidy-mode 1))
(add-hook 'perl-mode-hook 'my-perl-hook)
Note: I know nothing about this mode, or about the different perl modes, so you might to add your function to other hooks as well.

running scheme from emacs

I'm a newbie to LISP.
I am attempting to invoke the scheme interpreter from within emacs (version 23 running on windows). I loaded the xscheme library by telling emacs to M-x load-library and then entering xscheme at the prompt in the minibuffer. The library loaded, and then I issued the M-x run-scheme command. (I realize that all this loading can be done from .emacs at startup, but I am not concerned with that at the moment.)
So far so good - the *scheme* buffer has been created, and now I'm hoping that I'm able to talk to the scheme interpreter.
However, when I try to evaluate something in that *scheme*buffer (e.g. (define shoe-size 14)), I get this Output file descriptor of scheme is closed message in the minibuffer.
Does anybody know how to fix this in emacs?
(Also, how does one set the major-mode as REPL in the *scheme* buffer?)
Thank you.
Try setting the scheme-program-name variable to the path to your Scheme interpreter. The major-mode of the scheme buffer is probably just comint and you cannot do much about it unless you switch to something more capable like Geiser - something that I'd recommend you do.
Add this line to your .emacs file:
(setq scheme-program-name "gsi")
(Replace "gsi" with the name of your Scheme interpreter.)
You can then start the interpreter with M-x run-scheme. You can evaluate pieces of code by using C-x C-e (to evaluate the sexp before the point) or with C-M-x to evaluate the sexp you're in right now. You can also load a file with C-c C-l.
I'll start by saying that I'm very new to programming, scheme and SICP, but I'm trying to work through the course and watch the lectures that are online.
I'm running emacs on Ubuntu 12.10 and I really wanted to get MIT scheme working in emacs instead of relying on Edwin.
The above tips didn't work for me, but here's the step-by-step instructions that did work:
Install emacs 24 from the Ubuntu Software Center (search "emacs" and install it!)
Open a terminal (ALT + CTRL + t)
Go to your home directory (cd ~)
Open the hidden file .emacs in gedit (gedit .emacs)
On the first line of the file, type exactly what's after the colon: (require 'xscheme)
Save the changes to .emacs
That's it!!!
You can now open .scm files in emacs and use commands like C-x C-e.
*directions courtesy of http://alexott.net/en/writings/emacs-devenv/EmacsScheme.html#sec14
My guess is that it's just a known issue I still dunno how to sort that out (it's out of my current skills) but I got a macro that probably helps: just after writing the s-exp you can do Cc-Cz (it calls the geiser REPL) then C-spc, C-M-b, M-w, C-x-o, C-y and RET.
There are a variation (same, placed just after writing the s-exp): C-spc, C-M-b, M-w, C-c Cz, C-y and RET

How to Reload files upon save when using swank+leiningen+emacs

I'm looking to set up slime+lein-swank to reload source files referenced from the repl when i save the file. currently i do this:
edit file
save file
switch to repl
(use :reload-all 'com.package.namespace)
test stuff
I want to not have to remember to do step 4.
You can use SLIME's C-c C-k before switching to the REPL, for slime-compile-and-load-file. It will prompt you to save the file if you haven't already. When it's done, the things which you've redefined should be available at the SLIME REPL in their new versions. Then you could use C-c C-z to bring up the REPL (close it with C-x 0 when you don't need it anymore).
Setup a hook in .emacs:
(defun clojure-slime-maybe-compile-and-load-file ()
"Call function `slime-compile-and-load-file' if current buffer is connected to a swank server.
Meant to be used in `after-save-hook'."
(when (and (eq major-mode 'clojure-mode) (slime-connected-p))
(slime-compile-and-load-file)))
(add-hook 'after-save-hook 'clojure-slime-maybe-compile-and-load-file)
Like the previous answer I use those same keystrokes but record them into a macro and bind it to a key. That way it's just one keypress to save, compile and switch to the REPL. It ends up looking something like this:
(fset 'compile-and-goto-repl
"\C-x\C-s\C-c\C-k\C-c\C-z")
(global-set-key [f6] 'compile-and-goto-repl)

How can I reload .emacs after changing it?

How can I get Emacs to reload all my definitions that I have updated in .emacs without restarting Emacs?
You can use the command load-file (M-x load-file, and then press Return twice to accept the default filename, which is the current file being edited).
You can also just move the point to the end of any sexp and press C-x, C-e to execute just that sexp. Usually it's not necessary to reload the whole file if you're just changing a line or two.
There is the very convenient
M-x eval-buffer
It immediately evaluates all code in the buffer. It's the quickest method if your .emacs file is idempotent.
You can usually just re-evaluate the changed region. Mark the region of ~/.emacs that you've changed, and then use M-x eval-region RET. This is often safer than re-evaluating the entire file since it's easy to write a .emacs file that doesn't work quite right after being loaded twice.
If you've got your .emacs file open in the currently active buffer:
M-x eval-buffer
Solution
M-: (load user-init-file)
Notes
you type it in Eval: prompt (including the parentheses)
user-init-file is a variable holding the ~/.emacs value (pointing to the configuration file path) by default
(load) is shorter, older, and non-interactive version of (load-file); it is not an Emacs command (to be typed in M-x), but a mere Elisp function
Conclusion
M-: > M-x
M-x load-file
~/.emacs
Others already answered your question as stated, but I find that I usually want to execute the lines that I just wrote.
For that, Ctrl + Alt + X in the Elisp part works just fine.
The following should do it...
M-x load-file
I suggest that you don't do this, initially. Instead, start a new Emacs session and test whatever changes you made to see if they work correctly. The reason to do it this way is to avoid leaving you in a state where you have an inoperable .emacs file, which fails to load or fails to load cleanly. If you do all of your editing in the original session, and all of your testing in a new session, you'll always have something reliable to comment out offending code.
When you are finally happy with your changes, then go ahead and use one of the other answers to reload. My personal preference is to eval just the section you've added/changed, and to do that just highlight the region of added/changed code and call M-x eval-region. Doing that minimizes the code that's evaluated, minimizing any unintentional side-effects, as luapyad points out.
Keyboard shortcut:
(defun reload-init-file ()
(interactive)
(load-file user-init-file))
(global-set-key (kbd "C-c C-l") 'reload-init-file) ; Reload .emacs file
C-x C-e ;; current line
M-x eval-region ;; region
M-x eval-buffer ;; whole buffer
M-x load-file ~/.emacs.d/init.el
Define it in your init file and call by M-x reload-user-init-file
(defun reload-user-init-file()
(interactive)
(load-file user-init-file))
I'm currently on Ubuntu 15.04 (Vivid Vervet); I like to define a key for this.
[M-insert] translates to Alt + Ins on my keyboard.
Put this in your .emacs file:
(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))
Besides commands like M-x eval-buffer or M-x load-file, you can restart a fresh Emacs instance from the command line:
emacs -q --load "init.el"
Usage example: Company backends in GNU Emacs
Here is a quick and easy way to quick test your config. You can also use C-x C-e at the end of specific lisp to execute certain function individually.
C-x C-e runs the command eval-last-sexp (found in global-map), which
is an interactive compiled Lisp function.
It is bound to C-x C-e.
(eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL)
Evaluate sexp before point; print value in the echo area.
Interactively, with prefix argument, print output into current buffer.
Normally, this function truncates long output according to the value
of the variables ‘eval-expression-print-length’ and
‘eval-expression-print-level’. With a prefix argument of zero,
however, there is no such truncation. Such a prefix argument also
causes integers to be printed in several additional formats (octal,
hexadecimal, and character).
If ‘eval-expression-debug-on-error’ is non-nil, which is the default,
this command arranges for all errors to enter the debugger.
Although M-x eval-buffer will work, you may run into problems with toggles and other similar things. A better approach might be to "mark" or highlight what’s new in your .emacs file (or even scratch buffer if you're just messing around) and then M-x eval-region.
You can set a key binding for Emacs like this:
;; Reload Emacs configuration
(defun reload-init-file ()
(interactive)
(load-file "~/.emacs"))
(global-set-key (kbd "C-c r") 'reload-init-file)
If you happen to have a shell opened inside Emacs, you can also do:
. ~/.emacs
It may save a few key strokes.

How to do use C-x k to kill an Emacs buffer opened in server mode?

I use windows batch file to open files in an already-running instance of Emacs using emacsclientw.exe. However, any file opened that way is opened in server mode, which means that I have to use C-x # to kill it, instead of the usual C-x k. How do I change this behavior?
My solution was to rebind it (well M-w actually) to:
(lambda ()
(interactive)
(if server-buffer-clients
(server-edit)
(kill-this-buffer)))
[edit: having now read the code for server-edit, it might be better to use server-done (depending on what you want). server-edit will switch you to a server-edited buffer (if any still exist) but server-done will simply switch you to the next buffer. You could also use the output of server-done to see if the buffer was actually killed (not the case if the file was open before invoking emacsclient) and then kill it if not. Or use server-kill-buffer as suggested elsewhere.]
Here is what i put in my .emacs to do this :
(add-hook 'server-switch-hook
(lambda ()
(local-set-key (kbd "C-x k") '(lambda ()
(interactive)
(if server-buffer-clients
(server-edit)
(ido-kill-buffer))))))
Like this C-x k work the usual way when i'm not finding a file from emacsclient (which for me is ido-kill-buffer), and if i'm using emacsclient, C-x k does server-edit if the client is waiting, or run ido-kill-buffer otherwise (if i used emacsclient -n).
use:
D:\> emacsclientw -n foo.txt
the -n says "no wait". This is in GNU Emacs 22.2.1 (i386-mingw-nt5.1.2600) of 2008-03-26 on RELEASE (and many prior versions, IIRC).
You know, I hate to suggest workarounds instead of a real solution... but after reading the server code, I am confused as to how emacs even determines that a buffer is a server buffer.
With that in mind, why not open up files like:
emacsclient --eval '(find-file "/path/to/file")'
This way emacs doesn't know that your buffer was opened via emacsclient, which sounds like what you really want.
Edit:
I'm not really happy with this, but it seems to work:
(global-set-key (kbd "C-x k") (lambda () (interactive) (server-kill-buffer (current-buffer))))
Okay, THIS did work for me:
(global-set-key (kbd "C-x k") '(lambda ()
(interactive)
(if server-buffer-clients
(server-done)
(kill-this-buffer))))
(this is the code from IvanAndrus's answer with the explicit changes from edits and comments, and using jrockway's keybinding.)
And, yes -- I am rebinding a standard keybinding. BUT it's a functional replacement NOT something completely different (eg, replacing kill-ring-save with kill-buffer stuff).
BTW, EmacsWiki has a couple of pages on this topic-- KillKey and KillingBuffer -- neither of which shed better light for me than the above (although the first-function on KillKey also used "server-edit"...).
I am not sure if that will work on windows, but on linux, emacs -daemon is just great.
With it, you don't have to run a different program, and your bindings are the same. I guess there are other advantages, but since I could never learn those emacsclient bindings, I never really used it, and can't say.
I don't think -daemon had been released yet, I am using 23.0.60.1 from CVS.