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 ...
随机推荐
- 重复数据分析的三个常用语法distinct, group by, partition by
由于数据经常会出现重复现象,数据去重是数据分析中常用操作之一,而distinct,group by, partition by三者都可以实现去重功能,实践中需要根据不同的场景需求来选取相应的语法. d ...
- C# 委托和事件 与 观察者模式(发布-订阅模式)讲解 by天命
使用面向对象的思想 用c#控制台代码模拟猫抓老鼠 我们先来分析一下猫抓老鼠的过程 1.猫叫了 2.所有老鼠听到叫声,知道是哪只猫来了 3.老鼠们逃跑,边逃边喊:"xx猫来了,快跑啊!我是老鼠 ...
- awk 统计出现次数--转
知识点: 1)数组 数组是用来存储一系列值的变量,可通过索引来访问数组的值. Awk中数组称为关联数组,因为它的下标(索引)可以是数字也可以是字符串. 下标通常称为键,数组元素的键和值存储在Awk程序 ...
- php 验证码
$im =imagecreate(500,500); $bak =imagecolorallocate($im,200,100,0); $shk = imagecolorallocate($im,0, ...
- 统计黑ip的脚本
#!/bin/bash > ] do cd /var/openresty/nginx/logs#统计nginx日志,将结果重定向到文件blackip.txt中 cat access.log|gr ...
- C/C++ 记录时间
http://stackoverflow.com/questions/2808398/easily-measure-elapsed-time https://github.com/picanumber ...
- 安装好grunt,cmd 提示"grunt不是内部或外部命令" 怎么办?
Grunt和所有grunt插件都是基于nodejs来运行的,因此,必须安装node.js. (一) 去官网http://nodejs.org/ 下载安装包 node-v6.9.2.msi,直接点击安装 ...
- 关于onmouseover和onmouseout的bug
总结了一下关于使用onmouseover以及onmouseout会出现的bug 首先简单的布局: <div id="box"> <div>这是一个内容< ...
- WinForm开发之取送货管理1
一.取送货管理项目需求 该系统的业务背景如下:客户是一个针织半成品生产加工作坊,有很多生产加工人员从客户工厂那里取走半成品,加工成成品后送回来.客户根据加工每种半成品的加工单价和完成数量,付费用给生产 ...
- maven 三个基本插件 clean dependency compiler
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...