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.

539 lines
17 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. (flycheck-mode +1)
  198. (add-hook 'before-save-hook 'meghanada-code-beautify-before-save)))
  199. :config
  200. (setq indent-tabs-mode nil)
  201. (setq meghanada-server-remote-debug t)
  202. (setq meghanada-javac-xlint "-Xlint:all,-processing")
  203. (defhydra hydra-meghanada (:hint nil :exit t)
  204. "
  205. ^Edit^ ^Tast or Task^
  206. ^^^^^^-------------------------------------------------------
  207. _f_: meghanada-compile-file _m_: meghanada-restart
  208. _c_: meghanada-compile-project _t_: meghanada-run-task
  209. _o_: meghanada-optimize-import _j_: meghanada-run-junit-test-case
  210. _s_: meghanada-switch-test-case _J_: meghanada-run-junit-class
  211. _v_: meghanada-local-variable _R_: meghanada-run-junit-recent
  212. _i_: meghanada-import-all _r_: meghanada-reference
  213. _g_: magit-status _T_: meghanada-typeinfo
  214. _q_: exit
  215. "
  216. ("f" meghanada-compile-file)
  217. ("m" meghanada-restart)
  218. ("c" meghanada-compile-project)
  219. ("o" meghanada-optimize-import)
  220. ("s" meghanada-switch-test-case)
  221. ("v" meghanada-local-variable)
  222. ("i" meghanada-import-all)
  223. ("g" magit-status)
  224. ("t" meghanada-run-task)
  225. ("T" meghanada-typeinfo)
  226. ("j" meghanada-run-junit-test-case)
  227. ("J" meghanada-run-junit-class)
  228. ("R" meghanada-run-junit-recent)
  229. ("r" meghanada-reference)
  230. ("q" exit)
  231. ("z" nil "leave"))
  232. :bind
  233. (:map meghanada-mode-map
  234. ("C-S-t" . meghanada-switch-testcase)
  235. ("M-RET" . meghanada-local-variable)
  236. ("M-r" . meghanada-reference)
  237. ("M-t" . meghanada-typeinfo)
  238. ("C-z" . hydra-meghanada/body))
  239. :commands
  240. (meghanada-mode))
  241. #+END_SRC
  242. ** JavaScript
  243. #+BEGIN_SRC emacs-lisp
  244. (use-package js2-mode
  245. :mode "\\.js\\'"
  246. :hook ((js2-mode . js2-imenu-extras-mode)
  247. (js2-mode . (lambda () (add-hook 'xref-backend-functions #'xref-js2-xref-backend nil t))))
  248. :config
  249. (use-package js2-refactor
  250. :hook (js2-mode . js2-refactor-mode)
  251. :bind
  252. (:map js2-mode-map
  253. ("C-k" . js2r-kill))
  254. :config
  255. (js2r-add-keybindings-with-prefix "C-c C-r"))
  256. (use-package xref-js2
  257. :demand t)
  258. (define-key js-mode-map (kbd "M-.") nil)
  259. (defun rlbr/jump-to-definition ()
  260. "Jump to a definition."
  261. (interactive)
  262. (condition-case-unless-debug nil
  263. (js2-jump-to-definition)
  264. (error
  265. (progn
  266. (ignore-errors
  267. (xref-pop-marker-stack))
  268. (xref-find-definitions (xref-backend-identifier-at-point (xref-find-backend)))))))
  269. (define-key js-mode-map (kbd "M-.") #'rlbr/jump-to-definition))
  270. #+END_SRC
  271. ** Magit
  272. #+BEGIN_SRC emacs-lisp
  273. (use-package magit
  274. :bind (("C-x g" . magit-status))
  275. :config
  276. (use-package git-commit
  277. :hook (git-commit-setup . git-commit-turn-on-flyspell)))
  278. #+END_SRC
  279. ** Python
  280. *** Platform specific
  281. #+BEGIN_SRC emacs-lisp
  282. (setq elpy-rpc-python-command
  283. (cond
  284. ((string-equal system-type "gnu/linux")
  285. "python3")
  286. ((string-equal system-type "windows-nt")
  287. "pythonw.exe")))
  288. #+END_SRC
  289. *** custom feature
  290. #+BEGIN_SRC emacs-lisp
  291. (defun rlbr/join-venv-with-number (number-name)
  292. "Join a list with a name and a number"
  293. (let
  294. ((number (car number-name))
  295. (name (cdr number-name)))
  296. (if (= number 0)
  297. name
  298. (string-join (list name (number-to-string number))
  299. "~"))))
  300. (defun rlbr/split-venv-with-number (name-number)
  301. "Split a virtualenv name with either a ~ seperating the name and the number, or nothing"
  302. (let ((split-result (split-string name-number (regexp-quote "~")))
  303. (ret))
  304. (if (= 1 (length split-result))
  305. (progn
  306. (setq ret (car split-result))
  307. (push 0 ret))
  308. (progn
  309. (setq ret
  310. (string-join
  311. (butlast split-result)
  312. "~"))
  313. (push
  314. (string-to-number
  315. (car (last split-result)))
  316. ret)))
  317. ret))
  318. (defun rlbr/get-venv-name (&optional library-root)
  319. "Generate venv name based off of the base-name of the library root"
  320. (file-name-base
  321. (directory-file-name
  322. (if library-root
  323. library-root
  324. (elpy-library-root)))))
  325. (defun rlbr/handle-name-conflicts (venv-name)
  326. "Deal with potential name conflicts in venv"
  327. (let ((venv-conflicts)
  328. (venv-partition-name))
  329. (setq venv-partition-name (rlbr/split-venv-with-number venv-name))
  330. (setq venv-conflicts
  331. (seq-filter
  332. (lambda (item)
  333. (string-equal (cdr item)
  334. venv-name))
  335. (mapcar #'rlbr/split-venv-with-number (pyvenv-virtualenv-list))))
  336. (when venv-conflicts
  337. (setcar venv-partition-name (1+ (apply 'max (mapcar #'car venv-conflicts)))))
  338. (rlbr/join-venv-with-number venv-partition-name)))
  339. (require 'vc)
  340. (defun rlbr/setup-python-venv-dirlocals (&optional library-root venv-name)
  341. "Setup .dir-locals file in library root and tell vc system to ignore .dir-locals file"
  342. (let* ((library-root (if library-root
  343. library-root
  344. (elpy-library-root)))
  345. (venv-name (if venv-name venv-name (rlbr/get-venv-name library-root)))
  346. (default-directory library-root)
  347. (dir-locals-path (expand-file-name
  348. ".dir-locals.el")))
  349. (rlbr/save-buffer-func-to-file dir-locals-path 'add-dir-local-variable
  350. `(python-mode pyvenv-workon ,venv-name))
  351. (let* ((vc-root (vc-find-root dir-locals-path ".git"))
  352. (vc-ignore-file (vc-call-backend 'Git 'find-ignore-file vc-root)))
  353. (if (apply 'string-equal (mapcar 'directory-file-name (mapcar 'file-truename (list vc-root library-root))))
  354. (progn
  355. (unless (file-exists-p vc-ignore-file)
  356. (with-temp-buffer
  357. (write-file vc-ignore-file)))
  358. (vc-ignore ".dir-locals.el"))
  359. (when (y-or-n-p (format "Ignore .dir-locals.el in repo '%s' ?" vc-root))
  360. (unless (file-exists-p vc-ignore-file)
  361. (with-temp-buffer
  362. (write-file vc-ignore-file)))
  363. (vc-ignore ".dir-locals.el"))))))
  364. (defun rlbr/get-python-executable ()
  365. (read-file-name "Python interpreter to use: " (file-name-directory (executable-find "python"))
  366. nil nil "python"))
  367. (defun rlbr/init-python-venv-in-library-root (&optional library-root)
  368. "Prompt to either create one or use default" (let ((venv-name (rlbr/get-venv-name))
  369. (library-root (if library-root library-root (elpy-library-root))))
  370. (setq venv-name (rlbr/handle-name-conflicts venv-name))
  371. (if (y-or-n-p (format "Create venv '%s'?" venv-name))
  372. (pyvenv-create venv-name (rlbr/get-python-executable))
  373. (progn (unless (member "emacs-default-venv" (pyvenv-virtualenv-list))
  374. (pyvenv-create venv-name (rlbr/get-python-executable)))
  375. (setq venv-name "emacs-default-venv")))
  376. (rlbr/setup-python-venv-dirlocals library-root venv-name)
  377. venv-name))
  378. (require 'dash)
  379. (defun rlbr/init-venv ()
  380. (when (eq major-mode 'python-mode)
  381. (cond ((file-remote-p buffer-file-name)
  382. (setq-local pyvenv-workon "emacs-default-venv"))
  383. ((let ((buffer-file-name (file-truename buffer-file-name)))
  384. (-any (lambda (file-prefix)
  385. (string-prefix-p file-prefix buffer-file-name))
  386. (mapcar 'file-truename (list (elpy-rpc-get-or-create-virtualenv)
  387. (pyvenv-workon-home)))))
  388. nil)
  389. (t (cond ((and pyvenv-workon (not (member pyvenv-workon (pyvenv-virtualenv-list))))
  390. (if (y-or-n-p (format "Venv '%s' is specified but does not exist. Create it?" pyvenv-workon))
  391. (progn (pyvenv-create pyvenv-workon (rlbr/get-python-executable))
  392. (pyvenv-workon pyvenv-workon))
  393. (rlbr/save-buffer-func-to-file (let ((default-directory (elpy-library-root)))
  394. (expand-file-name ".dir-locals.el"))
  395. 'add-dir-local-variable '(python-mode pyvenv-workon "emacs-default-venv"))
  396. (setq-local pyvenv-workon "emacs-default-venv")))
  397. ((not pyvenv-workon)
  398. (setq-local pyvenv-workon (rlbr/init-python-venv-in-library-root))))))
  399. (pyvenv-workon pyvenv-workon)))
  400. #+END_SRC
  401. *** bindings/settings
  402. #+BEGIN_SRC emacs-lisp
  403. (use-package python
  404. :hook ((python-mode . blacken-mode)
  405. (python-mode . pyvenv-mode)
  406. (hack-local-variables . rlbr/init-venv))
  407. :config
  408. (use-package elpy
  409. :bind (("C-=" . elpy-goto-assignment))
  410. :config (when (require 'flycheck nil t)
  411. (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))))
  412. (elpy-enable))
  413. #+END_SRC
  414. ** SSH config mode
  415. #+BEGIN_SRC emacs-lisp
  416. (use-package ssh-config-mode
  417. :mode "~/.ssh/config\\'")
  418. #+END_SRC
  419. ** Tramp
  420. ** Webmode
  421. #+BEGIN_SRC emacs-lisp
  422. (use-package web-mode
  423. :mode
  424. (("\\.phtml\\'" . web-mode)
  425. ("\\.tpl\\.php\\'" . web-mode)
  426. ("\\.[agj]sp\\'" . web-mode)
  427. ("\\.as[cp]x\\'" . web-mode)
  428. ("\\.erb\\'" . web-mode)
  429. ("\\.mustache\\'" . web-mode)
  430. ("\\.djhtml\\'" . web-mode)
  431. ("\\.html?\\'" . web-mode)))
  432. #+END_SRC
  433. ** YAML
  434. #+BEGIN_SRC emacs-lisp
  435. (use-package yaml-mode
  436. :mode "\\.yml\\'")
  437. #+END_SRC
  438. * Minor modes/misc
  439. ** Kill the things
  440. *** Buffer
  441. #+BEGIN_SRC emacs-lisp
  442. (global-set-key (kbd "C-x k") 'kill-this-buffer)
  443. #+END_SRC
  444. *** Emacs
  445. #+BEGIN_SRC emacs-lisp
  446. (global-set-key (kbd "C-x C-k C-x C-k") 'kill-emacs)
  447. #+END_SRC
  448. ** Lispy
  449. #+BEGIN_SRC emacs-lisp
  450. (use-package lispy
  451. :hook ((emacs-lisp-mode) . lispy-mode))
  452. #+END_SRC
  453. ** Custom custom
  454. #+BEGIN_SRC emacs-lisp
  455. (advice-add 'custom-save-faces :after (lambda () (rlbr/multiline-sexp-with-symbol "custom-set-faces")))
  456. (advice-add 'custom-save-variables :after (lambda () (rlbr/multiline-sexp-with-symbol "custom-set-variables")))
  457. #+END_SRC
  458. * Navigation/auto-completion
  459. ** Ace window
  460. #+BEGIN_SRC emacs-lisp
  461. (use-package ace-window
  462. :bind (("M-Q" . ace-window)))
  463. #+END_SRC
  464. ** Hippie expand
  465. #+BEGIN_SRC emacs-lisp
  466. (use-package hippie-exp
  467. :bind ("M-/" . hippie-expand))
  468. #+END_SRC
  469. ** IBuffer mode
  470. #+BEGIN_SRC emacs-lisp
  471. (use-package ibbufer-vc
  472. :hook ((ibuffer-mode . ibuffer-vc-set-filter-groups-by-vc-root)))
  473. (use-package ibuffer
  474. :bind (("C-x C-b" . ibuffer))
  475. :config
  476. (define-ibuffer-column size-h
  477. ;; Use human readable Size column instead of original one
  478. (:name "Size" :inline t)
  479. (cond ((> (buffer-size) 1000000)
  480. (format "%7.1fM" (/ (buffer-size) 1000000.0)))
  481. ((> (buffer-size) 100000)
  482. (format "%7.0fk" (/ (buffer-size) 1000.0)))
  483. ((> (buffer-size) 1000)
  484. (format "%7.1fk" (/ (buffer-size) 1000.0)))
  485. (t
  486. (format "%8d" (buffer-size))))))
  487. #+END_SRC
  488. ** Ivy
  489. #+BEGIN_SRC emacs-lisp
  490. (use-package ivy
  491. :config
  492. (use-package swiper
  493. :bind ("C-s" . swiper))
  494. (ivy-mode))
  495. #+END_SRC
  496. * Look and feel
  497. ** Line numbers
  498. #+BEGIN_SRC emacs-lisp
  499. (global-display-line-numbers-mode)
  500. #+END_SRC
  501. ** Mode line bell
  502. #+BEGIN_SRC emacs-lisp
  503. (use-package mode-line-bell
  504. :config
  505. (mode-line-bell-mode))
  506. #+END_SRC
  507. ** Spaceline
  508. #+BEGIN_SRC emacs-lisp
  509. (use-package spaceline-config
  510. :config
  511. (use-package winum
  512. :init
  513. (setq winum-keymap
  514. (let ((map (make-sparse-keymap)))
  515. (define-key map (kbd "M-0") 'winum-select-window-0-or-10)
  516. (define-key map (kbd "M-1") 'winum-select-window-1)
  517. (define-key map (kbd "M-2") 'winum-select-window-2)
  518. (define-key map (kbd "M-3") 'winum-select-window-3)
  519. (define-key map (kbd "M-4") 'winum-select-window-4)
  520. (define-key map (kbd "M-5") 'winum-select-window-5)
  521. (define-key map (kbd "M-6") 'winum-select-window-6)
  522. (define-key map (kbd "M-7") 'winum-select-window-7)
  523. (define-key map (kbd "M-8") 'winum-select-window-8)
  524. map)))
  525. (spaceline-spacemacs-theme)
  526. (winum-mode))
  527. #+END_SRC