Subject: bug#57813: Icon images are non-functional

From: Juri Linkov <[email-masked]>
Date: Thu, 13 Oct 2022 10:51:35 +0300

This patch distinguishes two cases of using buttons in outline-minor-mode:

1. when buttons can be inserted to a read-only buffer,
   then it's possible to move point to the inserted button
   and use it by keyboard;

2. when the buffer is editable and should not be modified
   to insert buttons, when the only way to display buttons
   is with a before-string overlay.  The disadvantage is that
   buttons can be used only by clicking mouse.

A new variable 'outline-read-only' is added here to allow
setting it explicitly in buffers that can be modified
to insert buttons like the Help buffer:

diff --git a/lisp/help.el b/lisp/help.el
index b4b9120da3..713467c9b2 100644
--- a/lisp/help.el
+++ b/lisp/help.el
@@ -608,13 +608,8 @@ describe-bindings
           (setq-local outline-heading-end-regexp ":\n")
           (setq-local outline-level (lambda () 1))
           (setq-local outline-minor-mode-cycle t
-                      outline-minor-mode-highlight t)
+                      outline-minor-mode-highlight t
+                      outline-read-only t)
           (outline-minor-mode 1)
           (save-excursion
             (goto-char (point-min))
diff --git a/lisp/outline.el b/lisp/outline.el
index b87d3ac5e7..5a65c6c8e5 100644
--- a/lisp/outline.el
+++ b/lisp/outline.el
@@ -295,6 +295,9 @@ outline-minor-mode-use-buttons
 (defvar-local outline--use-buttons nil
   "Non-nil when buffer displays clickable buttons on the headings.")
 
+(defvar-local outline-read-only nil
+  "Non-nil when it's allowed to modify buffer to insert buttons.")
+
 (defvar-local outline--use-rtl nil
   "Non-nil when direction of clickable buttons is right-to-left.")
 
@@ -1652,18 +1655,24 @@ outline--make-button-overlay
                                    (if outline--use-rtl
                                        'outline-close-rtl
                                      'outline-close)
-                                 'outline-open)))
-          (inhibit-read-only t))
+                                 'outline-open))))
       ;; In editing buffers we use overlays only, but in other buffers
       ;; we use a mix of text properties, text and overlays to make
       ;; movement commands work more logically.
-      (when (derived-mode-p 'special-mode)
-        (put-text-property (point) (1+ (point)) 'face (plist-get icon 'face)))
-      (if-let ((image (plist-get icon 'image)))
-          (overlay-put o 'display image)
-        (overlay-put o 'display (concat (plist-get icon 'string)
-                                        (string (char-after (point)))))
-        (overlay-put o 'face (plist-get icon 'face))))
+      (if outline-read-only
+          (let ((inhibit-read-only t))
+            (put-text-property (point) (1+ (point)) 'face (plist-get icon 'face))
+            (if-let ((image (plist-get icon 'image)))
+                (overlay-put o 'display image)
+              (overlay-put o 'display (concat (plist-get icon 'string)
+                                              (string (char-after (point)))))
+              (overlay-put o 'face (plist-get icon 'face))))
+        (overlay-put
+         o 'before-string
+         (propertize " "
+                     'display
+                     (or (plist-get icon 'image)
+                         (plist-get icon 'string))))))
     o))
 
 (defun outline--make-margin-overlay (type)
@@ -1699,7 +1713,7 @@ outline--insert-open-button
       (beginning-of-line)
       (if use-margins
           (outline--make-margin-overlay 'open)
-        (when (derived-mode-p 'special-mode)
+        (when outline-read-only
           (let ((inhibit-read-only t))
             (insert "  ")
             (beginning-of-line)))
@@ -1716,7 +1730,7 @@ outline--insert-close-button
       (beginning-of-line)
       (if use-margins
           (outline--make-margin-overlay 'close)
-        (when (derived-mode-p 'special-mode)
+        (when outline-read-only
           (let ((inhibit-read-only t))
             (insert "  ")
             (beginning-of-line)))
diff --git a/lisp/textmodes/emacs-news-mode.el b/lisp/textmodes/emacs-news-mode.el
index d9decae4df..f7f56eb047 100644
--- a/lisp/textmodes/emacs-news-mode.el
+++ b/lisp/textmodes/emacs-news-mode.el
@@ -73,11 +73,7 @@ emacs-news-mode-font-lock-keywords
 
 (defun emacs-news--mode-common ()
   (setq-local font-lock-defaults '(emacs-news-mode-font-lock-keywords t))
-  ;; This `outline-regexp' matches leading spaces inserted
-  ;; by the current implementation of `outline-minor-mode-use-buttons'.
-  (setq-local outline-regexp "\\(?: +\\)?\\(\\*+\\) "
-              outline-level (lambda () (length (match-string 1)))
-              outline-minor-mode-cycle t
+  (setq-local outline-minor-mode-cycle t
               outline-minor-mode-highlight 'append)
   (outline-minor-mode)
   (setq-local imenu-generic-expression outline-imenu-generic-expression)