Issue #311 - Clean up blowfish.c/h, charset.c/h, diff.c/h, digraph.c/h, garray.c/h, hashtab.c/h, popupmnu.c/h, sha256.c/h, version.c/h. Update uncrustify to move logical operators to the beginning of the line when splitting. Also, clean up arabic.c/h and farsi.c/h

This commit is contained in:
David Z. Chen
2014-03-10 03:48:12 -07:00
committed by Thiago de Arruda
parent 8a6118ccac
commit baab238271
23 changed files with 5929 additions and 5251 deletions

View File

@@ -1,25 +1,11 @@
/* vi:set ts=8 sts=4 sw=4: /// @file arabic.c
* ///
* VIM - Vi IMproved by Bram Moolenaar /// Functions for Arabic language
* ///
* Do ":help uganda" in Vim to read copying and usage conditions. /// Included by main.c, when FEAT_ARABIC & FEAT_GUI is defined.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
#include "arabic.h" #include "arabic.h"
/*
* arabic.c: functions for Arabic language
*
* Included by main.c, when FEAT_ARABIC & FEAT_GUI is defined.
*
* --
*
* Author: Nadim Shaikli & Isam Bayazidi
*
*/
static int A_is_a(int cur_c); static int A_is_a(int cur_c);
static int A_is_s(int cur_c); static int A_is_s(int cur_c);
static int A_is_f(int cur_c); static int A_is_f(int cur_c);
@@ -40,9 +26,7 @@ static int A_is_ok(int c);
static int A_is_valid(int c); static int A_is_valid(int c);
static int A_is_special(int c); static int A_is_special(int c);
/* // Returns True if c is an ISO-8859-6 shaped ARABIC letter (user entered).
* Returns True if c is an ISO-8859-6 shaped ARABIC letter (user entered)
*/
static int A_is_a(int cur_c) static int A_is_a(int cur_c)
{ {
switch (cur_c) { switch (cur_c) {
@@ -89,9 +73,7 @@ static int A_is_a(int cur_c)
return FALSE; return FALSE;
} }
/* // Returns True if c is an Isolated Form-B ARABIC letter
* Returns True if c is an Isolated Form-B ARABIC letter
*/
static int A_is_s(int cur_c) static int A_is_s(int cur_c)
{ {
switch (cur_c) { switch (cur_c) {
@@ -137,9 +119,7 @@ static int A_is_s(int cur_c)
return FALSE; return FALSE;
} }
/* // Returns True if c is a Final shape of an ARABIC letter
* Returns True if c is a Final shape of an ARABIC letter
*/
static int A_is_f(int cur_c) static int A_is_f(int cur_c)
{ {
switch (cur_c) { switch (cur_c) {
@@ -187,9 +167,7 @@ static int A_is_f(int cur_c)
return FALSE; return FALSE;
} }
/* // Change shape - from ISO-8859-6/Isolated to Form-B Isolated
* Change shape - from ISO-8859-6/Isolated to Form-B Isolated
*/
static int chg_c_a2s(int cur_c) static int chg_c_a2s(int cur_c)
{ {
int tempc; int tempc;
@@ -243,7 +221,7 @@ static int chg_c_a2s(int cur_c)
tempc = a_s_ZAIN; tempc = a_s_ZAIN;
break; break;
case a_TATWEEL: /* exceptions */ case a_TATWEEL: // exceptions
tempc = cur_c; tempc = cur_c;
break; break;
@@ -350,9 +328,7 @@ static int chg_c_a2s(int cur_c)
return tempc; return tempc;
} }
/* // Change shape - from ISO-8859-6/Isolated to Initial
* Change shape - from ISO-8859-6/Isolated to Initial
*/
static int chg_c_a2i(int cur_c) static int chg_c_a2i(int cur_c)
{ {
int tempc; int tempc;
@@ -362,59 +338,59 @@ static int chg_c_a2i(int cur_c)
tempc = a_i_YEH_HAMZA; tempc = a_i_YEH_HAMZA;
break; break;
case a_HAMZA: /* exceptions */ case a_HAMZA: // exceptions
tempc = a_s_HAMZA; tempc = a_s_HAMZA;
break; break;
case a_ALEF_MADDA: /* exceptions */ case a_ALEF_MADDA: // exceptions
tempc = a_s_ALEF_MADDA; tempc = a_s_ALEF_MADDA;
break; break;
case a_ALEF_HAMZA_ABOVE: /* exceptions */ case a_ALEF_HAMZA_ABOVE: // exceptions
tempc = a_s_ALEF_HAMZA_ABOVE; tempc = a_s_ALEF_HAMZA_ABOVE;
break; break;
case a_WAW_HAMZA: /* exceptions */ case a_WAW_HAMZA: // exceptions
tempc = a_s_WAW_HAMZA; tempc = a_s_WAW_HAMZA;
break; break;
case a_ALEF_HAMZA_BELOW: /* exceptions */ case a_ALEF_HAMZA_BELOW: // exceptions
tempc = a_s_ALEF_HAMZA_BELOW; tempc = a_s_ALEF_HAMZA_BELOW;
break; break;
case a_ALEF: /* exceptions */ case a_ALEF: // exceptions
tempc = a_s_ALEF; tempc = a_s_ALEF;
break; break;
case a_TEH_MARBUTA: /* exceptions */ case a_TEH_MARBUTA: // exceptions
tempc = a_s_TEH_MARBUTA; tempc = a_s_TEH_MARBUTA;
break; break;
case a_DAL: /* exceptions */ case a_DAL: // exceptions
tempc = a_s_DAL; tempc = a_s_DAL;
break; break;
case a_THAL: /* exceptions */ case a_THAL: // exceptions
tempc = a_s_THAL; tempc = a_s_THAL;
break; break;
case a_REH: /* exceptions */ case a_REH: // exceptions
tempc = a_s_REH; tempc = a_s_REH;
break; break;
case a_ZAIN: /* exceptions */ case a_ZAIN: // exceptions
tempc = a_s_ZAIN; tempc = a_s_ZAIN;
break; break;
case a_TATWEEL: /* exceptions */ case a_TATWEEL: // exceptions
tempc = cur_c; tempc = cur_c;
break; break;
case a_WAW: /* exceptions */ case a_WAW: // exceptions
tempc = a_s_WAW; tempc = a_s_WAW;
break; break;
case a_ALEF_MAKSURA: /* exceptions */ case a_ALEF_MAKSURA: // exceptions
tempc = a_s_ALEF_MAKSURA; tempc = a_s_ALEF_MAKSURA;
break; break;
@@ -513,31 +489,29 @@ static int chg_c_a2i(int cur_c)
return tempc; return tempc;
} }
/* // Change shape - from ISO-8859-6/Isolated to Medial
* Change shape - from ISO-8859-6/Isolated to Medial
*/
static int chg_c_a2m(int cur_c) static int chg_c_a2m(int cur_c)
{ {
int tempc; int tempc;
switch (cur_c) { switch (cur_c) {
case a_HAMZA: /* exception */ case a_HAMZA: // exception
tempc = a_s_HAMZA; tempc = a_s_HAMZA;
break; break;
case a_ALEF_MADDA: /* exception */ case a_ALEF_MADDA: // exception
tempc = a_f_ALEF_MADDA; tempc = a_f_ALEF_MADDA;
break; break;
case a_ALEF_HAMZA_ABOVE: /* exception */ case a_ALEF_HAMZA_ABOVE: // exception
tempc = a_f_ALEF_HAMZA_ABOVE; tempc = a_f_ALEF_HAMZA_ABOVE;
break; break;
case a_WAW_HAMZA: /* exception */ case a_WAW_HAMZA: // exception
tempc = a_f_WAW_HAMZA; tempc = a_f_WAW_HAMZA;
break; break;
case a_ALEF_HAMZA_BELOW: /* exception */ case a_ALEF_HAMZA_BELOW: // exception
tempc = a_f_ALEF_HAMZA_BELOW; tempc = a_f_ALEF_HAMZA_BELOW;
break; break;
@@ -545,7 +519,7 @@ static int chg_c_a2m(int cur_c)
tempc = a_m_YEH_HAMZA; tempc = a_m_YEH_HAMZA;
break; break;
case a_ALEF: /* exception */ case a_ALEF: // exception
tempc = a_f_ALEF; tempc = a_f_ALEF;
break; break;
@@ -553,7 +527,7 @@ static int chg_c_a2m(int cur_c)
tempc = a_m_BEH; tempc = a_m_BEH;
break; break;
case a_TEH_MARBUTA: /* exception */ case a_TEH_MARBUTA: // exception
tempc = a_f_TEH_MARBUTA; tempc = a_f_TEH_MARBUTA;
break; break;
@@ -577,19 +551,19 @@ static int chg_c_a2m(int cur_c)
tempc = a_m_KHAH; tempc = a_m_KHAH;
break; break;
case a_DAL: /* exception */ case a_DAL: // exception
tempc = a_f_DAL; tempc = a_f_DAL;
break; break;
case a_THAL: /* exception */ case a_THAL: // exception
tempc = a_f_THAL; tempc = a_f_THAL;
break; break;
case a_REH: /* exception */ case a_REH: // exception
tempc = a_f_REH; tempc = a_f_REH;
break; break;
case a_ZAIN: /* exception */ case a_ZAIN: // exception
tempc = a_f_ZAIN; tempc = a_f_ZAIN;
break; break;
@@ -625,7 +599,7 @@ static int chg_c_a2m(int cur_c)
tempc = a_m_GHAIN; tempc = a_m_GHAIN;
break; break;
case a_TATWEEL: /* exception */ case a_TATWEEL: // exception
tempc = cur_c; tempc = cur_c;
break; break;
@@ -657,11 +631,11 @@ static int chg_c_a2m(int cur_c)
tempc = a_m_HEH; tempc = a_m_HEH;
break; break;
case a_WAW: /* exception */ case a_WAW: // exception
tempc = a_f_WAW; tempc = a_f_WAW;
break; break;
case a_ALEF_MAKSURA: /* exception */ case a_ALEF_MAKSURA: // exception
tempc = a_f_ALEF_MAKSURA; tempc = a_f_ALEF_MAKSURA;
break; break;
@@ -676,25 +650,22 @@ static int chg_c_a2m(int cur_c)
return tempc; return tempc;
} }
/* // Change shape - from ISO-8859-6/Isolated to final
* Change shape - from ISO-8859-6/Isolated to final
*/
static int chg_c_a2f(int cur_c) static int chg_c_a2f(int cur_c)
{ {
int tempc; int tempc;
/* NOTE: these encodings need to be accounted for // NOTE: these encodings need to be accounted for
//
a_f_ALEF_MADDA; // a_f_ALEF_MADDA;
a_f_ALEF_HAMZA_ABOVE; // a_f_ALEF_HAMZA_ABOVE;
a_f_ALEF_HAMZA_BELOW; // a_f_ALEF_HAMZA_BELOW;
a_f_LAM_ALEF_MADDA_ABOVE; // a_f_LAM_ALEF_MADDA_ABOVE;
a_f_LAM_ALEF_HAMZA_ABOVE; // a_f_LAM_ALEF_HAMZA_ABOVE;
a_f_LAM_ALEF_HAMZA_BELOW; // a_f_LAM_ALEF_HAMZA_BELOW;
*/
switch (cur_c) { switch (cur_c) {
case a_HAMZA: /* exception */ case a_HAMZA: // exception
tempc = a_s_HAMZA; tempc = a_s_HAMZA;
break; break;
@@ -798,7 +769,7 @@ static int chg_c_a2f(int cur_c)
tempc = a_f_GHAIN; tempc = a_f_GHAIN;
break; break;
case a_TATWEEL: /* exception */ case a_TATWEEL: // exception
tempc = cur_c; tempc = cur_c;
break; break;
@@ -849,9 +820,7 @@ static int chg_c_a2f(int cur_c)
return tempc; return tempc;
} }
/* // Change shape - from Initial to Medial
* Change shape - from Initial to Medial
*/
static int chg_c_i2m(int cur_c) static int chg_c_i2m(int cur_c)
{ {
int tempc; int tempc;
@@ -956,24 +925,21 @@ static int chg_c_i2m(int cur_c)
return tempc; return tempc;
} }
/* // Change shape - from Final to Medial
* Change shape - from Final to Medial
*/
static int chg_c_f2m(int cur_c) static int chg_c_f2m(int cur_c)
{ {
int tempc; int tempc;
switch (cur_c) { switch (cur_c) {
/* NOTE: these encodings are multi-positional, no ? // NOTE: these encodings are multi-positional, no ?
case a_f_ALEF_MADDA: // case a_f_ALEF_MADDA:
case a_f_ALEF_HAMZA_ABOVE: // case a_f_ALEF_HAMZA_ABOVE:
case a_f_ALEF_HAMZA_BELOW: // case a_f_ALEF_HAMZA_BELOW:
*/
case a_f_YEH_HAMZA: case a_f_YEH_HAMZA:
tempc = a_m_YEH_HAMZA; tempc = a_m_YEH_HAMZA;
break; break;
case a_f_WAW_HAMZA: /* exceptions */ case a_f_WAW_HAMZA: // exceptions
case a_f_ALEF: case a_f_ALEF:
case a_f_TEH_MARBUTA: case a_f_TEH_MARBUTA:
case a_f_DAL: case a_f_DAL:

View File

@@ -1,37 +1,20 @@
/* vi:set ts=8 sts=4 sw=4: /// @file arabic.h
* ///
* VIM - Vi IMproved by Bram Moolenaar /// Arabic characters are categorized into following types:
* ///
* Do ":help uganda" in Vim to read copying and usage conditions. /// Isolated - iso-8859-6 form char denoted with a_*
* Do ":help credits" in Vim to see a list of people who contributed. /// Initial - unicode form-B start char denoted with a_i_*
*/ /// Medial - unicode form-B middle char denoted with a_m_*
/// Final - unicode form-B final char denoted with a_f_*
/// Stand-Alone - unicode form-B isolated char denoted with a_s_* (NOT USED)
///
#ifndef SRC_ARABIC_H_ #ifndef NEOVIM_ARABIC_H
#define SRC_ARABIC_H_ #define NEOVIM_ARABIC_H
/* // Arabic ISO-10646-1 character set definition
* Arabic characters are categorized into following types:
*
* Isolated - iso-8859-6 form char denoted with a_*
* Initial - unicode form-B start char denoted with a_i_*
* Medial - unicode form-B middle char denoted with a_m_*
* Final - unicode form-B final char denoted with a_f_*
* Stand-Alone - unicode form-B isolated char denoted with a_s_* (NOT USED)
*
* --
*
* Author: Nadim Shaikli & Isam Bayazidi
* - (based on Unicode)
*
*/
/* // Arabic ISO-8859-6 (subset of 10646; 0600 - 06FF)
* Arabic ISO-10646-1 character set definition
*/
/*
* Arabic ISO-8859-6 (subset of 10646; 0600 - 06FF)
*/
#define a_COMMA 0x060C #define a_COMMA 0x060C
#define a_SEMICOLON 0x061B #define a_SEMICOLON 0x061B
#define a_QUESTION 0x061F #define a_QUESTION 0x061F
@@ -101,17 +84,15 @@
#define a_THOUSANDS 0x066c #define a_THOUSANDS 0x066c
#define a_STAR 0x066d #define a_STAR 0x066d
#define a_MINI_ALEF 0x0670 #define a_MINI_ALEF 0x0670
/* Rest of 8859-6 does not relate to Arabic */ // Rest of 8859-6 does not relate to Arabic
/* // Arabic Presentation Form-B (subset of 10646; FE70 - FEFF)
* Arabic Presentation Form-B (subset of 10646; FE70 - FEFF) //
* // s -> isolated
* s -> isolated // i -> initial
* i -> initial // m -> medial
* m -> medial // f -> final
* f -> final //
*
*/
#define a_s_FATHATAN 0xfe70 #define a_s_FATHATAN 0xfe70
#define a_m_TATWEEL_FATHATAN 0xfe71 #define a_m_TATWEEL_FATHATAN 0xfe71
#define a_s_DAMMATAN 0xfe72 #define a_s_DAMMATAN 0xfe72
@@ -257,7 +238,7 @@
#define a_BYTE_ORDER_MARK 0xfeff #define a_BYTE_ORDER_MARK 0xfeff
/* Range of Arabic characters that might be shaped. */ // Range of Arabic characters that might be shaped.
#define ARABIC_CHAR(c) ((c) >= a_HAMZA && (c) <= a_MINI_ALEF) #define ARABIC_CHAR(c) ((c) >= a_HAMZA && (c) <= a_MINI_ALEF)
#endif // SRC_ARABIC_H_ #endif // NEOVIM_ARABIC_H

View File

@@ -16,11 +16,11 @@
#include "message.h" #include "message.h"
#include "sha256.h" #include "sha256.h"
#define ARRAY_LENGTH(A) (sizeof(A)/sizeof(A[0])) #define ARRAY_LENGTH(A) (sizeof(A) / sizeof(A[0]))
#define BF_BLOCK 8 #define BF_BLOCK 8
#define BF_BLOCK_MASK 7 #define BF_BLOCK_MASK 7
#define BF_OFB_LEN (8*(BF_BLOCK)) #define BF_OFB_LEN (8 * (BF_BLOCK))
typedef union { typedef union {
uint32_t ul[2]; uint32_t ul[2];
@@ -305,7 +305,6 @@ static uint32_t sbi[4][256] = {
0xb74e6132u, 0xce77e25bu, 0x578fdfe3u, 0x3ac372e6u} 0xb74e6132u, 0xce77e25bu, 0x578fdfe3u, 0x3ac372e6u}
}; };
#define F1(i) \ #define F1(i) \
xl ^= pax[i]; \ xl ^= pax[i]; \
xr ^= ((sbx[0][xl >> 24] + \ xr ^= ((sbx[0][xl >> 24] + \
@@ -323,7 +322,9 @@ static uint32_t sbi[4][256] = {
static void bf_e_block(uint32_t *p_xl, uint32_t *p_xr) static void bf_e_block(uint32_t *p_xl, uint32_t *p_xr)
{ {
uint32_t temp, xl = *p_xl, xr = *p_xr; uint32_t temp;
uint32_t xl = *p_xl;
uint32_t xr = *p_xr;
F1(0) F2(1) F1(2) F2(3) F1(4) F2(5) F1(6) F2(7) F1(0) F2(1) F1(2) F2(3) F1(4) F2(5) F1(6) F2(7)
F1(8) F2(9) F1(10) F2(11) F1(12) F2(13) F1(14) F2(15) F1(8) F2(9) F1(10) F2(11) F1(12) F2(13) F1(14) F2(15)
@@ -336,15 +337,13 @@ static void bf_e_block(uint32_t *p_xl, uint32_t *p_xr)
*p_xr = xr; *p_xr = xr;
} }
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
# define htonl2(x) \ # define htonl2(x) \
x = ((((x) & 0xffL) << 24) | (((x) & 0xff00L) << 8) | \ x = ((((x) & 0xffL) << 24) | (((x) & 0xff00L) << 8) | \
(((x) & 0xff0000L) >> 8) | (((x) & 0xff000000L) >> 24)) (((x) & 0xff0000L) >> 8) | (((x) & 0xff000000L) >> 24))
#else #else // ifdef WORDS_BIGENDIAN
# define htonl2(x) # define htonl2(x)
#endif #endif // ifdef WORDS_BIGENDIAN
static void bf_e_cblock(char_u *block) static void bf_e_cblock(char_u *block)
{ {
@@ -359,31 +358,28 @@ static void bf_e_cblock(char_u *block)
memcpy(block, bk.uc, 8); memcpy(block, bk.uc, 8);
} }
// Initialize the crypt method using "password" as the encryption key and
/* // "salt[salt_len]" as the salt.
* Initialize the crypt method using "password" as the encryption key and
* "salt[salt_len]" as the salt.
*/
void bf_key_init(char_u *password, char_u *salt, int salt_len) void bf_key_init(char_u *password, char_u *salt, int salt_len)
{ {
int i, j, keypos = 0; // Process the key 1000 times.
unsigned u; // See http://en.wikipedia.org/wiki/Key_strengthening.
uint32_t val, data_l, data_r; char_u *key = sha256_key(password, salt, salt_len);
char_u *key;
int keylen;
/* Process the key 1000 times. int i;
* See http://en.wikipedia.org/wiki/Key_strengthening. */ for (i = 0; i < 1000; i++) {
key = sha256_key(password, salt, salt_len);
for (i = 0; i < 1000; i++)
key = sha256_key(key, salt, salt_len); key = sha256_key(key, salt, salt_len);
}
// Convert the key from 64 hex chars to 32 binary chars.
int keylen = (int)STRLEN(key) / 2;
/* Convert the key from 64 hex chars to 32 binary chars. */
keylen = (int)STRLEN(key) / 2;
if (keylen == 0) { if (keylen == 0) {
EMSG(_("E831: bf_key_init() called with empty password")); EMSG(_("E831: bf_key_init() called with empty password"));
return; return;
} }
unsigned u;
for (i = 0; i < keylen; i++) { for (i = 0; i < keylen; i++) {
sscanf((char *)&key[i * 2], "%2x", &u); sscanf((char *)&key[i * 2], "%2x", &u);
key[i] = u; key[i] = u;
@@ -391,21 +387,27 @@ void bf_key_init(char_u *password, char_u *salt, int salt_len)
mch_memmove(sbx, sbi, 4 * 4 * 256); mch_memmove(sbx, sbi, 4 * 4 * 256);
for (i = 0; i < 18; ++i) { int keypos = 0;
val = 0; for (i = 0; i < 18; i++) {
for (j = 0; j < 4; ++j) uint32_t val = 0;
int j;
for (j = 0; j < 4; j++) {
val = (val << 8) | key[keypos++ % keylen]; val = (val << 8) | key[keypos++ % keylen];
}
pax[i] = ipa[i] ^ val; pax[i] = ipa[i] ^ val;
} }
data_l = data_r = 0; uint32_t data_l = 0;
uint32_t data_r = 0;
for (i = 0; i < 18; i += 2) { for (i = 0; i < 18; i += 2) {
bf_e_block(&data_l, &data_r); bf_e_block(&data_l, &data_r);
pax[i + 0] = data_l; pax[i + 0] = data_l;
pax[i + 1] = data_r; pax[i + 1] = data_r;
} }
for (i = 0; i < 4; ++i) { for (i = 0; i < 4; i++) {
int j;
for (j = 0; j < 256; j += 2) { for (j = 0; j < 256; j += 2) {
bf_e_block(&data_l, &data_r); bf_e_block(&data_l, &data_r);
sbx[i][j + 0] = data_l; sbx[i][j + 0] = data_l;
@@ -414,19 +416,22 @@ void bf_key_init(char_u *password, char_u *salt, int salt_len)
} }
} }
/* /// BF Self test for corrupted tables or instructions
* BF Self test for corrupted tables or instructions static int bf_check_tables(uint32_t a_ipa[18], uint32_t a_sbi[4][256],
*/ uint32_t val)
static int bf_check_tables(uint32_t a_ipa[18], uint32_t a_sbi[4][256], uint32_t val)
{ {
int i, j;
uint32_t c = 0; uint32_t c = 0;
int i;
for (i = 0; i < 18; i++) for (i = 0; i < 18; i++) {
c ^= a_ipa[i]; c ^= a_ipa[i];
for (i = 0; i < 4; i++) }
for (j = 0; j < 256; j++)
for (i = 0; i < 4; i++) {
int j;
for (j = 0; j < 256; j++) {
c ^= a_sbi[i][j]; c ^= a_sbi[i][j];
}
}
return c == val; return c == val;
} }
@@ -435,50 +440,50 @@ typedef struct {
char_u salt[9]; char_u salt[9];
char_u plaintxt[9]; char_u plaintxt[9];
char_u cryptxt[9]; char_u cryptxt[9];
char_u badcryptxt[9]; /* cryptxt when big/little endian is wrong */ char_u badcryptxt[9]; // cryptxt when big/little endian is wrong.
uint32_t keysum; uint32_t keysum;
} struct_bf_test_data; } struct_bf_test_data;
/* // Assert bf(password, plaintxt) is cryptxt.
* Assert bf(password, plaintxt) is cryptxt. // Assert csum(pax sbx(password)) is keysum.
* Assert csum(pax sbx(password)) is keysum.
*/
static struct_bf_test_data bf_test_data[] = { static struct_bf_test_data bf_test_data[] = {
{ {
"password", "password",
"salt", "salt",
"plaintxt", "plaintxt",
"\xad\x3d\xfa\x7f\xe8\xea\x40\xf6", /* cryptxt */ "\xad\x3d\xfa\x7f\xe8\xea\x40\xf6", // cryptxt
"\x72\x50\x3b\x38\x10\x60\x22\xa7", /* badcryptxt */ "\x72\x50\x3b\x38\x10\x60\x22\xa7", // badcryptxt
0x56701b5du /* keysum */ 0x56701b5du // keysum
}, },
}; };
/* // Return FAIL when there is something wrong with blowfish encryption.
* Return FAIL when there is something wrong with blowfish encryption. static int bf_self_test(void)
*/ {
static int bf_self_test(void) {
int i, bn;
int err = 0; int err = 0;
block8 bk; if (!bf_check_tables(ipa, sbi, 0x6ffa520a)) {
if (!bf_check_tables(ipa, sbi, 0x6ffa520a))
err++; err++;
}
bn = ARRAY_LENGTH(bf_test_data); int bn = ARRAY_LENGTH(bf_test_data);
int i;
for (i = 0; i < bn; i++) { for (i = 0; i < bn; i++) {
bf_key_init((char_u *)(bf_test_data[i].password), bf_key_init((char_u *)(bf_test_data[i].password), bf_test_data[i].salt,
bf_test_data[i].salt,
(int)STRLEN(bf_test_data[i].salt)); (int)STRLEN(bf_test_data[i].salt));
if (!bf_check_tables(pax, sbx, bf_test_data[i].keysum))
err++;
/* Don't modify bf_test_data[i].plaintxt, self test is idempotent. */ if (!bf_check_tables(pax, sbx, bf_test_data[i].keysum)) {
err++;
}
// Don't modify bf_test_data[i].plaintxt, self test is idempotent.
block8 bk;
memcpy(bk.uc, bf_test_data[i].plaintxt, 8); memcpy(bk.uc, bf_test_data[i].plaintxt, 8);
bf_e_cblock(bk.uc); bf_e_cblock(bk.uc);
if (memcmp(bk.uc, bf_test_data[i].cryptxt, 8) != 0) { if (memcmp(bk.uc, bf_test_data[i].cryptxt, 8) != 0) {
if (err == 0 && memcmp(bk.uc, bf_test_data[i].badcryptxt, 8) == 0) if ((err == 0) && (memcmp(bk.uc, bf_test_data[i].badcryptxt, 8) == 0)) {
EMSG(_("E817: Blowfish big/little endian use wrong")); EMSG(_("E817: Blowfish big/little endian use wrong"));
}
err++; err++;
} }
} }
@@ -486,52 +491,51 @@ static int bf_self_test(void) {
return err > 0 ? FAIL : OK; return err > 0 ? FAIL : OK;
} }
/* Output feedback mode. */ // Output feedback mode.
static int randbyte_offset = 0; static int randbyte_offset = 0;
static int update_offset = 0; static int update_offset = 0;
static char_u ofb_buffer[BF_OFB_LEN]; /* 64 bytes */ static char_u ofb_buffer[BF_OFB_LEN]; // 64 bytes
/* // Initialize with seed "iv[iv_len]".
* Initialize with seed "iv[iv_len]".
*/
void bf_ofb_init(char_u *iv, int iv_len) void bf_ofb_init(char_u *iv, int iv_len)
{ {
int i, mi;
randbyte_offset = update_offset = 0; randbyte_offset = update_offset = 0;
vim_memset(ofb_buffer, 0, BF_OFB_LEN); vim_memset(ofb_buffer, 0, BF_OFB_LEN);
if (iv_len > 0) { if (iv_len > 0) {
mi = iv_len > BF_OFB_LEN ? iv_len : BF_OFB_LEN; int mi = iv_len > BF_OFB_LEN ? iv_len : BF_OFB_LEN;
for (i = 0; i < mi; i++) int i;
for (i = 0; i < mi; i++) {
ofb_buffer[i % BF_OFB_LEN] ^= iv[i % iv_len]; ofb_buffer[i % BF_OFB_LEN] ^= iv[i % iv_len];
} }
}
} }
#define BF_OFB_UPDATE(c) { \ #define BF_OFB_UPDATE(c) { \
ofb_buffer[update_offset] ^= (char_u)c; \ ofb_buffer[update_offset] ^= (char_u)c; \
if (++update_offset == BF_OFB_LEN) \ if (++update_offset == BF_OFB_LEN) { \
update_offset = 0; \ update_offset = 0; \
} \
} }
#define BF_RANBYTE(t) { \ #define BF_RANBYTE(t) { \
if ((randbyte_offset & BF_BLOCK_MASK) == 0) \ if ((randbyte_offset & BF_BLOCK_MASK) == 0) { \
bf_e_cblock(&ofb_buffer[randbyte_offset]); \ bf_e_cblock(&ofb_buffer[randbyte_offset]); \
} \
t = ofb_buffer[randbyte_offset]; \ t = ofb_buffer[randbyte_offset]; \
if (++randbyte_offset == BF_OFB_LEN) \ if (++randbyte_offset == BF_OFB_LEN) { \
randbyte_offset = 0; \ randbyte_offset = 0; \
} \
} }
/* // Encrypt "from[len]" into "to[len]".
* Encrypt "from[len]" into "to[len]". // "from" and "to" can be equal to encrypt in place.
* "from" and "to" can be equal to encrypt in place.
*/
void bf_crypt_encode(char_u *from, size_t len, char_u *to) void bf_crypt_encode(char_u *from, size_t len, char_u *to)
{ {
size_t i; size_t i;
int ztemp, t; for (i = 0; i < len; i++) {
int ztemp = from[i];
for (i = 0; i < len; ++i) { int t;
ztemp = from[i];
BF_RANBYTE(t); BF_RANBYTE(t);
BF_OFB_UPDATE(ztemp); BF_OFB_UPDATE(ztemp);
to[i] = t ^ ztemp; to[i] = t ^ ztemp;
@@ -544,9 +548,8 @@ void bf_crypt_encode(char_u *from, size_t len, char_u *to)
void bf_crypt_decode(char_u *ptr, long len) void bf_crypt_decode(char_u *ptr, long len)
{ {
char_u *p; char_u *p;
for (p = ptr; p < ptr + len; p++) {
int t; int t;
for (p = ptr; p < ptr + len; ++p) {
BF_RANBYTE(t); BF_RANBYTE(t);
*p ^= t; *p ^= t;
BF_OFB_UPDATE(*p); BF_OFB_UPDATE(*p);
@@ -556,15 +559,12 @@ void bf_crypt_decode(char_u *ptr, long len)
/* /*
* Initialize the encryption keys and the random header according to * Initialize the encryption keys and the random header according to
* the given password. * the given password.
* in: "passwd" password string with which to modify keys
*/ */
void void bf_crypt_init_keys(char_u *passwd)
bf_crypt_init_keys (
char_u *passwd /* password string with which to modify keys */
)
{ {
char_u *p; char_u *p;
for (p = passwd; *p != NUL; p++) {
for (p = passwd; *p != NUL; ++p) {
BF_OFB_UPDATE(*p); BF_OFB_UPDATE(*p);
} }
} }
@@ -579,7 +579,8 @@ static uint32_t save_sbx[4][256];
* Save the current crypt state. Can only be used once before * Save the current crypt state. Can only be used once before
* bf_crypt_restore(). * bf_crypt_restore().
*/ */
void bf_crypt_save(void) { void bf_crypt_save(void)
{
save_randbyte_offset = randbyte_offset; save_randbyte_offset = randbyte_offset;
save_update_offset = update_offset; save_update_offset = update_offset;
mch_memmove(save_ofb_buffer, ofb_buffer, BF_OFB_LEN); mch_memmove(save_ofb_buffer, ofb_buffer, BF_OFB_LEN);
@@ -591,7 +592,8 @@ void bf_crypt_save(void) {
* Restore the current crypt state. Can only be used after * Restore the current crypt state. Can only be used after
* bf_crypt_save(). * bf_crypt_save().
*/ */
void bf_crypt_restore(void) { void bf_crypt_restore(void)
{
randbyte_offset = save_randbyte_offset; randbyte_offset = save_randbyte_offset;
update_offset = save_update_offset; update_offset = save_update_offset;
mch_memmove(ofb_buffer, save_ofb_buffer, BF_OFB_LEN); mch_memmove(ofb_buffer, save_ofb_buffer, BF_OFB_LEN);
@@ -603,7 +605,8 @@ void bf_crypt_restore(void) {
* Run a test to check if the encryption works as expected. * Run a test to check if the encryption works as expected.
* Give an error and return FAIL when not. * Give an error and return FAIL when not.
*/ */
int blowfish_self_test(void) { int blowfish_self_test(void)
{
if (sha256_self_test() == FAIL) { if (sha256_self_test() == FAIL) {
EMSG(_("E818: sha256 test failed")); EMSG(_("E818: sha256 test failed"));
return FAIL; return FAIL;
@@ -614,4 +617,3 @@ int blowfish_self_test(void) {
} }
return OK; return OK;
} }

View File

@@ -1,6 +1,6 @@
#ifndef NEOVIM_BLOWFISH_H #ifndef NEOVIM_BLOWFISH_H
#define NEOVIM_BLOWFISH_H #define NEOVIM_BLOWFISH_H
/* blowfish.c */
void bf_key_init(char_u *password, char_u *salt, int salt_len); void bf_key_init(char_u *password, char_u *salt, int salt_len);
void bf_ofb_init(char_u *iv, int iv_len); void bf_ofb_init(char_u *iv, int iv_len);
void bf_crypt_encode(char_u *from, size_t len, char_u *to); void bf_crypt_encode(char_u *from, size_t len, char_u *to);
@@ -9,5 +9,6 @@ void bf_crypt_init_keys(char_u *passwd);
void bf_crypt_save(void); void bf_crypt_save(void);
void bf_crypt_restore(void); void bf_crypt_restore(void);
int blowfish_self_test(void); int blowfish_self_test(void);
/* vim: set ft=c : */
#endif /* NEOVIM_BLOWFISH_H */ // vim: set ft=c:
#endif // NEOVIM_BLOWFISH_H

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
#ifndef NEOVIM_CHARSET_H #ifndef NEOVIM_CHARSET_H
#define NEOVIM_CHARSET_H #define NEOVIM_CHARSET_H
/* charset.c */
int init_chartab(void); int init_chartab(void);
int buf_init_chartab(buf_T *buf, int global); int buf_init_chartab(buf_T *buf, int global);
void trans_characters(char_u *buf, int bufsize); void trans_characters(char_u *buf, int bufsize);
@@ -63,5 +63,6 @@ int rem_backslash(char_u *str);
void backslash_halve(char_u *p); void backslash_halve(char_u *p);
char_u *backslash_halve_save(char_u *p); char_u *backslash_halve_save(char_u *p);
void ebcdic2ascii(char_u *buffer, int len); void ebcdic2ascii(char_u *buffer, int len);
/* vim: set ft=c : */
#endif /* NEOVIM_CHARSET_H */ // vim: set ft=c:
#endif // NEOVIM_CHARSET_H

View File

@@ -31,16 +31,21 @@ static ulg crc_32_tab[256];
/* /*
* Fill the CRC table. * Fill the CRC table.
*/ */
static void make_crc_tab(void) { static void make_crc_tab(void)
ulg s,t,v; {
ulg s, t, v;
static int done = FALSE; static int done = FALSE;
if (done) if (done) {
return; return;
}
for (t = 0; t < 256; t++) { for (t = 0; t < 256; t++) {
v = t; v = t;
for (s = 0; s < 8; s++)
for (s = 0; s < 8; s++) {
v = (v >> 1) ^ ((v & 1) * (ulg)0xedb88320L); v = (v >> 1) ^ ((v & 1) * (ulg)0xedb88320L);
}
crc_32_tab[t] = v; crc_32_tab[t] = v;
} }
done = TRUE; done = TRUE;
@@ -55,7 +60,6 @@ static ulg keys[3]; /* keys defining the pseudo-random sequence */
*/ */
#define DECRYPT_BYTE_ZIP(t) { \ #define DECRYPT_BYTE_ZIP(t) { \
ush temp; \ ush temp; \
\
temp = (ush)keys[2] | 2; \ temp = (ush)keys[2] | 2; \
t = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); \ t = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); \
} }
@@ -107,18 +111,21 @@ void set_crypt_method(buf_T *buf, int method)
* the state. * the state.
* Must always be called symmetrically with crypt_pop_state(). * Must always be called symmetrically with crypt_pop_state().
*/ */
void crypt_push_state(void) { void crypt_push_state(void)
{
if (crypt_busy == 1) { if (crypt_busy == 1) {
/* save the state */ /* save the state */
if (use_crypt_method == 0) { if (use_crypt_method == 0) {
saved_keys[0] = keys[0]; saved_keys[0] = keys[0];
saved_keys[1] = keys[1]; saved_keys[1] = keys[1];
saved_keys[2] = keys[2]; saved_keys[2] = keys[2];
} else } else {
bf_crypt_save(); bf_crypt_save();
}
saved_crypt_method = use_crypt_method; saved_crypt_method = use_crypt_method;
} else if (crypt_busy > 1) } else if (crypt_busy > 1) {
EMSG2(_(e_intern2), "crypt_push_state()"); EMSG2(_(e_intern2), "crypt_push_state()");
}
++crypt_busy; ++crypt_busy;
} }
@@ -127,17 +134,21 @@ void crypt_push_state(void) {
* the saved state. * the saved state.
* Must always be called symmetrically with crypt_push_state(). * Must always be called symmetrically with crypt_push_state().
*/ */
void crypt_pop_state(void) { void crypt_pop_state(void)
{
--crypt_busy; --crypt_busy;
if (crypt_busy == 1) { if (crypt_busy == 1) {
use_crypt_method = saved_crypt_method; use_crypt_method = saved_crypt_method;
if (use_crypt_method == 0) { if (use_crypt_method == 0) {
keys[0] = saved_keys[0]; keys[0] = saved_keys[0];
keys[1] = saved_keys[1]; keys[1] = saved_keys[1];
keys[2] = saved_keys[2]; keys[2] = saved_keys[2];
} else } else {
bf_crypt_restore(); bf_crypt_restore();
} }
}
} }
/* /*
@@ -149,15 +160,16 @@ void crypt_encode(char_u *from, size_t len, char_u *to)
size_t i; size_t i;
int ztemp, t; int ztemp, t;
if (use_crypt_method == 0) if (use_crypt_method == 0) {
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
ztemp = from[i]; ztemp = from[i];
DECRYPT_BYTE_ZIP(t); DECRYPT_BYTE_ZIP(t);
UPDATE_KEYS_ZIP(ztemp); UPDATE_KEYS_ZIP(ztemp);
to[i] = t ^ ztemp; to[i] = t ^ ztemp;
} }
else } else {
bf_crypt_encode(from, len, to); bf_crypt_encode(from, len, to);
}
} }
/* /*
@@ -167,7 +179,7 @@ void crypt_decode(char_u *ptr, long len)
{ {
char_u *p; char_u *p;
if (use_crypt_method == 0) if (use_crypt_method == 0) {
for (p = ptr; p < ptr + len; ++p) { for (p = ptr; p < ptr + len; ++p) {
ush temp; ush temp;
@@ -175,21 +187,20 @@ void crypt_decode(char_u *ptr, long len)
temp = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); temp = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff);
UPDATE_KEYS_ZIP(*p ^= temp); UPDATE_KEYS_ZIP(*p ^= temp);
} }
else } else {
bf_crypt_decode(ptr, len); bf_crypt_decode(ptr, len);
}
} }
/* /*
* Initialize the encryption keys and the random header according to * Initialize the encryption keys and the random header according to
* the given password. * the given password.
* If "passwd" is NULL or empty, don't do anything. * If "passwd" is NULL or empty, don't do anything.
* in: "passwd" password string with which to modify keys
*/ */
void void crypt_init_keys(char_u *passwd)
crypt_init_keys (
char_u *passwd /* password string with which to modify keys */
)
{ {
if (passwd != NULL && *passwd != NUL) { if ((passwd != NULL) && (*passwd != NUL)) {
if (use_crypt_method == 0) { if (use_crypt_method == 0) {
char_u *p; char_u *p;
@@ -197,12 +208,14 @@ crypt_init_keys (
keys[0] = 305419896L; keys[0] = 305419896L;
keys[1] = 591751049L; keys[1] = 591751049L;
keys[2] = 878082192L; keys[2] = 878082192L;
for (p = passwd; *p!= NUL; ++p) {
for (p = passwd; *p != NUL; ++p) {
UPDATE_KEYS_ZIP((int)*p); UPDATE_KEYS_ZIP((int)*p);
} }
} else } else {
bf_crypt_init_keys(passwd); bf_crypt_init_keys(passwd);
} }
}
} }
/* /*
@@ -214,8 +227,9 @@ void free_crypt_key(char_u *key)
char_u *p; char_u *p;
if (key != NULL) { if (key != NULL) {
for (p = key; *p != NUL; ++p) for (p = key; *p != NUL; ++p) {
*p = 0; *p = 0;
}
vim_free(key); vim_free(key);
} }
} }
@@ -225,13 +239,10 @@ void free_crypt_key(char_u *key)
* When "store" is TRUE, the new key is stored in the 'key' option, and the * When "store" is TRUE, the new key is stored in the 'key' option, and the
* 'key' option value is returned: Don't free it. * 'key' option value is returned: Don't free it.
* When "store" is FALSE, the typed key is returned in allocated memory. * When "store" is FALSE, the typed key is returned in allocated memory.
* in: "twice" Ask for the key twice.
* Returns NULL on failure. * Returns NULL on failure.
*/ */
char_u * char_u *get_crypt_key(int store, int twice)
get_crypt_key (
int store,
int twice /* Ask for the key twice. */
)
{ {
char_u *p1, *p2 = NULL; char_u *p1, *p2 = NULL;
int round; int round;
@@ -239,17 +250,17 @@ get_crypt_key (
for (round = 0;; ++round) { for (round = 0;; ++round) {
cmdline_star = TRUE; cmdline_star = TRUE;
cmdline_row = msg_row; cmdline_row = msg_row;
p1 = getcmdline_prompt(NUL, round == 0 char_u *prompt = (round == 0)
? (char_u *)_("Enter encryption key: ") ? (char_u *) _("Enter encryption key: ")
: (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING, : (char_u *) _("Enter same key again: ");
NULL); p1 = getcmdline_prompt(NUL, prompt, 0, EXPAND_NOTHING, NULL);
cmdline_star = FALSE; cmdline_star = FALSE;
if (p1 == NULL) {
if (p1 == NULL)
break; break;
}
if (round == twice) { if (round == twice) {
if (p2 != NULL && STRCMP(p1, p2) != 0) { if ((p2 != NULL) && (STRCMP(p1, p2) != 0)) {
MSG(_("Keys don't match!")); MSG(_("Keys don't match!"));
free_crypt_key(p1); free_crypt_key(p1);
free_crypt_key(p2); free_crypt_key(p2);
@@ -259,7 +270,7 @@ get_crypt_key (
} }
if (store) { if (store) {
set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL); set_option_value((char_u *) "key", 0L, p1, OPT_LOCAL);
free_crypt_key(p1); free_crypt_key(p1);
p1 = curbuf->b_p_key; p1 = curbuf->b_p_key;
} }
@@ -269,12 +280,12 @@ get_crypt_key (
} }
/* since the user typed this, no need to wait for return */ /* since the user typed this, no need to wait for return */
if (msg_didout) if (msg_didout) {
msg_putchar('\n'); msg_putchar('\n');
}
need_wait_return = FALSE; need_wait_return = FALSE;
msg_didout = FALSE; msg_didout = FALSE;
free_crypt_key(p2); free_crypt_key(p2);
return p1; return p1;
} }

2379
src/diff.c

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
#ifndef NEOVIM_DIFF_H #ifndef NEOVIM_DIFF_H
#define NEOVIM_DIFF_H #define NEOVIM_DIFF_H
/* diff.c */
void diff_buf_delete(buf_T *buf); void diff_buf_delete(buf_T *buf);
void diff_buf_adjust(win_T *win); void diff_buf_adjust(win_T *win);
void diff_buf_add(buf_T *buf); void diff_buf_add(buf_T *buf);
@@ -29,5 +29,6 @@ linenr_T diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1,
buf_T *buf2, buf_T *buf2,
linenr_T lnum3); linenr_T lnum3);
linenr_T diff_lnum_win(linenr_T lnum, win_T *wp); linenr_T diff_lnum_win(linenr_T lnum, win_T *wp);
/* vim: set ft=c : */
#endif /* NEOVIM_DIFF_H */ // vim: set ft=c:
#endif // NEOVIM_DIFF_H

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
#ifndef NEOVIM_DIGRAPH_H #ifndef NEOVIM_DIGRAPH_H
#define NEOVIM_DIGRAPH_H #define NEOVIM_DIGRAPH_H
/* digraph.c */
int do_digraph(int c); int do_digraph(int c);
int get_digraph(int cmdline); int get_digraph(int cmdline);
int getdigraph(int char1, int char2, int meta_char); int getdigraph(int char1, int char2, int meta_char);
@@ -8,5 +8,6 @@ void putdigraph(char_u *str);
void listdigraphs(void); void listdigraphs(void);
char_u *keymap_init(void); char_u *keymap_init(void);
void ex_loadkeymap(exarg_T *eap); void ex_loadkeymap(exarg_T *eap);
/* vim: set ft=c : */
#endif /* NEOVIM_DIGRAPH_H */ // vim: set ft=c:
#endif // NEOVIM_DIGRAPH_H

View File

@@ -1,21 +1,13 @@
/* vi:set ts=8 sts=4 sw=4: /// @file farsi.c
* ///
* VIM - Vi IMproved by Bram Moolenaar /// Functions for Farsi language
* ///
* Do ":help uganda" in Vim to read copying and usage conditions. /// Included by main.c, when FEAT_FKMAP is defined.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
#include "farsi.h" #include "farsi.h"
#include "edit.h" #include "edit.h"
#include "ex_getln.h" #include "ex_getln.h"
/*
* farsi.c: functions for Farsi language
*
* Included by main.c, when FEAT_FKMAP is defined.
*/
static int toF_Xor_X_(int c); static int toF_Xor_X_(int c);
static int F_is_TyE(int c); static int F_is_TyE(int c);
@@ -38,9 +30,11 @@ static int F_isterm(int c);
static int toF_ending(int c); static int toF_ending(int c);
static void lrswapbuf(char_u *buf, int len); static void lrswapbuf(char_u *buf, int len);
/* /// Convert the given Farsi character into a _X or _X_ type
** Convert the given Farsi character into a _X or _X_ type ///
*/ /// @param c The character to convert.
///
/// @return Farsi character converted to a _X or _X_ type.
static int toF_Xor_X_(int c) static int toF_Xor_X_(int c)
{ {
int tempc; int tempc;
@@ -152,9 +146,11 @@ static int toF_Xor_X_(int c)
return 0; return 0;
} }
/* /// Convert the given Farsi character into Farsi capital character.
** Convert the given Farsi character into Farsi capital character . ///
*/ /// @param c The character to convert.
///
/// @return Character converted to the Farsi capital leter.
int toF_TyA(int c) int toF_TyA(int c)
{ {
switch (c) { switch (c) {
@@ -216,7 +212,8 @@ int toF_TyA(int c)
case _GHAF: case _GHAF:
return GHAF; return GHAF;
/* I am not sure what it is !!! case _KAF_H: */ // I am not sure what it is !!!
// case _KAF_H:
case _KAF: case _KAF:
return KAF; return KAF;
@@ -254,11 +251,14 @@ int toF_TyA(int c)
return c; return c;
} }
/* /// Is the character under the cursor+offset in the given buffer a join type.
** Is the character under the cursor+offset in the given buffer a join type. /// That is a character that is combined with the others.
** That is a character that is combined with the others. /// Note: the offset is used only for command line buffer.
** Note: the offset is used only for command line buffer. ///
*/ /// @param src
/// @param offset
///
/// @return TRUE if the character under the cursor+offset is a join type.
static int F_is_TyB_TyC_TyD(int src, int offset) static int F_is_TyB_TyC_TyD(int src, int offset)
{ {
int c; int c;
@@ -306,9 +306,11 @@ static int F_is_TyB_TyC_TyD(int src, int offset)
return FALSE; return FALSE;
} }
/* /// Is the Farsi character one of the terminating only type.
** Is the Farsi character one of the terminating only type. ///
*/ /// @param c The character to check.
///
/// @return TRUE if the Farsi character is one of the terminating only types.
static int F_is_TyE(int c) static int F_is_TyE(int c)
{ {
switch (c) { switch (c) {
@@ -327,9 +329,11 @@ static int F_is_TyE(int c)
return FALSE; return FALSE;
} }
/* /// Is the Farsi character one of the none leading type.
** Is the Farsi character one of the none leading type. ///
*/ /// @param c The character to check.
///
/// @return TRUE if the Farsi character is one of the none-leading types.
static int F_is_TyC_TyD(int c) static int F_is_TyC_TyD(int c)
{ {
switch (c) { switch (c) {
@@ -349,9 +353,11 @@ static int F_is_TyC_TyD(int c)
return FALSE; return FALSE;
} }
/* /// Convert a none leading Farsi char into a leading type.
** Convert a none leading Farsi char into a leading type. ///
*/ /// @param c The character to convert.
///
/// @return The character converted into a leading type.
static int toF_TyB(int c) static int toF_TyB(int c)
{ {
switch (c) { switch (c) {
@@ -365,13 +371,15 @@ static int toF_TyB(int c)
return _AYN; return _AYN;
case AYN_: case AYN_:
return AYN; /* exception - there are many of them */ // exception - there are many of them
return AYN;
case _GHAYN_: case _GHAYN_:
return _GHAYN; return _GHAYN;
case GHAYN_: case GHAYN_:
return GHAYN; /* exception - there are many of them */ // exception - there are many of them
return GHAYN;
case _HE_: case _HE_:
return _HE; return _HE;
@@ -391,9 +399,9 @@ static int toF_TyB(int c)
return c; return c;
} }
/* /// Overwrite the current redo and cursor characters + left adjust
** Overwrite the current redo and cursor characters + left adjust ///
*/ /// @param c
static void put_curr_and_l_to_X(int c) static void put_curr_and_l_to_X(int c)
{ {
int tempc; int tempc;
@@ -434,9 +442,7 @@ static void put_and_redo(int c)
AppendCharToRedobuff(c); AppendCharToRedobuff(c);
} }
/* /// Change the char. under the cursor to a X_ or X type
** Change the char. under the cursor to a X_ or X type
*/
static void chg_c_toX_orX(void) static void chg_c_toX_orX(void)
{ {
int tempc, curc; int tempc, curc;
@@ -582,9 +588,7 @@ static void chg_c_toX_orX(void)
} }
} }
/* /// Change the char. under the cursor to a _X_ or X_ type
** Change the char. under the cursor to a _X_ or X_ type
*/
static void chg_c_to_X_orX_(void) static void chg_c_to_X_orX_(void)
{ {
int tempc; int tempc;
@@ -643,9 +647,7 @@ static void chg_c_to_X_orX_(void)
} }
} }
/* /// Change the char. under the cursor to a _X_ or _X type
** Change the char. under the cursor to a _X_ or _X type
*/
static void chg_c_to_X_or_X(void) static void chg_c_to_X_or_X(void)
{ {
int tempc; int tempc;
@@ -669,15 +671,13 @@ static void chg_c_to_X_or_X(void)
} }
} }
/* /// Change the character left to the cursor to a _X_ or X_ type
** Change the character left to the cursor to a _X_ or X_ type
*/
static void chg_l_to_X_orX_(void) static void chg_l_to_X_orX_(void)
{ {
int tempc; int tempc;
if ((curwin->w_cursor.col != 0) && if ((curwin->w_cursor.col != 0)
(curwin->w_cursor.col + 1 == (colnr_T)STRLEN(ml_get_curline()))) { && (curwin->w_cursor.col + 1 == (colnr_T)STRLEN(ml_get_curline()))) {
return; return;
} }
@@ -751,9 +751,7 @@ static void chg_l_to_X_orX_(void)
} }
} }
/* /// Change the character left to the cursor to a X or _X type
** Change the character left to the cursor to a X or _X type
*/
static void chg_l_toXor_X(void) static void chg_l_toXor_X(void)
{ {
int tempc; int tempc;
@@ -833,9 +831,7 @@ static void chg_l_toXor_X(void)
} }
} }
/* /// Change the character right to the cursor to a _X or _X_ type
** Change the character right to the cursor to a _X or _X_ type
*/
static void chg_r_to_Xor_X_(void) static void chg_r_to_Xor_X_(void)
{ {
int tempc, c; int tempc, c;
@@ -856,9 +852,7 @@ static void chg_r_to_Xor_X_(void)
} }
} }
/* /// Map Farsi keyboard when in fkmap mode.
** Map Farsi keyboard when in fkmap mode.
*/
int fkmap(int c) int fkmap(int c)
{ {
int tempc; int tempc;
@@ -868,14 +862,15 @@ int fkmap(int c)
return c; return c;
} }
if (VIM_ISDIGIT(c) || if (VIM_ISDIGIT(c)
(((c == '.') || || (((c == '.')
(c == '+') || || (c == '+')
(c == '-') || || (c == '-')
(c == '^') || || (c == '^')
(c == '%') || || (c == '%')
(c == '#') || || (c == '#')
(c == '=')) && revins)) { || (c == '='))
&& revins)) {
if (!revins) { if (!revins) {
if (curwin->w_cursor.col) { if (curwin->w_cursor.col) {
if (!p_ri) { if (!p_ri) {
@@ -897,7 +892,7 @@ int fkmap(int c)
inc_cursor(); inc_cursor();
} }
++revins; revins++;
p_ri = 1; p_ri = 1;
} else { } else {
if (revins) { if (revins) {
@@ -906,28 +901,28 @@ int fkmap(int c)
revins = 0; revins = 0;
if (curwin->w_p_rl) { if (curwin->w_p_rl) {
while ((F_isdigit(gchar_cursor()) || while ((F_isdigit(gchar_cursor())
(gchar_cursor() == F_PERIOD || || (gchar_cursor() == F_PERIOD
gchar_cursor() == F_PLUS || || gchar_cursor() == F_PLUS
gchar_cursor() == F_MINUS || || gchar_cursor() == F_MINUS
gchar_cursor() == F_MUL || || gchar_cursor() == F_MUL
gchar_cursor() == F_DIVIDE || || gchar_cursor() == F_DIVIDE
gchar_cursor() == F_PERCENT || || gchar_cursor() == F_PERCENT
gchar_cursor() == F_EQUALS)) && || gchar_cursor() == F_EQUALS))
gchar_cursor() != NUL) { && gchar_cursor() != NUL) {
++curwin->w_cursor.col; curwin->w_cursor.col++;
} }
} else { } else {
if (curwin->w_cursor.col) { if (curwin->w_cursor.col) {
while ((F_isdigit(gchar_cursor()) || while ((F_isdigit(gchar_cursor())
(gchar_cursor() == F_PERIOD || || (gchar_cursor() == F_PERIOD
gchar_cursor() == F_PLUS || || gchar_cursor() == F_PLUS
gchar_cursor() == F_MINUS || || gchar_cursor() == F_MINUS
gchar_cursor() == F_MUL || || gchar_cursor() == F_MUL
gchar_cursor() == F_DIVIDE || || gchar_cursor() == F_DIVIDE
gchar_cursor() == F_PERCENT || || gchar_cursor() == F_PERCENT
gchar_cursor() == F_EQUALS)) && || gchar_cursor() == F_EQUALS))
--curwin->w_cursor.col) { && --curwin->w_cursor.col) {
} }
} }
@@ -963,7 +958,6 @@ int fkmap(int c)
} }
tempc = 0; tempc = 0;
switch (c) { switch (c) {
case '`': case '`':
case ' ': case ' ':
@@ -1022,10 +1016,8 @@ int fkmap(int c)
case NL: case NL:
case TAB: case TAB:
if (p_ri && (c == NL) && curwin->w_cursor.col) { if (p_ri && (c == NL) && curwin->w_cursor.col) {
/* // If the char before the cursor is _X_ or X_ do not change
** If the char before the cursor is _X_ or X_ do not change // the one under the cursor with X type.
** the one under the cursor with X type.
*/
dec_cursor(); dec_cursor();
if (F_isalpha(gchar_cursor())) { if (F_isalpha(gchar_cursor())) {
@@ -1853,9 +1845,11 @@ int fkmap(int c)
return c; return c;
} }
/* /// Convert a none leading Farsi char into a leading type.
** Convert a none leading Farsi char into a leading type. ///
*/ /// @param c The character to convert.
///
/// @return The non-leading Farsi character converted to a leading type.
static int toF_leading(int c) static int toF_leading(int c)
{ {
switch (c) { switch (c) {
@@ -1951,9 +1945,11 @@ static int toF_leading(int c)
return c; return c;
} }
/* /// Convert a given Farsi char into right joining type.
** Convert a given Farsi char into right joining type. ///
*/ /// @param c The character to convert.
///
/// @return The Farsi character converted into a right joining type
static int toF_Rjoin(int c) static int toF_Rjoin(int c)
{ {
switch (c) { switch (c) {
@@ -2052,9 +2048,11 @@ static int toF_Rjoin(int c)
return c; return c;
} }
/* /// Can a given Farsi character join via its left edj.
** Can a given Farsi character join via its left edj. ///
*/ /// @param c The character to check.
///
/// @return TRUE if the character can join via its left edj.
static int canF_Ljoin(int c) static int canF_Ljoin(int c)
{ {
switch (c) { switch (c) {
@@ -2124,9 +2122,11 @@ static int canF_Ljoin(int c)
return FALSE; return FALSE;
} }
/* /// Can a given Farsi character join via its right edj.
** Can a given Farsi character join via its right edj. ///
*/ /// @param c
///
/// @return TRUE if the character can join via its right edj.
static int canF_Rjoin(int c) static int canF_Rjoin(int c)
{ {
switch (c) { switch (c) {
@@ -2149,9 +2149,11 @@ static int canF_Rjoin(int c)
return canF_Ljoin(c); return canF_Ljoin(c);
} }
/* /// Is a given Farsi character a terminating type.
** is a given Farsi character a terminating type. ///
*/ /// @param c
///
/// @return TRUE if the character is a terminating type.
static int F_isterm(int c) static int F_isterm(int c)
{ {
switch (c) { switch (c) {
@@ -2174,9 +2176,11 @@ static int F_isterm(int c)
return FALSE; return FALSE;
} }
/* /// Convert the given Farsi character into a ending type.
** Convert the given Farsi character into a ending type . ///
*/ /// @param c The character to convert.
///
/// @return The character converted into an ending type.
static int toF_ending(int c) static int toF_ending(int c)
{ {
switch (c) { switch (c) {
@@ -2278,9 +2282,7 @@ static int toF_ending(int c)
return c; return c;
} }
/* /// Convert the Farsi 3342 standard into Farsi VIM.
** Convert the Farsi 3342 standard into Farsi VIM.
*/
void conv_to_pvim(void) void conv_to_pvim(void)
{ {
char_u *ptr; char_u *ptr;
@@ -2292,14 +2294,14 @@ void conv_to_pvim(void)
for (i = 0; i < llen - 1; i++) { for (i = 0; i < llen - 1; i++) {
if (canF_Ljoin(ptr[i]) && canF_Rjoin(ptr[i + 1])) { if (canF_Ljoin(ptr[i]) && canF_Rjoin(ptr[i + 1])) {
ptr[i] = toF_leading(ptr[i]); ptr[i] = toF_leading(ptr[i]);
++i; i++;
while (canF_Rjoin(ptr[i]) && i < llen) { while (canF_Rjoin(ptr[i]) && i < llen) {
ptr[i] = toF_Rjoin(ptr[i]); ptr[i] = toF_Rjoin(ptr[i]);
if (F_isterm(ptr[i]) || !F_isalpha(ptr[i])) { if (F_isterm(ptr[i]) || !F_isalpha(ptr[i])) {
break; break;
} }
++i; i++;
} }
if (!F_isalpha(ptr[i]) || !canF_Rjoin(ptr[i])) { if (!F_isalpha(ptr[i]) || !canF_Rjoin(ptr[i])) {
@@ -2311,30 +2313,22 @@ void conv_to_pvim(void)
} }
} }
/* // Following lines contains Farsi encoded character.
* Following lines contains Farsi encoded character.
*/
do_cmdline_cmd((char_u *)"%s/\202\231/\232/g"); do_cmdline_cmd((char_u *)"%s/\202\231/\232/g");
do_cmdline_cmd((char_u *)"%s/\201\231/\370\334/g"); do_cmdline_cmd((char_u *)"%s/\201\231/\370\334/g");
/* Assume the screen has been messed up: clear it and redraw. */ // Assume the screen has been messed up: clear it and redraw.
redraw_later(CLEAR); redraw_later(CLEAR);
MSG_ATTR(farsi_text_1, hl_attr(HLF_S)); MSG_ATTR(farsi_text_1, hl_attr(HLF_S));
} }
/* /// Convert the Farsi VIM into Farsi 3342 standard.
* Convert the Farsi VIM into Farsi 3342 standard.
*/
void conv_to_pstd(void) void conv_to_pstd(void)
{ {
char_u *ptr; char_u *ptr;
int lnum, llen, i; int lnum, llen, i;
/* // Following line contains Farsi encoded character.
* Following line contains Farsi encoded character.
*/
do_cmdline_cmd((char_u *)"%s/\232/\202\231/g"); do_cmdline_cmd((char_u *)"%s/\232/\202\231/g");
for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) { for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) {
ptr = ml_get((linenr_T)lnum); ptr = ml_get((linenr_T)lnum);
@@ -2344,14 +2338,15 @@ void conv_to_pstd(void)
} }
} }
/* Assume the screen has been messed up: clear it and redraw. */ // Assume the screen has been messed up: clear it and redraw.
redraw_later(CLEAR); redraw_later(CLEAR);
MSG_ATTR(farsi_text_2, hl_attr(HLF_S)); MSG_ATTR(farsi_text_2, hl_attr(HLF_S));
} }
/* /// left-right swap the characters in buf[len].
* left-right swap the characters in buf[len]. ///
*/ /// @param buf
/// @param len
static void lrswapbuf(char_u *buf, int len) static void lrswapbuf(char_u *buf, int len)
{ {
char_u *s, *e; char_u *s, *e;
@@ -2368,9 +2363,11 @@ static void lrswapbuf(char_u *buf, int len)
} }
} }
/* /// swap all the characters in reverse direction
* swap all the characters in reverse direction ///
*/ /// @param ibuf
///
/// @return The buffer with the characters swapped.
char_u* lrswap(char_u *ibuf) char_u* lrswap(char_u *ibuf)
{ {
if ((ibuf != NULL) && (*ibuf != NUL)) { if ((ibuf != NULL) && (*ibuf != NUL)) {
@@ -2379,9 +2376,12 @@ char_u* lrswap(char_u *ibuf)
return ibuf; return ibuf;
} }
/* /// swap all the Farsi characters in reverse direction
* swap all the Farsi characters in reverse direction ///
*/ /// @param cmdbuf
/// @param .
///
/// @return The buffer with all Farsi characters swapped.
char_u* lrFswap(char_u *cmdbuf, int len) char_u* lrFswap(char_u *cmdbuf, int len)
{ {
int i, cnt; int i, cnt;
@@ -2394,10 +2394,10 @@ char_u* lrFswap(char_u *cmdbuf, int len)
} }
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
for (cnt = 0; i + cnt < len && for (cnt = 0; i + cnt < len
(F_isalpha(cmdbuf[i + cnt]) || && (F_isalpha(cmdbuf[i + cnt])
F_isdigit(cmdbuf[i + cnt]) || || F_isdigit(cmdbuf[i + cnt])
cmdbuf[i + cnt] == ' '); ++cnt) { || cmdbuf[i + cnt] == ' '); ++cnt) {
} }
lrswapbuf(cmdbuf + i, cnt); lrswapbuf(cmdbuf + i, cnt);
@@ -2406,11 +2406,14 @@ char_u* lrFswap(char_u *cmdbuf, int len)
return cmdbuf; return cmdbuf;
} }
/* /// Reverse the characters in the search path and substitute section
* Reverse the characters in the search path and substitute section /// accordingly.
* accordingly. /// TODO: handle different separator characters. Use skip_regexp().
* TODO: handle different separator characters. Use skip_regexp(). ///
*/ /// @param ibuf
///
/// @return The buffer with the characters in the search path and substitute
/// section reversed.
char_u* lrF_sub(char_u *ibuf) char_u* lrF_sub(char_u *ibuf)
{ {
char_u *p, *ep; char_u *p, *ep;
@@ -2418,7 +2421,7 @@ char_u* lrF_sub(char_u *ibuf)
p = ibuf; p = ibuf;
/* Find the boundary of the search path */ // Find the boundary of the search path
while (((p = vim_strchr(p + 1, '/')) != NULL) && p[-1] == '\\') { while (((p = vim_strchr(p + 1, '/')) != NULL) && p[-1] == '\\') {
} }
@@ -2426,17 +2429,17 @@ char_u* lrF_sub(char_u *ibuf)
return ibuf; return ibuf;
} }
/* Reverse the Farsi characters in the search path. */ // Reverse the Farsi characters in the search path.
lrFswap(ibuf, (int)(p - ibuf)); lrFswap(ibuf, (int)(p - ibuf));
/* Now find the boundary of the substitute section */ // Now find the boundary of the substitute section
if ((ep = (char_u *)strrchr((char *)++p, '/')) != NULL) { if ((ep = (char_u *)strrchr((char *)++p, '/')) != NULL) {
cnt = (int)(ep - p); cnt = (int)(ep - p);
} else { } else {
cnt = (int)STRLEN(p); cnt = (int)STRLEN(p);
} }
/* Reverse the characters in the substitute section and take care of '\' */ // Reverse the characters in the substitute section and take care of '\'
for (i = 0; i < cnt - 1; i++) { for (i = 0; i < cnt - 1; i++) {
if (p[i] == '\\') { if (p[i] == '\\') {
p[i] = p[i + 1]; p[i] = p[i + 1];
@@ -2448,9 +2451,11 @@ char_u* lrF_sub(char_u *ibuf)
return ibuf; return ibuf;
} }
/* /// Map Farsi keyboard when in cmd_fkmap mode.
* Map Farsi keyboard when in cmd_fkmap mode. ///
*/ /// @param c
///
/// @return The mapped character.
int cmdl_fkmap(int c) int cmdl_fkmap(int c)
{ {
int tempc; int tempc;
@@ -2881,27 +2886,33 @@ int cmdl_fkmap(int c)
return c; return c;
} }
/* /// F_isalpha returns TRUE if 'c' is a Farsi alphabet
* F_isalpha returns TRUE if 'c' is a Farsi alphabet ///
*/ /// @param c The character to check.
///
/// @return TRUE if 'c' is a Farsi alphabet character.
int F_isalpha(int c) int F_isalpha(int c)
{ {
return (c >= TEE_ && c <= _YE) || return (c >= TEE_ && c <= _YE)
(c >= ALEF_A && c <= YE) || || (c >= ALEF_A && c <= YE)
(c >= _IE && c <= YE_); || (c >= _IE && c <= YE_);
} }
/* /// F_isdigit returns TRUE if 'c' is a Farsi digit
* F_isdigit returns TRUE if 'c' is a Farsi digit ///
*/ /// @param c The character to check.
///
/// @return TRUE if 'c' is a Farsi digit.
int F_isdigit(int c) int F_isdigit(int c)
{ {
return c >= FARSI_0 && c <= FARSI_9; return c >= FARSI_0 && c <= FARSI_9;
} }
/* /// F_ischar returns TRUE if 'c' is a Farsi character.
* F_ischar returns TRUE if 'c' is a Farsi character. ///
*/ /// @param c The character to check.
///
/// @return TRUE if 'c' is a Farsi character.
int F_ischar(int c) int F_ischar(int c)
{ {
return c >= TEE_ && c <= YE_; return c >= TEE_ && c <= YE_;

View File

@@ -1,31 +1,19 @@
/* vi:set ts=8 sts=4 sw=4: /// @file farsi.h
* ///
* VIM - Vi IMproved by Bram Moolenaar /// Farsi characters are categorized into following types:
* ///
* Do ":help uganda" in Vim to read copying and usage conditions. /// TyA (for capital letter representation)
* Do ":help credits" in Vim to see a list of people who contributed. /// TyB (for types that look like _X e.g. AYN)
*/ /// TyC (for types that look like X_ e.g. YE_)
/// TyD (for types that look like _X_ e.g. _AYN_)
/// TyE (for types that look like X e.g. RE)
#ifndef SRC_FARSI_H_ #ifndef NEOVIM_FARSI_H
#define SRC_FARSI_H_ #define NEOVIM_FARSI_H
/* // Farsi character set definition
* Farsi characters are categorized into following types:
*
* TyA (for capital letter representation)
* TyB (for types that look like _X e.g. AYN)
* TyC (for types that look like X_ e.g. YE_)
* TyD (for types that look like _X_ e.g. _AYN_)
* TyE (for types that look like X e.g. RE)
*/
/* // Begin of the non-standard part
* Farsi character set definition
*/
/*
* Begin of the non-standard part
*/
#define TEE_ 0x80 #define TEE_ 0x80
#define ALEF_U_H_ 0x81 #define ALEF_U_H_ 0x81
@@ -66,30 +54,26 @@
#define YEE_ 0xef #define YEE_ 0xef
#define YE_ 0xff #define YE_ 0xff
/* // End of the non-standard part
* End of the non-standard part
*/
/* // Standard part
* Standard part
*/
#define F_BLANK 0xa0 /* Farsi ' ' (SP) character */ #define F_BLANK 0xa0 // Farsi ' ' (SP) character
#define F_PSP 0xa1 /* PSP for capitalizing of a character */ #define F_PSP 0xa1 // PSP for capitalizing of a character
#define F_PCN 0xa2 /* PCN for redefining of the hamye meaning */ #define F_PCN 0xa2 // PCN for redefining of the hamye meaning
#define F_EXCL 0xa3 /* Farsi ! character */ #define F_EXCL 0xa3 // Farsi ! character
#define F_CURRENCY 0xa4 /* Farsi Rial character */ #define F_CURRENCY 0xa4 // Farsi Rial character
#define F_PERCENT 0xa5 /* Farsi % character */ #define F_PERCENT 0xa5 // Farsi % character
#define F_PERIOD 0xa6 /* Farsi '.' character */ #define F_PERIOD 0xa6 // Farsi '.' character
#define F_COMMA 0xa7 /* Farsi ',' character */ #define F_COMMA 0xa7 // Farsi ',' character
#define F_LPARENT 0xa8 /* Farsi '(' character */ #define F_LPARENT 0xa8 // Farsi '(' character
#define F_RPARENT 0xa9 /* Farsi ')' character */ #define F_RPARENT 0xa9 // Farsi ')' character
#define F_MUL 0xaa /* Farsi 'x' character */ #define F_MUL 0xaa // Farsi 'x' character
#define F_PLUS 0xab /* Farsi '+' character */ #define F_PLUS 0xab // Farsi '+' character
#define F_BCOMMA 0xac /* Farsi comma character */ #define F_BCOMMA 0xac // Farsi comma character
#define F_MINUS 0xad /* Farsi '-' character */ #define F_MINUS 0xad // Farsi '-' character
#define F_DIVIDE 0xae /* Farsi divide (/) character */ #define F_DIVIDE 0xae // Farsi divide (/) character
#define F_SLASH 0xaf /* Farsi '/' character */ #define F_SLASH 0xaf // Farsi '/' character
#define FARSI_0 0xb0 #define FARSI_0 0xb0
#define FARSI_1 0xb1 #define FARSI_1 0xb1
@@ -102,12 +86,12 @@
#define FARSI_8 0xb8 #define FARSI_8 0xb8
#define FARSI_9 0xb9 #define FARSI_9 0xb9
#define F_DCOLON 0xba /* Farsi ':' character */ #define F_DCOLON 0xba // Farsi ':' character
#define F_SEMICOLON 0xbb /* Farsi ';' character */ #define F_SEMICOLON 0xbb // Farsi ';' character
#define F_GREATER 0xbc /* Farsi '>' character */ #define F_GREATER 0xbc // Farsi '>' character
#define F_EQUALS 0xbd /* Farsi '=' character */ #define F_EQUALS 0xbd // Farsi '=' character
#define F_LESS 0xbe /* Farsi '<' character */ #define F_LESS 0xbe // Farsi '<' character
#define F_QUESTION 0xbf /* Farsi ? character */ #define F_QUESTION 0xbf // Farsi ? character
#define ALEF_A 0xc0 #define ALEF_A 0xc0
#define ALEF 0xc1 #define ALEF 0xc1
@@ -141,22 +125,22 @@
#define MIM 0xdd #define MIM 0xdd
#define NOON 0xde #define NOON 0xde
#define WAW 0xdf #define WAW 0xdf
#define F_HE 0xe0 /* F_ added for name clash with Perl */ #define F_HE 0xe0 // F_ added for name clash with Perl
#define YE 0xe1 #define YE 0xe1
#define TEE 0xfc #define TEE 0xfc
#define _KAF_H 0xfd #define _KAF_H 0xfd
#define YEE 0xfe #define YEE 0xfe
#define F_LBRACK 0xe2 /* Farsi '[' character */ #define F_LBRACK 0xe2 // Farsi '[' character
#define F_RBRACK 0xe3 /* Farsi ']' character */ #define F_RBRACK 0xe3 // Farsi ']' character
#define F_LBRACE 0xe4 /* Farsi '{' character */ #define F_LBRACE 0xe4 // Farsi '{' character
#define F_RBRACE 0xe5 /* Farsi '}' character */ #define F_RBRACE 0xe5 // Farsi '}' character
#define F_LQUOT 0xe6 /* Farsi left quotation character */ #define F_LQUOT 0xe6 // Farsi left quotation character
#define F_RQUOT 0xe7 /* Farsi right quotation character */ #define F_RQUOT 0xe7 // Farsi right quotation character
#define F_STAR 0xe8 /* Farsi '*' character */ #define F_STAR 0xe8 // Farsi '*' character
#define F_UNDERLINE 0xe9 /* Farsi '_' character */ #define F_UNDERLINE 0xe9 // Farsi '_' character
#define F_PIPE 0xea /* Farsi '|' character */ #define F_PIPE 0xea // Farsi '|' character
#define F_BSLASH 0xeb /* Farsi '\' character */ #define F_BSLASH 0xeb // Farsi '\' character
#define MAD 0xf0 #define MAD 0xf0
#define JAZR 0xf1 #define JAZR 0xf1
@@ -170,23 +154,19 @@
#define WAW_H 0xf9 #define WAW_H 0xf9
#define ALEF_D_H 0xfa #define ALEF_D_H 0xfa
/* // global definitions
* global definitions // ==================
* ==================
*/
#define SRC_EDT 0 #define SRC_EDT 0
#define SRC_CMD 1 #define SRC_CMD 1
#define AT_CURSOR 0 #define AT_CURSOR 0
/* // definitions for the window dependent functions (w_farsi).
* definitions for the window dependent functions (w_farsi).
*/
#define W_CONV 0x1 #define W_CONV 0x1
#define W_R_L 0x2 #define W_R_L 0x2
/* special Farsi text messages */ // special Farsi text messages
#ifdef DO_INIT #ifdef DO_INIT
EXTERN char_u farsi_text_1[] = { EXTERN char_u farsi_text_1[] = {
@@ -231,4 +211,4 @@ EXTERN char_u farsi_text_5[] = {
EXTERN char_u farsi_text_5[]; EXTERN char_u farsi_text_5[];
#endif #endif
#endif // SRC_FARSI_H_ #endif // NEOVIM_FARSI_H

View File

@@ -1,39 +1,38 @@
/* /// @file garray.c
* Functions for handling growing arrays. ///
*/ /// Functions for handling growing arrays.
#include "vim.h" #include "vim.h"
#include "ascii.h" #include "ascii.h"
#include "misc2.h" #include "misc2.h"
#include "garray.h" #include "garray.h"
//#include "globals.h"
// #include "globals.h"
#include "memline.h" #include "memline.h"
/* /// Clear an allocated growing array.
* Clear an allocated growing array.
*/
void ga_clear(garray_T *gap) void ga_clear(garray_T *gap)
{ {
vim_free(gap->ga_data); vim_free(gap->ga_data);
ga_init(gap); ga_init(gap);
} }
/* /// Clear a growing array that contains a list of strings.
* Clear a growing array that contains a list of strings. ///
*/ /// @param gap
void ga_clear_strings(garray_T *gap) void ga_clear_strings(garray_T *gap)
{ {
int i; int i;
for (i = 0; i < gap->ga_len; ++i) {
for (i = 0; i < gap->ga_len; ++i)
vim_free(((char_u **)(gap->ga_data))[i]); vim_free(((char_u **)(gap->ga_data))[i]);
}
ga_clear(gap); ga_clear(gap);
} }
/* /// Initialize a growing array. Don't forget to set ga_itemsize and
* Initialize a growing array. Don't forget to set ga_itemsize and /// ga_growsize! Or use ga_init2().
* ga_growsize! Or use ga_init2(). ///
*/ /// @param gap
void ga_init(garray_T *gap) void ga_init(garray_T *gap)
{ {
gap->ga_data = NULL; gap->ga_data = NULL;
@@ -41,6 +40,11 @@ void ga_init(garray_T *gap)
gap->ga_len = 0; gap->ga_len = 0;
} }
/// Initialize a growing array.
///
/// @param gap
/// @param itemsize
/// @param growsize
void ga_init2(garray_T *gap, int itemsize, int growsize) void ga_init2(garray_T *gap, int itemsize, int growsize)
{ {
ga_init(gap); ga_init(gap);
@@ -48,10 +52,12 @@ void ga_init2(garray_T *gap, int itemsize, int growsize)
gap->ga_growsize = growsize; gap->ga_growsize = growsize;
} }
/* /// Make room in growing array "gap" for at least "n" items.
* Make room in growing array "gap" for at least "n" items. ///
* Return FAIL for failure, OK otherwise. /// @param gap
*/ /// @param n
///
/// @return FAIL for failure, OK otherwise.
int ga_grow(garray_T *gap, int n) int ga_grow(garray_T *gap, int n)
{ {
size_t old_len; size_t old_len;
@@ -59,13 +65,17 @@ int ga_grow(garray_T *gap, int n)
char_u *pp; char_u *pp;
if (gap->ga_maxlen - gap->ga_len < n) { if (gap->ga_maxlen - gap->ga_len < n) {
if (n < gap->ga_growsize) if (n < gap->ga_growsize) {
n = gap->ga_growsize; n = gap->ga_growsize;
}
new_len = gap->ga_itemsize * (gap->ga_len + n); new_len = gap->ga_itemsize * (gap->ga_len + n);
pp = (gap->ga_data == NULL) pp = (gap->ga_data == NULL)
? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len); ? alloc((unsigned)new_len)
if (pp == NULL) : vim_realloc(gap->ga_data, new_len);
if (pp == NULL) {
return FAIL; return FAIL;
}
old_len = gap->ga_itemsize * gap->ga_maxlen; old_len = gap->ga_itemsize * gap->ga_maxlen;
vim_memset(pp + old_len, 0, new_len - old_len); vim_memset(pp + old_len, 0, new_len - old_len);
gap->ga_maxlen = gap->ga_len + n; gap->ga_maxlen = gap->ga_len + n;
@@ -74,71 +84,79 @@ int ga_grow(garray_T *gap, int n)
return OK; return OK;
} }
/* /// For a growing array that contains a list of strings: concatenate all the
* For a growing array that contains a list of strings: concatenate all the /// strings with a separating comma.
* strings with a separating comma. ///
* Returns NULL when out of memory. /// @param gap
*/ ///
char_u *ga_concat_strings(garray_T *gap) /// @returns NULL when out of memory.
char_u* ga_concat_strings(garray_T *gap)
{ {
int i; int i;
int len = 0; int len = 0;
char_u *s; char_u *s;
for (i = 0; i < gap->ga_len; ++i) for (i = 0; i < gap->ga_len; ++i) {
len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + 1; len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + 1;
}
s = alloc(len + 1); s = alloc(len + 1);
if (s != NULL) { if (s != NULL) {
*s = NUL; *s = NUL;
for (i = 0; i < gap->ga_len; ++i) { for (i = 0; i < gap->ga_len; ++i) {
if (*s != NUL) if (*s != NUL) {
STRCAT(s, ","); STRCAT(s, ",");
}
STRCAT(s, ((char_u **)(gap->ga_data))[i]); STRCAT(s, ((char_u **)(gap->ga_data))[i]);
} }
} }
return s; return s;
} }
/* /// Concatenate a string to a growarray which contains characters.
* Concatenate a string to a growarray which contains characters. /// Note: Does NOT copy the NUL at the end!
* Note: Does NOT copy the NUL at the end! ///
*/ /// @param gap
/// @param s
void ga_concat(garray_T *gap, char_u *s) void ga_concat(garray_T *gap, char_u *s)
{ {
int len = (int)STRLEN(s); int len = (int)STRLEN(s);
if (ga_grow(gap, len) == OK) { if (ga_grow(gap, len) == OK) {
mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len); mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
gap->ga_len += len; gap->ga_len += len;
} }
} }
/* /// Append one byte to a growarray which contains bytes.
* Append one byte to a growarray which contains bytes. ///
*/ /// @param gap
/// @param c
void ga_append(garray_T *gap, int c) void ga_append(garray_T *gap, int c)
{ {
if (ga_grow(gap, 1) == OK) { if (ga_grow(gap, 1) == OK) {
*((char *)gap->ga_data + gap->ga_len) = c; *((char *) gap->ga_data + gap->ga_len) = c;
++gap->ga_len; ++gap->ga_len;
} }
} }
#if defined(UNIX) || defined(WIN3264) #if defined(UNIX) || defined(WIN3264)
/*
* Append the text in "gap" below the cursor line and clear "gap". /// Append the text in "gap" below the cursor line and clear "gap".
*/ ///
/// @param gap
void append_ga_line(garray_T *gap) void append_ga_line(garray_T *gap)
{ {
/* Remove trailing CR. */ // Remove trailing CR.
if (gap->ga_len > 0 if ((gap->ga_len > 0)
&& !curbuf->b_p_bin && !curbuf->b_p_bin
&& ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR) && (((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)) {
--gap->ga_len; gap->ga_len--;
}
ga_append(gap, NUL); ga_append(gap, NUL);
ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE); ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
gap->ga_len = 0; gap->ga_len = 0;
} }
#endif
#endif // if defined(UNIX) || defined(WIN3264)

View File

@@ -1,20 +1,18 @@
#ifndef NEOVIM_GARRAY_H #ifndef NEOVIM_GARRAY_H
#define NEOVIM_GARRAY_H #define NEOVIM_GARRAY_H
/* /// Structure used for growing arrays.
* Structure used for growing arrays. /// This is used to store information that only grows, is deleted all at
* This is used to store information that only grows, is deleted all at /// once, and needs to be accessed by index. See ga_clear() and ga_grow().
* once, and needs to be accessed by index. See ga_clear() and ga_grow().
*/
typedef struct growarray { typedef struct growarray {
int ga_len; /* current number of items used */ int ga_len; // current number of items used
int ga_maxlen; /* maximum number of items possible */ int ga_maxlen; // maximum number of items possible
int ga_itemsize; /* sizeof(item) */ int ga_itemsize; // sizeof(item)
int ga_growsize; /* number of items to grow each time */ int ga_growsize; // number of items to grow each time
void *ga_data; /* pointer to the first item */ void *ga_data; // pointer to the first item
} garray_T; } garray_T;
#define GA_EMPTY {0, 0, 0, 0, NULL} #define GA_EMPTY { 0, 0, 0, 0, NULL }
void ga_clear(garray_T *gap); void ga_clear(garray_T *gap);
void ga_clear_strings(garray_T *gap); void ga_clear_strings(garray_T *gap);
@@ -26,4 +24,4 @@ void ga_concat(garray_T *gap, char_u *s);
void ga_append(garray_T *gap, int c); void ga_append(garray_T *gap, int c);
void append_ga_line(garray_T *gap); void append_ga_line(garray_T *gap);
#endif /* NEOVIM_GARRAY_H */ #endif // NEOVIM_GARRAY_H

View File

@@ -1,101 +1,102 @@
/* vi:set ts=8 sts=4 sw=4: /// @file hashtab.c
* ///
* VIM - Vi IMproved by Bram Moolenaar /// Handling of a hashtable with Vim-specific properties.
* ///
* Do ":help uganda" in Vim to read copying and usage conditions. /// Each item in a hashtable has a NUL terminated string key. A key can appear
* Do ":help credits" in Vim to see a list of people who contributed. /// only once in the table.
* See README.txt for an overview of the Vim source code. ///
*/ /// A hash number is computed from the key for quick lookup. When the hashes
/// of two different keys point to the same entry an algorithm is used to
/* /// iterate over other entries in the table until the right one is found.
* hashtab.c: Handling of a hashtable with Vim-specific properties. /// To make the iteration work removed keys are different from entries where a
* /// key was never present.
* Each item in a hashtable has a NUL terminated string key. A key can appear ///
* only once in the table. /// The mechanism has been partly based on how Python Dictionaries are
* /// implemented. The algorithm is from Knuth Vol. 3, Sec. 6.4.
* A hash number is computed from the key for quick lookup. When the hashes ///
* of two different keys point to the same entry an algorithm is used to /// The hashtable grows to accommodate more entries when needed. At least 1/3
* iterate over other entries in the table until the right one is found. /// of the entries is empty to keep the lookup efficient (at the cost of extra
* To make the iteration work removed keys are different from entries where a /// memory).
* key was never present.
*
* The mechanism has been partly based on how Python Dictionaries are
* implemented. The algorithm is from Knuth Vol. 3, Sec. 6.4.
*
* The hashtable grows to accommodate more entries when needed. At least 1/3
* of the entries is empty to keep the lookup efficient (at the cost of extra
* memory).
*/
#include "vim.h" #include "vim.h"
#include "hashtab.h" #include "hashtab.h"
#include "message.h" #include "message.h"
#include "misc2.h" #include "misc2.h"
/* Magic value for algorithm that walks through the array. */ // Magic value for algorithm that walks through the array.
#define PERTURB_SHIFT 5 #define PERTURB_SHIFT 5
static int hash_may_resize(hashtab_T *ht, int minitems); static int hash_may_resize(hashtab_T *ht, int minitems);
/* /// Initialize an empty hash table.
* Initialize an empty hash table. ///
*/ /// @param ht
void hash_init(hashtab_T *ht) void hash_init(hashtab_T *ht)
{ {
/* This zeroes all "ht_" entries and all the "hi_key" in "ht_smallarray". */ // This zeroes all "ht_" entries and all the "hi_key" in "ht_smallarray".
vim_memset(ht, 0, sizeof(hashtab_T)); vim_memset(ht, 0, sizeof(hashtab_T));
ht->ht_array = ht->ht_smallarray; ht->ht_array = ht->ht_smallarray;
ht->ht_mask = HT_INIT_SIZE - 1; ht->ht_mask = HT_INIT_SIZE - 1;
} }
/* /// Free the array of a hash table. Does not free the items it contains!
* Free the array of a hash table. Does not free the items it contains! /// If "ht" is not freed then you should call hash_init() next!
* If "ht" is not freed then you should call hash_init() next! ///
*/ /// @param ht
void hash_clear(hashtab_T *ht) void hash_clear(hashtab_T *ht)
{ {
if (ht->ht_array != ht->ht_smallarray) if (ht->ht_array != ht->ht_smallarray) {
vim_free(ht->ht_array); vim_free(ht->ht_array);
}
} }
/* /// Free the array of a hash table and all the keys it contains. The keys must
* Free the array of a hash table and all the keys it contains. The keys must /// have been allocated. "off" is the offset from the start of the allocate
* have been allocated. "off" is the offset from the start of the allocate /// memory to the location of the key (it's always positive).
* memory to the location of the key (it's always positive). ///
*/ /// @param ht
/// @param off
void hash_clear_all(hashtab_T *ht, int off) void hash_clear_all(hashtab_T *ht, int off)
{ {
long todo; long todo;
hashitem_T *hi; hashitem_T *hi;
todo = (long)ht->ht_used; todo = (long)ht->ht_used;
for (hi = ht->ht_array; todo > 0; ++hi) { for (hi = ht->ht_array; todo > 0; ++hi) {
if (!HASHITEM_EMPTY(hi)) { if (!HASHITEM_EMPTY(hi)) {
vim_free(hi->hi_key - off); vim_free(hi->hi_key - off);
--todo; todo--;
} }
} }
hash_clear(ht); hash_clear(ht);
} }
/* /// Find "key" in hashtable "ht". "key" must not be NULL.
* Find "key" in hashtable "ht". "key" must not be NULL. /// Always returns a pointer to a hashitem. If the item was not found then
* Always returns a pointer to a hashitem. If the item was not found then /// HASHITEM_EMPTY() is TRUE. The pointer is then the place where the key
* HASHITEM_EMPTY() is TRUE. The pointer is then the place where the key /// would be added.
* would be added. /// WARNING: The returned pointer becomes invalid when the hashtable is changed
* WARNING: The returned pointer becomes invalid when the hashtable is changed /// (adding, setting or removing an item)!
* (adding, setting or removing an item)! ///
*/ /// @param ht
hashitem_T *hash_find(hashtab_T *ht, char_u *key) /// @param key
///
/// @return Pointer to the hashitem stored with the given key.
hashitem_T* hash_find(hashtab_T *ht, char_u *key)
{ {
return hash_lookup(ht, key, hash_hash(key)); return hash_lookup(ht, key, hash_hash(key));
} }
/* /// Like hash_find(), but caller computes "hash".
* Like hash_find(), but caller computes "hash". ///
*/ /// @param ht
hashitem_T *hash_lookup(hashtab_T *ht, char_u *key, hash_T hash) /// @param key
/// @param hash
///
/// @return Pointer to the hashitem stored with the given key.
hashitem_T* hash_lookup(hashtab_T *ht, char_u *key, hash_T hash)
{ {
hash_T perturb; hash_T perturb;
hashitem_T *freeitem; hashitem_T *freeitem;
@@ -103,78 +104,83 @@ hashitem_T *hash_lookup(hashtab_T *ht, char_u *key, hash_T hash)
unsigned idx; unsigned idx;
#ifdef HT_DEBUG #ifdef HT_DEBUG
++hash_count_lookup; hash_count_lookup++;
#endif #endif // ifdef HT_DEBUG
/* // Quickly handle the most common situations:
* Quickly handle the most common situations: // - return if there is no item at all
* - return if there is no item at all // - skip over a removed item
* - skip over a removed item // - return if the item matches
* - return if the item matches
*/
idx = (unsigned)(hash & ht->ht_mask); idx = (unsigned)(hash & ht->ht_mask);
hi = &ht->ht_array[idx]; hi = &ht->ht_array[idx];
if (hi->hi_key == NULL) if (hi->hi_key == NULL) {
return hi; return hi;
if (hi->hi_key == HI_KEY_REMOVED) }
freeitem = hi;
else if (hi->hi_hash == hash && STRCMP(hi->hi_key, key) == 0)
return hi;
else
freeitem = NULL;
/* if (hi->hi_key == HI_KEY_REMOVED) {
* Need to search through the table to find the key. The algorithm freeitem = hi;
* to step through the table starts with large steps, gradually becoming } else if ((hi->hi_hash == hash) && (STRCMP(hi->hi_key, key) == 0)) {
* smaller down to (1/4 table size + 1). This means it goes through all return hi;
* table entries in the end. } else {
* When we run into a NULL key it's clear that the key isn't there. freeitem = NULL;
* Return the first available slot found (can be a slot of a removed }
* item).
*/ // Need to search through the table to find the key. The algorithm
// to step through the table starts with large steps, gradually becoming
// smaller down to (1/4 table size + 1). This means it goes through all
// table entries in the end.
// When we run into a NULL key it's clear that the key isn't there.
// Return the first available slot found (can be a slot of a removed
// item).
for (perturb = hash;; perturb >>= PERTURB_SHIFT) { for (perturb = hash;; perturb >>= PERTURB_SHIFT) {
#ifdef HT_DEBUG #ifdef HT_DEBUG
++hash_count_perturb; /* count a "miss" for hashtab lookup */ // count a "miss" for hashtab lookup
#endif hash_count_perturb++;
#endif // ifdef HT_DEBUG
idx = (unsigned)((idx << 2U) + idx + perturb + 1U); idx = (unsigned)((idx << 2U) + idx + perturb + 1U);
hi = &ht->ht_array[idx & ht->ht_mask]; hi = &ht->ht_array[idx & ht->ht_mask];
if (hi->hi_key == NULL)
if (hi->hi_key == NULL) {
return freeitem == NULL ? hi : freeitem; return freeitem == NULL ? hi : freeitem;
if (hi->hi_hash == hash }
&& hi->hi_key != HI_KEY_REMOVED
&& STRCMP(hi->hi_key, key) == 0) if ((hi->hi_hash == hash)
&& (hi->hi_key != HI_KEY_REMOVED)
&& (STRCMP(hi->hi_key, key) == 0)) {
return hi; return hi;
if (hi->hi_key == HI_KEY_REMOVED && freeitem == NULL) }
if ((hi->hi_key == HI_KEY_REMOVED) && (freeitem == NULL)) {
freeitem = hi; freeitem = hi;
} }
}
} }
/* /// Print the efficiency of hashtable lookups.
* Print the efficiency of hashtable lookups. /// Useful when trying different hash algorithms.
* Useful when trying different hash algorithms. /// Called when exiting.
* Called when exiting. void hash_debug_results(void)
*/ {
void hash_debug_results(void) {
#ifdef HT_DEBUG #ifdef HT_DEBUG
fprintf(stderr, "\r\n\r\n\r\n\r\n"); fprintf(stderr, "\r\n\r\n\r\n\r\n");
fprintf(stderr, "Number of hashtable lookups: %ld\r\n", hash_count_lookup); fprintf(stderr, "Number of hashtable lookups: %ld\r\n", hash_count_lookup);
fprintf(stderr, "Number of perturb loops: %ld\r\n", hash_count_perturb); fprintf(stderr, "Number of perturb loops: %ld\r\n", hash_count_perturb);
fprintf(stderr, "Percentage of perturb loops: %ld%%\r\n", fprintf(stderr, "Percentage of perturb loops: %ld%%\r\n",
hash_count_perturb * 100 / hash_count_lookup); hash_count_perturb * 100 / hash_count_lookup);
#endif #endif // ifdef HT_DEBUG
} }
/* /// Add item with key "key" to hashtable "ht".
* Add item with key "key" to hashtable "ht". ///
* Returns FAIL when out of memory or the key is already present. /// @param ht
*/ /// @param key
///
/// @returns FAIL when out of memory or the key is already present.
int hash_add(hashtab_T *ht, char_u *key) int hash_add(hashtab_T *ht, char_u *key)
{ {
hash_T hash = hash_hash(key); hash_T hash = hash_hash(key);
hashitem_T *hi; hashitem_T *hi = hash_lookup(ht, key, hash);
hi = hash_lookup(ht, key, hash);
if (!HASHITEM_EMPTY(hi)) { if (!HASHITEM_EMPTY(hi)) {
EMSG2(_(e_intern2), "hash_add()"); EMSG2(_(e_intern2), "hash_add()");
return FAIL; return FAIL;
@@ -182,73 +188,75 @@ int hash_add(hashtab_T *ht, char_u *key)
return hash_add_item(ht, hi, key, hash); return hash_add_item(ht, hi, key, hash);
} }
/* /// Add item "hi" with "key" to hashtable "ht". "key" must not be NULL and
* Add item "hi" with "key" to hashtable "ht". "key" must not be NULL and /// "hi" must have been obtained with hash_lookup() and point to an empty item.
* "hi" must have been obtained with hash_lookup() and point to an empty item. /// "hi" is invalid after this!
* "hi" is invalid after this! ///
* Returns OK or FAIL (out of memory). /// @param ht
*/ /// @param hi
/// @param key
/// @param hash
///
/// @returns OK or FAIL (out of memory).
int hash_add_item(hashtab_T *ht, hashitem_T *hi, char_u *key, hash_T hash) int hash_add_item(hashtab_T *ht, hashitem_T *hi, char_u *key, hash_T hash)
{ {
/* If resizing failed before and it fails again we can't add an item. */ // If resizing failed before and it fails again we can't add an item.
if (ht->ht_error && hash_may_resize(ht, 0) == FAIL) if (ht->ht_error && (hash_may_resize(ht, 0) == FAIL)) {
return FAIL; return FAIL;
}
++ht->ht_used; ht->ht_used++;
if (hi->hi_key == NULL) if (hi->hi_key == NULL) {
++ht->ht_filled; ht->ht_filled++;
}
hi->hi_key = key; hi->hi_key = key;
hi->hi_hash = hash; hi->hi_hash = hash;
/* When the space gets low may resize the array. */ // When the space gets low may resize the array.
return hash_may_resize(ht, 0); return hash_may_resize(ht, 0);
} }
/// Remove item "hi" from hashtable "ht". "hi" must have been obtained with
/* /// hash_lookup().
* Remove item "hi" from hashtable "ht". "hi" must have been obtained with ///
* hash_lookup(). /// The caller must take care of freeing the item itself.
* The caller must take care of freeing the item itself. ///
*/ /// @param ht
/// @param hi
void hash_remove(hashtab_T *ht, hashitem_T *hi) void hash_remove(hashtab_T *ht, hashitem_T *hi)
{ {
--ht->ht_used; ht->ht_used--;
hi->hi_key = HI_KEY_REMOVED; hi->hi_key = HI_KEY_REMOVED;
hash_may_resize(ht, 0); hash_may_resize(ht, 0);
} }
/* /// Lock a hashtable: prevent that ht_array changes.
* Lock a hashtable: prevent that ht_array changes. /// Don't use this when items are to be added!
* Don't use this when items are to be added! /// Must call hash_unlock() later.
* Must call hash_unlock() later. ///
*/ /// @param ht
void hash_lock(hashtab_T *ht) void hash_lock(hashtab_T *ht)
{ {
++ht->ht_locked; ht->ht_locked++;
} }
/// Unlock a hashtable: allow ht_array changes again.
/* /// Table will be resized (shrink) when necessary.
* Unlock a hashtable: allow ht_array changes again. /// This must balance a call to hash_lock().
* Table will be resized (shrink) when necessary.
* This must balance a call to hash_lock().
*/
void hash_unlock(hashtab_T *ht) void hash_unlock(hashtab_T *ht)
{ {
--ht->ht_locked; ht->ht_locked--;
(void)hash_may_resize(ht, 0); (void)hash_may_resize(ht, 0);
} }
/* /// Shrink a hashtable when there is too much empty space.
* Shrink a hashtable when there is too much empty space. /// Grow a hashtable when there is not enough empty space.
* Grow a hashtable when there is not enough empty space. ///
* Returns OK or FAIL (out of memory). /// @param ht
*/ /// @param minitems minimal number of items
static int ///
hash_may_resize ( /// @returns OK or FAIL (out of memory).
hashtab_T *ht, static int hash_may_resize(hashtab_T *ht, int minitems)
int minitems /* minimal number of items */
)
{ {
hashitem_T temparray[HT_INIT_SIZE]; hashitem_T temparray[HT_INIT_SIZE];
hashitem_T *oldarray, *newarray; hashitem_T *oldarray, *newarray;
@@ -260,73 +268,89 @@ hash_may_resize (
long_u newmask; long_u newmask;
hash_T perturb; hash_T perturb;
/* Don't resize a locked table. */ // Don't resize a locked table.
if (ht->ht_locked > 0) if (ht->ht_locked > 0) {
return OK; return OK;
}
#ifdef HT_DEBUG #ifdef HT_DEBUG
if (ht->ht_used > ht->ht_filled) if (ht->ht_used > ht->ht_filled) {
EMSG("hash_may_resize(): more used than filled"); EMSG("hash_may_resize(): more used than filled");
if (ht->ht_filled >= ht->ht_mask + 1) }
if (ht->ht_filled >= ht->ht_mask + 1) {
EMSG("hash_may_resize(): table completely filled"); EMSG("hash_may_resize(): table completely filled");
#endif }
#endif // ifdef HT_DEBUG
if (minitems == 0) { if (minitems == 0) {
/* Return quickly for small tables with at least two NULL items. NULL // Return quickly for small tables with at least two NULL items. NULL
* items are required for the lookup to decide a key isn't there. */ // items are required for the lookup to decide a key isn't there.
if (ht->ht_filled < HT_INIT_SIZE - 1 if ((ht->ht_filled < HT_INIT_SIZE - 1)
&& ht->ht_array == ht->ht_smallarray) && (ht->ht_array == ht->ht_smallarray)) {
return OK; return OK;
}
/* // Grow or refill the array when it's more than 2/3 full (including
* Grow or refill the array when it's more than 2/3 full (including // removed items, so that they get cleaned up).
* removed items, so that they get cleaned up). // Shrink the array when it's less than 1/5 full. When growing it is
* Shrink the array when it's less than 1/5 full. When growing it is // at least 1/4 full (avoids repeated grow-shrink operations)
* at least 1/4 full (avoids repeated grow-shrink operations)
*/
oldsize = ht->ht_mask + 1; oldsize = ht->ht_mask + 1;
if (ht->ht_filled * 3 < oldsize * 2 && ht->ht_used > oldsize / 5) if ((ht->ht_filled * 3 < oldsize * 2) && (ht->ht_used > oldsize / 5)) {
return OK; return OK;
}
if (ht->ht_used > 1000) if (ht->ht_used > 1000) {
minsize = ht->ht_used * 2; /* it's big, don't make too much room */ // it's big, don't make too much room
else minsize = ht->ht_used * 2;
minsize = ht->ht_used * 4; /* make plenty of room */
} else { } else {
/* Use specified size. */ // make plenty of room
if ((long_u)minitems < ht->ht_used) /* just in case... */ minsize = ht->ht_used * 4;
}
} else {
// Use specified size.
if ((long_u)minitems < ht->ht_used) {
// just in case...
minitems = (int)ht->ht_used; minitems = (int)ht->ht_used;
minsize = minitems * 3 / 2; /* array is up to 2/3 full */ }
// array is up to 2/3 full
minsize = minitems * 3 / 2;
} }
newsize = HT_INIT_SIZE; newsize = HT_INIT_SIZE;
while (newsize < minsize) { while (newsize < minsize) {
newsize <<= 1; /* make sure it's always a power of 2 */ // make sure it's always a power of 2
if (newsize == 0) newsize <<= 1;
return FAIL; /* overflow */ if (newsize == 0) {
// overflow
return FAIL;
}
} }
if (newsize == HT_INIT_SIZE) { if (newsize == HT_INIT_SIZE) {
/* Use the small array inside the hashdict structure. */ // Use the small array inside the hashdict structure.
newarray = ht->ht_smallarray; newarray = ht->ht_smallarray;
if (ht->ht_array == newarray) { if (ht->ht_array == newarray) {
/* Moving from ht_smallarray to ht_smallarray! Happens when there // Moving from ht_smallarray to ht_smallarray! Happens when there
* are many removed items. Copy the items to be able to clean up // are many removed items. Copy the items to be able to clean up
* removed items. */ // removed items.
mch_memmove(temparray, newarray, sizeof(temparray)); mch_memmove(temparray, newarray, sizeof(temparray));
oldarray = temparray; oldarray = temparray;
} else
oldarray = ht->ht_array;
} else { } else {
/* Allocate an array. */ oldarray = ht->ht_array;
newarray = (hashitem_T *)alloc((unsigned) }
(sizeof(hashitem_T) * newsize)); } else {
// Allocate an array.
newarray = (hashitem_T *)alloc((unsigned)(sizeof(hashitem_T) * newsize));
if (newarray == NULL) { if (newarray == NULL) {
/* Out of memory. When there are NULL items still return OK. // Out of memory. When there are NULL items still return OK.
* Otherwise set ht_error, because lookup may result in a hang if // Otherwise set ht_error, because lookup may result in a hang if
* we add another item. */ // we add another item.
if (ht->ht_filled < ht->ht_mask) if (ht->ht_filled < ht->ht_mask) {
return OK; return OK;
}
ht->ht_error = TRUE; ht->ht_error = TRUE;
return FAIL; return FAIL;
} }
@@ -334,36 +358,36 @@ hash_may_resize (
} }
vim_memset(newarray, 0, (size_t)(sizeof(hashitem_T) * newsize)); vim_memset(newarray, 0, (size_t)(sizeof(hashitem_T) * newsize));
/* // Move all the items from the old array to the new one, placing them in
* Move all the items from the old array to the new one, placing them in // the right spot. The new array won't have any removed items, thus this
* the right spot. The new array won't have any removed items, thus this // is also a cleanup action.
* is also a cleanup action.
*/
newmask = newsize - 1; newmask = newsize - 1;
todo = (int)ht->ht_used; todo = (int)ht->ht_used;
for (olditem = oldarray; todo > 0; ++olditem)
for (olditem = oldarray; todo > 0; ++olditem) {
if (!HASHITEM_EMPTY(olditem)) { if (!HASHITEM_EMPTY(olditem)) {
/* // The algorithm to find the spot to add the item is identical to
* The algorithm to find the spot to add the item is identical to // the algorithm to find an item in hash_lookup(). But we only
* the algorithm to find an item in hash_lookup(). But we only // need to search for a NULL key, thus it's simpler.
* need to search for a NULL key, thus it's simpler.
*/
newi = (unsigned)(olditem->hi_hash & newmask); newi = (unsigned)(olditem->hi_hash & newmask);
newitem = &newarray[newi]; newitem = &newarray[newi];
if (newitem->hi_key != NULL) {
if (newitem->hi_key != NULL)
for (perturb = olditem->hi_hash;; perturb >>= PERTURB_SHIFT) { for (perturb = olditem->hi_hash;; perturb >>= PERTURB_SHIFT) {
newi = (unsigned)((newi << 2U) + newi + perturb + 1U); newi = (unsigned)((newi << 2U) + newi + perturb + 1U);
newitem = &newarray[newi & newmask]; newitem = &newarray[newi & newmask];
if (newitem->hi_key == NULL) if (newitem->hi_key == NULL) {
break; break;
} }
}
}
*newitem = *olditem; *newitem = *olditem;
--todo; todo--;
}
} }
if (ht->ht_array != ht->ht_smallarray) if (ht->ht_array != ht->ht_smallarray) {
vim_free(ht->ht_array); vim_free(ht->ht_array);
}
ht->ht_array = newarray; ht->ht_array = newarray;
ht->ht_mask = newmask; ht->ht_mask = newmask;
ht->ht_filled = ht->ht_used; ht->ht_filled = ht->ht_used;
@@ -372,28 +396,31 @@ hash_may_resize (
return OK; return OK;
} }
/* /// Get the hash number for a key.
* Get the hash number for a key. /// If you think you know a better hash function: Compile with HT_DEBUG set and
* If you think you know a better hash function: Compile with HT_DEBUG set and /// run a script that uses hashtables a lot. Vim will then print statistics
* run a script that uses hashtables a lot. Vim will then print statistics /// when exiting. Try that with the current hash algorithm and yours. The
* when exiting. Try that with the current hash algorithm and yours. The /// lower the percentage the better.
* lower the percentage the better. ///
*/ /// @param key
///
/// @return Hash number for the key.
hash_T hash_hash(char_u *key) hash_T hash_hash(char_u *key)
{ {
hash_T hash; hash_T hash;
char_u *p; char_u *p;
if ((hash = *key) == 0) if ((hash = *key) == 0) {
return (hash_T)0; /* Empty keys are not allowed, but we don't // Empty keys are not allowed, but we don't want to crash if we get one.
want to crash if we get one. */ return (hash_T) 0;
}
p = key + 1; p = key + 1;
/* A simplistic algorithm that appears to do very well. // A simplistic algorithm that appears to do very well.
* Suggested by George Reilly. */ // Suggested by George Reilly.
while (*p != NUL) while (*p != NUL) {
hash = hash * 101 + *p++; hash = hash * 101 + *p++;
}
return hash; return hash;
} }

View File

@@ -1,15 +1,7 @@
/* vi:set ts=8 sts=4 sw=4: /// @file popupmnu.c
* ///
* VIM - Vi IMproved by Bram Moolenaar /// Popup menu (PUM)
* //
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* popupmnu.c: Popup menu (PUM)
*/
#include "vim.h" #include "vim.h"
#include "popupmnu.h" #include "popupmnu.h"
#include "charset.h" #include "charset.h"
@@ -22,40 +14,36 @@
#include "search.h" #include "search.h"
#include "window.h" #include "window.h"
static pumitem_T *pum_array = NULL; /* items of displayed pum */ static pumitem_T *pum_array = NULL; // items of displayed pum
static int pum_size; /* nr of items in "pum_array" */ static int pum_size; // nr of items in "pum_array"
static int pum_selected; /* index of selected item or -1 */ static int pum_selected; // index of selected item or -1
static int pum_first = 0; /* index of top item */ static int pum_first = 0; // index of top item
static int pum_height; /* nr of displayed pum items */ static int pum_height; // nr of displayed pum items
static int pum_width; /* width of displayed pum items */ static int pum_width; // width of displayed pum items
static int pum_base_width; /* width of pum items base */ static int pum_base_width; // width of pum items base
static int pum_kind_width; /* width of pum items kind column */ static int pum_kind_width; // width of pum items kind column
static int pum_scrollbar; /* TRUE when scrollbar present */ static int pum_scrollbar; // TRUE when scrollbar present
static int pum_row; /* top row of pum */ static int pum_row; // top row of pum
static int pum_col; /* left column of pum */ static int pum_col; // left column of pum
static int pum_do_redraw = FALSE; /* do redraw anyway */ static int pum_do_redraw = FALSE; // do redraw anyway
static int pum_set_selected(int n, int repeat); static int pum_set_selected(int n, int repeat);
#define PUM_DEF_HEIGHT 10 #define PUM_DEF_HEIGHT 10
#define PUM_DEF_WIDTH 15 #define PUM_DEF_WIDTH 15
/* /// Show the popup menu with items "array[size]".
* Show the popup menu with items "array[size]". /// "array" must remain valid until pum_undisplay() is called!
* "array" must remain valid until pum_undisplay() is called! /// When possible the leftmost character is aligned with screen column "col".
* When possible the leftmost character is aligned with screen column "col". /// The menu appears above the screen line "row" or at "row" + "height" - 1.
* The menu appears above the screen line "row" or at "row" + "height" - 1. ///
*/ /// @param array
void /// @param size
pum_display ( /// @param selected index of initially selected item, none if out of range
pumitem_T *array, void pum_display(pumitem_T *array, int size, int selected)
int size,
int selected /* index of initially selected item, none if
out of range */
)
{ {
int w; int w;
int def_width; int def_width;
@@ -76,46 +64,50 @@ redo:
kind_width = 0; kind_width = 0;
extra_width = 0; extra_width = 0;
/* Pretend the pum is already there to avoid that must_redraw is set when // Pretend the pum is already there to avoid that must_redraw is set when
* 'cuc' is on. */ // 'cuc' is on.
pum_array = (pumitem_T *)1; pum_array = (pumitem_T *)1;
validate_cursor_col(); validate_cursor_col();
pum_array = NULL; pum_array = NULL;
row = curwin->w_wrow + W_WINROW(curwin); row = curwin->w_wrow + W_WINROW(curwin);
if (firstwin->w_p_pvw) if (firstwin->w_p_pvw) {
top_clear = firstwin->w_height; top_clear = firstwin->w_height;
else } else {
top_clear = 0; top_clear = 0;
}
/* When the preview window is at the bottom stop just above it. Also // When the preview window is at the bottom stop just above it. Also
* avoid drawing over the status line so that it's clear there is a window // avoid drawing over the status line so that it's clear there is a window
* boundary. */ // boundary.
if (lastwin->w_p_pvw) if (lastwin->w_p_pvw) {
above_row -= lastwin->w_height + lastwin->w_status_height + 1; above_row -= lastwin->w_height + lastwin->w_status_height + 1;
}
/* // Figure out the size and position of the pum.
* Figure out the size and position of the pum. if (size < PUM_DEF_HEIGHT) {
*/
if (size < PUM_DEF_HEIGHT)
pum_height = size; pum_height = size;
else } else {
pum_height = PUM_DEF_HEIGHT; pum_height = PUM_DEF_HEIGHT;
if (p_ph > 0 && pum_height > p_ph) }
if ((p_ph > 0) && (pum_height > p_ph)) {
pum_height = p_ph; pum_height = p_ph;
}
/* Put the pum below "row" if possible. If there are few lines decide on // Put the pum below "row" if possible. If there are few lines decide on
* where there is more room. */ // where there is more room.
if (row + 2 >= above_row - pum_height if ((row + 2 >= above_row - pum_height)
&& row > (above_row - top_clear) / 2) { && (row > (above_row - top_clear) / 2)) {
/* pum above "row" */ // pum above "row"
/* Leave two lines of context if possible */ // Leave two lines of context if possible
if (curwin->w_wrow - curwin->w_cline_row >= 2) if (curwin->w_wrow - curwin->w_cline_row >= 2) {
context_lines = 2; context_lines = 2;
else } else {
context_lines = curwin->w_wrow - curwin->w_cline_row; context_lines = curwin->w_wrow - curwin->w_cline_row;
}
if (row >= size + context_lines) { if (row >= size + context_lines) {
pum_row = row - size - context_lines; pum_row = row - size - context_lines;
@@ -124,125 +116,148 @@ redo:
pum_row = 0; pum_row = 0;
pum_height = row - context_lines; pum_height = row - context_lines;
} }
if (p_ph > 0 && pum_height > p_ph) {
if ((p_ph > 0) && (pum_height > p_ph)) {
pum_row += pum_height - p_ph; pum_row += pum_height - p_ph;
pum_height = p_ph; pum_height = p_ph;
} }
} else { } else {
/* pum below "row" */ // pum below "row"
/* Leave two lines of context if possible */ // Leave two lines of context if possible
if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3) if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3) {
context_lines = 3; context_lines = 3;
else } else {
context_lines = curwin->w_cline_row context_lines = curwin->w_cline_row
+ curwin->w_cline_height - curwin->w_wrow; + curwin->w_cline_height - curwin->w_wrow;
pum_row = row + context_lines;
if (size > above_row - pum_row)
pum_height = above_row - pum_row;
else
pum_height = size;
if (p_ph > 0 && pum_height > p_ph)
pum_height = p_ph;
} }
/* don't display when we only have room for one line */ pum_row = row + context_lines;
if (pum_height < 1 || (pum_height == 1 && size > 1)) if (size > above_row - pum_row) {
return; pum_height = above_row - pum_row;
} else {
pum_height = size;
}
/* If there is a preview window at the top avoid drawing over it. */ if ((p_ph > 0) && (pum_height > p_ph)) {
pum_height = p_ph;
}
}
// don't display when we only have room for one line
if ((pum_height < 1) || ((pum_height == 1) && (size > 1))) {
return;
}
// If there is a preview window at the top avoid drawing over it.
if (firstwin->w_p_pvw if (firstwin->w_p_pvw
&& pum_row < firstwin->w_height && (pum_row < firstwin->w_height)
&& pum_height > firstwin->w_height + 4) { && (pum_height > firstwin->w_height + 4)) {
pum_row += firstwin->w_height; pum_row += firstwin->w_height;
pum_height -= firstwin->w_height; pum_height -= firstwin->w_height;
} }
/* Compute the width of the widest match and the widest extra. */ // Compute the width of the widest match and the widest extra.
for (i = 0; i < size; ++i) { for (i = 0; i < size; ++i) {
w = vim_strsize(array[i].pum_text); w = vim_strsize(array[i].pum_text);
if (max_width < w)
if (max_width < w) {
max_width = w; max_width = w;
}
if (array[i].pum_kind != NULL) { if (array[i].pum_kind != NULL) {
w = vim_strsize(array[i].pum_kind) + 1; w = vim_strsize(array[i].pum_kind) + 1;
if (kind_width < w)
if (kind_width < w) {
kind_width = w; kind_width = w;
} }
}
if (array[i].pum_extra != NULL) { if (array[i].pum_extra != NULL) {
w = vim_strsize(array[i].pum_extra) + 1; w = vim_strsize(array[i].pum_extra) + 1;
if (extra_width < w)
if (extra_width < w) {
extra_width = w; extra_width = w;
} }
} }
}
pum_base_width = max_width; pum_base_width = max_width;
pum_kind_width = kind_width; pum_kind_width = kind_width;
/* Calculate column */ // Calculate column
if (curwin->w_p_rl) if (curwin->w_p_rl) {
col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol - 1; col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol - 1;
else } else {
col = W_WINCOL(curwin) + curwin->w_wcol; col = W_WINCOL(curwin) + curwin->w_wcol;
}
/* if there are more items than room we need a scrollbar */ // if there are more items than room we need a scrollbar
if (pum_height < size) { if (pum_height < size) {
pum_scrollbar = 1; pum_scrollbar = 1;
++max_width; max_width++;
} else } else {
pum_scrollbar = 0; pum_scrollbar = 0;
}
if (def_width < max_width) if (def_width < max_width) {
def_width = max_width; def_width = max_width;
}
if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width) if ((((col < Columns - PUM_DEF_WIDTH) || (col < Columns - max_width))
&& !curwin->w_p_rl) && !curwin->w_p_rl)
|| (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width) || (curwin->w_p_rl && ((col > PUM_DEF_WIDTH) || (col > max_width)))) {
)) { // align pum column with "col"
/* align pum column with "col" */
pum_col = col; pum_col = col;
if (curwin->w_p_rl) {
if (curwin->w_p_rl)
pum_width = pum_col - pum_scrollbar + 1; pum_width = pum_col - pum_scrollbar + 1;
else } else {
pum_width = Columns - pum_col - pum_scrollbar; pum_width = Columns - pum_col - pum_scrollbar;
}
if (pum_width > max_width + kind_width + extra_width + 1 if ((pum_width > max_width + kind_width + extra_width + 1)
&& pum_width > PUM_DEF_WIDTH) { && (pum_width > PUM_DEF_WIDTH)) {
pum_width = max_width + kind_width + extra_width + 1; pum_width = max_width + kind_width + extra_width + 1;
if (pum_width < PUM_DEF_WIDTH)
if (pum_width < PUM_DEF_WIDTH) {
pum_width = PUM_DEF_WIDTH; pum_width = PUM_DEF_WIDTH;
} }
}
} else if (Columns < def_width) { } else if (Columns < def_width) {
/* not enough room, will use what we have */ // not enough room, will use what we have
if (curwin->w_p_rl) if (curwin->w_p_rl) {
pum_col = Columns - 1; pum_col = Columns - 1;
else } else {
pum_col = 0; pum_col = 0;
}
pum_width = Columns - 1; pum_width = Columns - 1;
} else { } else {
if (max_width > PUM_DEF_WIDTH) if (max_width > PUM_DEF_WIDTH) {
max_width = PUM_DEF_WIDTH; /* truncate */ // truncate
if (curwin->w_p_rl) max_width = PUM_DEF_WIDTH;
}
if (curwin->w_p_rl) {
pum_col = max_width - 1; pum_col = max_width - 1;
else } else {
pum_col = Columns - max_width; pum_col = Columns - max_width;
}
pum_width = max_width - pum_scrollbar; pum_width = max_width - pum_scrollbar;
} }
pum_array = array; pum_array = array;
pum_size = size; pum_size = size;
/* Set selected item and redraw. If the window size changed need to redo // Set selected item and redraw. If the window size changed need to redo
* the positioning. Limit this to two times, when there is not much // the positioning. Limit this to two times, when there is not much
* room the window size will keep changing. */ // room the window size will keep changing.
if (pum_set_selected(selected, redo_count) && ++redo_count <= 2) if (pum_set_selected(selected, redo_count) && (++redo_count <= 2)) {
goto redo; goto redo;
}
} }
/* /// Redraw the popup menu, using "pum_first" and "pum_selected".
* Redraw the popup menu, using "pum_first" and "pum_selected". void pum_redraw(void)
*/ {
void pum_redraw(void) {
int row = pum_row; int row = pum_row;
int col; int col;
int attr_norm = highlight_attr[HLF_PNI]; int attr_norm = highlight_attr[HLF_PNI];
@@ -260,14 +275,16 @@ void pum_redraw(void) {
int round; int round;
int n; int n;
/* Never display more than we have */ // Never display more than we have
if (pum_first > pum_size - pum_height) if (pum_first > pum_size - pum_height) {
pum_first = pum_size - pum_height; pum_first = pum_size - pum_height;
}
if (pum_scrollbar) { if (pum_scrollbar) {
thumb_heigth = pum_height * pum_height / pum_size; thumb_heigth = pum_height * pum_height / pum_size;
if (thumb_heigth == 0) if (thumb_heigth == 0) {
thumb_heigth = 1; thumb_heigth = 1;
}
thumb_pos = (pum_first * (pum_height - thumb_heigth) thumb_pos = (pum_first * (pum_height - thumb_heigth)
+ (pum_size - pum_height) / 2) + (pum_size - pum_height) / 2)
/ (pum_size - pum_height); / (pum_size - pum_height);
@@ -277,39 +294,55 @@ void pum_redraw(void) {
idx = i + pum_first; idx = i + pum_first;
attr = (idx == pum_selected) ? attr_select : attr_norm; attr = (idx == pum_selected) ? attr_select : attr_norm;
/* prepend a space if there is room */ // prepend a space if there is room
if (curwin->w_p_rl) { if (curwin->w_p_rl) {
if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1) if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1) {
screen_putchar(' ', row, pum_col + 1, attr); screen_putchar(' ', row, pum_col + 1, attr);
} else if (pum_col > 0) }
} else if (pum_col > 0) {
screen_putchar(' ', row, pum_col - 1, attr); screen_putchar(' ', row, pum_col - 1, attr);
}
/* Display each entry, use two spaces for a Tab. // Display each entry, use two spaces for a Tab.
* Do this 3 times: For the main text, kind and extra info */ // Do this 3 times: For the main text, kind and extra info
col = pum_col; col = pum_col;
totwidth = 0; totwidth = 0;
for (round = 1; round <= 3; ++round) { for (round = 1; round <= 3; ++round) {
width = 0; width = 0;
s = NULL; s = NULL;
switch (round) { switch (round) {
case 1: p = pum_array[idx].pum_text; break; case 1:
case 2: p = pum_array[idx].pum_kind; break; p = pum_array[idx].pum_text;
case 3: p = pum_array[idx].pum_extra; break; break;
case 2:
p = pum_array[idx].pum_kind;
break;
case 3:
p = pum_array[idx].pum_extra;
break;
} }
if (p != NULL)
if (p != NULL) {
for (;; mb_ptr_adv(p)) { for (;; mb_ptr_adv(p)) {
if (s == NULL) if (s == NULL) {
s = p; s = p;
}
w = ptr2cells(p); w = ptr2cells(p);
if (*p == NUL || *p == TAB || totwidth + w > pum_width) {
/* Display the text that fits or comes before a Tab. if ((*p == NUL) || (*p == TAB) || (totwidth + w > pum_width)) {
* First convert it to printable characters. */ // Display the text that fits or comes before a Tab.
// First convert it to printable characters.
char_u *st; char_u *st;
int saved = *p; int saved = *p;
*p = NUL; *p = NUL;
st = transstr(s); st = transstr(s);
*p = saved; *p = saved;
if (curwin->w_p_rl) { if (curwin->w_p_rl) {
if (st != NULL) { if (st != NULL) {
char_u *rt = reverse_text(st); char_u *rt = reverse_text(st);
@@ -319,25 +352,23 @@ void pum_redraw(void) {
int size; int size;
size = vim_strsize(rt); size = vim_strsize(rt);
if (size > pum_width) { if (size > pum_width) {
do { do {
size -= has_mbyte size -= has_mbyte ? (*mb_ptr2cells)(rt) : 1;
? (*mb_ptr2cells)(rt) : 1;
mb_ptr_adv(rt); mb_ptr_adv(rt);
} while (size > pum_width); } while (size > pum_width);
if (size < pum_width) { if (size < pum_width) {
/* Most left character requires // Most left character requires 2-cells but only 1 cell
* 2-cells but only 1 cell is // is available on screen. Put a '<' on the left of the
* available on screen. Put a // pum item
* '<' on the left of the pum
* item */
*(--rt) = '<'; *(--rt) = '<';
size++; size++;
} }
} }
screen_puts_len(rt, (int)STRLEN(rt), screen_puts_len(rt, (int)STRLEN(rt), row, col - size + 1,
row, col - size + 1, attr); attr);
vim_free(rt_start); vim_free(rt_start);
} }
vim_free(st); vim_free(st);
@@ -345,44 +376,51 @@ void pum_redraw(void) {
col -= width; col -= width;
} else { } else {
if (st != NULL) { if (st != NULL) {
screen_puts_len(st, (int)STRLEN(st), row, col, screen_puts_len(st, (int)STRLEN(st), row, col, attr);
attr);
vim_free(st); vim_free(st);
} }
col += width; col += width;
} }
if (*p != TAB) if (*p != TAB) {
break; break;
}
/* Display two spaces for a Tab. */ // Display two spaces for a Tab.
if (curwin->w_p_rl) { if (curwin->w_p_rl) {
screen_puts_len((char_u *)" ", 2, row, col - 1, screen_puts_len((char_u *)" ", 2, row, col - 1, attr);
attr);
col -= 2; col -= 2;
} else { } else {
screen_puts_len((char_u *)" ", 2, row, col, attr); screen_puts_len((char_u *)" ", 2, row, col, attr);
col += 2; col += 2;
} }
totwidth += 2; totwidth += 2;
s = NULL; /* start text at next char */ // start text at next char
s = NULL;
width = 0; width = 0;
} else } else {
width += w; width += w;
} }
}
}
if (round > 1) if (round > 1) {
n = pum_kind_width + 1; n = pum_kind_width + 1;
else } else {
n = 1; n = 1;
}
/* Stop when there is nothing more to display. */ // Stop when there is nothing more to display.
if (round == 3 if ((round == 3)
|| (round == 2 && pum_array[idx].pum_extra == NULL) || ((round == 2)
|| (round == 1 && pum_array[idx].pum_kind == NULL && (pum_array[idx].pum_extra == NULL))
&& pum_array[idx].pum_extra == NULL) || ((round == 1)
|| pum_base_width + n >= pum_width) && (pum_array[idx].pum_kind == NULL)
&& (pum_array[idx].pum_extra == NULL))
|| (pum_base_width + n >= pum_width)) {
break; break;
}
if (curwin->w_p_rl) { if (curwin->w_p_rl) {
screen_fill(row, row + 1, pum_col - pum_base_width - n + 1, screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
col + 1, ' ', ' ', attr); col + 1, ' ', ' ', attr);
@@ -395,37 +433,40 @@ void pum_redraw(void) {
totwidth = pum_base_width + n; totwidth = pum_base_width + n;
} }
if (curwin->w_p_rl) if (curwin->w_p_rl) {
screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ', screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ', ' ',
' ', attr);
else
screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
attr); attr);
} else {
screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', attr);
}
if (pum_scrollbar > 0) { if (pum_scrollbar > 0) {
if (curwin->w_p_rl) if (curwin->w_p_rl) {
screen_putchar(' ', row, pum_col - pum_width, screen_putchar(' ', row, pum_col - pum_width,
i >= thumb_pos && i < thumb_pos + thumb_heigth i >= thumb_pos && i < thumb_pos + thumb_heigth
? attr_thumb : attr_scroll); ? attr_thumb : attr_scroll);
else } else {
screen_putchar(' ', row, pum_col + pum_width, screen_putchar(' ', row, pum_col + pum_width,
i >= thumb_pos && i < thumb_pos + thumb_heigth i >= thumb_pos && i < thumb_pos + thumb_heigth
? attr_thumb : attr_scroll); ? attr_thumb : attr_scroll);
} }
}
++row; row++;
} }
} }
/* /// Set the index of the currently selected item. The menu will scroll when
* Set the index of the currently selected item. The menu will scroll when /// necessary. When "n" is out of range don't scroll.
* necessary. When "n" is out of range don't scroll. /// This may be repeated when the preview window is used:
* This may be repeated when the preview window is used: /// "repeat" == 0: open preview window normally
* "repeat" == 0: open preview window normally /// "repeat" == 1: open preview window but don't set the size
* "repeat" == 1: open preview window but don't set the size /// "repeat" == 2: don't open preview window
* "repeat" == 2: don't open preview window ///
* Returns TRUE when the window was resized and the location of the popup menu /// @param n
* must be recomputed. /// @param repeat
*/ ///
/// @returns TRUE when the window was resized and the location of the popup
/// menu must be recomputed.
static int pum_set_selected(int n, int repeat) static int pum_set_selected(int n, int repeat)
{ {
int resized = FALSE; int resized = FALSE;
@@ -433,81 +474,92 @@ static int pum_set_selected(int n, int repeat)
pum_selected = n; pum_selected = n;
if (pum_selected >= 0 && pum_selected < pum_size) { if ((pum_selected >= 0) && (pum_selected < pum_size)) {
if (pum_first > pum_selected - 4) { if (pum_first > pum_selected - 4) {
/* scroll down; when we did a jump it's probably a PageUp then // scroll down; when we did a jump it's probably a PageUp then
* scroll a whole page */ // scroll a whole page
if (pum_first > pum_selected - 2) { if (pum_first > pum_selected - 2) {
pum_first -= pum_height - 2; pum_first -= pum_height - 2;
if (pum_first < 0) if (pum_first < 0) {
pum_first = 0; pum_first = 0;
else if (pum_first > pum_selected) } else if (pum_first > pum_selected) {
pum_first = pum_selected; pum_first = pum_selected;
} else }
} else {
pum_first = pum_selected; pum_first = pum_selected;
}
} else if (pum_first < pum_selected - pum_height + 5) { } else if (pum_first < pum_selected - pum_height + 5) {
/* scroll up; when we did a jump it's probably a PageDown then // scroll up; when we did a jump it's probably a PageDown then
* scroll a whole page */ // scroll a whole page
if (pum_first < pum_selected - pum_height + 1 + 2) { if (pum_first < pum_selected - pum_height + 1 + 2) {
pum_first += pum_height - 2; pum_first += pum_height - 2;
if (pum_first < pum_selected - pum_height + 1) if (pum_first < pum_selected - pum_height + 1) {
pum_first = pum_selected - pum_height + 1;
} else
pum_first = pum_selected - pum_height + 1; pum_first = pum_selected - pum_height + 1;
} }
} else {
pum_first = pum_selected - pum_height + 1;
}
}
/* Give a few lines of context when possible. */ // Give a few lines of context when possible.
if (context > 3) if (context > 3) {
context = 3; context = 3;
}
if (pum_height > 2) { if (pum_height > 2) {
if (pum_first > pum_selected - context) { if (pum_first > pum_selected - context) {
/* scroll down */ // scroll down
pum_first = pum_selected - context; pum_first = pum_selected - context;
if (pum_first < 0)
if (pum_first < 0) {
pum_first = 0; pum_first = 0;
}
} else if (pum_first < pum_selected + context - pum_height + 1) { } else if (pum_first < pum_selected + context - pum_height + 1) {
/* scroll up */ // scroll up
pum_first = pum_selected + context - pum_height + 1; pum_first = pum_selected + context - pum_height + 1;
} }
} }
/* // Show extra info in the preview window if there is something and
* Show extra info in the preview window if there is something and // 'completeopt' contains "preview".
* 'completeopt' contains "preview". // Skip this when tried twice already.
* Skip this when tried twice already. // Skip this also when there is not much room.
* Skip this also when there is not much room. // NOTE: Be very careful not to sync undo!
* NOTE: Be very careful not to sync undo! if ((pum_array[pum_selected].pum_info != NULL)
*/ && (Rows > 10)
if (pum_array[pum_selected].pum_info != NULL && (repeat <= 1)
&& Rows > 10 && (vim_strchr(p_cot, 'p') != NULL)) {
&& repeat <= 1
&& vim_strchr(p_cot, 'p') != NULL) {
win_T *curwin_save = curwin; win_T *curwin_save = curwin;
int res = OK; int res = OK;
/* Open a preview window. 3 lines by default. Prefer // Open a preview window. 3 lines by default. Prefer
* 'previewheight' if set and smaller. */ // 'previewheight' if set and smaller.
g_do_tagpreview = 3; g_do_tagpreview = 3;
if (p_pvh > 0 && p_pvh < g_do_tagpreview)
if ((p_pvh > 0) && (p_pvh < g_do_tagpreview)) {
g_do_tagpreview = p_pvh; g_do_tagpreview = p_pvh;
}
resized = prepare_tagpreview(FALSE); resized = prepare_tagpreview(FALSE);
g_do_tagpreview = 0; g_do_tagpreview = 0;
if (curwin->w_p_pvw) { if (curwin->w_p_pvw) {
if (curbuf->b_fname == NULL if ((curbuf->b_fname == NULL)
&& curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f' && (curbuf->b_p_bt[0] == 'n')
&& curbuf->b_p_bh[0] == 'w') { && (curbuf->b_p_bt[2] == 'f')
/* Already a "wipeout" buffer, make it empty. */ && (curbuf->b_p_bh[0] == 'w')) {
while (!bufempty()) // Already a "wipeout" buffer, make it empty.
while (!bufempty()) {
ml_delete((linenr_T)1, FALSE); ml_delete((linenr_T)1, FALSE);
}
} else { } else {
/* Don't want to sync undo in the current buffer. */ // Don't want to sync undo in the current buffer.
++no_u_sync; no_u_sync++;
res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL); res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
--no_u_sync; no_u_sync--;
if (res == OK) { if (res == OK) {
/* Edit a new, empty buffer. Set options for a "wipeout" // Edit a new, empty buffer. Set options for a "wipeout"
* buffer. */ // buffer.
set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
set_option_value((char_u *)"bt", 0L, set_option_value((char_u *)"bt", 0L,
(char_u *)"nofile", OPT_LOCAL); (char_u *)"nofile", OPT_LOCAL);
@@ -517,11 +569,12 @@ static int pum_set_selected(int n, int repeat)
NULL, OPT_LOCAL); NULL, OPT_LOCAL);
} }
} }
if (res == OK) { if (res == OK) {
char_u *p, *e; char_u *p, *e;
linenr_T lnum = 0; linenr_T lnum = 0;
for (p = pum_array[pum_selected].pum_info; *p != NUL; ) { for (p = pum_array[pum_selected].pum_info; *p != NUL;) {
e = vim_strchr(p, '\n'); e = vim_strchr(p, '\n');
if (e == NULL) { if (e == NULL) {
ml_append(lnum++, p, 0, FALSE); ml_append(lnum++, p, 0, FALSE);
@@ -534,11 +587,13 @@ static int pum_set_selected(int n, int repeat)
} }
} }
/* Increase the height of the preview window to show the // Increase the height of the preview window to show the
* text, but no more than 'previewheight' lines. */ // text, but no more than 'previewheight' lines.
if (repeat == 0) { if (repeat == 0) {
if (lnum > p_pvh) if (lnum > p_pvh) {
lnum = p_pvh; lnum = p_pvh;
}
if (curwin->w_height < lnum) { if (curwin->w_height < lnum) {
win_setheight((int)lnum); win_setheight((int)lnum);
resized = TRUE; resized = TRUE;
@@ -550,31 +605,32 @@ static int pum_set_selected(int n, int repeat)
curwin->w_cursor.lnum = 1; curwin->w_cursor.lnum = 1;
curwin->w_cursor.col = 0; curwin->w_cursor.col = 0;
if (curwin != curwin_save && win_valid(curwin_save)) { if ((curwin != curwin_save) && win_valid(curwin_save)) {
/* Return cursor to where we were */ // Return cursor to where we were
validate_cursor(); validate_cursor();
redraw_later(SOME_VALID); redraw_later(SOME_VALID);
/* When the preview window was resized we need to // When the preview window was resized we need to
* update the view on the buffer. Only go back to // update the view on the buffer. Only go back to
* the window when needed, otherwise it will always be // the window when needed, otherwise it will always be
* redraw. */ // redraw.
if (resized) { if (resized) {
win_enter(curwin_save, TRUE); win_enter(curwin_save, TRUE);
update_topline(); update_topline();
} }
/* Update the screen before drawing the popup menu. // Update the screen before drawing the popup menu.
* Enable updating the status lines. */ // Enable updating the status lines.
pum_do_redraw = TRUE; pum_do_redraw = TRUE;
update_screen(0); update_screen(0);
pum_do_redraw = FALSE; pum_do_redraw = FALSE;
if (!resized && win_valid(curwin_save)) if (!resized && win_valid(curwin_save)) {
win_enter(curwin_save, TRUE); win_enter(curwin_save, TRUE);
}
/* May need to update the screen again when there are // May need to update the screen again when there are
* autocommands involved. */ // autocommands involved.
pum_do_redraw = TRUE; pum_do_redraw = TRUE;
update_screen(0); update_screen(0);
pum_do_redraw = FALSE; pum_do_redraw = FALSE;
@@ -584,43 +640,42 @@ static int pum_set_selected(int n, int repeat)
} }
} }
if (!resized) if (!resized) {
pum_redraw(); pum_redraw();
}
return resized; return resized;
} }
/* /// Undisplay the popup menu (later).
* Undisplay the popup menu (later). void pum_undisplay(void)
*/ {
void pum_undisplay(void) {
pum_array = NULL; pum_array = NULL;
redraw_all_later(SOME_VALID); redraw_all_later(SOME_VALID);
redraw_tabline = TRUE; redraw_tabline = TRUE;
status_redraw_all(); status_redraw_all();
} }
/* /// Clear the popup menu. Currently only resets the offset to the first
* Clear the popup menu. Currently only resets the offset to the first /// displayed item.
* displayed item. void pum_clear(void)
*/ {
void pum_clear(void) {
pum_first = 0; pum_first = 0;
} }
/* /// Overruled when "pum_do_redraw" is set, used to redraw the status lines.
* Return TRUE if the popup menu is displayed. ///
* Overruled when "pum_do_redraw" is set, used to redraw the status lines. /// @return TRUE if the popup menu is displayed.
*/ int pum_visible(void)
int pum_visible(void) { {
return !pum_do_redraw && pum_array != NULL; return !pum_do_redraw && pum_array != NULL;
} }
/* /// Gets the height of the menu.
* Return the height of the popup menu, the number of entries visible. ///
* Only valid when pum_visible() returns TRUE! /// @return the height of the popup menu, the number of entries visible.
*/ /// Only valid when pum_visible() returns TRUE!
int pum_get_height(void) { int pum_get_height(void)
{
return pum_height; return pum_height;
} }

View File

@@ -1,14 +1,12 @@
#ifndef NEOVIM_POPUPMNU_H #ifndef NEOVIM_POPUPMNU_H
#define NEOVIM_POPUPMNU_H #define NEOVIM_POPUPMNU_H
/* /// Used for popup menu items.
* Used for popup menu items.
*/
typedef struct { typedef struct {
char_u *pum_text; /* main menu text */ char_u *pum_text; // main menu text
char_u *pum_kind; /* extra kind text (may be truncated) */ char_u *pum_kind; // extra kind text (may be truncated)
char_u *pum_extra; /* extra menu text (may be truncated) */ char_u *pum_extra; // extra menu text (may be truncated)
char_u *pum_info; /* extra info */ char_u *pum_info; // extra info
} pumitem_T; } pumitem_T;
void pum_display(pumitem_T *array, int size, int selected); void pum_display(pumitem_T *array, int size, int selected);
@@ -17,5 +15,6 @@ void pum_undisplay(void);
void pum_clear(void); void pum_clear(void);
int pum_visible(void); int pum_visible(void);
int pum_get_height(void); int pum_get_height(void);
/* vim: set ft=c : */
#endif /* NEOVIM_POPUPMNU_H */ // vim: set ft=c:
#endif // NEOVIM_POPUPMNU_H

View File

@@ -1,45 +1,36 @@
/* vi:set ts=8 sts=4 sw=4: /// @file sha256.c
* ///
* VIM - Vi IMproved by Bram Moolenaar /// FIPS-180-2 compliant SHA-256 implementation
* /// GPL by Christophe Devine, applies to older version.
* Do ":help uganda" in Vim to read copying and usage conditions. /// Modified for md5deep, in public domain.
* Do ":help credits" in Vim to see a list of people who contributed. /// Modified For Vim, Mohsin Ahmed, http://www.cs.albany.edu/~mosh
* See README.txt for an overview of the Vim source code. /// Mohsin Ahmed states this work is distributed under the VIM License or GPL,
* /// at your choice.
* FIPS-180-2 compliant SHA-256 implementation ///
* GPL by Christophe Devine, applies to older version. /// Vim specific notes:
* Modified for md5deep, in public domain. /// Functions exported by this file:
* Modified For Vim, Mohsin Ahmed, http://www.cs.albany.edu/~mosh /// 1. sha256_key() hashes the password to 64 bytes char string.
* Mohsin Ahmed states this work is distributed under the VIM License or GPL, /// 2. sha2_seed() generates a random header.
* at your choice. /// sha256_self_test() is implicitly called once.
*
* Vim specific notes:
* Functions exported by this file:
* 1. sha256_key() hashes the password to 64 bytes char string.
* 2. sha2_seed() generates a random header.
* sha256_self_test() is implicitly called once.
*/
#include "vim.h" #include "vim.h"
#include "sha256.h" #include "sha256.h"
static void sha256_process(context_sha256_T *ctx, char_u data[64]); static void sha256_process(context_sha256_T *ctx, char_u data[64]);
#define GET_UINT32(n, b, i) \ #define GET_UINT32(n, b, i) { \
{ \ (n) = ((uint32_t)(b)[(i)] << 24) \
(n) = ( (uint32_t)(b)[(i) ] << 24) \ | ((uint32_t)(b)[(i) + 1] << 16) \
| ( (uint32_t)(b)[(i) + 1] << 16) \ | ((uint32_t)(b)[(i) + 2] << 8) \
| ( (uint32_t)(b)[(i) + 2] << 8) \ | ((uint32_t)(b)[(i) + 3]); \
| ( (uint32_t)(b)[(i) + 3] ); \ }
}
#define PUT_UINT32(n,b,i) \ #define PUT_UINT32(n, b, i) { \
{ \ (b)[(i)] = (char_u)((n) >> 24); \
(b)[(i) ] = (char_u)((n) >> 24); \
(b)[(i) + 1] = (char_u)((n) >> 16); \ (b)[(i) + 1] = (char_u)((n) >> 16); \
(b)[(i) + 2] = (char_u)((n) >> 8); \ (b)[(i) + 2] = (char_u)((n) >> 8); \
(b)[(i) + 3] = (char_u)((n) ); \ (b)[(i) + 3] = (char_u)((n)); \
} }
void sha256_start(context_sha256_T *ctx) void sha256_start(context_sha256_T *ctx)
{ {
@@ -91,17 +82,14 @@ static void sha256_process(context_sha256_T *ctx, char_u data[64])
#define F1(x, y, z) (z ^ (x & (y ^ z))) #define F1(x, y, z) (z ^ (x & (y ^ z)))
#define R(t) \ #define R(t) \
( \ (W[t] = S1(W[t - 2]) + W[t - 7] + \
W[t] = S1(W[t - 2]) + W[t - 7] + \ S0(W[t - 15]) + W[t - 16])
S0(W[t - 15]) + W[t - 16] \
)
#define P(a,b,c,d,e,f,g,h,x,K) \ #define P(a, b, c, d, e, f, g, h, x, K) { \
{ \
temp1 = h + S3(e) + F1(e, f, g) + K + x; \ temp1 = h + S3(e) + F1(e, f, g) + K + x; \
temp2 = S2(a) + F0(a, b, c); \ temp2 = S2(a) + F0(a, b, c); \
d += temp1; h = temp1 + temp2; \ d += temp1; h = temp1 + temp2; \
} }
A = ctx->state[0]; A = ctx->state[0];
B = ctx->state[1]; B = ctx->state[1];
@@ -112,70 +100,70 @@ static void sha256_process(context_sha256_T *ctx, char_u data[64])
G = ctx->state[6]; G = ctx->state[6];
H = ctx->state[7]; H = ctx->state[7];
P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98); P(A, B, C, D, E, F, G, H, W[0], 0x428A2F98);
P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491); P(H, A, B, C, D, E, F, G, W[1], 0x71374491);
P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF); P(G, H, A, B, C, D, E, F, W[2], 0xB5C0FBCF);
P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5); P(F, G, H, A, B, C, D, E, W[3], 0xE9B5DBA5);
P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B); P(E, F, G, H, A, B, C, D, W[4], 0x3956C25B);
P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1); P(D, E, F, G, H, A, B, C, W[5], 0x59F111F1);
P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4); P(C, D, E, F, G, H, A, B, W[6], 0x923F82A4);
P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5); P(B, C, D, E, F, G, H, A, W[7], 0xAB1C5ED5);
P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98); P(A, B, C, D, E, F, G, H, W[8], 0xD807AA98);
P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01); P(H, A, B, C, D, E, F, G, W[9], 0x12835B01);
P( G, H, A, B, C, D, E, F, W[10], 0x243185BE); P(G, H, A, B, C, D, E, F, W[10], 0x243185BE);
P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3); P(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74); P(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE); P(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7); P(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174); P(B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1); P(A, B, C, D, E, F, G, H, R(16), 0xE49B69C1);
P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786); P(H, A, B, C, D, E, F, G, R(17), 0xEFBE4786);
P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6); P(G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6);
P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC); P(F, G, H, A, B, C, D, E, R(19), 0x240CA1CC);
P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F); P(E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F);
P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA); P(D, E, F, G, H, A, B, C, R(21), 0x4A7484AA);
P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC); P(C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC);
P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA); P(B, C, D, E, F, G, H, A, R(23), 0x76F988DA);
P( A, B, C, D, E, F, G, H, R(24), 0x983E5152); P(A, B, C, D, E, F, G, H, R(24), 0x983E5152);
P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D); P(H, A, B, C, D, E, F, G, R(25), 0xA831C66D);
P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8); P(G, H, A, B, C, D, E, F, R(26), 0xB00327C8);
P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7); P(F, G, H, A, B, C, D, E, R(27), 0xBF597FC7);
P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3); P(E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3);
P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147); P(D, E, F, G, H, A, B, C, R(29), 0xD5A79147);
P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351); P(C, D, E, F, G, H, A, B, R(30), 0x06CA6351);
P( B, C, D, E, F, G, H, A, R(31), 0x14292967); P(B, C, D, E, F, G, H, A, R(31), 0x14292967);
P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85); P(A, B, C, D, E, F, G, H, R(32), 0x27B70A85);
P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138); P(H, A, B, C, D, E, F, G, R(33), 0x2E1B2138);
P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC); P(G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC);
P( F, G, H, A, B, C, D, E, R(35), 0x53380D13); P(F, G, H, A, B, C, D, E, R(35), 0x53380D13);
P( E, F, G, H, A, B, C, D, R(36), 0x650A7354); P(E, F, G, H, A, B, C, D, R(36), 0x650A7354);
P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB); P(D, E, F, G, H, A, B, C, R(37), 0x766A0ABB);
P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E); P(C, D, E, F, G, H, A, B, R(38), 0x81C2C92E);
P( B, C, D, E, F, G, H, A, R(39), 0x92722C85); P(B, C, D, E, F, G, H, A, R(39), 0x92722C85);
P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1); P(A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1);
P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B); P(H, A, B, C, D, E, F, G, R(41), 0xA81A664B);
P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70); P(G, H, A, B, C, D, E, F, R(42), 0xC24B8B70);
P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3); P(F, G, H, A, B, C, D, E, R(43), 0xC76C51A3);
P( E, F, G, H, A, B, C, D, R(44), 0xD192E819); P(E, F, G, H, A, B, C, D, R(44), 0xD192E819);
P( D, E, F, G, H, A, B, C, R(45), 0xD6990624); P(D, E, F, G, H, A, B, C, R(45), 0xD6990624);
P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585); P(C, D, E, F, G, H, A, B, R(46), 0xF40E3585);
P( B, C, D, E, F, G, H, A, R(47), 0x106AA070); P(B, C, D, E, F, G, H, A, R(47), 0x106AA070);
P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116); P(A, B, C, D, E, F, G, H, R(48), 0x19A4C116);
P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08); P(H, A, B, C, D, E, F, G, R(49), 0x1E376C08);
P( G, H, A, B, C, D, E, F, R(50), 0x2748774C); P(G, H, A, B, C, D, E, F, R(50), 0x2748774C);
P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5); P(F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5);
P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3); P(E, F, G, H, A, B, C, D, R(52), 0x391C0CB3);
P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A); P(D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A);
P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F); P(C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F);
P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3); P(B, C, D, E, F, G, H, A, R(55), 0x682E6FF3);
P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE); P(A, B, C, D, E, F, G, H, R(56), 0x748F82EE);
P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F); P(H, A, B, C, D, E, F, G, R(57), 0x78A5636F);
P( G, H, A, B, C, D, E, F, R(58), 0x84C87814); P(G, H, A, B, C, D, E, F, R(58), 0x84C87814);
P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208); P(F, G, H, A, B, C, D, E, R(59), 0x8CC70208);
P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA); P(E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA);
P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB); P(D, E, F, G, H, A, B, C, R(61), 0xA4506CEB);
P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7); P(C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7);
P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2); P(B, C, D, E, F, G, H, A, R(63), 0xC67178F2);
ctx->state[0] += A; ctx->state[0] += A;
ctx->state[1] += B; ctx->state[1] += B;
@@ -191,8 +179,9 @@ void sha256_update(context_sha256_T *ctx, char_u *input, uint32_t length)
{ {
uint32_t left, fill; uint32_t left, fill;
if (length == 0) if (length == 0) {
return; return;
}
left = ctx->total[0] & 0x3F; left = ctx->total[0] & 0x3F;
fill = 64 - left; fill = 64 - left;
@@ -200,10 +189,11 @@ void sha256_update(context_sha256_T *ctx, char_u *input, uint32_t length)
ctx->total[0] += length; ctx->total[0] += length;
ctx->total[0] &= 0xFFFFFFFF; ctx->total[0] &= 0xFFFFFFFF;
if (ctx->total[0] < length) if (ctx->total[0] < length) {
ctx->total[1]++; ctx->total[1]++;
}
if (left && length >= fill) { if (left && (length >= fill)) {
memcpy((void *)(ctx->buffer + left), (void *)input, fill); memcpy((void *)(ctx->buffer + left), (void *)input, fill);
sha256_process(ctx, ctx->buffer); sha256_process(ctx, ctx->buffer);
length -= fill; length -= fill;
@@ -217,8 +207,9 @@ void sha256_update(context_sha256_T *ctx, char_u *input, uint32_t length)
input += 64; input += 64;
} }
if (length) if (length) {
memcpy((void *)(ctx->buffer + left), (void *)input, length); memcpy((void *)(ctx->buffer + left), (void *)input, length);
}
} }
static char_u sha256_padding[64] = { static char_u sha256_padding[64] = {
@@ -258,10 +249,15 @@ void sha256_finish(context_sha256_T *ctx, char_u digest[32])
static unsigned int get_some_time(void); static unsigned int get_some_time(void);
/* /// Gets the hex digest of the buffer.
* Returns hex digest of "buf[buf_len]" in a static array. ///
* if "salt" is not NULL also do "salt[salt_len]". /// @param buf
*/ /// @param buf_len
/// @param salt
/// @param salt_len
///
/// @returns hex digest of "buf[buf_len]" in a static array.
/// if "salt" is not NULL also do "salt[salt_len]".
char_u *sha256_bytes(char_u *buf, int buf_len, char_u *salt, int salt_len) char_u *sha256_bytes(char_u *buf, int buf_len, char_u *salt, int salt_len)
{ {
char_u sha256sum[32]; char_u sha256sum[32];
@@ -273,31 +269,37 @@ char_u *sha256_bytes(char_u *buf, int buf_len, char_u *salt, int salt_len)
sha256_start(&ctx); sha256_start(&ctx);
sha256_update(&ctx, buf, buf_len); sha256_update(&ctx, buf, buf_len);
if (salt != NULL)
if (salt != NULL) {
sha256_update(&ctx, salt, salt_len); sha256_update(&ctx, salt, salt_len);
}
sha256_finish(&ctx, sha256sum); sha256_finish(&ctx, sha256sum);
for (j = 0; j < 32; j++)
sprintf((char *)hexit + j * 2, "%02x", sha256sum[j]); for (j = 0; j < 32; j++) {
sprintf((char *) hexit + j * 2, "%02x", sha256sum[j]);
}
hexit[sizeof(hexit) - 1] = '\0'; hexit[sizeof(hexit) - 1] = '\0';
return hexit; return hexit;
} }
/* /// Gets sha256(buf) as 64 hex characters in a static array.
* Returns sha256(buf) as 64 hex chars in static array. ///
*/ /// @param buf
char_u *sha256_key(char_u *buf, char_u *salt, int salt_len) /// @param salt
/// @param salt_len
///
/// @returns sha256(buf) as 64 hex chars in static array.
char_u* sha256_key(char_u *buf, char_u *salt, int salt_len)
{ {
/* No passwd means don't encrypt */ // No passwd means don't encrypt
if (buf == NULL || *buf == NUL) if ((buf == NULL) || (*buf == NUL)) {
return (char_u *)""; return (char_u *)"";
}
return sha256_bytes(buf, (int)STRLEN(buf), salt, salt_len); return sha256_bytes(buf, (int)STRLEN(buf), salt, salt_len);
} }
/* // These are the standard FIPS-180-2 test vectors
* These are the standard FIPS-180-2 test vectors
*/
static char *sha_self_test_msg[] = { static char *sha_self_test_msg[] = {
"abc", "abc",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
@@ -313,11 +315,11 @@ static char *sha_self_test_vector[] = {
"f1809a48a497200e046d39ccc7112cd0" "f1809a48a497200e046d39ccc7112cd0"
}; };
/* /// Perform a test on the SHA256 algorithm.
* Perform a test on the SHA256 algorithm. ///
* Return FAIL or OK. /// @return FAIL or OK.
*/ int sha256_self_test(void)
int sha256_self_test(void) { {
int i, j; int i, j;
char output[65]; char output[65];
context_sha256_T ctx; context_sha256_T ctx;
@@ -327,72 +329,88 @@ int sha256_self_test(void) {
char_u *hexit; char_u *hexit;
static int sha256_self_tested = 0; static int sha256_self_tested = 0;
if (sha256_self_tested > 0) if (sha256_self_tested > 0) {
return failures > 0 ? FAIL : OK; return failures > 0 ? FAIL : OK;
}
sha256_self_tested = 1; sha256_self_tested = 1;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
if (i < 2) { if (i < 2) {
hexit = sha256_bytes((char_u *)sha_self_test_msg[i], hexit = sha256_bytes((char_u *) sha_self_test_msg[i],
(int)STRLEN(sha_self_test_msg[i]), (int) STRLEN(sha_self_test_msg[i]),
NULL, 0); NULL, 0);
STRCPY(output, hexit); STRCPY(output, hexit);
} else { } else {
sha256_start(&ctx); sha256_start(&ctx);
vim_memset(buf, 'a', 1000); vim_memset(buf, 'a', 1000);
for (j = 0; j < 1000; j++)
sha256_update(&ctx, (char_u *)buf, 1000); for (j = 0; j < 1000; j++) {
sha256_update(&ctx, (char_u *) buf, 1000);
}
sha256_finish(&ctx, sha256sum); sha256_finish(&ctx, sha256sum);
for (j = 0; j < 32; j++)
for (j = 0; j < 32; j++) {
sprintf(output + j * 2, "%02x", sha256sum[j]); sprintf(output + j * 2, "%02x", sha256sum[j]);
} }
}
if (memcmp(output, sha_self_test_vector[i], 64)) { if (memcmp(output, sha_self_test_vector[i], 64)) {
failures++; failures++;
output[sizeof(output) - 1] = '\0'; output[sizeof(output) - 1] = '\0';
/* printf("sha256_self_test %d failed %s\n", i, output); */
// printf("sha256_self_test %d failed %s\n", i, output);
} }
} }
return failures > 0 ? FAIL : OK; return failures > 0 ? FAIL : OK;
} }
static unsigned int get_some_time(void) { static unsigned int get_some_time(void)
# ifdef HAVE_GETTIMEOFDAY {
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv; struct timeval tv;
/* Using usec makes it less predictable. */ // Using usec makes it less predictable.
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
return (unsigned int)(tv.tv_sec + tv.tv_usec); return (unsigned int) (tv.tv_sec + tv.tv_usec);
# else
return (unsigned int)time(NULL); #else // ifdef HAVE_GETTIMEOFDAY
# endif return (unsigned int) time(NULL);
#endif // ifdef HAVE_GETTIMEOFDAY
} }
/* /// Fill "header[header_len]" with random_data.
* Fill "header[header_len]" with random_data. /// Also "salt[salt_len]" when "salt" is not NULL.
* Also "salt[salt_len]" when "salt" is not NULL. ///
*/ /// @param header
/// @param header_len
/// @param salt
/// @param salt_len
void sha2_seed(char_u *header, int header_len, char_u *salt, int salt_len) void sha2_seed(char_u *header, int header_len, char_u *salt, int salt_len)
{ {
int i;
static char_u random_data[1000]; static char_u random_data[1000];
char_u sha256sum[32]; char_u sha256sum[32];
context_sha256_T ctx; context_sha256_T ctx;
srand(get_some_time()); srand(get_some_time());
for (i = 0; i < (int)sizeof(random_data) - 1; i++) int i;
random_data[i] = (char_u)((get_some_time() ^ rand()) & 0xff); for (i = 0; i < (int) sizeof(random_data) - 1; i++) {
random_data[i] = (char_u) ((get_some_time() ^ rand()) & 0xff);
}
sha256_start(&ctx); sha256_start(&ctx);
sha256_update(&ctx, (char_u *)random_data, sizeof(random_data)); sha256_update(&ctx, (char_u *) random_data, sizeof(random_data));
sha256_finish(&ctx, sha256sum); sha256_finish(&ctx, sha256sum);
/* put first block into header. */ // put first block into header.
for (i = 0; i < header_len; i++) for (i = 0; i < header_len; i++) {
header[i] = sha256sum[i % sizeof(sha256sum)]; header[i] = sha256sum[i % sizeof(sha256sum)];
}
/* put remaining block into salt. */ // put remaining block into salt.
if (salt != NULL) if (salt != NULL) {
for (i = 0; i < salt_len; i++) for (i = 0; i < salt_len; i++) {
salt[i] = sha256sum[(i + header_len) % sizeof(sha256sum)]; salt[i] = sha256sum[(i + header_len) % sizeof(sha256sum)];
}
}
} }

View File

@@ -8,14 +8,12 @@ typedef struct {
} context_sha256_T; } context_sha256_T;
void sha256_start(context_sha256_T *ctx); void sha256_start(context_sha256_T *ctx);
void sha256_update(context_sha256_T *ctx, char_u *input, void sha256_update(context_sha256_T *ctx, char_u *input, uint32_t length);
uint32_t length);
void sha256_finish(context_sha256_T *ctx, char_u digest[32]); void sha256_finish(context_sha256_T *ctx, char_u digest[32]);
char_u *sha256_bytes(char_u *buf, int buf_len, char_u *salt, char_u *sha256_bytes(char_u *buf, int buf_len, char_u *salt, int salt_len);
int salt_len);
char_u *sha256_key(char_u *buf, char_u *salt, int salt_len); char_u *sha256_key(char_u *buf, char_u *salt, int salt_len);
int sha256_self_test(void); int sha256_self_test(void);
void sha2_seed(char_u *header, int header_len, char_u *salt, void sha2_seed(char_u *header, int header_len, char_u *salt, int salt_len);
int salt_len);
/* vim: set ft=c : */ // vim: set ft=c:
#endif /* NEOVIM_SHA256_H */ #endif // NEOVIM_SHA256_H

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
#ifndef NEOVIM_VERSION_H #ifndef NEOVIM_VERSION_H
#define NEOVIM_VERSION_H #define NEOVIM_VERSION_H
/* version.c */
void make_version(void); void make_version(void);
int highest_patch(void); int highest_patch(void);
int has_patch(int n); int has_patch(int n);
@@ -9,5 +9,6 @@ void list_version(void);
void maybe_intro_message(void); void maybe_intro_message(void);
void intro_message(int colon); void intro_message(int colon);
void ex_intro(exarg_T *eap); void ex_intro(exarg_T *eap);
/* vim: set ft=c : */
#endif /* NEOVIM_VERSION_H */ // vim: set ft=c:
#endif // NEOVIM_VERSION_H

View File

@@ -1222,7 +1222,7 @@ pos_arith = ignore # ignore/join/lead/lead_brea
pos_assign = ignore # ignore/join/lead/lead_break/lead_force/trail/trail_break/trail_force pos_assign = ignore # ignore/join/lead/lead_break/lead_force/trail/trail_break/trail_force
# The position of boolean operators in wrapped expressions # The position of boolean operators in wrapped expressions
pos_bool = trail_break # ignore/join/lead/lead_break/lead_force/trail/trail_break/trail_force pos_bool = lead_break # ignore/join/lead/lead_break/lead_force/trail/trail_break/trail_force
# The position of comparison operators in wrapped expressions # The position of comparison operators in wrapped expressions
pos_compare = ignore # ignore/join/lead/lead_break/lead_force/trail/trail_break/trail_force pos_compare = ignore # ignore/join/lead/lead_break/lead_force/trail/trail_break/trail_force