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.

665 lines
18 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
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. ** Font
  22. #+begin_src emacs-lisp
  23. (set-face-attribute 'default nil :font "Hack-12")
  24. #+end_src
  25. ** Material Theme
  26. The [[https://github.com/cpaulik/emacs-material-theme][Material Theme]] comes in a dark and a light variant. Not too dark
  27. to be strenious though.
  28. #+begin_src emacs-lisp
  29. (use-package material-theme
  30. :if (window-system)
  31. :defer t
  32. :ensure t
  33. ;; :init
  34. ;; (load-theme 'material t)
  35. )
  36. #+end_src
  37. ** Apropospriate Theme
  38. Variants dark and light
  39. #+begin_src emacs-lisp
  40. (use-package apropospriate-theme
  41. :if (window-system)
  42. :defer t
  43. :ensure t
  44. :init
  45. (load-theme 'apropospriate-dark t)
  46. )
  47. #+end_src
  48. * Sane defaults
  49. 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]]
  50. These functions are useful. Activate them.
  51. #+begin_src emacs-lisp
  52. (put 'downcase-region 'disabled nil)
  53. (put 'upcase-region 'disabled nil)
  54. (put 'narrow-to-region 'disabled nil)
  55. (put 'dired-find-alternate-file 'disabled nil)
  56. #+end_src
  57. Answering just 'y' or 'n' should be enough.
  58. #+begin_src emacs-lisp
  59. (defalias 'yes-or-no-p 'y-or-n-p)
  60. #+end_src
  61. Keep all backup and auto-save files in one directory
  62. #+begin_src emacs-lisp
  63. (setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
  64. (setq auto-save-file-name-transforms '((".*" "~/.emacs.d/auto-save-list/" t)))
  65. #+end_src
  66. UTF-8 please
  67. #+begin_src emacs-lisp
  68. (setq locale-coding-system 'utf-8)
  69. (set-terminal-coding-system 'utf-8)
  70. (set-keyboard-coding-system 'utf-8)
  71. (set-selection-coding-system 'utf-8)
  72. (prefer-coding-system 'utf-8)
  73. #+end_src
  74. Avoid tabs in place of multiple spaces (they look bad in TeX)
  75. and show empty lines
  76. #+begin_src emacs-lisp
  77. (setq-default indent-tabs-mode nil)
  78. (setq-default indicate-empty-lines t)
  79. #+end_src
  80. Turn off blinking cursor
  81. #+begin_src emacs-lisp
  82. (blink-cursor-mode -1)
  83. #+end_src
  84. Don't count two spaces after a period as the end of a sentence.
  85. Just one space is needed
  86. #+begin_src emacs-lisp
  87. (setq sentence-end-double-space nil)
  88. #+end_src
  89. Delete the region when typing, just like as we expect nowadays.
  90. #+begin_src emacs-lisp
  91. (delete-selection-mode t)
  92. #+end_src
  93. Auto-indent when pressing RET, just new-line when C-j
  94. #+begin_src emacs-lisp
  95. (define-key global-map (kbd "RET") 'newline-and-indent)
  96. (define-key global-map (kbd "C-j") 'newline)
  97. #+end_src
  98. Various stuff
  99. #+begin_src emacs-lisp
  100. (show-paren-mode t)
  101. (column-number-mode t)
  102. (global-visual-line-mode)
  103. (diminish 'visual-line-mode)
  104. (setq uniquify-buffer-name-style 'forward)
  105. #+end_src
  106. * List buffers
  107. Ibuffer is the improved version of list-buffers.
  108. Make ibuffer the default buffer lister. [[http://ergoemacs.org/emacs/emacs_buffer_management.html][Source]]
  109. #+begin_src emacs-lisp
  110. (defalias 'list-buffers 'ibuffer)
  111. #+end_src
  112. Also auto refresh dired, but be quiet about it. [[http://whattheemacsd.com/sane-defaults.el-01.html][Source]]
  113. #+begin_src emacs-lisp
  114. (add-hook 'dired-mode-hook 'auto-revert-mode)
  115. (setq global-auto-revert-non-file-buffers t)
  116. (setq auto-revert-verbose nil)
  117. #+end_src
  118. * Org Mode
  119. ** Installation
  120. 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.]]
  121. #+begin_src emacs-lisp
  122. (use-package org
  123. :ensure org-plus-contrib
  124. :init
  125. (add-hook 'org-mode-hook
  126. (lambda ()
  127. (add-to-list 'company-backends 'company-capf)
  128. (add-hook 'completion-at-point-functions 'pcomplete-completions-at-point nil t)
  129. (company-mode t)))
  130. )
  131. #+end_src
  132. 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:
  133. #+begin_src sh:
  134. var ORG_DIR=(let* ((org-v (cadr (split-string (org-version nil t) "@"))) (len (length org-v))) (substring org-v 1 (- len 2)))
  135. rm ${ORG_DIR}/*.elc
  136. #+end_src
  137. *** Org key bindings
  138. Set up some global key bindings that integrate with Org mode features
  139. #+begin_src emacs-lisp
  140. (bind-key "C-c l" 'org-store-link)
  141. (bind-key "C-c c" 'org-capture)
  142. (bind-key "C-c a" 'org-agenda)
  143. #+end_src
  144. Org overwrites RET and C-j, so I need to disable the rebinds
  145. #+begin_src emacs-lisp
  146. (define-key org-mode-map (kbd "RET") nil) ;;org-return
  147. (define-key org-mode-map (kbd "C-j") nil) ;;org-return-indent
  148. #+end_src
  149. *** Org agenda
  150. For a more detailed example [[https://github.com/sachac/.emacs.d/blob/83d21e473368adb1f63e582a6595450fcd0e787c/Sacha.org#org-agenda][see here]].
  151. #+begin_src emacs-lisp
  152. (setq org-agenda-files
  153. (delq nil
  154. (mapcar (lambda (x) (and (file-exists-p x) x))
  155. '("~/Archiv/Dokumente/Agenda"))
  156. )
  157. )
  158. #+end_src
  159. *** Org capture
  160. #+begin_src emacs-lisp
  161. (bind-key "C-c c" 'org-capture)
  162. (setq org-default-notes-file "~/Archiv/Dokumente/Notizen/notes.org")
  163. #+end_src
  164. ** Org Setup
  165. 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.
  166. See the doc for speed keys by checking out the documentation for speed keys in Org mode.
  167. #+begin_src emacs-lisp
  168. (setq org-use-speed-commands t)
  169. (setq org-image-actual-width 550)
  170. (setq org-highlight-latex-and-related '(latex script entities))
  171. #+end_src
  172. ** Org tags
  173. The default value is -77, which is weird for smaller width windows. I'd rather have the tags align horizontally with the header.
  174. 45 is a good column number to do that.
  175. #+begin_src emacs-lisp
  176. (setq org-tags-column 45)
  177. #+end_src
  178. ** Org babel languages
  179. #+begin_src emacs-lisp
  180. (org-babel-do-load-languages
  181. 'org-babel-load-languages
  182. '((python . t)
  183. (C . t)
  184. (calc . t)
  185. (latex . t)
  186. (java . t)
  187. (ruby . t)
  188. (lisp . t)
  189. (scheme . t)
  190. (shell . t)
  191. (sqlite . t)
  192. (js . t)))
  193. (defun my-org-confirm-babel-evaluate (lang body)
  194. "Do not confirm evaluation for these languages."
  195. (not (or (string= lang "C")
  196. (string= lang "java")
  197. (string= lang "python")
  198. (string= lang "emacs-lisp")
  199. (string= lang "sqlite"))))
  200. (setq org-confirm-babel-evaluate 'my-org-confirm-babel-evaluate)
  201. #+end_src
  202. ** Org babel/source blocks
  203. 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
  204. 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
  205. #+begin_src emacs-lisp
  206. (setq org-src-fontify-natively t
  207. org-src-window-setup 'current-window
  208. org-src-strip-leading-and-trailing-blank-lines t
  209. org-src-preserve-indentation t
  210. org-src-tab-acts-natively t)
  211. #+end_src
  212. * which-key
  213. Greatly increases discovery of functions!
  214. Click [[https://github.com/justbur/emacs-which-key][here]] for source and more info.
  215. Info in Emacs: M-x customize-group which-key
  216. #+begin_src emacs-lisp
  217. (use-package which-key
  218. :ensure t
  219. :diminish which-key-mode
  220. :config
  221. (which-key-mode)
  222. (which-key-setup-side-window-right-bottom)
  223. (which-key-setup-minibuffer)
  224. (setq which-key-idle-delay 0.5)
  225. )
  226. #+end_src
  227. * Ido (currently inactive)
  228. better completion
  229. begin_src emacs-lisp
  230. (use-package ido
  231. :init
  232. (setq ido-enable-flex-matching t)
  233. (setq ido-everywhere t)
  234. (ido-mode t)
  235. (use-package ido-vertical-mode
  236. :ensure t
  237. :defer t
  238. :init
  239. (ido-vertical-mode 1)
  240. (setq ido-vertical-define-keys 'C-n-and-C-p-only)
  241. )
  242. )
  243. end_src
  244. * ivy / counsel / swiper
  245. Flx is required for fuzzy-matching
  246. Is it really necessary?
  247. begin_src emacs-lisp
  248. (use-package flx)
  249. end_src
  250. Ivy displays a window with suggestions for hotkeys and M-x
  251. #+begin_src emacs-lisp
  252. (use-package ivy
  253. :ensure t
  254. :diminish
  255. (ivy-mode . "") ;; does not display ivy in the mode line
  256. :init
  257. (ivy-mode 1)
  258. :bind
  259. ("C-c C-r" . ivy-resume)
  260. :config
  261. (setq ivy-use-virtual-buffers t) ;; recent files and bookmarks in ivy-switch-buffer
  262. (setq ivy-height 20) ;; height of ivy window
  263. (setq ivy-count-format "%d/%d") ;; current and total number
  264. (setq ivy-re-builders-alist ;; regex replaces spaces with *
  265. '((t . ivy--regex-plus)))
  266. )
  267. #+end_src
  268. Counsel replaces:
  269. - M-x
  270. - C-x C-f find-file
  271. - C-c h f describe-function
  272. - C-c h v describe-variable
  273. - M-i imenu
  274. The find-file replacement is nicer to navigate
  275. #+begin_src emacs-lisp
  276. (use-package counsel
  277. :ensure t
  278. :bind* ;; load counsel when pressed
  279. (("M-x" . counsel-M-x)
  280. ("C-x C-f" . counsel-find-file)
  281. ("C-c h f" . counsel-describe-function)
  282. ("C-c h v" . counsel-describe-variable)
  283. ("M-i" . counsel-imenu)
  284. )
  285. )
  286. #+end_src
  287. Swiper ivy-enhances isearch
  288. #+begin_src emacs-lisp
  289. (use-package swiper
  290. :ensure t
  291. :bind
  292. (("C-s" . swiper)
  293. ("C-c C-r" . ivy-resume)
  294. )
  295. )
  296. #+end_src
  297. * Recentf
  298. Requires counsel
  299. #+begin_src emacs-lisp
  300. (use-package recentf
  301. :bind ("C-x C-r" . counsel-recentf)
  302. :config
  303. (recentf-mode t)
  304. (setq recentf-max-saved-items 200)
  305. )
  306. #+end_src
  307. * Programming
  308. ** Common things
  309. List of plugins and settings which are shared between the language plugins
  310. Highlight whitespaces, tabs, empty lines.
  311. #+begin_src emacs-lisp
  312. (use-package whitespace
  313. :demand t
  314. :ensure nil
  315. :hook
  316. ((prog-mode . whitespace-turn-on)
  317. (text-mode . whitespace-turn-on))
  318. :config
  319. (setq-default whitespace-style '(face empty tab trailing))
  320. )
  321. #+end_src
  322. Disable Eldoc, it interferes with flycheck
  323. #+begin_src emacs-lisp
  324. (use-package eldoc
  325. :ensure nil
  326. :config
  327. (global-eldoc-mode -1)
  328. )
  329. #+end_src
  330. Colorize colors as text with their value
  331. #+begin_src emacs-lisp
  332. (use-package rainbow-mode
  333. :hook prog-mode
  334. :config
  335. (setq-default rainbow-x-colors-major-mode-list '())
  336. )
  337. #+end_src
  338. *** Company Mode
  339. Complete Anything!
  340. Activate company and make it react nearly instantly
  341. #+begin_src emacs-lisp
  342. (use-package company
  343. :ensure t
  344. :config
  345. (setq-default company-minimum-prefix-length 1
  346. company-tooltip-align-annotation t
  347. company-tooltop-flip-when-above t
  348. company-show-numbers t
  349. company-idle-delay 0.1)
  350. )
  351. #+end_src
  352. Addon to sort suggestions by usage
  353. #+begin_src emacs-lisp
  354. (use-package company-statistics
  355. :ensure t
  356. :after company
  357. :config
  358. (company-statistics-mode 1)
  359. )
  360. #+end_src
  361. Get a popup with documentation of the completion candidate.
  362. For the popups the package pos-tip.el is used and automatically installed.
  363. [[https://github.com/expez/company-quickhelp][Company Quickhelp]]
  364. [[https://www.emacswiki.org/emacs/PosTip][See here for Pos-Tip details]]
  365. #+begin_src emacs-lisp
  366. (use-package company-quickhelp
  367. :ensure t
  368. :after company
  369. :config
  370. (company-quickhelp-mode 1)
  371. )
  372. #+end_src
  373. Maybe add [[https://github.com/hlissner/emacs-company-dict][company-dict]]? It's a dictionary based on major modes, plus it has Yasnippet integration.
  374. ** Python
  375. Systemwide following packages need to be installed:
  376. - venv
  377. The virtual environments need to have following modules installed:
  378. - jedi
  379. - epc
  380. - pylint
  381. #+begin_src emacs-lisp
  382. (use-package flycheck
  383. :ensure t
  384. :init
  385. (add-hook 'after-init-hook #'global-flycheck-mode)
  386. (add-hook 'python-mode-hook (lambda ()
  387. (semantic-mode 1)
  388. (flycheck-select-checker 'python-pylint)))
  389. ;; (setq flycheck-checker 'python-pylint)))
  390. )
  391. #+end_src
  392. Automatically start python-mode when opening a .py-file.
  393. Not sure if python.el is better than python-mode.el.
  394. See [[https://github.com/jorgenschaefer/elpy/issues/887][here]] for info about ~python-shell-completion-native-enable~.
  395. 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]].
  396. Also limit the completion backends to those which make sense in Python.
  397. #+begin_src emacs-lisp
  398. (use-package python
  399. :mode ("\\.py\\'" . python-mode)
  400. :interpreter ("python" . python-mode)
  401. :init
  402. (add-hook 'python-mode-hook
  403. (lambda ()
  404. ;; (set (make-local-variable 'company-backends)
  405. ;; '((company-jedi company-dabbrev-code company-capf company-files)))
  406. (company-mode t)))
  407. :config
  408. (setq python-shell-completion-native-enable nil)
  409. ;; (defun my/run-python ()
  410. ;; (save-selected-window
  411. ;; (switch-to-buffer-other-window (process-buffer (python-shell-get-or-create-process (python-shell-parse-command)))))
  412. ;; )
  413. ;; (add-hook 'python-mode-hook 'my/run-python)
  414. )
  415. #+end_src
  416. Jedi is a backend for python autocompletion and needs to be installed on the server:
  417. - pip install jedi
  418. Code checks need to be installed, too:
  419. - pip install flake8
  420. #+begin_src emacs-lisp
  421. (use-package company-jedi
  422. :defer t
  423. ;; :after company
  424. :ensure t
  425. :config
  426. (setq jedi:environment-virtualenv (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  427. (setq jedi:python-environment-directory (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  428. (add-hook 'python-mode-hook 'jedi:setup)
  429. (setq jedi:complete-on-dot t)
  430. (setq jedi:use-shortcuts t)
  431. ;; (defun my/python-mode-hook()
  432. ;; (lambda ()
  433. ;; (set (make-local-variable 'company-backends)
  434. ;; '((company-jedi company-dabbrev) company-capf company-files))))
  435. ;; (add-to-list 'company-backends 'company-jedi))
  436. ;; (add-hook 'python-mode-hook 'my/python-mode-hook)
  437. ;; (add-hook 'python-mode-hook 'company-mode)
  438. )
  439. #+end_src
  440. A wrapper to handle virtual environments.
  441. I strongly recommend to install virtual environments on the terminal, not through this wrapper, but changing venvs is fine.
  442. pyvenv might need virtualenvwrapper, I have to test this.
  443. apt install
  444. #+begin_src emacs-lisp
  445. (use-package pyvenv
  446. :ensure t
  447. :init
  448. (setenv "WORKON_HOME" (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/"))
  449. :config
  450. (pyvenv-mode t)
  451. (defun my/post-activate-hook()
  452. (setq jedi:environment-root pyvenv-virtual-env)
  453. (setq jedi:environment-virtualenv pyvenv-virtual-env)
  454. (setq jedi:tooltip-method '(nil)) ;; variants: nil or pos-tip and/or popup
  455. (setq python-shell-virtualenv-root pyvenv-virtual-env)
  456. ;; default traceback, other option M-x jedi:toggle-log-traceback
  457. ;; traceback is in jedi:pop-to-epc-buffer
  458. (jedi:setup)
  459. (setq jedi:server-args '("--log-traceback")))
  460. (add-to-list 'company-backends 'company-jedi)
  461. ;; (add-to-list 'company-backends 'company-anaconda)
  462. ;; (lambda ()
  463. ;; (set (make-local-variable 'company-backends)
  464. ;; '((company-jedi company-dabbrev) company-capf company-files)))
  465. ;; (setq flycheck-checker 'python-pylint))
  466. ;; (flycheck-select-checker 'python-pylint))
  467. ;; (setq flycheck-checker 'python-flake8)
  468. (add-hook 'pyvenv-post-activate-hooks 'my/post-activate-hook)
  469. )
  470. #+end_src
  471. I want Emacs to automatically start the proper virtual environment.
  472. Required is a .python-version file with, content in the first line being /path/to/virtualenv/
  473. [[https://github.com/marcwebbie/auto-virtualenv][Github source]]
  474. Depends on pyvenv
  475. #+begin_src emacs-lisp
  476. (use-package auto-virtualenv
  477. :ensure t
  478. ;; :after pyvenv
  479. ;; :defer t
  480. :init
  481. (add-hook 'python-mode-hook 'auto-virtualenv-set-virtualenv)
  482. ;; activate on changing buffers
  483. ;; (add-hook 'window-configuration-change-hook 'auto-virtualenv-set-virtualenv)
  484. ;; activate on focus in
  485. ;; (add-hook 'focus-in-hook 'auto-virtualenv-set-virtualenv)
  486. )
  487. #+end_src
  488. Anaconda test
  489. begin_src emacs-lisp
  490. (use-package anaconda-mode
  491. :ensure t
  492. :defer t
  493. :init
  494. (add-hook 'python-mode-hook 'anaconda-mode)
  495. ;; (add-hook 'python-mode-hook 'anaconda-eldoc-mode)
  496. :config
  497. (setq anaconda-eldoc-mode 1)
  498. )
  499. end_src
  500. begin_src emacs-lisp
  501. (use-package company-anaconda
  502. :ensure t
  503. :defer t
  504. :init
  505. (defun my/company-anaconda-hook()
  506. (add-to-list 'company-backends 'company-anaconda))
  507. (add-hook 'python-mode-hook 'my/company-anaconda-hook)
  508. )
  509. end_src
  510. * Hydra
  511. Hydra allows grouping of commands
  512. #+begin_src emacs-lisp
  513. (use-package hydra
  514. :ensure t
  515. :bind
  516. ("C-c f" . hydra-flycheck/body)
  517. :config
  518. (setq-default hydra-default-hint nil)
  519. )
  520. #+end_src
  521. ** Hydra Flycheck
  522. Flycheck is necessary, obviously
  523. #+begin_src emacs-lisp
  524. (defhydra hydra-flycheck (:color blue)
  525. "
  526. ^
  527. ^Flycheck^ ^Errors^ ^Checker^
  528. ^────────^──────────^──────^────────────^───────^───────────
  529. _q_ quit _<_ previous _?_ describe
  530. _m_ manual _>_ next _d_ disable
  531. _v_ verify setup _f_ check _s_ select
  532. ^^ ^^ ^^
  533. "
  534. ("q" nil)
  535. ("<" flycheck-previous-error :color red)
  536. (">" flycheck-next-error :color red)
  537. ("?" flycheck-describe-checker)
  538. ("d" flycheck-disable-checker)
  539. ("f" flycheck-buffer)
  540. ("m" flycheck-manual)
  541. ("s" flycheck-select-checker)
  542. ("v" flycheck-verify-setup)
  543. )
  544. #+end_src
  545. * Quality of Life