aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-dired.el
blob: 21240e111732e7205b68cd64b8c2e45b6f8503dc (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
;;; 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)))))

(provide 'my-dired)
;;; my-dired.el ends here