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.

552 lines
18 KiB

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