teditor  1.8.0@@fee5e94
Terminal based editor written in C++
editor.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "buffer.h"
4 #include <stdarg.h>
5 #include <string>
6 #include <vector>
7 #include "logger.h"
8 #include "buffer.h"
9 #include "keys.h"
10 #include <unordered_map>
11 #include "cell_buffer.h"
12 #include "cmd_msg_bar.h"
13 #include "window.h"
14 #include "file_utils.h"
15 #include <sys/time.h>
16 
17 namespace teditor {
18 
19 class KeyCmdMap;
20 
21 typedef std::unordered_map<std::string, std::string> OptionMap;
22 
23 class Editor {
24 public:
25  Editor(const std::vector<FileInfo>& _files);
26  ~Editor();
27  void setTitle(const std::string& ti);
28  Buffer& getBuff() { return getWindow().getBuff(); }
29  const Buffer& getBuff() const { return getWindow().getBuff(); }
30  Buffer& getBuff(const std::string& name, bool noUndoRedo, bool& newOne);
31  Window& getWindow() { return windows.getWindow(); }
32  const Window& getWindow() const { return windows.getWindow(); }
33  Window& getCmBarWindow() { return *windows[0]; }
34  CmdMsgBar& getCmBar() { return *cmBar; }
36  std::string prompt(const std::string& msg, KeyCmdMap* kcMap=nullptr,
37  Choices* choices=nullptr,const std::string& defVal=std::string());
38  bool promptYesNo(const std::string& msg);
39  std::string promptEnum(const std::string& msg, OptionMap& opts);
40  void load(const std::string& file, int line);
41  void runCmd(const std::string& cmd);
42  const std::string clipboard() const;
43  void setClipboard(const std::string& in);
44 
45  int sendChar(int x, int y, const AttrColor& fg, const AttrColor& bg, char c);
46  int sendString(int x, int y, const AttrColor& fg, const AttrColor& bg,
47  const char* str, int len);
48  int sendStringf(int x, int y, const AttrColor& fg, const AttrColor& bg,
49  const char* fmt, ...);
50  void setColors(AttrColor fg, AttrColor bg);
51  void writeCursor(int x, int y);
52  void writeChar(uint32_t c, int x, int y);
53  void run();
54  void requestQuitEventLoop() { quitEventLoop = true; }
55  void requestQuitPromptLoop() { quitPromptLoop = true; }
56  void requestCancelPromptLoop() { cancelPromptLoop = true; }
59  void switchToBuff(const std::string& name);
60  void killCurrBuff();
61  void killOtherBuffs();
62  int currBuffId() const { return getWindow().currBuffId(); }
63  int buffSize() const { return (int)buffs.size(); }
64  void createScratchBuff(bool switchToIt=false);
65  void createReadOnlyBuff(const std::string& name, const std::string& contents,
66  bool switchToIt=false);
67  void selectCmBar() { cmdMsgBarActive = true; }
68  void unselectCmBar() { cmdMsgBarActive = false; }
69  Strings fileHistoryToString() const { return fileshist.get(); }
70  Strings buffNamesToString() const { return buffs.namesList(); }
71  void saveBuffer(Buffer& buf);
72  key_t getKey() const;
73  bool splitVertically() { return windows.splitVertically(); }
74  void clearAllWindows() { return windows.clearAll(); }
75  void incrementCurrWin() { windows.incrementCurrWin(); }
76  void refresh() { draw(); render(); }
77 
78 private:
79  CellBuffer backbuff, frontbuff;
80  AttrColor lastfg, lastbg;
81  CmdMsgBar* cmBar;
82  Buffers buffs, cmBarArr;
83  Windows windows;
84  bool quitEventLoop, quitPromptLoop, cancelPromptLoop, cmdMsgBarActive;
85  ColorMap defcMap;
86  KeyCmdMap ynMap;
87  std::vector<FileInfo> files;
88  struct timeval timeout;
89  FilesHist fileshist;
90 
95  void render();
96  int pollEvent();
99  void writeLiteral(const char* fmt, ...);
100  void clearScreen();
101  void resize();
102  void clearBackBuff();
103  void sendCell(int x, int y, const Cell& c) { backbuff.at(x, y) = c; }
104  void writef(const char* fmt, ...);
105  void draw();
106  void loadFiles();
107  void bufResize();
108  const AttrColor& getColor(const std::string& name) const;
109  int cmBarHeight() const;
110  void deleteBuffer(int idx);
111  void setCurrBuff(int i) { getWindow().setCurrBuff(i); }
112  void checkForModifiedBuffer(Buffer* mlb);
113 };
114 
115 }; // end namespace teditor
teditor::Cell
Definition: cell_buffer.h:13
teditor::Window::attachBuffs
void attachBuffs(Buffers *bs)
attach buffers list to this window. This list should be shared with all the other windows being displ...
Definition: window.cpp:13
teditor::Choices::setChoiceIdx
void setChoiceIdx(int idx)
Definition: cmd_msg_bar.h:27
teditor::OptionMap
std::unordered_map< std::string, std::string > OptionMap
Definition: editor.h:19
teditor::History::store
void store() const
Definition: utils.cpp:276
teditor::CmdMsgBar::getOptLoc
int getOptLoc() const
Definition: cmd_msg_bar.h:72
teditor::Buffer::load
virtual void load(const std::string &file, int line=0)
Definition: buffer.cpp:333
teditor::Window::decrementCurrBuff
void decrementCurrBuff()
Definition: window.cpp:23
teditor::TrieStatus
TrieStatus
Definition: key_cmd_map.h:7
teditor::Editor::splitVertically
bool splitVertically()
Definition: editor.h:73
teditor::Editor::getWindow
Window & getWindow()
Definition: editor.h:31
teditor::Editor::fileHistoryToString
Strings fileHistoryToString() const
Definition: editor.h:69
teditor::Buffer::isRO
bool isRO() const
Definition: buffer.h:203
teditor::Editor::getCmBar
CmdMsgBar & getCmBar()
Definition: editor.h:34
teditor::KeyCmdMap::traverse
TrieStatus traverse(const std::string &currKey)
Definition: key_cmd_map.cpp:18
teditor::Editor::getKey
key_t getKey() const
Definition: editor.cpp:509
logger.h
teditor::CellBuffer::clear
void clear(AttrColor fg, AttrColor bg)
Definition: cell_buffer.cpp:40
keys.h
teditor::Editor::incrementCurrBuff
void incrementCurrBuff()
Definition: editor.h:57
teditor::Editor::writeChar
void writeChar(uint32_t c, int x, int y)
Definition: editor.cpp:359
teditor::Windows::incrementCurrWin
void incrementCurrWin()
Definition: window.cpp:82
teditor::Windows::clearAll
void clearAll()
Definition: window.cpp:150
teditor::Editor::requestCancelPromptLoop
void requestCancelPromptLoop()
Definition: editor.h:56
teditor::Editor::setClipboard
void setClipboard(const std::string &in)
Definition: editor.cpp:84
teditor::Editor::createReadOnlyBuff
void createReadOnlyBuff(const std::string &name, const std::string &contents, bool switchToIt=false)
Definition: editor.cpp:129
teditor::Event_Key
@ Event_Key
Definition: terminal.h:32
key_cmd_map.h
teditor::Editor::decrementCurrBuff
void decrementCurrBuff()
Definition: editor.h:58
teditor::Terminal::mk
MetaKey mk
Definition: terminal.h:48
teditor::Editor::requestQuitEventLoop
void requestQuitEventLoop()
Definition: editor.h:54
teditor::Cell::ch
Chr ch
Definition: cell_buffer.h:14
teditor::Editor::clipboard
const std::string clipboard() const
Definition: editor.cpp:68
teditor::ColorMap
Definition: colors.h:87
teditor::Chr
uint32_t Chr
Definition: cell_buffer.h:10
CMBAR_MSG
#define CMBAR_MSG(ed, fmt,...)
Definition: logger.h:52
teditor::Editor::sendStringf
int sendStringf(int x, int y, const AttrColor &fg, const AttrColor &bg, const char *fmt,...)
Definition: editor.cpp:302
teditor::Editor::buffSize
int buffSize() const
Definition: editor.h:63
teditor::Editor::getWindow
const Window & getWindow() const
Definition: editor.h:32
teditor::History::get
const Strings & get() const
Definition: utils.h:136
teditor::Editor::clearAllWindows
void clearAllWindows()
Definition: editor.h:74
teditor::KeyCmdMap
Definition: key_cmd_map.h:19
teditor::Terminal::puts
void puts(const char *data, size_t len)
append data at the end of the current buffer contents
Definition: terminal.cpp:62
teditor::CmdMsgBar
Definition: cmd_msg_bar.h:50
teditor::Buffer::getPoint
const Point & getPoint() const
Definition: buffer.h:165
teditor::CmdMsgBar::clearChoices
void clearChoices()
Definition: cmd_msg_bar.cpp:141
teditor::Editor::buffNamesToString
Strings buffNamesToString() const
Definition: editor.h:70
teditor::Pos2d::y
T y
Definition: pos2d.h:16
teditor::KeyCmdMap::resetTraversal
void resetTraversal()
Definition: key_cmd_map.h:26
teditor::Editor::currBuffId
int currBuffId() const
Definition: editor.h:62
teditor::Choices
Definition: cmd_msg_bar.h:15
teditor::Editor::incrementCurrWin
void incrementCurrWin()
Definition: editor.h:75
teditor::Buffers
Definition: buffer.h:355
teditor::AttrColor
Definition: colors.h:56
teditor::Editor::promptYesNo
bool promptYesNo(const std::string &msg)
Definition: editor.cpp:390
teditor::Strings
std::vector< std::string > Strings
Definition: utils.h:42
teditor::Editor::promptEnum
std::string promptEnum(const std::string &msg, OptionMap &opts)
Definition: editor.cpp:395
teditor::Buffer::isModified
bool isModified() const
Definition: buffer.h:204
teditor::Editor::switchToBuff
void switchToBuff(const std::string &name)
Definition: editor.cpp:169
teditor::Editor::sendChar
int sendChar(int x, int y, const AttrColor &fg, const AttrColor &bg, char c)
Definition: editor.cpp:295
command.h
teditor::AttrColor::isUnderline
bool isUnderline() const
Definition: colors.h:70
teditor::PromptYesNoKeys::All
static std::vector< KeyCmdPair > All
Definition: editor.cpp:26
utils.h
teditor::Func_Italic
@ Func_Italic
Definition: terminal.h:21
teditor::Utf8::char2unicode
int char2unicode(uint32_t *out, const char *c)
Definition: utf8.cpp:23
teditor::Terminal::UndefinedSequence
static const int UndefinedSequence
Definition: terminal.h:95
teditor::Window::currBuffId
int currBuffId() const
Definition: window.h:55
teditor::TS_NULL
@ TS_NULL
Definition: key_cmd_map.h:8
teditor::Windows::splitVertically
bool splitVertically()
split the window into 2 vertical parts
Definition: window.cpp:129
teditor::Editor::getBuff
const Buffer & getBuff() const
Definition: editor.h:29
teditor::Editor::unselectCmBar
void unselectCmBar()
Definition: editor.h:68
teditor::key_t
uint32_t key_t
Definition: keys.h:9
teditor::CmdMsgBar::clear
void clear() override
Definition: cmd_msg_bar.cpp:134
teditor::dos2unix
void dos2unix(std::string &in)
Definition: utils.cpp:249
teditor::CmdMsgBar::setMinLoc
void setMinLoc(int loc)
Definition: cmd_msg_bar.h:61
teditor::CellBuffer::resize
void resize(int w, int h)
Definition: cell_buffer.cpp:47
teditor::Editor::~Editor
~Editor()
Definition: editor.cpp:56
teditor::Window
Definition: window.h:15
teditor::CmdMsgBar::insert
void insert(char c) override
Definition: cmd_msg_bar.cpp:108
teditor::KeyCmdMap::getCmd
const std::string getCmd() const
Definition: key_cmd_map.cpp:26
teditor::Windows::getWindow
Window & getWindow()
Definition: window.h:79
cmd_msg_bar.h
teditor::Buffer::getKeyCmdMap
KeyCmdMap & getKeyCmdMap()
Definition: buffer.h:294
teditor::Buffers::push_back
Buffer * push_back(const std::string &name, bool noUndoRedo=false)
Definition: buffer.cpp:834
teditor::CmdMsgBar::usingChoices
bool usingChoices() const
Definition: cmd_msg_bar.h:66
teditor::FilesHist::add
void add(const std::string &file, int line)
Definition: file_utils.cpp:367
teditor::CellBuffer::at
const Cell & at(int x, int y) const
Definition: cell_buffer.h:32
teditor::Option::getInt
int getInt() const
Definition: option.h:44
teditor::Editor::getBuff
Buffer & getBuff()
Definition: editor.h:28
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::Terminal::flush
void flush()
Definition: terminal.cpp:71
teditor::Terminal::waitAndFill
int waitAndFill(struct timeval *timeout)
Definition: terminal.cpp:269
teditor::Editor::killOtherBuffs
void killOtherBuffs()
Definition: editor.cpp:191
teditor::Editor::sendString
int sendString(int x, int y, const AttrColor &fg, const AttrColor &bg, const char *str, int len)
Definition: editor.cpp:282
teditor::Event_Resize
@ Event_Resize
Definition: terminal.h:33
file_utils.h
teditor::Windows::draw
void draw(Editor &ed, bool cmdMsgBarActive)
Definition: window.cpp:92
teditor::CellBuffer::w
unsigned w() const
Definition: cell_buffer.h:34
teditor::Cell::width
int width() const
Definition: cell_buffer.cpp:29
teditor::Utf8::unicode2char
int unicode2char(char *out, uint32_t c)
Definition: utf8.cpp:37
teditor::isAbs
bool isAbs(const std::string &file)
Definition: file_utils.cpp:116
teditor::getCmd
const Command & getCmd(const std::string &cmd)
Helper to return the command of interest.
Definition: command.cpp:23
teditor::Window::getBuff
Buffer & getBuff()
Definition: window.cpp:15
buffer.h
teditor::Buffer::makeReadOnly
void makeReadOnly()
Definition: buffer.cpp:345
teditor::Editor
Definition: editor.h:23
teditor::Editor::runCmd
void runCmd(const std::string &cmd)
Definition: editor.cpp:119
teditor::AttrColor::isBold
bool isBold() const
Definition: colors.h:69
teditor::Buffer::bufferName
const std::string & bufferName() const
Definition: buffer.h:200
cell_buffer.h
teditor::Editor::killCurrBuff
void killCurrBuff()
Definition: editor.cpp:180
DEBUG
#define DEBUG(fmt,...)
Definition: logger.h:73
teditor::KeyCmdMap::add
void add(char key, const std::string &cmd)
Definition: key_cmd_map.cpp:6
teditor::Windows::resize
void resize(int cmBarHt)
Definition: window.cpp:59
teditor::Func_ClearScreen
@ Func_ClearScreen
Definition: terminal.h:18
utf8.h
teditor::Windows
Definition: window.h:75
teditor::Editor::setTitle
void setTitle(const std::string &ti)
Definition: editor.cpp:63
teditor::AttrColor::isItalic
bool isItalic() const
Definition: colors.h:71
teditor::CmdMsgBar::getFinalChoice
std::string getFinalChoice() const
Definition: cmd_msg_bar.cpp:82
teditor::CellBuffer::h
unsigned h() const
Definition: cell_buffer.h:35
teditor::Editor::load
void load(const std::string &file, int line)
Definition: editor.cpp:151
teditor::Editor::refresh
void refresh()
Definition: editor.h:76
teditor::Editor::getCmBarWindow
Window & getCmBarWindow()
Definition: editor.h:33
teditor::Mode::createMode
static ModePtr createMode(const std::string &mode)
Helper to create mode object of the named mode.
Definition: core/mode.cpp:48
teditor::Editor::run
void run()
Definition: editor.cpp:212
teditor::rel2abs
std::string rel2abs(const std::string &pwd, const std::string &rel)
Definition: file_utils.cpp:118
teditor::Func_Sgr0
@ Func_Sgr0
Definition: terminal.h:19
teditor::Editor::Editor
Editor(const std::vector< FileInfo > &_files)
Definition: editor.cpp:35
teditor::CmdMsgBar::setChoices
void setChoices(Choices *ch)
Definition: cmd_msg_bar.h:64
teditor::Editor::selectCmBar
void selectCmBar()
Definition: editor.h:67
teditor::AttrColor::color
color_t color() const
Definition: colors.h:72
teditor::CellBuffer
Definition: cell_buffer.h:27
window.h
teditor::Editor::writeCursor
void writeCursor(int x, int y)
Definition: editor.cpp:355
teditor::PromptYesNoKeys
Definition: editor.cpp:25
teditor::Func_Bold
@ Func_Bold
Definition: terminal.h:22
mode.h
teditor::Editor::saveBuffer
void saveBuffer(Buffer &buf)
Definition: editor.cpp:92
teditor::Option
Class to define an option used in the editor.
Definition: option.h:14
teditor::FilesHist
Definition: file_utils.h:99
teditor::Editor::requestQuitPromptLoop
void requestQuitPromptLoop()
Definition: editor.h:55
teditor::Buffer::gotoLine
void gotoLine(int lineNum, const Point &dim)
Definition: buffer.cpp:797
ULTRA_DEBUG
#define ULTRA_DEBUG(fmt,...)
Definition: logger.h:74
teditor::Buffers::namesList
Strings namesList() const
Definition: buffer.cpp:853
teditor::Editor::setColors
void setColors(AttrColor fg, AttrColor bg)
Definition: editor.cpp:367
teditor::Buffer::insert
virtual void insert(char c)
Definition: buffer.cpp:25
teditor::Option::get
static Option & get(const std::string &n)
Definition: option.cpp:40
teditor::ColorMap::get
const AttrColor & get(const std::string &name) const
Definition: colors.cpp:67
teditor::Terminal::getInstance
static Terminal & getInstance()
Definition: terminal.cpp:57
teditor::MetaKey::getKey
key_t getKey() const
Definition: keys.h:192
option.h
teditor::Func_Underline
@ Func_Underline
Definition: terminal.h:20
teditor::Buffer::getColor
const AttrColor & getColor(const std::string &name) const
Definition: buffer.cpp:303
editor.h
teditor::Window::setCurrBuff
void setCurrBuff(int i)
Definition: window.h:56
teditor::format
std::string format(const char *fmt, va_list &vl)
Definition: utils.cpp:22
teditor::Buffer::save
virtual bool save(const std::string &fName="")
Definition: buffer.cpp:638
teditor::Editor::createScratchBuff
void createScratchBuff(bool switchToIt=false)
Definition: editor.cpp:124
terminal.h
teditor::getpwd
std::string getpwd()
Definition: file_utils.cpp:107
teditor::TS_LEAF
@ TS_LEAF
Definition: key_cmd_map.h:10
teditor::Editor::prompt
std::string prompt(const std::string &msg, KeyCmdMap *kcMap=nullptr, Choices *choices=nullptr, const std::string &defVal=std::string())
Definition: editor.cpp:413
teditor::Buffer
Definition: buffer.h:25
CMBAR
#define CMBAR(ed, fmt,...)
Definition: logger.h:49
teditor::Editor::getMessagesBuff
Buffer & getMessagesBuff()
Definition: editor.cpp:329
teditor
Definition: any.hpp:10
teditor::Window::incrementCurrBuff
void incrementCurrBuff()
Definition: window.cpp:18
teditor::Buffers::erase
void erase(int idx)
Definition: buffer.cpp:846
teditor::Buffer::getFileName
const std::string & getFileName() const
Definition: buffer.h:201