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.

552 lines
16 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
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. * Org Mode
  99. ** Installation
  100. 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.]]
  101. #+begin_src emacs-lisp
  102. (use-package org
  103. :ensure org-plus-contrib)
  104. #+end_src
  105. 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:
  106. #+begin_src sh:
  107. var ORG_DIR=(let* ((org-v (cadr (split-string (org-version nil t) "@"))) (len (length org-v))) (substring org-v 1 (- len 2)))
  108. rm ${ORG_DIR}/*.elc
  109. #+end_src
  110. *** Org activation bindings
  111. Set up some global key bindings that integrate with Org mode features
  112. #+begin_src emacs-lisp
  113. (bind-key "C-c l" 'org-store-link)
  114. (bind-key "C-c c" 'org-capture)
  115. (bind-key "C-c a" 'org-agenda)
  116. #+end_src
  117. *** Org agenda
  118. For a more detailed example [[https://github.com/sachac/.emacs.d/blob/83d21e473368adb1f63e582a6595450fcd0e787c/Sacha.org#org-agenda][see here]].
  119. #+begin_src emacs-lisp
  120. (setq org-agenda-files
  121. (delq nil
  122. (mapcar (lambda (x) (and (file-exists-p x) x))
  123. '("~/Archiv/Dokumente/Agenda"))
  124. )
  125. )
  126. #+end_src
  127. *** Org capture
  128. #+begin_src emacs-lisp
  129. (bind-key "C-c c" 'org-capture)
  130. (setq org-default-notes-file "~/Archiv/Dokumente/Notizen/notes.org")
  131. #+end_src
  132. ** Org Setup
  133. 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.
  134. See the doc for speed keys by checking out the documentation for speed keys in Org mode.
  135. #+begin_src emacs-lisp
  136. (setq org-use-speed-commands t)
  137. (setq org-image-actual-width 550)
  138. (setq org-highlight-latex-and-related '(latex script entities))
  139. #+end_src
  140. ** Org tags
  141. The default value is -77, which is weird for smaller width windows. I'd rather have the tags align horizontally with the header.
  142. 45 is a good column number to do that.
  143. #+begin_src emacs-lisp
  144. (setq org-tags-column 45)
  145. #+end_src
  146. ** Org babel languages
  147. #+begin_src emacs-lisp
  148. (org-babel-do-load-languages
  149. 'org-babel-load-languages
  150. '((python . t)
  151. (C . t)
  152. (calc . t)
  153. (latex . t)
  154. (java . t)
  155. (ruby . t)
  156. (lisp . t)
  157. (scheme . t)
  158. (shell . t)
  159. (sqlite . t)
  160. (js . t)))
  161. (defun my-org-confirm-babel-evaluate (lang body)
  162. "Do not confirm evaluation for these languages."
  163. (not (or (string= lang "C")
  164. (string= lang "java")
  165. (string= lang "python")
  166. (string= lang "emacs-lisp")
  167. (string= lang "sqlite"))))
  168. (setq org-confirm-babel-evaluate 'my-org-confirm-babel-evaluate)
  169. #+end_src
  170. ** Org babel/source blocks
  171. 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
  172. 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
  173. #+begin_src emacs-lisp
  174. (setq org-src-fontify-natively t
  175. org-src-window-setup 'current-window
  176. org-src-strip-leading-and-trailing-blank-lines t
  177. org-src-preserve-indentation t
  178. org-src-tab-acts-natively t)
  179. #+end_src
  180. * which-key
  181. Greatly increases discovery of functions!
  182. Click [[https://github.com/justbur/emacs-which-key][here]] for source and more info.
  183. Info in Emacs: M-x customize-group which-key
  184. #+begin_src emacs-lisp
  185. (use-package which-key
  186. :ensure t
  187. :diminish which-key-mode
  188. :config
  189. (which-key-mode)
  190. (which-key-setup-side-window-right-bottom)
  191. (which-key-setup-minibuffer)
  192. (setq which-key-idle-delay 0.5)
  193. )
  194. #+end_src
  195. * Ido
  196. better completion
  197. begin_src emacs-lisp
  198. (use-package ido
  199. :init
  200. (setq ido-enable-flex-matching t)
  201. (setq ido-everywhere t)
  202. (ido-mode t)
  203. (use-package ido-vertical-mode
  204. :ensure t
  205. :defer t
  206. :init
  207. (ido-vertical-mode 1)
  208. (setq ido-vertical-define-keys 'C-n-and-C-p-only)
  209. )
  210. )
  211. end_src
  212. * ivy / counsel / swiper
  213. Flx is required for fuzzy-matching
  214. Is it really necessary?
  215. #+begin_src emacs-lisp
  216. (use-package flx)
  217. #+end_src
  218. Ivy displays a window with suggestions for hotkeys and M-x
  219. #+begin_src emacs-lisp
  220. (use-package ivy
  221. :ensure t
  222. :diminish
  223. (ivy-mode . "") ;; does not display ivy in the mode line
  224. :init
  225. (ivy-mode 1)
  226. :bind
  227. ("C-c C-r" . ivy-resume)
  228. :config
  229. (setq ivy-use-virtual-buffers t) ;; recent files and bookmarks in ivy-switch-buffer
  230. (setq ivy-height 20) ;; height of ivy window
  231. (setq ivy-count-format "%d/%d") ;; current and total number
  232. (setq ivy-re-builders-alist ;; regex replaces spaces with *
  233. '((t . ivy--regex-plus)))
  234. )
  235. #+end_src
  236. Counsel replaces:
  237. - M-x
  238. - C-x C-f find-file
  239. - C-c h f describe-function
  240. - C-c h v describe-variable
  241. - M-i imenu
  242. The find-file replacement is nicer to navigate
  243. #+begin_src emacs-lisp
  244. (use-package counsel
  245. :ensure t
  246. :bind* ;; load counsel when pressed
  247. (("M-x" . counsel-M-x)
  248. ("C-x C-f" . counsel-find-file)
  249. ("C-c h f" . counsel-describe-function)
  250. ("C-c h v" . counsel-describe-variable)
  251. ("M-i" . counsel-imenu)
  252. )
  253. )
  254. #+end_src
  255. Swiper ivy-enhances isearch
  256. #+begin_src emacs-lisp
  257. (use-package swiper
  258. :ensure t
  259. :bind
  260. (("C-s" . swiper)
  261. ("C-c C-r" . ivy-resume)
  262. )
  263. )
  264. #+end_src
  265. * Recentf
  266. Requires counsel
  267. #+begin_src emacs-lisp
  268. (use-package recentf
  269. :bind ("C-x C-r" . counsel-recentf)
  270. :config
  271. (recentf-mode t)
  272. (setq recentf-max-saved-items 200)
  273. )
  274. #+end_src
  275. * Programming
  276. ** Common things
  277. List of plugins and settings which are shared between the language plugins
  278. *** Company Mode
  279. Complete Anything!
  280. Activate company and make it react nearly instantly
  281. #+begin_src emacs-lisp
  282. (use-package company
  283. :ensure t
  284. :config
  285. (add-to-list 'company-backends 'company-elisp)
  286. (setq-default company-minimum-prefix-length 2
  287. company-tooltip-align-annotation t
  288. company-tooltop-flip-when-above t
  289. company-show-numbers t
  290. company-idle-delay 0.1)
  291. (add-hook 'after-init-hook 'global-company-mode)
  292. ;; (global-company-mode t)
  293. )
  294. #+end_src
  295. Addon to sort suggestions by usage
  296. #+begin_src emacs-lisp
  297. (use-package company-statistics
  298. :ensure t
  299. :after company
  300. :config
  301. (company-statistics-mode 1)
  302. )
  303. #+end_src
  304. Get a popup with documentation of the completion candidate.
  305. #+begin_src emacs-lisp
  306. (use-package company-quickhelp
  307. :ensure t
  308. :after company
  309. :config
  310. (company-quickhelp-mode 1)
  311. )
  312. #+end_src
  313. Maybe add [[https://github.com/hlissner/emacs-company-dict][company-dict]]? It's a dictionary based on major modes, plus it has Yasnippet integration.
  314. ** Python
  315. Systemwide following packages need to be installed:
  316. - venv
  317. The virtual environments need to have following modules installed:
  318. - jedi
  319. - epc
  320. - pylint
  321. #+begin_src emacs-lisp
  322. (use-package flycheck
  323. :ensure t
  324. :init
  325. (add-hook 'after-init-hook #'global-flycheck-mode)
  326. (add-hook 'python-mode-hook (lambda ()
  327. (semantic-mode 1)
  328. (setq flycheck-checker 'python-pylint)))
  329. )
  330. #+end_src
  331. Automatically start python-mode when opening a .py-file.
  332. Not sure if python.el is better than python-mode.el.
  333. See [[https://github.com/jorgenschaefer/elpy/issues/887][here]] for info about ~python-shell-completion-native-enable~.
  334. 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]].
  335. #+begin_src emacs-lisp
  336. (use-package python
  337. :mode ("\\.py\\'" . python-mode)
  338. :interpreter ("python" . python-mode)
  339. :init
  340. (add-hook 'python-mode-hook 'eldoc-mode)
  341. :config
  342. (setq python-shell-completion-native-enable nil)
  343. ;; (defun my/run-python ()
  344. ;; (save-selected-window
  345. ;; (switch-to-buffer-other-window (process-buffer (python-shell-get-or-create-process (python-shell-parse-command)))))
  346. ;; )
  347. ;; (add-hook 'python-mode-hook 'my/run-python)
  348. )
  349. #+end_src
  350. Anaconda test
  351. begin_src emacs-lisp
  352. (use-package anaconda-mode
  353. :ensure t
  354. :defer t
  355. :init
  356. (add-hook 'python-mode-hook 'anaconda-mode)
  357. ;; (add-hook 'python-mode-hook 'anaconda-eldoc-mode)
  358. :config
  359. (setq anaconda-eldoc-mode 1)
  360. )
  361. end_src
  362. begin_src emacs-lisp
  363. (use-package company-anaconda
  364. :ensure t
  365. :defer t
  366. :init
  367. (defun my/company-anaconda-hook()
  368. (add-to-list 'company-backends 'company-anaconda))
  369. (add-hook 'python-mode-hook 'my/company-anaconda-hook)
  370. )
  371. end_src
  372. Jedi is a backend for python autocompletion and needs to be installed on the server:
  373. - pip install jedi
  374. Code checks need to be installed, too:
  375. - pip install flake8
  376. begin_src emacs-lisp
  377. (use-package company-jedi
  378. :defer t
  379. ;; :after company
  380. :ensure t
  381. :config
  382. (setq jedi:environment-virtualenv (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  383. (setq jedi:python-environment-directory (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  384. (add-hook 'python-mode-hook 'jedi:setup)
  385. (setq jedi:complete-on-dot t)
  386. (setq jedi:use-shortcuts t)
  387. (defun my/python-mode-hook()
  388. (add-to-list 'company-backends 'company-jedi))
  389. (add-hook 'python-mode-hook 'my/python-mode-hook)
  390. )
  391. #+end_src
  392. A wrapper to handle virtual environments.
  393. I strongly recommend to install virtual environments on the terminal, not through this wrapper, but changing venvs is fine.
  394. pyvenv might need virtualenvwrapper, I have to test this.
  395. apt install
  396. #+begin_src emacs-lisp
  397. (use-package pyvenv
  398. :ensure t
  399. :init
  400. (setenv "WORKON_HOME" (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/"))
  401. :config
  402. (pyvenv-mode t)
  403. (defun my/post-activate-hook()
  404. (setq jedi:environment-root pyvenv-virtual-env)
  405. (setq jedi:environment-virtualenv pyvenv-virtual-env)
  406. (setq jedi:tooltip-method nil)
  407. (setq python-shell-virtualenv-root pyvenv-virtual-env)
  408. ;; default traceback, other option M-x jedi:toggle-log-traceback
  409. ;; traceback is in jedi:pop-to-epc-buffer
  410. (jedi:setup)
  411. (setq jedi:server-args '("--log-traceback"))
  412. (add-to-list 'company-backends 'company-jedi)
  413. (add-to-list 'company-backends 'company-anaconda)
  414. ;; (setq flycheck-checker 'python-pylint))
  415. (flycheck-select-checker 'python-pylint))
  416. ;; (setq flycheck-checker 'python-flake8)
  417. (add-hook 'pyvenv-post-activate-hooks 'my/post-activate-hook)
  418. )
  419. #+end_src
  420. just a wild guess to get better popups for jedi
  421. #+begin_src emacs-lisp
  422. (use-package popup
  423. :ensure t
  424. )
  425. #+end_src
  426. I want Emacs to automatically start the proper virtual environment.
  427. Required is a .python-version file with, content in the first line being /path/to/virtualenv/
  428. [[https://github.com/marcwebbie/auto-virtualenv][Github source]]
  429. Depends on pyvenv
  430. #+begin_src emacs-lisp
  431. (use-package auto-virtualenv
  432. :ensure t
  433. ;; :after pyvenv
  434. ;; :defer t
  435. :init
  436. (add-hook 'python-mode-hook 'auto-virtualenv-set-virtualenv)
  437. ;; activate on changing buffers
  438. ;; (add-hook 'window-configuration-change-hook 'auto-virtualenv-set-virtualenv)
  439. ;; activate on focus in
  440. ;; (add-hook 'focus-in-hook 'auto-virtualenv-set-virtualenv)
  441. )
  442. #+end_src
  443. begin_src emacs-lisp
  444. (use-package virtualenvwrapper
  445. :ensure t
  446. :init
  447. (venv-initialize-interactive-shells)
  448. (venv-initialize-eshell)
  449. (setq venv-location "~/Archiv/Programmierprojekte/Python/virtualenv")
  450. (setq python-environment-directory venv-location)
  451. (add-hook 'venv-postmkvirtualenv-hook
  452. (lambda () (shell-command "pip install epc jedi")))
  453. (add-hook 'venv-postactivate-hook 'jedi:stop-server)
  454. (add-hook 'venv-postdeactivate-hook 'jedi:stop-server)
  455. )
  456. end_src