How do I make Org-mode open PDF files in Evince? - pdf

In Org-mode when I try to open a link to a PDF file nothing happens. Also, when I do C-c C-e d to export as LaTeX and process to PDF and open the PDF is generated but not opened. How do I make Org-mode open PDF files in Evince?
I am using Org-mode 7.6 in GNU Emacs 23.3.1 and Evince 3.2.1 in Ubuntu 11.10.

M-x customize-variable [RET] org-file-apps [RET]
If org uses your system defaults, you have to edit your ./mailcap file.
Try adding this line:
application/pdf; /usr/bin/evince %s

Another possible construct that could work for this would be to use eval-after-load rather than add-hook. It will only set the values once on startup, you won't have to worry about entries being added or not (unless you regularly reload org).
Combine that with setcdr and you can avoid having to delete from the list and then re-add, add if and you'll ensure that you either add or change the value. The if is only needed for values that aren't in the list by default, just to make sure you don't end up with conflicts somewhere down the line.
(eval-after-load "org"
'(progn
;; .txt files aren't in the list initially, but in case that changes
;; in a future version of org, use if to avoid errors
(if (assoc "\\.txt\\'" org-file-apps)
(setcdr (assoc "\\.txt\\'" org-file-apps) "notepad.exe %s")
(add-to-list 'org-file-apps '("\\.txt\\'" . "notepad.exe %s") t))
;; Change .pdf association directly within the alist
(setcdr (assoc "\\.pdf\\'" org-file-apps) "evince %s")))
Edit for clarification
eval-after-load only evaluates the block when (require 'org) is called. If org is already loaded it will evaluate immediately (I mistakenly thought it ran each time a library was loaded, but it seems to be only the first time). The difference between add-hook and eval-after-load is explained here.
Since org-file-apps is a defcustom it won't change the values if you set them before org is loaded, if you build the list from scratch (including default values as in your second (uglier) solution) you could simply setq in your init.el and everything would work. It also means it won't overwrite your changes.
Adding (if (assoc to the PDF entry won't hurt anything, it will simply ensure that if PDFs are ever removed from the default org-file-apps that it will still be added. The only solution that would not fail if PDFs were removed is your second one. The others all assume the entry exists in one form or another.

You can use a construct similar to https://stackoverflow.com/a/3985552/789593 but adapt it to PDF files and Evince. What you want to do is to alter the list org-file-apps. This can be done by adding the following to your .emacs:
;; PDFs visited in Org-mode are opened in Evince (and not in the default choice) https://stackoverflow.com/a/8836108/789593
(add-hook 'org-mode-hook
'(lambda ()
(delete '("\\.pdf\\'" . default) org-file-apps)
(add-to-list 'org-file-apps '("\\.pdf\\'" . "evince %s"))))
This will delete the default setting for PDF files and instead open them in Evince (and retain everything else included in org-file-apps). I am new to elisp so I do not know if this solution is robust, but it works for me and seems to be more elegant than the one below.
Another option, which seems uglier, is to instead look up the default values and set them all that but change the value for PDF files:
;; PDFs visited in Org-mode are opened in Evince (and other file extensions are handled according to the defaults)
(add-hook 'org-mode-hook
'(lambda ()
(setq org-file-apps
'((auto-mode . emacs)
("\\.mm\\'" . default)
("\\.x?html?\\'" . default)
("\\.pdf\\'" . "evince %s")))))

Related

How do I control into which directory Emacs makes backup files dynamically?

I've seen the post How do I control how Emacs makes backup files? And a few similar post showing very similar solutions. I'm well aware of this approach. But it doesn't do quite what I would like it to do. I would like each file that I'm going to back up have its own personalized backup directory.
For example, let's say I have the following files in the current directory, /Users/me/project_a/
apple.txt
banana.txt
coconut.txt
when I edit these files, I would like them to have their backups stored in directories as follows:
/Users/me/project_a/.backups/apple.txt/
/Users/me/project_a/.backups/banana.txt/
/Users/me/project_a/.backups/coconut.txt/
If I another project directory, say /Users/me/project_b/, with files
needle.doc
thread.doc
thimble.doc
Then, their respective backups should be located as
/Users/me/project_b/.backups/needle.doc/
/Users/me/project_b/.backups/thread.doc/
/Users/me/project_b/.backups/thimble.doc/
Yes, I'm using the name of the file being backed up as part of the path name for the directory into which it's been saved. So, if I have three previous versions of thimble.doc, the full path name for the backups would be:
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~1~
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~2~
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~3~
I have a work around to accomplish that (see below). Ideally, I would name the backups as:
/Users/me/project_b/.backups/thimble.doc/bak.~1~
/Users/me/project_b/.backups/thimble.doc/bak.~2~
/Users/me/project_b/.backups/thimble.doc/bak.~3~
I haven't yet figured out how to get there. (Any suggestions?)
Here's how I'm able to accomplish the less ideal version of this using the .dir-locals.el file with the following code for saving .txt and .tex files.
(let (a b)
(dolist (ae-fh (directory-files-recursively "." "\\.\\(txt\\|tex\\)$"))
(setq a (file-name-nondirectory ae-fh))
(setq a (replace-regexp-in-string "\\." "\\\\." a))
(setq b (concat "./.backups/" (file-name-nondirectory ae-fh)))
(add-to-list 'backup-directory-alist (cons a b))
))
I've already made backup-directory-alist a buffer-local variable.
This accomplishes what I want, but I don't really like this approach. I would like to altogether avoid using .dir-locals.el as a solution to this problem. But also, I would like to avoid junking up the backup-directory-alist. It would be nice if there were a hook I could apply to the backup process which would inform emacs on the fly how it should name the backup file.
Does anyone know how to do this or know where to point me? I've considered trying to redefine make-backup-file-name-function, but I don't entirely understand what I'm doing with the elisp and right now I've got projects that need to be worked on.
What can folks tell me?
I have a partial solution.
It avoids using .dir-locals.el as I want.
It somewhat creates the path on the fly as I want too.
It still uses backup-directory-alist
I add the following to my emacs init file:
(add-hook 'after-change-major-mode-hook
'(lambda ()
(let (a b)
(if (stringp (buffer-file-name))
(progn
(setq a (file-name-nondirectory (buffer-file-name)))
(setq a (replace-regexp-in-string "\\." "\\\\." a))
(setq b (concat "./.backups/" (file-name-nondirectory (buffer-file-name))))
(add-to-list 'backup-directory-alist (cons a b))
)
)
)))
I don't quite know enough about this hook. So, I'm not sure that this will always work. And, I should probably consider whether there might be files for which this convention really is undesirable.
The (stringp (buffer-file-name)) is minimally necessary in the case of buffers for which there is no underlying file: such as *scratch* or even the mini-buffers.
I am still interested in what solutions others might come up with. Ideally, the solution would be more like
/Users/me/project_b/.backups/thimble.doc/bak.~1~
/Users/me/project_b/.backups/thimble.doc/bak.~2~
/Users/me/project_b/.backups/thimble.doc/bak.~3~
than
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~1~
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~2~
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~3~
UPDATE: Persistence will get you places!!!!
I finally did a google search on: "emacs how do i redefine make-backup-file-name". I found this description of an approach which I adapted to my situation. Namely, I did the following:
(defun z:backup:truncate.backup.name (file)
(concat (file-name-directory file) "bak"))
(advice-add 'make-backup-file-name-1 :filter-return #'z:backup:truncate.backup.name)
I'm not entirely sure of what everything is about.
I don't know what the #' syntax does.
I'm not sure what :filter-return does, though I can guess.
I should point out that I still need mae add-hook for 'after-change-major-mode-hook. Without it, I get errors. Apparently, I just can't write the advice function as
(defun z:backup:truncate.backup.name (file)
(concat (file-name-directory file) (file-name-nondirectory) "/bak"))
I tried it and got an error about not being able to back things up. And I think the reason for this is that backup-directory-alist is used to determine the directory path. I've got some ideas for some tweaks here. But this is where things stand currently.
UPDATE: no more need for my add-hook to after-change-major-mode-hook
I've gotten rid of the issue with the backup directory alist not necessarily being set for the correct directory. This is my final form for z:backup:truncate.backup.name
(defun z:backup:truncate.backup.name ()
(let* (
(dir (file-name-directory (buffer-file-name)))
(fh (file-name-nondirectory (buffer-file-name)))
(qdir (concat dir ".backups/" fh))
)
(if (not (file-directory-p qdir))
(progn
(if (file-directory-p dir)
(progn
(make-directory qdir)
))))
(concat qdir "/bak")))
So now more having to tweak backup-directory-alist. I've accomplished everything I wanted. YEAH.

Unable to batch process through command-line

(Under Windows 7 x64)
I originally tried to use BIMP to apply a cutout effect to a folder but FU-cutout is one of the few that doesn't appear in the plug-in's list. I read somewhere that was because I hadn't install GIMP while customizing to allow compatibility with older plug-in and I de/reinstalled to try and fix this to no avail. So I decided to use script-fu instead but find myself unable to make it work out through the command line.
I have checked on a few sites explaining how to batch script (including the official GIMP doc) and ended with the follow code.
(define (hmd-batch-stylize pattern colours smoothness)
(let* ( (filelist (cadr (file-glob pattern 1) ) )
)
(while (not (null? filelist))
(let* ((filename (car filelist))
(image (car (gimp-file-load RUN-NONINTERACTIVE
filename filename)))
(drawable (car (gimp-image-get-active-layer image)))
)
(gimp-brightness-contrast drawable 35 40)
(FU-cutout RUN-NONINTERACTIVE
image drawable colours smoothness TRUE)
(gimp-file-save RUN-NONINTERACTIVE
image drawable filename filename)
(gimp-image-delete image)
)
(set! filelist (cdr filelist))
)
))
(I also register the script but for the sake of simplicity I won't add this code here). Then I open the command line and write
gimp -i -b '(hmd-batch-stylize "C:\Users\User\Desktop\3\*jpg" 32 1)' -b '(gimp-quit 0)'
in the gimp directory (I tried the directory where my files are but gimp isn't recognized as a command there). The gimp console starts up, fails to execute a bunch of dlls and a folder (all part of GMIC) with the mention "Exec format error", then I get two "Batch command completed successfully" at which point the command line freezes until I close it.
I searched online for days hoping to find a solution but no one seems to have the same problem as I do so no solution has worked yet. Help would be greatly appreciated.
PS: I thought of instead creating a script that would appear in BIMP and act as a middle-man to access the FU-cutout but I can't find how to pass the current image/filename being used by BIMP as an argument. If anyone knows how, I feel that might be my simplest way of working around this problem.

elisp: generate LaTeX PDF document

I trying to completely automatize sending job applications. First step, to put the name of the company in a letter. It almost works, but it is stuck because it asks what command to use. Reading the documentation, I thought it could be disabled by a prefix argument, but I got something wrong. Also, it doesn't need to flash by visually, it could be done completely as a background process. I'll paste the code and you'll understand immediately:
(Oh, I'm using LaTeX/P mode in emacs - the goal is to not only update the .tex but also the .pdf file)
(defun edit-letter (comp-name)
(let ((path "~/work/letter/comp"))
(edit-letter-file-path comp-name (concat path "/eng/letter.tex"))
(edit-letter-file-path comp-name (concat path "/swe/brev.tex")) ))
(defun edit-letter-file-path (company-name file-path)
(find-file file-path)
(goto-line 14)
(kill-line)
(insert (format "\\textbf{To %s}\n" company-name))
(setq current-prefix-arg nil)
(call-interactively 'TeX-command-master) ; asks what command
(kill-buffer) ) ; doesn't work
(edit-letter "Digital Power Now")
It's not entirely clear from your question what you're after, but if you want to use AucTeX to call a LaTeX/PDFTex/BibTex process without getting prompted for the command name, you can use this:
(TeX-command "LaTeX" 'TeX-master-file)
Try this in place of (call-interactively 'TeX-command-master) above. When you're using LaTeX/P "LaTeX" really means pdflatex.

.tex files not found when placed in a folder declared in TEXINPUTS

I have a set of documents based on a LaTeX template. Every document has its own folder, as following:
docs-folder
|-doc #1
|-doc #2
...
|-doc #n
|-texmf
|-tex
|-bibtex
|-fonts
|-docs
|-misc
|-logo.jpg
|-acronyms.tex
I wrote the template on my own, and, for every document (from #1 to #n) it loads the files logo.jpg and acronyms.tex with \includegraphics{logo.jpg} and \input{acronyms.tex}.
The path ..\docs-folder\texmf is set as a project root in MikTeX, and the local texmf tree is recognized properly, excluding the misc folder.
So, the path ..\docs-folder\texmf\misc is set as the value of the TEXINPUTS environment variable (under Windows). This is done in order to avoid an unwanted replication of the two files.
What happens is that, when I compile one of the documents, the file acronyms.tex is not found, while the logo.jpg is correctly found by PDFLaTeX.
I have no wonder why the acronyms.tex file is not loaded.
On unix systems the solution is to run texhash or mktexlsr. According to this page, the equivalent solution for MikTeX is to run MikTeX settings and click the "Refresh FNDB" button.

Emacs mode question

I'm starting to use objective C, and objective C mode works perfectly fine. However, ObjC uses .h files, just like C and C++. I need to be able to use all three. Does anyone know how to get emacs to tell if it should be in objC mode or C/C++ mode?
EDIT: There seems to be some confusion on what I'm asking for. The problem is that I have some .h files that are associated with .m files, and some .h files that are associated with .c files and some that are associated with .cpp files. What I WANT is something that I can stick in my c-mode-common-hook or somewhere that will check to see if it's an objective C .h file, and then force it to objective C-mode, or something. The idea being that then I can just open a .h file and it will automatically be in the correct mode.
Obviously I can manually change mode when I'm in the file, but that's a pain, and I often forget until I hit tab and something wacky happens. This is the solution I'm using now.
I think that the header comment is probably the right way to do this, but in order for it to be TRULY useful, I need to figure out how to get XCode to put the comment in there when It's creating the file for me...
EDIT2:
I'm currently using the header-comment solution. Until I can figure out how to make XCode automatically add the comment, I'm using the following elisp function:
(defun bp-add-objC-comment ()
"Adds the /* -*- mode: objc -*- */ line at the top of the file"
(interactive)
(objc-mode)
(let((p (point)))
(goto-char 0)
(insert "/* -*- mode: objc -*- */\n")
(goto-char (+ p (length "/* -*- mode: objc -*- */\n")))))
You can put a comment like this in the first line of the file:
/* -*- mode: objc -*- */
or
// -*- mode: c++ -*-
as appropriate. More details in Specifying File Variables in the Emacs manual.
Ok, how about a solution that doesn't require putting a special comment in the file?
Check this out:
;; need find-file to do this
(require 'find-file)
;; find-file doesn't grok objc files for some reason, add that
(push ".m" (cadr (assoc "\\.h\\'" cc-other-file-alist)))
(defun my-find-proper-mode ()
(interactive)
;; only run on .h files
(when (string-match "\\.h\\'" (buffer-file-name))
(save-window-excursion
(save-excursion
(let* ((alist (append auto-mode-alist nil)) ;; use whatever auto-mode-alist has
(ff-ignore-include t) ;; operate on buffer name only
(src (ff-other-file-name)) ;; find the src file corresponding to .h
re mode)
;; go through the association list
;; and find the mode associated with the source file
;; that is the mode we want to use for the .h file
(while (and alist
(setq mode (cdar alist))
(setq re (caar alist))
(not (string-match re src)))
(setq alist (cdr alist)))
(when mode (funcall mode)))))))
(add-hook 'find-file-hook 'my-find-proper-mode)
This uses Emacs' built in package to find the corresponding .h/.cc files. So, if the header file you just opened corresponds to a file in c++-mode, the .h file gets put in that mode, if the source is a objc-mode file, the header gets put in that mode.
No special Emacs variables in the comments required.
I would use eproject to help me handle this. Assuming that I don't mix-and-match languages inside a given project, I could just create a .eproject file in the project root that contains the text:
:cc-header-type :objc
Then, I'd set some sort of default mode for .h files:
(add-to-list 'auto-mode-alist '("[.]h$" . c-mode))
Then, add a hook for that mode:
(add-hook 'c-mode-hook (lambda ()
(let ((header-style (eproject-attribute :cc-header-type)))
(when (and header-style
(string-match "[.]h$" (buffer-file-name)))
(case header-style
(:objc (objc-mode))
(:c++ (c++-mode))
(:c (c-mode))))))
Now the mode will be changed automatically according to the setting you make in the .eproject file. (Note that the .eproject file is not the only way to set variables for eproject; if you have a heuristic to detect objective c projects, eproject can get the variable that way also.)
Anyway, if this works for you, let me know so I can add it to the eproject wiki.