Skip to content

Support for show rules that match on their own output #229

Closed

Description

@SrTobi
#show "Test": it => [Test]

Test

crashes the compiler:

image

Activity

  1. laurmaedje commented on Mar 23, 2023

    @laurmaedje
    Member

    Yeah, the styling system is a bit broken. That's one of the next things on my list. If you just use it within the output, it works fine but if you create a new element that matches again, it's broken.

  2. SrTobi commented on Mar 23, 2023

    @SrTobi
    Author

    Yeah, just trying around a bit :) What's maybe more important is that if the compiler crashes once, it stays broken until I reload the page. Not sure if that's the right behavior. I think it should restart the compiler after the next edit

  3. laurmaedje commented on Mar 23, 2023

    @laurmaedje
    Member

    It should do that, strange ...

  4. SrTobi commented on Mar 23, 2023

    @SrTobi
    Author

    Hmm ok, nvm... tried again and now it works...

  5. user202729 commented on Mar 26, 2023

    @user202729
    Contributor

    I think we just need some way to "unset" the #show.

    #show "Test": it => [
      #show "Test": none
      Test
    ]
    
    Test
    

    This works fine except that it makes the Test invisible.

    Although the behavior would be a bit difficult to predict (should it unset one layer or all layers?) given how the changes are being piled up at the moment:

    #show "Test": it => [a] + it
    #show "Test": it => [b] + it
    
    Test
    

    will result in baTest.

  6. SrTobi commented on Mar 26, 2023

    @SrTobi
    Author

    Or maybe show rules should just apply to things that come after them. I don't know how show-rule application works in typst, but it seems that they are applied within their scope, but only to text that comes after them.

    For example:

    a — b — c
    #[
      a — b — c
      
      #show "c": it => [fromC(a)]
      #show "a": it => [fromA(#it)] 
      #show "b": it => [fromB(a)]
      
      a — b — c
    ]
    a — b — c
    

    gives:

    a — b — c
    a — b — c     // show rules are not applied because text comes before (?)
    fromA(a) — fromB(fromA(a)) — fromC(fromA(a))   // rule "a" applied within rule "c" even though "c" comes before
    a — b — c
    

    If they would be applied only in whatever comes after them and excluding their own transformation function, all recursion stuff would be solved (because recursion is not possible anymore)... though on the other hand, that would invite behaviour where stuff looks different depending on the order of rules... though that is already the case...

  7. SrTobi commented on Mar 26, 2023

    @SrTobi
    Author

    I think we just need some way to "unset" the #show.

    #show "Test": it => [
      #show "Test": none
      Test
    ]
    
    Test
    

    This works fine except that it makes the Test invisible.

    One would think that using it => it instead of none would fix that... but that, in turn, crashes the compiler again 😆

  8. ndtodoroff commented on Mar 27, 2023

    @ndtodoroff

    I'm not sure how to solve this with show "Text": .... But from a quick look at the code it seems that functions have an internal name associated with them, so for show elem: f would this be enough?

    1. If f is a lambda expression then compile/process that first so we get a function object which references the current version of elem.
    2. Set the internal name of elem to something new (like "elem" + unique ID). Note that f necessarily references the version with the old name.
    3. Now process show elem: f just like we do currently.
  9. added
    bug
    styling
    on Apr 2, 2023
  10. SUPERCILEX commented on Apr 11, 2023

    @SUPERCILEX
    Contributor

    Slightly more complicated test case for the eventual PR:

    show enum: it => {
        for child in it.children {
          pad(child)
        }
      }
    
  11. added
    crash
    on Jul 11, 2023
  12. edgarogh commented on Aug 22, 2023

    @edgarogh

    I'm not sure this was clearly mentioned in this thread, but in the mean time, in situations involving strings and regexes, you can use a show rule with a function and reuse the it parameter in the output. That won't cause recursion.

    That doesn't work

    #show "My Project": underline("My Project")

    That works

    #show "My Project": it => underline(it)

    rendering of the previous code

  13. MultisampledNight commented on Nov 16, 2023

    @MultisampledNight
    Contributor

    Fwiw another case where it's not directly visible that the content is reproduced causing the stack overflow would be this:

    #show "stack": it => it.text
    
    "Smashing the stack for fun and profit" is a classic.
  14. XenHum commented on Nov 19, 2023

    @XenHum

    I see fixing this issue is on the roadmap - is there any timeline for the fix? I'm asking because my company won't move from Latex to typst until this is resolved. In any case, thanks for your work!

  15. 4 remaining items

  16. Andrew15-5 commented on Nov 26, 2023

    @Andrew15-5
    Contributor

    I failed to make an alias show "cmd": it => raw(it) because it is content instead of string. Adding repr/str also doesn't work. Does anyone have a workaround for this?

  17. MultisampledNight commented on Nov 26, 2023

    @MultisampledNight
    Contributor

    @Andrew15-5 The mark you wanted can be implemented without any changes to typst through a zero-width no-break space. Hacky, causing double-raws like below if cmd is already in a codeblock and possibly detrimental to accessibility, but it works for me.

    #let insert-zwsp(it) = {
      (it.first()
        + "\u{FEFF}"
        + it.clusters().slice(1).join())
    }
    #show "cmd": it => raw(insert-zwsp(it.text))
    
    Use cmd for more information, as in `cmd lol`.

    showhacks

    EDIT: Updated to use the non-breaking zero-width space U+FEFF instead of the breaking one (U+200B).

  18. Andrew15-5 commented on Nov 26, 2023

    @Andrew15-5
    Contributor

    Very hacky indeed. Thank you. I already forgot about this character. I found out about it a year or two ago. I think that's a great universal workaround. Okular was able to find both cmd, so the ZWSP char does not affect word search.

  19. T0mstone commented on Nov 26, 2023

    @T0mstone
    Contributor

    That's the thing — I'm talking about marking each processed "content", meaning that if the top list element was processed, then mark it, then separately mark each nested list element.

    We already do that, but it doesn't work anyway because, to use laurmaedje's example, with show "hey": box[hey], you get "hey" (unmarked) --> box("hey" (unmarked)) (marked) --> box(box("hey" (unmarked)) (marked)) (marked) -> etc.

  20. Andrew15-5 commented on Nov 26, 2023

    @Andrew15-5
    Contributor

    A related problem about "optimization". Is there a way to make this work?

      #for command in ("cmd1", "cmd2", "cmd3") {
        show command: it => shell(it.text)
      }
      // or
      #show: doc => {
        for command in ("cmd1", "cmd2", "cmd3") {
          show command: it => shell(it.text)
        }
        doc
      }

    It looks much nicer than the repetitive (working) way:

      #show "cmd1": it => shell(it.text)
      #show "cmd2": it => shell(it.text)
      #show "cmd3": it => shell(it.text)

    And the benefit increases as the number of show rules grows.

    Solution

    #let commands = ("cmd1", "cmd2", "cmd3")
    #show regex(commands.join("|")): it => shell(it.text)

    P.S. shell uses the invisible char insertion hack + adds raw shellscript wrapper.

  21. Andrew15-5 commented on Nov 26, 2023

    @Andrew15-5
    Contributor

    @MultisampledNight, note that BOM char is deprecated for this use case: https://en.wikipedia.org/wiki/Word_joiner. So you better use "\u{2060}" WJ. And you forgot to rename the function to insert-zwnbsp (but for WJ — insert-wj).

  22. MultisampledNight commented on Nov 26, 2023

    @MultisampledNight
    Contributor

    @Andrew15-5 the problem I see with the word joiner is that potentially emojis are formed with that. Maybe that's the case with U+FEFF as well, but idk.

    Anyway, maybe you want to check out the Discord server for quicker responses to your specific problem.

  23. Andrew15-5 commented on Nov 26, 2023

    @Andrew15-5
    Contributor

    potentially emojis are formed with that

    I agree, as this was one of my first thoughts when I read the article. But since we are (I'm assuming) specifically talking about text substitution show rules, it is safe to say that this is impossible to happen. After all, the main point is to simplify the source file and potentially remove all the emojis and stuff (it's especially noticeable in Neovim, since not all of them are shown as a single cluster) with another pack of show rules (show "green-heart": "💚").

  24. changed the title [-]"show"-recursion crashes compiler[/-] [+]Support for show rules that match on their own output[/+] on Dec 4, 2023
  25. added
    feature request
    and removed
    bug
    crash
    on Dec 4, 2023
  26. laurmaedje commented on Dec 4, 2023

    @laurmaedje
    Member

    With the latest changes, this doesn't crash anymore, instead giving a proper error message. Hence, I'm converting this from a bug to a feature request.

  27. laurmaedje commented on Feb 2, 2024

    @laurmaedje
    Member

    I have opened #3327 to fix this for text show rules. For other show rules, I think the current error is fine and should remain. I've explained my reasoning for that in the linked PR. The issue will be closed once the PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requeststyling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions