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.

445 lines
12 KiB

  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 company-minimum-prefix-length 2
  262. company-tooltip-align-annotation t
  263. company-tooltop-flip-when-above t
  264. company-show-numbers t)
  265. (setq 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. :config
  275. (company-statistics-mode 1)
  276. )
  277. #+end_src
  278. Get a popup with documentation of the completion candidate.
  279. I have to test if it even works.
  280. #+begin_src emacs-lisp
  281. (use-package company-quickhelp
  282. :ensure t
  283. :config
  284. (company-quickhelp-mode 1)
  285. )
  286. #+end_src
  287. Maybe add [[https://github.com/hlissner/emacs-company-dict][company-dict]]? It's a dictionary based on major modes, plus it has Yasnippet integration.
  288. ** Python
  289. Systemwide following packages need to be installed:
  290. - virtualenv
  291. The virtual environments need to have following modules installed:
  292. - jedi
  293. - epc
  294. - flake8
  295. Automatically start python-mode when opening a .py-file.
  296. Not sure if python.el is better than python-mode.el.
  297. See [[https://github.com/jorgenschaefer/elpy/issues/887][here]] for info about ~python-shell-completion-native-enable~.
  298. 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]].
  299. #+begin_src emacs-lisp
  300. (use-package python
  301. :mode ("\\.py\\'" . python-mode)
  302. :interpreter ("python" . python-mode)
  303. :config
  304. (setq python-shell-completion-native-enable nil)
  305. ;; (defun my/run-python ()
  306. ;; (save-selected-window
  307. ;; (switch-to-buffer-other-window (process-buffer (python-shell-get-or-create-process (python-shell-parse-command)))))
  308. ;; )
  309. ;; (add-hook 'python-mode-hook 'my/run-python)
  310. )
  311. #+end_src
  312. ~TEST~ ELPY
  313. begin_src emacs-lisp
  314. (use-package elpy
  315. :ensure t
  316. ;; :pin elpy
  317. :config
  318. (elpy-enable)
  319. (add-hook 'python-mode-hook 'elpy-mode)
  320. (setq elpy-rpc-backend "jedi")
  321. (pyvenv-mode 1)
  322. )
  323. end_src
  324. Jedi is a backend for python autocompletion and needs to be installed on the server:
  325. - pip install jedi
  326. Code checks need to be installed, too:
  327. - pip install flake8
  328. #+begin_src emacs-lisp
  329. (use-package company-jedi
  330. :defer t
  331. :ensure t
  332. :config
  333. (setq jedi:environment-virtualenv (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  334. (add-hook 'python-mode-hook 'jedi:setup)
  335. (setq jedi:complete-on-dot t)
  336. (setq jedi:use-shortcuts t)
  337. (defun my/python-mode-hook()
  338. (add-to-list 'company-backends 'company-jedi))
  339. (add-hook 'python-mode-hook 'my/python-mode-hook)
  340. )
  341. #+end_src
  342. A wrapper to handle virtual environments.
  343. I strongly recommend to install virtual environments on the terminal, not through this wrapper, but changing venvs is fine.
  344. #+begin_src emacs-lisp
  345. (use-package virtualenvwrapper
  346. :ensure t
  347. :init
  348. (venv-initialize-interactive-shells)
  349. (venv-initialize-eshell)
  350. (setq venv-location "~/Archiv/Programmierprojekte/Python/virtualenv")
  351. (setq python-environment-directory venv-location)
  352. (add-hook 'venv-postmkvirtualenv-hook
  353. (lambda () (shell-command "pip install epc jedi")))
  354. (add-hook 'venv-postactivate-hook 'jedi:stop-server)
  355. (add-hook 'venv-postdeactivate-hook 'jedi:stop-server)
  356. )
  357. #+end_src