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.

301 lines
9.6 KiB

  1. * Global
  2. ** Add features/modes
  3. *** string-inflection
  4. #+BEGIN_SRC emacs-lisp
  5. (require 'string-inflection)
  6. ;; C-q C-u is the key bindings similar to Vz Editor.
  7. (global-set-key (kbd "C-M-,") 'my-string-inflection-cycle-auto)
  8. (defun my-string-inflection-cycle-auto ()
  9. "switching by major-mode"
  10. (interactive)
  11. (cond
  12. ;; for emacs-lisp-mode
  13. ((eq major-mode 'emacs-lisp-mode)
  14. (string-inflection-all-cycle))
  15. ;; for python
  16. ((eq major-mode 'python-mode)
  17. (string-inflection-python-style-cycle))
  18. ;; for java
  19. ((eq major-mode 'java-mode)
  20. (string-inflection-java-style-cycle))
  21. (t
  22. ;; default
  23. (string-inflection-ruby-style-cycle))))
  24. #+END_SRC
  25. *** undo-tree
  26. #+begin_src emacs-lisp
  27. (require 'undo-tree)
  28. (global-undo-tree-mode)
  29. #+end_src
  30. *** ssh-config-mode
  31. #+begin_src emacs-lisp
  32. (require 'ssh-config-mode)
  33. (add-to-list 'auto-mode-alist '("~/.ssh/config\\'" . ssh-config-mode))
  34. #+end_src
  35. *** yaml-mode
  36. #+begin_src emacs-lisp
  37. (require 'yaml-mode)
  38. (add-to-list 'auto-mode-alist '("\\.yml\\'" . yaml-mode))
  39. #+end_src
  40. *** elpy move line up and down globally
  41. #+begin_src emacs-lisp
  42. (require 'elpy)
  43. (global-set-key (kbd "M-<up>") 'elpy-nav-move-line-or-region-up)
  44. (global-set-key (kbd "M-<down>") 'elpy-nav-move-line-or-region-down)
  45. (global-set-key (kbd "C-c <left>") 'windmove-left)
  46. (global-set-key (kbd "C-c <right>") 'windmove-right)
  47. (global-set-key (kbd "C-c <up>") 'windmove-up)
  48. (global-set-key (kbd "C-c <down>") 'windmove-down)
  49. #+end_src
  50. *** iedit-mode
  51. #+begin_src emacs-lisp
  52. (global-set-key (kbd "C-;") 'iedit-mode)
  53. #+end_src
  54. *** spellcheck entire buffer
  55. #+begin_src emacs-lisp
  56. (global-set-key (kbd "C-!") 'ispell-buffer)
  57. #+end_src
  58. *** find/replace
  59. #+begin_src emacs-lisp
  60. (require 'visual-regexp-steroids)
  61. (define-key global-map (kbd "C-c r") 'vr/replace)
  62. (define-key global-map (kbd "C-c q") 'vr/query-replace)
  63. #+end_src
  64. *** duplicate line function
  65. #+begin_src emacs-lisp
  66. (defun duplicate-line()
  67. (interactive)
  68. (move-beginning-of-line 1)
  69. (kill-line)
  70. (yank)
  71. (open-line 1)
  72. (next-line 1)
  73. (yank))
  74. (global-set-key (kbd "C-c d") 'duplicate-line)
  75. #+end_src
  76. *** magit
  77. #+begin_src emacs-lisp
  78. (global-set-key (kbd "C-x g") 'magit-status)
  79. #+end_src
  80. #+begin_src emacs-lisp
  81. (add-hook 'git-commit-setup-hook 'git-commit-turn-on-flyspell)
  82. #+end_src
  83. *** filename completion
  84. #+BEGIN_SRC emacs-lisp
  85. (global-set-key (kbd "C->") 'comint-dynamic-complete-filename)
  86. #+END_SRC
  87. *** hippie expand
  88. #+BEGIN_SRC emacs-lisp
  89. (global-set-key (kbd "M-/") 'hippie-expand)
  90. (setq hippie-expand-try-functions-list '(try-expand-dabbrev try-expand-dabbrev-all-buffers try-expand-dabbrev-from-kill
  91. try-complete-file-name-partially try-complete-file-name
  92. try-expand-all-abbrevs try-expand-list try-expand-line
  93. try-complete-lisp-symbol-partially try-complete-lisp-symbol))
  94. #+END_SRC
  95. *** window numbering mode
  96. #+BEGIN_SRC emacs-lisp
  97. (window-numbering-mode)
  98. #+END_SRC
  99. ** options
  100. *** global
  101. **** recent files
  102. #+begin_src emacs-lisp
  103. (require 'recentf)
  104. (recentf-mode 1)
  105. #+end_src
  106. **** backup
  107. #+begin_src emacs-lisp
  108. (let ((backup-dir (concat user-emacs-directory "backup"))
  109. (auto-save-dir (concat user-emacs-directory "autosave"))
  110. )
  111. (if (not (file-directory-p backup-dir))
  112. (make-directory backup-dir))
  113. (if (not(file-directory-p auto-save-dir))
  114. (make-directory auto-save-dir)
  115. )
  116. (setq backup-directory-alist
  117. `((".*" . , backup-dir)))
  118. (setq auto-save-file-name-transforms
  119. `((".*" , (concat auto-save-dir "/") t))))
  120. #+end_src
  121. **** kill buffer option
  122. #+begin_src emacs-lisp
  123. (global-set-key (kbd "C-x k") 'kill-this-buffer)
  124. #+end_src
  125. **** inhibit start screen
  126. #+begin_src emacs-lisp
  127. (setq inhibit-startup-screen t)
  128. #+end_src
  129. **** save options
  130. #+begin_src emacs-lisp
  131. (add-hook 'before-save-hook 'delete-trailing-whitespace)
  132. #+end_src
  133. **** theming
  134. #+begin_src emacs-lisp
  135. (load-theme 'dracula t)
  136. #+end_src
  137. **** bell
  138. #+BEGIN_SRC emacs-lisp
  139. (mode-line-bell-mode)
  140. #+END_SRC
  141. **** ivy
  142. #+BEGIN_SRC emacs-lisp
  143. (ivy-mode)
  144. (global-set-key (kbd "C-s") 'swiper)
  145. #+END_SRC
  146. **** kill-emacs key
  147. #+BEGIN_SRC emacs-lisp
  148. (global-set-key (kbd "C-x C-k C-x C-k") 'kill-emacs)
  149. #+END_SRC
  150. **** linum
  151. #+BEGIN_SRC emacs-lisp
  152. (global-display-line-numbers-mode)
  153. #+END_SRC
  154. **** enable commands
  155. #+BEGIN_SRC emacs-lisp
  156. (put 'downcase-region 'disabled nil)
  157. #+END_SRC
  158. *** dired
  159. #+begin_src emacs-lisp
  160. (setq ls-lisp-use-insert-directory-program t)
  161. #+end_src
  162. #+begin_src emacs-lisp
  163. (setq dired-listing-switches "-alh")
  164. #+end_src
  165. * Tramp configuration
  166. #+begin_src emacs-lisp
  167. (add-to-list 'tramp-remote-path 'tramp-own-remote-path)
  168. (add-to-list 'tramp-remote-path "/system/xbin")
  169. (add-to-list 'tramp-remote-path "/data/data/com.termux/file/usr/bin")
  170. #+end_src
  171. * Web things
  172. ** javascript stuff
  173. #+begin_src emacs-lisp
  174. (require 'js2-mode)
  175. (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  176. (add-hook 'js2-mode-hook #'js2-imenu-extras-mode)
  177. (require 'js2-refactor)
  178. (require 'xref-js2)
  179. (add-hook 'js2-mode-hook #'js2-refactor-mode)
  180. (js2r-add-keybindings-with-prefix "C-c C-r")
  181. (define-key js2-mode-map (kbd "C-k") #'js2r-kill)
  182. (define-key js-mode-map (kbd "M-.") nil)
  183. (add-hook 'js2-mode-hook (lambda ()
  184. (add-hook 'xref-backend-functions #'xref-js2-xref-backend nil t)))
  185. (define-key js2-mode-map (kbd "C-k") #'js2r-kill)
  186. #+end_src
  187. ** web mode
  188. #+begin_src emacs-lisp
  189. (require 'web-mode)
  190. (add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
  191. (add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
  192. (add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
  193. (add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
  194. (add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
  195. (add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
  196. (add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
  197. (add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
  198. #+end_src
  199. * Platform Specific
  200. #+begin_src emacs-lisp
  201. (cond
  202. #+end_src
  203. ** Windows
  204. #+begin_src emacs-lisp
  205. ((string-equal system-type "windows-nt")
  206. (progn
  207. (defun rlbr/quote-exe (path)
  208. (w32-short-file-name path))
  209. (defun rlbr/start-external-shell ()
  210. (interactive)
  211. (start-process-shell-command (format "cmd(%s)" default-directory) nil "start default.bat"))
  212. (global-set-key (kbd "C-S-C") 'rlbr/start-external-shell)
  213. (defun rlbr/start-windows-explorer-here ()
  214. (interactive)
  215. (start-process-shell-command "explorer" nil
  216. (format "explorer %s"
  217. (replace-regexp-in-string "/" (regexp-quote "\\") (expand-file-name default-directory))))
  218. )
  219. (global-set-key (kbd "C-S-E") 'rlbr/start-windows-explorer-here)
  220. (defun rlbr/case-insensitive-match (string1 string2)
  221. (apply 'string-equal (mapcar 'downcase (list string1 string2))))
  222. (defun rlbr/output-matches (output-matches-p exe args)
  223. "locate the executable whose output satifies output-matches-p when fed args and return the fullpath"
  224. (let
  225. (
  226. (exec-path exec-path)
  227. (output)
  228. (bad)
  229. (command-output)
  230. (current-exe)
  231. (failed)
  232. )
  233. (while (not (or output failed))
  234. (setq current-exe (executable-find exe))
  235. (if current-exe
  236. (progn
  237. (setq command-output (shell-command-to-string (format "%s %s" (rlbr/quote-exe current-exe) args)))
  238. (if (funcall output-matches-p command-output)
  239. (setq output current-exe)
  240. (progn
  241. (setq bad (replace-regexp-in-string "/$" "" (file-name-directory current-exe)))
  242. (setq exec-path (seq-filter (lambda (item) (not (rlbr/case-insensitive-match item bad))) exec-path)))))
  243. (setq failed t)))
  244. output))
  245. (let
  246. ((find)
  247. (grep)
  248. (ls))
  249. (progn
  250. (setq find (rlbr/output-matches (lambda (output) (string-equal ".\n" output)) "find" "-maxdepth 0"))
  251. (if find
  252. (setq find-program (rlbr/quote-exe find)))
  253. (setq grep (rlbr/output-matches (lambda (output) (string-match "grep (\\w+ grep)" output)) "grep" "-V"))
  254. (if grep
  255. (setq grep-program (rlbr/quote-exe grep)))
  256. (setq ls (rlbr/output-matches (lambda (output) (string-match "ls: .*'\\?/': No such file or directory" output)) "ls" "?/"))
  257. (if ls
  258. (setq insert-directory-program (rlbr/quote-exe ls)))))
  259. (setq python-shell-interpreter (rlbr/quote-exe (executable-find "python")))
  260. (setq python-check-command (rlbr/quote-exe (executable-find "flake8")))
  261. (setq delete-by-moving-to-trash t)
  262. (defun python-shell-interpreter-refresh ()
  263. (interactive)
  264. (setq python-shell-interpreter (rlbr/quote-exe (executable-find "python"))))
  265. (add-hook 'python-django-project-root-hook 'python-shell-interpreter-refresh)
  266. ))
  267. #+end_src
  268. ** Linux
  269. #+begin_src emacs-lisp
  270. ((string-equal system-type "gnu/linux")
  271. (progn
  272. (setq python-shell-interpreter "python3")
  273. (setq elpy-rpc-python-command python-shell-interpreter)
  274. (defun get-elpa-package-install-directory (pkg)
  275. "Return the install directory of elpa PKG. Return nil if it is not found."
  276. (let ((elpa-dir package-user-dir))
  277. (when (file-exists-p elpa-dir)
  278. (let* ((pkg-match (concat "\\`" (symbol-name pkg) "-[0-9]+"))
  279. (dir (car (directory-files elpa-dir 'full pkg-match))))
  280. (when dir (file-name-as-directory dir))))))
  281. (setq vr/command-python
  282. (format "python3 %s" (expand-file-name "regexp.py" (get-elpa-package-install-directory 'visual-regexp-steroids))))
  283. )))
  284. #+end_src
  285. * Python
  286. #+begin_src emacs-lisp
  287. (elpy-enable)
  288. (when (require 'flycheck nil t)
  289. (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
  290. (add-hook 'elpy-mode-hook 'flycheck-mode))
  291. (require 'blacken)
  292. (defun python-mode-keys ()
  293. "Modify python-mode local key map"
  294. (local-set-key (kbd "C-=") 'elpy-goto-assignment))
  295. (add-hook 'python-mode-hook 'python-mode-keys)
  296. (add-hook 'elpy-mode-hook 'blacken-mode)
  297. (setq elpy-syntax-check-command python-check-command)
  298. #+end_src