I'd like to be able to run a shell command on the current file that I'm editing and have the output be shown in the Shell Command Output window. I've been able to define the
function which is shown below.
(defun cpp-check ()
"Run cpp-check on current file the buffer is visiting."
(shell-command
(concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
(buffer-file-name))))
The only problem is that the output window isn't brought to the foreground in any way. What I'd like to happen is for the window to be split and the output window shown there. Also, am I on the right track here defining the function to be put in my .emacs file or is there a better way?
Any help would be appreciated. Thanks.
Take a look at the documentation for 'shell-command, this worked well for me:
(defun cpp-check ()
"Run cpp-check on current file the buffer is visiting."
(shell-command
(concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
(buffer-file-name))
"cpp-check"))
It creates a new buffer named "cpp-check" and puts the results there. The current frame is split in to, and the "cpp-check" buffer is put in the other window.
See the function `pop-to-buffer'. I think.
You should be able to give it a buffer name to pop to -- just give in the Shell Command Output buffer.
This is what I came up with. Thanks for the responses. I defined a function that will go ahead and run cpp-check. I wanted it bound to a key in c-mode so I add it as a hook. I ran into the difference between normal functions and ones that can be bound to keymaps so I had to make the function interactive. This article helped explain that. So now when the shortcut is pressed the results come up in another window, but the cursor remains in the original buffer, which is what I want. The only problem is that the output is shown in the minibuffer as well which isn't quite what I want. Any thoughts on fixing that little detail?
(defun cpp-check ()
(interactive)
"Run cpp-check on current file the buffer is visiting."
(shell-command
(concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
(buffer-file-name)))
(display-buffer "*Shell Command Output*"))
(add-hook 'c-mode-common-hook
(lambda ()
(define-key c-mode-base-map
"\C-x\p" 'cpp-check)))
splitting the window is (split-window-vertically) It has an optional arg of the size of the (top if positive, bottom if negative) part of the window.
Then, and you need to do is bring the shell results buffer to the front with switch-to-buffer or switch-to-buffer-other-window.
Remember that when you spit the window (frame) in emacs, you end up with two "windows" because of a naming confusing back in the day that it's too late to deal with now...
Related
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)))
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.
I have got a question about config of emacs startup behavior. I don't have any idea about lisp, I simply love emacs for editing.
What I would like to have is the following:
I call emacs like emacs FILE
emacs opens a window entitled FILE
emacs splits the window horizontally
in the upper frame it opens a file FILE.abc
goes to the bottom frame and it opens FILE.xyz
optimally comes back to the upper frame
I had a look here:
how to create a specific window setup on in .emacs
and it's half way through. However, the macro thing doesn't really work in my case because I need to pass an argument. Any help greatly appreciated.
In general, you will find yourself better served by Emacs if you stay in it just "visit" (as Emacs has been - for 40 years - calling the operation elsewhere known - for 30 years - as "open") files.
Here is the function (untested):
(defun open-two-files (file)
"Open FILE.abc in the top window and FILE.xyz in the bottom window.
http://stackoverflow.com/questions/15070481/specifying-emacs-startup-behavior"
(interactive "FEnter file base name: ")
(let ((old-window (selected-window))
(new-window (split-window-below)))
(find-file (concat file ".abc"))
(select-window new-window)
(find-file (concat file ".xyz"))
(select-window old-window)))
You need to put it into your ~/.emacs.el.
Now, if you have emacs already opened, you need to do M-x open-two-files RET foo RET to open foo.abc and foo.xyz.
If you want to start a new Emacs session, type emacs --eval '(open-two-files "foo")'
If I run M-x shell in emacs to get a terminal, it knows where to wrap lines automatically. For example, the output of ls is formatted into columns that fit the window properly.
My problem is if I then split the window vertically with C-x 3, shell-mode still thinks the window fills the whole frame. The result is ugly wrapping of command output. Is there a way to let shell-mode know it has to update the screen width?
EDIT:
Using HN's answer below, I came up with this fix:
(defun my-resize-window ()
"Reset the COLUMNS environment variable to the current width of the window."
(interactive)
(let ((proc (get-buffer-process (current-buffer)))
(str (format "export COLUMNS=%s" (window-width))))
(funcall comint-input-sender proc str)))
(defun my-shell-mode-hook ()
(local-set-key "\C-cw" 'my-resize-window))
I'm a bit late to the party, but COLUMNS isn't the way to do this. Here is an excerpt from my .emacs:
(defun comint-fix-window-size ()
"Change process window size."
(when (derived-mode-p 'comint-mode)
(set-process-window-size (get-buffer-process (current-buffer))
(window-height)
(window-width))))
(defun my-shell-mode-hook ()
;; add this hook as buffer local, so it runs once per window.
(add-hook 'window-configuration-change-hook 'comint-fix-window-size nil t))
(add-hook 'shell-mode-hook 'my-shell-mode-hook)
Unlike exporting COLUMNS each time, this method doesn't require you to be in
bash at the moment and it doesn't spam your session with blank prompts. This
code should probably be in comint itself, maybe I will file a bug report.
EDIT: If you modify window-configuration-change-hook in a buffer-local-way
the hook runs once per window, as opposed once per frame.
Here is a slightly improved resize function from #Christopher Monsanto's answer. The original one will cause a problem due to nil process. (e.g exit in shell-mode)
(defun comint-fix-window-size ()
"Change process window size."
(when (derived-mode-p 'comint-mode)
(let ((process (get-buffer-process (current-buffer))))
(unless (eq nil process)
(set-process-window-size process (window-height) (window-width))))))
This display is dictated by the COLUMNS environment variable. In my setup COLUMNS has a value of 202, after a vertical split ls displays correctly on shell-mode if I set columns to 80 via
export COLUMNS=80
There must be a way to code this up but I don't have enough elisp-fu to do that. If you'd rather avoid the hassle multi-term manages this automagically.
http://www.emacswiki.org/emacs/MultiTerm
Try M-x eshell; it doesn't have this problem.
As an Emacs beginner, I am working on writing a minor mode. My current (naive) method of programming elisp consists of making a change, closing out Emacs, restarting Emacs, and observing the change. How can I streamline this process? Is there a command to refresh everything?
You might try using M-C-x (eval-defun), which will re-evaluate the top-level form around point. Unlike M-x eval-buffer or C-x C-e (exal-last-sexp), this will reset variables declared with defvar and defcustom to their initial values, which might be what's tripping you up.
Also try out C-u C-M-x which evaluates the definition at point and sets a breakpoint there, so you get dropped into the debugger when you hit that function.
M-x ielm is also very useful as a more feature-rich Lisp REPL when developing Emacs code.
M-x eval-buffer should do it.
What Sean said. In addition, I have (eval-defun) bound to a key, along with a test. The development loop then becomes: 1) edit function, 2) press eval-and-test key, 3) observe results, 4) repeat. This is extremely fast.
During development I write a test, bind it to jmc-test, then use the above key to run it on my just-edited function. I edit more, then press key again, testing it again. When the function works, I zap jmc-test, edit another function, and write another jmc-test function. They're nearly always one line of code, so easy to just bang out.
(defun jmc-eval-and-test ()
(interactive)
(eval-defun nil)
(jmc-test))
(define-key emacs-lisp-mode-map (kbd "<kp-enter>") 'jmc-eval-and-test)
(when t
(defun myfunc (beer yum)
(+ beer yum))
(defun jmc-test () (message "out: %s" (myfunc 1 2))))
When editing "myfunc", if I hit keypad enter, it prints "out: 3".
It all depends on what you're writing and how you've written it. Toggling the mode should get you the new behavior. If you're using [define-minor-mode][1], you can add code in the body of the macro that keys off the mode variable:
(define-minor-mode my-minor-mode
"doc string"
nil
""
nil
(if my-minor-mode
(progn
;; do something when minor mode is on
)
;; do something when minor mode is off
)
But, another way to check it quickly would be to spawn a new Emacs from your existing one:
M-x shell-command emacs&
I just define a function called ldf (short for load-file) in my .emacs file,
like this:
(defun ldf (arg) (interactive "P") (load-file (buffer-file-name)))
As you can see, this little function looks up the filename of the current buffer and then loads the file. Whenever I need to reload the current buffer elisp file, just type "M-x ldf"