teditor  1.8.0@@fee5e94
Terminal based editor written in C++
buffer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include "colors.h"
5 #include <vector>
6 #include "utils.h"
7 #include "key_cmd_map.h"
8 #include "command.h"
9 #include "line.h"
10 #include "mode.h"
11 #include "pos2d.h"
12 #include <stack>
13 #include <vector>
14 #include <unordered_set>
15 #include "file_utils.h"
16 #include "parser/nfa.h"
17 
18 namespace teditor {
19 
20 class Editor;
21 class Window;
22 
23 
25 class Buffer {
26 public:
27  Buffer(const std::string& name="", bool noUndoRedo=false);
28  virtual ~Buffer() {}
29 
35  virtual void insert(char c);
37  virtual void insert(const std::string& str);
42  void remove(bool removeCurrent=false);
44  std::string removeAndCopy();
52  std::string removeRegion(const Point& start, const Point& end);
54  std::string killLine(bool pushToStack=true);
56  void sortRegion();
62  void keepRemoveLines(parser::NFA& regex, bool keep);
64  virtual void clear();
74  bool undo();
79  bool redo();
86  bool matchCurrentParen();
87 
89  virtual void load(const std::string& file, int line=0);
90 
92  int length() const { return (int)lines.size(); }
93 
98  Line& at(int idx) { return lines[idx]; }
99  const Line& at(int idx) const { return lines[idx]; }
106  Point buffer2screen(const Point& loc, const Point& start,
107  const Point& dim) const;
108  Point screen2buffer(const Point& loc, const Point& start,
109  const Point& dim) const;
113  char charAt(const Point& pos) const;
114 
119  virtual void draw(Editor& ed, const Window& win);
120  void drawPoint(Editor& ed, const AttrColor& bg, const Window& win);
130  void endOfLine() { longestX = cu.x = lengthOf(cu.y); }
132  void left();
134  void right();
136  void down();
138  void up();
140  void begin();
142  void end();
144  void pageDown(int ijump);
146  void pageUp(int ijump);
148  void nextPara();
150  void previousPara();
152  void nextWord();
154  void previousWord();
156  void gotoLine(int lineNum, const Point& dim);
164  bool hasPointOn(int line) const { return line == cu.y; }
165  const Point& getPoint() const { return cu; }
166  void setPoint(const Point& p) { cu = p; }
174  bool isRegionActive() const { return region != Point(-1, -1); }
176  std::string regionAsStr() const;
178  std::string regionAsStr(const Point& start, const Point& end) const;
180  void startRegion() { region = cu; }
182  void stopRegion() { region = Point(-1, -1); }
183  const Point& getRegion() const { return region; }
187  int lengthOf(int i) const { return lines[i].length(); }
188 
190  void indent();
191 
193  virtual int totalLinesNeeded(const Point& dim) const;
194 
195  virtual void lineUp(const Point& dim);
196  virtual void lineDown();
197  void lineReset() { startLine = 0; }
198  void lineEnd(const Point& start, const Point& dim);
199  virtual bool save(const std::string& fName="");
200  const std::string& bufferName() const { return buffName; }
201  const std::string& getFileName() const { return fileName; }
202  const std::string& pwd() const { return dirName; }
203  bool isRO() const { return readOnly; }
204  bool isModified() const { return modified; }
205  virtual int getMinStartLoc() const { return 0; }
206  std::string dirModeGetFileAtLine(int line);
207 
208  void reload();
209  const AttrColor& getColor(const std::string& name) const;
210  const std::string& getWord() const { return mode->word(); }
211  const std::string& modeName() const { return mode->name(); }
212  void makeReadOnly();
213  void setMode(ModePtr m) { mode = m; }
214 
215  template <typename ModeT>
216  ModeT* getMode(const std::string& name) {
217  ASSERT(modeName() == name, "getMode: expected '%s' but obtained '%s'!",
218  name.c_str(), modeName().c_str());
219  return dynamic_cast<ModeT*>(mode.get());
220  }
221 
222  Strings cmdNames() const { return mode->cmdNames(); }
223 
224 protected:
226  struct RemovedLine {
228  std::string str;
230  int num;
231  };
233  typedef std::vector<RemovedLine> RemovedLines;
234 
235 
237  enum OpType {
239  OpInsert = 0,
246  };
247 
248 
253  struct OpData {
259  std::string str;
264  }; // end class OpData
265 
266 
268  typedef std::stack<OpData> OpStack;
269 
270 
271  std::vector<Line> lines;
282  int longestX;
289 
290 
291  void insertImpl(char c);
292  void addLine() { lines.push_back(Line()); }
293  void resetBufferState(int line, const std::string& file, bool dir);
294  KeyCmdMap& getKeyCmdMap() { return mode->getKeyCmdMap(); }
295  void loadFile(const std::string& file, int line);
296  void loadDir(const std::string& dir);
297  std::string removeFrom(const Point& start, const Point& end);
298  Point matchCurrentParen(bool& isOpen);
300 
301 
306  void drawStatusBar(Editor& ed, const Window& win);
307  virtual int drawLine(int y, const std::string& line, Editor& ed, int lineNum,
308  const Window &win);
316  std::string removeChar();
318  std::string removeCurrentChar();
331  void applyInsertOp(OpData& op, bool pushToStack=true);
336  void applyDeleteOp(OpData& op);
338  void addLines(const RemovedLines& rlines);
340  void removeLines(const RemovedLines& rlines);
345  void pushNewOp(OpData& op);
347  void clearStack(OpStack& st);
350  friend class Editor;
351 };
352 
353 
355 class Buffers : public std::vector<Buffer*> {
356 public:
357  ~Buffers();
358 
360  Buffer* push_back(const std::string& name, bool noUndoRedo = false);
361  void push_back(Buffer* buf);
362 
363  void clear();
364  void erase(int idx);
365 
367  Strings namesList() const;
368 
369 
370 private:
371  std::unordered_set<std::string> buffNames;
372 
373  std::string uniquify(const std::string& name) const;
374 };
375 
376 }; // end namespace teditor
teditor::Buffer::load
virtual void load(const std::string &file, int line=0)
Definition: buffer.cpp:333
teditor::Line
Definition: line.h:11
teditor::Buffer::startRegion
void startRegion()
Definition: buffer.h:180
teditor::Buffer::insertImpl
void insertImpl(char c)
Definition: buffer.cpp:266
teditor::Buffer::OpInsert
@ OpInsert
Definition: buffer.h:239
teditor::Buffer::Buffer
Buffer(const std::string &name="", bool noUndoRedo=false)
Definition: buffer.cpp:14
teditor::Buffer::remove
void remove(bool removeCurrent=false)
main remove method
Definition: buffer.cpp:57
teditor::Buffer::RemovedLine::num
int num
Definition: buffer.h:230
teditor::Buffer::isRO
bool isRO() const
Definition: buffer.h:203
teditor::Buffer::loadFile
void loadFile(const std::string &file, int line)
Definition: buffer.cpp:358
teditor::Buffer::loadDir
void loadDir(const std::string &dir)
Definition: buffer.cpp:352
teditor::Buffer::draw
virtual void draw(Editor &ed, const Window &win)
Definition: buffer.cpp:391
teditor::Buffer::OpData::after
Point after
Definition: buffer.h:257
logger.h
teditor::Buffer::dirModeGetFileAtLine
std::string dirModeGetFileAtLine(int line)
Definition: buffer.cpp:443
teditor::Buffer::getMinStartLoc
virtual int getMinStartLoc() const
Definition: buffer.h:205
teditor::Buffer::lineDown
virtual void lineDown()
Definition: buffer.cpp:626
teditor::Buffer::applyInsertOp
void applyInsertOp(OpData &op, bool pushToStack=true)
Insert characters into the buffer.
Definition: buffer.cpp:250
teditor::Buffer::buffer2screen
Point buffer2screen(const Point &loc, const Point &start, const Point &dim) const
Definition: buffer.cpp:477
key_cmd_map.h
teditor::Line::split
Line split(int idx)
Definition: line.cpp:15
teditor::Buffer::left
void left()
Definition: buffer.cpp:670
teditor::Buffer::buffName
std::string buffName
Definition: buffer.h:274
teditor::copyFromRemote
std::string copyFromRemote(const std::string &file)
Definition: file_utils.cpp:306
teditor::Editor::sendStringf
int sendStringf(int x, int y, const AttrColor &fg, const AttrColor &bg, const char *fmt,...)
Definition: editor.cpp:302
teditor::Buffer::OpKeepRemoveLines
@ OpKeepRemoveLines
Definition: buffer.h:245
teditor::Buffer::~Buffer
virtual ~Buffer()
Definition: buffer.h:28
teditor::Buffer::isRegionActive
bool isRegionActive() const
Definition: buffer.h:174
teditor::Buffer::readOnly
bool readOnly
Definition: buffer.h:273
teditor::Mode::inferMode
static std::string inferMode(const std::string &file, bool isDir)
Helper to infer mode name from the file.
Definition: core/mode.cpp:40
teditor::Editor::buffSize
int buffSize() const
Definition: editor.h:63
teditor::Buffer::OpType
OpType
Definition: buffer.h:237
teditor::Buffer::length
int length() const
Definition: buffer.h:92
teditor::getMatchingParen
char getMatchingParen(char c)
Definition: utils.cpp:205
nfa.h
teditor::Key_Enter
static const key_t Key_Enter
Definition: keys.h:62
teditor::Window::start
const Pos2di & start() const
Definition: window.h:58
teditor::KeyCmdMap
Definition: key_cmd_map.h:19
teditor::Buffer::drawStatusBar
void drawStatusBar(Editor &ed, const Window &win)
Definition: buffer.cpp:413
teditor::Buffer::killLine
std::string killLine(bool pushToStack=true)
Definition: buffer.cpp:184
teditor::Buffer::removeChar
std::string removeChar()
Definition: buffer.cpp:86
teditor::Buffer::getPoint
const Point & getPoint() const
Definition: buffer.h:165
teditor::Buffer::pushNewOp
void pushNewOp(OpData &op)
Definition: buffer.cpp:291
teditor::ModePtr
std::shared_ptr< Mode > ModePtr
Definition: core/mode.h:19
teditor::Buffer::up
void up()
Definition: buffer.cpp:705
teditor::Pos2d::y
T y
Definition: pos2d.h:16
teditor::Buffer::resetBufferState
void resetBufferState(int line, const std::string &file, bool dir)
Definition: buffer.cpp:380
teditor::Buffer::OpData
The state before/after applying insertion/deletion operations on the Buffer object.
Definition: buffer.h:253
teditor::Buffer::nextWord
void nextWord()
Definition: buffer.cpp:756
teditor::isDir
bool isDir(const std::string &f)
Definition: file_utils.cpp:34
teditor::tempFileName
std::string tempFileName()
Definition: file_utils.cpp:148
teditor::Buffer::fileName
std::string fileName
Definition: buffer.h:274
teditor::Buffer::OpData::type
OpType type
Definition: buffer.h:263
teditor::Buffer::getMode
ModeT * getMode(const std::string &name)
Definition: buffer.h:216
teditor::Line::get
const std::string & get() const
Definition: line.h:51
teditor::Buffers
Definition: buffer.h:355
teditor::Buffer::tmpFileName
std::string tmpFileName
Definition: buffer.h:274
teditor::Buffer::undo
bool undo()
undo the previous operation
Definition: buffer.cpp:211
teditor::Buffer::reload
void reload()
Definition: buffer.cpp:340
teditor::AttrColor
Definition: colors.h:56
teditor::Buffer::at
Line & at(int idx)
Definition: buffer.h:98
teditor::Strings
std::vector< std::string > Strings
Definition: utils.h:42
teditor::parser::NFA
Ken-Thompson NFA as described here: https://swtch.com/~rsc/regexp/regexp1.html but adjusted to work w...
Definition: nfa.h:23
teditor::Buffer::addLines
void addLines(const RemovedLines &rlines)
Definition: buffer.cpp:532
teditor::Line::numLinesNeeded
int numLinesNeeded(int wid) const
Definition: line.cpp:49
teditor::Buffer::isModified
bool isModified() const
Definition: buffer.h:204
teditor::Buffer::lengthOf
int lengthOf(int i) const
Definition: buffer.h:187
line.h
teditor::Buffer::addLine
void addLine()
Definition: buffer.h:292
teditor::Buffer::redoStack
OpStack redoStack
Definition: buffer.h:286
teditor::Editor::sendChar
int sendChar(int x, int y, const AttrColor &fg, const AttrColor &bg, char c)
Definition: editor.cpp:295
command.h
teditor::Buffer::applyDeleteOp
void applyDeleteOp(OpData &op)
Delete characters/regions from the buffer.
Definition: buffer.cpp:279
teditor::Buffer::dirModeFileOffset
int dirModeFileOffset() const
Definition: buffer.h:299
teditor::Buffer::down
void down()
Definition: buffer.cpp:698
utils.h
teditor::Buffer::OpData::str
std::string str
Definition: buffer.h:259
teditor::Buffer::end
void end()
Definition: buffer.cpp:718
teditor::Window::currBuffId
int currBuffId() const
Definition: window.h:55
teditor::Buffer::RemovedLine::str
std::string str
Definition: buffer.h:228
teditor::Buffer::lines
std::vector< Line > lines
Definition: buffer.h:271
teditor::Buffer::nextPara
void nextPara()
Definition: buffer.cpp:734
teditor::Line::erase
std::string erase(int idx, int len=1)
Definition: line.cpp:7
teditor::isCloseParen
bool isCloseParen(char c)
Definition: utils.cpp:199
teditor::Buffers::~Buffers
~Buffers()
Definition: buffer.cpp:821
teditor::Window
Definition: window.h:15
teditor::Buffer::modified
bool modified
Definition: buffer.h:273
teditor::isReadOnly
bool isReadOnly(const char *f)
Definition: file_utils.cpp:52
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::Buffer::matchCurrentParen
bool matchCurrentParen()
find matching paren at the current location
Definition: buffer.cpp:551
teditor::Buffer::undoStack
OpStack undoStack
Definition: buffer.h:284
teditor::Buffer::keepRemoveLines
void keepRemoveLines(parser::NFA &regex, bool keep)
Keep/Remove lines that match the input regex.
Definition: buffer.cpp:500
teditor::Buffer::startLine
int startLine
Definition: buffer.h:272
teditor::Buffer::indent
void indent()
Definition: buffer.cpp:804
teditor::parser::NFA::NoMatch
static const size_t NoMatch
Definition: nfa.h:93
teditor::Buffer::RemovedLine
Definition: buffer.h:226
teditor::Buffer::previousWord
void previousWord()
Definition: buffer.cpp:776
teditor::parser::NFA::findAny
size_t findAny(const std::string &str, size_t &matchStartPos, size_t start=0, size_t end=0)
Tries for regex match starting from anywhere in the string.
Definition: nfa.cpp:76
teditor::Buffer::pageDown
void pageDown(int ijump)
Definition: buffer.cpp:723
teditor::Buffer::longestX
int longestX
Definition: buffer.h:282
teditor::Buffer::OpData::before
Point before
Definition: buffer.h:255
teditor::Buffer::RemovedLines
std::vector< RemovedLine > RemovedLines
Definition: buffer.h:233
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
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::num2str
std::string num2str(int num)
Definition: utils.cpp:66
file_utils.h
teditor::isAbs
bool isAbs(const std::string &file)
Definition: file_utils.cpp:116
teditor::basename
std::string basename(const std::string &file)
Definition: file_utils.cpp:133
teditor::Buffer::drawPoint
void drawPoint(Editor &ed, const AttrColor &bg, const Window &win)
Definition: buffer.cpp:402
teditor::Buffer::cu
Point cu
Definition: buffer.h:280
buffer.h
teditor::Buffer::makeReadOnly
void makeReadOnly()
Definition: buffer.cpp:345
teditor::Editor
Definition: editor.h:23
teditor::Buffer::modeName
const std::string & modeName() const
Definition: buffer.h:211
teditor::Buffer::startOfLine
void startOfLine()
Definition: buffer.h:128
teditor::Buffer::bufferName
const std::string & bufferName() const
Definition: buffer.h:200
teditor::Pos2d< int >
teditor::Buffer::right
void right()
Definition: buffer.cpp:686
DEBUG
#define DEBUG(fmt,...)
Definition: logger.h:73
teditor::Buffer::removeAndCopy
std::string removeAndCopy()
Definition: buffer.cpp:39
teditor::Buffer::dirName
std::string dirName
Definition: buffer.h:274
teditor::Mode
Definition: core/mode.h:27
teditor::Buffer::lineEnd
void lineEnd(const Point &start, const Point &dim)
Definition: buffer.cpp:628
teditor::Buffer::OpKillLine
@ OpKillLine
Definition: buffer.h:243
teditor::Buffer::begin
void begin()
Definition: buffer.cpp:713
colors.h
teditor::Buffer::sortRegion
void sortRegion()
Definition: buffer.cpp:596
teditor::Pos2d::find
int find(Pos2d< T > &start, Pos2d< T > &end, const Pos2d< T > &other) const
Finds the start/end points for the range defined by this and the other position.
Definition: pos2d.h:59
teditor::FilePerm::DirModeFileOffset
static const int DirModeFileOffset
Definition: file_utils.h:47
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::Buffer::redo
bool redo()
redo the previously undid operation.
Definition: buffer.cpp:231
teditor::Buffer::drawLine
virtual int drawLine(int y, const std::string &line, Editor &ed, int lineNum, const Window &win)
Definition: buffer.cpp:450
pos2d.h
teditor::Buffer::hasPointOn
bool hasPointOn(int line) const
Definition: buffer.h:164
teditor::Buffer::removeCurrentChar
std::string removeCurrentChar()
Definition: buffer.cpp:155
teditor::copyToRemote
void copyToRemote(const std::string &rfile, const std::string &local)
Definition: file_utils.cpp:315
teditor::rel2abs
std::string rel2abs(const std::string &pwd, const std::string &rel)
Definition: file_utils.cpp:118
teditor::Buffer::mode
ModePtr mode
Definition: buffer.h:278
teditor::Buffer::regionAsStr
std::string regionAsStr() const
Definition: buffer.cpp:307
teditor::Buffer::setMode
void setMode(ModePtr m)
Definition: buffer.h:213
window.h
teditor::Buffers::clear
void clear()
Definition: buffer.cpp:829
teditor::Buffer::endOfLine
void endOfLine()
Definition: buffer.h:130
teditor::Pos2d::isInside
bool isInside(int _y, int _x, const Pos2d< T > &cu) const
Check if the given location is in the regions.
Definition: pos2d.h:91
teditor::Buffer::clearStack
void clearStack(OpStack &st)
Definition: buffer.cpp:298
teditor::Buffer::lineUp
virtual void lineUp(const Point &dim)
Definition: buffer.cpp:620
teditor::Buffer::OpData::rlines
RemovedLines rlines
Definition: buffer.h:261
teditor::Buffer::lineReset
void lineReset()
Definition: buffer.h:197
mode.h
teditor::Buffer::pageUp
void pageUp(int ijump)
Definition: buffer.cpp:728
teditor::Buffer::previousPara
void previousPara()
Definition: buffer.cpp:745
teditor::Buffer::gotoLine
void gotoLine(int lineNum, const Point &dim)
Definition: buffer.cpp:797
teditor::Buffer::removeRegion
std::string removeRegion(const Point &start, const Point &end)
removes region between start and end
Definition: buffer.cpp:108
ULTRA_DEBUG
#define ULTRA_DEBUG(fmt,...)
Definition: logger.h:74
teditor::Buffers::namesList
Strings namesList() const
Definition: buffer.cpp:853
teditor::Buffer::insert
virtual void insert(char c)
Definition: buffer.cpp:25
teditor::Buffer::charAt
char charAt(const Point &pos) const
Definition: buffer.cpp:606
teditor::Buffer::screen2buffer
Point screen2buffer(const Point &loc, const Point &start, const Point &dim) const
Definition: buffer.cpp:488
teditor::Buffer::getWord
const std::string & getWord() const
Definition: buffer.h:210
teditor::Point
Pos2di Point
Definition: pos2d.h:112
teditor::Buffer::stopRegion
void stopRegion()
Definition: buffer.h:182
teditor::Buffer::getColor
const AttrColor & getColor(const std::string &name) const
Definition: buffer.cpp:303
editor.h
teditor::Buffer::removeFrom
std::string removeFrom(const Point &start, const Point &end)
Definition: buffer.cpp:115
teditor::dirname
std::string dirname(const std::string &file)
Definition: file_utils.cpp:139
teditor::Buffer::save
virtual bool save(const std::string &fName="")
Definition: buffer.cpp:638
teditor::getpwd
std::string getpwd()
Definition: file_utils.cpp:107
teditor::Buffer::totalLinesNeeded
virtual int totalLinesNeeded(const Point &dim) const
Definition: buffer.cpp:613
teditor::Pos2d::x
T x
Definition: pos2d.h:16
teditor::isOpenParen
bool isOpenParen(char c)
Definition: utils.cpp:195
teditor::Buffer::removeLines
void removeLines(const RemovedLines &rlines)
Definition: buffer.cpp:541
teditor::Buffer::OpDelete
@ OpDelete
Definition: buffer.h:241
teditor::Buffer::at
const Line & at(int idx) const
Definition: buffer.h:99
teditor::Buffer::region
Point region
Definition: buffer.h:276
teditor::Buffer::disableStack
bool disableStack
Definition: buffer.h:288
teditor::Buffer::getRegion
const Point & getRegion() const
Definition: buffer.h:183
teditor::Window::dim
const Pos2di & dim() const
Definition: window.h:59
teditor::LineCompare
bool LineCompare(const Line &a, const Line &b)
Definition: line.cpp:75
teditor::Buffer
Definition: buffer.h:25
teditor::Buffer::pwd
const std::string & pwd() const
Definition: buffer.h:202
teditor::Buffer::clear
virtual void clear()
Definition: buffer.cpp:173
teditor::Buffer::cmdNames
Strings cmdNames() const
Definition: buffer.h:222
teditor::Buffer::setPoint
void setPoint(const Point &p)
Definition: buffer.h:166
teditor::isRemote
bool isRemote(const std::string &f)
Definition: file_utils.cpp:79
teditor
Definition: any.hpp:10
teditor::Buffers::erase
void erase(int idx)
Definition: buffer.cpp:846
teditor::listDir2str
std::string listDir2str(const std::string &dir)
Definition: file_utils.cpp:241
teditor::Buffer::OpStack
std::stack< OpData > OpStack
Definition: buffer.h:268
teditor::Buffer::getFileName
const std::string & getFileName() const
Definition: buffer.h:201
teditor::Line::join
void join(const Line &other)
Definition: line.cpp:25