toshism/org-super-links

If a heading contains no text, sl-store-link / sl-insert-link fails

hpgisler opened this issue · 7 comments

Version 0.3:

How to reproduce:

Assume to have to follwoing 2 headings in an org-file:

* TODO Heading A :hgi:
DEADLINE: <2020-10-30 Fr>
:PROPERTIES:
:ID:       23ececef-b9d4-473a-bc02-6a06c4dc8c9a
:END:

* TODO Heading B :hgi:
SCHEDULED: <2020-10-14 Mi>
:PROPERTIES:
:ID:       1778ed38-ee23-4cf4-9ded-5e700913e394
:END:
:LOGBOOK:
- State "TODO"       from "____"       [2020-10-11 So 13:07]
:END:

some text

  1. Place point somewhere under 'Heading A'
  2. M-x sl-store-link
  3. Place point below 'some text' of 'Heading B'
  4. M-x sl-insert-link

Result:
The inserted link refers to 'Heading B' instead to 'Heading A'

Expected result:
The inserted link refers to 'Heading A'

Note:
The backlink however seems to be correct

Hmm... I am actually not able to recreate this. Could you post any non-default configuration options you've set?

Thank you for your help.

Below an exceprt of my .emacs file (hope I've got all the relevant - org-mode related - parts).
If you want the whole file (a bit large), let me know.

Note:

  • below comment 'org super links', you find the settings related to the super links package.
  • I've also rebound the shortcut-keys
  • I'am using: (add-to-list 'package-archives '("org" . "https://orgmode.org/elpa/") t) ;; perhaps this makes a difference?
(add-to-list 'package-archives '("org" . "https://orgmode.org/elpa/") t)
(require 'use-package)
(require 'org)
(require 'ox-odt)
(with-eval-after-load 'org
  (require 'org-id)
  (require 'ox-beamer)
  (require 'org-notmuch)

;;--------------------------------------------------------------------------------
;; org-mode

(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cc" 'my-org-capture)
(global-set-key "\C-cb" 'org-iswitchb)

;;(use-package org-bullets
;;  :config
;;  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1))))

(setq org-columns-default-format "%50ITEM(Task) %10CLOCKSUM %16TIMESTAMP_IA"
      org-todo-keywords '((sequence "TODO(t)" "WAIT(w)" "|" "DONE(d!)")
			  (type "WAIT(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@)")))

(setq
 org-columns-default-format "%80ITEM(Task) %10Estimate{:} %10CLOCKSUM"
 org-global-properties (quote (("Estimate_ALL" . "0:15 0:30 1:00 2:00 3:00 5:00 8:00 13:00")
			       ("STYLE_ALL" . "habit")))
 org-agenda-log-mode-items (quote (closed state))
 org-log-into-drawer t ;; 't means: use default drawler 'LOGBOOK' to put logs intoö
 org-ellipsis "…" ;; "➢" "…"
 org-directory "~/org"
 org-startup-indented t
 org-indent-indentation-per-level 2
 org-enforce-todo-dependencies t
 org-catch-invisible-edits t
 org-deadline-warning-days 10
 org-display-inline-images t
 org-agenda-window-setup 'current-window
 org-hierarchical-todo-statistics nil
 org-id-locations-file "~/org/.org-id-locations"
 org-default-notes-file (concat org-directory "/hp.org"))

(setq org-emphasis-alist   
(quote (("*" bold)
("/" italic)
("_" underline)
("=" (:foreground "orange"))
("~" org-verbatim verbatim)
("+"
(:strike-through t))
))) 

(setq org-capture-templates
      '(
	("p" "Private")
	("pa" "Appointment" entry (file+headline org-default-notes-file "Appointments") "* %?\n%t\n%i\n%a" :unnarrowed nil :prepend t)
	("pt" "Todo" entry (file "/home/hp/org/hp.org") "* ____ %?\n%i\n%a" :unnarrowed nil) 
	("pn" "Note" entry (file+headline org-default-notes-file "Notes") "* %?\n%i\n%a" :unnarrowed nil) 
	("pj" "Journal entry" entry (file+olp+datetree org-default-notes-file "Journal") "* %?\n%i\n%a")
        ("w" "Work")
	("wt" "Todos" entry (file "/home/hp/org-trafag/captures.org" ) "* ____ %?\n%i\n%a" :unnarrowed nil) 
	("t" "Todos"  entry (file "/home/hp/org-trafag/captures.org" ) "* ____ %?\n%a\n%i" :unnarrowed nil) 
	))

;; (defun my-org-screenshot ()
;;   "Take a screenshot into a time stamped unique-named file in the same directory as the org-buffer and insert a link to this file."
;;   (interactive)
;;   (setq filename
;;         (concat
;;          (make-temp-name
;;           (concat (buffer-file-name)
;;                   "_"
;;                   (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))
;;   (call-process "import" nil nil nil filename)
;;   (insert (concat "[[" filename "]]"))
;;   (org-display-inline-images))

(defun my-org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in a sub directory as the org-buffer and insert a link to this file."
  (interactive)
  (org-display-inline-images)
  (setq filename
        (concat
         (make-temp-name
          (concat (file-name-nondirectory (buffer-file-name))
                  "_imgs/"
                  (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))
  (unless (file-exists-p (file-name-directory filename))
    (make-directory (file-name-directory filename)))
  ; take screenshot
  (if (eq system-type 'darwin)
      (call-process "screencapture" nil nil nil "-i" filename))
  (if (eq system-type 'gnu/linux)
      (call-process "import" nil nil nil filename))
  ; insert into file if correctly taken
  (if (file-exists-p filename)
      (insert (concat "[[file:" filename "]]")))
      (org-display-inline-images))

(defun my-org-capture ()
  "Call 'org-capture' OR 'org-agenda-capture' depending on the major-mode we are in"
  (interactive)
  (if (eq major-mode 'org-agenda-mode)
      (org-agenda-capture)
    (org-capture)))

(setq browse-url-browser-function 'browse-url-generic
      browse-url-generic-program "chromium")

(setq org-refile-targets (quote ((nil :maxlevel . 9)
				 (org-agenda-files :maxlevel . 9))))

(setq org-refile-allow-creating-parent-nodes (quote confirm))



;; Do not dim blocked tasks
(setq org-agenda-dim-blocked-tasks nil)

;; Compact the block agenda view
(setq org-agenda-compact-blocks t)

)

(with-eval-after-load 'org
(add-hook 'org-mode-hook
          (lambda()
            (local-unset-key (kbd "C-c C-m"))
	    (local-unset-key (kbd "C-c C-l")))))

;;--------------------------------------------------------------------------------
;; org super links

(require 'quelpa)
(require 'quelpa-use-package)
(use-package org-super-links
;;  :quelpa (org-super-links :repo "toshism/org-super-links" :fetcher github :commit "0.3")
  :bind (("C-c i" . sl-link)
	   ("C-c l" . sl-store-link)
	   ("C-c C-l" . sl-insert-link)
	   ("C-c d" . sl-delete-link))
  :init
  (setq sl-backlink-into-drawer nil
	sl-backlink-prefix nil))

;; various settings

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(ansi-color-faces-vector
   [default default default italic underline success warning error])
 '(ansi-color-names-vector
   ["#2d3743" "#ff4242" "#74af68" "#dbdb95" "#34cae2" "#008b8b" "#00ede1" "#e1e1e0"])
 '(blink-cursor-mode nil)
 '(column-number-mode t)
 '(custom-safe-themes
   '("51ec7bfa54adf5fff5d466248ea6431097f5a18224788d0bd7eb1257a4f7b773" "285d1bf306091644fb49993341e0ad8bafe57130d9981b680c1dbd974475c5c7" "830877f4aab227556548dc0a28bf395d0abe0e3a0ab95455731c9ea5ab5fe4e1" "7f1d414afda803f3244c6fb4c2c64bea44dac040ed3731ec9d75275b9e831fe5" "13a8eaddb003fd0d561096e11e1a91b029d3c9d64554f8e897b2513dbf14b277" "c433c87bd4b64b8ba9890e8ed64597ea0f8eb0396f4c9a9e01bd20a04d15d358" "4aee8551b53a43a883cb0b7f3255d6859d766b6c5e14bcb01bed572fcbef4328" "2809bcb77ad21312897b541134981282dc455ccd7c14d74cc333b6e549b824f3" default))
 '(org-agenda-files '("~/org/" "~/org-trafag/"))
 '(org-id-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
 '(org-modules
   '(org-bbdb org-bibtex org-docview org-gnus org-habit org-info org-irc org-mhe org-rmail org-w3m))
 '(org-outline-path-complete-in-steps nil)
 '(org-refile-use-outline-path 'file)
 '(org-tags-column 0)
 '(package-selected-packages
   '(quelpa quelpa-use-package slime slime-company org-bullets solarized-theme yasnippet w3m use-package request-deferred protobuf-mode popup pinentry ox-pandoc ox-odt org-caldav modern-cpp-font-lock magit lsp-ui ivy-prescient htmlize handoff flycheck eglot disable-mouse counsel-projectile counsel-etags company-lsp clang-format calfw-org calfw amx ace-jump-mode))
 '(solarized-distinct-fringe-background t)
 '(solarized-emphasize-indicators nil)
 '(solarized-height-plus-1 1.1)
 '(solarized-scale-org-headlines t)
 '(solarized-use-variable-pitch t))

Below for illustration, a screen shot of the (failing link creation).
Note, that (strangely) the heading '* TODO Heading B :hgi:' is formatted incorrectly after having inserted the link under this heading.

Screenshot from 2020-10-12 08-54-07

For completnes, below a screenshot when it is working correctly, i.e. when having 'some other text' below 'Heading A' and then inserting the link below 'Heading B'

Screenshot from 2020-10-12 09-03-47

The problem seems to be located in this function:

(defun sl-insert-backlink (link desc)
  "Insert backlink to LINK with DESC.
Where the backlink is placed is determined by the variable `sl-backlink-into-drawer'."
  (let* ((org-log-into-drawer (sl-backlink-into-drawer))
	 (description (sl-default-description-formatter link desc))
	 (beg (org-log-beginning t)))
    (goto-char beg)
    (insert (sl-backlink-prefix))
    (org-insert-link nil link description)
    (insert (sl-backlink-postfix))
    (org-indent-region beg (point))))

if sl-backlink-into-drawer is nil, so is org-log-into-drawer.
This means, that (beg (org-log-beginning t))) does move point to * of * TODO Heading B :hgi:; because there is no notes drawer created.

After the function retruns org-store-link is called - still with point on 'Heading B'. Thus the wrong Link.

As a quick hack, I've appended (backward-char 1) as last function call to above sl-insert-backlink function, which mitigates this problem; however, the wrong formatting of Heading B remains, i.e. the .org file has to be reloaded to correctly format it again.

Of course, there has to be cleaner solution to this problem. I leave that up to you.

ah excellent! thanks a lot for the investigation. you have illustrated a hole in my testing approach. I rarely test with all linking in drawers disabled.

I'll work on this ASAP.

thanks again!

Finally had a chance to fix this. Thanks @hpgisler!