Dear All,
Many years ago, in a state of innocence, I hard-coded my LaTeX quotation marks.
Thus I have text that `looks like this'.
I now find that it would have been MUCH better (if not essential) to have done:
Thus I have text that \enquote{looks like this}
I can't do a search and replace, because there is no algorithm that will get the quotes right, but I would like to be able to do the following:
1. Select the text that needs changing with the mouse
2. Hit a key and have the text changed to the \enquote version.
I feel like this ought to be the sort of thing that TextMate excels at. How should I do it?
Very best wishes,
Nicholas
On Jun 15, 2009, at 11:11 AM, Nicholas Cole nicholas.cole@gmail.com wrote:
Dear All,
Many years ago, in a state of innocence, I hard-coded my LaTeX quotation marks.
Thus I have text that `looks like this'.
I now find that it would have been MUCH better (if not essential) to have done:
Thus I have text that \enquote{looks like this}
I can't do a search and replace, because there is no algorithm that will get the quotes right, but I would like to be able to do the following:
Select the text that needs changing with the mouse
Hit a key and have the text changed to the \enquote version.
I feel like this ought to be the sort of thing that TextMate excels at. How should I do I
How about a macro? It could take cursor position, expand outwards until it finds your left most quote after whitespace and then finds your right most quote followed by whitespace.
From there, it is just a matter of swapping in your substitution data.
Curious... Is your desired text to change always structured the same?
Why not use a regex based find and replace and connect that to a hot key via a bundle?
Perhaps show us a small sampling of all the possible input data you are looking to transform.
Sorry I can not provide examples, still waiting on textmate for iPhone :)
On Mon, Jun 15, 2009 at 7:24 PM, Scott Hanedatalklists@newgeo.com wrote:
On Jun 15, 2009, at 11:11 AM, Nicholas Cole nicholas.cole@gmail.com
Curious... Is your desired text to change always structured the same?
Why not use a regex based find and replace and connect that to a hot key via a bundle?
The problem with a regex is that the British use of single quotes makes apostrophes confusing.
So:
This is `some of Jefferson's text'
Needs to become:
This is \enquote{some of Jefferson's text}
I think only a human can get that right reliably.
A macro sounds interesting. How do I best make one of those for this purpose, then?
Best,
Nicholas
On Jun 15, 2009, at 11:45 AM, Nicholas Cole wrote:
On Mon, Jun 15, 2009 at 7:24 PM, Scott Hanedatalklists@newgeo.com wrote:
On Jun 15, 2009, at 11:11 AM, Nicholas Cole nicholas.cole@gmail.com
Curious... Is your desired text to change always structured the same?
Why not use a regex based find and replace and connect that to a hot key via a bundle?
The problem with a regex is that the British use of single quotes makes apostrophes confusing.
So:
This is `some of Jefferson's text'
Needs to become:
This is \enquote{some of Jefferson's text}
I think only a human can get that right reliably.
You need smarter regex's :)
How about:
`(.*?)'(?!s)
So it starts by matching `, and matches until it finds a single ' that is not followed by “s”.
Now you can replace this with
\enquote{$1}
—Alex
Alex Ross wrote:
You need smarter regex's :)
How about:
`(.*?)'(?!s)
So it starts by matching `, and matches until it finds a single ' that is not followed by “s”.
It will still fail in too many cases, such as plural possessive.
Alice said, `I need to fill my two cats' water bowl.'
Anyway, on to the macro.
1. Highlight the text you want replaced. For this macro, highlight everything inside the quotes, but not the quotes themselves. 2. Press ⌥⌘M (alt-option-M) to start recording a macro. 3. Press ⌘X (option-X) to cut the highlighted text. 4. Press → (right-arrow) to move past the closing quote. 5. Press ⌫ (delete) twice to delete both quotes. 6. Type: \enquote{ 7. Press ⌘V (option-V) to paste the text you cut earlier. 8. Type: } 9. Press ⌥⌘M (alt-option-M) again to stop recording the macro.
Now, to use the macro you simply need to highlight the next bit of quoted text and press ⇧⌘M (shift-option-M). The macro will be replayed, repeating the same sequence of operations outlined in steps 3-8. If you like you can save the macro with ⌃⌘M (control-option-M) to use at a later time.
On Mon, Jun 15, 2009 at 8:40 PM, Steve Kingsking@arbor.net wrote:
Alex Ross wrote:
You need smarter regex's :)
How about:
`(.*?)'(?!s)
So it starts by matching `, and matches until it finds a single ' that is not followed by “s”.
It will still fail in too many cases, such as plural possessive.
Alice said, `I need to fill my two cats' water bowl.'
Anyway, on to the macro.
Dear Steve,
Thanks! That is a very useful tutorial.
Best,
N
On Mon, Jun 15, 2009 at 8:01 PM, Alex Rosstm-alex@rosiba.com wrote:
On Jun 15, 2009, at 11:45 AM, Nicholas Cole wrote:
On Mon, Jun 15, 2009 at 7:24 PM, Scott Hanedatalklists@newgeo.com wrote:
On Jun 15, 2009, at 11:11 AM, Nicholas Cole nicholas.cole@gmail.com
Curious... Is your desired text to change always structured the same?
Why not use a regex based find and replace and connect that to a hot key via a bundle?
The problem with a regex is that the British use of single quotes makes apostrophes confusing.
So:
This is `some of Jefferson's text'
Needs to become:
This is \enquote{some of Jefferson's text}
I think only a human can get that right reliably.
You need smarter regex's :)
How about:
`(.*?)'(?!s)
So it starts by matching `, and matches until it finds a single ' that is not followed by “s”.
Now you can replace this with
\enquote{$1}
Dear Alex,
I will admit that is very, very cunning. Unfortunately, there is one other convention that messes it up, which is that some (especially classical) possessives are in the form Socrates'. Still, they are rare and that does make that regex useful!
Best,
N.
On 15/06/09 6:43 PM, "Nicholas Cole" nicholas.cole@gmail.com wrote:
On Mon, Jun 15, 2009 at 8:01 PM, Alex Rosstm-alex@rosiba.com wrote:
How about:
`(.*?)'(?!s)
So it starts by matching `, and matches until it finds a single ' that is not followed by ³s².
Now you can replace this with
\enquote{$1}
Dear Alex,
I will admit that is very, very cunning. Unfortunately, there is one other convention that messes it up, which is that some (especially classical) possessives are in the form Socrates'. Still, they are rare and that does make that regex useful!
I'm not sure how to capture this in regex, since I have not yet mastered that particular kung-fu, but shouldn't most quoted text terminate with punctuation, where as posessives and contractions would usually not have an additional piece of punctuation immediately beside them? For example:
This is `some of Jefferson's text.'
Or
The vagabond stared and said 'the rails are the way to travel,' and then moved
along.
So for a quotation the terminating quote mark will either be preceded or followed by punctuation (depending on whether you put your periods inside of the quote marks or outside). A possessive such as Socrates' or a contraction like 'I've' would normally be followed by a space or another character.
Something like this seems to work:
Find: `(.*?)(.|,)'(?!s)
Replace:
\enquote{$1$2}$3
jeff
On Jun 15, 2009, at 11:45 AM, Nicholas Cole wrote:
On Mon, Jun 15, 2009 at 7:24 PM, Scott Hanedatalklists@newgeo.com wrote:
On Jun 15, 2009, at 11:11 AM, Nicholas Cole nicholas.cole@gmail.com
Curious... Is your desired text to change always structured the same?
Why not use a regex based find and replace and connect that to a hot key via a bundle?
The problem with a regex is that the British use of single quotes makes apostrophes confusing.
So:
This is `some of Jefferson's text'
Needs to become:
This is \enquote{some of Jefferson's text}
Ahh, ok, I see. In that case, I believe you could fix the majority of issues with my previous email. However, there should be a limited set of these cases, 's being the most widely found one. I am sure there are others, but you can certainly come up with a list of them.
You may want to then get a little hacky with the macro, and work on the selection only. Here is how I would approach it: 1) Find 's replace to "something-you-will-never-have-in-your-code" Repeat that find and replace with every 's style you can think 2) Perform a normal regex of find/replace as per previous email. 3) Find "something-you-will-never-have-in-your-code" replace to 's Use different replace strings for each case.
You can work on making a smarter regex as well such as `(.*?)'(?!s) * Credit to Alex Ross for that pattern And you can simply add more !s type patterns to it for others. (Using OR)
A macro sounds interesting. How do I best make one of those for this purpose, then?
Macro's are very powerful because they are so simple to make. Simply work out how you want it to work manually. Go to the Bundles -> Macros -> Start Recording. When done with your work, in the same area, select Stop Recording, this will bring you to the bundle editor, for that macro. You then give it a nice name, and set a keyboard command to it.
Since you want to limit the fallout on this, when you are working in the find and replace window, hold down the option key, which will change a button to say "in selection".
From there, you would just hit the key command, and it will run. It is quote fast. I have done multi step macro's that do 30 or more actions on a file, and it happens acceptably quick.
On Jun 15, 2009, at 11:11 AM, Nicholas Cole wrote:
Dear All,
Many years ago, in a state of innocence, I hard-coded my LaTeX quotation marks.
Thus I have text that `looks like this'.
I now find that it would have been MUCH better (if not essential) to have done:
Thus I have text that \enquote{looks like this}
I can't do a search and replace, because there is no algorithm that will get the quotes right, but I would like to be able to do the following:
Select the text that needs changing with the mouse
Hit a key and have the text changed to the \enquote version.
I feel like this ought to be the sort of thing that TextMate excels at. How should I do it?
Just playing around, not sure if I understand your case. Starting with this sample text as in put, in a TM file:
`looks like this' dsa 'dsadad' `looks like this' dsadsad `looks like this'
Search on patterh: `(.+?)' Replace with pattern \enquote{$1}
Result: \enquote{looks like this} dsa 'dsadad' \enquote{looks like this} dsadsad \enquote{looks like this}
I made a Macro that works only on the selection, as you asked, but you can change it to work on the entire document if desired:
{ action = replaceAll; findInProjectIgnoreCase = 1; findString = "`(.+?)'"; ignoreCase = 1; regularExpression = 1; replaceAllScope = selection; replaceString = "\enquote{$1}"; wrapAround = 1; }
What wold be more ideal, I think, is to take the cursor position, and grow a selection to the left and right until the selection hits a known list of characters on the left and right. I am not sure how to alter the "select word", or "Select Enclosing Brackets", which I believe would be a good place to start in getting this to work as you want it.
Hope this helps.