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.

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