Difference between revisions of "Scripting: text editing"

From Freeplane - free mind mapping and knowledge management software
m (moved Scripting text editing to Scripting: text editing: Co0lon added to show it is part of a larger category)
m
Line 1: Line 1:
Feel free to add your own scripts there. If you give script a name using wiki text like
+
These are scripts which deal mostly with editing the text of nodes.
 +
 
 +
Some of them are also available in the editgoodies addon.
 +
 
 +
Feel free to add your own scripts here. If you give script a name using wiki text like
  
 
<pre><groovy name="yourScriptName">
 
<pre><groovy name="yourScriptName">

Revision as of 15:35, 7 December 2011

These are scripts which deal mostly with editing the text of nodes.

Some of them are also available in the editgoodies addon.

Feel free to add your own scripts here. If you give script a name using wiki text like

<groovy name="yourScriptName">
 your script
</groovy>

an extra download button is created for it, and it can be downloaded directly from this page.

For larger scripts there is a special Bazaar repository. Inspect it at contrib/groovy or get it via

 bzr branch bzr://freeplane.bzr.sourceforge.net/bzrroot/freeplane/contrib/groovy  [read only]
 bzr+ssh://USERNAME@freeplane.bzr.sourceforge.net/bzrroot/freeplane/contrib/groovy/ [developers only]


Set the color for all children

This is an example of iteration over child nodes.

<groovy name="blueChildren"> node.children.each{ it.style.nodeTextColor = java.awt.Color.BLUE } // @ExecutionModes({ON_SELECTED_NODE, ON_SELECTED_NODE_RECURSIVELY}) </groovy>

Transpose the two characters around the cursor (for bad typists)

Once this script is downloaded and placed in the script directory, it should be bound to a keystroke so that it can be called without moving from the keyboard. It is now also available as part of the Edit Goodies addon, as is the script below, to change case. That's much the easiest way to install it. This version should be considered a purely instructional example.

<groovy name="transpose"> // @ExecutionModes({ON_SINGLE_NODE})

// first we import the Java libraries needed // to access and manipulate the text of a node import java.awt.KeyboardFocusManager import javax.swing.JEditorPane

// Then wrap the action in a "try/catch" block so that it fails gracefully // though the record is written to a largely inaccessible log file, and should be displayed better

try {

    def focusOwner = KeyboardFocusManager.currentKeyboardFocusManager.focusOwner 

// these two lines check that the cursor is inside a node being actively edited

    if (focusOwner instanceof JEditorPane) { 
        JEditorPane editor = focusOwner 

// next, find the cursor position and save it in a variable

        cursor = editor.caretPosition - 1 

// Get a copy of the node text with all the html markup stripped out

        def oldstr = htmlUtils.htmlToPlain(editor.text)

// Make a new string with the letters around the cursor changed

        def newstr = oldstr.substring(0, cursor-1) + oldstr[cursor] + oldstr[cursor-1] + oldstr.substring(cursor+1) 

// And write it back into the node

        editor.text = newstr 

// finally, put the cursor back where it was

        editor.caretPosition = cursor + 1 
    } 
    else { 
        c.statusInfo = 'cannot transpose: inline editor not active' 
    } 

} catch (Exception e) {

   logger.severe('cannot transpose', e) 

} </groovy>

Author: user:seatrout with polish and editing by user:boercher

Change case of words, and sort bAD CApitalisation

This script, like the one above, is part of the edit goodies addon, available from the support site. If used from here, it should be bound to a key

<groovy name="caseChanger"> // @ExecutionModes({ON_SINGLE_NODE}) // @CacheScriptContent(true)


//import java.awt.Color import java.awt.KeyboardFocusManager import javax.swing.JEditorPane import java.text.BreakIterator // annotated a bit extra for instructional purposes // starts with wrapping everything in a "try" block so that it crashes gracefully

try {

   // first find the node we are working in
   def focusOwner = KeyboardFocusManager.currentKeyboardFocusManager.focusOwner 
   if (focusOwner instanceof JEditorPane) { 
       JEditorPane editor = focusOwner 
       cursor = editor.caretPosition-1 //Here we find the cursor

oldWord=editor.getSelectedText() plaintext=htmlUtils.htmlToPlain(editor.text) // and here strip out the html for manipulating the text isBigSelection=0 selectionStart=cursor //belt and braces to initialise this, but still // first, select the word the cursor is in, or merely hanging around // if there is no text presently selected if (oldWord==null){ BreakIterator bi=BreakIterator.getWordInstance() bi.setText(plaintext) // this next bit should be simple, but is complicated by the need // to do the right thing if the cursor is at either end of a word. // the next block captures the case if it is at the beginning of a word if (bi.isBoundary(cursor) && (bi.isBoundary(cursor-1))){ if (plaintext.substring(cursor-1,cursor) ==~/[A-z]/) { //Problem! It could be a single-letter word, in which case oldWord=plaintext.substring(cursor-1,cursor) selectionStart=cursor-2 } else { oldWord=plaintext.substring(cursor,bi.next(2)) } } else { oldWord=plaintext.substring(bi.preceding(cursor),bi.next()) selectionStart=bi.preceding(cursor) } } // or there may be text selected, perhaps several words. in that case ... else { selectionStart=editor.getSelectionStart() selectionEnd=editor.getSelectionEnd() oldWord=plaintext.substring(selectionStart-1,selectionEnd) isBigSelection=1 //ui.informationMessage(ui.frame, oldWord, "The selection is") } // either way, now we have a selection

               // and the next line does all the real work

newWord=cycleCase(oldWord)

// this next change changed to ensure that only one instance of a word is affected //(bug found in v 0.2)

editor.text=changeSelectedWordOnly(plaintext,selectionStart,oldWord,newWord)

               // now clean everything up and put the cursor back where it was 

if(isBigSelection==0){ editor.caretPosition = cursor+1 } else { editor.setCaretPosition(selectionStart) editor.moveCaretPosition(selectionEnd) }

}

   else { 
       c.statusInfo = 'cannot change case: inline editor not active' 
   } 

} catch (Exception e) {

   logger.severe('cannot change case', e) 

}

// acb routine to ensure only the selection is changed // could no doubt be more elegant but life is short

def changeSelectedWordOnly(text,start,oldWord,newWord){

   firsthalf=text.substring(0,start)
   secondhalf=text.substring(start)
   firsthalf+secondhalf.replaceFirst(oldWord,newWord)

}

// And the actual cycle case routine

def cycleCase(oldWord){ if (oldWord.toUpperCase()==oldWord){ oldWord.toLowerCase() } else if (oldWord.toLowerCase()==oldWord) { oldWord[0].toUpperCase()+oldWord.substring(1).toLowerCase() } else { oldWord.toUpperCase() } } </groovy>

Author: user:seatrout with polish and editing by user:boercher

Clean screen editor

A toggle script for when you want to concentrate: will either centre the selected node, hide all others, and darken the background, or switch back to the normal bright, and possibly cluttered, view.

<groovy name="Cleanscreen"> // @ExecutionModes({ON_SINGLE_NODE})


import java.awt.Color import org.freeplane.core.resources.ResourceController import org.freeplane.core.ui.ColorTracker import org.freeplane.core.util.ColorUtils import org.freeplane.core.util.TextUtils import org.freeplane.features.mode.Controller; import org.freeplane.features.styles.MapStyle import org.freeplane.features.styles.MapStyleModel


def setBackgroundColor(Color actionColor) {

   Controller controller = Controller.getCurrentController();
   MapStyle mapStyle = (MapStyle) controller.getModeController().getExtension(MapStyle.class);
   MapStyleModel model = (MapStyleModel) mapStyle.getMapHook();
   mapStyle.setBackgroundColor(model, actionColor);

}

def setBackgroundColorCode(String rgbString) {

   setBackgroundColor(ColorUtils.stringToColor(rgbString));

}

// note Groovy gotcha in the next line -- if "paleblue" // is defined as a colour explicitly, as in // "Color paleblue=new Color(218,230,239)" // the script breaks, because variables defined in this way are not accessible inside blocks // they must instead be defined "naked" as below. // see http://groovy.codehaus.org/Scoping+and+the+Semantics+of+%22def%22

paleblue=new Color(218,230,239)


if (this.node.style.edge.getColor()==paleblue){ clutterscreen() } else { clearscreen() }


def clearscreen(){ setBackgroundColor(paleblue) myname=this.node.getId() node.map.filter = { it.getId()==myname } this.node.style.edge.setColor(paleblue) c.centerOnNode(this.node) }

def clutterscreen(){ this.node.style.edge.setColor(Color.lightGray) setBackgroundColor(Color.white) node.map.undoFilter() }


</groovy>

Author: user:seatrout with the hard bits by user:boercher