0

Sorry for the vagueness of the question, I hope the following explanation will be clear enough.

I often keep notes of my readings and most of these notes are composed of extracts of these readings. I was wondering if some emacs/org-mode function existed that would do the following: extract the parts from a text that are marked up. Let's say that the markup would be _ (could be any other symbol).

For example, consider the following text

Productivity is a crucial factor in the production performance of firms and nations. 
Increasing national productivity can raise living standards because more real 
income improves people's ability to purchase goods and services, enjoy leisure, 
improve housing, and education and contribute to social and environmental programs. 
Productivity growth can also help businesses to be more profitable.

During my reading, I will manually emphasize 3 passages:

Productivity is a crucial factor in the production performance of _firms and nations_. 
Increasing national productivity _can raise_ living standards because more real 
income improves people's ability to purchase goods and services, enjoy leisure, 
improve housing, and education and contribute to social and environmental programs.
_Productivity_ growth can also help businesses to be more profitable.

The job of the function would be to extract the passages as follows:

firms and nations can raise Productivity
5
  • 1
    grep -o '_[^_]*_' /tmp/foo4.org is a start: you only need to get rid of the surrounding underscores. The only problem is the usual one of underscores that are not markup. It should be easy to turn that into an Emacs function (if worse comes to worst, the function can run the grep command, more-or-less as is).
    – NickD
    Commented Oct 16, 2023 at 20:52
  • Thank you @NickD. I notice something strange: grep -o '\*[^\*]*\*' ~/tmp/tmp.org > test.org works well on a terminal, but from emacs, (shell-command-on-region b e "grep -o '\*[^\*]*\*' > ~/tmp/tm") doesn't...
    – crocefisso
    Commented Oct 17, 2023 at 2:27
  • You need to double up backslashes in Emacs strings. That's one reason that none of these solutions is ideal (and there are many others). They should all be looked at with a jaundiced eye.
    – NickD
    Commented Oct 17, 2023 at 11:24
  • Also, * inside a character class construct [...] is not special and does not have to be escaped. In fact, trying to escape it as you have done, changes the meaning of the character class: "[^\\*]" (the Emacs string form) means any character other than backslash or asterisk. It's always good to remember what JWZ said about regexps, even if you do decide to use them.
    – NickD
    Commented Oct 17, 2023 at 11:44
  • Thank you for your explanations, (shell-command-on-region b e "grep -o '\\*[^*]*\\*' > ~/tmp/tmo") worked perfectly (I updated the solution accordingly).
    – crocefisso
    Commented Oct 17, 2023 at 13:11

1 Answer 1

0

The following interactive function will do the job for the _ markup:

(defun mda/markup-extraction (&optional b e)
  (interactive "r")
  (shell-command-on-region b e "grep -o '_[^_]*_' > ~/tmp/tmp")
  (shell-command "sed -i 's/_//g' ~/tmp/tmp")
  (find-file "~/tmp/tmp")
  )

Here is a variant for the * markup:

(defun mda/extract-bold (&optional b e)
  (interactive "r")
  (shell-command-on-region b e "grep -o '\\*[^*]*\\*' > ~/tmp/tmp")
  (shell-command "sed -i 's/*//g' ~/tmp/tmp")
  (find-file "~/tmp/tmp")
  )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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