[SLUG] That vi problem, with pike.

2002-10-22 Thread Bill Bennett
I received a couple of enquiries about the vi problem that
I posted a couple of days ago.

I use Elvis, which is a vi clone.

I wrote to Steve Kirkendall, who wrote Elvis, asking him for his
opinion. Hereunder is the problem amd his comments.


The Problem:

 consider the following heading:---
 
 in the quick armadillo and in the lazy goat
 
 The problem is to capitalize the first letter of each word
 that is made up of more than three letters. *However* the first
 word, regardless of the number of letters also has to begin with
 a capital.
 
 Thus the header becomes:---
 
 In the Quick Armadillo and in the Lazy Goat

Steve's reply:

In vim, you can do it in one step with...

:s/\(^\|\w\w\w\)\w\+/\u/g

In elvis, this command doesn't work because elvis doesn't
recognize the ^ metacharacter anywhere except as the first
character of the regular expression.  Elvis would need two
steps.  That's probably a bug, but it doesn't come up very
often, so I haven't worried about it much.

The traditional vi, and nvi, don't support the \w metacharacter,
so you'd need to use [a-zA-Z0-9_] instead.  And I'm not sure
about the \+ metacharacter either, so you should probably
replace it another [a-zA-Z0-9_] and a * operator.  And even
then, support for \| and a late ^ is very iffy.

I don't like the idea of using . to recognize characters in a
word, because it could be fooled by a very short word followed
by another word.  /\\/ could match in a.

The vim version of the command breaks down like this:

:s/\(^\|\w\w\w\)\w\+/\u/g
   1 334

1) Either the beginning of a line, or 3 characters that can appear
   in a word.

2) One or more additional characters that can appear in a word.
   Note that \+ is a greedy operator, so it'll always match the
   maximum number of word characters.  Because of this, we don't
   need to use any \ or \ metacharacters.

3) Capitalize the next character.  Note that lowercase \u is
   different from uppercase \U.  \U affects all following
   characters, until the next \E.  \u only affects one character,
   and doesn't need a \e to end it.

4) Use a copy of the whole original string.  The \u only affects
   the first character of that string.

This would probably still be fooled by apostrophes.  Words such as
don't might not be capitalized because they'd look like two short
words.


If anyone has comment on this, could you post it, please?

Regards,

Bill Bennett
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug



Re: [SLUG] A vi problem, with pike.

2002-10-14 Thread Angus Lees

At Fri, 11 Oct 2002 21:22:28 +1000, Bill Bennett wrote:
 I use vi as an editing language.
 I'd like a command that would put in the section command and
 capitalize the leading letters of all words in a line that
 are *not* of one, two or three letters. However, the first
 word in the line, regardless of the number of letters *must*
 have a leading capital.

i don't want to start an argument or anything, i just don't think
you're using the right tool for the job.

eg: here is a fairly straightforward (imo) piece of elisp that will do
exactly what you want:

(defun bozotitlecase-section-ify ()
  replace \and this is the current line\ with
\\\section{And This is the Current Line}\
  (interactive)
  (beginning-of-line)
  ;; match the beginning of any word of 4 or more characters, or the
  ;; first on the line
  (while (re-search-forward ^\\w\\|w\\{4\\} (point-at-eol) t)
;; upcase the first letter of whatever we just matched
(replace-match \\u\\))
  (beginning-of-line)
  (insert-string \\section{)
  (end-of-line)
  (insert-string }))

and if writing elisp is too difficult, you could have recorded a macro
that would have generated pretty much the same code.


if they were the only freestanding single lines in your text (ie:
they could be found without user interaction), you might also want to
consider preprocessing your document with a simple perl (or similar)
script.  even if the script got it wrong 10% of the time, you're
already way ahead.

-- 
 - Gus
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug



[SLUG] A vi problem, with pike.

2002-10-11 Thread Bill Bennett
Dear All,

I have a problem that I'd been thinking about for some time.

I don't think it can be done. 

Nevertheless, I'll post it. When such problems have been posted
in the past, they have generated brisk exchanges and much
corrosive sarcasm. OTOH, if a solution is found, it could be very
useful...

I've been given a text document, to be set in LaTeX. 

I use vi as an editing language.

There are a lot of sections in it.  
The section headings take up one line only.

I'd like a command that would put in the section command and
capitalize the leading letters of all words in a line that
are *not* of one, two or three letters. However, the first
word in the line, regardless of the number of letters *must*
have a leading capital.

For example, the line:---

A quick armadillo and a dead goat

becomes

\section{A Quick Armadillo and a Dead Goat}

(I do not wish to know that words shouldn't be capitalized thus
for a heading: my orders come From Above.)

A (very) partial solution would be:---

:.,.s/^\(.\)\(.*)/\\section{\U\1\2}/

which, in bits, is:---
:.,.command, for this line
s/^\(.\)remember the first character for later
substitution
\(.*\)  remember also the rest of the line for later
substitution
/\\section{ begin the substitution with \section{
(\\ because the backslash is a magic)
\U\1the uppercase version of the first character
\2}/followed by the rest of the line and a curly
bracket.

As I said, very partial.

The one, two and three letter words are what's the matter. My
own thinking would be not to specify them, but to specify
four and above with the first letter remembered: something
like \\(.\)...*\

Can anyone improve on this?

Regards,

Bill Bennett.
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug



Re: [SLUG] A vi problem, with pike.

2002-10-11 Thread John Ferlito
On Fri, Oct 11, 2002 at 09:22:28PM +1000, Bill Bennett wrote:
 \section{A Quick Armadillo and a Dead Goat}
 

A bit tough to do in one go. Assuming your using vim maybe it works in
other clones you can do something like this

map F2 ESC:s/^\(.\)/\U\1/CR:s/ \([a-z]\)\(\w\{3,}\)/ 
\U\1\l\2/gCR:s/^/\\section{/CR:s/$/}/CR

Then you just have to hit F2

I'm sure there's a better way

-- 
John
http://www.inodes.org/
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug