notepad4 icon indicating copy to clipboard operation
notepad4 copied to clipboard

新功能请求:增加“加工文件”功能和按钮

Open anbangli opened this issue 2 years ago • 2 comments

我想拿 notepad2 做成一个简单的 C/C++ 集成开发环境(IDE),就是说,想要两个小功能: 1、调用编译器对正在编辑的源程序进行编译加工,并显示出编译结果; 2、运行编译后生成的可执行程序。 我查看了 “自定义工具栏” 对话框,里面有“运行文件”按钮,实现不了我上面的需求。 我想了想,我上面需要的功能也并不复杂,程序内需要允许用户定义两个字符串, 第1个是作为编译命令,相当于“加工文件”的功能(还需要增加相应的按钮)。 第2个是指定同名的可执行文件。 希望作者能考虑增加我说的这些功能。谢谢。

anbangli avatar Feb 24 '23 02:02 anbangli

SciTE has these functions, https://www.scintilla.org/SciTE.html

image

zufuliu avatar Feb 24 '23 10:02 zufuliu

Notepad2 is not likely either to include IDE-like facilities (e.g. running external programs against current buffer) or to have an embedded scripting language (such as Lua) that make extensions easy.

Consequently it serves as little more than a mere text editor in the set of development tools, and the typical scenario of using it is opening a shell console beside it to type in building commands.

That said, workarounds are possible. The AutoHotkey scripting language is capable of making generic, native Windows GUI tweaks. For example, the following AutoHotkey code retrieves the file path of Notepad2's buffer from its title and presents two buttons to compile and execute the file:

NP2EditingFilePath() {
	; 获取Notepad2正在编辑的文件路径
	; Notepad2 的窗口标题显示要配置为“显示完整路径名称”
	
	s := " - Notepad2"
	title := WinGetTitle(s)
	if (title) {
		pos := InStr(title, s, true, , -1)
		title := SubStr(title, 1, pos-1)
	}
	return title
}

quote(str) {
	; 如果命令行的某一部分str(如文件名)带有空格,
	; 则在其两端加上双引号,以便识别为一个整体
	if (InStr(str, " ")) {
		str := "`"" . str . "`""
	}
	return str
}

compileCmd(name, nameext){
	; 构造实际编译命令行
	; 如compileCmd("hello", "hello.c")
	; 则返回"C:\w64devkit\bin\gcc.exe -o hello.exe hello.c -lkernel32"
	 	
	; 在此处定义编译器路径、依赖库等
	static compiler := "C:\w64devkit\bin\gcc.exe"
	static library := "-lkernel32"
	
	return Format(quote(compiler) . " -o {:s} {:s} " . library
	, quote(name . ".exe"), quote(nameext))
}

class CompilePanel {
	compileButton := []
	executeButton := []
	
	compile(h, *){
		srcpath := NP2EditingFilePath()
		if (srcpath) {
			SplitPath srcpath, &nameext, &dir,  , &name
			SetWorkingDir(dir . "\")
			Run compileCmd(name, nameext)
		}
	}
	
	execute(h, *){
		srcpath := NP2EditingFilePath()
		if (srcpath) {
			SplitPath srcpath, &nameext, &dir,  , &name
			SetWorkingDir(dir . "\")
			if (FileExist(name . ".exe")){
				;文件执行期间被占用,再次编译无用
				ControlSetEnabled false, this.compileButton
				RunWait quote(name . ".exe")
				ControlSetEnabled true, this.compileButton
			} else {
				MsgBox "执行文件不存在,请先编译"
			}
		}
	}
	
	__New(){
		g := Gui("-Resize", "", this)
		this.compileButton := g.AddButton("", "编译")
		this.executeButton := g.AddButton("ys", "执行")   ; ys:布置在上一按钮的右边
		
		this.compileButton.OnEvent("click", "compile")
		this.executeButton.OnEvent("click", "execute")
		g.Show()
	}
}

CompilePanel()

To have this code work you should first set NP2 to display the full path of current buffer in its title.

PNBRQK avatar Jan 21 '24 00:01 PNBRQK