123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- (require 'org)
- (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."
- (when (eq major-mode 'eww-mode)
- (org-store-link-props
- :type "eww"
- :link (if (< emacs-major-version 25)
- eww-current-url
- (eww-current-url))
- :url (url-view-url t)
- :description (if (< emacs-major-version 25)
- (or eww-current-title eww-current-url)
- (or (plist-get eww-data :title)
- (eww-current-url))))))
- (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-has-further-url-property-change-p ()
- "Return t if there is a next url property change else nil."
- (save-excursion
- (not (eq (point) (org-eww-goto-next-url-property-change)))))
- (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))
- (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.
- Further lines starting with a star get quoted with a comma to keep
- the structure of the org file."
- (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))
-
- (if (fboundp 'deactivate-mark) (deactivate-mark)))
- (message "Transforming links...")
- (save-excursion
- (goto-char transform-start)
- (while (and (not out-bound)
- (org-eww-has-further-url-property-change-p))
-
- (setq temp-position (point))
-
- (or (org-eww-url-below-point)
- (org-eww-goto-next-url-property-change))
- (assert (org-eww-url-below-point) t
- "program logic error: point must have an url below but it hasn't")
- (if (<= (point) transform-end)
- (progn
-
- (if (< temp-position (point))
- (setq return-content (concat return-content
- (buffer-substring
- temp-position (point)))))
-
- (setq link-location (org-eww-url-below-point))
-
- (setq link-title
- (buffer-substring
- (point)
- (org-eww-goto-next-url-property-change)))
-
- (setq return-content (concat return-content
- (org-make-link-string
- link-location link-title))))
- (goto-char temp-position)
- (setq out-bound t)
- ))
-
- (if (< (point) transform-end)
- (setq return-content
- (concat return-content
- (buffer-substring (point) transform-end))))
-
- (org-kill-new
- (with-temp-buffer
- (insert return-content)
- (goto-char 0)
- (while (re-search-forward "^\*" nil t)
- (replace-match ",*"))
- (buffer-string)))
- (message "Transforming links...done, use C-y to insert text into Org-mode file"))))
- (defun org-eww-extend-eww-keymap ()
- (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))
- (when (and (boundp 'eww-mode-map)
- (keymapp eww-mode-map))
- (org-eww-extend-eww-keymap))
- (add-hook
- 'eww-mode-hook
- (lambda () (org-eww-extend-eww-keymap)))
- (provide 'org-eww)
|