Stopping tex-shell from opening when compiling Latex from Emacs - emacs

How can I prevent the *tex-shell* buffer from opening when I compile Latex from Emacs? It splits the window in half, and I always just use C-x 1 to get rid of it immediately.
The solution is possibly related to
(setq special-display-buffer-names ("*tex-shell*"))
which makes the new buffer take up the whole frame instead of just half (not what I want).
I tried the following, but it has no effect for Latex:
(defadvice compilation-start
(around inhidbit-display (command &optional mode name-function highlight-regexp))
(flet (display-buffer) (fset 'display-buffer 'ignore) ad-do-it))
(ad-activate 'compilation-start)
(ad-deactivate 'compilation-start)

Well, you really should be using AUCTeX since it's much better. Nevertheless if you type C-hk and then a key sequence Emacs will tell you what would be run. In this case, for C-cC-f, it's tex-file so you will have to advise tex-file, or maybe (digging down into the source a little bit) tex-start-shell.

I use the following defun:
(defun tex-without-changing-windows ()
(interactive)
(save-buffer)
(save-window-excursion (tex-file)))
I bind it to C-c C-f to replace tex-file.

Related

emacs call-interactively and key simulation

I want to write a small function that saves the cursor's current position, mark the whole buffer, indents it and then goes back to the previous cursor position. I understand there might be easier way to achieve the same result but I'd like to understand how these principles work in Elisp.
Here's what I tried to do :
(defun indent-whole-buffer () (interactive)
(call-interactively 'point-to-register)
(call-interactively (kbd "RET"))
(mark-whole-buffer)
(call-interactively 'indent-region)
(call-interactively 'jump-to-register)
(call-interactively (kbd "RET"))
)
The blocking point here is the (call-interactively (kbd "RET")) how can I simulate the RET key, just as if I was doing
M-x point-to-register RET
Just use save-excursion. That's what it's for. It saves point and mark and which buffer is current, and then restores them for you.
(And if you did decide to roll your own, and you did decide to do it in the way you planned, then just call the functions you need directly - no need to use call-interactively. Use C-h f to see how each function is called. For example (point-to-register ?a) captures point in register a, and (indent-region (point-min) (point-max)) indents the whole buffer.)
Emacs Lisp is not just the macro language used by Emacs — it's the implementation language. Many Emacs functions have more functionality than what is exposed through the UI, and it is often possible to write more compact and elegant code by using this functionality.
In particular, if your goal is to indent the whole buffer, then there is no need to move point and mark around — the function indent-region is able to indent an arbitrary region:
(defun indent-whole-buffer ()
(interactive)
(indent-region (point-min) (point-max)))

Keep getting mode name when switching buffers in Emacs

I have strange thing in my Emacs and I can't locate it, everytime I switch a buffer I get message with major mode name even when I call the function I get minibuffer-inactive-mode
The only global function (for all modes) in my .emacs file (I think) is this:
(add-hook 'after-change-major-mode-hook (lambda ()
(if (not (memql (intern (major-mode))
'(fundamental-mode
erc-mode
text-mode
sql-mode)))
(local-set-key (kbd "RET")
'new-line-and-indent-fix))))
How to find the place that add this annoying thing? What different hook can be executed on each mode?
There is no major-mode function in vanilla Emacs. Whatever that function is in your config, it's probably responsible for displaying the message you're seeing.
You want to fix your code (as per Stefan's comment), but you probably also want to look into that non-standard function:
M-x find-function RET major-mode RET

Don't display *compilation* buffer in Emacs until the process exits with error or warning

I am looking for a way to have the Emacs compilation buffer triggered by M-x compile, M-x recompile or some compile on save script only appear when the compilation exits either with an error or a warning.
Note that I am not looking for a way to close the compile buffer if there are no errors or warnings as described in [1]. No I want the buffer to never appear until the compilation is fully finished and only appear if there is an error or warning to display.
The reasons are simple: The flickering compile buffer is disturbing and rearranges the position of the code on the screen. This becomes more annoying if you have compile on save turned on.
The compile buffer contains many different types of compile processes from make to pdflatex so it would be great if the function which determines whether the buffer should be displayed works across the board.
[1] emacs compile buffer auto close?
Looks like you can achieve what you want through temporarily disabling display-buffer across compilation-start.
This is a combination of what sds said and something posted on the comments # here
The comment there had a nasty problem with point jumping in the original source buffer that I appear to have worked out by also blocking set-window-point and goto-char. It feels like a dirty hack, but is working so far. YMMV!
(defun brian-compile-finish (buffer outstr)
(unless (string-match "finished" outstr)
(switch-to-buffer-other-window buffer))
t)
(setq compilation-finish-functions 'brian-compile-finish)
(require 'cl)
(defadvice compilation-start
(around inhibit-display
(command &optional mode name-function highlight-regexp))
(if (not (string-match "^\\(find\\|grep\\)" command))
(flet ((display-buffer)
(set-window-point)
(goto-char))
(fset 'display-buffer 'ignore)
(fset 'goto-char 'ignore)
(fset 'set-window-point 'ignore)
(save-window-excursion
ad-do-it))
ad-do-it))
(ad-activate 'compilation-start)
Now you should see that all compile buffers will only be shown if outstr doesn't return a successful finish status OR the invoked command started with "find" or "grep."
I edited #assem's answer to use cl-letf instead of flet.
(defun brian-compile-finish (buffer outstr)
(unless (string-match "finished" outstr)
(switch-to-buffer-other-window buffer))
t)
(setq compilation-finish-functions 'brian-compile-finish)
(defadvice compilation-start
(around inhibit-display
(command &optional mode name-function highlight-regexp))
(if (not (string-match "^\\(find\\|grep\\)" command))
(cl-letf ((display-buffer #'ignore)
(set-window-point #'ignoreco)
(goto-char #'ignore))
(save-window-excursion
ad-do-it))
ad-do-it))
(ad-activate 'compilation-start)
(provide 'only-display-compile-on-error)
The function compilation-start calls display-buffer on the compilation buffer. This should give you all the control you need.
I.e., you need to customize one of the action variables (display-buffer-overriding-action et al) so that it will handle compilation buffers specially buy displaying it in a separate frame and not displaying the frame itself.
Then you need to customize your compilation-filter-hook so that, whenever a warning or an error is inserted into the compilation buffer, the compilation buffer is displayed visibly (e.g., by popping up the aforementioned separate frame). Don't forget to bind your action variable to nil there!
assems answer has been overtaken by events, somewhat.
Emacs core, in their wisdom have decided to deprecate flet.
The suggested alternative is cl-flet. However, as discussed in this post, this seems to be lexically scoped rather than dynamically scoped. We explicitly want dynamic scoping.
Should `flet` be replaced with `cl-flet` or `cl-letf` ?
This page suggests replace flet to noflet, a third-party library.
This appears to only support the definition of functions and their bodies.
So the flet in assem's answer becomes
(noflet ((display-buffer ()) ....

How can I switch focus after buffer split in emacs?

I would like that after splitting the window (C-x 3 or C-x 2) to be able to automatically get to cursor in the new opened buffer (the other than the current). How can I achieve this behavior ?
You can switch between buffers with C-x o. As to do that automatically I don't think there is an existing command for that.
You can do it like this:
(global-set-key "\C-x2" (lambda () (interactive)(split-window-vertically) (other-window 1)))
(global-set-key "\C-x3" (lambda () (interactive)(split-window-horizontally) (other-window 1)))
In Emacs 24.3.1 it works if you change the argument 1 for 0.
!!!DO NOT USE THIS ANSWER!!! -- as pointed out in the comments, advising split-window can lead to undesired side-effects.
I recommend Bozhidar Batsov's answer instead.
Put the following in your .emacs file:
(defadvice split-window (after move-point-to-new-window activate)
"Moves the point to the newly created window after splitting."
(other-window 1))
As well as splitting the frame manually with C-x 2 or C-x 3, buffers are also automatically "popped-up" some times. These are also not selected/active by default.
This can be fixed by changing the function used to split a window. It's set to split-window-sensibly by default, but you can set it to your own function that calls split-window-sensibly and then selects the buffer.
Unfortunately, though, this has the side-effect of selecting the *Completions* buffer when you hit TAB in the minibuffer. So, it's worth checking to see if the minibuffer is active and not switching in this case. I'd bet there are other such undesirable scenarios as well. I'll try to update this post as and when I find them.
;; after splitting a frame automatically, switch to the new window (unless we
;; were in the minibuffer)
(setq split-window-preferred-function 'my/split-window-func)
(defun my/split-window-func (&optional window)
(let ((new-window (split-window-sensibly window)))
(if (not (active-minibuffer-window))
(select-window new-window))))
(Works with Emacs 24.5.1.)
My thought of when you would want to follow the window after a split-window was when it had the same buffer like in the following code:
(defun split-window--select-window (orig-func &rest args)
"Switch to the other window after a `split-window'"
(let ((cur-window (selected-window))
(new-window (apply orig-func args)))
(when (equal (window-buffer cur-window) (window-buffer new-window))
(select-window new-window))
new-window))
(advice-add 'split-window :around #'split-window--select-window)
Simple
C-x o will help you switch to the "other" buffer.

Run sgml-pretty-print when opening an xml file in emacs?

I can currently use sgml-pretty-print to pretty print an xml file in emacs, but it's a manual process:
M-<
C-space
M->
M-x sgml-pretty-print
I'd like this to happen automatically (or at least have some option to do so). I'm new to emacs/elisp, and do not understand how:
emacs knows what code to run when you open a file (does this start in files.el?)
If you wanted to override that code with your own, how to do that
This should do the trick for you:
(add-hook 'find-file-hook 'my-sgml-find-file-hook)
(defun my-sgml-find-file-hook ()
"run sgml pretty-print on the file when it's opened (if it's sgml)"
(when (eq major-mode 'sgml-mode)
(sgml-pretty-print (point-min) (point-max))))
The key pieces of information are the find-file-hook, point-min (-max), and major-mode.
If you want to learn more about elisp, you can take a look at this question, which gives some pointers on how to figure things out.
A slightly simpler alternative to Trey Jackson's answer. Just add this to your ~/.emacs file:
(add-hook 'sgml-mode-hook #'(lambda ()
(sgml-pretty-print (point-min) (point-max))))