Kohei Nozaki's blog 

Entries tagged [jedit]

jEdit conditional line deleting with regex


Posted on Sunday Jan 31, 2016 at 06:03PM in Technology


How to delete lines that contains a keyword?

Open Search And Replace dialog and turn Regular expressions on then put ^.*KEYWORD.*$\n to Search for box, leave Replace with blank then hit Replace All.

How to delete lines that NOT contains a keyword?

With ^((?!KEYWORD).)*$\n do the same to the above. for detail check http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word


My jEdit setup on OS X


Posted on Sunday Jan 31, 2016 at 04:00PM in Technology


Make Option key work

Put a file contains following line to $HOME/Library/jEdit/startup so shortcuts that used Option work.

Debug.ALT_KEY_PRESSED_DISABLED = false;

Check http://www.nailedtothex.org/roller/kyle/entry/installing-and-configuring-jedit-5 for further information about setup jEdit on OS X.

Basic configurations

Just for my preferences:

  • Go to View section

    • check Show full path of buffer in title bar

  • Go to Editing section

    • set Word wrap to soft

    • set Wrap margin to 100

    • check Soft (emulated with spaces) tabs

  • Go to Toolbar

    • uncheck Show tool bar

  • Go to Text Area

    • check Caret: thick

Customize shortcut

First, choose keymap Mac OS X. Then customize maps that unusable by default because OS X takes precedence.

  • Incremental Search: CMD+, launches application’s preferences window. so I bind it to Option+, instead.

Note: Draw multi-key shortcuts on screen menu bar option makes some shortcut such as move to dockables unusable, I don’t know why.

Basic operations

Macros

From Macros ⇒ New Macro, you can create a BeanShell macros. For example, the macro below puts current date to the position of caret:

textArea.setSelectedText(java.time.LocalDate.now().toString());

Project Viewer

This plugin enables managing set of files in a unit named project. imagine the same name thing that implemented in some IDEs for Java development such as Eclipse or NetBeans.

There are some plugin that requires it. For example, FastOpen is a companion plugin for Project Viewer that enables open files fast with keyboard. I’ve set Delay before searching option to the smallest value (0.5sec).

The documentation of this plugin can be obtained from http://plugins.jedit.org/plugindoc/ProjectViewer/

Console

This plugin integrates console as a jEdit dockable.

Its advantages are commands that for invoking jEdit’s functionalities. for example, %edit foo.txt opens a file as a jEdit buffer. another useful one is redirecting output to a new jEdit buffer. for example, typing echo hello, jEdit in the console and hitting Ctrl+ENTER yields a new jEdit buffer with contents of hello, jEdit. also there are many useful variable syntax. e.g. ${f} for the current buffer’s path name. BeanShell console is available as well.

Also SshConsole Plugin extends its functionality to support remote hosts can be connected with SSH.

I recommend that check all three options in Console ⇒ System Shell ⇒ Events to chdir at Plugin Options.

The documentation of this plugin can be obtained from http://plugins.jedit.org/plugindoc/Console/

WhiteSpace

As its name states it make whitespaces, tabs or control characters visible. I recommend you to set to show leading and trailing tabs / spaces by this plugin.

TextTools

It provides some useful text operations, such as Toggle Range Comment. I recommend you to replace shortcuts for built-in ones by this plugin.

jDiff Plugin

It shows difference between two files pretty nicely as IntelliJ.

Others

Some worth considering plugins are:

  • FTP

  • SQL

  • XML

  • MarkerSets


BeanShell recipies


Posted on Sunday Jan 24, 2016 at 04:00PM in Technology


BeanShell is a handy lightweight scripting language for Java. In this entry, I’ll introduce you some useful snippets powered by BeanShell, and some recipies about it.

Setup and hello world

Grab a copy of bsh-2.0b4.jar from http://www.beanshell.org and put following shell script named bsh into your PATH:

#!/bin/sh
BEANSHELL_JAR=$HOME/Downloads/bsh-2.0b4.jar # replace path to suit your environment
java -cp $BEANSHELL_JAR bsh.Interpreter $@

Then fire up bsh from your console then just put print("hello, world!"); to confirm it works.

$ bsh
BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % print("hello, world!");
hello, world!

Hit Ctrl+D to exit interpreter.

You can launch your BeanShell script in a file as follows:

$ echo 'print("hello, world!");' > hello.bsh
$ bsh hello.bsh
hello, world!

Stdin

Text filtering script can be written as follows:

Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  System.out.println(line.toUpperCase());
}

Save this script as toUpperCase.bsh . The script can be executed as follows:

$ echo foo | bsh toUpperCase.bsh
FOO

Command line arguments

Command line arguments can be used as follows:

sb = new StringBuilder();
for (arg : bsh.args) {
    sb.append(arg);
}
print(sb);

Save this script as args.bsh. The script can be executed as follows:

$ bsh args.bsh foo bar
foobar

Use of external jar

Any external jar can be added via addClassPath clause dynamically. For example, a SQL beautifier script powered by a Hibernate class can be written as follows:

addClassPath("/path/to/hibernate-core-4.3.7.Final.jar"); // replace path to suit your environment
import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;

scanner = new Scanner(System.in);
sb = new StringBuilder();
while (scanner.hasNextLine()) {
  sb.append(scanner.nextLine()).append('\n');
}

beautifized = new BasicFormatterImpl().format(sb.toString());
print(beautifized);

Save this script as sql-beautifier.bsh then execute following command:

$ SQL="SELECT t0.content AS a2, t0.contenttype AS a3, t0.email AS a4 FROM roller_comment t0, weblogentry t1 WHERE ((t1.websiteid = 'f0588427-f2ca-4843-ac87-bbb31aa6013c') AND (t1.id = t0.entryid)) ORDER BY t0.posttime DESC LIMIT 31 OFFSET 0;"
$ echo $SQL | bsh sql-beautifier.bsh

This yields nicely formatted SQL:

SELECT
    t0.content AS a2,
    t0.contenttype AS a3,
    t0.email AS a4
FROM
    roller_comment t0,
    weblogentry t1
WHERE
    (
        (
            t1.websiteid = 'f0588427-f2ca-4843-ac87-bbb31aa6013c'
        )
        AND (
            t1.id = t0.entryid
        )
    )
ORDER BY
    t0.posttime DESC LIMIT 31 OFFSET 0;

Maven plugin

If you have Maven installed, you can execute any BeanShell script without obtaining bsh-2.0b4.jar by hand. Maven and the beanshell-maven-plugin takes care of it instead of you:

$ mvn com.github.genthaler:beanshell-maven-plugin:1.0:run -Dbsh.file="hello.bsh"
...
[INFO] --- beanshell-maven-plugin:1.0:run (default-cli) @ standalone-pom ---
[INFO] Executing Script
[INFO] file class java.lang.String
[INFO] script class java.lang.Object
[INFO] interpreting file hello.bsh
hello, world!

Note that you don’t need to create pom.xml to execute a simple BeanShell script.

For managing complex dependencies, you can leave that duty to Maven with pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sql-beautifier</groupId>
    <artifactId>sql-beautifier</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>com.github.genthaler</groupId>
                <artifactId>beanshell-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <script><![CDATA[
                    import java.nio.charset.Charset;
                    import org.apache.commons.io.FileUtils;
                    import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;

                    file = new File(System.getProperty("sql"));
                    sql = FileUtils.readFileToString(file, Charset.defaultCharset());
                    result = new BasicFormatterImpl().format(sql);

                    print(result);
         ]]></script>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-core</artifactId>
                        <version>4.3.7.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>commons-io</groupId>
                        <artifactId>commons-io</artifactId>
                        <version>2.4</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

Save the SQL you want to beautify as original.sql and executing following command yields similar result:

$ mvn bsh:run -Dsql=original.sql

jEdit integration

jEdit has pretty nice integration with BeanShell. You can integrate that SQL beautifier as a jEdit macro. Put following snippet as ~/Library/jEdit/macros/FormatSQL.bsh (for OS X) or create it with Macros → New Macro from jEdit menu bar:

addClassPath("/path/to/hibernate-core-4.3.7.Final.jar"); // replace path to suit your environment
import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;

sql = textArea.getSelectedText();
beautifized = new BasicFormatterImpl().format(sql);
textArea.setSelectedText(beautifized);

Paste SQL to any jEdit buffer, and select SQL statement and execute the macro with Macros → FormatSQL to trigger formatting.


Installing and Configuring jEdit 5.2pre1 on OS X


Posted on Friday Jan 30, 2015 at 10:59PM in Technology


Recently I installed jEdit 5.2pre1 to OS X 10.9 and got stucked in some problems so leave some notes. I’m using Japanese environment and front end processor so some situation may vary for each environment such as about option keys.

Force jEdit.app to use Java7

UPDATE: now jEdit 5.2.0 is released and it runs fine with Java8, so this workaround is no longer needed.

UPDATE2: I have some problems on jEdit 5.2.0 on Java8 such as keyboard tester or tab moving (dragging) so I recommend OS X users to use jEdit with Java7 at the moment.

First, jEdit 5.2pre1 requires Java7, and currently it doesn’t work with Java8 on OS X. the problem seems fixed in trunk though. so we need to run jEdit with Java7, but the mechanism named appbundle which used in jEdit.app, enables standalone Java application to run as OS X application, it uses Java8 unexpectedly on my environment. the cause seems to that it uses JRE installed under /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home as default. you better to check the version of JRE as follows:

kyle-no-MacBook:~ kyle$ /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
kyle-no-MacBook:~ kyle$

So, I need to force jEdit.app to use Java7. my solution is following:

  1. Create a directory named Plugins inside jEdit.app/Contents

    $ pwd
    /Applications/jEdit.app/Contents
    $ mkdir Plugins
  2. Create a symbolic link to JRE as follows:

    $ cd Plugins/
    $ ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk jdk7
    $ tree
    .
    └── jdk7 -> /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk
    
    1 directory, 0 files
  3. Put following definition to Info.plist:

    <key>JVMRuntime</key>
    <string>jdk7</string>

Also replacing the stub at /Applications/jEdit.app/Contents/MacOS/jEdit with alternative one would be an another solution.

Prevent blurry rendering in Retina Macs

  1. Put following definition to Info.plist:

    <key>NSHighResolutionCapable</key>
    <true/>
  2. Run following command:

    /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f /Applications/jEdit.app

Make CMD+ENTER work as show Action Bar

There are 2 solutions:

  • Use Metal theme instead of OS X theme

  • Delete CMD+ENTER from primary shortcut of Action Bar, then set it to alternative shortcut instead

The issue is filed but left open. anyway it mentioned more about the workaround so check it: jEdit / Bugs / #3863 Mac OS X L&F: problems with Action Bar

UPDATE

I’ve confirmed that CMD+ENTER is working as Show Action Bar on my environment (OS X theme, jEdit 5.3.0, Oracle JRE8u71 and OS X 10.11.3), BufferTabs tab replacing with mouse dragging as well.

Make Option key work

In OS X, option key is reserved for special symbols but I’ve never used option keys for such purpose, just want to use other purpose like binding a function as keyboard shortcut. so following procedure is needed:

  1. Put https://github.com/kenfox/intellij-emacs/blob/master/Coding.keylayout into $HOME/Library/Keyboard Layouts

  2. Register Coding keyboard layout which just downloaded at OS X’s System Preferences - Keyboard. this discussion may helps.

  3. Put a BeanShell script contains a line Debug.ALT_KEY_PRESSED_DISABLED = false; to $HOME/Library/jEdit/startup/startup.bsh

  4. Select Coding keymap in the icon which is placed upper right on OS X’s menu bar

  5. Test if option key works as expected in Utilities - Troubleshooting - Keyboard tester.

Some key mnemonic such as ones available in Search and Replace dialog will work with the combination of Option + Control + [mnemonic]. it not works with Option + [mnemonic]. for example you can toggle Regular expressions with Option + Control + x.

Notes about Mac OS X Support plugin

I disabled all of 4 functions of Mac OS X Support v1.3, but it’s still necessary because it handles quit event triggered by Cmd+Q. when I unloaded it then Cmd+Q stop asking whether I’m sure to quit with unsaved files.

Shell script for launching from CLI

Put this into your PATH as jedit.

#!/bin/sh
open -a /Applications/jEdit.app $@

If the one above doesn’t work, Try following instead:

#!/bin/sh
app_bundle=/Applications/jEdit.app
settings_dir=${HOME}/Library/jEdit
port_file=${settings_dir}/server

# Make sure that jEdit is running
if [ ! -f ${port_file} ]; then
    open -a jEdit
    while [ ! -f ${port_file} ]
    do
        sleep 1
    done
fi

# Pass the list of files to the running instance
$JAVA_HOME/bin/java \
    -jar ${app_bundle}/Contents/Java/jedit.jar \
    -nosplash \
    -reuseview \
    -settings="${settings_dir}" \
    "$@"

# Bring to front
open -a jEdit