teditor  1.8.0@@fee5e94
Terminal based editor written in C++
core/mode.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <memory>
6 #include "utils.h"
7 #include "colors.h"
8 #include "command.h"
9 
10 namespace teditor {
11 
12 class Buffer;
13 class KeyCmdMap;
14 class ColorMap;
15 class Mode;
16 
17 
18 typedef Mode* (*ModeCreator)();
19 typedef std::shared_ptr<Mode> ModePtr;
20 typedef std::unordered_map<std::string,ModeCreator> ModeCreatorMap;
21 
22 typedef bool (*InferMode)(const std::string&);
23 typedef std::unordered_map<std::string,InferMode> ModeInferMap;
24 
25 
27 class Mode {
28 public:
34  Mode(const std::string& n, const std::string& w): name_(n), word_(w) {}
35 
37  virtual ~Mode() {}
38 
40  virtual const std::string& name() const { return name_; }
41 
43  virtual const std::string& word() const { return word_; }
44 
46  virtual int indent(Buffer& buf, int line) = 0;
47 
49  virtual KeyCmdMap& getKeyCmdMap() = 0;
50 
52  virtual ColorMap& getColorMap() = 0;
53 
55  virtual void getColorFor(AttrColor& fg, AttrColor& bg, int lineNum, int pos,
56  const Buffer& b, bool isHighlighted) = 0;
57 
59  virtual Strings cmdNames() const;
60 
66  static ModePtr createMode(const std::string& mode);
67 
74  static std::string inferMode(const std::string& file, bool isDir);
75 
76  struct Registrar {
77  Registrar(const std::string& mode, ModeCreator fptr, InferMode iptr);
78  }; // struct Registrar
79 
80 private:
82  std::string name_;
84  std::string word_;
85 }; // class Mode
86 
87 
90 
91 
92 #define REGISTER_MODE(Name, NameStr) \
93  Mode::Registrar mode ## Name(NameStr, Name::create, Name::modeCheck)
94 
95 }; // end namespace teditor
teditor::ledger::LedgerMode::LedgerMode
LedgerMode()
Definition: extensions/ledger/mode.cpp:13
teditor::readonly::ReadOnlyMode::ReadOnlyMode
ReadOnlyMode(const std::string &n="ro", const std::string &w="abcdefghijklmnopqrstuvwxyzABCDEGGHIJKLMNO" "PQRSTUVWXYZ0123456789_")
Definition: readonly.cpp:13
teditor::allModeNames
Strings allModeNames()
Definition: core/mode.cpp:16
teditor::grep::GrepMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: extensions/grep/mode.cpp:32
teditor::cpp::CppMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: extensions/cpp/mode.cpp:36
teditor::todo::TodoShowMode
Definition: extensions/todo/mode.cpp:27
teditor::calc::addCmd
void addCmd(const std::string &c)
Definition: extensions/calc/mode.cpp:22
teditor::History::store
void store() const
Definition: utils.cpp:276
teditor::grep::GrepMode
Definition: extensions/grep/mode.cpp:8
teditor::Mode::indent
virtual int indent(Buffer &buf, int line)=0
teditor::text::TextMode::TextMode
TextMode(const std::string &n="text", const std::string &w="abcdefghijklmnopqrstuvwxyzABCDEGGHIJKLMNOPQRS" "TUVWXYZ0123456789_")
Definition: text.cpp:10
teditor::cpp::REGISTER_MODE
REGISTER_MODE(CppMode, "c++")
teditor::Mode::name
virtual const std::string & name() const
Definition: core/mode.h:40
teditor::watch::WatchMode::cmdNames
Strings cmdNames() const
Definition: extensions/watch/mode.cpp:17
teditor::ledger::LedgerShowMode::create
static Mode * create()
Definition: extensions/ledger/mode.cpp:39
teditor::calc::VarMap
std::unordered_map< std::string, Num64 > VarMap
Definition: extensions/calc/mode.h:9
teditor::calc::CalcMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: extensions/calc/mode.cpp:37
teditor::grep::REGISTER_MODE
REGISTER_MODE(GrepMode, "grep")
teditor::cpp::CppMode::create
static Mode * create()
Definition: extensions/cpp/mode.cpp:34
teditor::ColorMap
Definition: colors.h:87
teditor::ModeInferMap
std::unordered_map< std::string, InferMode > ModeInferMap
Definition: core/mode.h:23
teditor::ModeCreator
Mode *(* ModeCreator)()
Definition: core/mode.h:18
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::Buffer::length
int length() const
Definition: buffer.h:92
nfa.h
teditor::calc::CalcMode
Definition: extensions/calc/mode.cpp:27
teditor::ledger::LedgerShowMode::LedgerShowMode
LedgerShowMode()
Definition: extensions/ledger/mode.cpp:34
teditor::KeyCmdMap
Definition: key_cmd_map.h:19
teditor::check_output
CmdStatus check_output(const std::string &cmd)
Definition: utils.cpp:110
teditor::modes
ModeCreatorMap & modes()
Definition: core/mode.cpp:6
teditor::dir::DirMode::DirMode
DirMode()
Definition: extensions/dir/mode.cpp:11
teditor::todo::TodoMode::create
static Mode * create()
Definition: extensions/todo/mode.cpp:15
teditor::ModePtr
std::shared_ptr< Mode > ModePtr
Definition: core/mode.h:19
mode.h
teditor::allCmdNames
Strings allCmdNames(CmdFilterOp filterOp)
Accessor function to the list of command names.
Definition: command.cpp:30
teditor::isDir
bool isDir(const std::string &f)
Definition: file_utils.cpp:34
teditor::watch::REGISTER_MODE
REGISTER_MODE(WatchMode, "watch")
teditor::todo::TodoShowMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: extensions/todo/mode.cpp:35
teditor::Line::get
const std::string & get() const
Definition: line.h:51
teditor::dir::DirMode::getColorFor
void getColorFor(AttrColor &fg, AttrColor &bg, int lineNum, int pos, const Buffer &b, bool isHighlighted)
Definition: extensions/dir/mode.cpp:16
teditor::Mode::Mode
Mode(const std::string &n, const std::string &w)
ctor
Definition: core/mode.h:34
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::todo::TodoMode::TodoMode
TodoMode()
Definition: extensions/todo/mode.cpp:13
teditor::todo::TodoShowMode::TodoShowMode
TodoShowMode()
Definition: extensions/todo/mode.cpp:29
teditor::cpp::CppMode::indent
int indent(Buffer &buf, int line)
Definition: extensions/cpp/mode.cpp:16
command.h
teditor::calc::CalcMode::~CalcMode
~CalcMode()
Definition: extensions/calc/mode.cpp:34
teditor::grep::GrepMode::GrepMode
GrepMode()
Definition: extensions/grep/mode.cpp:10
utils.h
teditor::dir::DirMode::create
static Mode * create()
Definition: extensions/dir/mode.cpp:32
teditor::ledger::LedgerMode::cmdNames
Strings cmdNames() const
Definition: extensions/ledger/mode.cpp:15
teditor::Mode::cmdNames
virtual Strings cmdNames() const
Definition: core/mode.cpp:25
teditor::InferMode
bool(* InferMode)(const std::string &)
Definition: core/mode.h:22
teditor::Mode::getColorFor
virtual void getColorFor(AttrColor &fg, AttrColor &bg, int lineNum, int pos, const Buffer &b, bool isHighlighted)=0
teditor::parser::NFA::NoMatch
static const size_t NoMatch
Definition: nfa.h:93
teditor::dir::DirMode
Definition: extensions/dir/mode.cpp:9
teditor::ledger::LedgerShowMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: extensions/ledger/mode.cpp:40
teditor::Mode::word
virtual const std::string & word() const
Definition: core/mode.h:43
teditor::ledger::REGISTER_MODE
REGISTER_MODE(LedgerMode, "ledger")
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::watch::WatchMode::start
void start(Buffer *buf, const std::string &cmd, int sleepLenMs=0)
Definition: extensions/watch/mode.cpp:35
teditor::History::add
void add(const std::string &elem)
Definition: utils.cpp:285
teditor::text::TextMode
Definition: text.h:11
file_utils.h
teditor::todo::TodoMode
Definition: extensions/todo/mode.cpp:11
teditor::cpp::CppMode::CppMode
CppMode()
Definition: extensions/cpp/mode.cpp:11
buffer.h
teditor::readonly::ReadOnlyMode::getKeyCmdMap
KeyCmdMap & getKeyCmdMap()
Definition: readonly.h:17
teditor::Mode::~Mode
virtual ~Mode()
Definition: core/mode.h:37
teditor::grep::GrepMode::getColorFor
void getColorFor(AttrColor &fg, AttrColor &bg, int lineNum, int pos, const Buffer &b, bool isHighlighted)
Definition: extensions/grep/mode.cpp:15
teditor::calc::CalcMode::CalcMode
CalcMode()
Definition: extensions/calc/mode.cpp:29
teditor::calc::CalcMode::create
static Mode * create()
Definition: extensions/calc/mode.cpp:36
teditor::Mode::Registrar
Definition: core/mode.h:76
time_utils.h
teditor::Mode
Definition: core/mode.h:27
colors.h
teditor::dir::REGISTER_MODE
REGISTER_MODE(DirMode, "dir")
teditor::ledger::LedgerMode
Definition: extensions/ledger/mode.cpp:11
teditor::Mode::Registrar::Registrar
Registrar(const std::string &mode, ModeCreator fptr, InferMode iptr)
Definition: core/mode.cpp:31
teditor::todo::TodoMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: extensions/todo/mode.cpp:17
teditor::History
Container to store history (files-visited or command-history, etc)
Definition: utils.h:115
teditor::watch::WatchMode::stop
void stop()
Definition: extensions/watch/mode.cpp:44
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::dir::DirMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: extensions/dir/mode.cpp:34
mode.h
teditor::grep::GrepMode::create
static Mode * create()
Definition: extensions/grep/mode.cpp:30
teditor::todo::REGISTER_MODE
REGISTER_MODE(TodoMode, "todo")
teditor::ledger::LedgerShowMode
Definition: extensions/ledger/mode.cpp:32
teditor::todo::TodoShowMode::create
static Mode * create()
Definition: extensions/todo/mode.cpp:34
teditor::Mode::getColorMap
virtual ColorMap & getColorMap()=0
teditor::parser::NFA::find
size_t find(const std::string &str, size_t start=0, size_t end=0)
String match function.
Definition: nfa.cpp:64
mode.h
teditor::Mode::getKeyCmdMap
virtual KeyCmdMap & getKeyCmdMap()=0
teditor::ledger::LedgerMode::create
static Mode * create()
Definition: extensions/ledger/mode.cpp:21
teditor::Option
Class to define an option used in the editor.
Definition: option.h:14
teditor::calc::vars
VarMap & vars()
Definition: extensions/calc/mode.cpp:11
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::cpp::CppMode
Definition: extensions/cpp/mode.cpp:9
teditor::calc::REGISTER_MODE
REGISTER_MODE(CalcMode, "calc")
option.h
teditor::infers
ModeInferMap & infers()
Definition: core/mode.cpp:11
teditor::format
std::string format(const char *fmt, va_list &vl)
Definition: utils.cpp:22
teditor::ledger::LedgerMode::modeCheck
static bool modeCheck(const std::string &file)
Definition: extensions/ledger/mode.cpp:23
number.h
teditor::ModeCreatorMap
std::unordered_map< std::string, ModeCreator > ModeCreatorMap
Definition: core/mode.h:20
teditor::calc::cmds
History & cmds()
Definition: extensions/calc/mode.cpp:16
teditor::Buffer
Definition: buffer.h:25
teditor::Buffer::clear
virtual void clear()
Definition: buffer.cpp:173
teditor
Definition: any.hpp:10
teditor::readonly::ReadOnlyMode::getColorMap
ColorMap & getColorMap()
Definition: readonly.h:18
teditor::readonly::ReadOnlyMode
Definition: readonly.h:11
teditor::currentTimeToStr
std::string currentTimeToStr()
Definition: time_utils.cpp:46
teditor::watch::WatchMode::WatchMode
WatchMode()
Definition: extensions/watch/mode.cpp:9