emacs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 nil)
  95. (setq org-agenda-span 3)
  96. ;; Use calfw https://github.com/kiwanami/emacs-calfw
  97. (require 'calfw)
  98. (require 'calfw-org)
  99. (global-set-key (kbd "<f9>") 'cfw:open-org-calendar)
  100. ;; Make sure org-latex uses the OT1 font encoding instead of OT. The
  101. ;; latter has licensing issues in Parabola GNU/Linux. If the defaults
  102. ;; of "org-latex-default-packages-alist" should ever change, then run
  103. ;; "M-x apropos RET org-latex-default-packages-alist" and read them
  104. ;; from there. Also, to preview a LaTeX formula when using X, run C-c
  105. ;; C-x C-l.
  106. (with-eval-after-load 'org
  107. (setq org-latex-default-packages-alist
  108. '(("AUTO" "inputenc" t)
  109. ("OT1" "fontenc" t)
  110. ("" "fixltx2e" nil)
  111. ("" "graphicx" t)
  112. ("" "longtable" nil)
  113. ("" "float" nil)
  114. ("" "wrapfig" nil)
  115. ("" "rotating" nil)
  116. ("normalem" "ulem" t)
  117. ("" "amsmath" t)
  118. ("" "textcomp" t)
  119. ("" "marvosym" t)
  120. ("" "wasysym" t)
  121. ("" "amssymb" t)
  122. ("" "hyperref" nil)
  123. "\\tolerance=1000")
  124. )
  125. )
  126. ;; Make the formulas larger when previewing:
  127. ;;(with-eval-after-load 'org
  128. ;; (setq org-format-latex-options (plist-put org-format-latex-options :scale 1.5))
  129. ;; )
  130. ;; Load emacs speaks statistics
  131. ;;(add-to-list 'load-path "/usr/share/emacs/site-lisp/ess")
  132. ;;(require 'ess-site)
  133. ;; Allow executing "GNU calc" code blocks in org-mode
  134. (org-babel-do-load-languages
  135. 'org-babel-load-languages
  136. '((calc . t)
  137. (octave . t)
  138. (R . t)
  139. (python . t)
  140. (shell . t)))
  141. ;; Make the keyboard sortcut "< s TAB" create a code block that also
  142. ;; exports the results when exporting.
  143. (add-to-list 'org-structure-template-alist
  144. '("s" "#+BEGIN_SRC ? :exports both\n\n#+END_SRC"))
  145. ;; Add syntax highlighting in code blocks
  146. (setq org-src-fontify-natively t)
  147. ;; Use tab to indent inside code blocks
  148. (setq org-src-tab-acts-natively t)
  149. ;; Tell emacs to opem .m-files in the Octave-mode
  150. (add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))
  151. ;; Firstly, make C-x C-m always compile. Secondly make the compilation
  152. ;; always run "make -k", unless a prefix argument has been given,
  153. ;; e.g. C-u C-x C-m.
  154. (global-set-key "\C-x\C-m" 'compile)
  155. (setq compilation-read-command nil
  156. compilation-scroll-output t)
  157. ;; Tell emacs to use the current emacs cursor position for pasting,
  158. ;; instead of first moving to the mouse cursor position and then
  159. ;; pasting.
  160. (setq mouse-yank-at-point t)
  161. ;; Tell emacs to align at equal sign in bibtex files. Do the aligning
  162. ;; ourself with C-c C-q.
  163. (setq bibtex-align-at-equal-sign t)
  164. ;; Reftex
  165. (require 'reftex)
  166. (add-hook 'latex-mode-hook 'turn-on-reftex)
  167. ;; Set same TODO list for all. Individually it should be:
  168. ;;#+SEQ_TODO: TODO(t) WAIT(w!/!) MEET(m) | DONE(d) CANCELLED(c@)
  169. (setq calendar-file (concat keepass-folder "calendar.org"))
  170. (setq org-todo-keywords
  171. '((sequence "TODO(t)" "WAIT(w!/!)" "MEET(m)" "IDEA(i)" "|" "DONE(d)" "CANCELLED(c@)")))
  172. (setq org-todo-keyword-faces
  173. '(("MEET" . (:foreground "#486f66" :background "#f4bbce" :weight ultra-bold))))
  174. ;; Define custom org-capture templates
  175. ;; see http://cachestocaches.com/2016/9/my-workflow-org-agenda/
  176. (setq org-capture-templates
  177. '(("t" "todo" entry (file+headline calendar-file "Unclassified")
  178. "* TODO %? %A")
  179. ("m" "Meeting" entry (file+headline calendar-file "Unclassified")
  180. "* MEET %? %A")))
  181. ;; Use the C-c c key globally to create an org-item from the context
  182. (define-key global-map "\C-cc" 'org-capture)
  183. ;; org-agenda files, can be directories (doesn't look at subdirs) or files
  184. (setq org-agenda-files (list calendar-file))
  185. ;; Remove unnecessary " calendar: " from each entry on the left.
  186. (setq org-agenda-prefix-format
  187. '((agenda . " %i %?-12t% s") ; default: " %i %-12:c%?-12t% s"
  188. (todo . " %i %-12:c")
  189. (tags . " %i %-12:c")
  190. (search . " %i %-12:c")))
  191. (setq org-agenda-remove-tags t)
  192. ;; Never use tabs, always use spaces for indenting
  193. (setq indent-tabs-mode nil)
  194. ;; Use 2 spaces when indenting CSS, JS and shell instead of the default 4
  195. (setq css-indent-offset 2)
  196. (setq js-indent-level 2)
  197. (setq sh-indentation 2)
  198. ;; Helm-Bibtex
  199. ;; See https://github.com/tmalsburg/helm-bibtex
  200. (autoload 'helm-bibtex "helm-bibtex" "" t)
  201. (setq bibtex-completion-bibliography (list (concat bibliography-folder "references.bib"))) ; needs (list ...) for new org-ref to work
  202. (setq bibtex-completion-library-path (list (concat bibliography-folder "bibtex-pdfs/"))) ; needs (list ...) for new org-ref to work
  203. (setq bibtex-completion-pdf-field "File")
  204. (setq bibtex-completion-notes-path (concat bibliography-folder "notes.org"))
  205. (setq bibtex-completion-additional-search-fields '(keywords tags))
  206. (setq bibtex-completion-pdf-symbol "⌘")
  207. (setq bibtex-completion-notes-symbol "✎")
  208. ;; Requires a newer version of bibtex-completion!
  209. ;; (setq bibtex-completion-pdf-extension '(".pdf" ".djvu"))
  210. (setq while-no-input-ignore-events '())
  211. ;; See this also:
  212. ;; https://github.com/tmalsburg/helm-bibtex#create-a-bibtex-file-containing-only-specific-entries
  213. (require 'helm)
  214. (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.
  215. (define-key helm-command-map "b" 'helm-bibtex)
  216. (define-key helm-command-map "B" 'helm-bibtex-with-local-bibliography)
  217. (define-key helm-command-map "n" 'helm-bibtex-with-notes)
  218. (define-key helm-command-map (kbd "<f12>") 'helm-resume)
  219. (define-key helm-command-map "c" 'org-ref-helm-insert-cite-link)
  220. (define-key helm-command-map "p" 'org-ref-open-pdf-at-point)
  221. (define-key helm-command-map "r" 'org-ref-helm-load-completions-async)
  222. ;; sort helm-bibtex in the same order as the bib file
  223. (eval-after-load "helm-bibtex"
  224. '(advice-add 'bibtex-completion-candidates
  225. :filter-return 'reverse))
  226. ;; org-ref config
  227. (setq reftex-default-bibliography (list (concat bibliography-folder "references.bib")))
  228. (setq org-ref-bibliography-notes (concat bibliography-folder "notes.org")
  229. org-ref-default-bibliography (list (concat bibliography-folder "references.bib"))
  230. org-ref-pdf-directory (concat bibliography-folder "bibtex-pdfs/"))
  231. (require 'doi-utils)
  232. (require 'org-ref-isbn)
  233. (require 'org-ref-pubmed)
  234. (require 'org-ref-arxiv)
  235. (require 'x2bib)
  236. ;; Open pdf's as org-mode does (see above).
  237. (setq bibtex-completion-pdf-open-function 'org-open-file)
  238. ;; Journal abbreviations
  239. (add-to-list 'org-ref-bibtex-journal-abbreviations
  240. '("IUMJ" "Indiana University Mathematics Journal" "Indiana Univ. Math. J."))
  241. ;; Sci-hub
  242. (defun sci-hub-pdf-url (doi)
  243. "Get url to the pdf from SCI-HUB"
  244. (setq *doi-utils-pdf-url* (concat "https://sci-hub.st/" doi) ;captcha
  245. *doi-utils-waiting* t
  246. )
  247. ;; try to find PDF url (if it exists)
  248. (url-retrieve (concat "https://sci-hub.st/" doi)
  249. (lambda (status)
  250. (goto-char (point-min))
  251. (while (search-forward-regexp "\\(https://\\|//sci-hub.st/downloads\\).+download=true'" nil t)
  252. (let ((foundurl (match-string 0)))
  253. (message foundurl)
  254. (if (string-match "https:" foundurl)
  255. (setq *doi-utils-pdf-url* foundurl)
  256. (setq *doi-utils-pdf-url* (concat "https:" foundurl))))
  257. (setq *doi-utils-waiting* nil))))
  258. (while *doi-utils-waiting* (sleep-for 0.1))
  259. *doi-utils-pdf-url*)
  260. (defun doi-utils-get-bibtex-entry-pdf (&optional arg)
  261. "Download pdf for entry at point if the pdf does not already exist locally.
  262. The entry must have a doi. The pdf will be saved to
  263. `org-ref-pdf-directory', by the name %s.pdf where %s is the
  264. bibtex label. Files will not be overwritten. The pdf will be
  265. checked to make sure it is a pdf, and not some html failure
  266. page. You must have permission to access the pdf. We open the pdf
  267. at the end if `doi-utils-open-pdf-after-download' is non-nil.
  268. With one prefix ARG, directly get the pdf from a file (through
  269. `read-file-name') instead of looking up a DOI. With a double
  270. prefix ARG, directly get the pdf from an open buffer (through
  271. `read-buffer-to-switch') instead. These two alternative methods
  272. work even if the entry has no DOI, and the pdf file is not
  273. checked."
  274. (interactive "P")
  275. (save-excursion
  276. (bibtex-beginning-of-entry)
  277. (let ( ;; get doi, removing http://dx.doi.org/ if it is there.
  278. (doi (replace-regexp-in-string
  279. "https?://\\(dx.\\)?.doi.org/" ""
  280. (bibtex-autokey-get-field "doi")))
  281. (key (cdr (assoc "=key=" (bibtex-parse-entry))))
  282. (pdf-url)
  283. (pdf-file))
  284. (setq pdf-file (concat
  285. (if org-ref-pdf-directory
  286. (file-name-as-directory org-ref-pdf-directory)
  287. (read-directory-name "PDF directory: " "."))
  288. key ".pdf"))
  289. ;; now get file if needed.
  290. (unless (file-exists-p pdf-file)
  291. (cond
  292. ((and (not arg)
  293. doi
  294. (if (doi-utils-get-pdf-url doi)
  295. (setq pdf-url (doi-utils-get-pdf-url doi))
  296. (setq pdf-url "https://www.sciencedirect.com/science/article/")))
  297. (url-copy-file pdf-url pdf-file)
  298. ;; now check if we got a pdf
  299. (if (org-ref-pdf-p pdf-file)
  300. (message "%s saved" pdf-file)
  301. (delete-file pdf-file)
  302. ;; sci-hub fallback option
  303. (setq pdf-url (sci-hub-pdf-url doi))
  304. (url-copy-file pdf-url pdf-file)
  305. ;; now check if we got a pdf
  306. (if (org-ref-pdf-p pdf-file)
  307. (message "%s saved" pdf-file)
  308. (delete-file pdf-file)
  309. (message "No pdf was downloaded.") ; SH captcha
  310. (browse-url pdf-url))))
  311. ;; End of sci-hub fallback option
  312. ((equal arg '(4))
  313. (copy-file (expand-file-name (read-file-name "Pdf file: " nil nil t))
  314. pdf-file))
  315. ((equal arg '(16))
  316. (with-current-buffer (read-buffer-to-switch "Pdf buffer: ")
  317. (write-file pdf-file)))
  318. (t
  319. (message "We don't have a recipe for this journal.")))
  320. (when (and doi-utils-open-pdf-after-download (file-exists-p pdf-file))
  321. (org-open-file pdf-file))))))
  322. ;; Asymptote mode
  323. ;; if "asy" is not on path, use:
  324. ;;(add-to-list 'load-path "ASYDIR")
  325. ;; for compiling with C-c C-c
  326. (autoload 'asy-mode "asy-mode.el" "Asymptote major mode." t)
  327. (autoload 'lasy-mode "asy-mode.el" "hybrid Asymptote/Latex major mode." t)
  328. (autoload 'asy-insinuate-latex "asy-mode.el" "Asymptote insinuate LaTeX." t)
  329. (add-to-list 'auto-mode-alist '("\\.asy$" . asy-mode))
  330. ;; Set color theme (generated using M-x customize-theme)
  331. (custom-set-variables
  332. ;; custom-set-variables was added by Custom.
  333. ;; If you edit it by hand, you could mess it up, so be careful.
  334. ;; Your init file should contain only one such instance.
  335. ;; If there is more than one, they won't work right.
  336. '(ansi-color-faces-vector
  337. [default default default italic underline success warning error])
  338. '(custom-enabled-themes (quote (dracula)))
  339. '(custom-safe-themes
  340. (quote
  341. ("fe1c13d75398b1c8fd7fdd1241a55c286b86c3e4ce513c4292d01383de152cb7" default)))
  342. '(org-file-apps
  343. (quote
  344. ((auto-mode . emacs)
  345. ("\\.mm\\'" . default)
  346. ("\\.x?html?\\'" . default)
  347. ("\\.pdf\\'" . "xdg-open %s")
  348. ("\\.djvu\\'" . "xdg-open %s"))))
  349. '(org-ref-clean-bibtex-entry-hook
  350. (quote
  351. (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)))
  352. '(package-selected-packages
  353. (quote
  354. (auth-source-xoauth2 oauth2 bbdb geiser-chez geiser graphviz-dot-mode slime-company slime company smartparens expand-region nyan-mode calfw-org calfw helm-bibtex helm dracula-theme suomalainen-kalenteri dash bibtex-completion org-ref magit markdown-mode))))
  355. ;; Set markdown-command to run pandoc
  356. (setq markdown-command "pandoc")
  357. ;; Use HTML5 and UTF-8 for markdown export to html
  358. (eval-after-load "markdown-mode"
  359. '(defalias 'markdown-add-xhtml-header-and-footer 'as/markdown-add-xhtml-header-and-footer))
  360. (defun as/markdown-add-xhtml-header-and-footer (title)
  361. "Wrap XHTML header and footer with given TITLE around current buffer."
  362. (goto-char (point-min))
  363. (insert "<!DOCTYPE html5>\n"
  364. "<html>\n"
  365. "<head>\n<title>")
  366. (insert title)
  367. (insert "</title>\n")
  368. (insert "<meta charset=\"utf-8\" />\n")
  369. (when (> (length markdown-css-paths) 0)
  370. (insert (mapconcat 'markdown-stylesheet-link-string markdown-css-paths "\n")))
  371. (insert "\n</head>\n\n"
  372. "<body>\n\n")
  373. (goto-char (point-max))
  374. (insert "\n"
  375. "</body>\n"
  376. "</html>\n"))
  377. ;; Emacs-magit
  378. (require 'magit)
  379. ;; Make C-x g show the magit status
  380. (define-key global-map "\C-xg" 'magit-status)