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

Support doing buttonization, and inserting links, in unrecognized file types (e.g. source code?) #364

Closed
alexispurslane opened this issue May 19, 2024 · 9 comments

Comments

@alexispurslane
Copy link

alexispurslane commented May 19, 2024

When I saw that Denote supported buttonization in non-org modes like Markdown and plain text, it occurred to me that this could be used to link to Denote notes from basically any file, including the comments of source code, which could be extremely useful.

Additionally, there's no reason you couldn't insert a Denote link in any file type, even if it isn't recognized, right? There would just need to be a variable set to tell it how to format that link by default, since it wouldn't be able to tell what markup to use based on the filetype. Maybe the denote-file-type variable could do double duty for that?

Or, hell, you could do in unrecognized text files what you already do in plain text files and just default to org format!

If you're interested I'd be willing to attempt to make a PR to do this.

@alexispurslane
Copy link
Author

I just adviced denote's function to test whether a file type is supported to always return true, and links between denote notes and code files worked beautifully, as did buttonization.

@protesilaos
Copy link
Owner

One way to handle this is to always format links using the Org syntax. Then use M-x org-open-at-point-global on it.

@alexispurslane
Copy link
Author

alexispurslane commented May 20, 2024

One way to handle this is to always format links using the Org syntax. Then use M-x org-open-at-point-global on it.

That's a very good point, that would work to open a note link, but it wouldn't allow inserting the note link, despite all the information necessary to do so being at hand, and it also wouldn't highlight any available links. The solution I went with was this:

(advice-add 'denote-file-has-supported-extension-p :filter-return (lambda (x) t))

And then relying on (add-hook 'find-file-hook #'denote-link-buttonize-buffer) from the manual — that made link inserting, link clicking, and link buttonization all work.

I'm not sure if this has any unforseen drawbacks, but as far as I can tell it doesn't — denote just treats the file like an org mode file.

protesilaos added a commit that referenced this issue May 20, 2024
Thanks to Alexis Purslane for suggesting this idea in issue 364:
<#364>.
@protesilaos
Copy link
Owner

I just added this to the manual, which is probably safer:

(defun my-denote-link-global (file file-type description &optional id-only)
  "Like the `denote-link', but works in any buffer.
The FILE, FILE-TYPE, DESCRIPTION, and ID-ONLY have the same meaning as
in `denote-link'."
  (interactive
   (let* ((file (denote-file-prompt nil "Link to FILE"))
          (file-type (denote-filetype-heuristics buffer-file-name))
          (description (when (file-exists-p file)
                         (denote--link-get-description file))))
     (list file file-type description current-prefix-arg)))
  (unless (file-exists-p file)
    (user-error "The linked file does not exists"))
  (let ((beg (point)))
    (denote--delete-active-region-content)
    (insert (denote-format-link file description file-type id-only))
    (unless (derived-mode-p 'org-mode)
      (make-button beg (point) 'type 'denote-link-button))))

I am open to having something like this in denote.el though it is just that nobody asked for it before. In such cases, we keep the code in the manual and think what to do about it once we have more feedback.

Note that if you are tracking denote.git, this depends on commit bd14d37. If you do try it, please let me know if it works for you.

@alexispurslane
Copy link
Author

Thank you so much! I'll try it out

@protesilaos
Copy link
Owner

protesilaos commented May 20, 2024 via email

@alexispurslane
Copy link
Author

If you do try it, please let me know if it works for you.

FYI, I put this in my config and gave it a spin and it seems to work great! I haven't used it in anger yet, but I don't see why it would stop working.

@protesilaos
Copy link
Owner

protesilaos commented May 21, 2024 via email

@alexispurslane
Copy link
Author

Oh, I did find an issue with your more surgical code (as opposed to my sledgehammer approach): with the original file-type-recognized function intact, denote-link-buttonize-buffer now only works on file types it considers "safe", so buttonization only works in e.g. code comments the first time you insert it, and not subsequently. I don't know how I missed that the first time around. I guess I don't restart emacs that often.

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

No branches or pull requests

2 participants