[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/denote a86267786b 15/32: Add denote-org.el, which defin
|
From: |
ELPA Syncer |
|
Subject: |
[elpa] externals/denote a86267786b 15/32: Add denote-org.el, which defines 'denote-org-link-to-heading' command |
|
Date: |
Sat, 20 Jan 2024 00:57:42 -0500 (EST) |
branch: externals/denote
commit a86267786b5b51dd26b157bda98ed5fbe3b7b40a
Author: Protesilaos Stavrou <info@protesilaos.com>
Commit: Protesilaos Stavrou <info@protesilaos.com>
Add denote-org.el, which defines 'denote-org-link-to-heading' command
---
denote-org.el | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 113 insertions(+)
diff --git a/denote-org.el b/denote-org.el
new file mode 100644
index 0000000000..0afc9f9c56
--- /dev/null
+++ b/denote-org.el
@@ -0,0 +1,113 @@
+;;; denote-org.el --- Denote extensions for Org mode -*- lexical-binding: t -*-
+
+;; Copyright (C) 2024 Free Software Foundation, Inc.
+
+;; Author: Protesilaos Stavrou <info@protesilaos.com>
+;; Maintainer: Protesilaos Stavrou <info@protesilaos.com>
+;; URL: https://github.com/protesilaos/denote
+
+;; This file is NOT part of GNU Emacs.
+
+;; This program 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.
+;;
+;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+;; WORK-IN-PROGRESS
+
+;;; Code:
+
+(require 'denote)
+(require 'org)
+
+;;;; Link to file and heading
+
+(defun denote-org--get-outline (file)
+ "Return `outline-regexp' headings and line numbers of FILE."
+ (with-current-buffer (find-file-noselect file)
+ (let ((outline-regexp (format "^\\(?:%s\\)" (or (bound-and-true-p
outline-regexp) "[*\^L]+")))
+ candidates)
+ (save-excursion
+ (goto-char (point-min))
+ (while (if (bound-and-true-p outline-search-function)
+ (funcall outline-search-function)
+ (re-search-forward outline-regexp nil t))
+ (push
+ ;; NOTE 2024-01-20: The -5 (minimum width) is a
+ ;; sufficiently high number to keep the alignment
+ ;; consistent in most cases. Larger files will simply
+ ;; shift the heading text in minibuffer, but this is not an
+ ;; issue anymore.
+ (format "%-5s %s"
+ (line-number-at-pos (point))
+ (buffer-substring-no-properties (line-beginning-position)
(line-end-position)))
+ candidates)
+ (goto-char (1+ (line-end-position)))))
+ (if candidates
+ (nreverse candidates)
+ (user-error "No outline")))))
+
+(defun denote-org--outline-prompt (&optional file)
+ "Prompt for outline among headings retrieved by `denote-org--get-outline'.
+With optional FILE use the outline of it, otherwise use that of
+the current file."
+ (completing-read
+ "Go to outline: "
+ (denote--completion-table-no-sort 'imenu (denote-org--get-outline (or file
buffer-file-name)))
+ nil :require-match))
+
+(defun denote-org--get-heading-and-id-from-line (line file)
+ "Return heading text and CUSTOM_ID from the given LINE in FILE."
+ (with-current-buffer (find-file-noselect file)
+ (save-excursion
+ (goto-char (point-min))
+ (forward-line line)
+ (cons (org-get-heading :no-tags :no-todo :no-priority :no-comment)
+ (denote-link-ol-get-id)))))
+
+(defun denote-org-format-link-with-heading (file heading-id description)
+ "Prepare link to FILE with HEADING-ID using DESCRIPTION.
+
+FILE-TYPE and ID-ONLY are used to get the format of the link.
+See the `:link' property of `denote-file-types'."
+ (format "[[denote:%s::#%s][%s]]"
+ (denote-retrieve-filename-identifier file)
+ heading-id
+ description))
+
+(defun denote-org-format-link-get-description (file heading-text)
+ "Return link description for FILE with HEADING-TEXT at the end."
+ (format "%s::%s"
+ (denote--retrieve-title-or-filename file 'org)
+ heading-text))
+
+;;;###autoload
+(defun denote-org-link-to-heading ()
+ "Link to file and then specify a heading to extend the link to.
+
+The resulting link has the following pattern:
+
+[[denote:IDENTIFIER::#ORG-HEADING-CUSTOM-ID]][File title::Heading text]]."
+ (declare (interactive-only t))
+ (interactive)
+ (when-let ((file (denote-file-prompt ".*\\.org"))
+ (heading (denote-org--outline-prompt file))
+ (line (string-to-number (car (split-string heading "\t"))))
+ (heading-data (denote-org--get-heading-and-id-from-line line
file))
+ (heading-text (car heading-data))
+ (heading-id (cdr heading-data))
+ (description (denote-org-format-link-get-description file
heading-text)))
+ (insert (denote-org-format-link-with-heading file heading-id
description))))
+
+(provide 'denote-org)
+;;; denote-org.el ends here
- [elpa] externals/denote d21e5e0644 31/32: Merge pull request #227 from jeanphilippegg/fix-denote--trim-right-token-characters, (continued)
- [elpa] externals/denote d21e5e0644 31/32: Merge pull request #227 from jeanphilippegg/fix-denote--trim-right-token-characters, ELPA Syncer, 2024/01/20
- [elpa] externals/denote 48b8abc43d 29/32: Merge pull request #226 from jeanphilippegg/empty-region-and-links, ELPA Syncer, 2024/01/20
- [elpa] externals/denote a4f11d3a77 32/32: Simplify regexp in denote--trim-right-token-characters, ELPA Syncer, 2024/01/20
- [elpa] externals/denote 3319970d08 02/32: Make links to Org headings only work for Org, ELPA Syncer, 2024/01/20
- [elpa] externals/denote 2957fe832d 04/32: Expand the documentation of user option to link to Org heading, ELPA Syncer, 2024/01/20
- [elpa] externals/denote 10b9b984e4 07/32: Add denote--completion-table-no-sort, ELPA Syncer, 2024/01/20
- [elpa] externals/denote cc393d7616 05/32: Rename user option to denote-org-store-link-to-heading and document it better, ELPA Syncer, 2024/01/20
- [elpa] externals/denote 10d332bb29 11/32: Expand the documentation of user option to link to Org heading, ELPA Syncer, 2024/01/20
- [elpa] externals/denote 2e04e409ae 13/32: Fix typo in the manual, ELPA Syncer, 2024/01/20
- [elpa] externals/denote cc02a5160d 14/32: Merge branch 'link-to-org-headings' of github.com:protesilaos/denote into link-to-org-headings, ELPA Syncer, 2024/01/20
- [elpa] externals/denote a86267786b 15/32: Add denote-org.el, which defines 'denote-org-link-to-heading' command,
ELPA Syncer <=
- [elpa] externals/denote 423122e5e6 17/32: Clarify that org-insert-link is needed after org-store-link, ELPA Syncer, 2024/01/20
- [elpa] externals/denote ff61ed7fed 20/32: Define denote-link-ol-get-heading function, ELPA Syncer, 2024/01/20
- [elpa] externals/denote 8faccbd216 23/32: Mention denote-org-store-link-to-heading in the link handler, ELPA Syncer, 2024/01/20
- [elpa] externals/denote cdb8a19116 26/32: Document the denote-org-link-to-heading command in the manual, ELPA Syncer, 2024/01/20
- [elpa] externals/denote cfd8961ac4 27/32: Mention 'denote-link' in 'denote-org-link-to-heading' doc string, ELPA Syncer, 2024/01/20
- [elpa] externals/denote e11a468873 28/32: Merge pull request #225 from jeanphilippegg/file-name-rules, ELPA Syncer, 2024/01/20
- [elpa] externals/denote 3110c6dd16 30/32: Fix denote--trim-right-token-characters, ELPA Syncer, 2024/01/20