* Remove references to global `qe_state` Add `QEmacsState *qs` field in context and object structures: - `QEditScreen`, `EditState` (renamed from `qe_state`), `QuitState` - `WinWindow` (win32.c), `X11State` (x11.c), `QEArgs` (cmd line arguments) - `CmdLineOptionDef::func_arg()`, `QEModeData` - move `QEKeyContext` to `QEmacsState` Pass `QEmacsState *qs` to configuration and state functions: - all `module_xxx_init` and static `xxx_init` functions - `QEDisplay::dpy_init()`, `qe_register_display()`, `screen_init()̀` - `load_input_methods()`, `unload_input_methods()` - `register_input_method()`, `find_input_method()` - `find_resource_file()`, `open_resource_file()` - `qe_find_current_binding()`, `qe_list_bindings()` - `qe_register_mode()`, `qe_find_mode()`, `qe_find_mode_filename()` - `qe_register_cmd_line_options()`, `set_user_option()`, `set_tty_charset()` - `qe_register_commands()`, `qe_register_bindings()`, `qe_find_cmd()` - `qe_register_completion()`, `qe_register_variables()` - `eb_register_data_type()`, `parse_command_line()` - `eb_trace_bytes()`, `qe_toggle_control_h()` (split from `do_toggle_control_h()`) - `unget_key()`, `qe_grab_keys()`, `qe_ungrab_keys()`, `qe_key_process()` - `eb_new()`, `eb_find()`, `eb_find_new()`, `eb_scratch()`, `eb_find_file()` - `new_shell_buffer()` `macro_add_key()`, `clear_macro(), `stop_macro()` - `check_window()`, `check_buffer()`, `qe_kill_buffer()`, `qe_get_history()` - `qe_register_charset()`, `find_charset()` Add error message handlers: - add `QEDisplay::dpy_error()` method - add `qe_cfg_error()`, use for all expression parsing errors - pass opaque pointer to `css_error()` - add `qe_put_error()` and `qe_dpy_error()` Add context argument: - always pass a non-null window pointer to `do_refresh()`, `new_help_buffer()` Rename functions: - `find_resource_file` -> `qe_find_resource_file` - `open_resource_file` -> `qe_open_resource_file` - `input_methods_init` -> `qe_input_methods_init` - `load_input_methods` -> `qe_load_input_methods` - `unload_input_methods` -> `qe_unload_input_methods` - `find_input_methods` -> `qe_find_input_methods` - `register_input_method` -> `qe_register_input_method` - `parse_command_line` -> `qe_parse_command_line` - `eb_trace_bytes` -> `qe_trace_bytes` - `eb_init` -> `qe_data_init` - `eb_count_buffers` -> `qe_count_buffers` - `eb_get_buffer_from_index` -> `qe_get_buffer_from_index` - `init_all_modules` -> `qe_init_all_modules` - `exit_all_modules` -> `qe_exit_all_modules` - `load_all_modules` -> `qe_load_all_modules` - `maybe_set_mark` -> `do_maybe_set_mark` - `unget_key` -> `qe_unget_key` - `minibuffer_init` -> `qe_minibuffer_init` - `edit_display` -> `qe_display` - `new_yank_buffer` -> `qe_new_yank_buffer` - `new_shell_buffer` -> `qe_new_shell_buffer` - `shell_find_mode` -> `qe_shell_find_mode` - `save_selection` -> `qe_save_selection` - `register_emulation_bindings` -> `qe_register_emulation_bindings` - `macro_add_key` -> `qe_macro_add_key` - `clear_macro` -> `qe_clear_macro` - `stop_macro` -> `qe_stop_macro` - `eb_format_message` -> `qe_format_message` - `screen_init` -> `qe_screen_init` - `charset_jis_init` -> `qe_charset_jis_init` - `charset_more_init` -> `qe_charset_more_init` - `check_window` -> `qe_check_window` - `check_buffer` -> `qe_check_buffer` - `eb_find_file` -> `qe_find_buffer_file` - `eb_find` -> `qe_find_buffer_name` - `eb_new` -> `qe_new_buffer` - `eb_find_new` -> `qe_new_buffer` with `BC_REUSE` - `eb_scratch` -> `qe_new_buffer` with `BC_REUSE | BC_CLEAR` - `find_charset` -> `qe_find_charset` Add asan, msan and ubsan targets Fix `qe_encode64` buffer length computation. Add `qe_new_shell_buffer` flags `SF_REUSE_BUFFER` and `SF_ERASE_BUFFER` Bumped version to 6.3.3 fix redundant typedef fix compilation errors
409 lines
13 KiB
C
409 lines
13 KiB
C
/*
|
|
* Perl Source mode for QEmacs.
|
|
*
|
|
* Copyright (c) 2000-2024 Charlie Gordon.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "qe.h"
|
|
#include "clang.h"
|
|
|
|
/*---------------- Perl colors ----------------*/
|
|
|
|
static char const perl_keywords[] = {
|
|
/* Perl keywords */
|
|
"ge|gt|le|lt|cmp|eq|ne|int|x|or|and|not|xor|" // special case x=
|
|
"do|else|elsif|if|for|until|while|foreach|unless|last|"
|
|
"require|package|use|strict|BEGIN|bless|isa|"
|
|
"sub|return|eval|try|catch|with|throw|except|otherwise|finally|"
|
|
"undef|true|false|"
|
|
"exit|die|warn|system|"
|
|
"print|printf|open|close|readline|read|binmode|seek|tell|flock|"
|
|
"opendir|closedir|readdir|unlink|rename|chdir|truncate|"
|
|
"chmod|kill|killall|"
|
|
"chomp|pos|length|substr|lc|uc|lcfirst|ucfirst|split|hex|"
|
|
"sprintf|index|"
|
|
"reverse|pop|push|shift|unshift|splice|join|map|sort|"
|
|
"delete|insert|keys|values|exists|defined|"
|
|
"scalar|wantarray|ref|"
|
|
"STDIN|STDOUT|STDERR|"
|
|
};
|
|
|
|
static char const perl_types[] = {
|
|
"my|local|"
|
|
};
|
|
|
|
// qq~ multiline string ~
|
|
|
|
enum {
|
|
PERL_STYLE_TEXT = QE_STYLE_DEFAULT,
|
|
PERL_STYLE_SHBANG = QE_STYLE_PREPROCESS,
|
|
PERL_STYLE_COMMENT = QE_STYLE_COMMENT,
|
|
PERL_STYLE_STRING = QE_STYLE_STRING,
|
|
PERL_STYLE_REGEX = QE_STYLE_STRING,
|
|
PERL_STYLE_DELIM = QE_STYLE_KEYWORD,
|
|
PERL_STYLE_KEYWORD = QE_STYLE_KEYWORD,
|
|
PERL_STYLE_FUNCTION = QE_STYLE_FUNCTION,
|
|
PERL_STYLE_VAR = QE_STYLE_VARIABLE,
|
|
PERL_STYLE_NUMBER = QE_STYLE_NUMBER,
|
|
};
|
|
|
|
enum {
|
|
IN_PERL_STRING1 = 0x01, /* single quote */
|
|
IN_PERL_STRING2 = 0x02, /* double quote */
|
|
IN_PERL_FORMAT = 0x04, /* format = ... */
|
|
IN_PERL_HEREDOC = 0x08,
|
|
IN_PERL_POD = 0x10,
|
|
};
|
|
|
|
/* CG: bogus if multiple regions are colorized, should use signature */
|
|
/* XXX: should move this to mode data */
|
|
static char32_t perl_eos[100];
|
|
static int perl_eos_len;
|
|
|
|
static int perl_var(const char32_t *str, int j, int n)
|
|
{
|
|
if (qe_isdigit_(str[j]))
|
|
return j;
|
|
for (; j < n; j++) {
|
|
if (qe_isalnum_(str[j]))
|
|
continue;
|
|
if (str[j] == '\'' && qe_isalpha_(str[j + 1]))
|
|
j++;
|
|
else
|
|
break;
|
|
}
|
|
return j;
|
|
}
|
|
|
|
static int perl_number(const char32_t *str, int j, qe__unused__ int n)
|
|
{
|
|
if (str[j] == '0') {
|
|
j++;
|
|
if (str[j] == 'x' || str[j] == 'X') {
|
|
/* hexadecimal numbers */
|
|
// XXX: should verify if 'X' is really accepted
|
|
// XXX: should accept embedded '_'
|
|
do { j++; } while (qe_isxdigit(str[j]));
|
|
return j;
|
|
}
|
|
if (str[j] >= '0' && str[j] <= '7') {
|
|
/* octal numbers */
|
|
// XXX: should accept embedded '_'
|
|
do { j++; } while (str[j] >= '0' && str[j] <= '7');
|
|
return j;
|
|
}
|
|
}
|
|
// XXX: should accept embedded '_'
|
|
while (qe_isdigit(str[j]))
|
|
j++;
|
|
|
|
/* integral part is optional */
|
|
if (str[j] == '.') {
|
|
// XXX: should accept embedded '_'
|
|
do { j++; } while (qe_isdigit(str[j]));
|
|
}
|
|
|
|
// XXX: should verify if 'E' is really accepted
|
|
if (str[j] == 'E' || str[j] == 'e') {
|
|
j++;
|
|
if (str[j] == '-' || str[j] == '+')
|
|
j++;
|
|
while (qe_isdigit(str[j]))
|
|
j++;
|
|
}
|
|
return j;
|
|
}
|
|
|
|
/* return offset of matching delimiter or end of string */
|
|
static int perl_string(const char32_t *str, char32_t delim, int j, int n) {
|
|
for (; j < n; j++) {
|
|
if (str[j] == '\\')
|
|
j++;
|
|
else
|
|
if (str[j] == delim)
|
|
return j;
|
|
}
|
|
return j;
|
|
}
|
|
|
|
static void perl_colorize_line(QEColorizeContext *cp,
|
|
const char32_t *str, int n,
|
|
QETermStyle *sbuf, ModeDef *syn)
|
|
{
|
|
int i = 0, start = i, j, s1, s2, delim = 0, indent, style, klen;
|
|
char32_t c, c1, c2;
|
|
int colstate = cp->colorize_state;
|
|
char kbuf[64];
|
|
|
|
indent = cp_skip_blanks(str, 0, n);
|
|
|
|
if (colstate & (IN_PERL_STRING1 | IN_PERL_STRING2)) {
|
|
delim = (colstate & IN_PERL_STRING1) ? '\'' : '\"';
|
|
i = perl_string(str, delim, start, n);
|
|
if (i < n) {
|
|
i++;
|
|
colstate &= ~(IN_PERL_STRING1 | IN_PERL_STRING2);
|
|
}
|
|
SET_STYLE(sbuf, start, i, PERL_STYLE_STRING);
|
|
} else
|
|
if (colstate & IN_PERL_FORMAT) {
|
|
i = n;
|
|
if (n == 1 && str[0] == '.')
|
|
colstate &= ~IN_PERL_FORMAT;
|
|
SET_STYLE(sbuf, start, i, PERL_STYLE_STRING);
|
|
}
|
|
if (colstate & IN_PERL_HEREDOC) {
|
|
i = n;
|
|
if (n == perl_eos_len && !umemcmp(perl_eos, str, n)) {
|
|
colstate &= ~IN_PERL_HEREDOC;
|
|
SET_STYLE(sbuf, start, i, PERL_STYLE_KEYWORD);
|
|
} else {
|
|
SET_STYLE(sbuf, start, i, PERL_STYLE_STRING);
|
|
}
|
|
}
|
|
if (str[i] == '=' && qe_isalpha(str[i + 1])) {
|
|
colstate |= IN_PERL_POD;
|
|
}
|
|
if (colstate & IN_PERL_POD) {
|
|
if (ustrstart(str + i, "=cut", NULL)) {
|
|
colstate &= ~IN_PERL_POD;
|
|
}
|
|
if (str[i] == '=' && qe_isalpha(str[i + 1])) {
|
|
i = n;
|
|
SET_STYLE(sbuf, start, i, PERL_STYLE_KEYWORD);
|
|
} else {
|
|
i = n;
|
|
SET_STYLE(sbuf, start, i, PERL_STYLE_COMMENT);
|
|
}
|
|
}
|
|
|
|
style = 0;
|
|
while (i < n) {
|
|
start = i;
|
|
c = str[i++];
|
|
c1 = str[i];
|
|
switch (c) {
|
|
case '$':
|
|
if (c1 == '^' && qe_isalpha(str[i + 1])) {
|
|
i += 2;
|
|
} else
|
|
if (c1 == '#' && qe_isalpha_(str[i + 1])) {
|
|
i += 2;
|
|
} else
|
|
if (qe_findchar("|%=-~^123456789&`'+_./\\,\"#$?*0[];!@", c1)) {
|
|
/* Special variable */
|
|
i += 1;
|
|
style = PERL_STYLE_KEYWORD;
|
|
break;
|
|
}
|
|
fallthrough;
|
|
case '*':
|
|
case '@': /* arrays */
|
|
case '%': /* associative arrays */
|
|
case '&':
|
|
if (i < n) {
|
|
s1 = perl_var(str, i, n);
|
|
if (s1 > i) {
|
|
i = s1;
|
|
style = PERL_STYLE_VAR;
|
|
break;
|
|
}
|
|
}
|
|
continue;
|
|
case '-':
|
|
if (c1 == '-') {
|
|
i += 1;
|
|
continue;
|
|
}
|
|
if (qe_isalpha(c1) && !qe_isalnum(str[i + 1])) {
|
|
i += 2;
|
|
style = PERL_STYLE_KEYWORD;
|
|
break;
|
|
}
|
|
continue;
|
|
case '#':
|
|
style = PERL_STYLE_COMMENT;
|
|
if (start == 0 && c1 == '!')
|
|
style = PERL_STYLE_SHBANG;
|
|
i = n;
|
|
break;
|
|
case '<':
|
|
if (c1 == '<') {
|
|
/* Should check for unary context */
|
|
s1 = cp_skip_blanks(str, i + 1, n);
|
|
c2 = str[s1];
|
|
if (c2 == '"' || c2 == '\'' || c2 == '`') {
|
|
s2 = perl_string(str, c2, ++s1, n);
|
|
} else {
|
|
s2 = perl_var(str, s1, n);
|
|
}
|
|
if (s2 > s1) {
|
|
perl_eos_len = min_int((int)(s2 - s1), countof(perl_eos) - 1);
|
|
umemcpy(perl_eos, str + s1, perl_eos_len);
|
|
perl_eos[perl_eos_len] = '\0';
|
|
colstate |= IN_PERL_HEREDOC;
|
|
}
|
|
i += 1;
|
|
continue;
|
|
}
|
|
delim = '>';
|
|
goto string;
|
|
case '/':
|
|
case '?':
|
|
/* Should check for unary context */
|
|
/* parse regex */
|
|
s1 = perl_string(str, c, i, n);
|
|
if (s1 >= n)
|
|
break;
|
|
//SET_STYLE1(sbuf, start, PERL_STYLE_DELIM);
|
|
i = s1;
|
|
//SET_STYLE(sbuf, start + 1, i, PERL_STYLE_REGEX);
|
|
start = i;
|
|
while (++i < n && qe_isalpha(str[i]))
|
|
continue;
|
|
//SET_STYLE(sbuf, start, i, PERL_STYLE_DELIM);
|
|
//continue;
|
|
style = PERL_STYLE_REGEX;
|
|
break;
|
|
case '\'':
|
|
case '`':
|
|
case '"':
|
|
delim = c;
|
|
string:
|
|
/* parse string const */
|
|
s1 = perl_string(str, delim, i, n);
|
|
if (s1 >= n) {
|
|
if (c == '\'') {
|
|
colstate |= IN_PERL_STRING1;
|
|
i = n;
|
|
style = PERL_STYLE_STRING;
|
|
break;
|
|
}
|
|
if (c == '\"') {
|
|
colstate |= IN_PERL_STRING2;
|
|
i = n;
|
|
style = PERL_STYLE_STRING;
|
|
break;
|
|
}
|
|
/* ` string spanning more than one line treated as
|
|
* operator.
|
|
*/
|
|
break;
|
|
}
|
|
s1++;
|
|
i = s1;
|
|
style = PERL_STYLE_STRING;
|
|
break;
|
|
case '.':
|
|
if (qe_isdigit(c1))
|
|
goto number;
|
|
continue;
|
|
|
|
default:
|
|
if (qe_isdigit(c)) {
|
|
number:
|
|
i = perl_number(str, start, n);
|
|
style = PERL_STYLE_NUMBER;
|
|
break;
|
|
}
|
|
if (!qe_isalpha_(c))
|
|
continue;
|
|
|
|
//j = perl_var(str, start, n);
|
|
klen = ustr_get_identifier(kbuf, countof(kbuf), c, str, i, n);
|
|
j = i += klen;
|
|
|
|
if (i >= n)
|
|
goto keyword;
|
|
|
|
/* Should check for context */
|
|
if ((klen == 1 && (c == 'm' || c == 'q'))
|
|
|| (klen == 2 && c == 'q' && (c1 == 'q' || c1 == 'x'))) {
|
|
s1 = perl_string(str, str[j], j + 1, n);
|
|
if (s1 >= n)
|
|
goto keyword;
|
|
SET_STYLE(sbuf, start, j + 1, PERL_STYLE_DELIM);
|
|
i = s1;
|
|
SET_STYLE(sbuf, j + 1, i, PERL_STYLE_REGEX);
|
|
while (++i < n && qe_isalpha(str[i]))
|
|
continue;
|
|
SET_STYLE(sbuf, s1, i, PERL_STYLE_DELIM);
|
|
continue;
|
|
}
|
|
/* Should check for context */
|
|
if ((klen == 1 && (c == 's' /* || c == 'y' */))
|
|
|| (klen == 2 && c == 't' && c1 == 'r')) {
|
|
s1 = perl_string(str, str[j], j + 1, n);
|
|
if (s1 >= n)
|
|
goto keyword;
|
|
s2 = perl_string(str, str[j], s1 + 1, n);
|
|
if (s2 >= n)
|
|
goto keyword;
|
|
SET_STYLE(sbuf, start, j + 1, PERL_STYLE_DELIM);
|
|
SET_STYLE(sbuf, j + 1, s1, PERL_STYLE_REGEX);
|
|
SET_STYLE1(sbuf, s1, PERL_STYLE_DELIM);
|
|
SET_STYLE(sbuf, s1 + 1, s2, PERL_STYLE_REGEX);
|
|
i = s2;
|
|
while (++i < n && qe_isalpha(str[i]))
|
|
continue;
|
|
SET_STYLE(sbuf, s2, i, PERL_STYLE_DELIM);
|
|
continue;
|
|
}
|
|
keyword:
|
|
if (klen == 6 && !strcasecmp(kbuf, "format")) {
|
|
if (start == indent) {
|
|
/* keyword is first on line */
|
|
colstate |= IN_PERL_FORMAT;
|
|
}
|
|
}
|
|
if (strfind(syn->keywords, kbuf)) {
|
|
style = PERL_STYLE_KEYWORD;
|
|
} else {
|
|
style = PERL_STYLE_FUNCTION;
|
|
}
|
|
break;
|
|
}
|
|
if (style) {
|
|
SET_STYLE(sbuf, start, i, style);
|
|
style = 0;
|
|
}
|
|
}
|
|
cp->colorize_state = colstate;
|
|
}
|
|
|
|
static ModeDef perl_mode = {
|
|
.name = "Perl",
|
|
.extensions = "pl|perl|pm",
|
|
.shell_handlers = "perl|perl5",
|
|
.colorize_func = perl_colorize_line,
|
|
.keywords = perl_keywords,
|
|
.types = perl_types,
|
|
.indent_func = c_indent_line,
|
|
.auto_indent = 1,
|
|
};
|
|
|
|
static int perl_init(QEmacsState *qs)
|
|
{
|
|
qe_register_mode(qs, &perl_mode, MODEF_SYNTAX);
|
|
return 0;
|
|
}
|
|
|
|
qe_module_init(perl_init);
|