;;; -*- lexical-binding: t -*- ;;; ;;; this file: ;;; http://user.it.uu.se/~embe8573/emacs-init/dired-my.el ;;; https://dataswamp.org/~incal/emacs-init/dired-my.el (require 'dired) (require 'files-my) (require 'scroll) (require 'sudo-user-path) (require 'super) (require 'time-my) (require 'w3m-my) (setq dired-auto-revert-buffer #'dired-directory-changed-p) (setq completion-ignored-extensions (nconc completion-ignored-extensions '(".bcf" ".elc" ".run.xml")) ) ;; ls (setq dired-listing-switches "-AGlX --group-directories-first -I \"*.meta\" -I \"#*#\" -I \"*.elc\"") (defun dired-ls-toggle (switch) (if (string-match-p switch dired-listing-switches) (setq dired-listing-switches (replace-regexp-in-string switch "" dired-listing-switches) ) (setq dired-listing-switches (format "%s %s" dired-listing-switches switch) )) (dired-sort-other dired-listing-switches) ) (defun dired-toggle-human () (interactive) (dired-ls-toggle "-h") ) (defun dired-toggle-modification-sort () (interactive) (dired-ls-toggle "-t") ) ;; delete (setq dired-recursive-deletes 'always) (setq dired-deletion-confirmer (lambda (_) t)) ; no confirm ;; operate file (defun dired-toggle-mark () (interactive) (if (= dired-marker-char (char-after (point-at-bol))) (dired-unmark 1) (dired-mark 1) )) ;; move point (defun dired-first-file () (interactive) (goto-char (point-min)) (dired-next-line 2) ) (defun dired-last-file () (interactive) (goto-char (point-max)) (dired-previous-line 1) ) ;; kill path (defun echo-kill () (message (car kill-ring)) ) (defun dired-kill-path-dwim () (interactive) (kill-new (or (dired-get-filename nil t) ; kill file including path, (dired-current-directory) )) ; or dir path if file u/a (echo-kill) ) (defun dired-kill-file-name () (interactive) (kill-new (dired-get-filename 'no-dir)) (message (car kill-ring)) (echo-kill) ) ;; touch (defun dired-invoke-touch (filename) "Invoke touch\\(1\\) in the dired directory, without considering the currently selected file." (interactive "s touch: ") (shell-command (format "touch %s/%s" dired-directory filename)) (revert-buffer) ) (defun dired-touch-time-stamp-text-file () (interactive) (dired-invoke-touch (format "%s.txt" (get-date)))) ;; find file (defun find-whatever-file (&optional file) (interactive) (let*((the-file (or file (thing-at-point 'filename))) (true-file (file-truename the-file)) (sudo-prefix "/sudo:") (already-sudo (string-prefix-p sudo-prefix true-file)) ) (if already-sudo (find-file true-file) (let*((home-dir (getenv "HOME")) (in-home (string-prefix-p home-dir true-file)) (final-file (if in-home the-file (sudo-path-no-user true-file))) ) (find-file final-file) )))) (defun dired-find-file-chase-symlink () "Find the dired selected file, chase it if it is a symlink." (interactive) (find-whatever-file (file-truename (dired-get-file-for-visit))) ) ;; more (defun replace-regexp-in-marked-files (match-regexp replace-string) (interactive "sMatch regexp: \nsReplace string: ") (save-window-excursion (let ((case-fold-search t)) (dolist (f (dired-get-marked-files)) (find-file f) (while (re-search-forward match-regexp nil t) ; BOUND NOERROR (replace-match replace-string nil nil)) ; FIXEDCASE LITERAL (save-buffer) ))) (dired-unmark-all-marks) ) ;; keys (defun dired-init-keys () (let ((the-map dired-mode-map)) (disable-super-global-keys the-map) ;; disable (define-key the-map "D" nil) ;; new file/directory (define-key the-map "E" #'dired-touch-time-stamp-text-file) (define-key the-map "m" #'dired-create-directory) (define-key the-map "t" #'dired-invoke-touch) ;; kill (define-key the-map "S" #'dired-kill-file-name) (define-key the-map "s" #'dired-kill-path-dwim) ;; file (define-key the-map "\r" #'dired-find-file-chase-symlink) (define-key the-map "c" #'dired-do-copy) (define-key the-map "r" #'dired-do-rename) (define-key the-map "U" #'dired-toggle-mark) (define-key the-map "W" #'dired-view-in-w3m) ;; first/last file (define-key the-map "\C-oi" #'dired-first-file) (define-key the-map "\C-ok" #'dired-last-file) ;; prev/next line (define-key the-map [backtab] #'dired-previous-line) (define-key the-map "\t" #'dired-next-line) (define-key the-map "i" #'dired-previous-line) (define-key the-map "k" #'dired-next-line) ;; directory (define-key the-map "b" #'dired-up-directory) (define-key the-map "w" #'dired-up-directory) ;; ls (define-key the-map "h" #'dired-toggle-human) (define-key the-map "l" #'dired-toggle-modification-sort) )) (dired-init-keys) (provide 'dired-my)