emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Kill in eww and keep the links for org
@ 2014-10-07 10:11 Marco Wahl
  2014-10-07 10:41 ` Nicolas Goaziou
  0 siblings, 1 reply; 3+ messages in thread
From: Marco Wahl @ 2014-10-07 10:11 UTC (permalink / raw)
  To: emacs-orgmode

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

Hi,

to kill in a w3m-buffer and yank into an org-mode-buffer by keeping the
links is a long known feature in org-module org-w3m.  The patch below
provides this functionality for eww.  Note that the code is very similar
to the code for the w3m case.

What do you think?  Shall I try to push the code?


Ciao,  Marco


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch: copy from eww to org and keep the links --]
[-- Type: text/x-diff, Size: 5975 bytes --]

From 5f03c47861a828b7a75d890e05121c028a003296 Mon Sep 17 00:00:00 2001
From: Marco Wahl <marcowahlsoft@gmail.com>
Date: Thu, 2 Oct 2014 23:09:11 +0200
Subject: [PATCH] org-eww.el: Special kill in eww for Org for keeping the links

* contrib/lisp/org-eww.el(org-eww-copy-for-org-mode): Realization of
  the kill.
  (org-eww-goto-next-url-property-change,
  org-eww-no-next-link-p, org-eww-url-below-point,
  org-eww-url-below-point-p): Auxiliaries for accessing a eww buffer

The code has emerged from the very similar org-w3m.el.
---
 contrib/lisp/org-eww.el | 107 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 101 insertions(+), 6 deletions(-)

diff --git a/contrib/lisp/org-eww.el b/contrib/lisp/org-eww.el
index c25057d..3aae37a 100644
--- a/contrib/lisp/org-eww.el
+++ b/contrib/lisp/org-eww.el
@@ -1,4 +1,4 @@
-;;; org-eww.el --- Storing link in eww-mode for Org-mode
+;;; org-eww.el --- Store url and kill from eww-mode for Org
 
 ;; Copyright (C) 2014 Free Software Foundation, Inc.
 
@@ -27,17 +27,25 @@
 ;; When this module is active `org-store-link' (often on key C-c l) in
 ;; a eww buffer stores a link to the current url of the eww buffer.
 
-;; `org-eww-store-link' below is almost the same as
-;; `org-w3m-store-link' of the org-w3m module.
+;; In an eww buffer function `org-eww-copy-for-org-mode' kills either
+;; a region or the whole buffer if no region is set and transforms the
+;; text on the fly so that it can be pasted into an org-mode buffer
+;; with hot links.
 
-;; Hint: There are further features in module org-w3m which might be
-;; interesting for org-eww also.
+;; C-c C-x C-w and C-c C-x M-w trigger `org-eww-copy-for-org-mode'.
+
+;; Hint: A lot of code of this module comes from module org-w3m which
+;; has been written by Andy Steward.  Thanks!
+
+;; Potential: Since the code for w3m and eww is so similar one could
+;; try to refactor.
 
 \f
 ;;; Code:
-
 (require 'org)
 
+\f
+;; Store Org-link in eww-mode buffer
 (add-hook 'org-store-link-functions 'org-eww-store-link)
 (defun org-eww-store-link ()
   "Store a link to the url of a eww buffer."
@@ -49,6 +57,93 @@
      :description (or eww-current-title eww-current-url))))
 
 \f
+;; Some auxiliary functions concerning links in eww buffers
+(defun org-eww-goto-next-url-property-change ()
+  "Move cursor to the start of next link if exists.  Else no
+move.  Return point."
+  (goto-char
+   (or (next-single-property-change (point) 'shr-url)
+       (point))))
+
+(defun org-eww-no-next-link-p ()
+  "Whether there is no next link after the cursor.
+Return t if there is no next link; otherwise, return nil."
+  (save-excursion
+    (equal (point) (org-eww-goto-next-url-property-change))))
+
+(defun org-eww-url-below-point-p ()
+  "Return t if below point there is a url; otherwise, return nil."
+  (not (eq nil (org-eww-url-below-point))))
+
+(defun org-eww-url-below-point ()
+  "Return the url below point if there is an url; otherwise, return nil."
+  (get-text-property (point) 'shr-url))
+
+\f
+(defun org-eww-copy-for-org-mode ()
+  "Copy current buffer content or active region with `org-mode' style links.
+This will encode `link-title' and `link-location' with
+`org-make-link-string', and insert the transformed test into the kill ring,
+so that it can be yanked into an Org-mode buffer with links working correctly."
+  (interactive)
+  (let* ((regionp (org-region-active-p))
+         (transform-start (point-min))
+         (transform-end (point-max))
+         return-content
+         link-location link-title
+         temp-position out-bound)
+    (when regionp
+      (setq transform-start (region-beginning))
+      (setq transform-end (region-end))
+      ;; Deactivate mark if current mark is activate.
+      (if (fboundp 'deactivate-mark) (deactivate-mark)))
+    (message "Transforming links...")
+    (save-excursion
+      (goto-char transform-start)
+      (while (and (not out-bound)                 ; still inside region to copy
+                  (not (org-eww-no-next-link-p))) ; there is a next link
+        ;; store current point before jump next anchor
+        (setq temp-position (point))
+        ;; move to next anchor when current point is not at anchor
+        (or (org-eww-url-below-point-p)
+	    (org-eww-goto-next-url-property-change))
+	(assert (org-eww-url-below-point-p) t
+                "program logic error: point must have an url below but it hasn't")
+	(if (<= (point) transform-end)  ; if point is inside transform bound
+	    (progn
+	      ;; get content between two links.
+	      (if (> (point) temp-position)
+		  (setq return-content (concat return-content
+					       (buffer-substring
+						temp-position (point)))))
+	      ;; get link location at current point.
+	      (setq link-location (org-eww-url-below-point))
+	      ;; get link title at current point.
+	      (setq link-title
+		    (buffer-substring
+		     (point)
+		     (org-eww-goto-next-url-property-change)))
+              ;; concat `org-mode' style url to `return-content'.
+              (setq return-content (concat return-content
+                                           (org-make-link-string
+                                            link-location link-title))))
+	  (goto-char temp-position)     ; reset point before jump next anchor
+	  (setq out-bound t)            ; for break out `while' loop
+	  ))
+      ;; add the rest until end of the region to be copied
+      (if (< (point) transform-end)
+          (setq return-content
+                (concat return-content
+                        (buffer-substring (point) transform-end))))
+      (org-kill-new return-content)
+      (message "Transforming links...done, use C-y to insert text into Org-mode file"))))
+
+\f
+;; Additional keys for eww-mode
+(define-key eww-mode-map "\C-c\C-x\M-w" 'org-eww-copy-for-org-mode)
+(define-key eww-mode-map "\C-c\C-x\C-w" 'org-eww-copy-for-org-mode)
+
+\f
 (provide 'org-eww)
 
 ;;; org-eww.el ends here
-- 
2.1.2


[-- Attachment #3: Type: text/plain, Size: 44 bytes --]


-- 
http://www.wahlzone.de
PGP: 0x0A3AE6F2

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

* Re: [PATCH] Kill in eww and keep the links for org
  2014-10-07 10:11 [PATCH] Kill in eww and keep the links for org Marco Wahl
@ 2014-10-07 10:41 ` Nicolas Goaziou
  2014-10-07 14:16   ` Marco Wahl
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Goaziou @ 2014-10-07 10:41 UTC (permalink / raw)
  To: Marco Wahl; +Cc: emacs-orgmode

Hello,

Marco Wahl <marcowahlsoft@gmail.com> writes:

> to kill in a w3m-buffer and yank into an org-mode-buffer by keeping the
> links is a long known feature in org-module org-w3m.  The patch below
> provides this functionality for eww.  Note that the code is very similar
> to the code for the w3m case.
>
> What do you think?  Shall I try to push the code?

Since the file is in contrib and you're its author, you can obviously
push the code.

Some minor comments follow.

> +;;; org-eww.el --- Store url and kill from eww-mode for Org

Eww mode

> +(defun org-eww-no-next-link-p ()
> +  "Whether there is no next link after the cursor.
> +Return t if there is no next link; otherwise, return nil."
> +  (save-excursion
> +    (equal (point) (org-eww-goto-next-url-property-change))))

  (eq (point) (org-eww-goto-next-url-property-change))

> +(defun org-eww-url-below-point-p ()
> +  "Return t if below point there is a url; otherwise, return nil."
> +  (not (eq nil (org-eww-url-below-point))))

  (org-eww-url-below-point)

or, if you insist on returning t,

  (and (org-eww-url-below-point) t)


Regards,

-- 
Nicolas Goaziou

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

* Re: [PATCH] Kill in eww and keep the links for org
  2014-10-07 10:41 ` Nicolas Goaziou
@ 2014-10-07 14:16   ` Marco Wahl
  0 siblings, 0 replies; 3+ messages in thread
From: Marco Wahl @ 2014-10-07 14:16 UTC (permalink / raw)
  To: emacs-orgmode

Hi!

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> Since the file is in contrib and you're its author, you can obviously
> push the code.

Thanks for making that clear.

> Some minor comments follow.
>
>> +;;; org-eww.el --- Store url and kill from eww-mode for Org
>
> Eww mode
>
>> +    (equal (point) (org-eww-goto-next-url-property-change))))
>
>   (eq (point) (org-eww-goto-next-url-property-change))
>
>> +  (not (eq nil (org-eww-url-below-point))))
>
>   (org-eww-url-below-point)
>
> or, if you insist on returning t,
>
>   (and (org-eww-url-below-point) t)

I insist ;)

Thanks for your hints.  Applied and pushed.


Best regards,  Marco
-- 
http://www.wahlzone.de
PGP: 0x0A3AE6F2

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

end of thread, other threads:[~2014-10-07 14:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-07 10:11 [PATCH] Kill in eww and keep the links for org Marco Wahl
2014-10-07 10:41 ` Nicolas Goaziou
2014-10-07 14:16   ` Marco Wahl

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