Wait the light to fall

如何在 Raku 中输入 Unicode

焉知非鱼

How to Type Unicode in Raku

Raku 有非常好的 Unicode 支持, 但是输入 Unicode 一直是一个很大的问题。在 REPL 中, 输入 Unicode 都不太方便。下面介绍几种方法来方便地输入 Unicode。

Comma

最方便的是使用 Raku 自己的 IDE, Comma。但是需要做两个设置:

首先, 切换到 Comma IDE, 点击左上角菜单栏的 Comma 选项, 在弹出的 Preference 窗口中, 找到左侧菜单栏中的 Editor 选项, 点击 Code Style 中的 Raku, 在右侧的窗口的最上面, 找到 Schema 一行, 点击倒三角图标, 在下拉菜单中选择 Project。然后在第二行文字中找到 Editor Behavior, 勾选 Convert operators to Unicode。完成后点击右下角的 Apply, 然后点击 OK

这样当你在 Comma 中键入 >> 这样的运算符后, Comma 会自动把它转为 Unicode 形式的 »。目前已支持大部分 Unicode 运算符了。Comma 是跨平台的 Raku 编辑器, 支持 Windows、Linux 和 MacOS, 所以你想跨平台开发的话, Comma 值得一试。

Kitty

如果你想在 Vim 中开发 Raku, 又不想失去输入 Unicode 的乐趣, 请使用 Kitty 终端。点开 Kitty 终端, 使用快捷键 Ctrl + Shift + U 即可触发输入 Unnicode 的界面(也可以键入命令 kitty +kitten unicode_input 来触发)。再通过快捷键 F1、F2、F3 和 F4 (或通过 Ctrl+[ 和 Ctrl+] 切换标签)可以分别按代码搜索 Unicode、按名字搜索 Unicode、按表情搜索和收藏。

例如在 REPL 中键入 Unicode 字符:

$ raku
Welcome to 𝐑𝐚𝐤𝐮𝐝𝐨™ v2021.02.
Implementing the 𝐑𝐚𝐤𝐮™ programming language v6.d.
Built on MoarVM version 2021.02.

To exit type 'exit' or '^D'
> my @a = 1..5;
> @a».sin

借助 Kitty, 数组 @a 后面的 » 可以这样输入, 先输入 @a, 然后使用快捷键 Shift + Ctrl + U 调出 Kitty 的 Unicode input 界面, 一般有两种方法, 第一种是通过快捷键 F1, 然后输入对应的 Unicode 编码, 再回车。第二种是通过快捷键 F2, 通过名字搜索, 例如输入单词 quota, 会出现一堆引号符号供你选择, 按上下方向键选中 », 再回车即可。Vim 中同理。

Julia REPL

在 REPL 中输入 \′ + 单词 + Tab, 例如输入 \heartsuit` 再按 Tab, 就会自动变成 ♡

julia> \heartsuit

IPython

\^ 输入上标, \_p 输入下标。

$ ipython
\^a
\_p

VScode

\\′ + 完整的单词 + Tab, VsCode 会自动转换为 Unicode。 例如 ^a_p`

dmenu

#!/usr/bin/perl

#use strict;
#use warnings;
#use autodie;
#use utf8;


open $FH, " | dmenu -i -l 30 -fn '-xos4-terminus-medium-r-*-*-10-*' | sed 's/^\\(.\\).*/\\1/' | tr -d '\n' | { pbcopy; pbpaste; } ";

# if you do not have the xos4-terminus font that makes characters a little bigger :
# uncoment this line and use the second here document
#open $FH, " | dmenu -i -l 30 | sed 's/^\\(.\\).*/\\1/' | tr -d '\n' | { xsel -ib; xsel -ip; } ";

# also you can compile dmenu from source and change the font size directly in the C done
# in the file config.h / config.def.h

# this heredoc is well aligned with the -xos4-terminus-medium-r-*-*-10-* font

print $FH <<'EOF'
«         hyper operator
»         hyper operator
∈        membership
∉        non-membership
∪         set union
∩         set intersection
∖         set difference
≡         set equality
≢        set inequality
⊖        symmetric set difference
⊍         baggy multiplication
⊎         baggy addition
∅        empty set
⊆        subset
⊈         not a subset
⊂        strict subset
⊄        not a strict subset
∋        reverse membership
∌          reverse non-membership
⊇        superset
⊉         not a superset
⊃        strict superset
⊅        not a strict superset
∞         infinity
⚛        atomic operator
π        pi
τ         tau
𝑒         Euler's number
∘         function composition
×        multiplication 
÷        division
≤         inferior or equal
≥         greater or equal
≠   	     inequality
−        substraction
≅       approximatively equal
…      sequence operator
‘         left single quotation mark
’         right single quotation mark
‚         single low-9 quotation mark 
”         right double quotation mark
“         left double quotation mark
„         double low-9 quotation mark
「         halfwidth left corner bracket
」         halfwidth right corner bracket
⁺         plus superscript
⁻         minus superscript
⁰         0 superscript
¹         1 superscript 
²         2 superscript 
³         3 superscript 
⁴         4 superscript 
⁵         5 superscript 
⁶         6 superscript 
⁷         7 superscript 
⁸         8 superscript 
⁹         9 superscript 
EOF
;

# use this without -xos4-terminus-medium-r-*-*-10-* font
# the default font may change given which font packages are installed and given the font configurations

#print $FH <<'EOF'
#«         hyper operator
#»         hyper operator
#∈         membership
#∉         non-membership
#∪         set union
#∩         set intersection
#∖         set difference
#≡         set equality
#≢         set inequality
#⊖         symmetric set difference
#⊍         baggy multiplication
#⊎         baggy addition
#∅         empty set
#⊆         subset
#⊈         not a subset
#⊂         strict subset
#⊄         not a strict subset
#∋         reverse membership
#∌         reverse non-membership
#⊇         superset
#⊉         not a superset
#⊃         strict superset
#⊅         not a strict superset
#∞         infinity
#⚛         atomic operator
#π         pi
#τ         tau
#𝑒         Euler's number
#∘         function composition
#×         multiplication 
#÷         division
#≤         inferior or equal
#≥         greater or equal
#≠         inequality
#−         substraction
#≅         approximatively equal
#…         sequence operator
#‘         left single quotation mark
#’         right single quotation mark
#‚         single low-9 quotation mark 
#”         right double quotation mark
#“         left double quotation mark
#„         double low-9 quotation mark
#「         halfwidth left corner bracket
#」         halfwidth right corner bracket
#⁺         plus superscript
#⁻         minus superscript
#⁰         0 superscript
#¹         1 superscript 
#²         2 superscript 
#³         3 superscript 
#⁴         4 superscript 
#⁵         5 superscript 
#⁶         6 superscript 
#⁷         7 superscript 
#⁸         8 superscript 
#⁹         9 superscript 
#EOF
#;



close $FH;



__END__


=pod 

=head1 NAME

lazy_raku_unicode.pl

=head1 SYNOPSIS

This script allows you to select a Unicode symbol from a drop-down menu that you can simply paste into your text editor, terminal or IDE.

All it does is send a heredoc through a pipe to dmenu, and copy to the clipboard the character that you selected.

Dmenu is a program that reads lines from stdin, display them in a menu, and write to stdout the lines selected.

It is not practical to execute this script from a terminal each time you want a character though. This is why it should be executed by sxhkd.

Sxhkd is the program that will call this script each time you press the right key combination.

Only the most useful Unicode characters are present to not make dmenu get too slow. (It is usually very fast when there is only ascii text.)

This include most of the Raku operators that have a Unicode version, plus some quoting characters, superscripts, and mathematical constants.

=head1 MANUAL INSTALLATION STEPS


=head2 INSTALLING DMENU

B<On Debian or Ubuntu :>

	sudo apt install suckless-tools

B<On Archlinux :>

	sudo pacman -Sy dmenu

B<Compile it from source :>

	https://tools.suckless.org/dmenu/

=head2 CHANGING DMENU FONT SIZE (OPTIIONAL)

You have only two ways of changing the font size :

Change the default font with the B<-fn> flag (See dmenu(1) for the specifics).
	
	dmenu -l 30 -fn '-xos4-terminus-medium-r-*-*-10-*'


Modify directly the C source code of dmenu. Only the files B<config.h / config.def.h>.

Simply edit this line and change 10 by 14 for example.

	static const char *fonts[] = {
		"monospace:size=10"
	};

Then execute sudo make install and you're ready to go.

=head2 INSTALLING SXHKD


B<On Debian or Ubuntu :>

	sudo apt install sxhkd

B<On Archlinux :>

	sudo pacman -Sy sxhkd


=head2 CONFIGURING SXHKD

B<Autostarting sxhkd at startup :>

simply put this line in B<~/.xprofile> :

	sxhkd &


B<Setting the keybinding :>
	

Add these two lines in B<~/.config/sxhkd/sxhkdrc>

	ctrl + apostrophe
		/path/to/lazy_raku_unicode.pl

The keybinding have to start at the beginning of a line, and the command has to follow on the next line and be preceded by a tabulation.

To make sxhkd reload the config file, either kill it and relaunch it, or send a SIGUSR1 signal. This command can do it :

	pidof sxhkd | kill -SIGUSR1 $(cat /dev/stdin)

If you want to use a different keybinding, please see sxhkd(1) and use xev(1) to find the name of keys.

Here the name of some of the keys : ctrl, super, alt, Return .

=head1 SEE ALSO

dmenu(1), sxhkd(1), xev(1)


=cut

参考链接: https://docs.raku.org/language/unicode_entry