Subject: bug#23798: 25.0.90; Underscore for emphasis in Info?

From: Drew Adams <[email-masked]>
Date: Sat, 18 Jun 2016 23:53:17 -0700 (PDT)

> > C-h i h n
> > Why are there underscores surrounding the text "please don't"?
> 
> That's how Info renders @emph.
> 
> > They don't cause that text to be rendered any differently.  Emacs has
> > had faces for quite some time now.  Why not use a specific face for
> > this?  Is this vestigial?  Does it really serve a purpose?
> 
> Because no one wrote code to implement that.

Then please consider this report as an enhancement request for that.

Below is a patch for one simple possibility.  It uses face `info-emphasis'
to highlight emphasized single words, such as _foobar_.

Trying to emphasize more than single words can lead to trouble: there
are places where it won't do the right thing, and readers can lose
information (since the underscores are hidden).

If the regexp is "_\\([^_]+\\)_", for instance, then it can continue
across newlines and is more likely to cause trouble than if it is
"_\\([^_\n]+\\)_" (unrelated underscores are mistaken as emphasizing).

But even the latter causes trouble in a node such as (elisp) `Symbol
Type', where there is this line:

     +-*/_~!@$%^&=:<>{}  ; A symbol named `+-*/_~!@$%^&=:<>{}'.

Even just matching words possibly separated by whitespace can lead
to trouble.  E.g., for regexp "_\\(\\(\\sw\\|[[:space:]]\\)+\\)_"
node (elisp) `Sequence Functions' is screwed up because of this code
there:

    (seq-let (_ a _ b) '(1 2 3 4)
      (list a b))

Similarly, trying to fontifie emphasized symbol names instead of
just words can be problematic.

Whatever matching is used, even just single words, there could be
some Info text that is problematic.  For instance, a Lisp example
might use _foo_ and foo as symbol names.  The fontification hides
the underscores, so that both become the same name, foo.

Still, I think fontifying only single words is pretty good.  If you
are worried about this you could add an option for such highlighting.

Is there any other kind of emphasis that is used?  For example,
does Info use double underscore for stronger emphasis, e.g.,
__abc__?  I couldn't find any such in the Emacs or Elisp manuals.

diff -u info.el info-patched.el
--- info.el	2016-06-18 22:48:10.795523400 -0700
+++ info-patched.el	2016-06-18 23:26:36.084378400 -0700
@@ -132,6 +132,12 @@
   :version "22.1"
   :group 'info)
 
+(defface info-emphasis
+  '((t (:inherit italic)))
+  "*Face for emphasized text (enclosed with underscores)."
+  :version "25.1"
+  :group 'info)
+
 (defcustom Info-fontify-visited-nodes t
   "Non-nil to fontify references to visited nodes in `info-xref-visited' face."
   :version "22.1"
@@ -4717,6 +4723,16 @@
 		  (put-text-property (match-beginning 1) (match-end 1)
 				     'invisible t)))))))
 
+      ;; Fontify emphasis: _..._
+      (goto-char (point-min))
+      (when (and font-lock-mode  not-fontified-p)
+        (while (re-search-forward "_\\(\\sw+\\)_" nil t)
+          (add-text-properties (match-beginning 0) (1+ (match-beginning 0))
+                               '(invisible t front-sticky nil rear-nonsticky t))
+          (add-text-properties (1- (match-end 0)) (match-end 0)
+                               '(invisible t front-sticky nil rear-nonsticky t))
+          (put-text-property (match-beginning 1) (match-end 1) 'font-lock-face 'info-emphasis)))
+
       ;; Fontify titles
       (goto-char (point-min))
       (when (and font-lock-mode not-fontified-p)