emacs 17 KB

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