Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This extension gives you a org-mode like buffer menu in dired in order to sort files and directories.
;;; dired-sort-menu.el -- makes sorting with dired easy
;; Filename: dired-sort-menu.el
;; Description: Extension to Dired
;; Author: Florian Adamsky
;; Maintainer: Florian Adamsky (concat "fa-emacs" "@" "haktar" ".org")
;; Licence: Beer-ware (Revision 42)
;; As long as you retain this notice you can do whatever
;; you want with this stuff. If we meet some day, and you
;; think this stuff is worth it, you can buy me a beer in
;; return
;; Created: 07.08.2013 22:49:45
;; Version: 0.1
;; Comment:
;;
;; I recommand to bind the function dired-sort-menu with the following
;; code to the s key in dired:
;; (define-key dired-mode-map (kbd "s") 'dired-sort-menu)
(defvar dired-sort-menu-option ""
"This variable contains additional options for dired-sort-menu")
(defun dired-sort-by-options (option)
"This function takes an option as parameter and concatenates it
with the variable 'dired-sort-menu-option'. This new value is the
parameter for the 'ls' command."
(dired-sort-other (concat (concat dired-listing-switches
option) dired-sort-menu-option)))
(defun dired-sort-menu (&optional options)
"This function opens a new buffer and provides a list of
different sorting options in dired"
(interactive)
(setq dired-listing-switches "-lXGh")
(save-window-excursion
(split-window-vertically)
(switch-to-buffer-other-window "*Dired Sort Menu*")
(erase-buffer)
(insert "Press key for an sorting command:
--------------------------------
")
(insert (format "[r] Reverse order: %s\n\n"
(if (memq 'reverse options) "On " "Off")))
(insert "[s] sort by size [a] sort by access time
[x] sort by extension [t] sort by time
[u] sort by create time (utime) [n] sort by name
[q] quit")
(shrink-window-if-larger-than-buffer)
(goto-line 0)
;; ask for selection
(message "Press key for sort command:")
(setq selection (char-to-string (read-char-exclusive))))
(let ((args (if (memq 'reverse options) "r" "")))
(cond
((string= selection "s") (dired-sort-by-options (concat args "S")))
((string= selection "x") (dired-sort-by-options (concat args "X")))
((string= selection "u") (dired-sort-by-options (concat args "ct")))
((string= selection "a") (dired-sort-by-options (concat args "ut")))
((string= selection "t") (dired-sort-by-options (concat args "t")))
((string= selection "r")
(if (memq 'reverse options) (dired-sort-menu) (dired-sort-menu '(reverse))))
((string= selection "n") (dired-sort-by-options args)))
(if (get-buffer "*Dired Sort Menu*")
(kill-buffer "*Dired Sort Menu*"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment