about summary refs log tree commit diff
path: root/Functions/Zle/insert-composed-char
blob: 7b97c96d48cd0841c1a4005edb813f4e7829af1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Accented characters.  Inputs two keys.  There are two types: those
# with a base character followed by an accent (see below for codes for
# accents), and those with a two-character mnemonic for the composed
# character.  These are (with the exception of the Euro) the codes
# given by RFC 1345.  Note that some codes in RFC 1345 require three
# characters to be input; none of these are handled.
#
# For best results zsh should have been built with support for
# multibyte characters (--enable-multibyte), but single character sets
# also work.
#
# Outputs the character converted from Unicode into the local representation.
# (The conversion is done within the shell, using whatever facilities
# the C library provides.)
#
# When used as a zle widget, the character is inserted at the cursor
# position.  With a numeric argument, preview in status line; outside zle,
# print character (and newline) to standard output.
#
# The set of accented characters is reasonably complete up to U+0180, the
# set of special characters less so.  However, it mostly gives up at that
# point.  Adding new Unicode characters is easy, however.  Please send any
# additions to zsh-workers@sunsite.dk .
#
# Some of the accent codes are a little more obscure than others.
#  !   Grave
#  '   Acute
#  >   Circumflex
#  ?   Tilde
#  -   Macron.  (A horizonal bar over the letter.)
#  (   Breve.  (A shallow dish shape over the letter.)
#  .   Dot above, or no dot with lower case i, or dot in the middle of L or l.
#  :   Diaeresis (Umlaut)
#  ,   Cedilla
#  _   Underline (none of these currently)
#  /   Stroke through character
#  "   Double acute
#  ;   Ogonek.  (A little forward facing hook at the bottom right
#      of the character.)
#  <   Caron.  (A little v over the letter.)
#  0   Circle
#  2   Hook
#  9   Horn
# Hence A! is upper case A with a grave, c, is lower case c with cedilla.
#
# Some other composed charaters:
# Various ligatures:
#  AE ae OE oe IJ ij
#
# ASCII characters not on all keyboards:
#  <(           [
#  //           \
#  )>           ]
#  (!           {
#  !!           |
#  !)           }
#  '?           ~
#
# Special letters:
#  ss		Eszett (schafes S)
#  D- d- TH th  Eth and thorn
#  kk           kra
#  'n           'n
#  NG ng        ng
#  OI oi        OI
#  yr           yr
#  ED           ezh
#
# Currency symbols:
#  Ct           Cent
#  Pd           Pound sterling
#  Cu           Currency
#  Ye           Yen
#  Eu           Euro (not in RFC 1345 but logical)
#
# Punctuation
#  !I           Inverted !
#  BB           Broken vertical bar
#  SE           Section
#  Co           Copyright
#  -a           Spanish feminine ordinal indicator
#  <<           Left guillemet
#  --           Soft hyphen
#  Rg           Registered trade mark
#  PI           Pilcrow (paragraph)
#  -o           Spanish masculine ordinal indicator
#  >>           Right guillemet
#  ?I           Inverted question mark
#  -1           Hyphen
#  -N           en dash
#  -M           em dash
#  -3           horizontal bar
#  :3           vertical ellipsis
#  .3           horizontal midline ellipsis
#  !2           double vertical line
#  =2           double low line
#  '6           Left single quote
#  '9           Right single quote
#  .9           "Right" low quote
#  9'           Reversed "right" quote
#  "6           Left double quote
#  "9           Right double quote
#  :9           "Right" low double quote
#  9"           Reversed "right" double quote
#  /-           Dagger
#  /=           Double dagger
#
# Mathematical
#  DG           Degree
#  +-           +/-
#  2S           Superscript 2
#  3S           Superscript 3
#  My           Micro
#  .M           Middle dot
#  1S           Superscript 1
#  14           Quarter
#  12           Half
#  34           Three quarters
#  *X           Multiplication
#  -:           Division
#  %0           Per mille
#
# Accents with no base character
# '>            Circumflex (caret)
# '!            Grave (backtick)
# ',            Cedilla
# ':            Diaeresis (Umlaut)
# 'm            Macron
# ''            Acute

emulate -LR zsh
setopt cbases extendedglob printeightbit

local accent basechar ochar error

if [[ -n $WIDGET ]]; then
  error=(zle -M)
else
  error=print
fi

if (( ${+zsh_accented_chars} == 0 )); then
  # Save quite a lot of memory by running and then erasing
  # the function that defines the characters.
  autoload -U define-composed-chars
  define-composed-chars
  unfunction define-composed-chars
fi

read -k basechar || return 1
read -k accent || return 1

local -A charmap
charmap=(${=zsh_accented_chars[$accent]})

if [[ ${#charmap} -eq 0 || -z $charmap[$basechar] ]]; then
  $error "Combination ${basechar}${accent} is not available."
  return 1
fi

if [[ -z $WIDGET ]]; then
  [[ -t 1 ]] && print
  print "\U${(l.8..0.)charmap[$basechar]}"
else
  ochar="$(print -n "\U${(l.8..0.)charmap[$basechar]}")"

  if (( ${+NUMERIC} )); then
    $error "Character ${(l.8..0.)charmap[$basechar]}: $ochar"
  else
    LBUFFER+=$ochar
  fi
fi