Subject: bug#39190: 28.0.50; two buffers with same buffer-file-name (diff-syntax-fontify-props)

From: Stefan Monnier <[email-masked]>
Date: Tue, 18 Feb 2020 18:31:38 -0500

>> Maybe a simpler variant would be to use some other variable to distinguish
>> sub-mode and its parent, instead of relying on delay-mode-hooks?
>
> That's kinda hard if you want to use define-derived-mode and we do: when
> we call `conf-foo-mode` the first things it does is: bind
> `delay-mode-hooks` and call the parent (`conf-mode`), so the only
> difference between this call to the parent and a direct call to the
> generic entry point is the binding of `delay-mode-hooks`.

IOW, I think we should start with something like the patch below which
makes `conf-guess-mode` available, after which we can start fixing the
various callers to call `conf-guess-mode` instead.

        Stefan

diff --git a/lisp/textmodes/conf-mode.el b/lisp/textmodes/conf-mode.el
index 86db698043..b3325eeff6 100644
--- a/lisp/textmodes/conf-mode.el
+++ b/lisp/textmodes/conf-mode.el
@@ -351,7 +351,37 @@ conf-outline-level
 
 
 ;;;###autoload
-(defun conf-mode ()
+(defun conf-guess-mode ()
+  "Enable the conf-*-mode that seems most appropriate."
+  (let ((unix 0) (win 0) (equal 0) (colon 0) (space 0) (jp 0))
+    (save-excursion
+      (goto-char (point-min))
+      (while (not (eobp))
+	(skip-chars-forward " \t\f")
+	(cond ((eq (char-after) ?\#) (setq unix (1+ unix)))
+	      ((eq (char-after) ?\;) (setq win (1+ win)))
+	      ((eq (char-after) ?\[))	; nop
+	      ((eolp))			; nop
+	      ((eq (char-after) ?}))	; nop
+	      ;; recognize at most double spaces within names
+	      ((looking-at "[^ \t\n=:]+\\(?:  ?[^ \t\n=:]+\\)*[ \t]*[=:]")
+	       (if (eq (char-before (match-end 0)) ?=)
+		   (setq equal (1+ equal))
+		 (setq colon (1+ colon))))
+	      ((looking-at "/[/*]") (setq jp (1+ jp)))
+	      ((looking-at ".*{"))		; nop
+	      ((setq space (1+ space))))
+	(forward-line)))
+    (cond
+     ((> jp (max unix win 3)) (conf-javaprop-mode))
+     ((> colon (max equal space)) (conf-colon-mode))
+     ((> space (max equal colon)) (conf-space-mode))
+     ((or (> win unix) (and (= win unix) (eq system-type 'windows-nt)))
+      (conf-windows-mode))
+     (t (conf-unix-mode)))))
+
+;;;###autoload
+(define-derived-mode conf-mode nil "Conf[?]"
   "Mode for Unix and Windows Conf files and Java properties.
 Most conf files know only three kinds of constructs: parameter
 assignments optionally grouped into sections and comments.  Yet
@@ -372,7 +402,7 @@ conf-mode
 quite right.  Patches that clearly identify some special case,
 without breaking the general ones, are welcome.
 
-If instead you start this mode with the generic `conf-mode'
+If instead you start this mode with the generic `conf-guess-mode'
 command, it will parse the buffer.  It will generally well
 identify the first four cases listed below.  If the buffer
 doesn't have enough contents to decide, this is identical to
@@ -381,46 +411,13 @@ conf-mode
 `conf-ppd-mode' and `conf-xdefaults-mode'.
 
 \\{conf-mode-map}"
-
-  (interactive)
-  ;; `conf-mode' plays two roles: it's the parent of several sub-modes
-  ;; but it's also the function that chooses between those submodes.
-  ;; To tell the difference between those two cases where the function
-  ;; might be called, we check `delay-mode-hooks'.
-  ;; (adopted from tex-mode.el)
+  ;; `conf-mode' is the parent of several sub-modes but it's also sometimes
+  ;; abused as the function that chooses between those submodes.  They should
+  ;; call `conf-guess-mode' directly, but for historical reasons, we try to
+  ;; accommodate those misuses with this ugly hack.
   (if (not delay-mode-hooks)
-      ;; try to guess sub-mode of conf-mode based on buffer content
-      (let ((unix 0) (win 0) (equal 0) (colon 0) (space 0) (jp 0))
-	(save-excursion
-	  (goto-char (point-min))
-	  (while (not (eobp))
-	    (skip-chars-forward " \t\f")
-	    (cond ((eq (char-after) ?\#) (setq unix (1+ unix)))
-		  ((eq (char-after) ?\;) (setq win (1+ win)))
-		  ((eq (char-after) ?\[))	; nop
-		  ((eolp))			; nop
-		  ((eq (char-after) ?}))	; nop
-		  ;; recognize at most double spaces within names
-		  ((looking-at "[^ \t\n=:]+\\(?:  ?[^ \t\n=:]+\\)*[ \t]*[=:]")
-		   (if (eq (char-before (match-end 0)) ?=)
-		       (setq equal (1+ equal))
-		     (setq colon (1+ colon))))
-		  ((looking-at "/[/*]") (setq jp (1+ jp)))
-		  ((looking-at ".*{"))		; nop
-		  ((setq space (1+ space))))
-	    (forward-line)))
-	(cond
-         ((> jp (max unix win 3)) (conf-javaprop-mode))
-         ((> colon (max equal space)) (conf-colon-mode))
-         ((> space (max equal colon)) (conf-space-mode))
-         ((or (> win unix) (and (= win unix) (eq system-type 'windows-nt)))
-          (conf-windows-mode))
-         (t (conf-unix-mode))))
-
-    (kill-all-local-variables)
-    (use-local-map conf-mode-map)
-    (setq major-mode 'conf-mode
-	  mode-name "Conf[?]")
+      ;; Try to guess sub-mode of conf-mode based on buffer content.
+      (conf-guess-mode)
     (set (make-local-variable 'font-lock-defaults)
          '(conf-font-lock-keywords nil t nil nil))
     ;; Let newcomment.el decide this for itself.
@@ -438,8 +435,7 @@ conf-mode
 	    ;; [section]
 	    (nil "^[ \t]*\\[[ \t]*\\(.+\\)[ \t]*\\]" 1)
 	    ;; section { ... }
-	    (nil "^[ \t]*\\([^=:{} \t\n][^=:{}\n]+\\)[ \t\n]*{" 1)))
-    (run-mode-hooks 'conf-mode-hook)))
+	    (nil "^[ \t]*\\([^=:{} \t\n][^=:{}\n]+\\)[ \t\n]*{" 1)))))
 
 (defun conf-mode-initialize (comment &optional font-lock)
   "Initializations for sub-modes of `conf-mode'.