unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Jean Louis <bugs@gnu.support>
To: help-gnu-emacs@gnu.org
Subject: Re: Function to find symlink target
Date: Sun, 22 May 2022 16:52:56 +0300	[thread overview]
Message-ID: <YopAOJklMhoklv9I@protected.localdomain> (raw)
In-Reply-To: <87wneixx7x.fsf@dataswamp.org>

* Emanuel Berg <incal@dataswamp.org> [2022-05-19 02:02]:
> Jean Louis wrote:
> 
> > (defun rcd-dired-find-symlink ()
> >   "Find target of a symlink"
> >   (interactive)
> >   (when (dired-get-marked-files)
> >     (let ((file (car (dired-get-marked-files))))
> >       (if (file-symlink-p file)
> > 	  (let* ((file (file-truename file))
> > 		 (directory (file-name-directory file))
> > 		 (file (file-name-nondirectory file))
> > 		 (back (length file)))
> > 	    (find-file directory)
> > 	    (search-forward file)
> > 	    (backward-char back))
> > 	(message "Not a symlink: %s" file)))))
> >
> > Let me know if I should improve this by any means.
> 
> Hello again Jean!
> 
> 1. Do auto-indentation ...

Thanks. Thought it indended in my file. Maybe email copy and paste
changed something.

> 2. `require' is needed for `dired-get-marked-files', do
>    byte-compile to find out and always do that before
>    posting here.

If you mean (require 'dired) that is in my file. I understand.

> 3. The defun has a confusing name, you have already found the
>    symlink one would think?

I will call it: `rcd-dired-show-symlink-target'

> 4. Emacs thinks of the docstring that the "[f]irst sentence
>    should end with punctuation". Always do the style check
>    before posting here BTW, here's an example how. [1]

OK. Like this:

(defun rcd-dired-show-symlink-target ()
  "Show target of a symlink."

> 5. `dired-get-marked-files' is invoked twice, instead just do
>     it once and reuse the result with and in `let'.


> 
> 6. "file" appears as a let-binding twice in `let*', confusing
>    and probably no good reason to do so either, right?
> 
> 7. I'm unsure but if you `find-file' a directory that is
>    already open in a buffer, you still end up at `point-min'?
>    If you don't, you need to got there before the search,
>    (goto-char (point-min)) as you know.
>    
> 8. Instead of doing `backward-char', do something like this:
>    [you can try it below as well]
> 
> (when (search-forward "key" (point-max) t)
>   (goto-char (match-beginning 0)) )

(defun rcd-dired-show-symlink-target ()
  "Show target of a symlink."
  (interactive)
  (let ((file (car (dired-get-marked-files))))
    (if (and file (file-symlink-p file))
      (let* ((target (file-truename file))
	     (target-exists (file-exists-p target))
	     (directory (file-name-directory target))
	     (target (file-name-nondirectory target)))
	(if target-exists
	    (progn
	      (find-file directory)
	      (when (search-forward target (point-max) t)
		(goto-char (match-beginning 0))))
	  (message "Target does not exist: %s" target)))
      (message "Not a symlink: %s" file))))

Yes, I have tried this, and now I see that finding is not accurate.

Example is this one:

S lrwxrwxrwx     1    8 May 22 16:15 test1.pdf -> test.pdf
  -rw-r--r--     1   90 May 22 15:24 basic.yml
  -rw-r--r--     1  15K May 22 15:13 test.pdf

as if I invoke function on first line it will come to -> test.pdf
which is not what I would like.

Then I have tried with this version, unsuccessfully:

(defun rcd-dired-show-symlink-target ()
  "Show target of a symlink."
  (interactive)
  (let ((file (car (dired-get-marked-files))))
    (if (and file (file-symlink-p file))
      (let* ((target (file-truename file))
	     (target-exists (file-exists-p target))
	     (directory (file-name-directory target))
	     (search (concat "\d\d " (file-name-nondirectory target))))
	(if target-exists
	    (progn
	      (find-file directory)
	      (when (search-forward-regexp search (point-max) t)
		(goto-char (match-beginning 0))))
	  (message "Target does not exist: %s" target)))
      (message "Not a symlink: %s" file))))

as for that version I think that at least 2 digits should be included
before the space when searching in Dired and I am doing something
wrong.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



  reply	other threads:[~2022-05-22 13:52 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-18 20:55 Function to find symlink target Jean Louis
2022-05-18 23:00 ` Emanuel Berg
2022-05-22 13:52   ` Jean Louis [this message]
2022-05-22 18:02     ` Emanuel Berg
2022-05-23  9:05       ` Jean Louis
2022-05-23 20:46         ` Emanuel Berg
2022-05-24  0:59           ` Emanuel Berg
2022-05-24  1:09             ` Michael Heerdegen
2022-05-24  2:14               ` Emanuel Berg
2022-05-24  2:27                 ` Michael Heerdegen
2022-05-24  8:04                   ` Emanuel Berg
2022-05-24  8:52                     ` Michael Heerdegen
2022-05-25  5:54                       ` Michael Heerdegen
2022-05-27  2:25                         ` Jean Louis
2022-05-27  3:31                           ` Michael Heerdegen
2022-05-27  8:00                             ` Jean Louis
2022-05-27 14:47                           ` Emanuel Berg
2022-05-28  6:43                             ` Jean Louis
2022-05-28 15:29                               ` Emanuel Berg
2022-05-28 15:43                                 ` Emanuel Berg
2022-05-28 16:08                                   ` Eli Zaretskii
2022-05-28 16:46                                     ` Emanuel Berg
2022-05-28 16:55                                       ` Eli Zaretskii
2022-05-28 17:05                                         ` Emanuel Berg
2022-05-31  6:27                                 ` Jean Louis
2022-05-31 12:24                                   ` Emanuel Berg
2022-05-31  6:31                                 ` Jean Louis
2022-05-31 12:16                                   ` Emanuel Berg
2022-06-01  7:43                                     ` Jean Louis
2022-06-02  0:40                                       ` Emanuel Berg
2022-06-02  5:15                                         ` Jean Louis
2022-06-05  5:40                                           ` Emanuel Berg
2022-05-25  5:05                 ` Jean Louis
2022-05-25 22:33                   ` Emanuel Berg
2022-05-29  2:01                     ` Emanuel Berg
2022-05-19 23:14 ` Nick Dokos
2022-05-20  0:42   ` Emanuel Berg
2022-05-23  8:17   ` Jean Louis
2022-05-23 22:49     ` Michael Heerdegen
2022-05-23 22:57       ` Emanuel Berg
2022-05-24  0:59         ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YopAOJklMhoklv9I@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).