lem-project / lem

Common Lisp editor/IDE with high expansibility
http://lem-project.github.io/
MIT License
2.39k stars 175 forks source link

Lem breaks terminal colors on exit #490

Closed svetlyak40wt closed 3 months ago

svetlyak40wt commented 4 years ago

Here is a demo:

lem-colors-demo

Hamayama commented 4 years ago

It seems that there is no portable way to restore terminal's default colors. (ref. https://stackoverflow.com/questions/36051061/color-not-ended-in-curses )

One altenative way is to write your terminal's default color codes in ~/.lem/init.lisp as follows.

#+lem-ncurses
(progn
  ;; set ansi 256 colors
  ;; (ref. https://en.wikipedia.org/wiki/ANSI_escape_code#Colors )
  (when (= charms/ll:*colors* 256)
    (lem.term:term-set-color  0 #x00 #x00 #x00)
    (lem.term:term-set-color  1 #x80 #x00 #x00)
    (lem.term:term-set-color  2 #x00 #x80 #x00)
    (lem.term:term-set-color  3 #x80 #x80 #x00)
    (lem.term:term-set-color  4 #x00 #x00 #x80)
    (lem.term:term-set-color  5 #x80 #x00 #x80)
    (lem.term:term-set-color  6 #x00 #x80 #x80)
    (lem.term:term-set-color  7 #xc0 #xc0 #xc0)
    (lem.term:term-set-color  8 #x80 #x80 #x80)
    (lem.term:term-set-color  9 #xff #x00 #x00)
    (lem.term:term-set-color 10 #x00 #xff #x00)
    (lem.term:term-set-color 11 #xff #xff #x00)
    (lem.term:term-set-color 12 #x00 #x00 #xff)
    (lem.term:term-set-color 13 #xff #x00 #xff)
    (lem.term:term-set-color 14 #x00 #xff #xff)
    (lem.term:term-set-color 15 #xff #xff #xff)
    (let ((code (vector #x00 #x5f #x87 #xaf #xd7 #xff)))
      (loop :for i :from 0 :to 215
            :for r := (aref code (mod (floor i (* 6 6)) 6))
            :for g := (aref code (mod (floor i 6) 6))
            :for b := (aref code (mod i 6))
            :do (lem.term:term-set-color (+ i 16) r g b)))
    (loop :for i :from 0 :to 23
          :for c := (+ (* i 10) 8)
          :do (lem.term:term-set-color (+ i 232) c c c))))

Lem uses this color table to get color index from rgb. (ref. https://github.com/cxxxr/lem/blob/0ada0951c47817964e6994b1e76b559f9859447d/frontends/ncurses/term.lisp#L341 )

I don't know your terminal's default colors (and also how many colors are supported) . So, the above color codes might need to be changed.

svetlyak40wt commented 4 years ago

But how does GNU Emacs manages to do this when I running it in the terminal?

Probably, there is some way we just not know about it???

Hamayama commented 4 years ago

Uhm, as a non-portable way, tput init command might reset terminal colors.

(ref. https://superuser.com/questions/317343/reset-colors-of-terminal-after-ssh-exit-logout )

If this works on your terminal, writing the same escape sequence (you can see it by tput init | od -t x1z ) to lem::*terminal-io-saved* in *exit-editor-hook* might also work. (Though I don't have such environment and can't test it ...)

(ref. mouse-sgr1006 uses a similar escape sequence https://github.com/cxxxr/lem/blob/f531cc2863bbb44e0524f60abdce82c70f9c96aa/contrib/mouse-sgr1006/main.lisp#L146-L174 )

svetlyak40wt commented 4 years ago

tput init work in the standard Terminal app on OSX. But not in iTerm2 :(

Hamayama commented 4 years ago

Environment variable TERM might change something.

(ref. Q: How do I get 256 color mode working in vim? https://www.iterm2.com/faq.html )

(ref. Support new "iterm2" entry from terminfo.src (2017-08-16) https://github.com/neovim/neovim/issues/7209 )

After setting 'Preferences > Profiles > Terminal > Report Terminal Type' to xterm-256color or iterm2, tput init works ?

svetlyak40wt commented 4 years ago

This setting already was xterm-256color.

And tput init does not work in this mode.

If I set this option to iterm2, then lem does not start. It exits with Error opening terminal: iterm2. And also a backspace does not work in the terminal.

Hamayama commented 4 years ago

The message 'Error opening terminal: iterm2.' is made by ncurses. (ref. https://github.com/mirror/ncurses/blob/47d2fb4537d9ad5bb14f4810561a327930ca4280/ncurses/base/lib_initscr.c#L95 )

This indicates that your terminfo database doesn't have iterm2 entry.

I will search how to update it on Mac OSX.

(Similar issue ? https://trac.macports.org/ticket/56873?cversion=0&cnum_hist=10 )

Hamayama commented 4 years ago

Or another approach is to change frontends/ncurses/term.lisp .

Lem changes terminal color here. https://github.com/cxxxr/lem/blob/0ada0951c47817964e6994b1e76b559f9859447d/frontends/ncurses/term.lisp#L43-L314

Change call-init-color to nil.

Change init-colors function to same as your terminal color setting. (this is required to search color index by Lem.)

It might work ...

svetlyak40wt commented 4 years ago

Ok, I'll try. By the way, I already tried to remember original colors in term-set-color and to restore them on exit. But this did't work for some reason.

Plisp commented 3 years ago

Use the oc/orig_colors terminfo capability. This should be provided by most sane terminals providing initc (this naturally excludes urxvt; few useful things are portable in the terminal world).

tput init in any case is definitely not what you're looking for, as it outputs the is* initialization strings rather than the rs* reset strings (use tput reset instead).

see man terminfo for details