Skip to content

MadPython Introduction

Minggang Li edited this page Jul 27, 2015 · 3 revisions

MadPython for MadEdit Mod

Backgroud:

MadEdit is a cross-platform Text/Hex Editor written in C++ & wxWidgets.

MadEdit supports many useful functions, e.g. SyntaxHighlightings, WordWraps, Encodings, Column/Hex Modes, and a Plugin system(not work yet).

In HexMode, MadEdit can open large files which size is up to 32GB (INT_MAX*16).

About:

MadPython is a minimized Python which is used as an embedded macro script for automation of MadEdit-Mod. The Python is based on the work of Adie and was updated to Python 2.7.10. Lots of Python modules were removed to reduce the size of it. So, you may get errors while importing some of the standard modules.

However, I don't think it's a problem for the purpose to use it as a embedded macro script language.

Below is the list of the functions supported by MadPython. It's not a good doc and it's to be improved. It's only for the advanced users to try and test.

Note: The locale of the MadPython should be the default locale of your system.

Don't support locale changing unless there was oustanding reason for the feature!

Note: The APIs are listed as C++ functions.

C++ Key word In Python's world
void No return value
int integer
std::string string
tuple tuple
bool bool

Simple examples:

  1. Find all "abc" in current file, not using regular expression, case insensitive, not whole word match(Simulate the Find All of Find Dialog)

    me=MadEdit() # Create a MadEdit object

    res = me.FindTextAll("a", False, False, False)

    print res #how many

  2. Replace "\r\n"(Dos/Windows) to "\n"(Unix) by regular expression, then replace double line feedings with single one

    me=MadEdit() # Create a MadEdit object

    res = me.ReplaceTextAll("\r\n", "\n", True, False, False)

    res = me.ReplaceTextAll("\n\n", "\n", True, False, False)

  3. Move caret to line #10

    me=MadEdit() # Create a MadEdit object

    me.GoToLine(10)

  4. Insert 10 "A"s at current caret position(Or use InsertStr for unicode string====python only has string)

    me=MadEdit() # Create a MadEdit object

    for i in range(10):

     me.InsertWChar(65) # Or me.InsertStr('A') or me.InsertStr("A")
    

Supported method list:

void ProcessCommand(int command)

void InsertWChar(int key)

void InsertStr(const std::string &str)

void InsertIncrementalNumber(int initial, int step, int total, int stepType, int fmt, int align, bool zeroPad)

void ScrollLineUp()

void ScrollLineDown()

void ScrollPageUp()

void ScrollPageDown()

void ScrollLeft()

void ScrollRight()

void GoToLine(int line)

void GoToLineColumn(int line, int column)

void SetSyntax(const std::string &title)

std::string GetSyntaxTitle()

void LoadDefaultSyntaxScheme()

void SetEncoding(const wxString &encname)

std::string GetEncodingName()

std::string GetEncodingDescription()

int GetEncodingType()

bool GetRecordCaretMovements()

void SetRecordCaretMovements(bool value)

void SetTextFont(const std::string &name, int size, bool forceReset)

void SetHexFont(const std::string &name, int size, bool forceReset)

mad_py::tuple GetTextFont()

mad_py::tuple GetHexFont()

mad_py::tuple GetFontNameSize()

void SetFontA(const std::string &name, int size)

void SetFixedWidthMode(bool mode)

bool GetFixedWidthMode()

void SetLineSpacing(int percent)

long GetLineSpacing()

void SetEditMode(MadEditMode mode)

int GetEditMode()

void SetSingleLineMode(bool mode)

void SetTabColumns(long value)

long GetTabColumns()

void SetIndentColumns(long value)

long GetIndentColumns()

void SetInsertSpacesInsteadOfTab(bool value)

bool GetInsertSpacesInsteadOfTab()

void SetWantTab(bool value)

bool GetWantTab()

void SetWordWrapMode(int mode)

int GetWordWrapMode()

void SetShowEndOfLine(bool value)

void SetShowTabChar(bool value)

void SetShowSpaceChar(bool value)

void SetMarkActiveLine(bool value)

void SetDisplayLineNumber(bool value)

bool GetDisplayLineNumber()

void SetDisplayLineNumber(bool value)

bool GetDisplayLineNumber()

bool GetShowEndOfLine()

bool GetShowTabChar()

bool GetShowSpaceChar()

bool GetMarkActiveLine()

void SetMarkBracePair(bool value)

bool GetMarkBracePair()

long GetMaxColumns()

void SetMaxColumns(long cols)

bool GetAutoIndent()

void SetAutoIndent(bool value)

void SetAutoCompletePair(bool value)

bool GetAutoCompletePair()

void SetInsertMode(bool mode) // true: insert, false: overwrite

bool GetInsertMode()

void SetCaretType(int type)

bool GetMouseSelectToCopy()

void SetMouseSelectToCopy(bool value)

bool GetMouseSelectToCopyWithCtrlKey()

void SetMouseSelectToCopyWithCtrlKey(bool value)

bool GetMiddleMouseToPaste()

void SetMiddleMouseToPaste(bool value)

int GetMaxWordWrapWidth()

int GetUCharWidth(int uc)

int GetHexUCharWidth(int uc)

int GetUCharType(int uc)

// all are zero-based

mad_py::tuple GetCaretPositionA()

int GetCaretPositionB()

std::string GetFileName()

int GetFileSize()

bool IsSelected()

int GetSelectionSize()

int GetSelectionBeginPos()

int GetSelectionEndPos()

// return -1 for no selection

mad_py::tuple GetSelectionLineId()

int GetLineCount()

void ConvertNewLineType(MadNewLineType type)

void SetInsertNewLineType(MadNewLineType type)

int GetNewLineType()

int GetInsertNewLineType()

bool IsModified()

long GetModificationTime()

void SetReadOnly(bool value)

bool IsReadOnly()

bool IsTextFile()

std::string GetSelText()

std::string GetText(bool ignoreBOM = true)

void SetText(const std::string &ws)

// line: zero based

// return true for full line, false for partial line

mad_py::tuple GetLine(int line, size_t maxlen = 0, bool ignoreBOM = true)

int GetLineByPos(int pos)

const std::string GetSelHexString(bool withSpace)

const std::string GetWordFromCaretPos()

void Delete()

void CutLine()

void DeleteLine()

void InsertTabChar()

void InsertDateTime()

void HighlightWords()

void SelectAll()

void CutToClipboard()

void CopyToClipboardA()

void PasteFromClipboard()

void DndBegDrag()

void DndDrop()

bool CanPaste()

void CopyToClipboardB(std::string &txt)

bool CanUndo()

bool CanRedo()

void Undo()

void Redo()

void SetCaretPosition(int pos, int selbeg=-1, int selend=-1)

bool HasBracePair()

void GoToLeftBrace()

void GoToRightBrace()

// search in [rangeFrom, rangeTo], default in [CaretPos, EndOfDoc]

int FindTextNext(const std::string &text,

                    bool bRegex, bool bCaseSensitive, bool bWholeWord,

                    int rangeFrom = -1, int rangeTo = -1)

// search in [rangeFrom, rangeTo], rangeFrom > rangeTo, default in [CaretPos, BeginOfDoc]

int FindTextPrevious(const std::string &text, bool bRegex, bool bCaseSensitive, bool bWholeWord, int rangeFrom = -1, int rangeTo = -1)

// search in [rangeFrom, rangeTo], default in [CaretPos, EndOfDoc]

int FindHexNext(const std::string &hexstr, int rangeFrom = -1, int rangeTo = -1)

// search in [rangeFrom, rangeTo], rangeFrom > rangeTo, default in [CaretPos, BeginOfDoc]

int FindHexPrevious(const std::string &hexstr, int rangeFrom = -1, int rangeTo = -1)

// replace the selected text that must match expr

int ReplaceText(const std::string &expr, const std::string &fmt, bool bRegex, bool bCaseSensitive, bool bWholeWord, int rangeFrom = -1, int rangeTo = -1)

int ReplaceHex(const std::string &expr, const std::string &fmt, int rangeFrom = -1, int rangeTo = -1)

// return the replaced count or SR_EXPR_ERROR

int ReplaceTextAll(const std::string &expr, const std::string &fmt, bool bRegex, bool bCaseSensitive, bool bWholeWord, int rangeFrom = -1, int rangeTo = -1)

int ReplaceHexAll(const std::string &expr, const std::string &fmt, int rangeFrom = -1, int rangeTo = -1)

// list the matched data to pbegpos & pendpos

// return the found count or SR_EXPR_ERROR

int FindTextAll(const std::string &expr, bool bRegex, bool bCaseSensitive, bool bWholeWord)

int FindHexAll(const std::string &expr)

bool LoadFromFile(const std::string &filename, const std::string &encoding=std::string(""))

bool SaveToFile(const std::string &filename)

bool Reload()

// if the file is modified by another app, reload it.

bool ReloadByModificationTime()

// restore pos in Reload(), ConvertEncoding()

void RestorePosition(int pos, int toprow)

// write back to the original FileName

// if FileName is empty, ask the user to get filename

// return wxID_YES(Saved), wxID_NO(Not Saved), or wxID_CANCEL

int Save(bool ask, const std::string &title, bool saveas)

void SetBookmark()

void GotoNextBookmark()

void GotoPreviousBookmark()

void ConvertEncoding(const std::string &newenc, int flag)

void ConvertChineseA(int flag)

bool HasBOM()

void ToggleBOM()

void IncreaseDecreaseIndent(bool incIndent)

bool HasLineComment()

void CommentUncomment(bool comment)

void ToUpperCase()

void ToLowerCase()

void InvertCase()

void Capitalize()

void ToHalfWidth(bool ascii=true, bool japanese=true, bool korean=true, bool other=true)

void ToFullWidth(bool ascii=true, bool japanese=true, bool korean=true, bool other=true)

void TrimTrailingSpaces()

void DeleteEmptyLines()

void DeleteEmptyLinesWithSpaces()

void JoinLines()

// startline<0 : sort all lines otherwise sort [beginline, endline]

void SortLines(MadSortFlags flags, int beginline, int endline)

// convert WordWraps to NewLine-chars in the SelText or whole file

void ConvertWordWrapToNewLine()

// convert NewLine-chars to WordWraps in the SelText

void ConvertNewLineToWordWrap()

void ConvertSpaceToTab()

void ConvertTabToSpace()

void CopyAsHexString(bool withSpace)

void CopyRevertHex(const std::string &delimiters)

mad_py::tuple WordCount(bool selection)

void ColumnAlign()

void InsertIncrementalNumber(int initial, int step, int total, int stepType, int fmt, int align, bool zeroPad)

MadEditCommand

None

CharFirst

CharLast

CaretCommandFirst

Left

Right

Up

Down

BeginLine

EndLine

BeginDoc

EndDoc

PrevPage

NextPage

PrevWord

NextWord

LeftBrace

RightBrace

CaretCommandLast

SelCommandFirst

SelLeft

SelRight

SelUp

SelDown

SelBeginLine

SelEndLine

SelBeginDoc

SelEndDoc

SelPrevPage

SelNextPage

SelPrevWord

SelNextWord

SelLeftBrace

SelRightBrace

SelCommandLast

HighlightWords

SeltAll

ScrollLineUp

ScrollLineDown

ScrollPageUp

ScrollPageDown

ScrollLeft

ScrollRight

Return

ReturnNoIndent

Tab

InsertTabChar

Delete

BackSpace

DelPrevWord

DelNextWord

CutLine

DeleteLine

Undo

Redo

Cut

Copy

Paste

ToggleInsertMode

TextMode

ColumnMode

HexMode

NoWrap

WrapByWindow

WrapByColumn

ToggleWindow

IncreaseIndent

DreaseIndent

Comment

Uncomment

ToUpperCase

ToLowerCase

InvertCase

ToHalfWidth

ToFullWidth

InsertDateTime

MouseNotify

Note: Some of the command is meanless with scriptin, such like MouseNotify. And some of them

have meanfull clone like ToUpperCase, Paste, etc

MadWordWrapMode

NoWrap

WrapByWindow

WrapByColumn

MadEditMode

TextMode

ColumnMode

HexMode

MadCaretType

VerticalLine

HorizontalLine

Block

MadNewLineType

Default

DOS

UNIX

MAC

MadConvertEncodingFlag

None

SC2TC// Simplified Chinese ==> Traditional Chinese

TC2SC// Traditional Chinese ==> Simplified Chinese

JK2TC// Japanese Kanji ==> Traditional Chinese

JK2SC// Japanese Kanji ==> Simplified Chinese

C2JK // Trad&Simp Chinese ==> Japanese Kanji

MadNumberingStepType

Linear

Exponential

MadNumberFormat

DEC

HEX

BIN

OCT

MadNumberAlign

Left

Right