You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

517 lines
15 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #+TITLE: Emacs Configuration
  2. #+AUTHOR: Marc Pohling
  3. * Personal Information
  4. #+begin_src emacs-lisp
  5. (setq user-full-name "Marc Pohling"
  6. user-mail-address "marc.pohling@googlemail.com")
  7. #+end_src
  8. * Update config in a running config
  9. Two options:
  10. - reload the open file: M-x load-file, then press twice to accept
  11. the default filename, which is the currently opened
  12. - Point at the end of any sexp and press C-x C-e
  13. * Customize settings
  14. Move the customize settings to its own file, instead of saving
  15. customize settings in [[file:init.el][init.el]].
  16. #+begin_src emacs-lisp
  17. (setq custom-file (expand-file-name "custom.el" user-emacs-directory))
  18. (load custom-file)
  19. #+end_src
  20. * Theme
  21. ** Material Theme
  22. The [[https://github.com/cpaulik/emacs-material-theme][Material Theme]] comes in a dark and a light variant. Not too dark
  23. to be strenious though.
  24. #+begin_src emacs-lisp
  25. (use-package material-theme
  26. :if (window-system)
  27. :defer t
  28. :ensure t
  29. :init
  30. (load-theme 'material t)
  31. )
  32. #+end_src
  33. * Sane defaults
  34. Sources for this section include [[https://github.com/magnars/.emacs.d/blob/master/settings/sane-defaults.el][Magnars Sveen]] and [[http://pages.sachachua.com/.emacs.d/Sacha.html][Sacha Chua]]
  35. These functions are useful. Activate them.
  36. #+begin_src emacs-lisp
  37. (put 'downcase-region 'disabled nil)
  38. (put 'upcase-region 'disabled nil)
  39. (put 'narrow-to-region 'disabled nil)
  40. (put 'dired-find-alternate-file 'disabled nil)
  41. #+end_src
  42. Answering just 'y' or 'n' should be enough.
  43. #+begin_src emacs-lisp
  44. (defalias 'yes-or-no-p 'y-or-n-p)
  45. #+end_src
  46. Keep all backup and auto-save files in one directory
  47. #+begin_src emacs-lisp
  48. (setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
  49. (setq auto-save-file-name-transforms '((".*" "~/.emacs.d/auto-save-list/" t)))
  50. #+end_src
  51. UTF-8 please
  52. #+begin_src emacs-lisp
  53. (setq locale-coding-system 'utf-8)
  54. (set-terminal-coding-system 'utf-8)
  55. (set-keyboard-coding-system 'utf-8)
  56. (set-selection-coding-system 'utf-8)
  57. (prefer-coding-system 'utf-8)
  58. #+end_src
  59. Avoid tabs in place of multiple spaces (they look bad in TeX)
  60. and show empty lines
  61. #+begin_src emacs-lisp
  62. (setq-default indent-tabs-mode nil)
  63. (setq-default indicate-empty-lines t)
  64. #+end_src
  65. Turn off blinking cursor
  66. #+begin_src emacs-lisp
  67. (blink-cursor-mode -1)
  68. #+end_src
  69. Don't count two spaces after a period as the end of a sentence.
  70. Just one space is needed
  71. #+begin_src emacs-lisp
  72. (setq sentence-end-double-space nil)
  73. #+end_src
  74. Delete the region when typing, just like as we expect nowadays.
  75. #+begin_src emacs-lisp
  76. (delete-selection-mode t)
  77. #+end_src
  78. Various stuff
  79. #+begin_src emacs-lisp
  80. (show-paren-mode t)
  81. (column-number-mode t)
  82. (global-visual-line-mode)
  83. (diminish 'visual-line-mode)
  84. (setq uniquify-buffer-name-style 'forward)
  85. #+end_src
  86. * List buffers
  87. Ibuffer is the improved version of list-buffers.
  88. Make ibuffer the default buffer lister. [[http://ergoemacs.org/emacs/emacs_buffer_management.html][Source]]
  89. #+begin_src emacs-lisp
  90. (defalias 'list-buffers 'ibuffer)
  91. #+end_src
  92. Also auto refresh dired, but be quiet about it. [[http://whattheemacsd.com/sane-defaults.el-01.html][Source]]
  93. #+begin_src emacs-lisp
  94. (add-hook 'dired-mode-hook 'auto-revert-mode)
  95. (setq global-auto-revert-non-file-buffers t)
  96. (setq auto-revert-verbose nil)
  97. #+end_src
  98. * Recentf
  99. #+begin_src emacs-lisp
  100. (use-package recentf
  101. :bind ("C-x C-r" . helm-recentf)
  102. :config
  103. (recentf-mode t)
  104. (setq recentf-max-saved-items 200)
  105. )
  106. #+end_src
  107. * Org Mode
  108. ** Installation
  109. Although org mode ships with Emacs, the latest version can be installed externally. The configuration here follows the [[http://orgmode.org/elpa.html][Org mode ELPA Installation instructions.]]
  110. #+begin_src emacs-lisp
  111. (use-package org
  112. :ensure org-plus-contrib)
  113. #+end_src
  114. To avoid problems executing source blocks out of the box. [[https://emacs.stackexchange.com/a/28604][Others have the same problem, too]]. The solution is to remove the .elc files form the package directory:
  115. #+begin_src sh:
  116. var ORG_DIR=(let* ((org-v (cadr (split-string (org-version nil t) "@"))) (len (length org-v))) (substring org-v 1 (- len 2)))
  117. rm ${ORG_DIR}/*.elc
  118. #+end_src
  119. *** Org activation bindings
  120. Set up some global key bindings that integrate with Org mode features
  121. #+begin_src emacs-lisp
  122. (bind-key "C-c l" 'org-store-link)
  123. (bind-key "C-c c" 'org-capture)
  124. (bind-key "C-c a" 'org-agenda)
  125. #+end_src
  126. *** Org agenda
  127. For a more detailed example [[https://github.com/sachac/.emacs.d/blob/83d21e473368adb1f63e582a6595450fcd0e787c/Sacha.org#org-agenda][see here]].
  128. #+begin_src emacs-lisp
  129. (setq org-agenda-files
  130. (delq nil
  131. (mapcar (lambda (x) (and (file-exists-p x) x))
  132. '("~/Archiv/Dokumente/Agenda"))
  133. )
  134. )
  135. #+end_src
  136. *** Org capture
  137. #+begin_src emacs-lisp
  138. (bind-key "C-c c" 'org-capture)
  139. (setq org-default-notes-file "~/Archiv/Dokumente/Notizen/notes.org")
  140. #+end_src
  141. ** Org Setup
  142. Speed commands are a nice and quick way to perform certain actions while at the beginning of a heading. It's not activated by default.
  143. See the doc for speed keys by checking out the documentation for speed keys in Org mode.
  144. #+begin_src emacs-lisp
  145. (setq org-use-speed-commands t)
  146. (setq org-image-actual-width 550)
  147. (setq org-highlight-latex-and-related '(latex script entities))
  148. #+end_src
  149. ** Org tags
  150. The default value is -77, which is weird for smaller width windows. I'd rather have the tags align horizontally with the header.
  151. 45 is a good column number to do that.
  152. #+begin_src emacs-lisp
  153. (setq org-tags-column 45)
  154. #+end_src
  155. ** Org babel languages
  156. #+begin_src emacs-lisp
  157. (org-babel-do-load-languages
  158. 'org-babel-load-languages
  159. '((python . t)
  160. (C . t)
  161. (calc . t)
  162. (latex . t)
  163. (java . t)
  164. (ruby . t)
  165. (lisp . t)
  166. (scheme . t)
  167. (shell . t)
  168. (sqlite . t)
  169. (js . t)))
  170. (defun my-org-confirm-babel-evaluate (lang body)
  171. "Do not confirm evaluation for these languages."
  172. (not (or (string= lang "C")
  173. (string= lang "java")
  174. (string= lang "python")
  175. (string= lang "emacs-lisp")
  176. (string= lang "sqlite"))))
  177. (setq org-confirm-babel-evaluate 'my-org-confirm-babel-evaluate)
  178. #+end_src
  179. ** Org babel/source blocks
  180. I like to have source blocks properly syntax highlighted and with the editing popup window staying within the same window so all the windows don't jump around. Also, having the top and bottom trailing lines in the block is a waste of space, so we can remove them
  181. I noticed that fontification doesn't work with markdown mode when the block is indented after editing it in the org src buffer - the leading #s for headers don't get fontified properly because they apppear as Org comments. Setting ~org-src-preserve-identation~ makes things consistent as it doesn't pad source blocks with leading spaces
  182. #+begin_src emacs-lisp
  183. (setq org-src-fontify-natively t
  184. org-src-window-setup 'current-window
  185. org-src-strip-leading-and-trailing-blank-lines t
  186. org-src-preserve-indentation t
  187. org-src-tab-acts-natively t)
  188. #+end_src
  189. * which-key
  190. Greatly increases discovery of functions!
  191. Click [[https://github.com/justbur/emacs-which-key][here]] for source and more info.
  192. Info in Emacs: M-x customize-group which-key
  193. #+begin_src emacs-lisp
  194. (use-package which-key
  195. :ensure t
  196. :diminish which-key-mode
  197. :config
  198. (which-key-mode)
  199. (which-key-setup-side-window-right-bottom)
  200. (which-key-setup-minibuffer)
  201. (setq which-key-idle-delay 0.5)
  202. )
  203. #+end_src
  204. * ivy / counsel / swiper
  205. Ivy displays a window with suggestions for hotkeys and M-x
  206. #+begin_src emacs-lisp
  207. (use-package ivy
  208. :ensure t
  209. :diminish
  210. (ivy-mode . "") ;; does not display ivy in the mode line
  211. :init
  212. (ivy-mode 1)
  213. :bind
  214. ("C-c C-r" . ivy-resume)
  215. :config
  216. (setq ivy-use-virtual-buffers t) ;; recent files and bookmarks in ivy-switch-buffer
  217. (setq ivy-height 20) ;; height of ivy window
  218. (setq ivy-count-format "%d/%d") ;; current and total number
  219. )
  220. #+end_src
  221. Counsel replaces:
  222. - M-x
  223. - C-x C-f find-file
  224. - C-c h f describe-function
  225. - C-c h v describe-variable
  226. - M-i imenu
  227. The find-file replacement is nicer to navigate
  228. #+begin_src emacs-lisp
  229. (use-package counsel
  230. :ensure t
  231. :bind* ;; load counsel when pressed
  232. (("M-x" . counsel-M-x)
  233. ("C-x C-f" . counsel-find-file)
  234. ("C-c h f" . counsel-describe-function)
  235. ("C-c h v" . counsel-describe-variable)
  236. ("M-i" . counsel-imenu)
  237. )
  238. )
  239. #+end_src
  240. Swiper ivy-enhances isearch
  241. #+begin_src emacs-lisp
  242. (use-package swiper
  243. :ensure t
  244. :bind
  245. (("C-s" . swiper)
  246. ("C-c C-r" . ivy-resume)
  247. )
  248. )
  249. #+end_src
  250. * Programming
  251. ** Common things
  252. List of plugins and settings which are shared between the language plugins
  253. *** Company Mode
  254. Complete Anything!
  255. Activate company and make it react nearly instantly
  256. #+begin_src emacs-lisp
  257. (use-package company
  258. :ensure t
  259. :config
  260. (add-to-list 'company-backends 'company-elisp)
  261. (setq-default company-minimum-prefix-length 2
  262. company-tooltip-align-annotation t
  263. company-tooltop-flip-when-above t
  264. company-show-numbers t
  265. company-idle-delay 0.1)
  266. (add-hook 'after-init-hook 'global-company-mode)
  267. ;; (global-company-mode t)
  268. )
  269. #+end_src
  270. Addon to sort suggestions by usage
  271. #+begin_src emacs-lisp
  272. (use-package company-statistics
  273. :ensure t
  274. :after company
  275. :config
  276. (company-statistics-mode 1)
  277. )
  278. #+end_src
  279. Get a popup with documentation of the completion candidate.
  280. #+begin_src emacs-lisp
  281. (use-package company-quickhelp
  282. :ensure t
  283. :after company
  284. :config
  285. (company-quickhelp-mode 1)
  286. )
  287. #+end_src
  288. Maybe add [[https://github.com/hlissner/emacs-company-dict][company-dict]]? It's a dictionary based on major modes, plus it has Yasnippet integration.
  289. ** Python
  290. Systemwide following packages need to be installed:
  291. - venv
  292. The virtual environments need to have following modules installed:
  293. - jedi
  294. - epc
  295. - pylint
  296. #+begin_src emacs-lisp
  297. (use-package flycheck
  298. :ensure t
  299. :init
  300. (add-hook 'after-init-hook #'global-flycheck-mode)
  301. (add-hook 'python-mode-hook (lambda ()
  302. (semantic-mode 1)
  303. (setq flycheck-checker 'python-pylint)))
  304. )
  305. #+end_src
  306. Automatically start python-mode when opening a .py-file.
  307. Not sure if python.el is better than python-mode.el.
  308. See [[https://github.com/jorgenschaefer/elpy/issues/887][here]] for info about ~python-shell-completion-native-enable~.
  309. The custom function is to run inferiour processes (do I really need that?), see [[https://emacs.stackexchange.com/questions/16361/how-to-automatically-run-inferior-process-when-loading-major-mode][here]].
  310. #+begin_src emacs-lisp
  311. (use-package python
  312. :mode ("\\.py\\'" . python-mode)
  313. :interpreter ("python" . python-mode)
  314. :init
  315. (add-hook 'python-mode-hook 'eldoc-mode)
  316. :config
  317. (setq python-shell-completion-native-enable nil)
  318. ;; (defun my/run-python ()
  319. ;; (save-selected-window
  320. ;; (switch-to-buffer-other-window (process-buffer (python-shell-get-or-create-process (python-shell-parse-command)))))
  321. ;; )
  322. ;; (add-hook 'python-mode-hook 'my/run-python)
  323. )
  324. #+end_src
  325. Anaconda test
  326. #+begin_src emacs-lisp
  327. (use-package anaconda-mode
  328. :ensure t
  329. :defer t
  330. :init
  331. (add-hook 'python-mode-hook 'anaconda-mode)
  332. ;; (add-hook 'python-mode-hook 'anaconda-eldoc-mode)
  333. :config
  334. (setq anaconda-eldoc-mode 1)
  335. )
  336. #+end_src
  337. #+begin_src emacs-lisp
  338. (use-package company-anaconda
  339. :ensure t
  340. :defer t
  341. :init
  342. (defun my/company-anaconda-hook()
  343. (add-to-list 'company-backends 'company-anaconda))
  344. (add-hook 'python-mode-hook 'my/company-anaconda-hook)
  345. )
  346. #+end_src
  347. Jedi is a backend for python autocompletion and needs to be installed on the server:
  348. - pip install jedi
  349. Code checks need to be installed, too:
  350. - pip install flake8
  351. begin_src emacs-lisp
  352. (use-package company-jedi
  353. :defer t
  354. ;; :after company
  355. :ensure t
  356. :config
  357. (setq jedi:environment-virtualenv (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  358. (setq jedi:python-environment-directory (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  359. (add-hook 'python-mode-hook 'jedi:setup)
  360. (setq jedi:complete-on-dot t)
  361. (setq jedi:use-shortcuts t)
  362. (defun my/python-mode-hook()
  363. (add-to-list 'company-backends 'company-jedi))
  364. (add-hook 'python-mode-hook 'my/python-mode-hook)
  365. )
  366. #+end_src
  367. A wrapper to handle virtual environments.
  368. I strongly recommend to install virtual environments on the terminal, not through this wrapper, but changing venvs is fine.
  369. pyvenv might need virtualenvwrapper, I have to test this.
  370. apt install
  371. #+begin_src emacs-lisp
  372. (use-package pyvenv
  373. :ensure t
  374. :init
  375. (setenv "WORKON_HOME" (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/"))
  376. :config
  377. (pyvenv-mode t)
  378. (defun my/post-activate-hook()
  379. (setq jedi:environment-root pyvenv-virtual-env)
  380. (setq jedi:environment-virtualenv pyvenv-virtual-env)
  381. (setq python-shell-virtualenv-root pyvenv-virtual-env)
  382. ;; default traceback, other option M-x jedi:toggle-log-traceback
  383. ;; traceback is in jedi:pop-to-epc-buffer
  384. (jedi:setup)
  385. (setq jedi:server-args '("--log-traceback"))
  386. (add-to-list 'company-backends 'company-jedi)
  387. (add-to-list 'company-backends 'company-anaconda)
  388. ;; (setq flycheck-checker 'python-pylint))
  389. (flycheck-select-checker 'python-pylint))
  390. ;; (setq flycheck-checker 'python-flake8)
  391. (add-hook 'pyvenv-post-activate-hooks 'my/post-activate-hook)
  392. )
  393. #+end_src
  394. I want Emacs to automatically start the proper virtual environment.
  395. Required is a .python-version file with, content in the first line being /path/to/virtualenv/
  396. [[https://github.com/marcwebbie/auto-virtualenv][Github source]]
  397. Depends on pyvenv
  398. #+begin_src emacs-lisp
  399. (use-package auto-virtualenv
  400. :ensure t
  401. ;; :after pyvenv
  402. ;; :defer t
  403. :init
  404. (add-hook 'python-mode-hook 'auto-virtualenv-set-virtualenv)
  405. ;; activate on changing buffers
  406. ;; (add-hook 'window-configuration-change-hook 'auto-virtualenv-set-virtualenv)
  407. ;; activate on focus in
  408. ;; (add-hook 'focus-in-hook 'auto-virtualenv-set-virtualenv)
  409. )
  410. #+end_src
  411. begin_src emacs-lisp
  412. (use-package virtualenvwrapper
  413. :ensure t
  414. :init
  415. (venv-initialize-interactive-shells)
  416. (venv-initialize-eshell)
  417. (setq venv-location "~/Archiv/Programmierprojekte/Python/virtualenv")
  418. (setq python-environment-directory venv-location)
  419. (add-hook 'venv-postmkvirtualenv-hook
  420. (lambda () (shell-command "pip install epc jedi")))
  421. (add-hook 'venv-postactivate-hook 'jedi:stop-server)
  422. (add-hook 'venv-postdeactivate-hook 'jedi:stop-server)
  423. )
  424. end_src