Extending list in .emacs init file - emacs

I'm using Emacs with AUCTeX and I want to add a command to the TeX-command-list, namely makeglossaries.
I refered to the manual and got this far:
(eval-after-load "tex"
'(add-to-list 'TeX-command-list
'("Makeglossaries" "makeglossaries %t" TeX-run-command nil
(latex-mode)
:help "Run makeglossaries script, which will choose xindy or makeindex") t))
However, when I execute this in emacs with C-c C-c I get
Running Makeglossaries' onnameOfFile' with ``makeglossaries nameOfFile.tex''
Don't pass the tex file to makeglossaries:
either omit the extension to make all the glossaries, or specify one of the glossary files, e.g. nameOfFile.tex.glo, to make just that glossary.
The message is pretty clear. I somehow have to change %t so it omits the ".tex", however I don't know how to do that or where to look it up. Is this elisp code? Don't want to learn the language just for that.
Somebody knows how I can fix this problem?
Thanks it advance!
After a little more research I found I can replace %t with %s. %s apparently is the filename without extension, though I don't know where this is defined. Anyway this works for me:
(eval-after-load "tex" '(add-to-list 'TeX-command-list
'("Makeglossaries" "makeglossaries %s" TeX-run-command nil
(latex-mode)
:help "Run makeglossaries script, which will choose xindy or makeindex") t))

Yes, that's elisp.
You can test for the extension with the following test:
(equal ".tex" (substring "file.tex" -4))
To remove it, use substring again:
(substring "file.tex" 0 -4)
Negative numbers count positions from the right.

Related

AUCTeX TeX-doc & texdoc

As far as I can tell from the manual, running C-c ? in AUCTeX ought to run texdoc %s on the package name specified. For the vast majority of packages it does, however some just aren't found - M-! texdoc memoir works fine, but C-c ? memoir fails both at detecting the package under point and then at loading the documentation when I type it in manually.
I've been trying to find the variable which controls the invocation of texdoc, but can't. M-: (executable-find "texdoc") returns /usr/bin/texdoc as expected, but that's as far as I got.
Any suggestions would be greatly appreciated...
Starting from version 11.89, AUCTeX binds by default C-c ? to TeX-documentation-texdoc, which does exactly what is asked.
For previous versions of AUCTeX, see the original answer below.
I find TeX-doc (the function bound to C-c ?) overly complicated: in the case of memoir class it never calls texdoc memoir because the doc file is named memman.pdf instead of memoir.pdf. That should be fixed upstream.
For the time being, you can use this much simpler function which blindly runs texdoc <symbol-at-point> without further checks:
(defun mg-TeX-doc ()
"Search documentation with texdoc for symbol at point."
(interactive)
(call-process "texdoc" nil 0 nil "--view" (thing-at-point 'symbol)))
You can bind it to C-c ? if you want to replace standard TeX-doc with the following code:
(eval-after-load "tex"
'(progn
(define-key TeX-mode-map (kbd "C-c ?") 'mg-TeX-doc)))

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.

get and set line content in emacs buffer programmatically

I would like to translate the following function from vim script to emacs elisp (I use it to set the email recipients when writing emails).
My question is mainly how to get and set line contents in emacs, because with quick googling I could not find this out (probably I just did not know the right terms to google for, "getline" and "setline" in any case did show any results).
function! G_set_to()
let address = system('my-address-script') "shell command to choose addresses
let line = getline (2)
if strlen(line) == 4
call setline(2, line . address)
else
call setline(2, line . "; " . address)
endif
endfunction
Sorry if the answer is obvious, I am am completely new to emacs and don't even know how to use its in-built help system.
Cheers
Arian
Give this a shot, obviously customizing the variable G-address-script to suit your needs.
(require 'sendmail)
(defvar G-address-script "echo hi#google.com")
(defun G-set-to ()
"execute script in G-address-script and populate the To: line with the results"
(interactive)
(save-excursion
(mail-to)
(unless (= (current-column) 4)
(insert "; "))
(insert (shell-command-to-string G-address-script))
(when (looking-at "^$") ; when shell command has extra newline, delete it
(delete-backward-char 1))))
As far as using the help, there's a reasonable introduction to Emacs lisp here. But the quick intro is:
C-h i (that's control-h then i to see the info pages, there is one for Emacs and one for Emacs Lisp.
M-x apropos searches for terms
C-h f gives you help on a function
C-h v gives you help on a variable
In the *scratch* buffer, C-j will evaluate the expression right before the point (cursor)
M-x edebug-defun when the point is in a emacs lisp function will turn the debugger on, and the next time a function is run you'll step through it (M-x eval-defun in the function will turn the debugger off)
M-x eldoc-mode will turn on automatic documentation for emacs lisp functions and variables as you type them, read more here.
I took Trey's code and changed a few things and now it seems to work:
(defvar G-address-script "goobook_dmenu_with_cache")
(defun G-set-to ()
"execute script in G-address-script and populate the To: line with the results"
(interactive)
(save-excursion
(goto-line 2)
(re-search-forward "$")
(unless (= (current-column) 4)
(insert "; "))
(insert (shell-command-to-string G-address-script))))
Some remaining questions:
The bit where Trey's code checks if the string returned from G-address-script is empty doesn't work at the moment, what do I have to change?
Is there really no function to get the content of a specified line as a string?
Is there a better/more elegant way to do this? The vim script solution looks so much simpler, surely I am missing something.
Anyway, I'll mark this as solved in a day or so if there are no more answers as Trey solution basically does what I wanted. Thank you Trey!

Setting slime-enable-evaluate-in-emacs

I am using SBCL with slime, and have the following code:
(swank::eval-in-emacs
'(with-current-buffer (slime-repl-buffer)
(insert (propertize "foo" 'font-lock-face '(:foreground "red")))))
(print "here is some text")
In general, if I try to execute anything with swank:: prefixed to it, emacs will give a security error, and this particular one tells me I need to set slime-enable-evaluate-in-emacs to true. Where is this value? I haven't been able to find a slime or swank config. & settings file. Thanks much.
You can simply add this to your .emacs:
(setq slime-enable-evaluate-in-emacs t)
If non-nil, the inferior Lisp can evaluate arbitrary forms in Emacs.
The default is nil, as this feature can be a security risk.

How to call latexmk in emacs, and jump to next-error

I would like to use latexmk to compile my LaTeX documents in Emacs. Especially I need the Emacs functionality next-error, which is typically called with C-x `, and jumps to the next LaTeX error in the document.
I would like to call latexmk either using C-x compile or the AUCTeX C-c C-c.
First, I set latexmk to use
$pdflatex = 'pdflatex -interaction=nonstopmode';
Option 1: C-x compile
I press C-x compile and type latexmk -pdf foo, which runs pdflatex. But next-error will not jump to the errors, even if the *compilation* buffer contains errors:
! Paragraph ended before \author was complete.
<to be read again>
\par
l.48
[...]
Compilation exited abnormally with code 12
How can I automatically jump to this error in line 48?
Note that this question of parsing the latex output has nothing to do with latexmk directly. The same problem occurs when I just do C-x compile pdflatex -interaction=nonstopmode foo.
Option 2: AUCTeX
How can I set AUCTeX to call latexmk -pdf instead of pdflatex on my .tex file? Of course, I want next-error to work here too.
UPDATE: I started a bounty because if this worked it would be a great tool for many people. I consider the question answered if a solution is given that lets me easily compile my LaTeX document using latexmk in Emacs and jump to the errors using next-error (of course, the errors might be in included .tex files, not necessarily in the master file or the current buffer).
UPDATE: Thanks to Ivan (and Chris) for making AUCTeX+Latexmk work. In the meantime, I found that using Rubber to compile LaTeX is also an excellent choice. It will display error messages in the format used by gcc and other compilers, so it naturally works with Emacs C-x compile, e.g. C-x compile rubber --pdf foo, and the error messages are parsed correctly.
Sorry I don't have the ability to comment, or I would just add this as a comment. Chris Conway's answer works, except that it should use TeX-run-TeX instead of TeX-run-command so that AucTeX knows to process the error messages.
(add-hook 'LaTeX-mode-hook (lambda ()
(push
'("Latexmk" "latexmk -pdf %s" TeX-run-TeX nil t
:help "Run Latexmk on file")
TeX-command-list)))
It might also be wise to add something like
'("%(-PDF)"
(lambda ()
(if (and (not TeX-Omega-mode)
(or TeX-PDF-mode TeX-DVI-via-PDFTeX))
"-pdf" "")))
to TeX-expand-list and use "latexmk %(-PDF) %s" so that it will work in both pdf and dvi mode. Personally, I find it easier to use customize especially when you are experimenting.
It's relatively easy to get AucTeX to run latexmk with C-c C-c. The following will add a Latexmk choice to the list of TeX commands:
(add-hook 'LaTeX-mode-hook (lambda ()
(push
'("Latexmk" "latexmk -pdf %s" TeX-run-command nil t
:help "Run Latexmk on file")
TeX-command-list)))
The trick is getting next-error to work. If you dig around in the AucTeX sources, you can probably find the regex it uses on TeX output buffers; it's not automatically applied to the buffer created by TeX-run-command. (You might also be able to convince compile mode to use this regex.)
Another approach is to redefine the variable LaTeX-command. This is a bit iffy because I think a lot of AucTeX functions assume they can tack command-line options onto this string and execute the result.
Adding %(mode) gives latexmk some more options like noninteractive if it is set so in auctex.
(add-hook 'LaTeX-mode-hook (lambda ()
(push
'("Latexmk" "latexmk -pdf %(mode) %s" TeX-run-TeX nil t
:help "Run Latexmk on file")
TeX-command-list)))