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 ...
随机推荐
- 后台dom拼接xml,动态显示统计图
这个东西让我好生头疼,贴代码吧 // 两秒后模拟点击 setTimeout(function() { // IE if(document.all) { document.getElementById( ...
- Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 64 bytes) in D
Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 64 bytes) in D 从数据库 ...
- ios xib或storyBoard的那些小方法
今天看了一下xib里的一些小技巧,但是百度一搜的话,网上已经有人写过教程了,在这里我也就懒一下,不写那么详细了,就写一些如何百度的方法! 1."通过KVC修改占位文字的颜色" [t ...
- c语言折半查找
折半查找又称为二分查找,它的前提是线性表中的记录必须是有序的(通常从小到大有序),线性表必须采用顺序存储. 折半查找的基本思想是 : 在有序表中,取中间记录作为比较对象,若给定值与中间记录的关键字相等 ...
- yarn container启动失败
在yarn资源管理的集群上运行spark程序,无法读取的数据多与少,都会报这个错误,但是其他程序在集群上能够正常运行. 16/11/14 00:13:44 WARN cluster.YarnSched ...
- python课程第一周重点记录
- js(ext)中,设置[!!异步!!]上传的简单进度条
代码在updateHmis的历史记录中,此处存档 handler : function() { //显示进度条 Ext.MessageBox.wait('数据上传中...','提示'); //上传数据 ...
- [转]C#在创建完项目后如何重命名项目名称。
今天写了个C#的小测试程序,一开始使用的默认命名WindowsFormsApplication2,写完后觉得名字不好看,于是想改个名字,但是试了一下,想完整的改名还挺复杂,不但要改解决方案名,项目名, ...
- C语言的关键字,标示符以及数据类型
1. 关键字 1> 关键字就是C语言提供的有特殊含义的符号,也叫做“保留字” 2> C语言一共提供了32个关键字,这些关键字都被C语言赋予了特殊含义 auto doub ...
- 一个书店管理系统java
自己的第一个小程序 ps:书是在集合里面后面文件处理的有一点小问题,希望有人会给点意见 //客户类 import java.io.Serializable; public class Customer ...