dajva / rg.el

Emacs search tool based on ripgrep
https://rgel.readthedocs.io
GNU General Public License v3.0
465 stars 38 forks source link

Feature request: search symlinked file, but make the file path in "*rg*" buffer the path of the symlink #156

Closed QiangF closed 1 year ago

QiangF commented 1 year ago

People use symlinks to organize text documents, a file may have links in different dirs. For example, if we use dirs as catalogues, when a file is opened following the search result in a "rg" buffer, it would be convenient to jump to the catalogue by just M-x "dired". However, the current behavior is to jump to the dir that has the symlink destination file. For people using git-annex, the symlink destination dir could be a database dir, and it is not supposed to be accessed directly.

This request is to change the "default-directory" based on the search dir, not based on where the symlink destination dir.

dajva commented 1 year ago

Not sure I understand fully what you mean here. Could you give a more explicit example how a real directory structure with symlinks and all, what search you do in what dir and what you would expect to come out of it.

QiangF commented 1 year ago

Here is an example, the annex dir is managed with git-annex, and in the android dir I have several symlinks:

q@desktop:android$ pwd
/home/q/my_lib/annex/android
q@desktop:android$ ls -la
total 20
drwxr-xr-x  3 q wheel 4096 2023-04-11 16:55 ./
drwxr-xr-x 21 q  1000 4096 2023-04-20 21:40 ../
lrwxrwxrwx  1 q wheel  197 2023-03-09 08:45 antennapod-feeds-2023-03-08.opml -> ../.git/annex/objects/xv/KJ/SHA256E-s4940--902506c74d2775e2e244b5cabc35df59f521356033740162b2d181703cadffbc.opml/SHA256E-s4940--902506c74d2775e2e244b5cabc35df59f521356033740162b2d181703cadffbc.opml
lrwxrwxrwx  1 q wheel  199 2023-03-09 08:45 contacts.vcf -> ../.git/annex/objects/zq/9w/SHA256E-s122001--82077ded2603a512e7b60b55ee4d968b42a23b5808993d97972f1f1224e15ce8.vcf/SHA256E-s122001--82077ded2603a512e7b60b55ee4d968b42a23b5808993d97972f1f1224e15ce8.vcf
drwxr-xr-x  4 q wheel 4096 2023-04-17 08:54 okaypad_okaybook/

Now I search for "english" with emacs rg in the android dir:

-*- mode: rg; default-directory: "~/my_lib/annex/android/" -*-
rg started at Fri Jun 16 09:25:04

/usr/bin/rg --color=always --colors=match:fg:red --colors=path:fg:magenta --colors=line:fg:green --colors=column:none -n --column -z --pcre2 -L -i --heading --no-config --type-add=custom\:\* --type=custom -e english

File: antennapod-feeds-2023-03-08.opml
  15  60     <outline text="Science &amp; Technology - VOA Learning English" title="Science &amp; Technology - VOA Learning English" type="rss" xmlUrl="https://learningenglish.voanews.com/podcast/?count=50&amp;zoneId=1579" htmlUrl="https://learningenglish.voanews.com/z/1579" />
  22  33     <outline text="VOA Learning English Podcast - VOA Learning English" title="VOA Learning English Podcast - VOA Learning English" type="rss" xmlUrl="https://learningenglish.voanews.com/podcast/?count=20&amp;zoneId=1689" htmlUrl="https://learningenglish.voanews.com/z/1689" />

rg finished (10 matches found) at Fri Jun 16 09:25:04

Now I move point to the first match line (line with 15 60), and press enter. The file is opend, but the file path is:

~/my_lib/annex/.git/annex/objects/xv/KJ/SHA256E-s4940--902506c74d2775e2e244b5cabc35df59f521356033740162b2d181703cadffbc.opml/SHA256E-s4940--902506c74d2775e2e244b5cabc35df59f521356033740162b2d181703cadffbc.opml

The path above (let me call it path1) is not the symlink path "~/my_lib/annex/android/antennapod-feeds-2023-03-08.opml". From path1 you can not jump to the dir that contains the symlink.

QiangF commented 1 year ago

I have tried this:

(remove-hook 'find-file-hook #'vc-refresh-state)
dajva commented 1 year ago

Ok, now I understand. So you essentially don't want symlinks to be resolved when jumping to a file. This is handled by the upstream compile package in emacs, more specifically compilation-find-file seems to do the job here. I suggest you dig into that and see what's going on. I think I have seen the opposite from emacs xref buffers, iow that symlinks are not resolved so might be something that may be tweakable.

QiangF commented 1 year ago

One possible way to disable the symlink resolving is to open the file before compile-find-file. I just removed the file-truename function from compile-find-file, works for me now.

dajva commented 1 year ago

This should probably be exposed as a customization of the compilation package. But that would be an emacs bug to report.