al-dired.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ;;; al-dired.el --- Additional functionality for dired
  2. ;; Copyright © 2012–2018 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'dired)
  17. (require 'dired-x)
  18. (require 'al-process)
  19. (defun al/dired-start-process (program &optional args)
  20. "Open current file with a PROGRAM."
  21. ;; Shell command looks like this: "program [ARGS]... FILE" (ARGS can
  22. ;; be nil, so remove it).
  23. (apply #'al/start-process
  24. program
  25. (remove nil (list args (dired-get-file-for-visit)))))
  26. (defun al/dired-start-process-on-marked-files (program &optional args)
  27. "Open marked files with a PROGRAM."
  28. (apply #'al/start-process
  29. program
  30. (remove nil (append args (dired-get-marked-files)))))
  31. ;; Moving to the first/last files - from <http://whattheemacsd.com/>.
  32. (defun al/dired-beginning-of-buffer ()
  33. "Move point to the first file."
  34. (interactive)
  35. (goto-char (point-min))
  36. (dired-next-line 4))
  37. (defun al/dired-end-of-buffer ()
  38. "Move point to the last file."
  39. (interactive)
  40. (goto-char (point-max))
  41. (dired-next-line -1))
  42. (defun al/dired-find-file (&optional arg)
  43. "In Dired, visit the file or directory named on this line.
  44. If ARG is non-nil, visit it with `find-file-literally'."
  45. (interactive "P")
  46. (if arg
  47. (find-file-literally (dired-get-file-for-visit))
  48. (dired-find-file)))
  49. (defun al/dired-get-size (&optional arg)
  50. "Show size of all marked files in dired mode.
  51. If ARG is non-nil, do not use human readable format (size in bytes)."
  52. (interactive "P")
  53. (let ((args (concat "-sc"
  54. (if arg "b" "h")))
  55. (files (dired-get-marked-files)))
  56. (with-temp-buffer
  57. (apply #'call-process "du" nil t nil args files)
  58. (message "Size of all marked files: %s"
  59. (progn
  60. (re-search-backward "\\(^.+\\)[[:blank:]]*total$")
  61. (match-string 1))))))
  62. (defun al/dired-stat (&optional arg)
  63. "Call `stat' program on marked files in dired mode.
  64. With prefix (if ARG is non-nil), use the next ARG files instead."
  65. (interactive "P")
  66. (dired-do-shell-command
  67. "stat" nil
  68. (dired-get-marked-files t arg)))
  69. (defun al/dired-copy-filename-as-kill (&optional arg)
  70. "Copy names of marked (or next ARG) files into the kill ring.
  71. This function is similar to `dired-copy-filename-as-kill',
  72. except it quotes file names for a shell."
  73. (interactive "P")
  74. (let ((string (mapconcat
  75. #'shell-quote-argument
  76. (if arg
  77. (cond ((zerop (prefix-numeric-value arg))
  78. (dired-get-marked-files))
  79. ((consp arg)
  80. (dired-get-marked-files t))
  81. (t
  82. (dired-get-marked-files
  83. 'no-dir (prefix-numeric-value arg))))
  84. (dired-get-marked-files 'no-dir))
  85. " ")))
  86. (kill-new string)
  87. (message "%s" string)))
  88. (defun al/man-file-p (file)
  89. "Return non-nil, if FILE is a Man-file."
  90. ;; Is there a more reliable way?
  91. (string-match-p (rx "." (any "0-8") (? ".gz") string-end)
  92. file))
  93. (defun al/dired-man-or-chmod ()
  94. "Perform `dired-man' on a Man-file or `dired-do-chmod' otherwise."
  95. (interactive)
  96. (let ((file (dired-get-filename)))
  97. (if (al/man-file-p file)
  98. (dired-man)
  99. (dired-do-chmod))))
  100. (declare-function image-dired-backward-image
  101. "image-dired" (&optional arg))
  102. (declare-function image-dired-modify-mark-on-thumb-original-file
  103. "image-dired" (command))
  104. (defun al/image-dired-unmark-thumb-original-file-backward ()
  105. "Move up and unmark original image file in associated dired buffer."
  106. (interactive)
  107. (image-dired-backward-image)
  108. (image-dired-modify-mark-on-thumb-original-file 'unmark))
  109. (defvar al/mode-info)
  110. (defun al/dired-sort-set-mode-line ()
  111. "Replacement for `dired-sort-set-mode-line'."
  112. (when (eq major-mode 'dired-mode)
  113. (setq al/mode-info
  114. (let (case-fold-search)
  115. (cond ((string-match-p
  116. dired-sort-by-name-regexp dired-actual-switches)
  117. "name")
  118. ((string-match-p
  119. dired-sort-by-date-regexp dired-actual-switches)
  120. "date")
  121. (t dired-actual-switches))))))
  122. (provide 'al-dired)
  123. ;;; al-dired.el ends here