Subject: bug#22828: 25.1.50; Mark symlinks whose target matches regexp

From: Tino Calancha <[email-masked]>
Date: Sat, 27 Feb 2016 23:27:28 +0900 (JST)

dired-mark-files-regexp checks the NAME of a symbolic link.
Its useful to introduce one function which look for a match
in the TARGET of the symbolic links.
Such function could be bound to '%@'.

In GNU Emacs 25.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.24.29)
Repository revision: 25c5651951ef39f650927652dac8b4bbfccb60fa

diff --git a/lisp/dired.el b/lisp/dired.el
index 6c7445c..5f7a484 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -1493,6 +1493,7 @@ dired-mode-map
     (define-key map "%d" 'dired-flag-files-regexp)
     (define-key map "%g" 'dired-mark-files-containing-regexp)
     (define-key map "%m" 'dired-mark-files-regexp)
+    (define-key map "%@" 'dired-mark-symlinks-regexp)
     (define-key map "%r" 'dired-do-rename-regexp)
     (define-key map "%C" 'dired-do-copy-regexp)
     (define-key map "%H" 'dired-do-hardlink-regexp)
@@ -1730,6 +1731,9 @@ dired-mode-map
     (define-key map [menu-bar regexp flag]
       '(menu-item "Flag..." dired-flag-files-regexp
 		  :help "Flag files matching regexp for deletion"))
+    (define-key map [menu-bar regexp mark-sym]
+      '(menu-item "Mark Symlinks Target..." dired-mark-symlinks-regexp
+		  :help "Mark symbolic links whose target matches regexp for future operations"))
     (define-key map [menu-bar regexp mark]
       '(menu-item "Mark..." dired-mark-files-regexp
 		  :help "Mark files matching regexp for future operations"))
@@ -3345,6 +3349,31 @@ dired-mark-files-regexp
 	    (and fn (string-match-p regexp fn))))
      "matching file")))
 
+(defun dired-mark-symlinks-regexp (regexp &optional marker-char)
+  "Mark all symbolic links whose target file matches REGEXP
+for use in later commands.
+A prefix argument means to unmark them instead.
+
+REGEXP is an Emacs regexp, not a shell wildcard.  Thus, use ‘\.o$’ for
+object files--just ‘.o’ will mark more than you might think."
+  (interactive
+   (list (read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
+                              " symlinks target (regexp): ")
+                      nil 'dired-regexp-history)
+		 (if current-prefix-arg ?\040)))
+  (let ((dired-marker-char (or marker-char dired-marker-char)))
+    (dired-mark-if
+     (and (looking-at-p dired-re-sym)
+		  (not (looking-at-p dired-re-dot))
+		  (not (eolp))			; empty line
+		  
+		  (let* ((eol (line-end-position))
+				 (fn (save-excursion
+					   (search-forward " -> " eol t)
+					   (buffer-substring-no-properties (point) eol))))
+			(and fn (string-match-p regexp fn))))
+			"matching symbolic link")))
+
 (defun dired-mark-files-containing-regexp (regexp &optional marker-char)
   "Mark all files with contents containing REGEXP for use in later commands.
 A prefix argument means to unmark them instead.