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.

1884 lines
56 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
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. I need a function to know what computer emacs is running on. The display width of 1152 pixel is an oddity of hyper-v and for my usecase specific enough to tell the machine.
  9. #+BEGIN_SRC emacs-lisp
  10. (defvar my/whoami
  11. (if (string-equal user-login-name "POH")
  12. "work_remote"
  13. (if (equal (display-pixel-width) 1152)
  14. "work_hyperv"
  15. (if (string-equal system-type "gnu/linux")
  16. "home"))))
  17. #+END_SRC
  18. * Stuff to add / to fix
  19. - smartparens
  20. a sane default configuration for navigation, manipulation etc. is still necessary
  21. - Spaceline / Powerline or similar
  22. I want a pretty status bar!
  23. - Git gutter:
  24. Do some configuration to make it useful (see given source link in the [[*Git][Section]] of gutter)
  25. Maybe only enable it for modes where it is likely I use git?
  26. - Some webmode stuff
  27. - markdown:
  28. add hooks for certain file extensions, maybe add a smart way to start gfm-mode if markdown-file is in a git-project
  29. - move package dependend configurations inside the package configuration itself, like keymaps for magit
  30. * Update config in a running config
  31. Two options:
  32. - reload the open file: M-x load-file, then press twice to accept
  33. the default filename, which is the currently opened
  34. - Point at the end of any sexp and press C-x C-e
  35. * Customize default settings
  36. Keep the .emacs.d clean by moving user files into separate directories.
  37. - user-local: directory for machine specific files
  38. - user-global: directory for files which work on any machine
  39. - the backup and auto-save files go right to /tmp
  40. #+BEGIN_SRC emacs-lisp
  41. (defvar PATH_USER_LOCAL (expand-file-name "~/.emacs.d/user-local/"))
  42. (defvar PATH_USER_GLOBAL (expand-file-name "~/.emacs.d/user-global/"))
  43. (setq bookmark-default-file (concat PATH_USER_LOCAL "bookmarks"))
  44. (setq recentf-save-file (concat PATH_USER_LOCAL "recentf"))
  45. (setq custom-file (concat PATH_USER_LOCAL "custom.el")) ;don't spam init.el with saved customize settings
  46. (setq abbrev-file-name (concat PATH_USER_GLOBAL "abbrev_defs"))
  47. (setq backup-directory-alist `((".*" . ,temporary-file-directory)))
  48. (setq auto-save-file-name-transforms `((".*" ,temporary-file-directory)))
  49. (setq save-abbrevs 'silently) ; don't bother me with asking if new abbrevs should be saved
  50. #+END_SRC
  51. These functions are useful. Activate them.
  52. #+BEGIN_SRC emacs-lisp
  53. (put 'downcase-region 'disabled nil)
  54. (put 'upcase-region 'disabled nil)
  55. (put 'narrow-to-region 'disabled nil)
  56. (put 'dired-find-alternate-file 'disabled nil)
  57. #+END_SRC
  58. Disable lock files, which cause trouble in e.g. lsp-mode
  59. #+BEGIN_SRC emacs-lisp
  60. (setq-default create-lockfiles nil)
  61. #+END_SRC
  62. Answering just 'y' or 'n' should be enough.
  63. #+BEGIN_SRC emacs-lisp
  64. (defalias 'yes-or-no-p 'y-or-n-p)
  65. #+END_SRC
  66. Don't ask me if I want to load themes.
  67. #+BEGIN_SRC emacs-lisp
  68. (setq custom-safe-themes t)
  69. #+END_SRC
  70. Don't count two spaces after a period as the end of a sentence.
  71. Just one space is needed
  72. #+BEGIN_SRC emacs-lisp
  73. (setq sentence-end-double-space nil)
  74. #+END_SRC
  75. Scroll to the end / beginning of buffer before you throw an error
  76. #+BEGIN_SRC emacs-lisp
  77. (setq scroll-error-top-bottom t)
  78. #+END_SRC
  79. Delete the region when typing, just like as we expect nowadays.
  80. #+BEGIN_SRC emacs-lisp
  81. (delete-selection-mode t)
  82. #+END_SRC
  83. Auto-indent when pressing RET, just new-line when C-j
  84. #+BEGIN_SRC emacs-lisp
  85. (define-key global-map (kbd "RET") 'newline-and-indent)
  86. (define-key global-map (kbd "C-j") 'newline)
  87. #+END_SRC
  88. Set the default window size depending on the system emacs is running on.
  89. ;; TODO:
  90. ;; This size is only reasonable for linux@home
  91. ;; hyperv is way smaller, use fullscreen here
  92. ;; pm should be fullscreen, too
  93. #+BEGIN_SRC emacs-lisp
  94. (if (display-graphic-p)
  95. (pcase my/whoami
  96. ("home" (progn
  97. (setq initial-frame-alist
  98. '((width . 165)
  99. (height . 70)))
  100. (setq default-frame-alist
  101. '((width . 165)
  102. (height . 70)))))
  103. ("work_remote" (add-to-list 'initial-frame-alist '(fullscreen . maximized)))
  104. ("work_hyperv" (add-to-list 'initial-frame-alist '(fullscreen . maximized)))))
  105. #+END_SRC
  106. * Windows specific stuff
  107. ** Performance
  108. [[https://github.com/cbowdon/Config/blob/master/emacs/init.org][Got it from here]]
  109. #+BEGIN_SRC emacs-lisp
  110. (when (eq system-type 'windows-nt)
  111. (if (>= emacs-major-version 25)
  112. (remove-hook 'find-file-hooks 'vc-refresh-state)
  113. (remove-hook 'find-file-hooks 'vc-find-file-hook))
  114. (progn
  115. (setq gc-cons-threshold (* 511 1024 1024)
  116. gc-cons-percentage 0.5
  117. garbage-collection-messages t
  118. w32-get-true-file-attributes nil)
  119. (run-with-idle-timer 5 t #'garbage-collect)))
  120. #+END_SRC
  121. Hopefully this makes magit faster
  122. #+BEGIN_SRC emacs-lisp
  123. (when (eq system-type 'windows-nt)
  124. (setq w32-pipe-read-delay 0)
  125. (setq w32-get-true-file-attributes nil))
  126. #+END_SRC
  127. * Visuals
  128. ** Font
  129. Don't add the font in the work environment, which I am logged in as POH
  130. #+BEGIN_SRC emacs-lisp
  131. (pcase my/whoami
  132. ("home" (set-face-attribute 'default nil :font "Hack-10"))
  133. ("work_hyperv" (set-face-attribute 'default nil :font "Hack-12"))
  134. )
  135. #+END_SRC
  136. ** Themes
  137. *** Material Theme
  138. The [[https://github.com/cpaulik/emacs-material-theme][Material Theme]] comes in a dark and a light variant. Not too dark
  139. to be strenious though.
  140. b
  141. #+BEGIN_SRC emacs-lisp
  142. (use-package material-theme
  143. :if (window-system)
  144. :defer t
  145. :ensure t
  146. )
  147. #+END_SRC
  148. *** Apropospriate Theme
  149. Variants dark and light
  150. #+BEGIN_SRC emacs-lisp
  151. (use-package apropospriate-theme
  152. :if (window-system)
  153. :defer t
  154. :ensure t
  155. )
  156. #+END_SRC
  157. *** Ample Theme
  158. Variants:
  159. - ample
  160. - ample-flat
  161. - ample-light
  162. #+BEGIN_SRC emacs-lisp
  163. (use-package ample-theme
  164. :if (window-system)
  165. :defer t
  166. :ensure t
  167. :init
  168. (load-theme 'ample-flat t)
  169. )
  170. #+END_SRC
  171. ** Prettier Line Wraps
  172. By default there is no line wrapping. M-q actually modifies the buffer, which might not be wanted.
  173. So: enable visual wrapping and keep indentation if there are any.
  174. #+BEGIN_SRC emacs-lisp
  175. (global-visual-line-mode)
  176. (diminish 'visual-line-mode)
  177. (use-package adaptive-wrap
  178. :ensure t
  179. :init
  180. (when (fboundp 'adaptive-wrap-prefix-mode)
  181. (defun my-activate-adaptive-wrap-prefix-mode ()
  182. "Toggle `visual-line-mode' and `adaptive-wrap-prefix-mode' simultaneously."
  183. (adaptive-wrap-prefix-mode (if visual-line-mode 1 -1)))
  184. (add-hook 'visual-line-mode-hook 'my-activate-adaptive-wrap-prefix-mode))
  185. )
  186. #+END_SRC
  187. ** Mode Line
  188. Change the default mode line to something prettier. [[https://github.com/Malabarba/smart-mode-line][Source]]
  189. #+BEGIN_SRC emacs-lisp
  190. (use-package smart-mode-line
  191. :ensure t
  192. :config
  193. (tool-bar-mode -1)
  194. (setq sml/theme 'respectful)
  195. (setq sml/name-width 40)
  196. (setq sml/mode-width 'full)
  197. (set-face-attribute 'mode-line nil
  198. :box nil)
  199. (sml/setup))
  200. #+END_SRC
  201. ** Line numbers
  202. #+BEGIN_SRC emacs-lisp
  203. (use-package linum
  204. :ensure t
  205. :init
  206. (add-hook 'prog-mode-hook 'linum-mode))
  207. #+END_SRC
  208. ** Misc
  209. UTF-8 please, but don't mess with line endings.
  210. #+BEGIN_SRC emacs-lisp
  211. (setq locale-coding-system 'utf-8)
  212. (set-terminal-coding-system 'utf-8)
  213. (set-keyboard-coding-system 'utf-8)
  214. (set-selection-coding-system 'utf-8)
  215. (if (eq system-type 'windows-nt)
  216. (prefer-coding-system 'utf-8-dos)
  217. (prefer-coding-system 'utf-8))
  218. #+END_SRC
  219. Turn off blinking cursor
  220. #+BEGIN_SRC emacs-lisp
  221. (blink-cursor-mode -1)
  222. #+END_SRC
  223. #+BEGIN_SRC emacs-lisp
  224. (show-paren-mode t)
  225. (column-number-mode t)
  226. (setq uniquify-buffer-name-style 'forward)
  227. #+END_SRC
  228. Avoid tabs in place of multiple spaces (they look bad in TeX) and show empty lines
  229. #+BEGIN_SRC emacs-lisp
  230. (setq-default indent-tabs-mode nil)
  231. (setq-default indicate-empty-lines t)
  232. #+END_SRC
  233. Smooth scrolling. Emacs tends to be jumpy, this should change it.
  234. #+BEGIN_SRC emacs-lisp
  235. (setq scroll-margin 5
  236. scroll-conservatively 10000
  237. scroll-preserve-screen-position 1
  238. scroll-step 1)
  239. #+END_SRC
  240. Highlight current line
  241. #+BEGIN_SRC emacs-lisp
  242. (global-hl-line-mode t)
  243. #+END_SRC
  244. * Usability
  245. ** which-key
  246. Greatly increases discovery of functions!
  247. Click [[https://github.com/justbur/emacs-which-key][here]] for source and more info.
  248. Info in Emacs: M-x customize-group which-key
  249. #+BEGIN_SRC emacs-lisp
  250. (use-package which-key
  251. :ensure t
  252. :diminish which-key-mode
  253. :config
  254. (which-key-mode)
  255. (which-key-setup-side-window-right-bottom)
  256. (which-key-setup-minibuffer)
  257. (setq which-key-idle-delay 0.5)
  258. )
  259. #+END_SRC
  260. ** Recentf
  261. Activate and configure recentf
  262. #+BEGIN_SRC emacs-lisp
  263. (recentf-mode t)
  264. (setq recentf-max-saved-items 200)
  265. #+END_SRC
  266. ** Hydra
  267. Hydra allows grouping of commands
  268. #+BEGIN_SRC emacs-lisp
  269. (use-package hydra
  270. :ensure t
  271. :bind
  272. ("C-c f" . hydra-flycheck/body)
  273. ("C-c g" . hydra-git-gutter/body)
  274. :config
  275. (setq-default hydra-default-hint nil)
  276. )
  277. #+END_SRC
  278. ** Evil
  279. So... Evil Mode might be worth a try
  280. #+BEGIN_SRC emacs-lisp
  281. (use-package evil
  282. :ensure t
  283. :defer .1 ;; don't block emacs when starting, load evil immediately after startup
  284. :init
  285. (setq evil-want-integration nil) ;; required by evil-collection
  286. :config
  287. (evil-mode 1)) ;; for now deactivate per default
  288. #+END_SRC
  289. Evil-collection is a bundle of configs for different modes.
  290. 2018-05-01: evil collection causes error
  291. "Invalid function: with-helm-buffer"
  292. #+BEGIN_SRC emacs-lisp
  293. ;(use-package evil-collection
  294. ; :after evil
  295. ; :ensure t
  296. ; :config
  297. ; (evil-collection-init))
  298. #+END_SRC
  299. Evil-goggles give visual hints when editing texts, so it's more obvious what is actually happening. [[https://github.com/edkolev/evil-goggles][Source]]
  300. #+BEGIN_SRC emacs-lisp
  301. (use-package evil-goggles
  302. :after evil
  303. :ensure t
  304. :diminish evil-goggles-mode
  305. :config
  306. (evil-goggles-mode)
  307. (evil-goggles-use-diff-faces))
  308. #+END_SRC
  309. ** General (keymapper)
  310. I just use general.el to define keys and keymaps. With it I can set leader keys and create keymaps for them. It also integrates well with which-key.
  311. [[https://github.com/noctuid/general.el][Source]]
  312. #+BEGIN_SRC emacs-lisp
  313. (use-package general
  314. :ensure t
  315. )
  316. #+END_SRC
  317. ** Custom key mappings
  318. Now some keymaps.
  319. If there is no map defined, it is considered the global key map.
  320. #+BEGIN_SRC emacs-lisp
  321. (general-define-key
  322. :states '(normal visual insert emacs)
  323. :prefix "SPC"
  324. :non-normal-prefix "M-SPC"
  325. "TAB" '(ivy-switch-buffer :which-key "prev buffer")
  326. "SPC" '(counsel-M-x :which-key "M-x")
  327. "g" '(:ignore t :which-key "Git")
  328. "gs" '(magit-status :which-key "git-status")
  329. )
  330. #+END_SRC
  331. A map for org-mode
  332. #+BEGIN_SRC emacs-lisp
  333. (general-define-key
  334. :states '(normal visual insert emacs)
  335. :keymaps 'org-mode-map
  336. :prefix "SPC"
  337. :non-normal-prefix "M-SPC"
  338. "t" '(counsel-org-tag :which-key "org-tag"))
  339. #+END_SRC
  340. A map for dired, based on [[https://github.com/emacs-evil/evil-collection/blob/master/evil-collection-dired.el][evil-collection]]
  341. #+BEGIN_SRC emacs-lisp
  342. (general-define-key
  343. :states '(normal visual insert emacs)
  344. :keymaps 'dired-mode-map
  345. "q" '(quit-window :which-key "quit-window")
  346. "j" '(dired-next-line :which-key "next line")
  347. "k" '(dired-previous-line :which-key "previous line")
  348. "D" '(dired-do-delete :which-key "delete")
  349. "C" '(dired-do-copy :which-key "copy")
  350. "t" '(:ignore t :which-key "dir navigation")
  351. "td" '(dired-tree-down :which-key "tree down")
  352. "tu" '(dired-tree-up :which-key "tree up")
  353. "tn" '(dired-next-subdir :which-key "next subdir")
  354. "tp" '(dired-prev-subdir :which-key "previous subdir")
  355. )
  356. #+END_SRC
  357. #+BEGIN_SRC emacs-lisp
  358. (general-define-key
  359. :states '(normal)
  360. :keymaps 'lsp-ui-imenu-mode-map
  361. (kbd "RET") '(lsp-ui-imenu--view :which-key "view")
  362. (kbd "M-RET") '(lsp-ui-imenu--visit :which-key "visit")
  363. )
  364. #+END_SRC
  365. ** List buffers
  366. Ibuffer is the improved version of list-buffers.
  367. Make ibuffer the default buffer lister. [[http://ergoemacs.org/emacs/emacs_buffer_management.html][Source]]
  368. #+BEGIN_SRC emacs-lisp
  369. (defalias 'list-buffers 'ibuffer)
  370. #+END_SRC
  371. Also auto refresh dired, but be quiet about it. [[http://whattheemacsd.com/sane-defaults.el-01.html][Source]]
  372. #+BEGIN_SRC emacs-lisp
  373. (add-hook 'dired-mode-hook 'auto-revert-mode)
  374. (setq global-auto-revert-non-file-buffers t)
  375. (setq auto-revert-verbose nil)
  376. #+END_SRC
  377. ** ivy / counsel / swiper
  378. Flx is required for fuzzy-matching
  379. Is it really necessary?
  380. BEGIN_SRC emacs-lisp
  381. (use-package flx)
  382. end_src
  383. Ivy displays a window with suggestions for hotkeys and M-x
  384. #+BEGIN_SRC emacs-lisp
  385. (use-package ivy
  386. :ensure t
  387. :diminish
  388. (ivy-mode . "") ;; does not display ivy in the mode line
  389. :init
  390. (ivy-mode 1)
  391. :bind
  392. ("C-c C-r" . ivy-resume)
  393. :config
  394. (setq ivy-use-virtual-buffers t) ;; recent files and bookmarks in ivy-switch-buffer
  395. (setq ivy-height 20) ;; height of ivy window
  396. (setq ivy-count-format "%d/%d") ;; current and total number
  397. (setq ivy-re-builders-alist ;; regex replaces spaces with *
  398. '((t . ivy--regex-plus)))
  399. )
  400. #+END_SRC
  401. The find-file replacement is nicer to navigate
  402. #+BEGIN_SRC emacs-lisp
  403. (use-package counsel
  404. :ensure t
  405. :bind* ;; load counsel when pressed
  406. (("M-x" . counsel-M-x)
  407. ("C-x C-f" . counsel-find-file)
  408. ("C-x C-r" . counsel-recentf)
  409. ("C-c C-f" . counsel-git)
  410. ("C-c h f" . counsel-describe-function)
  411. ("C-c h v" . counsel-describe-variable)
  412. ("M-i" . counsel-imenu)
  413. )
  414. )
  415. #+END_SRC
  416. Swiper ivy-enhances isearch
  417. #+BEGIN_SRC emacs-lisp
  418. (use-package swiper
  419. :ensure t
  420. :bind
  421. (("C-s" . swiper)
  422. ("C-c C-r" . ivy-resume)
  423. )
  424. )
  425. #+END_SRC
  426. Ivy-Hydra adds stuff in minibuffer when you press C-o
  427. #+BEGIN_SRC emacs-lisp
  428. (use-package ivy-hydra
  429. :ensure t)
  430. #+END_SRC
  431. ** Helm
  432. This is just a try to see how it works differently.
  433. #+BEGIN_SRC emacs-lisp
  434. (use-package helm
  435. :ensure t
  436. :init
  437. (helm-mode 1)
  438. :bind
  439. ; (("M-x" . helm-M-x)
  440. ; ("C-x C-f" . helm-find-files)
  441. ; ("C-x C-r" . helm-recentf)
  442. ; ("C-x b" . helm-buffers-list))
  443. :config
  444. (setq helm-buffers-fuzzy-matching t)
  445. )
  446. (use-package helm-descbinds
  447. :ensure t
  448. :bind
  449. ("C-h b" . helm-descbinds))
  450. (use-package helm-projectile
  451. :ensure t
  452. :config
  453. (helm-projectile-on))
  454. #+END_SRC
  455. ** Undo
  456. Show an undo tree in a new buffer which can be navigated.
  457. #+BEGIN_SRC emacs-lisp
  458. (use-package undo-tree
  459. :ensure t
  460. :diminish undo-tree-mode
  461. :init
  462. (global-undo-tree-mode 1))
  463. #+END_SRC
  464. ** Ido (currently inactive)
  465. better completion
  466. #+BEGIN_SRC emacs-lisp
  467. ;(use-package ido
  468. ; :init
  469. ; (setq ido-enable-flex-matching t)
  470. ; (setq ido-everywhere t)
  471. ; (ido-mode t)
  472. ; (use-package ido-vertical-mode
  473. ; :ensure t
  474. ; :defer t
  475. ; :init
  476. ; (ido-vertical-mode 1)
  477. ; (setq ido-vertical-define-keys 'C-n-and-C-p-only)
  478. ; )
  479. ;)
  480. #+END_SRC
  481. ** imenu-list
  482. A minor mode to show imenu in a sidebar.
  483. Call imenu-list-smart-toggle.
  484. [[https://github.com/bmag/imenu-list][Source]]
  485. #+BEGIN_SRC emacs-lisp
  486. (use-package imenu-list
  487. :ensure t
  488. :config
  489. (setq imenu-list-focus-after-activation t
  490. imenu-list-auto-resize t
  491. imenu-list-position 'right)
  492. :bind
  493. (:map global-map
  494. ([f9] . imenu-list-smart-toggle))
  495. )
  496. #+END_SRC
  497. ** Treemacs
  498. A file manager comparable to neotree.
  499. [[https://github.com/Alexander-Miller/treemacs][Github]]
  500. 2018-09-01: Treemacs needs emacs 25.2, debian stretch runs 25.1. Sucks.
  501. It has some requirements, which gets used here anyway:
  502. - ace-window
  503. - hydra
  504. - projectile
  505. - python
  506. I copied the configuration example from the github site.
  507. No idea what this executable-find is about.
  508. TODO check it out!
  509. BEGIN_SRC emacs-lisp
  510. (use-package treemacs
  511. :ensure t
  512. :defer t
  513. :config
  514. (setq treemacs-collapse-dirs (if (executable-find "python") 3 0)
  515. treemacs-file-event-delay 5000
  516. treemacs-follow-after-init t
  517. treemacs-follow-recenter-distance 0.1
  518. treemacs-goto-tag-strategy 'refetch-index
  519. treemacs-indentation 2
  520. treemacs-indentation-string " "
  521. treemacs-is-never-other-window nil
  522. treemacs-no-png-images nil
  523. treemacs-project-follow-cleanup nil
  524. treemacs-recenter-after-file-follow nil
  525. treemacs-recenter-after-tag-follow nil
  526. treemacs-show-hidden-files t
  527. treemacs-silent-filewatch nil
  528. treemacs-silent-refresh nil
  529. treemacs-sorting 'alphabetic-desc
  530. treemacs-tag-follow-cleanup t
  531. treemacs-tag-follow-delay 1.5
  532. treemacs-width 35)
  533. (treemacs-follow-mode t)
  534. (treemacs-filewatch-mode t)
  535. (pcase (cons (not (null (executable-find "git")))
  536. (not (null (executable-find "python3"))))
  537. (`(t . t)
  538. (treemacs-git-mode 'extended))
  539. (`(t . _)
  540. (treemacs-git-mode 'simple)))
  541. :bind
  542. (:map global-map
  543. ([f8] . treemacs))
  544. )
  545. END_SRC
  546. Treemacs-Evil is necessary when using evil
  547. BEGIN_SRC emacs-lisp
  548. (use-package treemacs-evil
  549. :after treemacs evil
  550. :ensure t)
  551. END_SRC
  552. Treemacs-projectile is useful for uhh.. TODO explain!
  553. BEGIN_SRC emacs-lisp
  554. (use-package treemacs-projectile
  555. :ensure t
  556. :after treemacs projectile
  557. :defer t
  558. :config
  559. (setq treemacs-header-function #'treemacs-projectile-create-header)
  560. )
  561. END_SRC
  562. TODO
  563. Hydrastuff or keybindings for functions:
  564. - treemacs-projectile
  565. - treemacs-projectile-toggle
  566. - treemacs-toggle
  567. - treemacs-bookmark
  568. - treemacs-find-file
  569. - treemacs-find-tag
  570. ** Window Handling
  571. Some tools to easen the navigation, creation and deletion of windows
  572. *** Ace-Window
  573. #+BEGIN_SRC emacs-lisp
  574. (use-package ace-window
  575. :ensure t
  576. :init
  577. (global-set-key (kbd "C-x o") 'ace-window)
  578. )
  579. #+END_SRC
  580. *** Windmove
  581. Windmove easens the navigation between windows.
  582. Here we are setting the default keybindings (shift+arrow)
  583. CURRENTLY NOT WORKING, defaults are blocked.
  584. Also not sure if necessary when using ace-window.
  585. #+BEGIN_SRC emacs-lisp
  586. (use-package windmove
  587. :ensure t
  588. :config
  589. (windmove-default-keybindings)
  590. )
  591. #+END_SRC
  592. ** Tramp
  593. With tramp you can handle remote files like local files.
  594. Usage example:
  595. C-x C-f /ssh:name@server:/path
  596. To open a file as sudo:
  597. C-x C-f /ssh:/name@server|sudo:name@server:/path
  598. #+BEGIN_SRC emacs-lisp
  599. (use-package tramp
  600. :ensure t
  601. )
  602. #+END_SRC
  603. ** misc
  604. Visual feedback when using regexp on the buffer
  605. #+BEGIN_SRC emacs-lisp
  606. (use-package visual-regexp
  607. :ensure t
  608. :defer t
  609. :bind (("C-c r s" . query-replace)
  610. ("C-c r R" . vr/replace)
  611. ("C-c r r" . vr/query-replace)
  612. ("C-c r m" . vr/mc-mark)))
  613. #+END_SRC
  614. Newline at the end of file
  615. #+BEGIN_SRC emacs-lisp
  616. (setq require-final-newline t)
  617. #+END_SRC
  618. Delete the selection with a keypress
  619. #+BEGIN_SRC emacs-lisp
  620. (delete-selection-mode t)
  621. #+END_SRC
  622. Remember the current location in a file
  623. #+BEGIN_SRC emacs-lisp
  624. (use-package saveplace
  625. :unless noninteractive
  626. :config
  627. (save-place-mode))
  628. #+END_SRC
  629. * Company Mode
  630. Complete Anything!
  631. Activate company and make it react nearly instantly
  632. #+BEGIN_SRC emacs-lisp
  633. (use-package company
  634. :ensure t
  635. :config
  636. (setq-default company-minimum-prefix-length 1
  637. company-tooltip-align-annotation t
  638. company-tooltop-flip-when-above t
  639. company-show-numbers t
  640. company-idle-delay 0.1)
  641. ; (define-key company-active-map (kbd "RET") nil)
  642. (company-tng-configure-default)
  643. :bind
  644. (:map company-active-map
  645. ("TAB" . company-complete-selection)
  646. ("<tab>" . company-complete-selection))
  647. )
  648. #+END_SRC
  649. For a nicer suggestion box: company-box ([[https://github.com/sebastiencs/company-box][Source]])
  650. It is only available for emacs 26 and higher.
  651. #+BEGIN_SRC emacs-lisp
  652. (when (> emacs-major-version 25)
  653. (use-package company-box
  654. :ensure t
  655. :init
  656. (add-hook 'company-mode-hook 'company-box-mode)))
  657. #+END_SRC
  658. * Org Mode
  659. ** Installation
  660. 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.]]
  661. Added a hook to complete org functions, company-capf is necessary for this
  662. #+BEGIN_SRC emacs-lisp
  663. (use-package org
  664. :ensure org-plus-contrib
  665. :init
  666. (add-hook 'org-mode-hook 'company/org-mode-hook)
  667. )
  668. (add-hook 'org-mode-hook 'company/org-mode-hook)
  669. #+END_SRC
  670. 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:
  671. #+BEGIN_SRC shell
  672. var ORG_DIR=(let* ((org-v (cadr (split-string (org-version nil t) "@"))) (len (length org-v))) (substring org-v 1 (- len 2)))
  673. rm ${ORG_DIR}/*.elc
  674. echo 'cleaned .elc from package directory'
  675. #+END_SRC
  676. ** Setup
  677. *** Paths
  678. Paths need to be different for work and home
  679. #+BEGIN_SRC emacs-lisp
  680. (pcase my/whoami
  681. ("work_remote"
  682. (progn
  683. (defvar PATH_ORG_FILES "p:/Eigene Dateien/Notizen/")
  684. (defvar PATH_ORG_JOURNAL "p:/Eigene Dateien/Notizen/Journal/")
  685. (defvar PATH_START "p:/Eigene Dateien/Notizen/"))
  686. (setq org-default-notes-file (concat PATH_ORG_FILES "notes.org"))
  687. (setq org-agenda-files (list(concat PATH_ORG_FILES "notes.org")
  688. (concat PATH_ORG_FILES "projects.org")
  689. (concat PATH_ORG_FILES "todo.org"))))
  690. ("home"
  691. (progn
  692. (defvar PATH_ORG_FILES_MOBILE "~/Archiv/Organisieren/mobile/")
  693. (defvar PATH_ORG_JOURNAL "~/Archiv/Organisieren/Journal/")
  694. (defvar PATH_ORG_FILES "~/Archiv/Organisieren/")
  695. (setq org-default-notes-file (concat PATH_ORG_FILES "notes.org"))
  696. ;; TODO: ignore notes.org, which shouldn't include any todos
  697. ;; (setq org-agenda-file-regexp "'\\`[^.].*\\.org\\'")
  698. (setq org-agenda-files (list PATH_ORG_FILES PATH_ORG_FILES_MOBILE))))
  699. )
  700. (setq org-id-locations-file (concat PATH_USER_LOCAL ".org-id-locations"))
  701. #+END_SRC
  702. *** Settings
  703. 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.
  704. See the doc for speed keys by checking out the documentation for speed keys in Org mode.
  705. #+BEGIN_SRC emacs-lisp
  706. (setq org-use-speed-commands t)
  707. (setq org-image-actual-width 550)
  708. (setq org-highlight-latex-and-related '(latex script entities))
  709. #+END_SRC
  710. Hide emphasis markup (e.g. / ... / for italics, etc.)
  711. #+BEGIN_SRC emacs-lisp
  712. (setq org-hide-emphasis-markers t)
  713. #+END_SRC
  714. The default value for the org tag column is -77, which is weird for smaller width windows. I'd rather have the tags align horizontally with the header.
  715. 45 is a good column number to do that.
  716. #+BEGIN_SRC emacs-lisp
  717. (setq org-tags-column 45)
  718. #+END_SRC
  719. Set the org-refile targets.
  720. TODO: change maxlevel if necessary
  721. #+BEGIN_SRC emacs-lisp
  722. (setq org-refile-targets '((org-agenda-files . (:maxlevel . 1))))
  723. #+END_SRC
  724. *** Org key bindings
  725. Set up some global key bindings that integrate with Org mode features
  726. #+BEGIN_SRC emacs-lisp
  727. (bind-key "C-c l" 'org-store-link)
  728. (bind-key "C-c c" 'org-capture)
  729. (bind-key "C-c a" 'org-agenda)
  730. #+END_SRC
  731. Org overwrites RET and C-j, so I need to disable the rebinds
  732. #+BEGIN_SRC emacs-lisp
  733. (define-key org-mode-map (kbd "RET") nil) ;;org-return
  734. (define-key org-mode-map (kbd "C-j") nil) ;;org-return-indent
  735. #+END_SRC
  736. *** Org agenda
  737. For a more detailed example [[https://github.com/sachac/.emacs.d/blob/83d21e473368adb1f63e582a6595450fcd0e787c/Sacha.org#org-agenda][see here]].
  738. Custom todo-keywords, depending on environment
  739. #+BEGIN_SRC emacs-lisp
  740. (pcase my/whoami
  741. ("work_remote")
  742. (setq org-todo-keywords
  743. '((sequence "OPEN" "TODO" "UNCLEAR" "|" "DONE" "IMPOSSIBLE")))
  744. )
  745. #+END_SRC
  746. Sort org agenda by deadline and priority
  747. #+BEGIN_SRC emacs-lisp
  748. (setq org-agenda-sorting-strategy
  749. (quote
  750. ((agenda deadline-up priority-down)
  751. (todo priority-down category-keep)
  752. (tags priority-down category-keep)
  753. (search category-keep)))
  754. )
  755. #+END_SRC
  756. Customize the org agenda
  757. #+BEGIN_SRC emacs-lisp
  758. (defun my-org-skip-subtree-if-priority (priority)
  759. "Skip an agenda subtree if it has a priority of PRIORITY.
  760. PRIORITY may be one of the characters ?A, ?B, or ?C."
  761. (let ((subtree-end (save-excursion (org-end-of-subtree t)))
  762. (pri-value (* 1000 (- org-lowest-priority priority)))
  763. (pri-current (org-get-priority (thing-at-point 'line t))))
  764. (if (= pri-value pri-current)
  765. subtree-end
  766. nil)))
  767. (setq org-agenda-custom-commands
  768. '(("c" "Simple agenda view"
  769. ((tags "PRIORITY=\"A\""
  770. ((org-agenda-skip-function '(org-agenda-skip-entry-if 'todo 'done))
  771. (org-agenda-overriding-header "Hohe Priorität:")))
  772. (agenda ""
  773. ((org-agenda-span 7)
  774. (org-agenda-start-on-weekday nil)
  775. (org-agenda-overriding-header "Nächsten 7 Tage:")))
  776. (alltodo ""
  777. ((org-agenda-skip-function '(or (my-org-skip-subtree-if-priority ?A)
  778. (org-agenda-skip-if nil '(scheduled deadline))))
  779. (org-agenda-overriding-header "Sonstige Aufgaben:"))))))
  780. )
  781. #+END_SRC
  782. *** Org Capture
  783. The basic format is:
  784. hotkey - name - type - location - content
  785. For todos a prompt for the title is demanded. This made it possible to place the cursor for the content to the right position. Just typing, C-c C-c, done!
  786. #+BEGIN_SRC emacs-lisp
  787. (pcase my/whoami
  788. ("home"
  789. (setq org-capture-templates
  790. `(("n" "note" entry (org-default-notes-file))
  791. ("t" "todo" entry (file+headline ,(concat PATH_ORG_FILES "tasks.org") "Tasks")
  792. "* TODO %^{Title}\n %u\n %?\n")
  793. ("c" "coding todo" entry (file+headline ,(concat PATH_ORG_FILES "tasks.org") "Tasks")
  794. "* TODO %^{Title}\n %u %a\n %?\n")
  795. ("p" "project" entry (file+headline ,(concat PATH_ORG_FILES "tasks.org") "Projects")
  796. "* TODO %^{Title} %^g\n %u\n %?\n"))))
  797. ("work_remote"
  798. (setq org-capture-templates
  799. `(("n" "note" entry (file org-default-notes-file))
  800. ("t" "todo" entry (file+headline ,(concat PATH_ORG_FILES "todo.org") "Todos")
  801. "* TODO %^{Title}\n %u\n %?\n")
  802. ("p" "project" entry (file+headline ,(concat PATH_ORG_FILES "projects.org") "Projekte")
  803. "* OPEN %^{Title}\n %u\n** Beschreibung\n %?\n** Zu erledigen\n*** \n** Verlauf\n*** a\n"))))
  804. )
  805. #+END_SRC
  806. ** Org babel languages
  807. This code block is linux specific. Loading languages which aren't available seems to be a problem.
  808. New: Load languages on demand. I need to test if this works as intended.
  809. #+BEGIN_SRC emacs-lisp
  810. (defadvice org-babel-execute-src-block (around load-language nil activate)
  811. "Load language if needed"
  812. (let ((language (org-element-property :language (org-element-at-point))))
  813. (unless (cdr (assoc (intern language) org-babel-load-languages))
  814. (add-to-list 'org-babel-load-languages (cons (intern language) t))
  815. (org-babel-do-load-languages 'org-babel-load-languages org-babel-load-languages))
  816. ad-do-it))
  817. #+END_SRC
  818. BEGIN_SRC emacs-lisp
  819. (cond ((eq system-type 'gnu/linux)
  820. (org-babel-do-load-languages
  821. 'org-babel-load-languages
  822. '(
  823. (C . t)
  824. (calc . t)
  825. (java . t)
  826. (ipython . t)
  827. (js . t)
  828. (latex . t)
  829. (ledger . t)
  830. (beancount . t)
  831. (lisp . t)
  832. (python . t)
  833. (R . t)
  834. (ruby . t)
  835. (scheme . t)
  836. (shell . t)
  837. (sqlite . t)
  838. )
  839. ))
  840. )
  841. END_SRC
  842. #+BEGIN_SRC emacs-lisp
  843. (defun my-org-confirm-babel-evaluate (lang body)
  844. "Do not confirm evaluation for these languages."
  845. (not (or (string= lang "beancount")
  846. (string= lang "C")
  847. (string= lang "emacs-lisp")
  848. (string= lang "ipython")
  849. (string= lang "java")
  850. (string= lang "ledger")
  851. (string= lang "python")
  852. (string= lang "R")
  853. (string= lang "sqlite"))))
  854. (setq org-confirm-babel-evaluate 'my-org-confirm-babel-evaluate)
  855. #+END_SRC
  856. #+BEGIN_SRC emacs-lisp
  857. (add-hook 'org-babel-after-execute-hook 'org-display-inline-images)
  858. (add-hook 'org-mode-hook 'org-display-inline-images)
  859. #+END_SRC
  860. ** Org babel/source blocks
  861. 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
  862. 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
  863. #+BEGIN_SRC emacs-lisp
  864. (setq org-src-fontify-natively t
  865. org-src-window-setup 'current-window
  866. org-src-strip-leading-and-trailing-blank-lines t
  867. org-src-preserve-indentation nil ; these two lines respect the indentation of
  868. org-edit-src-content-indentation 0 ; the surrounding text around the source block
  869. org-src-tab-acts-natively t)
  870. #+END_SRC
  871. ** Org babel helper functions
  872. * Pandoc
  873. Convert between formats, like from org to html.
  874. Pandoc needs to be installed on the system
  875. #+BEGIN_EXAMPLE
  876. sudo apt install pandoc
  877. #+END_EXAMPLE
  878. Pandoc-mode is a minor mode to interact with pandoc
  879. #+BEGIN_SRC emacs-lisp
  880. (use-package pandoc-mode
  881. :ensure t
  882. :init
  883. (add-hook 'markdown-mode-hook 'pandoc-mode))
  884. #+END_SRC
  885. * Emails
  886. Currently following tools are required:
  887. - notmuch (edit, read, tag, delete emails)
  888. - isync /mbsync (fetch or sync emails)
  889. After setting up mbsync, notmuch must be configured. Execute "notmuch" from the command line to launch the setup wizard. After it, "notmuch new" to create a new database, which will index the available local e-mails.
  890. TODO:
  891. - setup of mbsync on linux
  892. - setup of notmuch on linux
  893. - shell script for installation of isync and notmuch
  894. - more config for notmuch?
  895. - hydra for notmuch?
  896. - maybe org-notmuch?
  897. - some way to refresh the notmuch db before I run notmuch?
  898. #+BEGIN_SRC emacs-lisp
  899. (unless (string-equal my/whoami "work_remote")
  900. (use-package notmuch
  901. :defer t
  902. :ensure t
  903. )
  904. )
  905. #+END_SRC
  906. * Personal Finances
  907. After trying ledger, I chose beancount. It is closer to real bookkeeping and has stricter rules.
  908. Since there is no debian package, it is an option to install it via pip.
  909. I picked /opt for the installation path
  910. #+BEGIN_EXAMPLE
  911. sudo su
  912. cd /opt
  913. python3 -m venv beancount
  914. source ./beancount/bin/activate
  915. pip3 install wheel
  916. pip3 install beancount
  917. sleep 100
  918. echo "shell running!"
  919. deactivate
  920. #+END_EXAMPLE
  921. When using beancount, it will automatically pick the created virtual environment.
  922. Activate the beancount mode. ATTENTION: This mode is made by myself.
  923. #+BEGIN_SRC emacs-lisp
  924. (unless (string-equal my/whoami "work_remote")
  925. (load "/home/marc/.emacs.d/user-local/elisp/beancount-mode.el") ; somehow load-path in use-package doesn't work
  926. (use-package beancount
  927. :load-path "/home/marc/.emacs.d/elisp"
  928. :defer t
  929. :mode ("\\.beancount$" . beancount-mode)
  930. :init
  931. (add-hook 'beancount-mode-hook 'company/beancount-mode-hook)
  932. (setenv "PATH"
  933. (concat
  934. "/opt/beancount/bin:"
  935. (getenv "PATH"))
  936. )
  937. :config
  938. (setq beancount-filename-main "/home/marc/Archiv/Finanzen/Transaktionen/transactions.beancount")
  939. )
  940. )
  941. #+END_SRC
  942. To support org-babel, check if it can find the symlink to ob-beancount.el.
  943. #+BEGIN_SRC shell
  944. orgpath=`find /home/marc/.emacs.d/elpa/ -type d -name "org-plus*" -print`
  945. beansym="$orgpath/ob-beancount.el"
  946. bean="/home/marc/Archiv/Programmierprojekte/Lisp/beancount-mode/ob-beancount.el"
  947. if [ -h "$beansym" ]
  948. then
  949. echo "$beansym found"
  950. elif [ -e "$bean" ]
  951. then
  952. echo "creating symlink"
  953. ln -s "$bean" "$beansym"
  954. else
  955. echo "$bean not found, symlink creation aborted"
  956. fi
  957. #+END_SRC
  958. #+RESULTS:
  959. : /home/marc/.emacs.d/elpa/org-plus-contrib-20180521/ob-beancount.el found
  960. Installing fava for reports is strongly recommended.
  961. #+BEGIN_EXAMPLE
  962. cd /opt
  963. python3 -m venv vava
  964. source ./vava/bin/activate
  965. pip3 install wheel
  966. pip3 install fava
  967. deactivate
  968. #+END_EXAMPLE
  969. Start fava with
  970. #+BEGIN_EXAMPLE
  971. fava my_file.beancount
  972. #+END_EXAMPLE
  973. It is accessable on this URL: [[http://127.0.0.1:5000][Fava]]
  974. Beancount-mode can start fava and open the URL right away.
  975. * Programming
  976. ** Common things
  977. List of plugins and settings which are shared between the language plugins
  978. Highlight whitespaces, tabs, empty lines.
  979. #+BEGIN_SRC emacs-lisp
  980. (use-package whitespace
  981. :demand t
  982. :ensure nil
  983. :diminish whitespace-mode;;mode shall be active, but not shown in mode line
  984. :init
  985. (dolist (hook '(prog-mode-hook
  986. text-mode-hook
  987. conf-mode-hook))
  988. (add-hook hook #'whitespace-mode))
  989. ;; :hook ;;not working in use-package 2.3
  990. ;; ((prog-mode . whitespace-turn-on)
  991. ;; (text-mode . whitespace-turn-on))
  992. :config
  993. (setq-default whitespace-style '(face empty tab trailing))
  994. )
  995. #+END_SRC
  996. Disable Eldoc, it interferes with flycheck
  997. #+BEGIN_SRC emacs-lisp
  998. (use-package eldoc
  999. :ensure nil
  1000. :config
  1001. (global-eldoc-mode -1)
  1002. )
  1003. #+END_SRC
  1004. Colorize colors as text with their value
  1005. #+BEGIN_SRC emacs-lisp
  1006. (use-package rainbow-mode
  1007. :ensure t
  1008. :init
  1009. (add-hook 'prog-mode-hook 'rainbow-mode t)
  1010. :diminish rainbow-mode
  1011. ;; :hook prog-mode ;; not working in use-package 2.3
  1012. :config
  1013. (setq-default rainbow-x-colors-major-mode-list '())
  1014. )
  1015. #+END_SRC
  1016. Highlight parens etc. for improved readability
  1017. #+BEGIN_SRC emacs-lisp
  1018. (use-package rainbow-delimiters
  1019. :ensure t
  1020. :config
  1021. (add-hook 'prog-mode-hook 'rainbow-delimiters-mode)
  1022. )
  1023. #+END_SRC
  1024. Treat CamelCase combined words as individual words
  1025. #+BEGIN_SRC emacs-lisp
  1026. (use-package subword
  1027. :diminish subword-mode
  1028. :config
  1029. (add-hook 'python-mode-hook 'subword-mode))
  1030. #+END_SRC
  1031. ** Smartparens
  1032. Smartparens is a beast on its own, so it's worth having a dedicated section for it
  1033. #+BEGIN_SRC emacs-lisp
  1034. (use-package smartparens
  1035. :ensure t
  1036. :diminish smartparens-mode
  1037. :config
  1038. (add-hook 'prog-mode-hook 'smartparens-mode)
  1039. )
  1040. #+END_SRC
  1041. ** Git
  1042. *** Magit
  1043. [[https://magit.vc/manual/magit/index.html][Link]]
  1044. I want to do git stuff here, not in a separate terminal window
  1045. Little crashcourse in magit:
  1046. - magit-init to init a git project
  1047. - magit-status (C-x g) to call the status window
  1048. in status buffer:
  1049. - s stage files
  1050. - u unstage files
  1051. - U unstage all files
  1052. - a apply changed to staging
  1053. - c c commit (type commit message, then C-c C-c to commit)
  1054. - b b switch to another branch
  1055. - P u git push
  1056. - F u git pull
  1057. #+BEGIN_SRC emacs-lisp
  1058. (use-package magit
  1059. :ensure t
  1060. :defer t
  1061. :init
  1062. ;; set git-path in work environment
  1063. (if (string-equal user-login-name "POH")
  1064. (setq magit-git-executable "P:/Eigene Dateien/Tools/Git/bin/git.exe")
  1065. )
  1066. :defer t
  1067. :bind (("C-x g" . magit-status))
  1068. )
  1069. #+END_SRC
  1070. *** Git-gutter
  1071. Display line changes in gutter based on git history. Enable it everywhere
  1072. [[https://github.com/syohex/emacs-git-gutter][Source]]
  1073. #+BEGIN_SRC emacs-lisp
  1074. (use-package git-gutter
  1075. :ensure t
  1076. :defer t
  1077. :config
  1078. (global-git-gutter-mode t)
  1079. :diminish git-gutter-mode
  1080. )
  1081. #+END_SRC
  1082. Some persistent navigation in git-gutter is nice, so here's a hydra for it:
  1083. #+BEGIN_SRC emacs-lisp
  1084. (defhydra hydra-git-gutter (:body-pre (git-gutter-mode 1)
  1085. :hint nil)
  1086. "
  1087. ^Git Gutter^ ^Git^ ^misc^
  1088. ^──────────^────────^───^────────────────^────^──────────────────────────
  1089. _j_: next hunk _s_tage hunk _q_uit
  1090. _k_: previous hunk _r_evert hunk _g_ : call magit-status
  1091. _h_: first hunk _p_opup hunk
  1092. _l_: last hunk set start _R_evision
  1093. ^^ ^^ ^^
  1094. "
  1095. ("j" git-gutter:next-hunk)
  1096. ("k" git-gutter:previous-hunk)
  1097. ("h" (progn (goto-char (point-min))
  1098. (git-gutter:next-hunk 1)))
  1099. ("l" (progn (goto-char (point-min))
  1100. (git-gutter:previous-hunk 1)))
  1101. ("s" git-gutter:stage-hunk)
  1102. ("r" git-gutter:revert-hunk)
  1103. ("p" git-gutter:popup-hunk)
  1104. ("R" git-gutter:set-start-revision)
  1105. ("q" nil :color blue)
  1106. ("g" magit-status)
  1107. )
  1108. #+END_SRC
  1109. *** Git-timemachine
  1110. Time machine lets me step through the history of a file as recorded in git.
  1111. [[https://github.com/pidu/git-timemachine][Source]]
  1112. #+BEGIN_SRC emacs-lisp
  1113. (use-package git-timemachine
  1114. :ensure t
  1115. :defer t
  1116. )
  1117. #+END_SRC
  1118. ** Company backend hooks
  1119. Backend configuration for python-mode
  1120. Common backends are:
  1121. - company-files: files & directory
  1122. - company-keywords: keywords
  1123. - company-capf: ??
  1124. - company-abbrev: ??
  1125. - company-dabbrev: dynamic abbreviations
  1126. - company-ispell: ??
  1127. So far I cannot differenciate a true python mode and a source block of ipython in org-mode, so the python-mode-hook should include both completion backends.
  1128. #+BEGIN_SRC emacs-lisp
  1129. (defun company/python-mode-hook()
  1130. (message "company/python-mode-hook activated")
  1131. (set (make-local-variable 'company-backends)
  1132. '(company-jedi))
  1133. ; '((company-jedi company-yasnippet company-dabbrev-code) company-dabbrev))
  1134. ; '((company-ob-ipython company-lsp)))
  1135. ; '((company-ob-ipython company-jedi)))
  1136. ; '((company-jedi company-dabbrev-code company-yasnippet) company-capf company-files))
  1137. ; '((company-lsp company-yasnippet) company-capf company-dabbrev company-files))
  1138. (company-mode t)
  1139. )
  1140. #+END_SRC
  1141. I have yet to find the proper hook to call this.
  1142. #+BEGIN_SRC emacs-lisp
  1143. (defun company/ipython-mode-hook()
  1144. (message "company/ipython-mode-hook activated")
  1145. (set (make-local-variable 'company-backends)
  1146. '((company-ob-ipython)))
  1147. (company-mode t)
  1148. )
  1149. #+END_SRC
  1150. #+BEGIN_SRC emacs-lisp
  1151. (defun company/ess-mode-hook()
  1152. (message "company/ess-mode-hook activated")
  1153. ; (set (make-local-variable 'company-backends)
  1154. ; '((company-ess-backend company-R-args company-R-objects)))
  1155. (ess-indent-with-fancy-comments nil)
  1156. (company-mode t))
  1157. #+END_SRC
  1158. (defun add-pcomplete-to-capf ()
  1159. (add-hook 'completion-at-point-functions 'pcomplete-completions-at-point nil t))
  1160. ;; (add-hook 'completion-at-point-functions 'pcomplete-completions-at-point nil t)
  1161. (add-hook 'org-mode-hook #'add-pcomplete-to-capf)
  1162. Backend for Orgmode
  1163. #+BEGIN_SRC emacs-lisp
  1164. (defun company/org-mode-hook()
  1165. (set (make-local-variable 'company-backends)
  1166. '(company-capf company-files))
  1167. (add-hook 'completion-at-point-functions 'pcomplete-completions-at-point nil t)
  1168. (message "company/org-mode-hook")
  1169. (company-mode t)
  1170. )
  1171. #+END_SRC
  1172. Backend configuration for lisp-mode
  1173. #+BEGIN_SRC emacs-lisp
  1174. (defun company/elisp-mode-hook()
  1175. (set (make-local-variable 'company-backends)
  1176. '((company-elisp company-dabbrev) company-capf company-files))
  1177. (company-mode t)
  1178. )
  1179. #+END_SRC
  1180. Backend configuration for beancount
  1181. #+BEGIN_SRC emacs-lisp
  1182. (defun company/beancount-mode-hook()
  1183. (set (make-local-variable 'company-backends)
  1184. '(company-beancount))
  1185. ; '((company-beancount company-dabbrev) company-capf company-files))
  1186. (company-mode t)
  1187. )
  1188. #+END_SRC
  1189. ** Misc Company packages
  1190. Addon to sort suggestions by usage
  1191. #+BEGIN_SRC emacs-lisp
  1192. (use-package company-statistics
  1193. :ensure t
  1194. :after company
  1195. :init
  1196. (setq company-statistics-file (concat PATH_USER_LOCAL "company-statistics-cache.el"));~/.emacs.d/user-dir/company-statistics-cache.el")
  1197. :config
  1198. (company-statistics-mode 1)
  1199. )
  1200. #+END_SRC
  1201. Get a popup with documentation of the completion candidate.
  1202. For the popups the package pos-tip.el is used and automatically installed.
  1203. [[https://github.com/expez/company-quickhelp][Company Quickhelp]]
  1204. [[https://www.emacswiki.org/emacs/PosTip][See here for Pos-Tip details]]
  1205. #+BEGIN_SRC emacs-lisp
  1206. (use-package company-quickhelp
  1207. :ensure t
  1208. :after company
  1209. :config
  1210. (company-quickhelp-mode 1)
  1211. )
  1212. #+END_SRC
  1213. Maybe add [[https://github.com/hlissner/emacs-company-dict][company-dict]]? It's a dictionary based on major modes, plus it has Yasnippet integration.
  1214. ** Flycheck
  1215. Show errors right away!
  1216. #+BEGIN_SRC emacs-lisp
  1217. (use-package flycheck
  1218. :ensure t
  1219. :diminish flycheck-mode " ✓"
  1220. :init
  1221. (setq flycheck-emacs-lisp-load-path 'inherit)
  1222. (add-hook 'after-init-hook #'global-flycheck-mode)
  1223. ; (add-hook 'python-mode-hook (lambda ()
  1224. ; (semantic-mode 1)
  1225. ; (flycheck-select-checker 'python-pylint)))
  1226. )
  1227. #+END_SRC
  1228. ** Projectile
  1229. Brings search functions on project level
  1230. #+BEGIN_SRC emacs-lisp
  1231. (use-package projectile
  1232. :ensure t
  1233. :defer t
  1234. :bind
  1235. (("C-c p p" . projectile-switch-project)
  1236. ("C-c p s s" . projectile-ag))
  1237. :init
  1238. (setq-default
  1239. projectile-cache-file (concat PATH_USER_LOCAL ".projectile-cache")
  1240. projectile-known-projects-file (concat PATH_USER_LOCAL ".projectile-bookmarks"))
  1241. :config
  1242. (projectile-mode t)
  1243. (setq-default
  1244. projectile-completion-system 'ivy
  1245. projectile-enable-caching t
  1246. projectile-mode-line '(:eval (projectile-project-name)))
  1247. )
  1248. #+END_SRC
  1249. ** Yasnippet
  1250. Snippets!
  1251. TODO: yas-minor-mode? what's that?
  1252. #+BEGIN_SRC emacs-lisp
  1253. (use-package yasnippet
  1254. :ensure t
  1255. :defer t
  1256. :diminish yas-minor-mode
  1257. :init
  1258. (yas-global-mode t)
  1259. (setq yas-snippet-dirs (concat PATH_USER_GLOBAL "snippets"))
  1260. :mode ("\\.yasnippet" . snippet-mode)
  1261. :config
  1262. (unless (string-equal my/whoami "work_remote") ; very hacky, but yas-reload-all throws a wrongp error at work
  1263. (yas-reload-all)) ;; ensure snippets are updated and available, necessary when not using global-mode
  1264. )
  1265. #+END_SRC
  1266. #+RESULTS:
  1267. ** Lisp
  1268. Not sure about this one, but dynamic binding gets some bad vibes.
  1269. #+BEGIN_SRC emacs-lisp
  1270. (setq lexical-binding t)
  1271. #+END_SRC
  1272. #+BEGIN_SRC emacs-lisp
  1273. (add-hook 'emacs-lisp-mode-hook 'company/elisp-mode-hook)
  1274. #+END_SRC
  1275. Add some helpers to handle and understand macros
  1276. #+BEGIN_SRC emacs-lisp
  1277. (use-package macrostep
  1278. :ensure t
  1279. :defer t
  1280. :init
  1281. (define-key emacs-lisp-mode-map (kbd "C-c e") 'macrostep-expand)
  1282. (define-key emacs-lisp-mode-map (kbd "C-c c") 'macrostep-collapse))
  1283. #+END_SRC
  1284. ** R
  1285. TODO: test it
  1286. For now only enable ESS at home. Not sure if it is useful at work.
  1287. Also I got "locate-file" errors at work; the debugger listet random files not related to anything within the emacs configuraion
  1288. #+BEGIN_SRC emacs-lisp
  1289. (pcase my/whoami
  1290. ("home"
  1291. (use-package ess-r-mode ;ess-site lädt alle Sprachen (Julia, Stata, S, S+, SAS, BUGS/JAGS
  1292. :ensure ess
  1293. ; :load-path "/usr/share/emacs/site-lisp/ess/"
  1294. ; :load-path "/home/marc/.emacs.d/elpa/ess-20180825.900/"
  1295. :defer t
  1296. :mode (("\\.R$" . R-mode)
  1297. ("\\.r$" . R-mode))
  1298. ; :init (require 'ess-site)
  1299. :commands
  1300. (R-mode)
  1301. :config
  1302. (use-package ess-R-data-view :ensure t)
  1303. ; (use-package ess-smart-equals :ensure t) ; requires julia-mode
  1304. (use-package ess-smart-underscore :ensure t)
  1305. ; (use-package ess-view :ensure t) ; requires julia-mode
  1306. (setq ess-use-flymake nil
  1307. ess-use-ido nil ;;else ESS will use ido whenever possible
  1308. ess-eval-visibly 'nowait
  1309. ess-ask-for-ess-directory nil
  1310. ess-local-process-name "R"
  1311. ess-use-tracebug t
  1312. ess-indent-with-fancy-comments nil ; otherwise ess indents comments sometimes
  1313. ess-describe-at-point-method 'tooltip))) ; 'tooltip or nil (buffer)
  1314. )
  1315. #+END_SRC
  1316. ** Lua
  1317. #+BEGIN_SRC emacs-lisp
  1318. (use-package lua-mode
  1319. :defer t
  1320. :ensure t
  1321. :mode "\\.lua\\'"
  1322. :config
  1323. (setq lua-indent-level 2))
  1324. #+END_SRC
  1325. ** Python
  1326. *** Intro
  1327. Systemwide following packages need to be installed:
  1328. - venv
  1329. - pylint / pylint3 (depending on default python version)
  1330. flycheck complains if no pylint is available and org tries to fontify python code natively.
  1331. The virtual environments need to have following modules installed:
  1332. - wheel (for some reason it isn't pulled by other packages, yet they complain about missing wheel)
  1333. - jedi
  1334. - epc
  1335. - pylint
  1336. *** Python-Mode
  1337. Automatically start python-mode when opening a .py-file.
  1338. Not sure if python.el is better than python-mode.el.
  1339. See [[https://github.com/jorgenschaefer/elpy/issues/887][here]] for info about ~python-shell-completion-native-enable~.
  1340. 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]].
  1341. Also limit the completion backends to those which make sense in Python.
  1342. #+BEGIN_SRC emacs-lisp
  1343. (use-package python
  1344. :mode ("\\.py\\'" . python-mode)
  1345. :interpreter ("python" . python-mode)
  1346. :defer t
  1347. :after company-mode
  1348. :init
  1349. (message "python mode init")
  1350. (add-hook 'python-mode-hook (lambda ()
  1351. (company/python-mode-hook)
  1352. (semantic-mode t)
  1353. (flycheck-select-checker 'python-pylint)))
  1354. :config
  1355. (setq python-shell-completion-native-enable nil)
  1356. )
  1357. #+END_SRC
  1358. *** IPython-Mode
  1359. Not sure if this configuraton will interfere with Python-Mode
  1360. #+BEGIN_SRC emacs-lisp
  1361. (use-package ob-ipython
  1362. :ensure t
  1363. :defer t
  1364. :init
  1365. (add-hook 'ob-ipython-mode-hook (lambda ()
  1366. 'company/ipython-mode-hook
  1367. (semantic-mode t)
  1368. (flycheck-select-checker 'pylint))))
  1369. #+END_SRC
  1370. *** Jedi / Company
  1371. Jedi is a backend for python autocompletion and needs to be installed on the server:
  1372. - pip install jedi
  1373. Code checks need to be installed, too:
  1374. - pip install flake8
  1375. If jedi doesn't work, it might be a problem with jediepcserver.py.
  1376. See [[https://github.com/tkf/emacs-jedi/issues/293][here]]
  1377. To fix it:
  1378. - Figure out which jediepcserver is running (first guess is melpa/jedi-core../jediepcserver.py
  1379. - Change some code:
  1380. #+BEGIN_SRC python
  1381. 100 return dict(
  1382. 101 # p.get_code(False) should do the job. But jedi-vim use replace.
  1383. 102 # So follow what jedi.vim does...
  1384. 103 - params=[p.get_code().replace('\n', '') for p in call_def.params],
  1385. 103 + params=[p.name for p in call_def.params],
  1386. 104 index=call-def.index,
  1387. 105 - call_name=call_def.call_name,
  1388. 105 + call_name=call_def.name,
  1389. 106 )
  1390. #+END_SRC
  1391. #+BEGIN_SRC emacs-lisp
  1392. (use-package company-jedi
  1393. :defer t
  1394. ;; :after company
  1395. :ensure t
  1396. :config
  1397. (setq jedi:environment-virtualenv (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  1398. (setq jedi:python-environment-directory (list (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/")))
  1399. (add-hook 'python-mode-hook 'jedi:setup)
  1400. (setq jedi:complete-on-dot t)
  1401. (setq jedi:use-shortcuts t)
  1402. ;; (add-hook 'python-mode-hook 'company/python-mode-hook)
  1403. )
  1404. #+END_SRC
  1405. *** Virtual Environments
  1406. A wrapper to handle virtual environments.
  1407. I strongly recommend to install virtual environments on the terminal, not through this wrapper, but changing venvs is fine.
  1408. TODO: automatically start an inferior python process or switch to it if already created
  1409. #+BEGIN_SRC emacs-lisp
  1410. (use-package pyvenv
  1411. :ensure t
  1412. :defer t
  1413. :init
  1414. (setenv "WORKON_HOME" (expand-file-name "~/Archiv/Programmierprojekte/Python/virtualenv/"))
  1415. :config
  1416. (pyvenv-mode t)
  1417. (defun my/pyvenv-post-activate-hook()
  1418. ; (setq jedi:environment-root pyvenv-virtual-env)
  1419. ; (setq jedi:environment-virtualenv pyvenv-virtual-env)
  1420. ; (setq jedi:tooltip-method '(nil)) ;; variants: nil or pos-tip and/or popup
  1421. (setq python-shell-virtualenv-root pyvenv-virtual-env)
  1422. ;; default traceback, other option M-x jedi:toggle-log-traceback
  1423. ;; traceback is in jedi:pop-to-epc-buffer
  1424. ; (jedi:setup)
  1425. ;; (company/python-mode-hook)
  1426. ; (setq jedi:server-args '("--log-traceback"))
  1427. (message "pyvenv-post-activate-hook activated"))
  1428. (add-hook 'pyvenv-post-activate-hooks 'my/pyvenv-post-activate-hook)
  1429. )
  1430. #+END_SRC
  1431. I want Emacs to automatically start the proper virtual environment.
  1432. Required is a .python-version file with, content in the first line being /path/to/virtualenv/
  1433. [[https://github.com/marcwebbie/auto-virtualenv][Github source]]
  1434. Depends on pyvenv
  1435. #+BEGIN_SRC emacs-lisp
  1436. (use-package auto-virtualenv
  1437. :ensure t
  1438. ;; :after pyvenv
  1439. :defer t
  1440. :init
  1441. (add-hook 'python-mode-hook 'auto-virtualenv-set-virtualenv)
  1442. ;; activate on changing buffers
  1443. ;; (add-hook 'window-configuration-change-hook 'auto-virtualenv-set-virtualenv)
  1444. ;; activate on focus in
  1445. ;; (add-hook 'focus-in-hook 'auto-virtualenv-set-virtualenv)
  1446. )
  1447. #+END_SRC
  1448. *** Visuals
  1449. Highlight indentations
  1450. #+BEGIN_SRC emacs-lisp
  1451. (use-package highlight-indentation
  1452. :ensure t
  1453. :init
  1454. (add-hook 'python-mode-hook 'highlight-indentation-current-column-mode)
  1455. (add-hook 'python-mode-hook 'highlight-indentation-mode)
  1456. :config
  1457. (set-face-background 'highlight-indentation-face "#454545")
  1458. (set-face-background 'highlight-indentation-current-column-face "#656565"))
  1459. #+END_SRC
  1460. *** Python language server (inactive)
  1461. On python side following needs to be installed:
  1462. - python-language-server[all]
  1463. First test for lsp-python.
  1464. Some intro: [[https://vxlabs.com/2018/06/08/python-language-server-with-emacs-and-lsp-mode/][Intro]]
  1465. Source python language server: [[https://github.com/palantir/python-language-server][Link]]
  1466. Source lsp-mode:
  1467. Source lsp-python: [[https://github.com/emacs-lsp/lsp-python][Link]]
  1468. Source company-lsp: [[https://github.com/tigersoldier/company-lsp][Link]]
  1469. Source lsp-ui: [[https://github.com/emacs-lsp/lsp-ui][Link]]
  1470. BEGIN_SRC emacs-lisp
  1471. (use-package lsp-mode
  1472. :ensure t
  1473. :config
  1474. ;; make sure we have lsp-imenu everywhere we have lsp
  1475. (require 'lsp-imenu)
  1476. (add-hook 'lsp-after-open-hook 'lsp-enable-imenu)
  1477. ;; get lsp-python-enable defined
  1478. ;; nb: use either projectile-project-root or ffip-get-project-root-directory
  1479. ;; or any other function that can be used to find the root dir of a project
  1480. (lsp-define-stdio-client lsp-python "python"
  1481. #'projectile-project-root
  1482. '("pyls"))
  1483. ;; make sure this is activated when python-mode is activated
  1484. ;; lsp-python-enable is created by macro above
  1485. (add-hook 'python-mode-hook
  1486. (lambda ()
  1487. (lsp-python-enable)
  1488. (company/python-mode-hook)))
  1489. ;; lsp extras
  1490. (use-package lsp-ui
  1491. :ensure t
  1492. :config
  1493. (setq lsp-ui-sideline-ignore-duplicate t)
  1494. (add-hook 'lsp-mode-hook 'lsp-ui-mode))
  1495. (use-package company-lsp
  1496. :ensure t)
  1497. ; :config
  1498. ; (push 'company-lsp company-backends))
  1499. ;; NB: only required if you prefer flake8 instead of the default
  1500. ;; send pyls config via lsp-after-initialize-hook -- harmless for
  1501. ;; other servers due to pyls key, but would prefer only sending this
  1502. ;; when pyls gets initialised (:initialize function in
  1503. ;; lsp-define-stdio-client is invoked too early (before server
  1504. ;; start))
  1505. (defun lsp-set-cfg ()
  1506. (let ((lsp-cfg `(:pyls (:configurationSources ("flake8")))))
  1507. ;; TODO: check lsp--cur-workspace here to decide per server / project
  1508. (lsp--set-configuration lsp-cfg)))
  1509. (add-hook 'lsp-after-initialize-hook 'lsp-set-cfg)
  1510. )
  1511. END_SRC
  1512. BEGIN_SRC emacs-lisp
  1513. (use-package lsp-mode
  1514. :ensure t
  1515. :defer t)
  1516. (add-hook 'lsp-mode-hook #'(lambda ()
  1517. (customize-set-variable 'lsp-enable-eldoc nil)
  1518. (flycheck-mode 1)
  1519. (company-mode 1)))
  1520. (use-package lsp-ui
  1521. :ensure t
  1522. :defer t)
  1523. (use-package company-lsp
  1524. :ensure t
  1525. :defer t)
  1526. (use-package lsp-python
  1527. :ensure t
  1528. :after lsp-mode
  1529. :defer t
  1530. :init
  1531. (add-hook 'python-mode-hook #'(lambda ()
  1532. (lsp-python-enable)
  1533. (flycheck-select-checker 'python-flake8))))
  1534. END_SRC
  1535. ** Latex
  1536. Requirements for Linux:
  1537. - Latex
  1538. - pdf-tools
  1539. The midnight mode hook is disabled for now, because CVs with my pic just look weird in this mode.
  1540. #+BEGIN_SRC emacs-lisp
  1541. (unless (string-equal my/whoami "work_remote")
  1542. (use-package pdf-tools
  1543. :ensure t
  1544. :defer t
  1545. :mode (("\\.pdf\\'" . pdf-view-mode))
  1546. :init
  1547. ; (add-hook 'pdf-view-mode-hook 'pdf-view-midnight-minor-mode)
  1548. :config
  1549. (pdf-tools-install)
  1550. (setq pdf-view-resize-factor 1.1 ;; more finegraned zoom
  1551. pdf-view-midnight-colors '("#c6c6c6" . "#363636")
  1552. TeX-view-program-selection '((output-pdf "pdf-tools"))
  1553. TeX-view-program-list '(("pdf-tools" "Tex-pdf-tools-sync-view")))
  1554. )
  1555. )
  1556. #+END_SRC
  1557. For latex-preview-pane a patch might be necessary (as of 2017-10), see the issue [[https://github.com/jsinglet/latex-preview-pane/issues/37][here]]
  1558. Update 2018-03: It seems to work without this patch. I will keep it here in case something breaks again.
  1559. #+BEGIN_SRC
  1560. latex-preview-pane-update-p()
  1561. --- (doc-view-revert-buffer nil t)
  1562. +++ (revert-buffer-nil t 'preserve-modes)
  1563. #+END_SRC
  1564. After that M-x byte-compile-file
  1565. #+BEGIN_SRC emacs-lisp
  1566. (use-package latex-preview-pane
  1567. :ensure t
  1568. :defer t
  1569. :init
  1570. ;; one of these works
  1571. (add-hook 'LaTeX-mode-hook 'latex-preview-pane-mode)
  1572. (add-hook 'latex-mode-hook 'latex-preview-pane-mode)
  1573. (setq auto-mode-alist
  1574. (append '(("\\.tex$" . latex-mode)) auto-mode-alist))
  1575. )
  1576. ;; necessary, because linum-mode isn't compatible and prints errors
  1577. (add-hook 'pdf-view-mode-hook (lambda () (linum-mode -1)))
  1578. #+END_SRC
  1579. ** Markdown
  1580. Major mode to edit markdown files.
  1581. For previews it needs markdown installed on the system.
  1582. For debian:
  1583. #+BEGIN_EXAMPLE
  1584. sudo apt install markdown
  1585. #+END_EXAMPLE
  1586. #+BEGIN_SRC emacs-lisp
  1587. (use-package markdown-mode
  1588. :ensure t
  1589. :defer t)
  1590. #+END_SRC
  1591. ** Config languages
  1592. #+BEGIN_SRC emacs-lisp
  1593. (use-package nginx-mode
  1594. :ensure t
  1595. :defer t)
  1596. #+END_SRC
  1597. ** Hydra Flycheck
  1598. Flycheck is necessary, obviously
  1599. #+BEGIN_SRC emacs-lisp
  1600. (defhydra hydra-flycheck (:color blue)
  1601. "
  1602. ^
  1603. ^Flycheck^ ^Errors^ ^Checker^
  1604. ^────────^──────────^──────^────────────^───────^───────────
  1605. _q_ quit _<_ previous _?_ describe
  1606. _m_ manual _>_ next _d_ disable
  1607. _v_ verify setup _f_ check _s_ select
  1608. ^^ ^^ ^^
  1609. "
  1610. ("q" nil)
  1611. ("<" flycheck-previous-error :color red)
  1612. (">" flycheck-next-error :color red)
  1613. ("?" flycheck-describe-checker)
  1614. ("d" flycheck-disable-checker)
  1615. ("f" flycheck-buffer)
  1616. ("m" flycheck-manual)
  1617. ("s" flycheck-select-checker)
  1618. ("v" flycheck-verify-setup)
  1619. )
  1620. #+END_SRC
  1621. * Orchestrate the configuration
  1622. Some settings should be set for all systems, some need to be specific (like my job-emacs doesn't need development tools).
  1623. ** Common
  1624. ** Home
  1625. ** Work
  1626. I mainly only use org
  1627. ** Work, Hyper-V
  1628. For testing purproses I keep a working emacs in a debian on hyper-v. The demands here are different to the other work-emacs
  1629. * Finishing
  1630. Stuff which I want to run in the end
  1631. #+BEGIN_SRC emacs-lisp
  1632. (message (emacs-init-time))
  1633. #+END_SRC