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.

338 lines
9.9 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. (cond
  171. ((string-equal system-type "gnu/linux")
  172. "python3")
  173. ((string-equal system-type "windows-nt")
  174. "python.exe"))
  175. #+END_SRC
  176. *** custom feature
  177. #+BEGIN_SRC emacs-lisp
  178. (defun rlbr/split-venv-with-number (name-number)
  179. "Split a virtualenv name with either a ~ seperating the name and the number, or nothing"
  180. (let ((split-result (split-string name-number (regexp-quote "~")))
  181. (ret))
  182. (if (= 1 (length split-result))
  183. (progn
  184. (setq ret (car split-result))
  185. (push 0 ret))
  186. (progn
  187. (setq ret
  188. (string-join
  189. (butlast split-result)
  190. "~"))
  191. (push
  192. (string-to-number
  193. (car (last split-result)))
  194. ret)))
  195. ret))
  196. (defun rlbr/join-venv-with-number (number-name)
  197. "Join a list with a name and a number"
  198. (let
  199. ((number (car number-name))
  200. (name (cdr number-name)))
  201. (if (= number 0)
  202. name
  203. (string-join (list name (number-to-string number)) "~"))))
  204. #+END_SRC
  205. *** bindings/settings
  206. #+BEGIN_SRC emacs-lisp
  207. (use-package python
  208. :config
  209. (use-package elpy
  210. :bind (("C-=" . elpy-goto-assignment))
  211. :config (when (require 'flycheck nil t)
  212. (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))))
  213. (elpy-enable)
  214. (blacken-mode))
  215. #+END_SRC
  216. ** SSH config mode
  217. #+BEGIN_SRC emacs-lisp
  218. (use-package ssh-config-mode
  219. :mode "~/.ssh/config\\'")
  220. #+END_SRC
  221. ** Tramp
  222. ** Webmode
  223. #+BEGIN_SRC emacs-lisp
  224. (use-package web-mode
  225. :mode
  226. (("\\.phtml\\'" . web-mode)
  227. ("\\.tpl\\.php\\'" . web-mode)
  228. ("\\.[agj]sp\\'" . web-mode)
  229. ("\\.as[cp]x\\'" . web-mode)
  230. ("\\.erb\\'" . web-mode)
  231. ("\\.mustache\\'" . web-mode)
  232. ("\\.djhtml\\'" . web-mode)
  233. ("\\.html?\\'" . web-mode)))
  234. #+END_SRC
  235. ** YAML
  236. #+BEGIN_SRC emacs-lisp
  237. (use-package yaml-mode
  238. :mode "\\.yml\\'")
  239. #+END_SRC
  240. * Minor modes/misc
  241. ** Kill the things
  242. *** Buffer
  243. #+BEGIN_SRC emacs-lisp
  244. (global-set-key (kbd "C-x k") 'kill-this-buffer)
  245. #+END_SRC
  246. *** Emacs
  247. #+BEGIN_SRC emacs-lisp
  248. (global-set-key (kbd "C-x C-k C-x C-k") 'kill-emacs)
  249. #+END_SRC
  250. ** Lispy
  251. #+BEGIN_SRC emacs-lisp
  252. (use-package lispy
  253. :hook ((emacs-lisp-mode) . lispy-mode))
  254. #+END_SRC
  255. ** Custom custom
  256. #+BEGIN_SRC emacs-lisp
  257. (require 'isearch)
  258. (require 'lispy)
  259. (defun rlbr/multiline-sexp-with-symbol (symbol-name)
  260. (save-excursion
  261. (beginning-of-buffer)
  262. (search-forward-regexp (isearch-symbol-regexp symbol-name))
  263. (backward-up-list)
  264. (lispy-alt-multiline)))
  265. (advice-add 'custom-save-faces :after (lambda () (rlbr/multiline-sexp-with-symbol "custom-set-faces")))
  266. (advice-add 'custom-save-variables :after (lambda () (rlbr/multiline-sexp-with-symbol "custom-set-variables")))
  267. #+END_SRC
  268. * Navigation/auto-completion
  269. ** Ace window
  270. #+BEGIN_SRC emacs-lisp
  271. (use-package ace-window
  272. :bind (("M-Q" . ace-window)))
  273. #+END_SRC
  274. ** Hippie expand
  275. #+BEGIN_SRC emacs-lisp
  276. (use-package hippie-exp
  277. :bind ("M-/" . hippie-expand))
  278. #+END_SRC
  279. ** IBuffer mode
  280. #+BEGIN_SRC emacs-lisp
  281. (use-package ibbufer-vc
  282. :hook ((ibuffer-mode . ibuffer-vc-set-filter-groups-by-vc-root)))
  283. (use-package ibuffer
  284. :bind (("C-x C-b" . ibuffer))
  285. :config
  286. (define-ibuffer-column size-h
  287. ;; Use human readable Size column instead of original one
  288. (:name "Size" :inline t)
  289. (cond ((> (buffer-size) 1000000)
  290. (format "%7.1fM" (/ (buffer-size) 1000000.0)))
  291. ((> (buffer-size) 100000)
  292. (format "%7.0fk" (/ (buffer-size) 1000.0)))
  293. ((> (buffer-size) 1000)
  294. (format "%7.1fk" (/ (buffer-size) 1000.0)))
  295. (t
  296. (format "%8d" (buffer-size))))))
  297. #+END_SRC
  298. ** Ivy
  299. #+BEGIN_SRC emacs-lisp
  300. (use-package ivy
  301. :config
  302. (use-package swiper
  303. :bind ("C-s" . swiper))
  304. (ivy-mode))
  305. #+END_SRC
  306. * Look and feel
  307. ** Line numbers
  308. #+BEGIN_SRC emacs-lisp
  309. (global-display-line-numbers-mode)
  310. #+END_SRC
  311. ** Mode line bell
  312. #+BEGIN_SRC emacs-lisp
  313. (use-package mode-line-bell
  314. :config
  315. (mode-line-bell-mode))
  316. #+END_SRC
  317. ** Spaceline
  318. #+BEGIN_SRC emacs-lisp
  319. (use-package spaceline-config
  320. :config
  321. (use-package winum
  322. :init
  323. (setq winum-keymap
  324. (let ((map (make-sparse-keymap)))
  325. (define-key map (kbd "M-0") 'winum-select-window-0-or-10)
  326. (define-key map (kbd "M-1") 'winum-select-window-1)
  327. (define-key map (kbd "M-2") 'winum-select-window-2)
  328. (define-key map (kbd "M-3") 'winum-select-window-3)
  329. (define-key map (kbd "M-4") 'winum-select-window-4)
  330. (define-key map (kbd "M-5") 'winum-select-window-5)
  331. (define-key map (kbd "M-6") 'winum-select-window-6)
  332. (define-key map (kbd "M-7") 'winum-select-window-7)
  333. (define-key map (kbd "M-8") 'winum-select-window-8)
  334. map)))
  335. (spaceline-spacemacs-theme)
  336. (winum-mode))
  337. #+END_SRC