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.

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