2

I'm looking for an org function to evaluate to the text of the current heading without full link text (just the descriptive form).

*** Check out the new [[https://www.google.com][Google]] logo!

should evaluate to

"Check out the new Google logo!"

in contrast to the full heading with full link text (the undesired behavior):

Check out the new [[https://www.google.com][Google]] logo!

org-heading-components for example returns the latter. I'm looking for a DRY solution.

1

I'm not sure about how generic this will be, but at least it looks short enough to try:

(defun my/org-strip-links (text)
  (replace-regexp-in-string
   org-any-link-re
   (lambda (match)
     (or (match-string 4 match) (match-string 2 match)))
   text))

I think that links are normally handled by backends in order to produce any kind of text, but I'm not exactly sure as to what makes links appear the way they do (and I'm doubtful the function may be reused to process strings).

1
  • This is a very clean solution.
    – ebpa
    Sep 5 '15 at 1:17
1

I was hoping that org-copy-visible but apparently it does only skips hidden subtrees. So here is an alternative version (inspired by org-copy-visible's code) which copies only visible text

(require 'cl-lib)
(defun my-copy-only-visible-text ()
  (interactive)
  (kill-new (apply #'string (delq nil ; Last element is nil if region end corresponds to buffer end
                                  (cl-loop for point in (number-sequence (region-beginning) (region-end))
                                           unless (get-char-property point 'invisible)
                                           collect (char-after point))))))
1
  • This is a really neat approach that should prove broadly useful in org-mode.
    – ebpa
    Sep 5 '15 at 1:20

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.