0

The function org-emphasize puts formatting on a region, e. g. * at the beginning end end of a bold sentence or word.

To remove the bold attribute I can mark the same region, execute M-x org-emphasize and hit space.

Question: Is there a way to remove emphasis on a sentence without the need to mark the region?

Thoughts: Maybe it is possible to detect the next and previous emphasis marker and delete them. As an alternative: How would I mark the text between the next and previous emphasis marker in a function?

  • 1
    How would you deal with this is *bold* and that is *bolder* when point is say after the h of that? – NickD Jun 17 at 16:07
  • Good question. One option would be to delete the most adjacent * and format the whole text in bold: *bold and that is bolder*. Another option to parse the whole paragraph and ask for a selection of the occurence (1 or 2) and delete the corresponding formatting. But I have absolutely no clue how to do either in elisp. :-/ If a function selects the region between the two * then there would be some visual feeback to move the cursor. What do you think? – breathe_in_breathe_out Jun 17 at 16:42
  • How often do you have to do that? For the few times that I would make any use of such a function, and given that it might require some somewhat complicated logic to deal with possibly ambiguous situations, I would forego the function and just delete the emphasis characters by hand. That would allow me to tailor the response to the exact situation, so I could resolve ambiguities in real time. IOW, IMO it's not worth automating. OTOH, if you are looking to learn some lisp, it might be a good exercise. – NickD Jun 17 at 18:37
1

You can do this with smartparens:

(require 'smartparens)
(require 'smartparens-config)   
(define-key smartparens-mode-map (kbd "C-<backspace>") 'sp-unwrap-sexp)
(define-key smartparens-mode-map (kbd "M-<backspace>") 'sp-backward-unwrap-sexp)

For example:

This is *bold* and th|at is *bolder*.

Pressing M-<backspace> unwraps the word "bold". Pressing C-<backspace> unwraps the word "bolder". Also, to make a word *bold* (/italic/ etc.), type * once and the the cursor is wrapped automatically.

Alternatively, you can try the following command (lightly tested).

(defun toggle-emphasis ()
  (interactive)
  (save-match-data
    (if (and (org-in-regexp org-emph-re 2)
             (not (region-active-p)))
        (let ((beg (match-beginning 3))
              (end (match-end 4)))
          (when (and (>= (point) (1- beg))
                     (<= (point) (1+ end)))
            (save-excursion
              (goto-char end)
              (delete-char 1)
              (goto-char beg)
              (delete-char 1))))
      (call-interactively #'org-emphasize))))

(global-set-key [remap org-emphasize] #'toggle-emphasis)

Pressing C-c C-x C-f should work as usual with the cursor outside the marked area (see below). Otherwise the word "bold" is unwrapped.

This is *bold* and...
       ^^^^^^^^
| improve this answer | |
  • Thanks for the effort! I will check it out on the weekend. – breathe_in_breathe_out Jun 19 at 7:58
  • I whish I had known smartparens before. This makes formatting text a breeze. I chose M-<backspace> for sp-unwrap-sexp and S-<backspace> for sp-backward-unwrap-sexp because it feels mor mnemonic with Shift being on the left side (back) and Alt being on the right (forward). – breathe_in_breathe_out Jun 22 at 18:18

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.