Sync Emacs AUCTeX with Sumatra PDF - emacs

With these lines in my init.el I am able to sync the Emacs LaTeX buffer with Sumatra:
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-method 'synctex)
(setq TeX-view-program-list
'(("Sumatra PDF" ("\"C:/bin86/SumatraPDF/SumatraPDF.exe\" -reuse-instance"
(mode-io-correlate " -forward-search %b %n ") " %o"))))
(setq TeX-view-program-selection
'(((output-dvi style-pstricks) "dvips and start") (output-dvi "Yap")
(output-pdf "Sumatra PDF") (output-html "start")))
To set a double click on the PDF to get me to the related LaTeX code, I also set in Sumatra options Set inverse search command line to:
"c:\bin86\GNU Emacs 24.2\bin\emacsclient.exe" --no-wait +%l "%f"
Despite the sync works, I’d like to code it differently.
If I didn’t set the last expression, (setq TeX-view-program-selection..., I would get the default values, which are the same as above, apart from the value for the PDF output that would be: (output-pdf "start").
I’d like to change this one to "Sumatra PDF" and leave the other values to their default, that is, I’d like to ask Emacs the default values for the viewers and change only the PDF value.
It is mostly an ELisp question concerning the manipulation of the variable TeX-view-program-selection.
Thanks for helping.
P.S. Please tell me if this question is best fit on tex.stackexchange
Update based on lunaryorn comments/answer
To update TeX-view-program-selection I could use:
(assq-delete-all 'output-pdf TeX-view-program-selection)
(add-to-list 'TeX-view-program-selection '(output-pdf "Sumatra PDF"))
The first line is optional, but it makes the list look "cleaner".
In both cases (with or without assq-delete-all) I now need to insert the code in the proper hook, since TeX-view-program-selection is void in init.el.

My credits to lunaryorn for suggestions! I am repackaging the steps involved to the benefits of the others.
Emacs side
Add to your init.el:
(setq TeX-PDF-mode t)
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-method 'synctex)
(setq TeX-view-program-list
'(("Sumatra PDF" ("\"path/to/SumatraPDF/SumatraPDF.exe\" -reuse-instance"
(mode-io-correlate " -forward-search %b %n ") " %o"))))
(eval-after-load 'tex
'(progn
(assq-delete-all 'output-pdf TeX-view-program-selection)
(add-to-list 'TeX-view-program-selection '(output-pdf "Sumatra PDF"))))
Sumatra side
In Settings->Options dialog set Set inverse search command line to:
"path\to\GNU Emacs ver\bin\emacsclient.exe" --no-wait +%l "%f"
How to use
In your LaTeX document in Emacs type C-c C-v or double click the related PDF in Sumatra and ... enjoy))

Use add-to-list instead of setq. See C-h f add-to-list for more information.
TeX-view-program-selection is defined in tex.el, so you'll need to execute this code after this library is loaded:
(eval-after-load 'tex
'(progn
(assq-delete-all 'output-pdf TeX-view-program-selection)
(add-to-list 'TeX-view-program-selection '(output-pdf "Sumatra PDF"))))

I was driven nearly mad by the quoting conventions for windows ->emacs
If you install SumatraPDF in the default place, then this works on windows 8.1
(setq TeX-view-program-list
'(("Sumatra PDF" ("\"C:/Program Files (x86)/SumatraPDF/SumatraPDF.exe\" -reuse-instance"
(mode-io-correlate " -forward-search %b %n ") " %o")))))
That's an hour or so I'll never get back :)

I have prepared a batch file, Emacs_SumatraPDF.bat.
It works perfect with sumatra-forward.el to realize forward and backward search.
For example:
Download
http://www.ai.soc.i.kyoto-u.ac.jp/~shi/files/Emacs_SumatraPDF.bat
http:/william.famille-blum.org/software/sumatra/sumatra-forward.el
Prepare
put Emacs_SumatraPDF.bat and SumatraPDF.exe -->
%Emacs_Home%/bin.
put sumatra-forward.el --> %Emacs_Home%/site-lisp
Usage
;Emacs_Home=C:/Program Files (x86)/GNU Emacs 23.4
;~\.emacs add following information
;;; Emacs_SumatraPDF.bat
;; #info: pdf viewer
; #refer:
(setq TeX-view-program-list '(
("Sumatra" "C:/Program Files (x86)/GNU Emacs 23.4/bin/Emacs_SumatraPDF.bat
%o %t %n") ))
(setq TeX-view-program-selection '(
(output-pdf "Sumatra")
(output-dvi "Yap")
((output-dvi style-pstricks) "dvips and start")
(output-html "start")))
;;; sumatra-forward.el
;; #info: forward search.
; #refer: http:/william.famille-blum.org/blog/static.php?page=static081010-000413
(require 'sumatra-forward)
That is all.
Besides
Emacs 4 Latex Support under Windows 7 for newcomer for Emacs.
http://chunqishi.github.io/emacs4ls/

It's worth mentioning that Set inverse search command line is invisible in the Settings->Options dialog by default. To show this option field, go to Settings->Advanced options or open SumatraPDF-settings.txt and turn on EnableTeXEnhancements = true.

Related

How to create a pdf from a Rmd file using Emacs, ESS, pandoc-mode and polymode?

This is an adaptation of a "classic" Rmd file that I want to knit as a pdf using Emacs (Emacs Speak Statistics) and polymode. I can't find the right commands to do that. There is little documentation about polymode. I am using Emacs Starter Kit for the Social Sciences.
---
title: "Untitled"
author: "SB"
date: "Wednesday, February 04, 2015"
output: pdf_document
---
You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
As the doc says use M-n w and M-n W to set/change the weaver. With ESS you probably should use knitr-ESS weaver as it uses current *R* process.
You can use rmarkdown::render() from the rmarkdown package to knit an .Rmd file to markdown and render the output file (PDF, Word, HTML, etc.) with a single command!
I wasn't sure if support for an rmarkdown workflow was already included in ESS (and I'm trying to dabble in elisp) so I wrote a function that calls rmarkdown::render() and allows customizing the inputs to rmarkdown::render() function call with a prefix arg (e.g., C-u).
;; spa/rmd-render
;; Global history list allows Emacs to "remember" the last
;; render commands and propose as suggestions in the minibuffer.
(defvar rmd-render-history nil "History list for spa/rmd-render.")
(defun spa/rmd-render (arg)
"Render the current Rmd file to PDF output.
With a prefix arg, edit the R command in the minibuffer"
(interactive "P")
;; Build the default R render command
(setq rcmd (concat "rmarkdown::render('" buffer-file-name "',"
"output_dir = '../reports',"
"output_format = 'pdf_document')"))
;; Check for prefix argument
(if arg
(progn
;; Use last command as the default (if non-nil)
(setq prev-history (car rmd-render-history))
(if prev-history
(setq rcmd prev-history)
nil)
;; Allow the user to modify rcmd
(setq rcmd
(read-from-minibuffer "Run: " rcmd nil nil 'rmd-render-history))
)
;; With no prefix arg, add default rcmd to history
(setq rmd-render-history (add-to-history 'rmd-render-history rcmd)))
;; Build and evaluate the shell command
(setq command (concat "echo \"" rcmd "\" | R --vanilla"))
(compile command))
(define-key polymode-mode-map (kbd "C-c r") 'spa/rmd-render)
Note that I have some specific parameter settings like output_dir = '../reports' but the elisp can be easily customized to suit your needs.
With this in your init file, you only need to enter C-c r from inside your .Rmd file (or C-u C-c r to render to a different format, location, etc.). The command will open a new window with a buffer called *compilation* where any errors will appear.
This could definitely be improved and I'd love to hear suggestions.

Emacs: How to switch mode after typing certain string?

I am writing R Markdown (.Rmd) file with a lot of LaTeX contents. The major mode is Markdown-mode. But what I want is that: when I try to type some LaTeX commands between $$, I want to automatically turn on the LaTeX major mode, and after I end the LaTeX chunk with $$ or $, it can turn off LaTeX major mode and turn on Markdown-mode again.
The inline format for LaTeX is
$\alpha$
The equation(displaymath) format for LaTeX is
$$
\alpha + \beta
$$
A similar example is when I was writing a .Rnw file, the major mode is LaTeX, but when I edit the R
code chunk:
<<>>=
y <- 1000
#
between <<>>= and #, it turns on the ESS mode.
So can anyone help me to do the similar stuff in Markdown-LaTeX case? Thanks!
Updates after Inaimathi's answer:
Following http://www.emacswiki.org/emacs/MultipleModes#toc6, I need to modify the two-mode-mode.el
- (if (string= to-mode mode-name)
+ (if (string= to-mode major-mode)
and for emacs 24
- (make-local-hook 'post-command-hook)
Then, by Inaimathi, I put the following in my .emacs:
(require 'two-mode-mode)
(setq default-mode (list "Markdown" 'markdown-mode)
second-modes (list (list "LaTeX" "\\\[" "\\\]" 'latex-mode)))
Then the major mode switches between \[ and \].
If you're ok with your major mode changing as you navigate, two-mode-mode should be able to do what you need. You need to include that .el, then add a block that looks something like
(require 'two-mode-mode)
(setq default-mode (list "Haskell" 'haskell-mode)
second-modes (list (list "Markdown" "\[markdown|" "|\]" 'markdown-mode)))
to your ~/.emacs. The above is what I do to highlight markdown blocks while editing Elm code. For your particular use case, you'd probably need
(require 'two-mode-mode)
(setq default-mode (list "Markdown" 'markdown-mode)
second-modes (list (list "Latex" "\$\$" "\$\$" 'latex-mode)
(list "Latex" "<<>>=" "#" 'latex-mode)))
Instead. This is completely untested, includes no warranty, etc etc.
Ideally, what I'd like in that situation is for the rest of the buffer to keep markdown-mode highlighting, and have just the particular region get highlighted in latex-mode or whatever. Near as I can tell, there isn't a ready, general solution for that, but if you're up for some Elisp hacking, you could give this a read and see what you can come up with.

Is there an option in emacs like vimdiff?

I simply want to supply two files to emacs from terminal and it should open them in two separate windows in diff mode.
You're looking for ediff. In emacs you can activate it via M-x ediff and the file names.
To call it from commandline try something like this (taken from emacs wiki):
(defun command-line-diff (switch)
(let ((file1 (pop command-line-args-left))
(file2 (pop command-line-args-left)))
(ediff file1 file2)))
(add-to-list 'command-switch-alist '("diff" . command-line-diff))
;; Usage: emacs -diff file1 file2
To get you started here are a few additional "saner" defaults"
;; saner ediff default
(setq ediff-diff-options "-w")
(setq ediff-split-window-function 'split-window-horizontally)
(setq ediff-window-setup-function 'ediff-setup-windows-plain)
These will always split to have ediff windows side-by-side.
If you want to always force a new frame (it is cleaner this way), try these additionally:
(add-hook 'ediff-before-setup-hook 'new-frame)
(add-hook 'ediff-quit-hook 'delete-frame)
I hope that helps for the beginning.

Sublime Text 2's "Goto Anything" (or instant search) for Emacs?

I tried out Sublime Text 2 recently, and I found Goto Anything superbly useful for navigating source code (Ctrl-P file#symbol seems to work really well). Is there something similar for Emacs? Preferably something that just works, without a ton of custom elisp.
What I've tried so far:
I've seen Helm and Anything, but as far as I understand neither of them is capable of actual "instant" search (see edit below).
I've used multi-occur-in-matching-buffers, but it too seems unable to satisfy the "instant" criterion.
imenu / idomenu works well for single files, but doesn't work across files.
I currently use #2 and #3 together, as a poor substitute for Goto Anything.
If not an exact clone of Goto Anything, then I could make do with a naive instant search solution (one that searches for a given string across all open buffers and displays results dynamically). So that's acceptable too.
I use Emacs 24.2, so any v24-only elisp is also fine.
EDIT: I gave Helm another shot, at event_jr's suggestion, and I found that it does support instant searching across all open buffers. helm-multi-occur + helm-follow-mode comes surprisingly close to meeting my needs, the only minor issues being (at the risk of sounding nit-picky):
I haven't found a way to turn on helm-follow-mode automatically when I run helm-multi-occur. I have to invoke it manually with C-c C-f. Anyone care to take a shot at this with a snippet of elisp? (see edit #2 below)
it isn't "intelligent" like ST2's Goto Anything (i.e., it doesn't understand "symbols" in source code, like Goto Anything does).
EDIT #2: Now I've got most of Goto Anything, thanks to event_jr's answer below (and of course, thanks to Helm's creator, Thierry Volpiatto). I recommend it heartily to anyone looking for a similar feature. Below is the elisp I'm currently using:
;; instant recursive grep on a directory with helm
(defun instant-rgrep-using-helm ()
"Recursive grep in a directory."
(interactive)
(let ((helm-after-initialize-hook #'helm-follow-mode))
(helm-do-grep)))
;; instant search across all buffers with helm
(defun instant-search-using-helm ()
"Multi-occur in all buffers backed by files."
(interactive)
(let ((helm-after-initialize-hook #'helm-follow-mode))
(helm-multi-occur
(delq nil
(mapcar (lambda (b)
(when (buffer-file-name b) (buffer-name b)))
(buffer-list))))))
;; set keybindings
(global-set-key (kbd "C-M-s") 'instant-search-using-helm)
(global-set-key (kbd "C-M-S-s") 'helm-resume)
(global-set-key (kbd "C-M-g") 'instant-rgrep-using-helm)
Just use helm.
It is perhaps more configuration than you asked for, but once you get it
configured how you like, it should be quite comfortable. Very much like Emacs
;).
And you should file a bug with Thierry for getting some more newbie friendly
defaults. He is quite responsive with issues.
helm-multi-occur
Primarily multi-buffer interactive "occur" is provided through
helm-multi-occur. If you execute the command, you'll notice that you have
to pick some buffers first (use C-SPC to select from the list,
M-SPC to select all). Then you can enter your query at the next
prompt. It's easy to make your own version that skips the buffer selection
like so:
(eval-after-load "helm-regexp"
'(setq helm-source-moccur
(helm-make-source "Moccur"
'helm-source-multi-occur :follow 1)))
(defun my-helm-multi-all ()
"multi-occur in all buffers backed by files."
(interactive)
(helm-multi-occur
(delq nil
(mapcar (lambda (b)
(when (buffer-file-name b) (buffer-name b)))
(buffer-list)))))
helm-buffers-list
Often you don't care about the exact occurrences of the query string, but want a
list of all buffers that contain it.
helm-buffers-list has some tricks up its sleeve. The first symbol you
specify is filtering by major-mode, and you can use the "#" prefix to narrow
the list to buffers that contain a string.
To wit, "ruby #prompt" will show you a list of buffers whose major-mode
contains "ruby" and whose contents contains "prompt". Or you can just use "#prompt" to show all buffers that contain "prompt".
Powerful and comfortable once you get used to it.
EDIT modified my-helm-multi-all to enable helm-follow-mode.
EDIT 2 update helm-follow-mode code to reflect helm changes.
EDIT 3 updated again to reflect helm changes
Emacs has Projectile satisfy your need:
jump to a file in project
multi-occur in project buffers
Heml is far from the fuzzy searching of ST3.
Fiplr looks promising but doesn't work on my laptop (see first issue on the github)
Simp.el looks like Fiplr but doesn't work either on my end.
Projectile works for me! Here's your solution!
I used also ido-mode and flx-ido for the fuzzy searching,
and for the vertical way of displaying results I use this in my .emacs:
;; Display ido results vertically, rather than horizontally
(setq ido-decorations (quote ("\n-> " "" "\n " "\n ..." "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]" " [Confirm]")))
(defun ido-disable-line-truncation () (set (make-local-variable 'truncate-lines) nil))
(add-hook 'ido-minibuffer-setup-hook 'ido-disable-line-truncation)
(defun ido-define-keys () ;; C-n/p is more intuitive in vertical layout
(define-key ido-completion-map (kbd "C-n") 'ido-next-match)
(define-key ido-completion-map (kbd "C-p") 'ido-prev-match))
(add-hook 'ido-setup-hook 'ido-define-keys)
Icicles offers some features that are similar to what it seems you are looking for.
C-x b and C-x C-f, to choose buffers or files, allow multi-completion: you can type a pattern to match the buffer/file name and/or a pattern to match content in the buffer/file. Candidates are filtered incrementally as you type (what you call "instant" is what Emacs calls "incremental"). You can refine either or both search patterns progressively, narrowing the choices in different ways. You can visit any number of buffers/files that match, at the same time. You can also use the same method to search the marked files in Dired: C-F.
C-c `(icicle-search) incrementally searches across multiple buffers or files. Again, progressive refinement etc.
The main difference between #1 and #2 is this:
For #1, you just want to find matching buffers or files. You don't care immediately about finding particular occurrences --- any match suffices.
For #2, you provide the buffers or files to search, and you want to navigate among search hits.
You can also use #1 to locate the buffers and files you want, then search their contents: The content-matching pattern you last used is available as the search pattern for Isearch (C-s).
for emacs I customize and modify this solution (for use install helm):
(defun helm-occur-from-point (initial-value)
"Invoke `helm-occur' from point."
(interactive)
(let ((input initial-value)
(bufs (list (buffer-name (current-buffer)))))
;; (isearch-exit)
(helm-occur-init-source)
(helm-attrset 'moccur-buffers bufs helm-source-occur)
(helm-set-local-variable 'helm-multi-occur-buffer-list bufs)
(helm-set-local-variable
'helm-multi-occur-buffer-tick
(cl-loop for b in bufs
collect (buffer-chars-modified-tick (get-buffer b))))
(helm :sources 'helm-source-occur
:buffer "*helm occur*"
:history 'helm-grep-history
:input input
:truncate-lines t)))
(defun get-point-text ()
"Get 'interesting' text at point; either word, or region"
(if mark-active
(buffer-substring (mark) (point))
(thing-at-point 'symbol)))
(defun helm-occur-1 (initial-value)
"Preconfigured helm for Occur with initial input."
(helm-occur-from-point initial-value))
(defun bk-helm-occur ()
"Invoke helm-occur with initial input configured from text at point"
(interactive)
(helm-occur-1 (get-point-text)))
(global-set-key (kbd "M-s-o") 'bk-helm-occur)
primary it based on
#see https://news.ycombinator.com/item?id=6872508 but on last helm versions not work but fixed with my changes (just copy/paste from some internal helm modules)

Getting vc-diff to use ediff in Emacs 23.2

Had this working well in Emacs 23.1.x but it appears to have broke in the move to Emacs 23.2
I want to use ediff when comparing working copy of a file with SVN HEAD.
Normally I press C-x v = and ediff runs because of the following configuration in my .emacs
;; Use ediff and not diff
(setq diff-command "ediff")
But, alas I still get the normal vc-diff buffer appearing and no ediff session...
Has anyone else encountered this and know what might be the problem?
Am a bit skeptical that the above setting did what you say it did.
That said, this will bind '=' to use 'ediff-revision:
(eval-after-load "vc-hooks"
'(define-key vc-prefix-map "=" 'ediff-revision))
I use the command vc-ediff to skip entering the file names: just compare the current modified copy with the base version (HEAD).
(eval-after-load "vc-hooks"
'(define-key vc-prefix-map "=" 'vc-ediff))
Then C-x v = will bring up the Ediff session.
Found out I could just rebind C-x v = to the following:
(defun ediff-current-buffer-revision ()
"Run Ediff to diff current buffer's file against VC depot.
Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
(interactive)
(let ((file (or (buffer-file-name)
(error "Current buffer is not visiting a file"))))
(if (and (buffer-modified-p)
(y-or-n-p (message "Buffer %s is modified. Save buffer? "
(buffer-name))))
(save-buffer (current-buffer)))
(ediff-load-version-control)
(funcall
(intern (format "ediff-%S-internal" ediff-version-control-package))
"" "" nil)))
This approach means you avoid having to specify the versions to compare as it defaults to comparing HEAD and the current file state.
Source: http://www.groupsrv.com/computers/about152826.html