* .emacs.d/init.el: Add 'b/invert-default-face' as a simple light/dark theme toggle.
[~bandali/configs] / .emacs.d / lisp / bandali-dired.el
1 ;;; bandali-dired.el --- bandali's dired setup -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2018-2020 Amin Bandali
4
5 ;; Author: Amin Bandali <bandali@gnu.org>
6 ;; Keywords: files
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; My dired setup and customizations.
24
25 ;;; Code:
26
27 (with-eval-after-load 'dired
28 (csetq
29 dired-dwim-target t
30 dired-listing-switches "-alh"
31 dired-recent-directories-file (b/var "dired-recent-directories.el")
32 ls-lisp-dirs-first t
33 ls-lisp-use-insert-directory-program nil)
34
35 (declare-function dired-dwim-target-directory "dired-aux")
36 ;; easily diff 2 marked files
37 ;; https://oremacs.com/2017/03/18/dired-ediff/
38 (defun dired-ediff-files ()
39 (interactive)
40 (require 'dired-aux)
41 (defvar ediff-after-quit-hook-internal)
42 (let ((files (dired-get-marked-files))
43 (wnd (current-window-configuration)))
44 (if (<= (length files) 2)
45 (let ((file1 (car files))
46 (file2 (if (cdr files)
47 (cadr files)
48 (read-file-name
49 "file: "
50 (dired-dwim-target-directory)))))
51 (if (file-newer-than-file-p file1 file2)
52 (ediff-files file2 file1)
53 (ediff-files file1 file2))
54 (add-hook 'ediff-after-quit-hook-internal
55 (lambda ()
56 (setq ediff-after-quit-hook-internal nil)
57 (set-window-configuration wnd))))
58 (error "no more than 2 files should be marked"))))
59
60 (defun b/dired-start-process (program &optional args)
61 "Open current file with a PROGRAM."
62 ;; Shell command looks like this: "program [ARGS]... FILE" (ARGS
63 ;; can be nil, so remove it).
64 (declare-function dired-get-file-for-visit "dired")
65 (apply #'b/start-process
66 program
67 (remove nil (list args (dired-get-file-for-visit)))))
68
69 ;; local key bindings
70 (define-key dired-mode-map (kbd "b") #'dired-up-directory)
71 (define-key dired-mode-map (kbd "E") #'dired-ediff-files)
72 (define-key dired-mode-map (kbd "e") #'dired-toggle-read-only)
73 (define-key dired-mode-map (kbd "\\") #'dired-hide-details-mode)
74 (define-key dired-mode-map (kbd "z")
75 (lambda ()
76 (interactive)
77 (b/dired-start-process "zathura")))
78
79 (require 'dired-x)
80 (csetq dired-guess-shell-alist-user
81 '(("\\.pdf\\'" "evince" "zathura" "okular")
82 ("\\.doc\\'" "libreoffice")
83 ("\\.docx\\'" "libreoffice")
84 ("\\.ppt\\'" "libreoffice")
85 ("\\.pptx\\'" "libreoffice")
86 ("\\.xls\\'" "libreoffice")
87 ("\\.xlsx\\'" "libreoffice")
88 ("\\.flac\\'" "mpv"))))
89 ;; hooks
90 (add-hook 'dired-mode-hook #'dired-hide-details-mode)
91
92 (provide 'bandali-dired)
93 ;;; bandali-dired.el ends here