all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Font-lock in a buffer inserted in Dired buffer?
@ 2020-11-26 16:21 Arthur Miller
  0 siblings, 0 replies; only message in thread
From: Arthur Miller @ 2020-11-26 16:21 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: dired-auto-readme.el --]
[-- Type: text/plain, Size: 3022 bytes --]

;;; dired-auto-readme.el --- -*- lexical-binding: t; -*-

;; Copyright (C) 2020  Arthur Miller

;; Author: Arthur Miller <arthur.miller@live.com>
;; Keywords: 

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(defvar dired-auto-readme-files '("README"
                                 "README.md"
                                 "README.org"
                                 "README.txt")
  
  "List of README file names to automatically display.  First file in the
  directory matching the list is used.  ")

(defun dired-auto-readme--insert ()
  "Insert README file in the current buffer.  "
  (setq inhibit-read-only t)
  (catch 'break
    (dolist (file dired-auto-readme-files)
      (when (file-exists-p file)
        (save-excursion
          (let ((readme-text))
            (with-current-buffer (get-buffer-create file)
              (insert-file-contents file)
              (font-lock-ensure)
              (indent-region (point-min) (point-max))
              (setq readme-text (buffer-substring (point-min) (point-max)))
              ;;(kill-buffer)
              )
            (goto-char (point-max))
            (make-variable-buffer-local 'dired-auto-readme--start-point)
            (setq dired-auto-readme--start-point (point))
            (insert readme-text))
        (setq dired-auto-readme--inserted t)
        (setq inhibit-read-only nil)
        (throw 'break t))))))

(defun dired-auto-readme--remove ()
  (setq inhibit-read-only t)
  (save-excursion
    (goto-char dired-auto-readme--start-point)
    (delete-region (point) (point-max)))
  (setq dired-auto-readme--inserted nil)
  (setq inhibit-read-only t))

;;;###autoload
(define-minor-mode dired-auto-readme-mode
  "Dired minor mode to preview README in current directory."
  :global nil :lighter " README"
  (when (derived-mode-p 'dired-mode)
    (make-variable-buffer-local 'dired-auto-readme--inserted)
    (cond (dired-auto-readme-mode
           (unless dired-auto-readme--inserted
             (dired-auto-readme--insert))
          ;; hook run by `dired-revert'
           (add-hook 'dired-after-readin-hook
                     #'dired-auto-readme--insert nil t))
          (t
           (when dired-auto-readme--inserted
             (dired-auto-readme--remove))
           (remove-hook 'dired-after-readin-hook
                        #'dired-auto-readme--insert t)))))

(provide 'dired-auto-readme)
;;; dired-auto-readme.el ends here

[-- Attachment #2: Type: text/plain, Size: 504 bytes --]


I am opening a file in it's own buffer, and hoping trying to get Emacs
to font-lock it. I am not sure if font-lock will do it's thing when
buffer is loaded or when it is displayed so I am calling
(font-lock-ensure) to get it to (hopefully) kick-in. I see also dired is
trying to font-lock inserted text.

1. what do I need to call to make it to font-lock
2. how do I tell Dired to give crap to font-lock the text after the
   insertion? Can I disable just a region from font-lock?

Thanks for any help!

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-11-26 16:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-26 16:21 Font-lock in a buffer inserted in Dired buffer? Arthur Miller

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.