GithubHelp home page GithubHelp logo

richcode.ahk's Introduction

RichCode.ahk

A wrapper around a RichEdit control to provide code editing features.

Using

Create an instance of the RichCode class, passing it a Settings array whose members are as listed below. As a second, optional parameter you can pass control options to be used in the Gui, Add command.

See Examples/Demo.ahk for more advanced usage.

class RichCode({"TabSize": 4     ; Width of a tab in characters
	, "Indent": "`t"             ; What text to insert on indent
	, "FGColor": 0xRRGGBB        ; Foreground (text) color
	, "BGColor": 0xRRGGBB        ; Background color
	, "Font"                     ; Font to use
	: {"Typeface": "Courier New" ; Name of the typeface
		, "Size": 12             ; Font size in points
		, "Bold": False}         ; Bold weight (True/False)
	, "WordWrap": False          ; Whether to enable WordWrap
	
	
	; Whether to use the highlighter, or leave it as plain text
	, "UseHighlighter": True
	
	; Delay after typing before the highlighter is run
	, "HighlightDelay": 200
	
	; The highlighter function (FuncObj or name)
	; to generate the highlighted RTF. It will be passed
	; two parameters, the first being this settings array
	; and the second being the code to be highlighted
	, "Highlighter": Func("HighlightAHK")
	
	; The colors to be used by the highlighter function.
	; This is currently used only by the highlighter, not at all by the
	; RichCode class. As such, the RGB ordering is by convention only.
	; You can add as many colors to this array as you want.
	, "Colors"
	: {"Comments": 0xRRGGBB
		, "Functions": 0xRRGGBB
		, "Numbers": 0xRRGGBB,
		, "Strings": 0xRRGGBB}})

richcode.ahk's People

Contributors

g33kdude avatar lintalist avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

richcode.ahk's Issues

Wordwrap option?

Would it possible to add a word wrap option to the settings?

Settings :=
( LTrim Join Comments
{
	"TabSize": 4,
	"Indent": "`t",
	"WordWrap": true,
....

I think this would be useful to have.

The Contextmenu Doesn't Work Properly.

RichCode.ahk/RichCode.ahk

Lines 207 to 241 in 9a54252

; Create the right click menu
this.menu := Menu()
for Index, Entry in RichCode.MenuItems
(entry == "") ? this.menu.Add() : this.menu.Add(Entry, (*) => this.RightClickMenu.Bind(this))
; Get the ITextDocument object
bufpIRichEditOle := Buffer(A_PtrSize, 0)
this.SendMsg(0x43C, 0, bufpIRichEditOle) ; EM_GETOLEINTERFACE
this.pIRichEditOle := NumGet(bufpIRichEditOle, "UPtr")
this.IRichEditOle := ComValue(9, this.pIRichEditOle, 1)
; ObjAddRef(this.pIRichEditOle)
this.pITextDocument := ComObjQuery(this.IRichEditOle, RichCode.IID_ITextDocument)
this.ITextDocument := ComValue(9, this.pITextDocument, 1)
; ObjAddRef(this.pITextDocument)
}
RightClickMenu(ItemName, ItemPos, MenuName)
{
if (ItemName == "Cut")
Clipboard := this.SelectedText, this.SelectedText := ""
else if (ItemName == "Copy")
Clipboard := this.SelectedText
else if (ItemName == "Paste")
this.SelectedText := A_Clipboard
else if (ItemName == "Delete")
this.SelectedText := ""
else if (ItemName == "Select All")
this.Selection := [0, -1]
else if (ItemName == "UPPERCASE")
this.SelectedText := Format("{:U}", this.SelectedText)
else if (ItemName == "lowercase")
this.SelectedText := Format("{:L}", this.SelectedText)
else if (ItemName == "TitleCase")
this.SelectedText := Format("{:T}", this.SelectedText)
}

I apologise for not being able to point out exactly where the problem is, but this is what I have done so far to fix it.
I'm sure you'll understand as soon as you see it.

; Create the right click menu
this.menu := Menu()
for Index, Entry in RichCode.MenuItems
	this.menu.Add(Entry?, IsSet(Entry) ? RightClickMenu : unset)

; Get the ITextDocument object
bufpIRichEditOle := Buffer(A_PtrSize, 0)
this.SendMsg(0x43C, 0, bufpIRichEditOle) ; EM_GETOLEINTERFACE
this.pIRichEditOle := NumGet(bufpIRichEditOle, "UPtr")
this.IRichEditOle := ComValue(9, this.pIRichEditOle, 1)
; ObjAddRef(this.pIRichEditOle)
this.pITextDocument := ComObjQuery(this.IRichEditOle, RichCode.IID_ITextDocument)
this.ITextDocument := ComValue(9, this.pITextDocument, 1)
; ObjAddRef(this.pITextDocument)

RightClickMenu(ItemName, ItemPos, MenuName)
{
	Switch ItemName {
	case "Cut"       : A_Clipboard := this.SelectedText, this.SelectedText := ""
	case "Copy"      : A_Clipboard := this.SelectedText
	case "Paste"     : this.SelectedText := A_Clipboard
	case "Delete"    : this.SelectedText := ""
	case "Select All": this.Selection    := [0, -1]
	case "UPPERCASE" : this.SelectedText := Format("{:U}", this.SelectedText)
	case "lowercase" : this.SelectedText := Format("{:L}", this.SelectedText)
	case "TitleCase" : this.SelectedText := Format("{:T}", this.SelectedText)
	}
}

Crash on paste when using v1.1.27.01 (doesn't happen with v1.1.26.01) ?

Not sure what is going on but I just upgraded to AHK v1.1.27.01, unicode, and when I paste text in a RichCode control "AutoHotkey stops working" and it crashes. But when I run it using v1.1.26.01 pasting text is no problem.

Is it just my system or do you experience this as well (just paste some text into 'demo')

Edit: Also seems to crashing when pressing the TAB key ? text text TAB -> crash

Question: Two controls, two syntaxes, remembers last

In the previous version I could add multiple controls in one GUI and set the "language" for each control, which worked well. With this version, it seems it only remembers the last "language" (the most recently added control).

If you type something (just hitting enter will do) in the first control it changes the HTML highlighting to AHK highlighting (you see the colors change or just type something like 'this' for example to see it, this being a "flow". A modified Examples\demo2.ahk to illustrate: (I didn't bother to update to block/unblock code, I did change the ChangeLang: label)

#NoEnv
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

#Include %A_ScriptDir%\..
#Include RichCode.ahk
#Include Highlighters\AHK.ahk
#Include Highlighters\CSS.ahk
#Include Highlighters\HTML.ahk
#Include Highlighters\JS.ahk

; Table of supported languages and sample codes for the demo
Codes :=
( LTrim Join Comments
{
	"AHK": {
		"Highlighter": "HighlightAHK",
		"Code": FileOpen(A_ScriptFullPath, "r").Read()
	},
	"HTML": {
		"Highlighter": "HighlightHTML",
		"Code": FileOpen("Language Samples\Sample.html", "r").Read()
	},
	"CSS": {
		"Highlighter": "HighlightCSS",
		"Code": FileOpen("Language Samples\Sample.css", "r").Read()
	},
	"JS": {
		"Highlighter": "HighlightJS",
		"Code": FileOpen("Language Samples\Sample.js", "r").Read()
	},
	"Plain": {
		"Highlighter": "",
		"Code": FileOpen("Language Samples\Sample.txt", "r").Read()
	}
}
)

; Settings array for the RichCode control
Settings :=
( LTrim Join Comments
{
	"TabSize": 4,
	"Indent": "`t",
	"FGColor": 0xEDEDCD,
	"BGColor": 0x3F3F3F,
	"Font": {"Typeface": "Consolas", "Size": 11},
	"WordWrap": False,
	
	"UseHighlighter": True,
	"HighlightDelay": 200,
	"Colors": {
		"Comments":     0x7F9F7F,
		"Functions":    0x7CC8CF,
		"Keywords":     0xE4EDED,
		"Multiline":    0x7F9F7F,
		"Numbers":      0xF79B57,
		"Punctuation":  0x97C0EB,
		"Strings":      0xCC9893,
		
		; AHK
		"A_Builtins":   0xF79B57,
		"Commands":     0xCDBFA3,
		"Directives":   0x7CC8CF,
		"Flow":         0xE4EDED,
		"KeyNames":     0xCB8DD9,
		
		; CSS
		"ColorCodes":   0x7CC8CF,
		"Properties":   0xCDBFA3,
		"Selectors":    0xE4EDED,
		
		; HTML
		"Attributes":   0x7CC8CF,
		"Entities":     0xF79B57,
		"Tags":         0xCDBFA3,
		
		; JS
		"Builtins":     0xE4EDED,
		"Constants":    0xF79B57,
		"Declarations": 0xCDBFA3
	}
}
)

; Add some controls
Gui, Add, DropDownList, gChangeLang vLanguage, AHK||CSS|HTML|JS|Plain
Gui, Add, Button, ym gBlockComment, Block &Comment
Gui, Add, Button, ym gBlockUncomment, Block &Uncomment

; Add the RichCode
RC1 := new RichCode(Settings, "xm w640 h270")
RC2 := new RichCode(Settings, "xm xp w640 h270")
GuiControl, Focus, % RC1.hWnd

; Set its starting contents
GoSub, ChangeLang

Gui, Show
return


GuiClose:
; Overwrite RC, leaving the only reference from the GUI
RC1 := ""
RC2 := ""

; Destroy the GUI, freeing the RichCode instance
Gui, Destroy

; Close the script
ExitApp
return


BlockComment:
; Get the selected language from the GUI
GuiControlGet, Language

; Apply an appropriate block comment transformation
if (Language == "AHK")
	RC.IndentSelection(False, ";")
else if (Language == "HTML")
	RC.SelectedText := "<!-- " RC.SelectedText " -->"
else if (Language == "CSS" || Language == "JS")
	RC.SelectedText := "/* " RC.SelectedText " */"
return

BlockUncomment:
; Get the selected language from the GUI
GuiControlGet, Language

; Remove an appropriate block comment transformation
if (Language == "AHK")
	RC.IndentSelection(True, ";")
else if (Language == "HTML")
	RC.SelectedText := RegExReplace(RC.SelectedText, "s)<!-- ?(.+?) ?-->", "$1")
else if (Language == "CSS" || Language == "JS")
	RC.SelectedText := RegExReplace(RC.SelectedText, "s)\/\* ?(.+?) ?\*\/", "$1")
return

ChangeLang:
; Keep a back up of the contents
if Language
	Codes[Language].Code := RC.Value

; Get the selected language from the GUI
language:="html"
; Set the new highlighter and contents
RC1.Settings.Highlighter := Codes[Language].Highlighter
RC1.Value := Codes[Language].Code
language:="ahk"
RC2.Settings.Highlighter := Codes[Language].Highlighter
RC2.Value := Codes[Language].Code
return

HTML regex fix

From @kczx3 on December 23, 2017 20:36

For the HTML regex, you need the lazy modifier on the multi-line comments for them to highlight properly.

	static Needle := "
	( LTrim Join Comments
		ODims)
		(\<\!--.*?--\>)        ; Multiline comments
		|(<(?:\/\s*)?)(\w+)   ; Tag
		|([<>\/])             ; Punctuation
		|(&\w+?;)             ; Entities
		|((?<=[>;])[^<>&]+)   ; Text
		|(""[^""]*""|'[^']*') ; String
		|(\w+\s*)(=)          ; Attribute
	)"

Copied from original issue: G33kDude/MultiTester.ahk#1

highlighting #Directives and A_Variables

When you type #Directives or A_Variables, they are highlighted even if they are not part of the AHK syntax. This makes it confusing when you are not sure if you typed it right. Likely it is better to just make sure that it is typed right. ๐Ÿ™ƒ ๐Ÿ‘‹

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.