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.

293 lines
8.5 KiB

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 dependant
  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 satifies output-matches-p when fed args and return the fullpath"
  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. (require 'kv)
  105. (defun rlbr/add-config-to-tramp (matches-regexp config-plist)
  106. (let ((config-alist (kvplist->alist config-plist)))
  107. (dolist (pair config-alist)
  108. (let ((config (list
  109. matches-regexp
  110. (car pair)
  111. (cdr pair))))
  112. (add-to-list
  113. 'tramp-connection-properties
  114. config)))))
  115. #+END_SRC
  116. ** Android
  117. #+begin_src emacs-lisp
  118. (let ((android-config (let ((default-directory "/data/data/com.termux/files"))
  119. (list "tmpdir" (expand-file-name "home/temp/")
  120. "remote-shell" (expand-file-name "usr/bin/sh")
  121. "remote-process-environment" (append (list (concat "PREFIX=" default-directory "usr")) tramp-remote-process-environment)
  122. "remote-path" (append (mapcar 'expand-file-name '("home/.local/bin" "usr/bin" "usr/bin/applets")) '("/sbin" "/vendor/bin" "/system/sbin" "/system/bin" "/system/xbin"))))))
  123. (rlbr/add-config-to-tramp "/ssh:termux.*:" android-config))
  124. #+end_src
  125. * Major modes
  126. ** Java
  127. ** JavaScript
  128. #+BEGIN_SRC emacs-lisp
  129. (use-package js2-mode
  130. :mode "\\.js\\'"
  131. :hook ((js2-mode . js2-imenu-extras-mode)
  132. (js2-mode . (lambda () (add-hook 'xref-backend-functions #'xref-js2-xref-backend nil t))))
  133. :config
  134. (use-package js2-refactor
  135. :hook (js2-mode . js2-refactor-mode)
  136. :bind
  137. (:map js2-mode-map
  138. ("C-k" . js2r-kill))
  139. :config
  140. (js2r-add-keybindings-with-prefix "C-c C-r"))
  141. (use-package xref-js2
  142. :demand t)
  143. (define-key js-mode-map (kbd "M-.") nil)
  144. (defun rlbr/jump-to-definition ()
  145. "Jump to a definition."
  146. (interactive)
  147. (condition-case-unless-debug nil
  148. (js2-jump-to-definition)
  149. (error
  150. (progn
  151. (ignore-errors
  152. (xref-pop-marker-stack))
  153. (xref-find-definitions (xref-backend-identifier-at-point (xref-find-backend)))))))
  154. (define-key js-mode-map (kbd "M-.") #'rlbr/jump-to-definition))
  155. #+END_SRC
  156. ** Magit
  157. #+BEGIN_SRC emacs-lisp
  158. (use-package magit
  159. :bind (("C-x g" . magit-status))
  160. :config
  161. (use-package git-commit
  162. :hook (git-commit-setup . git-commit-turn-on-flyspell)))
  163. #+END_SRC
  164. ** Python
  165. *** Platform specific
  166. #+BEGIN_SRC emacs-lisp
  167. (cond
  168. ((string-equal system-type "gnu/linux")
  169. "python3")
  170. ((string-equal system-type "windows-nt")
  171. "python.exe"))
  172. #+END_SRC
  173. *** custom feature
  174. *** bindings/settings
  175. #+begin_src emacs-lisp
  176. (use-package python
  177. :config
  178. (use-package elpy
  179. :bind (("C-=" . elpy-goto-assignment))
  180. :config (when (require 'flycheck nil t)
  181. (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))))
  182. (elpy-enable)
  183. (blacken-mode))
  184. #+END_SRC
  185. ** SSH config mode
  186. #+BEGIN_SRC emacs-lisp
  187. (use-package ssh-config-mode
  188. :mode "~/.ssh/config\\'")
  189. #+END_SRC
  190. ** Tramp
  191. ** Webmode
  192. #+BEGIN_SRC emacs-lisp
  193. (use-package web-mode
  194. :mode
  195. (("\\.phtml\\'" . web-mode)
  196. ("\\.tpl\\.php\\'" . web-mode)
  197. ("\\.[agj]sp\\'" . web-mode)
  198. ("\\.as[cp]x\\'" . web-mode)
  199. ("\\.erb\\'" . web-mode)
  200. ("\\.mustache\\'" . web-mode)
  201. ("\\.djhtml\\'" . web-mode)
  202. ("\\.html?\\'" . web-mode)))
  203. #+END_SRC
  204. ** YAML
  205. #+BEGIN_SRC emacs-lisp
  206. (use-package yaml-mode
  207. :mode "\\.yml\\'")
  208. #+END_SRC
  209. * Minor modes/misc
  210. ** Kill the things
  211. *** Buffer
  212. #+BEGIN_SRC emacs-lisp
  213. (global-set-key (kbd "C-x k") 'kill-this-buffer)
  214. #+END_SRC
  215. *** Emacs
  216. #+BEGIN_SRC emacs-lisp
  217. (global-set-key (kbd "C-x C-k C-x C-k") 'kill-emacs)
  218. #+END_SRC
  219. ** Lispy
  220. #+BEGIN_SRC emacs-lisp
  221. (use-package lispy
  222. :hook ((emacs-lisp-mode) . lispy-mode))
  223. #+END_SRC
  224. * Navigation/autocompletion
  225. ** Ace window
  226. #+BEGIN_SRC emacs-lisp
  227. (use-package ace-window
  228. :bind (("M-Q" . ace-window)))
  229. #+END_SRC
  230. ** Hippie expand
  231. #+BEGIN_SRC emacs-lisp
  232. (use-package hippie-exp
  233. :bind ("M-/" . hippie-expand))
  234. #+END_SRC
  235. ** IBuffer mode
  236. #+BEGIN_SRC emacs-lisp
  237. (use-package ibbufer-vc
  238. :hook ((ibuffer-mode . ibuffer-vc-set-filter-groups-by-vc-root)))
  239. (use-package ibuffer
  240. :bind (("C-x C-b" . ibuffer))
  241. :config
  242. (define-ibuffer-column size-h
  243. ;; Use human readable Size column instead of original one
  244. (:name "Size" :inline t)
  245. (cond ((> (buffer-size) 1000000)
  246. (format "%7.1fM" (/ (buffer-size) 1000000.0)))
  247. ((> (buffer-size) 100000)
  248. (format "%7.0fk" (/ (buffer-size) 1000.0)))
  249. ((> (buffer-size) 1000)
  250. (format "%7.1fk" (/ (buffer-size) 1000.0)))
  251. (t
  252. (format "%8d" (buffer-size))))))
  253. #+END_SRC
  254. ** Ivy
  255. #+BEGIN_SRC emacs-lisp
  256. (use-package ivy
  257. :config
  258. (use-package swiper
  259. :bind ("C-s" . swiper))
  260. (ivy-mode))
  261. #+END_SRC
  262. * Look and feel
  263. ** Line numbers
  264. #+BEGIN_SRC emacs-lisp
  265. (global-display-line-numbers-mode)
  266. #+END_SRC
  267. ** Mode line bell
  268. #+BEGIN_SRC emacs-lisp
  269. (use-package mode-line-bell
  270. :config
  271. (mode-line-bell-mode))
  272. #+END_SRC
  273. ** Spaceline
  274. #+BEGIN_SRC emacs-lisp
  275. (use-package spaceline-config
  276. :config
  277. (use-package winum
  278. :init
  279. (setq winum-keymap
  280. (let ((map (make-sparse-keymap)))
  281. (define-key map (kbd "M-0") 'winum-select-window-0-or-10)
  282. (define-key map (kbd "M-1") 'winum-select-window-1)
  283. (define-key map (kbd "M-2") 'winum-select-window-2)
  284. (define-key map (kbd "M-3") 'winum-select-window-3)
  285. (define-key map (kbd "M-4") 'winum-select-window-4)
  286. (define-key map (kbd "M-5") 'winum-select-window-5)
  287. (define-key map (kbd "M-6") 'winum-select-window-6)
  288. (define-key map (kbd "M-7") 'winum-select-window-7)
  289. (define-key map (kbd "M-8") 'winum-select-window-8)
  290. map)))
  291. (spaceline-spacemacs-theme)
  292. (winum-mode))
  293. #+END_SRC