1
0
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-11-30 08:08:26 +00:00
org-mode/lisp/ol-eww.el
Rick Lupton 95554543b9
org-id.el: Add search strings, inherit parent IDs
* lisp/ol.el (org-store-link): Refactor org-id links to use standard
`org-store-link-functions'.
(org-link-search): Create new headings at appropriate level.
(org-link-precise-link-target): New function extracting logic to
identify a precise link target, e.g. a heading, named object, or text
search.
(org-link-try-link-store-functions): Extract logic to call external
link store functions. Pass them a new `interactive?' argument.
* lisp/ol-bbdb.el (org-bbdb-store-link):
* lisp/ol-bibtex.el (org-bibtex-store-link):
* lisp/ol-docview.el (org-docview-store-link):
* lisp/ol-eshell.el (org-eshell-store-link):
* lisp/ol-eww.el (org-eww-store-link):
* lisp/ol-gnus.el (org-gnus-store-link):
* lisp/ol-info.el (org-info-store-link):
* lisp/ol-irc.el (org-irc-store-link):
* lisp/ol-man.el (org-man-store-link):
* lisp/ol-mhe.el (org-mhe-store-link):
* lisp/ol-rmail.el (org-rmail-store-link): Accept optional arg.
* lisp/org-id.el (org-id-link-consider-parent-id): New option to allow
a parent heading with an id to be considered as a link target.
(org-id-link-use-context): New option to add context to org-id links.
(org-id-get): Add optional `inherit' argument which considers parents'
IDs if the current entry does not have one.
(org-id-store-link): Consider IDs of parent headings as link targets
when current heading has no ID and `org-id-link-consider-parent-id' is
set.  Add a search string to the link when enabled.
(org-id-store-link-maybe): Function set as :store option for custom id
link property. Move logic from `org-store-link' here to determine when
an org-id link should be stored using `org-id-store-link'.
(org-id-open): Recognise search strings after "::" in org-id links.
* lisp/org-lint.el: Add checker for "::" in ID properties.
* testing/lisp/test-ol.el: Add tests for
`org-link-precise-link-target' and `org-id-store-link' functions,
testing new options.
* doc/org-manual.org: Update documentation about links.
* etc/ORG-NEWS: Document changes and new options.

These feature allows for more precise links when using org-id to link to
org headings, without requiring every single headline to have an id.

Link: https://list.orgmode.org/118435e8-0b20-46fd-af6a-88de8e19fac6@app.fastmail.com/
2024-02-24 15:56:44 +03:00

179 lines
6.0 KiB
EmacsLisp
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; ol-eww.el --- Store URL and kill from Eww mode -*- lexical-binding: t -*-
;; Copyright (C) 2014-2024 Free Software Foundation, Inc.
;; Author: Marco Wahl <marcowahlsoft>a<gmailcom>
;; Keywords: link, eww
;; URL: https://orgmode.org
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; When this module is active `org-store-link' (often on key C-c l) in
;; an EWW buffer stores a link to the current url of the eww buffer.
;; 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 buffer with
;; hot links.
;; C-c C-x C-w (and also 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 based on the idea of Richard
;; Riley. Thanks!
;; Potential: Since the code for w3m and eww is so similar one could
;; try to refactor.
;;; Code:
(require 'org-macs)
(org-assert-version)
(require 'ol)
(require 'cl-lib)
(require 'eww)
;; Store Org link in Eww mode buffer
(org-link-set-parameters "eww"
:follow #'org-eww-open
:store #'org-eww-store-link)
(defun org-eww-open (url _)
"Open URL with Eww in the current buffer."
(eww url))
(defun org-eww-store-link (&optional _interactive?)
"Store a link to the url of an EWW buffer."
(when (eq major-mode 'eww-mode)
(org-link-store-props
:type "eww"
:link (eww-current-url)
:url (url-view-url t)
:description (or (plist-get eww-data :title)
(eww-current-url)))))
;; Some auxiliary functions concerning links in Eww buffers
(defun org-eww-goto-next-url-property-change ()
"Move to the start of next link if exists.
Otherwise point is not moved. Return point."
(goto-char
(or (next-single-property-change (point) 'shr-url)
(point))))
(defun org-eww-has-further-url-property-change-p ()
"Non-nil if there is a next url property change."
(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-link-make-string' and insert the transformed text 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))
;; Deactivate mark if current mark is activate.
(deactivate-mark))
(message "Transforming links...")
(save-excursion
(goto-char transform-start)
(while (and (not out-bound) ; still inside region to copy
(org-eww-has-further-url-property-change-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)
(org-eww-goto-next-url-property-change))
(cl-assert
(org-eww-url-below-point) 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.
(when (< temp-position (point))
(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
(if (org-string-nw-p link-location)
;; Hint: link-location is different
;; for form-elements.
(org-link-make-string link-location link-title)
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.
(when (< (point) transform-end)
(setq return-content
(concat return-content
(buffer-substring (point) transform-end))))
;; Quote lines starting with *.
(org-kill-new (replace-regexp-in-string "^\\*" ",*" return-content))
(message "Transforming links...done, use C-y to insert text into Org mode file"))))
;; Additional keys for eww-mode
(defun org-eww-extend-eww-keymap ()
"Add ol-eww bindings to `eww-mode-map'."
(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)) ; eww is already up.
(org-eww-extend-eww-keymap))
(add-hook 'eww-mode-hook #'org-eww-extend-eww-keymap)
(provide 'ol-eww)
;;; ol-eww.el ends here