/* * :mode=beanshell:tabSize=4:indentSize=4:noTabs=false: * :folding=none:collapseFolds=0:wrap=none:maxLineLen=80: * * $Source$ * Copyright (C) 2003 Robert Fletcher * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import gnu.regexp.*; RE UNICODE_REGEXP = new RE("(?:\\\\u[0-9a-fA-F]{4})+", 0, RESearchMatcher.RE_SYNTAX_JEDIT); String selection = textArea.getSelectedText(); if(selection == null || selection.length() == 0) { Macros.error(view, "There is no selected text."); return; } try { StringBuffer buf = new StringBuffer(); if(UNICODE_REGEXP.isMatch(selection)) { for(int i = 0; i < selection.length(); i+=6) { char c = (char)Integer.parseInt(selection.substring(i+2, i+6), 16); buf.append(c); } } else { for(int i = 0; i < selection.length(); i++) { int c = (int)selection.charAt(i); String hex = Integer.toHexString(c); buf.append("\\u"); while((buf.length() + hex.length()) % 6 != 0) { buf.append('0'); } buf.append(hex); } } textArea.setSelectedText(buf.toString()); } catch(Exception e) { Macros.error(view, e.toString()); } /* Macro index data (in DocBook format) Unicode_Escape.bsh Converts all selected characters to unicode escapes unless the selection is a sequence of unicode escapes in which case they are converted back to characters. */ // end Unicode_Escape.bsh