On Mon, Nov 22, 2010 at 3:25 PM, Douglas Wade <-> wrote:
> Paul,
> That's sound great. I would love to see your code.
>
Gmail "ate" leading tabs and turned them into spaces. You might want to
rename leading spaces back to tabs somehow ... it will improve readability.
I hid some internal information with this:
supportpersonororganizationgoeshere.
I added some comments just now preceded by 20101122 on the first line of
those new comments. Mostly they say, "Uh, I forget why." When we initially
deployed this we called it on every open. Despite modifying the code to pay
attention to nesting, there were edge cases we gave up trying to catch and
now authors call it manually if/when they want it. Also, I think we are
currently stripping the PI automatically on a save callback we developed
originally for some other thing because we suspected (but never proved) this
PI was causing a problem for one of our production processes.
Anyhow, you'll want to deploy this to or call it from editinit or
doctype.acl or some such.
hide and unhide are presently attached to menu commands by other code.
Assume all the usual disclaimers and read the code before deploying,
especially in a production environment.
----------------------------
package highlight_nbsp
# forward declarations
function get_pos_end_of_last_nbsp() {}
function highlight_nbsp() {}
function over_nested() {}
function startup_highlight_nbsp() {}
function unhighlight_nbsp() {}
# Function to test for for nesting depth prior to inserting to
prevent Editor
# hard crashing after insert attempt bringing the depth to 99 (or is it
crossing the 99-level
# boundary? Anyhow.
# 20101122: This is inconsistently called. I can't remember why.
function over_nested() {
if (oid_level(oid_root(),oid_caret()) > 75) {
response('Nesting exceeded the seventy-fifth level. This is probably the
result of an error condition within the Show NBSP code but may reflect a
highly complex document. Please notify supportpersonororganizationgoeshere.
This early exit should have no negative consequences other than probably NOT
highlighting all occurrences of NBSP.');
return 1;
}
return 0;
}
# Function to return the character count of the last character in the last
.
# Note: length() and match() count characters with the expanded as a
named entity.
# goto_oid($oid, $pos) counts the as a single character when
evaluating $pos
# resulting in a 5 character discrepancy.
function get_pos_end_of_last_nbsp(nbsp_element_content) {
local end_pos = 0;
local end_pos_temp = 0;
local keep_looking_for_nbsp_in_element = 1
while (keep_looking_for_nbsp_in_element) {
if (match($nbsp_element_content,'') > 0) {
$end_pos = $end_pos + match($nbsp_element_content,'');
$end_pos_temp = match($nbsp_element_content,'') + 6;
if (length($nbsp_element_content) + 1 == $end_pos_temp) {
end_pos = -1;
$keep_looking_for_nbsp_in_element = 0;
}
$nbsp_element_content = substr($nbsp_element_content, $end_pos_temp);
}
else {
$keep_looking_for_nbsp_in_element = 0;
}
}
return end_pos;
}
# Function to remove all instances. The guts behind Hide
NBSPs.
function unhighlight_nbsp() {
local chgtrk = option("changetracking")
local current_oid = oid_caret();
local current_pos = oid_caret_pos();
set changetracking = off;
goto_oid(oid_root());
find -t -q -nosel -nowrapscan "<_font>"
if (main::status == 0) {
while(main::status == 0) {
delete_tag;
find -t -q -nosel -nowrapscan "<_font>"
}
goto_oid(current_oid, current_pos);
set changetracking = $chgtrk;
return;
}
goto_oid(current_oid, current_pos);
set changetracking = $chgtrk;
return;
}
# Function called on open of document. Bugs out right away if any of the
following
# elements root the chunk: big_element1, big_element2, big_element3,
big_element4.
# to be called from doctype.acl or some ACL in editinit.
# 20101122: we stopped using this very
# soon after initial deployment for some reason I can't remember. prossibly
# to do with over-nesting.
function startup_highlight_nbsp() {
if (oid_name(oid_root()) == 'big_element1' || oid_name(oid_root()) ==
'big_element2' || oid_name(oid_root()) == 'big_element3' ||
oid_name(oid_root()) == 'big_element4') {
return;
}
else {
highlight_nbsp();
}
}
# Function that does all the wrapping of s with .
function highlight_nbsp() {
# save current location of caret, change tracking setting.
local chgtrk = option("changetracking")
local current_oid = oid_caret();
local current_pos = oid_caret_pos();
# A counter for erroring out of the main loop after "too many" loops.
# Should prevent infinite loops. Error number (50,000 when code released)
# could conceivable need to be adjusted for long -full documents.
local i = 0;
set changetracking = off;
doc_update_display(current_doc(), 0);
# Let's get ready to go.
goto_oid(oid_root());
# Let's go. Priming find loop logic.
keep_looking = find('', 0x0530);
# Main program loop.
while (keep_looking) {
# The element are we going to be working with for the rest of the loop.
oid = oid_caret();
# Save this so when we do our next search, we can try again if
# we catch the element we just processed.
oid_last = oid_caret();
oid_caret_pos_last = oid_caret_pos();
# Get starting character positions for activities to come.
first_nbsp_pos = oid_caret_pos();
left_of_first_nbsp = oid_caret_pos() - 1;
#response($first_nbsp_pos);
# Avoid nesting if at all possible.
# Avoid trying to insert within comments which Editor
doesn't like.
if (! inside_tag("_font") && ! inside_tag("_comment")) {
#response('||' . oid_content($oid) . '||');
#response(substr(oid_content($oid),1,$left_of_first_nbsp));
# 1 and -1 are special.
# 1 occurs when is first in an element.
# -1 occurs when is last (and possibly first) in an element.
# length of content with counts the as 6 characters instead
of 1
#
if (first_nbsp_pos == -1) {
#response('-1 Condition: is last (possibly first and last)
character in element.');
#response(length(oid_content($oid)));
if (length(oid_content($oid)) > 6) {
# Some characters precede the final .
# response('Some characters precede the final .');
# if (over_nested()) {return}
mark -noselection begin
caret prevword;
caret prevword;
mark -selection end
if (! over_nested()) {
insert_tag('_font')
modify_attr('FontColor', '#9400D3')
td -none
}
}
else {
# is the only character in the element.
# response(' is the only character in the element.');
# No further attention required for elements containing only an .
# if (over_nested()) {return}
CharLeftExtend
insert_tag('_font')
modify_attr('FontColor', '#9400D3')
modify_attr('BackColor', '#9400D3')
# td -local -full
td -none
}
}
else if (first_nbsp_pos == 1) {
# response('1 Condition: is first (but not first and last)
character in element.');
# response($first_nbsp_pos);
# insert_string('+');
# if (over_nested()) {return}
caret nextword;
mark -noselection begin
goto_oid($oid,$first_nbsp_pos)
mark -selection end
insert_tag('_font')
modify_attr('FontColor', '#9400D3')
td -none
}
else if (first_nbsp_pos == 0) {
response('0 Condition: Please save a copy of this XML instance and
e-mail it to supportpersonororganizationgoeshere. It contains an XML
structure not encountered during development. This should not have any
negative consequences other than possibly NOT highlighting a few occurrences
of NBSP.');
}
else {
# response('>1 Condition: is neither first nor last character in
element.');
# if (over_nested()) {return}
caret nextword;
mark -noselection begin
caret prevword;
caret prevword;
caret prevword;
mark -selection end
insert_tag('_font')
modify_attr('FontColor', '#9400D3')
td -none
caret nextword;
caret nextword;
#insert_string('+');
last_pos = oid_caret_pos();
junk = find('', 0x0530);
# This loop processes the remainder of the element initially caught in
the outer while loop.
# It is definitely missing some occurrences and needs more attention.
while (oid_caret() == $oid) {
# if (over_nested()) {return}
caret nextword;
mark -noselection begin
caret prevword;
caret prevword;
caret prevword;
mark -selection end
insert_tag('_font')
modify_attr('FontColor', '#9400D3')
td -none
caret nextword;
caret nextword;
# Find the next next occurrence.
junk = find('', 0x0530);
# response(oid_last . ' ' . oid_caret());
# response(oid_caret_pos_last . ' ' . oid_caret_pos());
# Keep trying if we've found one we've already worked.
# THIS IS PROBABLY SKIPPING SOMETIMES.
if (oid_last == oid_caret() && oid_caret_pos_last = oid_caret_pos()) {
junk = find('', 0x0530);
}
}
}
} # fi
# Get to end of currently working element
# This preps the next find to really "move on" to the next oid in the
tree.
goto_oid($oid, -1)
# Bug out if we loop too many times during testing.
# 50,000 is an arbitrary number. If we run into ti,
# I'll be surprised, but one never knows.
i++;
if (i > 50000) {
response('50,000 NBSPs processed. This is probably the result of an error
condition within the Show NBSP code but may reflect a very long document.
Please notify supportpersonororganizationgoeshere. This early exit should
have no negative consequences other than probably NOT highlighting all
occurrences of NBSP.');
goto_oid(current_oid, current_pos);
set changetracking = $chgtrk;
doc_update_display(current_doc(), 1);
return
} # fi
# Reprime the find pump with next occurrence before end of outer while
loop.
keep_looking = find('', 0x0530);
# response(oid_last . ' ' . oid_caret());
# response(oid_caret_pos_last . ' ' . oid_caret_pos());
# Make sure we didn't somehow re-find the element we just handled.
if (oid_last == oid_caret() && oid_caret_pos_last = oid_caret_pos()) {
keep_looking = find('', 0x0530);
}
} # elihw
# Reset all of the options/settings to their status prior to running this
code.
doc_update_display(current_doc(), 1);
set changetracking = $chgtrk;
goto_oid(current_oid, current_pos);
# response('We found ~' . $i . ' s.');
# response('Grace.');
} # end highlight_nbsp
--
Paul Nagai