Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make find-file-at-point support <nixpkgs> #59

Closed
wants to merge 1 commit into from

Conversation

jabranham
Copy link
Contributor

Closes #58

I think this also respects direnv but I don't use it so it would be nice if someone could check.

If STR contains nixpkgs, call nix-instantiate to find the
location of STR."
;; The < > around <nixpkgs> don't get passed to us
(when (string-match "nixpkgs" str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... can this be altered to generally match any path within <...>s? It won't always be <nixpkgs...> but instead could be almost anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The < and > characters don't get passed through to us, unfortunately. So we can't tell if we're inside <> or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep the brackets with:

(push '(nix-mode "--:\\\\${}<>+@-Z_[:alpha:]~*?" "@" "@;.,!:") ffap-string-at-point-mode-alist)

@grahamc
Copy link
Member

grahamc commented Dec 11, 2018

After applying this patch locally, I get:

In nix-mode-ffap-nixpkgs-path:
nix-mode.el:439:26:Warning: reference to free variable
    `nix-instantiate-executable'
nix-mode.el:481:1:Warning: unescaped character literals `?(', `?)', `?[', `?]'
    detected!
Compiling /nix/store/ljaxyxk4vf674w1kh17m878al5cwjvx8-emacs-nix-mode-20180629.1501/share/emacs/site-lisp/elpa/nix-mode-20180629.1501/nix-prettify-mode.el...

When I call find-file-at-point on <nixpkgs/nixos/release.nix> (or directly inside the <) it doesn't seem to locate the file.

@jabranham
Copy link
Contributor Author

I'm not sure why you'd get a warning about nix-instantiate-executable not being defined yet. It's in nix.el, which is required by nix-mode.el

It won't work if point is on the <. So if | is point, <|nixpkgs/nixos/release.nix> M-x ffap should result in something like /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/release.nix. Did (nix-mode . nix-mode-ffap-nixpkgs-path) get added to ffap-alist for you?

(let ((file (shell-command-to-string
(concat nix-instantiate-executable " --eval -E '<" str ">'"))))
;; Remove trailing newline
(replace-regexp-in-string "\n$" "" file))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the regex you can do (substring 0 (- (length file) 1) file)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I guess that kind of breaks when we don't have a trailing new line.

Copy link
Member

@matthewbauer matthewbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@matthewbauer
Copy link
Member

Here is a small modification to the function, feel free to use them:

(defun nix-mode-ffap-nixpkgs-path (str)
  "Support `ffap' for <nixpkgs> declarations.
If STR contains nixpkgs, call nix-instantiate to find the
location of STR."
  (when (string-match nix-re-bracket-path str)
    (with-temp-buffer
      (when (eq (call-process nix-instantiate-executable nil (current-buffer) nil "--eval" "-E" str) 0)
        ;; Remove trailing newline
        (substring (buffer-string) 0 (- (buffer-size) 1))))))

@matthewbauer
Copy link
Member

call-process is useful because we can get the exit code of nix-instantiate.

@matthewbauer
Copy link
Member

I think nix-instantiate-executable was added recently so that could be causing problems. The first warning should have been fixed with:

588246b

@matthewbauer
Copy link
Member

matthewbauer commented Dec 12, 2018

Merged in 9d4eb7d. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants