Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode Input #263

Closed
sharkdp opened this issue Nov 20, 2023 · 4 comments · Fixed by #275
Closed

Unicode Input #263

sharkdp opened this issue Nov 20, 2023 · 4 comments · Fixed by #275

Comments

@sharkdp
Copy link
Owner

sharkdp commented Nov 20, 2023

Julia has a great feature where you can input Unicode characters using LaTeX-like abbreviations. This would be great for Numbats REPLs as well. We could type \pi<tab> to get π or \hbar to get ħ.

Open questions:

  • It's probably fine to have a list of hard-coded sequences for this. Or should this be fully user-customizable? If so, which (new) construct should introduce those assignments?
@eminence
Copy link
Contributor

I took a quick look at this, and this doesn't seem as trivial as it might initially seem: rustyline doesn't seem to handle tab completions when the completion (like π) isn't a prefix of the input (like \pi).

Relevant issue: kkawakam/rustyline#593

@Bzero
Copy link
Contributor

Bzero commented Nov 25, 2023

If it is too hard to do tab completions I think a reasonable alternative could be to allow LaTeX math abbreviations in the REPL input which are not replaced interactively while entering them but which are replaced in a step before passing the input to the actual numbat compiler.

@sharkdp
Copy link
Owner Author

sharkdp commented Nov 28, 2023

I took a quick look at this, and this doesn't seem as trivial as it might initially seem: rustyline doesn't seem to handle tab completions when the completion (like π) isn't a prefix of the input (like \pi).

It looks like it did support that at some point, because kalker is based on (an old version of) rustyline and it allows you to type sqrt<tab> and it will be replaced by . Their code is here. I tried something similar using code like this:

diff --git a/numbat-cli/src/completer.rs b/numbat-cli/src/completer.rs
index 572bc55..6746ee8 100644
--- a/numbat-cli/src/completer.rs
+++ b/numbat-cli/src/completer.rs
@@ -20,6 +20,16 @@ impl Completer for NumbatCompleter {
         pos: usize,
         _: &rustyline::Context<'_>,
     ) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
+        if line[..pos].ends_with("\\hbar") {
+            return Ok((
+                pos - "\\hbar".len(),
+                vec![Pair {
+                    display: "ℏ".into(),
+                    replacement: "ℏ".into(),
+                }],
+            ));
+        }
+
         if line.starts_with("use ") {
             return Ok((
                 0,

but it does not work as expected. Maybe this is a regression in rustyline? Unicode characters also seem to be handled incorrectly, because replacing e.g. h<tab> with works, but replacing h<tab> with x does not work — only with something longer. So they probably call "ℏ".len() instead of "ℏ".unicode_width() somewhere.

@sharkdp
Copy link
Owner Author

sharkdp commented Nov 28, 2023

Oh — it works with CompletionType::Circular! But not with CompletionType::List, which is what we currently use in Numbat.

Upstream ticket: kkawakam/rustyline#748

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

Successfully merging a pull request may close this issue.

3 participants