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

Emacs ANSI support #235

Closed
hvesalai opened this issue Mar 6, 2018 · 15 comments
Closed

Emacs ANSI support #235

hvesalai opened this issue Mar 6, 2018 · 15 comments
Milestone

Comments

@hvesalai
Copy link
Contributor

hvesalai commented Mar 6, 2018

Emacs comint announces it self as DUMP terminal, but it still support ANSI colors.

JLine Terminal should detect emacs based on the INSIDE_EMACS environment variable and turn on ANSI support for it.

I could make a PR and test it if somebody gave me a few pointers where this would be best implemented in JLine.

@hvesalai
Copy link
Contributor Author

hvesalai commented Mar 6, 2018

@fommil We need to keep track of this issue so that emacs-sbt-mode does not break when sbt and scala start using JLine3

@hvesalai
Copy link
Contributor Author

hvesalai commented Mar 6, 2018

Here are the changes comint mode makes to the environment before starting a process if the system is using terminfo

TERMCAP=
TERM=dumb
COLUMNS=290
INSIDE_EMACS=24.5.1,comint
EMACS=true

If the system is using termcap, the values are

TERM=emacs
TERMCAP=emacs:co#290:tc=unknown:
INSIDE_EMACS=24.5.1,comint
EMACS=true

(some versions have EMACS=t instead of true)

@hvesalai
Copy link
Contributor Author

hvesalai commented Mar 6, 2018

Starting with emacs 26.1 one can define the TERM used with terminfo. If there is some other value that would make this work without changes to JLine, we could use that

See https://github.com/emacs-mirror/emacs/blob/master/lisp/comint.el#L462

@gnodet
Copy link
Member

gnodet commented Mar 7, 2018

The code to look at would be in TerminalBuilder:
https://github.com/jline/jline3/blob/master/terminal/src/main/java/org/jline/terminal/TerminalBuilder.java#L230

I'm not sure at this point which jline Terminal implementation would be best suited for running inside emacs. Does emacs actually create a pty or is it a simple emulation of a terminal ?

@hvesalai
Copy link
Contributor Author

hvesalai commented Mar 7, 2018

TL;DR of below is that emacs is like a dump terminal (no terminal emulation) that still can interpret some ANSI control codes, mostly the colors.

The long version:

In emacs all console interaction happens with the help of comint-mode. Comint sets the environment as described above and then starts the given process redirecting the process output to a given editor buffer. Input to the process can only be sent with a special command, i.e. users input will not be directly sent to the process. This means that features such as completion are not normally available.

So to answer your question, no, there is no pty or even actual terminal emulation. Comint-mode just redirects the process output to the given editor buffer. However as emacs does support colors, there is support for interpreting some ANSI control sequences and turning them into emacs colors. Also some in-line movement may be supported, but things like movement between lines or relative to terminal screen is not supported and are not applicable.

Furthermore, while the processes output comes to the end of the buffer, the user can move the cursor freely within the buffer. As said, input to the process is sent with a special command that sends either the input given to it as an argument, or alternatively it picks up the input from the cursor location (which again, can be anywhere) using special rules (such as sending everything on the line of the cursor, except maybe a prompt that is detected with a separate regexp).

The use case for supporting emacs in JLine is that developers using emacs routinely run commands from within emacs. A trivial example is running a compilation command and getting back the compilation errors, which emacs can detect from the buffer using regular expressions and turn into links to the respective files.

An other example is support for special programs, such as the scala REPL, which is supported by the sbt-mode. In this case the mode implements some of the features available through a normal terminal, such as command completion.

@hvesalai
Copy link
Contributor Author

hvesalai commented Mar 7, 2018

The most important thing though is that while emacs comint-mode makes an attempt to suppress input echo, it does not work properly. Hence echo should be off for emacs.

@gnodet
Copy link
Member

gnodet commented Mar 7, 2018

Here's an experimental branch 1b10052.
I haven't tested on emacs and it does need some work, but that's an idea on how to solve the problem. I think it's very similar to supporting idea and eclipse...

@hvesalai
Copy link
Contributor Author

hvesalai commented Mar 7, 2018

Added a few comments to the branch

@fommil
Copy link

fommil commented Mar 9, 2018

@gnodet awesome, thanks for looking into this. Do you think a backport to jline2 would be feasible? The entire scala tooling ecosystem is using jline2 and it would be years before they can migrate to jline3. They are also very unlikely to make patch releases to old versions of the core tools to facilitate this change, so we will have to document how users can use a binary-compatible jline2 locally.

@hvesalai
Copy link
Contributor Author

hvesalai commented Mar 9, 2018

@fommil I was planning on making similar PR to jline2, there is already an issue where I asked if that would be ok and @gnodet said yes. We also need to work on the draft gnodet made and make it work in both Java8 and Java9.

@hvesalai
Copy link
Contributor Author

@gnodet Is your target for JLine3 Java8 or Java9?

@gnodet
Copy link
Member

gnodet commented Mar 20, 2018

Java 8 for now.

@gnodet
Copy link
Member

gnodet commented Mar 20, 2018

The https://github.com/jline/jline3/compare/issue-235-ansi branch contains a JDK 8 compatible fix I think.

@hvesalai
Copy link
Contributor Author

The branch looks good as far as ANSI. What about echo? Reading the code I gather that it should be disabled by default?

@gnodet
Copy link
Member

gnodet commented Mar 29, 2018

Yes, there should be no echo for dumb terminals.

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

No branches or pull requests

3 participants