setup-general.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; Code:
  2. (require 'autorevert)
  3. (require 'epa-file)
  4. (require 'savehist)
  5. (require 'recentf)
  6. (require 'saveplace)
  7. (require 'nsm)
  8. (require 'comp)
  9. (require 'async)
  10. (require 'async-bytecomp)
  11. (require 'browse-kill-ring)
  12. (defcustom user-short-name ""
  13. "A short user name."
  14. :type 'string
  15. :group 'distopico)
  16. (defcustom distopico:after-init-load-hook nil
  17. "Hook called after the `init.el' load all required dependencies."
  18. :type 'list
  19. :group 'distopico)
  20. ;; Require misc stuff
  21. (defvar distopico:backup-dir (in-emacs-d ".cache/backup/")
  22. "Place backups in `~/.emacs.d/.cache/backups/' directory.")
  23. (defvar distopico:auto-save-dir (in-emacs-d ".cache/auto-save-list/")
  24. "Place auto-save files in `~/.cache/auto-save-list/' directory.")
  25. (defvar distopico:swapping-buffer nil
  26. "Control of swapping buffers.")
  27. (defvar distopico:swapping-window nil
  28. "Control of swapping window.")
  29. ;; Basic user data
  30. (setq user-full-name "Distopico"
  31. user-mail-address "distopico@riseup.net"
  32. user-short-name "distopico")
  33. ;; Save custom variables in another file
  34. (setq custom-file (in-emacs-d "custom.el"))
  35. (load custom-file 'noerror)
  36. ;; Keep cache
  37. ;; TODO: remove this and use the default (disabled)
  38. (setq epa-file-cache-passphrase-for-symmetric-encryption t)
  39. ;; Network
  40. (setq nsm-settings-file (in-emacs-d ".cache/network-security.data"))
  41. (when (boundp 'request-storage-directory)
  42. (setq request-storage-directory (in-emacs-d ".cache/request")))
  43. ;; Create directory if no exist
  44. (when (and (not (file-directory-p distopico:backup-dir)))
  45. (make-directory distopico:backup-dir t))
  46. ;; Defined backup/auto-save directories
  47. (setq backup-directory-alist (list (cons ".*" distopico:backup-dir))
  48. auto-save-list-file-prefix (concat distopico:auto-save-dir ".saves-")
  49. auto-save-no-message t)
  50. ;; Native compile cache
  51. (when (functionp 'startup-redirect-eln-cache)
  52. (startup-redirect-eln-cache (in-emacs-d ".cache/eln-cache")))
  53. ;; Save commands history
  54. (setq savehist-file (in-emacs-d ".cache/history"))
  55. (savehist-mode +1)
  56. ;; Recent files and places
  57. (setq recentf-auto-cleanup 300
  58. recentf-max-saved-items 300
  59. recentf-exclude
  60. (append recentf-exclude
  61. '("\\.emacs.d/el-get/" "~$" "\\.autosaves/"
  62. "\\.emacs.d/elpa/" "/.emacs.bmk$"
  63. "\\.ido.last" "session\\.[a-f0-9]*$"
  64. "\\.recentf" "/.emacs.d/TAGS"
  65. "\\.cache/" "\\.mail/"
  66. "\\.gz"))
  67. save-place-forget-unreadable-files nil ; no checks every loaded file is readable
  68. save-place-file (in-emacs-d ".cache/places"))
  69. (recentf-mode +1)
  70. (save-place-mode +1)
  71. (run-with-idle-timer (* 10 60) t 'recentf-save-list)
  72. ;; Clean up the backups
  73. (setq backup-by-copying t
  74. delete-old-versions t
  75. version-control t
  76. kept-new-versions 3
  77. kept-old-versions 2)
  78. ;; Initialize emacs server mode
  79. ;; (server-start)
  80. ;; Verify UTF-8
  81. (setq locale-coding-system 'utf-8
  82. x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
  83. (set-terminal-coding-system 'utf-8)
  84. (set-keyboard-coding-system 'utf-8)
  85. (set-selection-coding-system 'utf-8)
  86. (prefer-coding-system 'utf-8)
  87. ;; Allow recursive minibuffers
  88. ;; TODO: check if this is useful
  89. (setq enable-recursive-minibuffers nil)
  90. (minibuffer-depth-indicate-mode -1)
  91. ;; Transparently open compressed files
  92. (auto-compression-mode t)
  93. ;; Move files to trash when deleting
  94. (setq delete-by-moving-to-trash t)
  95. ;; Auto refresh buffers
  96. (setq global-auto-revert-non-file-buffers t
  97. auto-revert-verbose nil)
  98. (global-auto-revert-mode t)
  99. ;; Silent native compilation warnings
  100. (setq native-comp-async-report-warnings-errors 'silent)
  101. ;; ignore byte-compile warnings
  102. (setq byte-compile-warnings '(not nresolved
  103. free-vars
  104. callargs
  105. redefine
  106. obsolete
  107. noruntime
  108. cl-functions
  109. interactive-only))
  110. ;; Enable async byte-compile
  111. (async-bytecomp-package-mode t)
  112. ;; Browse kill ring
  113. (setq browse-kill-ring-highlight-current-entry t
  114. browse-kill-ring-show-preview t)
  115. ;; Custom functions
  116. (defun distopico:shutdown ()
  117. "Shutdown Emacs server instance.
  118. save buffers, Quit, and Shutdown (kill) server"
  119. (interactive)
  120. (save-some-buffers)
  121. (call-interactively #'kill-emacs))
  122. (defun distopico:delete-old-backups ()
  123. "Delete backups files older than a week."
  124. (interactive)
  125. (if (file-directory-p distopico:backup-dir)
  126. (message "Deleting old backup files...")
  127. (let ((week (* 60 60 24 7))
  128. (current (float-time (current-time))))
  129. (dolist (file (directory-files distopico:backup-dir t))
  130. (when (and (backup-file-name-p file)
  131. (> (- current (float-time (cl-fifth (file-attributes file))))
  132. week))
  133. (message "%s" file)
  134. (delete-file file))))))
  135. (defun distopico:warn-if-symlink ()
  136. "Progn here to execute both as part of else statement together."
  137. (if (file-symlink-p buffer-file-name)
  138. (message "File is a symlink")))
  139. ;; Advice functions
  140. (advice-add #'recentf-save-list :around 'distopico:inhibit-outputs-advisor)
  141. (advice-add #'recentf-cleanup :around 'distopico:inhibit-outputs-advisor)
  142. ;; Hooks
  143. ;; (add-hook 'before-save-hook 'delete-trailing-whitespace)
  144. (add-hook 'find-file-hook 'distopico:warn-if-symlink)
  145. (add-hook 'emacs-lisp-mode-hook 'byte-compile-when-save)
  146. ;; Prevent show my password when I'm entering.
  147. (add-hook 'comint-output-filter-functions
  148. 'comint-watch-for-password-prompt)
  149. (provide 'setup-general)