emacs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. ;; Always revert files, i.e. reload them to buffers if emacs notices
  2. ;; them changing by e.g. an external program.
  3. (global-auto-revert-mode 1)
  4. ;; Global keys for using Org-mode
  5. ;; Read the tutorial at http://orgmode.org/worg/org-tutorials/orgtutorial_dto.html
  6. ;; Load the MELPA package library
  7. (require 'package)
  8. (add-to-list 'package-archives
  9. '("melpa-stable" . "https://stable.melpa.org/packages/") t)
  10. (package-initialize)
  11. (setq mail-user-agent 'gnus-user-agent)
  12. ;; Constants
  13. (setq keepass-folder "~/Sync/keepass/"
  14. bibliography-folder "~/Documents/bibliography/")
  15. ;; BBDB settings
  16. (require 'bbdb)
  17. (setq bbdb-file (concat keepass-folder "bbdb"))
  18. (bbdb-initialize 'gnus 'message)
  19. (bbdb-mua-auto-update-init 'gnus 'message)
  20. ;; Display BBDB in popup window
  21. (setq bbdb-mua-pop-up t)
  22. (setq bbdb-mua-pop-up-window-size 3)
  23. ;; Look for existing contact, interactively prompt to create
  24. (setq bbdb-mua-update-interactive-p '(query . create))
  25. ;; Nyan-mode.
  26. (require 'nyan-mode)
  27. (nyan-mode 1)
  28. (nyan-start-animation)
  29. ;; Company (complete anything) mode
  30. (require 'company)
  31. (add-hook 'latex-mode-hook 'company-mode)
  32. (setq company-tooltip-align-annotations 1
  33. company-minimum-prefix-length 3
  34. company-idle-delay 0.0)
  35. ;; SLIME + company (For Common LISP)
  36. (require 'slime)
  37. (setq inferior-lisp-program "clisp")
  38. (slime-setup '(slime-company))
  39. (slime-setup '(slime-fancy))
  40. ;; Geiser-Chez (For Chez Scheme), start with M-x run-geiser
  41. (require 'geiser-chez)
  42. ;; Don't use text color when rendering html using shr (links ok)
  43. (setq shr-use-colors nil)
  44. ;; Expand region see https://github.com/magnars/expand-region.el
  45. (require 'expand-region)
  46. (global-set-key (kbd "<f11>") 'er/expand-region)
  47. (delete-selection-mode 1)
  48. ;; Smartparens
  49. (require 'smartparens-config)
  50. (add-hook 'lisp-mode-hook 'show-paren-mode)
  51. (add-hook 'lisp-mode-hook 'smartparens-mode)
  52. (add-hook 'latex-mode-hook 'show-paren-mode)
  53. (add-hook 'latex-mode-hook 'smartparens-mode)
  54. (add-hook 'markdown-mode-hook 'smartparens-mode)
  55. ;; Make new buffers split the window vertically instead of horizontally
  56. (setq split-height-threshold 10)
  57. (setq split-width-threshold 80)
  58. ;; Winner mode: C-c ← and C-c → for going between used window configurations
  59. (winner-mode 1)
  60. ;; Word wrap
  61. (setq-default word-wrap t)
  62. ;; Unfill paragraph. E.g. when needing to copy to an external program.
  63. (defun unfill-paragraph ()
  64. "Takes a multi-line paragraph and makes it into a single line of text."
  65. (interactive)
  66. (let ((fill-column (point-max)))
  67. (fill-paragraph nil)))
  68. ;; Handy key definition: alt-Q (alt+shift+q)
  69. (define-key global-map "\M-Q" 'unfill-paragraph)
  70. ;; Windmove
  71. (windmove-default-keybindings)
  72. (setq org-replace-disputed-keys t) ; This must be loaded before org-mode
  73. (require 'org)
  74. (define-key global-map "\C-cl" 'org-store-link)
  75. (define-key global-map "\C-ca" 'org-agenda)
  76. (setq org-log-done 'time)
  77. ;; The below is so that PDF-files or others can be opened with \C-o in org-mode
  78. ;; It makes xdg-open work in eshell. See
  79. ;; https://askubuntu.com/questions/646631/emacs-doesnot-work-with-xdg-open
  80. ;; and
  81. ;; https://emacs.stackexchange.com/questions/19344/why-does-xdg-open-not-work-in-eshell
  82. ;; WARNING: it might break other things. Some have mentioned gnuplot.
  83. (setq process-connection-type nil)
  84. ;; Allow refiling to level 9 branches with C-c C-w
  85. (setq org-refile-targets '((org-agenda-files :maxlevel . 9)))
  86. ;; In calendar.org refilling C-c C-w will put to beginning of list instead of end
  87. (setq org-reverse-note-order '(("calendar.org" . t)))
  88. ;; Show e.g. Work/Research/LUT/self-improvement instead of just self-improvement
  89. (setq org-refile-use-outline-path t)
  90. ;; Show done recurring tasks in org-mode log (agenda, then press l)
  91. (setq org-agenda-log-mode-items '(closed clock state))
  92. (setq org-clock-idle-time 15)
  93. ;; Let the agenda always show 3 days in advance instead of the current week
  94. (setq org-agenda-start-on-weekday 1) ; nil means current day
  95. (setq org-agenda-span 7)
  96. ;; Make sure org-latex uses the OT1 font encoding instead of OT. The
  97. ;; latter has licensing issues in Parabola GNU/Linux. If the defaults
  98. ;; of "org-latex-default-packages-alist" should ever change, then run
  99. ;; "M-x apropos RET org-latex-default-packages-alist" and read them
  100. ;; from there. Also, to preview a LaTeX formula when using X, run C-c
  101. ;; C-x C-l.
  102. (with-eval-after-load 'org
  103. (setq org-latex-default-packages-alist
  104. '(("AUTO" "inputenc" t)
  105. ("OT1" "fontenc" t)
  106. ("" "fixltx2e" nil)
  107. ("" "graphicx" t)
  108. ("" "longtable" nil)
  109. ("" "float" nil)
  110. ("" "wrapfig" nil)
  111. ("" "rotating" nil)
  112. ("normalem" "ulem" t)
  113. ("" "amsmath" t)
  114. ("" "textcomp" t)
  115. ("" "marvosym" t)
  116. ("" "wasysym" t)
  117. ("" "amssymb" t)
  118. ("" "hyperref" nil)
  119. "\\tolerance=1000")
  120. )
  121. )
  122. ;; Make the formulas larger when previewing:
  123. ;;(with-eval-after-load 'org
  124. ;; (setq org-format-latex-options (plist-put org-format-latex-options :scale 1.5))
  125. ;; )
  126. ;; Load emacs speaks statistics
  127. ;;(add-to-list 'load-path "/usr/share/emacs/site-lisp/ess")
  128. ;;(require 'ess-site)
  129. ;; Allow executing "GNU calc" code blocks in org-mode
  130. (org-babel-do-load-languages
  131. 'org-babel-load-languages
  132. '((calc . t)
  133. (octave . t)
  134. (R . t)
  135. (python . t)
  136. (shell . t)))
  137. ;; Make the keyboard sortcut "< s TAB" create a code block that also
  138. ;; exports the results when exporting.
  139. (add-to-list 'org-structure-template-alist
  140. '("s" "#+BEGIN_SRC ? :exports both\n\n#+END_SRC"))
  141. ;; Add syntax highlighting in code blocks
  142. (setq org-src-fontify-natively t)
  143. ;; Use tab to indent inside code blocks
  144. (setq org-src-tab-acts-natively t)
  145. ;; Tell emacs to opem .m-files in the Octave-mode
  146. (add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))
  147. ;; Firstly, make C-x C-m always compile. Secondly make the compilation
  148. ;; always run "make -k", unless a prefix argument has been given,
  149. ;; e.g. C-u C-x C-m.
  150. (global-set-key "\C-x\C-m" 'compile)
  151. (setq compilation-read-command nil
  152. compilation-scroll-output t)
  153. ;; Tell emacs to use the current emacs cursor position for pasting,
  154. ;; instead of first moving to the mouse cursor position and then
  155. ;; pasting.
  156. (setq mouse-yank-at-point t)
  157. ;; Tell emacs to align at equal sign in bibtex files. Do the aligning
  158. ;; ourself with C-c C-q.
  159. (setq bibtex-align-at-equal-sign t)
  160. ;; Reftex
  161. (require 'reftex)
  162. (add-hook 'latex-mode-hook 'turn-on-reftex)
  163. ;; Set same TODO list for all. Individually it should be:
  164. ;;#+SEQ_TODO: TODO(t) WAIT(w!/!) MEET(m) | DONE(d) CANCELLED(c@)
  165. (setq calendar-file (concat keepass-folder "calendar.org"))
  166. (setq org-todo-keywords
  167. '((sequence "TODO(t)" "WAIT(w!/!)" "MEET(m)" "IDEA(i)" "|" "DONE(d)" "CANCELLED(c@)")))
  168. (setq org-todo-keyword-faces
  169. '(("MEET" . (:foreground "#486f66" :background "#f4bbce" :weight ultra-bold))))
  170. ;; Define custom org-capture templates
  171. ;; see http://cachestocaches.com/2016/9/my-workflow-org-agenda/
  172. (setq org-capture-templates
  173. '(("t" "todo" entry (file+headline calendar-file "Unclassified")
  174. "* TODO %? %A")
  175. ("m" "Meeting" entry (file+headline calendar-file "Unclassified")
  176. "* MEET %? %A")))
  177. ;; Use the C-c c key globally to create an org-item from the context
  178. (define-key global-map "\C-cc" 'org-capture)
  179. ;; org-agenda files, can be directories (doesn't look at subdirs) or files
  180. (setq org-agenda-files (list calendar-file))
  181. ;; Remove unnecessary " calendar: " from each entry on the left.
  182. (setq org-agenda-prefix-format
  183. '((agenda . " %i %?-12t% s") ; default: " %i %-12:c%?-12t% s"
  184. (todo . " %i %-12:c")
  185. (tags . " %i %-12:c")
  186. (search . " %i %-12:c")))
  187. (setq org-agenda-remove-tags t)
  188. ;; Never use tabs, always use spaces for indenting
  189. (setq indent-tabs-mode nil)
  190. ;; Use 2 spaces when indenting CSS, JS and shell instead of the default 4
  191. (setq css-indent-offset 2)
  192. (setq js-indent-level 2)
  193. (setq sh-indentation 2)
  194. ;; Helm-Bibtex
  195. ;; See https://github.com/tmalsburg/helm-bibtex
  196. (autoload 'helm-bibtex "helm-bibtex" "" t)
  197. (setq bibtex-completion-bibliography (list (concat bibliography-folder "references.bib"))) ; needs (list ...) for new org-ref to work
  198. (setq bibtex-completion-library-path (list (concat bibliography-folder "bibtex-pdfs/"))) ; needs (list ...) for new org-ref to work
  199. (setq bibtex-completion-pdf-field "File")
  200. (setq bibtex-completion-notes-path (concat bibliography-folder "notes.org"))
  201. (setq bibtex-completion-additional-search-fields '(keywords tags))
  202. (setq bibtex-completion-pdf-symbol "⌘")
  203. (setq bibtex-completion-notes-symbol "✎")
  204. ;; Requires a newer version of bibtex-completion!
  205. ;; (setq bibtex-completion-pdf-extension '(".pdf" ".djvu"))
  206. (setq while-no-input-ignore-events '())
  207. ;; See this also:
  208. ;; https://github.com/tmalsburg/helm-bibtex#create-a-bibtex-file-containing-only-specific-entries
  209. (require 'helm)
  210. (global-set-key (kbd "<f12>") 'helm-command-prefix) ;; use <apps> for the menu button in other OS's, or for example <f12> for Termux and see its wiki for showing this button on its interface. Also <X86Launch1> works in Mate desktop, but not terminals.
  211. (define-key helm-command-map "b" 'helm-bibtex)
  212. (define-key helm-command-map "B" 'helm-bibtex-with-local-bibliography)
  213. (define-key helm-command-map "n" 'helm-bibtex-with-notes)
  214. (define-key helm-command-map (kbd "<f12>") 'helm-resume)
  215. (define-key helm-command-map "c" 'org-ref-helm-insert-cite-link)
  216. (define-key helm-command-map "p" 'org-ref-open-pdf-at-point)
  217. (define-key helm-command-map "r" 'org-ref-helm-load-completions-async)
  218. ;; sort helm-bibtex in the same order as the bib file
  219. (eval-after-load "helm-bibtex"
  220. '(advice-add 'bibtex-completion-candidates
  221. :filter-return 'reverse))
  222. ;; org-ref config
  223. (setq reftex-default-bibliography (list (concat bibliography-folder "references.bib")))
  224. (setq org-ref-bibliography-notes (concat bibliography-folder "notes.org")
  225. org-ref-default-bibliography (list (concat bibliography-folder "references.bib"))
  226. org-ref-pdf-directory (concat bibliography-folder "bibtex-pdfs/"))
  227. (require 'doi-utils)
  228. (require 'org-ref-isbn)
  229. (require 'org-ref-pubmed)
  230. (require 'org-ref-arxiv)
  231. (require 'x2bib)
  232. ;; Open pdf's as org-mode does (see above).
  233. (setq bibtex-completion-pdf-open-function 'org-open-file)
  234. ;; Journal abbreviations
  235. (add-to-list 'org-ref-bibtex-journal-abbreviations
  236. '("IUMJ" "Indiana University Mathematics Journal" "Indiana Univ. Math. J."))
  237. ;; Sci-hub
  238. (defun sci-hub-pdf-url (doi)
  239. "Get url to the pdf from SCI-HUB"
  240. (setq *doi-utils-pdf-url* (concat "https://sci-hub.st/" doi) ;captcha
  241. *doi-utils-waiting* t
  242. )
  243. ;; try to find PDF url (if it exists)
  244. (url-retrieve (concat "https://sci-hub.st/" doi)
  245. (lambda (status)
  246. (goto-char (point-min))
  247. (while (search-forward-regexp "\\(https://\\|//sci-hub.st/downloads\\).+download=true'" nil t)
  248. (let ((foundurl (match-string 0)))
  249. (message foundurl)
  250. (if (string-match "https:" foundurl)
  251. (setq *doi-utils-pdf-url* foundurl)
  252. (setq *doi-utils-pdf-url* (concat "https:" foundurl))))
  253. (setq *doi-utils-waiting* nil))))
  254. (while *doi-utils-waiting* (sleep-for 0.1))
  255. *doi-utils-pdf-url*)
  256. (defun doi-utils-get-bibtex-entry-pdf (&optional arg)
  257. "Download pdf for entry at point if the pdf does not already exist locally.
  258. The entry must have a doi. The pdf will be saved to
  259. `org-ref-pdf-directory', by the name %s.pdf where %s is the
  260. bibtex label. Files will not be overwritten. The pdf will be
  261. checked to make sure it is a pdf, and not some html failure
  262. page. You must have permission to access the pdf. We open the pdf
  263. at the end if `doi-utils-open-pdf-after-download' is non-nil.
  264. With one prefix ARG, directly get the pdf from a file (through
  265. `read-file-name') instead of looking up a DOI. With a double
  266. prefix ARG, directly get the pdf from an open buffer (through
  267. `read-buffer-to-switch') instead. These two alternative methods
  268. work even if the entry has no DOI, and the pdf file is not
  269. checked."
  270. (interactive "P")
  271. (save-excursion
  272. (bibtex-beginning-of-entry)
  273. (let ( ;; get doi, removing http://dx.doi.org/ if it is there.
  274. (doi (replace-regexp-in-string
  275. "https?://\\(dx.\\)?.doi.org/" ""
  276. (bibtex-autokey-get-field "doi")))
  277. (key (cdr (assoc "=key=" (bibtex-parse-entry))))
  278. (pdf-url)
  279. (pdf-file))
  280. (setq pdf-file (concat
  281. (if org-ref-pdf-directory
  282. (file-name-as-directory org-ref-pdf-directory)
  283. (read-directory-name "PDF directory: " "."))
  284. key ".pdf"))
  285. ;; now get file if needed.
  286. (unless (file-exists-p pdf-file)
  287. (cond
  288. ((and (not arg)
  289. doi
  290. (if (doi-utils-get-pdf-url doi)
  291. (setq pdf-url (doi-utils-get-pdf-url doi))
  292. (setq pdf-url "https://www.sciencedirect.com/science/article/")))
  293. (url-copy-file pdf-url pdf-file)
  294. ;; now check if we got a pdf
  295. (if (org-ref-pdf-p pdf-file)
  296. (message "%s saved" pdf-file)
  297. (delete-file pdf-file)
  298. ;; sci-hub fallback option
  299. (setq pdf-url (sci-hub-pdf-url doi))
  300. (url-copy-file pdf-url pdf-file)
  301. ;; now check if we got a pdf
  302. (if (org-ref-pdf-p pdf-file)
  303. (message "%s saved" pdf-file)
  304. (delete-file pdf-file)
  305. (message "No pdf was downloaded.") ; SH captcha
  306. (browse-url pdf-url))))
  307. ;; End of sci-hub fallback option
  308. ((equal arg '(4))
  309. (copy-file (expand-file-name (read-file-name "Pdf file: " nil nil t))
  310. pdf-file))
  311. ((equal arg '(16))
  312. (with-current-buffer (read-buffer-to-switch "Pdf buffer: ")
  313. (write-file pdf-file)))
  314. (t
  315. (message "We don't have a recipe for this journal.")))
  316. (when (and doi-utils-open-pdf-after-download (file-exists-p pdf-file))
  317. (org-open-file pdf-file))))))
  318. ;; Asymptote mode
  319. ;; if "asy" is not on path, use:
  320. ;;(add-to-list 'load-path "ASYDIR")
  321. ;; for compiling with C-c C-c
  322. (autoload 'asy-mode "asy-mode.el" "Asymptote major mode." t)
  323. (autoload 'lasy-mode "asy-mode.el" "hybrid Asymptote/Latex major mode." t)
  324. (autoload 'asy-insinuate-latex "asy-mode.el" "Asymptote insinuate LaTeX." t)
  325. (add-to-list 'auto-mode-alist '("\\.asy$" . asy-mode))
  326. ;; Set color theme (generated using M-x customize-theme)
  327. (custom-set-variables
  328. ;; custom-set-variables was added by Custom.
  329. ;; If you edit it by hand, you could mess it up, so be careful.
  330. ;; Your init file should contain only one such instance.
  331. ;; If there is more than one, they won't work right.
  332. '(ansi-color-faces-vector
  333. [default default default italic underline success warning error])
  334. '(custom-enabled-themes (quote (dracula)))
  335. '(custom-safe-themes
  336. (quote
  337. ("fe1c13d75398b1c8fd7fdd1241a55c286b86c3e4ce513c4292d01383de152cb7" default)))
  338. '(org-file-apps
  339. (quote
  340. ((auto-mode . emacs)
  341. ("\\.mm\\'" . default)
  342. ("\\.x?html?\\'" . default)
  343. ("\\.pdf\\'" . "xdg-open %s")
  344. ("\\.djvu\\'" . "xdg-open %s"))))
  345. '(org-ref-clean-bibtex-entry-hook
  346. (quote
  347. (org-ref-bibtex-format-url-if-doi orcb-key-comma orcb-& orcb-% org-ref-title-case-article orcb-clean-year orcb-key orcb-clean-doi orcb-clean-pages orcb-check-journal org-ref-sort-bibtex-entry)))
  348. '(package-selected-packages
  349. (quote
  350. (auth-source-xoauth2 oauth2 bbdb geiser-chez geiser graphviz-dot-mode slime-company slime company smartparens expand-region nyan-mode helm-bibtex helm dracula-theme suomalainen-kalenteri dash bibtex-completion org-ref magit markdown-mode))))
  351. ;; Set markdown-command to run pandoc
  352. (setq markdown-command "pandoc")
  353. ;; Use HTML5 and UTF-8 for markdown export to html
  354. (eval-after-load "markdown-mode"
  355. '(defalias 'markdown-add-xhtml-header-and-footer 'as/markdown-add-xhtml-header-and-footer))
  356. (defun as/markdown-add-xhtml-header-and-footer (title)
  357. "Wrap XHTML header and footer with given TITLE around current buffer."
  358. (goto-char (point-min))
  359. (insert "<!DOCTYPE html5>\n"
  360. "<html>\n"
  361. "<head>\n<title>")
  362. (insert title)
  363. (insert "</title>\n")
  364. (insert "<meta charset=\"utf-8\" />\n")
  365. (when (> (length markdown-css-paths) 0)
  366. (insert (mapconcat 'markdown-stylesheet-link-string markdown-css-paths "\n")))
  367. (insert "\n</head>\n\n"
  368. "<body>\n\n")
  369. (goto-char (point-max))
  370. (insert "\n"
  371. "</body>\n"
  372. "</html>\n"))
  373. ;; Emacs-magit
  374. (require 'magit)
  375. ;; Make C-x g show the magit status
  376. (define-key global-map "\C-xg" 'magit-status)