Embed mruby and better clipboard support
This commit is contained in:
1
.clangd
1
.clangd
@@ -2,6 +2,7 @@ CompileFlags:
|
|||||||
Add: [
|
Add: [
|
||||||
-I/home/syed/main/crib/include,
|
-I/home/syed/main/crib/include,
|
||||||
-I/home/syed/main/crib/libs,
|
-I/home/syed/main/crib/libs,
|
||||||
|
-I/home/syed/main/crib/libs/mruby/include,
|
||||||
-std=c++23
|
-std=c++23
|
||||||
]
|
]
|
||||||
Remove: []
|
Remove: []
|
||||||
|
|||||||
4
.gitmodules
vendored
4
.gitmodules
vendored
@@ -2,3 +2,7 @@
|
|||||||
path = libs/libgrapheme
|
path = libs/libgrapheme
|
||||||
url = git://git.suckless.org/libgrapheme
|
url = git://git.suckless.org/libgrapheme
|
||||||
ignore = dirty
|
ignore = dirty
|
||||||
|
[submodule "libs/mruby"]
|
||||||
|
path = libs/mruby
|
||||||
|
url = https://github.com/mruby/mruby.git
|
||||||
|
ignore = dirty
|
||||||
|
|||||||
12
Makefile
12
Makefile
@@ -21,7 +21,7 @@ CFLAGS_DEBUG :=\
|
|||||||
-g -fno-omit-frame-pointer \
|
-g -fno-omit-frame-pointer \
|
||||||
-Wno-unused-command-line-argument \
|
-Wno-unused-command-line-argument \
|
||||||
-fsanitize=address \
|
-fsanitize=address \
|
||||||
-I./include -I./libs
|
-I./include -I./libs -I/home/syed/main/crib/libs/mruby/include
|
||||||
|
|
||||||
CFLAGS_RELEASE :=\
|
CFLAGS_RELEASE :=\
|
||||||
-static --target=x86_64-linux-musl \
|
-static --target=x86_64-linux-musl \
|
||||||
@@ -32,7 +32,7 @@ CFLAGS_RELEASE :=\
|
|||||||
-fomit-frame-pointer -DNDEBUG -s \
|
-fomit-frame-pointer -DNDEBUG -s \
|
||||||
-mllvm -vectorize-loops \
|
-mllvm -vectorize-loops \
|
||||||
-Wno-unused-command-line-argument \
|
-Wno-unused-command-line-argument \
|
||||||
-I./include -I./libs
|
-I./include -I./libs -I/home/syed/main/crib/libs/mruby/include
|
||||||
|
|
||||||
PCH_CFLAGS_DEBUG := $(CFLAGS_DEBUG) -x c++-header
|
PCH_CFLAGS_DEBUG := $(CFLAGS_DEBUG) -x c++-header
|
||||||
PCH_CFLAGS_RELEASE := $(CFLAGS_RELEASE) -x c++-header
|
PCH_CFLAGS_RELEASE := $(CFLAGS_RELEASE) -x c++-header
|
||||||
@@ -43,12 +43,12 @@ UNICODE_OBJ_DEBUG := $(patsubst libs/unicode_width/%.c,$(OBJ_DIR)/debug/unicode_
|
|||||||
UNICODE_OBJ_RELEASE := $(patsubst libs/unicode_width/%.c,$(OBJ_DIR)/release/unicode_width/%.o,$(UNICODE_SRC))
|
UNICODE_OBJ_RELEASE := $(patsubst libs/unicode_width/%.c,$(OBJ_DIR)/release/unicode_width/%.o,$(UNICODE_SRC))
|
||||||
|
|
||||||
LIBS_RELEASE := \
|
LIBS_RELEASE := \
|
||||||
libs/libgrapheme/libgrapheme.a \
|
libs/libgrapheme/libgrapheme.a ./libs/mruby/build/host/lib/libmruby.a \
|
||||||
-Wl,-Bstatic,--gc-sections -lpcre2-8 -lmruby
|
-Wl,-Bstatic,--gc-sections -lpcre2-8
|
||||||
|
|
||||||
LIBS_DEBUG := \
|
LIBS_DEBUG := \
|
||||||
libs/libgrapheme/libgrapheme.a \
|
libs/libgrapheme/libgrapheme.a ./libs/mruby/build/host/lib/libmruby.a \
|
||||||
-Wl,-Bdynamic -lpcre2-8 -lmruby
|
-Wl,-Bdynamic -lpcre2-8
|
||||||
|
|
||||||
SRC := $(wildcard $(SRC_DIR)/**/*.cc) $(wildcard $(SRC_DIR)/*.cc)
|
SRC := $(wildcard $(SRC_DIR)/**/*.cc) $(wildcard $(SRC_DIR)/*.cc)
|
||||||
OBJ_DEBUG := $(patsubst $(SRC_DIR)/%.cc,$(OBJ_DIR)/debug/%.o,$(SRC))
|
OBJ_DEBUG := $(patsubst $(SRC_DIR)/%.cc,$(OBJ_DIR)/debug/%.o,$(SRC))
|
||||||
|
|||||||
6
TODO.md
6
TODO.md
@@ -4,13 +4,13 @@ Copyright 2025 Syed Daanish
|
|||||||
|
|
||||||
##### BTW Check each lsp with each of the features implemented
|
##### BTW Check each lsp with each of the features implemented
|
||||||
|
|
||||||
* [ ] Make a proper qeued system for bar contents from ruby
|
* [ ] Add mgems for most common things and a ruby library to allow combining true ruby with mruby
|
||||||
* [ ] Allow clipbaord setting & alpha in ini files
|
- Or revert to cruby and retry with manual linking . maybe it might work?
|
||||||
|
* [ ] color alpha in ini files
|
||||||
* [ ] Make warning before ctrl+q for saving
|
* [ ] Make warning before ctrl+q for saving
|
||||||
* [ ] **LSP Bug:** Check why `fish-lsp` is behaving so off with completions filtering.
|
* [ ] **LSP Bug:** Check why `fish-lsp` is behaving so off with completions filtering.
|
||||||
* [ ] **Line move:** fix the move line functions to work without the calculations from folds as folds are removed.
|
* [ ] **Line move:** fix the move line functions to work without the calculations from folds as folds are removed.
|
||||||
* [ ] **Editor Indentation Fix:** - Main : merger indentation with the parser for more accurate results.
|
* [ ] **Editor Indentation Fix:** - Main : merger indentation with the parser for more accurate results.
|
||||||
* [ ] Fix bug where enter at start of line with ending type crashes
|
|
||||||
* [ ] Keep cache of language maps in engine to reduce lookup time.
|
* [ ] Keep cache of language maps in engine to reduce lookup time.
|
||||||
* [ ] In indents add function to support tab which indents if before any content and inserts a pure \t otherwise.
|
* [ ] In indents add function to support tab which indents if before any content and inserts a pure \t otherwise.
|
||||||
* [ ] And backspace which undents if before any content.
|
* [ ] And backspace which undents if before any content.
|
||||||
|
|||||||
@@ -115,8 +115,8 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line);
|
|||||||
// Each subsequent call returns the next line as a null terminated string
|
// Each subsequent call returns the next line as a null terminated string
|
||||||
// `it` is the iterator returned from begin_l_iter
|
// `it` is the iterator returned from begin_l_iter
|
||||||
// After getting the necessary lines free the iterator (no need to go upto
|
// After getting the necessary lines free the iterator (no need to go upto
|
||||||
// the end) returns null if there are no more lines All return strings
|
// the end) returns null if there are no more lines
|
||||||
// `must` be freed by the caller
|
// The string must not be freed
|
||||||
char *next_line(LineIterator *it, uint32_t *out_len);
|
char *next_line(LineIterator *it, uint32_t *out_len);
|
||||||
|
|
||||||
// Returns the previous line as a null terminated string
|
// Returns the previous line as a null terminated string
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||||
#define PCRE_WORKSPACE_SIZE 512
|
#define PCRE_WORKSPACE_SIZE 512
|
||||||
|
|
||||||
#include <mruby.h>
|
#include "mruby.h"
|
||||||
#include <mruby/array.h>
|
#include "mruby/array.h"
|
||||||
#include <mruby/compile.h>
|
#include "mruby/compile.h"
|
||||||
#include <mruby/hash.h>
|
#include "mruby/hash.h"
|
||||||
#include <mruby/irep.h>
|
#include "mruby/irep.h"
|
||||||
#include <mruby/string.h>
|
#include "mruby/string.h"
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <pcre2.h>
|
#include <pcre2.h>
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ struct BarLine {
|
|||||||
void setup_ruby_bindings(mrb_state *mrb, RClass *C_module);
|
void setup_ruby_bindings(mrb_state *mrb, RClass *C_module);
|
||||||
void ruby_start();
|
void ruby_start();
|
||||||
void ruby_shutdown();
|
void ruby_shutdown();
|
||||||
|
void ruby_copy(const char *text, size_t len);
|
||||||
|
std::string ruby_paste();
|
||||||
void load_theme();
|
void load_theme();
|
||||||
void load_languages_info();
|
void load_languages_info();
|
||||||
uint8_t read_line_endings();
|
uint8_t read_line_endings();
|
||||||
|
|||||||
@@ -1,3 +1,65 @@
|
|||||||
|
module Clipboard
|
||||||
|
@clip = ""
|
||||||
|
@os = :os_name_placed_here
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def command_exists?(cmd)
|
||||||
|
system("command -v #{cmd} > /dev/null 2>&1")
|
||||||
|
end
|
||||||
|
def copy(text)
|
||||||
|
if @os == :windows
|
||||||
|
IO.popen("clip", "w") { |f| f.write(text) }
|
||||||
|
elsif @os == :mac
|
||||||
|
IO.popen("pbcopy", "w") { |f| f.write(text) }
|
||||||
|
elsif @os == :linux
|
||||||
|
if ENV["XDG_SESSION_TYPE"]&.downcase == "wayland" || ENV["WAYLAND_DISPLAY"]
|
||||||
|
if command_exists?("wl-copy")
|
||||||
|
IO.popen("wl-copy", "w") { |f| f.write(text) }
|
||||||
|
else
|
||||||
|
osc52_copy(text)
|
||||||
|
end
|
||||||
|
elsif ENV["XDG_SESSION_TYPE"]&.downcase == "x11" || ENV["DISPLAY"]
|
||||||
|
if command_exists?("xsel")
|
||||||
|
IO.popen("xsel --clipboard --input", "w") { |f| f.write(text) }
|
||||||
|
elsif command_exists?("xclip")
|
||||||
|
IO.popen("xclip -selection clipboard", "w") { |f| f.write(text) }
|
||||||
|
else
|
||||||
|
osc52_copy(text)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@clip = text
|
||||||
|
end
|
||||||
|
def paste
|
||||||
|
if @os == :windows
|
||||||
|
return `powershell -NoProfile -Command Get-Clipboard`
|
||||||
|
elsif @os == :mac
|
||||||
|
return `pbpaste`
|
||||||
|
elsif @os == :linux
|
||||||
|
if ENV["XDG_SESSION_TYPE"]&.downcase == "wayland" || ENV["WAYLAND_DISPLAY"]
|
||||||
|
if command_exists?("wl-copy")
|
||||||
|
return `wl-paste`
|
||||||
|
end
|
||||||
|
elsif ENV["XDG_SESSION_TYPE"]&.downcase == "x11" || ENV["DISPLAY"]
|
||||||
|
if command_exists?("xsel")
|
||||||
|
return `xsel --clipboard --output`
|
||||||
|
elsif command_exists?("xclip")
|
||||||
|
return `xclip -selection clipboard -o`
|
||||||
|
else
|
||||||
|
return @clip
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
def osc52_copy(text)
|
||||||
|
encoded = [text].pack("m0")
|
||||||
|
print "\e]52;c;#{encoded}\a"
|
||||||
|
text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
module C
|
module C
|
||||||
@lsp_config = {
|
@lsp_config = {
|
||||||
"clangd" => [
|
"clangd" => [
|
||||||
@@ -262,7 +324,7 @@ module C
|
|||||||
@highlighters = {}
|
@highlighters = {}
|
||||||
@b_startup = nil
|
@b_startup = nil
|
||||||
@b_shutdown = nil
|
@b_shutdown = nil
|
||||||
@b_bar = lambda do |info|
|
@b_bar = proc do |info|
|
||||||
# mode, lang_name, warnings, lsp_name, filename, foldername, line, max_line, width
|
# mode, lang_name, warnings, lsp_name, filename, foldername, line, max_line, width
|
||||||
# puts info.inspect
|
# puts info.inspect
|
||||||
mode_color = 0x82AAFF
|
mode_color = 0x82AAFF
|
||||||
@@ -294,20 +356,27 @@ module C
|
|||||||
highlights << { fg: lang_info[:color], bg: 0x24272d, start: 13, length: 2 }
|
highlights << { fg: lang_info[:color], bg: 0x24272d, start: 13, length: 2 }
|
||||||
highlights << { fg: 0xced4df, bg: 0x24272d, start: 15, length: filename.length }
|
highlights << { fg: 0xced4df, bg: 0x24272d, start: 15, length: filename.length }
|
||||||
highlights << { fg: 0x24272d, bg: 0x000000, start: 15 + filename.length, length: 1 }
|
highlights << { fg: 0x24272d, bg: 0x000000, start: 15 + filename.length, length: 1 }
|
||||||
return {
|
next {
|
||||||
text: starting,
|
text: starting,
|
||||||
highlights: highlights
|
highlights: highlights
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@b_copy = proc do |text|
|
||||||
|
Clipboard.copy(text)
|
||||||
|
end
|
||||||
|
@b_paste = proc do
|
||||||
|
next Clipboard.paste
|
||||||
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :theme, :lsp_config, :languages,
|
attr_accessor :theme, :lsp_config, :languages,
|
||||||
:line_endings, :highlighters
|
:line_endings, :highlighters
|
||||||
attr_reader :b_startup, :b_shutdown, :b_extra_highlights, :b_bar
|
attr_reader :b_startup, :b_shutdown, :b_extra_highlights,
|
||||||
|
:b_bar, :b_copy, :b_paste
|
||||||
|
|
||||||
# def bar=(block)
|
def bar=(&block)
|
||||||
# @b_bar = block
|
@b_bar = block
|
||||||
# end
|
end
|
||||||
|
|
||||||
def startup(&block)
|
def startup(&block)
|
||||||
@b_startup = block
|
@b_startup = block
|
||||||
@@ -317,6 +386,14 @@ module C
|
|||||||
@b_shutdown = block
|
@b_shutdown = block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def copy(&block)
|
||||||
|
@b_copy = block
|
||||||
|
end
|
||||||
|
|
||||||
|
def paste(&block)
|
||||||
|
@b_paste = block
|
||||||
|
end
|
||||||
|
|
||||||
def extra_highlights(&block)
|
def extra_highlights(&block)
|
||||||
@b_extra_highlights = block
|
@b_extra_highlights = block
|
||||||
end
|
end
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -5,17 +5,12 @@
|
|||||||
|
|
||||||
struct LineTree {
|
struct LineTree {
|
||||||
void clear() {
|
void clear() {
|
||||||
std::unique_lock lock(mtx);
|
|
||||||
clear_node(root);
|
clear_node(root);
|
||||||
root = nullptr;
|
root = nullptr;
|
||||||
stack_size = 0;
|
stack_size = 0;
|
||||||
}
|
}
|
||||||
void build(uint32_t x) {
|
void build(uint32_t x) { root = build_node(x); }
|
||||||
std::unique_lock lock(mtx);
|
|
||||||
root = build_node(x);
|
|
||||||
}
|
|
||||||
LineData *at(uint32_t x) {
|
LineData *at(uint32_t x) {
|
||||||
std::shared_lock lock(mtx);
|
|
||||||
LineNode *n = root;
|
LineNode *n = root;
|
||||||
while (n) {
|
while (n) {
|
||||||
uint32_t left_size = n->left ? n->left->size : 0;
|
uint32_t left_size = n->left ? n->left->size : 0;
|
||||||
@@ -31,7 +26,6 @@ struct LineTree {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
LineData *start_iter(uint32_t x) {
|
LineData *start_iter(uint32_t x) {
|
||||||
std::shared_lock lock(mtx);
|
|
||||||
stack_size = 0;
|
stack_size = 0;
|
||||||
LineNode *n = root;
|
LineNode *n = root;
|
||||||
while (n) {
|
while (n) {
|
||||||
@@ -52,7 +46,6 @@ struct LineTree {
|
|||||||
}
|
}
|
||||||
void end_iter() { stack_size = 0; }
|
void end_iter() { stack_size = 0; }
|
||||||
LineData *next() {
|
LineData *next() {
|
||||||
std::shared_lock lock(mtx);
|
|
||||||
while (stack_size) {
|
while (stack_size) {
|
||||||
auto &f = stack[stack_size - 1];
|
auto &f = stack[stack_size - 1];
|
||||||
LineNode *n = f.node;
|
LineNode *n = f.node;
|
||||||
@@ -73,21 +66,16 @@ struct LineTree {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
void insert(uint32_t x, uint32_t y) {
|
void insert(uint32_t x, uint32_t y) {
|
||||||
std::unique_lock lock(mtx);
|
|
||||||
if (x > subtree_size(root))
|
if (x > subtree_size(root))
|
||||||
x = subtree_size(root);
|
x = subtree_size(root);
|
||||||
root = insert_node(root, x, y);
|
root = insert_node(root, x, y);
|
||||||
}
|
}
|
||||||
void erase(uint32_t x, uint32_t y) {
|
void erase(uint32_t x, uint32_t y) {
|
||||||
std::unique_lock lock(mtx);
|
|
||||||
if (x + y > subtree_size(root))
|
if (x + y > subtree_size(root))
|
||||||
x = subtree_size(root) - y;
|
x = subtree_size(root) - y;
|
||||||
root = erase_node(root, x, y);
|
root = erase_node(root, x, y);
|
||||||
}
|
}
|
||||||
uint32_t count() {
|
uint32_t count() { return subtree_size(root); }
|
||||||
std::shared_lock lock(mtx);
|
|
||||||
return subtree_size(root);
|
|
||||||
}
|
|
||||||
~LineTree() { clear(); }
|
~LineTree() { clear(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -117,7 +105,6 @@ private:
|
|||||||
LineNode *root = nullptr;
|
LineNode *root = nullptr;
|
||||||
Frame stack[32];
|
Frame stack[32];
|
||||||
std::atomic<uint8_t> stack_size = 0;
|
std::atomic<uint8_t> stack_size = 0;
|
||||||
std::shared_mutex mtx;
|
|
||||||
static constexpr uint32_t LEAF_TARGET = 256;
|
static constexpr uint32_t LEAF_TARGET = 256;
|
||||||
LineTree::LineNode *erase_node(LineNode *n, uint32_t x, uint32_t y) {
|
LineTree::LineNode *erase_node(LineNode *n, uint32_t x, uint32_t y) {
|
||||||
if (!n || y == 0)
|
if (!n || y == 0)
|
||||||
|
|||||||
@@ -17,10 +17,8 @@ struct Parser {
|
|||||||
mrb_value parser_block = mrb_nil_value();
|
mrb_value parser_block = mrb_nil_value();
|
||||||
mrb_value match_block = mrb_nil_value();
|
mrb_value match_block = mrb_nil_value();
|
||||||
bool is_custom{false};
|
bool is_custom{false};
|
||||||
std::atomic<uint32_t> scroll_max{UINT32_MAX - 2048};
|
std::atomic<uint32_t> scroll_max{0};
|
||||||
std::atomic<bool> scroll_dirty{false};
|
std::atomic<bool> scroll_dirty{false};
|
||||||
std::mutex mutex;
|
|
||||||
std::mutex data_mutex;
|
|
||||||
LineTree line_tree;
|
LineTree line_tree;
|
||||||
UniqueQueue<uint32_t> dirty_lines;
|
UniqueQueue<uint32_t> dirty_lines;
|
||||||
|
|
||||||
|
|||||||
@@ -155,9 +155,6 @@ std::string get_exe_dir();
|
|||||||
char *load_file(const char *path, uint32_t *out_len, bool *out_eol);
|
char *load_file(const char *path, uint32_t *out_len, bool *out_eol);
|
||||||
Language language_for_file(const char *filename);
|
Language language_for_file(const char *filename);
|
||||||
|
|
||||||
void copy_to_clipboard(const char *text, size_t len);
|
|
||||||
char *get_from_clipboard(uint32_t *out_len);
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline T *safe_get(std::map<uint16_t, T> &m, uint16_t key) {
|
inline T *safe_get(std::map<uint16_t, T> &m, uint16_t key) {
|
||||||
auto it = m.find(key);
|
auto it = m.find(key);
|
||||||
|
|||||||
1
libs/mruby
vendored
Submodule
1
libs/mruby
vendored
Submodule
Submodule libs/mruby added at 7d08c6246d
@@ -11,7 +11,7 @@ void cut(Editor *editor) {
|
|||||||
Coord start;
|
Coord start;
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
char *text = get_selection(editor, &len, &start);
|
char *text = get_selection(editor, &len, &start);
|
||||||
copy_to_clipboard(text, len);
|
ruby_copy(text, len);
|
||||||
len = count_clusters(text, len, 0, len);
|
len = count_clusters(text, len, 0, len);
|
||||||
edit_erase(editor, start, len);
|
edit_erase(editor, start, len);
|
||||||
free(text);
|
free(text);
|
||||||
@@ -23,22 +23,20 @@ void copy(Editor *editor) {
|
|||||||
return;
|
return;
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
char *text = get_selection(editor, &len, nullptr);
|
char *text = get_selection(editor, &len, nullptr);
|
||||||
copy_to_clipboard(text, len);
|
ruby_copy(text, len);
|
||||||
free(text);
|
free(text);
|
||||||
editor->selection_active = false;
|
editor->selection_active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void paste(Editor *editor) {
|
void paste(Editor *editor) {
|
||||||
uint32_t len;
|
|
||||||
if (mode == NORMAL) {
|
if (mode == NORMAL) {
|
||||||
char *text = get_from_clipboard(&len);
|
std::string text = ruby_paste();
|
||||||
if (text) {
|
if (text.empty())
|
||||||
insert_str(editor, text, len);
|
return;
|
||||||
free(text);
|
insert_str(editor, (char *)text.c_str(), text.length());
|
||||||
}
|
|
||||||
} else if (mode == SELECT) {
|
} else if (mode == SELECT) {
|
||||||
char *text = get_from_clipboard(&len);
|
std::string text = ruby_paste();
|
||||||
if (text) {
|
if (!text.empty()) {
|
||||||
Coord start, end;
|
Coord start, end;
|
||||||
selection_bounds(editor, &start, &end);
|
selection_bounds(editor, &start, &end);
|
||||||
uint32_t start_byte =
|
uint32_t start_byte =
|
||||||
@@ -46,8 +44,7 @@ void paste(Editor *editor) {
|
|||||||
uint32_t end_byte =
|
uint32_t end_byte =
|
||||||
line_to_byte(editor->root, end.row, nullptr) + end.col;
|
line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||||
edit_erase(editor, start, end_byte - start_byte);
|
edit_erase(editor, start, end_byte - start_byte);
|
||||||
edit_insert(editor, editor->cursor, text, len);
|
edit_insert(editor, editor->cursor, (char *)text.c_str(), text.length());
|
||||||
free(text);
|
|
||||||
}
|
}
|
||||||
editor->selection_active = false;
|
editor->selection_active = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,6 @@ void render_editor(Editor *editor) {
|
|||||||
while (warn_it != editor->warnings.end() &&
|
while (warn_it != editor->warnings.end() &&
|
||||||
warn_it->line < editor->scroll.row)
|
warn_it->line < editor->scroll.row)
|
||||||
++warn_it;
|
++warn_it;
|
||||||
std::unique_lock<std::mutex> lock;
|
|
||||||
if (editor->parser)
|
|
||||||
lock = std::unique_lock<std::mutex>(editor->parser->mutex);
|
|
||||||
LineData *line_data = nullptr;
|
LineData *line_data = nullptr;
|
||||||
auto get_type = [&](uint32_t col) {
|
auto get_type = [&](uint32_t col) {
|
||||||
if (!line_data)
|
if (!line_data)
|
||||||
@@ -450,8 +447,6 @@ void render_editor(Editor *editor) {
|
|||||||
global_byte_offset += line_len + 1;
|
global_byte_offset += line_len + 1;
|
||||||
line_index++;
|
line_index++;
|
||||||
}
|
}
|
||||||
if (lock.owns_lock())
|
|
||||||
lock.unlock();
|
|
||||||
while (rendered_rows < editor->size.row) {
|
while (rendered_rows < editor->size.row) {
|
||||||
for (uint32_t col = 0; col < editor->size.col; col++)
|
for (uint32_t col = 0; col < editor->size.col; col++)
|
||||||
update(editor->position.row + rendered_rows, editor->position.col + col,
|
update(editor->position.row + rendered_rows, editor->position.col + col,
|
||||||
|
|||||||
61
src/main.cc
61
src/main.cc
@@ -37,7 +37,30 @@ inline uint8_t index_of(Editor *ed) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void input_listener() {
|
int main(int argc, char *argv[]) {
|
||||||
|
ruby_start();
|
||||||
|
load_theme();
|
||||||
|
load_languages_info();
|
||||||
|
load_custom_highlighters();
|
||||||
|
|
||||||
|
Coord screen = start_screen();
|
||||||
|
const char *filename = (argc > 1) ? argv[1] : "";
|
||||||
|
uint8_t eol = read_line_endings();
|
||||||
|
Editor *editor =
|
||||||
|
new_editor(filename, {0, 0}, {screen.row - 2, screen.col}, eol);
|
||||||
|
bar.init(screen);
|
||||||
|
|
||||||
|
if (!editor) {
|
||||||
|
end_screen();
|
||||||
|
fprintf(stderr, "Failed to load editor\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
editors.push_back(editor);
|
||||||
|
current_editor = editors.size() - 1;
|
||||||
|
|
||||||
|
std::thread lsp_thread(background_lsp);
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
KeyEvent event = throttle(1ms, read_key);
|
KeyEvent event = throttle(1ms, read_key);
|
||||||
if (event.key_type == KEY_NONE)
|
if (event.key_type == KEY_NONE)
|
||||||
@@ -67,44 +90,12 @@ void input_listener() {
|
|||||||
if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) && event.c)
|
if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) && event.c)
|
||||||
free(event.c);
|
free(event.c);
|
||||||
render:
|
render:
|
||||||
|
throttle(4ms, editor_worker, editors[current_editor]);
|
||||||
|
bar.work();
|
||||||
bar.render();
|
bar.render();
|
||||||
render_editor(editors[current_editor]);
|
render_editor(editors[current_editor]);
|
||||||
throttle(4ms, render);
|
throttle(4ms, render);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
ruby_start();
|
|
||||||
load_theme();
|
|
||||||
load_languages_info();
|
|
||||||
load_custom_highlighters();
|
|
||||||
|
|
||||||
Coord screen = start_screen();
|
|
||||||
const char *filename = (argc > 1) ? argv[1] : "";
|
|
||||||
uint8_t eol = read_line_endings();
|
|
||||||
Editor *editor =
|
|
||||||
new_editor(filename, {0, 0}, {screen.row - 2, screen.col}, eol);
|
|
||||||
bar.init(screen);
|
|
||||||
|
|
||||||
if (!editor) {
|
|
||||||
end_screen();
|
|
||||||
fprintf(stderr, "Failed to load editor\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
editors.push_back(editor);
|
|
||||||
current_editor = editors.size() - 1;
|
|
||||||
|
|
||||||
std::thread input_thread(input_listener);
|
|
||||||
std::thread lsp_thread(background_lsp);
|
|
||||||
|
|
||||||
while (running) {
|
|
||||||
throttle(16ms, editor_worker, editors[current_editor]);
|
|
||||||
bar.work();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (input_thread.joinable())
|
|
||||||
input_thread.join();
|
|
||||||
|
|
||||||
if (lsp_thread.joinable())
|
if (lsp_thread.joinable())
|
||||||
lsp_thread.join();
|
lsp_thread.join();
|
||||||
|
|||||||
@@ -19,14 +19,29 @@ while read -r line; do
|
|||||||
fi
|
fi
|
||||||
done <"$INPUT"
|
done <"$INPUT"
|
||||||
|
|
||||||
|
OS="$(uname -s)"
|
||||||
|
OS_TYPE="unknown"
|
||||||
|
|
||||||
|
case "$OS" in
|
||||||
|
Linux*)
|
||||||
|
OS_TYPE="linux"
|
||||||
|
;;
|
||||||
|
Darwin*)
|
||||||
|
OS_TYPE="mac"
|
||||||
|
;;
|
||||||
|
CYGWIN* | MINGW* | MSYS*)
|
||||||
|
OS_TYPE="windows"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
{
|
{
|
||||||
echo " freeze"
|
echo " freeze"
|
||||||
echo "end"
|
echo "end"
|
||||||
echo
|
echo
|
||||||
cat "$SCRIPT_DIR/../include/scripting/libcrib.rb"
|
cat "$SCRIPT_DIR/../include/scripting/libcrib.rb" | sed "s/os_name_placed_here/$OS_TYPE/g"
|
||||||
} >>"$TMP"
|
} >>"$TMP"
|
||||||
|
|
||||||
mrbc -o$OUTPUT $TMP
|
"$SCRIPT_DIR/../libs/mruby/bin/mrbc" -o$OUTPUT $TMP
|
||||||
|
|
||||||
{
|
{
|
||||||
echo "#pragma once"
|
echo "#pragma once"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include "io/sysio.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "scripting/decl.h"
|
#include "scripting/decl.h"
|
||||||
#include "scripting/ruby_compiled.h"
|
#include "scripting/ruby_compiled.h"
|
||||||
@@ -148,8 +149,13 @@ BarLine bar_contents(uint8_t mode, std::string lang_name, uint32_t warnings,
|
|||||||
mrb_value mod_val = mrb_obj_value(C_module);
|
mrb_value mod_val = mrb_obj_value(C_module);
|
||||||
mrb_value block = mrb_funcall(mrb, mod_val, "b_bar", 0);
|
mrb_value block = mrb_funcall(mrb, mod_val, "b_bar", 0);
|
||||||
mrb_value val_line = mrb_funcall(mrb, block, "call", 1, info);
|
mrb_value val_line = mrb_funcall(mrb, block, "call", 1, info);
|
||||||
if (mrb->exc)
|
if (mrb->exc) {
|
||||||
|
end_screen();
|
||||||
|
fputs("Error when executing Ruby code:\n", stderr);
|
||||||
|
mrb_print_error(mrb);
|
||||||
|
mrb_close(mrb);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
}
|
||||||
mrb_value text_val = mrb_hash_get(
|
mrb_value text_val = mrb_hash_get(
|
||||||
mrb, val_line, mrb_symbol_value(mrb_intern_cstr(mrb, "text")));
|
mrb, val_line, mrb_symbol_value(mrb_intern_cstr(mrb, "text")));
|
||||||
const char *ptr = RSTRING_PTR(text_val);
|
const char *ptr = RSTRING_PTR(text_val);
|
||||||
@@ -161,6 +167,43 @@ BarLine bar_contents(uint8_t mode, std::string lang_name, uint32_t warnings,
|
|||||||
return bar_line;
|
return bar_line;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ruby_copy(const char *text, size_t len) {
|
||||||
|
if (C_module == nullptr)
|
||||||
|
return;
|
||||||
|
mrb_value mod_val = mrb_obj_value(C_module);
|
||||||
|
mrb_value block = mrb_funcall(mrb, mod_val, "b_copy", 0);
|
||||||
|
if (!mrb_nil_p(block))
|
||||||
|
mrb_funcall(mrb, block, "call", 1, mrb_str_new(mrb, text, len));
|
||||||
|
if (mrb->exc) {
|
||||||
|
end_screen();
|
||||||
|
fputs("Error when executing Ruby code:\n", stderr);
|
||||||
|
mrb_print_error(mrb);
|
||||||
|
mrb_close(mrb);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ruby_paste() {
|
||||||
|
if (C_module == nullptr)
|
||||||
|
return "";
|
||||||
|
mrb_value mod_val = mrb_obj_value(C_module);
|
||||||
|
mrb_value block = mrb_funcall(mrb, mod_val, "b_paste", 0);
|
||||||
|
if (!mrb_nil_p(block)) {
|
||||||
|
mrb_value val = mrb_funcall(mrb, block, "call", 0);
|
||||||
|
if (mrb->exc) {
|
||||||
|
end_screen();
|
||||||
|
fputs("Error when executing Ruby code:\n", stderr);
|
||||||
|
mrb_print_error(mrb);
|
||||||
|
mrb_close(mrb);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (mrb_string_p(val))
|
||||||
|
return std::string(RSTRING_PTR(val), RSTRING_LEN(val));
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
void ruby_shutdown() {
|
void ruby_shutdown() {
|
||||||
if (C_module == nullptr)
|
if (C_module == nullptr)
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ Parser::Parser(Editor *n_editor, std::string n_lang, uint32_t n_scroll_max) {
|
|||||||
|
|
||||||
void Parser::edit(uint32_t start_line, uint32_t old_end_line,
|
void Parser::edit(uint32_t start_line, uint32_t old_end_line,
|
||||||
uint32_t inserted_rows) {
|
uint32_t inserted_rows) {
|
||||||
std::lock_guard lock(data_mutex);
|
|
||||||
if (((int64_t)old_end_line - (int64_t)start_line) > 0)
|
if (((int64_t)old_end_line - (int64_t)start_line) > 0)
|
||||||
line_tree.erase(start_line, old_end_line - start_line);
|
line_tree.erase(start_line, old_end_line - start_line);
|
||||||
if (inserted_rows > 0)
|
if (inserted_rows > 0)
|
||||||
@@ -45,143 +44,90 @@ void Parser::edit(uint32_t start_line, uint32_t old_end_line,
|
|||||||
void Parser::work() {
|
void Parser::work() {
|
||||||
if (!editor || !editor->root)
|
if (!editor || !editor->root)
|
||||||
return;
|
return;
|
||||||
std::shared_lock k_lock(editor->knot_mtx);
|
std::vector<uint32_t> batch;
|
||||||
k_lock.unlock();
|
|
||||||
uint32_t capacity = 256;
|
|
||||||
char *text = (char *)calloc((capacity + 1), sizeof(char));
|
|
||||||
std::unique_lock lock_data(data_mutex);
|
|
||||||
lock_data.unlock();
|
|
||||||
std::unique_lock lock(mutex);
|
|
||||||
lock.unlock();
|
|
||||||
uint32_t c_line;
|
uint32_t c_line;
|
||||||
while (dirty_lines.pop(c_line)) {
|
while (dirty_lines.pop(c_line))
|
||||||
if (!running.load(std::memory_order_relaxed)) {
|
batch.push_back(c_line);
|
||||||
free(text);
|
for (uint32_t c_line : batch) {
|
||||||
return;
|
if (!running.load(std::memory_order_relaxed))
|
||||||
}
|
break;
|
||||||
if (c_line > scroll_max + 40) {
|
uint32_t min_line = scroll_max > 50 ? scroll_max - 50 : 0;
|
||||||
|
uint32_t max_line = scroll_max + 10;
|
||||||
|
if (c_line < min_line || c_line > max_line) {
|
||||||
dirty_lines.push(c_line);
|
dirty_lines.push(c_line);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (scroll_max > 50 && c_line < scroll_max - 50) {
|
uint32_t scroll_snapshot = scroll_max;
|
||||||
dirty_lines.push(c_line);
|
std::shared_ptr<void> prev_state = nullptr;
|
||||||
continue;
|
uint32_t line_count;
|
||||||
|
line_count = line_tree.count();
|
||||||
|
if (c_line > 0 && c_line < line_count)
|
||||||
|
prev_state = line_tree.at(c_line - 1)->out_state;
|
||||||
|
std::shared_lock k_lock(editor->knot_mtx);
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, c_line);
|
||||||
|
uint32_t cur_line = c_line;
|
||||||
|
while (cur_line < line_count) {
|
||||||
|
if (!running.load(std::memory_order_relaxed))
|
||||||
|
break;
|
||||||
|
if (scroll_snapshot != scroll_max) {
|
||||||
|
dirty_lines.push(cur_line);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
uint32_t line_count = line_tree.count();
|
if (cur_line < min_line || cur_line > max_line) {
|
||||||
lock_data.lock();
|
dirty_lines.push(cur_line);
|
||||||
std::shared_ptr<void> prev_state =
|
break;
|
||||||
(c_line > 0) && c_line < line_tree.count()
|
|
||||||
? line_tree.at(c_line - 1)->out_state
|
|
||||||
: nullptr;
|
|
||||||
lock_data.unlock();
|
|
||||||
while (c_line < line_count) {
|
|
||||||
if (!running.load(std::memory_order_relaxed)) {
|
|
||||||
free(text);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (scroll_dirty.exchange(false, std::memory_order_acq_rel)) {
|
uint32_t len;
|
||||||
dirty_lines.push(c_line);
|
char *line = next_line(it, &len);
|
||||||
c_line = scroll_max < 50 ? 0 : scroll_max - 50;
|
if (!line)
|
||||||
}
|
break;
|
||||||
k_lock.lock();
|
LineData *line_data = line_tree.at(cur_line);
|
||||||
if (c_line > editor->root->line_count) {
|
|
||||||
k_lock.unlock();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
uint32_t r_offset, r_len;
|
|
||||||
r_offset = line_to_byte(editor->root, c_line, &r_len);
|
|
||||||
if (r_len > capacity) {
|
|
||||||
capacity = r_len;
|
|
||||||
text = (char *)realloc(text, capacity + 1);
|
|
||||||
memset(text, 0, capacity + 1);
|
|
||||||
}
|
|
||||||
read_into(editor->root, r_offset, r_len, text);
|
|
||||||
k_lock.unlock();
|
|
||||||
if (c_line < scroll_max &&
|
|
||||||
((scroll_max > 100 && c_line > scroll_max - 100) || c_line < 100))
|
|
||||||
lock.lock();
|
|
||||||
if (line_tree.count() < c_line) {
|
|
||||||
if (lock.owns_lock())
|
|
||||||
lock.unlock();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
lock_data.lock();
|
|
||||||
LineData *line_data = line_tree.at(c_line);
|
|
||||||
if (!line_data) {
|
if (!line_data) {
|
||||||
lock_data.unlock();
|
cur_line++;
|
||||||
if (lock.owns_lock())
|
|
||||||
lock.unlock();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::shared_ptr<void> new_state{nullptr};
|
std::shared_ptr<void> new_state;
|
||||||
if (is_custom) {
|
if (is_custom) {
|
||||||
mrb_value state = mrb_nil_value();
|
mrb_value state = mrb_nil_value();
|
||||||
if (prev_state) {
|
if (prev_state)
|
||||||
std::shared_ptr<CustomState> state_ptr =
|
state = std::static_pointer_cast<CustomState>(prev_state)->state;
|
||||||
std::static_pointer_cast<CustomState>(prev_state);
|
|
||||||
state = state_ptr->state;
|
|
||||||
}
|
|
||||||
mrb_value out_state = parse_custom(&line_data->tokens, parser_block,
|
mrb_value out_state = parse_custom(&line_data->tokens, parser_block,
|
||||||
text, r_len, state, c_line);
|
line, len, state, cur_line);
|
||||||
std::shared_ptr<CustomState> out_state_ptr =
|
new_state = std::make_shared<CustomState>(out_state);
|
||||||
std::make_shared<CustomState>(out_state);
|
|
||||||
new_state = out_state_ptr;
|
|
||||||
} else {
|
} else {
|
||||||
new_state =
|
new_state =
|
||||||
parse_func(&line_data->tokens, prev_state, text, r_len, c_line);
|
parse_func(&line_data->tokens, prev_state, line, len, cur_line);
|
||||||
}
|
}
|
||||||
line_data->in_state = prev_state;
|
line_data->in_state = prev_state;
|
||||||
line_data->out_state = new_state;
|
line_data->out_state = new_state;
|
||||||
if (!running.load(std::memory_order_relaxed)) {
|
bool done = false;
|
||||||
free(text);
|
if (cur_line + 1 < line_count) {
|
||||||
return;
|
LineData *next_line_data = line_tree.at(cur_line + 1);
|
||||||
|
if (next_line_data) {
|
||||||
|
if (is_custom) {
|
||||||
|
mrb_value a =
|
||||||
|
prev_state
|
||||||
|
? std::static_pointer_cast<CustomState>(new_state)->state
|
||||||
|
: mrb_nil_value();
|
||||||
|
mrb_value b = next_line_data->in_state
|
||||||
|
? std::static_pointer_cast<CustomState>(
|
||||||
|
next_line_data->in_state)
|
||||||
|
->state
|
||||||
|
: mrb_nil_value();
|
||||||
|
done = custom_compare(match_block, a, b);
|
||||||
|
} else {
|
||||||
|
done = state_match_func(new_state, next_line_data->in_state);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
prev_state = new_state;
|
prev_state = new_state;
|
||||||
c_line++;
|
cur_line++;
|
||||||
if (c_line < line_count && c_line > scroll_max + 50 && scroll_max < 50 &&
|
if (done)
|
||||||
c_line < scroll_max + 50) {
|
|
||||||
lock_data.unlock();
|
|
||||||
if (lock.owns_lock())
|
|
||||||
lock.unlock();
|
|
||||||
if (c_line > 0)
|
|
||||||
dirty_lines.push(c_line - 1);
|
|
||||||
dirty_lines.push(c_line);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (c_line < line_count && (line_data = line_tree.at(c_line))) {
|
free(it->buffer);
|
||||||
bool done = false;
|
free(it);
|
||||||
if (is_custom) {
|
|
||||||
mrb_value in_state_v = mrb_nil_value();
|
|
||||||
if (prev_state)
|
|
||||||
in_state_v =
|
|
||||||
std::static_pointer_cast<CustomState>(prev_state)->state;
|
|
||||||
mrb_value out_state_v = mrb_nil_value();
|
|
||||||
if (line_data->in_state)
|
|
||||||
out_state_v =
|
|
||||||
std::static_pointer_cast<CustomState>(line_data->in_state)
|
|
||||||
->state;
|
|
||||||
done = custom_compare(match_block, in_state_v, out_state_v);
|
|
||||||
} else {
|
|
||||||
done = state_match_func(prev_state, line_data->in_state);
|
|
||||||
}
|
}
|
||||||
if (done) {
|
|
||||||
lock_data.unlock();
|
|
||||||
if (lock.owns_lock())
|
|
||||||
lock.unlock();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lock_data.unlock();
|
|
||||||
if (lock.owns_lock())
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
if (!running.load(std::memory_order_relaxed)) {
|
|
||||||
free(text);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
free(text);
|
|
||||||
lock_data.lock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::scroll(uint32_t line) {
|
void Parser::scroll(uint32_t line) {
|
||||||
@@ -190,7 +136,6 @@ void Parser::scroll(uint32_t line) {
|
|||||||
uint32_t c_line = line > 50 ? line - 50 : 0;
|
uint32_t c_line = line > 50 ? line - 50 : 0;
|
||||||
if (c_line >= line_tree.count())
|
if (c_line >= line_tree.count())
|
||||||
return;
|
return;
|
||||||
std::unique_lock lock_data(data_mutex);
|
|
||||||
if (line_tree.at(c_line)->in_state || line_tree.at(c_line)->out_state)
|
if (line_tree.at(c_line)->in_state || line_tree.at(c_line)->out_state)
|
||||||
return;
|
return;
|
||||||
scroll_dirty = true;
|
scroll_dirty = true;
|
||||||
|
|||||||
@@ -115,52 +115,3 @@ Language language_for_file(const char *filename) {
|
|||||||
}
|
}
|
||||||
return Language{};
|
return Language{};
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_from_clipboard(uint32_t *out_len) {
|
|
||||||
FILE *pipe = popen("xclip -selection clipboard -o", "r");
|
|
||||||
if (!pipe) {
|
|
||||||
*out_len = 0;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
size_t capacity = 4096;
|
|
||||||
size_t length = 0;
|
|
||||||
char *buffer = (char *)malloc(capacity);
|
|
||||||
if (!buffer) {
|
|
||||||
pclose(pipe);
|
|
||||||
*out_len = 0;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
size_t n;
|
|
||||||
while ((n = fread(buffer + length, 1, capacity - length, pipe)) > 0) {
|
|
||||||
length += n;
|
|
||||||
if (length == capacity) {
|
|
||||||
capacity *= 2;
|
|
||||||
char *tmp = (char *)realloc(buffer, capacity);
|
|
||||||
if (!tmp) {
|
|
||||||
free(buffer);
|
|
||||||
pclose(pipe);
|
|
||||||
*out_len = 0;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
buffer = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pclose(pipe);
|
|
||||||
char *result = (char *)realloc(buffer, length + 1);
|
|
||||||
if (result) {
|
|
||||||
result[length] = '\0';
|
|
||||||
buffer = result;
|
|
||||||
} else {
|
|
||||||
buffer[length] = '\0';
|
|
||||||
}
|
|
||||||
*out_len = length;
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void copy_to_clipboard(const char *text, size_t len) {
|
|
||||||
FILE *pipe = popen("xclip -selection clipboard", "w");
|
|
||||||
if (!pipe)
|
|
||||||
return;
|
|
||||||
fwrite(text, sizeof(char), len, pipe);
|
|
||||||
pclose(pipe);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user