JEDIT应用指南【基础篇】

94 views
Skip to first unread message

pi1ot

unread,
Jan 19, 2009, 11:29:08 AM1/19/09
to jEdit中文用户组
jEdit应用指南【基础篇】
作者:tedeyang
创作时间:2005年9月24日


jEdit是一个非常强大和灵活的文本编辑器.在java开发过程中,我一直使用eclipse,UltraEdit和jEdit.经常在
它们之间来回切换.因为ultraEdit太简单,而eclipse又太复杂,所以现在我要介绍的就是简单又复杂的jEdit,它将大大加速你的编程,
我会将重点放在其内嵌的Beanshell脚本上。

第一章 jEdit是Java编写,强大,易用的程序员文本编辑器
以下部分介绍是我从其官方网站翻译来的:

jEdit是一个成熟的,设计优秀的程序员文本编辑器,已经有了7年的开发历史。在功能和易用性方面压倒许多昂贵的开发工具时,jEdit
在GNU公用许可证(GPL)下发布成了开源软件。

jEdit的核心主要由Slava Pestov开发完成,一支由世界各地的程序员组成的队伍正在为jEdit开发插件(plugin)。

下面是jEdit的几个特色:

用java编写,所以它可以运行在Mac OS X, OS/2, Unix, VMS 和Windows平台上(山颠:非java程序员不推荐使
用)
内建宏语言;可扩展的插件体系;目前已经有了很多宏和插件.(山颠:内建BeanShell脚本语言,通过插件可以支持其他脚本语言.这点是我之所以在
这里费力的原因 ^_^)
使用jEdit的插件管理器可以下载插件并安装.(山颠:如果网络状况允许,这个功能确实非常好.不过建议想这么干的家伙去sf看看那些插件都是干什么
的先,全装了会影响性能)
提供超过130总编程语言的自动缩进和语法高亮.(山颠:很棒.据我所知,BeanShell的高亮显示好象只有它支持)
支持UTF8和Unicode在内的大量字符编码
代码折叠
自动换行
极高的可配置性和可定制性
所有其他你希望在一个文本编辑器里找到的功能,不管是基础性的还是高级的,你都可以在jEdit中找到.请查看全部功能清单
看看这些截图,从直观上感受一下jEdit:http://www.jedit.org/index.php?page=screenshots

是啊,千变万化的jEdit。融合VIM,EditPlus,Emacs等编辑器的优点于一身。请查看维基百科中的文本编辑器比较。在这个有些片面(显
然测试的是个阳春版本,没有加任何插件)的比较中,jEdit的表现仍然很是抢眼,全面超越它的只有Emacs(这个超级难用的程序)。作为一个
Java程序员,我找遍了整个地球,在苦恼地承认自己的智商还不足以轻松学好Emacs或Vim后欣喜地发现了:开源、强大、易用、Java编写的
jEdit正是我要的。
相关:从其他编辑器转移到jEdit http://community.jedit.org/cgi-bin/TWiki/view/Main/SwitchingToJEdit


第二章 jEdit的下载、安装和插件配置
jEdit的官方主页是www.jedit.org,目前最高版本是4.3,稳定版本是4.2final。因为有些插件还不支持4.3,因此建议
下载jEdit4.2版本。jEdit还需要JDK1.4的支持

开源项目网址:http://sourceforge.net/projects/jedit/
官方网站下载页面:http://www.jedit.org/index.php?page=download
用户指南:http://jedit.org/users-guide/
安装FAQ:http://www.jedit.org/FAQ/installation.html#id2877328
官方Wiki:http://community.jedit.org/cgi-bin/TWiki/view/Main/WebHome
Note:jEdit的各个版本都有平台独立的版本和相应平台的版本。不过针对windows用户,jEdit还提供了一个启动器
jEditLauncher。(目前官方已经不提供启动器了,相关的讨论见:http://community.jedit.org/?q=node/
view/1411,其实以前的启动程序还是能用的,下载我使用的启动程序包,释放到jedit安装目录下就行了)

安装完jEdit,下一步就是安装插件。和Eclipse类似,jEdit的插件安装也有两种途径:直接在jEdit中升级和手工下载插
件包。建议选择直接升级。

1. 直接升级。

按Plugins->Plugin Manager打开插件管理器,进入Install,选择你想装的插件,点击install就可以安装了。注意,一
次安装不要选中太多插件,否则这个过程将持续很长时间。安装完成后可以在Plugins->Plugin Options中进行配置。

2. 手工安装

jEdit插件是个开源项目,见http://sourceforge.net/projects/jedit-plugins/

在官方主页上也有这些插件的介绍:http://plugins.jedit.org/list.php。仔细的看看插件的说明,确定你需要的。下载后
把压缩包后解压缩到jEdit安装目录的jars目录中(确保有jar文件在jars目录下,如果你下载的是完全版的插件,jars目录下还会有个包含
源文件的同名文件夹),之后重启jEdit。

Note:必备的几个美化界面的插件是:Buffer Tabs(类似UltraEdit的标签)、Background(为View――编辑区添加背
景图片)、LookAndFeel(外观选择)。如果有需要,其他插件会在后续文章中按用途进行介绍。

第三章 Beanshell脚本在jEdit的应用
第一节 强大、简单、独到的宏
在目前的Java IDE领域中,Eclipse是最好的。但jEdit并不是一个IDE,而是“程序员文本编辑器”。在文本编辑领域,jEdit拥有
很多独到的优势,其中之一,也是作者最感兴趣的部分,就是它的宏——其实就是Beanshell脚本。内嵌的Beanshell引擎能够直接访问
jEdit的内部对象和API,例如:你可以写出这样的宏命令:

buffer.undo(textArea); //撤销当前buffer的一个操作

Registers.paste(textArea,'$',false);//粘贴到当前文本域

textArea.setSelectedText(" ");//将选定文本设为“ ”

textArea.goToNextLine(false);//移动光标到下一行

Registers.copy(textArea,'$');//copy

textArea.showWordCountDialog();//显示对话框,统计字数

textArea.backspace();//按一下后退键

buffer、textArea都是jEdit的内部对象,Registers是jEdit中的寄存器类。可见Beanshell脚本具
有直接访问jEdit内部资源的能力,这使我们写出一个高度自动化的宏命令成为可能。
除了手工编写宏文件(.bsh文件)外,jEdit也可以录制宏。 录制宏有三种途径:
1.使用菜单:打开 Macros->Record Macro,进行操作,完成时点击stop Recording。
2.使用快捷键:开始录制用c+m c+r,结束录制用c+m c+s。(其中c+m c+r表示先按Ctrl+e,再按Ctrl+r,也
可以按住Ctrl,然后再接着按m和r键)
3.使用action bar:Ctrl+Enter组合键打开action bar,输入record,按Tab键,回车,就开始录制
宏。完成后打开action bar,输入recording,按tab,回车。
Note:多种使用方式体现出jEdit强大的操作性;有关action bar的相关资料,请查看帮助。
Note:宏文件必须存放在jedit\macros\目录或\Documents and Settings\uername
\.jedit\macros目录下,前者是系统宏目录,后者是用户宏目录。


第二节 使用 jEdit中的Beanshell编写宏
到了这一步,看来我们又要学一门新的语言了。“太麻烦了!”,你可能已经决定放弃jEdit了。幸好,Beanshell是Java阵营的脚本
语言,我们基本上不需要什么精力就学会怎么在jEdit中使用它——当然前提你必须熟悉Java,这就是我在前面推荐Java程序员使用jEdit的原
因。

Beanshell 有脚本语言的特性,如弱类型、闭包等,但同时也完全兼容Java语法。为了简便起见,我们这里就象java一样来写
beanshell,只要记住一点:它是解释执行的。更深入的学习推荐访问以下网站:

1. http://dev.csdn.net/develop/article/15/15090.shtm

2. http://www-128.ibm.com/developerworks/cn/java/l-beanshell/index.html

3. 官方网站:http://www.beanshell.org

4. 在线教程:http://www.beanshell.org/manual/contents.html

下面通过jEdit自带的一个宏文件来看看宏是怎么“炼成”的。
实例:jEdit 4.2\macros\java\Java_File_Save.bsh

文件源代码如下:中文部分是添加的注释

/*
* Java_File_Save.bsh - a BeanShell macro for saving new java files.
*
* Copyright (C) 2004 Nicholas O'Leary n...@deferential.net
*
* :mode=beanshell:tabSize=3:indentSize=3:maxLineLen=0:noTabs=true:

* :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
*
*
* Notes:
* Only the first 250 lines of the buffer are scanned for a suitable
* class or interface declaration.
*
* Changes:
* 17-May-04: Only scans if the edit mode is either 'java' or the
default mode
* : Ignores declarations that are in multiline comments
* 08-Jun-04: If an infinite loop is hit (1000 iterations) in the
comment
* : parsing, it now opens the default save dialog, rather
than
* : just returning.
* $Id: Java_File_Save.bsh,v 1.1 2004/06/26 19:10:58 spestov Exp $
*/
//以上是宏作者写的说明

// Check this is a new file。
// 在jEdit中buffer对象指向当前正在编辑的文件,打开一个文件,就是在当前视图(view)的textArea中创建一个buffer
// 在帮助中可以查看这些类的API:对象buffer: org.gjt.sp.jedit.Buffer,
// 对象textArea:
org.gjt.sp.jedit.textarea.JEditTextArea
// 对象view: org.gjt.sp.jedit.View
// beanshell能访问的内存中的对象有view,buffer,textArea,editPane,wm,scriptPath,
// 详情请看帮助中的 Chapter 13. Macro Basics-->Predefined Variables in
BeanShell小节
if (buffer.isNewFile() && buffer.getPath() != null)
{
// Only look further if the mode is 'java', or still the default
String buffer_mode = buffer.getMode().toString();//编辑模式,jEdit的默认编辑模式
是text
if (buffer_mode.equals("java") || buffer_mode.equals
(jEdit.getProperty("buffer.defaultMode","")))
{
String fullpath = buffer.getPath();//打开文件的路径
VFS vfs = VFSManager.getVFSForPath(fullpath);//根据路径生成虚拟文件系统
// Split into constituent parts
String path = vfs.getParentOfPath(fullpath);//获得所在目录
String name = vfs.getFileName(fullpath);//获得文件名

// At most, check the first 250 lines - this sounds reasonable
to me
//以下利用GNU的正则表达式包检查代码,提取出类名(最多检查250行)
int maxLine = Math.min(buffer.getLineCount(),250);
import gnu.regexp.RE;
import gnu.regexp.REMatch;//这样引入包,jar文件必须能被jEdit找到(只要将jar文件放在jars
目录下)
// Build the regex - based on the offical java language spec.
RE regex = new RE("^\\s*(public|protected|private|static|
abstract|final|native|synchronized|transient|volatile|strictfp)?\\s*
(class|interface)\\s*([^ {/]*)");
int regexMinimum = regex.getMinimumLength();//可能构成一次匹配的最小字符数
boolean inComment = false;//是否在注释里
for(int i=0;i<maxLine;i++)
{
String txt = buffer.getLineText(i);//第i行的字符串
int count = 0;
// See if this line has a the start or finish of a
multiline comment
//确定提取的类名是有效的,不在注释里
while (txt.indexOf("/*")!=-1 || txt.indexOf("*/")!=-1)
{
// A little paranoia on my part
count++;
if (count==1000)//最多执行1000次,如果这行长度超过2000就不管了,直接保存为默认文件名
{
Log.log(Log.ERROR,BeanShell.class,"Infinite loop:["+txt
+"]");
//log: org.gjt.sp.util.Log jEdit的log系统
buffer.save(view,null,true);//以默认名保存
return;//结束脚本执行
}
// Look for the next starting comment if we're not in
a comment
if (!inComment)
{
int commentStartIndex = txt.indexOf("/*");
if (commentStartIndex != -1)
{
inComment = true;
if (commentStartIndex+2 == txt.length())
txt = "";
else
txt = txt.substring(commentStartIndex+2);
}
}
// Look for the next ending comment if we are in a
comment
if (inComment)
{
int commentEndIndex = txt.indexOf("*/");
if (commentEndIndex != -1)
{
inComment = false;
if (commentEndIndex+2 == txt.length())
txt = "";
else
txt = txt.substring(commentEndIndex+2);
} else {
continue;
}
}
}

// We now know if the remainder of the line is in a
comment or not
if (!inComment)
{
// Ignore lines that are too short for the regex
if (txt.length() < regexMinimum)
continue;
REMatch match = regex.getMatch(txt);
// See if it matches
if (match!=null)
{
int startIndex = match.getStartIndex(3);
int endIndex = match.getEndIndex(3);
// Extract the class/interface name 得出第三个匹配,就是类名
name = txt.substring(startIndex,endIndex)+".java";//文件名
break;
}
}
}

// Open the VFSBrowser
String[] files = GUIUtilities.showVFSFileDialog(view,path+name,

VFSBrowser.SAVE_DIALOG,false);
if(files == null)
return false;
buffer.save(view,files[0],true);
return;
}
}

// This isn't a file that has been scanned, so just do a normal save
buffer.save(view,null,true);

/*

Macro index data (in DocBook format)

<listitem>
<para><filename>Java_File_Save.bsh</filename></para>
<abstract><para>Acts as a wrapper script to the Save As action.
If the buffer
is a new file, it scans the first 250 lines for a Java class or
interface
declaration. On finding one, it extracts the appropriate
filename to be
used in the Save As dialog.</para></abstract>
</listitem>

*/
这个bsh脚本能自动得到文件中的类名,以此作为文件名的前缀保存。要执行这个文件,点击Macros-Java-
java_File_save。要快速执行,可以设定快捷键。打开Utilities-Global option,选择shortcuts-
macros进行快捷键设置。
jEdit自带的Macro就是很好的例子,配合帮助中的API,beanshell宏是很容易上手的。

Reply all
Reply to author
Forward
0 new messages