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.

340 lines
10 KiB

6 years ago
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. * Save/load
  18. ** Backup/auto-save
  19. #+BEGIN_SRC emacs-lisp
  20. (let ((backup-dir "~/.emacs.d/backup")
  21. (auto-save-dir "~/.emacs.d/autosave"))
  22. (if (not (file-directory-p backup-dir))
  23. (make-directory backup-dir))
  24. (if (not (file-directory-p
  25. auto-save-dir))
  26. (make-directory auto-save-dir)))
  27. #+END_SRC
  28. ** On save
  29. #+BEGIN_SRC emacs-lisp
  30. (add-hook 'before-save-hook 'delete-trailing-whitespace)
  31. #+END_SRC
  32. ** Recent files mode
  33. #+BEGIN_SRC emacs-lisp
  34. (use-package recentf
  35. :config
  36. (recentf-mode 1))
  37. #+END_SRC
  38. * Platform dependent
  39. ** Windows
  40. #+BEGIN_SRC emacs-lisp
  41. (when (string-equal system-type "windows-nt")
  42. (progn
  43. (defun rlbr/quote-exe (path)
  44. (w32-short-file-name path))
  45. (defun rlbr/start-external-shell ()
  46. (interactive)
  47. (start-process-shell-command (format "cmd(%s)" default-directory) nil "start default.bat"))
  48. (global-set-key (kbd "C-S-C") 'rlbr/start-external-shell)
  49. (defun rlbr/start-windows-explorer-here ()
  50. (interactive)
  51. (start-process-shell-command "explorer" nil (format "explorer %s" (replace-regexp-in-string "/" (regexp-quote "\\") (expand-file-name default-directory)))))
  52. (global-set-key (kbd "C-S-E") 'rlbr/start-windows-explorer-here)
  53. (defun rlbr/case-insensitive-match (string1 string2)
  54. (apply 'string-equal (mapcar 'downcase (list string1 string2))))
  55. (defun rlbr/output-matches (output-matches-p exe args)
  56. "locate the executable whose output satisfies output-matches-p when fed args and return the full-path"
  57. (let ((exec-path exec-path)
  58. (output)
  59. (bad)
  60. (command-output)
  61. (current-exe)
  62. (failed))
  63. (while (not (or output failed))
  64. (setq current-exe
  65. (executable-find exe))
  66. (if current-exe
  67. (progn
  68. (setq command-output
  69. (shell-command-to-string (format "%s %s" (rlbr/quote-exe current-exe) args)))
  70. (if (funcall output-matches-p command-output)
  71. (setq output current-exe)
  72. (progn
  73. (setq bad
  74. (replace-regexp-in-string "/$" "" (file-name-directory current-exe)))
  75. (setq exec-path
  76. (seq-filter (lambda (item) (not (rlbr/case-insensitive-match item bad))) exec-path)))))
  77. (setq failed t)))
  78. output))
  79. (let ((find)
  80. (grep)
  81. (ls))
  82. (progn
  83. (setq find
  84. (rlbr/output-matches
  85. (lambda (output) (string-equal ".\n" output))
  86. "find" "-maxdepth 0"))
  87. (if find
  88. (setq find-program (rlbr/quote-exe find)))
  89. (setq grep (rlbr/output-matches
  90. (lambda (output) (string-match "grep (\\w+ grep)" output))
  91. "grep" "-V"))
  92. (if grep
  93. (setq grep-program
  94. (rlbr/quote-exe grep)))
  95. (setq ls (rlbr/output-matches
  96. (lambda (output) (string-match "ls: .*'\\?/': No such file or directory" output))
  97. "ls" "?/"))
  98. (if ls
  99. (setq insert-directory-program (rlbr/quote-exe ls)))))))
  100. #+END_SRC
  101. * Tramp configuration
  102. ** Tramp append plist to connection properties
  103. #+BEGIN_SRC emacs-lisp
  104. (use-package kv
  105. :config
  106. (defun rlbr/add-config-to-tramp (matches-regexp config-plist)
  107. (let ((config-alist (kvplist->alist config-plist)))
  108. (dolist (pair config-alist)
  109. (let ((config (list
  110. matches-regexp
  111. (car pair)
  112. (cdr pair))))
  113. (add-to-list
  114. 'tramp-connection-properties
  115. config))))))
  116. #+END_SRC
  117. ** Android
  118. #+BEGIN_SRC emacs-lisp
  119. (use-package tramp
  120. :config
  121. (let ((android-config (let ((default-directory "/data/data/com.termux/files"))
  122. (list "tmpdir" (expand-file-name "home/temp/")
  123. "remote-shell" (expand-file-name "usr/bin/sh")
  124. "remote-process-environment" (append (list (concat "PREFIX=" default-directory "usr")) tramp-remote-process-environment)
  125. "remote-path" (append (mapcar 'expand-file-name '("home/.local/bin" "usr/bin" "usr/bin/applets")) '("/sbin" "/vendor/bin" "/system/sbin" "/system/bin" "/system/xbin"))))))
  126. (rlbr/add-config-to-tramp "/ssh:termux.*:" android-config)))
  127. #+END_SRC
  128. * Major modes
  129. ** Java
  130. ** JavaScript
  131. #+BEGIN_SRC emacs-lisp
  132. (use-package js2-mode
  133. :mode "\\.js\\'"
  134. :hook ((js2-mode . js2-imenu-extras-mode)
  135. (js2-mode . (lambda () (add-hook 'xref-backend-functions #'xref-js2-xref-backend nil t))))
  136. :config
  137. (use-package js2-refactor
  138. :hook (js2-mode . js2-refactor-mode)
  139. :bind
  140. (:map js2-mode-map
  141. ("C-k" . js2r-kill))
  142. :config
  143. (js2r-add-keybindings-with-prefix "C-c C-r"))
  144. (use-package xref-js2
  145. :demand t)
  146. (define-key js-mode-map (kbd "M-.") nil)
  147. (defun rlbr/jump-to-definition ()
  148. "Jump to a definition."
  149. (interactive)
  150. (condition-case-unless-debug nil
  151. (js2-jump-to-definition)
  152. (error
  153. (progn
  154. (ignore-errors
  155. (xref-pop-marker-stack))
  156. (xref-find-definitions (xref-backend-identifier-at-point (xref-find-backend)))))))
  157. (define-key js-mode-map (kbd "M-.") #'rlbr/jump-to-definition))
  158. #+END_SRC
  159. ** Magit
  160. #+BEGIN_SRC emacs-lisp
  161. (use-package magit
  162. :bind (("C-x g" . magit-status))
  163. :config
  164. (use-package git-commit
  165. :hook (git-commit-setup . git-commit-turn-on-flyspell)))
  166. #+END_SRC
  167. ** Python
  168. *** Platform specific
  169. #+BEGIN_SRC emacs-lisp
  170. (setq elpy-rpc-python-command
  171. (cond
  172. ((string-equal system-type "gnu/linux")
  173. "python3")
  174. ((string-equal system-type "windows-nt")
  175. "pythonw.exe")))
  176. #+END_SRC
  177. *** custom feature
  178. #+BEGIN_SRC emacs-lisp
  179. (defun rlbr/split-venv-with-number (name-number)
  180. "Split a virtualenv name with either a ~ seperating the name and the number, or nothing"
  181. (let ((split-result (split-string name-number (regexp-quote "~")))
  182. (ret))
  183. (if (= 1 (length split-result))
  184. (progn
  185. (setq ret (car split-result))
  186. (push 0 ret))
  187. (progn
  188. (setq ret
  189. (string-join
  190. (butlast split-result)
  191. "~"))
  192. (push
  193. (string-to-number
  194. (car (last split-result)))
  195. ret)))
  196. ret))
  197. (defun rlbr/join-venv-with-number (number-name)
  198. "Join a list with a name and a number"
  199. (let
  200. ((number (car number-name))
  201. (name (cdr number-name)))
  202. (if (= number 0)
  203. name
  204. (string-join (list name (number-to-string number)) "~"))))
  205. #+END_SRC
  206. *** bindings/settings
  207. #+BEGIN_SRC emacs-lisp
  208. (use-package python
  209. :hook ((python-mode . blacken-mode)
  210. (python-mode . pyvenv-mode))
  211. :config
  212. (use-package elpy
  213. :bind (("C-=" . elpy-goto-assignment))
  214. :config (when (require 'flycheck nil t)
  215. (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))))
  216. (elpy-enable))
  217. #+END_SRC
  218. ** SSH config mode
  219. #+BEGIN_SRC emacs-lisp
  220. (use-package ssh-config-mode
  221. :mode "~/.ssh/config\\'")
  222. #+END_SRC
  223. ** Tramp
  224. ** Webmode
  225. #+BEGIN_SRC emacs-lisp
  226. (use-package web-mode
  227. :mode
  228. (("\\.phtml\\'" . web-mode)
  229. ("\\.tpl\\.php\\'" . web-mode)
  230. ("\\.[agj]sp\\'" . web-mode)
  231. ("\\.as[cp]x\\'" . web-mode)
  232. ("\\.erb\\'" . web-mode)
  233. ("\\.mustache\\'" . web-mode)
  234. ("\\.djhtml\\'" . web-mode)
  235. ("\\.html?\\'" . web-mode)))
  236. #+END_SRC
  237. ** YAML
  238. #+BEGIN_SRC emacs-lisp
  239. (use-package yaml-mode
  240. :mode "\\.yml\\'")
  241. #+END_SRC
  242. * Minor modes/misc
  243. ** Kill the things
  244. *** Buffer
  245. #+BEGIN_SRC emacs-lisp
  246. (global-set-key (kbd "C-x k") 'kill-this-buffer)
  247. #+END_SRC
  248. *** Emacs
  249. #+BEGIN_SRC emacs-lisp
  250. (global-set-key (kbd "C-x C-k C-x C-k") 'kill-emacs)
  251. #+END_SRC
  252. ** Lispy
  253. #+BEGIN_SRC emacs-lisp
  254. (use-package lispy
  255. :hook ((emacs-lisp-mode) . lispy-mode))
  256. #+END_SRC
  257. ** Custom custom
  258. #+BEGIN_SRC emacs-lisp
  259. (require 'isearch)
  260. (require 'lispy)
  261. (defun rlbr/multiline-sexp-with-symbol (symbol-name)
  262. (save-excursion
  263. (beginning-of-buffer)
  264. (search-forward-regexp (isearch-symbol-regexp symbol-name))
  265. (backward-up-list)
  266. (lispy-alt-multiline)))
  267. (advice-add 'custom-save-faces :after (lambda () (rlbr/multiline-sexp-with-symbol "custom-set-faces")))
  268. (advice-add 'custom-save-variables :after (lambda () (rlbr/multiline-sexp-with-symbol "custom-set-variables")))
  269. #+END_SRC
  270. * Navigation/auto-completion
  271. ** Ace window
  272. #+BEGIN_SRC emacs-lisp
  273. (use-package ace-window
  274. :bind (("M-Q" . ace-window)))
  275. #+END_SRC
  276. ** Hippie expand
  277. #+BEGIN_SRC emacs-lisp
  278. (use-package hippie-exp
  279. :bind ("M-/" . hippie-expand))
  280. #+END_SRC
  281. ** IBuffer mode
  282. #+BEGIN_SRC emacs-lisp
  283. (use-package ibbufer-vc
  284. :hook ((ibuffer-mode . ibuffer-vc-set-filter-groups-by-vc-root)))
  285. (use-package ibuffer
  286. :bind (("C-x C-b" . ibuffer))
  287. :config
  288. (define-ibuffer-column size-h
  289. ;; Use human readable Size column instead of original one
  290. (:name "Size" :inline t)
  291. (cond ((> (buffer-size) 1000000)
  292. (format "%7.1fM" (/ (buffer-size) 1000000.0)))
  293. ((> (buffer-size) 100000)
  294. (format "%7.0fk" (/ (buffer-size) 1000.0)))
  295. ((> (buffer-size) 1000)
  296. (format "%7.1fk" (/ (buffer-size) 1000.0)))
  297. (t
  298. (format "%8d" (buffer-size))))))
  299. #+END_SRC
  300. ** Ivy
  301. #+BEGIN_SRC emacs-lisp
  302. (use-package ivy
  303. :config
  304. (use-package swiper
  305. :bind ("C-s" . swiper))
  306. (ivy-mode))
  307. #+END_SRC
  308. * Look and feel
  309. ** Line numbers
  310. #+BEGIN_SRC emacs-lisp
  311. (global-display-line-numbers-mode)
  312. #+END_SRC
  313. ** Mode line bell
  314. #+BEGIN_SRC emacs-lisp
  315. (use-package mode-line-bell
  316. :config
  317. (mode-line-bell-mode))
  318. #+END_SRC
  319. ** Spaceline
  320. #+BEGIN_SRC emacs-lisp
  321. (use-package spaceline-config
  322. :config
  323. (use-package winum
  324. :init
  325. (setq winum-keymap
  326. (let ((map (make-sparse-keymap)))
  327. (define-key map (kbd "M-0") 'winum-select-window-0-or-10)
  328. (define-key map (kbd "M-1") 'winum-select-window-1)
  329. (define-key map (kbd "M-2") 'winum-select-window-2)
  330. (define-key map (kbd "M-3") 'winum-select-window-3)
  331. (define-key map (kbd "M-4") 'winum-select-window-4)
  332. (define-key map (kbd "M-5") 'winum-select-window-5)
  333. (define-key map (kbd "M-6") 'winum-select-window-6)
  334. (define-key map (kbd "M-7") 'winum-select-window-7)
  335. (define-key map (kbd "M-8") 'winum-select-window-8)
  336. map)))
  337. (spaceline-spacemacs-theme)
  338. (winum-mode))
  339. #+END_SRC