Personal emacs config
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.

522 lines
16 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. * Editing
  2. ** IEdit mode
  3. #+BEGIN_SRC emacs-lisp
  4. (use-package iedit
  5. :bind ("C-;" . iedit-mode))
  6. #+END_SRC
  7. ** Spellcheck
  8. #+BEGIN_SRC emacs-lisp
  9. (global-set-key (kbd "C-!") 'ispell-buffer)
  10. #+END_SRC
  11. ** Undo tree
  12. #+BEGIN_SRC emacs-lisp
  13. (use-package undo-tree
  14. :config
  15. (global-undo-tree-mode))
  16. #+END_SRC
  17. * Added functionality
  18. ** Multiline sexp with symbol
  19. Jump to symbol, go up list, lispy-multiline. Great for diff-friendly custom
  20. #+BEGIN_SRC emacs-lisp
  21. (require 'isearch)
  22. (require 'lispy)
  23. (defun rlbr/multiline-sexp-with-symbol (symbol-name)
  24. (save-excursion
  25. (beginning-of-buffer)
  26. (search-forward-regexp (isearch-symbol-regexp symbol-name))
  27. (backward-up-list)
  28. (lispy-alt-multiline)))
  29. #+END_SRC
  30. ** Output matches
  31. Run command for each matching exe and see if output-p is true when fed the command output
  32. #+BEGIN_SRC emacs-lisp
  33. (defun rlbr/output-matches (output-matches-p exe args)
  34. "locate the executable whose output satisfies output-matches-p when fed args and return the full-path"
  35. (let ((exec-path exec-path)
  36. (output)
  37. (bad)
  38. (command-output)
  39. (current-exe)
  40. (failed))
  41. (while (not (or output failed))
  42. (setq current-exe
  43. (executable-find exe))
  44. (if current-exe
  45. (progn
  46. (setq command-output
  47. (shell-command-to-string (format "%s %s" (rlbr/quote-exe current-exe) args)))
  48. (if (funcall output-matches-p command-output)
  49. (setq output current-exe)
  50. (progn
  51. (setq bad
  52. (replace-regexp-in-string "/$" "" (file-name-directory current-exe)))
  53. (setq exec-path
  54. (seq-filter (lambda (item) (not (rlbr/case-insensitive-match item bad))) exec-path)))))
  55. (setq failed t)))
  56. output))
  57. #+END_SRC
  58. ** Save buffer-output to file
  59. This handy function is a customized ripoff of custom-save-all
  60. #+BEGIN_SRC emacs-lisp
  61. (defun rlbr/save-buffer-func-to-file (visit-file func args)
  62. "Rip off of custom-save-all"
  63. (let* ((filename visit-file)
  64. (recentf-exclude (if recentf-mode
  65. (append
  66. `(,(concat "\\`" (regexp-quote (recentf-expand-file-name visit-file)) "\\'")
  67. ,(concat "\\`" (regexp-quote (file-truename (recentf-expand-file-name visit-file))) "\\'"))
  68. recentf-exclude)))
  69. (old-buffer (find-buffer-visiting filename))
  70. old-buffer-name)
  71. (with-current-buffer
  72. (let ((find-file-visit-truename t))
  73. (or old-buffer
  74. (let ((delay-mode-hooks t))
  75. (find-file-noselect filename))))
  76. (when old-buffer
  77. (setq old-buffer-name
  78. (buffer-file-name))
  79. (set-visited-file-name
  80. (file-chase-links filename)))
  81. (unless (eq major-mode
  82. 'emacs-lisp-mode)
  83. (delay-mode-hooks
  84. (emacs-lisp-mode)))
  85. (let ((inhibit-read-only t)
  86. (print-length nil)
  87. (print-level nil))
  88. (apply func args))
  89. (let ((file-precious-flag t))
  90. (save-buffer))
  91. (if old-buffer
  92. (progn
  93. (set-visited-file-name
  94. old-buffer-name)
  95. (set-buffer-modified-p nil))
  96. (kill-buffer (current-buffer))))))
  97. #+END_SRC
  98. * Save/load
  99. ** Backup/auto-save
  100. #+BEGIN_SRC emacs-lisp
  101. (let ((backup-dir "~/.emacs.d/backup")
  102. (auto-save-dir "~/.emacs.d/autosave"))
  103. (if (not (file-directory-p backup-dir))
  104. (make-directory backup-dir))
  105. (if (not (file-directory-p
  106. auto-save-dir))
  107. (make-directory auto-save-dir)))
  108. #+END_SRC
  109. ** On save
  110. #+BEGIN_SRC emacs-lisp
  111. (add-hook 'before-save-hook 'delete-trailing-whitespace)
  112. #+END_SRC
  113. ** Recent files mode
  114. #+BEGIN_SRC emacs-lisp
  115. (use-package recentf
  116. :config
  117. (recentf-mode 1))
  118. #+END_SRC
  119. * Platform dependent
  120. ** Windows
  121. #+BEGIN_SRC emacs-lisp
  122. (when (string-equal system-type "windows-nt")
  123. (progn
  124. (defun rlbr/quote-exe (path)
  125. (w32-short-file-name path))
  126. (defun rlbr/start-external-shell ()
  127. (interactive)
  128. (start-process-shell-command (format "cmd(%s)" default-directory) nil "start default.bat"))
  129. (global-set-key (kbd "C-S-C") 'rlbr/start-external-shell)
  130. (defun rlbr/start-windows-explorer-here ()
  131. (interactive)
  132. (start-process-shell-command "explorer" nil (format "explorer %s" (replace-regexp-in-string "/" (regexp-quote "\\") (expand-file-name default-directory)))))
  133. (global-set-key (kbd "C-S-E") 'rlbr/start-windows-explorer-here)
  134. (defun rlbr/case-insensitive-match (string1 string2)
  135. (apply 'string-equal (mapcar 'downcase (list string1 string2))))
  136. (let ((find)
  137. (grep)
  138. (ls))
  139. (progn
  140. (setq find
  141. (rlbr/output-matches
  142. (lambda (output) (string-equal ".\n" output))
  143. "find" "-maxdepth 0"))
  144. (if find
  145. (setq find-program (rlbr/quote-exe find)))
  146. (setq grep (rlbr/output-matches
  147. (lambda (output) (string-match "grep (\\w+ grep)" output))
  148. "grep" "-V"))
  149. (if grep
  150. (setq grep-program
  151. (rlbr/quote-exe grep)))
  152. (setq ls (rlbr/output-matches
  153. (lambda (output) (string-match "ls: .*'\\?/': No such file or directory" output))
  154. "ls" "?/"))
  155. (if ls
  156. (setq insert-directory-program (rlbr/quote-exe ls)))))))
  157. #+END_SRC
  158. * Tramp configuration
  159. ** Tramp append plist to connection properties
  160. #+BEGIN_SRC emacs-lisp
  161. (use-package kv
  162. :config
  163. (defun rlbr/add-config-to-tramp (matches-regexp config-plist)
  164. (let ((config-alist (kvplist->alist config-plist)))
  165. (dolist (pair config-alist)
  166. (let ((config (list
  167. matches-regexp
  168. (car pair)
  169. (cdr pair))))
  170. (add-to-list
  171. 'tramp-connection-properties
  172. config))))))
  173. #+END_SRC
  174. ** Android
  175. #+BEGIN_SRC emacs-lisp
  176. (use-package tramp
  177. :config
  178. (let ((android-config (let ((default-directory "/data/data/com.termux/files"))
  179. (list "tmpdir" (expand-file-name "home/temp/")
  180. "remote-shell" (expand-file-name "usr/bin/sh")
  181. "remote-process-environment" (append (list (concat "PREFIX=" default-directory "usr")) tramp-remote-process-environment)
  182. "remote-path" (append (mapcar 'expand-file-name '("home/.local/bin" "usr/bin" "usr/bin/applets")) '("/sbin" "/vendor/bin" "/system/sbin" "/system/bin" "/system/xbin"))))))
  183. (rlbr/add-config-to-tramp "/ssh:termux.*:" android-config)))
  184. #+END_SRC
  185. * Major modes
  186. ** Java
  187. *** Meghanada
  188. #+BEGIN_SRC emacs-lisp
  189. (use-package autodisass-java-bytecode
  190. :defer t)
  191. (use-package meghanada
  192. :defer t
  193. :init
  194. (add-hook 'java-mode-hook
  195. (lambda ()
  196. (meghanada-mode t)
  197. (add-hook 'before-save-hook 'meghanada-code-beautify-before-save)))
  198. :config
  199. (setq indent-tabs-mode nil)
  200. (setq meghanada-server-remote-debug t)
  201. (setq meghanada-javac-xlint "-Xlint:all,-processing")
  202. (defhydra hydra-meghanada (:hint nil :exit t)
  203. "
  204. ^Edit^ ^Tast or Task^
  205. ^^^^^^-------------------------------------------------------
  206. _f_: meghanada-compile-file _m_: meghanada-restart
  207. _c_: meghanada-compile-project _t_: meghanada-run-task
  208. _o_: meghanada-optimize-import _j_: meghanada-run-junit-test-case
  209. _s_: meghanada-switch-test-case _J_: meghanada-run-junit-class
  210. _v_: meghanada-local-variable _R_: meghanada-run-junit-recent
  211. _i_: meghanada-import-all _r_: meghanada-reference
  212. _g_: magit-status _T_: meghanada-typeinfo
  213. _q_: exit
  214. "
  215. ("f" meghanada-compile-file)
  216. ("m" meghanada-restart)
  217. ("c" meghanada-compile-project)
  218. ("o" meghanada-optimize-import)
  219. ("s" meghanada-switch-test-case)
  220. ("v" meghanada-local-variable)
  221. ("i" meghanada-import-all)
  222. ("g" magit-status)
  223. ("t" meghanada-run-task)
  224. ("T" meghanada-typeinfo)
  225. ("j" meghanada-run-junit-test-case)
  226. ("J" meghanada-run-junit-class)
  227. ("R" meghanada-run-junit-recent)
  228. ("r" meghanada-reference)
  229. ("q" exit)
  230. ("z" nil "leave"))
  231. :bind
  232. (:map meghanada-mode-map
  233. ("C-S-t" . meghanada-switch-testcase)
  234. ("M-RET" . meghanada-local-variable)
  235. ("M-r" . meghanada-reference)
  236. ("M-t" . meghanada-typeinfo)
  237. ("C-z" . hydra-meghanada/body))
  238. :commands
  239. (meghanada-mode))
  240. #+END_SRC
  241. ** JavaScript
  242. #+BEGIN_SRC emacs-lisp
  243. (use-package js2-mode
  244. :mode "\\.js\\'"
  245. :hook ((js2-mode . js2-imenu-extras-mode)
  246. (js2-mode . (lambda () (add-hook 'xref-backend-functions #'xref-js2-xref-backend nil t))))
  247. :config
  248. (use-package js2-refactor
  249. :hook (js2-mode . js2-refactor-mode)
  250. :bind
  251. (:map js2-mode-map
  252. ("C-k" . js2r-kill))
  253. :config
  254. (js2r-add-keybindings-with-prefix "C-c C-r"))
  255. (use-package xref-js2
  256. :demand t)
  257. (define-key js-mode-map (kbd "M-.") nil)
  258. (defun rlbr/jump-to-definition ()
  259. "Jump to a definition."
  260. (interactive)
  261. (condition-case-unless-debug nil
  262. (js2-jump-to-definition)
  263. (error
  264. (progn
  265. (ignore-errors
  266. (xref-pop-marker-stack))
  267. (xref-find-definitions (xref-backend-identifier-at-point (xref-find-backend)))))))
  268. (define-key js-mode-map (kbd "M-.") #'rlbr/jump-to-definition))
  269. #+END_SRC
  270. ** Magit
  271. #+BEGIN_SRC emacs-lisp
  272. (use-package magit
  273. :bind (("C-x g" . magit-status))
  274. :config
  275. (use-package git-commit
  276. :hook (git-commit-setup . git-commit-turn-on-flyspell)))
  277. #+END_SRC
  278. ** Python
  279. *** Platform specific
  280. #+BEGIN_SRC emacs-lisp
  281. (setq elpy-rpc-python-command
  282. (cond
  283. ((string-equal system-type "gnu/linux")
  284. "python3")
  285. ((string-equal system-type "windows-nt")
  286. "pythonw.exe")))
  287. #+END_SRC
  288. *** custom feature
  289. #+BEGIN_SRC emacs-lisp
  290. (defun rlbr/split-venv-with-number (name-number)
  291. "Split a virtualenv name with either a ~ seperating the name and the number, or nothing"
  292. (let ((split-result (split-string name-number (regexp-quote "~")))
  293. (ret))
  294. (if (= 1 (length split-result))
  295. (progn
  296. (setq ret (car split-result))
  297. (push 0 ret))
  298. (progn
  299. (setq ret
  300. (string-join
  301. (butlast split-result)
  302. "~"))
  303. (push
  304. (string-to-number
  305. (car (last split-result)))
  306. ret)))
  307. ret))
  308. (defun rlbr/join-venv-with-number (number-name)
  309. "Join a list with a name and a number"
  310. (let
  311. ((number (car number-name))
  312. (name (cdr number-name)))
  313. (if (= number 0)
  314. name
  315. (string-join (list name (number-to-string number)) "~"))))
  316. (defun rlbr/get-venv-name (&optional library-root)
  317. "Generate venv name based off of the base-name of the library root"
  318. (file-name-base
  319. (directory-file-name
  320. (if library-root
  321. library-root
  322. (elpy-library-root)))))
  323. (defun rlbr/handle-name-conflicts (venv-name)
  324. "Deal with potential name conflicts in venv"
  325. (let ((venv-conflicts)
  326. (venv-partition-name))
  327. (setq venv-partition-name (rlbr/split-venv-with-number venv-name))
  328. (setq venv-conflicts
  329. (seq-filter
  330. (lambda (item) (string-equal (cdr item) venv-name))
  331. (mapcar #'rlbr/split-venv-with-number (pyvenv-virtualenv-list))))
  332. (when venv-conflicts
  333. (setcar venv-partition-name (1+ (apply 'max (mapcar #'car venv-conflicts)))))
  334. (rlbr/join-venv-with-number venv-partition-name)))
  335. (require 'vc)
  336. (defun rlbr/setup-python-venv-dirlocals (&optional library-root venv-name)
  337. "Setup .dir-locals file in library root and tell vc system to ignore .dir-locals file"
  338. (let* ((library-root (if library-root
  339. library-root
  340. (elpy-library-root)))
  341. (venv-name (if venv-name venv-name (rlbr/get-venv-name library-root)))
  342. (default-directory library-root)
  343. (dir-locals-path (expand-file-name
  344. ".dir-locals.el")))
  345. (rlbr/save-buffer-func-to-file dir-locals-path 'add-dir-local-variable
  346. `(python-mode pyvenv-workon ,venv-name))
  347. (let* ((vc-root (vc-find-root dir-locals-path ".git"))
  348. (vc-ignore-file (vc-call-backend 'Git 'find-ignore-file vc-root)))
  349. (if (apply 'string-equal (mapcar 'directory-file-name (mapcar 'file-truename (list vc-root library-root))))
  350. (progn
  351. (unless (file-exists-p vc-ignore-file)
  352. (with-temp-buffer
  353. (write-file vc-ignore-file)))
  354. (vc-ignore ".dir-locals.el"))
  355. (when (y-or-n-p (format "Ignore .dir-locals.el in repo '%s' ?" vc-root))
  356. (unless (file-exists-p vc-ignore-file)
  357. (with-temp-buffer
  358. (write-file vc-ignore-file)))
  359. (vc-ignore ".dir-locals.el"))))))
  360. (defun rlbr/init-python-venv-in-library-root (&optional library-root)
  361. "Prompt to either create one or use default"
  362. (let ((venv-name (rlbr/get-venv-name))
  363. (library-root (if library-root
  364. library-root
  365. (elpy-library-root))))
  366. (setq venv-name (rlbr/handle-name-conflicts venv-name))
  367. (if (y-or-n-p (format "Create venv '%s'?" venv-name))
  368. (pyvenv-create venv-name (read-file-name "Python interpreter to use: "
  369. (file-name-directory (executable-find "python"))
  370. nil nil "python"))
  371. (progn
  372. (unless (member "emacs-default-venv" (pyvenv-virtualenv-list))
  373. (pyvenv-create venv-name (read-file-name "Python interpreter to use: "
  374. (file-name-directory (executable-find "python"))
  375. nil nil "python")))
  376. (setq venv-name "emacs-default-venv")))
  377. (rlbr/setup-python-venv-dirlocals library-root venv-name)
  378. venv-name))
  379. (defun rlbr/init-venv ()
  380. (when (eq major-mode 'python-mode)
  381. (unless (or pyvenv-workon (apply 'string-prefix-p (mapcar 'file-truename (list (pyvenv-workon-home) buffer-file-name))))
  382. (setq-local pyvenv-workon (rlbr/init-python-venv-in-library-root)))))
  383. #+END_SRC
  384. *** bindings/settings
  385. #+BEGIN_SRC emacs-lisp
  386. (use-package python
  387. :hook ((python-mode . blacken-mode)
  388. (python-mode . pyvenv-mode)
  389. (hack-local-variables . rlbr/init-venv))
  390. :config
  391. (use-package elpy
  392. :bind (("C-=" . elpy-goto-assignment))
  393. :config (when (require 'flycheck nil t)
  394. (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))))
  395. (elpy-enable))
  396. #+END_SRC
  397. ** SSH config mode
  398. #+BEGIN_SRC emacs-lisp
  399. (use-package ssh-config-mode
  400. :mode "~/.ssh/config\\'")
  401. #+END_SRC
  402. ** Tramp
  403. ** Webmode
  404. #+BEGIN_SRC emacs-lisp
  405. (use-package web-mode
  406. :mode
  407. (("\\.phtml\\'" . web-mode)
  408. ("\\.tpl\\.php\\'" . web-mode)
  409. ("\\.[agj]sp\\'" . web-mode)
  410. ("\\.as[cp]x\\'" . web-mode)
  411. ("\\.erb\\'" . web-mode)
  412. ("\\.mustache\\'" . web-mode)
  413. ("\\.djhtml\\'" . web-mode)
  414. ("\\.html?\\'" . web-mode)))
  415. #+END_SRC
  416. ** YAML
  417. #+BEGIN_SRC emacs-lisp
  418. (use-package yaml-mode
  419. :mode "\\.yml\\'")
  420. #+END_SRC
  421. * Minor modes/misc
  422. ** Kill the things
  423. *** Buffer
  424. #+BEGIN_SRC emacs-lisp
  425. (global-set-key (kbd "C-x k") 'kill-this-buffer)
  426. #+END_SRC
  427. *** Emacs
  428. #+BEGIN_SRC emacs-lisp
  429. (global-set-key (kbd "C-x C-k C-x C-k") 'kill-emacs)
  430. #+END_SRC
  431. ** Lispy
  432. #+BEGIN_SRC emacs-lisp
  433. (use-package lispy
  434. :hook ((emacs-lisp-mode) . lispy-mode))
  435. #+END_SRC
  436. ** Custom custom
  437. #+BEGIN_SRC emacs-lisp
  438. (advice-add 'custom-save-faces :after (lambda () (rlbr/multiline-sexp-with-symbol "custom-set-faces")))
  439. (advice-add 'custom-save-variables :after (lambda () (rlbr/multiline-sexp-with-symbol "custom-set-variables")))
  440. #+END_SRC
  441. * Navigation/auto-completion
  442. ** Ace window
  443. #+BEGIN_SRC emacs-lisp
  444. (use-package ace-window
  445. :bind (("M-Q" . ace-window)))
  446. #+END_SRC
  447. ** Hippie expand
  448. #+BEGIN_SRC emacs-lisp
  449. (use-package hippie-exp
  450. :bind ("M-/" . hippie-expand))
  451. #+END_SRC
  452. ** IBuffer mode
  453. #+BEGIN_SRC emacs-lisp
  454. (use-package ibbufer-vc
  455. :hook ((ibuffer-mode . ibuffer-vc-set-filter-groups-by-vc-root)))
  456. (use-package ibuffer
  457. :bind (("C-x C-b" . ibuffer))
  458. :config
  459. (define-ibuffer-column size-h
  460. ;; Use human readable Size column instead of original one
  461. (:name "Size" :inline t)
  462. (cond ((> (buffer-size) 1000000)
  463. (format "%7.1fM" (/ (buffer-size) 1000000.0)))
  464. ((> (buffer-size) 100000)
  465. (format "%7.0fk" (/ (buffer-size) 1000.0)))
  466. ((> (buffer-size) 1000)
  467. (format "%7.1fk" (/ (buffer-size) 1000.0)))
  468. (t
  469. (format "%8d" (buffer-size))))))
  470. #+END_SRC
  471. ** Ivy
  472. #+BEGIN_SRC emacs-lisp
  473. (use-package ivy
  474. :config
  475. (use-package swiper
  476. :bind ("C-s" . swiper))
  477. (ivy-mode))
  478. #+END_SRC
  479. * Look and feel
  480. ** Line numbers
  481. #+BEGIN_SRC emacs-lisp
  482. (global-display-line-numbers-mode)
  483. #+END_SRC
  484. ** Mode line bell
  485. #+BEGIN_SRC emacs-lisp
  486. (use-package mode-line-bell
  487. :config
  488. (mode-line-bell-mode))
  489. #+END_SRC
  490. ** Spaceline
  491. #+BEGIN_SRC emacs-lisp
  492. (use-package spaceline-config
  493. :config
  494. (use-package winum
  495. :init
  496. (setq winum-keymap
  497. (let ((map (make-sparse-keymap)))
  498. (define-key map (kbd "M-0") 'winum-select-window-0-or-10)
  499. (define-key map (kbd "M-1") 'winum-select-window-1)
  500. (define-key map (kbd "M-2") 'winum-select-window-2)
  501. (define-key map (kbd "M-3") 'winum-select-window-3)
  502. (define-key map (kbd "M-4") 'winum-select-window-4)
  503. (define-key map (kbd "M-5") 'winum-select-window-5)
  504. (define-key map (kbd "M-6") 'winum-select-window-6)
  505. (define-key map (kbd "M-7") 'winum-select-window-7)
  506. (define-key map (kbd "M-8") 'winum-select-window-8)
  507. map)))
  508. (spaceline-spacemacs-theme)
  509. (winum-mode))
  510. #+END_SRC