init.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. ;; my favorite folding mode for emacs
  2. ;; Use `C-c @ C-s' to show entry
  3. ;;
  4. ;;{{{ Instructions For Folding
  5. ;; C-c @ C-x hide entry
  6. ;; C-c @ C-u Get out/in of folding
  7. ;; C-c @ C-w Fold whole buffer
  8. ;; C-c @ C-o Unfold whole buffer
  9. ;;}}}
  10. ;;{{{ License (GPL3)
  11. ;; This program is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; This program is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. ;;}}}
  22. ;;{{{ Package manager
  23. (require 'package)
  24. (setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
  25. (setq package-archives
  26. '(("gnu" . "http://elpa.gnu.org/packages/")
  27. ("melpa" . "https://melpa.org/packages/")
  28. ;; ("marmalade" . "https://marmalade-repo.org/packages/")
  29. ("org" . "http://orgmode.org/elpa/")))
  30. (package-initialize)
  31. ;; fetch packages available
  32. (unless package-archive-contents
  33. (package-refresh-contents))
  34. ;; list of my packages
  35. (setq my-package-list
  36. '(zenburn-theme
  37. folding
  38. use-package
  39. paredit
  40. magit
  41. smex
  42. ido-vertical-mode
  43. ido-yes-or-no
  44. prolog
  45. sql-indent))
  46. ;; fetch the list of packages available
  47. (unless package-archive-contents
  48. (package-refresh-contents))
  49. ;; install the missing packages
  50. (dolist (curr-package my-package-list)
  51. (unless (package-installed-p curr-package)
  52. (package-install curr-package)))
  53. ;;}}}
  54. ;;{{{ Basic setting
  55. (server-start)
  56. (scroll-bar-mode -1)
  57. (menu-bar-mode -1)
  58. (tool-bar-mode -1)
  59. (set-fringe-mode 0)
  60. (load-theme 'zenburn t)
  61. (setq inhibit-splash-screen t)
  62. (set-language-environment "UTF-8")
  63. (set-default-coding-systems 'utf-8)
  64. (setq show-paren-delay 0)
  65. (show-paren-mode 1)
  66. (setq backup-directory-alist
  67. `((".*" . ,temporary-file-directory)))
  68. (setq auto-save-file-name-transforms
  69. `((".*" ,temporary-file-directory t)))
  70. (global-prettify-symbols-mode 1)
  71. (setq display-time-day-and-date t)
  72. (display-time-mode 1)
  73. (setq erc-generate-log-file-name-function
  74. (quote erc-generate-log-file-name-with-date))
  75. (setq erc-save-buffer-on-part t)
  76. ;;}}}
  77. ;;{{{ Basic key-bindings
  78. ;; for easier auto complete
  79. (global-set-key
  80. (kbd "C-; C-;")
  81. (lambda ()
  82. (interactive)
  83. (dabbrev-expand nil)))
  84. (global-set-key (kbd "C-.") #'goto-line)
  85. (require 'recentf)
  86. (recentf-mode 1)
  87. (defun sudo-edit ()
  88. (interactive)
  89. (find-file (concat "/sudo:root@localhost:"
  90. buffer-file-name)))
  91. (global-set-key (kbd "C-; r") 'sudo-edit)
  92. ;; Here I toggle copyright on top of file
  93. (add-hook 'c-mode-common-hook #'elide-head)
  94. ;; Adding sqlind-minor-mode to sql-mode
  95. (add-hook 'sql-mode-hook 'sqlind-minor-mode)
  96. (global-set-key (kbd "C-; l") 'elide-head)
  97. (global-set-key (kbd "C-; L")
  98. (lambda ()
  99. (interactive)
  100. (elide-head t)))
  101. ;; vi like % for paren matching
  102. (defun goto-match-paren (arg)
  103. "Go to the matching parenthesis if on parenthesis,
  104. otherwise insert %. vi style of % jumping to
  105. matching brace."
  106. (interactive "p")
  107. (cond ((looking-at "\\s\(")
  108. (forward-list 1) (backward-char 1))
  109. ((looking-at "\\s\)")
  110. (forward-char 1) (backward-list 1))
  111. (t (self-insert-command (or arg 1)))))
  112. (global-set-key (kbd "C-%") 'goto-match-paren)
  113. (defun sort-lines (reverse beg end)
  114. "Sort lines in region alphabetically; argument means descending order.
  115. Called from a program, there are three arguments:
  116. REVERSE (non-nil means reverse order), BEG and END (region to sort).
  117. The variable `sort-fold-case' determines whether alphabetic case affects
  118. the sort order."
  119. (interactive "P\nr")
  120. (save-excursion
  121. (save-restriction
  122. (narrow-to-region beg end)
  123. (goto-char (point-min))
  124. (let ((inhibit-field-text-motion t))
  125. (sort-subr reverse 'forward-line 'end-of-line)))))
  126. (global-set-key (kbd "C-; a") 'sort-lines)
  127. ;; source: http://steve.yegge.googlepages.com/my-dot-emacs-file
  128. (defun rename-file-and-buffer (new-name)
  129. "Renames both current buffer and file it's visiting to NEW-NAME."
  130. (interactive "sNew name: ")
  131. (let ((name (buffer-name))
  132. (filename (buffer-file-name)))
  133. (if (not filename)
  134. (message "Buffer '%s' is not visiting a file!" name)
  135. (if (get-buffer new-name)
  136. (message "A buffer named '%s' already exists!" new-name)
  137. (progn
  138. (rename-file name new-name 1)
  139. (rename-buffer new-name)
  140. (set-visited-file-name new-name)
  141. (set-buffer-modified-p nil))))))
  142. (global-set-key (kbd "C-; n") 'rename-file-and-buffer)
  143. ;;}}}
  144. ;;{{{ Misc
  145. (defun create-scratch-buffer nil
  146. "create a scratch buffer"
  147. (interactive)
  148. (switch-to-buffer (get-buffer-create "*scratch*"))
  149. (lisp-interaction-mode))
  150. (setq eshell-visual-commands '("wicd-curses"))
  151. (defun switch-to-minibuffer-window ()
  152. "switch to minibuffer window (if active)"
  153. (interactive)
  154. (when (active-minibuffer-window)
  155. (select-window (active-minibuffer-window))))
  156. (setq asm-comment-char ?#)
  157. (add-to-list 'auto-mode-alist '("\\.ino\\'" . c++-mode))
  158. ;;}}}
  159. ;;{{{ Specific config
  160. ;;{{{ C
  161. (add-hook 'c-mode-hook
  162. (lambda ()
  163. (setq prettify-symbols-alist
  164. '(("->" . ?→)
  165. ("!=" . ?≠)
  166. ("<=" . ?≤)
  167. (">=" . ?≥)))))
  168. (add-hook 'c-mode-hook
  169. (lambda ()
  170. (setq indent-tabs-mode t)
  171. (setq show-trailing-whitespace t)
  172. (c-set-style "linux")))
  173. (setq c-offsets-alist '((func-decl-cont . 0)))
  174. ;;}}}
  175. ;;{{{ C++
  176. (add-hook 'c++-mode-hook
  177. (lambda ()
  178. (setq prettify-symbols-alist
  179. '(("->" . ?→)
  180. ("!=" . ?≠)
  181. ("<=" . ?≤)
  182. (">=" . ?≥)))))
  183. (add-hook 'c++-mode-hook
  184. (lambda ()
  185. (setq show-trailing-whitespace t)))
  186. ;;}}}
  187. ;;{{{ elisp
  188. (add-hook 'emacs-lisp-mode-hook
  189. (lambda ()
  190. (setq prettify-symbols-alist
  191. '(("lambda" . ?λ)
  192. ("->" . ?→)
  193. ("!=" . ?≠)
  194. ("<=" . ?≤)
  195. (">=" . ?≥)))))
  196. (global-set-key (kbd "C-; c")
  197. (lambda ()
  198. (interactive)
  199. (byte-recompile-directory
  200. (file-name-directory (buffer-file-name)))))
  201. (global-set-key (kbd "C-; C")
  202. (lambda ()
  203. (interactive)
  204. (byte-recompile-file
  205. (buffer-file-name))))
  206. ;;}}}
  207. ;;{{{ prolog
  208. (setq prolog-system 'swi
  209. prolog-program-switches '((swi ("-G128M" "-T128M" "-L128M" "-O"))
  210. (t nil))
  211. prolog-electric-if-then-else-flag t)
  212. ;;}}}
  213. ;;{{{ Org
  214. (setq org-log-done t)
  215. (add-hook 'org-mode-hook
  216. (lambda()
  217. (flyspell-mode 1)
  218. (auto-fill-mode)))
  219. ;;}}}
  220. ;;{{{ Folding
  221. (use-package folding
  222. :config
  223. (add-hook 'emacs-lisp-mode-hook #'folding-mode))
  224. ;;}}}
  225. ;;{{{ Paredit
  226. (use-package paredit
  227. :init
  228. (autoload 'enable-paredit-mode "paredit"
  229. "Turn on pseudo-structural editing of Lisp code." t)
  230. (add-hook 'emacs-lisp-mode-hook #'enable-paredit-mode)
  231. (add-hook 'eval-expression-minibuffer-setup-hook #'enable-paredit-mode)
  232. (add-hook 'ielm-mode-hook #'enable-paredit-mode)
  233. (add-hook 'lisp-mode-hook #'enable-paredit-mode)
  234. (add-hook 'lisp-interaction-mode-hook #'enable-paredit-mode)
  235. (add-hook 'scheme-mode-hook #'enable-paredit-mode)
  236. (add-hook 'racket-mode-hook #'enable-paredit-mode)
  237. (global-set-key (kbd "C-; k") 'delete-backward-char))
  238. ;;}}}
  239. ;;{{{ Ido
  240. (use-package ido
  241. :init
  242. (ido-mode t))
  243. ;;}}}
  244. ;;{{{ Ido vetical mode
  245. (use-package ido-vertical-mode
  246. :config
  247. (ido-vertical-mode 1)
  248. (setq ido-vertical-define-keys 'C-n-and-C-p-only))
  249. ;;}}}
  250. ;;{{{ Ido yes or no
  251. (use-package ido-yes-or-no
  252. :init
  253. (ido-yes-or-no-mode))
  254. ;;}}}
  255. ;;{{{ Smex
  256. (use-package smex
  257. :init
  258. (smex-initialize)
  259. (global-set-key (kbd "M-x") 'smex))
  260. ;;}}}
  261. ;;{{{ Mode line customization
  262. ; See https://emacs-fu.blogspot.com/2011/08/customizing-mode-line.html for more
  263. ; https://emacs.stackexchange.com/questions/26222/show-something-in-active-mode-line-instead-of-all-mode-lines as well
  264. (defvar ml-selected-window nil)
  265. (defun ml-record-selected-window ()
  266. (setq ml-selected-window (selected-window)))
  267. (defun ml-update-all ()
  268. (force-mode-line-update t))
  269. (add-hook 'post-command-hook 'ml-record-selected-window)
  270. (add-hook 'buffer-list-update-hook 'ml-update-all)
  271. (setq-default mode-line-format
  272. (list
  273. ;; the buffer name; the file name as a tool tip
  274. '(:eval
  275. (propertize "%b " 'help-echo (buffer-file-name)
  276. 'face '(:weight semi-bold)))
  277. ;; the current major mode for the buffer.
  278. "["
  279. '(:eval
  280. (propertize "%m" 'help-echo buffer-file-coding-system))
  281. "] "
  282. ;; relative position, size of file
  283. "["
  284. (propertize "%p")
  285. "/"
  286. (propertize "%I")
  287. "] "
  288. "[" ;; insert vs overwrite mode, input-method in a tooltip
  289. '(:eval (propertize
  290. (if overwrite-mode "Ovr" "Ins")
  291. 'help-echo
  292. (concat "Buffer is in "
  293. (if overwrite-mode "overwrite" "insert") " mode")))
  294. ;; was this buffer modified since the last save?
  295. '(:eval (when (buffer-modified-p)
  296. (concat "," (propertize
  297. "Mod"
  298. 'help-echo "Buffer has been modified"))))
  299. ;; is this buffer read-only?
  300. '(:eval (when buffer-read-only
  301. (concat "," (propertize "RO"
  302. 'help-echo "Buffer is read-only"))))
  303. "] "
  304. ;; line and column
  305. "(" ;; '%02' to set to 2 chars at least; prevents flickering
  306. (propertize "%02l")
  307. ","
  308. (propertize "%02c")
  309. ") "
  310. ;; add the time, with the date and the emacs uptime in the tooltip
  311. '(:eval (propertize (format-time-string "%H:%M ")))
  312. '(:eval (if (eq ml-selected-window (selected-window))
  313. (propertize "☰☰☰"
  314. 'face '(:foreground "SkyBlue3" :weight))
  315. (propertize "☷☷☷"
  316. 'face '(:foreground "DodgerBlue4"))))))
  317. ;;}}}
  318. ;;}}}
  319. (put 'downcase-region 'disabled nil)
  320. (put 'upcase-region 'disabled nil)
  321. (custom-set-variables
  322. ;; custom-set-variables was added by Custom.
  323. ;; If you edit it by hand, you could mess it up, so be careful.
  324. ;; Your init file should contain only one such instance.
  325. ;; If there is more than one, they won't work right.
  326. '(package-selected-packages
  327. (quote
  328. (ido-yes-or-no sql-indent ido-vertical-mode smex magit paredit use-package zenburn-theme))))
  329. (custom-set-faces
  330. ;; custom-set-faces was added by Custom.
  331. ;; If you edit it by hand, you could mess it up, so be careful.
  332. ;; Your init file should contain only one such instance.
  333. ;; If there is more than one, they won't work right.
  334. )