=begin pod

=TITLE Entering Unicode Characters

=SUBTITLE Input methods for unicode characters in editors and the shell

Perl 6 allows the use of unicode characters as variable names.  Many
operators are defined with unicode symbols (in particular the L<set/bag
operators|/language/setbagmix#Set%2FBag_Operators>) as well as some quoting
constructs.  Hence it is good to know how to enter these symbols into editors,
the Perl 6 shell and the command line, especially if the symbols aren't
available as actual characters on a keyboard.

General information about entering unicode under various operating systems
and environments can be found on the Wikipedia L<unicode input
page|https://en.wikipedia.org/wiki/Unicode_input>.

=head1 XCompose

Xorg includes digraph support using a
L<I<Compose key>|https://en.wikipedia.org/wiki/Compose_key#GNU.2FLinux> .
The default of C<AltGr + Shift> can be remapped to something easier such as
C<Capslock>. In I<GNOME 2> and I<MATE> this can be setup under
C<Preferences → Keyboard → Layouts → Options → Position of Compose Key>.
So, for example, to input C<»+«> you could type
C<< CAPSLOCK > > + CAPSLOCK < < >>

I<XCompose> allows customising the digraph sequences using a C<.XCompose>
file and
L<https://github.com/kragen/xcompose/blob/master/dotXCompose> is an extremely
complete one. In I<GNOME>, I<XCompose> was overridden and replaced with a
hardcoded list, but it is possible to restore I<XCompose> by setting
C<GTK_IM_MODULE=xim> in your environment. It might be necessary to install
a xim bridge as well, such as C<uim-xim>.

=head1 Editors and shells

=head2 Vim

In L<Vim|http://www.vim.org/>, unicode characters are entered (in
insert-mode) by pressing first C<Ctrl-V> (also denoted C<^V>), then C<u> and
then the hexadecimal value of the unicode character to be entered.  For
example, the Greek letter λ (lambda) is entered via the key combination:

    ^Vu03BB

You can also use C<Ctrl-K>/C<^K> along with a digraph to type in some
characters.  So an alternative to the above using digraphs looks like this:

    ^Kl*

The list of digraphs Vim provides is documented
L<here|http://vimdoc.sourceforge.net/htmldoc/digraph.html>; you can add
your own with the C<:digraph> command.

Further information about entering special characters in Vim can be found on
the Vim Wikia page about L<entering special
characters|http://vim.wikia.com/wiki/Entering_special_characters>.

=head2 Emacs

In L<Emacs|http://www.gnu.org/software/emacs/>, unicode characters are
entered by first entering the chord C<C-x 8 RET> at which point the
text C<Unicode (name or hex):> appears in the minibuffer.  One then enters
the unicode code point hexadecimal number followed by the enter key.  The
unicode character will now appear in the document.  Thus, to enter the Greek
letter λ (lambda), one uses the following key combination:

    C-x 8 RET 3bb RET

Further information about unicode and its entry into Emacs can be found on
the L<Unicode Encoding Emacs wiki
page|https://www.emacswiki.org/emacs/UnicodeEncoding>.

You can also use L<RFC 1345|https://tools.ietf.org/html/rfc1345> character
mnemonics by typing:

    C-x RET C-\ rfc1345 RET

Or C<C-u C-\ rfc1345 RET>.

To type special characters, type C<&> followed by a mnemonic.
Emacs will show the possible characters in the echo area.
For example, Greek letter λ (lambda) can be entered by typing:

    &l*

You can use C<C-\> to toggle
L<input method|https://www.gnu.org/software/emacs/manual/html_node/emacs/Select-Input-Method.html>.

Another L<input method|https://www.emacswiki.org/emacs/InputMethods>
you can use to insert special characters is
L<TeX|https://www.emacswiki.org/emacs/TeXInputMethod>.
Select it by typing C<C-u C-\ TeX RET>. You can enter a special character
by using a prefix such as C<\>. For example, to enter λ, type:

    \lambda

To view characters and sequences provided by an input method,
run the C<describe-input-method> command:

    C-h I TeX

=head2 Unix shell

At the bash shell, one enters unicode characters by using entering
C<Ctrl-Shift-u>, then the unicode code point value followed by enter.  For
instance, to enter the character for the element-of operator (∈) use the
following key combination (whitespace has been added for clarity):

    Ctrl-Shift-u 2208 Enter

This also the method one would use to enter unicode characters into the
C<perl6> REPL, if one has started the REPL inside a Unix shell.

=head2 Screen

L<GNU Screen|https://www.gnu.org/software/screen/> does sport a B<digraph>
command but with a rather limited digraph table. Thanks to B<bindkey> and
B<exec> an external program can be used to insert characters to the current
screen window.

    bindkey ^K exec .! digraphs

This will bind ctrk-k to the shell command digraphs. You can use
L<digraphs|https://github.com/gfldex/digraphs> if you prefer a Perl 6 friendly
digraph table over L<RFC 1345|https://tools.ietf.org/html/rfc1345> or change it
to your needs.

=head1 Unicode characters useful in Perl 6

=head2 L<Guillemets|https://en.wikipedia.org/wiki/Guillemet>

These characters are used in French and German as quotation marks. In Perl 6
they are used as L<interpolation word quotes|/language/quoting#Word_quoting_with_interpolation_and_quote_protection:_qqww>,
L<hyper operators|/language/operators#Hyper_Operators> and as bracket
alternative in POD6.

=begin table
    symbol   unicode code point   ascii equivalent
    ------   ------------------   ----------------
    «        U+00AB               <<
    »        U+00BB               >>
=end table

Thus constructs such as these are now possible:

    C« fixed-width POD text »
    say (1, 2) »+« (3, 4);     # (4 6) - element-wise add
    @array »+=» 42;            # add 42 to each element of @array
    say «moo»;                 # moo
    my $baa = 123; say «$baa»; # (123)

=head2 Set/bag operators

The L<set/bag operators|/language/setbagmix#Set%2FBag_Operators> operators all
have set-theory-related symbols, the unicode code points and their ascii
equivalents are listed below.  To compose such a character, it is merely
necessary to enter the character composition chord (e.g. C<Ctrl-V u> in Vim;
C<Ctrl-Shift-u> in Bash) then the unicode code point hexadecimal number.

=begin table
    operator   unicode code point   ascii equivalent
    --------   ------------------   ----------------
    ∈          U+2208               (elem)
    ∉          U+2209               !(elem)
    ∋          U+220B               (cont)
    ∌          U+220C               !(cont)
    ⊆          U+2286               (<=)
    ⊈          U+2288               !(<=)
    ⊂          U+2282               (<)
    ⊄          U+2284               !(<)
    ⊇          U+2287               (>=)
    ⊉          U+2289               !(>=)
    ⊃          U+2283               (>)
    ⊅          U+2285               !(>)
    ≼          U+227C               (<+)
    ≽          U+227D               (>+)
    ∪          U+222A               (|)
    ∩          U+2229               (&)
    ∖          U+2216               (-)
    ⊖          U+2296               (^)
    ⊍          U+228D               (.)
    ⊎          U+228E               (+)
=end table

=head2 Mathematical symbols

Wikipedia contains a full list of L<mathematical operators and symbols in
unicode|https://en.wikipedia.org/wiki/Mathematical_operators_and_symbols_in_Unicode>
as well as links to their mathematical meaning.

=head2 Greek characters

Greek characters may be used as variable names.  For a list of Greek and
Coptic characters and their unicode code points see the L<Greek in Unicode
Wikipedia article|https://en.wikipedia.org/wiki/Greek_alphabet#Greek_in_Unicode>.

For example, to assign the value 3 to π, enter the following in Vim
(whitespace added to the compose sequences for clarity):

=for code :allow<B>
    my $B<Ctrl-V u 03C0> = 3;  # same as: my $π = 3;
    say $B<Ctrl-V u 03C0>;     # 3    same as: say $π;

=head2 Superscripts and subscripts

A limited set of
L<superscripts and subscripts|https://en.wikipedia.org/wiki/Superscripts_and_Subscripts>
can be created directly in unicode by using the C<U+207x>, C<U+208x> and
(less often) the C<U+209x> ranges.  However, to produce a value squared (to
the power of 2) or cubed (to the power of 3), one needs to use C<U+00B2> and
C<U+00B3> since these are defined in the
L<Latin1 supplement Unicode block|https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)>.

Thus, to write the L<Taylor series|https://en.wikipedia.org/wiki/Taylor_series>
expansion around zero of the function C<exp(x)> one would input into e.g.
vim the following:

=for code :allow<B>
    exp(x) = 1 + x + xB<Ctrl-V u 00B2>/2! + xB<Ctrl-V u 00B3>/3!
    + ... + xB<Ctrl-V u 207F>/n!
    # which would appear as
    exp(x) = 1 + x + x²/2! + x³/3! + ... + xⁿ/n!

Or to specify the elements in a list from C<1> up to C<k>:

=for code :allow<B>
    AB<Ctrl-V u 2081>, AB<Ctrl-V u 2082>, ..., AB<Ctrl-V u 2096>
    # which would appear as
    A₁, A₂, ..., Aₖ

=end pod
