help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help with upcasing words first char


From: Pascal J. Bourguignon
Subject: Re: Help with upcasing words first char
Date: Tue, 25 Aug 2009 23:02:47 +0200
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

Harry Putnam <reader@newsguy.com> writes:

> If I might try your patience even further.. I wondered if you might
> take time to explain what controls where the cursor ends up... in the
> code you proposed earlier. (A slightly changed version is included at
> the end)

Good question! :-)




> What I see looks like this (Upcase X is where cursor ends up):
>
>   some more xrays
>
>   SomeMoreXrays
>           -
> Is it this bit where that happens?
>        (delete-region (match-beginning 0) (match-end 0))))
>   (set-marker end nil))))

No.  This is about the only expression that doesn't move the cursor
(other than leaving it at the start of the deleted region if it was
inside).


> Can something be altered in there to make the cursor land at the end
> of the line?
>
>   SomeMoreXrays[here]

Yes, indeed.


> Or is it done where `m' is assigned the value it holds?

No.  The marker is used to keep track of the end position of the
region, since we are changing the length of that region by deleting
parts.


> It appears to my untrained eye that `m' is told to hold the position of
> the end of the effected text... 

Yes, that's correct, and we will use it to position the cursor there,
adding a (goto-char end) before resetting the marker with (set-marker end nil).


> which would be the last place a space
> or tab was removed... (I guess.)

Not exactly.  The two expressions that move the cursor are (goto-char
start) that puts it at the beginning of the region, and
(re-search-forward ...) that moves it to the start of the matching
string (ie. the position of (match-beginning 0)).

Deleting the region will leave it at this position, and when there is
no more any space till the end, it will stay here.  Hence why it was
left at the beginning of the last word.  

If you selected trailling spaces in the region, it would move here.


> Anyway... how would I go about making this useful function place the
> cursor where I want it (at the end of the effected text (including
> what ever string of text is past the last affected area [spc/\t] where
> action is taken?

Using goto-char, you can position the cursor at the position you want:
 
(defun camelize-region (start end)
  (interactive "r")
  (capitalize-region start end)
  (let ((end (let ((m (make-marker))) (set-marker m end) m)))
    (unwind-protect
         (progn
           (goto-char start)
           (while (re-search-forward "[ \t]+" end t) 
             (delete-region (match-beginning 0) (match-end 0))))
      (goto-char end)
      (set-marker end nil))))

-- 
__Pascal Bourguignon__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]