4

I have following content in a org file:

* test
[[file:foo1.pdf][test file1]]
[[file:foo2.pdf][test file2]]
[[file:foo3.pdf][test file3]]

Normally, the file name (eg foo1.pdf) is folded and invisible. And when I do a search for foo (with ctrl-s), I can't find anything... How can I enable the isearch for those invisible text? I tried M-s i on the isearch command and it does not work.

  • 1
    M-s i causes invisible text to be searched. But, unfortunately it does not make text with invisible-text properties visible. This only works for overlays having the invisible property. – Tobias Mar 25 '16 at 13:41
4

Try M-x org-toggle-link-display or the menu item Org -> Hyperlinks -> Literal Links before using isearch or query-replace.

The following code does that automagically for you when you use isearch or query-replace/query-replace-regexp. It restores the old state when you are done with searching.

The code is tested with Emacs 25.1.50.2.

EDIT: Added (setq-local search-invisible t) to search in texts that are hidden by the invisible text property.

(defvar-local org-descriptive-links-isearch nil
  "Backup of the original value `org-descriptive-links' before isearch.")

(defun org-isearch-mode-hook-function ()
  "Save value of `org-descriptive-links' before isearch."
  (setq org-descriptive-links-isearch org-descriptive-links))

(defun org-isearch-mode-end-hook-function ()
  "Restore original value of `org-descriptive-links' after isearch."
  (unless (eq org-descriptive-links-isearch org-descriptive-links)
    (org-toggle-link-display)))

(defun org-mode-isearch-filter (found)
  "Toggle to literal links if match is found within a string that is hidden in the target of descriptive links."
  (when (and found
         (text-property-any (match-beginning 0) (match-end 0) 'invisible 'org-link)
         org-descriptive-links)
    (org-toggle-link-display))
  found)

(defun org-query-replace-isearch-wrapper (oldfun &rest args)
  "Toggle to literal links if match is found within a string that is hidden in the target of descriptive links."
  (org-isearch-mode-hook-function)
  (prog1 (apply oldfun args)
    (org-isearch-mode-end-hook-function)))

(defun org-mode-setup-isearch ()
  "Setup some specials for isearch in org-mode.
1. Toggle descriptive links if necessary."
  (setq-local search-invisible t)
  (add-hook 'isearch-mode-hook 'org-isearch-mode-hook-function)
  (add-hook 'isearch-mode-end-hook 'org-isearch-mode-end-hook-function)
  (add-function :filter-return (local 'isearch-filter-predicate) #'org-mode-isearch-filter)
  (advice-add 'query-replace :around #'org-query-replace-isearch-wrapper)
  (advice-add 'query-replace-regexp :around #'org-query-replace-isearch-wrapper))

(add-hook 'org-mode-hook 'org-mode-setup-isearch)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.