specifying emacs startup behavior - emacs

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")'

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)))

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.

Opening file from filemanager as a full window

When opening a file in emacs using Window Explorer or GNOME Nautilus File, the file opened in split view and the bottom window contain the emacs welcome screen.
Is there any way that the file opens as a single window and remaining buffer like *scratch", message etc remains open but hidden.
Here's how to do it for Ubuntu:
Write to file /usr/local/share/applications/emacsclient.desktop:
[Desktop Entry]
Name=Emacsclient
GenericName=Text Editor
Comment=View and edit files
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Exec=/usr/local/bin/emacsclient %F
Icon=emacs
Type=Application
Terminal=false
Categories=Development;TextEditor;
StartupWMClass=Emacs
Just make sure that emacsclient is indeed located in /usr/local/bin/emacsclient
(you can use which emacsclient in bash to see this).
Then execute from bash:
sudo update-desktop-database
Finally, add to your ~/.emacs:
(require 'server)
(or (server-running-p) (server-start))
After this, once an Emacs instance is running, clicking on a file in Nautilus
will open it in the current window of Emacs, without changing the window configuration.
If you want that behaviour permanently you can use:
(add-hook 'find-file-hook 'delete-other-windows)
in your emacs-initialization.
In general, the function display-buffer (or its related family of functions, e.g., pop-to-buffer) defaults to a split-window if no other settings control the window / frame selection. If you have further problems, one of your initial buffer choices is likely using something like that -- or you have a display-buffer-alist setting which is causing it.
Using (setq initial-buffer-choice t) will eliminate the Welcome screen and leave you with just a *scratch* buffer.
I use this in my Emacs setup file:
(setq initial-scratch-message nil)
(setq initial-buffer-choice t)
Depending upon whether you have desktop-save-mode active, you may need something like this also . . . I have modified the desktop-read function so that I can use it subsequent to the after-init-hook (which loads before the emacs-startup-hook).
(add-hook 'emacs-startup-hook (lambda ()
(bury-buffer "*scratch*") ))

Splitting window in Emacs Lisp function

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...

How do I get list of recent files in GNU Emacs?

When I use Emacs I want to be able to easily display and navigate through a list of files I worked on from not just the current session but from previous sessions. (BTW, running Emacs 22.2 on Windows)
From Joe Grossberg's blog (no longer available):
But if you're using GNU Emacs 21.2
(the latest version, which includes
this as part of the standard distro),
you can just put the following lines
into your .emacs file
;; recentf stuff
(require 'recentf)
(recentf-mode 1)
(setq recentf-max-menu-items 25)
(global-set-key "\C-x\ \C-r" 'recentf-open-files)
Then, when you launch emacs, hit
CTRL-X CTRL-R. It will show a list of
the recently-opened files in a buffer.
Move the cursor to a line and press
ENTER. That will open the file in
question, and move it to the top of
your recent-file list.
(Note: Emacs records file names.
Therefore, if you move or rename a
file outside of Emacs, it won't
automatically update the list. You'll
have to open the renamed file with the
normal CTRL-X CTRL-F method.)
Jayakrishnan Varnam has a page
including screenshots of how this
package works.
Note: You don't need the (require 'recentf) line.
Even if you don't have recentf turned on, Emacs is saving a list of files entered via the minibuffer in the variable file-name-history. Also, executing (savehist-mode 1) in your .emacs file makes that variable persist across invocations of Emacs.
So here's a little function that displays the files that actually exist from that list (anyone is welcome to use/build on this):
(defun dir-of-recent-files ()
"See the list of recently entered files in a Dired buffer."
(interactive)
(dired (cons
"*Recent Files*"
(seq-filter
'file-exists-p
(delete-dups
(mapcar (lambda (s) (string-trim-right s "/*"))
file-name-history)
))))
)
I find this quite useful and have it bound to one of those little special function keys on my desktop keyboard. (And so I have not seen the point of turning on recentf...)