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

Re: XEmacs - how to automate mark-search-cut



On 2007-05-26, Russell L. Harris <rlharris@oplink.net> wrote:
> I use XEmacs as my editor.  
>
> I need to extract footnotes from a very long LaTeX document.  I would
> like to start with a copy of the document, then delete from the copy
> all text which is not a footnote.
>
> Is there a way to:
>
>     (1) set the mark
>
>     (2) search forward for the next instance of the string
>     "\footnote", highlighting all the text between the mark and the
>     search target
>
>     (3) cut the highlighted text
>

Hi again,

prescript: I wrote out a long explanation of a hard way to do what you
want. Then I thought of the quick and easy way that follows. I've left
the long tricky bits in case they are also helpful.

The easy way:

You can use occur to extract all your footnotes to a separate buffer:

M-x occur
\\footnote{[^}]*

This will pop all your footnotes into a read-only buffer. They are
marred with line numbers, but you can yank them into a new buffer, and
then do a query-regexp-replace to clean out the line numbers. That's
probably the fastest way to do what you want.


The harder ways:

With the caveat that I'm working on GNU Emacs and not Xemacs, which we
learned yesterday can be the source of unexpected surprises, here's
one way to do what you want:

using query-replace-regexp, which for me is bound to C-M-%, enter the
following search expression:

/(.*
\)*.*\\footnote

and the replace expression:

\\footnote

The tricky part is entering the newline in the search expression. You
can't do this by pressing enter. I did it by pressing C-j, or you
might be able to use C-q enter. This will highlight everything between
the start of the search and the following \footnote, and delete
everything before the \footnote. 

What this expression doesn't do is skip over the body of the footnote
to the closing }, so subsequent searches will offer to delete the body
of the previous footnote. It's possible to include the body of the
footnote, but I don't have time just now to work out the regexp.

If you know any elisp you can automate this by writing it into a short
function, and then calling that function directly rather than calling
query-replace-regexp in each time. In GNU emacs M-p cycles backwards
through previous regexps once you're in the query-replace-regexp
minibuffer, so at least you only need to type in the expression once
if you don't write your own function.


> When I try to do this, after the search I see the message "Mark saved
> where search started", but I see no highlighting.  When I try to cut
> the text, I see the message "The region is not active now".

'The region is not active now' indicates you are working in
transient-mark-mode. If you turn this off the region is always active,
which might solve the problem you ran into with your approach. You can
toggle this on and off with M-x transient-mark-mode.


Anyways, some bits and pieces that might help!

Tyler



Reply to: