I need to preserve some text, so i get that text from scrolling field and stored in arrays and variables before my editing, after my editing, i restore the text in that places. I am using this code for this process, it works fine but it tooks lot of time. How to reduce that time?
command Store
global ELine,m,EELine,myEELength,myELength,EQLine,myEQLength,EALine,myEALength,ESLine,ESuLine,myESLength,myESuLength,TSLine,myTSLength,stsLine,sta,stab,Tta
global wPreserve,woPreserve,endwPreserve,cnt
put the text of field "MytextField" into woPreserve
put the number of lines of woPreserve into cnt
--answer cnt
repeat with x = 1 to the number of lines of woPreserve
if line x of woPreserve contains "begin{thebibliography}" then
put line x to cnt in woPreserve into endwPreserve
delete line x+1 to cnt in field "MytextField"
--answer endwPreserve
end if
end repeat
repeat with x = 1 to the number of lines of woPreserve
if line x of woPreserve contains "begin{document}" then
put line 1 to x-1 in woPreserve into wPreserve
delete line 1 to x-1 in field "MytextField"
--answer wPreserve
end if
end repeat
set the caseSensitive to true
put "" into ELine
replace "\$" with "{\XXdollarXX}" in field "MytextField"
put the text of field "MytextField" into Tvar
--put replaceText(Tvar,"[a-z,A-Z]\$"
put replaceText(Tvar,"\n","^EOL") into field "MytextField"
put the text of field "MytextField" into Tvar
put the number of paragraphs in Tvar into ppara
repeat with p = 0 to ppara
put paragraph p of Tvar into q
--put replaceText(q,"\s\n","") after aa
put replaceText(q,"\n","") after aa
end repeat
--put aa into field "MytextField"
replace "^EOL\begin{Seqnarray}" with cr & "\begin{Seqnarray}" in field "MytextField"
replace "\end{Seqnarray}^EOL" with "\end{Seqnarray}" & cr in field "MytextField"
replace "^EOL\begin{subequation}" with cr & "\begin{subequation}" in field "MytextField"
replace "\end{subequation}^EOL" with "\end{subequation}" & cr in field "MytextField"
replace "^EOL\begin{equation}" with cr & "\begin{equation}" in field "MytextField"
replace "\end{equation}^EOL" with "\end{equation}" & cr in field "MytextField"
replace "^EOL\begin{eqnarray}" with cr & "\begin{eqnarray}" in field "MytextField"
replace "\end{eqnarray}^EOL" with "\end{eqnarray}" & cr in field "MytextField"
replace "^EOL\begin{align}" with cr & "\begin{align}" in field "MytextField"
replace "\end{align}^EOL" with "\end{align}" & cr in field "MytextField"
replace "^EOL\begin{Table}" with cr & "\begin{Table}" in field "MytextField"
replace "\end{Table}^EOL" with "\end{Table}" & cr in field "MytextField"
replace "^EOL\begin{table}" with cr & "\begin{table}" in field "MytextField"
replace "\end{table}^EOL" with "\end{table}" & cr in field "MytextField"
--replace "\begin{Seqnarray}" with cr & "\begin{Seqnarray}" in field "MytextField"
replace "\end{Seqnarray}" with "\end{Seqnarray}" & cr in field "MytextField"
--replace "^EOL\begin{subequation}" with cr & "\begin{subequation}" in field "MytextField"
replace "\end{subequation}" with "\end{subequation}" & cr in field "MytextField"
--replace "^EOL\begin{equation}" with cr & "\begin{equation}" in field "MytextField"
replace "\end{equation}" with "\end{equation}" & cr in field "MytextField"
--replace "^EOL\begin{eqnarray}" with cr & "\begin{eqnarray}" in field "MytextField"
replace "\end{eqnarray}" with "\end{eqnarray}" & cr in field "MytextField"
--replace "^EOL\begin{align}" with cr & "\begin{align}" in field "MytextField"
replace "\end{align}" with "\end{align}" in field "MytextField"
--put the text of field "MytextField" into aa
put 0 into k
put 0 into z
put 0 into n
put 0 into j
put 0 into l
put 0 into Ta
put 0 into stab
put the text of field "MytextField" into aa
repeat for each lines ET in aa
if ET contains "\begin{subequation}" then
add 1 to k
put "-----SubDisplayEquation-----" & k into es
replace ET with es in field "MytextField"
put ET & cr after ESLine
end if
if ET contains "\begin{Seqnarray}" then
add 1 to z
put "-----SDisplayEquation-----" & z into eSu
replace ET with eSu in field "MytextField"
put ET & cr after ESuLine
end if
if ET contains "\begin{equation}" then
add 1 to n
put "-----DisplayEquation-----" & n into eo
replace ET with eo in field "MytextField"
put ET & cr after EELine
end if
if ET contains "\begin{eqnarray}" then
add 1 to j
put "-----EqnarryDisplayEquation-----" & j into er
replace ET with er in field "MytextField"
put ET & cr after EQLine
end if
if ET contains "\begin{align}" then
add 1 to l
put "-----AlignDisplayEquation-----" & l into ea
replace ET with ea in field "MytextField"
put ET & cr after EALine
end if
if ET contains "\begin{Table}" then
add 1 to Ta
put "-----Table-----" & Ta into Tta
replace ET with Tta in field "MytextField"
put ET & cr after TSLine
end if
if ET contains "\begin{table}" then
add 1 to stab
put "-----_table_-----" & stab into sta
replace ET with sta in field "MytextField"
put ET & cr after stsLine
end if
end repeat
put the text of field "MytextField" into ss
put "" into yy
put 0 into tmp
repeat with i = 1 to the number of chars in ss
if char i of ss contains "$" then
add 1 to tmp
if tmp = 1 then
put "^SOE" & CR & char i of ss after yy
else
put char i of ss & CR & "^EOE" after yy
put 0 into tmp
end if
else
put char i of ss after yy
end if
end repeat
put yy into the field "MytextField"
put 0 into m
repeat for each lines iT in yy
if iT contains "$" then
add 1 to m
put "---In_Line_Equation---" & m into so
replace IT with so in field "MytextField"
put iT & cr after ELine
end if
end repeat
--answer ELine
put 0 into m
put the text of field "MytextField" into SSEE
put replaceText(SSEE, "\^SOE\n","") into SSEE
put replaceText(SSEE, "\n\^EOE","") into SSEE
put replaceText(SSEE, "\^EOL",cr) into SSEE
--answer SSEE
put SSEE into field "MytextField"
end Store
command Restore
global ELine,m,EELine,myEELength,myELength,EQLine,myEQLength,EALine,myEALength,ESLine,ESuLine,myESLength,myESuLength,TSLine,myTSLength,stsLine,sta,stab,mystsLength
global wPreserve,x,woPreserve,endwPreserve,cnt
put cr into line 0 of field "MytextField"
put wPreserve into line 0 of field "MytextField"
put "" into wPreserve
put cr into last line of field "MytextField"
put endwPreserve into last line of field "MytextField"
put "" into endwPreserve
put the number of lines of ESLine into myESLength
split ESLine by cr
put myESLength into k
repeat with i = myESLength down to 1
put "-----SubDisplayEquation-----" & k into es
replace es with ESLine[i] in field "MytextField"
subtract 1 from k
end repeat
put the number of lines of ESuLine into myESuLength
split ESuLine by cr
put myESuLength into z
repeat with i = myESuLength down to 1
put "-----SDisplayEquation-----" & z into eSu
replace eSu with ESuLine[i] in field "MytextField"
subtract 1 from z
end repeat
put the number of lines of EELine into myEELength
split EELine by cr
put myEELength into n
repeat with i = myEELength down to 1
put "-----DisplayEquation-----" & n into eo
replace eo with EELine[i] in field "MytextField"
subtract 1 from n
end repeat
put the number of lines of EQLine into myEQLength
split EQLine by cr
put myEQLength into j
repeat with i = myEQLength down to 1
put "-----EqnarryDisplayEquation-----" & j into er
replace er with EQLine[i] in field "MytextField"
subtract 1 from j
end repeat
put the number of lines of EALine into myEALength
split EALine by cr
put myEALength into l
repeat with i = myEALength down to 1
put "-----AlignDisplayEquation-----" & l into ea
replace ea with EALine[i] in field "MytextField"
subtract 1 from l
end repeat
put the number of lines of TSLine into myTSLength
split TSLine by cr
put myTSLength into Ts
repeat with i = myTSLength down to 1
put "-----Table-----" & Ts into Tta
replace Tta with TSLine[i] in field "MytextField"
subtract 1 from Ts
end repeat
put the number of lines of stsLine into mystsLength
split stsLine by cr
put mystsLength into stab
repeat with i = mystsLength down to 1
put "-----_table_-----" & stab into sta
replace sta with stsLine[i] in field "MytextField"
subtract 1 from stab
end repeat
put the number of lines of ELine into myELength
split ELine by cr
put myELength into m
repeat with i = myELength down to 1
put "---In_Line_Equation---" & m into so
replace so with ELine[i] in field "MytextField"
subtract 1 from m
end repeat
replace "^EOL" with cr in field "MytextField"
replace "\$" with "{\XXdollarXX}" in field "MytextField"
end Restore
Is there any alternate way to do this?.
It looks like you're doing all the text replacement in a field. You should put the text content into a variable first, do all your replacing there, then put the variable's contents back into the field.
Related
Escape Issue
Hello all,
I have the bellow python string and I am trying to basically to ignore the '' scape characters. My Goal is to have the two single apostrophe before and after the DD but the raw string is not working as expected as seen in the image. Is there a way I could make them appear as I am expecting ?
r"""hello my name is "John" I cannot remove ''DD'' the escape character in order to show
the two single quotes together"""
Thanks,
I tried using replacements functions and other methods that I researched and non worked as expected.
chars = r""""John" ''DD''"""
chars # '"John" \'\'DD\'\''
'"John" \'\'DD\'\''
Above you see escaped inner single quotes (apostrophes) because the output is presented as single-quoted string. In fact, there are no reverse solidi (backslashes) in the chars string:
len(chars) # 13
print( chars) # "John" ''DD''
"John" ''DD''
Another proof (chars string as a sequence characters):
import unicodedata
for jj, char in enumerate( chars):
print( f'{jj:3}', char, unicodedata.name(char,'???'))
0 " QUOTATION MARK
1 J LATIN CAPITAL LETTER J
2 o LATIN SMALL LETTER O
3 h LATIN SMALL LETTER H
4 n LATIN SMALL LETTER N
5 " QUOTATION MARK
6 SPACE
7 ' APOSTROPHE
8 ' APOSTROPHE
9 D LATIN CAPITAL LETTER D
10 D LATIN CAPITAL LETTER D
11 ' APOSTROPHE
12 ' APOSTROPHE
Read more about escaping in String and Bytes literals
In a text file given the following text:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
with AppleScript and BBEdit I'd like to be able to cut the day Sunday and move it before Monday but when I reference the BBEdit dictionary I see the ability to cut:
when I try to cut the text and add it before the line I get an error:
BBEdit got an error: "Sunday" doesn’t understand the “cut” message.
the code:
tell application "BBEdit"
set theFile to "foobar.txt"
select insertion point before first character of text document theFile
repeat
set theWeekend to find "(?<=Saturday\\n)Sunday" searching in text 1 of text document theFile options {search mode:grep} with selecting match
if theWeekend is not found then exit repeat
set theWeekend to found text of theWeekend
select insertion point before first character of text document theFile
set beforeMon to find "(?!<=Sunday\\n)Monday" searching in text 1 of text document theFile options {search mode:grep} with selecting match
if beforeMon is found then
set beforeMon to found text of beforeMon
set theLine to startLine of selection
set addDay to select insertion point before first character of line theLine of text document theFile
set thePull to cut theWeekend ## where error occurs
set before line theLine of text of text document theFile to (theWeekend & return)
end if
end repeat
end tell
If I comment out set thePull to cut theWeekend the script works in a continuous loop and places Sunday before Monday but I can't break the loop because my grep variable theWeekend is still false.
Other failed attempts:
cut selection of (theWeekend)
cut theWeekend
cut theWeekend of selection
cut selection of contents of (theWeekend)
cut contents of theWeekend
In BBEdit and AppleScript how can I cut the day and move it?
The error is that the theWeekend variable contains a string, not a reference to a string.
The cut command need a reference to a (character, word or line) in the document, like this:
cut characters 51 thru 56 of text document 1
cut line 7 of text document 1
cut word 7 of text document 1
cut selection -- or this
So use the found object property instead of the found text.
tell application "BBEdit"
activate
tell text document "foobar.txt"
select insertion point before first character
repeat
set theWeekend to find "(?<=Saturday\\n)Sunday" searching in text 1 options {search mode:grep} with selecting match
if theWeekend is not found then exit repeat
select insertion point before first character
set beforeMon to find "(?!<=Sunday\\n)Monday" searching in text 1 options {search mode:grep} with selecting match
if beforeMon is found then
cut (found object of theWeekend) -- the cut command returns nothing, it's useless to put the result in a variable
set before (found object of beforeMon) to (found text of theWeekend) & return
end if
end repeat
end tell
end tell
Here is a modified version of your code that worked for me in testing:
I removed the set theWeekend to found text of theWeekend line completely, while modifying:
set before line theLine of text of text document theFile to (theWeekend & return)
To:
set before line theLine of text of text document theFile to (found text of theWeekend & linefeed)
theWeekend in this case is a list returned from the find command, leave it as a list because you actually need to use two of the properties from it.
found text in this instance and also found object in the set thePull to cut ... line above this.
I typically use linefeed instead of return because in some instances, although not this one, the latter does end up as 0D instead of 0A. In other words, it ends up as a \r and not what is typical, \n in macOS documents.
I also changed set thePull to cut theWeekend, to:
set thePull to cut (found object of theWeekend)
Reworked AppleScript code:
tell application "BBEdit"
set theFile to "foobar.txt"
select insertion point before first character of text document theFile
repeat
set theWeekend to find "(?<=Saturday\\n)Sunday" searching in text 1 of text document theFile options {search mode:grep} with selecting match
log theWeekend
if theWeekend is not found then exit repeat
select insertion point before first character of text document theFile
set beforeMon to find "(?!<=Sunday\\n)Monday" searching in text 1 of text document theFile options {search mode:grep} with selecting match
if beforeMon is found then
set beforeMon to found text of beforeMon
set theLine to startLine of selection
set addDay to select insertion point before first character of line theLine of text document theFile
set thePull to cut (found object of theWeekend)
set before line theLine of text of text document theFile to (found text of theWeekend & return)
end if
end repeat
end tell
Input:
boston beach summer figural yellow blue
boston floral flowers still still-life food pink figural
boston horse pink purple house flowers floral figural
Expected output:
"boston beach summer figural yellow blue"
"boston floral flowers still still-life food pink figural"
"boston horse pink purple house flowers floral figural"
The actual input file has 600+ lines, and I'm looking at a quick way to wrap each line in quotes? Does the method involve using multiple cursors? How about macros?
I would use a multiple cursors approach like this:
Windows
Ctrl + A (Select everything)
Ctrl + Shift + L (Split into lines)
End (Put the cursor at the end of the line)
" (Add the quote at the end of the line)
Home (Go to the first character of the line)
Home (Go to the beginning of the line... like if you have tabs or spaces)
" (Add the quote at the beginning of the line)
Mac
Cmd + A (Select everything)
Cmd + Shift + L (Split into lines)
Cmd + → (Put the cursor at the end of the line)
" (Add the quote at the end of the line)
Cmd + ← (Go to the first character of the line)
Cmd + ← (Go to the beginning of the line... like if you have tabs or spaces)
" (Add the quote at the beginning of the line)
Method 1:
no multiple cursors
+ best performance (use for large files)
- slightly clumsy
Replace (.*) with "\1"
Method 2:
multiple cursors
+ best in regards to usability/comfort
- slower for bigger files
- wont work if the file has empty lines
Ctrl+a, Ctrl+Shift+l, "
Method 3:
multiple cursors
+ close to #2 as usability, but works always
- slower for bigger files
Ctrl+a, Ctrl+Shift+l, End, ", Home, "
None of these worked in Sublime Text 3 for a multi-column TSV file with tab spacing.
I found this worked for column 1:
Find: ^\s*\S+
Replace: "$0"
Say I have something like
a & 1234567890
b & 1234567890
c & 1234567890
d & 1234567890
e & 1234567890
f & 1234567890
Is there a way to use a vim macro such that I can run a macro/command x amount of times per line, where x depends on the line?
In this case, I run 2wx^ on each line x times, where x is the line number such that the result becomes
a & 234567890
b & 34567890
c & 4567890
d & 567890
e & 67890
f & 7890
Thanks in advance
If your macro is recorded in register q, then you can run:
:exec 'normal ' . line('.') . '#q'
on any line you want. Your macro will want the cursor kept on 1st column before being run.
You can also probably do it - better - in a different way, if you describe what you want to do. For example, perhaps you could skip the macro altogether and use something like this instead:
:exec 'normal ^2w' . line('.') . 'xj'
If you need a line offset (e.g. of 1), you could use:
:let nr = line('.') - 1 | execute 'normal ^2w' . nr . 'xj'
How do I replace every number n that matches a certain pattern with n+1? E.g. I want to replace all numbers in a line that are in brackets with the value+1.
1 2 <3> 4 <5> 6 7 <8> <9> <10> 11 12
should become
1 2 <4> 4 <6> 6 7 <9> <10> <11> 11 12
%s/<\zs\d\+\ze>/\=(submatch(0)+1)/g
By way of explanation:
%s " replace command
"""""
< " prefix
\zs " start of the match
\d\+ " match numbers
\ze " end of the match
> " suffix
"""""
\= " replace the match part with the following expression
(
submatch(0) " the match part
+1 " add one
)
"""""
g " replace all numbers, not only the first one
Edit:
If you only want to replace in specific line, move your cursor on that line, and execute
s/<\zs\d\+\ze>/\=(submatch(0)+1)/g
or use
LINENUMs/<\zs\d\+\ze>/\=(submatch(0)+1)/g
(replace LINENUM with the actual line number, eg. 13)
In vim you can increment (decrement) the numeric digit on or after the cursor by pressing
NUMBER<ctrl-a> to add NUMBER to the digit
(NUMBER<ctrl-x> to substract NUMBER from the digit)
If only incrementing (decrementing) by one you don't need to specify NUMBER. In your case I would use a simple macro for this:
qaf<<ctrl-a>q
100<altgr-q>a
Here a brief explanation of the macro: It uses the find (f) commant to place the cursor on the opening < bracket. It is not necessary to position the cursor on the digit. When press the number on the cursor or the nearest number after the cursor will get incremented.
If you want an even shorter series of commands you can position your curser ONCE by pressing f<, increment the number with ctrl-a and then just repeatedly press ;.. The ; command repeats the last cursor movement i.e. the find command. The . command repeats the last text changing command.
Check out this link for further information or use the built in documentation: h: ctrl-a.