Kungsgeten / yankpad

Paste snippets from an org-mode file
MIT License
216 stars 18 forks source link

Support Noweb Reference Syntax (i.e., include one snippet into another) #72

Closed TRSx80 closed 3 years ago

TRSx80 commented 4 years ago

Creating new issue to track this, as discussed here.

Reference (Org manual): Noweb Reference Syntax

In vanilla Orgmode, you can include one snippet into another, via Noweb reference syntax.

In the particular case I ran across this, say I have a bunch of various bash snippets. Further say I have one for a print help function (that I want to use by itself some times) but I also have a "new bash skeleton" snippet where I would also like to include the "print help" function, without needing to maintain said function in 2 separate places (with all attendant problems inherent in such):

* sh-mode

** f_print_help                                                                               :src:

#+name: f-print-help
#+begin_src shell
  f_print_help () {
      cat << EOF

  <brief intro>

  USAGE:

    $0 [OPTION]... [IN-BASEDIR] [OUT-BASEDIR]

  OPTIONS:

  EXAMPLES:

    $0 -p\\
     --a
     --b
     --c

  EOF
  }

#+end_src

** new script empty skeleton                                                                  :src:

#+begin_src shell :noweb yes
#!/bin/bash

#* Commentary

#* Variables

#* Functions

<<f-print-help>>

#* Main

#+end_src

I think I have included everything required (above), but when I couldn't get it to work I may have deleted some stuff. I try to put everything back for complete example though. However if you can not get it to work in regular Orgmode, let me know and I look again.

I did have the inclusion working at some point in regular Orgmode.

I might venture a guess that Yankpad is only pulling the source block from the referenced subtree, and thus is not aware of the noweb reference perhaps? Not sure. I'm sure it's also very dependent on how Babel itself works. I have not dig into this at all yet.

No idea how much complexity this might add, if it's worth it (or even possible) but there it is. At least we can discuss / track it as a separate idea now.

Thank you for your consideration.

Kungsgeten commented 4 years ago

Thanks for posting the suggestion. Yankpad only looks at the src-block at the moment, not :noweb references etc. Including snippets in other snippets like this might be useful in some cases. I have an idea which doesn't involve noweb, I'll get back to you :)

Kungsgeten commented 4 years ago

The latest commit doesn't add noweb support, but might solve the problem (if you're using Yasnippet together with Yankpad). Now there's a function named yankpad-snippet-text which gets the result of a snippet as a string. Yasnippet has functionality to insert strings returned from Elisp functions. This means you could do the following:

* sh-mode

** f_print_help                                                                               :src:

#+name: f-print-help
#+begin_src shell
  f_print_help () {
      cat << EOF

  <brief intro>

  USAGE:

    $0 [OPTION]... [IN-BASEDIR] [OUT-BASEDIR]

  OPTIONS:

  EXAMPLES:

    $0 -p\\
     --a
     --b
     --c

  EOF
  }

#+end_src

** new script empty skeleton                                                                  :src:

#+begin_src shell :noweb yes
#!/bin/bash

#* Commentary

#* Variables

#* Functions

`(yankpad-snippet-text "f_print_help")`

#* Main

#+end_src

As you can see <<f-print-help>> has been replaced. However yankpad-snippet-text requires that the snippet you want to get the text from is among your currently active snippets. I haven't tried it much, so please let me know if it works for you!

Kungsgeten commented 3 years ago

I think the solution I posted solves the issue, even if it doesn't do it with noweb. Also I haven't heard back from the original poster for a while.

TRSx80 commented 3 years ago

Yeah sorry for falling off the earth. I never had a chance to circle back to this. $DAYJOB taking up all my time, what little is left over goes toward getting my new PinePhone working these days (much excite! :smile:), so this fell way down the priority list somehow. But I really appreciate your help/response! :beers:

TRSx80 commented 1 year ago

Hi @Kungsgeten,

I found myself circling back to this today for whatever reason.

I just wanted to let you know that I have been using the 'new' (lol) function yankpad-snippet-text for a long time now and it works great!

Thanks for writing it!

Cheers! :beers: