Make Notepad++ auto close HTML/XML tags after the slash(the Dreamweaver way)
I've been looking for a Notepad++ plugin that can close HTML/XML tags after a slash just like the way Dreamweaver does for a long time.
The only things I could find(TextFX, XML Tools etc.) close the tags right after ">" is typed in, which was not what I wanted.
A couple days ago I found a plugin called Automation Scripts. It allows me to write scripts in C#, so I don't have to spend time learning how to write Notepad++ plugins.
So here you go:
//npp_shortcut Ctrl+Shift+Z //Automation Scripts plugin needs to be installed for this to work
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System;
using System.Windows.Forms;
using NppScripts; public class Script : NppScript
{
public Script()
{
this.OnNotification = (notification) =>
{
if(notification.nmhdr.code == (uint)SciMsg.SCN_CHARADDED)
{
doInsertHtmlCloseTag((char)notification.ch);
}
};
} public override void Run()
{
checkInsertHtmlCloseTag();
} bool doCloseTag; void checkInsertHtmlCloseTag()
{
doCloseTag = !doCloseTag;
Win32.SendMessage(Npp.NppHandle, NppMsg.NPPM_SETMENUITEMCHECK, Plugin.FuncItems.Items[this.ScriptId]._cmdID, doCloseTag ? : );
} string GetStartTagName(string input)
{
Regex regex=new Regex(@"<[A-Za-z][A-Za-z0-9]*[^>]*[^\/]>|<[A-Za-z]>");
if(!regex.IsMatch(input)) return "";
StringBuilder result=new StringBuilder();
int i=;
while(input[i]!=' ' && input[i]!='>' & i<input.Length)
{
result.Append(input[i]);
i++;
}
return result.ToString();
} string GetEndTagName(string input)
{
Regex regex=new Regex(@"<\/[A-Za-z][A-Za-z0-9]*[^>]*>");
if(!regex.IsMatch(input)) return "";
StringBuilder result=new StringBuilder();
int i=;
while(input[i]!=' ' && input[i]!='>' & i<input.Length)
{
result.Append(input[i]);
i++;
}
return result.ToString();
} string GetSelection()
{
IntPtr hCurrentEditView = Npp.CurrentScintilla;
StringBuilder result=new StringBuilder();
Win32.SendMessage(hCurrentEditView,SciMsg.SCI_GETSELTEXT,,result);
return result.ToString();
} int FindNextTag(int pos)
{
IntPtr hCurrentEditView = Npp.CurrentScintilla;
string pattern=@"<\/?[A-Za-z][A-Za-z0-9]*[^>]*[^\/]>|<\/?[A-Za-z]>";
Win32.SendMessage(hCurrentEditView, SciMsg.SCI_SETSEL, pos, pos);
Win32.SendMessage(hCurrentEditView,SciMsg.SCI_SEARCHANCHOR,,);
return (int)Win32.SendMessage(hCurrentEditView,SciMsg.SCI_SEARCHPREV,(int)SciMsg.SCFIND_REGEXP,pattern);
} void doInsertHtmlCloseTag(char newChar)
{
LangType docType = LangType.L_TEXT;
Win32.SendMessage(Npp.NppHandle, NppMsg.NPPM_GETCURRENTLANGTYPE, , ref docType);
bool isDocTypeHTML = (docType == LangType.L_HTML || docType == LangType.L_XML || docType == LangType.L_PHP);
if (doCloseTag && isDocTypeHTML && newChar=='/')
{
IntPtr hCurrentEditView = Npp.CurrentScintilla;
int currentPos = (int)Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETCURRENTPOS, , );
char lastChar=(char)Win32.SendMessage(hCurrentEditView,SciMsg.SCI_GETCHARAT,currentPos-,);
StringBuilder insertString=new StringBuilder();
if(lastChar=='<')
{
int pos=currentPos;
Stack<string> stack=new Stack<string>();
string tag;
while(true)
{
pos=FindNextTag(pos);
if(pos==-)
{
Win32.SendMessage(hCurrentEditView, SciMsg.SCI_SETSEL, currentPos, currentPos);
return;
}
tag=GetSelection();
if(tag[]=='/')
{
stack.Push(GetEndTagName(tag));
}
else
{
tag=GetStartTagName(tag);
if(stack.Count==)
break;
else
{
string endTag=stack.Pop();
while(tag!=endTag && stack.Count>)
{
endTag=stack.Pop();
}
if(tag!=endTag) break;
}
}
}
insertString.Append(tag+">");
Win32.SendMessage(hCurrentEditView, SciMsg.SCI_BEGINUNDOACTION, , );
Win32.SendMessage(hCurrentEditView, SciMsg.SCI_SETSEL, currentPos, currentPos);
Win32.SendMessage(hCurrentEditView, SciMsg.SCI_REPLACESEL, , insertString);
Win32.SendMessage(hCurrentEditView, SciMsg.SCI_SETSEL, currentPos+insertString.Length, currentPos+insertString.Length);
Win32.SendMessage(hCurrentEditView, SciMsg.SCI_ENDUNDOACTION, , );
}
}
}
}
Make Notepad++ auto close HTML/XML tags after the slash(the Dreamweaver way)的更多相关文章
- Using Notepad++ To Quickly Format XML
http://geek.sylvainw.com/2010/03/28/using-notepad-to-quickly-format-xml/ My favorite way to quickly ...
- 通过notepad++将混乱的xml配置的格式进行美化
需求描述: 最近在进行hbase配置文件的修改之后,发现xml文件的格式很不美观, 然后,在网上找了些方法,实测,通过notepad++的xml tools插件就可 达到美化效果. 操作过程: 1.以 ...
- Notepad++安装xml插件
环境: win7 64位 Notepad++7.3.3 原生的Notepad++不自带xml文件的插件,所以在显示xml文件时并不分行(如下图所示),对于用户编辑,查看的操作而言,并不友好,所以需要安 ...
- Android Auto开发之一《开始学习Auto 》
共同学习,共同进步, 转载请注明出处.欢迎微信交流:sfssqs,申请注明"Android Car"字样 ================= =================== ...
- Notepad++编写Markdown
Markdown语法高亮 下载userDefineLang_markdown.xml 打开Notepad++的 Language 菜单,选中底部的 Define your language... 在 ...
- Notepad++配置Python开发环境
1. 安装Python 1 下载 我选择了32位的2.7版本.https://www.python.org/ftp/python/2.7.8/python-2.7.8.msi 2. 安装 安装的时候可 ...
- 实战android菜单项之XML加载菜单与动态菜单项[转]
原文地址:http://blog.csdn.net/kaiwii/article/details/7767225 自定义android应用程序的菜单项首先要知道切入点.经过学习,知道主要是两个Acti ...
- 【Markdown】notepad++ 支持 markdown语法、预览
Notepad++中支持Markdown 最近在学习Markdown语言的使用,很想在XP主机上使用Markdown的离线编辑器,但MarkdownPad.作业部分的离线客户端都不能再XP上运行, ...
- xml 和 json 的区别
JSON(Javascript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于Javascript Programming Langu ...
随机推荐
- @SpringBootApplication
1. spring 文档说明 Many Spring Boot developers always have their main class annotated with @Configuratio ...
- videoconverter转换
以前录制的avi用vfw可以解码的,但是现在变成win7系统了,无法解码了.只好用视频转换软件把avi转成无压缩的. 选择losses uncompressed avi,点进去选UYVY就行了.
- linux批量复制或删除同命名规则的文件
如图所示,有多个不同后缀的文件,但他们的前缀都是"QC_TZ.impute." 此时想批量复制图中的文件的话,可以考虑用命令行 cp QC_TZ.impute.* /your/de ...
- DNX SDK版本 “dnx-clr-win-x86.1.0.0-beta5”无法安装
打开VS2015,出现 “DNX SDK版本 “dnx-clr-win-x86.1.0.0-beta5”无法安装,该解决方案将对此会话使用DNX SDK版本“dnx-clr-win-x86.1.0.0 ...
- jQuery Mobile 网格布局
jQuery Mobile 布局网格 jQuery Mobile 提供了一套基于 CSS 的列布局方案.不过,一般不推荐在移动设备上使用列布局,这是由于移动设备的屏幕宽度所限. 但是有时你需要定位更小 ...
- Java(多态)动手动脑
1> 请看以下"变态"的类(参看示例ParentChildTest.java) 上述代码的特点是: 子类和父类定义了一模一样的字段和方法 运行以下测试代码 1. 上边的程序运 ...
- 反射,System.Type类
http://m.blog.csdn.net/blog/woddle/40623333 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到 ...
- GO_order
Gpos Q8RP81 有GO:0000015 P47437 有GO:0000049 P06535 有GO:0000155 Q99027 有GO:0000160 P35594 有GO:0000166 ...
- CentOS 6.5 RPM包方式安装 Mysql 5.6
1. 下载MySQL 5.6 下载页面:http://dev.mysql.com/downloads/mysql/此处选择“Red Hat Enterprise Linux 6 / Oracle Li ...
- 记一次PHP7+opcache+zmq出现SIGSEGV 问题的查找(一次不成功的bug查找)
Title: 记一次PHP7+opcache+zmq出现SEGSEGV问题的查找(一次不成功的bug查找) bug来历自述:线上代码PHP环境是5.2,为了提升性能(逼格),于是升级为PHP7并使用 ...