0
0
Fork 1
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-09-29 21:37:50 +00:00

ol: Fix some corner cases when normalizing context in links

* lisp/ol.el (org-link--normalize-string): Handle blanks between
special characters.
(org-link-heading-search-string): Small refactoring.
* testing/lisp/test-ol.el (test-ol/store-link): Add tests.
This commit is contained in:
Nicolas Goaziou 2020-03-26 22:57:16 +01:00
parent 5b96988706
commit 7a78eb1be0
2 changed files with 111 additions and 22 deletions

View file

@ -745,20 +745,24 @@ This function removes contiguous white spaces and statistics
cookies. When optional argument CONTEXT is non-nil, it assumes cookies. When optional argument CONTEXT is non-nil, it assumes
STRING is a context string, and also removes special search STRING is a context string, and also removes special search
syntax around the string." syntax around the string."
(when context (let ((string
(while (cond ((and (string-prefix-p "(" string) (org-trim
(string-suffix-p ")" string)) (replace-regexp-in-string
(setq string (substring string 1 -1))) (rx (one-or-more (any " \t")))
((string-match "\\`[#*]+" string) " "
(setq string (substring string (match-end 0)))) (replace-regexp-in-string
(t nil))))
(org-trim
(replace-regexp-in-string
(rx (or (one-or-more (any " \t"))
;; Statistics cookie regexp. ;; Statistics cookie regexp.
(seq "[" (0+ digit) (or "%" (seq "/" (0+ digit))) "]"))) (rx (seq "[" (0+ digit) (or "%" (seq "/" (0+ digit))) "]"))
" " " "
string))) string)))))
(when context
(while (cond ((and (string-prefix-p "(" string)
(string-suffix-p ")" string))
(setq string (org-trim (substring string 1 -1))))
((string-match "\\`[#*]+[ \t]*" string)
(setq string (substring string (match-end 0))))
(t nil))))
string))
;;; Public API ;;; Public API
@ -1251,14 +1255,11 @@ statistics cookies are removed, and contiguous spaces are packed
into a single one. into a single one.
When optional argument STRING is non-nil, assume it a headline, When optional argument STRING is non-nil, assume it a headline,
without any TODO or COMMENT keyword, and without any priority without any asterisk, TODO or COMMENT keyword, and without any
cookie or tag." priority cookie or tag."
(org-link--normalize-string (concat "*"
(if (not string) (org-link--normalize-string
(concat "*" (org-trim (org-get-heading t t t t))) (or string (org-get-heading t t t t)))))
(let ((s (org-trim string)))
(if (string-prefix-p "*" s) s
(concat "*" s))))))
(defun org-link-open-as-file (path arg) (defun org-link-open-as-file (path arg)
"Pretend PATH is a file name and open it. "Pretend PATH is a file name and open it.

View file

@ -263,7 +263,95 @@
(fundamental-mode) (fundamental-mode)
(let ((file (buffer-file-name))) (let ((file (buffer-file-name)))
(equal (format "[[file:%s][file:%s]]" file file) (equal (format "[[file:%s][file:%s]]" file file)
(org-store-link '(16)))))))) (org-store-link '(16)))))))
;; Context does not include special search syntax.
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "(two)"
(fundamental-mode)
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::two]]" file file)
(org-store-link nil))))))
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "# two"
(fundamental-mode)
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::two]]" file file)
(org-store-link nil))))))
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "*two"
(fundamental-mode)
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::two]]" file file)
(org-store-link nil))))))
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "( two )"
(fundamental-mode)
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::two]]" file file)
(org-store-link nil))))))
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "# two"
(fundamental-mode)
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::two]]" file file)
(org-store-link nil))))))
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "#( two )"
(fundamental-mode)
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::two]]" file file)
(org-store-link nil))))))
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "#** ((## two) )"
(fundamental-mode)
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::two]]" file file)
(org-store-link nil))))))
(should-not
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "(two"
(fundamental-mode)
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::two]]" file file)
(org-store-link nil))))))
;; Context also ignore statistics cookies and special headlines
;; data.
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "* TODO [#A] COMMENT foo :bar:"
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::*foo][foo]]" file file)
(org-store-link nil))))))
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "* foo[33%]bar"
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::*foo bar][foo bar]]" file file)
(org-store-link nil))))))
(should
(let ((org-stored-links nil)
(org-context-in-file-links t))
(org-test-with-temp-text-in-file "* [%][/] foo [35%] bar[3/5]"
(let ((file (buffer-file-name)))
(equal (format "[[file:%s::*foo bar][foo bar]]" file file)
(org-store-link nil)))))))
;;; Radio Targets ;;; Radio Targets