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.

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