emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-insert-link with HTML title as default description
@ 2012-09-29 12:23 Miro Bezjak
  2012-09-29 14:42 ` Bastien
  0 siblings, 1 reply; 6+ messages in thread
From: Miro Bezjak @ 2012-09-29 12:23 UTC (permalink / raw)
  To: emacs-orgmode

Hi all.

I'm new to the whole emacs/elisp/org-mode thing and I have to say I'm
amazed by it. Thank you for all the hard work.

That having been said, I've hacked together two functions that are
useful to me. I was wondering what are your thoughts on including
their idea (but with a better implementation) to org-mode? I've done
some reasonable googling but haven't concluded that org-mode has
something similar.


8<------------------------------------------------------------------------

(defun my-org-insert-link ()
  "Insert org link where default description is set to html title."
  (interactive)
  (let* ((url (read-string "URL: "))
         (title (get-html-title-from-url url)))
    (org-insert-link nil url title)))

(defun get-html-title-from-url (url)
  "Return content in <title> tag."
  (let (x1 x2 (download-buffer (url-retrieve-synchronously url)))
    (save-excursion
      (set-buffer download-buffer)
      (beginning-of-buffer)
      (setq x1 (search-forward "<title>"))
      (search-forward "</title>")
      (setq x2 (search-backward "<"))
      (buffer-substring-no-properties x1 x2))))

-------------------------------------------------------------------------->8

Cheers,
Miro

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

* Re: org-insert-link with HTML title as default description
  2012-09-29 12:23 org-insert-link with HTML title as default description Miro Bezjak
@ 2012-09-29 14:42 ` Bastien
  2012-09-29 15:09   ` Sylvain Rousseau
  0 siblings, 1 reply; 6+ messages in thread
From: Bastien @ 2012-09-29 14:42 UTC (permalink / raw)
  To: Miro Bezjak; +Cc: emacs-orgmode

Hi Miro,

Miro Bezjak <bezjak.miro@gmail.com> writes:

> That having been said, I've hacked together two functions that are
> useful to me. I was wondering what are your thoughts on including
> their idea (but with a better implementation) to org-mode? 

Nice.

I'm not really in favor of including this command because it depends on
whether the web is accessible or not.  When it is, I guess most people
store links with C-c C-l (in w3m) or use a capture template -- but some
might enjoy your hack.

IMHO this is a good candidates for the hacks we add on this page:

  http://orgmode.org/worg/org-hacks.html

If you want to contribute to Worg please send me your public key.

Thanks!

-- 
 Bastien

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

* Re: org-insert-link with HTML title as default description
  2012-09-29 14:42 ` Bastien
@ 2012-09-29 15:09   ` Sylvain Rousseau
  2012-09-29 18:43     ` Miro Bezjak
  2012-09-29 20:53     ` Bastien
  0 siblings, 2 replies; 6+ messages in thread
From: Sylvain Rousseau @ 2012-09-29 15:09 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode, Miro Bezjak

Hi Miro and Bastien,

This can be done by setting the function
`org-make-link-description-function'. However when set, the function
is supposed to handle all type of links and return a string no matter
what. There is no fallback mechanism. Here is a patch that fixes it:


diff --git a/lisp/org.el b/lisp/org.el
index bdb85de..3630623 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -9527,10 +9527,12 @@ Use TAB to complete link prefixes, then RET for type-spe
            (setq desc path))))

     (if org-make-link-description-function
-       (setq desc (funcall org-make-link-description-function link desc))
-      (if default-description (setq desc default-description)
-       (setq desc (or (and auto-desc desc)
-                      (read-string "Description: " desc)))))
+       (setq desc (or (funcall org-make-link-description-function link desc)
+                      desc)))
+
+    (if default-description (setq desc default-description)
+      (setq desc (or (and auto-desc desc)
+                    (read-string "Description: " desc))))

     (unless (string-match "\\S-" desc) (setq desc nil))
     (if remove (apply 'delete-region remove))


For example my `org-make-link-description-function' is:

    (setq org-link-to-description
          '(("\\`file:.*/\\([^/:]+\\)\\(::.*\\)" . "\\1")
            ("\\`file:.*/\\([^/:]+\\)" . "\\1")))

    (setq org-make-link-description-function
          (lambda (link description)
            (let ((found (assoc-default link org-link-to-description
'string-match)))
              (cond
               ((stringp found) (match-substitute-replacement found t
nil link))))))


HTH,

Sylvain.

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

* Re: org-insert-link with HTML title as default description
  2012-09-29 15:09   ` Sylvain Rousseau
@ 2012-09-29 18:43     ` Miro Bezjak
  2012-09-29 20:53     ` Bastien
  1 sibling, 0 replies; 6+ messages in thread
From: Miro Bezjak @ 2012-09-29 18:43 UTC (permalink / raw)
  To: Sylvain Rousseau; +Cc: Bastien, emacs-orgmode

Hi Sylvain, Bastien,

I have indeed noticed `org-make-link-description-function'. Though,
from its documentation I've somehow concluded that it makes the final
(and not default) description. By rereading documentation and source
code for `org-insert-link', it appears that with addition of Sylvain's
patch I could indeed use `org-make-link-description-function' and not
wrap around `org-insert-link'.

Cheers,
Miro

P.S. I'm sending SSH public key in a separate mail.

On Sat, Sep 29, 2012 at 5:09 PM, Sylvain Rousseau <thisirs@gmail.com> wrote:
> Hi Miro and Bastien,
>
> This can be done by setting the function
> `org-make-link-description-function'. However when set, the function
> is supposed to handle all type of links and return a string no matter
> what. There is no fallback mechanism. Here is a patch that fixes it:
>
>
> diff --git a/lisp/org.el b/lisp/org.el
> index bdb85de..3630623 100644
> --- a/lisp/org.el
> +++ b/lisp/org.el
> @@ -9527,10 +9527,12 @@ Use TAB to complete link prefixes, then RET for type-spe
>             (setq desc path))))
>
>      (if org-make-link-description-function
> -       (setq desc (funcall org-make-link-description-function link desc))
> -      (if default-description (setq desc default-description)
> -       (setq desc (or (and auto-desc desc)
> -                      (read-string "Description: " desc)))))
> +       (setq desc (or (funcall org-make-link-description-function link desc)
> +                      desc)))
> +
> +    (if default-description (setq desc default-description)
> +      (setq desc (or (and auto-desc desc)
> +                    (read-string "Description: " desc))))
>
>      (unless (string-match "\\S-" desc) (setq desc nil))
>      (if remove (apply 'delete-region remove))
>
>
> For example my `org-make-link-description-function' is:
>
>     (setq org-link-to-description
>           '(("\\`file:.*/\\([^/:]+\\)\\(::.*\\)" . "\\1")
>             ("\\`file:.*/\\([^/:]+\\)" . "\\1")))
>
>     (setq org-make-link-description-function
>           (lambda (link description)
>             (let ((found (assoc-default link org-link-to-description
> 'string-match)))
>               (cond
>                ((stringp found) (match-substitute-replacement found t
> nil link))))))
>
>
> HTH,
>
> Sylvain.

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

* Re: org-insert-link with HTML title as default description
  2012-09-29 15:09   ` Sylvain Rousseau
  2012-09-29 18:43     ` Miro Bezjak
@ 2012-09-29 20:53     ` Bastien
  2012-09-29 23:00       ` Sylvain Rousseau
  1 sibling, 1 reply; 6+ messages in thread
From: Bastien @ 2012-09-29 20:53 UTC (permalink / raw)
  To: Sylvain Rousseau; +Cc: emacs-orgmode, Miro Bezjak

Hi Sylvain,

Sylvain Rousseau <thisirs@gmail.com> writes:

> This can be done by setting the function
> `org-make-link-description-function'. However when set, the function
> is supposed to handle all type of links and return a string no matter
> what. There is no fallback mechanism. Here is a patch that fixes it:

Thanks for pointing at this -- I implemented a different fallback 
that interactively prompt for a description when the adhoc function
fails (I also prevented an error in this function to stop the
insertion of a link.)

Let me know if this is okay for you.

Best,

-- 
 Bastien

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

* Re: org-insert-link with HTML title as default description
  2012-09-29 20:53     ` Bastien
@ 2012-09-29 23:00       ` Sylvain Rousseau
  0 siblings, 0 replies; 6+ messages in thread
From: Sylvain Rousseau @ 2012-09-29 23:00 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode, Miro Bezjak

This is definitely better!

Your version is actually more faithful to the original one as it
ignores `default-description' when
`org-make-link-description-function' is set (and succeed).

Cheers,

Sylvain.

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

end of thread, other threads:[~2012-09-29 23:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-29 12:23 org-insert-link with HTML title as default description Miro Bezjak
2012-09-29 14:42 ` Bastien
2012-09-29 15:09   ` Sylvain Rousseau
2012-09-29 18:43     ` Miro Bezjak
2012-09-29 20:53     ` Bastien
2012-09-29 23:00       ` Sylvain Rousseau

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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