Compare commits
123 Commits
507bd57854
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20d71722ea | ||
|
|
cf383c9772 | ||
|
|
82dceaca68 | ||
|
|
4f4940bcd3 | ||
|
|
aff3403404 | ||
|
|
b04b566681 | ||
|
|
aa85d9420a | ||
|
|
4c21026842 | ||
|
|
cab882de60 | ||
|
|
fadf4cd698 | ||
|
|
94ee03800d | ||
|
|
207fc65fec | ||
|
|
4ad9e6bf20 | ||
|
|
edb461bde8 | ||
|
|
3c6153380d | ||
|
|
7cfae9af92 | ||
|
|
e1149680d9 | ||
|
|
9b2c8626d4 | ||
|
|
52c55e27d9 | ||
|
|
56a729b617 | ||
|
|
fde4e463ad | ||
|
|
65ba886ddf | ||
|
|
84fa04e1db | ||
|
|
17f4306fc3 | ||
|
|
80df86df3d | ||
|
|
7fad476b61 | ||
|
|
2acf7fbb45 | ||
|
|
36d39f9417 | ||
|
|
3ab6639afb | ||
|
|
7a73a982d2 | ||
|
|
51f1fd1b4f | ||
|
|
930620a49e | ||
|
|
8cb1b49cd8 | ||
|
|
830be12b24 | ||
|
|
ed3be39037 | ||
|
|
33d623268a | ||
|
|
5198c11fc6 | ||
|
|
bc7c52e1ec | ||
|
|
bbf97eba2f | ||
|
|
52390a7aa8 | ||
|
|
d5d99cddf7 | ||
|
|
217659256b | ||
|
|
4d9cfcd302 | ||
|
|
d5099cee38 | ||
|
|
017b70f3e6 | ||
|
|
903159d2bd | ||
|
|
a87d2491bb | ||
|
|
70b752eccb | ||
|
|
805f5de852 | ||
|
|
c9acc31cfc | ||
|
|
2d1edd800a | ||
|
|
22a82db946 | ||
|
|
4e8987101d | ||
|
|
034ac5d452 | ||
|
|
9c34a4dd52 | ||
|
|
0c488bc313 | ||
|
|
1c25b39df0 | ||
|
|
2454370736 | ||
|
|
eec1e137b7 | ||
|
|
a9730924d7 | ||
|
|
88dbfb1a6c | ||
|
|
f3e709f07c | ||
|
|
7ae4e74f50 | ||
|
|
863f140edc | ||
|
|
3618d906e6 | ||
|
|
26691ab6d3 | ||
|
|
923131a5b6 | ||
|
|
7b47f2f6f5 | ||
|
|
f5825f050c | ||
|
|
05b6a7bb3c | ||
|
|
cc69d807e3 | ||
|
|
dc839cb3e0 | ||
|
|
ba6de21396 | ||
|
|
b75e0a8a55 | ||
|
|
9a64fd8e01 | ||
|
|
b0a0fca8ee | ||
|
|
710269a876 | ||
|
|
4a410c3c1a | ||
|
|
4fa1a49765 | ||
|
|
9614f01e20 | ||
|
|
36839df841 | ||
|
|
2b1eab54b3 | ||
|
|
8128c2932e | ||
|
|
851eac0ec2 | ||
|
|
df963b0893 | ||
|
|
84c992c574 | ||
|
|
1ce128a5d5 | ||
|
|
372287f557 | ||
|
|
8ecfc82d8f | ||
|
|
9d60b110f5 | ||
|
|
70f28de0e1 | ||
|
|
76e279920f | ||
|
|
37cb6248a5 | ||
|
|
37154a2067 | ||
|
|
e52450a3df | ||
|
|
6fc5f0538f | ||
|
|
c970c1c7d6 | ||
|
|
01488e4eca | ||
|
|
ce1fe5feb1 | ||
|
|
270109a56b | ||
|
|
663286b45c | ||
|
|
d5c3794a93 | ||
|
|
dcda334767 | ||
|
|
69c9f7a336 | ||
|
|
ca3d087aa3 | ||
|
|
d2be40a282 | ||
|
|
6c200f7764 | ||
|
|
8de20eb0e2 | ||
|
|
86c91668e1 | ||
|
|
1e4e9fbccc | ||
|
|
36180c5c90 | ||
|
|
5ab20ce8ab | ||
|
|
61fc0cd339 | ||
|
|
3864060699 | ||
|
|
db06c783a9 | ||
|
|
f33c9873d0 | ||
|
|
66483f6c99 | ||
|
|
da821cb581 | ||
|
|
f85697d037 | ||
|
|
02c45e0dfd | ||
|
|
05d5de4b7d | ||
|
|
e575569d8a | ||
|
|
4c9c0e5210 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -6,6 +6,7 @@ src/external/SDL/
|
||||
build/
|
||||
package/
|
||||
|
||||
project.te
|
||||
*.rdbg
|
||||
*.spall
|
||||
*.sublime-workspace
|
||||
@@ -17,3 +18,4 @@ te_debug
|
||||
text_editor.js
|
||||
*.map
|
||||
*.exe
|
||||
scratch
|
||||
121
README.md
121
README.md
@@ -1,9 +1,118 @@
|
||||
# te, a text editor
|
||||
# te
|
||||
|
||||

|
||||
A from-scratch text editor that aims to stay fast, hackable, and practical for day-to-day coding.
|
||||
|
||||
In order to build on windows you need to call build.bat from
|
||||
the x64 Native Command Tools prompt that ships with Visual Studio.
|
||||

|
||||
|
||||
For Linux you will need clang, SDL3 and libbacktrace libraries to be installed and
|
||||
available on your system. Just call build.sh when these are available.
|
||||
## What this project is
|
||||
|
||||
`te` is a native desktop editor written as a single C++ executable (SDL3 + OpenGL + custom editor core).
|
||||
It is built around a straightforward single-threaded architecture, with coroutines used for asynchronous workflows (searching, UI flows, process jobs) so behavior stays easy to reason about and debug.
|
||||
|
||||
The codebase includes its own:
|
||||
|
||||
- text buffer + undo/redo engine
|
||||
- window/view layout system
|
||||
- command system + keybinding parser
|
||||
- fuzzy selection UI
|
||||
- process execution and output streaming
|
||||
- built-in font fallback (baked into the binary)
|
||||
|
||||
## Highlights
|
||||
|
||||
- VS Code-style keybindings and command palette (`Ctrl+Shift+P`)
|
||||
- Multi-cursor editing (keyboard and mouse workflows)
|
||||
- Fuzzy-open patterns for commands, files, and open buffers
|
||||
- Project-wide search (`Ctrl+Shift+F`) and replace-all workflow
|
||||
- Shell integration directly from editor input:
|
||||
- `:Command` to run editor commands
|
||||
- `:Set ...` to configure options and keybindings
|
||||
- `!cmd` / `!!cmd` to execute shell commands
|
||||
- `py:...` to execute Python snippets via discovered Python interpreter
|
||||
- Build panel integration (`Ctrl+B`, `Alt+B`, etc.) with configurable build commands
|
||||
- Config-driven behavior (font, colors, project commands, keymaps, paths)
|
||||
|
||||
## Build
|
||||
|
||||
### Linux
|
||||
|
||||
Requirements:
|
||||
|
||||
- `clang`
|
||||
- `cmake`
|
||||
- `libbacktrace`
|
||||
- SDL3 (the build script can clone and install SDL when missing)
|
||||
|
||||
Build commands:
|
||||
|
||||
```bash
|
||||
./build.sh # debug build (default)
|
||||
./build.sh release # optimized build
|
||||
./build.sh slow # extra asserts/instrumentation path
|
||||
```
|
||||
|
||||
Run:
|
||||
|
||||
```bash
|
||||
./build/te
|
||||
./build/te path/to/file.cpp
|
||||
```
|
||||
|
||||
### Windows
|
||||
|
||||
Use the **x64 Native Tools Command Prompt for Visual Studio**.
|
||||
|
||||
Requirements:
|
||||
|
||||
- Visual Studio C++ toolchain (`cl`, `msbuild`)
|
||||
- `cmake`
|
||||
|
||||
Build commands:
|
||||
|
||||
```bat
|
||||
build.bat
|
||||
build.bat release
|
||||
build.bat slow
|
||||
```
|
||||
|
||||
Run:
|
||||
|
||||
```bat
|
||||
build\te.exe
|
||||
build\te.exe path\to\file.cpp
|
||||
```
|
||||
|
||||
## First steps in the editor
|
||||
|
||||
- `Ctrl+N`: new buffer
|
||||
- `Ctrl+O`: open current file's folder / directory navigation view
|
||||
- `Ctrl+P`: fuzzy list of open buffers
|
||||
- `Ctrl+Shift+P`: all commands
|
||||
- `Ctrl+Q` or `F12`: open thing under caret (path / link / command)
|
||||
- `Ctrl+F`, `F3`, `Shift+F3`: in-buffer search flow
|
||||
- `Ctrl+Shift+F`: interactive search across open project buffers
|
||||
- `Alt+Shift+Up/Down`: add cursors vertically
|
||||
- `Ctrl+B`: run Build1 command (configurable)
|
||||
|
||||
## Configuration
|
||||
|
||||
On startup, `te` loads a config file from SDL's app preference directory (`config.te`).
|
||||
You can also pass additional `.te` files as CLI arguments.
|
||||
|
||||
Useful examples:
|
||||
|
||||
```text
|
||||
:Set FontSize 16
|
||||
:Set PathToFont '/path/to/font.ttf'
|
||||
:Set Build1OnUnix 'sh build.sh release'
|
||||
:Set Build1OnWindows 'build.bat release'
|
||||
:Set TextColor ff202020
|
||||
:Set BackgroundColor fffdf6e3
|
||||
```
|
||||
|
||||
## Notes on architecture
|
||||
|
||||
- Single-threaded main loop (event/update/render)
|
||||
- Coroutines for async editor tasks instead of multi-threaded complexity
|
||||
- "Plugin" modules are integrated in the source tree and compiled in
|
||||
- Focus on predictable behavior and low-latency interaction
|
||||
|
||||
@@ -6,6 +6,7 @@ for %%a in (%*) do set "%%~a=1"
|
||||
if not "%release%"=="1" set debug=1
|
||||
if "%debug%"=="1" set release=0 && echo [debug mode]
|
||||
if "%release%"=="1" set debug=0 && echo [release mode]
|
||||
if "%slow%"=="1" echo [slow mode]
|
||||
if "%package%"=="1" echo [package zip]
|
||||
|
||||
if not exist "src\external\SDL" (
|
||||
@@ -28,9 +29,11 @@ pushd build
|
||||
|
||||
set flags=/EHsc- /MT /Zi /FC /nologo /WX /W3 /wd4200 /wd4334 /diagnostics:column
|
||||
if "%release%"=="1" set flags=%flags% -O2 -DDEBUG_BUILD=0
|
||||
if "%slow%"=="1" set flags=%flags% -DSLOW_BUILD=1
|
||||
|
||||
set libs=%sdllib%/SDL3-static.lib %sdllib%/SDL_uclibc.lib kernel32.lib gdi32.lib user32.lib Imm32.lib ole32.lib Shell32.lib OleAut32.lib Cfgmgr32.lib Setupapi.lib Advapi32.lib version.lib winmm.lib
|
||||
cl %flags% ../src/text_editor/text_editor.cpp -Fe:te.exe -I%sdl%/include -I../src/external/glad -I../src/ %libs% -link /SUBSYSTEM:WINDOWS /NODEFAULTLIB:LIBCMT /NODEFAULTLIB:MSVCRTD
|
||||
rem rc ..\data\icon.rc
|
||||
cl %flags% ../src/text_editor.cpp -Fe:te.exe -I%sdl%/include -I../src/external/glad %libs% -link /SUBSYSTEM:WINDOWS /NODEFAULTLIB:LIBCMT /NODEFAULTLIB:MSVCRTD ..\data\icon.res
|
||||
|
||||
:: small util when working on the editor using the editor
|
||||
copy te.exe te2.exe
|
||||
|
||||
39
build.sh
39
build.sh
@@ -1,13 +1,38 @@
|
||||
#!/usr/bin/bash
|
||||
|
||||
mkdir build
|
||||
for arg in "$@"; do declare $arg='1'; done
|
||||
if [ ! -v release ]; then debug=1; fi
|
||||
if [ -v debug ]; then echo "[debug build]"; fi
|
||||
if [ -v release ]; then echo "[release build]"; fi
|
||||
if [ -v slow ]; then echo "[slow build]"; fi
|
||||
if [ -v addr ]; then echo "[address sanitizer build]"; fi
|
||||
|
||||
mkdir -p build
|
||||
|
||||
if [ ! -e "src/external/SDL" ]; then
|
||||
cd src/external
|
||||
git clone https://github.com/libsdl-org/SDL.git
|
||||
cd SDL
|
||||
git checkout release-3.2.30
|
||||
# We need older version of SDL3 because there is a bug on wayland that
|
||||
# doubles click events and it's kind of unusable
|
||||
cmake -S . -B build_linux -DSDL_PIPEWIRE=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr
|
||||
cd build_linux
|
||||
sudo make -j16 install
|
||||
cd ../../../..
|
||||
fi
|
||||
|
||||
cd build
|
||||
|
||||
FLAGS="-Wall -Wno-missing-braces -Wno-writable-strings -nostdlib++ -fsanitize=address -fno-exceptions -fdiagnostics-absolute-paths -g -Wno-writable-strings -I../src -DDEBUG_BUILD=1"
|
||||
# clang -o metaprogram $FLAGS ../src/metaprogram/metaprogram.cpp
|
||||
# ./metaprogram
|
||||
I="-I../src/external/SDL/include -I../src/external/glad"
|
||||
flags="-Wall -Wextra -Werror -Wformat=2 -Wundef -Wshadow -Wno-missing-field-initializers -Wno-missing-braces -Wno-writable-strings \
|
||||
-g -fdiagnostics-absolute-paths \
|
||||
-nostdlib++ -fno-exceptions"
|
||||
|
||||
I="-I../src/external/SDL/include -I../src/external/lua/src -I../src/external/glad"
|
||||
clang -o te $FLAGS ../src/text_editor/text_editor.cpp $I -lSDL3 -lm -lbacktrace
|
||||
if [ -v debug ]; then flags="$flags -fsanitize=undefined -fno-omit-frame-pointer -DDEBUG_BUILD=1"; fi
|
||||
if [ -v release ]; then flags="$flags -DDEBUG_BUILD=0 -O2"; fi
|
||||
if [ -v slow ]; then flags="$flags -DSLOW_BUILD=1"; fi
|
||||
if [ -v addr ]; then flags="$flags -fsanitize=address"; fi
|
||||
|
||||
# -v -Wl,--verbose
|
||||
time clang -o te $flags ../src/text_editor.cpp $I -lSDL3 -lm -lbacktrace
|
||||
./te :RunTests
|
||||
BIN
data/icon.res
Normal file
BIN
data/icon.res
Normal file
Binary file not shown.
@@ -1,79 +0,0 @@
|
||||
! What precise workflow do I need for me to be viable to use this?
|
||||
! From a user (novice) point of view, how does it look like?
|
||||
|
||||
- Buffer edit history separate from caret history (tagging history blocks with carets???)
|
||||
- We need regex for: [FormatCode matching, IsCode matching,
|
||||
- Remedybg commands integrated! (like clicking f5 and opening up the window)
|
||||
- Macros
|
||||
- ctrl-e started doing no-ops again ... ??
|
||||
- Window position: vscode opens in fullscreen and then remembers what position it was close in (could be a config option)
|
||||
- On Linux: Try to implement is_debugger_present()
|
||||
- Probably shouldn't emit (Key pressed when ctrl etc. is not clicked!!)
|
||||
|
||||
- OnUpdate view hooks!
|
||||
- OnSave buffer hooks which will execute the config on save
|
||||
- Make the special view hooks also available for modification and registered but maybe under different name or something
|
||||
- Maybe IPC for te.exe when it's already open and file arguments are passed it should perhaps open a buffer in current window??
|
||||
|
||||
Use session 4
|
||||
- Add <<File>> <<WorkDir>> template strings to Open (Then remove SEtWorkdirhere)
|
||||
- :Set Filename to name current buffer ??? :O and others like that!!
|
||||
|
||||
- Make a fuzzy command !> grep and fuzzy over it??? (doesn't seem very useful for grep)
|
||||
- Make the equivalent of SearchProject but for cmds like !@git grep -n "@>"
|
||||
- Add Bool variable
|
||||
- RegisterCommand should_appear_in_listing variable
|
||||
|
||||
Use session 3:
|
||||
- Maybe status view, commit changes (like to buffer name or line) on enter?
|
||||
|
||||
How to go about search/replace, opening code and other considerations
|
||||
- Search and replace sign Find@>ReplaceWith
|
||||
- We can use sed + find to search and replace, the automatic reopen should do the job
|
||||
|
||||
Use session 2
|
||||
- Guide on the first page for new users with links to configs, tutorials
|
||||
|
||||
Debug session:
|
||||
- Report errorf - use coroutine dialogs
|
||||
- Replace in render layer also
|
||||
- BlockAllocator something is not working there which only showed after executing OpenCode on many files
|
||||
- Some bad allocating happening in Clipboard for sure OR MAYBE BLOCK ALLOCATOR ACTION
|
||||
|
||||
New UI Session
|
||||
- Uneditable buffers ?
|
||||
- Maybe marked allocations??? So that we can associate allocations with a buffer or view and then dealloc all at the same time
|
||||
|
||||
- Open with seek string (open at pattern) filename:32 filename:/^Window$/
|
||||
- Show what process/coroutines are running and allow to kill (active process buffer?)
|
||||
- Database idea: use special buffers to store information
|
||||
- Editing the buffer doesn't seem to be the slow part rather, accessing the data and putting it into the buffer (potentially hitting many different memory locations) I have a crazy idea to use buffers in order to store the names in a serialized format
|
||||
- non editable buffers (raw ops ok, non-raw no op)
|
||||
- DBBuffer
|
||||
|
||||
- Try to add Tracking Allocator and rewrite the app, free all memory at the end of the app and check all is well
|
||||
|
||||
backlog
|
||||
FEATURE Some decl/function indexing in fuzzy format
|
||||
ISSUE? Fix jump scroll, the query ends up the last line on screen, kind of wacky
|
||||
FEATURE dump text editor state to file, restore state
|
||||
- Search and replace
|
||||
- word complete
|
||||
- Search all buffers in 10X style, incrementally searched results popping up on every key press (maybe we need coroutine library in C so this is easier?)
|
||||
- escapeing multiple cursor after ctrl + d should put the cursor where it was (probably will need to swap secondary and primary cursor for new cursor
|
||||
- draw indentation levels like in sublime (those lines) - we render chars one by one so seems relatively easy to figure out if whitespace belongs to beginning of line (make sure to add max value like 40 because of big files)
|
||||
- code sections, visual demarkation if beginning of line has a very specific text + goto next / goto prev section hotkey!
|
||||
- combine glyph and selection rendering
|
||||
|
||||
- Test stdin writing code
|
||||
- Implement shell interaction (the valid cmd lines should start with '>' or '$', user can add more lines like this to expand the command size maybe?, if we have a case where we have a line with '>' but the last line doesn't have (just a space) then it should execute?)
|
||||
- drop text into window
|
||||
- I think the way sublime text and we display line highlights is confusing with multiple cursors (line highlight can be confused with selection)
|
||||
- make the editor replayable, store events and then replay, be careful about globals
|
||||
- maybe open should return multiple options if there are many more? (like in sublime if many symbols you get a window and you choose and it automatically jumps you to the symbol in the background)
|
||||
- I want a way to assign flags to buffers/views/windows from user perspective so that console window concept can be created from user space
|
||||
- font cache and on demand unicode loads
|
||||
- color parens, braces
|
||||
- gap buffer
|
||||
- optimize rendering - command buffer, and vertice buffer instead of vertice buffer with scissor
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
Basic Latin
|
||||
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
|
||||
Latin-1 Supplement
|
||||
¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ
|
||||
Latin Extended-A
|
||||
Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď ď Đ đ Ē ē Ĕ ĕ Ė ė Ę ę Ě ě Ĝ ĝ Ğ ğ Ġ ġ Ģ ģ Ĥ ĥ Ħ ħ Ĩ ĩ Ī ī Ĭ ĭ Į į İ ı IJ ij Ĵ ĵ Ķ ķ ĸ Ĺ ĺ Ļ ļ Ľ ľ Ŀ ŀ Ł ł Ń ń Ņ ņ Ň ň ʼn Ŋ ŋ Ō ō Ŏ ŏ Ő ő Œ œ Ŕ ŕ Ŗ ŗ Ř ř Ś ś Ŝ ŝ Ş ş Š š Ţ ţ Ť ť Ŧ ŧ Ũ ũ Ū ū Ŭ ŭ Ů ů Ű ű Ų ų Ŵ ŵ Ŷ ŷ Ÿ Ź ź Ż ż Ž ž ſ
|
||||
Latin Extended-B
|
||||
ƀ Ɓ Ƃ ƃ Ƅ ƅ Ɔ Ƈ ƈ Ɖ Ɗ Ƌ ƌ ƍ Ǝ Ə Ɛ Ƒ ƒ Ɠ Ɣ ƕ Ɩ Ɨ Ƙ ƙ ƚ ƛ Ɯ Ɲ ƞ Ɵ Ơ ơ Ƣ ƣ Ƥ ƥ Ʀ Ƨ ƨ Ʃ ƪ ƫ Ƭ ƭ Ʈ Ư ư Ʊ Ʋ Ƴ ƴ Ƶ ƶ Ʒ Ƹ ƹ ƺ ƻ Ƽ ƽ ƾ ƿ ǀ ǁ ǂ ǃ DŽ Dž dž LJ Lj lj NJ Nj nj Ǎ ǎ Ǐ ǐ Ǒ ǒ Ǔ ǔ Ǖ ǖ Ǘ ǘ Ǚ ǚ Ǜ ǜ ǝ Ǟ ǟ Ǡ ǡ Ǣ ǣ Ǥ ǥ Ǧ ǧ Ǩ ǩ Ǫ ǫ Ǭ ǭ Ǯ ǯ ǰ DZ Dz dz Ǵ ǵ Ǻ ǻ Ǽ ǽ Ǿ ǿ Ȁ ȁ Ȃ ȃ ...
|
||||
IPA Extensions
|
||||
ɐ ɑ ɒ ɓ ɔ ɕ ɖ ɗ ɘ ə ɚ ɛ ɜ ɝ ɞ ɟ ɠ ɡ ɢ ɣ ɤ ɥ ɦ ɧ ɨ ɩ ɪ ɫ ɬ ɭ ɮ ɯ ɰ ɱ ɲ ɳ ɴ ɵ ɶ ɷ ɸ ɹ ɺ ɻ ɼ ɽ ɾ ɿ ʀ ʁ ʂ ʃ ʄ ʅ ʆ ʇ ʈ ʉ ʊ ʋ ʌ ʍ ʎ ʏ ʐ ʑ ʒ ʓ ʔ ʕ ʖ ʗ ʘ ʙ ʚ ʛ ʜ ʝ ʞ ʟ ʠ ʡ ʢ ʣ ʤ ʥ ʦ ʧ ʨ
|
||||
Spacing Modifier Letters
|
||||
ʰ ʱ ʲ ʳ ʴ ʵ ʶ ʷ ʸ ʹ ʺ ʻ ʼ ʽ ʾ ʿ ˀ ˁ ˂ ˃ ˄ ˅ ˆ ˇ ˈ ˉ ˊ ˋ ˌ ˍ ˎ ˏ ː ˑ ˒ ˓ ˔ ˕ ˖ ˗ ˘ ˙ ˚ ˛ ˜ ˝ ˞ ˠ ˡ ˢ ˣ ˤ ˥ ˦ ˧ ˨ ˩
|
||||
Combining Diacritical Marks
|
||||
̀ ́ ̂ ̃ ̄ ̅ ̆ ̇ ̈ ̉ ̊ ̋ ̌ ̍ ̎ ̏ ̐ ̑ ̒ ̓ ̔ ̕ ̖ ̗ ̘ ̙ ̚ ̛ ̜ ̝ ̞ ̟ ̠ ̡ ̢ ̣ ̤ ̥ ̦ ̧ ̨ ̩ ̪ ̫ ̬ ̭ ̮ ̯ ̰ ̱ ̲ ̳ ̴ ̵ ̶ ̷ ̸ ̹ ̺ ̻ ̼ ̽ ̾ ̿ ̀ ́ ͂ ̓ ̈́ ͅ ͠ ͡
|
||||
Greek
|
||||
ʹ ͵ ͺ ; ΄ ΅ Ά · Έ Ή Ί Ό Ύ Ώ ΐ Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω Ϊ Ϋ ά έ ή ί ΰ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ ς σ τ υ φ χ ψ ω ϊ ϋ ό ύ ώ ϐ ϑ ϒ ϓ ϔ ϕ ϖ Ϛ Ϝ Ϟ Ϡ Ϣ ϣ Ϥ ϥ Ϧ ϧ Ϩ ϩ Ϫ ϫ Ϭ ϭ Ϯ ϯ ϰ ϱ ϲ ϳ
|
||||
Cyrillic
|
||||
Ё Ђ Ѓ Є Ѕ І Ї Ј Љ Њ Ћ Ќ Ў Џ А Б В Г Д Е Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Ъ Ы Ь Э Ю Я а б в г д е ж з и й к л м н о п р с т у ф х ц ч ш щ ъ ы ь э ю я ё ђ ѓ є ѕ і ї ј љ њ ћ ќ ў џ Ѡ ѡ Ѣ ѣ Ѥ ѥ Ѧ ѧ Ѩ ѩ Ѫ ѫ Ѭ ѭ Ѯ ѯ Ѱ ѱ Ѳ ѳ Ѵ ѵ Ѷ ѷ Ѹ ѹ Ѻ ѻ Ѽ ѽ Ѿ ѿ Ҁ ҁ ҂ ҃ ...
|
||||
Armenian
|
||||
Ա Բ Գ Դ Ե Զ Է Ը Թ Ժ Ի Լ Խ Ծ Կ Հ Ձ Ղ Ճ Մ Յ Ն Շ Ո Չ Պ Ջ Ռ Ս Վ Տ Ր Ց Ւ Փ Ք Օ Ֆ ՙ ՚ ՛ ՜ ՝ ՞ ՟ ա բ գ դ ե զ է ը թ ժ ի լ խ ծ կ հ ձ ղ ճ մ յ ն շ ո չ պ ջ ռ ս վ տ ր ց ւ փ ք օ ֆ և ։
|
||||
Hebrew
|
||||
֑ ֒ ֓ ֔ ֕ ֖ ֗ ֘ ֙ ֚ ֛ ֜ ֝ ֞ ֟ ֠ ֡ ֣ ֤ ֥ ֦ ֧ ֨ ֩ ֪ ֫ ֬ ֭ ֮ ֯ ְ ֱ ֲ ֳ ִ ֵ ֶ ַ ָ ֹ ֻ ּ ֽ ־ ֿ ׀ ׁ ׂ ׃ ׄ א ב ג ד ה ו ז ח ט י ך כ ל ם מ ן נ ס ע ף פ ץ צ ק ר ש ת װ ױ ײ ׳ ״
|
||||
Arabic
|
||||
، ؛ ؟ ء آ أ ؤ إ ئ ا ب ة ت ث ج ح خ د ذ ر ز س ش ص ض ط ظ ع غ ـ ف ق ك ل م ن ه و ى ي ً ٌ ٍ َ ُ ِ ّ ْ ٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩ ٪ ٫ ٬ ٭ ٰ ٱ ٲ ٳ ٴ ٵ ٶ ٷ ٸ ٹ ٺ ٻ ټ ٽ پ ٿ ڀ ځ ڂ ڃ ڄ څ چ ڇ ڈ ډ ڊ ڋ ڌ ڍ ڎ ڏ ڐ ڑ ڒ ړ ڔ ڕ ږ ڗ ژ ڙ ښ ڛ ڜ ڝ ڞ ڟ ڠ ڡ ڢ ڣ ڤ ڥ ڦ ڧ ڨ ک ڪ ګ ڬ ڭ ڮ گ ڰ ڱ ...
|
||||
Devanagari
|
||||
ँ ं ः अ आ इ ई उ ऊ ऋ ऌ ऍ ऎ ए ऐ ऑ ऒ ओ औ क ख ग घ ङ च छ ज झ ञ ट ठ ड ढ ण त थ द ध न ऩ प फ ब भ म य र ऱ ल ळ ऴ व श ष स ह ़ ऽ ा ि ी ु ू ृ ॄ ॅ ॆ े ै ॉ ॊ ो ौ ् ॐ ॑ ॒ ॓ ॔ क़ ख़ ग़ ज़ ड़ ढ़ फ़ य़ ॠ ॡ ॢ ॣ । ॥ ० १ २ ३ ४ ५ ६ ७ ८ ९ ॰
|
||||
Bengali
|
||||
ঁ ং ঃ অ আ ই ঈ উ ঊ ঋ ঌ এ ঐ ও ঔ ক খ গ ঘ ঙ চ ছ জ ঝ ঞ ট ঠ ড ঢ ণ ত থ দ ধ ন প ফ ব ভ ম য র ল শ ষ স হ ় া ি ী ু ূ ৃ ৄ ে ৈ ো ৌ ্ ৗ ড় ঢ় য় ৠ ৡ ৢ ৣ ০ ১ ২ ৩ ৪ ৫ ৬ ৭ ৮ ৯ ৰ ৱ ৲ ৳ ৴ ৵ ৶ ৷ ৸ ৹ ৺
|
||||
Gurmukhi
|
||||
ਂ ਅ ਆ ਇ ਈ ਉ ਊ ਏ ਐ ਓ ਔ ਕ ਖ ਗ ਘ ਙ ਚ ਛ ਜ ਝ ਞ ਟ ਠ ਡ ਢ ਣ ਤ ਥ ਦ ਧ ਨ ਪ ਫ ਬ ਭ ਮ ਯ ਰ ਲ ਲ਼ ਵ ਸ਼ ਸ ਹ ਼ ਾ ਿ ੀ ੁ ੂ ੇ ੈ ੋ ੌ ੍ ਖ਼ ਗ਼ ਜ਼ ੜ ਫ਼ ੦ ੧ ੨ ੩ ੪ ੫ ੬ ੭ ੮ ੯ ੰ ੱ ੲ ੳ ੴ
|
||||
Gujarati
|
||||
ઁ ં ઃ અ આ ઇ ઈ ઉ ઊ ઋ ઍ એ ઐ ઑ ઓ ઔ ક ખ ગ ઘ ઙ ચ છ જ ઝ ઞ ટ ઠ ડ ઢ ણ ત થ દ ધ ન પ ફ બ ભ મ ય ર લ ળ વ શ ષ સ હ ઼ ઽ ા િ ી ુ ૂ ૃ ૄ ૅ ે ૈ ૉ ો ૌ ્ ૐ ૠ ૦ ૧ ૨ ૩ ૪ ૫ ૬ ૭ ૮ ૯
|
||||
Oriya
|
||||
ଁ ଂ ଃ ଅ ଆ ଇ ଈ ଉ ଊ ଋ ଌ ଏ ଐ ଓ ଔ କ ଖ ଗ ଘ ଙ ଚ ଛ ଜ ଝ ଞ ଟ ଠ ଡ ଢ ଣ ତ ଥ ଦ ଧ ନ ପ ଫ ବ ଭ ମ ଯ ର ଲ ଳ ଶ ଷ ସ ହ ଼ ଽ ା ି ୀ ୁ ୂ ୃ େ ୈ ୋ ୌ ୍ ୖ ୗ ଡ଼ ଢ଼ ୟ ୠ ୡ ୦ ୧ ୨ ୩ ୪ ୫ ୬ ୭ ୮ ୯ ୰
|
||||
Tamil
|
||||
ஂ ஃ அ ஆ இ ஈ உ ஊ எ ஏ ஐ ஒ ஓ ஔ க ங ச ஜ ஞ ட ண த ந ன ப ம ய ர ற ல ள ழ வ ஷ ஸ ஹ ா ி ீ ு ூ ெ ே ை ொ ோ ௌ ் ௗ ௧ ௨ ௩ ௪ ௫ ௬ ௭ ௮ ௯ ௰ ௱ ௲
|
||||
Telugu
|
||||
ఁ ం ః అ ఆ ఇ ఈ ఉ ఊ ఋ ఌ ఎ ఏ ఐ ఒ ఓ ఔ క ఖ గ ఘ ఙ చ ఛ జ ఝ ఞ ట ఠ డ ఢ ణ త థ ద ధ న ప ఫ బ భ మ య ర ఱ ల ళ వ శ ష స హ ా ి ీ ు ూ ృ ౄ ె ే ై ొ ో ౌ ్ ౕ ౖ ౠ ౡ ౦ ౧ ౨ ౩ ౪ ౫ ౬ ౭ ౮ ౯
|
||||
Kannada
|
||||
ಂ ಃ ಅ ಆ ಇ ಈ ಉ ಊ ಋ ಌ ಎ ಏ ಐ ಒ ಓ ಔ ಕ ಖ ಗ ಘ ಙ ಚ ಛ ಜ ಝ ಞ ಟ ಠ ಡ ಢ ಣ ತ ಥ ದ ಧ ನ ಪ ಫ ಬ ಭ ಮ ಯ ರ ಱ ಲ ಳ ವ ಶ ಷ ಸ ಹ ಾ ಿ ೀ ು ೂ ೃ ೄ ೆ ೇ ೈ ೊ ೋ ೌ ್ ೕ ೖ ೞ ೠ ೡ ೦ ೧ ೨ ೩ ೪ ೫ ೬ ೭ ೮ ೯
|
||||
Malayalam
|
||||
ം ഃ അ ആ ഇ ഈ ഉ ഊ ഋ ഌ എ ഏ ഐ ഒ ഓ ഔ ക ഖ ഗ ഘ ങ ച ഛ ജ ഝ ഞ ട ഠ ഡ ഢ ണ ത ഥ ദ ധ ന പ ഫ ബ ഭ മ യ ര റ ല ള ഴ വ ശ ഷ സ ഹ ാ ി ീ ു ൂ ൃ െ േ ൈ ൊ ോ ൌ ് ൗ ൠ ൡ ൦ ൧ ൨ ൩ ൪ ൫ ൬ ൭ ൮ ൯
|
||||
Thai
|
||||
ก ข ฃ ค ฅ ฆ ง จ ฉ ช ซ ฌ ญ ฎ ฏ ฐ ฑ ฒ ณ ด ต ถ ท ธ น บ ป ผ ฝ พ ฟ ภ ม ย ร ฤ ล ฦ ว ศ ษ ส ห ฬ อ ฮ ฯ ะ ั า ำ ิ ี ึ ื ุ ู ฺ ฿ เ แ โ ใ ไ ๅ ๆ ็ ่ ้ ๊ ๋ ์ ํ ๎ ๏ ๐ ๑ ๒ ๓ ๔ ๕ ๖ ๗ ๘ ๙ ๚ ๛
|
||||
Lao
|
||||
ກ ຂ ຄ ງ ຈ ຊ ຍ ດ ຕ ຖ ທ ນ ບ ປ ຜ ຝ ພ ຟ ມ ຢ ຣ ລ ວ ສ ຫ ອ ຮ ຯ ະ ັ າ ຳ ິ ີ ຶ ື ຸ ູ ົ ຼ ຽ ເ ແ ໂ ໃ ໄ ໆ ່ ້ ໊ ໋ ໌ ໍ ໐ ໑ ໒ ໓ ໔ ໕ ໖ ໗ ໘ ໙ ໜ ໝ
|
||||
Tibetan
|
||||
ༀ ༁ ༂ ༃ ༄ ༅ ༆ ༇ ༈ ༉ ༊ ་ ༌ ། ༎ ༏ ༐ ༑ ༒ ༓ ༔ ༕ ༖ ༗ ༘ ༙ ༚ ༛ ༜ ༝ ༞ ༟ ༠ ༡ ༢ ༣ ༤ ༥ ༦ ༧ ༨ ༩ ༪ ༫ ༬ ༭ ༮ ༯ ༰ ༱ ༲ ༳ ༴ ༵ ༶ ༷ ༸ ༹ ༺ ༻ ༼ ༽ ༾ ༿ ཀ ཁ ག གྷ ང ཅ ཆ ཇ ཉ ཊ ཋ ཌ ཌྷ ཎ ཏ ཐ ད དྷ ན པ ཕ བ བྷ མ ཙ ཚ ཛ ཛྷ ཝ ཞ ཟ འ ཡ ར ལ ཤ ཥ ས ཧ ཨ ཀྵ ཱ ི ཱི ུ ཱུ ྲྀ ཷ ླྀ ཹ ེ ཻ ོ ཽ ཾ ཿ ྀ ཱྀ ྂ ྃ ྄ ྅ ྆ ྇ ...
|
||||
Georgian
|
||||
Ⴀ Ⴁ Ⴂ Ⴃ Ⴄ Ⴅ Ⴆ Ⴇ Ⴈ Ⴉ Ⴊ Ⴋ Ⴌ Ⴍ Ⴎ Ⴏ Ⴐ Ⴑ Ⴒ Ⴓ Ⴔ Ⴕ Ⴖ Ⴗ Ⴘ Ⴙ Ⴚ Ⴛ Ⴜ Ⴝ Ⴞ Ⴟ Ⴠ Ⴡ Ⴢ Ⴣ Ⴤ Ⴥ ა ბ გ დ ე ვ ზ თ ი კ ლ მ ნ ო პ ჟ რ ს ტ უ ფ ქ ღ ყ შ ჩ ც ძ წ ჭ ხ ჯ ჰ ჱ ჲ ჳ ჴ ჵ ჶ ჻
|
||||
Hangul Jamo
|
||||
ᄀ ᄁ ᄂ ᄃ ᄄ ᄅ ᄆ ᄇ ᄈ ᄉ ᄊ ᄋ ᄌ ᄍ ᄎ ᄏ ᄐ ᄑ ᄒ ᄓ ᄔ ᄕ ᄖ ᄗ ᄘ ᄙ ᄚ ᄛ ᄜ ᄝ ᄞ ᄟ ᄠ ᄡ ᄢ ᄣ ᄤ ᄥ ᄦ ᄧ ᄨ ᄩ ᄪ ᄫ ᄬ ᄭ ᄮ ᄯ ᄰ ᄱ ᄲ ᄳ ᄴ ᄵ ᄶ ᄷ ᄸ ᄹ ᄺ ᄻ ᄼ ᄽ ᄾ ᄿ ᅀ ᅁ ᅂ ᅃ ᅄ ᅅ ᅆ ᅇ ᅈ ᅉ ᅊ ᅋ ᅌ ᅍ ᅎ ᅏ ᅐ ᅑ ᅒ ᅓ ᅔ ᅕ ᅖ ᅗ ᅘ ᅙ ᅟ ᅠ ᅡ ᅢ ᅣ ᅤ ᅥ ᅦ ᅧ ᅨ ᅩ ᅪ ᅫ ᅬ ᅭ ᅮ ᅯ ᅰ ᅱ ᅲ ᅳ ᅴ ᅵ ᅶ ᅷ ᅸ ᅹ ᅺ ᅻ ᅼ ᅽ ᅾ ᅿ ᆀ ᆁ ᆂ ᆃ ᆄ ...
|
||||
Latin Extended Additional
|
||||
Ḁ ḁ Ḃ ḃ Ḅ ḅ Ḇ ḇ Ḉ ḉ Ḋ ḋ Ḍ ḍ Ḏ ḏ Ḑ ḑ Ḓ ḓ Ḕ ḕ Ḗ ḗ Ḙ ḙ Ḛ ḛ Ḝ ḝ Ḟ ḟ Ḡ ḡ Ḣ ḣ Ḥ ḥ Ḧ ḧ Ḩ ḩ Ḫ ḫ Ḭ ḭ Ḯ ḯ Ḱ ḱ Ḳ ḳ Ḵ ḵ Ḷ ḷ Ḹ ḹ Ḻ ḻ Ḽ ḽ Ḿ ḿ Ṁ ṁ Ṃ ṃ Ṅ ṅ Ṇ ṇ Ṉ ṉ Ṋ ṋ Ṍ ṍ Ṏ ṏ Ṑ ṑ Ṓ ṓ Ṕ ṕ Ṗ ṗ Ṙ ṙ Ṛ ṛ Ṝ ṝ Ṟ ṟ Ṡ ṡ Ṣ ṣ Ṥ ṥ Ṧ ṧ Ṩ ṩ Ṫ ṫ Ṭ ṭ Ṯ ṯ Ṱ ṱ Ṳ ṳ Ṵ ṵ Ṷ ṷ Ṹ ṹ Ṻ ṻ Ṽ ṽ Ṿ ṿ ...
|
||||
Greek Extended
|
||||
ἀ ἁ ἂ ἃ ἄ ἅ ἆ ἇ Ἀ Ἁ Ἂ Ἃ Ἄ Ἅ Ἆ Ἇ ἐ ἑ ἒ ἓ ἔ ἕ Ἐ Ἑ Ἒ Ἓ Ἔ Ἕ ἠ ἡ ἢ ἣ ἤ ἥ ἦ ἧ Ἠ Ἡ Ἢ Ἣ Ἤ Ἥ Ἦ Ἧ ἰ ἱ ἲ ἳ ἴ ἵ ἶ ἷ Ἰ Ἱ Ἲ Ἳ Ἴ Ἵ Ἶ Ἷ ὀ ὁ ὂ ὃ ὄ ὅ Ὀ Ὁ Ὂ Ὃ Ὄ Ὅ ὐ ὑ ὒ ὓ ὔ ὕ ὖ ὗ Ὑ Ὓ Ὕ Ὗ ὠ ὡ ὢ ὣ ὤ ὥ ὦ ὧ Ὠ Ὡ Ὢ Ὣ Ὤ Ὥ Ὦ Ὧ ὰ ά ὲ έ ὴ ή ὶ ί ὸ ό ὺ ύ ὼ ώ ᾀ ᾁ ᾂ ᾃ ᾄ ᾅ ᾆ ᾇ ᾈ ᾉ ᾊ ᾋ ᾌ ᾍ ...
|
||||
General Punctuation
|
||||
‐ ‑ ‒ – — ― ‖ ‗ ‘ ’ ‚ ‛ “ ” „ ‟ † ‡ • ‣ ․ ‥ … ‧
‰ ‱ ′ ″ ‴ ‵ ‶ ‷ ‸ ‹ › ※ ‼ ‽ ‾ ‿ ⁀ ⁁ ⁂ ⁃ ⁄ ⁅ ⁆
|
||||
Superscripts and Subscripts
|
||||
⁰ ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ ⁺ ⁻ ⁼ ⁽ ⁾ ⁿ ₀ ₁ ₂ ₃ ₄ ₅ ₆ ₇ ₈ ₉ ₊ ₋ ₌ ₍ ₎
|
||||
Currency Symbols
|
||||
₠ ₡ ₢ ₣ ₤ ₥ ₦ ₧ ₨ ₩ ₪ ₫
|
||||
Combining Marks for Symbols
|
||||
⃐ ⃑ ⃒ ⃓ ⃔ ⃕ ⃖ ⃗ ⃘ ⃙ ⃚ ⃛ ⃜ ⃝ ⃞ ⃟ ⃠ ⃡
|
||||
Letterlike Symbols
|
||||
℀ ℁ ℂ ℃ ℄ ℅ ℆ ℇ ℈ ℉ ℊ ℋ ℌ ℍ ℎ ℏ ℐ ℑ ℒ ℓ ℔ ℕ № ℗ ℘ ℙ ℚ ℛ ℜ ℝ ℞ ℟ ℠ ℡ ™ ℣ ℤ ℥ Ω ℧ ℨ ℩ K Å ℬ ℭ ℮ ℯ ℰ ℱ Ⅎ ℳ ℴ ℵ ℶ ℷ ℸ
|
||||
Number Forms
|
||||
⅓ ⅔ ⅕ ⅖ ⅗ ⅘ ⅙ ⅚ ⅛ ⅜ ⅝ ⅞ ⅟ Ⅰ Ⅱ Ⅲ Ⅳ Ⅴ Ⅵ Ⅶ Ⅷ Ⅸ Ⅹ Ⅺ Ⅻ Ⅼ Ⅽ Ⅾ Ⅿ ⅰ ⅱ ⅲ ⅳ ⅴ ⅵ ⅶ ⅷ ⅸ ⅹ ⅺ ⅻ ⅼ ⅽ ⅾ ⅿ ↀ ↁ ↂ
|
||||
Arrows
|
||||
← ↑ → ↓ ↔ ↕ ↖ ↗ ↘ ↙ ↚ ↛ ↜ ↝ ↞ ↟ ↠ ↡ ↢ ↣ ↤ ↥ ↦ ↧ ↨ ↩ ↪ ↫ ↬ ↭ ↮ ↯ ↰ ↱ ↲ ↳ ↴ ↵ ↶ ↷ ↸ ↹ ↺ ↻ ↼ ↽ ↾ ↿ ⇀ ⇁ ⇂ ⇃ ⇄ ⇅ ⇆ ⇇ ⇈ ⇉ ⇊ ⇋ ⇌ ⇍ ⇎ ⇏ ⇐ ⇑ ⇒ ⇓ ⇔ ⇕ ⇖ ⇗ ⇘ ⇙ ⇚ ⇛ ⇜ ⇝ ⇞ ⇟ ⇠ ⇡ ⇢ ⇣ ⇤ ⇥ ⇦ ⇧ ⇨ ⇩ ⇪
|
||||
Mathematical Operators
|
||||
∀ ∁ ∂ ∃ ∄ ∅ ∆ ∇ ∈ ∉ ∊ ∋ ∌ ∍ ∎ ∏ ∐ ∑ − ∓ ∔ ∕ ∖ ∗ ∘ ∙ √ ∛ ∜ ∝ ∞ ∟ ∠ ∡ ∢ ∣ ∤ ∥ ∦ ∧ ∨ ∩ ∪ ∫ ∬ ∭ ∮ ∯ ∰ ∱ ∲ ∳ ∴ ∵ ∶ ∷ ∸ ∹ ∺ ∻ ∼ ∽ ∾ ∿ ≀ ≁ ≂ ≃ ≄ ≅ ≆ ≇ ≈ ≉ ≊ ≋ ≌ ≍ ≎ ≏ ≐ ≑ ≒ ≓ ≔ ≕ ≖ ≗ ≘ ≙ ≚ ≛ ≜ ≝ ≞ ≟ ≠ ≡ ≢ ≣ ≤ ≥ ≦ ≧ ≨ ≩ ≪ ≫ ≬ ≭ ≮ ≯ ≰ ≱ ≲ ≳ ≴ ≵ ≶ ≷ ≸ ≹ ≺ ≻ ≼ ≽ ≾ ≿ ...
|
||||
Miscellaneous Technical
|
||||
⌀ ⌂ ⌃ ⌄ ⌅ ⌆ ⌇ ⌈ ⌉ ⌊ ⌋ ⌌ ⌍ ⌎ ⌏ ⌐ ⌑ ⌒ ⌓ ⌔ ⌕ ⌖ ⌗ ⌘ ⌙ ⌚ ⌛ ⌜ ⌝ ⌞ ⌟ ⌠ ⌡ ⌢ ⌣ ⌤ ⌥ ⌦ ⌧ ⌨ 〈 〉 ⌫ ⌬ ⌭ ⌮ ⌯ ⌰ ⌱ ⌲ ⌳ ⌴ ⌵ ⌶ ⌷ ⌸ ⌹ ⌺ ⌻ ⌼ ⌽ ⌾ ⌿ ⍀ ⍁ ⍂ ⍃ ⍄ ⍅ ⍆ ⍇ ⍈ ⍉ ⍊ ⍋ ⍌ ⍍ ⍎ ⍏ ⍐ ⍑ ⍒ ⍓ ⍔ ⍕ ⍖ ⍗ ⍘ ⍙ ⍚ ⍛ ⍜ ⍝ ⍞ ⍟ ⍠ ⍡ ⍢ ⍣ ⍤ ⍥ ⍦ ⍧ ⍨ ⍩ ⍪ ⍫ ⍬ ⍭ ⍮ ⍯ ⍰ ⍱ ⍲ ⍳ ⍴ ⍵ ⍶ ⍷ ⍸ ⍹ ⍺
|
||||
Control Pictures
|
||||
␀ ␁ ␂ ␃ ␄ ␅ ␆ ␇ ␈ ␉ ␊ ␋ ␌ ␍ ␎ ␏ ␐ ␑ ␒ ␓ ␔ ␕ ␖ ␗ ␘ ␙ ␚ ␛ ␜ ␝ ␞ ␟ ␠ ␡ ␢ ␣ 
|
||||
Optical Character Recognition
|
||||
⑀ ⑁ ⑂ ⑃ ⑄ ⑅ ⑆ ⑇ ⑈ ⑉ ⑊
|
||||
Enclosed Alphanumerics
|
||||
① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩ ⑪ ⑫ ⑬ ⑭ ⑮ ⑯ ⑰ ⑱ ⑲ ⑳ ⑴ ⑵ ⑶ ⑷ ⑸ ⑹ ⑺ ⑻ ⑼ ⑽ ⑾ ⑿ ⒀ ⒁ ⒂ ⒃ ⒄ ⒅ ⒆ ⒇ ⒈ ⒉ ⒊ ⒋ ⒌ ⒍ ⒎ ⒏ ⒐ ⒑ ⒒ ⒓ ⒔ ⒕ ⒖ ⒗ ⒘ ⒙ ⒚ ⒛ ⒜ ⒝ ⒞ ⒟ ⒠ ⒡ ⒢ ⒣ ⒤ ⒥ ⒦ ⒧ ⒨ ⒩ ⒪ ⒫ ⒬ ⒭ ⒮ ⒯ ⒰ ⒱ ⒲ ⒳ ⒴ ⒵ Ⓐ Ⓑ Ⓒ Ⓓ Ⓔ Ⓕ Ⓖ Ⓗ Ⓘ Ⓙ Ⓚ Ⓛ Ⓜ Ⓝ Ⓞ Ⓟ Ⓠ Ⓡ Ⓢ Ⓣ Ⓤ Ⓥ Ⓦ Ⓧ Ⓨ Ⓩ ⓐ ⓑ ⓒ ⓓ ⓔ ⓕ ⓖ ⓗ ⓘ ⓙ ⓚ ⓛ ⓜ ⓝ ⓞ ⓟ ...
|
||||
Box Drawing
|
||||
─ ━ │ ┃ ┄ ┅ ┆ ┇ ┈ ┉ ┊ ┋ ┌ ┍ ┎ ┏ ┐ ┑ ┒ ┓ └ ┕ ┖ ┗ ┘ ┙ ┚ ┛ ├ ┝ ┞ ┟ ┠ ┡ ┢ ┣ ┤ ┥ ┦ ┧ ┨ ┩ ┪ ┫ ┬ ┭ ┮ ┯ ┰ ┱ ┲ ┳ ┴ ┵ ┶ ┷ ┸ ┹ ┺ ┻ ┼ ┽ ┾ ┿ ╀ ╁ ╂ ╃ ╄ ╅ ╆ ╇ ╈ ╉ ╊ ╋ ╌ ╍ ╎ ╏ ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ ╭ ╮ ╯ ╰ ╱ ╲ ╳ ╴ ╵ ╶ ╷ ╸ ╹ ╺ ╻ ╼ ╽ ╾ ╿
|
||||
Block Elements
|
||||
▀ ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ ▉ ▊ ▋ ▌ ▍ ▎ ▏ ▐ ░ ▒ ▓ ▔ ▕
|
||||
Geometric Shapes
|
||||
■ □ ▢ ▣ ▤ ▥ ▦ ▧ ▨ ▩ ▪ ▫ ▬ ▭ ▮ ▯ ▰ ▱ ▲ △ ▴ ▵ ▶ ▷ ▸ ▹ ► ▻ ▼ ▽ ▾ ▿ ◀ ◁ ◂ ◃ ◄ ◅ ◆ ◇ ◈ ◉ ◊ ○ ◌ ◍ ◎ ● ◐ ◑ ◒ ◓ ◔ ◕ ◖ ◗ ◘ ◙ ◚ ◛ ◜ ◝ ◞ ◟ ◠ ◡ ◢ ◣ ◤ ◥ ◦ ◧ ◨ ◩ ◪ ◫ ◬ ◭ ◮ ◯
|
||||
Miscellaneous Symbols
|
||||
☀ ☁ ☂ ☃ ☄ ★ ☆ ☇ ☈ ☉ ☊ ☋ ☌ ☍ ☎ ☏ ☐ ☑ ☒ ☓ ☚ ☛ ☜ ☝ ☞ ☟ ☠ ☡ ☢ ☣ ☤ ☥ ☦ ☧ ☨ ☩ ☪ ☫ ☬ ☭ ☮ ☯ ☰ ☱ ☲ ☳ ☴ ☵ ☶ ☷ ☸ ☹ ☺ ☻ ☼ ☽ ☾ ☿ ♀ ♁ ♂ ♃ ♄ ♅ ♆ ♇ ♈ ♉ ♊ ♋ ♌ ♍ ♎ ♏ ♐ ♑ ♒ ♓ ♔ ♕ ♖ ♗ ♘ ♙ ♚ ♛ ♜ ♝ ♞ ♟ ♠ ♡ ♢ ♣ ♤ ♥ ♦ ♧ ♨ ♩ ♪ ♫ ♬ ♭ ♮ ♯
|
||||
Dingbats
|
||||
✁ ✂ ✃ ✄ ✆ ✇ ✈ ✉ ✌ ✍ ✎ ✏ ✐ ✑ ✒ ✓ ✔ ✕ ✖ ✗ ✘ ✙ ✚ ✛ ✜ ✝ ✞ ✟ ✠ ✡ ✢ ✣ ✤ ✥ ✦ ✧ ✩ ✪ ✫ ✬ ✭ ✮ ✯ ✰ ✱ ✲ ✳ ✴ ✵ ✶ ✷ ✸ ✹ ✺ ✻ ✼ ✽ ✾ ✿ ❀ ❁ ❂ ❃ ❄ ❅ ❆ ❇ ❈ ❉ ❊ ❋ ❍ ❏ ❐ ❑ ❒ ❖ ❘ ❙ ❚ ❛ ❜ ❝ ❞ ❡ ❢ ❣ ❤ ❥ ❦ ❧ ❶ ❷ ❸ ❹ ❺ ❻ ❼ ❽ ❾ ❿ ➀ ➁ ➂ ➃ ➄ ➅ ➆ ➇ ➈ ➉ ➊ ➋ ➌ ➍ ➎ ➏ ➐ ➑ ➒ ➓ ➔ ➘ ➙ ➚ ➛ ➜ ➝ ...
|
||||
CJK Symbols and Punctuation
|
||||
、 。 〃 〄 々 〆 〇 〈 〉 《 》 「 」 『 』 【 】 〒 〓 〔 〕 〖 〗 〘 〙 〚 〛 〜 〝 〞 〟 〠 〡 〢 〣 〤 〥 〦 〧 〨 〩 〪 〫 〬 〭 〮 〯 〰 〱 〲 〳 〴 〵 〶 〷 〿
|
||||
Hiragana
|
||||
ぁ あ ぃ い ぅ う ぇ え ぉ お か が き ぎ く ぐ け げ こ ご さ ざ し じ す ず せ ぜ そ ぞ た だ ち ぢ っ つ づ て で と ど な に ぬ ね の は ば ぱ ひ び ぴ ふ ぶ ぷ へ べ ぺ ほ ぼ ぽ ま み む め も ゃ や ゅ ゆ ょ よ ら り る れ ろ ゎ わ ゐ ゑ を ん ゔ ゙ ゚ ゛ ゜ ゝ ゞ
|
||||
Katakana
|
||||
ァ ア ィ イ ゥ ウ ェ エ ォ オ カ ガ キ ギ ク グ ケ ゲ コ ゴ サ ザ シ ジ ス ズ セ ゼ ソ ゾ タ ダ チ ヂ ッ ツ ヅ テ デ ト ド ナ ニ ヌ ネ ノ ハ バ パ ヒ ビ ピ フ ブ プ ヘ ベ ペ ホ ボ ポ マ ミ ム メ モ ャ ヤ ュ ユ ョ ヨ ラ リ ル レ ロ ヮ ワ ヰ ヱ ヲ ン ヴ ヵ ヶ ヷ ヸ ヹ ヺ ・ ー ヽ ヾ
|
||||
Bopomofo
|
||||
ㄅ ㄆ ㄇ ㄈ ㄉ ㄊ ㄋ ㄌ ㄍ ㄎ ㄏ ㄐ ㄑ ㄒ ㄓ ㄔ ㄕ ㄖ ㄗ ㄘ ㄙ ㄚ ㄛ ㄜ ㄝ ㄞ ㄟ ㄠ ㄡ ㄢ ㄣ ㄤ ㄥ ㄦ ㄧ ㄨ ㄩ ㄪ ㄫ ㄬ
|
||||
@@ -20,6 +20,7 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#undef Yield
|
||||
|
||||
API void *VReserve(size_t size) {
|
||||
void *result = (uint8_t *)VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE);
|
||||
@@ -91,6 +92,7 @@ API bool VDecommit(void *p, size_t size) {
|
||||
#endif
|
||||
|
||||
API void *SystemAllocatorProc(void *object, int kind, void *p, size_t size) {
|
||||
Unused(object);
|
||||
void *result = NULL;
|
||||
if (kind == AllocatorKind_Allocate) {
|
||||
result = malloc(size);
|
||||
@@ -124,6 +126,7 @@ thread_local Array<MemoryRecord> MemoryTrackingRecord;
|
||||
|
||||
|
||||
API void *TrackingAllocatorProc(void *object, int kind, void *p, size_t size) {
|
||||
Unused(object);
|
||||
void *result = NULL;
|
||||
|
||||
if (kind == AllocatorKind_Allocate) {
|
||||
@@ -248,6 +251,7 @@ API void PopToPos(VirtualArena *arena, size_t pos) {
|
||||
}
|
||||
|
||||
API void *ArenaAllocatorProc(void *object, int kind, void *p, size_t size) {
|
||||
Unused(p);
|
||||
if (kind == AllocatorKind_Allocate) {
|
||||
return PushSize((VirtualArena *)object, size);
|
||||
} else if (AllocatorKind_Deallocate) {
|
||||
@@ -315,6 +319,7 @@ API void Unwind(BlockArena *arena, U8 *pos) {
|
||||
}
|
||||
|
||||
API void *BlockArenaAllocatorProc(void *object, int kind, void *p, size_t size) {
|
||||
Unused(p);
|
||||
BlockArena *arena = (BlockArena *)object;
|
||||
if (kind == AllocatorKind_Allocate) {
|
||||
return PushSize(arena, size);
|
||||
@@ -28,9 +28,13 @@ API Allocator GetTrackingAllocator();
|
||||
|
||||
#define MemoryZero(x, size) memset(x, 0, size)
|
||||
#define MemoryZeroStruct(x) memset(x, 0, sizeof(*x))
|
||||
#define MemoryCopy(dst, src, size) memcpy(dst, src, size)
|
||||
#define MemoryMove(dst, src, size) memmove(dst, src, size)
|
||||
|
||||
static inline void MemoryCopy(void *dst, void *src, size_t size) {
|
||||
if (src == NULL) return;
|
||||
memcpy(dst, src, size);
|
||||
}
|
||||
|
||||
///////////////////
|
||||
// Block Arena
|
||||
///////////////////
|
||||
@@ -47,6 +51,7 @@ struct BlockArena {
|
||||
U8 *end;
|
||||
BlockArenaNode *blocks;
|
||||
Allocator allocator;
|
||||
int refs; // :CoroutineLeakCheck
|
||||
|
||||
operator Allocator() { return {BlockArenaAllocatorProc, this}; }
|
||||
};
|
||||
@@ -66,6 +71,7 @@ struct VirtualArena {
|
||||
size_t reserve;
|
||||
size_t commit;
|
||||
size_t align;
|
||||
int refs; // :CoroutineLeakCheck
|
||||
|
||||
operator Allocator() {
|
||||
void *ArenaAllocatorProc(void *object, int kind, void *p, size_t size);
|
||||
@@ -112,28 +118,32 @@ API VirtualArena *GetScratchEx(VirtualArena **conflicts, int conflict_count);
|
||||
struct Scratch {
|
||||
VirtualArena *arena;
|
||||
U64 p;
|
||||
Scratch() {arena = &ScratchArenas[0]; p = arena->len;}
|
||||
Scratch() {arena = &ScratchArenas[0]; p = arena->len; arena->refs += 1;}
|
||||
Scratch(VirtualArena *conflict) {
|
||||
VirtualArena *conf[] = {conflict};
|
||||
arena = GetScratchEx(conf, Lengthof(conf));
|
||||
p = arena->len;
|
||||
arena->refs += 1;
|
||||
}
|
||||
Scratch(VirtualArena *c1, VirtualArena *c2) {
|
||||
VirtualArena *conf[] = {c1, c2};
|
||||
arena = GetScratchEx(conf, Lengthof(conf));
|
||||
p = arena->len;
|
||||
arena->refs += 1;
|
||||
}
|
||||
Scratch(Allocator conflict) {
|
||||
VirtualArena *conf[] = {(VirtualArena *)conflict.object};
|
||||
arena = GetScratchEx(conf, Lengthof(conf));
|
||||
p = arena->len;
|
||||
arena->refs += 1;
|
||||
}
|
||||
Scratch(Allocator c1, Allocator c2) {
|
||||
VirtualArena *conf[] = {(VirtualArena *)c1.object, (VirtualArena *)c2.object};
|
||||
arena = GetScratchEx(conf, Lengthof(conf));
|
||||
p = arena->len;
|
||||
arena->refs += 1;
|
||||
}
|
||||
~Scratch() { SetLen(arena, p); }
|
||||
~Scratch() { SetLen(arena, p); arena->refs -= 1; }
|
||||
operator VirtualArena *() { return arena; }
|
||||
operator Allocator() { return *arena; }
|
||||
|
||||
@@ -211,12 +211,11 @@ void Reserve(Array<T> *arr, int64_t size) {
|
||||
|
||||
T *new_data = AllocArray(arr->allocator, T, size);
|
||||
Assert(new_data);
|
||||
memcpy(new_data, arr->data, arr->len * sizeof(T));
|
||||
Dealloc(arr->allocator, arr->data);
|
||||
|
||||
MemoryCopy(new_data, arr->data, arr->len * sizeof(T));
|
||||
if (arr->data) Dealloc(arr->allocator, arr->data);
|
||||
arr->data = new_data;
|
||||
arr->cap = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -310,6 +309,16 @@ bool Contains(Array<T> &arr, T item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
int64_t FindValueIndex(Array<T> &arr, T item) {
|
||||
for (int64_t i = 0; i < arr.len; i += 1) {
|
||||
if (arr.data[i] == item) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int64_t GetIndex(Array<T> &arr, const T &item) {
|
||||
ptrdiff_t index = (ptrdiff_t)(&item - arr.data);
|
||||
@@ -483,16 +492,16 @@ struct ReverseIter {
|
||||
friend bool operator==(const ReverseIter &a, const ReverseIter &b) { return a.data == b.data; };
|
||||
friend bool operator!=(const ReverseIter &a, const ReverseIter &b) { return a.data != b.data; };
|
||||
|
||||
ReverseIter begin() { return ReverseIter{arr->end() - 1, arr}; }
|
||||
ReverseIter end() { return ReverseIter{arr->begin() - 1, arr}; }
|
||||
ReverseIter begin() { return ReverseIter{arr->end() ? arr->end() - 1 : NULL, arr}; }
|
||||
ReverseIter end() { return ReverseIter{arr->begin() ? arr->begin() - 1 : NULL, arr}; }
|
||||
};
|
||||
|
||||
template <class T>
|
||||
ReverseIter<T> IterateInReverse(Array<T> *arr) {
|
||||
return {arr->end() - 1, &arr->slice};
|
||||
return {arr->end() ? arr->end() - 1 : NULL, &arr->slice};
|
||||
}
|
||||
template <class T>
|
||||
ReverseIter<T> IterateInReverse(Slice<T> *slice) {
|
||||
return {slice->end() - 1, slice};
|
||||
return {slice->end ? slice->end() - 1 : NULL, slice};
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
#define OS_POSIX 1
|
||||
#define OS_MAC 1
|
||||
@@ -70,12 +69,30 @@
|
||||
#define DEBUG_BUILD 1
|
||||
#endif
|
||||
|
||||
#ifndef SLOW_BUILD
|
||||
#define SLOW_BUILD 0
|
||||
#endif
|
||||
|
||||
#if SLOW_BUILD
|
||||
#define IF_SLOW_BUILD(x) x
|
||||
#else
|
||||
#define IF_SLOW_BUILD(x)
|
||||
#endif
|
||||
|
||||
#if DEBUG_BUILD
|
||||
#define IF_DEBUG(x) x
|
||||
#else
|
||||
#define IF_DEBUG(x)
|
||||
#endif
|
||||
|
||||
#if OS_WINDOWS
|
||||
#define IF_OS_WINDOWS_ELSE(x, y) x
|
||||
#define IF_OS_WINDOWS(x) x
|
||||
#else
|
||||
#define IF_OS_WINDOWS_ELSE(x, y) y
|
||||
#define IF_OS_WINDOWS(x)
|
||||
#endif
|
||||
|
||||
#if OS_WINDOWS
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
@@ -84,6 +101,7 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#undef Yield
|
||||
#define BREAK() if (IsDebuggerPresent()) {__debugbreak();}
|
||||
#elif OS_LINUX
|
||||
#define BREAK() raise(SIGTRAP)
|
||||
@@ -107,8 +125,9 @@ EM_JS(void, JS_Breakpoint, (), {
|
||||
#define MiB(x) (KiB(x) * 1024ull)
|
||||
#define GiB(x) (MiB(x) * 1024ull)
|
||||
#define TiB(x) (GiB(x) * 1024ull)
|
||||
#define Lengthof(x) ((int64_t)((sizeof(x) / sizeof((x)[0]))))
|
||||
#define SLICE_LAST INT64_MAX
|
||||
#define Lengthof(x) ((int64_t)((sizeof(x) / sizeof((x)[0]))))
|
||||
#define Unused(x) (void)(x)
|
||||
|
||||
using U8 = uint8_t;
|
||||
using U16 = uint16_t;
|
||||
@@ -122,6 +141,8 @@ using Int = S64;
|
||||
using UInt = U64;
|
||||
using Float = double;
|
||||
|
||||
#define IntMax INT64_MAX
|
||||
|
||||
template <class T>
|
||||
T Min(T a, T b) {
|
||||
if (a > b) return b;
|
||||
@@ -237,3 +258,14 @@ inline uint64_t GetRandomU64(RandomSeed *state) {
|
||||
#define STRINGIFY(x) STRINGIFY_(x)
|
||||
#define CONCAT_(a, b) a ## b
|
||||
#define CONCAT(a, b) CONCAT_(a, b)
|
||||
|
||||
Int SizeToInt(size_t size) {
|
||||
Assert(size <= (size_t)IntMax);
|
||||
return (Int)size;
|
||||
}
|
||||
|
||||
Int Strlen(const char *string) {
|
||||
size_t size = strlen(string);
|
||||
Int result = SizeToInt(size);
|
||||
return result;
|
||||
}
|
||||
@@ -18,16 +18,20 @@ typedef void OSErrorReport(const char *, ...);
|
||||
#include <poll.h>
|
||||
#include <execinfo.h>
|
||||
#include <backtrace.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
|
||||
API void (*Error)(const char *, ...);
|
||||
|
||||
struct backtrace_state *backtrace_state = NULL;
|
||||
|
||||
void BacktraceOnError(void *data, const char *msg, int errnum) {
|
||||
Unused(data);
|
||||
Error("libbacktrace error: %s (errnum: %d)\n", msg, errnum);
|
||||
}
|
||||
|
||||
int BacktraceOnPrint(void *data, uintptr_t pc, const char *filename, int lineno, const char *function) {
|
||||
Unused(data); Unused(pc);
|
||||
bool printed = false;
|
||||
if (filename != NULL) {
|
||||
char buffer[1024];
|
||||
@@ -46,6 +50,7 @@ int BacktraceOnPrint(void *data, uintptr_t pc, const char *filename, int lineno,
|
||||
}
|
||||
|
||||
void CrashHandler(int signal, siginfo_t* info, void* context) {
|
||||
Unused(signal); Unused(info); Unused(context);
|
||||
backtrace_full(backtrace_state, 2, BacktraceOnPrint, BacktraceOnError, NULL);
|
||||
exit(1);
|
||||
}
|
||||
@@ -102,7 +107,7 @@ API bool WriteFile(String path, String data) {
|
||||
FILE *f = fopen((const char *)null_term.data, "w");
|
||||
if (f) {
|
||||
size_t written = fwrite(data.data, 1, data.len, f);
|
||||
if (written == data.len) {
|
||||
if (SizeToInt(written) == data.len) {
|
||||
result = true;
|
||||
}
|
||||
fclose(f);
|
||||
@@ -254,204 +259,6 @@ API FileIter IterateFiles(Allocator alo, String path) {
|
||||
return it;
|
||||
}
|
||||
|
||||
struct UnixProcess {
|
||||
pid_t pid;
|
||||
int child_stdout_read;
|
||||
int stdin_write;
|
||||
};
|
||||
|
||||
API Array<char *> SplitCommand(Allocator allocator, String command_line) {
|
||||
Array<char *> cmd = {allocator};
|
||||
|
||||
String curr = {};
|
||||
for (int i = 0; i < command_line.len; i += 1) {
|
||||
if (command_line.data[i] == ' ') {
|
||||
if (curr.len > 0) {
|
||||
Add(&cmd, Copy(allocator, curr).data);
|
||||
curr = {};
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (curr.len == 0) {
|
||||
curr.data = command_line.data + i;
|
||||
}
|
||||
curr.len += 1;
|
||||
}
|
||||
|
||||
if (curr.len > 0) {
|
||||
Add(&cmd, Copy(allocator, curr).data);
|
||||
}
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
API Process SpawnProcess(String command_line, String working_dir, String write_stdin, Array<String> enviroment) {
|
||||
Scratch scratch;
|
||||
const int PIPE_READ = 0;
|
||||
const int PIPE_WRITE = 1;
|
||||
bool error = false;
|
||||
|
||||
working_dir = Copy(scratch, working_dir);
|
||||
chdir(working_dir.data);
|
||||
|
||||
Process process = {};
|
||||
UnixProcess *plat = (UnixProcess *)&process.platform;
|
||||
Array<char *> args = SplitCommand(scratch, command_line);
|
||||
Array<char *> env = {scratch};
|
||||
|
||||
For (enviroment) {
|
||||
Add(&env, Copy(scratch, it).data);
|
||||
}
|
||||
|
||||
int stdout_desc[2] = {};
|
||||
int stdin_desc[2] = {};
|
||||
posix_spawn_file_actions_t actions = {};
|
||||
|
||||
if (posix_spawn_file_actions_init(&actions) != 0) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_init, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
defer {
|
||||
posix_spawn_file_actions_destroy(&actions);
|
||||
};
|
||||
|
||||
if (pipe(stdout_desc) == -1) {
|
||||
Error("Libc function failed: pipe, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
defer {
|
||||
if (error) {
|
||||
close(stdout_desc[PIPE_READ]);
|
||||
close(stdout_desc[PIPE_WRITE]);
|
||||
} else {
|
||||
close(stdout_desc[PIPE_WRITE]);
|
||||
}
|
||||
};
|
||||
|
||||
if (pipe(stdin_desc) == -1) {
|
||||
Error("Libc function failed: pipe, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
defer {
|
||||
if (error) {
|
||||
close(stdin_desc[PIPE_READ]);
|
||||
close(stdin_desc[PIPE_WRITE]);
|
||||
} else {
|
||||
close(stdin_desc[PIPE_READ]);
|
||||
}
|
||||
};
|
||||
|
||||
error = posix_spawn_file_actions_addclose(&actions, stdout_desc[PIPE_READ]) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_addclose, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
error = posix_spawn_file_actions_adddup2(&actions, stdout_desc[PIPE_WRITE], STDOUT_FILENO) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_adddup2 STDOUT_FILENO, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
error = posix_spawn_file_actions_adddup2(&actions, stdout_desc[PIPE_WRITE], STDERR_FILENO) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_adddup2 STDERR_FILENO, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
error = posix_spawn_file_actions_addclose(&actions, stdin_desc[PIPE_WRITE]) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_addclose, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
error = posix_spawn_file_actions_adddup2(&actions, stdin_desc[PIPE_READ], STDIN_FILENO) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_adddup2 STDIN_FILENO, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
pid_t process_pid = 0;
|
||||
error = posix_spawnp(&process_pid, args[0], &actions, NULL, args.data, env.data) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: failed to create process\n, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
|
||||
plat->child_stdout_read = stdout_desc[PIPE_READ];
|
||||
plat->stdin_write = stdin_desc[PIPE_WRITE];
|
||||
plat->pid = process_pid;
|
||||
|
||||
if (write_stdin.len) {
|
||||
WriteStdin(&process, write_stdin);
|
||||
CloseStdin(&process);
|
||||
}
|
||||
|
||||
process.id = process_pid;
|
||||
process.is_valid = true;
|
||||
return process;
|
||||
}
|
||||
|
||||
API bool IsValid(Process *process) {
|
||||
UnixProcess *plat = (UnixProcess *)&process->platform;
|
||||
if (process->is_valid == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
pollfd p = {};
|
||||
p.fd = plat->child_stdout_read;
|
||||
p.events = POLLRDHUP | POLLERR | POLLHUP | POLLNVAL;
|
||||
int res = poll(&p, 1, 0);
|
||||
if (res > 0) {
|
||||
pid_t result = waitpid(plat->pid, &status, 0);
|
||||
Assert(result != -1);
|
||||
process->exit_code = WEXITSTATUS(status);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
API void KillProcess(Process *process) {
|
||||
Assert(process->is_valid);
|
||||
UnixProcess *plat = (UnixProcess *)process->platform;
|
||||
kill(plat->pid, SIGKILL);
|
||||
process->exit_code = -1;
|
||||
}
|
||||
|
||||
API String PollStdout(Allocator allocator, Process *process, bool force_read) {
|
||||
Assert(process->is_valid);
|
||||
UnixProcess *plat = (UnixProcess *)process->platform;
|
||||
|
||||
String result = {};
|
||||
result.data = AllocArray(allocator, char, 16 * 4096);
|
||||
|
||||
pollfd p = {};
|
||||
p.fd = plat->child_stdout_read;
|
||||
p.events = POLLIN;
|
||||
int res = poll(&p, 1, 0);
|
||||
if (res > 0 || force_read) {
|
||||
result.len = read(plat->child_stdout_read, result.data, 4 * 4096);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
API void WriteStdin(Process *process, String string) {
|
||||
if (string.len == 0) return;
|
||||
Assert(process->is_valid);
|
||||
|
||||
UnixProcess *plat = (UnixProcess *)process->platform;
|
||||
ssize_t size = write(plat->stdin_write, string.data, string.len);
|
||||
|
||||
Assert(size == string.len);
|
||||
}
|
||||
|
||||
API void CloseStdin(Process *process) {
|
||||
UnixProcess *plat = (UnixProcess *)process->platform;
|
||||
close(plat->stdin_write);
|
||||
}
|
||||
|
||||
#elif OS_WINDOWS
|
||||
|
||||
@@ -462,6 +269,7 @@ API void CloseStdin(Process *process) {
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#undef Yield
|
||||
#include <stdio.h>
|
||||
#include <intrin.h>
|
||||
|
||||
@@ -721,210 +529,6 @@ API MakeDirResult MakeDir(String path) {
|
||||
return result;
|
||||
}
|
||||
|
||||
struct Win32Process {
|
||||
HANDLE handle;
|
||||
HANDLE child_stdout_read;
|
||||
HANDLE child_stdout_write;
|
||||
HANDLE child_stdin_read;
|
||||
HANDLE child_stdin_write;
|
||||
};
|
||||
// static_assert(sizeof(Win32Process) < sizeof(Process::platform));
|
||||
|
||||
static void Win32ReportError(String msg, String cmd) {
|
||||
LPVOID lpMsgBuf;
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
|
||||
defer { LocalFree(lpMsgBuf); };
|
||||
char *buff = (char *)lpMsgBuf;
|
||||
size_t buffLen = strlen((const char *)buff);
|
||||
if (buffLen > 0 && buff[buffLen - 1] == '\n') buff[buffLen - 1] = 0;
|
||||
Error("%S: %S! %s", msg, cmd, (char *)lpMsgBuf);
|
||||
}
|
||||
|
||||
static void Win32CloseProcess(Process *process) {
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
if (p->handle != INVALID_HANDLE_VALUE) CloseHandle(p->handle);
|
||||
if (p->child_stdout_write != INVALID_HANDLE_VALUE) CloseHandle(p->child_stdout_write);
|
||||
if (p->child_stdout_read != INVALID_HANDLE_VALUE) CloseHandle(p->child_stdout_read);
|
||||
if (p->child_stdin_read != INVALID_HANDLE_VALUE) CloseHandle(p->child_stdin_read);
|
||||
if (p->child_stdin_write != INVALID_HANDLE_VALUE) CloseHandle(p->child_stdin_write);
|
||||
process->is_valid = false;
|
||||
}
|
||||
|
||||
static void Win32ProcessError(Process *process, String msg, String cmd) {
|
||||
Win32ReportError(msg, cmd);
|
||||
Win32CloseProcess(process);
|
||||
}
|
||||
|
||||
API void WriteStdin(Process *process, String string) {
|
||||
if (string.len == 0) return;
|
||||
Assert(process->is_valid);
|
||||
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
Assert(p->child_stdin_write != INVALID_HANDLE_VALUE);
|
||||
|
||||
DWORD written = 0;
|
||||
bool write_error = WriteFile(p->child_stdin_write, string.data, (DWORD)string.len, &written, NULL) == 0;
|
||||
|
||||
Assert(write_error == false);
|
||||
Assert(written == string.len);
|
||||
}
|
||||
|
||||
API void CloseStdin(Process *process) {
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
CloseHandle(p->child_stdin_write);
|
||||
p->child_stdin_write = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
API Process SpawnProcess(String command_line, String working_dir, String write_stdin, Array<String> enviroment) {
|
||||
Process process = {};
|
||||
Win32Process *p = (Win32Process *)process.platform;
|
||||
|
||||
Scratch scratch;
|
||||
command_line = Format(scratch, "/C %S", command_line);
|
||||
|
||||
p->handle = INVALID_HANDLE_VALUE;
|
||||
p->child_stdout_write = INVALID_HANDLE_VALUE;
|
||||
p->child_stdout_read = INVALID_HANDLE_VALUE;
|
||||
p->child_stdin_read = INVALID_HANDLE_VALUE;
|
||||
p->child_stdin_write = INVALID_HANDLE_VALUE;
|
||||
|
||||
SECURITY_ATTRIBUTES security_atrb = {};
|
||||
security_atrb.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
security_atrb.bInheritHandle = TRUE;
|
||||
|
||||
if (!CreatePipe(&p->child_stdout_read, &p->child_stdout_write, &security_atrb, 0)) {
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (!SetHandleInformation(p->child_stdout_read, HANDLE_FLAG_INHERIT, 0)) {
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (!CreatePipe(&p->child_stdin_read, &p->child_stdin_write, &security_atrb, 0)) {
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (!SetHandleInformation(p->child_stdin_write, HANDLE_FLAG_INHERIT, 0)) {
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
STARTUPINFOW startup = {};
|
||||
startup.cb = sizeof(STARTUPINFO);
|
||||
startup.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
|
||||
startup.hStdInput = p->child_stdin_read;
|
||||
startup.hStdOutput = p->child_stdout_write;
|
||||
startup.hStdError = p->child_stdout_write;
|
||||
startup.wShowWindow = SW_HIDE;
|
||||
|
||||
String16 cwd = ToString16(scratch, working_dir);
|
||||
String16 cmd = ToString16(scratch, command_line);
|
||||
|
||||
char *env = NULL;
|
||||
if (enviroment.len) {
|
||||
Int size = GetSize(enviroment) + enviroment.len + 1;
|
||||
env = AllocArray(scratch, char, size);
|
||||
Int i = 0;
|
||||
For(enviroment) {
|
||||
MemoryCopy(env + i, it.data, it.len);
|
||||
i += it.len;
|
||||
|
||||
env[i++] = 0;
|
||||
}
|
||||
env[i++] = 0;
|
||||
}
|
||||
|
||||
DWORD dwCreationFlags = 0;
|
||||
BOOL bInheritHandles = TRUE;
|
||||
|
||||
PROCESS_INFORMATION info = {};
|
||||
if (!CreateProcessW(L"c:\\windows\\system32\\cmd.exe", (wchar_t *)cmd.data, 0, 0, bInheritHandles, dwCreationFlags, env, (wchar_t *)cwd.data, &startup, &info)) {
|
||||
Win32ProcessError(&process, "failed to create process", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
// Close handles to the stdin and stdout pipes no longer needed by the child process.
|
||||
// If they are not explicitly closed, there is no way to recognize that the child process has ended.
|
||||
CloseHandle(info.hThread);
|
||||
CloseHandle(p->child_stdin_read);
|
||||
CloseHandle(p->child_stdout_write);
|
||||
p->child_stdin_read = INVALID_HANDLE_VALUE;
|
||||
p->child_stdout_write = INVALID_HANDLE_VALUE;
|
||||
|
||||
p->handle = info.hProcess;
|
||||
process.is_valid = true;
|
||||
process.id = (int64_t)p->handle;
|
||||
|
||||
if (write_stdin.len) {
|
||||
WriteStdin(&process, write_stdin);
|
||||
CloseStdin(&process);
|
||||
}
|
||||
|
||||
return process;
|
||||
}
|
||||
|
||||
API bool IsValid(Process *process) {
|
||||
if (process->is_valid == false) return false;
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
|
||||
if (WaitForSingleObject(p->handle, 0) != WAIT_OBJECT_0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DWORD exit_code;
|
||||
bool get_exit_code_failed = GetExitCodeProcess(p->handle, &exit_code) == 0;
|
||||
if (get_exit_code_failed) {
|
||||
exit_code = -1;
|
||||
}
|
||||
|
||||
process->exit_code = (int)exit_code;
|
||||
Win32CloseProcess(process);
|
||||
return false;
|
||||
}
|
||||
|
||||
API void KillProcess(Process *process) {
|
||||
Assert(process->is_valid);
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
|
||||
bool terminate_process_error = TerminateProcess(p->handle, -1) == 0;
|
||||
if (terminate_process_error) {
|
||||
Assert(0);
|
||||
}
|
||||
Win32CloseProcess(process);
|
||||
process->exit_code = -1;
|
||||
}
|
||||
|
||||
API String PollStdout(Allocator allocator, Process *process, bool force_read) {
|
||||
Assert(process->is_valid);
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
|
||||
DWORD bytes_avail = 0;
|
||||
bool peek_error = PeekNamedPipe(p->child_stdout_read, NULL, 0, NULL, &bytes_avail, NULL) == 0;
|
||||
if (peek_error) {
|
||||
return {};
|
||||
} else if (bytes_avail == 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
size_t buffer_size = ClampTop(bytes_avail, (DWORD)(4096 * 16));
|
||||
char *buffer = AllocArray(allocator, char, buffer_size);
|
||||
|
||||
DWORD bytes_read = 0;
|
||||
bool read_error = ReadFile(p->child_stdout_read, buffer, (DWORD)buffer_size, &bytes_read, 0) == 0;
|
||||
if (read_error) {
|
||||
Win32ReportError("Failed to read the stdout of child process", "ReadFile");
|
||||
Dealloc(allocator, buffer);
|
||||
Win32CloseProcess(process);
|
||||
return {};
|
||||
}
|
||||
|
||||
String result = {buffer, bytes_read};
|
||||
return result;
|
||||
}
|
||||
|
||||
#elif OS_WASM
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -1118,56 +722,30 @@ API FileIter IterateFiles(Allocator alo, String path) {
|
||||
return it;
|
||||
}
|
||||
|
||||
API Array<char *> SplitCommand(Allocator allocator, String command_line) {
|
||||
Array<char *> cmd = {allocator};
|
||||
|
||||
String curr = {};
|
||||
for (int i = 0; i < command_line.len; i += 1) {
|
||||
if (command_line.data[i] == ' ') {
|
||||
if (curr.len > 0) {
|
||||
Add(&cmd, Copy(allocator, curr).data);
|
||||
curr = {};
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (curr.len == 0) {
|
||||
curr.data = command_line.data + i;
|
||||
}
|
||||
curr.len += 1;
|
||||
}
|
||||
|
||||
if (curr.len > 0) {
|
||||
Add(&cmd, Copy(allocator, curr).data);
|
||||
}
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
API Process SpawnProcess(String command_line, String working_dir, String write_stdin, Array<String> enviroment) {
|
||||
return {};
|
||||
}
|
||||
|
||||
API bool IsValid(Process *process) {
|
||||
return false;
|
||||
}
|
||||
|
||||
API void KillProcess(Process *process) {
|
||||
}
|
||||
|
||||
API String PollStdout(Allocator allocator, Process *process, bool force_read) {
|
||||
return {};
|
||||
}
|
||||
|
||||
API void WriteStdin(Process *process, String string) {
|
||||
return;
|
||||
}
|
||||
|
||||
API void CloseStdin(Process *process) {
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
API double GetTimeSeconds() {
|
||||
return GetTimeMicros() / 1000000.0;
|
||||
}
|
||||
|
||||
API String WriteTempFile(Allocator allocator, String data) {
|
||||
Scratch scratch(allocator);
|
||||
|
||||
#if OS_WINDOWS
|
||||
int buffer_len = MAX_PATH+1;
|
||||
char16_t *buffer = AllocArray(scratch, char16_t, buffer_len);
|
||||
Assert(sizeof(char16_t) == sizeof(wchar_t));
|
||||
DWORD result = GetTempPath2W(buffer_len, (LPWSTR)buffer);
|
||||
Assert(result != 0);
|
||||
String16 temp16 = {buffer, result};
|
||||
NormalizePathInPlace(temp16);
|
||||
String temp_directory = ToString(allocator, temp16);
|
||||
#else
|
||||
String temp_directory = "/tmp";
|
||||
#endif
|
||||
|
||||
String temp_filename = Format(allocator, "%S/temp%llu", temp_directory, GetTimeNanos());
|
||||
bool done = WriteFile(temp_filename, data);
|
||||
Assert(done);
|
||||
return temp_filename;
|
||||
}
|
||||
@@ -32,23 +32,11 @@ bool IsFile(String path);
|
||||
String GetWorkingDir(Allocator arena);
|
||||
bool IsAbsolute(String path);
|
||||
int64_t GetFileModTime(String file);
|
||||
String WriteTempFile(Allocator allocator, String data);
|
||||
|
||||
|
||||
struct Process {
|
||||
bool is_valid;
|
||||
int exit_code;
|
||||
char platform[6 * 8];
|
||||
int64_t id;
|
||||
|
||||
int64_t view_id; // text editor view
|
||||
bool scroll_to_end;
|
||||
};
|
||||
|
||||
Process SpawnProcess(String command_line, String working_dir, String write_stdin = {}, Array<String> enviroment = {});
|
||||
bool IsValid(Process *process);
|
||||
void KillProcess(Process *process);
|
||||
String PollStdout(Allocator allocator, Process *process, bool force_read);
|
||||
void WriteStdin(Process *process, String string);
|
||||
void CloseStdin(Process *process);
|
||||
double GetTimeMicros(void);
|
||||
|
||||
enum MakeDirResult {
|
||||
@@ -58,4 +46,4 @@ enum MakeDirResult {
|
||||
MakeDirResult_ErrorOther,
|
||||
};
|
||||
|
||||
MakeDirResult MakeDir(String path);
|
||||
MakeDirResult MakeDir(String path);
|
||||
@@ -188,7 +188,7 @@ API String Copy(Allocator allocator, String string) {
|
||||
}
|
||||
|
||||
API String Copy(Allocator allocator, char *string) {
|
||||
return Copy(allocator, {string, (int64_t)strlen(string)});
|
||||
return Copy(allocator, {string, Strlen(string)});
|
||||
}
|
||||
|
||||
API void NormalizePathInPlace(String s) {
|
||||
@@ -5,9 +5,9 @@ struct String {
|
||||
int64_t len;
|
||||
|
||||
String() = default;
|
||||
String(char *s) : data(s), len(strlen(s)) {}
|
||||
String(char *s) : data(s), len(Strlen(s)) {}
|
||||
String(char *s, int64_t l) : data((char *)s), len(l) {}
|
||||
String(const char *s) : data((char *)s), len(strlen((char *)s)) {}
|
||||
String(const char *s) : data((char *)s), len(Strlen((char *)s)) {}
|
||||
String(const char *s, int64_t l) : data((char *)s), len(l) {}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ API bool IsDigit(char16_t a) {
|
||||
}
|
||||
|
||||
API bool IsHexDigit(char16_t a) {
|
||||
bool result = a >= u'0' && a <= u'9' || a == 'a' || a == 'b' || a == 'c' || a == 'd' || a == 'e' || a == 'f';
|
||||
bool result = (a >= u'0' && a <= u'9') || a == 'a' || a == 'b' || a == 'c' || a == 'd' || a == 'e' || a == 'f';
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
#define BUFFER_DEBUG 0
|
||||
#define BUFFER_DEBUG SLOW_BUILD
|
||||
|
||||
API bool AreEqual(XY a, XY b) {
|
||||
bool result = a.x == b.x && a.y == b.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
API Range MakeRange(Int a, Int b) {
|
||||
Range result = {Min(a, b), Max(a, b)};
|
||||
@@ -59,11 +64,6 @@ API Range GetBufferEndAsRange(Buffer *buffer) {
|
||||
return result;
|
||||
}
|
||||
|
||||
API Range GetBufferBeginAsRange(Buffer *buffer) {
|
||||
Range result = {0, 0};
|
||||
return result;
|
||||
}
|
||||
|
||||
API Range GetRange(Buffer *buffer) {
|
||||
Range result = {0, buffer->len};
|
||||
return result;
|
||||
@@ -226,11 +226,9 @@ API String16 GetLineStringWithoutNL(Buffer *buffer, Int line) {
|
||||
API Int PosToLine(Buffer *buffer, Int pos) {
|
||||
Add(&buffer->line_starts, buffer->len + 1);
|
||||
|
||||
// binary search
|
||||
Int low = 0;
|
||||
|
||||
// -2 here because we use 2 indices and combine them into one line range so we
|
||||
// don't want to access last item since that would index past array.
|
||||
Int low = 0;
|
||||
Int high = buffer->line_starts.len - 2;
|
||||
Int result = 0;
|
||||
|
||||
@@ -307,7 +305,7 @@ API Int GetWordEnd(Buffer *buffer, Int pos) {
|
||||
}
|
||||
|
||||
API bool IsLoadWord(char16_t w) {
|
||||
bool result = w == u'-' || w == u'/' || w == u'\\' || w == u':' || w == u'$' || w == u'_' || w == u'.' || w == u'!' || w == u'@';
|
||||
bool result = w == u'-' || w == u'/' || w == u'\\' || w == u':' || w == u'$' || w == u'_' || w == u'.' || w == u'!' || w == u'@' || w == '{' || w == '}';
|
||||
if (!result) {
|
||||
result = !(IsSymbol(w) || IsWhitespace(w));
|
||||
}
|
||||
@@ -417,8 +415,7 @@ API Int OnCharClassBoundary_GetNextWordEnd(Buffer *buffer, Int pos) {
|
||||
}
|
||||
|
||||
API Int OnCharClassBoundary_GetPrevWordStart(Buffer *buffer, Int pos) {
|
||||
pos = Clamp(pos, (Int)0, buffer->len);
|
||||
Int i = pos - 1;
|
||||
Int i = Clamp(pos - 1, (Int)0, buffer->len);
|
||||
|
||||
if (i >= 0 && GetCharClass(buffer->str[i]) == CharClass_Whitespace) {
|
||||
i -= 1;
|
||||
@@ -464,10 +461,6 @@ API Int GetBufferEnd(Buffer *buffer) {
|
||||
return buffer->len;
|
||||
}
|
||||
|
||||
API Int GetBufferStart(Buffer *buffer) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
API Int GetNextEmptyLineStart(Buffer *buffer, Int pos) {
|
||||
Int result = pos;
|
||||
Int next_line = PosToLine(buffer, pos) + 1;
|
||||
@@ -666,6 +659,9 @@ void UpdateLines(Buffer *buffer, Range range, String16 string) {
|
||||
}
|
||||
|
||||
void RawValidateLineStarts(Buffer *buffer) {
|
||||
if (buffer->no_line_starts) {
|
||||
return;
|
||||
}
|
||||
Int line = 0;
|
||||
for (Int i = 0; i < buffer->len; i += 1) {
|
||||
Int l = PosToLine(buffer, i);
|
||||
@@ -740,7 +736,6 @@ inline bool MergeSortCompare(Edit *EntryA, Edit *EntryB) {
|
||||
|
||||
template<class T>
|
||||
void MergeSort(int64_t Count, T *First, T *Temp) {
|
||||
ProfileFunction();
|
||||
// SortKey = range.min
|
||||
if (Count == 1) {
|
||||
// NOTE(casey): No work to do.
|
||||
@@ -793,7 +788,9 @@ void MergeSort(int64_t Count, T *First, T *Temp) {
|
||||
API void ApplyEditsMultiCursor(Buffer *buffer, Array<Edit> edits) {
|
||||
ProfileFunction();
|
||||
#if BUFFER_DEBUG
|
||||
Assert(buffer->line_starts.len);
|
||||
if (buffer->no_line_starts == false) {
|
||||
Assert(buffer->line_starts.len);
|
||||
}
|
||||
Assert(edits.len);
|
||||
For(edits) {
|
||||
Assert(it.range.min >= 0);
|
||||
@@ -818,7 +815,9 @@ API void ApplyEditsMultiCursor(Buffer *buffer, Array<Edit> edits) {
|
||||
// We need to sort from lowest to highest based on range.min
|
||||
Scratch scratch(buffer->line_starts.allocator);
|
||||
Array<Edit> edits_copy = TightCopy(scratch, edits);
|
||||
if (edits.len > 1) MergeSort(edits.len, edits_copy.data, edits.data);
|
||||
if (edits.len > 1) {
|
||||
MergeSort(edits.len, edits_copy.data, edits.data);
|
||||
}
|
||||
edits = edits_copy;
|
||||
|
||||
#if BUFFER_DEBUG
|
||||
@@ -893,53 +892,61 @@ void SaveHistoryBeforeApplyEdits(Buffer *buffer, Array<HistoryEntry> *stack, Arr
|
||||
API void RedoEdit(Buffer *buffer, Array<Caret> *carets) {
|
||||
ProfileFunction();
|
||||
if (buffer->no_history) return;
|
||||
if (buffer->redo_stack.len <= 0) return;
|
||||
|
||||
HistoryEntry entry = Pop(&buffer->redo_stack);
|
||||
for (;buffer->redo_stack.len > 0;) {
|
||||
HistoryEntry entry = Pop(&buffer->redo_stack);
|
||||
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->undo_stack, *carets);
|
||||
e->time = entry.time;
|
||||
SaveHistoryBeforeApplyEdits(buffer, &buffer->undo_stack, entry.edits);
|
||||
ApplyEditsMultiCursor(buffer, entry.edits);
|
||||
|
||||
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->undo_stack, *carets);
|
||||
e->time = entry.time;
|
||||
SaveHistoryBeforeApplyEdits(buffer, &buffer->undo_stack, entry.edits);
|
||||
ApplyEditsMultiCursor(buffer, entry.edits);
|
||||
Dealloc(carets);
|
||||
*carets = entry.carets;
|
||||
|
||||
Dealloc(carets);
|
||||
*carets = entry.carets;
|
||||
Allocator sys_allocator = GetSystemAllocator();
|
||||
For(entry.edits) Dealloc(sys_allocator, it.string.data);
|
||||
Dealloc(&entry.edits);
|
||||
|
||||
Allocator sys_allocator = GetSystemAllocator();
|
||||
For(entry.edits) Dealloc(sys_allocator, it.string.data);
|
||||
Dealloc(&entry.edits);
|
||||
|
||||
if (buffer->redo_stack.len > 0) {
|
||||
HistoryEntry *next = GetLast(buffer->redo_stack);
|
||||
if ((next->time - entry.time) <= UndoMergeTime) {
|
||||
RedoEdit(buffer, carets);
|
||||
if (buffer->redo_stack.len > 0) {
|
||||
HistoryEntry *next = GetLast(buffer->redo_stack);
|
||||
if ((next->time - entry.time) <= UndoMergeTime) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
API void UndoEdit(Buffer *buffer, Array<Caret> *carets) {
|
||||
ProfileFunction();
|
||||
if (buffer->no_history) return;
|
||||
if (buffer->undo_stack.len <= 0) return;
|
||||
|
||||
HistoryEntry entry = Pop(&buffer->undo_stack);
|
||||
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->redo_stack, *carets);
|
||||
e->time = entry.time;
|
||||
SaveHistoryBeforeApplyEdits(buffer, &buffer->redo_stack, entry.edits);
|
||||
ApplyEditsMultiCursor(buffer, entry.edits);
|
||||
static bool warning_reported;
|
||||
for (int i = 0;buffer->undo_stack.len > 0; i += 1) {
|
||||
HistoryEntry entry = Pop(&buffer->undo_stack);
|
||||
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->redo_stack, *carets);
|
||||
e->time = entry.time;
|
||||
SaveHistoryBeforeApplyEdits(buffer, &buffer->redo_stack, entry.edits);
|
||||
ApplyEditsMultiCursor(buffer, entry.edits);
|
||||
|
||||
Dealloc(carets);
|
||||
*carets = entry.carets;
|
||||
Dealloc(carets);
|
||||
*carets = entry.carets;
|
||||
|
||||
Allocator sys_allocator = GetSystemAllocator();
|
||||
For(entry.edits) Dealloc(sys_allocator, it.string.data);
|
||||
Dealloc(&entry.edits);
|
||||
Allocator sys_allocator = GetSystemAllocator();
|
||||
For(entry.edits) Dealloc(sys_allocator, it.string.data);
|
||||
Dealloc(&entry.edits);
|
||||
|
||||
if (buffer->undo_stack.len > 0) {
|
||||
HistoryEntry *next = GetLast(buffer->undo_stack);
|
||||
if ((entry.time - next->time) <= UndoMergeTime) {
|
||||
UndoEdit(buffer, carets);
|
||||
if (i > 1000 && !warning_reported) {
|
||||
ReportConsolef("WARNING: Undoing more then 1000 edits at once, optimizations is needed I think, too much memory usage?");
|
||||
warning_reported = true;
|
||||
}
|
||||
if (buffer->undo_stack.len > 0) {
|
||||
HistoryEntry *next = GetLast(buffer->undo_stack);
|
||||
if ((entry.time - next->time) <= UndoMergeTime) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1026,7 +1033,9 @@ API void AdjustCarets(Array<Edit> edits, Array<Caret> *carets) {
|
||||
for (Int i = 0; i < carets->len; i += 1) carets->data[i] = new_carets[i];
|
||||
}
|
||||
|
||||
API void EndEdit(Buffer *buffer, Array<Edit> *edits, Array<Caret> *carets, bool kill_selection) {
|
||||
constexpr bool EndEdit_KillSelection = true;
|
||||
constexpr bool EndEdit_SkipFixingCaretsIWantToDoThatMyself = true;
|
||||
API void EndEdit(Buffer *buffer, Array<Edit> *edits, Array<Caret> *carets, bool kill_selection, bool skip_fixing_carets_user_will_do_that_himself = false) {
|
||||
ProfileFunction();
|
||||
|
||||
{
|
||||
@@ -1045,6 +1054,10 @@ API void EndEdit(Buffer *buffer, Array<Edit> *edits, Array<Caret> *carets, bool
|
||||
Assert(buffer->edit_phase == 2);
|
||||
buffer->edit_phase -= 2;
|
||||
|
||||
if (edits->len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if BUFFER_DEBUG
|
||||
if (buffer->no_history == false) {
|
||||
HistoryEntry *entry = GetLast(buffer->undo_stack);
|
||||
@@ -1056,40 +1069,42 @@ API void EndEdit(Buffer *buffer, Array<Edit> *edits, Array<Caret> *carets, bool
|
||||
}
|
||||
#endif
|
||||
|
||||
// Adjust carets
|
||||
// this one also moves the carets forward if they are aligned with edit
|
||||
//
|
||||
Scratch scratch;
|
||||
Array<Caret> new_carets = TightCopy(scratch, *carets);
|
||||
ForItem(edit, *edits) {
|
||||
Int remove_size = GetSize(edit.range);
|
||||
Int insert_size = edit.string.len;
|
||||
Int offset = insert_size - remove_size;
|
||||
if (skip_fixing_carets_user_will_do_that_himself == false) {
|
||||
// Adjust carets
|
||||
// this one also moves the carets forward if they are aligned with edit
|
||||
//
|
||||
Scratch scratch;
|
||||
Array<Caret> new_carets = TightCopy(scratch, *carets);
|
||||
ForItem(edit, *edits) {
|
||||
Int remove_size = GetSize(edit.range);
|
||||
Int insert_size = edit.string.len;
|
||||
Int offset = insert_size - remove_size;
|
||||
|
||||
for (Int i = 0; i < carets->len; i += 1) {
|
||||
Caret &old_cursor = carets->data[i];
|
||||
Caret &new_cursor = new_carets.data[i];
|
||||
|
||||
if (old_cursor.range.min == edit.range.min) {
|
||||
new_cursor.range.min += insert_size;
|
||||
} else if (old_cursor.range.min > edit.range.min) {
|
||||
new_cursor.range.min += offset;
|
||||
}
|
||||
|
||||
if (old_cursor.range.max == edit.range.max) {
|
||||
new_cursor.range.max += insert_size;
|
||||
} else if (old_cursor.range.max > edit.range.max) {
|
||||
new_cursor.range.max += offset;
|
||||
}
|
||||
|
||||
Assert(new_cursor.range.max >= new_cursor.range.min);
|
||||
}
|
||||
}
|
||||
|
||||
for (Int i = 0; i < carets->len; i += 1) {
|
||||
Caret &old_cursor = carets->data[i];
|
||||
Caret &new_cursor = new_carets.data[i];
|
||||
|
||||
if (old_cursor.range.min == edit.range.min) {
|
||||
new_cursor.range.min += insert_size;
|
||||
} else if (old_cursor.range.min > edit.range.min) {
|
||||
new_cursor.range.min += offset;
|
||||
carets->data[i] = new_carets[i];
|
||||
if (kill_selection) {
|
||||
carets->data[i].range.max = carets->data[i].range.min;
|
||||
}
|
||||
|
||||
if (old_cursor.range.max == edit.range.max) {
|
||||
new_cursor.range.max += insert_size;
|
||||
} else if (old_cursor.range.max > edit.range.max) {
|
||||
new_cursor.range.max += offset;
|
||||
}
|
||||
|
||||
Assert(new_cursor.range.max >= new_cursor.range.min);
|
||||
}
|
||||
}
|
||||
|
||||
for (Int i = 0; i < carets->len; i += 1) {
|
||||
carets->data[i] = new_carets[i];
|
||||
if (kill_selection) {
|
||||
carets->data[i].range.max = carets->data[i].range.min;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1137,14 +1152,17 @@ API void InitBuffer(Allocator allocator, Buffer *buffer, BufferID id = {}, Strin
|
||||
if (!buffer->no_line_starts) {
|
||||
Add(&buffer->line_starts, (Int)0);
|
||||
}
|
||||
if (DebugTraceBufferInits) printf("InitBuffer %.*s %p\n", (int)name.len, name.data, buffer->data);
|
||||
}
|
||||
|
||||
API void DeinitBuffer(Buffer *buffer) {
|
||||
Allocator allocator = buffer->line_starts.allocator;
|
||||
Dealloc(allocator, buffer->data);
|
||||
Dealloc(&buffer->commands);
|
||||
Dealloc(&buffer->line_starts);
|
||||
DeallocHistoryArray(&buffer->undo_stack);
|
||||
DeallocHistoryArray(&buffer->redo_stack);
|
||||
if (DebugTraceBufferInits) printf("DeinitBuffer %.*s %p\n", (int)buffer->name.len, buffer->name.data, buffer->data);
|
||||
}
|
||||
|
||||
// Indexing starts from 0 not 1 because this routine creates also the zero buffer
|
||||
@@ -1225,7 +1243,7 @@ void RunBufferTest() {
|
||||
Assert(buffer.data[15] == L'\n');
|
||||
Assert(buffer.data[16] == L't');
|
||||
|
||||
RawReplaceText(&buffer, GetBufferBeginAsRange(&buffer), u"Things as is\nand stuff\n");
|
||||
RawReplaceText(&buffer, {}, u"Things as is\nand stuff\n");
|
||||
Assert(buffer.line_starts.len == 4);
|
||||
Assert(PosToLine(&buffer, 12) == 0);
|
||||
Assert(buffer.data[12] == L'\n');
|
||||
@@ -1240,7 +1258,7 @@ void RunBufferTest() {
|
||||
Assert(PosToLine(&buffer, 39) == 3);
|
||||
Assert(buffer.data[39] == L't');
|
||||
|
||||
RawReplaceText(&buffer, GetBufferBeginAsRange(&buffer), u"a");
|
||||
RawReplaceText(&buffer, {}, u"a");
|
||||
Assert(buffer.line_starts.len == 4);
|
||||
Assert(PosToLine(&buffer, 13) == 0);
|
||||
Assert(PosToLine(&buffer, 14) == 1);
|
||||
@@ -1309,7 +1327,7 @@ void RunBufferTest() {
|
||||
AddEdit(&edits, {0, 7}, u"t");
|
||||
AddEdit(&edits, {8, 9}, u"T");
|
||||
AddEdit(&edits, GetBufferEndAsRange(&buffer), u"\nnewThing");
|
||||
EndEdit(&buffer, &edits, &carets, KILL_SELECTION);
|
||||
EndEdit(&buffer, &edits, &carets, EndEdit_KillSelection);
|
||||
String16 s = GetString(&buffer);
|
||||
Assert(s == u"t\nThings\nnewThing");
|
||||
DeinitBuffer(&buffer);
|
||||
@@ -1333,7 +1351,7 @@ void RunBufferTest() {
|
||||
AddEdit(&edits, {0, 7}, u"t");
|
||||
AddEdit(&edits, {8, 9}, u"T");
|
||||
AddEdit(&edits, GetBufferEndAsRange(&buffer), u"\nnewThing");
|
||||
EndEdit(&buffer, &edits, &carets, KILL_SELECTION);
|
||||
EndEdit(&buffer, &edits, &carets, EndEdit_KillSelection);
|
||||
String16 s = GetString(&buffer);
|
||||
Assert(s == u"t\nThings\nnewThing");
|
||||
Assert(buffer.line_starts.len == 0);
|
||||
@@ -1354,14 +1372,28 @@ BufferID AllocBufferID(Buffer *buffer) {
|
||||
}
|
||||
|
||||
Buffer *GetBuffer(BufferID id, Buffer *default_buffer = Buffers[0]) {
|
||||
For(Buffers) {
|
||||
if (it->id == id) return it;
|
||||
Int left = 0;
|
||||
Int right = Buffers.len - 1;
|
||||
Buffer *result = default_buffer;
|
||||
|
||||
while (left <= right) {
|
||||
Int mid = left + (right - left) / 2;
|
||||
Buffer *it = Buffers[mid];
|
||||
if (it->id == id) {
|
||||
result = it;
|
||||
break;
|
||||
} else if (it->id.id < id.id) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
return default_buffer;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Buffer *GetBuffer(String name, Buffer *default_buffer = Buffers[0]) {
|
||||
For(Buffers) {
|
||||
For (Buffers) {
|
||||
if (it->name == name) return it;
|
||||
}
|
||||
return default_buffer;
|
||||
@@ -1404,29 +1436,6 @@ String GetUniqueBufferName(String working_dir, String prepend_name, String exten
|
||||
return buffer_name;
|
||||
}
|
||||
|
||||
void InitBuffers() {
|
||||
Allocator sys_allocator = GetSystemAllocator();
|
||||
Scratch scratch;
|
||||
Buffer *null_buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "logs", ""));
|
||||
null_buffer->special = true;
|
||||
View *null_view = CreateView(null_buffer->id);
|
||||
null_view->special = true;
|
||||
Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID);
|
||||
TraceBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "trace"));
|
||||
TraceBuffer->special = true;
|
||||
TraceBuffer->no_history = true;
|
||||
GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "gc"));
|
||||
GCInfoBuffer->special = true;
|
||||
GCInfoBuffer->no_history = true;
|
||||
EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "events"));
|
||||
EventBuffer->no_history = true;
|
||||
EventBuffer->special = true;
|
||||
Buffer *search_project = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "search_project"));
|
||||
search_project->no_history = true;
|
||||
search_project->special = true;
|
||||
SearchProjectBufferID = search_project->id;
|
||||
}
|
||||
|
||||
Int ConvertUTF8ToUTF16UnixLine(String string, char16_t *buffer, Int buffer_cap) {
|
||||
if (string.len == 0) {
|
||||
return 0;
|
||||
@@ -1441,7 +1450,7 @@ Int ConvertUTF8ToUTF16UnixLine(String string, char16_t *buffer, Int buffer_cap)
|
||||
}
|
||||
if (string.data[i] == '\t') {
|
||||
// @WARNING: DONT INCREASE THE SIZE CARELESSLY, WE NEED TO ADJUST BUFFER SIZE
|
||||
for (Int i = 0; i < 4; i += 1) buffer[buffer_len++] = u' ';
|
||||
for (Int ii = 0; ii < 4; ii += 1) buffer[buffer_len++] = u' ';
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
@@ -1500,8 +1509,10 @@ Buffer *BufferOpenFile(String path) {
|
||||
buffer = CreateBuffer(sys_allocator, path);
|
||||
} else if (IsDir(path)) {
|
||||
buffer = CreateBuffer(sys_allocator, path);
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
buffer->is_dir = true;
|
||||
buffer->temp = true;
|
||||
#endif
|
||||
} else {
|
||||
String string = ReadFile(scratch, path);
|
||||
buffer = CreateBuffer(sys_allocator, path, string.len * 4 + 4096);
|
||||
@@ -1513,28 +1524,12 @@ Buffer *BufferOpenFile(String path) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool BufferIsReferenced(BufferID buffer_id) {
|
||||
if (buffer_id == NullBufferID) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (FindView(buffer_id, NULL)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ReopenBuffer(Buffer *buffer) {
|
||||
Scratch scratch;
|
||||
if (buffer->is_dir) {
|
||||
ResetBuffer(buffer);
|
||||
RawAppendf(buffer, "..\n");
|
||||
for (FileIter it = IterateFiles(scratch, buffer->name); IsValid(it); Advance(&it)) {
|
||||
RawAppendf(buffer, "%S\n", it.filename);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
if (buffer->is_dir) { InsertDirectoryNavigation(buffer); return; }
|
||||
#endif
|
||||
|
||||
String string = ReadFile(scratch, buffer->name);
|
||||
if (string.len == 0) {
|
||||
@@ -1548,7 +1543,13 @@ void ReopenBuffer(Buffer *buffer) {
|
||||
buffer->dirty = false;
|
||||
}
|
||||
|
||||
void BasicSaveBuffer(Buffer *buffer) {
|
||||
void SaveBuffer(Buffer *buffer) {
|
||||
ProfileFunction();
|
||||
|
||||
if (TrimTrailingWhitespace) {
|
||||
TrimWhitespace(buffer, false);
|
||||
}
|
||||
|
||||
Scratch scratch;
|
||||
String string = AllocCharString(scratch, buffer);
|
||||
bool success = WriteFile(buffer->name, string);
|
||||
@@ -1558,14 +1559,37 @@ void BasicSaveBuffer(Buffer *buffer) {
|
||||
buffer->dirty = false;
|
||||
buffer->temp = false;
|
||||
} else {
|
||||
ReportWarningf("Failed to save file with name: %S", buffer->name);
|
||||
ReportErrorf("Failed to save file with name: %S", buffer->name);
|
||||
}
|
||||
}
|
||||
|
||||
void SaveBuffer(Buffer *buffer) {
|
||||
if (TrimTrailingWhitespace) {
|
||||
TrimWhitespace(buffer);
|
||||
void SaveAll() {
|
||||
For(Buffers) {
|
||||
// NOTE: file_mod_time is only set when buffer got read or written to disk already so should be saved
|
||||
if (it->file_mod_time && it->dirty) {
|
||||
SaveBuffer(it);
|
||||
}
|
||||
}
|
||||
|
||||
BasicSaveBuffer(buffer);
|
||||
}
|
||||
|
||||
String GetDirectory(Buffer *buffer) {
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
if (buffer->is_dir) {
|
||||
return buffer->name;
|
||||
}
|
||||
#endif
|
||||
String result = ChopLastSlash(buffer->name);
|
||||
return result;
|
||||
}
|
||||
|
||||
void TryReopeningWhenModified(Buffer *it) {
|
||||
if (it->file_mod_time) {
|
||||
int64_t new_file_mod_time = GetFileModTime(it->name);
|
||||
if (it->file_mod_time != new_file_mod_time) {
|
||||
it->changed_on_disk = true;
|
||||
if (it->dirty == false) {
|
||||
ReopenBuffer(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
259
src/commands.cpp
Normal file
259
src/commands.cpp
Normal file
@@ -0,0 +1,259 @@
|
||||
void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string) {
|
||||
View *view = GetViewForBuffer(buffer);
|
||||
Array<Caret> carets = Copy(GetSystemAllocator(), view->carets);
|
||||
|
||||
Scratch scratch;
|
||||
SelectRange(view, range);
|
||||
ReplaceEx(scratch, view, string);
|
||||
|
||||
Dealloc(&view->carets);
|
||||
view->carets = carets;
|
||||
}
|
||||
|
||||
// @todo: revamp interface since it scrolls ALL VIEWS??? or maybe not??
|
||||
void Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line) {
|
||||
Scratch scratch;
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
|
||||
struct ViewInfo {
|
||||
View *view;
|
||||
Array<Caret> carets;
|
||||
bool scroll_to_end;
|
||||
};
|
||||
|
||||
Array<ViewInfo> view_info = {scratch};
|
||||
ForItem(it_view, Views) {
|
||||
if (it_view->active_buffer != buffer->id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ViewInfo vi = {it_view};
|
||||
if (scroll_to_end_if_cursor_on_last_line) {
|
||||
Int line = PosToLine(buffer, GetFront(it_view->carets[0]));
|
||||
if (line == buffer->line_starts.len - 1) {
|
||||
vi.scroll_to_end = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!vi.scroll_to_end) {
|
||||
vi.carets = Copy(GetSystemAllocator(), it_view->carets);
|
||||
}
|
||||
|
||||
Add(&view_info, vi);
|
||||
}
|
||||
|
||||
SelectRange(view, GetBufferEndAsRange(buffer));
|
||||
Replace(view, string);
|
||||
|
||||
For (view_info) {
|
||||
if (it.scroll_to_end) {
|
||||
it.view->carets[0] = MakeCaret(GetBufferEndAsRange(buffer).min);
|
||||
} else {
|
||||
Dealloc(&it.view->carets);
|
||||
it.view->carets = it.carets;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line) {
|
||||
Scratch scratch;
|
||||
String16 string16 = ToString16(scratch, string);
|
||||
Append(view, string16, scroll_to_end_if_cursor_on_last_line);
|
||||
}
|
||||
|
||||
void Appendf(View *view, const char *fmt, ...) {
|
||||
Scratch scratch;
|
||||
STRING_FORMAT(scratch, fmt, string);
|
||||
Append(view, string, true);
|
||||
}
|
||||
|
||||
void ReportConsolef(const char *fmt, ...) {
|
||||
Scratch scratch;
|
||||
STRING_FORMAT(scratch, fmt, string);
|
||||
Appendf(LogView, "%S\n", string);
|
||||
}
|
||||
|
||||
void ReportErrorf(const char *fmt, ...) {
|
||||
ErrorCount += 1;
|
||||
Scratch scratch;
|
||||
STRING_FORMAT(scratch, fmt, string);
|
||||
if (BreakOnError) {
|
||||
BREAK();
|
||||
}
|
||||
|
||||
if (LogView) {
|
||||
Appendf(LogView, "%S\n", string);
|
||||
} else {
|
||||
printf("%.*s\n", (int)string.len, string.data);
|
||||
}
|
||||
}
|
||||
|
||||
void CMD_CenterView() {
|
||||
CenterView(PrimaryWindowID);
|
||||
} RegisterCommand(CMD_CenterView, "", "");
|
||||
|
||||
void TrimWhitespace(Buffer *buffer, bool trim_lines_with_caret) {
|
||||
Scratch scratch;
|
||||
|
||||
bool is_active_view = false;
|
||||
View *view = GetViewForBuffer(buffer, &is_active_view);
|
||||
if (!is_active_view && !trim_lines_with_caret) {
|
||||
trim_lines_with_caret = true;
|
||||
}
|
||||
|
||||
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
|
||||
MergeCarets(buffer, &view->carets);
|
||||
|
||||
Array<Range> lines_to_skip_triming = {};
|
||||
if (!trim_lines_with_caret) {
|
||||
lines_to_skip_triming = GetSelectedLinesSortedExclusive(scratch, view);
|
||||
}
|
||||
|
||||
for (Int i = 0; i < buffer->line_starts.len; i += 1) {
|
||||
Int range_index = FindRangeByPos(&lines_to_skip_triming, i);
|
||||
if (range_index != -1) continue;
|
||||
|
||||
Range range = GetLineRangeWithoutNL(buffer, i);
|
||||
Int whitespace_end = range.max;
|
||||
for (; whitespace_end > range.min; whitespace_end -= 1) {
|
||||
U16 w = buffer->data[whitespace_end - 1];
|
||||
bool is_whitespace = w == ' ' || w == '\t' || w == '\v' || w == '\r';
|
||||
if (!is_whitespace) break;
|
||||
}
|
||||
|
||||
Range whitespace_range = {whitespace_end, range.max};
|
||||
AddEdit(&edits, whitespace_range, u"");
|
||||
}
|
||||
|
||||
EndEdit(buffer, &edits, &view->carets, !EndEdit_KillSelection);
|
||||
view->update_scroll = false;
|
||||
}
|
||||
|
||||
void ApplyFormattingTool(Buffer *buffer, String tool) {
|
||||
Scratch scratch;
|
||||
String string = AllocCharString(scratch, buffer);
|
||||
ExecResult exec_result = ExecAndWait(scratch, tool, GetDirectory(buffer), string);
|
||||
String16 string16 = {exec_result.buffer->str, exec_result.buffer->len};
|
||||
if (exec_result.exit_code == 0) {
|
||||
ReplaceWithoutMovingCarets(buffer, GetRange(buffer), string16);
|
||||
} else {
|
||||
Append(LogView, string16, true);
|
||||
}
|
||||
}
|
||||
|
||||
// @todo: plugin_languages ? and then branch out language_cpp ...
|
||||
void CMD_FormatSelection() {
|
||||
Scratch scratch;
|
||||
BSet primary = GetBSet(PrimaryWindowID);
|
||||
|
||||
String tool = "";
|
||||
if (EndsWith(primary.buffer->name, ".c") || EndsWith(primary.buffer->name, ".cpp") || EndsWith(primary.buffer->name, ".h") || EndsWith(primary.buffer->name, ".hpp")) {
|
||||
tool = "clang-format";
|
||||
} else if (EndsWith(primary.buffer->name, ".go")) {
|
||||
tool = "gofmt";
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
Array<Edit> edits = BeginEdit(scratch, primary.buffer, primary.view->carets);
|
||||
MergeCarets(primary.buffer, &primary.view->carets);
|
||||
For (primary.view->carets) {
|
||||
String input_string = ToString(scratch, GetString(primary.buffer, it.range));
|
||||
ExecResult exec_result = ExecAndWait(scratch, tool, GetDirectory(primary.buffer), input_string);
|
||||
String16 string16 = {exec_result.buffer->str, exec_result.buffer->len};
|
||||
AddEdit(&edits, it.range, string16);
|
||||
}
|
||||
EndEdit(primary.buffer, &edits, &primary.view->carets, EndEdit_KillSelection);
|
||||
} RegisterCommand(CMD_FormatSelection, "", "");
|
||||
|
||||
void CMD_KillProcess() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
KillProcess(main.view);
|
||||
} RegisterCommand(CMD_KillProcess, "", "Kill process in the last active primary window");
|
||||
|
||||
void CMD_MakeFontLarger() {
|
||||
FontSize += 1;
|
||||
ReloadFont(PathToFont, (U32)FontSize);
|
||||
CenterPrimaryViews();
|
||||
} RegisterCommand(CMD_MakeFontLarger, "ctrl-equals", "Increase the font size");
|
||||
|
||||
void CMD_MakeFontSmaller() {
|
||||
if (FontSize > 4) {
|
||||
FontSize -= 1;
|
||||
ReloadFont(PathToFont, (U32)FontSize);
|
||||
CenterPrimaryViews();
|
||||
}
|
||||
} RegisterCommand(CMD_MakeFontSmaller, "ctrl-minus", "Decrease the font size");
|
||||
|
||||
void CMD_ToggleFullscreen() {
|
||||
if (IsInFullscreen) {
|
||||
SDL_SetWindowSize(SDLWindow, FullScreenSizeX, FullScreenSizeY);
|
||||
SDL_SetWindowPosition(SDLWindow, FullScreenPositionX, FullScreenPositionY);
|
||||
} else {
|
||||
SDL_GetWindowSize(SDLWindow, &FullScreenSizeX, &FullScreenSizeY);
|
||||
SDL_GetWindowPosition(SDLWindow, &FullScreenPositionX, &FullScreenPositionY);
|
||||
SDL_DisplayID display = SDL_GetDisplayForWindow(SDLWindow);
|
||||
const SDL_DisplayMode *dm = SDL_GetCurrentDisplayMode(display);
|
||||
SDL_SetWindowSize(SDLWindow, dm->w, dm->h);
|
||||
SDL_SetWindowPosition(SDLWindow, 0, 0);
|
||||
}
|
||||
IsInFullscreen = !IsInFullscreen;
|
||||
} RegisterCommand(CMD_ToggleFullscreen, "f11", "Switches between the fullscreen and non-fulscreen mode");
|
||||
|
||||
void CMD_OpenLogs() {
|
||||
ErrorCount = 0;
|
||||
Open(LogBuffer->name);
|
||||
} RegisterCommand(CMD_OpenLogs, "", "Opens the text editor logs and clear error counter");
|
||||
|
||||
void CMD_OpenScratch() {
|
||||
ErrorCount = 0;
|
||||
Buffer *buffer = GetBuffer(NullBufferID);
|
||||
Open(buffer->name);
|
||||
} RegisterCommand(CMD_OpenScratch, "", "Opens the scratch buffer");
|
||||
|
||||
void CMD_Errors() {
|
||||
CMD_OpenLogs();
|
||||
} RegisterCommand(CMD_Errors, "", "Opens the text editor logs and clear error counter");
|
||||
|
||||
void CheckKeybindingColission() {
|
||||
ForItem (x, GlobalCommands) {
|
||||
if (x.trigger == NULL) continue;
|
||||
ForItem (y, GlobalCommands) {
|
||||
if (y.trigger == NULL) continue;
|
||||
if (&x == &y) continue;
|
||||
if (x.trigger == y.trigger) {
|
||||
ReportErrorf("Hotkey colission between: '%S' and '%S'", x.name, y.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Range EncloseScope(Buffer *buffer, Int pos_min, Int pos_max, String16 open, String16 close) {
|
||||
Range result = {pos_min, pos_max};
|
||||
String16 buffer_string = GetString(buffer);
|
||||
for (Int i = pos_min - 1; i >= 0; i -= 1) {
|
||||
String16 string = Skip(buffer_string, i);
|
||||
if (StartsWith(string, open)) {
|
||||
result.min = i + open.len;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (Int i = pos_max; i < buffer->len; i += 1) {
|
||||
String16 string = Skip(buffer_string, i);
|
||||
if (StartsWith(string, close)) {
|
||||
result.max = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void CMD_SelectComment() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
For (active.view->carets) {
|
||||
Range scope = EncloseScope(active.buffer, it.range.min, it.range.max, u"/*", u"*/");
|
||||
it.range = scope;
|
||||
}
|
||||
MergeCarets(active.buffer, &active.view->carets);
|
||||
|
||||
} RegisterCommand(CMD_SelectComment, "ctrl-semicolon", "Find /* and */ and select the content in between");
|
||||
@@ -40,9 +40,13 @@ void ClipboardCopy(View *view) {
|
||||
// First, if there is no selection - select the entire line
|
||||
For(view->carets) {
|
||||
if (GetSize(it.range) == 0) {
|
||||
Int line = PosToLine(buffer, it.range.min);
|
||||
Range line_range = GetLineRange(buffer, line);
|
||||
it.range = line_range;
|
||||
Int line = PosToLine(buffer, it.range.min);
|
||||
Int eof = 0;
|
||||
Range line_range = GetLineRange(buffer, line, &eof);
|
||||
it.range = line_range;
|
||||
if (eof) {
|
||||
it.range.min = ClampBottom(0ll, it.range.min - 1ll);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +76,7 @@ void ClipboardPaste(View *view) {
|
||||
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
|
||||
MergeCarets(buffer, &view->carets);
|
||||
For(view->carets) AddEdit(&edits, it.range, string);
|
||||
EndEdit(buffer, &edits, &view->carets, KILL_SELECTION);
|
||||
EndEdit(buffer, &edits, &view->carets, EndEdit_KillSelection);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -80,26 +84,26 @@ void ClipboardPaste(View *view) {
|
||||
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
|
||||
MergeCarets(buffer, &view->carets);
|
||||
for (int64_t i = 0; i < view->carets.len; i += 1) {
|
||||
String16 string = SavedClipboardCarets[i];
|
||||
Caret &it = view->carets[i];
|
||||
AddEdit(&edits, it.range, string);
|
||||
String16 saved_string = SavedClipboardCarets[i];
|
||||
Caret &it = view->carets[i];
|
||||
AddEdit(&edits, it.range, saved_string);
|
||||
}
|
||||
EndEdit(buffer, &edits, &view->carets, KILL_SELECTION);
|
||||
EndEdit(buffer, &edits, &view->carets, EndEdit_KillSelection);
|
||||
}
|
||||
|
||||
void CMD_Paste(HookParam param) {
|
||||
void CMD_Paste() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
ClipboardPaste(active.view);
|
||||
} RegisterCommand(CMD_Paste, "ctrl-v");
|
||||
} RegisterCommand(CMD_Paste, "ctrl-v", "Paste the content of system clipboard at caret");
|
||||
|
||||
void CMD_Copy(HookParam param) {
|
||||
void CMD_Copy() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
ClipboardCopy(active.view);
|
||||
} RegisterCommand(CMD_Copy, "ctrl-c");
|
||||
} RegisterCommand(CMD_Copy, "ctrl-c", "Copy currently selected content to system clipboard");
|
||||
|
||||
void CMD_Cut(HookParam param) {
|
||||
void CMD_Cut() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
|
||||
ClipboardCopy(active.view);
|
||||
Replace(active.view, u"");
|
||||
} RegisterCommand(CMD_Cut, "ctrl-x");
|
||||
} RegisterCommand(CMD_Cut, "ctrl-x", "Copy and delete currently selected content to system clipboard");
|
||||
@@ -1,12 +1,3 @@
|
||||
struct Lexer {
|
||||
Allocator allocator;
|
||||
char *at;
|
||||
char *start;
|
||||
char *end;
|
||||
char *name;
|
||||
int line, column;
|
||||
};
|
||||
|
||||
enum TriggerKind {
|
||||
TriggerKind_Error,
|
||||
TriggerKind_Key,
|
||||
@@ -34,35 +25,7 @@ struct CachedTrigger {
|
||||
};
|
||||
Array<CachedTrigger> CachedTriggers;
|
||||
|
||||
void Advance(Lexer *lex) {
|
||||
if (lex->at < lex->end) {
|
||||
if (lex->at[0] == '\n') {
|
||||
lex->line += 1;
|
||||
lex->column = 0;
|
||||
} else {
|
||||
lex->column += 1;
|
||||
}
|
||||
lex->at += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void Advance(Lexer *lex, int n) {
|
||||
for (int i = 0; i < n; i += 1) Advance(lex);
|
||||
}
|
||||
|
||||
char At(Lexer *lex) {
|
||||
if (lex->at < lex->end) {
|
||||
return lex->at[0];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
String AsString(Lexer *lex) {
|
||||
String result = {lex->at, lex->end - lex->at};
|
||||
return result;
|
||||
}
|
||||
|
||||
void EatWhitespace(Lexer *lex) {
|
||||
void EatWhitespaceEx(Lexer *lex) {
|
||||
while (At(lex) != '\n' && IsWhitespace(At(lex))) {
|
||||
Advance(lex);
|
||||
}
|
||||
@@ -80,7 +43,7 @@ Trigger *TriggerBinary(Lexer *lex, Trigger *left, Trigger *right, int key) {
|
||||
Trigger *ParseKeyAtom(Lexer *lex) {
|
||||
Trigger *result = AllocType(lex->allocator, Trigger);
|
||||
result->kind = TriggerKind_Key;
|
||||
EatWhitespace(lex);
|
||||
EatWhitespaceEx(lex);
|
||||
for (;;) {
|
||||
String lex_string = AsString(lex);
|
||||
if (StartsWith(lex_string, "ctrl")) {
|
||||
@@ -118,12 +81,12 @@ Trigger *ParseKeyAtom(Lexer *lex) {
|
||||
|
||||
if (!found) {
|
||||
result->kind = TriggerKind_Error;
|
||||
ReportErrorf("%s:%d:%d: Failed to parse key trigger, unexpected identifier: '%d'", lex->name, lex->line, lex->column, result->key);
|
||||
ReportErrorf("%s:%d:%d: Failed to parse key trigger, unexpected identifier: '%d'", lex->file, lex->line, lex->column, result->key);
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
result->kind = TriggerKind_Error;
|
||||
ReportErrorf("%s:%d:%d: Failed to parse key trigger, unexpected character: '%c'", lex->name, lex->line, lex->column, At(lex));
|
||||
ReportErrorf("%s:%d:%d: Failed to parse key trigger, unexpected character: '%c'", lex->file, lex->line, lex->column, At(lex));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -138,21 +101,21 @@ Trigger *ParseKeyAtom(Lexer *lex) {
|
||||
|
||||
Trigger *ParseKeyChord(Lexer *lex) {
|
||||
Trigger *left = ParseKeyAtom(lex);
|
||||
EatWhitespace(lex);
|
||||
EatWhitespaceEx(lex);
|
||||
while (IsAlphanumeric(At(lex))) {
|
||||
left = TriggerBinary(lex, left, ParseKeyChord(lex), ' ');
|
||||
EatWhitespace(lex);
|
||||
EatWhitespaceEx(lex);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
Trigger *ParseKeyOr(Lexer *lex) {
|
||||
Trigger *left = ParseKeyChord(lex);
|
||||
EatWhitespace(lex);
|
||||
EatWhitespaceEx(lex);
|
||||
while (At(lex) == '|') {
|
||||
Advance(lex);
|
||||
left = TriggerBinary(lex, left, ParseKeyOr(lex), '|');
|
||||
EatWhitespace(lex);
|
||||
EatWhitespaceEx(lex);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
@@ -227,7 +190,7 @@ void TestParser() {
|
||||
Scratch scratch;
|
||||
{
|
||||
char *cmd = "ctrl-b";
|
||||
Lexer base_lex = {scratch, cmd, cmd, cmd + strlen(cmd), "keybinding"};
|
||||
Lexer base_lex = {scratch, cmd, cmd, cmd + Strlen(cmd), "keybinding"};
|
||||
Trigger *trigger = ParseKeyCatchAll(&base_lex);
|
||||
Assert(trigger->kind == TriggerKind_Key);
|
||||
Assert(trigger->key == SDLK_B);
|
||||
@@ -237,7 +200,7 @@ void TestParser() {
|
||||
|
||||
{
|
||||
char *cmd = "ctrl-b shift-ctrl-a";
|
||||
Lexer base_lex = {scratch, cmd, cmd, cmd + strlen(cmd), "keybinding"};
|
||||
Lexer base_lex = {scratch, cmd, cmd, cmd + Strlen(cmd), "keybinding"};
|
||||
Trigger *trigger = ParseKeyCatchAll(&base_lex);
|
||||
Assert(trigger->kind == TriggerKind_Binary);
|
||||
Assert(trigger->key == ' ');
|
||||
@@ -253,7 +216,7 @@ void TestParser() {
|
||||
|
||||
{
|
||||
char *cmd = "ctrl-b shift-ctrl-a | ctrl-c | ctrl-d";
|
||||
Lexer base_lex = {scratch, cmd, cmd, cmd + strlen(cmd), "keybinding"};
|
||||
Lexer base_lex = {scratch, cmd, cmd, cmd + Strlen(cmd), "keybinding"};
|
||||
Trigger *trigger = ParseKeyCatchAll(&base_lex);
|
||||
Assert(trigger->kind == TriggerKind_Binary);
|
||||
Assert(trigger->key == '|');
|
||||
@@ -283,71 +246,3 @@ void TestParser() {
|
||||
Assert(!ok);
|
||||
}
|
||||
} RegisterFunction(&TestFunctions, TestParser);
|
||||
|
||||
void CMD_OpenConfig(HookParam param) {
|
||||
Buffer *buffer = GetBuffer(GlobalConfigBufferID);
|
||||
Open(buffer->name);
|
||||
} RegisterCommand(CMD_OpenConfig, "", "Open the global config file");
|
||||
|
||||
void CMD_OpenConfigOptions(HookParam param) {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
JumpTempBuffer(&main);
|
||||
NextActiveWindowID = main.window->id;
|
||||
For (Variables) {
|
||||
RawAppendf(main.buffer, "\n:Set %-50S ", it.name);
|
||||
switch(it.type) {
|
||||
case VariableType_Color: RawAppendf(main.buffer, "%x", it.color->value); break;
|
||||
case VariableType_String: RawAppendf(main.buffer, "'%S'", *it.string); break;
|
||||
case VariableType_Int: RawAppendf(main.buffer, "%lld", (long long)*it.i); break;
|
||||
case VariableType_Float: RawAppendf(main.buffer, "%f", *it.f); break;
|
||||
default: InvalidCodepath();
|
||||
}
|
||||
}
|
||||
For (GlobalHooks) {
|
||||
RawAppendf(main.buffer, "\n:Set %-50S '%S'", it.name, it.binding);
|
||||
}
|
||||
} RegisterCommand(CMD_OpenConfigOptions, "", "List available variables and associated documentation inside the command window");
|
||||
|
||||
void EvalCommandsLineByLine(BSet set) {
|
||||
WindowID save_last = PrimaryWindowID;
|
||||
WindowID save_active = ActiveWindowID;
|
||||
WindowID save_next = NextActiveWindowID;
|
||||
Caret save_caret = set.view->carets[0];
|
||||
ActiveWindowID = set.window->id;
|
||||
PrimaryWindowID = set.window->id;
|
||||
NextActiveWindowID = set.window->id;
|
||||
for (Int i = 0; i < set.buffer->line_starts.len; i += 1) {
|
||||
Range range = GetLineRangeWithoutNL(set.buffer, i);
|
||||
String16 string = GetString(set.buffer, range);
|
||||
string = Trim(string);
|
||||
if (string.len == 0) {
|
||||
continue;
|
||||
}
|
||||
if (StartsWith(string, u"//")) {
|
||||
continue;
|
||||
}
|
||||
Open(string);
|
||||
}
|
||||
set.view->carets[0] = save_caret;
|
||||
PrimaryWindowID = save_last;
|
||||
ActiveWindowID = save_active;
|
||||
NextActiveWindowID = save_next;
|
||||
}
|
||||
|
||||
void CMD_EvalCommandsLineByLine(HookParam param) {
|
||||
BSet set = GetBSet(PrimaryWindowID);
|
||||
EvalCommandsLineByLine(set);
|
||||
} RegisterCommand(CMD_EvalCommandsLineByLine, "", "Goes line by line over a buffer and evaluates every line as a command, ignores empty or lines starting with '//'");
|
||||
|
||||
BufferID LoadConfig(String config_path) {
|
||||
ReportConsolef("Loading config %S...", config_path);
|
||||
Window *window = GetWindow(NullWindowID);
|
||||
View *view = WindowOpenBufferView(window, config_path);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
buffer->special = true;
|
||||
EvalCommandsLineByLine({window, view, buffer});
|
||||
if (window->active_view == view->id) {
|
||||
window->active_view = NullViewID;
|
||||
}
|
||||
return buffer->id;
|
||||
}
|
||||
99
src/coroutines.cpp
Normal file
99
src/coroutines.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
typedef void CoroutineFunction(mco_coro *co);
|
||||
CCtx *_CoroutineContext = NULL;
|
||||
|
||||
CCtx *GetCoroutineContext() {
|
||||
Assert(_CoroutineContext);
|
||||
return _CoroutineContext;
|
||||
}
|
||||
|
||||
void DestroyCoroutine(CCtx *n) {
|
||||
mco_destroy(n->co);
|
||||
Release(&n->arena);
|
||||
}
|
||||
|
||||
void RemoveCoroutine(String name) {
|
||||
IterRemove(ActiveCoroutines) {
|
||||
IterRemovePrepare(ActiveCoroutines);
|
||||
if (it.name == name) {
|
||||
DestroyCoroutine(&it);
|
||||
remove_item = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CCtx CreateCoroutine(CoroutineFunction *func, String name) {
|
||||
mco_desc desc = mco_desc_init(func, 0);
|
||||
mco_coro *coro = NULL;
|
||||
mco_result ok = mco_create(&coro, &desc);
|
||||
assert(ok == MCO_SUCCESS);
|
||||
return {coro, name};
|
||||
}
|
||||
|
||||
#define AddCoroutine(x) AddCoroutineEx(x, #x)
|
||||
CCtx *AddCoroutineEx(CoroutineFunction *func, String name) {
|
||||
CCtx coroutine = CreateCoroutine(func, name);
|
||||
Add(&ActiveCoroutines, coroutine);
|
||||
return GetLast(ActiveCoroutines);
|
||||
}
|
||||
|
||||
void ResumeCoroutine(CCtx *dat) {
|
||||
CCtx *prev_curr = _CoroutineContext;
|
||||
_CoroutineContext = dat;
|
||||
mco_result ok = mco_resume(dat->co);
|
||||
Assert(ok == MCO_SUCCESS);
|
||||
_CoroutineContext = prev_curr;
|
||||
}
|
||||
|
||||
void UpdateCoroutines(Event *event) {
|
||||
ProfileFunction();
|
||||
double start = GetTimeSeconds();
|
||||
for (;ActiveCoroutines.len;) {
|
||||
IterRemove(ActiveCoroutines) {
|
||||
IterRemovePrepare(ActiveCoroutines);
|
||||
ProfileScopeEx(it.name);
|
||||
|
||||
mco_state status = mco_status(it.co);
|
||||
if (status == MCO_DEAD) {
|
||||
DestroyCoroutine(&it);
|
||||
remove_item = true;
|
||||
} else {
|
||||
mco_push(it.co, &event, sizeof(Event *));
|
||||
_CoroutineContext = ⁢
|
||||
mco_result ok = mco_resume(it.co);
|
||||
if (ok != MCO_SUCCESS) {
|
||||
ReportErrorf("failed to resume coroutine %d", ok);
|
||||
DestroyCoroutine(&it);
|
||||
remove_item = true;
|
||||
}
|
||||
_CoroutineContext = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#if SLOW_BUILD
|
||||
//:CoroutineLeakCheck
|
||||
// Make sure we don't leak scratch arenas between coroutine boundaries.
|
||||
// it leads to memory corruption! If you hit Assert here make sure you
|
||||
// don't leak the scratch arena.
|
||||
for (int i = 0; i < Lengthof(ScratchArenas); i += 1) {
|
||||
Assert(ScratchArenas[i].refs == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
double took = GetTimeSeconds() - start;
|
||||
bool dont_loop_on_coroutines_when_budget_is_ok_exit_immediately = Testing;
|
||||
if (dont_loop_on_coroutines_when_budget_is_ok_exit_immediately || (took > (0.016666 / 3.0))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Event *Yield(mco_coro *co) {
|
||||
mco_result ok = mco_yield(co);
|
||||
Assert(ok == MCO_SUCCESS);
|
||||
|
||||
Event *event = NULL;
|
||||
ok = mco_pop(co, &event, sizeof(Event *));
|
||||
Assert(ok == MCO_SUCCESS);
|
||||
|
||||
return event;
|
||||
}
|
||||
220
src/data_desc.cpp
Normal file
220
src/data_desc.cpp
Normal file
@@ -0,0 +1,220 @@
|
||||
typedef U32 TokenKind;
|
||||
enum {
|
||||
TokenKind_EOF = 0,
|
||||
TokenKind_Ident = (1<<0),
|
||||
TokenKind_String = (1<<1),
|
||||
TokenKind_Numeric = (1<<2),
|
||||
TokenKind_Comment = (1<<3),
|
||||
TokenKind_Error = (1<<4),
|
||||
|
||||
TokenKind_OpenBrace = (1<<5),
|
||||
TokenKind_CloseBrace = (1<<6),
|
||||
TokenKind_Colon = (1<<7),
|
||||
TokenKind_Comma = (1<<8),
|
||||
TokenKind_Minus = (1<<9),
|
||||
TokenKind_Tag = (1<<10),
|
||||
TokenKind_Or = (1<<11),
|
||||
};
|
||||
|
||||
typedef U32 TokenGroup;
|
||||
enum {
|
||||
TokenGroup_String = (TokenKind_Numeric|TokenKind_String|TokenKind_Ident),
|
||||
TokenGroup_Symbol = (TokenKind_OpenBrace|TokenKind_CloseBrace|TokenKind_Colon|TokenKind_Comma|TokenKind_Minus|TokenKind_Tag|TokenKind_Or),
|
||||
};
|
||||
|
||||
struct Token {
|
||||
TokenKind kind;
|
||||
union {
|
||||
struct {char *data; Int len;};
|
||||
String string;
|
||||
};
|
||||
Float f;
|
||||
Int line, column;
|
||||
char *file;
|
||||
};
|
||||
|
||||
struct Lexer {
|
||||
Allocator allocator;
|
||||
char *at;
|
||||
char *start;
|
||||
char *end;
|
||||
char *file;
|
||||
Int line, column;
|
||||
};
|
||||
|
||||
Lexer MakeLexer(Allocator allocator, String string, char *file, Int line, Int column) {
|
||||
Lexer lexer = {allocator, string.data, string.data, string.data + string.len, file, line, column};
|
||||
return lexer;
|
||||
}
|
||||
|
||||
void Advance(Lexer *lex) {
|
||||
if (lex->at < lex->end) {
|
||||
if (lex->at[0] == '\n') {
|
||||
lex->line += 1;
|
||||
lex->column = 0;
|
||||
}
|
||||
lex->column += 1;
|
||||
lex->at += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void Advance(Lexer *lex, int n) {
|
||||
for (int i = 0; i < n; i += 1) Advance(lex);
|
||||
}
|
||||
|
||||
String AsString(Lexer *lex) {
|
||||
String result = {lex->at, lex->end - lex->at};
|
||||
return result;
|
||||
}
|
||||
|
||||
char At(Lexer *lex) {
|
||||
if (lex->at < lex->end) {
|
||||
return lex->at[0];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void EatWhitespace(Lexer *lex) {
|
||||
while (IsWhitespace(At(lex))) {
|
||||
Advance(lex);
|
||||
}
|
||||
}
|
||||
|
||||
void LexString(Lexer *lex, Token *t) {
|
||||
t->kind = TokenKind_String;
|
||||
char end_char = t->data[0];
|
||||
t->data += 1;
|
||||
for (;;) {
|
||||
char c = At(lex);
|
||||
if (c == 0) {
|
||||
break;
|
||||
}
|
||||
if (c == end_char) {
|
||||
break;
|
||||
}
|
||||
if (c == '\\') {
|
||||
Advance(lex);
|
||||
}
|
||||
Advance(lex);
|
||||
}
|
||||
Advance(lex);
|
||||
t->len = (Int)(lex->at - t->data) - 1;
|
||||
}
|
||||
|
||||
void LexDigit(Lexer *lex, Token *t) {
|
||||
t->kind = TokenKind_Numeric;
|
||||
for (;;) {
|
||||
char c = At(lex);
|
||||
if (c == 0) {
|
||||
break;
|
||||
}
|
||||
if (!IsDigit(c)) {
|
||||
break;
|
||||
}
|
||||
Advance(lex);
|
||||
}
|
||||
t->len = (Int)(lex->at - t->data);
|
||||
t->f = (Float)strtoll(t->data, NULL, 10);
|
||||
}
|
||||
|
||||
bool IsOkForIdent(Lexer *lex, char c) {
|
||||
Unused(lex);
|
||||
bool result = IsAlphanumeric(c) || c == '_' || c == '/';
|
||||
return result;
|
||||
}
|
||||
|
||||
Token Next(Lexer *lex) {
|
||||
EatWhitespace(lex);
|
||||
Token t = {};
|
||||
t.data = lex->at;
|
||||
t.len = 1;
|
||||
t.line = lex->line;
|
||||
t.column = lex->column;
|
||||
t.file = lex->file;
|
||||
char c = At(lex);
|
||||
Advance(lex);
|
||||
|
||||
if (c == 0) {
|
||||
return t;
|
||||
} else if (c == '{') {
|
||||
t.kind = TokenKind_OpenBrace;
|
||||
} else if (c == '}') {
|
||||
t.kind = TokenKind_CloseBrace;
|
||||
} else if (c == ':') {
|
||||
t.kind = TokenKind_Colon;
|
||||
} else if (c == ',') {
|
||||
t.kind = TokenKind_Comma;
|
||||
} else if (IsDigit(c)) {
|
||||
LexDigit(lex, &t);
|
||||
} else if (c == '"') {
|
||||
LexString(lex, &t);
|
||||
} else if (c == '`') {
|
||||
LexString(lex, &t);
|
||||
} else if (IsOkForIdent(lex, c)) {
|
||||
t.kind = TokenKind_String;
|
||||
for (;;) {
|
||||
char cc = At(lex);
|
||||
bool ok = IsOkForIdent(lex, cc);
|
||||
if (!ok) {
|
||||
break;
|
||||
}
|
||||
Advance(lex);
|
||||
}
|
||||
t.len = (Int)(lex->at - t.data);
|
||||
} else {
|
||||
t.kind = TokenKind_Error;
|
||||
t.string = Format(lex->allocator, "Got invalid character when parsing: '%c'", c);
|
||||
return t;
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void TestDataDesc() {
|
||||
Scratch scratch;
|
||||
String s = "{/usr/bin/python3, `Hidden`, Input:Clipboard}";
|
||||
Lexer lexer = MakeLexer(scratch, s, "test", 0, 0);
|
||||
{
|
||||
Token tok = {};
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_OpenBrace);
|
||||
Assert(tok.len == 1);
|
||||
Assert(tok.data[0] == '{');
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_String);
|
||||
Assert(tok.string == "/usr/bin/python3");
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_Comma);
|
||||
Assert(tok.string == ",");
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_String);
|
||||
Assert(tok.string == "Hidden");
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_Comma);
|
||||
Assert(tok.string == ",");
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_String);
|
||||
Assert(tok.string == "Input");
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_Colon);
|
||||
Assert(tok.string == ":");
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_String);
|
||||
Assert(tok.string == "Clipboard");
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_CloseBrace);
|
||||
Assert(tok.string == "}");
|
||||
|
||||
tok = Next(&lexer);
|
||||
Assert(tok.kind == TokenKind_EOF);
|
||||
}
|
||||
|
||||
} RegisterFunction(&TestFunctions, TestDataDesc);
|
||||
@@ -10,6 +10,13 @@ Rect2I GetVisibleCells(Window *window) {
|
||||
|
||||
Int _cx = size.x / window->font->char_spacing;
|
||||
Int _cy = size.y / window->font->line_spacing;
|
||||
|
||||
// This function is mostly used for rendering, these magic numbers are here
|
||||
// to make sure we are not showing unrendered part of the screen to the user.
|
||||
// These were derived by testing and experience. We are only rendering the
|
||||
// part that is on screen but need to account for the arbitrary pixel scrolling.
|
||||
// That is a cell can be half on screen, it's not like vim where you are stuck
|
||||
// to the grid.
|
||||
Int cx = _cx + 1;
|
||||
Int cy = _cy + 2;
|
||||
|
||||
@@ -58,7 +65,7 @@ void DrawVisibleText(Window *window, Color tint) {
|
||||
Rect2 rect = Rect2FromSize(p + g->offset, g->size);
|
||||
|
||||
if (codepoint != '\n' && codepoint != '\r' && codepoint != ' ' && codepoint != '\t') {
|
||||
PushQuad2D(RenderArena, &Vertices, rect, g->atlas_bounding_box, tint);
|
||||
PushQuad2D(RenderArena, &Vertices, Round(rect), g->atlas_bounding_box, tint);
|
||||
}
|
||||
|
||||
text_offset_x += window->font->char_spacing;
|
||||
@@ -96,6 +103,7 @@ void DrawUnderline(Window *window, View *view, Buffer *buffer, Range range, Colo
|
||||
Vec2I max = {xy_max.col * window->font->char_spacing, (xy_max.line + 1) * window->font->line_spacing};
|
||||
Rect2I rect = {min, max};
|
||||
Rect2I scrolled_rect = rect - view->scroll + window->document_rect.min;
|
||||
if (scrolled_rect.min.x < window->document_rect.min.x) scrolled_rect.min.x = window->document_rect.min.x;
|
||||
DrawRectOutline(scrolled_rect, color);
|
||||
}
|
||||
|
||||
@@ -200,21 +208,22 @@ void DrawWindow(Window *window, Event &event) {
|
||||
}
|
||||
|
||||
// Underline word under mouse cursor
|
||||
Caret caret = view->carets[0];
|
||||
Vec2I mouse = MouseVec2I();
|
||||
bool mouse_in_document = AreOverlapping(mouse, window->document_rect);
|
||||
if (mouse_in_document) {
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse);
|
||||
if (p != -1) {
|
||||
Range range = EncloseLoadWord(buffer, p);
|
||||
if (InBounds(caret.range, p)) range = caret.range;
|
||||
DrawUnderline(window, view, buffer, range, MouseUnderlineColor, 2);
|
||||
if (1) {
|
||||
Caret caret = view->carets[0];
|
||||
Vec2I mouse = MouseVec2I();
|
||||
bool mouse_in_document = AreOverlapping(mouse, window->document_rect);
|
||||
if (mouse_in_document) {
|
||||
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse);
|
||||
if (p != -1) {
|
||||
Range range = EncloseLoadWord(buffer, p);
|
||||
if (InBounds(caret.range, p)) range = caret.range;
|
||||
DrawUnderline(window, view, buffer, range, MouseUnderlineColor, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Ctrl()) {
|
||||
if (event.ctrl) {
|
||||
Caret caret = view->carets[0];
|
||||
if (is_active) {
|
||||
if (GetSize(caret.range) == 0) {
|
||||
Range range = EncloseLoadWord(buffer, caret.range.min);
|
||||
@@ -263,10 +272,12 @@ void DrawWindow(Window *window, Event &event) {
|
||||
}
|
||||
}
|
||||
|
||||
#if PLUGIN_SEARCH_WINDOW
|
||||
if (SearchWindowID == window->id) {
|
||||
SetScissor(window->line_numbers_rect);
|
||||
DrawString(window->font, u"Find: ", ToVec2(window->line_numbers_rect.min), color_text_line_numbers);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Draw scrollbar
|
||||
if (DrawScrollbar && window->draw_scrollbar) {
|
||||
@@ -347,11 +347,18 @@ struct { String string; SDL_Keycode value; } SDLKeycodeConversionTable[] = {
|
||||
};
|
||||
|
||||
void FillEventWithBasicData(Event *event) {
|
||||
SDL_Keymod mod = SDL_GetModState();
|
||||
event->shift = (mod & SDL_KMOD_SHIFT) != 0;
|
||||
event->ctrl = (mod & SDL_KMOD_CTRL) != 0;
|
||||
event->alt = (mod & SDL_KMOD_ALT) != 0;
|
||||
event->super = (mod & SDL_KMOD_GUI) != 0;
|
||||
#if OS_WINDOWS
|
||||
if (GetKeyState(VK_SHIFT) & 0x8000) event->shift = 1;
|
||||
if (GetKeyState(VK_CONTROL) & 0x8000) event->ctrl = 1;
|
||||
if (GetKeyState(VK_MENU) & 0x8000) event->alt = 1;
|
||||
if (GetKeyState(VK_LWIN) & 0x8000) event->super = 1;
|
||||
#else
|
||||
SDL_Keymod mod = SDL_GetModState();
|
||||
event->shift = (mod & SDL_KMOD_SHIFT) != 0;
|
||||
event->ctrl = (mod & SDL_KMOD_CTRL) != 0;
|
||||
event->alt = (mod & SDL_KMOD_ALT) != 0;
|
||||
event->super = (mod & SDL_KMOD_GUI) != 0;
|
||||
#endif
|
||||
|
||||
float xmouse, ymouse;
|
||||
SDL_GetMouseState(&xmouse, &ymouse);
|
||||
@@ -371,6 +378,7 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
|
||||
FillEventWithBasicData(&event);
|
||||
|
||||
switch (input_event->type) {
|
||||
|
||||
case SDL_EVENT_QUIT: {
|
||||
event.kind = EVENT_QUIT;
|
||||
} break;
|
||||
@@ -442,10 +450,12 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
|
||||
} break;
|
||||
|
||||
case SDL_EVENT_DROP_FILE: {
|
||||
event.kind = EVENT_DROP_FILE;
|
||||
SDL_DropEvent &b = input_event->drop;
|
||||
String string = b.data;
|
||||
event.text = Intern(&GlobalInternTable, string).data;
|
||||
event.kind = EVENT_DROP_FILE;
|
||||
SDL_DropEvent &b = input_event->drop;
|
||||
String string = b.data;
|
||||
event.text = Intern(&GlobalInternTable, string).data;
|
||||
event.xmouse = (int16_t)roundf(DPIScale * b.x);
|
||||
event.ymouse = (int16_t)roundf(DPIScale * b.y);
|
||||
} break;
|
||||
default: {
|
||||
};
|
||||
@@ -472,109 +482,35 @@ inline void AddEvent(Array<Event> *events, Event ev) {
|
||||
Add(events, ev);
|
||||
}
|
||||
|
||||
Array<Event> GetEventsForFrame(Allocator allocator) {
|
||||
void GetEventsForFrame(Array<Event> *events) {
|
||||
ProfileFunction();
|
||||
Array<Event> result = {allocator};
|
||||
if (EventPlayback.len) {
|
||||
result = TightCopy(allocator, EventPlayback);
|
||||
EventPlayback.len = 0;
|
||||
For (EventPlayback) {
|
||||
Add(events, it);
|
||||
}
|
||||
EventPlayback.len = 0;
|
||||
|
||||
SDL_Event event;
|
||||
if (WaitForEventsState) {
|
||||
SDL_WaitEvent(&event);
|
||||
Event ev = TranslateSDLEvent(&event);
|
||||
AddEvent(&result, ev);
|
||||
AddEvent(events, ev);
|
||||
}
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
Event ev = TranslateSDLEvent(&event);
|
||||
AddEvent(&result, ev);
|
||||
AddEvent(events, ev);
|
||||
}
|
||||
|
||||
if (result.len == 0) {
|
||||
Event event = {};
|
||||
FillEventWithBasicData(&event);
|
||||
event.kind = EVENT_UPDATE;
|
||||
Add(&result, event);
|
||||
if (events->len == 0) {
|
||||
Event ev = {};
|
||||
FillEventWithBasicData(&ev);
|
||||
ev.kind = EVENT_UPDATE;
|
||||
Add(events, ev);
|
||||
}
|
||||
|
||||
Assert(result.len);
|
||||
return result;
|
||||
Assert(events->len);
|
||||
}
|
||||
|
||||
struct Serializer {
|
||||
Buffer *buffer; // for writing
|
||||
};
|
||||
|
||||
void Serialize(Serializer *s, String name, EventKind *kind) {
|
||||
RawAppendf(s->buffer, "ev.%S = %s; ", name, EventKindStrings[*kind]);
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, Int *datum) {
|
||||
if (*datum == 0) {
|
||||
return;
|
||||
}
|
||||
if (name == "key") {
|
||||
RawAppendf(s->buffer, "ev.%S = %s; ", name, SDLKeycodeToName((SDL_Keycode)*datum));
|
||||
} else {
|
||||
RawAppendf(s->buffer, "ev.%S = %lld; ", name, (long long)*datum);
|
||||
}
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, uint32_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(s, name, &d);
|
||||
*datum = (uint32_t)d;
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, uint8_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(s, name, &d);
|
||||
*datum = (uint8_t)d;
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, int16_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(s, name, &d);
|
||||
*datum = (int16_t)d;
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, float *datum) {
|
||||
if (*datum == 0.f) {
|
||||
return;
|
||||
}
|
||||
RawAppendf(s->buffer, "ev.%S = %f; ", name, *datum);
|
||||
}
|
||||
|
||||
void Serialize(Serializer *s, String name, char **text) {
|
||||
String str = *text;
|
||||
if (str.len == 0) {
|
||||
return;
|
||||
}
|
||||
RawAppendf(s->buffer, "ev.%S = \"%S\"; ", name, str);
|
||||
}
|
||||
|
||||
void SerializeBegin(Serializer *s) {
|
||||
RawAppendf(s->buffer, "{Event ev = {};");
|
||||
}
|
||||
|
||||
void SerializeEnd(Serializer *s) {
|
||||
RawAppendf(s->buffer, "Add(&EventPlayback, ev);}\n");
|
||||
}
|
||||
|
||||
// :Event
|
||||
void Serialize(Serializer *s, Event *e) {
|
||||
SerializeBegin(s);
|
||||
#define X(TYPE, KIND, NAME) Serialize(s, #NAME, &e->NAME);
|
||||
EVENT_FIELDS
|
||||
#undef X
|
||||
SerializeEnd(s);
|
||||
}
|
||||
|
||||
#define Ctrl() event.ctrl
|
||||
#define Alt() event.alt
|
||||
#define Shift() event.shift
|
||||
|
||||
#define Press(KEY) (event.key == KEY)
|
||||
#define CtrlPress(KEY) (event.key == KEY && event.ctrl)
|
||||
@@ -583,10 +519,8 @@ void Serialize(Serializer *s, Event *e) {
|
||||
#define CtrlShiftPress(KEY) (event.key == KEY && event.ctrl && event.shift)
|
||||
#define CtrlAltPress(KEY) (event.key == KEY && event.ctrl && event.alt)
|
||||
#define AltShiftPress(KEY) (event.key == KEY && event.shift && event.alt)
|
||||
#define MouseVec2() \
|
||||
Vec2 { (float)event.xmouse, (float)event.ymouse }
|
||||
#define MouseVec2I() \
|
||||
Vec2I { (Int) event.xmouse, (Int)event.ymouse }
|
||||
#define MouseVec2() Vec2 { (float)event.xmouse, (float)event.ymouse }
|
||||
#define MouseVec2I() Vec2I { (Int) event.xmouse, (Int)event.ymouse }
|
||||
#define Mouse(x) (event.kind == EVENT_MOUSE_##x)
|
||||
#define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE))
|
||||
#define MouseUp() (Mouse(LEFT_UP) || Mouse(RIGHT_UP) || Mouse(MIDDLE_UP))
|
||||
5
src/external/minicoro.h
vendored
5
src/external/minicoro.h
vendored
@@ -429,8 +429,8 @@ MCO_API const char *mco_result_description(mco_result res); /* Get the descripti
|
||||
#else
|
||||
#ifdef thread_local
|
||||
#define MCO_THREAD_LOCAL thread_local
|
||||
#elif __STDC_VERSION__ >= 201112 && !defined(__STDC_NO_THREADS__)
|
||||
#define MCO_THREAD_LOCAL _Thread_local
|
||||
// #elif __STDC_VERSION__ >= 201112 && !defined(__STDC_NO_THREADS__)
|
||||
// #define MCO_THREAD_LOCAL _Thread_local
|
||||
#elif defined(_WIN32) && (defined(_MSC_VER) || defined(__ICL) || defined(__DMC__) || defined(__BORLANDC__))
|
||||
#define MCO_THREAD_LOCAL __declspec(thread)
|
||||
#elif defined(__GNUC__) || defined(__SUNPRO_C) || defined(__xlC__)
|
||||
@@ -476,6 +476,7 @@ MCO_API const char *mco_result_description(mco_result res); /* Get the descripti
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#undef Yield
|
||||
#endif
|
||||
|
||||
#ifndef MCO_NO_DEFAULT_ALLOCATOR
|
||||
|
||||
160
src/fuzzy_search_view.cpp
Normal file
160
src/fuzzy_search_view.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
// float NewFuzzyRate(String16 s, String16 p) {
|
||||
// float score = 0;
|
||||
// // try to do this: https://github.com/junegunn/fzf/blob/master/src/algo/algo.go
|
||||
// return score;
|
||||
// }
|
||||
|
||||
float FuzzyRate(String16 s, String16 p) {
|
||||
float score = 0;
|
||||
for (Int outer_pi = 0; outer_pi < p.len; outer_pi += 1) {
|
||||
String16 pit = Skip(p, outer_pi);
|
||||
if (IsWhitespace(At(pit, 0))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
float matching = 0;
|
||||
for (Int outer_si = 0; outer_si < s.len; outer_si += 1) {
|
||||
String16 sit = Skip(s, outer_si);
|
||||
if (IsWhitespace(At(sit, 0))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Int si = 0;
|
||||
Int pi = 0;
|
||||
for (;si < sit.len && pi < pit.len;) {
|
||||
while (si < sit.len && IsWhitespace(sit[si])) si += 1;
|
||||
while (pi < pit.len && IsWhitespace(pit[pi])) pi += 1;
|
||||
if (pi >= pit.len) break;
|
||||
if (si >= sit.len) break;
|
||||
|
||||
if (ToLowerCase(sit[si]) == ToLowerCase(pit[pi])) {
|
||||
matching += 1.0f;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
si += 1;
|
||||
pi += 1;
|
||||
}
|
||||
}
|
||||
score += matching;
|
||||
}
|
||||
score = score / (float)s.len;
|
||||
return score;
|
||||
}
|
||||
|
||||
inline bool MergeSortCompare(FuzzyPair *a, FuzzyPair *b) {
|
||||
bool result = a->rating > b->rating;
|
||||
return result;
|
||||
}
|
||||
|
||||
Array<FuzzyPair> FuzzySearchLines(Allocator allocator, Buffer *buffer, Int line_min, Int line_max, String16 needle) {
|
||||
ProfileFunction();
|
||||
if (line_min < 0 || line_min >= buffer->line_starts.len) return {};
|
||||
if (line_max < 0 || line_min > buffer->line_starts.len) return {};
|
||||
Array<FuzzyPair> ratings = {allocator};
|
||||
Reserve(&ratings, line_max - line_min + 4);
|
||||
for (Int i = line_min; i < line_max; i += 1) {
|
||||
String16 s = GetLineStringWithoutNL(buffer, i);
|
||||
|
||||
Int idx = 0;
|
||||
if (Seek(s, u"||>", &idx, SeekFlag_None)) {
|
||||
s = GetPrefix(s, idx);
|
||||
} else if (Seek(s, u"<||", &idx, SeekFlag_None)) {
|
||||
s = GetPrefix(s, idx);
|
||||
}
|
||||
s = Trim(s);
|
||||
|
||||
float rating = FuzzyRate(s, needle);
|
||||
Add(&ratings, {(int32_t)i, rating});
|
||||
}
|
||||
Array<FuzzyPair> temp = Copy(allocator, ratings);
|
||||
MergeSort(ratings.len, ratings.data, temp.data);
|
||||
return ratings;
|
||||
}
|
||||
|
||||
void UpdateFuzzySearchView() {
|
||||
ProfileFunction();
|
||||
Scratch scratch;
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
String16 line_string = GetLineStringWithoutNL(active.buffer, 0);
|
||||
uint64_t hash = HashBytes(line_string.data, line_string.len * sizeof(char16_t));
|
||||
if (active.view->prev_search_line_hash != hash) {
|
||||
active.view->prev_search_line_hash = hash;
|
||||
Array<FuzzyPair> ratings = FuzzySearchLines(scratch, active.buffer, 1, active.buffer->line_starts.len, line_string);
|
||||
|
||||
Buffer *scratch_buff = CreateScratchBuffer(scratch, active.buffer->cap);
|
||||
RawAppend(scratch_buff, line_string);
|
||||
For (IterateInReverse(&ratings)) {
|
||||
String16 s = GetLineStringWithoutNL(active.buffer, it.index);
|
||||
if (s.len == 0) continue;
|
||||
RawAppend(scratch_buff, u"\n");
|
||||
RawAppend(scratch_buff, s);
|
||||
}
|
||||
|
||||
Caret caret = active.view->carets[0];
|
||||
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
|
||||
SelectEntireBuffer(active.view);
|
||||
Replace(active.view, GetString(scratch_buff));
|
||||
active.view->carets[0] = caret;
|
||||
}
|
||||
}
|
||||
|
||||
String16 FetchFuzzyViewLoadLine(View *view) {
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Range range = view->carets[0].range;
|
||||
String16 string = GetString(buffer, range);
|
||||
Int line = PosToLine(buffer, range.min);
|
||||
if (GetSize(range) == 0 || line == 0) {
|
||||
if (line == 0) {
|
||||
line = ClampTop(1ll, buffer->line_starts.len - 1ll);
|
||||
}
|
||||
string = GetLineStringWithoutNL(buffer, line);
|
||||
|
||||
Int idx = 0;
|
||||
String16 delim = u"||>";
|
||||
if (Seek(string, delim, &idx, SeekFlag_None)) {
|
||||
string = Skip(string, idx + delim.len);
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
void CMD_OpenLoadWord() {
|
||||
Scratch scratch;
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
For (active.view->carets) {
|
||||
Range range = it.range;
|
||||
if (GetSize(range) == 0) {
|
||||
range = EncloseLoadWord(active.buffer, GetFront(it));
|
||||
}
|
||||
String16 load_word = GetString(active.buffer, range);
|
||||
Open(load_word);
|
||||
}
|
||||
} RegisterCommand(CMD_OpenLoadWord, "ctrl-q | f12", "Open a link under the caret (file link, url, command) or open the selection");
|
||||
|
||||
void CMD_OpenForFuzzyView() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
NextActiveWindowID = main.window->id;
|
||||
if (active.view->carets.len == 1 && GetSize(active.view->carets[0].range) == 0) {
|
||||
String16 string = FetchFuzzyViewLoadLine(active.view);
|
||||
Open(string);
|
||||
} else {
|
||||
CMD_OpenLoadWord();
|
||||
}
|
||||
}
|
||||
|
||||
void CMD_OpenAndSetGotoNext() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
NextActiveWindowID = main.window->id;
|
||||
String16 string = FetchFuzzyViewLoadLine(active.view);
|
||||
main.window->active_goto_list = active.view->id;
|
||||
main.window->goto_list_pos = active.view->carets[0].range.min;
|
||||
Open(string);
|
||||
}
|
||||
|
||||
void SetFuzzy(View *view) {
|
||||
AddCommand(&view->commands, "Open", OpenKeySet, CMD_OpenForFuzzyView);
|
||||
view->update_hook = UpdateFuzzySearchView;
|
||||
}
|
||||
@@ -1,17 +1,21 @@
|
||||
SDL_Window *SDLWindow;
|
||||
SDL_GLContext SDL_WindowGLContext;
|
||||
SDL_Cursor *SDL_MouseCursor;
|
||||
SDL_SystemCursor SDL_MouseCursorLastID;
|
||||
bool IsInFullscreen;
|
||||
int FullScreenSizeX, FullScreenSizeY;
|
||||
int FullScreenPositionX, FullScreenPositionY;
|
||||
bool Testing = false;
|
||||
bool AppIsRunning = true;
|
||||
bool WaitForEventsState = true;
|
||||
bool RunGCThisFrame;
|
||||
bool SearchCaseSensitive = false;
|
||||
bool SearchWordBoundary = false;
|
||||
bool BreakOnError = false;
|
||||
Int ErrorCount;
|
||||
bool DebugTraceBufferInits = false;
|
||||
bool Testing = false;
|
||||
|
||||
Allocator SysAllocator = {SystemAllocatorProc};
|
||||
String ConfigDir;
|
||||
float DPIScale = 1.0f;
|
||||
|
||||
// @WARNING: be careful about using this, should only be used for debugging
|
||||
@@ -28,25 +32,26 @@ Array<Window *> Windows;
|
||||
Array<View *> Views;
|
||||
Array<Buffer *> Buffers;
|
||||
|
||||
// First window
|
||||
View *LogView;
|
||||
Buffer *LogBuffer;
|
||||
|
||||
// Replace with ref to null buffer?
|
||||
BufferID NullBufferID;
|
||||
ViewID NullViewID;
|
||||
WindowID NullWindowID;
|
||||
|
||||
// hidden floating window
|
||||
WindowID DebugWindowID;
|
||||
ViewID DebugViewID;
|
||||
BufferID DebugBufferID;
|
||||
WindowID CommandWindowID;
|
||||
WindowID StatusBarWindowID;
|
||||
WindowID SearchWindowID;
|
||||
ViewID SearchViewID;
|
||||
BufferID SearchBufferID;
|
||||
WindowID BuildWindowID;
|
||||
ViewID BuildViewID;
|
||||
BufferID BuildBufferID;
|
||||
BufferID SearchProjectBufferID;
|
||||
BufferID GlobalConfigBufferID;
|
||||
#if PLUGIN_SEARCH_WINDOW
|
||||
WindowID SearchWindowID;
|
||||
ViewID SearchViewID;
|
||||
BufferID SearchBufferID;
|
||||
#endif
|
||||
|
||||
Trigger *EscapeKey;
|
||||
Trigger *EnterKey;
|
||||
Trigger *OpenKeySet;
|
||||
Trigger *EnterOrEscapeKeySet;
|
||||
Trigger *AltEnterKeySet;
|
||||
Trigger *ShiftEnterKeySet;
|
||||
|
||||
WindowID NextActiveWindowID;
|
||||
WindowID ActiveWindowID;
|
||||
@@ -58,13 +63,10 @@ WindowID ResizerHover = {-1};
|
||||
Caret DocumentAnchor;
|
||||
Vec2I MouseMiddleAnchor;
|
||||
|
||||
Buffer *GCInfoBuffer;
|
||||
Buffer *EventBuffer;
|
||||
Buffer *TraceBuffer;
|
||||
View *TraceView;
|
||||
|
||||
RandomSeed UniqueBufferNameSeed = {};
|
||||
Array<Event> EventPlayback;
|
||||
Array<Event> MacroPlayback;
|
||||
bool RecordingMacro = false;
|
||||
BlockArena Perm;
|
||||
|
||||
// clipboard
|
||||
@@ -92,15 +94,21 @@ String Intern(InternTable *table, String string) {
|
||||
// optimize worst offenders (like event text)
|
||||
InternTable GlobalInternTable;
|
||||
|
||||
Array<Hook> GlobalHooks;
|
||||
Array<Command> GlobalCommands;
|
||||
Array<FunctionData> TestFunctions;
|
||||
Array<Variable> Variables;
|
||||
|
||||
Array<Process> ActiveProcesses = {};
|
||||
Array<String> ProcessEnviroment = {};
|
||||
|
||||
struct CoData { mco_coro *co; String name; BlockArena arena; bool dont_wait_until_resolved; void *user_ctx; };
|
||||
Array<CoData> ActiveCoroutines;
|
||||
struct CCtx {
|
||||
mco_coro *co;
|
||||
String name;
|
||||
BlockArena arena;
|
||||
bool dont_wait_until_resolved;
|
||||
void *user_ctx;
|
||||
};
|
||||
Array<CCtx> ActiveCoroutines;
|
||||
|
||||
Color GruvboxDark0Hard = {0x1d, 0x20, 0x21, 0xff};
|
||||
Color GruvboxDark0 = {0x28, 0x28, 0x28, 0xff};
|
||||
@@ -164,14 +172,35 @@ RegisterVariable(Int, WaitForEvents, 1);
|
||||
RegisterVariable(Int, DrawLineNumbers, 1);
|
||||
RegisterVariable(Int, DrawScrollbar, 1);
|
||||
RegisterVariable(Int, IndentSize, 4);
|
||||
RegisterVariable(String, IndentKindWhichIsTabsOrSpaces, "spaces");
|
||||
RegisterVariable(Int, FontSize, 15);
|
||||
RegisterVariable(String, WorkDir, "");
|
||||
RegisterVariable(String, PathToFont, "");
|
||||
RegisterVariable(String, WindowsVCVarsPathToLoadDevEnviroment, "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat");
|
||||
RegisterVariable(Float, UndoMergeTime, 0.3);
|
||||
RegisterVariable(Float, JumpHistoryMergeTime, 0.3);
|
||||
RegisterVariable(String, InternetBrowser, "firefox");
|
||||
RegisterVariable(String, OpenCodePatterns, ".c .h .cpp .hpp .cc .cxx .rs .go .zig .py .lua .js .ts .jsx .tsx .java .kt .swift .cs .rb .php .html .css .scss .bat .sh .bash .zsh .sql .asm .s .cmake .make .json .yaml .toml .ini .txt .md .rst .Makefile .Dockerfile .gitignore .bashrc .zshrc");
|
||||
RegisterVariable(String, OpenCodeExcludePatterns, "");
|
||||
RegisterVariable(Int, TrimTrailingWhitespace, 1);
|
||||
RegisterVariable(String, HomeFolder, "");
|
||||
RegisterVariable(String, ConfigFolder, "");
|
||||
|
||||
// PROJECT_MANAGEMENT
|
||||
// Set at the beginning of the program to current directory
|
||||
RegisterVariable(String, ProjectFolder, "");
|
||||
|
||||
// PLUGIN_BUILD_WINDOW
|
||||
RegisterVariable(String, Build1OnWindows, "build.bat");
|
||||
RegisterVariable(String, Build1OnUnix, "sh build.sh");
|
||||
RegisterVariable(String, Build2OnWindows, "build.bat release");
|
||||
RegisterVariable(String, Build2OnUnix, "sh build.sh release");
|
||||
RegisterVariable(String, Build3OnWindows, "build.bat slow");
|
||||
RegisterVariable(String, Build3OnUnix, "sh build.sh slow");
|
||||
RegisterVariable(String, Build4OnWindows, "build.bat");
|
||||
RegisterVariable(String, Build4OnUnix, "sh build.sh");
|
||||
|
||||
|
||||
// PLUGIN_LOAD_VCVARS
|
||||
RegisterVariable(String, VCVarsPath, "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat");
|
||||
|
||||
// PLUGIN_REMEDYBG
|
||||
RegisterVariable(String, BinaryUnderDebug, "");
|
||||
RegisterVariable(String, RemedyBGPath, "remedybg.exe");
|
||||
@@ -90,6 +90,14 @@ Vec2I GetMid(Rect2I r) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2 Round(Rect2 r) {
|
||||
r.min.x = roundf(r.min.x);
|
||||
r.max.x = roundf(r.max.x);
|
||||
r.min.y = roundf(r.min.y);
|
||||
r.max.y = roundf(r.max.y);
|
||||
return r;
|
||||
}
|
||||
|
||||
Rect2I CutLeft(Rect2I *r, Int value) {
|
||||
Int minx = r->min.x;
|
||||
r->min.x = Min(r->min.x + value, r->max.x);
|
||||
@@ -142,4 +150,8 @@ Int SafeDivide(Int a, Int b) {
|
||||
bool AreOverlapping(Vec2I point, Rect2I rec) {
|
||||
bool result = (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y);
|
||||
return result;
|
||||
}
|
||||
|
||||
Int Absolute(Int value) {
|
||||
return llabs(value);
|
||||
}
|
||||
273
src/plugin_basic_commands.cpp
Normal file
273
src/plugin_basic_commands.cpp
Normal file
@@ -0,0 +1,273 @@
|
||||
void CMD_Redo() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
RedoEdit(active.buffer, &active.view->carets);
|
||||
} RegisterCommand(CMD_Redo, "ctrl-shift-z", "Redo after undoing changes to the buffer");
|
||||
|
||||
void CMD_Undo() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
UndoEdit(active.buffer, &active.view->carets);
|
||||
} RegisterCommand(CMD_Undo, "ctrl-z", "Undo last change you made to the buffer");
|
||||
|
||||
void CMD_EncloseLine() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
EncloseLine(active.view);
|
||||
} RegisterCommand(CMD_EncloseLine, "ctrl-l", "Select the entire line on which your caret is placed");
|
||||
|
||||
void CMD_SelectAll() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
SelectEntireBuffer(active.view);
|
||||
active.view->update_scroll = false;
|
||||
} RegisterCommand(CMD_SelectAll, "ctrl-a", "Select the entire buffer");
|
||||
|
||||
void CMD_KillSelectedLines() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
KillSelectedLines(active.view);
|
||||
} RegisterCommand(CMD_KillSelectedLines, "ctrl-shift-k", "Delete the selected lines, don't put to clipboard");
|
||||
|
||||
void CMD_IndentSelectedLines() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
IndentSelectedLines(active.view);
|
||||
} RegisterCommand(CMD_IndentSelectedLines, "ctrl-rightbracket | tab", "");
|
||||
|
||||
void CMD_DedentSelectedLines() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
IndentSelectedLines(active.view, true);
|
||||
} RegisterCommand(CMD_DedentSelectedLines, "ctrl-leftbracket | shift-tab", "");
|
||||
|
||||
void CMD_DuplicateLineDown() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
DuplicateLine(active.view, DIR_DOWN);
|
||||
} RegisterCommand(CMD_DuplicateLineDown, "ctrl-shift-alt-down", "");
|
||||
|
||||
void CMD_CreateCursorDown() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
CreateCursorVertical(active.view, DIR_DOWN);
|
||||
} RegisterCommand(CMD_CreateCursorDown, "alt-shift-down", "");
|
||||
|
||||
void CMD_SelectDownToEmptyLine() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_DOWN, CTRL_PRESSED, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectDownToEmptyLine, "ctrl-shift-down", "");
|
||||
|
||||
void CMD_MoveLineDown() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCaretsLine(active.view, DIR_DOWN);
|
||||
} RegisterCommand(CMD_MoveLineDown, "alt-down", "");
|
||||
|
||||
void CMD_MoveDownToEmptyLine() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_DOWN, CTRL_PRESSED);
|
||||
} RegisterCommand(CMD_MoveDownToEmptyLine, "ctrl-down", "");
|
||||
|
||||
void CMD_SelectDown() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_DOWN, false, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectDown, "shift-down", "");
|
||||
|
||||
void CMD_MoveDown() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_DOWN);
|
||||
} RegisterCommand(CMD_MoveDown, "down", "");
|
||||
|
||||
void CMD_DuplicateLineUp() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
DuplicateLine(active.view, DIR_UP);
|
||||
} RegisterCommand(CMD_DuplicateLineUp, "ctrl-shift-alt-up", "");
|
||||
|
||||
void CMD_CreateCursorUp() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
CreateCursorVertical(active.view, DIR_UP);
|
||||
} RegisterCommand(CMD_CreateCursorUp, "alt-shift-up", "");
|
||||
|
||||
void CMD_SelectUpToEmptyLine() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_UP, CTRL_PRESSED, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectUpToEmptyLine, "ctrl-shift-up", "");
|
||||
|
||||
void CMD_MoveLineUp() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCaretsLine(active.view, DIR_UP);
|
||||
} RegisterCommand(CMD_MoveLineUp, "alt-up", "");
|
||||
|
||||
void CMD_MoveUpToEmptyLine() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_UP, CTRL_PRESSED);
|
||||
} RegisterCommand(CMD_MoveUpToEmptyLine, "ctrl-up", "");
|
||||
|
||||
void CMD_SelectUp() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_UP, false, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectUp, "shift-up", "");
|
||||
|
||||
void CMD_MoveUp() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_UP);
|
||||
} RegisterCommand(CMD_MoveUp, "up", "");
|
||||
|
||||
void CMD_BoundarySelectLeft() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_LEFT, CTRL_PRESSED, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_BoundarySelectLeft, "ctrl-shift-left", "");
|
||||
|
||||
void CMD_BoundaryMoveLeft() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_LEFT, CTRL_PRESSED);
|
||||
} RegisterCommand(CMD_BoundaryMoveLeft, "ctrl-left", "");
|
||||
|
||||
void CMD_SelectLeft() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_LEFT, false, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectLeft, "shift-left", "");
|
||||
|
||||
void CMD_MoveLeft() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_LEFT);
|
||||
} RegisterCommand(CMD_MoveLeft, "left", "");
|
||||
|
||||
void CMD_BoundarySelectRight() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_RIGHT, CTRL_PRESSED, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_BoundarySelectRight, "ctrl-shift-right", "");
|
||||
|
||||
void CMD_BoundaryMoveRight() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_RIGHT, CTRL_PRESSED);
|
||||
} RegisterCommand(CMD_BoundaryMoveRight, "ctrl-right", "");
|
||||
|
||||
void CMD_SelectRight() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_RIGHT, false, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectRight, "shift-right", "");
|
||||
|
||||
void CMD_MoveRight() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCarets(active.view, DIR_RIGHT);
|
||||
} RegisterCommand(CMD_MoveRight, "right", "");
|
||||
|
||||
void CMD_MoveUpAPage() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCursorByPageSize(active.window, DIR_UP);
|
||||
} RegisterCommand(CMD_MoveUpAPage, "pageup", "");
|
||||
|
||||
void CMD_SelectUpPage() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCursorByPageSize(active.window, DIR_UP, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectUpPage, "shift-pageup", "");
|
||||
|
||||
void CMD_MoveToStart() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
SelectRange(active.view, MakeRange(0));
|
||||
} RegisterCommand(CMD_MoveToStart, "ctrl-pageup", "");
|
||||
|
||||
void CMD_SelectDownPage() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCursorByPageSize(active.window, DIR_DOWN, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectDownPage, "shift-pagedown", "");
|
||||
|
||||
void CMD_MoveToEnd() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
SelectRange(active.view, MakeRange(active.buffer->len));
|
||||
} RegisterCommand(CMD_MoveToEnd, "ctrl-pagedown", "");
|
||||
|
||||
void CMD_MoveDownPage() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCursorByPageSize(active.window, DIR_DOWN);
|
||||
} RegisterCommand(CMD_MoveDownPage, "pagedown", "");
|
||||
|
||||
void CMD_SelectToLineStart() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCursorToSide(active.view, DIR_LEFT, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectToLineStart, "shift-home", "");
|
||||
|
||||
void CMD_MoveToLineStart() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCursorToSide(active.view, DIR_LEFT);
|
||||
} RegisterCommand(CMD_MoveToLineStart, "home", "");
|
||||
|
||||
void CMD_MoveToLineEnd() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCursorToSide(active.view, DIR_RIGHT);
|
||||
} RegisterCommand(CMD_MoveToLineEnd, "end", "");
|
||||
|
||||
void CMD_SelectToLineEnd() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
MoveCursorToSide(active.view, DIR_RIGHT, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_SelectToLineEnd, "shift-end", "");
|
||||
|
||||
void CMD_DeleteCharacter() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
Delete(active.view, DIR_LEFT);
|
||||
} RegisterCommand(CMD_DeleteCharacter, "shift-backspace | backspace", "");
|
||||
|
||||
void CMD_DeleteBoundary() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
Delete(active.view, DIR_LEFT, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_DeleteBoundary, "ctrl-backspace", "");
|
||||
|
||||
void CMD_DeleteForward() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
Delete(active.view, DIR_RIGHT);
|
||||
} RegisterCommand(CMD_DeleteForward, "shift-delete | delete", "");
|
||||
|
||||
void CMD_DeleteForwardBoundary() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
Delete(active.view, DIR_RIGHT, SHIFT_PRESS);
|
||||
} RegisterCommand(CMD_DeleteForwardBoundary, "ctrl-delete", "");
|
||||
|
||||
void CMD_InsertNewLineUp() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
|
||||
MoveCursorToSide(active.view, DIR_LEFT);
|
||||
IndentedNewLine(active.view);
|
||||
MoveCarets(active.view, DIR_UP);
|
||||
} RegisterCommand(CMD_InsertNewLineUp, "ctrl-shift-enter", "");
|
||||
|
||||
void CMD_InsertNewLineDown() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
|
||||
MoveCursorToSide(active.view, DIR_RIGHT);
|
||||
IndentedNewLine(active.view);
|
||||
} RegisterCommand(CMD_InsertNewLineDown, "ctrl-enter", "");
|
||||
|
||||
void CMD_NewLine() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
IndentedNewLine(active.view);
|
||||
} RegisterCommand(CMD_NewLine, "enter | shift-enter", "");
|
||||
|
||||
void CMD_SearchAllOccurences() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
String16 needle = GetString(active.buffer, active.view->carets[0].range);
|
||||
SelectAllOccurences(active.view, needle);
|
||||
} RegisterCommand(CMD_SearchAllOccurences, "ctrl-shift-l", "Use the selected word as needle and selects all the possible occurences in current buffer");
|
||||
|
||||
void CMD_CreateCaretOnNextFind() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
String16 string = GetString(active.buffer, active.view->carets[0].range);
|
||||
Caret caret = FindNext(active.buffer, string, active.view->carets[0]);
|
||||
Insert(&active.view->carets, caret, 0);
|
||||
MergeCarets(active.buffer, &active.view->carets);
|
||||
} RegisterCommand(CMD_CreateCaretOnNextFind, "ctrl-d", "");
|
||||
|
||||
void CMD_ClearCarets() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
active.view->carets.len = 1;
|
||||
active.view->carets[0] = MakeCaret(GetFront(active.view->carets[0]));
|
||||
} RegisterCommand(CMD_ClearCarets, "escape", "Clear all carets and reset to 1 caret, also do some windowing stuff that closes things on escape");
|
||||
|
||||
void CMD_ToggleMacroRecording() {
|
||||
if (RecordingMacro) {
|
||||
RecordingMacro = false;
|
||||
Pop(&MacroPlayback); // Remove the ctrl-m from macro playback thing
|
||||
} else {
|
||||
RecordingMacro = true;
|
||||
MacroPlayback.len = 0;
|
||||
}
|
||||
} RegisterCommand(CMD_ToggleMacroRecording, "ctrl-m", "Start recording a macro");
|
||||
|
||||
void CMD_PlayMacro() {
|
||||
if (RecordingMacro) {
|
||||
return;
|
||||
}
|
||||
|
||||
For (MacroPlayback) Add(&EventPlayback, it);
|
||||
} RegisterCommand(CMD_PlayMacro, "alt-m", "Start playing back a macro recording");
|
||||
92
src/plugin_build_window.cpp
Normal file
92
src/plugin_build_window.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
WindowID BuildWindowID;
|
||||
ViewID BuildViewID;
|
||||
BufferID BuildBufferID;
|
||||
|
||||
void InitBuildWindow() {
|
||||
Window *window = CreateWind();
|
||||
BuildWindowID = window->id;
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(ProjectFolder, "build"));
|
||||
buffer->special = true;
|
||||
buffer->no_history = true;
|
||||
BuildBufferID = buffer->id;
|
||||
View *view = CreateView(buffer->id);
|
||||
view->special = true;
|
||||
BuildViewID = view->id;
|
||||
window->active_view = view->id;
|
||||
window->secondary_window_style = true;
|
||||
window->draw_line_highlight = true;
|
||||
window->primary = false;
|
||||
window->visible = false;
|
||||
window->lose_visibility_on_escape = true;
|
||||
window->jump_history = false;
|
||||
}
|
||||
|
||||
void LayoutBuildWindow(Rect2I *rect, int16_t wx, int16_t wy) {
|
||||
Unused(wx); Unused(wy);
|
||||
Window *window = GetWindow(BuildWindowID);
|
||||
Rect2I copy_rect = *rect;
|
||||
if (!window->visible) {
|
||||
rect = ©_rect;
|
||||
}
|
||||
Int barsize = window->font->line_spacing * 10;
|
||||
window->document_rect = window->total_rect = CutBottom(rect, barsize);
|
||||
}
|
||||
|
||||
BSet ExecBuild(String windows_cmd, String unix_cmd, String working_dir = ProjectFolder) {
|
||||
SaveAll();
|
||||
Scratch scratch;
|
||||
BSet build = GetBSet(BuildWindowID);
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
SelectRange(build.view, Range{});
|
||||
ResetBuffer(build.buffer);
|
||||
{
|
||||
ExecArgs args = ShellArgs(scratch, build.view->id, OS_WINDOWS ? windows_cmd : unix_cmd, working_dir);
|
||||
args.scroll_to_end = true;
|
||||
Exec(args);
|
||||
}
|
||||
main.window->active_goto_list = build.view->id;
|
||||
main.window->goto_list_pos = -1;
|
||||
build.window->visible = true;
|
||||
return build;
|
||||
}
|
||||
|
||||
void CMD_Build1() {
|
||||
ExecBuild(Build1OnWindows, Build1OnUnix);
|
||||
} RegisterCommand(CMD_Build1, "ctrl-b", "Run Build1OnWindows or OnUnix in working directory, output is printed in a popup console and a special build buffer");
|
||||
|
||||
void CMD_Build2() {
|
||||
ExecBuild(Build2OnWindows, Build2OnUnix);
|
||||
} RegisterCommand(CMD_Build2, "alt-b", "Run Build2OnWindows or OnUnix in working directory, output is printed in a popup console and a special build buffer");
|
||||
|
||||
void CMD_Build3() {
|
||||
ExecBuild(Build3OnWindows, Build3OnUnix);
|
||||
} RegisterCommand(CMD_Build3, "shift-b", "Run Build3OnWindows or OnUnix in working directory, output is printed in a popup console and a special build buffer");
|
||||
|
||||
void CMD_Build4() {
|
||||
ExecBuild(Build4OnWindows, Build4OnUnix);
|
||||
} RegisterCommand(CMD_Build4, "ctrl-alt-b", "Run Build4OnWindows or OnUnix in working directory, output is printed in a popup console and a special build buffer");
|
||||
|
||||
void CMD_RunFile() {
|
||||
Scratch scratch;
|
||||
BSet primary = GetBSet(PrimaryWindowID);
|
||||
// @todo: this calls for language_cpp, language_c, language_python etc.
|
||||
String windows_command = "";
|
||||
String unix_command = "";
|
||||
if (EndsWith(primary.buffer->name, ".py")) {
|
||||
unix_command = windows_command = Format(scratch, "python %S", primary.buffer->name);
|
||||
} else {
|
||||
windows_command = Format(scratch, "cl %S -Fe:cfile.exe /Zi /FC /nologo /WX /W3 /wd4200 /wd4334 /diagnostics:column && cfile.exe && del cfile.*", primary.buffer->name);
|
||||
unix_command = Format(scratch, "bash <<EOF\nclang %S -o cfile.exe -g -Wall -Wno-missing-braces -Wno-writable-strings -Wno-writable-strings -fsanitize=address -fdiagnostics-absolute-paths -lm \n./cfile.exe \nrm cfile.exe\nEOF", primary.buffer->name);
|
||||
}
|
||||
ExecBuild(windows_command, unix_command, GetDirectory(primary.buffer));
|
||||
} RegisterCommand(CMD_RunFile, "", "Run and build current file, currently only C/C++ supported");
|
||||
|
||||
void CMD_ShowBuildWindow() {
|
||||
BSet main = GetBSet(BuildWindowID);
|
||||
if (ActiveWindowID != BuildWindowID) {
|
||||
main.window->visible = true;
|
||||
NextActiveWindowID = BuildWindowID;
|
||||
} else {
|
||||
main.window->visible = false;
|
||||
}
|
||||
} RegisterCommand(CMD_ShowBuildWindow, "ctrl-grave", "Toggles visibility of the build window");
|
||||
86
src/plugin_command_window.cpp
Normal file
86
src/plugin_command_window.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
WindowID CommandWindowID;
|
||||
|
||||
void CMD_ShowCommands() {
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
NextActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For (GlobalCommands) {
|
||||
RawAppendf(command_bar.buffer, "\n:%-30S <|| ", it.name);
|
||||
if (it.docs.len) {
|
||||
RawAppendf(command_bar.buffer, "%S", it.docs);
|
||||
}
|
||||
}
|
||||
command_bar.view->update_scroll = true;
|
||||
SelectRange(command_bar.view, Range{});
|
||||
} RegisterCommand(CMD_ShowCommands, "ctrl-shift-p", "List available commands and their documentation inside the command window");
|
||||
|
||||
void CMD_ShowDebugBufferList() {
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
NextActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For (Buffers) {
|
||||
bool is_special = it->special || it->temp || it->dont_try_to_save_in_bulk_ops;
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
is_special |= it->is_dir;
|
||||
#endif
|
||||
if (!is_special) {
|
||||
continue;
|
||||
}
|
||||
RawAppendf(command_bar.buffer, "\n%S", it->name);
|
||||
}
|
||||
command_bar.view->update_scroll = true;
|
||||
SelectRange(command_bar.view, Range{});
|
||||
} RegisterCommand(CMD_ShowDebugBufferList, "ctrl-shift-alt-p", "Show full list of buffers, including the special ones that normally just clutter list");
|
||||
|
||||
void CMD_ShowBufferList() {
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
NextActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For (Buffers) {
|
||||
bool is_special = it->special || it->temp || it->dont_try_to_save_in_bulk_ops;
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
is_special |= it->is_dir;
|
||||
#endif
|
||||
if (is_special) {
|
||||
continue;
|
||||
}
|
||||
RawAppendf(command_bar.buffer, "\n%S", it->name);
|
||||
}
|
||||
command_bar.view->update_scroll = true;
|
||||
SelectRange(command_bar.view, Range{});
|
||||
} RegisterCommand(CMD_ShowBufferList, "ctrl-p", "List open buffers inside the command window that you can fuzzy search over");
|
||||
|
||||
void LayoutCommandWindow(Rect2I *rect, int16_t wx, int16_t wy) {
|
||||
Unused(wy);
|
||||
Window *window = GetWindow(CommandWindowID);
|
||||
Rect2I copy_rect = *rect;
|
||||
if (!window->visible) {
|
||||
rect = ©_rect;
|
||||
}
|
||||
Int barsize = Clamp((Int)window->font->line_spacing*10, (Int)0, (Int)wx - 100);
|
||||
window->document_rect = window->total_rect = CutBottom(rect, barsize);
|
||||
}
|
||||
|
||||
void InitCommandWindow() {
|
||||
Window *window = CreateWind();
|
||||
CommandWindowID = window->id;
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(ProjectFolder, "command_bar"));
|
||||
buffer->special = true;
|
||||
buffer->no_history = true;
|
||||
View *view = CreateView(buffer->id);
|
||||
view->special = true;
|
||||
SetFuzzy(view);
|
||||
window->active_view = view->id;
|
||||
window->draw_line_numbers = false;
|
||||
window->draw_scrollbar = false;
|
||||
window->secondary_window_style = true;
|
||||
window->draw_line_highlight = true;
|
||||
window->primary = false;
|
||||
window->visible = false;
|
||||
window->sync_visibility_with_focus = true;
|
||||
window->lose_focus_on_escape = true;
|
||||
window->jump_history = false;
|
||||
}
|
||||
156
src/plugin_config.cpp
Normal file
156
src/plugin_config.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
#if PLUGIN_CONFIG
|
||||
BufferID GlobalConfigBufferID;
|
||||
|
||||
void CMD_OpenConfig() {
|
||||
Buffer *buffer = GetBuffer(GlobalConfigBufferID);
|
||||
Open(buffer->name);
|
||||
} RegisterCommand(CMD_OpenConfig, "", "Open the global config file");
|
||||
|
||||
void CMD_OpenConfigOptions() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
JumpTempBuffer(&main);
|
||||
NextActiveWindowID = main.window->id;
|
||||
For (Variables) {
|
||||
RawAppendf(main.buffer, "\n:Set %-50S ", it.name);
|
||||
switch(it.type) {
|
||||
case VariableType_Color: RawAppendf(main.buffer, "%x", it.color->value); break;
|
||||
case VariableType_String: RawAppendf(main.buffer, "'%S'", *it.string); break;
|
||||
case VariableType_Int: RawAppendf(main.buffer, "%lld", (long long)*it.i); break;
|
||||
case VariableType_Float: RawAppendf(main.buffer, "%f", *it.f); break;
|
||||
default: InvalidCodepath();
|
||||
}
|
||||
}
|
||||
For (GlobalCommands) {
|
||||
RawAppendf(main.buffer, "\n:Set %-50S '%S'", it.name, it.binding);
|
||||
}
|
||||
} RegisterCommand(CMD_OpenConfigOptions, "", "List available variables and associated documentation inside the command window");
|
||||
|
||||
void EvalCommandsLineByLine(BSet set) {
|
||||
WindowID save_last = PrimaryWindowID;
|
||||
WindowID save_active = ActiveWindowID;
|
||||
WindowID save_next = NextActiveWindowID;
|
||||
Caret save_caret = set.view->carets[0];
|
||||
ActiveWindowID = set.window->id;
|
||||
PrimaryWindowID = set.window->id;
|
||||
NextActiveWindowID = set.window->id;
|
||||
for (Int i = 0; i < set.buffer->line_starts.len; i += 1) {
|
||||
Range range = GetLineRangeWithoutNL(set.buffer, i);
|
||||
String16 string = GetString(set.buffer, range);
|
||||
string = Trim(string);
|
||||
if (string.len == 0) {
|
||||
continue;
|
||||
}
|
||||
if (StartsWith(string, u"//")) {
|
||||
continue;
|
||||
}
|
||||
Open(string);
|
||||
}
|
||||
set.view->carets[0] = save_caret;
|
||||
PrimaryWindowID = save_last;
|
||||
ActiveWindowID = save_active;
|
||||
NextActiveWindowID = save_next;
|
||||
}
|
||||
|
||||
void CMD_EvalCommandsLineByLine() {
|
||||
BSet set = GetBSet(PrimaryWindowID);
|
||||
EvalCommandsLineByLine(set);
|
||||
} RegisterCommand(CMD_EvalCommandsLineByLine, "", "Goes line by line over a buffer and evaluates every line as a command, ignores empty or lines starting with '//'");
|
||||
|
||||
BufferID LoadConfig(String config_path) {
|
||||
ReportConsolef("Loading config %S...", config_path);
|
||||
Window *window = GetWindow(NullWindowID);
|
||||
ViewID active_view_before = window->active_view;
|
||||
View *view = WindowOpenBufferView(window, config_path);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
EvalCommandsLineByLine({window, view, buffer});
|
||||
if (window->active_view == view->id) {
|
||||
window->active_view = active_view_before;
|
||||
}
|
||||
return buffer->id;
|
||||
}
|
||||
|
||||
#define ExpectP(x, ...) \
|
||||
if (!(x)) { \
|
||||
ReportErrorf("Failed to parse command " __VA_ARGS__); \
|
||||
return; \
|
||||
}
|
||||
|
||||
void Set(String string) {
|
||||
String name = SkipIdent(&string);
|
||||
ExpectP(name.len != 0, "expected a variable name, instead got '%S'", string);
|
||||
|
||||
Variable *var = GetVariable(name);
|
||||
if (var) {
|
||||
SkipWhitespace(&string);
|
||||
if (var->type == VariableType_String) {
|
||||
char c = At(string, 0);
|
||||
ExpectP(c == u'"' || c == u'\'', "Expected string to follow the command name, instead got %S", string);
|
||||
string = Skip(string, 1);
|
||||
String quote = SkipUntil(&string, {&c, 1});
|
||||
ExpectP(At(string, 0) == c, ":Set %S <error here>, unclosed quote", name);
|
||||
// ReportConsolef(":Set %S %c%S%c", name, c, quote, c);
|
||||
*var->string = Intern(&GlobalInternTable, quote);
|
||||
} else if (var->type == VariableType_Int) {
|
||||
ExpectP(IsDigit(At(string, 0)), "Expected an integer to follow the command name, instead got: %S", string);
|
||||
Int number = SkipInt(&string);
|
||||
// ReportConsolef(":Set %S %lld", name, number);
|
||||
*var->i = number;
|
||||
} else if (var->type == VariableType_Float) {
|
||||
ExpectP(IsDigit(At(string, 0)), "Expected float to follow the command name, instead got: %S", string);
|
||||
Float number = SkipFloat(&string);
|
||||
// ReportConsolef(":Set %S %f", name, number);
|
||||
*var->f = number;
|
||||
} else if (var->type == VariableType_Color) {
|
||||
ExpectP(IsHexDigit(At(string, 0)), "Expected hex integer to follow the command name, instead got: %S", string);
|
||||
String begin = {string.data, 0};
|
||||
while (IsHexDigit(At(string, 0))) {
|
||||
string = Skip(string, 1);
|
||||
begin.len += 1;
|
||||
}
|
||||
// ReportConsolef(":Set %S %S", name, begin);
|
||||
var->color->value = (uint32_t)strtoll(begin.data, NULL, 16);
|
||||
} ElseInvalidCodepath();
|
||||
|
||||
|
||||
if (name == "FontSize" || name == "PathToFont") {
|
||||
ReloadFont(PathToFont, (U32)FontSize);
|
||||
}
|
||||
|
||||
#if PLUGIN_PROJECT_MANAGEMENT
|
||||
if (name == "ProjectFolder") {
|
||||
SetProjectFolder(*var->string);
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
Command *cmd = NULL;
|
||||
For (GlobalCommands) {
|
||||
if (it.name == name) {
|
||||
cmd = ⁢
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (cmd) {
|
||||
SkipWhitespace(&string);
|
||||
char c = At(string, 0);
|
||||
ExpectP(c == u'"' || c == u'\'', "Expected string to follow the command name, instead got %S", string);
|
||||
string = Skip(string, 1);
|
||||
String quote = SkipUntil(&string, {&c, 1});
|
||||
ExpectP(At(string, 0) == c, "Failed to parse command, unclose quote");
|
||||
quote = Intern(&GlobalInternTable, quote);
|
||||
ReportConsolef(":Set %S %c%S%c", name, c, quote, c);
|
||||
Trigger *trigger = ParseKeyCached(quote);
|
||||
if (trigger) {
|
||||
cmd->binding = quote;
|
||||
cmd->trigger = trigger;
|
||||
CheckKeybindingColission();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ReportErrorf("Failed to :Set, no such variable found: %S", name);
|
||||
}
|
||||
|
||||
#endif
|
||||
4
src/plugin_config.h
Normal file
4
src/plugin_config.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#if PLUGIN_CONFIG
|
||||
void Set(String string);
|
||||
String InsertVariables(Allocator allocator, String string);
|
||||
#endif
|
||||
67
src/plugin_debug_window.cpp
Normal file
67
src/plugin_debug_window.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
WindowID DebugWindowID;
|
||||
ViewID DebugViewID;
|
||||
BufferID DebugBufferID;
|
||||
|
||||
void InitDebugWindow() {
|
||||
Window *window = CreateWind();
|
||||
DebugWindowID = window->id;
|
||||
window->draw_line_numbers = false;
|
||||
window->draw_scrollbar = false;
|
||||
window->visible = false;
|
||||
window->z = 2;
|
||||
window->primary = false;
|
||||
window->jump_history = false;
|
||||
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(ProjectFolder, "debug"));
|
||||
DebugBufferID = buffer->id;
|
||||
buffer->no_history = true;
|
||||
buffer->special = true;
|
||||
|
||||
View *view = CreateView(buffer->id);
|
||||
view->special = true;
|
||||
DebugViewID = view->id;
|
||||
window->active_view = view->id;
|
||||
window->visible = false;
|
||||
}
|
||||
|
||||
void LayoutDebugWindow(Rect2I *rect, int16_t wx, int16_t wy) {
|
||||
Unused(rect);
|
||||
Window *window = GetWindow(DebugWindowID);
|
||||
Rect2 screen_rect = Rect0Size((float)wx, (float)wy);
|
||||
Vec2 size = GetSize(screen_rect);
|
||||
|
||||
Rect2 a = CutRight(&screen_rect, 0.3f * size.x);
|
||||
Rect2 b = CutTop(&a, 0.4f * size.y);
|
||||
Rect2 c = Shrink(b, 20);
|
||||
window->document_rect = window->total_rect = ToRect2I(c);
|
||||
}
|
||||
|
||||
void UpdateDebugWindow() {
|
||||
ProfileFunction();
|
||||
BSet set = GetBSet(DebugWindowID);
|
||||
if (!set.window->visible) {
|
||||
return;
|
||||
}
|
||||
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
if (active.buffer->id.id == set.buffer->id.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
RawReplaceText(set.buffer, GetRange(set.buffer), u"");
|
||||
#if OS_POSIX
|
||||
struct rusage usage;
|
||||
getrusage(RUSAGE_SELF, &usage);
|
||||
RawAppendf(set.buffer, "MemoryUsage: %lld\n", usage.ru_maxrss);
|
||||
#endif
|
||||
|
||||
For (Views) {
|
||||
Buffer *buffer = GetBuffer(it->active_buffer);
|
||||
RawAppendf(set.buffer, "view->id:%lld, buffer->id:%lld, buffer->name:%S\n", (long long)it->id.id, (long long)buffer->id.id, buffer->name);
|
||||
}
|
||||
}
|
||||
|
||||
void CMD_ToggleDebug() {
|
||||
Window *window = GetWindow(DebugWindowID);
|
||||
window->visible = !window->visible;
|
||||
} RegisterCommand(CMD_ToggleDebug, "ctrl-0", "Open a floating window that might become useful for debugging");
|
||||
206
src/plugin_directory_navigation.cpp
Normal file
206
src/plugin_directory_navigation.cpp
Normal file
@@ -0,0 +1,206 @@
|
||||
// @todo: On save rename files, delete files it should apply the changes
|
||||
// Instead of toying with Reopen maybe it should actually detect changes in directory etc. on update
|
||||
|
||||
void CMD_OpenUpFolder() {
|
||||
// @todo: Test this for weird cases
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
String name = ChopLastSlash(main.buffer->name);
|
||||
Open(name);
|
||||
} RegisterCommand(CMD_OpenUpFolder, "ctrl-o", "Open current's file directory or up directory in other cases");
|
||||
|
||||
void CMD_OpenSystemFileBrowser() {
|
||||
Scratch scratch;
|
||||
String string = GetPrimaryDirectory();
|
||||
#if OS_WINDOWS
|
||||
Open(Format(scratch, "!!explorer %S", string));
|
||||
#else
|
||||
Open(Format(scratch, "!!xdg-open %S & disown", string));
|
||||
#endif
|
||||
} RegisterCommand(CMD_OpenSystemFileBrowser, "ctrl-alt-r", "Opens the directory of current file in the systems file browser");
|
||||
|
||||
void InsertDirectoryNavigation(Buffer *buffer) {
|
||||
Assert(buffer->is_dir);
|
||||
Scratch scratch;
|
||||
ResetBuffer(buffer);
|
||||
RawAppendf(buffer, "\n");
|
||||
RawAppendf(buffer, "..\n");
|
||||
for (FileIter it = IterateFiles(scratch, buffer->name); IsValid(it); Advance(&it)) {
|
||||
RawAppendf(buffer, "%S\n", it.filename);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateDirectoryNavigation() {
|
||||
#if 0
|
||||
ProfileFunction();
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
Buffer *buffer = active.buffer;
|
||||
|
||||
Scratch scratch;
|
||||
Array<String> existing_lines = Split(scratch, AllocCharString(scratch, buffer), "\n");
|
||||
bool modified = false;
|
||||
for (FileIter it = IterateFiles(scratch, buffer->name); IsValid(it); Advance(&it)) {
|
||||
bool found = false;
|
||||
ForItem (existing, existing_lines) {
|
||||
if (existing == it.filename) {
|
||||
found = true;
|
||||
UnorderedRemove(&existing_lines, existing);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found == false) {
|
||||
modified = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (existing_lines.len > 1) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
ReopenBuffer(buffer);
|
||||
}
|
||||
|
||||
UpdateFuzzySearchView();
|
||||
#endif
|
||||
}
|
||||
|
||||
void OpenDirectoryNavigation(View *view) {
|
||||
SetFuzzy(view);
|
||||
// view->update_hook = UpdateDirectoryNavigation;
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
InsertDirectoryNavigation(buffer);
|
||||
SelectRange(view, Range{});
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
typedef struct {
|
||||
HANDLE dir;
|
||||
OVERLAPPED ov;
|
||||
HANDLE event;
|
||||
BYTE buffer[4096];
|
||||
BOOL pending;
|
||||
} DirectoryWatcher;
|
||||
|
||||
BOOL InitDirectoryWatcher(DirectoryWatcher *w, const wchar_t *path)
|
||||
{
|
||||
ZeroMemory(w, sizeof(*w));
|
||||
|
||||
w->dir = CreateFileW(
|
||||
path,
|
||||
FILE_LIST_DIRECTORY,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
|
||||
NULL
|
||||
);
|
||||
|
||||
if (w->dir == INVALID_HANDLE_VALUE)
|
||||
return FALSE;
|
||||
|
||||
w->event = CreateEventW(NULL, TRUE, FALSE, NULL);
|
||||
if (!w->event)
|
||||
return FALSE;
|
||||
|
||||
w->ov.hEvent = w->event;
|
||||
w->pending = FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL WasDirectoryModified(DirectoryWatcher *w)
|
||||
{
|
||||
DWORD bytes;
|
||||
|
||||
if (!w->pending) {
|
||||
ResetEvent(w->event);
|
||||
w->pending = ReadDirectoryChangesW(
|
||||
w->dir,
|
||||
w->buffer,
|
||||
sizeof(w->buffer),
|
||||
TRUE,
|
||||
FILE_NOTIFY_CHANGE_FILE_NAME |
|
||||
FILE_NOTIFY_CHANGE_DIR_NAME |
|
||||
FILE_NOTIFY_CHANGE_SIZE |
|
||||
FILE_NOTIFY_CHANGE_LAST_WRITE,
|
||||
NULL,
|
||||
&w->ov,
|
||||
NULL
|
||||
);
|
||||
if (!w->pending)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (WaitForSingleObject(w->event, 0) == WAIT_OBJECT_0) {
|
||||
GetOverlappedResult(w->dir, &w->ov, &bytes, FALSE);
|
||||
w->pending = FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void DestroyDirectoryWatcher(DirectoryWatcher *w)
|
||||
{
|
||||
if (w->dir != INVALID_HANDLE_VALUE)
|
||||
CloseHandle(w->dir);
|
||||
if (w->event)
|
||||
CloseHandle(w->event);
|
||||
}
|
||||
|
||||
//
|
||||
DirectoryWatcher w;
|
||||
InitDirectoryWatcher(&w, L"C:\\mydir");
|
||||
|
||||
if (WasDirectoryModified(&w)) {
|
||||
// something changed
|
||||
}
|
||||
|
||||
#if OS_LINUX
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
int wd;
|
||||
} DirectoryWatcher;
|
||||
|
||||
int InitDirectoryWatcher(DirectoryWatcher *w, const char *path)
|
||||
{
|
||||
w->fd = inotify_init1(IN_NONBLOCK);
|
||||
if (w->fd < 0)
|
||||
return 0;
|
||||
|
||||
w->wd = inotify_add_watch(
|
||||
w->fd,
|
||||
path,
|
||||
IN_CREATE | IN_DELETE | IN_MOVE | IN_MODIFY
|
||||
);
|
||||
|
||||
return w->wd >= 0;
|
||||
}
|
||||
|
||||
int WasDirectoryModified(DirectoryWatcher *w)
|
||||
{
|
||||
char buffer[4096];
|
||||
ssize_t len = read(w->fd, buffer, sizeof(buffer));
|
||||
|
||||
if (len > 0)
|
||||
return 1;
|
||||
|
||||
return 0; // no data = no changes
|
||||
}
|
||||
|
||||
void DestroyDirectoryWatcher(DirectoryWatcher *w)
|
||||
{
|
||||
if (w->wd >= 0)
|
||||
inotify_rm_watch(w->fd, w->wd);
|
||||
if (w->fd >= 0)
|
||||
close(w->fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
2
src/plugin_directory_navigation.h
Normal file
2
src/plugin_directory_navigation.h
Normal file
@@ -0,0 +1,2 @@
|
||||
void InsertDirectoryNavigation(struct Buffer *buffer);
|
||||
void OpenDirectoryNavigation(struct View *view);
|
||||
183
src/plugin_file_commands.cpp
Normal file
183
src/plugin_file_commands.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
#if PLUGIN_FILE_COMMANDS
|
||||
|
||||
void New(Window *window, String name = "") {
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
|
||||
Scratch scratch;
|
||||
String dir = GetDirectory(buffer);
|
||||
if (name != "") {
|
||||
if (!IsAbsolute(name)) {
|
||||
name = Format(scratch, "%S/%S", dir, name);
|
||||
}
|
||||
name = GetAbsolutePath(scratch, name);
|
||||
} else {
|
||||
name = GetUniqueBufferName(dir, "new");
|
||||
}
|
||||
WindowOpenBufferView(window, name);
|
||||
RunGCThisFrame = true;
|
||||
}
|
||||
|
||||
void CMD_New() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
New(main.window, "");
|
||||
} RegisterCommand(CMD_New, "ctrl-n", "Open a new buffer with automatically generated name, use :Rename");
|
||||
|
||||
void CMD_SaveAll() {
|
||||
SaveAll();
|
||||
} RegisterCommand(CMD_SaveAll, "ctrl-shift-s", "");
|
||||
|
||||
void CMD_Save() {
|
||||
BSet active = GetBSet(PrimaryWindowID);
|
||||
SaveBuffer(active.buffer);
|
||||
} RegisterCommand(CMD_Save, "ctrl-s", "Save buffer currently open in the last primary window");
|
||||
|
||||
void CMD_Reopen() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
ReopenBuffer(main.buffer);
|
||||
NextActiveWindowID = main.window->id;
|
||||
} RegisterCommand(CMD_Reopen, "", "Reads again from disk the current buffer");
|
||||
|
||||
void CO_Rename(mco_coro *co) {
|
||||
CCtx *ctx = GetCoroutineContext();
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
Buffer *original_buffer = main.buffer;
|
||||
JumpTempBuffer(&main);
|
||||
NextActiveWindowID = main.window->id;
|
||||
RawAppendf(main.buffer, "Rename and click enter to submit: [%S]\n :Rename :Cancel", original_buffer->name);
|
||||
|
||||
main.view->carets[0] = FindNext(main.buffer, u"]", MakeCaret(0));
|
||||
main.view->carets[0].range.max = main.view->carets[0].range.min;
|
||||
AddUIAction(main.view, "Rename", EnterKey, UIAction_Yes);
|
||||
AddUIAction(main.view, "Cancel", EscapeKey, UIAction_Cancel);
|
||||
UIAction action = WaitForUIAction(co, main);
|
||||
if (action != UIAction_Yes) {
|
||||
return;
|
||||
}
|
||||
|
||||
Caret a = FindNext(main.buffer, u"[", MakeCaret(-1));
|
||||
Caret b = FindNext(main.buffer, u"]", MakeCaret(-1));
|
||||
if (a.range.min == -1 || b.range.max == -1) {
|
||||
ReportErrorf("Failed to extract the name for the Rename operation from the buffer");
|
||||
return;
|
||||
}
|
||||
String16 string16 = GetString(main.buffer, {a.range.max + 1, b.range.max});
|
||||
String string = ToString(ctx->arena, string16);
|
||||
original_buffer->name = Intern(&GlobalInternTable, string);
|
||||
} RegisterCoroutineCommand(CO_Rename, "", "Opens a UI asking for a new name of a buffer open in the last active primary window");
|
||||
|
||||
void CO_Close(mco_coro *co) {
|
||||
CCtx *ctx = GetCoroutineContext();
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
if (main.buffer->special || main.buffer->temp) {
|
||||
Close(main.view->id);
|
||||
return;
|
||||
}
|
||||
|
||||
bool ref = false;
|
||||
For (Views) {
|
||||
if (it->id == main.view->id) {
|
||||
continue;
|
||||
}
|
||||
if (it->active_buffer == main.buffer->id) {
|
||||
ref = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ref) {
|
||||
Close(main.view->id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!main.buffer->dirty) {
|
||||
Close(main.buffer->id);
|
||||
return;
|
||||
}
|
||||
|
||||
String question = Format(ctx->arena, "Do you want to save [%S] before closing?", main.buffer->name);
|
||||
UIAction ui_action = QueryUserYesNoCancel(co, main, question);
|
||||
if (ui_action == UIAction_Yes) {
|
||||
SaveBuffer(main.buffer);
|
||||
Close(main.buffer->id);
|
||||
} else if (ui_action == UIAction_No) {
|
||||
Close(main.buffer->id);
|
||||
} else if (ui_action == UIAction_Cancel) {
|
||||
return;
|
||||
} ElseInvalidCodepath();
|
||||
} RegisterCoroutineCommand(CO_Close, "ctrl-w", "Close open view in the last active primary window");
|
||||
|
||||
void CMD_DeleteFile() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
DeleteFile(main.buffer->name);
|
||||
Close(main.buffer->id);
|
||||
} RegisterCommand(CMD_DeleteFile, "", "Close the open buffer and delete it's corresponding file on disk");
|
||||
|
||||
void CO_CloseAll(mco_coro *co) {
|
||||
ShowCloseAllUI(co);
|
||||
} RegisterCoroutineCommand(CO_CloseAll, "", "Ask user which files to save and close all open normal views and buffers");
|
||||
|
||||
void CO_ReplaceAll(mco_coro *co) {
|
||||
CCtx *ctx = GetCoroutineContext();
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
String16 string = FetchLoadWord(main.view);
|
||||
String string8 = ToString(ctx->arena, string);
|
||||
String16 needle = {};
|
||||
String16 replace = {};
|
||||
|
||||
{
|
||||
JumpTempBuffer(&main);
|
||||
NextActiveWindowID = main.window->id;
|
||||
RawAppendf(main.buffer, ":Submit (enter) :Cancel (escape)\nString to search for::%S", string8);
|
||||
|
||||
Caret field_seek = BaseFindNext(main.buffer, u"for::", MakeCaret(0), SeekFlag_None);
|
||||
main.view->carets[0] = MakeCaret(main.buffer->len, field_seek.range.max);
|
||||
AddUIAction(main.view, "Submit", EnterKey, UIAction_Yes);
|
||||
AddUIAction(main.view, "Cancel", EscapeKey, UIAction_Cancel);
|
||||
UIAction action = WaitForUIAction(co, main);
|
||||
if (action == UIAction_Cancel) {
|
||||
return;
|
||||
}
|
||||
|
||||
field_seek = BaseFindNext(main.buffer, u"for::", MakeCaret(0), SeekFlag_None);
|
||||
Range range = {field_seek.range.max, main.buffer->len};
|
||||
needle = Copy16(ctx->arena, GetString(main.buffer, range));
|
||||
}
|
||||
|
||||
{
|
||||
JumpTempBuffer(&main);
|
||||
NextActiveWindowID = main.window->id;
|
||||
RawAppendf(main.buffer, ":Submit (enter) :Cancel (escape)\nString to replace with::%S", ToString(ctx->arena, needle));
|
||||
|
||||
Caret field_seek = BaseFindNext(main.buffer, u"with::", MakeCaret(0), SeekFlag_None);
|
||||
main.view->carets[0] = MakeCaret(main.buffer->len, field_seek.range.max);
|
||||
AddUIAction(main.view, "Submit", EnterKey, UIAction_Yes);
|
||||
AddUIAction(main.view, "Cancel", EscapeKey, UIAction_Cancel);
|
||||
UIAction action = WaitForUIAction(co, main);
|
||||
if (action == UIAction_Cancel) {
|
||||
return;
|
||||
}
|
||||
|
||||
field_seek = BaseFindNext(main.buffer, u"with::", MakeCaret(0), SeekFlag_None);
|
||||
Range range = {field_seek.range.max, main.buffer->len};
|
||||
replace = GetString(main.buffer, range);
|
||||
}
|
||||
|
||||
ForItem (buffer, Buffers) {
|
||||
if (buffer->special || buffer->temp || buffer->dont_try_to_save_in_bulk_ops) {
|
||||
continue;
|
||||
}
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
if (buffer->is_dir) {
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
View *view = OpenBufferView(buffer->name);
|
||||
if (SelectAllOccurences(view, needle)) {
|
||||
Replace(view, replace);
|
||||
}
|
||||
}
|
||||
} RegisterCoroutineCommand(CO_ReplaceAll, "ctrl-shift-r", "Search and replace over the entire project, you need to select a text with format like this 'FindAnd@>ReplaceWith' and executing the command will change all occurences of FindAnd to ReplaceWith");
|
||||
|
||||
#endif
|
||||
39
src/plugin_load_vcvars.cpp
Normal file
39
src/plugin_load_vcvars.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
void Windows_SetupVCVarsall(mco_coro *co) {
|
||||
View *view = NULL;
|
||||
{
|
||||
Scratch scratch;
|
||||
String working_dir = ProjectFolder;
|
||||
String buffer_name = GetUniqueBufferName(working_dir, "vcvarsall-");
|
||||
String cmd = Format(scratch, "\"%S\" && set", VCVarsPath);
|
||||
view = OpenBufferView(buffer_name);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
buffer->dont_try_to_save_in_bulk_ops = true;
|
||||
Exec(ShellArgs(scratch, view->id, cmd, working_dir));
|
||||
}
|
||||
for (;;) {
|
||||
if (!ProcessIsActive(view->id)) {
|
||||
break;
|
||||
}
|
||||
Yield(co);
|
||||
}
|
||||
|
||||
Scratch scratch;
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
String16 string16 = GetString(buffer);
|
||||
String string = ToString(scratch, string16);
|
||||
Array<String> lines = Split(scratch, string, "\n");
|
||||
ProcessEnviroment.len = 0;
|
||||
For (lines) {
|
||||
String s = Trim(it);
|
||||
Array<String> values = Split(scratch, s, "=");
|
||||
if (values.len == 1) continue;
|
||||
Add(&ProcessEnviroment, Copy(GetSystemAllocator(), s));
|
||||
}
|
||||
}
|
||||
|
||||
void LoadVCVars() {
|
||||
RemoveCoroutine("Windows_SetupVCVarsall");
|
||||
CCtx *co_data = AddCoroutine(Windows_SetupVCVarsall);
|
||||
co_data->dont_wait_until_resolved = true;
|
||||
ResumeCoroutine(co_data);
|
||||
}
|
||||
31
src/plugin_profiler.cpp
Normal file
31
src/plugin_profiler.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#if PLUGIN_PROFILER
|
||||
|
||||
void CMD_BeginProfile() {
|
||||
BeginProfiler();
|
||||
} RegisterCommand(CMD_BeginProfile, "", "Start gathering profile data");
|
||||
|
||||
void CMD_EndProfile() {
|
||||
EndProfiler();
|
||||
} RegisterCommand(CMD_EndProfile, "", "Stop gathering profile data and write to disk");
|
||||
|
||||
SPALL_FN void BeginProfiler() {
|
||||
ActivelyProfiling = true;
|
||||
Scratch scratch;
|
||||
String filename = Format(scratch, "te%llu.spall", (unsigned long long)GetTimeNanos());
|
||||
spall_init_file(filename.data, 1, &spall_ctx);
|
||||
|
||||
int buffer_size = 1 * 1024 * 1024;
|
||||
unsigned char *buffer = (unsigned char *)malloc(buffer_size);
|
||||
spall_buffer = {buffer, (size_t)buffer_size};
|
||||
|
||||
spall_buffer_init(&spall_ctx, &spall_buffer);
|
||||
}
|
||||
|
||||
SPALL_FN void EndProfiler() {
|
||||
ActivelyProfiling = false;
|
||||
spall_buffer_quit(&spall_ctx, &spall_buffer);
|
||||
free(spall_buffer.data);
|
||||
spall_quit(&spall_ctx);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
#if BUILD_DEBUG
|
||||
#if PLUGIN_PROFILER
|
||||
// SPDX-FileCopyrightText: © 2023 Phillip Trudeau-Tavara <pmttavara@protonmail.com>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
@@ -21,9 +21,6 @@ TODO: Optional Helper APIs:
|
||||
spall_ring_flush
|
||||
*/
|
||||
|
||||
#ifndef SPALL_H
|
||||
#define SPALL_H
|
||||
|
||||
#if !defined(_MSC_VER) || defined(__clang__)
|
||||
#define SPALL_NOINSTRUMENT __attribute__((no_instrument_function))
|
||||
#define SPALL_FORCEINLINE __attribute__((always_inline))
|
||||
@@ -140,9 +137,10 @@ typedef struct SpallBuffer {
|
||||
uint64_t first_ts;
|
||||
} SpallBuffer;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
static SpallProfile spall_ctx;
|
||||
static SpallBuffer spall_buffer;
|
||||
uint64_t GetTimeNanos(void);
|
||||
bool ActivelyProfiling = false;
|
||||
|
||||
SPALL_FN SPALL_FORCEINLINE bool spall__file_write(SpallProfile *ctx, const void *p, size_t n) {
|
||||
if (fwrite(p, n, 1, (FILE *)ctx->data) != 1) return false;
|
||||
@@ -296,6 +294,7 @@ SPALL_FN bool spall_flush(SpallProfile *ctx) {
|
||||
}
|
||||
|
||||
SPALL_FN bool spall_buffer_init(SpallProfile *ctx, SpallBuffer *wb) {
|
||||
(void)(ctx);
|
||||
// Fails if buffer is not big enough to contain at least one event!
|
||||
if (wb->length < sizeof(SpallBufferHeader) + sizeof(SpallBeginEventMax)) {
|
||||
return false;
|
||||
@@ -307,6 +306,7 @@ SPALL_FN bool spall_buffer_init(SpallProfile *ctx, SpallBuffer *wb) {
|
||||
|
||||
|
||||
SPALL_FN SPALL_FORCEINLINE bool spall_buffer_begin_args(SpallProfile *ctx, SpallBuffer *wb, const char *name, int32_t name_len, const char *args, int32_t args_len, uint64_t when) {
|
||||
if (!ActivelyProfiling) return false;
|
||||
if ((wb->head + sizeof(SpallBeginEventMax)) > wb->length) {
|
||||
if (!spall__buffer_flush(ctx, wb, when)) {
|
||||
return false;
|
||||
@@ -323,6 +323,7 @@ SPALL_FN bool spall_buffer_begin(SpallProfile *ctx, SpallBuffer *wb, const char
|
||||
}
|
||||
|
||||
SPALL_FN bool spall_buffer_end(SpallProfile *ctx, SpallBuffer *wb, uint64_t when) {
|
||||
if (!ActivelyProfiling) return false;
|
||||
if ((wb->head + sizeof(SpallEndEvent)) > wb->length) {
|
||||
if (!spall__buffer_flush(ctx, wb, when)) {
|
||||
return false;
|
||||
@@ -355,60 +356,25 @@ SPALL_FN bool spall_buffer_name_process(SpallProfile *ctx, SpallBuffer *wb, cons
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // SPALL_H
|
||||
|
||||
|
||||
static SpallProfile spall_ctx;
|
||||
static SpallBuffer spall_buffer;
|
||||
uint64_t GetTimeNanos(void);
|
||||
|
||||
void BeginProfiler() {
|
||||
spall_init_file("te.spall", 1, &spall_ctx);
|
||||
|
||||
int buffer_size = 1 * 1024 * 1024;
|
||||
unsigned char *buffer = (unsigned char *)malloc(buffer_size);
|
||||
spall_buffer = {buffer, (size_t)buffer_size};
|
||||
|
||||
spall_buffer_init(&spall_ctx, &spall_buffer);
|
||||
}
|
||||
|
||||
void EndProfiler() {
|
||||
spall_buffer_quit(&spall_ctx, &spall_buffer);
|
||||
free(spall_buffer.data);
|
||||
spall_quit(&spall_ctx);
|
||||
}
|
||||
|
||||
void _BeginProfileScope(const char *name, int len) {
|
||||
spall_buffer_begin(&spall_ctx, &spall_buffer,
|
||||
name, // name of your name
|
||||
len, // name len minus the null terminator
|
||||
GetTimeNanos() // timestamp in microseconds -- start of your timing block
|
||||
);
|
||||
}
|
||||
#define BeginProfileScope(name) _BeginProfileScope(#name, sizeof(#name) - 1)
|
||||
|
||||
void EndProfileScope() {
|
||||
spall_buffer_end(&spall_ctx, &spall_buffer,
|
||||
GetTimeNanos() // timestamp in microseconds -- end of your timing block
|
||||
);
|
||||
}
|
||||
#define ProfileScopeEx(name) ProfileScopeClass PROFILE_SCOPE_VAR_((name).data, (int)(name).len)
|
||||
#define ProfileScope(name) ProfileScopeClass PROFILE_SCOPE_VAR_##name(#name, sizeof(#name) - 1)
|
||||
#define ProfileFunction() ProfileScopeClass PROFILE_SCOPE_FUNCTION(__FUNCTION__, sizeof(__FUNCTION__) - 1)
|
||||
#define BeginProfileScopeEx(name, len) spall_buffer_begin(&spall_ctx, &spall_buffer, (name), (len), GetTimeNanos())
|
||||
#define BeginProfileScope(name) BeginProfileScopeEx(#name, sizeof(#name) - 1)
|
||||
#define EndProfileScope() spall_buffer_end(&spall_ctx, &spall_buffer, GetTimeNanos())
|
||||
struct ProfileScopeClass {
|
||||
ProfileScopeClass(const char *name, int len) { _BeginProfileScope(name, len); }
|
||||
ProfileScopeClass(const char *name, int len) { BeginProfileScopeEx(name, len); }
|
||||
~ProfileScopeClass() { EndProfileScope(); }
|
||||
};
|
||||
#define ProfileScopeEx(name) ProfileScopeClass PROFILE_SCOPE_VAR_((name).data, (int)(name).len)
|
||||
#define ProfileScope(name) ProfileScopeClass PROFILE_SCOPE_VAR_##name(#name, sizeof(#name) - 1)
|
||||
#define ProfileFunction() ProfileScopeClass PROFILE_SCOPE_FUNCTION(__FUNCTION__, sizeof(__FUNCTION__) - 1)
|
||||
SPALL_FN void BeginProfiler();
|
||||
SPALL_FN void EndProfiler();
|
||||
#else
|
||||
#define ProfileScopeEx(name)
|
||||
#define ProfileScope(name)
|
||||
#define ProfileFunction()
|
||||
#define BeginProfiler()
|
||||
#define EndProfiler()
|
||||
#define BeginProfileScopeEx(name, len)
|
||||
#define BeginProfileScope(name)
|
||||
#define EndProfileScope()
|
||||
#endif
|
||||
137
src/plugin_project_management.cpp
Normal file
137
src/plugin_project_management.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
void SetProjectFolder(String dir) {
|
||||
ProjectFolder = Intern(&GlobalInternTable, dir);
|
||||
Scratch scratch;
|
||||
For (Buffers) {
|
||||
if (it->special) {
|
||||
String name = SkipToLastSlash(it->name);
|
||||
it->name = Intern(&GlobalInternTable, Format(scratch, "%S/%S", dir, name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CO_OpenCode(mco_coro *co) {
|
||||
CCtx *ctx = GetCoroutineContext();
|
||||
Array<String> patterns = SplitWhitespace(ctx->arena, OpenCodePatterns);
|
||||
Array<String> exclude_patterns = SplitWhitespace(ctx->arena, OpenCodeExcludePatterns);
|
||||
Array<String> dirs = {ctx->arena};
|
||||
Add(&dirs, Copy(ctx->arena, ProjectFolder));
|
||||
for (int diri = 0; diri < dirs.len; diri += 1) {
|
||||
for (FileIter it = IterateFiles(ctx->arena, dirs[diri]); IsValid(it); Advance(&it)) {
|
||||
bool should_open = true;
|
||||
if (!it.is_directory) {
|
||||
should_open = false;
|
||||
ForItem (ending, patterns) {
|
||||
if (EndsWith(it.absolute_path, ending)) {
|
||||
should_open = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ForItem (ending, exclude_patterns) {
|
||||
if (EndsWith(it.absolute_path, ending)) {
|
||||
should_open = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!should_open) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (it.is_directory) {
|
||||
Add(&dirs, it.absolute_path);
|
||||
} else {
|
||||
BufferOpenFile(it.absolute_path);
|
||||
}
|
||||
|
||||
Yield(co);
|
||||
}
|
||||
}
|
||||
} RegisterCoroutineCommand(
|
||||
CO_OpenCode,
|
||||
"",
|
||||
"Open all code files in current ProjectFolder, the code files are determined through NonCodePatterns_EndsWith config variable list",
|
||||
data->dont_wait_until_resolved = true;
|
||||
);
|
||||
|
||||
void OpenProject(String directory) {
|
||||
SetProjectFolder(directory);
|
||||
#if PLUGIN_CONFIG
|
||||
Scratch scratch;
|
||||
for (FileIter it = IterateFiles(scratch, directory); IsValid(it); Advance(&it)) {
|
||||
if (EndsWith(it.filename, "project.te")) {
|
||||
LoadConfig(it.absolute_path);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void CMD_OpenProject() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
String directory = GetDirectory(main.buffer);
|
||||
OpenProject(directory);
|
||||
} RegisterCommand(CMD_OpenProject, "", "Sets the project directory, opens the project file etc.");
|
||||
|
||||
String16 CreateProjectBuildBat = uR"==(@echo off
|
||||
mkdir build
|
||||
pushd build
|
||||
cl ../src/main.cpp /Zi /FC /nologo /WX /W3 /wd4200 /wd4334 /diagnostics:column
|
||||
popd
|
||||
)==";
|
||||
|
||||
String16 CreateProjectBuildSh = uR"==(#!/usr/bin/bash
|
||||
mkdir build
|
||||
cd build
|
||||
clang ../src/main.cpp -g -Wall -Wno-missing-braces -Wno-writable-strings -fdiagnostics-absolute-paths
|
||||
)==";
|
||||
|
||||
String16 CreateProjectMain = uR"==(#include <stdio.h>
|
||||
int main() {
|
||||
printf("hello world!\n");
|
||||
return 0;
|
||||
}
|
||||
)==";
|
||||
|
||||
void CO_CreateProject(mco_coro *co) {
|
||||
String16 project_name16 = QueryUserString(co, "Please tell me, how would you like to name your project?");
|
||||
String directory = GetPrimaryDirectory();
|
||||
|
||||
// - project_directory
|
||||
// - project.te
|
||||
// - build.bat
|
||||
// - build.sh
|
||||
// - src
|
||||
// - main.c
|
||||
{
|
||||
Scratch scratch;
|
||||
String project_name = ToString(scratch, project_name16);
|
||||
String project_dir = Format(scratch, "%S/%S", directory, project_name);
|
||||
String src_dir = Format(scratch, "%S/src", project_dir);
|
||||
MakeDir(project_dir);
|
||||
MakeDir(src_dir);
|
||||
SetProjectFolder(project_dir);
|
||||
|
||||
Buffer *project = BufferOpenFile(Format(scratch, "%S/project.te", project_dir));
|
||||
RawAppendf(project, ":OpenCode\n:Set BinaryUnderDebug '%S/build/main.exe'\n", project_dir);
|
||||
SaveBuffer(project);
|
||||
|
||||
Buffer *build_bat = BufferOpenFile(Format(scratch, "%S/build.bat", project_dir));
|
||||
RawAppend(build_bat, CreateProjectBuildBat);
|
||||
SaveBuffer(build_bat);
|
||||
|
||||
Buffer *build_sh = BufferOpenFile(Format(scratch, "%S/build.sh", project_dir));
|
||||
RawAppend(build_sh, CreateProjectBuildSh);
|
||||
SaveBuffer(build_sh);
|
||||
|
||||
Buffer *main_file = BufferOpenFile(Format(scratch, "%S/main.cpp", src_dir));
|
||||
RawAppend(main_file, CreateProjectMain);
|
||||
SaveBuffer(main_file);
|
||||
|
||||
OpenProject(project_dir);
|
||||
Open(main_file->name);
|
||||
}
|
||||
|
||||
CO_OpenCode(co);
|
||||
} RegisterCoroutineCommand(CO_CreateProject, "", "Creates a project in current buffer directory with template files and all that, asks user for input etc.");
|
||||
1
src/plugin_project_management.h
Normal file
1
src/plugin_project_management.h
Normal file
@@ -0,0 +1 @@
|
||||
void SetProjectFolder(String name);
|
||||
70
src/plugin_record_events.cpp
Normal file
70
src/plugin_record_events.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
Buffer *EventBuffer;
|
||||
|
||||
void Serialize(Buffer *buffer, String name, EventKind *kind) {
|
||||
RawAppendf(buffer, "ev.%S = %s; ", name, EventKindStrings[*kind]);
|
||||
}
|
||||
|
||||
void Serialize(Buffer *buffer, String name, Int *datum) {
|
||||
if (*datum == 0) {
|
||||
return;
|
||||
}
|
||||
if (name == "key") {
|
||||
RawAppendf(buffer, "ev.%S = %s; ", name, SDLKeycodeToName((SDL_Keycode)*datum));
|
||||
} else {
|
||||
RawAppendf(buffer, "ev.%S = %lld; ", name, (long long)*datum);
|
||||
}
|
||||
}
|
||||
|
||||
void Serialize(Buffer *buffer, String name, uint32_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(buffer, name, &d);
|
||||
*datum = (uint32_t)d;
|
||||
}
|
||||
|
||||
void Serialize(Buffer *buffer, String name, uint8_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(buffer, name, &d);
|
||||
*datum = (uint8_t)d;
|
||||
}
|
||||
|
||||
void Serialize(Buffer *buffer, String name, int16_t *datum) {
|
||||
Int d = *datum;
|
||||
Serialize(buffer, name, &d);
|
||||
*datum = (int16_t)d;
|
||||
}
|
||||
|
||||
void Serialize(Buffer *buffer, String name, float *datum) {
|
||||
if (*datum == 0.f) {
|
||||
return;
|
||||
}
|
||||
RawAppendf(buffer, "ev.%S = %f; ", name, *datum);
|
||||
}
|
||||
|
||||
void Serialize(Buffer *buffer, String name, char **text) {
|
||||
String str = *text;
|
||||
if (str.len == 0) {
|
||||
return;
|
||||
}
|
||||
RawAppendf(buffer, "ev.%S = \"%S\"; ", name, str);
|
||||
}
|
||||
|
||||
void SerializeBegin(Buffer *buffer) {
|
||||
RawAppendf(buffer, "{Event ev = {};");
|
||||
}
|
||||
|
||||
void SerializeEnd(Buffer *buffer) {
|
||||
RawAppendf(buffer, "Add(&EventPlayback, ev);}\n");
|
||||
}
|
||||
|
||||
// :Event
|
||||
void Serialize(Buffer *buffer, Event *e) {
|
||||
SerializeBegin(buffer);
|
||||
#define X(TYPE, KIND, NAME) Serialize(buffer, #NAME, &e->NAME);
|
||||
EVENT_FIELDS
|
||||
#undef X
|
||||
SerializeEnd(buffer);
|
||||
}
|
||||
|
||||
void CMD_CopyEvents() {
|
||||
SaveStringInClipboard(GetString(EventBuffer));
|
||||
} RegisterCommand(CMD_CopyEvents, "ctrl-shift-alt-j", "Copy all the events from the EventBuffer");
|
||||
1
src/plugin_record_gc.cpp
Normal file
1
src/plugin_record_gc.cpp
Normal file
@@ -0,0 +1 @@
|
||||
Buffer *GCInfoBuffer;
|
||||
2314
src/plugin_remedybg.cpp
Normal file
2314
src/plugin_remedybg.cpp
Normal file
File diff suppressed because it is too large
Load Diff
129
src/plugin_search_open_buffers.cpp
Normal file
129
src/plugin_search_open_buffers.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
struct SearchOpenBuffersParams {
|
||||
String16 needle;
|
||||
BufferID buffer;
|
||||
};
|
||||
|
||||
void Coro_SearchOpenBuffers(mco_coro *co) {
|
||||
CCtx *ctx = GetCoroutineContext();
|
||||
SearchOpenBuffersParams *param = (SearchOpenBuffersParams *)ctx->user_ctx;
|
||||
|
||||
Array<BufferID> buffers = {ctx->arena};
|
||||
For (Buffers) {
|
||||
Add(&buffers, it->id);
|
||||
}
|
||||
|
||||
SeekFlag flag = SearchCaseSensitive ? SeekFlag_None : SeekFlag_IgnoreCase;
|
||||
if (SearchWordBoundary) {
|
||||
flag |= SeekFlag_WordBoundary;
|
||||
}
|
||||
bool ignore_case = flag & SeekFlag_IgnoreCase ? true : false;
|
||||
bool word_boundary = flag & SeekFlag_WordBoundary;
|
||||
Int max_iters_per_yield = 50000;
|
||||
Int yield_iters = 0;
|
||||
|
||||
ForItem (id, buffers) {
|
||||
Buffer *it = GetBuffer(id, NULL);
|
||||
if (it == NULL || it->special || it->temp || it->dont_try_to_save_in_bulk_ops) {
|
||||
continue;
|
||||
}
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
if (it->is_dir) {
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
// :Seek
|
||||
Buffer *out_buffer = GetBuffer(param->buffer);
|
||||
String16 find = param->needle;
|
||||
String16 string = GetString(it);
|
||||
for (int64_t i = 0; i < string.len; i++) {
|
||||
String16 substring = GetSlice(string, i, i + find.len);
|
||||
if (AreEqual(substring, find, ignore_case)) {
|
||||
bool ok_boundary = true;
|
||||
if (word_boundary) {
|
||||
String16 left = GetSlice(string, i - 1, i);
|
||||
String16 right = GetSlice(string, i + find.len, i + find.len + 1);
|
||||
if (left.len != 0 && !IsNonWord(left.data[0])) {
|
||||
ok_boundary = false;
|
||||
}
|
||||
if (right.len != 0 && !IsNonWord(right.data[0])) {
|
||||
ok_boundary = false;
|
||||
}
|
||||
}
|
||||
if (ok_boundary) {
|
||||
Int line = PosToLine(it, i);
|
||||
Range range = GetLineRangeWithoutNL(it, line);
|
||||
Int column = i - range.min;
|
||||
String16 line_string = GetString(it, range);
|
||||
U8 *pos = ctx->arena.start;
|
||||
String line_string8 = ToString(ctx->arena, line_string);
|
||||
RawAppendf(out_buffer, "%S ||> %S:%lld:%lld\n", line_string8, it->name, (long long)line + 1, (long long)column + 1);
|
||||
Unwind(&ctx->arena, pos);
|
||||
}
|
||||
}
|
||||
|
||||
yield_iters += 1;
|
||||
if (yield_iters > max_iters_per_yield) {
|
||||
Yield(co);
|
||||
yield_iters = 0;
|
||||
out_buffer = GetBuffer(param->buffer, NULL);
|
||||
if (out_buffer == NULL) {
|
||||
return;
|
||||
}
|
||||
it = GetBuffer(id, NULL);
|
||||
if (it == NULL) {
|
||||
goto skip_buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
skip_buffer:;
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateSearchOpenBuffersView() {
|
||||
Scratch scratch;
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
String16 line_string = GetLineStringWithoutNL(active.buffer, 0);
|
||||
uint64_t hash = HashBytes(line_string.data, line_string.len * sizeof(char16_t));
|
||||
if (active.view->prev_search_line_hash != hash) {
|
||||
active.view->prev_search_line_hash = hash;
|
||||
if (line_string.len > 0) {
|
||||
// @todo: somehow reintroduce history but manual, do the UndoKinds EditKind or something
|
||||
// and implement EditKind_ExactBufferContent just save the
|
||||
Caret caret = active.view->carets[0];
|
||||
SelectEntireBuffer(active.view);
|
||||
Replace(active.view, line_string);
|
||||
Append(active.view, "\n", false);
|
||||
|
||||
RemoveCoroutine("Coro_SearchOpenBuffers");
|
||||
CCtx *dat = AddCoroutine(Coro_SearchOpenBuffers);
|
||||
SearchOpenBuffersParams *param = AllocType(dat->arena, SearchOpenBuffersParams);
|
||||
param->needle = Copy16(dat->arena, line_string);
|
||||
param->buffer = active.buffer->id;
|
||||
dat->user_ctx = param;
|
||||
dat->dont_wait_until_resolved = true;
|
||||
ResumeCoroutine(dat);
|
||||
|
||||
active.view->carets[0] = caret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CMD_SearchOpenBuffers() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
String16 string = {};
|
||||
if (main.view->carets.len == 1 && GetSize(main.view->carets[0]) > 0) {
|
||||
string = GetString(main.buffer, main.view->carets[0].range);
|
||||
}
|
||||
|
||||
NextActiveWindowID = main.window->id;
|
||||
JumpTempBuffer(&main);
|
||||
main.buffer->no_history = true;
|
||||
AddCommand(&main.view->commands, "Open", OpenKeySet, CMD_OpenAndSetGotoNext);
|
||||
main.view->update_hook = UpdateSearchOpenBuffersView;
|
||||
if (string.len) {
|
||||
SelectRange(main.view, GetLineRangeWithoutNL(main.buffer, 0));
|
||||
Replace(main.view, string);
|
||||
}
|
||||
SelectRange(main.view, GetLineRangeWithoutNL(main.buffer, 0));
|
||||
} RegisterCommand(CMD_SearchOpenBuffers, "ctrl-shift-f", "Interactive search over the entire project in a new buffer view");
|
||||
@@ -1,6 +1,9 @@
|
||||
void CMD_Search(HookParam param) {
|
||||
Int SearchBufferChangeID;
|
||||
|
||||
void CMD_Search() {
|
||||
BSet main = GetBSet(ActiveWindowID);
|
||||
String16 string = {};
|
||||
main.window->search_bar_anchor = main.view->carets[0];
|
||||
if (main.view->carets.len == 1 && GetSize(main.view->carets[0]) > 0) {
|
||||
string = GetString(main.buffer, main.view->carets[0].range);
|
||||
}
|
||||
@@ -12,6 +15,7 @@ void CMD_Search(HookParam param) {
|
||||
Replace(set.view, string);
|
||||
SelectEntireBuffer(set.view);
|
||||
}
|
||||
SearchBufferChangeID = set.buffer->change_id;
|
||||
} RegisterCommand(CMD_Search, "ctrl-f", "Open up a search window");
|
||||
|
||||
void SearchWindowFindNext(bool forward = true) {
|
||||
@@ -19,27 +23,28 @@ void SearchWindowFindNext(bool forward = true) {
|
||||
BSet set = GetBSet(SearchWindowID);
|
||||
String16 seek = GetString(set.buffer, GetRange(set.buffer));
|
||||
Find(main.view, seek, forward);
|
||||
main.window->search_bar_anchor = main.view->carets[0];
|
||||
CenterView(PrimaryWindowID);
|
||||
if (!IsMainCaretOnScreen(main.window).caret_on_screen) {
|
||||
CenterView(main.window->id);
|
||||
}
|
||||
}
|
||||
|
||||
void CMD_SearchNextInSearch(HookParam param) {
|
||||
void CMD_SearchNextInSearch() {
|
||||
SearchWindowFindNext(true);
|
||||
}
|
||||
|
||||
void CMD_SearchPrevInSearch(HookParam param) {
|
||||
void CMD_SearchPrevInSearch() {
|
||||
SearchWindowFindNext(false);
|
||||
}
|
||||
|
||||
void CMD_SearchNext(HookParam param) {
|
||||
void CMD_SearchNext() {
|
||||
SearchWindowFindNext(true);
|
||||
} RegisterCommand(CMD_SearchNext, "f3", "Go to the next occurence of the search window needle");
|
||||
|
||||
void CMD_SearchPrev(HookParam param) {
|
||||
void CMD_SearchPrev() {
|
||||
SearchWindowFindNext(false);
|
||||
} RegisterCommand(CMD_SearchPrev, "shift-f3", "Go to the previous occurence of the search window needle");
|
||||
|
||||
void CMD_SearchAll(HookParam param) {
|
||||
void CMD_SearchAll() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
BSet set = GetBSet(SearchWindowID);
|
||||
String16 needle = GetString(set.buffer, GetRange(set.buffer));
|
||||
@@ -47,41 +52,18 @@ void CMD_SearchAll(HookParam param) {
|
||||
set.window->visible = false;
|
||||
} RegisterCommand(CMD_SearchAll, "alt-f3", "Use the search window needle and seek all the possible occurences in current buffer");
|
||||
|
||||
void CMD_ToggleCaseSensitiveSearch(HookParam param) {
|
||||
void CMD_ToggleCaseSensitiveSearch() {
|
||||
SearchCaseSensitive = !SearchCaseSensitive;
|
||||
} RegisterCommand(CMD_ToggleCaseSensitiveSearch, "alt-c", "Text editor wide search toggle, should apply to most search things");
|
||||
|
||||
void CMD_ToggleSearchWordBoundary(HookParam param) {
|
||||
void CMD_ToggleSearchWordBoundary() {
|
||||
SearchWordBoundary = !SearchWordBoundary;
|
||||
} RegisterCommand(CMD_ToggleSearchWordBoundary, "alt-w", "Text editor wide search toggle, should apply to most search things");
|
||||
|
||||
void UpdateSearchWindow(HookParam param) {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
if (active.window->id == SearchWindowID && active.buffer->begin_frame_change_id != active.buffer->change_id) {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
BSet set = GetBSet(SearchWindowID);
|
||||
main.view->carets[0] = main.window->search_bar_anchor;
|
||||
String16 seek = GetString(set.buffer, GetRange(set.buffer));
|
||||
Find(main.view, seek, true);
|
||||
CenterView(main.window->id);
|
||||
}
|
||||
} RegisterHook(UpdateSearchWindow, HookKind_AppUpdate, "", "Update the search window");
|
||||
|
||||
void LayoutSearchWindow(HookParam param) {
|
||||
auto p = param.layout;
|
||||
Rect2I copy_rect = *p.rect;
|
||||
if (!p.window->visible) {
|
||||
p.rect = ©_rect;
|
||||
}
|
||||
Int barsize = GetExpandingBarSize(p.window);
|
||||
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
|
||||
p.window->line_numbers_rect = CutLeft(&p.window->document_rect, p.window->font->char_spacing * 6);
|
||||
}
|
||||
|
||||
void InitSearchWindow(HookParam param) {
|
||||
void InitSearchWindow() {
|
||||
Window *window = CreateWind();
|
||||
SearchWindowID = window->id;
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "search"));
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(ProjectFolder, "search"));
|
||||
buffer->special = true;
|
||||
SearchBufferID = buffer->id;
|
||||
View *view = CreateView(buffer->id);
|
||||
@@ -96,8 +78,35 @@ void InitSearchWindow(HookParam param) {
|
||||
window->visible = false;
|
||||
window->lose_visibility_on_escape = true;
|
||||
window->jump_history = false;
|
||||
AddCommand(&view->hooks, "SearchAll", "alt-enter", CMD_SearchAll);
|
||||
AddCommand(&view->hooks, "SearchPrevInSearch", "shift-enter", CMD_SearchPrevInSearch);
|
||||
AddCommand(&view->hooks, "SearchNextInSearch", "enter", CMD_SearchNextInSearch);
|
||||
AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutSearchWindow", "", LayoutSearchWindow);
|
||||
} RegisterHook(InitSearchWindow, HookKind_AppInit, "", "Init the search window");
|
||||
AddCommand(&view->commands, "SearchAll", AltEnterKeySet, CMD_SearchAll);
|
||||
AddCommand(&view->commands, "SearchPrevInSearch", ShiftEnterKeySet, CMD_SearchPrevInSearch);
|
||||
AddCommand(&view->commands, "SearchNextInSearch", EnterKey, CMD_SearchNextInSearch);
|
||||
}
|
||||
|
||||
void LayoutSearchWindow(Rect2I *rect, int16_t wx, int16_t wy) {
|
||||
Unused(wx); Unused(wy);
|
||||
Window *window = GetWindow(SearchWindowID);
|
||||
Rect2I copy_rect = *rect;
|
||||
if (!window->visible) {
|
||||
rect = ©_rect;
|
||||
}
|
||||
Int barsize = GetExpandingBarSize(window);
|
||||
window->document_rect = window->total_rect = CutBottom(rect, barsize);
|
||||
window->line_numbers_rect = CutLeft(&window->document_rect, window->font->char_spacing * 6);
|
||||
}
|
||||
|
||||
void UpdateSearchWindow() {
|
||||
if (ActiveWindowID == SearchWindowID) {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
if (SearchBufferChangeID != active.buffer->change_id) {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
main.view->carets[0] = main.window->search_bar_anchor;
|
||||
String16 seek = GetString(active.buffer, GetRange(active.buffer));
|
||||
Find(main.view, seek, true);
|
||||
if (!IsMainCaretOnScreen(main.window).caret_on_screen) {
|
||||
CenterView(main.window->id);
|
||||
}
|
||||
SearchBufferChangeID = active.buffer->change_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
src/plugin_search_window.h
Normal file
3
src/plugin_search_window.h
Normal file
@@ -0,0 +1,3 @@
|
||||
void InitSearchWindow();
|
||||
void LayoutSearchWindow(Rect2I *rect, int16_t wx, int16_t wy);
|
||||
void UpdateSearchWindow();
|
||||
99
src/plugin_status_window.cpp
Normal file
99
src/plugin_status_window.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
WindowID StatusWindowID;
|
||||
|
||||
void InitStatusWindow() {
|
||||
Window *window = CreateWind();
|
||||
StatusWindowID = window->id;
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(ProjectFolder, "status_bar"));
|
||||
buffer->special = true;
|
||||
View *view = CreateView(buffer->id);
|
||||
view->special = true;
|
||||
window->active_view = view->id;
|
||||
window->draw_line_numbers = false;
|
||||
window->draw_scrollbar = false;
|
||||
window->draw_line_highlight = true;
|
||||
window->secondary_window_style = true;
|
||||
window->primary = false;
|
||||
window->jump_history = false;
|
||||
window->lose_focus_on_escape = true;
|
||||
}
|
||||
|
||||
void LayoutStatusWindow(Rect2I *rect, int16_t wx, int16_t wy) {
|
||||
Unused(wx); Unused(wy);
|
||||
Window *window = GetWindow(StatusWindowID);
|
||||
Rect2I copy_rect = *rect;
|
||||
if (!window->visible) {
|
||||
rect = ©_rect;
|
||||
}
|
||||
Int barsize = GetExpandingBarSize(window);
|
||||
window->document_rect = window->total_rect = CutBottom(rect, barsize);
|
||||
}
|
||||
|
||||
void UpdateStatusWindow() {
|
||||
ProfileFunction();
|
||||
Scratch scratch;
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
BSet title = GetBSet(StatusWindowID);
|
||||
title.view->scroll.y = 0;
|
||||
if (title.window->id == ActiveWindowID) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if 0
|
||||
Vec2I doc_rect = GetSize(title.window->document_rect);
|
||||
Int width_in_chars = doc_rect.x / title.window->font->char_spacing;
|
||||
|
||||
char buff[1024];
|
||||
for (int i = 0; i < 1024; i += 1) buff[i] = ' ';
|
||||
|
||||
RawReplaceText(title.buffer, GetRange(title.buffer), u"");
|
||||
RawAppendf(title.buffer, "%.*s", width_in_chars - 1, buff);
|
||||
RawAppendf(title.buffer, "@");
|
||||
return;
|
||||
#endif
|
||||
|
||||
String16 buffer_string = GetString(title.buffer);
|
||||
Range replace_range = {0, title.buffer->len};
|
||||
Seek(buffer_string, u"\n", &replace_range.max);
|
||||
|
||||
Caret caret = main.view->carets[0];
|
||||
XY xy = PosToXY(main.buffer, GetFront(caret));
|
||||
|
||||
String indicators = "";
|
||||
if (main.buffer->dirty) {
|
||||
indicators = Format(scratch, "%S!", indicators);
|
||||
}
|
||||
if (SearchCaseSensitive) {
|
||||
indicators = Format(scratch, "%SC", indicators);
|
||||
}
|
||||
if (SearchWordBoundary) {
|
||||
indicators = Format(scratch, "%SW", indicators);
|
||||
}
|
||||
|
||||
String commands = Format(scratch, ":Errors[%d]", ErrorCount);
|
||||
if (main.buffer->changed_on_disk) {
|
||||
commands = Format(scratch, "%S :Reopen", commands);
|
||||
}
|
||||
For (ActiveProcesses) {
|
||||
if (it.args.output_view == main.view->id) {
|
||||
commands = Format(scratch, "%S :KillProcess", commands);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String s = Format(scratch, "%S:%lld:%lld %S | :Prev :Next :Close %S", main.buffer->name, (long long)xy.line + 1ll, (long long)xy.col + 1ll, indicators, commands);
|
||||
For (ActiveProcesses) {
|
||||
if (it.args.output_view == main.view->id) {
|
||||
s = Format(scratch, "%S %lld :KillProcess", s, (long long)it.id);
|
||||
}
|
||||
}
|
||||
|
||||
String16 string = ToString16(scratch, s);
|
||||
String16 string_to_replace = GetString(title.buffer, replace_range);
|
||||
if (string_to_replace != string) {
|
||||
SelectRange(title.view, replace_range);
|
||||
ReplaceEx(scratch, title.view, string);
|
||||
}
|
||||
|
||||
SelectRange(title.view, MakeRange(0));
|
||||
ResetHistory(title.buffer);
|
||||
}
|
||||
123
src/plugin_tests.cpp
Normal file
123
src/plugin_tests.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#if PLUGIN_TESTS
|
||||
|
||||
|
||||
void Wait(mco_coro *co) {
|
||||
{Event ev = {};ev.kind = EVENT_KIND_INVALID; Add(&EventPlayback, ev);}
|
||||
Event *event = NULL;
|
||||
for (event = Yield(co); event->kind != EVENT_KIND_INVALID; event = Yield(co)) {
|
||||
}
|
||||
}
|
||||
|
||||
void Wait(mco_coro *co, int updates) {
|
||||
for (int i = 0; i < updates; i += 1) {
|
||||
{Event ev = {};ev.kind = EVENT_UPDATE; Add(&EventPlayback, ev);}
|
||||
}
|
||||
Wait(co);
|
||||
}
|
||||
|
||||
void OpenCloseCodeTest(mco_coro *co) {
|
||||
Int initial_buffers_count = Buffers.len;
|
||||
CO_OpenCode(co);
|
||||
Assert(Buffers.len > initial_buffers_count);
|
||||
CO_CloseAll(co);
|
||||
Wait(co);
|
||||
Assert(initial_buffers_count - 1 == Buffers.len);
|
||||
}
|
||||
|
||||
void CO_RunTests(mco_coro *co) {
|
||||
Testing = true;
|
||||
WaitForEvents = false;
|
||||
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LSHIFT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.shift = 1; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.shift = 1; ev.text = "M"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "e"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "m"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "e"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "s"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_SPACE; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = " "; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LSHIFT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.shift = 1; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "a"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "n"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "d"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_SPACE; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = " "; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "s"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "t"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "u"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "f"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "f"; Add(&EventPlayback, ev);}
|
||||
Wait(co);
|
||||
|
||||
String16 result = uR"FOO(
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Memes and stuff)FOO";
|
||||
BSet set = GetBSet(PrimaryWindowID);
|
||||
Assert(AreEqual(result, GetString(set.buffer)));
|
||||
|
||||
// Test the box selection
|
||||
CMD_New();
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_RETURN; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LSHIFT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.shift = 1; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.shift = 1; ev.text = "M"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "e"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "m"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "e"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "s"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_SPACE; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = " "; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LSHIFT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.shift = 1; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "a"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "n"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "d"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_SPACE; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = " "; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "s"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "t"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "u"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "f"; Add(&EventPlayback, ev);}
|
||||
{Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1412; ev.ywindow = 1032; ev.xmouse = 1234; ev.ymouse = 594; ev.text = "f"; Add(&EventPlayback, ev);}
|
||||
Wait(co);
|
||||
|
||||
result = uR"FOO(
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Memes and stuff)FOO";
|
||||
set = GetBSet(PrimaryWindowID);
|
||||
Assert(AreEqual(result, GetString(set.buffer)));
|
||||
|
||||
if (ErrorCount != 0) {
|
||||
Scratch scratch;
|
||||
String string = AllocCharString(scratch, LogBuffer);
|
||||
printf("TEXT EDITOR ERRORS!\n==========================\n%.*s", (int)string.len, string.data);
|
||||
}
|
||||
printf("RunTests OK\n");
|
||||
fflush(stdout);
|
||||
void CMD_QuitWithoutSaving();
|
||||
CMD_QuitWithoutSaving();
|
||||
} RegisterCoroutineCommand(CO_RunTests, "", "Basic tests");
|
||||
|
||||
|
||||
#endif
|
||||
82
src/plugin_window_management.cpp
Normal file
82
src/plugin_window_management.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
void CMD_JumpPrev() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
JumpToLastValidView(main.window);
|
||||
NextActiveWindowID = main.window->id;
|
||||
} RegisterCommand(CMD_JumpPrev, "ctrl-tab", "Go to the previous open view in primary window");
|
||||
|
||||
void CMD_Prev() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
main.window->skip_checkpoint = true;
|
||||
JumpBack(main.window);
|
||||
NextActiveWindowID = main.window->id;
|
||||
} RegisterCommand(CMD_Prev, "alt-q | mousex1 | ctrl-alt-minus", "Go to previous position (either previous view that was open or caret position) in the primary window");
|
||||
|
||||
void CMD_Next() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
main.window->skip_checkpoint = true;
|
||||
JumpForward(main.window);
|
||||
NextActiveWindowID = main.window->id;
|
||||
} RegisterCommand(CMD_Next, "alt-shift-q | mousex2 | ctrl-shift-minus", "Go to next position, after backtracking, in the primary window");
|
||||
|
||||
void CMD_FocusLeftWindow() {
|
||||
NextActiveWindowID = SwitchWindow(DIR_LEFT)->id;
|
||||
} RegisterCommand(CMD_FocusLeftWindow, "alt-left", "Switch the window focus to the window on the left position of current one");
|
||||
|
||||
void CMD_FocusRightWindow() {
|
||||
NextActiveWindowID = SwitchWindow(DIR_RIGHT)->id;
|
||||
} RegisterCommand(CMD_FocusRightWindow, "alt-right", "Switch the window focus to the window on the right position of current one");
|
||||
|
||||
void CMD_FocusWindow1() {
|
||||
NextActiveWindowID = GetOverlappingWindow({0,0}, GetWindow(ActiveWindowID))->id;
|
||||
} RegisterCommand(CMD_FocusWindow1, "ctrl-1", "Select the left-most window");
|
||||
|
||||
void CMD_FocusWindow2() {
|
||||
Window *first = GetOverlappingWindow({0,0}, GetWindow(ActiveWindowID));
|
||||
Vec2I p = GetSideOfWindow(first, DIR_RIGHT);
|
||||
NextActiveWindowID = GetOverlappingWindow(p, GetWindow(ActiveWindowID))->id;
|
||||
} RegisterCommand(CMD_FocusWindow2, "ctrl-2", "Select the window to the right of left-most window");
|
||||
|
||||
void CMD_FocusWindow3() {
|
||||
Window *first = GetOverlappingWindow({0,0});
|
||||
if (first) {
|
||||
Window *second = GetOverlappingWindow(GetSideOfWindow(first, DIR_RIGHT));
|
||||
if (second) {
|
||||
Window *third = GetOverlappingWindow(GetSideOfWindow(second, DIR_RIGHT));
|
||||
if (third) {
|
||||
NextActiveWindowID = third->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
} RegisterCommand(CMD_FocusWindow3, "ctrl-3", "Select the 3rd window, counting from left");
|
||||
|
||||
void CMD_FocusWindow4() {
|
||||
Window *first = GetOverlappingWindow({0,0});
|
||||
if (first) {
|
||||
Window *second = GetOverlappingWindow(GetSideOfWindow(first, DIR_RIGHT));
|
||||
if (second) {
|
||||
Window *third = GetOverlappingWindow(GetSideOfWindow(second, DIR_RIGHT));
|
||||
if (third) {
|
||||
Window *fourth = GetOverlappingWindow(GetSideOfWindow(third, DIR_RIGHT));
|
||||
if (fourth) NextActiveWindowID = fourth->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
} RegisterCommand(CMD_FocusWindow4, "ctrl-4", "Select the 4th window, counting from left");
|
||||
|
||||
void CMD_NewWindow() {
|
||||
CreateWind();
|
||||
} RegisterCommand(CMD_NewWindow, "ctrl-backslash", "Creates a new window");
|
||||
|
||||
void CMD_CloseWindow() {
|
||||
Close(PrimaryWindowID);
|
||||
} RegisterCommand(CMD_CloseWindow, "", "Close the last active primary window");
|
||||
|
||||
void CMD_GotoNextInList() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
GotoNextInList(main.window, 1);
|
||||
} RegisterCommand(CMD_GotoNextInList, "ctrl-e | f8", "For example: when jumping from build panel to build error, a jump point is setup, user can click this button to go over to the next compiler error");
|
||||
|
||||
void CMD_GotoPrevInList() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
GotoNextInList(main.window, -1);
|
||||
} RegisterCommand(CMD_GotoPrevInList, "alt-e | shift-f8", "For example: when jumping from build panel to build error, a jump point is setup, user can click this button to go over to the previous compiler error");
|
||||
252
src/plugin_word_complete.cpp
Normal file
252
src/plugin_word_complete.cpp
Normal file
@@ -0,0 +1,252 @@
|
||||
// MAAAAAAAAAAAAN I DONT LIKE THIS CODE, BUT HOPE IT WORKS
|
||||
|
||||
// @todo: potentially bad that we are not checking against end! lexer->at[0] == 0 check is not enough
|
||||
struct Lexer2 {
|
||||
char16_t *at;
|
||||
};
|
||||
|
||||
Lexer2 BeginLexing(String16 data) {
|
||||
Lexer2 result = {data.data};
|
||||
return result;
|
||||
}
|
||||
|
||||
String16 Next(Lexer2 *lexer) {
|
||||
while (lexer->at[0] != 0 && IsNonWord(lexer->at[0])) {
|
||||
lexer->at += 1;
|
||||
}
|
||||
String16 result = {lexer->at, 0};
|
||||
while (lexer->at[0] != 0 && IsWord(lexer->at[0])) {
|
||||
lexer->at += 1;
|
||||
result.len += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
struct StringAndDistance {
|
||||
String16 string;
|
||||
Int distance;
|
||||
};
|
||||
|
||||
Int FindValueIndex(Array<StringAndDistance> *arr, String16 string) {
|
||||
for (int64_t i = 0; i < arr->len; i += 1) {
|
||||
if (arr->data[i].string == string) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline bool MergeSortCompare(StringAndDistance *EntryA, StringAndDistance *EntryB) {
|
||||
bool result = EntryA->distance > EntryB->distance;
|
||||
return result;
|
||||
}
|
||||
|
||||
struct {
|
||||
BlockArena arena;
|
||||
String16 prefix_string;
|
||||
String16 last_string;
|
||||
mco_coro *co;
|
||||
Buffer *buffer;
|
||||
View *view;
|
||||
Int original_caret_pos;
|
||||
Int direction;
|
||||
} CWS;
|
||||
|
||||
void CWSLexIdentifiers(Array<StringAndDistance> *out_idents, Buffer *buffer) {
|
||||
Array<StringAndDistance> idents = {CWS.arena};
|
||||
String16 string = GetString(buffer);
|
||||
Lexer2 lexer = BeginLexing(string.data);
|
||||
for (;;) {
|
||||
String16 token = Next(&lexer);
|
||||
if (token.len <= 0) {
|
||||
break;
|
||||
}
|
||||
if (StartsWith(token, CWS.prefix_string) && token != CWS.prefix_string) {
|
||||
Int pos = token.data - buffer->str;
|
||||
// Here we are computing distance based on position from currently open
|
||||
// buffer but should be fine. We are sorting then putting into the array
|
||||
// and it doesn't displace the original ones so it's fine
|
||||
Int distance = Absolute(CWS.original_caret_pos - pos);
|
||||
int64_t value_index = FindValueIndex(&idents, token);
|
||||
if (value_index != -1) {
|
||||
if (idents[value_index].distance > distance) {
|
||||
idents[value_index].distance = distance;
|
||||
}
|
||||
} else {
|
||||
Add(&idents, {Copy16(CWS.arena, token), distance});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (idents.len > 1) {
|
||||
Array<StringAndDistance> temp = TightCopy(CWS.arena, idents);
|
||||
MergeSort(idents.len, idents.data, temp.data);
|
||||
}
|
||||
For (idents) {
|
||||
if (FindValueIndex(out_idents, it.string) == -1) Add(out_idents, it);
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
void CWSGetNextBuffer(mco_coro *co) {
|
||||
// Would be nice to iterate through 64 last active buffers
|
||||
// - Then all buffers
|
||||
Add(&CWS.visited_buffers, CWS.buffer->id);
|
||||
mco_result res = mco_push(co, &CWS.buffer->id, sizeof(CWS.buffer->id));
|
||||
Assert(res == MCO_SUCCESS);
|
||||
mco_yield(co);
|
||||
|
||||
For (IterateInReverse(&Buffers)) {
|
||||
Add(&buffers, it->id);
|
||||
}
|
||||
|
||||
ForItem (window, Windows) {
|
||||
if (!window->visible) {
|
||||
continue;
|
||||
}
|
||||
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
if (Contains(CWS.visited_buffers, buffer->id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Add(&CWS.visited_buffers, buffer->id);
|
||||
mco_result res = mco_push(co, &buffer->id, sizeof(buffer->id));
|
||||
Assert(res == MCO_SUCCESS);
|
||||
mco_yield(co);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void WordComplete(mco_coro *co) {
|
||||
Array<BufferID> buffers = {CWS.arena};
|
||||
Add(&buffers, CWS.buffer->id);
|
||||
For (IterateInReverse(&Buffers)) {
|
||||
Add(&buffers, it->id);
|
||||
}
|
||||
|
||||
Int buffer_i = 0;
|
||||
Array<StringAndDistance> idents = {CWS.arena};
|
||||
Add(&idents, {CWS.prefix_string, 0});
|
||||
for (Int i = 1;;) {
|
||||
if (i >= idents.len) {
|
||||
while (buffer_i < buffers.len) {
|
||||
Buffer *buffer = GetBuffer(buffers[buffer_i++]);
|
||||
CWSLexIdentifiers(&idents, buffer);
|
||||
if (i < idents.len) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= idents.len) {
|
||||
goto yield;
|
||||
}
|
||||
}
|
||||
CWS.last_string = Copy16(CWS.arena, idents[i].string);
|
||||
SelectRange(CWS.view, EncloseWord(CWS.buffer, CWS.original_caret_pos));
|
||||
Replace(CWS.view, CWS.last_string);
|
||||
yield:;
|
||||
mco_yield(co);
|
||||
if (CWS.direction == -1 && i > 0 && i == idents.len) {
|
||||
// special case for when we are at the end of the list and we want to go back
|
||||
// to make it sure we don't need to click twice to flip
|
||||
i -= 1;
|
||||
}
|
||||
i += CWS.direction;
|
||||
i = Clamp(i, (Int)0, idents.len);
|
||||
}
|
||||
}
|
||||
|
||||
void WordComplete(View *view, Int pos) {
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Range prefix_string_range = {GetWordStart(buffer, pos), pos};
|
||||
String16 prefix = GetString(buffer, prefix_string_range);
|
||||
if (prefix == u"") {
|
||||
return;
|
||||
}
|
||||
|
||||
bool continue_with_previous = CWS.co && CWS.last_string == prefix && CWS.buffer == buffer && CWS.view == view;
|
||||
if (!continue_with_previous) {
|
||||
if (CWS.co) {
|
||||
mco_result res = mco_destroy(CWS.co);
|
||||
Assert(res == MCO_SUCCESS);
|
||||
CWS.co = NULL;
|
||||
}
|
||||
Release(&CWS.arena);
|
||||
MemoryZero(&CWS, sizeof(CWS));
|
||||
|
||||
// CWS.visited_buffers.allocator = CWS.arena;
|
||||
CWS.buffer = buffer;
|
||||
CWS.view = view;
|
||||
CWS.original_caret_pos = pos;
|
||||
CWS.prefix_string = Copy16(CWS.arena, prefix);
|
||||
|
||||
mco_desc desc = mco_desc_init(WordComplete, 0);
|
||||
mco_result res = mco_create(&CWS.co, &desc);
|
||||
Assert(res == MCO_SUCCESS);
|
||||
}
|
||||
CWS.direction = 1;
|
||||
mco_result res = mco_resume(CWS.co);
|
||||
Assert(res == MCO_SUCCESS);
|
||||
|
||||
if (mco_status(CWS.co) == MCO_DEAD) {
|
||||
res = mco_destroy(CWS.co);
|
||||
Assert(res == MCO_SUCCESS);
|
||||
CWS.co = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CompletePrevWord(View *view, Int pos) {
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Range prefix_string_range = {GetWordStart(buffer, pos), pos};
|
||||
String16 prefix = GetString(buffer, prefix_string_range);
|
||||
bool continue_with_previous = CWS.co && CWS.last_string == prefix && CWS.buffer == buffer;
|
||||
if (!continue_with_previous) {
|
||||
return;
|
||||
}
|
||||
CWS.direction = -1;
|
||||
mco_result res = mco_resume(CWS.co);
|
||||
Assert(res == MCO_SUCCESS);
|
||||
if (mco_status(CWS.co) == MCO_DEAD) {
|
||||
res = mco_destroy(CWS.co);
|
||||
Assert(res == MCO_SUCCESS);
|
||||
CWS.co = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void CMD_WordComplete() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
bool ok = active.view->carets.len == 1 && GetSize(active.view->carets[0].range) == 0;
|
||||
if (!ok) {
|
||||
return;
|
||||
}
|
||||
WordComplete(active.view, active.view->carets[0].range.min);
|
||||
} RegisterCommand(CMD_WordComplete, "ctrl-space", "Completes the current word");
|
||||
|
||||
void CMD_CompletePrevWord() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
bool ok = active.view->carets.len == 1 && GetSize(active.view->carets[0].range) == 0;
|
||||
if (!ok) {
|
||||
return;
|
||||
}
|
||||
CompletePrevWord(active.view, active.view->carets[0].range.min);
|
||||
} RegisterCommand(CMD_CompletePrevWord, "ctrl-shift-space", "If already completing a word, iterate to previous word");
|
||||
|
||||
void CMD_CompletePrevWordOrDedent() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
if (active.view->carets.len == 1 && GetSize(active.view->carets[0].range) == 0) {
|
||||
CMD_CompletePrevWord();
|
||||
} else {
|
||||
IndentSelectedLines(active.view, true);
|
||||
}
|
||||
} RegisterCommand(CMD_CompletePrevWordOrDedent, "", "Completes the current word or it indents it, when single caret with no selection it goes for word complete");
|
||||
|
||||
void CMD_WordCompleteOrIndent() {
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
if (active.view->carets.len == 1 && GetSize(active.view->carets[0].range) == 0) {
|
||||
CMD_WordComplete();
|
||||
} else {
|
||||
IndentSelectedLines(active.view);
|
||||
}
|
||||
} RegisterCommand(CMD_WordCompleteOrIndent, "", "Completes the current word or it indents it, when single caret with no selection it goes for word complete");
|
||||
545
src/process.cpp
Normal file
545
src/process.cpp
Normal file
@@ -0,0 +1,545 @@
|
||||
#if OS_WINDOWS
|
||||
struct Win32Process {
|
||||
HANDLE handle;
|
||||
HANDLE child_stdout_read;
|
||||
HANDLE child_stdout_write;
|
||||
HANDLE child_stdin_read;
|
||||
HANDLE child_stdin_write;
|
||||
};
|
||||
static_assert(sizeof(Win32Process) < sizeof(Process::platform));
|
||||
|
||||
static void Win32ReportError(String msg, String cmd) {
|
||||
LPVOID lpMsgBuf;
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
|
||||
defer { LocalFree(lpMsgBuf); };
|
||||
char *buff = (char *)lpMsgBuf;
|
||||
size_t buffLen = strlen((const char *)buff);
|
||||
if (buffLen > 0 && buff[buffLen - 1] == '\n') buff[buffLen - 1] = 0;
|
||||
Error("%S: %S! %s", msg, cmd, (char *)lpMsgBuf);
|
||||
}
|
||||
|
||||
static void Win32CloseProcess(Process *process) {
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
if (p->handle != INVALID_HANDLE_VALUE) CloseHandle(p->handle);
|
||||
if (p->child_stdout_write != INVALID_HANDLE_VALUE) CloseHandle(p->child_stdout_write);
|
||||
if (p->child_stdout_read != INVALID_HANDLE_VALUE) CloseHandle(p->child_stdout_read);
|
||||
if (p->child_stdin_read != INVALID_HANDLE_VALUE) CloseHandle(p->child_stdin_read);
|
||||
if (p->child_stdin_write != INVALID_HANDLE_VALUE) CloseHandle(p->child_stdin_write);
|
||||
process->is_valid = false;
|
||||
}
|
||||
|
||||
static void Win32ProcessError(Process *process, String msg, String cmd) {
|
||||
Win32ReportError(msg, cmd);
|
||||
Win32CloseProcess(process);
|
||||
}
|
||||
|
||||
void WriteStdin(Process *process, String string) {
|
||||
if (string.len == 0) return;
|
||||
Assert(process->is_valid);
|
||||
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
Assert(p->child_stdin_write != INVALID_HANDLE_VALUE);
|
||||
|
||||
DWORD written = 0;
|
||||
bool write_error = WriteFile(p->child_stdin_write, string.data, (DWORD)string.len, &written, NULL) == 0;
|
||||
|
||||
Assert(write_error == false);
|
||||
Assert(written == string.len);
|
||||
}
|
||||
|
||||
void CloseStdin(Process *process) {
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
CloseHandle(p->child_stdin_write);
|
||||
p->child_stdin_write = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
Process SpawnProcess(ExecArgs args) {
|
||||
Scratch scratch;
|
||||
Process process = {};
|
||||
process.args = args;
|
||||
Win32Process *p = (Win32Process *)process.platform;
|
||||
|
||||
p->handle = INVALID_HANDLE_VALUE;
|
||||
p->child_stdout_write = INVALID_HANDLE_VALUE;
|
||||
p->child_stdout_read = INVALID_HANDLE_VALUE;
|
||||
p->child_stdin_read = INVALID_HANDLE_VALUE;
|
||||
p->child_stdin_write = INVALID_HANDLE_VALUE;
|
||||
|
||||
SECURITY_ATTRIBUTES security_atrb = {};
|
||||
security_atrb.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
security_atrb.bInheritHandle = TRUE;
|
||||
|
||||
if (!CreatePipe(&p->child_stdout_read, &p->child_stdout_write, &security_atrb, 0)) {
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", args.cmd);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (!SetHandleInformation(p->child_stdout_read, HANDLE_FLAG_INHERIT, 0)) {
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", args.cmd);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (args.open_stdin) {
|
||||
if (!CreatePipe(&p->child_stdin_read, &p->child_stdin_write, &security_atrb, 0)) {
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", args.cmd);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (!SetHandleInformation(p->child_stdin_write, HANDLE_FLAG_INHERIT, 0)) {
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", args.cmd);
|
||||
return process;
|
||||
}
|
||||
}
|
||||
|
||||
STARTUPINFOW startup = {};
|
||||
startup.cb = sizeof(STARTUPINFO);
|
||||
startup.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
|
||||
startup.hStdOutput = p->child_stdout_write;
|
||||
startup.hStdError = p->child_stdout_write;
|
||||
startup.wShowWindow = SW_HIDE;
|
||||
if (args.open_stdin) {
|
||||
startup.hStdInput = p->child_stdin_read;
|
||||
}
|
||||
|
||||
String16 cwd = ToString16(scratch, args.cwd);
|
||||
String16 cmd = ToString16(scratch, args.cmd);
|
||||
String16 exe = ToString16(scratch, args.exe);
|
||||
|
||||
char *env = NULL;
|
||||
if (args.env.len) {
|
||||
Int size = GetSize(args.env) + args.env.len + 1;
|
||||
env = AllocArray(scratch, char, size);
|
||||
Int i = 0;
|
||||
For (args.env) {
|
||||
MemoryCopy(env + i, it.data, it.len);
|
||||
i += it.len;
|
||||
|
||||
env[i++] = 0;
|
||||
}
|
||||
env[i++] = 0;
|
||||
}
|
||||
|
||||
DWORD dwCreationFlags = 0;
|
||||
BOOL bInheritHandles = TRUE;
|
||||
|
||||
PROCESS_INFORMATION info = {};
|
||||
if (!CreateProcessW((wchar_t *)exe.data, (wchar_t *)cmd.data, 0, 0, bInheritHandles, dwCreationFlags, env, (wchar_t *)cwd.data, &startup, &info)) {
|
||||
Win32ProcessError(&process, "failed to create process", args.cmd);
|
||||
return process;
|
||||
}
|
||||
|
||||
// Close handles to the stdin and stdout pipes no longer needed by the child process.
|
||||
// If they are not explicitly closed, there is no way to recognize that the child process has ended.
|
||||
CloseHandle(info.hThread);
|
||||
CloseHandle(p->child_stdout_write);
|
||||
p->child_stdout_write = INVALID_HANDLE_VALUE;
|
||||
|
||||
p->handle = info.hProcess;
|
||||
process.is_valid = true;
|
||||
process.id = (int64_t)p->handle;
|
||||
|
||||
return process;
|
||||
}
|
||||
|
||||
bool IsValid(Process *process) {
|
||||
if (process->is_valid == false) return false;
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
|
||||
if (WaitForSingleObject(p->handle, 0) != WAIT_OBJECT_0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DWORD exit_code;
|
||||
bool get_exit_code_failed = GetExitCodeProcess(p->handle, &exit_code) == 0;
|
||||
if (get_exit_code_failed) {
|
||||
exit_code = -1;
|
||||
}
|
||||
|
||||
process->exit_code = (int)exit_code;
|
||||
Win32CloseProcess(process);
|
||||
return false;
|
||||
}
|
||||
|
||||
void KillProcess(Process *process) {
|
||||
Assert(process->is_valid);
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
|
||||
bool terminate_process_error = TerminateProcess(p->handle, -1) == 0;
|
||||
if (terminate_process_error) {
|
||||
Assert(0);
|
||||
}
|
||||
Win32CloseProcess(process);
|
||||
process->exit_code = -1;
|
||||
}
|
||||
|
||||
String PollStdout(Allocator allocator, Process *process, bool force_read) {
|
||||
Assert(process->is_valid);
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
|
||||
DWORD bytes_avail = 0;
|
||||
bool peek_error = PeekNamedPipe(p->child_stdout_read, NULL, 0, NULL, &bytes_avail, NULL) == 0;
|
||||
if (peek_error) {
|
||||
return {};
|
||||
} else if (bytes_avail == 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
size_t buffer_size = ClampTop(bytes_avail, (DWORD)(4096 * 16));
|
||||
char *buffer = AllocArray(allocator, char, buffer_size);
|
||||
|
||||
DWORD bytes_read = 0;
|
||||
bool read_error = ReadFile(p->child_stdout_read, buffer, (DWORD)buffer_size, &bytes_read, 0) == 0;
|
||||
if (read_error) {
|
||||
Win32ReportError("Failed to read the stdout of child process", "ReadFile");
|
||||
Dealloc(allocator, buffer);
|
||||
Win32CloseProcess(process);
|
||||
return {};
|
||||
}
|
||||
|
||||
String result = {buffer, bytes_read};
|
||||
return result;
|
||||
}
|
||||
|
||||
#elif OS_POSIX
|
||||
|
||||
struct UnixProcess {
|
||||
pid_t pid;
|
||||
int child_stdout_read;
|
||||
int stdin_write;
|
||||
};
|
||||
|
||||
Array<char *> SplitCommand(Allocator allocator, String command_line) {
|
||||
Array<char *> cmd = {allocator};
|
||||
|
||||
String curr = {};
|
||||
for (int i = 0; i < command_line.len; i += 1) {
|
||||
if (command_line.data[i] == ' ') {
|
||||
if (curr.len > 0) {
|
||||
Add(&cmd, Copy(allocator, curr).data);
|
||||
curr = {};
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (curr.len == 0) {
|
||||
curr.data = command_line.data + i;
|
||||
}
|
||||
curr.len += 1;
|
||||
}
|
||||
|
||||
if (curr.len > 0) {
|
||||
Add(&cmd, Copy(allocator, curr).data);
|
||||
}
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
Process SpawnProcess(ExecArgs args) {
|
||||
Scratch scratch;
|
||||
const int PIPE_READ = 0;
|
||||
const int PIPE_WRITE = 1;
|
||||
bool error = false;
|
||||
|
||||
String working_dir = Copy(scratch, args.cwd);
|
||||
chdir(working_dir.data);
|
||||
|
||||
Process process = {};
|
||||
process.args = args;
|
||||
UnixProcess *plat = (UnixProcess *)&process.platform;
|
||||
Array<char *> args_cmd = SplitCommand(scratch, args.cmd);
|
||||
Array<char *> env = {scratch};
|
||||
char *exe = Copy(scratch, args.exe).data;
|
||||
|
||||
For (args.env) {
|
||||
Add(&env, it.data);
|
||||
}
|
||||
Add(&env, (char *)"\0");
|
||||
|
||||
|
||||
int stdout_desc[2] = {};
|
||||
int stdin_desc[2] = {};
|
||||
posix_spawn_file_actions_t actions = {};
|
||||
|
||||
if (posix_spawn_file_actions_init(&actions) != 0) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_init, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
defer {
|
||||
posix_spawn_file_actions_destroy(&actions);
|
||||
};
|
||||
|
||||
if (pipe(stdout_desc) == -1) {
|
||||
Error("Libc function failed: pipe, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
defer {
|
||||
if (error) {
|
||||
close(stdout_desc[PIPE_READ]);
|
||||
close(stdout_desc[PIPE_WRITE]);
|
||||
} else {
|
||||
close(stdout_desc[PIPE_WRITE]);
|
||||
}
|
||||
};
|
||||
|
||||
if (pipe(stdin_desc) == -1) {
|
||||
Error("Libc function failed: pipe, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
defer {
|
||||
if (error) {
|
||||
close(stdin_desc[PIPE_READ]);
|
||||
close(stdin_desc[PIPE_WRITE]);
|
||||
} else {
|
||||
close(stdin_desc[PIPE_READ]);
|
||||
}
|
||||
};
|
||||
|
||||
error = posix_spawn_file_actions_addclose(&actions, stdout_desc[PIPE_READ]) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_addclose, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
error = posix_spawn_file_actions_adddup2(&actions, stdout_desc[PIPE_WRITE], STDOUT_FILENO) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_adddup2 STDOUT_FILENO, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
error = posix_spawn_file_actions_adddup2(&actions, stdout_desc[PIPE_WRITE], STDERR_FILENO) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_adddup2 STDERR_FILENO, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
if (args.open_stdin) {
|
||||
error = posix_spawn_file_actions_addclose(&actions, stdin_desc[PIPE_WRITE]) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_addclose, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
error = posix_spawn_file_actions_adddup2(&actions, stdin_desc[PIPE_READ], STDIN_FILENO) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: posix_spawn_file_actions_adddup2 STDIN_FILENO, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
}
|
||||
|
||||
pid_t process_pid = 0;
|
||||
error = posix_spawnp(&process_pid, exe, &actions, NULL, args_cmd.data, env.data) != 0;
|
||||
if (error) {
|
||||
Error("Libc function failed: failed to create process\n, with error: %s", strerror(errno));
|
||||
return process;
|
||||
}
|
||||
|
||||
|
||||
plat->child_stdout_read = stdout_desc[PIPE_READ];
|
||||
plat->pid = process_pid;
|
||||
if (args.open_stdin) {
|
||||
plat->stdin_write = stdin_desc[PIPE_WRITE];
|
||||
}
|
||||
|
||||
process.id = process_pid;
|
||||
process.is_valid = true;
|
||||
return process;
|
||||
}
|
||||
|
||||
bool IsValid(Process *process) {
|
||||
UnixProcess *plat = (UnixProcess *)&process->platform;
|
||||
if (process->is_valid == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
pid_t result = waitpid(plat->pid, &status, WNOHANG);
|
||||
if (result == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Assert(result != -1);
|
||||
process->exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
|
||||
process->is_valid = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
void KillProcess(Process *process) {
|
||||
Assert(process->is_valid);
|
||||
UnixProcess *plat = (UnixProcess *)process->platform;
|
||||
kill(plat->pid, SIGKILL);
|
||||
process->exit_code = -1;
|
||||
}
|
||||
|
||||
String PollStdout(Allocator allocator, Process *process, bool force_read) {
|
||||
Assert(process->is_valid);
|
||||
UnixProcess *plat = (UnixProcess *)process->platform;
|
||||
|
||||
String result = {};
|
||||
result.data = AllocArray(allocator, char, 16 * 4096);
|
||||
|
||||
pollfd p = {};
|
||||
p.fd = plat->child_stdout_read;
|
||||
p.events = POLLIN;
|
||||
int res = poll(&p, 1, 0);
|
||||
if (res > 0 || force_read) {
|
||||
result.len = read(plat->child_stdout_read, result.data, 16 * 4096);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void WriteStdin(Process *process, String string) {
|
||||
if (string.len == 0) return;
|
||||
Assert(process->is_valid);
|
||||
|
||||
UnixProcess *plat = (UnixProcess *)process->platform;
|
||||
ssize_t size = write(plat->stdin_write, string.data, string.len);
|
||||
|
||||
Assert(size == string.len);
|
||||
}
|
||||
|
||||
void CloseStdin(Process *process) {
|
||||
UnixProcess *plat = (UnixProcess *)process->platform;
|
||||
close(plat->stdin_write);
|
||||
}
|
||||
|
||||
#else
|
||||
Process SpawnProcess(ExecArgs args) { return {}; }
|
||||
bool IsValid(Process *process) { return false; }
|
||||
void KillProcess(Process *process) { }
|
||||
String PollStdout(Allocator allocator, Process *process, bool force_read) { return {}; }
|
||||
void WriteStdin(Process *process, String string) { return; }
|
||||
void CloseStdin(Process *process) { return; }
|
||||
#endif
|
||||
|
||||
// @todo: perhaps rework the PATH ProcessEnviroment into an object from data_desc
|
||||
String GetEnv(String name) {
|
||||
For (ProcessEnviroment) {
|
||||
if (StartsWith(it, name, true)) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
String FindPython(Allocator allocator) {
|
||||
Scratch scratch(allocator);
|
||||
String path = GetEnv("PATH="); // ignore case on windows so it's fine. 'Path=' not needed
|
||||
Assert(path.len);
|
||||
String delim = IF_OS_WINDOWS_ELSE(";", ":");
|
||||
Array<String> paths = Split(scratch, path, delim);
|
||||
String tries[] = {"python3", "python", "py"};
|
||||
for (int i = 0; i < Lengthof(tries); i += 1) {
|
||||
For (paths) {
|
||||
String path_it = Format(scratch, "%S/%S" IF_OS_WINDOWS_ELSE(".exe", ""), it, tries[i]);
|
||||
#if OS_WINDOWS
|
||||
bool is_bad_bad_ms = EndsWith(it, "AppData\\Local\\Microsoft\\WindowsApps") || EndsWith(it, "AppData/Local/Microsoft/WindowsApps");
|
||||
if (is_bad_bad_ms) {
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
if (FileExists(path_it)) {
|
||||
return Copy(allocator, path_it);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void SetShell(Allocator allocator, String *exe, String *cmd, String in_cmd) {
|
||||
#if OS_WINDOWS
|
||||
*exe = "c:\\windows\\system32\\cmd.exe";
|
||||
*cmd = Format(allocator, "%S /C %S", *exe, in_cmd);
|
||||
#else
|
||||
*exe = "/usr/bin/bash";
|
||||
String temp_file = WriteTempFile(allocator, in_cmd); // @todo: maybe try to pass by stdio here
|
||||
*cmd = Format(allocator, "%S %S", *exe, temp_file);
|
||||
#endif
|
||||
}
|
||||
|
||||
// WARNING: seems that this maybe can't work reliably?
|
||||
// in case of 'git grep a' it's possible that child process spawns it's own
|
||||
// child process and then it won't print anything because it won't have
|
||||
// the appropriate handles. This happens in this case when git grep calls
|
||||
// 'less' program which errors out and doesn't print anything
|
||||
// @todo: maybe I should ask someone smarter about this!
|
||||
void UpdateProcesses() {
|
||||
IterRemove(ActiveProcesses) {
|
||||
IterRemovePrepare(ActiveProcesses);
|
||||
Scratch scratch;
|
||||
View *view = GetView(it.args.output_view);
|
||||
|
||||
String poll = PollStdout(scratch, &it, false);
|
||||
if (poll.len > 0) {
|
||||
Append(view, poll, it.args.scroll_to_end);
|
||||
} else if (!IsValid(&it)) {
|
||||
ReportConsolef("process %lld exit code = %d", it.id, it.exit_code);
|
||||
remove_item = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ExecArgs ShellArgs(Allocator allocator, ViewID output_view, String cmd, String working_directory) {
|
||||
ExecArgs args = {};
|
||||
SetShell(allocator, &args.exe, &args.cmd, cmd);
|
||||
args.env = ProcessEnviroment;
|
||||
args.cwd = working_directory;
|
||||
args.output_view = output_view;
|
||||
args.poll_process = 1;
|
||||
return args;
|
||||
}
|
||||
|
||||
Process Exec(ExecArgs args) {
|
||||
Process process = SpawnProcess(args);
|
||||
ReportConsolef("process %lld start. is_valid = %d cmd = %S working_dir = %S", process.id, process.is_valid, args.cmd, args.cwd);
|
||||
if (process.is_valid && args.poll_process) {
|
||||
Add(&ActiveProcesses, process);
|
||||
}
|
||||
return process;
|
||||
}
|
||||
|
||||
struct ExecResult {
|
||||
Buffer *buffer;
|
||||
int exit_code;
|
||||
};
|
||||
|
||||
ExecResult ExecAndWait(Allocator allocator, String cmd, String working_dir, String stdin_string = {}) {
|
||||
ReportConsolef("ExecAndWait cmd = %S working_dir = %S stdin_string = %S", cmd, working_dir, stdin_string);
|
||||
Buffer *scratch_buff = CreateScratchBuffer(allocator, 4096 * 4);
|
||||
|
||||
ExecArgs args = ShellArgs(allocator, {}, cmd, working_dir);
|
||||
args.poll_process = 0;
|
||||
args.open_stdin = 1;
|
||||
Process process = Exec(args);
|
||||
WriteStdin(&process, stdin_string);
|
||||
CloseStdin(&process);
|
||||
for (;IsValid(&process);) {
|
||||
Scratch scratch(allocator);
|
||||
String poll = PollStdout(scratch, &process, true);
|
||||
if (poll.len) RawAppend(scratch_buff, poll);
|
||||
}
|
||||
|
||||
return {scratch_buff, process.exit_code};
|
||||
}
|
||||
|
||||
void KillProcess(View *view) {
|
||||
IterRemove(ActiveProcesses) {
|
||||
IterRemovePrepare(ActiveProcesses);
|
||||
|
||||
ViewID view_id = {it.args.output_view};
|
||||
if (view_id == view->id) {
|
||||
KillProcess(&it);
|
||||
remove_item = true;
|
||||
String string = "process was killed by user\n";
|
||||
Append(view, string, it.args.scroll_to_end);
|
||||
// dont break because that will fuck with removal ...
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ProcessIsActive(ViewID view) {
|
||||
For (ActiveProcesses) {
|
||||
if (it.args.output_view == view) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -119,7 +119,7 @@ Font CreateFont(Atlas *atlas, int32_t size, String path) {
|
||||
}
|
||||
|
||||
stbtt_fontinfo stb_font;
|
||||
int success = stbtt_InitFont(&stb_font, (const unsigned char *)file.data, 0);
|
||||
int success = stbtt_InitFont(&stb_font, (const unsigned char *)file.data, 0);
|
||||
if (!success) {
|
||||
return result;
|
||||
}
|
||||
@@ -18,22 +18,20 @@ struct VertexList2D {
|
||||
|
||||
struct Shader {
|
||||
GLuint program; // linked program (vertex+fragment)
|
||||
GLint uni_invHalf; // uniform location for inv half-screen size
|
||||
GLint uni_texture; // sampler location
|
||||
GLint uni_invHalf; // uniform location for inv half-screen size
|
||||
GLint uni_texture; // sampler location
|
||||
};
|
||||
|
||||
VertexList2D Vertices;
|
||||
int64_t TotalVertexCount;
|
||||
int64_t TotalVertexCount;
|
||||
|
||||
unsigned VBO, VAO;
|
||||
Shader Shader2D;
|
||||
Shader Shader2D;
|
||||
BlockArena RenderArena;
|
||||
Rect2 CurrentScissor;
|
||||
Rect2 CurrentScissor;
|
||||
|
||||
Font PrimaryFont;
|
||||
Font SecondaryFont;
|
||||
|
||||
// ---------- shaders (ES3 / WebGL2) ----------
|
||||
static const char *glsl_vshader_es3 = R"==(#version 300 es
|
||||
precision highp float;
|
||||
|
||||
@@ -73,15 +71,15 @@ static const char *glsl_fshader_es3 = R"==(#version 300 es
|
||||
}
|
||||
)==";
|
||||
|
||||
void ReportWarningf(const char *fmt, ...);
|
||||
void ReportErrorf(const char *fmt, ...);
|
||||
void GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *user) {
|
||||
ReportWarningf("OpenGL message: %s", message);
|
||||
Unused(source); Unused(type); Unused(id); Unused(length); Unused(user);
|
||||
ReportErrorf("OpenGL message: %s", message);
|
||||
if (severity == GL_DEBUG_SEVERITY_HIGH || severity == GL_DEBUG_SEVERITY_MEDIUM) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OpenGL error", message, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- helper: compile/link ----------
|
||||
static GLuint CompileShaderSrc(GLenum kind, const char *src) {
|
||||
GLuint s = glCreateShader(kind);
|
||||
glShaderSource(s, 1, &src, NULL);
|
||||
@@ -136,7 +134,6 @@ Shader CreateShaderES3(const char *vsrc, const char *fsrc) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// ---------- InitRender for ES3 ----------
|
||||
void InitRender() {
|
||||
#if !OS_WASM
|
||||
glDebugMessageCallback(&GLDebugCallback, NULL);
|
||||
@@ -184,7 +181,6 @@ void BeginFrameRender(float wx, float wy) {
|
||||
CurrentScissor = Rect0Size(wx, wy);
|
||||
}
|
||||
|
||||
// ---------- EndFrameRender for ES3 ----------
|
||||
void EndFrameRender(float wx, float wy, Color color) {
|
||||
ProfileFunction();
|
||||
glEnable(GL_BLEND);
|
||||
@@ -288,7 +284,7 @@ void PushVertex2D(Allocator allocator, VertexList2D *list, Vertex2D *vertices, i
|
||||
for (int i = 0; i < count; i += 1) result[i] = vertices[i];
|
||||
}
|
||||
|
||||
void PushQuad2D(Allocator arena, VertexList2D *list, Rect2 rect, Rect2 tex, Color color, float rotation = 0.f, Vec2 rotation_point = {}) {
|
||||
void PushQuad2D(Allocator arena, VertexList2D *list, Rect2 rect, Rect2 tex, Color color) {
|
||||
Vertex2D *v = AllocVertex2D(arena, list, 6);
|
||||
v[0] = { {rect.min.x, rect.max.y}, { tex.min.x, tex.max.y}, color };
|
||||
v[1] = { {rect.max.x, rect.max.y}, { tex.max.x, tex.max.y}, color };
|
||||
@@ -296,15 +292,6 @@ void PushQuad2D(Allocator arena, VertexList2D *list, Rect2 rect, Rect2 tex, Colo
|
||||
v[3] = { {rect.min.x, rect.min.y}, { tex.min.x, tex.min.y}, color };
|
||||
v[4] = { {rect.max.x, rect.max.y}, { tex.max.x, tex.max.y}, color };
|
||||
v[5] = { {rect.max.x, rect.min.y}, { tex.max.x, tex.min.y}, color };
|
||||
if (rotation != 0.f) {
|
||||
float s = sinf(rotation);
|
||||
float c = cosf(rotation);
|
||||
for (int i = 0; i < 6; i += 1) {
|
||||
v[i].pos -= rotation_point;
|
||||
v[i].pos = {v[i].pos.x * c + v[i].pos.y * (-s), v[i].pos.x * s + v[i].pos.y * c};
|
||||
v[i].pos += rotation_point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DrawRect(Rect2 rect, Color color) {
|
||||
@@ -336,7 +323,7 @@ Vec2 DrawString(Font *font, String16 string, Vec2 pos, Color color, bool draw =
|
||||
Glyph *g = GetGlyph(font, it);
|
||||
Rect2 rect = Rect2FromSize(pos + g->offset, g->size);
|
||||
if (draw && it != '\n' && it != ' ' && it != '\t') {
|
||||
PushQuad2D(RenderArena, &Vertices, rect, g->atlas_bounding_box, color);
|
||||
PushQuad2D(RenderArena, &Vertices, Round(rect), g->atlas_bounding_box, color);
|
||||
}
|
||||
pos.x += g->xadvance;
|
||||
}
|
||||
@@ -399,6 +386,9 @@ void ReloadFont(String path, U32 size) {
|
||||
Scratch scratch;
|
||||
Atlas atlas = CreateAtlas(scratch, {2048, 2048});
|
||||
PrimaryFont = CreateFont(&atlas, (uint32_t)ClampBottom(2u, (U32)size), path);
|
||||
SecondaryFont = CreateFont(&atlas, 12, path);
|
||||
SecondaryFont.texture_id = PrimaryFont.texture_id = UploadAtlas(&atlas);
|
||||
PrimaryFont.texture_id = UploadAtlas(&atlas);
|
||||
}
|
||||
|
||||
void CleanupRender() {
|
||||
Dealloc(&PrimaryFont.glyphs);
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
String TestDir;
|
||||
BlockArena TestArena;
|
||||
|
||||
void AddCtrlPress(SDL_Keycode key) {
|
||||
Event event = {};
|
||||
event.key = key;
|
||||
event.ctrl = 1;
|
||||
event.xwindow = 1280;
|
||||
event.ywindow = 720;
|
||||
Add(&EventPlayback, event);
|
||||
}
|
||||
|
||||
void AddText(String string) {
|
||||
Event event = {};
|
||||
event.kind = EVENT_TEXT_INPUT;
|
||||
event.xwindow = 1280;
|
||||
event.ywindow = 720;
|
||||
event.text = Copy(SysAllocator, string).data;
|
||||
Add(&EventPlayback, event);
|
||||
}
|
||||
|
||||
void Wait(mco_coro *co) {
|
||||
Add(&EventPlayback, {EVENT_KIND_INVALID});
|
||||
for (Event *event = CoYield(co); event->kind != EVENT_KIND_INVALID; event = CoYield(co)) {
|
||||
}
|
||||
}
|
||||
|
||||
void PlayTestOpen(mco_coro *co) {
|
||||
// Open file, move a little, then open again and verify the caret didn't move
|
||||
// String basic_env_cpp = Format(SysAllocator, "%S/test_env", TestDir);
|
||||
|
||||
// AddCtrlPress(SDLK_P);
|
||||
// Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_UP, 1280, 720});
|
||||
// AddCtrlPress(SDLK_Q);
|
||||
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_PERIOD; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_PERIOD; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_F; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_S; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "s"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_R; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "r"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_C; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "c"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_ESCAPE; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_ESCAPE; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LCTRL; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_Q; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LCTRL; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_F; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_T; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "t"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_E; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "e"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_S; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "s"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_T; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "t"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_ESCAPE; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_ESCAPE; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LCTRL; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_Q; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LCTRL; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_F; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_T; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "t"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_E; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "e"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_S; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_TEXT_INPUT; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.text = "s"; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_ESCAPE; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_ESCAPE; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_LCTRL; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
// {Event ev = {};ev.kind = EVENT_KEY_PRESS; ev.key = SDLK_Q; ev.xwindow = 1910; ev.ywindow = 2040; ev.xmouse = 581; ev.ymouse = 952; ev.ctrl = 1; Add(&EventPlayback, ev);}
|
||||
|
||||
// Wait(co);
|
||||
|
||||
// {
|
||||
// BSet main = GetActiveMainSet();
|
||||
// Assert(main.buffer->name == basic_env_cpp);
|
||||
// Assert(main.view->carets[0].range.min == 0);
|
||||
// Assert(main.view->carets[0].range.max == 0);
|
||||
// Int line = PosToLine(main.buffer, main.view->carets[0].range.min);
|
||||
// Assert(line == 0);
|
||||
// }
|
||||
|
||||
// AddCtrlPress(SDLK_DOWN);
|
||||
// AddCtrlPress(SDLK_DOWN);
|
||||
// AddCtrlPress(SDLK_DOWN);
|
||||
// Wait(co);
|
||||
|
||||
// Range range = {};
|
||||
// {
|
||||
// BSet main = GetActiveMainSet();
|
||||
// Assert(main.view->carets[0].range.min > 0);
|
||||
// Assert(main.view->carets[0].range.max > 0);
|
||||
// range = main.view->carets[0].range;
|
||||
// }
|
||||
|
||||
// AddCtrlPress(SDLK_P);
|
||||
// Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_UP, 1280, 720});
|
||||
// AddCtrlPress(SDLK_Q);
|
||||
// Wait(co);
|
||||
|
||||
// Int buffer_len = 0;
|
||||
// {
|
||||
// BSet main = GetActiveMainSet();
|
||||
// Assert(main.buffer->name == basic_env_cpp);
|
||||
// Assert(main.view->carets[0].range.min == range.min);
|
||||
// Assert(main.view->carets[0].range.max == range.max);
|
||||
// buffer_len = main.buffer->len;
|
||||
// }
|
||||
|
||||
// AddText(Format(SysAllocator, "%S:20", basic_env_cpp));
|
||||
// AddCtrlPress(SDLK_Q);
|
||||
// Wait(co);
|
||||
|
||||
// {
|
||||
// BSet main = GetActiveMainSet();
|
||||
// Int pos = main.view->carets[0].range.min;
|
||||
// Int line = PosToLine(main.buffer, pos);
|
||||
// Assert(line == 19);
|
||||
// Assert(main.buffer->name == basic_env_cpp);
|
||||
// Assert(main.buffer->len != buffer_len);
|
||||
// Assert(main.buffer->dirty);
|
||||
// }
|
||||
|
||||
// AddCtrlPress(SDLK_Z);
|
||||
// AddCtrlPress(SDLK_PAGEUP);
|
||||
// Wait(co);
|
||||
|
||||
// {
|
||||
// BSet main = GetActiveMainSet();
|
||||
// Assert(main.buffer->len == buffer_len);
|
||||
// Assert(main.view->carets[0].range.min == 0);
|
||||
// Assert(main.view->carets[0].range.max == 0);
|
||||
// }
|
||||
ReportConsolef("%s DONE", __FUNCTION__);
|
||||
|
||||
}
|
||||
|
||||
void Test(mco_coro *co) {
|
||||
Wait(co); // First phase starts immediately but stuff is not initialized so Open acts weird
|
||||
Open(TestDir);
|
||||
PlayTestOpen(co);
|
||||
Release(&TestArena);
|
||||
|
||||
|
||||
// Add(&EventPlayback, {EVENT_QUIT});
|
||||
}
|
||||
|
||||
void InitTests() {
|
||||
WaitForEvents = false;
|
||||
TestDir = Format(TestArena, "%S/test_env", GetExeDir(TestArena));
|
||||
|
||||
CoRemove("Test");
|
||||
CoData *data = CoAdd(Test);
|
||||
data->dont_wait_until_resolved = true;
|
||||
CoResume(data);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -6,7 +6,11 @@ struct ViewID { Int id; View *o; };
|
||||
struct WindowID { Int id; Window *o; };
|
||||
union Range { struct { Int min; Int max; }; Int e[2]; };
|
||||
struct Caret { union { Range range; Int pos[2]; }; Int ifront;};
|
||||
struct XY { Int col; Int line; };
|
||||
union XY {
|
||||
struct {Int col; Int line;};
|
||||
struct {Int x; Int y; };
|
||||
};
|
||||
|
||||
typedef void Function();
|
||||
|
||||
struct FunctionData {
|
||||
@@ -25,12 +29,79 @@ struct HistoryEntry {
|
||||
double time;
|
||||
};
|
||||
|
||||
typedef void CMDFunction();
|
||||
struct Command {
|
||||
String name;
|
||||
String docs;
|
||||
CMDFunction *function;
|
||||
String binding; // DONT USE, only for ParseKeyCached
|
||||
struct Trigger *trigger;
|
||||
};
|
||||
|
||||
enum UIAction {
|
||||
UIAction_Null,
|
||||
UIAction_Cancel,
|
||||
UIAction_Yes,
|
||||
UIAction_No,
|
||||
};
|
||||
|
||||
struct ExecArgs {
|
||||
String exe;
|
||||
String cmd;
|
||||
String cwd;
|
||||
Array<String> env;
|
||||
|
||||
ViewID output_view; // poll_process
|
||||
struct {
|
||||
U32 poll_process : 1;
|
||||
U32 open_stdin : 1;
|
||||
U32 scroll_to_end : 1;
|
||||
};
|
||||
};
|
||||
|
||||
struct Process {
|
||||
bool is_valid;
|
||||
int exit_code;
|
||||
char platform[6 * 8];
|
||||
Int id;
|
||||
|
||||
ExecArgs args; // memory for exe and all that is invalid
|
||||
};
|
||||
|
||||
enum OpenKind {
|
||||
OpenKind_Invalid,
|
||||
OpenKind_Skip,
|
||||
OpenKind_Exec,
|
||||
OpenKind_Goto,
|
||||
OpenKind_Command,
|
||||
#if PLUGIN_CONFIG
|
||||
OpenKind_Set,
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef uint32_t ResolveOpenMeta;
|
||||
enum {
|
||||
ResolveOpenMeta_Normal = 0,
|
||||
ResolveOpenMeta_DontError = 2,
|
||||
ResolveOpenMeta_DontExec = 4, // dont error +
|
||||
};
|
||||
|
||||
struct ResolvedOpen {
|
||||
OpenKind kind;
|
||||
String path;
|
||||
Int line, col;
|
||||
|
||||
struct {
|
||||
U32 existing_buffer : 1;
|
||||
U32 exec_in_background : 1;
|
||||
U32 use_python_shell : 1;
|
||||
};
|
||||
};
|
||||
|
||||
struct Buffer {
|
||||
BufferID id;
|
||||
String name;
|
||||
Int begin_frame_change_id;
|
||||
Int change_id;
|
||||
Int user_change_id;
|
||||
Int file_mod_time;
|
||||
|
||||
union {
|
||||
@@ -44,71 +115,22 @@ struct Buffer {
|
||||
Array<HistoryEntry> undo_stack;
|
||||
Array<HistoryEntry> redo_stack;
|
||||
int edit_phase;
|
||||
Array<Command> commands;
|
||||
struct {
|
||||
unsigned no_history : 1;
|
||||
unsigned no_line_starts : 1;
|
||||
unsigned dirty : 1;
|
||||
unsigned changed_on_disk : 1;
|
||||
unsigned temp : 1;
|
||||
unsigned dont_try_to_save_in_bulk_ops : 1;
|
||||
unsigned close : 1;
|
||||
unsigned special : 1;
|
||||
unsigned is_dir : 1;
|
||||
uint32_t no_history : 1;
|
||||
uint32_t no_line_starts : 1;
|
||||
uint32_t dirty : 1;
|
||||
uint32_t changed_on_disk : 1;
|
||||
uint32_t temp : 1;
|
||||
uint32_t dont_try_to_save_in_bulk_ops : 1;
|
||||
uint32_t close : 1;
|
||||
uint32_t special : 1;
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
uint32_t is_dir : 1;
|
||||
#endif
|
||||
};
|
||||
};
|
||||
|
||||
enum HookKind {
|
||||
HookKind_Invalid,
|
||||
|
||||
HookKind_AppInit,
|
||||
HookKind_AppUpdate,
|
||||
HookKind_AppQuit,
|
||||
|
||||
// Should we have commands like PostCommand, PreCommand? what would be the purpose?
|
||||
HookKind_Command,
|
||||
|
||||
|
||||
// Currently we are only basically allowing control over non-layouted windows.
|
||||
// How can this be expanded?
|
||||
// - Changing the layout algorithm: this seems like a decent one
|
||||
// - What beside that?
|
||||
HookKind_LayoutWindow,
|
||||
HookKind_RenderWindow
|
||||
|
||||
HookKind_BeforeBufferSave,
|
||||
HookKind_BufferSave,
|
||||
HookKind_AfterBufferSave,
|
||||
|
||||
HookKind_BeforeBufferOpen,
|
||||
HookKind_BufferOpen,
|
||||
HookKind_AfterBufferOpen,
|
||||
|
||||
HookKind_ViewUpdate,
|
||||
};
|
||||
|
||||
// Global hooks, per (window, view, buffer) hooks
|
||||
|
||||
struct HookParam {
|
||||
union {
|
||||
struct {
|
||||
Window *window;
|
||||
Rect2I *rect;
|
||||
int16_t wx;
|
||||
int16_t wy;
|
||||
} layout;
|
||||
};
|
||||
};
|
||||
|
||||
typedef void HookFunction(HookParam param);
|
||||
struct Hook {
|
||||
HookKind kind;
|
||||
String name;
|
||||
String docs;
|
||||
HookFunction *function;
|
||||
String binding;
|
||||
struct Trigger *trigger;
|
||||
};
|
||||
|
||||
struct View {
|
||||
ViewID id;
|
||||
BufferID active_buffer;
|
||||
@@ -119,12 +141,13 @@ struct View {
|
||||
Caret main_caret_on_begin_frame;
|
||||
bool update_scroll;
|
||||
|
||||
String hook_cmd;
|
||||
Array<Hook> hooks;
|
||||
UIAction ui_action;
|
||||
Array<Command> commands;
|
||||
Function *update_hook;
|
||||
uint64_t prev_search_line_hash;
|
||||
struct {
|
||||
unsigned close : 1;
|
||||
unsigned special : 1;
|
||||
uint32_t close : 1;
|
||||
uint32_t special : 1;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -148,7 +171,7 @@ struct Window {
|
||||
double mouse_scroller_offset;
|
||||
int z;
|
||||
double weight;
|
||||
Caret search_bar_anchor;
|
||||
Caret search_bar_anchor; // maybe move to view @todo
|
||||
|
||||
GotoCrumb begin_frame_crumb;
|
||||
Array<GotoCrumb> goto_history;
|
||||
@@ -157,7 +180,9 @@ struct Window {
|
||||
ViewID active_goto_list;
|
||||
Int goto_list_pos;
|
||||
|
||||
Array<Hook> hooks;
|
||||
String ui_query_file;
|
||||
void (*after_resolve_open_hook)(Window *window, ResolvedOpen *resolved);
|
||||
Array<Command> commands;
|
||||
struct {
|
||||
uint32_t draw_scrollbar : 1;
|
||||
uint32_t draw_line_numbers : 1;
|
||||
@@ -277,50 +302,30 @@ struct Register_Variable {
|
||||
type name = __VA_ARGS__; \
|
||||
Register_Variable var_##name(&Variables, VariableType_##type, #name, &name)
|
||||
|
||||
enum OpenKind {
|
||||
OpenKind_Invalid,
|
||||
OpenKind_Skip,
|
||||
OpenKind_Exec,
|
||||
OpenKind_BackgroundExec,
|
||||
OpenKind_Goto,
|
||||
OpenKind_Command,
|
||||
OpenKind_Set,
|
||||
};
|
||||
|
||||
typedef uint32_t ResolveOpenMeta;
|
||||
enum {
|
||||
ResolveOpenMeta_Normal = 0,
|
||||
ResolveOpenMeta_DontError = 2,
|
||||
ResolveOpenMeta_DontExec = 4, // dont error +
|
||||
};
|
||||
|
||||
struct ResolvedOpen {
|
||||
OpenKind kind;
|
||||
String path;
|
||||
Int line, col;
|
||||
bool existing_buffer;
|
||||
};
|
||||
|
||||
void AddHook(Array<Hook> *arr, HookKind kind, String name, String binding, HookFunction *function);
|
||||
void AddCommand(Array<Hook> *arr, String name, String binding, HookFunction *function);
|
||||
#define RegisterCommand(name, ...) Register_Command RC__##name(&GlobalHooks, HookKind_Command, name, #name, __VA_ARGS__)
|
||||
#define RegisterHook(name, kind, bindings, docs) Register_Command RC__##name(&GlobalHooks, kind, name, #name, bindings, docs)
|
||||
void AddCommand(Array<Command> *arr, String name, struct Trigger *trigger, CMDFunction *function);
|
||||
struct Register_Command {
|
||||
Register_Command(Array<Hook> *funcs, HookKind kind, HookFunction *function, String name, String binding, String docs = "") {
|
||||
Register_Command(Array<Command> *funcs, CMDFunction *function, String name, String binding, String docs = "") {
|
||||
int64_t pos = 0;
|
||||
if (Seek(name, "_", &pos, 0)) {
|
||||
name = Skip(name, pos + 1);
|
||||
}
|
||||
Hook hook = {};
|
||||
hook.kind = kind;
|
||||
hook.name = name;
|
||||
hook.binding = binding;
|
||||
hook.function = function;
|
||||
hook.docs = docs;
|
||||
Command cmd = {};
|
||||
cmd.name = name;
|
||||
cmd.binding = binding;
|
||||
cmd.function = function;
|
||||
cmd.docs = docs;
|
||||
Reserve(funcs, 512);
|
||||
Add(funcs, hook);
|
||||
Add(funcs, cmd);
|
||||
}
|
||||
};
|
||||
#define RegisterCommand(name, binding, docs) Register_Command RC__##name(&GlobalCommands, name, #name, binding, docs)
|
||||
#define RegisterCoroutineCommand(name, binding, docs, ...) void CMD_##name() {\
|
||||
RemoveCoroutine(#name);\
|
||||
CCtx *data = AddCoroutine(name);\
|
||||
__VA_ARGS__\
|
||||
ResumeCoroutine(data);\
|
||||
}\
|
||||
Register_Command RC__##name(&GlobalCommands, CMD_##name, #name, binding, docs)
|
||||
|
||||
#define RegisterFunction(functions, name) Register_Function RF__##name(functions, #name, name)
|
||||
struct Register_Function {
|
||||
@@ -340,7 +345,6 @@ constexpr int DIR_UP = 3;
|
||||
constexpr int DIR_COUNT = 4;
|
||||
constexpr bool CTRL_PRESSED = true;
|
||||
constexpr bool SHIFT_PRESS = true;
|
||||
constexpr bool KILL_SELECTION = true;
|
||||
constexpr Int LAST_LINE = INT64_MAX;
|
||||
|
||||
BSet GetBSet(struct Window *window);
|
||||
@@ -348,7 +352,6 @@ BSet GetBSet(WindowID window_id);
|
||||
void Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line);
|
||||
void Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line);
|
||||
void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string);
|
||||
void ReportDebugf(const char *fmt, ...);
|
||||
void ReportConsolef(const char *fmt, ...);
|
||||
void ReportErrorf(const char *fmt, ...);
|
||||
void ReportWarningf(const char *fmt, ...);
|
||||
@@ -357,13 +360,12 @@ View *CreateView(BufferID active_buffer);
|
||||
void EvalCommand(String command);
|
||||
void EvalCommand(String16 command);
|
||||
Rect2I GetVisibleCells(Window *window);
|
||||
ResolvedOpen ResolveOpen(Allocator scratch, String path, ResolveOpenMeta meta);
|
||||
ResolvedOpen ResolveOpen(Allocator scratch, Window *window, String path, ResolveOpenMeta meta);
|
||||
BSet Open(String path, ResolveOpenMeta meta = ResolveOpenMeta_Normal);
|
||||
BSet Open(String16 path, ResolveOpenMeta meta = ResolveOpenMeta_Normal);
|
||||
void CenterView(WindowID window);
|
||||
void TrimWhitespace(Buffer *buffer, bool trim_lines_with_caret = false);
|
||||
void JumpTempBuffer(BSet *set, String buffer_name = "");
|
||||
View *FindView(BufferID buffer_id, View *default_view = NULL);
|
||||
|
||||
bool operator==(BufferID a, BufferID b) { return a.id == b.id; }
|
||||
bool operator==(ViewID a, ViewID b) { return a.id == b.id; }
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,82 +0,0 @@
|
||||
typedef void CoroutineProc(mco_coro *co);
|
||||
CoData *CoCurr = NULL;
|
||||
|
||||
void CoDestroy(CoData *n) {
|
||||
mco_destroy(n->co);
|
||||
Release(&n->arena);
|
||||
}
|
||||
|
||||
void CoRemove(String name) {
|
||||
IterRemove(ActiveCoroutines) {
|
||||
IterRemovePrepare(ActiveCoroutines);
|
||||
if (it.name == name) {
|
||||
mco_destroy(it.co);
|
||||
Release(&it.arena);
|
||||
remove_item = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define CoAdd(x) CoAddEx(x, #x)
|
||||
CoData *CoAddEx(CoroutineProc *proc, String name) {
|
||||
mco_desc desc = mco_desc_init(proc, 0);
|
||||
mco_coro *coro = NULL;
|
||||
mco_result ok = mco_create(&coro, &desc);
|
||||
if (ok != MCO_SUCCESS) {
|
||||
ReportWarningf("failed to create coroutine %d", ok);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Add(&ActiveCoroutines, {coro, name});
|
||||
return GetLast(ActiveCoroutines);
|
||||
}
|
||||
|
||||
void CoResume(CoData *dat) {
|
||||
CoData *prev_curr = CoCurr;
|
||||
CoCurr = dat;
|
||||
mco_result ok = mco_resume(dat->co);
|
||||
Assert(ok == MCO_SUCCESS);
|
||||
CoCurr = prev_curr;
|
||||
}
|
||||
|
||||
void CoUpdate(Event *event) {
|
||||
ProfileFunction();
|
||||
double start = GetTimeSeconds();
|
||||
for (;ActiveCoroutines.len;) {
|
||||
IterRemove(ActiveCoroutines) {
|
||||
IterRemovePrepare(ActiveCoroutines);
|
||||
ProfileScopeEx(it.name);
|
||||
|
||||
mco_state status = mco_status(it.co);
|
||||
if (status == MCO_DEAD) {
|
||||
CoDestroy(&it);
|
||||
remove_item = true;
|
||||
} else {
|
||||
mco_push(it.co, &event, sizeof(Event *));
|
||||
CoCurr = ⁢
|
||||
mco_result ok = mco_resume(it.co);
|
||||
if (ok != MCO_SUCCESS) {
|
||||
ReportWarningf("failed to resume coroutine %d", ok);
|
||||
CoDestroy(&it);
|
||||
remove_item = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double took = GetTimeSeconds() - start;
|
||||
if (took > (0.016666 / 3.0)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Event *CoYield(mco_coro *co) {
|
||||
mco_result ok = mco_yield(co);
|
||||
Assert(ok == MCO_SUCCESS);
|
||||
|
||||
Event *event = NULL;
|
||||
ok = mco_pop(co, &event, sizeof(Event *));
|
||||
Assert(ok == MCO_SUCCESS);
|
||||
|
||||
return event;
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
// WARNING: seems that this maybe can't work reliably?
|
||||
// in case of 'git grep a' it's possible that child process spawns it's own
|
||||
// child process and then it won't print anything because it won't have
|
||||
// the appropriate handles. This happens in this case when git grep calls
|
||||
// 'less' program which errors out and doesn't print anything
|
||||
// @todo: maybe I should ask someone smarter about this!
|
||||
void UpdateProcesses(HookParam param) {
|
||||
IterRemove(ActiveProcesses) {
|
||||
IterRemovePrepare(ActiveProcesses);
|
||||
Scratch scratch;
|
||||
View *view = GetView({it.view_id});
|
||||
|
||||
String poll = PollStdout(scratch, &it, false);
|
||||
if (poll.len) {
|
||||
Append(view, poll, it.scroll_to_end);
|
||||
}
|
||||
if (!IsValid(&it)) {
|
||||
ReportDebugf("process %lld exit code = %d", it.id, it.exit_code);
|
||||
remove_item = true;
|
||||
}
|
||||
}
|
||||
} RegisterHook(UpdateProcesses, HookKind_AppUpdate, "", "Polls all the processes and outputs the results to corresponding views");
|
||||
|
||||
void Exec(ViewID view, bool scroll_to_end, String cmd, String working_dir) {
|
||||
Process process = SpawnProcess(cmd, working_dir, {}, ProcessEnviroment);
|
||||
ReportDebugf("process %lld start. is_valid = %d cmd = %S working_dir = %S", process.id, process.is_valid, cmd, working_dir);
|
||||
process.view_id = view.id;
|
||||
process.scroll_to_end = scroll_to_end;
|
||||
if (process.is_valid) Add(&ActiveProcesses, process);
|
||||
}
|
||||
|
||||
void Exec(ViewID view, bool scroll_to_end, String16 cmd16, String working_dir) {
|
||||
Scratch scratch;
|
||||
String cmd = ToString(scratch, cmd16);
|
||||
Exec(view, scroll_to_end, cmd, working_dir);
|
||||
}
|
||||
|
||||
struct ExecResult {
|
||||
Buffer *buffer;
|
||||
int exit_code;
|
||||
};
|
||||
|
||||
ExecResult ExecAndWait(Allocator allocator, String cmd, String working_dir, String stdin_string = {}) {
|
||||
ReportDebugf("ExecAndWait cmd = %S working_dir = %S stdin_string = %S", cmd, working_dir, stdin_string);
|
||||
|
||||
Buffer *scratch_buff = CreateScratchBuffer(allocator, 4096 * 4);
|
||||
Process process = SpawnProcess(cmd, working_dir, stdin_string, ProcessEnviroment);
|
||||
for (;IsValid(&process);) {
|
||||
Scratch scratch(allocator);
|
||||
String poll = PollStdout(scratch, &process, true);
|
||||
if (poll.len) RawAppend(scratch_buff, poll);
|
||||
}
|
||||
|
||||
return {scratch_buff, process.exit_code};
|
||||
}
|
||||
|
||||
void KillProcess(View *view) {
|
||||
IterRemove(ActiveProcesses) {
|
||||
IterRemovePrepare(ActiveProcesses);
|
||||
|
||||
ViewID view_id = {it.view_id};
|
||||
if (view_id == view->id) {
|
||||
KillProcess(&it);
|
||||
remove_item = true;
|
||||
String string = "process was killed by user\n";
|
||||
Append(view, string, it.scroll_to_end);
|
||||
// dont break because that will fuck with removal ...
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ProcessIsActive(ViewID view) {
|
||||
For (ActiveProcesses) {
|
||||
if (it.view_id == view.id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -1,304 +0,0 @@
|
||||
WindowID AllocWindowID(Window *window) {
|
||||
return {WindowIDs.id++, window};
|
||||
}
|
||||
|
||||
Window *CreateWind() {
|
||||
Allocator allocator = GetSystemAllocator();
|
||||
Window *w = AllocType(allocator, Window);
|
||||
w->font = &PrimaryFont;
|
||||
w->visible = true;
|
||||
w->primary = true;
|
||||
w->draw_scrollbar = true;
|
||||
w->draw_line_numbers = true;
|
||||
w->draw_line_highlight = true;
|
||||
w->jump_history = true;
|
||||
w->id = AllocWindowID(w);
|
||||
w->weight = 1.0;
|
||||
Add(&Windows, w);
|
||||
return w;
|
||||
}
|
||||
|
||||
Window *GetWindow(WindowID id, Window *default_window = Windows[0]) {
|
||||
For(Windows) {
|
||||
if (it->id == id) return it;
|
||||
}
|
||||
return default_window;
|
||||
}
|
||||
|
||||
void Close(WindowID id) {
|
||||
Window *window = GetWindow(id);
|
||||
if (window->id.id == 0) {
|
||||
return;
|
||||
}
|
||||
window->close = true;
|
||||
RunGCThisFrame = true;
|
||||
}
|
||||
|
||||
Window *FindWindow(ViewID view_id, Window *default_window = NULL) {
|
||||
For(Windows) {
|
||||
if (it->active_view == view_id) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return default_window;
|
||||
}
|
||||
|
||||
Window *FindWindow(String buffer_name, Window *default_window = NULL) {
|
||||
For(Windows) {
|
||||
View *it_view = GetView(it->active_view);
|
||||
Buffer *it_buffer = GetBuffer(it_view->active_buffer);
|
||||
if (it_buffer->name == buffer_name) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return default_window;
|
||||
}
|
||||
|
||||
Window *FindWindow(BufferID buffer_id) {
|
||||
For(Windows) {
|
||||
View *view = GetView(it->active_view);
|
||||
if (view->active_buffer == buffer_id) return it;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Window *GetActiveWind() {
|
||||
return GetWindow(ActiveWindowID);
|
||||
}
|
||||
|
||||
bool IsDocumentSelectionValid() {
|
||||
if (DocumentSelected.id == -1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsScrollbarSelectionValid() {
|
||||
if (ScrollbarSelected.id == -1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
Array<Window *> GetWindowZOrder(Allocator allocator) {
|
||||
Array<Window *> order = {allocator};
|
||||
For(Windows) if (it->z == 2) Add(&order, it);
|
||||
For(Windows) if (it->z == 1) Add(&order, it);
|
||||
For(Windows) if (it->z == 0) Add(&order, it);
|
||||
return order;
|
||||
}
|
||||
|
||||
Int GetExpandingBarSize(Window *window) {
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
float result = (float)buffer->line_starts.len * window->font->line_spacing;
|
||||
return (Int)result;
|
||||
}
|
||||
|
||||
View *WindowOpenBufferView(Window *new_parent_window, String name) {
|
||||
View *view = FindView(name);
|
||||
if (!view) {
|
||||
View *result = OpenBufferView(name);
|
||||
new_parent_window->active_view = result->id;
|
||||
return result;
|
||||
}
|
||||
|
||||
Window *window = FindWindow(view->id);
|
||||
if (!window) {
|
||||
new_parent_window->active_view = view->id;
|
||||
return view;
|
||||
}
|
||||
if (window == new_parent_window) {
|
||||
return view;
|
||||
}
|
||||
|
||||
Assert(window->active_view.id == view->id.id);
|
||||
View *result = OpenBufferView(name);
|
||||
new_parent_window->active_view = result->id;
|
||||
return result;
|
||||
}
|
||||
|
||||
void CalcNiceties(Window *n) {
|
||||
float scrollbar_size = (10.f * DPIScale);
|
||||
float line_numbers_size = (float)n->font->char_spacing * 10.f;
|
||||
if (DrawScrollbar && n->draw_scrollbar) n->scrollbar_rect = CutRight(&n->document_rect, (Int)scrollbar_size);
|
||||
if (DrawLineNumbers && n->draw_line_numbers) n->line_numbers_rect = CutLeft(&n->document_rect, (Int)line_numbers_size);
|
||||
}
|
||||
|
||||
double WindowCalcEvenResizerValue(Int screen_size_x, Int *out_count = NULL) {
|
||||
double w = 0;
|
||||
Int c = 0;
|
||||
ForItem(n, Windows) {
|
||||
if (n->primary) {
|
||||
w += n->weight;
|
||||
c += 1;
|
||||
}
|
||||
}
|
||||
if (out_count) *out_count = c;
|
||||
return (double)screen_size_x / w;
|
||||
}
|
||||
|
||||
void LayoutWindows(int16_t wx, int16_t wy) {
|
||||
ProfileFunction();
|
||||
Rect2I screen_rect = RectI0Size(wx, wy);
|
||||
|
||||
ForItem (n, Windows) {
|
||||
ForItem (hook, n->hooks) {
|
||||
if (hook.kind == HookKind_LayoutWindow) {
|
||||
ProfileScopeEx(it.name);
|
||||
HookParam param = {};
|
||||
param.layout.window = n;
|
||||
param.layout.rect = &screen_rect;
|
||||
param.layout.wx = wx;
|
||||
param.layout.wy = wy;
|
||||
hook.function(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Column layout
|
||||
Int c = 0;
|
||||
double size = WindowCalcEvenResizerValue(wx, &c);
|
||||
if (c == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
ForItem(n, Windows) {
|
||||
if (!n->primary) {
|
||||
continue;
|
||||
}
|
||||
|
||||
n->total_rect = n->document_rect = CutLeft(&screen_rect, (Int)(size * n->weight));
|
||||
if (i != (c - 1)) {
|
||||
Int resizer_size = (Int)(PrimaryFont.char_spacing*0.5f);
|
||||
n->resizer_rect = CutRight(&n->document_rect, resizer_size);
|
||||
} else {
|
||||
n->resizer_rect = {};
|
||||
}
|
||||
CalcNiceties(n);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
Window *GetOverlappingWindow(Vec2I p, Window *default_window = NULL) {
|
||||
For(Windows) {
|
||||
if (AreOverlapping(p, it->total_rect)) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return default_window;
|
||||
}
|
||||
|
||||
Vec2I GetSideOfWindow(Window *window, int direction) {
|
||||
Vec2I p = {};
|
||||
Rect2I rect = window->total_rect;
|
||||
float resizer_size = (float)window->font->char_spacing*0.5f; // @check_codebase_when_changing
|
||||
if (direction == DIR_LEFT) {
|
||||
p.x = rect.min.x - (Int)(resizer_size + window->font->char_spacing);
|
||||
p.y = rect.min.y + (rect.max.y / 2);
|
||||
} else if (direction == DIR_RIGHT) {
|
||||
p.x = rect.max.x + (Int)(resizer_size + window->font->char_spacing);
|
||||
p.y = rect.min.y + (rect.max.y / 2);
|
||||
} else if (direction == DIR_UP) {
|
||||
p.x = rect.min.x + (rect.max.x / 2);
|
||||
p.y = rect.min.y - (Int)(resizer_size + window->font->line_spacing);
|
||||
} else {
|
||||
Assert(direction == DIR_DOWN);
|
||||
p.x = rect.min.x + (rect.max.x / 2);
|
||||
p.y = rect.max.y + (Int)(resizer_size + window->font->line_spacing);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
Window *SwitchWindow(int direction) {
|
||||
Window *window = GetWindow(ActiveWindowID);
|
||||
Vec2I p = GetSideOfWindow(window, direction);
|
||||
Window *result = GetOverlappingWindow(p, window);
|
||||
return result;
|
||||
}
|
||||
|
||||
Int ScreenSpaceToBufferPos(Window *window, View *view, Buffer *buffer, Vec2I mouse) {
|
||||
Vec2I mworld = mouse - window->document_rect.min + view->scroll;
|
||||
double px = (double)mworld.x / (double)window->font->char_spacing;
|
||||
double py = (double)mworld.y / (double)window->font->line_spacing;
|
||||
XY xy = {(Int)round(px), (Int)floor(py)};
|
||||
Int result = XYToPosWithoutNL(buffer, xy);
|
||||
return result;
|
||||
}
|
||||
|
||||
Int ScreenSpaceToBufferPosErrorOutOfBounds(Window *window, View *view, Buffer *buffer, Vec2I mouse) {
|
||||
Vec2I mworld = mouse - window->document_rect.min + view->scroll;
|
||||
double px = (double)mworld.x / (double)window->font->char_spacing;
|
||||
double py = (double)mworld.y / (double)window->font->line_spacing;
|
||||
XY xy = {(Int)round(px), (Int)floor(py)};
|
||||
Int result = XYToPosErrorOutOfBounds(buffer, xy);
|
||||
return result;
|
||||
}
|
||||
|
||||
GotoCrumb PopCrumb(Array<GotoCrumb> *cr) {
|
||||
for (; cr->len;) {
|
||||
GotoCrumb c = Pop(cr);
|
||||
View *view = FindView(c.view_id, NULL);
|
||||
if (view) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
View *GetLastValidView(Window *window) {
|
||||
For (IterateInReverse(&window->goto_redo)) {
|
||||
if (it.view_id == window->active_view) continue;
|
||||
View *view = FindView(it.view_id, NULL);
|
||||
if (view) return view;
|
||||
}
|
||||
For (IterateInReverse(&window->goto_history)) {
|
||||
if (it.view_id == window->active_view) continue;
|
||||
View *view = FindView(it.view_id, NULL);
|
||||
if (view) return view;
|
||||
}
|
||||
return Views[0];
|
||||
}
|
||||
|
||||
View *JumpToLastValidView(Window *window) {
|
||||
View *view = GetLastValidView(window);
|
||||
window->active_view = view->id;
|
||||
return view;
|
||||
}
|
||||
|
||||
void JumpBack(Window *window) {
|
||||
if (window->jump_history == false) return;
|
||||
if (window->goto_history.len <= 0) return;
|
||||
BSet set = GetBSet(window);
|
||||
Add(&window->goto_redo, {set.view->id, set.view->carets[0], GetTimeSeconds()});
|
||||
|
||||
GotoCrumb c = PopCrumb(&window->goto_history);
|
||||
window->active_view = c.view_id;
|
||||
View *view = GetView(c.view_id);
|
||||
view->carets[0] = c.caret;
|
||||
CenterView(window->id);
|
||||
|
||||
if (window->goto_history.len) {
|
||||
GotoCrumb *next = GetLast(window->goto_history);
|
||||
if (c.view_id == next->view_id && ((c.time - next->time) <= JumpHistoryMergeTime)) {
|
||||
JumpBack(window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JumpForward(Window *window) {
|
||||
if (window->jump_history == false) return;
|
||||
if (window->goto_redo.len <= 0) return;
|
||||
BSet set = GetBSet(window);
|
||||
Add(&window->goto_history, {set.view->id, set.view->carets[0], GetTimeSeconds()});
|
||||
|
||||
GotoCrumb c = PopCrumb(&window->goto_redo);
|
||||
window->active_view = c.view_id;
|
||||
View *view = GetView(c.view_id);
|
||||
view->carets[0] = c.caret;
|
||||
CenterView(window->id);
|
||||
|
||||
if (window->goto_redo.len) {
|
||||
GotoCrumb *next = GetLast(window->goto_redo);
|
||||
if ((c.view_id == next->view_id) && ((next->time - c.time) <= JumpHistoryMergeTime)) {
|
||||
JumpForward(window);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
void LayoutBuildWindow(HookParam param) {
|
||||
auto p = param.layout;
|
||||
Rect2I copy_rect = *p.rect;
|
||||
if (!p.window->visible) {
|
||||
p.rect = ©_rect;
|
||||
}
|
||||
Int barsize = p.window->font->line_spacing * 10;
|
||||
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
|
||||
}
|
||||
|
||||
void CMD_ShowBuildWindow(HookParam param) {
|
||||
BSet main = GetBSet(BuildWindowID);
|
||||
if (ActiveWindowID != BuildWindowID) {
|
||||
main.window->visible = true;
|
||||
NextActiveWindowID = BuildWindowID;
|
||||
} else {
|
||||
main.window->visible = false;
|
||||
}
|
||||
} RegisterCommand(CMD_ShowBuildWindow, "ctrl-grave");
|
||||
|
||||
void InitBuildWindow(HookParam param) {
|
||||
Window *window = CreateWind();
|
||||
BuildWindowID = window->id;
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "build"));
|
||||
buffer->special = true;
|
||||
buffer->no_history = true;
|
||||
BuildBufferID = buffer->id;
|
||||
View *view = CreateView(buffer->id);
|
||||
view->special = true;
|
||||
BuildViewID = view->id;
|
||||
window->active_view = view->id;
|
||||
window->secondary_window_style = true;
|
||||
window->draw_line_highlight = true;
|
||||
window->primary = false;
|
||||
window->visible = false;
|
||||
window->lose_visibility_on_escape = true;
|
||||
window->jump_history = false;
|
||||
AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutBuildWindow", "", LayoutBuildWindow);
|
||||
} RegisterHook(InitBuildWindow, HookKind_AppInit, "", "Init the build window");
|
||||
@@ -1,252 +0,0 @@
|
||||
void CMD_ShowCommands(HookParam param) {
|
||||
// @todo: maybe redo this, similar behavior but use View stored information
|
||||
// if (ActiveWindowID == CommandWindowID && LastExecutedManualCommand == CMD_ShowCommands) {
|
||||
// NextActiveWindowID = PrimaryWindowID;
|
||||
// return;
|
||||
// }
|
||||
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
NextActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For (GlobalHooks) {
|
||||
if (it.name == "OpenCommand") {
|
||||
continue;
|
||||
}
|
||||
// RawAppendf(command_bar.buffer, "\n:%-30S <|| :Set %-30S '%-30S'", it.name, it.name, it.binding);
|
||||
RawAppendf(command_bar.buffer, "\n:%-30S <|| ", it.name);
|
||||
if (it.docs.len) {
|
||||
RawAppendf(command_bar.buffer, "%S", it.docs);
|
||||
}
|
||||
}
|
||||
command_bar.view->update_scroll = true;
|
||||
SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
|
||||
} RegisterCommand(CMD_ShowCommands, "ctrl-shift-p", "List available commands and their documentation inside the command window");
|
||||
|
||||
void CMD_ShowDebugBufferList(HookParam param) {
|
||||
// @todo: maybe redo this, similar behavior but use View stored information
|
||||
// if (ActiveWindowID == CommandWindowID && LastExecutedManualCommand == CMD_ShowDebugBufferList) {
|
||||
// NextActiveWindowID = PrimaryWindowID;
|
||||
// return;
|
||||
// }
|
||||
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
NextActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For (Buffers) {
|
||||
bool is_special = it->special || it->temp || it->is_dir || it->dont_try_to_save_in_bulk_ops;
|
||||
if (!is_special) {
|
||||
continue;
|
||||
}
|
||||
RawAppendf(command_bar.buffer, "\n%S", it->name);
|
||||
}
|
||||
command_bar.view->update_scroll = true;
|
||||
SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
|
||||
} RegisterCommand(CMD_ShowDebugBufferList, "ctrl-shift-alt-p", "Show full list of buffers, including the special ones that normally just clutter list");
|
||||
|
||||
void CMD_ShowBufferList(HookParam param) {
|
||||
// @todo: maybe redo this, similar behavior but use View stored information
|
||||
// if (ActiveWindowID == CommandWindowID && LastExecutedManualCommand == CMD_ShowBufferList) {
|
||||
// NextActiveWindowID = PrimaryWindowID;
|
||||
// return;
|
||||
// }
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
NextActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For (Buffers) {
|
||||
bool is_special = it->special || it->temp || it->is_dir || it->dont_try_to_save_in_bulk_ops;
|
||||
if (is_special) {
|
||||
continue;
|
||||
}
|
||||
RawAppendf(command_bar.buffer, "\n%S", it->name);
|
||||
}
|
||||
command_bar.view->update_scroll = true;
|
||||
SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
|
||||
} RegisterCommand(CMD_ShowBufferList, "ctrl-p", "List open buffers inside the command window that you can fuzzy search over");
|
||||
|
||||
void OpenCommand(BSet active) {
|
||||
String16 string = FetchFuzzyViewLoadLine(active.view);
|
||||
Open(string);
|
||||
}
|
||||
|
||||
struct SearchProjectParams {
|
||||
String16 needle;
|
||||
BufferID buffer;
|
||||
};
|
||||
|
||||
void Coro_SearchProject(mco_coro *co) {
|
||||
SearchProjectParams *param = (SearchProjectParams *)CoCurr->user_ctx;
|
||||
|
||||
Array<BufferID> buffers = {CoCurr->arena};
|
||||
For (Buffers) {
|
||||
Add(&buffers, it->id);
|
||||
}
|
||||
|
||||
ForItem (id, buffers) {
|
||||
Buffer *it = GetBuffer(id, NULL);
|
||||
if (it == NULL || it->special || it->is_dir || it->temp || it->dont_try_to_save_in_bulk_ops) {
|
||||
continue;
|
||||
}
|
||||
|
||||
{
|
||||
Scratch scratch;
|
||||
Array<Caret> occurences = FindAll(scratch, it, param->needle);
|
||||
Buffer *out_buffer = GetBuffer(param->buffer);
|
||||
ForItem (caret, occurences) {
|
||||
Int pos = caret.range.min;
|
||||
Int line = PosToLine(it, pos);
|
||||
Range range = GetLineRangeWithoutNL(it, line);
|
||||
Int column = pos - range.min;
|
||||
String16 line_string = GetString(it, range);
|
||||
String line_string8 = ToString(scratch, line_string);
|
||||
RawAppendf(out_buffer, "%S ||> %S:%lld:%lld\n", line_string8, it->name, (long long)line + 1, (long long)column + 1);
|
||||
}
|
||||
}
|
||||
CoYield(co);
|
||||
}
|
||||
}
|
||||
|
||||
float NewFuzzyRate(String16 s, String16 p) {
|
||||
float score = 0;
|
||||
// try to do this: https://github.com/junegunn/fzf/blob/master/src/algo/algo.go
|
||||
return score;
|
||||
}
|
||||
|
||||
float FuzzyRate(String16 s, String16 p) {
|
||||
float score = 0;
|
||||
for (Int outer_pi = 0; outer_pi < p.len; outer_pi += 1) {
|
||||
String16 pit = Skip(p, outer_pi);
|
||||
if (IsWhitespace(At(pit, 0))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
float matching = 0;
|
||||
for (Int outer_si = 0; outer_si < s.len; outer_si += 1) {
|
||||
String16 sit = Skip(s, outer_si);
|
||||
if (IsWhitespace(At(sit, 0))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Int si = 0;
|
||||
Int pi = 0;
|
||||
for (;si < sit.len && pi < pit.len;) {
|
||||
while (si < sit.len && IsWhitespace(sit[si])) si += 1;
|
||||
while (pi < pit.len && IsWhitespace(pit[pi])) pi += 1;
|
||||
if (pi >= pit.len) break;
|
||||
if (si >= sit.len) break;
|
||||
|
||||
if (ToLowerCase(sit[si]) == ToLowerCase(pit[pi])) {
|
||||
matching += 1.0f;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
si += 1;
|
||||
pi += 1;
|
||||
}
|
||||
}
|
||||
score += matching;
|
||||
}
|
||||
score = score / (float)s.len;
|
||||
return score;
|
||||
}
|
||||
|
||||
inline bool MergeSortCompare(FuzzyPair *a, FuzzyPair *b) {
|
||||
bool result = a->rating > b->rating;
|
||||
return result;
|
||||
}
|
||||
|
||||
Array<FuzzyPair> FuzzySearchLines(Allocator allocator, Buffer *buffer, Int line_min, Int line_max, String16 needle) {
|
||||
ProfileFunction();
|
||||
if (line_min < 0 || line_min >= buffer->line_starts.len) return {};
|
||||
if (line_max < 0 || line_min > buffer->line_starts.len) return {};
|
||||
Array<FuzzyPair> ratings = {allocator};
|
||||
Reserve(&ratings, line_max - line_min + 4);
|
||||
for (Int i = line_min; i < line_max; i += 1) {
|
||||
String16 s = GetLineStringWithoutNL(buffer, i);
|
||||
|
||||
Int idx = 0;
|
||||
if (Seek(s, u"||>", &idx, SeekFlag_None)) {
|
||||
s = GetPrefix(s, idx);
|
||||
} else if (Seek(s, u"<||", &idx, SeekFlag_None)) {
|
||||
s = GetPrefix(s, idx);
|
||||
}
|
||||
s = Trim(s);
|
||||
|
||||
float rating = FuzzyRate(s, needle);
|
||||
Add(&ratings, {(int32_t)i, rating});
|
||||
}
|
||||
Array<FuzzyPair> temp = Copy(allocator, ratings);
|
||||
MergeSort(ratings.len, ratings.data, temp.data);
|
||||
return ratings;
|
||||
}
|
||||
|
||||
void UpdateFuzzySearchView(HookParam param) {
|
||||
Scratch scratch;
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
String16 line_string = GetLineStringWithoutNL(active.buffer, 0);
|
||||
uint64_t hash = HashBytes(line_string.data, line_string.len * sizeof(char16_t));
|
||||
if (active.view->prev_search_line_hash != hash) {
|
||||
active.view->prev_search_line_hash = hash;
|
||||
Array<FuzzyPair> ratings = FuzzySearchLines(scratch, active.buffer, 1, active.buffer->line_starts.len, line_string);
|
||||
|
||||
Buffer *scratch_buff = CreateScratchBuffer(scratch, active.buffer->cap);
|
||||
RawAppend(scratch_buff, line_string);
|
||||
For(IterateInReverse(&ratings)) {
|
||||
String16 s = GetLineStringWithoutNL(active.buffer, it.index);
|
||||
if (s.len == 0) continue;
|
||||
RawAppend(scratch_buff, u"\n");
|
||||
RawAppend(scratch_buff, s);
|
||||
}
|
||||
|
||||
Caret caret = active.view->carets[0];
|
||||
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
|
||||
SelectEntireBuffer(active.view);
|
||||
Replace(active.view, GetString(scratch_buff));
|
||||
active.view->carets[0] = caret;
|
||||
}
|
||||
}
|
||||
|
||||
void SetFuzzy(View *view) {
|
||||
AddCommand(&view->hooks, "Open", "ctrl-q | enter | f12", [](HookParam){
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
NextActiveWindowID = main.window->id;
|
||||
String16 string = FetchFuzzyViewLoadLine(active.view);
|
||||
Open(string);
|
||||
});
|
||||
AddHook(&view->hooks, HookKind_AppUpdate, "UpdateFuzzySearchView", "", UpdateFuzzySearchView);
|
||||
}
|
||||
|
||||
void LayoutCommandWindow(HookParam param) {
|
||||
auto p = param.layout;
|
||||
Rect2I copy_rect = *p.rect;
|
||||
if (!p.window->visible) {
|
||||
p.rect = ©_rect;
|
||||
}
|
||||
Int barsize = Clamp((Int)p.window->font->line_spacing*10, (Int)0, (Int)p.wx - 100);
|
||||
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
|
||||
}
|
||||
|
||||
void InitCommandWindow(HookParam param) {
|
||||
Window *window = CreateWind();
|
||||
CommandWindowID = window->id;
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "command_bar"));
|
||||
buffer->special = true;
|
||||
buffer->no_history = true;
|
||||
View *view = CreateView(buffer->id);
|
||||
view->special = true;
|
||||
SetFuzzy(view);
|
||||
window->active_view = view->id;
|
||||
window->draw_line_numbers = false;
|
||||
window->draw_scrollbar = false;
|
||||
window->secondary_window_style = true;
|
||||
window->draw_line_highlight = true;
|
||||
window->primary = false;
|
||||
window->visible = false;
|
||||
window->sync_visibility_with_focus = true;
|
||||
window->lose_focus_on_escape = true;
|
||||
window->jump_history = false;
|
||||
AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutCommandWindow", "", LayoutCommandWindow);
|
||||
} RegisterHook(InitCommandWindow, HookKind_AppInit, "", "Init command window");
|
||||
@@ -1,87 +0,0 @@
|
||||
void LayoutDebugWindow(HookParam param) {
|
||||
auto p = param.layout;
|
||||
Rect2 screen_rect = Rect0Size((float)p.wx, (float)p.wy);
|
||||
Vec2 size = GetSize(screen_rect);
|
||||
|
||||
Rect2 a = CutRight(&screen_rect, 0.3f * size.x);
|
||||
Rect2 b = CutTop(&a, 0.4f * size.y);
|
||||
Rect2 c = Shrink(b, 20);
|
||||
p.window->document_rect = p.window->total_rect = ToRect2I(c);
|
||||
}
|
||||
|
||||
void UpdateDebugWindow(HookParam param) {
|
||||
ProfileFunction();
|
||||
BSet set = GetBSet(DebugWindowID);
|
||||
if (!set.window->visible) {
|
||||
return;
|
||||
}
|
||||
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
if (active.buffer->id.id == set.buffer->id.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
|
||||
Scratch scratch;
|
||||
String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld\n", (int)main.window->id.id, (int)main.view->id.id, (int)main.buffer->id.id, (long long)FrameID);
|
||||
String16 string = ToString16(scratch, s);
|
||||
RawReplaceText(set.buffer, GetRange(set.buffer), string);
|
||||
|
||||
float xmouse, ymouse;
|
||||
SDL_GetMouseState(&xmouse, &ymouse);
|
||||
RawAppendf(set.buffer, "mouse: [%f, %f]\n", roundf(DPIScale * xmouse), roundf(DPIScale * ymouse));
|
||||
|
||||
RawAppendf(set.buffer, "BufferID id = %d\n", main.buffer->id.id);
|
||||
RawAppendf(set.buffer, "String name = %S\n", main.buffer->name);
|
||||
RawAppendf(set.buffer, "Int change_id = %lld\n", (long long)main.buffer->change_id);
|
||||
RawAppendf(set.buffer, "Int user_change_id = %lld\n", (long long)main.buffer->user_change_id);
|
||||
RawAppendf(set.buffer, "Int file_mod_time = %lld\n", (long long)main.buffer->file_mod_time);
|
||||
RawAppendf(set.buffer, "\n");
|
||||
|
||||
RawAppendf(set.buffer, "U16 *data = %zu\n", main.buffer->data);
|
||||
RawAppendf(set.buffer, "Int len = %lld\n", (long long)main.buffer->len);
|
||||
RawAppendf(set.buffer, "Int cap = %lld\n", (long long)main.buffer->cap);
|
||||
RawAppendf(set.buffer, "Array<Int> line_starts = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->line_starts.len, (long long)main.buffer->line_starts.cap, main.buffer->line_starts.data);
|
||||
RawAppendf(set.buffer, "\n");
|
||||
|
||||
RawAppendf(set.buffer, "Array<HistoryEntry> undo_stack = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->undo_stack.len, (long long)main.buffer->undo_stack.cap, main.buffer->undo_stack.data);
|
||||
RawAppendf(set.buffer, "Array<HistoryEntry> redo_stack = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->redo_stack.len, (long long)main.buffer->redo_stack.cap, main.buffer->redo_stack.data);
|
||||
RawAppendf(set.buffer, "int edit_phase = %d", main.buffer->edit_phase);
|
||||
RawAppendf(set.buffer, "\n");
|
||||
|
||||
RawAppendf(set.buffer, "int no_history = %d\n", main.buffer->no_history);
|
||||
RawAppendf(set.buffer, "int no_line_starts = %d\n", main.buffer->no_line_starts);
|
||||
RawAppendf(set.buffer, "int dirty = %d\n", main.buffer->dirty);
|
||||
RawAppendf(set.buffer, "int changed_on_disk = %d\n", main.buffer->changed_on_disk);
|
||||
RawAppendf(set.buffer, "int temp = %d\n", main.buffer->temp);
|
||||
} RegisterHook(UpdateDebugWindow, HookKind_AppUpdate, "", "Update the debug window");
|
||||
|
||||
void InitDebugWindow(HookParam param) {
|
||||
Window *window = CreateWind();
|
||||
DebugWindowID = window->id;
|
||||
window->draw_line_numbers = false;
|
||||
window->draw_scrollbar = false;
|
||||
window->visible = false;
|
||||
window->z = 2;
|
||||
window->primary = false;
|
||||
window->jump_history = false;
|
||||
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "debug"));
|
||||
DebugBufferID = buffer->id;
|
||||
buffer->no_history = true;
|
||||
buffer->special = true;
|
||||
|
||||
View *view = CreateView(buffer->id);
|
||||
view->special = true;
|
||||
DebugViewID = view->id;
|
||||
window->active_view = view->id;
|
||||
window->visible = false;
|
||||
|
||||
AddHook(&window->hooks, HookKind_LayoutWindow, "@todo: LayoutDebugWindow", "", LayoutDebugWindow);
|
||||
} RegisterHook(InitDebugWindow, HookKind_AppInit, "", "Init the debug window");
|
||||
|
||||
void CMD_ToggleDebug(HookParam param) {
|
||||
Window *window = GetWindow(DebugWindowID);
|
||||
window->visible = !window->visible;
|
||||
} RegisterCommand(CMD_ToggleDebug, "ctrl-0", "Open a floating window that might become useful for debugging");
|
||||
@@ -1,99 +0,0 @@
|
||||
void StatusWindowUpdate(HookParam param) {
|
||||
ProfileFunction();
|
||||
Window *status_bar_window = GetWindow(StatusBarWindowID, NULL);
|
||||
Scratch scratch;
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
BSet title = GetBSet(status_bar_window);
|
||||
title.view->scroll.y = 0;
|
||||
|
||||
String16 buffer_string = GetString(title.buffer);
|
||||
Range replace_range = {0, title.buffer->len};
|
||||
bool found_separator = Seek(buffer_string, u" |", &replace_range.max);
|
||||
|
||||
// Parse the title and line
|
||||
if (title.window->id == ActiveWindowID) {
|
||||
if (title.buffer->change_id == title.buffer->begin_frame_change_id) {
|
||||
return;
|
||||
}
|
||||
String16 buffer_name = GetString(title.buffer, replace_range);
|
||||
buffer_name = Trim(buffer_name);
|
||||
|
||||
Int column = ChopNumber(&buffer_name);
|
||||
if (column == -1) return;
|
||||
|
||||
Chop(&buffer_name, u":");
|
||||
Int line = ChopNumber(&buffer_name);
|
||||
if (line == -1) {
|
||||
line = column;
|
||||
column = 0;
|
||||
}
|
||||
Chop(&buffer_name, u":");
|
||||
|
||||
Int buffer_pos = XYToPos(main.buffer, {column, line});
|
||||
Caret &caret = main.view->carets[0];
|
||||
if (GetFront(caret) != buffer_pos) {
|
||||
caret = MakeCaret(buffer_pos);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Caret caret = main.view->carets[0];
|
||||
XY xy = PosToXY(main.buffer, GetFront(caret));
|
||||
|
||||
// add separator at the end of buffer
|
||||
if (!found_separator) {
|
||||
SelectRange(title.view, GetBufferEndAsRange(title.buffer));
|
||||
ReplaceEx(scratch, title.view, u" | :Prev :Next :Close");
|
||||
}
|
||||
|
||||
// replace data up to separator with filename and stuff
|
||||
const char *reopen = main.buffer->changed_on_disk ? " :Reopen" : "";
|
||||
const char *case_sens = SearchCaseSensitive ? " C" : "";
|
||||
const char *word_bound = SearchWordBoundary ? " W" : "";
|
||||
const char *dirty = main.buffer->dirty ? " !" : "";
|
||||
String s = Format(scratch, "%S:%lld:%lld%s%s%s", main.buffer->name, (long long)xy.line + 1ll, (long long)xy.col + 1ll, dirty, case_sens, word_bound, reopen);
|
||||
For (ActiveProcesses) {
|
||||
if (it.view_id == main.view->id.id) {
|
||||
s = Format(scratch, "%S %lld :KillProcess", s, (long long)it.id);
|
||||
}
|
||||
}
|
||||
|
||||
String16 string = ToString16(scratch, s);
|
||||
String16 string_to_replace = GetString(title.buffer, replace_range);
|
||||
if (string_to_replace != string) {
|
||||
SelectRange(title.view, replace_range);
|
||||
ReplaceEx(scratch, title.view, string);
|
||||
}
|
||||
|
||||
SelectRange(title.view, MakeRange(0));
|
||||
ResetHistory(title.buffer);
|
||||
} RegisterHook(StatusWindowUpdate, HookKind_AppUpdate, "", "");
|
||||
|
||||
void LayoutStatusWindow(HookParam param) {
|
||||
auto p = param.layout;
|
||||
Rect2I copy_rect = *p.rect;
|
||||
if (!p.window->visible) {
|
||||
p.rect = ©_rect;
|
||||
}
|
||||
Int barsize = GetExpandingBarSize(p.window);
|
||||
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
|
||||
} RegisterHook(LayoutStatusWindow, HookKind_LayoutWindow, "", "Layout the status window");
|
||||
|
||||
void StatusWindowInit(HookParam param) {
|
||||
Window *window = CreateWind();
|
||||
StatusBarWindowID = window->id;
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "status_bar"));
|
||||
buffer->special = true;
|
||||
View *view = CreateView(buffer->id);
|
||||
view->special = true;
|
||||
window->active_view = view->id;
|
||||
// window->font = &SecondaryFont;
|
||||
window->draw_line_numbers = false;
|
||||
window->draw_scrollbar = false;
|
||||
window->draw_line_highlight = true;
|
||||
window->secondary_window_style = true;
|
||||
window->primary = false;
|
||||
window->jump_history = false;
|
||||
window->lose_focus_on_escape = true;
|
||||
AddHook(&window->hooks, HookKind_LayoutWindow, "@todo LayoutStatusWindow", "", LayoutStatusWindow);
|
||||
} RegisterHook(StatusWindowInit, HookKind_AppInit, "", "Init the status window");
|
||||
583
src/ui.cpp
Normal file
583
src/ui.cpp
Normal file
@@ -0,0 +1,583 @@
|
||||
void JumpTempBuffer(BSet *set, String buffer_name) {
|
||||
if (buffer_name.len == 0) {
|
||||
buffer_name = GetUniqueBufferName(GetDirectory(set->buffer), "temp");
|
||||
}
|
||||
set->view = WindowOpenBufferView(set->window, buffer_name);
|
||||
set->buffer = GetBuffer(set->view->active_buffer);
|
||||
set->buffer->temp = true;
|
||||
RunGCThisFrame = true;
|
||||
}
|
||||
|
||||
void AddCommand(Array<Command> *arr, String name, Trigger *trigger, CMDFunction *function) {
|
||||
IterRemove(*arr) {
|
||||
IterRemovePrepare(*arr);
|
||||
if (it.name == name) {
|
||||
remove_item = true;
|
||||
}
|
||||
}
|
||||
Command cmd = {};
|
||||
cmd.name = name;
|
||||
cmd.function = function;
|
||||
cmd.trigger = trigger;
|
||||
cmd.docs = "Not listing commands attached to things anywhere currently, maybe should change?";
|
||||
Add(arr, cmd);
|
||||
}
|
||||
|
||||
#define AddUIAction(VIEW,NAME,TRIGGER,ACTION) AddCommand(&((VIEW)->commands),(NAME),(TRIGGER),[](){BSet active = GetBSet(ActiveWindowID); active.view->ui_action = (ACTION);})
|
||||
UIAction WaitForUIAction(mco_coro *co, BSet main) {
|
||||
UIAction result = UIAction_Cancel;
|
||||
for (;;) {
|
||||
if (main.window->active_view != main.view->id || (main.window->id != PrimaryWindowID && main.window->id != ActiveWindowID) || main.window->close) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (main.view->ui_action != UIAction_Null) {
|
||||
result = main.view->ui_action;
|
||||
break;
|
||||
}
|
||||
|
||||
Yield(co);
|
||||
}
|
||||
Close(main.buffer->id);
|
||||
return result;
|
||||
}
|
||||
|
||||
UIAction QueryUserYesNoCancel(mco_coro *co, BSet main, String question) {
|
||||
JumpTempBuffer(&main);
|
||||
NextActiveWindowID = main.window->id;
|
||||
RawAppendf(main.buffer, R"==(
|
||||
%S
|
||||
|
||||
:Yes :No :Cancel
|
||||
)==", question);
|
||||
main.view->carets[0] = FindNext(main.buffer, u":Yes", MakeCaret(0));
|
||||
main.view->carets[0].range.min = main.view->carets[0].range.max;
|
||||
AddUIAction(main.view, "Yes", EnterKey, UIAction_Yes);
|
||||
AddUIAction(main.view, "No", NULL, UIAction_No);
|
||||
AddUIAction(main.view, "Cancel", EscapeKey, UIAction_Cancel);
|
||||
UIAction ui_action = WaitForUIAction(co, main);
|
||||
return ui_action;
|
||||
}
|
||||
|
||||
void ShowUIMessagef(const char *fmt, ...) {
|
||||
Scratch scratch;
|
||||
STRING_FORMAT(scratch, fmt, string);
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
JumpTempBuffer(&main);
|
||||
NextActiveWindowID = main.window->id;
|
||||
RawAppendf(main.buffer, "\n %S\n :Close\n", string);
|
||||
main.view->carets[0] = FindNext(main.buffer, u":Close", MakeCaret(0));
|
||||
AddCommand(&main.view->commands, "Close", EnterOrEscapeKeySet, [](){
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
Close(active.buffer->id);
|
||||
});
|
||||
}
|
||||
|
||||
void DetectUserFileCallback(Window *window, ResolvedOpen *resolved) {
|
||||
if (resolved->kind == OpenKind_Goto) {
|
||||
if (IsFile(resolved->path)) {
|
||||
resolved->kind = OpenKind_Skip;
|
||||
window->ui_query_file = Intern(&GlobalInternTable, resolved->path);
|
||||
window->after_resolve_open_hook = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String16 QueryUserString(mco_coro *co, String ask) {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
JumpTempBuffer(&main);
|
||||
NextActiveWindowID = main.window->id;
|
||||
RawAppendf(main.buffer, R"==(
|
||||
(Enter) :Submit :Cancel (Escape)
|
||||
|
||||
%S
|
||||
================================================
|
||||
>)==", ask);
|
||||
|
||||
main.view->carets[0] = MakeCaret(GetBufferEnd(main.buffer));
|
||||
AddUIAction(main.view, "Submit", EnterKey, UIAction_Yes);
|
||||
AddUIAction(main.view, "Cancel", EscapeKey, UIAction_Cancel);
|
||||
UIAction action = WaitForUIAction(co, main);
|
||||
if (action != UIAction_Yes) {
|
||||
return u"";
|
||||
}
|
||||
|
||||
Caret a = FindNext(main.buffer, u">", MakeCaret(-1));
|
||||
if (a.range.min == -1) {
|
||||
ReportErrorf("You changed the format of the UI buffer, I was looking for the '>' but can't find it, can't proceed");
|
||||
return u"";
|
||||
}
|
||||
|
||||
a.range.min = a.range.max + 1;
|
||||
a.range.max = GetBufferEnd(main.buffer);
|
||||
return GetString(main.buffer, a.range);
|
||||
}
|
||||
|
||||
// Considerations with coroutines:
|
||||
// 1. Does scratch memory leak across Yield boundary? Or interacts badly with Yield stuff in any way?
|
||||
// 2. Are pointers and globals correct over time? Or might they get deleted etc.
|
||||
// 3. Imagine a scenario where the coroutine gets deleted before completion, will the memory leak?
|
||||
UIAction ShowCloseAllUI(mco_coro *co) {
|
||||
CCtx *ctx = GetCoroutineContext();
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
Array<BufferID> buffers = {ctx->arena};
|
||||
For (Buffers) Add(&buffers, it->id);
|
||||
ForItem (id, buffers) {
|
||||
Buffer *it = GetBuffer(id, NULL);
|
||||
if (it == NULL || it->special || !it->dirty) {
|
||||
continue;
|
||||
}
|
||||
if (it->temp || it->dont_try_to_save_in_bulk_ops) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String question = Format(ctx->arena, "Do you want to save [%S] before closing?", it->name);
|
||||
UIAction ui_action = QueryUserYesNoCancel(co, main, question);
|
||||
it = GetBuffer(id, NULL);
|
||||
if (it && ui_action == UIAction_Yes) {
|
||||
SaveBuffer(it);
|
||||
} else if (ui_action == UIAction_No) {
|
||||
} else if (ui_action == UIAction_Cancel) {
|
||||
return UIAction_Cancel;
|
||||
} ElseInvalidCodepath();
|
||||
}
|
||||
|
||||
For(Buffers) {
|
||||
Close(it->id);
|
||||
}
|
||||
return UIAction_Yes;
|
||||
}
|
||||
|
||||
String QueryUserFile(mco_coro *co) {
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
Window *window = GetWindow(PrimaryWindowID);
|
||||
ViewID original_view = window->active_view;
|
||||
Open(GetPrimaryDirectory());
|
||||
BSet set = GetBSet(window);
|
||||
RawReplaceText(set.buffer, {}, u"Point at a file you want to open!");
|
||||
SelectRange(set.view, GetLineRangeWithoutNL(set.buffer, 0));
|
||||
window->after_resolve_open_hook = DetectUserFileCallback;
|
||||
String filename = "";
|
||||
for (;;) {
|
||||
BSet main = GetBSet(window);
|
||||
if ((main.window->id != PrimaryWindowID && main.window->id != ActiveWindowID) || main.window->close) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (window->after_resolve_open_hook == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
Yield(co);
|
||||
}
|
||||
window->active_view = original_view;
|
||||
return window->ui_query_file;
|
||||
#else
|
||||
String16 string16 = QueryUserString(co, "Please input the path to the desired file");
|
||||
String result = "";
|
||||
{
|
||||
Scratch scratch;
|
||||
String i = ToString(scratch, string16);
|
||||
result = Intern(&GlobalInternTable, i);
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CO_TestQueryFile(mco_coro *co) {
|
||||
//String file = QueryUserFile(co);
|
||||
String16 file16 = QueryUserString(co, "Input some kind of a string PLEASE!");
|
||||
{
|
||||
Scratch scratch;
|
||||
String file = ToString(scratch, file16);
|
||||
ReportConsolef("%S", file);
|
||||
}
|
||||
} RegisterCoroutineCommand(CO_TestQueryFile, "", "");
|
||||
|
||||
void MouseLoadWord(Event event, ResolveOpenMeta meta = ResolveOpenMeta_Normal) {
|
||||
Vec2I mouse = MouseVec2I();
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
|
||||
bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect);
|
||||
if (mouse_in_document) {
|
||||
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(active.window, active.view, active.buffer, mouse);
|
||||
if (p != -1) {
|
||||
Range enclose = EncloseLoadWord(active.buffer, p);
|
||||
if (InBounds(active.view->carets[0].range, p)) enclose = active.view->carets[0].range;
|
||||
String16 string = GetString(active.buffer, enclose);
|
||||
|
||||
active.view->carets.len = 1;
|
||||
active.view->carets[0] = MakeCaret(p);
|
||||
Open(string, meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool IsOpenBoundary(char c) {
|
||||
bool result = c == 0 || c == ':' || c == '\t' || c == '\n' || c == '"' || c == '\'';
|
||||
return result;
|
||||
}
|
||||
|
||||
Variable *GetVariable(String name) {
|
||||
Variable *var = NULL;
|
||||
For (Variables) {
|
||||
if (name == it.name) {
|
||||
var = ⁢
|
||||
break;
|
||||
}
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
String InsertVariables(Allocator allocator, String string) {
|
||||
Scratch scratch(allocator);
|
||||
Array<String> parts = {scratch};
|
||||
String it = string;
|
||||
for (;;) {
|
||||
int64_t idx = 0;
|
||||
bool found = Seek(it, "@", &idx, SeekFlag_None);
|
||||
if (!found) {
|
||||
if (it.len > 0) {
|
||||
Add(&parts, it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
String prev = GetPrefix(it, idx);
|
||||
if (prev.len > 0) {
|
||||
Add(&parts, prev);
|
||||
}
|
||||
|
||||
it = Skip(it, idx + 1);
|
||||
char c = At(it, 0);
|
||||
String name = {};
|
||||
if (c == '@') {
|
||||
Add(&parts, String{"@", 1});
|
||||
it = Skip(it, 1);
|
||||
continue;
|
||||
} else if (c == '(') {
|
||||
char *start = it.data + 1;
|
||||
while (At(it, 0) && At(it, 0) != ')') {
|
||||
it = Skip(it, 1);
|
||||
}
|
||||
Int len = it.data - start;
|
||||
name = {start, len};
|
||||
it = Skip(it, 1); // skip ')'
|
||||
} else {
|
||||
char *start = it.data;
|
||||
while (IsAlphanumeric(At(it, 0))) {
|
||||
it = Skip(it, 1);
|
||||
}
|
||||
Int len = it.data - start;
|
||||
name = {start, len};
|
||||
}
|
||||
Variable *variable = GetVariable(name);
|
||||
if (!variable) {
|
||||
ReportErrorf("Variable: %S, not found", name);
|
||||
return string;
|
||||
}
|
||||
if (variable->type != VariableType_String) {
|
||||
// @todo: this will not report - open will override
|
||||
ReportErrorf("Variable: %S, not of type String", variable->type);
|
||||
return string;
|
||||
}
|
||||
|
||||
Add(&parts, *variable->string);
|
||||
}
|
||||
|
||||
String result = Merge(allocator, parts, "");
|
||||
return result;
|
||||
}
|
||||
|
||||
void TestInsertVariable() {
|
||||
Scratch scratch;
|
||||
|
||||
String a = "Thing/@(ProjectFolder)/Another";
|
||||
String b = "Thing/@ProjectFolder/Another";
|
||||
Assert(InsertVariables(scratch, a) == InsertVariables(scratch, b));
|
||||
|
||||
} RegisterFunction(&TestFunctions, TestInsertVariable);
|
||||
|
||||
/*
|
||||
|
||||
Variables:
|
||||
@ProjectFolder/build/te
|
||||
@(ProjectFolder)/build/te
|
||||
|
||||
Start of string matchers:
|
||||
! Execute with default shell
|
||||
!! Execute hidden with default shell
|
||||
:Command Execute editor command
|
||||
:Set Special case for config stuff
|
||||
http://
|
||||
https://
|
||||
commit
|
||||
py: @todo, execute in python shell, this will use the python3 shell and pass the rest to it's stdin or maybe as a file
|
||||
|
||||
More comprehensive syntax for commands:
|
||||
!{/usr/bin/python,Hidden,Input:Clipboard,Output:Clipboard,WorkingDirectory:@ProjectFolder,Env:{Thing:asd}}
|
||||
|
||||
Otherwise it does filepath parsing:
|
||||
C:/windows/path
|
||||
/unix/path
|
||||
./it/also/does/linenums:32:3
|
||||
/as/well/as/this/format(111,1)
|
||||
./thing(10)
|
||||
|
||||
|
||||
USECASE: Wouldn't it be cool to just select a part of codebase pipe that into a script
|
||||
and get a result in a clipboard or capture the output and change the selection?
|
||||
|
||||
PREV IDEA:
|
||||
!{bash,Out:Sel} SCRIPT
|
||||
!{bash,Out:Clip} SCRIPT
|
||||
Use variables for injecting selection: @Sel
|
||||
|
||||
TODO: I would pause the data desc language, seems a bit cumbersome... think of more concrete, constrained ideas
|
||||
TODO: Unify lexers (Set and Trigger)
|
||||
|
||||
*/
|
||||
ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpenMeta meta) {
|
||||
ResolvedOpen result = {};
|
||||
path = Trim(path);
|
||||
bool exec = !(ResolveOpenMeta_DontExec & meta);
|
||||
|
||||
#if PLUGIN_CONFIG
|
||||
path = InsertVariables(alo, path);
|
||||
|
||||
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, ":Set ")) {
|
||||
result.kind = OpenKind_Set;
|
||||
result.path = Skip(path, 5);
|
||||
}
|
||||
#endif
|
||||
|
||||
// :Command
|
||||
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, ":")) {
|
||||
result.kind = OpenKind_Command;
|
||||
path = Skip(path, 1);
|
||||
result.path.data = path.data;
|
||||
for (Int i = 0; i < path.len; i += 1) {
|
||||
if (IsNonWord(path.data[i])) {
|
||||
break;
|
||||
}
|
||||
result.path.len += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// !!exec_hidden
|
||||
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, "!!")) {
|
||||
result.kind = OpenKind_Exec;
|
||||
result.exec_in_background = 1;
|
||||
result.path = Skip(path, 2);
|
||||
}
|
||||
|
||||
// !exec
|
||||
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, "!")) {
|
||||
result.kind = OpenKind_Exec;
|
||||
result.path = Skip(path, 1);
|
||||
}
|
||||
|
||||
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, "py:")) {
|
||||
result.kind = OpenKind_Exec;
|
||||
result.path = Skip(path, 3);
|
||||
result.use_python_shell = 1;
|
||||
}
|
||||
|
||||
// https://web
|
||||
bool web = StartsWith(path, "https://") || StartsWith(path, "http://");
|
||||
if (exec && result.kind == OpenKind_Invalid && web) {
|
||||
result.path = Format(alo, "%S %S", InternetBrowser, path);
|
||||
result.kind = OpenKind_Exec;
|
||||
result.exec_in_background = 1;
|
||||
}
|
||||
|
||||
// commit 3kc09as92
|
||||
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, "commit ")) {
|
||||
path = Skip(path, 7);
|
||||
result.path = Format(alo, "git --no-pager show %S", path);
|
||||
result.kind = OpenKind_Exec;
|
||||
}
|
||||
|
||||
// c:/filename:32:12
|
||||
if (result.kind == OpenKind_Invalid) {
|
||||
String p = NormalizePath(alo, path);
|
||||
if (p.len == 2 && IsAlphabetic(ToLowerCase(At(p, 0))) && At(p, 1) == ':') {
|
||||
p = Format(alo, "%S/", p);
|
||||
}
|
||||
String pstart = p;
|
||||
|
||||
bool is_absolute = false;
|
||||
if (IsAlphabetic(ToLowerCase(At(p, 0))) && At(p, 1) == ':' && At(p, 2) == '/') {
|
||||
is_absolute = true;
|
||||
p = Skip(p, 3);
|
||||
} else if (At(p, 0) == '/') {
|
||||
is_absolute = true;
|
||||
p = Skip(p, 1);
|
||||
}
|
||||
|
||||
while (!IsOpenBoundary(At(p, 0))) {
|
||||
p = Skip(p, 1);
|
||||
}
|
||||
path = {pstart.data, (Int)(p.data - pstart.data)};
|
||||
|
||||
// For (LINE:COLUMN): error: - we can either backtrack at the end since we are including
|
||||
// the parenthesis and whitespace or alternatively we can look for patterns on every
|
||||
// character move in the loop... For now let's do backtracking. This doesn't handle all paths
|
||||
// but not sure if that's even what we want. ALL paths is hard.
|
||||
{
|
||||
Int i = path.len - 1;
|
||||
if (At(path, i) == ')') {
|
||||
i -= 1;
|
||||
|
||||
Int end = i;
|
||||
while (IsDigit(At(path, i))) {
|
||||
i -= 1;
|
||||
}
|
||||
Int start = i;
|
||||
String b = {path.data + 1 + start, (end - start)};
|
||||
|
||||
if (At(path, i) == '(') {
|
||||
i -= 1;
|
||||
path.len = i + 1;
|
||||
result.line = strtoll(b.data, NULL, 10);
|
||||
} else if (At(path, i) == ',') {
|
||||
i -= 1;
|
||||
|
||||
end = i;
|
||||
while (IsDigit(At(path, i))) {
|
||||
i -= 1;
|
||||
}
|
||||
start = i;
|
||||
String a = {path.data + 1 + start, (end - start)};
|
||||
|
||||
if (At(path, i) == '(') {
|
||||
i -= 1;
|
||||
path.len = i + 1;
|
||||
result.line = strtoll(a.data, NULL, 10);
|
||||
result.col = strtoll(b.data, NULL, 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.line == 0 && At(p, 0) == ':') {
|
||||
p = Skip(p, 1);
|
||||
result.line = SkipInt(&p);
|
||||
if (At(p, 0) == ':') {
|
||||
p = Skip(p, 1);
|
||||
Int b = SkipInt(&p);
|
||||
result.col = b;
|
||||
}
|
||||
}
|
||||
|
||||
Buffer *existing_buffer = GetBuffer(path, NULL);
|
||||
if (result.kind == OpenKind_Invalid && existing_buffer != NULL) {
|
||||
result.path = path;
|
||||
result.kind = OpenKind_Goto;
|
||||
result.existing_buffer = true;
|
||||
}
|
||||
|
||||
if (result.kind == OpenKind_Invalid && is_absolute && FileExists(path)) {
|
||||
result.path = path;
|
||||
result.kind = OpenKind_Goto;
|
||||
}
|
||||
|
||||
if (result.kind == OpenKind_Invalid) {
|
||||
String rel_path = Format(alo, "%S/%S", GetDirectory(window), path);
|
||||
existing_buffer = GetBuffer(rel_path, NULL);
|
||||
if (existing_buffer || FileExists(rel_path)) {
|
||||
if (existing_buffer) {
|
||||
result.existing_buffer = 1;
|
||||
}
|
||||
result.path = rel_path;
|
||||
result.kind = OpenKind_Goto;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.kind == OpenKind_Invalid && (meta & ResolveOpenMeta_DontError)) {
|
||||
result.path = path;
|
||||
result.kind = OpenKind_Skip;
|
||||
}
|
||||
|
||||
if (window->after_resolve_open_hook) {
|
||||
window->after_resolve_open_hook(window, &result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
BSet Open(Window *window, String path, ResolveOpenMeta meta, bool set_active = true) {
|
||||
Scratch scratch;
|
||||
BSet set = GetBSet(window);
|
||||
ResolvedOpen o = ResolveOpen(scratch, window, path, meta);
|
||||
if (o.kind == OpenKind_Goto) {
|
||||
if (set_active) {
|
||||
NextActiveWindowID = set.window->id;
|
||||
}
|
||||
View *view = WindowOpenBufferView(set.window, o.path);
|
||||
if (IsDir(o.path)) {
|
||||
#if PLUGIN_DIRECTORY_NAVIGATION
|
||||
OpenDirectoryNavigation(view);
|
||||
#endif
|
||||
}
|
||||
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
if (o.line != 0) {
|
||||
if (o.col == 0) o.col = 1;
|
||||
Int pos = XYToPos(buffer, {o.col - 1, o.line - 1});
|
||||
SelectRange(view, MakeCaret(pos));
|
||||
}
|
||||
CenterView(window->id);
|
||||
} else if (o.kind == OpenKind_Exec) {
|
||||
ExecArgs args = {};// ShellArgs(scratch, LogView->id, o.path, GetPrimaryDirectory());
|
||||
args.env = ProcessEnviroment;
|
||||
args.poll_process = 1;
|
||||
args.output_view = LogView->id;
|
||||
args.cwd = GetPrimaryDirectory();
|
||||
if (o.exec_in_background == 0) {
|
||||
if (set_active) {
|
||||
NextActiveWindowID = set.window->id;
|
||||
}
|
||||
JumpTempBuffer(&set);
|
||||
RawAppend(set.buffer, u"\n");
|
||||
args.output_view = set.view->id;
|
||||
}
|
||||
|
||||
if (o.use_python_shell == 1) {
|
||||
args.exe = FindPython(scratch);
|
||||
String temp_file = WriteTempFile(scratch, o.path);
|
||||
args.cmd = Format(scratch, "%S %S", args.exe, temp_file);
|
||||
} else {
|
||||
SetShell(scratch, &args.exe, &args.cmd, o.path);
|
||||
}
|
||||
|
||||
Exec(args);
|
||||
} else if (o.kind == OpenKind_Command) {
|
||||
EvalCommand(o.path);
|
||||
}
|
||||
#if PLUGIN_CONFIG
|
||||
else if (o.kind == OpenKind_Set) {
|
||||
Set(o.path);
|
||||
}
|
||||
#endif
|
||||
else if (o.kind == OpenKind_Skip) {
|
||||
return {};
|
||||
} else {
|
||||
ReportErrorf("Failed to open: %S", path);
|
||||
}
|
||||
|
||||
return GetBSet(window);
|
||||
}
|
||||
|
||||
BSet Open(String path, ResolveOpenMeta meta) {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
main = Open(main.window, path, meta);
|
||||
return main;
|
||||
}
|
||||
|
||||
BSet Open(String16 path, ResolveOpenMeta meta) {
|
||||
Scratch scratch;
|
||||
String string = ToString(scratch, path);
|
||||
return Open(string, meta);
|
||||
}
|
||||
@@ -8,7 +8,7 @@ API View *CreateView(BufferID active_buffer) {
|
||||
view->id = AllocViewID(view);
|
||||
view->active_buffer = active_buffer;
|
||||
view->carets.allocator = al;
|
||||
view->hooks.allocator = al;
|
||||
view->commands.allocator = al;
|
||||
Add(&view->carets, {0, 0});
|
||||
Add(&Views, view);
|
||||
return view;
|
||||
@@ -16,31 +16,62 @@ API View *CreateView(BufferID active_buffer) {
|
||||
|
||||
void Dealloc(View *view) {
|
||||
Dealloc(&view->carets);
|
||||
Dealloc(&view->hooks);
|
||||
Dealloc(&view->commands);
|
||||
Dealloc(view->carets.allocator, view);
|
||||
}
|
||||
|
||||
// TODO: default_view should probably be Views[0] but could break stuff
|
||||
API View *FindView(ViewID view_id, View *default_view = NULL) {
|
||||
For(Views) {
|
||||
if (it->id == view_id) {
|
||||
return it;
|
||||
View *GetView(ViewID id, View *default_view = Views[0]) {
|
||||
Int left = 0;
|
||||
Int right = Views.len - 1;
|
||||
View *result = default_view;
|
||||
|
||||
while (left <= right) {
|
||||
Int mid = left + (right - left) / 2;
|
||||
View *it = Views[mid];
|
||||
if (it->id == id) {
|
||||
result = it;
|
||||
break;
|
||||
} else if (it->id.id < id.id) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
return default_view;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
API View *FindView(BufferID buffer_id, View *default_view) {
|
||||
For(Views) {
|
||||
if (it->active_buffer == buffer_id) {
|
||||
return it;
|
||||
}
|
||||
View *GetViewForBuffer(Buffer *buffer, bool *is_active = NULL) {
|
||||
View *view = NULL;
|
||||
if (is_active) {
|
||||
*is_active = false;
|
||||
}
|
||||
return default_view;
|
||||
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
if (active.buffer->id == buffer->id) {
|
||||
if (is_active) {
|
||||
*is_active = true;
|
||||
}
|
||||
return active.view;
|
||||
}
|
||||
|
||||
For(Views) {
|
||||
if (it->active_buffer != buffer->id) {
|
||||
continue;
|
||||
}
|
||||
view = it;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!view) {
|
||||
view = CreateView(buffer->id);
|
||||
}
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
API View *FindView(String name, View *default_view = NULL) {
|
||||
For(Views) {
|
||||
API View *GetView(String name, View *default_view = Views[0]) {
|
||||
For (Views) {
|
||||
Buffer *buffer = GetBuffer(it->active_buffer);
|
||||
if (buffer->name == name) {
|
||||
return it;
|
||||
@@ -49,8 +80,13 @@ API View *FindView(String name, View *default_view = NULL) {
|
||||
return default_view;
|
||||
}
|
||||
|
||||
API View *GetView(ViewID id) {
|
||||
return FindView(id, Views[0]);
|
||||
API View *FindView(BufferID buffer_id, View *default_view) {
|
||||
For (Views) {
|
||||
if (it->active_buffer == buffer_id) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return default_view;
|
||||
}
|
||||
|
||||
API View *OpenBufferView(String name) {
|
||||
@@ -67,6 +103,22 @@ API bool ViewIsCrumb(ViewID view_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
API Int GetViewReferencedInHistoryRating(ViewID view_id) {
|
||||
ForItem (window, Windows) {
|
||||
For (window->goto_redo) if (it.view_id == view_id) return 1;
|
||||
Int i = 0;
|
||||
For (window->goto_history) {
|
||||
i += 1;
|
||||
if (it.view_id == view_id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool ViewIsActive(ViewID id) {
|
||||
For (Windows) {
|
||||
if (it->active_view == id) {
|
||||
@@ -76,22 +128,34 @@ bool ViewIsActive(ViewID id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
API bool ViewIsReferenced(ViewID view) {
|
||||
if (view == NullViewID) {
|
||||
API Int ViewIsReferenced(View *view) {
|
||||
if (view->special) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ViewIsActive(view->id)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return GetViewReferencedInHistoryRating(view->id);
|
||||
}
|
||||
|
||||
bool BufferIsReferenced(Buffer *buffer) {
|
||||
if (buffer->special) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ViewIsCrumb(view)) {
|
||||
if (FindView(buffer->id, NULL)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ViewIsActive(view);
|
||||
return false;
|
||||
}
|
||||
|
||||
void Close(ViewID id) {
|
||||
View *view = GetView(id);
|
||||
View *view = GetView(id, NULL);
|
||||
if (view) {
|
||||
if (view->id.id == 0) {
|
||||
if (view->special) {
|
||||
return;
|
||||
}
|
||||
view->close = true;
|
||||
@@ -110,65 +174,39 @@ String16 FetchLoadWord(View *view) {
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Caret caret = view->carets[0];
|
||||
Range range = caret.range;
|
||||
if (GetSize(caret.range) == 0) range = EncloseLoadWord(buffer, GetFront(caret));
|
||||
if (GetSize(caret.range) == 0) {
|
||||
range = EncloseLoadWord(buffer, GetFront(caret));
|
||||
}
|
||||
String16 string = GetString(buffer, range);
|
||||
return string;
|
||||
}
|
||||
|
||||
String16 FetchFuzzyViewLoadLine(View *view) {
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Range range = view->carets[0].range;
|
||||
String16 string = GetString(buffer, range);
|
||||
if (GetSize(range) == 0) {
|
||||
Int line = PosToLine(buffer, range.min);
|
||||
if (line == 0) {
|
||||
line = ClampTop(1ll, buffer->line_starts.len - 1ll);
|
||||
}
|
||||
string = GetLineStringWithoutNL(buffer, line);
|
||||
Array<Range> GetSelectedLinesSortedExclusive(Allocator allocator, View *view) {
|
||||
Scratch scratch(allocator);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Array<Caret> caret_copy = TightCopy(scratch, view->carets);
|
||||
Array<Caret> temp = TightCopy(scratch, view->carets);
|
||||
if (view->carets.len > 1) MergeSort(view->carets.len, caret_copy.data, temp.data);
|
||||
|
||||
Int idx = 0;
|
||||
String16 delim = u"||>";
|
||||
if (Seek(string, delim, &idx, SeekFlag_None)) {
|
||||
string = Skip(string, idx + delim.len);
|
||||
Array<Range> result = {allocator};
|
||||
For(caret_copy) {
|
||||
Int min_line = PosToLine(buffer, it.range.min);
|
||||
Int max_line = PosToLine(buffer, it.range.max);
|
||||
Range line_range = {min_line, max_line + 1};
|
||||
|
||||
if (result.len == 0) {
|
||||
Add(&result, line_range);
|
||||
continue;
|
||||
}
|
||||
|
||||
Range *last = GetLast(result);
|
||||
if (AreOverlapping(*last, line_range)) {
|
||||
last->max = Max(last->max, line_range.max);
|
||||
} else {
|
||||
Add(&result, line_range);
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
char16_t GetIndentChar() {
|
||||
char16_t c = u' ';
|
||||
if (IndentKindWhichIsTabsOrSpaces == "spaces") {
|
||||
c = u' ';
|
||||
} else if (IndentKindWhichIsTabsOrSpaces == "tabs") {
|
||||
c = u'\t';
|
||||
} else {
|
||||
ReportErrorf("Invalid IndentKindWhichIsTabsOrSpaces value: %S", IndentKindWhichIsTabsOrSpaces);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
String16 GetIndentString(Allocator allocator, Int indent_size) {
|
||||
char16_t *result = AllocArray(allocator, char16_t, indent_size + 1);
|
||||
char16_t c = GetIndentChar();
|
||||
|
||||
for (int i = 0; i < indent_size; i += 1) {
|
||||
result[i] = c;
|
||||
}
|
||||
result[indent_size] = 0;
|
||||
String16 res = {result, indent_size};
|
||||
return res;
|
||||
}
|
||||
|
||||
String GetIndentString8(Allocator allocator, Int indent_size) {
|
||||
char *result = AllocArray(allocator, char, indent_size + 1);
|
||||
char c = (char)GetIndentChar();
|
||||
|
||||
for (int i = 0; i < indent_size; i += 1) {
|
||||
result[i] = c;
|
||||
}
|
||||
result[indent_size] = 0;
|
||||
String res = {result, indent_size};
|
||||
return res;
|
||||
return result;
|
||||
}
|
||||
|
||||
void IndentedNewLine(View *view) {
|
||||
@@ -179,24 +217,38 @@ void IndentedNewLine(View *view) {
|
||||
For(view->carets) {
|
||||
Int front = GetFront(it);
|
||||
Int indent = GetLineIndent(buffer, PosToLine(buffer, front));
|
||||
String indent_string = GetIndentString8(scratch, indent);
|
||||
String string = Format(scratch, "\n%S", indent_string);
|
||||
String string = Format(scratch, "\n%.*s", indent, " ");
|
||||
String16 string16 = ToString16(scratch, string);
|
||||
AddEdit(&edits, it.range, string16);
|
||||
}
|
||||
EndEdit(buffer, &edits, &view->carets, KILL_SELECTION);
|
||||
EndEdit(buffer, &edits, &view->carets, EndEdit_KillSelection);
|
||||
}
|
||||
|
||||
Caret FindPrev(Buffer *buffer, String16 needle, Caret caret) {
|
||||
Int pos = GetMin(caret);
|
||||
String16 medium = GetString(buffer, {0, pos});
|
||||
SeekFlag flag = SearchCaseSensitive ? SeekFlag_None : SeekFlag_IgnoreCase;
|
||||
if (SearchWordBoundary) {
|
||||
flag |= SeekFlag_WordBoundary;
|
||||
}
|
||||
// WARNING: Don't use in user facing stuff
|
||||
Caret BaseFindNext(Buffer *buffer, String16 needle, Caret caret, SeekFlag flag) {
|
||||
Int pos = GetMax(caret);
|
||||
String16 medium = GetString(buffer, {pos, INT64_MAX});
|
||||
|
||||
Caret result = caret;
|
||||
Int index = 0;
|
||||
Int index = 0;
|
||||
if (Seek(medium, needle, &index, flag)) {
|
||||
result = MakeCaret(pos + index + needle.len, pos + index);
|
||||
} else {
|
||||
medium = GetString(buffer);
|
||||
if (Seek(medium, needle, &index, flag)) {
|
||||
result = MakeCaret(index + needle.len, index);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// WARNING: Don't use in user facing stuff
|
||||
Caret BaseFindPrev(Buffer *buffer, String16 needle, Caret caret, SeekFlag flag) {
|
||||
Int pos = GetMin(caret);
|
||||
String16 medium = GetString(buffer, {0, pos});
|
||||
Caret result = caret;
|
||||
Int index = 0;
|
||||
if (Seek(medium, needle, &index, flag | SeekFlag_MatchFindLast)) {
|
||||
result = MakeCaret(index, index + needle.len);
|
||||
} else {
|
||||
@@ -209,26 +261,20 @@ Caret FindPrev(Buffer *buffer, String16 needle, Caret caret) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Caret FindNext(Buffer *buffer, String16 needle, Caret caret) {
|
||||
Int pos = GetMax(caret);
|
||||
String16 medium = GetString(buffer, {pos, INT64_MAX});
|
||||
Caret FindPrev(Buffer *buffer, String16 needle, Caret caret) {
|
||||
SeekFlag flag = SearchCaseSensitive ? SeekFlag_None : SeekFlag_IgnoreCase;
|
||||
if (SearchWordBoundary) {
|
||||
flag |= SeekFlag_WordBoundary;
|
||||
}
|
||||
return BaseFindPrev(buffer, needle, caret, flag);
|
||||
}
|
||||
|
||||
Caret result = caret;
|
||||
Int index = 0;
|
||||
if (Seek(medium, needle, &index, flag)) {
|
||||
result = MakeCaret(pos + index + needle.len, pos + index);
|
||||
} else {
|
||||
medium = GetString(buffer);
|
||||
if (Seek(medium, needle, &index, flag)) {
|
||||
result = MakeCaret(index + needle.len, index);
|
||||
}
|
||||
Caret FindNext(Buffer *buffer, String16 needle, Caret caret) {
|
||||
SeekFlag flag = SearchCaseSensitive ? SeekFlag_None : SeekFlag_IgnoreCase;
|
||||
if (SearchWordBoundary) {
|
||||
flag |= SeekFlag_WordBoundary;
|
||||
}
|
||||
|
||||
return result;
|
||||
return BaseFindNext(buffer, needle, caret, flag);
|
||||
}
|
||||
|
||||
void FindAllEx(Array<Caret> *n, Buffer *buffer, String16 needle) {
|
||||
@@ -389,52 +435,91 @@ void MoveCarets(View *view, int direction, bool ctrl = false, bool shift = false
|
||||
}
|
||||
|
||||
void MoveCaretsLine(View *view, int direction) {
|
||||
// This is so hard...
|
||||
// It's because of the corner cases in the implementation, would be nice to simplify this somehow
|
||||
// but don't know how. The multiple carets constraints + not every line ends with new line make it into
|
||||
// really tricky code.
|
||||
Assert(direction == DIR_DOWN || direction == DIR_UP);
|
||||
Scratch scratch;
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
|
||||
// @todo: this doesn't work well at the end of buffer
|
||||
struct XYPair {
|
||||
XY front;
|
||||
XY back;
|
||||
};
|
||||
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
|
||||
MergeCarets(buffer, &view->carets);
|
||||
// Save caret positions to fix them at end to the expected incremented by one positions
|
||||
// :PreserveXYCarets (kind of)
|
||||
struct XYPair { XY front; XY back; };
|
||||
Array<XYPair> saved_xy = {scratch};
|
||||
For(view->carets) {
|
||||
Int eof_current = 0;
|
||||
Range lines_to_move_range = {GetFullLineStart(buffer, it.range.min), GetFullLineEnd(buffer, it.range.max, &eof_current)};
|
||||
if (lines_to_move_range.min == 0 && direction == DIR_UP) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Int eof = 0;
|
||||
Int next_line_start = lines_to_move_range.max;
|
||||
Int next_line_end = GetFullLineEnd(buffer, next_line_start, &eof);
|
||||
Int prev_line_end = lines_to_move_range.min - 1;
|
||||
Int prev_line_start = GetFullLineStart(buffer, prev_line_end);
|
||||
|
||||
if (direction == DIR_DOWN && eof) {
|
||||
continue;
|
||||
} else if (direction == DIR_UP && eof_current) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String16 string = Copy16(scratch, GetString(buffer, lines_to_move_range));
|
||||
|
||||
AddEdit(&edits, lines_to_move_range, {});
|
||||
if (direction == DIR_DOWN) {
|
||||
AddEdit(&edits, MakeRange(next_line_end), string);
|
||||
} else {
|
||||
AddEdit(&edits, MakeRange(prev_line_start), string);
|
||||
}
|
||||
|
||||
For (view->carets) {
|
||||
Add(&saved_xy, {PosToXY(buffer, GetFront(it)), PosToXY(buffer, GetBack(it))});
|
||||
}
|
||||
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
|
||||
|
||||
|
||||
Int line_offset = direction == DIR_UP ? -1 : +1;
|
||||
int side_idx = direction == DIR_UP ? 0 : 1;
|
||||
|
||||
Array<Range> line_ranges = GetSelectedLinesSortedExclusive(scratch, view); // This is <min_line, max_line> not positions
|
||||
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
|
||||
MergeCarets(buffer, &view->carets);
|
||||
ForItem (_lines, line_ranges) {
|
||||
Range lines = {_lines.min, _lines.max - 1}; // inclusive
|
||||
Int swap_line = lines.e[side_idx] + line_offset;
|
||||
|
||||
Range total_range = {};
|
||||
{
|
||||
Int total_min_line = 0;
|
||||
Int total_max_line = 0;
|
||||
if (direction == DIR_UP) {
|
||||
total_min_line = swap_line;
|
||||
total_max_line = lines.max;
|
||||
if (total_min_line < 0) {
|
||||
edits.len = 0;
|
||||
saved_xy.len = 0;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
total_min_line = lines.min;
|
||||
total_max_line = swap_line;
|
||||
if (total_max_line >= buffer->line_starts.len) {
|
||||
edits.len = 0;
|
||||
saved_xy.len = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Range arange = GetLineRange(buffer, total_min_line);
|
||||
Range brange = GetLineRange(buffer, total_max_line);
|
||||
total_range = {arange.min, brange.max};
|
||||
}
|
||||
|
||||
String16 replacement_string = {};
|
||||
{
|
||||
Range selected_range_min = GetLineRange(buffer, lines.min);
|
||||
Range selected_range_max = GetLineRange(buffer, lines.max);
|
||||
Range swap_line_range = GetLineRange(buffer, swap_line);
|
||||
String16 selected_string = GetString(buffer, {selected_range_min.min, selected_range_max.max});
|
||||
String16 swap_string = GetString(buffer, swap_line_range);
|
||||
if (direction == DIR_UP) {
|
||||
bool ends_with_new_line = selected_string.len == 0 || (selected_string.len && selected_string[selected_string.len - 1] == u'\n');
|
||||
bool doesnt_end_with_new_line_special_case = !ends_with_new_line;
|
||||
if (doesnt_end_with_new_line_special_case) {
|
||||
if (swap_string.len) swap_string.len -= 1;
|
||||
selected_string = Concat(scratch, selected_string, u"\n");
|
||||
}
|
||||
replacement_string = Concat(scratch, selected_string, swap_string);
|
||||
} else {
|
||||
bool ends_with_new_line = swap_string.len == 0 || (swap_string.len && swap_string[swap_string.len - 1] == u'\n');
|
||||
bool doesnt_end_with_new_line_special_case = !ends_with_new_line;
|
||||
if (doesnt_end_with_new_line_special_case) {
|
||||
if (selected_string.len) selected_string.len -= 1;
|
||||
swap_string = Concat(scratch, swap_string, u"\n");
|
||||
}
|
||||
replacement_string = Concat(scratch, swap_string, selected_string);
|
||||
}
|
||||
}
|
||||
|
||||
AddEdit(&edits, total_range, replacement_string);
|
||||
}
|
||||
EndEdit(buffer, &edits, &view->carets, EndEdit_KillSelection, EndEdit_SkipFixingCaretsIWantToDoThatMyself);
|
||||
|
||||
for (Int i = 0; i < saved_xy.len; i += 1) {
|
||||
Caret &caret = view->carets[i];
|
||||
XYPair &xypair = saved_xy[i];
|
||||
@@ -442,9 +527,10 @@ void MoveCaretsLine(View *view, int direction) {
|
||||
xypair.back.line += line_offset;
|
||||
Int front = XYToPos(buffer, xypair.front);
|
||||
Int back = XYToPos(buffer, xypair.back);
|
||||
|
||||
caret = MakeCaret(front, back);
|
||||
}
|
||||
MergeCarets(buffer, &view->carets);
|
||||
IF_DEBUG(AssertRanges(view->carets));
|
||||
}
|
||||
|
||||
void CreateCursorVertical(View *view, int direction) {
|
||||
@@ -453,9 +539,9 @@ void CreateCursorVertical(View *view, int direction) {
|
||||
|
||||
Int line_offset = direction == DIR_UP ? -1 : 1;
|
||||
|
||||
Scratch scratch;
|
||||
Scratch scratch;
|
||||
Array<Caret> arr = {scratch};
|
||||
For(view->carets) {
|
||||
For (view->carets) {
|
||||
if (PosToLine(buffer, it.range.min) == PosToLine(buffer, it.range.max)) {
|
||||
Int f = OffsetByLine(buffer, GetFront(it), line_offset);
|
||||
Int b = OffsetByLine(buffer, GetBack(it), line_offset);
|
||||
@@ -486,7 +572,7 @@ void Delete(View *view, int direction, bool ctrl = false) {
|
||||
// Delete indent in multiple of IndentSize
|
||||
Range indent_range = GetIndentRangeAtPos(buffer, it.range.min);
|
||||
if (ctrl == false && it.range.min > indent_range.min && it.range.max <= indent_range.max) {
|
||||
Int offset = it.range.min - indent_range.min;
|
||||
Int offset = it.range.min - indent_range.min;
|
||||
Int to_delete = (offset % (IndentSize));
|
||||
if (to_delete == 0) to_delete = IndentSize;
|
||||
to_delete = Clamp(to_delete, (Int)1, IndentSize);
|
||||
@@ -504,7 +590,7 @@ void Delete(View *view, int direction, bool ctrl = false) {
|
||||
|
||||
MergeCarets(buffer, &view->carets);
|
||||
For(view->carets) AddEdit(&edits, it.range, {});
|
||||
EndEdit(buffer, &edits, &view->carets, true);
|
||||
EndEdit(buffer, &edits, &view->carets, EndEdit_KillSelection);
|
||||
}
|
||||
|
||||
void EncloseSpace(View *view) {
|
||||
@@ -515,34 +601,6 @@ void EncloseSpace(View *view) {
|
||||
}
|
||||
}
|
||||
|
||||
Array<Range> GetSelectedLinesSorted(Allocator allocator, View *view) {
|
||||
Scratch scratch(allocator);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Array<Caret> caret_copy = TightCopy(scratch, view->carets);
|
||||
Array<Caret> temp = TightCopy(scratch, view->carets);
|
||||
if (view->carets.len > 1) MergeSort(view->carets.len, caret_copy.data, temp.data);
|
||||
|
||||
Array<Range> result = {allocator};
|
||||
For(caret_copy) {
|
||||
Int min_line = PosToLine(buffer, it.range.min);
|
||||
Int max_line = PosToLine(buffer, it.range.max);
|
||||
Range line_range = {min_line, max_line + 1};
|
||||
|
||||
if (result.len == 0) {
|
||||
Add(&result, line_range);
|
||||
continue;
|
||||
}
|
||||
|
||||
Range *last = GetLast(result);
|
||||
if (AreOverlapping(*last, line_range)) {
|
||||
last->max = Max(last->max, line_range.max);
|
||||
} else {
|
||||
Add(&result, line_range);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void IndentSelectedLines(View *view, bool shift = false) {
|
||||
Scratch scratch;
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
@@ -550,27 +608,59 @@ void IndentSelectedLines(View *view, bool shift = false) {
|
||||
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
|
||||
MergeCarets(buffer, &view->carets);
|
||||
|
||||
char16_t indent_char = GetIndentChar();
|
||||
String16 indent_string = GetIndentString(scratch, IndentSize);
|
||||
Array<Range> line_ranges_to_indent = GetSelectedLinesSorted(scratch, view);
|
||||
For(line_ranges_to_indent) {
|
||||
// :PreserveXYCarets - maybe make it as one of the strategies of adjusting carets?
|
||||
struct XYPair { XY front; XY back; };
|
||||
Array<XYPair> saved_xy = {scratch};
|
||||
For (view->carets) Add(&saved_xy, {PosToXY(buffer, GetFront(it)), PosToXY(buffer, GetBack(it))});
|
||||
|
||||
Array<Int> not_allowed_to_be_skipped_list = {scratch};
|
||||
For (view->carets) {
|
||||
if (GetSize(it.range) == 0) {
|
||||
Add(¬_allowed_to_be_skipped_list, PosToLine(buffer, it.range.min));
|
||||
}
|
||||
}
|
||||
|
||||
Array<Range> line_ranges_to_indent = GetSelectedLinesSortedExclusive(scratch, view);
|
||||
For (line_ranges_to_indent) {
|
||||
for (Int i = it.min; i < it.max; i += 1) {
|
||||
Range pos_range_of_line = GetLineRange(buffer, i);
|
||||
String16 string = GetString(buffer, pos_range_of_line);
|
||||
String16 without_whitespace = GetString(buffer, GetLineRangeWithoutNL(buffer, i));
|
||||
if (without_whitespace.len == 0 && !Contains(not_allowed_to_be_skipped_list, i)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String16 indent_string = u" ";
|
||||
indent_string.len = IndentSize;
|
||||
if (!shift) {
|
||||
AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min}, indent_string);
|
||||
ForItem (xy, saved_xy) {
|
||||
if (xy.front.y == i) xy.front.x += indent_string.len;
|
||||
if (xy.back.y == i) xy.back.x += indent_string.len;
|
||||
}
|
||||
} else {
|
||||
String16 string = GetString(buffer, pos_range_of_line);
|
||||
Int whitespace_len = 0;
|
||||
for (Int i = 0; i < IndentSize && i < string.len && string.data[i] == indent_char; i += 1) {
|
||||
for (Int ii = 0; ii < IndentSize && ii < string.len && string.data[ii] == u' '; ii += 1) {
|
||||
whitespace_len += 1;
|
||||
}
|
||||
ForItem (xy, saved_xy) {
|
||||
if (xy.front.y == i) xy.front.x -= whitespace_len;
|
||||
if (xy.back.y == i) xy.back.x -= whitespace_len;
|
||||
}
|
||||
|
||||
AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min + whitespace_len}, u"");
|
||||
}
|
||||
}
|
||||
}
|
||||
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
|
||||
EndEdit(buffer, &edits, &view->carets, !EndEdit_KillSelection);
|
||||
|
||||
for (Int i = 0; i < saved_xy.len; i += 1) {
|
||||
Caret &caret = view->carets[i];
|
||||
XYPair &xypair = saved_xy[i];
|
||||
Int front = XYToPos(buffer, xypair.front);
|
||||
Int back = XYToPos(buffer, xypair.back);
|
||||
caret = MakeCaret(front, back);
|
||||
}
|
||||
view->update_scroll = false;
|
||||
}
|
||||
|
||||
@@ -607,7 +697,7 @@ Array<Edit> ReplaceEx(Allocator scratch, View *view, String16 string) {
|
||||
For(view->carets) {
|
||||
AddEdit(&edits, it.range, string);
|
||||
}
|
||||
EndEdit(buffer, &edits, &view->carets, KILL_SELECTION);
|
||||
EndEdit(buffer, &edits, &view->carets, EndEdit_KillSelection);
|
||||
return edits;
|
||||
}
|
||||
|
||||
@@ -636,8 +726,9 @@ void DuplicateLine(View *view, int direction) {
|
||||
Int pos = direction == DIR_UP ? range.min : range.max;
|
||||
AddEdit(&edits, MakeRange(pos), string);
|
||||
}
|
||||
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
|
||||
EndEdit(buffer, &edits, &view->carets, !EndEdit_KillSelection);
|
||||
|
||||
// Move carets now in the duplicate direction
|
||||
Int coef = direction == DIR_UP ? -1 : 1;
|
||||
for (Int i = 0; i < edits.len; i += 1) {
|
||||
Caret *caret = view->carets.data + i;
|
||||
484
src/window.cpp
Normal file
484
src/window.cpp
Normal file
@@ -0,0 +1,484 @@
|
||||
WindowID AllocWindowID(Window *window) {
|
||||
return {WindowIDs.id++, window};
|
||||
}
|
||||
|
||||
Window *CreateWind() {
|
||||
Allocator allocator = GetSystemAllocator();
|
||||
Window *w = AllocType(allocator, Window);
|
||||
w->font = &PrimaryFont;
|
||||
w->visible = true;
|
||||
w->primary = true;
|
||||
w->draw_scrollbar = true;
|
||||
w->draw_line_numbers = true;
|
||||
w->draw_line_highlight = true;
|
||||
w->jump_history = true;
|
||||
w->id = AllocWindowID(w);
|
||||
w->weight = 1.0;
|
||||
Add(&Windows, w);
|
||||
return w;
|
||||
}
|
||||
|
||||
Window *GetWindow(WindowID id, Window *default_window = Windows[0]) {
|
||||
For(Windows) {
|
||||
if (it->id == id) return it;
|
||||
}
|
||||
return default_window;
|
||||
}
|
||||
|
||||
void Close(WindowID id) {
|
||||
Window *window = GetWindow(id);
|
||||
if (window->id.id == 0) {
|
||||
return;
|
||||
}
|
||||
window->close = true;
|
||||
RunGCThisFrame = true;
|
||||
}
|
||||
|
||||
Window *FindWindow(ViewID view_id, Window *default_window = NULL) {
|
||||
For(Windows) {
|
||||
if (it->active_view == view_id) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return default_window;
|
||||
}
|
||||
|
||||
Window *FindWindow(String buffer_name, Window *default_window = NULL) {
|
||||
For(Windows) {
|
||||
View *it_view = GetView(it->active_view, NULL);
|
||||
if (it_view) {
|
||||
Buffer *it_buffer = GetBuffer(it_view->active_buffer, NULL);
|
||||
Assert(it_buffer); // Probably we are missing something when GCing / closing
|
||||
if (it_buffer && it_buffer->name == buffer_name) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
}
|
||||
return default_window;
|
||||
}
|
||||
|
||||
Window *FindWindow(BufferID buffer_id) {
|
||||
For (Windows) {
|
||||
View *view = GetView(it->active_view, Views[0]);
|
||||
if (view->active_buffer == buffer_id) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Window *GetActiveWind() {
|
||||
return GetWindow(ActiveWindowID);
|
||||
}
|
||||
|
||||
bool IsDocumentSelectionValid() {
|
||||
if (DocumentSelected.id == -1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsScrollbarSelectionValid() {
|
||||
if (ScrollbarSelected.id == -1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
Array<Window *> GetWindowZOrder(Allocator allocator) {
|
||||
Array<Window *> order = {allocator};
|
||||
For(Windows) if (it->z == 2) Add(&order, it);
|
||||
For(Windows) if (it->z == 1) Add(&order, it);
|
||||
For(Windows) if (it->z == 0) Add(&order, it);
|
||||
return order;
|
||||
}
|
||||
|
||||
Int GetExpandingBarSize(Window *window) {
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
float result = (float)buffer->line_starts.len * window->font->line_spacing;
|
||||
return (Int)result;
|
||||
}
|
||||
|
||||
View *WindowOpenBufferView(Window *new_parent_window, String name) {
|
||||
View *view = GetView(name, NULL);
|
||||
if (!view) {
|
||||
View *result = OpenBufferView(name);
|
||||
new_parent_window->active_view = result->id;
|
||||
return result;
|
||||
}
|
||||
|
||||
Window *window = FindWindow(view->id);
|
||||
if (!window) {
|
||||
new_parent_window->active_view = view->id;
|
||||
return view;
|
||||
}
|
||||
if (window == new_parent_window) {
|
||||
return view;
|
||||
}
|
||||
|
||||
Assert(window->active_view.id == view->id.id);
|
||||
View *result = OpenBufferView(name);
|
||||
new_parent_window->active_view = result->id;
|
||||
return result;
|
||||
}
|
||||
|
||||
void CalcNiceties(Window *n) {
|
||||
float scrollbar_size = (10.f * DPIScale);
|
||||
float line_numbers_size = (float)n->font->char_spacing * 10.f;
|
||||
if (DrawScrollbar && n->draw_scrollbar) n->scrollbar_rect = CutRight(&n->document_rect, (Int)scrollbar_size);
|
||||
if (DrawLineNumbers && n->draw_line_numbers) n->line_numbers_rect = CutLeft(&n->document_rect, (Int)line_numbers_size);
|
||||
}
|
||||
|
||||
double WindowCalcEvenResizerValue(Int screen_size_x, Int *out_count = NULL) {
|
||||
double w = 0;
|
||||
Int c = 0;
|
||||
ForItem(n, Windows) {
|
||||
if (n->primary) {
|
||||
w += n->weight;
|
||||
c += 1;
|
||||
}
|
||||
}
|
||||
if (out_count) *out_count = c;
|
||||
return (double)screen_size_x / w;
|
||||
}
|
||||
|
||||
Window *GetOverlappingWindow(Vec2I p, Window *default_window = NULL) {
|
||||
For(Windows) {
|
||||
if (AreOverlapping(p, it->total_rect)) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return default_window;
|
||||
}
|
||||
|
||||
Vec2I GetSideOfWindow(Window *window, int direction) {
|
||||
Vec2I p = {};
|
||||
Rect2I rect = window->total_rect;
|
||||
float resizer_size = (float)window->font->char_spacing*0.5f; // @check_codebase_when_changing
|
||||
if (direction == DIR_LEFT) {
|
||||
p.x = rect.min.x - (Int)(resizer_size + window->font->char_spacing);
|
||||
p.y = rect.min.y + (rect.max.y / 2);
|
||||
} else if (direction == DIR_RIGHT) {
|
||||
p.x = rect.max.x + (Int)(resizer_size + window->font->char_spacing);
|
||||
p.y = rect.min.y + (rect.max.y / 2);
|
||||
} else if (direction == DIR_UP) {
|
||||
p.x = rect.min.x + (rect.max.x / 2);
|
||||
p.y = rect.min.y - (Int)(resizer_size + window->font->line_spacing);
|
||||
} else {
|
||||
Assert(direction == DIR_DOWN);
|
||||
p.x = rect.min.x + (rect.max.x / 2);
|
||||
p.y = rect.max.y + (Int)(resizer_size + window->font->line_spacing);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
Window *SwitchWindow(int direction) {
|
||||
Window *window = GetWindow(ActiveWindowID);
|
||||
Vec2I p = GetSideOfWindow(window, direction);
|
||||
Window *result = GetOverlappingWindow(p, window);
|
||||
return result;
|
||||
}
|
||||
|
||||
XY ScreenSpaceToXY(Window *window, View *view, Vec2I mouse) {
|
||||
Vec2I mworld = mouse - window->document_rect.min + view->scroll;
|
||||
double px = (double)mworld.x / (double)window->font->char_spacing;
|
||||
double py = (double)mworld.y / (double)window->font->line_spacing;
|
||||
XY xy = {(Int)round(px), (Int)floor(py)};
|
||||
return xy;
|
||||
}
|
||||
|
||||
Int ScreenSpaceToBufferPos(Window *window, View *view, Buffer *buffer, Vec2I mouse) {
|
||||
XY xy = ScreenSpaceToXY(window, view, mouse);
|
||||
Int result = XYToPosWithoutNL(buffer, xy);
|
||||
return result;
|
||||
}
|
||||
|
||||
Int ScreenSpaceToBufferPosErrorOutOfBounds(Window *window, View *view, Buffer *buffer, Vec2I mouse) {
|
||||
XY xy = ScreenSpaceToXY(window, view, mouse);
|
||||
Int result = XYToPosErrorOutOfBounds(buffer, xy);
|
||||
return result;
|
||||
}
|
||||
|
||||
GotoCrumb PopCrumb(Array<GotoCrumb> *cr) {
|
||||
for (; cr->len;) {
|
||||
GotoCrumb c = Pop(cr);
|
||||
View *view = GetView(c.view_id, NULL);
|
||||
if (view) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
View *GetLastValidView(Window *window) {
|
||||
For (IterateInReverse(&window->goto_redo)) {
|
||||
if (it.view_id == window->active_view) continue;
|
||||
View *view = GetView(it.view_id, NULL);
|
||||
if (view) return view;
|
||||
}
|
||||
For (IterateInReverse(&window->goto_history)) {
|
||||
if (it.view_id == window->active_view) continue;
|
||||
View *view = GetView(it.view_id, NULL);
|
||||
if (view) return view;
|
||||
}
|
||||
return Views[0];
|
||||
}
|
||||
|
||||
View *JumpToLastValidView(Window *window) {
|
||||
View *view = GetLastValidView(window);
|
||||
window->active_view = view->id;
|
||||
return view;
|
||||
}
|
||||
|
||||
void JumpBack(Window *window) {
|
||||
if (window->jump_history == false) return;
|
||||
if (window->goto_history.len <= 0) return;
|
||||
BSet set = GetBSet(window);
|
||||
Add(&window->goto_redo, {set.view->id, set.view->carets[0], GetTimeSeconds()});
|
||||
|
||||
GotoCrumb c = PopCrumb(&window->goto_history);
|
||||
window->active_view = c.view_id;
|
||||
View *view = GetView(c.view_id);
|
||||
view->carets[0] = c.caret;
|
||||
CenterView(window->id);
|
||||
|
||||
if (window->goto_history.len) {
|
||||
GotoCrumb *next = GetLast(window->goto_history);
|
||||
if (c.view_id == next->view_id && ((c.time - next->time) <= JumpHistoryMergeTime)) {
|
||||
JumpBack(window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JumpForward(Window *window) {
|
||||
if (window->jump_history == false) return;
|
||||
if (window->goto_redo.len <= 0) return;
|
||||
BSet set = GetBSet(window);
|
||||
Add(&window->goto_history, {set.view->id, set.view->carets[0], GetTimeSeconds()});
|
||||
|
||||
GotoCrumb c = PopCrumb(&window->goto_redo);
|
||||
window->active_view = c.view_id;
|
||||
View *view = GetView(c.view_id);
|
||||
view->carets[0] = c.caret;
|
||||
CenterView(window->id);
|
||||
|
||||
if (window->goto_redo.len) {
|
||||
GotoCrumb *next = GetLast(window->goto_redo);
|
||||
if ((c.view_id == next->view_id) && ((next->time - c.time) <= JumpHistoryMergeTime)) {
|
||||
JumpForward(window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GotoNextInList(Window *window, Int line_offset = 1) {
|
||||
Assert(line_offset == 1 || line_offset == -1);
|
||||
View *active_view = GetView(window->active_view);
|
||||
|
||||
View *view_goto = GetView(window->active_goto_list);
|
||||
window->active_view = view_goto->id;
|
||||
|
||||
Buffer *buffer_goto = GetBuffer(view_goto->active_buffer);
|
||||
int64_t pos = window->goto_list_pos;
|
||||
Int start_line = 0;
|
||||
if (pos < 0) {
|
||||
start_line = line_offset > 0 ? 0 : buffer_goto->line_starts.len - 1;
|
||||
} else {
|
||||
Int line = PosToLine(buffer_goto, pos);
|
||||
start_line = line + line_offset;
|
||||
}
|
||||
|
||||
bool opened = false;
|
||||
for (Int i = start_line; i >= 0 && i < buffer_goto->line_starts.len; i += line_offset) {
|
||||
Range line_range = GetLineRangeWithoutNL(buffer_goto, i);
|
||||
String16 string_line = GetString(buffer_goto, line_range);
|
||||
|
||||
{
|
||||
Int idx = 0;
|
||||
String16 delim = u"||>";
|
||||
if (Seek(string_line, delim, &idx, SeekFlag_None)) {
|
||||
string_line = Skip(string_line, idx + delim.len);
|
||||
}
|
||||
}
|
||||
|
||||
view_goto->carets[0] = MakeCaret(line_range.min);
|
||||
window->goto_list_pos = line_range.min;
|
||||
string_line = Trim(string_line);
|
||||
|
||||
MergeCarets(buffer_goto, &view_goto->carets);
|
||||
IF_DEBUG(AssertRanges(view_goto->carets));
|
||||
if (string_line.len == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Range before_jump_range = active_view->carets[0].range;
|
||||
|
||||
BSet set = Open(string_line, ResolveOpenMeta_DontError | ResolveOpenMeta_DontExec);
|
||||
if (set.window == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (set.view == active_view) {
|
||||
if (AreOverlapping(set.view->carets[0].range, before_jump_range)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
opened = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!opened) window->active_view = active_view->id;
|
||||
}
|
||||
|
||||
void UpdateScroll(Window *window, bool update_caret_scrolling) {
|
||||
ProfileFunction();
|
||||
BSet set = GetBSet(window);
|
||||
|
||||
// Scrolling with caret
|
||||
if (update_caret_scrolling) {
|
||||
Caret c = set.view->carets[0];
|
||||
Int front = GetFront(c);
|
||||
XY xy = PosToXY(set.buffer, front);
|
||||
|
||||
Rect2I visible = GetVisibleCells(window);
|
||||
Vec2I visible_cells = GetSize(visible);
|
||||
Vec2I visible_size = visible_cells * Vec2I{window->font->char_spacing, window->font->line_spacing};
|
||||
Vec2I rect_size = GetSize(window->document_rect);
|
||||
|
||||
if (xy.line >= visible.max.y - 2) {
|
||||
Int set_view_at_line = xy.line - (visible_cells.y - 1);
|
||||
Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y);
|
||||
set.view->scroll.y = (set_view_at_line * window->font->line_spacing) + cut_off_y;
|
||||
}
|
||||
|
||||
if (xy.line < visible.min.y + 1) {
|
||||
set.view->scroll.y = xy.line * window->font->line_spacing;
|
||||
}
|
||||
|
||||
if (xy.col >= visible.max.x - 1) {
|
||||
Int set_view_at_line = xy.col - (visible_cells.x - 1);
|
||||
Int cut_off_x = Max((Int)0, visible_size.x - rect_size.x);
|
||||
set.view->scroll.x = (set_view_at_line * window->font->char_spacing) + cut_off_x;
|
||||
}
|
||||
|
||||
if (xy.col <= visible.min.x) {
|
||||
set.view->scroll.x = xy.col * window->font->char_spacing;
|
||||
}
|
||||
}
|
||||
|
||||
// Clip scroll
|
||||
{
|
||||
Int last_line = LastLine(set.buffer);
|
||||
set.view->scroll.y = Clamp(set.view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * window->font->line_spacing));
|
||||
|
||||
// @note:
|
||||
// GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for
|
||||
// calculating this value incrementally but do we even need X scrollbar or x clipping?
|
||||
set.view->scroll.x = ClampBottom(set.view->scroll.x, (Int)0);
|
||||
}
|
||||
}
|
||||
|
||||
void CenterView(WindowID window) {
|
||||
BSet set = GetBSet(window);
|
||||
Caret c = set.view->carets[0];
|
||||
Int front = GetFront(c);
|
||||
XY xy = PosToXY(set.buffer, front);
|
||||
Vec2I size = GetSize(set.window->document_rect);
|
||||
Int y = size.y / 2 / set.window->font->line_spacing;
|
||||
set.view->scroll.x = 0;
|
||||
if (xy.line > y) {
|
||||
set.view->scroll.y = (xy.line) * set.window->font->line_spacing - (size.y / 2);
|
||||
}
|
||||
}
|
||||
|
||||
void CenterPrimaryViews() {
|
||||
For (Windows) {
|
||||
if (it->primary) {
|
||||
CenterView(it->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BSet GetBSet(Window *window) {
|
||||
BSet set = {window};
|
||||
set.view = GetView(set.window->active_view);
|
||||
set.buffer = GetBuffer(set.view->active_buffer);
|
||||
return set;
|
||||
}
|
||||
|
||||
BSet GetBSet(WindowID window_id) {
|
||||
Window *window = GetWindow(window_id);
|
||||
BSet result = GetBSet(window);
|
||||
return result;
|
||||
}
|
||||
|
||||
String GetDirectory(Window *window) {
|
||||
BSet set = GetBSet(window->id);
|
||||
return GetDirectory(set.buffer);
|
||||
}
|
||||
|
||||
String GetPrimaryDirectory() {
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
return GetDirectory(main.buffer);
|
||||
}
|
||||
|
||||
struct IsOnScreenResult {
|
||||
bool caret_on_screen;
|
||||
Int offset_from_top;
|
||||
};
|
||||
|
||||
IsOnScreenResult IsMainCaretOnScreen(Window *window) {
|
||||
BSet set = GetBSet(window);
|
||||
Int front = GetFront(set.view->carets[0]);
|
||||
XY front_xy = PosToXY(set.buffer, front);
|
||||
Int caret_pixel_size = front_xy.y * set.window->font->line_spacing;
|
||||
Vec2I doc_pixel_size = GetSize(set.window->document_rect);
|
||||
|
||||
Int ystart = set.view->scroll.y;
|
||||
Int yend = ystart + doc_pixel_size.y;
|
||||
bool caret_on_screen = caret_pixel_size >= ystart && caret_pixel_size < yend;
|
||||
Int offset_from_top = caret_pixel_size - ystart;
|
||||
return {caret_on_screen, offset_from_top};
|
||||
}
|
||||
|
||||
bool SetStoredOffsetFromTop(Window *window, IsOnScreenResult res) {
|
||||
if (res.caret_on_screen) {
|
||||
BSet set = GetBSet(window);
|
||||
Int front = GetFront(set.view->carets[0]);
|
||||
XY front_xy = PosToXY(set.buffer, front);
|
||||
Int caret_pixel_size = front_xy.y * set.window->font->line_spacing;
|
||||
set.view->scroll.y = caret_pixel_size - res.offset_from_top;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void MoveCursorByPageSize(Window *window, int direction, bool shift = false) {
|
||||
Assert(direction == DIR_UP || direction == DIR_DOWN);
|
||||
BSet set = GetBSet(window);
|
||||
IsOnScreenResult is_on_screen_res = IsMainCaretOnScreen(window);
|
||||
Rect2I visible_cells_rect = GetVisibleCells(window);
|
||||
Int y = GetSize(visible_cells_rect).y - 2;
|
||||
if (direction == DIR_UP) {
|
||||
y = -y;
|
||||
}
|
||||
|
||||
For (set.view->carets) {
|
||||
XY xy = PosToXY(set.buffer, GetFront(it));
|
||||
xy.col = 0;
|
||||
if (direction == DIR_DOWN && xy.line == set.buffer->line_starts.len - 1) {
|
||||
Range line_range = GetLineRange(set.buffer, xy.line);
|
||||
xy.col = line_range.max - line_range.min;
|
||||
}
|
||||
xy.line += y;
|
||||
|
||||
Int pos = XYToPosWithoutNL(set.buffer, xy);
|
||||
if (shift) {
|
||||
it = SetFront(it, pos);
|
||||
} else {
|
||||
it = MakeCaret(pos);
|
||||
}
|
||||
}
|
||||
|
||||
IsOnScreenResult r = IsMainCaretOnScreen(window);
|
||||
if (!r.caret_on_screen) {
|
||||
SetStoredOffsetFromTop(window, is_on_screen_res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user