unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Elisp: help on string operations
@ 2016-11-05 19:17 Karl Voit
  2016-11-08 14:16 ` Michael Heerdegen
  0 siblings, 1 reply; 4+ messages in thread
From: Karl Voit @ 2016-11-05 19:17 UTC (permalink / raw)
  To: help-gnu-emacs

Hi!

For Memacs[1], I'd like to come up with a very fast Org-mode method to
open files independent of their location on your disk. I accomplished
everything necessary so far. However, I've got issues writing an elisp
function for extracting a file and sending it to a function that calls
the operating system app associated. My elisp knowledge is still too
bad :-(

What I've got is a string (from the lookup) which contains:
(1) nothing
(2) one line like "** <2008-06-17 21:43> [[file:/home/user/dir/2008-06-17 description of file.odp][2008-06-17 description of file.odp]]"
(3) multiple lines like (2) but with different files and links

I'd like to get a "Sorry" message for (1). Easy, if only I know how to
count lines in strings (as opposed to buffers).

For (2) I'd like to call my-open-in-external-app(string).

For (3), I'd like to use the first line just as in (2) as a starter. I
do have some ideas on gracious "error-recovering" on this one but
that's maybe too complicated to code for now: getting rid of
substrings delimited by not-'a-zA-Z0-9' from the end of the link
until a match is found; message in case there is no unique match which
could be found this way.

Could you please help me here?



The fragment I have so far is following but the regex seems to be
broken:

#+BEGIN_SRC elisp
    (let (
      (result (replace-regexp-in-string "\r?\n$" ""
                        (shell-command-to-string (concat "grep "
                                         "2008-06-17"
                                         " ~/org/memacs/files.org_archive")
                                     )
                        )
          )
      )
      (message (concat "result -> " result))
      (re-search-forward " \[\[.+\]\[" nil t 1)
      (let (
        (firstlink ((match-string 1)))
        )
        (message (concat "first link -> " firstlink))
       )
      )
#+END_SRC



[1] https://github.com/novoid/Memacs

-- 
All in all, one of the most disturbing things today is the definitive
fact that the NSA, GCHQ, and many more government organizations are
massively terrorizing the freedom of us and the next generations.
                                                  http://Karl-Voit.at




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

* Re: Elisp: help on string operations
  2016-11-05 19:17 Elisp: help on string operations Karl Voit
@ 2016-11-08 14:16 ` Michael Heerdegen
  2016-11-08 17:36   ` Karl Voit
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Heerdegen @ 2016-11-08 14:16 UTC (permalink / raw)
  To: Karl Voit; +Cc: Karl Voit, help-gnu-emacs

Hello Karl,

> I'd like to get a "Sorry" message for (1). Easy, if only I know how to
> count lines in strings (as opposed to buffers).

You could use `with-temp-buffer', insert the string and do your work
there.  Alternatively, go with `count-matches' or `split-string'.


>       (message (concat "result -> " result))

To avoid that a dynamically computed argument of `message' gets
interpreted with format specifier syntax considered, it's better to use
it like

        (message "%s" (compute-some-string-here))


>       (re-search-forward " \[\[.+\]\[" nil t 1)
                             ^^^^^^^^^^ 
That looks wrong.  You want to have a backslash char before the
brackets.  But since backslash is an escape character in the read syntax
of strings, you need to escape the backslash characters, which means
you need to double the backslashes.

Any questions left?


Regards,

Michael.



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

* Re: Elisp: help on string operations
  2016-11-08 14:16 ` Michael Heerdegen
@ 2016-11-08 17:36   ` Karl Voit
  2016-11-08 22:29     ` Michael Heerdegen
  0 siblings, 1 reply; 4+ messages in thread
From: Karl Voit @ 2016-11-08 17:36 UTC (permalink / raw)
  To: help-gnu-emacs

* Michael Heerdegen <michael_heerdegen@web.de> wrote:
> Hello Karl,

Hallo Michael!

>> I'd like to get a "Sorry" message for (1). Easy, if only I know how to
>> count lines in strings (as opposed to buffers).
>
> You could use `with-temp-buffer', insert the string and do your work
> there.  Alternatively, go with `count-matches' or `split-string'.

Ah, so there are no string operations on strings in "memory" but
rather (lots of) string operations on strings in buffers. I see.

>>       (message (concat "result -> " result))
>
> To avoid that a dynamically computed argument of `message' gets
> interpreted with format specifier syntax considered, it's better to use
> it like
>
>         (message "%s" (compute-some-string-here))

Good point.

>>       (re-search-forward " \[\[.+\]\[" nil t 1)
>                              ^^^^^^^^^^ 
> That looks wrong.  You want to have a backslash char before the
> brackets.  But since backslash is an escape character in the read syntax
> of strings, you need to escape the backslash characters, which means
> you need to double the backslashes.

That's it, I see.

Man, I hate those different levels of escaping everywhere :-(

> Any questions left?

I got help from John Kitchin as well. So far I need to digest
everything I learned for now and try to come up with a solution that
works.

Danke!

-- 
All in all, one of the most disturbing things today is the definitive
fact that the NSA, GCHQ, and many more government organizations are
massively terrorizing the freedom of us and the next generations.
                                                  http://Karl-Voit.at




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

* Re: Elisp: help on string operations
  2016-11-08 17:36   ` Karl Voit
@ 2016-11-08 22:29     ` Michael Heerdegen
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Heerdegen @ 2016-11-08 22:29 UTC (permalink / raw)
  To: Karl Voit; +Cc: Karl Voit, help-gnu-emacs

Karl Voit <devnull@Karl-Voit.at> writes:

> > You could use `with-temp-buffer', insert the string and do your work
> > there.  Alternatively, go with `count-matches' or `split-string'.
>
> Ah, so there are no string operations on strings in "memory" but
> rather (lots of) string operations on strings in buffers. I see.

No no, there are tons of string operations, `split-string' is one of
them.  But if you feel more comfortable with buffer operations, you can
go with temp buffers, unless speed is very crucial.

> Danke!

Aber gerne doch!


Michael.



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

end of thread, other threads:[~2016-11-08 22:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-11-05 19:17 Elisp: help on string operations Karl Voit
2016-11-08 14:16 ` Michael Heerdegen
2016-11-08 17:36   ` Karl Voit
2016-11-08 22:29     ` Michael Heerdegen

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).