File: Emacs_Transpose_Lines.bsh

package info (click to toggle)
jedit 5.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,252 kB
  • ctags: 11,190
  • sloc: java: 98,480; xml: 94,070; makefile: 52; sh: 42; cpp: 6; python: 6
file content (45 lines) | stat: -rw-r--r-- 1,160 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * Transpose line at caret with previous line, and move caret to next line.
 *  Emulates Emacs "transpose-lines" command (without prefix argument support).
 */

source (MiscUtilities.constructPath(dirname(scriptPath), "EmacsUtil.bsh"));

void emacsTransposeLines()
{
    caret = textArea.getCaretPosition();
    if ((caret == 0) || atEndOfBuffer())
    {
        beep();
        return;
    }

    caretLine = textArea.getCaretLine();
    lineStart = textArea.getLineStartOffset (caretLine);
    lineEnd = textArea.getLineEndOffset (caretLine);
    lineIndex = textArea.getLineOfOffset (caretLine);

    selection = new Selection.Range (lineStart, lineEnd);
    line = textArea.getSelectedText(selection);

    // Get the location of the previous line

    textArea.goToPrevLine (false);
    prevLineCaret = textArea.getCaretLine();
    prevLineStart = textArea.getLineStartOffset (prevLineCaret);

    // Go back to the original location.
    
    textArea.setCaretPosition (caret);
    
    // Delete the line
    
    textArea.deleteLine();
    
    // Insert it in the new location.

    buffer.insert (prevLineStart, line);
}

emacsTransposeLines();