mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.99k stars 715 forks source link

[BUG] Garbage instead of Cyrillic text when connecting to Kakoune server launched as a daemon using `launchd` #5145

Closed VKondakoff closed 7 months ago

VKondakoff commented 7 months ago

Version of Kakoune

2023.08.05

Reproducer

Looks like Kakoune 2023.08.05 does not display Cyrillic text. I created test file with single line: Кириллица - Cyrillic.

Outcome

Here is a screenshot of the file opened in Kakoune, Helix, Nova and Zed:

Снимок экрана 2024-04-17 в 00 21 37

Expectations

Expected to see Кириллица - Cyrillic line.

Additional information

No response

VKondakoff commented 7 months ago

Sorry, this is not the full info. If I launch Kakoune with -n switch the issue is not reproducible: Cyrillic text is displayed correctly. Investigating further.

VKondakoff commented 7 months ago

OK, it turns out, that Cyrillic text is not displayed only if I open the file in a client, connected to a Kakoune server, launched as a daemon using launchd (I'm on Mac). When I'm connecting to any other Kakoune server the text is displaying properly. What may be wrong here?

Screwtapello commented 7 months ago

Kakoune reads bytes from disk, but it needs to display characters and look up information about them like "how many character cells does this character take up" and "is this character whitespace". Kakoune depends on the C standard library's locale features for this information, which depend on environment variables like $LANG and $LC_CTYPE and such to tell it what locale settings to use, including how to convert bytes into characters.

If you run !locale<ret> in a Kakoune session where Cyrillic works, I bet you'll see a bunch of environment variable values that end in .utf8 or .UTF-8, and if you run the same command in a session where Cyrillic is broken, I bet you'll see they're all set to "C" (which is the default, for backwards-compatibility with 1972).

On macOS, locale configuration is done via the Settings app. When you log in and it sets up the graphical environment, one of the things it does is read that configuration and convert it to locale environment variables for terminal apps to use. As a result, anything that runs outside the graphical environment (such as from launchd) will not have access to that information.

I don't know exactly how launchd is configured, but there's probably a way to set custom environment variables before starting the program in question. If you set LANG=en_US.UTF-8 (or some other language if you don't like US English), you should get the behaviour you want. If you run the shell command locale -a it should print a list of all the locales your machine supports.

VKondakoff commented 7 months ago

Hey, thanks a lot for such detailed comment!

Yes, that was the issue: all the LANG* variables in the session, started by launchd were set to their default values. I have not found an info of how I can set environment variables to the processes started by launchd, but I was able to launch Kakoune in daemon mode at login with a shell script and Keyboard Maestro. The environment variables are set correctly in this case, so there are no issues with Cyrillic.

Thank you once again, closing the issue by now.