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.

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