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

后端 未结 3 620
鱼传尺愫
鱼传尺愫 2020-12-15 03:25

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

3条回答
  •  时光说笑
    2020-12-15 04:01

    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.

提交回复
热议问题