#Detect if Cursor is in the Beginning of a Line

7 messages · Page 1 of 1 (latest)

rugged panther
#

Hey,

I want to check if the "cursor" is currently at the start of a line. If it isn't, I want to insert a linebreak.
Something like this:

if (cursor_at_start) {
  fleuron
  #h(1fr)
  fleuron
} else {
  #linebreak()
  fleuron
  #h(1fr)
  fleuron
}

Basically, if the current line was preceded by a block or by a linebreak, I don't need an additional linebreak.

radiant plinth
#

you simply have to make it a block:

#

?r ```ansi
hello
#block[
#sym.floral
#h(1fr)
#sym.floral
]
world

tender shell
#

Because the question mentions blocks already and is really specific, here is a more specific answer.

AFAIK, it's not yet possible to get page sizes, but typst sets the margins to 2.5/21 times the smaller dimension of the page by default source in the docs. You can lookup default page sizes somewhere or borrow them here. Or just define your own 🤷
Anyway locate gives you a location which has a position which has a horizontal x which you can compare with the left margin.

#

?r

#let left-margin = 1cm
#set page(paper: "a7", height: auto, margin: (x: left-margin))

#let linebreaker = locate(loc => {
  if loc.position().x != left-margin { text(fill: red)[not left] + linebreak() } else { text(fill: blue)[left] }
})

this #linebreaker and so on, but 

#linebreaker this is left