blob: 83607ab613a196368b86d7ad6b1769634810e2a1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
;;; my-dired.el -- Extension for dired -*- lexical-binding: t -*-
;; Copyright (C) 2023 Free Software Foundation.
;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))
;; This file is part of dotfiles.
;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; dotfiles is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
;; Public License for more details.
;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Extension for dired.
;;; Code:
(require 'my-utils)
(defvar my-dired-reverse-sorting nil)
(defun my-dired-find-or-alternate (arg)
"Find if file, alternate find if dir.
If prefix then xdg-open, dir or file."
(interactive "P")
(if arg (call-process "xdg-open" nil 0 nil (dired-get-filename nil t))
(if (file-directory-p (dired-get-filename nil t))
(dired-find-alternate-file)
(dired-find-file))))
(defun my-dired-do-rename-and-symlink-back (arg)
(interactive "P")
(dired-do-create-files 'move-and-symlink #'my-rename-and-symlink-back
"Move and symlink back" arg dired-keep-marker-rename
"Rename and symlink"))
(defun my-dired-sort-by-size ()
(interactive)
(setq dired-actual-switches
(concat dired-listing-switches " -S"
(when my-dired-reverse-sorting "r")))
(revert-buffer)
(setq mode-name "Dired by size"))
(defun my-dired-sort-by-time ()
(interactive)
(setq dired-actual-switches
(concat dired-listing-switches " -t"
(when my-dired-reverse-sorting "r")))
(revert-buffer)
(setq mode-name "Dired by time"))
(defun my-dired-sort-by-extension ()
(interactive)
(setq dired-actual-switches
;; FIXME: reverse sorting not working
(concat dired-listing-switches " -X"
(when my-dired-reverse-sorting "r")))
(revert-buffer)
(setq mode-name "Dired by extension"))
(defun my-dired-sort-by-name ()
(interactive)
(setq dired-actual-switches
(concat dired-listing-switches
(when my-dired-reverse-sorting " -r")))
(revert-buffer)
(setq mode-name "Dired by name"))
(defun my-dired-toggle-sorting (arg)
"Cycle dired sorting methods.
With a prefix arg, toggle `my-dired-reverse-sorting' instead."
(interactive "P")
(if arg
(progn
(setq my-dired-reverse-sorting
(not my-dired-reverse-sorting))
(cond ((equal mode-name "Dired by name")
(my-dired-sort-by-name))
((equal mode-name "Dired by time")
(my-dired-sort-by-time))
((equal mode-name "Dired by size")
(my-dired-sort-by-size))
((equal mode-name "Dired by extension")
(my-dired-sort-by-extension))))
(cond ((equal mode-name "Dired by name")
(my-dired-sort-by-time))
((equal mode-name "Dired by time")
(my-dired-sort-by-size))
((equal mode-name "Dired by size")
(my-dired-sort-by-extension))
((equal mode-name "Dired by extension")
(my-dired-sort-by-name)))))
(defun my-empty-xdg-trash ()
"Empty the xdg trash"
(interactive)
(let* ((xdg-data-dir
(directory-file-name
(expand-file-name "Trash"
(or (getenv "XDG_DATA_HOME")
"~/.local/share"))))
(trash-files-dir (expand-file-name "files" xdg-data-dir))
(trash-info-dir (expand-file-name "info" xdg-data-dir)))
(delete-directory trash-files-dir t)
(delete-directory trash-info-dir t)))
(defun my-dired-do-delete (delete-fun &optional arg)
"Wrapper of `dired-do-delete'.
With a prefix-arg, delete without moving to trash."
(interactive "P")
(if arg
(let ((delete-by-moving-to-trash nil))
(funcall delete-fun))
(funcall delete-fun))
)
(defun my-dired-do-rename (rename-fun &optional arg)
"Wrapper on `dired-do-rename'
With a prefix-arg, call it without using `vc-rename-file'."
(interactive "P")
(if arg
(let ((dired-vc-rename-file nil))
(funcall rename-fun))
(funcall rename-fun)))
(defun my-dired-display-file (_)
(dired-display-file))
(define-minor-mode my-dired-follow-mode
"Diplay file at point in dired after a move."
:lighter " dired-f"
:global t
:group 'dired
(if my-dired-follow-mode
(advice-add 'dired-next-line :after #'my-dired-display-file)
(advice-remove 'dired-next-line #'my-dired-display-file)))
(defun my-dired-rename-by-mtime-at-point ()
"Generates new file name based on mtime.
Format: yyyymmdd_hhmmss.ext"
(interactive)
(my-dired-rename-by-mtime (expand-file-name (dired-file-name-at-point))))
(defun my-dired-rename-by-mtime (file-name)
"Generates new file name based on mtime.
Format: yyyymmdd_hhmmss[_N].ext"
(let* ((ext (file-name-extension file-name))
(dir (file-name-directory file-name))
(new-file-name-base
(file-name-concat
dir
(concat
(format-time-string
"%Y%m%d_%H%M%S"
(file-attribute-modification-time (file-attributes file-name))))))
(new-file-name
(format "%s.%s" new-file-name-base ext))
(counter 0))
(unless (equal file-name new-file-name)
(while (file-exists-p new-file-name)
(setq counter (1+ counter)
new-file-name (format "%s_%d" new-file-name-base counter)))
(dired-rename-file file-name new-file-name nil))))
(defun my-dired-do-rename-by-mtime ()
"Generates new file name based on mtime.
Format: yyyymmdd_hhmmss_[N].ext"
(interactive)
(seq-do
'my-dired-rename-by-mtime
(dired-map-over-marks (dired-get-filename) nil)))
(provide 'my-dired)
;;; my-dired.el ends here
|