summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/introspection/here.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-10-27 19:04:55 +0100
committerGitHub <noreply@github.com>2024-10-27 18:04:55 +0000
commitbe7cfc85d08c545abfac08098b7b33b4bd71f37e (patch)
treef4137fa2aaa57babae1f7603a9b2ed7e688f43d8 /crates/typst-library/src/introspection/here.rs
parentb8034a343831e8609aec2ec81eb7eeda57aa5d81 (diff)
Split out four new crates (#5302)
Diffstat (limited to 'crates/typst-library/src/introspection/here.rs')
-rw-r--r--crates/typst-library/src/introspection/here.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/crates/typst-library/src/introspection/here.rs b/crates/typst-library/src/introspection/here.rs
new file mode 100644
index 00000000..9d613381
--- /dev/null
+++ b/crates/typst-library/src/introspection/here.rs
@@ -0,0 +1,52 @@
+use comemo::Tracked;
+
+use crate::diag::HintedStrResult;
+use crate::foundations::{func, Context};
+use crate::introspection::Location;
+
+/// Provides the current location in the document.
+///
+/// You can think of `here` as a low-level building block that directly extracts
+/// the current location from the active [context]. Some other functions use it
+/// internally: For instance, `{counter.get()}` is equivalent to
+/// `{counter.at(here())}`.
+///
+/// Within show rules on [locatable]($location/#locatable) elements, `{here()}`
+/// will match the location of the shown element.
+///
+/// If you want to display the current page number, refer to the documentation
+/// of the [`counter`] type. While `here` can be used to determine the physical
+/// page number, typically you want the logical page number that may, for
+/// instance, have been reset after a preface.
+///
+/// # Examples
+/// Determining the current position in the document in combination with the
+/// [`position`]($location.position) method:
+/// ```example
+/// #context [
+/// I am located at
+/// #here().position()
+/// ]
+/// ```
+///
+/// Running a [query] for elements before the current position:
+/// ```example
+/// = Introduction
+/// = Background
+///
+/// There are
+/// #context query(
+/// selector(heading).before(here())
+/// ).len()
+/// headings before me.
+///
+/// = Conclusion
+/// ```
+/// Refer to the [`selector`] type for more details on before/after selectors.
+#[func(contextual)]
+pub fn here(
+ /// The callsite context.
+ context: Tracked<Context>,
+) -> HintedStrResult<Location> {
+ context.location()
+}