typst_library/introspection/here.rs
1use comemo::Tracked;
2
3use crate::diag::HintedStrResult;
4use crate::foundations::{func, Context};
5use crate::introspection::Location;
6
7/// Provides the current location in the document.
8///
9/// You can think of `here` as a low-level building block that directly extracts
10/// the current location from the active [context]. Some other functions use it
11/// internally: For instance, `{counter.get()}` is equivalent to
12/// `{counter.at(here())}`.
13///
14/// Within show rules on [locatable]($location/#locatable) elements, `{here()}`
15/// will match the location of the shown element.
16///
17/// If you want to display the current page number, refer to the documentation
18/// of the [`counter`] type. While `here` can be used to determine the physical
19/// page number, typically you want the logical page number that may, for
20/// instance, have been reset after a preface.
21///
22/// # Examples
23/// Determining the current position in the document in combination with the
24/// [`position`]($location.position) method:
25/// ```example
26/// #context [
27/// I am located at
28/// #here().position()
29/// ]
30/// ```
31///
32/// Running a [query] for elements before the current position:
33/// ```example
34/// = Introduction
35/// = Background
36///
37/// There are
38/// #context query(
39/// selector(heading).before(here())
40/// ).len()
41/// headings before me.
42///
43/// = Conclusion
44/// ```
45/// Refer to the [`selector`] type for more details on before/after selectors.
46#[func(contextual)]
47pub fn here(context: Tracked<Context>) -> HintedStrResult<Location> {
48 context.location()
49}