Execute the expression and print its value in Emacs python mode - emacs

I'm using Emacs python-mode and ipython as my IDE to write the python code. Python-mode provides a nice function "py-execute-line" to run one line code, and I've bound it to key "F7".
So for the following code:
cell = "Human" # Move the cursor to this line and run it by pressing F7
print cell # Move the cursor to this line and run it by pressing F7
I can get the output printed in the ipython buffer.
In [80]:
In [81]: Human
I'm wondering whether there's more direct way to check the value of "cell" without the print command. Something like move the cursor to the variable, press some key, and then the value is printed in the ipython output buffer.
cell = "Human" # Move the cursor to this line and run it by pressing F7
cell = "Human" # Move the cursor to "cell" and print its value by pressing ?? key or function
I have tried function (py-execute-expression-ipython), but there is no output printed ...
Thanks in advance!

Without highlighting, I use:
(defun python-eval-expression ()
(interactive)
(let ((py-command (read-string "Command: ")))
(save-excursion
(setq elpy-shell-echo-output t)
(setq elpy-shell-echo-input t)
(elpy-shell--with-maybe-echo (python-shell-send-string py-command)))))
Should be easy to adapt to choose the current selection if exists (using

Maybe consider a feature request at
https://bugs.launchpad.net/python-mode

Not exactly what you want, but you can highlight a region (by pressing C-SPC to activate the mark and moving to the other side of the region) and then type M-| to run shell-command-on-region. Then simply type python RET to run Python on the region.
For example, select these two lines:
message = "Hello, Stack Overflow!"
print message
Then type M-| python RET. `Hello, Stack Overflow!" will appear as a message at the bottom of the screen. You can do this in any mode, with any shell command. See also: Shell Commands.

Related

Emacs - Open buffer list without replacing another buffer

I have one frame, one window.
I use Cx 3, I now have two windows.
I use Cx Cb in order to see the list of buffers, however it opens it in another window but doesn't put the focus on it. It is even more annoying if I had opened a buffer on the 2nd window.
I would prefer to either open the buffer list in the window which currently has the focus, or temporarily change the focus to the buffer list.
First of all I want to start by saying that the ibuffer function does similary to what you want and does so in the current window, its definitely worth checking out.
Now onto your actual question. C-x C-b by default calls the function list-buffers. If you search for that command using C-h f it will show you the documentation, from there you can view the source code for the function by clicking on the underlined text that says buff-menu.el.
Now we can view the source of the list-buffers, the first function called is display-buffer. That sounds promising. We can now use C-h f once again to search for the display-buffer command. Reading though this documentation we see that the variable display-buffer-alist dictates how the display-buffer and in turn how list-buffers works. We can then see that by adding ("*Buffer List*" . display-buffer-same-window) to display-buffer-alist you will get the desired result.
All in all you simply need to put (add-to-list 'display-buffer-alist '("*Buffer List*" . display-buffer-same-window)) in your init file for your changes to take place.
Please just try one of these:
open the buffer list who currently has the focus:
(defun my:list-buffers (&optional arg)
(interactive "P")
(display-buffer (list-buffers-no-select arg) '(display-buffer-same-window)))
change the focus
(defun my:list-buffers2 (&optional arg)
(interactive "P")
(select-window (list-buffers arg)))

"Press" a key from inside an interactive function

I need to programmatically press a key from inside of an interactive function. Here's an outline of what I have so far:
(defun answer-to-life-the-universe-and-everything ()
(interactive)
(insert "(* 6 7)")
;; Need to automagically press the RETURN key here
)
My use case: in a REPL buffer, I need frequently execute a long command. I can use the above code to create an interactive function that inserts the required string, but I still have to hit RETURN manually for the REPL to read it. Terminating the string with \n or \r won't do what I need it to.
How can I do this inside of my interactive function definition?
A simpler way to do this is to find out what command the enter key is bound to in the REPL and then call that command in your interactive function. (To find out, go to the REPL buffer and hit C-h k <return>.)
For example, enter is bound to inferior-ess-send-input when using the R REPL via ess, so this command inserts the string and "hits enter":
(defun try-this ()
(interactive)
(insert "print(\"hi\")")
(inferior-ess-send-input))

Opening a window into a specified buffer

How can I open a new window (for example using C-x 3) into a new buffer, rather than a mirrored buffer that just echoes what I type.
So for example, let's say I'm messing around with python and I want to run the script in the shell. As it is currently I do this: C-x 3, M-x shell and then start it up and running. I'd rather just C-x 3 and it automatically opens into shell. I'm really new to Emacs so I don't know where to look for this.
It sounds to me like this, or something similar, is what you are looking for:
(defun pop-to-buff-at-right (buffer)
"Pop to BUFFER at the right of the current window."
(interactive "B")
(pop-to-buffer buffer '(display-buffer-in-side-window
(side . right)
(inhibit-same-window . t))))
You do not want to just split the window, which is specifically about showing the same buffer twice. You want to switch to another buffer, but you want it to be displayed to the right of the current window.
In emacs it is easy to define custom commands and bind it to keys. For instance, if you add this to your init file:
(defun open-shell-at-left ()
(interactive) ;; Tell emacs this function can be called interactively
(split-window-right) ;; Just what C-x 3 does
(shell)) ;; Just what M-x shell does
(global-set-key (kbd "C-c 3") 'open-shell-at-left)
You will have what you want when you type C-c 3. In general, you can find documentation about what a key binding does by typing C-h k and the keybinding. From that point, it is easy to chain existing commands into new ones.

In Emacs, how do I select the completion list with the keyboard?

When I tab-complete in a minibuffer and Emacs displays a completion list in a new buffer, how do I switch to that buffer without using the mouse?
I tried C-x o, but that just switched to the first buffer, out of which I entered the minibuffer.
I also tried C-x b, but that gives me command attempted to use minibuffer while in minibuffer.
Lastly I tried C-x <C-right>, which gives me cannot switch buffers in minibuffer window.
EDIT: I spoke about minibuffer completion in my example, but being able to access the completion list (using the keyboard) from within a regular buffer is also important to me, and not working. The M-v shortcut was suggested, but it only seems to work inside the minibuffer accessed by M-x, in every other buffer I've tried, M-v is bound to scroll down command and does not switch to the completion list. I doesn't even seem to work in other minibuffers. For example, it doesn't work in the shell command minibuffer invoked by M-! either.
You can use M-v (documented here) which switches to the completion buffer and puts the cursor on the first completion:
Typing M-v, while in the minibuffer, selects the window showing the
completion list (switch-to-completions). This paves the way for using
the commands below. <PageUp> or <prior> does the same. You can also
select the window in other ways...
EDIT: It looks like based on your edit to the original question that what you're asking for is a way to switch to the completions buffer more globally. There is a function switch-to-completions that selects the completions list - you might consider binding that function to a key of your choosing, e.g.:
(define-key global-map (kbd "C-x t") 'switch-to-completions)
For example, such a binding allows me to switch to completions from "Shell command: " invoked by M-!, and places the cursor on the first possible completion.
You can use C-xo twice, or use M--C-xo, which switches to the previous buffer instead of the next one.

Emacs mini-buffer command with parameter

I'd like to use the command to resize split windows via the mini-buffer. In the GNU documentation I found the description (Resizing-Windows):
Example: enlarge-window-horizontally size &optional horizontal.
If I type M-x enlarge-window-horizontally the window will get resized by one column. But it is not possible to add a number for the size in the mini-buffer, as on pressing spacebar emacs tries to complete the command.
Does someone know how to use the optional parameters in mini-buffer? Respectively how to resize a window by more than one column at once.
Thanks.
Passing parameters to interactive command like this uses the universal argument.
You can enlarge the window by 10 columns by typing C-u 10 M-x enlarge-window-horizontally. You can change 10 to any integer. By the way, typing C-u num to supply a numeric argument works with all interactive emacs commands that expect an argument.
Note there is also a keyboard short cut: C-u 10 C-x }.
And to shrink the window: C-u 10 C-x {.
You can also specify numbers by typing holding down the meta key M-10 C-x {
What you are looking for is eval-expression.
M-: (enlarge-window-horizontally horizontal)
M-: will change the minibuffer to an eval prompt that lets you enter in a Lisp expression to be evaluated.