From: <ha...@us...> - 2009-06-11 14:02:07
|
Revision: 11001 http://jmol.svn.sourceforge.net/jmol/?rev=11001&view=rev Author: hansonr Date: 2009-06-11 14:01:45 +0000 (Thu, 11 Jun 2009) Log Message: ----------- version=11.7.42_dev # new feature: {xxxx}.label("") shows current label # new feature: [array].sort # new feature: [array].reverse # new feature: [array].min # new feature: [array].max # new feature: [array].average # new feature: [array].stddev # new feature: (value).label("C++ printf format") Modified Paths: -------------- trunk/Jmol/src/org/jmol/modelset/Atom.java trunk/Jmol/src/org/jmol/util/TextFormat.java trunk/Jmol/src/org/jmol/viewer/Eval.java trunk/Jmol/src/org/jmol/viewer/Jmol.properties trunk/Jmol/src/org/jmol/viewer/Token.java trunk/Jmol/src/org/jmol/viewer/Variable.java Modified: trunk/Jmol/src/org/jmol/modelset/Atom.java =================================================================== --- trunk/Jmol/src/org/jmol/modelset/Atom.java 2009-06-11 02:50:45 UTC (rev 11000) +++ trunk/Jmol/src/org/jmol/modelset/Atom.java 2009-06-11 14:01:45 UTC (rev 11001) @@ -1201,7 +1201,10 @@ ch = atom.getInsertionCode(); return (ch == '\0' ? "" : "" + ch); case Token.label: - return atom.group.chain.modelSet.getAtomLabel(atom.getAtomIndex()); + String s = atom.group.chain.modelSet.getAtomLabel(atom.getAtomIndex()); + if (s == null) + s = ""; + return s; case Token.structure: return JmolConstants.getProteinStructureName(atom.getProteinStructureType()); case Token.symmetry: Modified: trunk/Jmol/src/org/jmol/util/TextFormat.java =================================================================== --- trunk/Jmol/src/org/jmol/util/TextFormat.java 2009-06-11 02:50:45 UTC (rev 11000) +++ trunk/Jmol/src/org/jmol/util/TextFormat.java 2009-06-11 14:01:45 UTC (rev 11001) @@ -282,6 +282,62 @@ /** * + * formatCheck checks p and q formats and duplicates if necessary + * "%3.5p xxxx" ==> "%3.5p%3.5p%3.5p xxxx" + * + * @param f + * @return f or dupicated format + */ + public static String formatCheck(String f) { + int pt; + if (f.length() < 3 || f.charAt(0) != '%') + return f; + if ((pt = f.indexOf('p')) >= 0) + f = fdup(f, pt, 3); + if ((pt = f.indexOf('q')) >= 0) + f = fdup(f, pt, 4); + return f; + } + + /** + * + * fdup duplicates p or q formats for formatCheck + * and the format() function. + * + * @param f + * @param pt + * @param n + * @return %3.5q%3.5q%3.5q%3.5q or %3.5p%3.5p%3.5p + */ + private static String fdup(String f, int pt, int n) { + char ch; + int count = 0; + for (int i = pt; --i >= 1; ) { + if (Character.isDigit(ch = f.charAt(i))) + continue; + switch (ch) { + case '.': + if (count++ != 0) + return f; + continue; + case '-': + if (i != 1) + return f; + continue; + default: + return f; + } + } + String s = f.substring(0, pt + 1); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < n; i++) + sb.append(s); + sb.append(f.substring(pt + 1)); + return sb.toString(); + } + + /** + * * proper splitting, even for Java 1.3 -- if the text ends in the run, * no new line is appended. * @@ -492,4 +548,5 @@ sb.append(c).append(s[i]); return sb.toString(); } + } Modified: trunk/Jmol/src/org/jmol/viewer/Eval.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Eval.java 2009-06-11 02:50:45 UTC (rev 11000) +++ trunk/Jmol/src/org/jmol/viewer/Eval.java 2009-06-11 14:01:45 UTC (rev 11001) @@ -7906,13 +7906,17 @@ Object getBitsetIdent(BitSet bs, String label, Object tokenValue, boolean useAtomMap, int index) { boolean isAtoms = !(tokenValue instanceof BondSet); - if (isAtoms && label == null) - label = viewer.getStandardLabelFormat(); + if (isAtoms) { + if (label == null) + label = viewer.getStandardLabelFormat(); + else if (label.length() == 0) + label = "%[label]"; + } int pt = (label == null ? -1 : label.indexOf("%")); boolean haveIndex = (index != Integer.MAX_VALUE); - if(bs == null || isSyntaxCheck || isAtoms && pt < 0) + if (bs == null || isSyntaxCheck || isAtoms && pt < 0) return new String[] { label == null ? "" : label }; - int len = (haveIndex ? index + 1 : bs.size()); + int len = (haveIndex ? index + 1 : bs.size()); int nmax = (haveIndex ? 1 : BitSetUtil.cardinalityOf(bs)); String[] sout = new String[nmax]; ModelSet modelSet = viewer.getModelSet(); @@ -7922,10 +7926,11 @@ if (indices == null && label != null && label.indexOf("%D") > 0) indices = viewer.getAtomIndices(bs); boolean asIdentity = (label == null || label.length() == 0); - Hashtable htValues = (isAtoms || asIdentity ? null : LabelToken.getBondLabelValues()); - LabelToken[] tokens = (asIdentity ? null - : isAtoms ? LabelToken.compile(viewer, label, '\0', null) - : LabelToken.compile(viewer, label, '\1', htValues)); + Hashtable htValues = (isAtoms || asIdentity ? null : LabelToken + .getBondLabelValues()); + LabelToken[] tokens = (asIdentity ? null : isAtoms ? LabelToken.compile( + viewer, label, '\0', null) : LabelToken.compile(viewer, label, '\1', + htValues)); for (int j = (haveIndex ? index : 0); j < len; j++) if (index == j || bs.get(j)) { String str; @@ -7933,7 +7938,8 @@ if (asIdentity) str = modelSet.getAtomAt(j).getInfo(); else - str = LabelToken.formatLabel(modelSet.getAtomAt(j), null, tokens, '\0', indices); + str = LabelToken.formatLabel(modelSet.getAtomAt(j), null, tokens, + '\0', indices); } else { Bond bond = modelSet.getBondAt(j); if (asIdentity) @@ -7941,7 +7947,7 @@ else str = LabelToken.formatLabel(bond, tokens, htValues, indices); } - str = TextFormat.formatString(str, "#", (n+1)); + str = TextFormat.formatString(str, "#", (n + 1)); sout[n++] = str; if (haveIndex) break; @@ -8266,6 +8272,8 @@ } if (minmaxtype == Token.all) { int len = vout.size(); + if (isString && len == 1) + return vout.get(0); if (tok == Token.sequence) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < len; i++) @@ -12354,8 +12362,8 @@ if (logMessages) { - //dumpStacks("addOp entry\naddOp: " + op + " oPt=" + oPt + " ifPt = " + ifPt - // + " skipping=" + skipping + " wasX=" + wasX); + dumpStacks("addOp entry\naddOp: " + op + " oPt=" + oPt + " ifPt = " + ifPt + + " skipping=" + skipping + " wasX=" + wasX); } // are we skipping due to a ( ? : ) construct? @@ -12475,7 +12483,7 @@ isLeftOp = true; break; } - if (wasX == isLeftOp) + if (wasX == isLeftOp && tok0 != Token.propselector) // for now, because we have .label and .label() return false; break; } @@ -12519,8 +12527,6 @@ // if not, it's time to operate - // TODO: precedence on .label without () is not working -- .label.size, - // for example if (!operate()) return false; tok0 = (oPt >= 0 ? oStack[oPt].tok : 0); @@ -12715,6 +12721,10 @@ case Token.mul: case Token.div: return evaluateList(op.intValue, args); + case Token.format: + if (isSyntaxCheck) + return addX(""); + return addX(Variable.sprintf(args)); case Token.label: return evaluateLabel(args); case Token.data: @@ -13433,8 +13443,6 @@ private boolean evaluateLabel(Variable[] args) throws ScriptException { Variable x1 = getX(); String format = (args.length == 0 ? "%U" : Variable.sValue(args[0])); - if (args.length > 1) - return false; if (isSyntaxCheck) return addX(""); if (x1.tok == Token.bitset) @@ -13715,6 +13723,20 @@ if (!(v instanceof Variable)) return false; x2 = (Variable) v; + } else if(xPt >= 1 && xStack[xPt].tok == Token.opEQ || x2.tok == Token.opEQ) { + // special case {xxx}.list[n] because we also have {xxx}.list("format") + // so for now at least we have to do the selection manually. + int index = Integer.MIN_VALUE; + if (x2.tok == Token.list) { + x2.intValue = 1; + index = Variable.iValue(x2); + x2 = (Variable) getX().value; + if (x2.intValue != iv) + return false; + } + x2 = getX(); + if (index != Integer.MIN_VALUE) + x2.intValue = index; } if (op.tok == x2.tok) x2 = getX(); Modified: trunk/Jmol/src/org/jmol/viewer/Jmol.properties =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Jmol.properties 2009-06-11 02:50:45 UTC (rev 11000) +++ trunk/Jmol/src/org/jmol/viewer/Jmol.properties 2009-06-11 14:01:45 UTC (rev 11001) @@ -3,6 +3,7 @@ version=11.7.42_dev +# new feature: {xxxx}.label("") shows current label # new feature: [array].sort # new feature: [array].reverse # new feature: [array].min Modified: trunk/Jmol/src/org/jmol/viewer/Token.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Token.java 2009-06-11 02:50:45 UTC (rev 11000) +++ trunk/Jmol/src/org/jmol/viewer/Token.java 2009-06-11 14:01:45 UTC (rev 11001) @@ -543,19 +543,21 @@ // the min/max mask requires that the first number here must not exceed 63 // the only other requirement is that these numbers be unique - // xxx(a) - static int getMaxMathParams(int tokCommand) { return ((tokCommand >> 9) & 0x7); } - final static int function = 1 | 0 << 9 | mathfunc | flowCommand | noeval; - + // xxx(a,b,c,d,e,...) + final static int array = 1 | 0 << 9 | mathfunc; - final static int getproperty = 2 | 0 << 9 | mathfunc | command; - final static int write = 3 | 0 << 9 | mathfunc | command; + final static int format = 2 | 0 << 9 | mathfunc; + final static int function = 3 | 0 << 9 | mathfunc | flowCommand | noeval; + final static int getproperty = 4 | 0 << 9 | mathfunc | command; + final static int write = 5 | 0 << 9 | mathfunc | command; + // xxx(a) + final static int load = 1 | 1 << 9 | mathfunc | command; final static int substructure = 2 | 1 << 9 | mathfunc; final static int javascript = 3 | 1 << 9 | mathfunc | implicitStringCommand; @@ -566,7 +568,6 @@ final static int forcmd = 8 | 1 << 9 | mathfunc | flowCommand; final static int ifcmd = 9 | 1 << 9 | mathfunc | flowCommand; - // ___.xxx(a) // a.distance(b) is in a different set -- distance(b,c) -- because it CAN take @@ -617,7 +618,6 @@ final static int within = 1 | 5 << 9 | mathfunc; final public static int connected = 2 | 5 << 9 | mathfunc; - // more SET parameters final public static int ambient = setparam | 1; @@ -788,6 +788,7 @@ "file", new Token(file), "font", new Token(font), "for", new Token(forcmd), + "format", new Token(format), "frame", new Token(frame), "frames", null, "frank", new Token(frank), Modified: trunk/Jmol/src/org/jmol/viewer/Variable.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Variable.java 2009-06-11 02:50:45 UTC (rev 11000) +++ trunk/Jmol/src/org/jmol/viewer/Variable.java 2009-06-11 14:01:45 UTC (rev 11001) @@ -717,7 +717,27 @@ vq[0]= (Point4f) var.value; return TextFormat.sprintf(strFormat, of ); } - - + /** + * sprintf accepts arguments from the format() function First argument is a + * format string. + * + * @param args + * @return formatted string + */ + public static String sprintf(Variable[] args) { + switch (args.length) { + case 0: + return ""; + case 1: + return sValue(args[0]); + } + String[] format = TextFormat.split(TextFormat.simpleReplace( + sValue(args[0]), "%%", "\1"), '%'); + StringBuffer sb = new StringBuffer(); + sb.append(format[0]); + for (int i = 1; i < format.length; i++) + sb.append(sprintf(TextFormat.formatCheck("%" + format[i]), args[i])); + return sb.toString().replace('\1', '%'); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |