1

Is it possible to exclude the tilde ~ from being interpreted as emphasis-marker in Org-Mode in general?

Meaning not being escaped but being normal interpreted as simple Text?

Emphasis-and-Monospace, Worg

Edit:

The following is now set in my init.el via customize-variable, but doesn't have any effect at all...

Of course, I restarted org-mode. The target is to replace ~ with backtics for code-blocks.

'(org-emphasis-alist
  (quote
  (("*" bold)
   ("/" italic)
   ("_" underline)
   ("=" org-verbatim verbatim)
   ("`" org-code verbatim)
   ("+"
  (:strike-through t)))))
Tobias
  • 32,569
  • 1
  • 34
  • 75
OsunSeyi
  • 31
  • 4
  • 1
    Markdown isn’t part of Emacs. Read the documentation for the markdown processor that you are using. – db48x Nov 21 '21 at 01:03
  • 1
    Details please: who interprets `~` as a markdown marker? – NickD Nov 21 '21 at 03:04
  • I really don't know... But Emacs treat them as 'org face' so there will be a possibility to configurate... I would guess Emacs passed them to the used Org-Interpreter. – OsunSeyi Nov 21 '21 at 08:08
  • So you are talking about an Org mode file? `~` *is* interpreted in Org mode files (as a "verbatim" marker). In other modes however (i.e. generally), it is just a tilde: not interpreted at all. So it's not clear what you want to do: have the tilde not interpreted in Org mode files at all? Have you considered *not* using Org mode on the file? E.g. changing the major mode to `text-mode`? – NickD Nov 21 '21 at 16:49
  • Sorry - I meant in Org-Mode... I use the tilde for lines and can also use ```=``` as verbatim-marker. So it would be nice to exclude the tilde from being interpreter as marker. – OsunSeyi Nov 21 '21 at 21:46
  • EDIT: ```~``` is for inline code and ```=``` for verbatim – OsunSeyi Nov 21 '21 at 23:02
  • EDIT: found it in documentation: ```org-emphasis-alist```, sorry RTFM... – OsunSeyi Nov 21 '21 at 23:54
  • Please note that org-emphasis-alist only affects how faces are used for displaying text in the buffer, it does not affect eg exports. – JeanPierre Nov 22 '21 at 17:55
  • For export you also need to set `org-verbatim-re`. `org-set-emph-re` is supposed to do so when you set `org-emphasis-alist`, but it is buggy in that respect (at least in org-mode 9.3.7). – Tobias Nov 23 '21 at 11:02
  • Trying and trying but no effect. Pls I edited the question, I don't know how I can get this to work... – OsunSeyi Nov 25 '21 at 08:02
  • Note, that you need to set the value of `org-emphasis-alist` with some customization command like `customize-option`. – Tobias Nov 25 '21 at 21:57
  • [how-to-make-my-own-org-mode-text-emphasis-work-again](https://emacs.stackexchange.com/questions/35626/how-to-make-my-own-org-mode-text-emphasis-work-again) --- I think perhaps it won't work properly, so it might be better to let it be... – OsunSeyi Nov 26 '21 at 13:14
  • Edit: I used ```customize-option```, configured there, write to ```init.el``` and restarted Emacs. No success. If I reopen ```customize-option``` the changes are still present, but nevertheless they don't have any effect. – OsunSeyi Nov 26 '21 at 13:21

1 Answers1

1

org-emphasis-alist wrongly suggests that you could change the emphasis markers. No, that is not the case. It rather gives you the possibility to change the faces for the emphasis markers rigidly given by the syntax specification of org-mode.

Nevertheless, the code below makes some functions of Org-Mode Version 9.2.3 somewhat more general, such that it accepts modifications of the emphasis markers. The modifications are marked by comments starting with ;; Tobias:.

Note, that I used the unspecified third element of the entries of org-emphasis-alist to identify verbatim markers.

(require 'cl-lib)
(require 'org)

(defun org+-emphasis-chars ()
  "Return a list (EMPH VERB) of strings of markers.
EMPH is a string containing all marker chars
for non-verbatim emphasis.
VERB is a string containing all marker chars
for verbatim emphasis."
  (let (emph verb)
    (cl-loop for el in org-emphasis-alist
         if (eq (nth 2 el) 'verbatim)
         do (push (car el) verb)
         else
         do (push (car el) emph))
    (list
     (mapconcat #'identity emph "")
     (mapconcat #'identity verb ""))))

(defvar org+-emph-chars ""
  "String of characters working as emphasis markers.")

(defvar org+-verb-chars ""
  "String of characters working as verbatim markers.")

;; Modified version of `org-set-emph-re':
(defun org+-set-emph-re (var val)
  "Set variable and compute the emphasis regular expression."
  (set var val)
  (when (and (boundp 'org-emphasis-alist)
         (boundp 'org-emphasis-regexp-components)
         org-emphasis-alist org-emphasis-regexp-components)
    (pcase-let*
    ((`(,pre ,post ,border ,body ,nl) org-emphasis-regexp-components)
     (body (if (<= nl 0) body
         (format "%s*?\\(?:\n%s*?\\)\\{0,%d\\}" body body nl)))
     (template
      (format (concat "\\([%s]\\|^\\)" ;before markers
              "\\(\\([%%s]\\)\\([^%s]\\|[^%s]%s[^%s]\\)\\3\\)"
              "\\([%s]\\|$\\)") ;after markers
          pre border border body border post)))
      ;; Tobias: Added:
      (cl-multiple-value-setq (org+-emph-chars org+-verb-chars) (org+-emphasis-chars))
      (setq org-emph-re (format template org+-emph-chars)) ;; Tobias: Replaced literal "*+/_"
      (setq org-verbatim-re (format template org+-verb-chars))))) ;; Tobias: Replaced literal "~="

(advice-add 'org-set-emph-re :override #'org+-set-emph-re)

(org-set-emph-re 'org-emphasis-alist org-emphasis-alist)

;; Modified version of `org-do-emphasis-faces':
(defun org+-do-emphasis-faces (limit)
  "Run through the buffer and emphasize strings."
  (let ((quick-re (format "\\([%s]\\|^\\)\\([%s%s]\\)" ;; Tobias: modified format string
              (car org-emphasis-regexp-components)
              org+-emph-chars ;; Tobias: added
              org+-verb-chars))) ;; Tobias: added
    (catch :exit
      (while (re-search-forward quick-re limit t)
    (let* ((marker (match-string 2))
           (verbatim? (seq-contains org+-verb-chars (string-to-char marker)))) ;; Tobias: replaced (member ... '("=" "~"))
      (when (save-excursion
          (goto-char (match-beginning 0))
          (and
           ;; Do not match table hlines.
           (not (and (equal marker "+")
                 (org-match-line
                  "[ \t]*\\(|[-+]+|?\\|\\+[-+]+\\+\\)[ \t]*$")))
           ;; Do not match headline stars.  Do not consider
           ;; stars of a headline as closing marker for bold
           ;; markup either.
           (not (and (equal marker "*")
                 (save-excursion
                   (forward-char)
                   (skip-chars-backward "*")
                   (looking-at-p org-outline-regexp-bol))))
           ;; Match full emphasis markup regexp.
           (looking-at (if verbatim? org-verbatim-re org-emph-re))
           ;; Do not span over paragraph boundaries.
           (not (string-match-p org-element-paragraph-separate
                    (match-string 2)))
           ;; Do not span over cells in table rows.
           (not (and (save-match-data (org-match-line "[ \t]*|"))
                 (string-match-p "|" (match-string 4))))))
        (pcase-let ((`(,_ ,face ,_) (assoc marker org-emphasis-alist)))
          (font-lock-prepend-text-property
           (match-beginning 2) (match-end 2) 'face face)
          (when verbatim?
        (org-remove-flyspell-overlays-in
         (match-beginning 0) (match-end 0))
        (remove-text-properties (match-beginning 2) (match-end 2)
                    '(display t invisible t intangible t)))
          (add-text-properties (match-beginning 2) (match-end 2)
                   '(font-lock-multiline t org-emphasis t))
          (when org-hide-emphasis-markers
        (add-text-properties (match-end 4) (match-beginning 5)
                     '(invisible org-link))
        (add-text-properties (match-beginning 3) (match-end 3)
                     '(invisible org-link)))
          (throw :exit t))))))))

(advice-add 'org-do-emphasis-faces :override #'org+-do-emphasis-faces)
Tobias
  • 32,569
  • 1
  • 34
  • 75