Macros for the JED Text Editor

Author: Dave Kuhlman
Address:
dkuhlman@rexx.com
http://www.rexx.com/~dkuhlman
Revision: 1.0a
Date: Oct. 12, 2005
Copyright: Copyright (c) 2003 Dave Kuhlman. This documentation is covered by The MIT License: http://www.opensource.org/licenses/mit-license.

Abstract

This document describes and provides a number of macros that I use to customize the JED text editor.

Contents

1   JED

JED is a powerful but light-weight text editor. I've used a variety of text editors, and JED is my favorite.

Here is the description given at the JED home page:

"JED is a freely available text editor for Unix, VMS, MSDOS, OS/2, BeOS, QNX, and win9X/NT platforms. Although it is a powerful editor designed for use by programmers, its drop-down menu facility make it one of the friendliest text editors around. Hence it is ideal for composing simple email messages as well as editing complex programs in a variety of computer languages."

The JED home page is here: http://www.jedsoft.org/jed/.

One powerful feature of JED is the ability to customize and extend JED with its macro language S-Lang.

This document provides a number of macros that I've written and that I use.

You can install any or all of them by copying and pasting the code provided at the end of your .jedrc file. While I find the key bindings provided convenient, you should change these bindings to keys that suit your own use.

There is a file containing the code described and listed in this file here: http://www.rexx.com/~dkuhlman/dkuhlman_jedmacros.zip.

2   Switch to Previous and Next Buffer

This macro switches to the previous or the next buffer in the buffer ring.

It skips buffers in which we have no interest, for example *scratch*. If you do want to be able to switch to those buffers, modify the if statement after the comment.

%
% Switch to previous or next buffer.
%   This code originated in ide_next_buffer() in ide.sl.
%
define next_buffer (previous)
{
    variable n, buf;
    n = buffer_list (); % get the buffers on the stack
    if (previous)
    {
        _stk_reverse (n-1);
    }
    loop (n) {
        buf = ();
        n--;
        % Skip *scratch* and other buffers that are not of interest.
        if ((buf[0] == '*') or (buf[0] == ' '))
        {
            continue;
        }
        sw2buf (buf);
        _pop_n (n);
        return;
    }
}

% Bind Alt-. and Alt-, (Meta-period and Meta-comma) to next_buffer().
setkey ("next_buffer (0)", "\e,");
setkey ("next_buffer (1)", "\e.");

3   Jump to Previous/Next Occurence of Current Word

This macro will search forward or backward for the next occurance of the word that the cursor is on. It skips occurances of the word within a longer word. Note that it requires the txtutils extensions to JED (Tools for text processing (marking, string processing, formatting). You can find txtutils at http://jedmodes.sourceforge.net/mode/txtutils/.

autoload("get_word", "txtutils");

define search_current_word(direction)
{
    variable word;
    variable mark;
    variable result;
    variable s1;
    mark = create_user_mark();
    skip_word();
    bskip_word();
    word = get_word();
    if (direction == 1)
    {
        while (1)
        {
            go_right_1();
            result = fsearch(word);
            if (result == 0)
            {
                break;
            }
            % Search again if the word is a substring of a longer word.
            s1 = get_word();
            !if (strcmp(s1, word))
            {
                break;
            }
        }
    }
    else
    {
        while (1)
        {
            result = bsearch(word);
            if (result == 0)
            {
                break;
            }
            % Search again if the word is a substring of a longer word.
            s1 = get_word();
            !if (strcmp(s1, word))
            {
                break;
            }
        }
    }
    if (result == 0)
    {
        goto_user_mark(mark);
        message(word + " not found");
    }
}

% Bind to Alt-z+Alt-comma (alt-z then alt-comma) and Alt-z+Alt-period.
setkey ("search_current_word(-1)", "\ez\e,");
setkey ("search_current_word(1)", "\ez\e.");

3.1   Word characters

Note that the behavior of search_current_word is influenced by define_word(). Since the programming language that I typically use (Python) includes underscores as a word character, I added the following to my .jedrc:

define_word("_A-Za-z0-9");

For your typical usage, a slightly different set of word characters may be more suitable. Also be aware that define_word() also affects the behavior of things like kill_word, skip_word, bskip_word, and possibly others.

4   Copy Word or Line

The first two macros copy the current word or the current line respectively to the kill ring. The third macro duplicates a line, then places the cursor on the same column in the duplicated line. Note that copy_word_as_kill also requires get_word and mark_word from txtutils. See above.

autoload("yp_copy_region_as_kill", "yankpop");
autoload("mark_word", "txtutils");

define copy_word_as_kill()
{
    variable s1;
    push_spot();
    mark_word();
    () = dupmark();
    s1 = bufsubstr();
    yp_copy_region_as_kill();
    pop_spot();
    message("\"" + s1 + "\" copied to kill ring");
}

define copy_line_as_kill()
{
    variable s1;
    push_spot();
    bol();
    set_mark_cmd();
    down(1);
    yp_copy_region_as_kill();
    pop_spot();
    message("line copied to kill ring");
}

define duplicate_line()
{
    variable col;
    push_spot();
    col = what_column();
    bol();
    push_mark();
    down_1();
    yp_copy_region_as_kill();
    yp_yank();
    pop_spot();
    down_1();
    goto_column(col);
}

setkey ("copy_word_as_kill()", "\eze");
setkey ("copy_line_as_kill()", "\ezr");
setkey("duplicate_line", "\ezl");

5   Macros for Docutils/reST

These macros are useful when editing restructured text (reST), which is the input for the Docutils text formatting system, which is implemented in Python.

You can learn more about Docutils here: http://docutils.sourceforge.net/ and here http://sourceforge.net/projects/docutils/.

The first macro does automatic line wrapping for lines that start a bullet list or an enumerated list.

The second macro provides a quick way to "adorn" (underline or under- and over-line) a section title. The default adornment character is an equal sign, but you are asked for a replacement. If any argument is entered before executing this macro, the adornment characters are added both over and under the current line. Without an argument, only underlining is performed. Note: On my system, an argument is entered, for example, by pressing ESC-1.

Installation -- See notes after the code.

% Line wrapping for lines that start a bullet list or enumerated list.
% See doc/txt/hooks.txt.
% This code originated from hooks.txt.
%
static define text_mode_wrap_hook ()
{
    variable p;
    push_spot ();
    go_up(1);
    bol ();
    skip_white ();
    p = what_column ();
    if ((looking_at("- ")) or
        (looking_at("* ")) or
        (looking_at("+ "))
        )
    {
        go_down(1);
        bol ();
        skip_white ();
        p = what_column ();
        bol ();
        trim ();
        whitespace (p + 1);
    }
    else
        if (
            (looking_at("1. ")) or
            (looking_at("2. ")) or
            (looking_at("3. ")) or
            (looking_at("4. ")) or
            (looking_at("5. ")) or
            (looking_at("6. ")) or
            (looking_at("7. ")) or
            (looking_at("8. ")) or
            (looking_at("9. "))
            )
        {
        go_down(1);
        bol ();
        skip_white ();
        p = what_column ();
        bol ();
        trim ();
        whitespace (p + 2);
        }
    pop_spot ();
}

%
% Underline (adorn) a title (Docutils style).
% With any prefix argument, also add adornment *above* the title.
%
define txt_adorn_title()
{
    variable loc1;
    variable loc2;
    variable idx;
    variable char_count;
    variable adornment_char;
    variable arg;
    arg = prefix_argument(-1);
    adornment_char = read_mini("Enter adornment character:", "=", "");
    bol();
    loc1 = what_column();
    eol();
    loc2 = what_column();
    char_count = loc2 - loc1;

    if (arg != -1)
    {
        bol();
        for (idx = 0; idx < char_count; idx++)
        {
            insert(adornment_char);
        }
        insert("\n");
        eol();
    }
    insert("\n");
    for (idx = 0; idx < char_count; idx++)
    {
        insert(adornment_char);
    }
    insert("\n\n");
}

5.1   Installation

To install these for JED's text mode, add something like the following to your global_mode_hook or to your text mode hook. Again, choose your own key bindings.

define global_mode_hook (hook_name)
{
    o
    o
    o
    else if (hook_name == "text_mode_hook")
    {
        local_setkey("txt_adorn_title", "\ezt");
        set_buffer_hook ("wrap_hook", &text_mode_wrap_hook);
    }
    o
    o
    o
}

6   Load File Under Cursor

If the cursor is on a path/file name that exists on your file system, then this macro will load that file into a buffer.

define load_file_under_cursor()
{
   variable s1;
   variable wordchars1;
   push_spot();
   wordchars1 = "-_A-Za-z0-9./";
   mark_word(wordchars1);
   %() = dupmark();
   s1 = bufsubstr();
   %yp_copy_region_as_kill();
   pop_spot();
   find_file(s1);
   message("\"" + s1 + "\" opened");
}

setkey("load_file_under_cursor", "\ezf");

And, the following version has the side-effect of copying the file name to the copy/paste buffer:

define load_file_under_cursor()
{
   variable s1;
   variable wordchars1;
   push_spot();
   wordchars1 = "-_A-Za-z0-9./";
   mark_word(wordchars1);
   () = dupmark();
   s1 = bufsubstr();
   yp_copy_region_as_kill();
   pop_spot();
   find_file(s1);
   message("\"" + s1 + "\" opened");
}

setkey("load_file_under_cursor", "\ezf");

"Find File At Point" is a more sophisticated implementation of a similar capability. You can find it at: http://jedmodes.sourceforge.net/mode/ffap/

7   See Also

The JED home page: For more information about JED, links to JED macros and mode implementations, documentation, information about S-Lang (the macro language for JED), etc.

Docutils: Documentation Utilities: For more information on Docutils.