unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* string searching and saving results to a variable
@ 2011-02-15 23:15 ken
  2011-02-15 23:33 ` Perry Smith
  0 siblings, 1 reply; 11+ messages in thread
From: ken @ 2011-02-15 23:15 UTC (permalink / raw)
  To: GNU Emacs List

It's time again to write an elisp function!!

One thing it needs to do a couple of times is save a string to a
variable.  The string to save will be an html header, like:

<h3>Section 4</h3>

but it could be multiple lines like this

<h3 class="newest-chapter-section-type" align="center">On
the origins of elisp confusion</h3>

It could even be three or four lines long.  Also, the line(s) could be
indented and so have unwanted white space in the first several columns.

Assuming the point is somewhere on that line or one of those lines, we do:

(end-of-line)    ; to preclude the point being at the far left.
; find the start of the string:
(re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
;;somehow mark this as the beginning of the string???

;find the endpoint of the string:
(re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)

;;save string to a variable to do other things with... how???


Thanks for any help.




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-15 23:15 string searching and saving results to a variable ken
@ 2011-02-15 23:33 ` Perry Smith
  2011-02-16  6:55   ` ken
  2011-02-17 10:51   ` ken
  0 siblings, 2 replies; 11+ messages in thread
From: Perry Smith @ 2011-02-15 23:33 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List


On Feb 15, 2011, at 5:15 PM, ken wrote:

> It's time again to write an elisp function!!
> 
> One thing it needs to do a couple of times is save a string to a
> variable.  The string to save will be an html header, like:
> 
> <h3>Section 4</h3>
> 
> but it could be multiple lines like this
> 
> <h3 class="newest-chapter-section-type" align="center">On
> the origins of elisp confusion</h3>
> 
> It could even be three or four lines long.  Also, the line(s) could be
> indented and so have unwanted white space in the first several columns.
> 
> Assuming the point is somewhere on that line or one of those lines, we do:

To be clean, first declare some variables

(defvar ....)

> (end-of-line)    ; to preclude the point being at the far left.
> ; find the start of the string:
> (re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
> ;;somehow mark this as the beginning of the string???

save the current point with:

(setq beg (point))

> 
> ;find the endpoint of the string:
> (re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)
> 
> ;;save string to a variable to do other things with... how???

(setq dog (buffer-substring beg (point)))

dog now has the string




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-15 23:33 ` Perry Smith
@ 2011-02-16  6:55   ` ken
  2011-02-16  9:21     ` Oleksandr Gavenko
  2011-02-16 11:28     ` Le Wang
  2011-02-17 10:51   ` ken
  1 sibling, 2 replies; 11+ messages in thread
From: ken @ 2011-02-16  6:55 UTC (permalink / raw)
  To: GNU Emacs List


On 02/15/2011 06:33 PM Perry Smith wrote:
> On Feb 15, 2011, at 5:15 PM, ken wrote:
> 
>> It's time again to write an elisp function!!
>>
>> One thing it needs to do a couple of times is save a string to a
>> variable.  The string to save will be an html header, like:
>>
>> <h3>Section 4</h3>
>>
>> but it could be multiple lines like this
>>
>> <h3 class="newest-chapter-section-type" align="center">On
>> the origins of elisp confusion</h3>
>>
>> It could even be three or four lines long.  Also, the line(s) could be
>> indented and so have unwanted white space in the first several columns.
>>
>> Assuming the point is somewhere on that line or one of those lines, we do:
> 
> To be clean, first declare some variables
> 
> (defvar ....)
> 
>> (end-of-line)    ; to preclude the point being at the far left.
>> ; find the start of the string:
>> (re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
>> ;;somehow mark this as the beginning of the string???
> 
> save the current point with:
> 
> (setq beg (point))
> 
>> ;find the endpoint of the string:
>> (re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)
>>
>> ;;save string to a variable to do other things with... how???
> 
> (setq dog (buffer-substring beg (point)))
> 
> dog now has the string
> 

Thanks!!

I'm guessing it could then be done more elispishly as

(setq dog (buffer-substring
  (re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
  (re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)))




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-16  6:55   ` ken
@ 2011-02-16  9:21     ` Oleksandr Gavenko
  2011-02-16 16:58       ` ken
  2011-02-16 11:28     ` Le Wang
  1 sibling, 1 reply; 11+ messages in thread
From: Oleksandr Gavenko @ 2011-02-16  9:21 UTC (permalink / raw)
  To: help-gnu-emacs

On 16.02.2011 8:55, ken wrote:
> I'm guessing it could then be done more elispishly as
>
> (setq dog (buffer-substring
>    (re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
>    (re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)))
>
How about if search fail?

Evaluating of

   (buffer-substring nil nil)

take (wrong-type-argument integer-or-marker-p nil).




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-16  6:55   ` ken
  2011-02-16  9:21     ` Oleksandr Gavenko
@ 2011-02-16 11:28     ` Le Wang
  1 sibling, 0 replies; 11+ messages in thread
From: Le Wang @ 2011-02-16 11:28 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List

[-- Attachment #1: Type: text/plain, Size: 602 bytes --]

On Wed, Feb 16, 2011 at 2:55 PM, ken <gebser@mousecar.com> wrote:

> (setq dog (buffer-substring
>   (re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
>   (re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)))
>

What mode are you using to edit html?  What version of Emacs?  It's likely
your major-mode supports balanced tag movements, so this hacky regexp
searching isn't necessary.

What do you want o do with the saved text?  Why don't you try to compose the
function to the best of your ability, and I'm sure someone will give you
hints for what you can't get working.

-- 
Le

[-- Attachment #2: Type: text/html, Size: 1041 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-16  9:21     ` Oleksandr Gavenko
@ 2011-02-16 16:58       ` ken
  2011-02-19 11:16         ` Oleksandr Gavenko
  0 siblings, 1 reply; 11+ messages in thread
From: ken @ 2011-02-16 16:58 UTC (permalink / raw)
  Cc: help-gnu-emacs


On 02/16/2011 04:21 AM Oleksandr Gavenko wrote:
> On 16.02.2011 8:55, ken wrote:
>> I'm guessing it could then be done more elispishly as
>>
>> (setq dog (buffer-substring
>>    (re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
>>    (re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)))
>>
> How about if search fail?
> 
> Evaluating of
> 
>   (buffer-substring nil nil)
> 
> take (wrong-type-argument integer-or-marker-p nil).

Error-checking is an excellent idea, I agree.  But I don't understand
the elisp code you wrote.  Could you explain it for me please.


Thanks.





^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-15 23:33 ` Perry Smith
  2011-02-16  6:55   ` ken
@ 2011-02-17 10:51   ` ken
  2011-02-17 13:30     ` Perry Smith
  1 sibling, 1 reply; 11+ messages in thread
From: ken @ 2011-02-17 10:51 UTC (permalink / raw)
  To: GNU Emacs List

On 02/15/2011 06:33 PM Perry Smith wrote:
> On Feb 15, 2011, at 5:15 PM, ken wrote:
> 
>> ....
>>
>> <h3>Section 4</h3>
>>
>> but it could be multiple lines like this
>>
>> <h3 class="newest-chapter-section-type" align="center">On
>> the origins of elisp confusion</h3>
>>
>> It could even be three or four lines long.  Also, the line(s) could be
>> indented and so have unwanted white space in the first several columns.
>>
>> ....

Thanks, Perry.  That got me through that step.

Next little thing: I want to check if the initial heading tag might
*already* contain the "name" attribute.  So with the point at the start
of that tag, I'd do something like this:

(re-search-forward
"name=\"\\|name[whitespace]=\"\\|name[whitespace]=[whitespace]\""
end-heading nil nil)

Is there a shortcut way in elisp to express any and all combinations of
whitespace...?  if not, how to do this?


tia,
ken



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-17 10:51   ` ken
@ 2011-02-17 13:30     ` Perry Smith
  2011-02-18 17:33       ` ken
  0 siblings, 1 reply; 11+ messages in thread
From: Perry Smith @ 2011-02-17 13:30 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List


On Feb 17, 2011, at 4:51 AM, ken wrote:

> On 02/15/2011 06:33 PM Perry Smith wrote:
>> On Feb 15, 2011, at 5:15 PM, ken wrote:
>> 
>>> ....
>>> 
>>> <h3>Section 4</h3>
>>> 
>>> but it could be multiple lines like this
>>> 
>>> <h3 class="newest-chapter-section-type" align="center">On
>>> the origins of elisp confusion</h3>
>>> 
>>> It could even be three or four lines long.  Also, the line(s) could be
>>> indented and so have unwanted white space in the first several columns.
>>> 
>>> ....
> 
> Thanks, Perry.  That got me through that step.
> 
> Next little thing: I want to check if the initial heading tag might
> *already* contain the "name" attribute.  So with the point at the start
> of that tag, I'd do something like this:
> 
> (re-search-forward
> "name=\"\\|name[whitespace]=\"\\|name[whitespace]=[whitespace]\""
> end-heading nil nil)
> 
> Is there a shortcut way in elisp to express any and all combinations of
> whitespace...?  if not, how to do this?

The answer to your question appears to be `\s ' is for any whitespace
character.  (backslash s space) But let me try to teach you how to fish.

In emacs, do C-h i to get into "Info".  On some systems, that will not work
at all and thats bad.  On other systems, it will put you into a larger directory
of "Info" topics.  Search down and pick "Emacs".  C-h m will tell you something
about the Info mode that you are in to teach you how to work within
info.  On other systems, the initial C-h i puts you into the Emacs info (like
on my Mac).

Once you get into Info and get into the Emacs node, look for Regular and you
will see at least three topics.  One is Regexp Backslash.  That has the
info I just found for your.

One reply to your question asked which mode you are in.  A good mode will
understand the html far better than your searches.

HTH,
Perry




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-17 13:30     ` Perry Smith
@ 2011-02-18 17:33       ` ken
  2011-02-18 18:16         ` Le Wang
  0 siblings, 1 reply; 11+ messages in thread
From: ken @ 2011-02-18 17:33 UTC (permalink / raw)
  To: GNU Emacs List


On 02/17/2011 08:30 AM Perry Smith wrote:
> On Feb 17, 2011, at 4:51 AM, ken wrote:
> 
>> On 02/15/2011 06:33 PM Perry Smith wrote:
>>> On Feb 15, 2011, at 5:15 PM, ken wrote:
>>>
>>>> ....
>>>>
>>>> <h3>Section 4</h3>
>>>>
>>>> but it could be multiple lines like this
>>>>
>>>> <h3 class="newest-chapter-section-type" align="center">On
>>>> the origins of elisp confusion</h3>
>>>>
>>>> It could even be three or four lines long.  Also, the line(s) could be
>>>> indented and so have unwanted white space in the first several columns.
>>>>
>>>> ....
>> Thanks, Perry.  That got me through that step.
>>
>> Next little thing: I want to check if the initial heading tag might
>> *already* contain the "name" attribute.  So with the point at the start
>> of that tag, I'd do something like this:
>>
>> (re-search-forward
>> "name=\"\\|name[whitespace]=\"\\|name[whitespace]=[whitespace]\""
>> end-heading nil nil)
>>
>> Is there a shortcut way in elisp to express any and all combinations of
>> whitespace...?  if not, how to do this?
> 
> The answer to your question appears to be `\s ' is for any whitespace
> character.  (backslash s space) But let me try to teach you how to fish.

Thanks again.


> 
> In emacs, do C-h i to get into "Info".  ....

I've known about "info" for a couple decades and use it (from within a
terminal window mostly) on occasion (and for more than just emacs), but
I've found over the years that it doesn't often give me the answer I
need.  In particular, it doesn't contain a lot of working examples...
these are much more helpful in learning and more prevalent on the web.
So I'm more likely to go to the web... which is what I did.  I guess in
this instance I should have gone to "info" first.

Let me make a disclaimer.  I'm figuring out as much of the coding as I
can on my own... and this before I come to this list.  It's only when I
search for an answer for a couple hours and get nowhere that I figure
it's time to ask the pros.  I mean isn't this a major reason for the
existence of this list?  The other thing is that I don't have a lot of
spare time for coding like this.  (I originally thought I might be able
to code this whole routine and start using it the same day.  Well, it's
been a good part of several days I've been on it now.  But I'm learning
a little more elisp, so that's okay... probably.)  And I try to post
questions which aren't going to require someone to either read or write
a lot.  And I'm not asking anyone to write the entire routine for me.
I'm kind of surprised that with all the people on this list, just one
person is providing any answers.  Maybe I'm on the wrong list (?).


> One reply to your question asked which mode you are in.  A good mode will
> understand the html far better than your searches.

I'm using html-helper-mode.  I've looked through a lot of the code (but,
yeah, not all of it) over the years and while the functions are pretty
basic in terms of functionality (more so than what I'm writing), the
code is not completely transparent.  I've tweaked a lot of it in trivial
ways, grepped it looking for stuff, but I'm not thinking it would be
terribly useful to read through the entirety of it.


> 
> HTH,
> Perry

Yeah, it did help.  Thanks again,
ken




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-18 17:33       ` ken
@ 2011-02-18 18:16         ` Le Wang
  0 siblings, 0 replies; 11+ messages in thread
From: Le Wang @ 2011-02-18 18:16 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List

[-- Attachment #1: Type: text/plain, Size: 586 bytes --]

On Sat, Feb 19, 2011 at 1:33 AM, ken <gebser@mousecar.com> wrote:

> Let me make a disclaimer.  I'm figuring out as much of the coding as I
> can on my own... and this before I come to this list.  It's only when I
> search for an answer for a couple hours and get nowhere that I figure
> it's time to ask the pros.  I mean isn't this a major reason for the
> existence of this list?


I asked you some questions else where in this thread and got no response.
 That makes it hard for me to help you further.  I still don't understand
what you're trying to do with your function.

-- 
Le

[-- Attachment #2: Type: text/html, Size: 915 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: string searching and saving results to a variable
  2011-02-16 16:58       ` ken
@ 2011-02-19 11:16         ` Oleksandr Gavenko
  0 siblings, 0 replies; 11+ messages in thread
From: Oleksandr Gavenko @ 2011-02-19 11:16 UTC (permalink / raw)
  To: help-gnu-emacs

On 2011-02-16 16:58, ken wrote:
> On 02/16/2011 04:21 AM Oleksandr Gavenko wrote:
>> How about if search fail?
>>
>> Evaluating of
>>
>>    (buffer-substring nil nil)
>>
>> take (wrong-type-argument integer-or-marker-p nil).
>
> Error-checking is an excellent idea, I agree.  But I don't understand
> the elisp code you wrote.  Could you explain it for me please.
>
This copied from top of *Backtrace* buffer when eval expression
(which appear on error).
So it is not a working elisp code.

-- 
Best regards!




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2011-02-19 11:16 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-15 23:15 string searching and saving results to a variable ken
2011-02-15 23:33 ` Perry Smith
2011-02-16  6:55   ` ken
2011-02-16  9:21     ` Oleksandr Gavenko
2011-02-16 16:58       ` ken
2011-02-19 11:16         ` Oleksandr Gavenko
2011-02-16 11:28     ` Le Wang
2011-02-17 10:51   ` ken
2011-02-17 13:30     ` Perry Smith
2011-02-18 17:33       ` ken
2011-02-18 18:16         ` Le Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).