files.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. ;;; files.el --- Working with files; dired, sunrise-commander, …
  2. ;; Copyright © 2014–2018 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. (require 'al-key)
  16. ;;; Global keys
  17. (al/bind-key* "M-C-f" find-file-at-point)
  18. (al/bind-keys*
  19. :prefix-map al/find-file-map
  20. :prefix-docstring "Map for finding files."
  21. :prefix "C-f"
  22. ("C-f" . find-file)
  23. ("p" . al/find-file-in-path)
  24. ("S" . al/sudo-find-file)
  25. ("h" . al/ssh-find-file)
  26. ("z" . al/router-get-log)
  27. ("u" . browse-url-emacs)
  28. ("l" . find-library)
  29. ("e" (al/find-file al/emacs-dir))
  30. ("C-c" (al/find-file al/emacs-init-dir))
  31. ("C-s" (find-file (al/emacs-init-dir-file "settings.el")))
  32. ("k" (find-file (al/emacs-init-dir-file "keys.el")))
  33. ("i" (find-file (al/emacs-init-dir-file "init.el")))
  34. ("t" (find-file (al/emacs-init-dir-file "text.el")))
  35. ("v" (find-file (al/emacs-init-dir-file "visual.el")))
  36. ("c" (al/find-file (al/emacs-my-packages-dir-file "alect-themes")))
  37. ("C-M-c" (find-file (al/emacs-my-packages-dir-file
  38. "alect-themes/alect-themes.el"))))
  39. (al/bind-keys
  40. :prefix-map al/bookmark-map
  41. :prefix-docstring "Map for bookmarks and finding files."
  42. :prefix "M-f"
  43. ("M-f" . bookmark-jump)
  44. ("n" . bookmark-set)
  45. ("k" . bookmark-delete)
  46. ("l" . bookmark-bmenu-list)
  47. ("S" . al/sr-toggle)
  48. ("q" (al/find-file
  49. (al/src-dir-file "emacs/melpa/recipes")))
  50. ("h" (al/find-file "~"))
  51. ("d" (al/find-file al/journal-dir))
  52. ("w" (al/find-file al/download-dir))
  53. ("M-d" (find-file al/download-dir))
  54. ("e" (find-file al/echo-download-dir))
  55. ("M-n" (al/find-file al/notes-dir))
  56. ("t" (al/find-file al/tmp-dir))
  57. ("m" (al/find-file al/music-dir))
  58. ("p" (al/find-file al/progs-dir))
  59. ("b" (al/find-file (al/config-dir-file "shell")))
  60. ("g" (al/find-file (al/config-dir-file "guile")))
  61. ("M-c" (al/find-file al/config-dir))
  62. ("C-M-c" (find-file (al/config-dir-file "config.scm")))
  63. ("M-g" (al/find-file al/guix-profile-dir))
  64. ("c" (al/find-file (al/config-dir-file "conkeror")))
  65. ("s" (al/find-file (al/config-dir-file "stumpwm")))
  66. ("v" (al/find-file "/var/log")))
  67. (al/bind-keys
  68. :prefix-map al/grep-find-map
  69. :prefix-docstring "Map for find/grep commands."
  70. :prefix "M-F"
  71. ("g" . grep)
  72. ("n" . find-name-dired)
  73. ("a" . find-dired)
  74. ("f" . grep-find))
  75. ;;; Backup and autosave
  76. (let ((dir (al/emacs-data-dir-file "auto-save")))
  77. ;; Emacs does not create a directory of an autosave file and just
  78. ;; complains when it doesn't exist.
  79. (unless (file-exists-p dir)
  80. (with-demoted-errors "ERROR during making auto-save directory: %S"
  81. (mkdir dir t)))
  82. (setq auto-save-file-name-transforms
  83. `((".*" ,(file-name-as-directory dir) t))))
  84. (setq
  85. auto-save-list-file-prefix
  86. (al/emacs-data-dir-file "auto-save-list/.saves-")
  87. backup-directory-alist
  88. `( ;;(,tramp-file-name-regexp . nil)
  89. (".*" . ,(al/emacs-data-dir-file "backup")))
  90. backup-by-copying t ; overwrite backups, not originals files
  91. version-control t
  92. kept-old-versions 2
  93. kept-new-versions 4
  94. delete-old-versions t
  95. vc-make-backup-files t)
  96. (when (require 'al-backup nil t)
  97. (setq backup-enable-predicate 'al/backup-enable-predicate)
  98. (advice-add 'make-backup-file-name-1
  99. :override 'al/make-backup-file-name-1))
  100. ;;; Dired
  101. (with-eval-after-load 'dired
  102. (setq
  103. dired-auto-revert-buffer 'dired-directory-changed-p
  104. dired-dwim-target t
  105. dired-listing-switches "-alvDh --group-directories-first"
  106. ;; Do not ask about copying/deleting directories.
  107. dired-recursive-copies 'always
  108. dired-recursive-deletes 'always)
  109. (defconst al/dired-keys
  110. '("c" "M-$"
  111. ("SPC" . al/dired-get-size)
  112. ("N" . dired-create-directory)
  113. ("M" . al/dired-man-or-chmod)
  114. ("f" . dired-show-file-type)
  115. ("F" . al/dired-stat)
  116. ("o" . dired-up-directory)
  117. ("u" . al/dired-find-file)
  118. ("U" . dired-do-find-marked-files)
  119. ("." . dired-previous-line)
  120. ("e" . dired-next-line)
  121. (">" . dired-prev-dirline)
  122. ("E" . dired-next-dirline)
  123. ("C-M-." . dired-prev-dirline)
  124. ("C-M-e" . dired-next-dirline)
  125. ("H-a" . al/dired-beginning-of-buffer)
  126. ("H-i" . al/dired-end-of-buffer)
  127. ("M-d" . dired-toggle-read-only)
  128. ("p" . pathify-dired)
  129. ("t" . image-dired-display-thumbs)
  130. ("T" . dired-do-touch)
  131. ("z" . dired-unmark)
  132. ("Z" . dired-unmark-all-marks)
  133. ("b" (dired-mark-extension '("elc" "go")))
  134. ("d" . dired-display-file)
  135. ("C-d" . dired-find-file-other-window)
  136. ("C-l" . dired-omit-mode)
  137. ("c 0" . al/default-directory-to-kill-ring)
  138. ("c RET" (al/dired-copy-filename-as-kill 0))
  139. ("r" . dired-do-query-replace-regexp)
  140. ("<ctrl-m> p" . emms-play-dired)
  141. ("<ctrl-m> a" . emms-add-dired))
  142. "Alist of auxiliary keys for `dired-mode'.")
  143. (al/bind-keys-from-vars 'dired-mode-map 'al/dired-keys)
  144. (al/bind-keys
  145. :map dired-mode-map
  146. :prefix-map al/dired-isearch-map
  147. :prefix-docstring "Map for isearch in dired."
  148. :prefix "M-s"
  149. ("s" . dired-do-isearch)
  150. ("r" . dired-do-isearch-regexp)
  151. ("f" . dired-isearch-filenames-regexp)
  152. ("F" . dired-isearch-filenames))
  153. (al/bind-keys
  154. :map dired-mode-map
  155. :prefix-map al/dired-open-file-map
  156. :prefix-docstring "Map for opening files in external programs in dired."
  157. :prefix "C-j"
  158. ("M-j" (al/dired-start-process "xdg-open"))
  159. ("C-j" . al/dired-open-file)
  160. ("v d" (al/dired-start-process "baobab"))
  161. ("v f" (al/dired-start-process "gdmap" "-f"))
  162. ("m" (al/dired-start-process "mupdf"))
  163. ("z" (al/dired-start-process "zathura"))
  164. ("s" (al/dired-start-process-on-marked-files "sxiv"))
  165. ("c" (al/browse-url-conkeror
  166. (browse-url-file-url (dired-get-filename))))
  167. ("w" (w3m-browse-url
  168. (browse-url-file-url (dired-get-filename)))))
  169. (al/add-hook-maybe 'dired-mode-hook 'hl-line-mode)
  170. (when (require 'al-mode-line nil t)
  171. (al/mode-line-default-buffer-identification 'dired-mode))
  172. (require 'dired-x nil t)
  173. (when (require 'al-dired nil t)
  174. (advice-add 'dired-sort-set-mode-line
  175. :override 'al/dired-sort-set-mode-line)))
  176. (setq
  177. dired-guess-shell-gnutar "tar"
  178. dired-bind-jump nil
  179. dired-bind-man nil)
  180. (al/bind-key "H-j" dired-jump)
  181. (al/autoload "dired-x" dired-jump)
  182. (with-eval-after-load 'dired-x
  183. (setq
  184. ;; Do not show "hidden" files only.
  185. dired-omit-files "^\\..*"
  186. dired-omit-extensions nil)
  187. ;; Do not rebind my keys!!
  188. (al/bind-keys-from-vars 'dired-mode-map 'al/dired-keys t)
  189. (when (require 'al-file nil t)
  190. (setq dired-guess-shell-alist-user
  191. `((,(al/file-regexp "jpg" "png" "gif") "sxiv" "eog")
  192. (,(al/file-regexp "tif" "tiff") "sxiv" "evince" "eog")
  193. (,(al/file-regexp "pdf") "zathura" "mupdf")
  194. (,(al/file-regexp "djvu" "djv") "zathura")
  195. (,(al/file-regexp "wav" "oga" "ogg")
  196. "play -q" "aplay" "mplayer -really-quiet" "mpv --really-quiet")
  197. (,(al/file-regexp "odt" "doc") "lowriter")))))
  198. (with-eval-after-load 'wdired
  199. (al/bind-keys-from-vars 'wdired-mode-map)
  200. (when (require 'dim nil t)
  201. ;; "Dired" `mode-name' is hard-coded in
  202. ;; `wdired-change-to-dired-mode'.
  203. (advice-add 'wdired-change-to-dired-mode
  204. :after #'dim-set-major-name)))
  205. (with-eval-after-load 'image-dired
  206. (al/bind-keys
  207. :map image-dired-thumbnail-mode-map
  208. ("." . image-dired-backward-image)
  209. ("e" . image-dired-forward-image)
  210. ("C-." . image-dired-previous-line)
  211. ("C-e" . image-dired-next-line)
  212. ("o" . image-dired-display-previous-thumbnail-original)
  213. ("u" . image-dired-display-next-thumbnail-original)
  214. ("C-M-m" . image-dired-unmark-thumb-original-file)
  215. ("DEL" . al/image-dired-unmark-thumb-original-file-backward)))
  216. ;;; Misc settings and packages
  217. (al/with-check
  218. :var 'completion-ignored-extensions
  219. (push ".go" completion-ignored-extensions))
  220. (setq
  221. directory-free-space-args "-Ph"
  222. grep-command "grep -nHi -e "
  223. enable-local-variables :safe
  224. enable-dir-local-variables nil
  225. ;; safe-local-variable-values '((lexical-binding . t))
  226. ;; enable-local-eval nil
  227. )
  228. (with-eval-after-load 'mailcap
  229. ;; Use "sxiv" instead of "display" to open image files. Actually,
  230. ;; (mailcap-add "image/.*" "sxiv %s") can be used, but it adds the
  231. ;; entry to the beginning of "image" alist, while I want to fallback
  232. ;; to "sxiv" as it is done with "display".
  233. (let* ((image-alist (cdr (assoc "image" mailcap-mime-data)))
  234. (display-alist (cdr (assoc ".*" image-alist))))
  235. (setcdr (assq 'viewer display-alist) "sxiv %s")))
  236. (with-eval-after-load "mule-cmds" ; there is no `mule-cmds' feature
  237. (defconst al/mule-keys
  238. '(("d" (revert-buffer-with-coding-system 'cp855))
  239. ("w" (revert-buffer-with-coding-system 'cp1251)))
  240. "Alist of auxiliary keys for `mule-keymap'.")
  241. (al/bind-keys-from-vars 'mule-keymap 'al/mule-keys))
  242. (with-eval-after-load 'bookmark
  243. (setq
  244. bookmark-save-flag 1
  245. bookmark-default-file (al/emacs-data-dir-file "bookmarks"))
  246. (defconst al/bookmark-keys
  247. '(("u" . bookmark-bmenu-relocate)
  248. ("d" . bookmark-bmenu-other-window)
  249. ("C-d" . bookmark-bmenu-switch-other-window)
  250. ("R" . bookmark-bmenu-rename)
  251. ("z" . bookmark-bmenu-unmark)
  252. ("D" . bookmark-bmenu-delete)
  253. ("M-d" . bookmark-bmenu-edit-annotation))
  254. "Alist of auxiliary keys for `bookmark-bmenu-mode'.")
  255. (al/bind-keys-from-vars 'bookmark-bmenu-mode-map
  256. '(al/lazy-moving-keys al/bookmark-keys)))
  257. (with-eval-after-load 'recentf
  258. (setq
  259. recentf-keep nil
  260. recentf-auto-cleanup 'never
  261. recentf-max-saved-items 99
  262. recentf-save-file (al/emacs-data-dir-file "recentf")))
  263. (with-eval-after-load 'saveplace
  264. (setq
  265. save-place-ignore-files-regexp
  266. (rx-to-string `(or (and string-start "/gnu")
  267. (regexp ,save-place-ignore-files-regexp))
  268. 'no-group)
  269. save-place-file (al/emacs-data-dir-file "save-places")
  270. save-place-limit 999)
  271. (remove-hook 'dired-initial-position-hook #'save-place-dired-hook)
  272. (when (require 'al-saveplace nil t)
  273. (advice-add 'save-places-to-alist
  274. :override #'al/save-places-to-alist)))
  275. (al/add-after-init-hook 'save-place-mode)
  276. (with-eval-after-load 'al-file-cmd
  277. (setq
  278. al/ssh-default-user (list user-login-name "root" "lena")
  279. al/ssh-default-host "hyperion"))
  280. (with-eval-after-load 'sunrise-commander
  281. (setq
  282. sr-listing-switches "-alh --group-directories-first --no-group"
  283. sr-show-hidden-files nil
  284. sr-confirm-kill-viewer nil
  285. sr-modeline-use-utf8-marks t)
  286. ;; Do not block windows resizing with `sr-lock-window'.
  287. (remove-hook 'window-size-change-functions 'sr-lock-window)
  288. (defconst al/sr-keys
  289. '(("i" . sr-show-files-info)
  290. ("o" . sr-dired-prev-subdir)
  291. ("u" . sr-advertised-find-file)
  292. ("M-u" . sr-advertised-find-file-other)
  293. ("," . sr-history-prev)
  294. ("p" . sr-history-next)
  295. ("y" . sr-synchronize-panes)
  296. ("H-a" . sr-beginning-of-buffer)
  297. ("H-i" . sr-end-of-buffer)
  298. ("V" (sr-quick-view t)))
  299. "Alist of auxiliary keys for `sr-mode-map'.")
  300. (al/bind-keys-from-vars 'sr-mode-map 'al/sr-keys))
  301. ;;; files.el ends here