Subject: bug#40573: 27.0.90; flymake-mode broken in scratch buffer

From: João Távora <[email-masked]>
Date: Sun, 19 Apr 2020 00:36:36 +0100

Here's the final patch as a sum of all the commits in the
scratch/add-lisp-data-mode
branch.  I fixed the small lisp-mode-variables backward incompatibility
issue.

Any comments/objections?  Does this need documentation?  Stefan,
you want to add an emacs-lisp-data-mode in between lisp-data-mode
and emacs-lisp-mode?

Thanks,
João

diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi
index df7458c3fb..fc68ee1b32 100644
--- b/doc/lispref/modes.texi
+++ a/doc/lispref/modes.texi
@@ -1356,9 +1356,9 @@

 @smallexample
 @group
-(defun lisp-mode-variables (&optional syntax keywords-case-insensitive
elisp)
-  (when syntax
-    (set-syntax-table lisp-mode-syntax-table))
+(defun lisp-mode-variables ()
+  "Common initialization routine for lisp modes."
+  (setq-local paragraph-ignore-fill-prefix t)
   @dots{}
 @end group
 @end smallexample
@@ -1414,7 +1414,7 @@

 @smallexample
 @group
-(define-derived-mode lisp-mode prog-mode "Lisp"
+(define-derived-mode lisp-mode lisp-data-mode "Lisp"
   "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
 Commands:
 Delete converts tabs to spaces as it moves back.
@@ -1425,7 +1425,7 @@
 or to switch back to an existing one."
 @end group
 @group
-  (lisp-mode-variables nil t)
+  (lisp-mode-variables)
   (setq-local find-tag-default-function 'lisp-find-tag-default)
   (setq-local comment-start-skip
               "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
diff --git a/lisp/bookmark.el b/lisp/bookmark.el
index f2384973e9..720ad18c16 100644
--- b/lisp/bookmark.el
+++ a/lisp/bookmark.el
@@ -734,8 +734,10 @@
   (if (memq (coding-system-base coding) '(undecided prefer-utf-8))
       (setq coding 'utf-8-emacs))
   (insert
-   (format ";;;; Emacs Bookmark Format Version %d ;;;; -*- coding: %S
-*-\n"
-           bookmark-file-format-version (coding-system-base coding)))
+   (format
+    ";;;; Emacs Bookmark Format Version %d\
+;;;; -*- coding: %S mode: lisp-data -*-\n"
+    bookmark-file-format-version (coding-system-base coding)))
   (insert ";;; This format is meant to be slightly human-readable;\n"
           ";;; nevertheless, you probably don't want to edit it.\n"
           ";;; "
diff --git a/lisp/chistory.el b/lisp/chistory.el
index 485515d07b..c9aa927b94 100644
--- b/lisp/chistory.el
+++ a/lisp/chistory.el
@@ -139,7 +139,7 @@

 Keybindings:
 \\{command-history-mode-map}"
-  (lisp-mode-variables nil)
+  (lisp-mode-variables)
   (set (make-local-variable 'revert-buffer-function)
'command-history-revert)
   (set-syntax-table emacs-lisp-mode-syntax-table))

diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el
index 13263f2fb5..3b0f5493ee 100644
--- b/lisp/emacs-lisp/lisp-mode.el
+++ a/lisp/emacs-lisp/lisp-mode.el
@@ -611,15 +611,10 @@
   ;; a single docstring.  Let's fix it here.
   (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") ""))

-(defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive
-                                      elisp)
+(defun lisp-mode-variables (&rest ignored)
   "Common initialization routine for lisp modes.
-The LISP-SYNTAX argument is used by code in inf-lisp.el and is
-\(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
-score-mode.el.  KEYWORDS-CASE-INSENSITIVE non-nil means that for
-font-lock keywords will not be case sensitive."
-  (when lisp-syntax
-    (set-syntax-table lisp-mode-syntax-table))
+Any number of parameters is accepted and ignored."
+  (set-syntax-table lisp-mode-syntax-table)
   (setq-local paragraph-ignore-fill-prefix t)
   (setq-local fill-paragraph-function 'lisp-fill-paragraph)
   (setq-local adaptive-fill-function #'lisp-adaptive-fill)
@@ -643,21 +638,23 @@
   (setq-local multibyte-syntax-as-symbol t)
   ;; (setq-local syntax-begin-function 'beginning-of-defun)  ;;Bug#16247.
   (setq font-lock-defaults
- `(,(if elisp '(lisp-el-font-lock-keywords
-                       lisp-el-font-lock-keywords-1
-                       lisp-el-font-lock-keywords-2)
-             '(lisp-cl-font-lock-keywords
-               lisp-cl-font-lock-keywords-1
-               lisp-cl-font-lock-keywords-2))
-  nil ,keywords-case-insensitive nil nil
-  (font-lock-mark-block-function . mark-defun)
-          (font-lock-extra-managed-props help-echo)
-  (font-lock-syntactic-face-function
-   . lisp-font-lock-syntactic-face-function)))
+ (list nil nil nil nil nil
+              '(font-lock-mark-block-function . mark-defun)
+              '(font-lock-extra-managed-props help-echo)
+              '(font-lock-syntactic-face-function
+                . lisp-font-lock-syntactic-face-function)))
   (setq-local prettify-symbols-alist lisp-prettify-symbols-alist)
   (setq-local electric-pair-skip-whitespace 'chomp)
   (setq-local electric-pair-open-newline-between-pairs nil))

+;;;###autoload
+(define-derived-mode lisp-data-mode prog-mode "Lisp-Data"
+  "Major mode for buffers holding data written in Lisp syntax."
+  :group 'lisp
+  (lisp-mode-variables)
+  (setq-local electric-quote-string t)
+  (setq imenu-case-fold-search nil))
+
 (defun lisp-outline-level ()
   "Lisp mode `outline-level' function."
   (let ((len (- (match-end 0) (match-beginning 0))))
@@ -737,7 +734,7 @@
   "Keymap for ordinary Lisp mode.
 All commands in `lisp-mode-shared-map' are inherited by this map.")

-(define-derived-mode lisp-mode prog-mode "Lisp"
+(define-derived-mode lisp-mode lisp-data-mode "Lisp"
   "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
 Commands:
 Delete converts tabs to spaces as it moves back.
@@ -746,7 +743,11 @@
 \\{lisp-mode-map}
 Note that `run-lisp' may be used either to start an inferior Lisp job
 or to switch back to an existing one."
-  (lisp-mode-variables nil t)
+  (setf
+   (nth 0 font-lock-defaults) '(lisp-cl-font-lock-keywords
+                                lisp-cl-font-lock-keywords-1
+                                lisp-cl-font-lock-keywords-2)
+   (nth 2 font-lock-defaults) t)
   (setq-local lisp-indent-function 'common-lisp-indent-function)
   (setq-local find-tag-default-function 'lisp-find-tag-default)
   (setq-local comment-start-skip
diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el
index 4d8bf52934..3df7b0e368 100644
--- b/lisp/emacs-lisp/pp.el
+++ a/lisp/emacs-lisp/pp.el
@@ -42,7 +42,7 @@
 OBJECT can be any Lisp object.  Quoting characters are used as needed
 to make output that `read' can handle, whenever this is possible."
   (with-temp-buffer
-    (lisp-mode-variables nil)
+    (lisp-mode-variables)
     (set-syntax-table emacs-lisp-mode-syntax-table)
     (let ((print-escape-newlines pp-escape-newlines)
           (print-quoted t))
diff --git a/lisp/files.el b/lisp/files.el
index 56d4679ad7..fa72e51c49 100644
--- b/lisp/files.el
+++ a/lisp/files.el
@@ -2657,6 +2657,13 @@
      ("\\.ltx\\'" . latex-mode)
      ("\\.dtx\\'" . doctex-mode)
      ("\\.org\\'" . org-mode)
+     ;; .dir-locals.el is not really elisp.  Could use the
+     ;; `dir-locals-file' constant if it weren't defined below.
+     ("\\.dir-locals\\(-2\\)?\\.el\\'" . lisp-data-mode)
+     ("eww-bookmarks\\'" . lisp-data-mode)
+     ("tramp\\'" . lisp-data-mode)
+     ("places\\'" . lisp-data-mode)
+     ("\\.emacs-places\\'" . lisp-data-mode)
      ("\\.el\\'" . emacs-lisp-mode)
      ("Project\\.ede\\'" . emacs-lisp-mode)
      ("\\.\\(scm\\|stk\\|ss\\|sch\\)\\'" . scheme-mode)
diff --git a/lisp/help-fns.el b/lisp/help-fns.el
index dbf2cb807b..0a99b8d6a3 100644
--- b/lisp/help-fns.el
+++ a/lisp/help-fns.el
@@ -1000,7 +1000,7 @@
     (terpri)
                     (let ((buf (current-buffer)))
                       (with-temp-buffer
-                        (lisp-mode-variables nil)
+                        (lisp-mode-variables)
                         (set-syntax-table emacs-lisp-mode-syntax-table)
                         (insert print-rep)
                         (pp-buffer)
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index a4544023f6..c83884fd25 100644
--- b/lisp/net/eww.el
+++ a/lisp/net/eww.el
@@ -1733,7 +1733,7 @@

 (defun eww-write-bookmarks ()
   (with-temp-file (expand-file-name "eww-bookmarks"
eww-bookmarks-directory)
-    (insert ";; Auto-generated file; don't edit\n")
+    (insert ";; Auto-generated file; don't edit -*- mode: lisp-data -*-\n")
     (pp eww-bookmarks (current-buffer))))

 (defun eww-read-bookmarks ()
diff --git a/lisp/net/nsm.el b/lisp/net/nsm.el
index cc22427e6d..2d36c5e257 100644
--- b/lisp/net/nsm.el
+++ a/lisp/net/nsm.el
@@ -964,6 +964,7 @@

 (defun nsm-write-settings ()
   (with-temp-file nsm-settings-file
+    (insert ";;;; -*- mode: lisp-data -*-\n")
     (insert "(\n")
     (dolist (setting nsm-permanent-host-settings)
       (insert " ")
diff --git a/lisp/net/tramp-cache.el b/lisp/net/tramp-cache.el
index 6d87ce297b..09e30f000f 100644
--- b/lisp/net/tramp-cache.el
+++ a/lisp/net/tramp-cache.el
@@ -472,7 +472,7 @@
  ;; Dump it.
  (with-temp-file tramp-persistency-file-name
   (insert
-   ";; -*- emacs-lisp -*-"
+   ";; -*- lisp-data -*-"
    ;; `time-stamp-string' might not exist in all Emacs flavors.
    (condition-case nil
        (progn
diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el
index 2f231781ba..f85fd771ca 100644
--- b/lisp/progmodes/elisp-mode.el
+++ a/lisp/progmodes/elisp-mode.el
@@ -250,7 +250,7 @@
     map))

 ;;;###autoload
-(define-derived-mode emacs-lisp-mode prog-mode
+(define-derived-mode emacs-lisp-mode lisp-data-mode
   `("ELisp"
     (lexical-binding (:propertize "/l"
                       help-echo "Using lexical-binding mode")
@@ -268,35 +268,26 @@
 \\{emacs-lisp-mode-map}"
   :group 'lisp
   (defvar project-vc-external-roots-function)
-  (lisp-mode-variables nil nil 'elisp)
+  (setcar font-lock-defaults
+          '(lisp-el-font-lock-keywords
+            lisp-el-font-lock-keywords-1
+            lisp-el-font-lock-keywords-2))
+  (set-syntax-table emacs-lisp-mode-syntax-table)
   (add-hook 'after-load-functions #'elisp--font-lock-flush-elisp-buffers)
   (if (boundp 'electric-pair-text-pairs)
       (setq-local electric-pair-text-pairs
                   (append '((?\` . ?\') (?\‘ . ?\’))
                           electric-pair-text-pairs))
     (add-hook 'electric-pair-mode-hook
#'emacs-lisp-set-electric-text-pairs))
-  (setq-local electric-quote-string t)
-  (setq imenu-case-fold-search nil)
   (add-hook 'eldoc-documentation-functions
             #'elisp-eldoc-documentation-function nil t)
   (add-hook 'xref-backend-functions #'elisp--xref-backend nil t)
   (setq-local project-vc-external-roots-function #'elisp-load-path-roots)
   (add-hook 'completion-at-point-functions
             #'elisp-completion-at-point nil 'local)
-  ;; .dir-locals.el and lock files will cause the byte-compiler and
-  ;; checkdoc emit spurious warnings, because they don't follow the
-  ;; conventions of Emacs Lisp sources.  Until we have a better fix,
-  ;; like teaching elisp-mode about files that only hold data
-  ;; structures, we disable the ELisp Flymake backend for these files.
-  (unless
-      (let* ((bfname (buffer-file-name))
-             (fname (and (stringp bfname) (file-name-nondirectory
bfname))))
-        (and (stringp fname)
-             (or (string-match "\\`\\.#" fname)
-                 (string-equal dir-locals-file fname))))
-    (add-hook 'flymake-diagnostic-functions #'elisp-flymake-checkdoc nil t)
-    (add-hook 'flymake-diagnostic-functions
-              #'elisp-flymake-byte-compile nil t)))
+  (add-hook 'flymake-diagnostic-functions #'elisp-flymake-checkdoc nil t)
+  (add-hook 'flymake-diagnostic-functions
+              #'elisp-flymake-byte-compile nil t))

 ;; Font-locking support.

diff --git a/lisp/progmodes/inf-lisp.el b/lisp/progmodes/inf-lisp.el
index fca803be18..9f34a377f4 100644
--- b/lisp/progmodes/inf-lisp.el
+++ a/lisp/progmodes/inf-lisp.el
@@ -274,7 +274,7 @@
 to continue it."
   (setq comint-prompt-regexp inferior-lisp-prompt)
   (setq mode-line-process '(":%s"))
-  (lisp-mode-variables t)
+  (lisp-mode-variables)
   (setq comint-get-old-input (function lisp-get-old-input))
   (setq comint-input-filter (function lisp-input-filter)))

diff --git a/lisp/saveplace.el b/lisp/saveplace.el
index f78639db24..fa0e181bb1 100644
--- b/lisp/saveplace.el
+++ a/lisp/saveplace.el
@@ -248,7 +248,7 @@
       (delete-region (point-min) (point-max))
       (when save-place-forget-unreadable-files
  (save-place-forget-unreadable-files))
-      (insert (format ";;; -*- coding: %s -*-\n"
+      (insert (format ";;; -*- coding: %s mode: lisp-data -*-\n"
                       (symbol-name coding-system-for-write)))
       (let ((print-length nil)
             (print-level nil))

On Sat, Apr 18, 2020 at 4:10 PM João Távora <[email-masked]> wrote:

> On Sat, Apr 18, 2020 at 1:35 PM Eli Zaretskii <[email-masked]> wrote:
>
> > > > > > why not add it to auto-mod-alist?
> > > > > Anyway, I can surely switch to auto-mode-alist if you
> > > > > insist, no problem. Do you insist in this?
> > > > Do I have to insist?
> > > Any problem with doing so? I'll interpret that as a "Yes".
> > It's actually the other way around: if I don't feel my opinion is
> > strong enough, I usually say so.
>
> This is what actually-actually what happened: you asked "why
> not add it to auto-mod-alist? " I answered and asked if you
> insisted, because a question doesn't really sound imperative
> to me. If it was an instrument of rhetoric, it wasn't very effective.
>
> > > Anyway, I went and looked at the three files you mention, and
> > > I discovered they're not "fixed" as I thought. They're defcustom
> > > and defined way after auto-mode-alist, so we'd have to add-to-list,
> >
> > No need for such complexity.  I meant to mention the standard names in
> > auto-mode-alist, under the assumption that many/most users don't
> > change the default names.
>
> OK.
>
> João
>

-- 
João Távora