From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: Function to find symlink target Date: Sun, 22 May 2022 16:52:56 +0300 Message-ID: References: <87wneixx7x.fsf@dataswamp.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16770"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.2.0 (2022-02-12) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun May 22 15:54:53 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nsm2n-0004D7-1z for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 22 May 2022 15:54:53 +0200 Original-Received: from localhost ([::1]:47702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nsm2l-00035p-IJ for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 22 May 2022 09:54:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38994) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsm1P-00035f-Jx for help-gnu-emacs@gnu.org; Sun, 22 May 2022 09:53:27 -0400 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:39933) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsm1N-0000Xe-0y for help-gnu-emacs@gnu.org; Sun, 22 May 2022 09:53:27 -0400 Original-Received: from localhost ([::ffff:154.228.56.200]) (AUTH: PLAIN admin, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 0000000000067FC0.00000000628A4051.00003069; Sun, 22 May 2022 06:53:21 -0700 Mail-Followup-To: help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: <87wneixx7x.fsf@dataswamp.org> Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:137350 Archived-At: * Emanuel Berg [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/