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

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

Re: function to copy org buffer substring with link description only


From: Gene
Subject: Re: function to copy org buffer substring with link description only
Date: Wed, 9 Dec 2015 11:57:26 -0800 (PST)
User-agent: G2/1.0

On Sunday, December 6, 2015 at 3:46:00 PM UTC-5, Paul wrote:
> Hallo people.
> 
> (buffer-substring-no-properties (point) (mark)) on some part of an org 
> buffer that contains a link returns the following example:
> 
> "while [[(some-link)][description]] does not"
> 
> 
> Do You know a ready to use function that would return the following?:
> 
> "while description does not"
> 
> best regards,
> Paul

I think you might want to consider how you'd use this in practice.
It seems that you want to perform a destructive, non-restorable edit, rather 
than using two views of the same data via lentic or such.
As the string returned by the example contains the ASCII text of/for an org 
link this string can be parsed via split-string for both "[[" and "]]" to find 
beginning and end of the link to use as point and mark.
Or -- if you think about it -- searching for "][" should drop the cursor/point 
at the start of the description string of interest which ends at "]]"

As a general pointer/clue, `elt' is your friend because it operates on any 
sequence ... both strings and vectors in your case.
The structure of an org-mode link is a vector containing two vectors.
The description can be stripped from the vector either as a vector or while 
it's embedded in the string produced from the substring function.

The function which generates an org link is org-make-link-string.
Perhaps if you read the source for that function you will discover how links 
are assembled and therein obtain insights into how to disassemble them.

Here's a snippet of some code I crafted while going the other way ... starting 
with an URL then generating an org-mode link:

(defun 
  ddg2org-link 
 (
  lst ; the argument
 )
 "For every duckduckgo URL in lst produce an org-mode link"
 (interactive)
 (mapcar
 (lambda (ddg-URL)
    (org-make-link-string 
        ddg-URL              ; LINK
        (mapconcat
           #'identity                    ; FUNCTION 
           (split-string 
            (cadr                   ;\
              (split-string         ; \
                ddg-URL ; STRING    ;  > ; SEQUENCE 
               "?q="    ; SEPARATOR ; /
              )                     ;/
             )
             "%20"                       ; SEPARATOR for split-string
            );split-string
           " "                           ; SEPARATOR for mapconcat
        ) ; mapconcat
   ) ;org-make-link-string
  ) ; lambda
  lst
 );mapcar
)

; test it with a list of ddg URLs
(ddg2org-link 
'(
  "https://duckduckgo.com/html/?q=monsters%20are%20real%20stephen%20king";
  "https://duckduckgo.com/html/?q=love%20map";
))


I hope these clues help you think your way through to a workable solution.

Cheers!


reply via email to

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