emacs auctex commandto compile and view - emacs

I use EMACS/AucTeX. In order to compile I do C-c C-c, then it asks
"Command: (default LaTeX)"
I press RET and it is compiled. To view the compiled document I do C-c C-v.
I would like to have a simple shortcut, like pressing F1 or some other key combination to compile and then view the document. There is any simple command/function that can be inserted in .emacs to do that?
Thanks
Pietro

C-c C-a (TeX-command-run-all) will do the job in AUCTeX 11.89.

I don't think there's anything that will work out of the box, and the naive approach of just calling the two commands in sequence won't work because you need the compilation process to finish before you can view the output.
Here's a quick and dirty solution that might work for you:
(defun my/TeX-view-once (doc)
"View TeX output and clean up after `my/TeX-compile-and-view'.
Call `TeX-view' to display TeX output, and remove this function
from `TeX-after-TeX-LaTeX-command-finished-hook', where it may
have been placed by `my/TeX-compile-and-view'."
(TeX-view)
(remove-hook 'TeX-after-TeX-LaTeX-command-finished-hook #'my/TeX-view-once))
(defun my/TeX-compile-and-view ()
"Compile current master file using LaTeX then view output.
Run the \"LaTeX\" command on the master file for active buffer.
When compilation is complete, view output with default
viewer (using `TeX-view').
(interactive)
(TeX-command "LaTeX" 'TeX-master-file)
(add-hook 'TeX-after-TeX-LaTeX-command-finished-hook #'my/TeX-view-once))
You may want to tinker with the TeX-command line in my/TeX-compile-and-view, since it hard-codes a lot of things that C-c C-c (TeX-command-master) does not. In particular, I'm not sure what it will do if there is no master file set, and it will recompile even if it doesn't need to.
EDIT: After some tinkering, it looks like everything runs fine without a master file, so long as you have this line in your .emacs:
(setq-default TeX-master nil)
I'm not sure why this is the case, since this says AUCTeX should query you for a master file if it's not already set, and this command does no querying even in that case. If you don't want to use this line, it shouldn't be too hard to make the above function work on the buffer instead.

You could bind F1 to one function, TeX-master-command, since C-c C-c will set the viewer if you use it just after compiling with C-c C-c. Here is a quote from auctex manual
Once you started the command selection with C-c C-c, C-c C-s or C-c C-b you will be prompted for the type of command. AUCTeX will try to guess which command is appropriate in the given situation and propose it as default. Usually this is a processor like ‘TeX’ or ‘LaTeX’ if the document was changed or a viewer if the document was just typeset.
To set this function to F1, you should try something like:
(add-hook 'LaTeX-mode-hook
'(lambda()
(local-set-key (kbd "<F1>") 'TeX-master-command)
))
You still will be prompted and have to press RET after F1. Binding F1 to Aaron Haaris TeX-compile-and-view might spare this RET.
Since you are tired of those C-c C-c, you should try latexmk. All you have is to launch it once, and then it will compile your .tex after every new save. You can set the viewer, how often latexmk checks if your .tex file as changed and, many, many other things.

Related

Why does Emacs local-set-key not overwrite a globally set key in mode hook?

In Emacs' tide-mode (typescript development) I would like to use M-q, which is normally bound to fill-paragraph, to rather run tide-format. I have a mode hook like
(defun setup-tide-mode ()
...
(local-set-key [M-q] 'tide-format)
(describe-key [M-q]))
(add-hook 'typescript-mode-hook #'setup-tide-mode)
When I open a typescript file I do see the *Help* buffer which indeed shows
<M-q> runs the command tide-format ...
Yet when I then run C-h k M-q to describe the key binding of M-q, I get
M-q runs the command fill-paragraph
There is this suspicious difference in the printout between <M-q> and M-q. This is probably telling me something, but I don't know what.
What would be the correct way to locally overwrite M-q to run a different command?
You want to use [?\M-q] instead of [M-q] because ?\M-q is the event generated when you press the Alt/Meta modifier along with the Q key.

Emacs/AUCTeX prefix arguments

In LaTeX mode C-c C-c is bound to:
(TeX-command-master &optional OVERRIDE-CONFIRM)
Normally this interactive function runs a command, perhaps a LaTeX compilation, asking for confirmation.
In tex-buf.el it reads:
If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
depend on it being positive instead of the entry in `TeX-command-list'.
This is a bit cryptic for me and reading C-h v TeX-command-list didn't help.
How can I pass the prefix argument to "TeX-command-master" so that I avoid all the confirmation requests?
Take a look at Emacs' documentation to find out about prefix arguments. In general, you can pass a command a prefix argument with C-u followed by a number. For one-digit numbers, you can also just type Meta followed by the digit. Thus to pass a positive prefix argument to TeX-command-master you could type:
M-1 C-c C-c
However, this will actually add another minibuffer confirmation, namely about the shell command to be used to compile the LaTeX source. Without the prefix argument, a command-dependent default is used for that.
If you want to avoid the question about the command to use, you can bind the undocumented variable TeX-command-force to "LaTeX" via:
(setq TeX-command-force "LaTeX")
However, this will have the downside that you're basically binding C-c C-c to the "latex" command, you cannot use any of the other commands such as "bibtex" or "view".
Other than that, LaTeX-mode does not allow for any customization of C-c C-c. Your best options are to either advise the function TeX-command-query or to bind C-c C-c to a wrapper function to set TeX-command-force dynamically. The latter would probably be the preferred option if you also want to auto-save the buffer.
It seems that the mystery of the OVERRIDE-CONFIRM continues. In the meantime a fellow suggests that, if we are unable to manage TeX-command-master, we can simply rewrite it.
In my version, based on his, if the buffer is not modified, the external viewer is launched; if the buffer is modified the compiler is run.
Everything with no confirmation for saving or running the given command.
(defun my-run-latex ()
(interactive)
(if (buffer-modified-p)
(progn
(setq TeX-save-query nil)
(TeX-save-document (TeX-master-file))
(TeX-command "LaTeX" 'TeX-master-file -1))
(TeX-view)))
Of course one can bind my-run-latex to whatever keybinding.
On the user's point of view this is a solution to my own question.
Do I click the close tag? Well, on the curious guy point of view I am still interested in understanding the mysterious TeX-command-master technicalities.
If someone should happen to know...
P.S.
Yes, TeX-save-query overrides the save-file request, also with TeX-command-master, that is C-c C-c. But you will still be asked to confirm the command action.
Build & view
Again, this solution, instead of modifying the behaviour of the TeX-command-master, rewrites it. The rewritten version of the command, named build-view, follows a rather straightforward logic.
If the LaTeX file buffer is not-modified, it runs the default viewer;
If the buffer is dirty, it runs the default LaTeX compiler and, after the build, opens the output in the default viewer.
Here's the code:
(defun build-view ()
(interactive)
(if (buffer-modified-p)
(progn
(let ((TeX-save-query nil))
(TeX-save-document (TeX-master-file)))
(setq build-proc (TeX-command "LaTeX" 'TeX-master-file -1))
(set-process-sentinel build-proc 'build-sentinel))
(TeX-view)))
(defun build-sentinel (process event)
(if (string= event "finished\n")
(TeX-view)
(message "Errors! Check with C-`")))
You can now type M-x build-view and start the told build-view process or associate it with a new keybinding such as “F2”:
(add-hook 'LaTeX-mode-hook '(lambda () (local-set-key (kbd "<f2>") 'build-view)))
Note: As suggested by Tyler, TeX-save-query variable is changed locally, therefore the old C-c C-c/ TeX-command-master is unaffected and will keep asking confirmations.
Do edit this code to make it better or easier to read!
I puzzled over the OVERRIDE-CONFIRM bit for a while, and couldn't figure out how it was supposed to work. If you want to automatically run Latex on your file, without being bothered about saving it first, or confirming that you want latex (rather than view, bibtex etc), you could use a function like this:
(defun my-run-latex ()
(interactive)
(TeX-save-document (TeX-master-file))
(TeX-command "LaTeX" 'TeX-master-file -1))
Bind this to something handy, and you'll still have C-c C-c for when you want to use the default processing commands. You may want to modify the TeX-command line if "Latex" isn't the processor you want to call.
If you are just looking to compile the latex source without a confirmation dialog, just add the following to your .emacs:
(setq TeX-command-force "")
You can then compile the source with C-c C-c and it won't ask to confirm. The only problem with this solution is that you can no longer change the command, but with most documents you won't want to. I might suggest that at the same time you can add this to your .emacs for even more flexibility, giving you a C-c C-c equivalent to the former behavior:
(define-key LaTeX-mode-map "\C-c\C-a"
;;; 'a' for ask, change to anything you want
(lambda (arg) (interactive "P")
(let ((TeX-command-force nil))
(TeX-command-master arg))))
You can then just work away at your document, do a C-x C-s, C-c C-c and then C-c C-v to see it. Like others have suggested you can also do the same for the save command and have it compile automatically on save, but some of my documents are in CVS and so I avoid putting hooks on that.
Credit to Ivan for some help on this one - don't know if he is on StackOverflow
I think the gist of this question is "how do I quickly compile my TeX document from AUCTeX without all the key presses and confirmations?"
My answer to that is to use the latexmk command rather than trying to coerce AUCTeX to do it.
latexmk -pdf -pvc myfile.tex
latexmk will monitor the file in question and rebuilt it as soon as you save it. If you use a good pdf viewer, it will notice the change in PDF and re-display it immediately. On OS X, skim works well for this.

Emacs macro works in .emacs file but not in a source file?

I have an emacs macro (global-set-key) that works perfectly fine in my .emacs file, but for whatever reason, it does not work in my .c file.
(global-set-key "\C-c\C-d" "\C-a\C- \C-e\M-w\C-j\C-y")
If I close and re-open my .emacs file and start messing around, this macro behaves as expected, copying a line to a line below. However, when I open a C file the same macro simply deletes a character (it only seems to pick up on C-d).
Any ideas?
The cc-mode defines C-c C-d in c-mode-base-map to be c-hungry-delete-forward, which is hiding your binding at the global level. So, the better way to solve this is to undefine the binding that cc-mode made, and you do that with the following:
(eval-after-load "cc-mode"
'(define-key c-mode-base-map (kbd "C-c C-d") nil))
You could also do it in a hook, but I prefer eval-after-load because it only gets executed once.
Note: I determined the existing binding by opening up a file in c-mode and typing C-h C-k C-c C-d (aka M-x describe-binding C-c C-d), and seeing:
C-c C-d runs the command c-hungry-delete-forward, which is an
interactive compiled Lisp function in `cc-cmds.el'.
This made it pretty clear that the binding was set up in (one of the) c-modes, so I just opened up (or greped) the source files for c-hungry-delete-forward whereupon I found:
(define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
And then the answer was straight forward.
I think it's better to undefine local bindings that hide the global bindings you want, rather than redefining them. It's just as much work to find the problematic bindings, and this way if you want to change the function for the global binding, you only have to do it in one place.
Clearly the C mode is removing the binding, or changing it. You can try to add it to a c-mode-hook and see if it works then. Similar to this:
(add-hook `c-mode-hook '(lambda ()
(global-set-key "\C-c\C-d" "\C-a\C- \C-e\M-w\C-j\C-y")))
Either c-mode-hook or c-mode-common-hook. You can also use local-set-key instead of the global one to apply the binding just to this buffer.

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.