jEdit应用指南【基础篇】

 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的高亮显示好象只有它支持)
  • 支持UTF8Unicode在内的大量字符编码
  • 代码折叠
  • 自动换行
  • 极高的可配置性和可定制性
  • 所有其他你希望在一个文本编辑器里找到的功能,不管是基础性的还是高级的,你都可以在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

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

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

  *
 * Copyright (C) 2004 Nicholas O'Leary nol@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 savebuffer.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宏是很容易上手的。

posted on 2015-09-22 19:54  荒荒9468  阅读(713)  评论(0编辑  收藏  举报