amno1 / dired-auto-readme

An Emacs package to automatically display a README file when one is present in a dired buffer.
GNU General Public License v3.0
48 stars 4 forks source link

Conflicts with dired-git-info #1

Closed clemera closed 7 months ago

clemera commented 3 years ago

As discussed on reddit, when I played with it I noticed the following preserves the overlays and doesn't seem to break the dired listing highlighting:

(defun dar--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 (text (ext (file-name-extension file)))
            (with-current-buffer (get-buffer-create file)
              (insert-file-contents file)
              (cond ((or (equal ext "md")
                         (equal ext "markdown"))
                     (when (require 'markdown-mode)
                       (call-interactively 'markdown-mode)))
                    ((equal ext "org")
                     (when (require 'org)
                       (goto-char (point-min))
                       (call-interactively 'org-mode))))
              (font-lock-ensure)
              (goto-char (point-min))
              (while (not (eobp))
                (insert "  ")
                (forward-line 1))
              (setq text (buffer-substring (point-min) (point-max)))
              (kill-buffer))
            (make-variable-buffer-local 'dar--orig-buff)
            (setq dar--orig-buff (buffer-substring (point-min) (point-max)))
            (font-lock-mode -1)
            (make-variable-buffer-local 'dar--start-point)
            ;; (delete-region (point-min) (point-max))
            ;; (insert dar--orig-buff)
            (goto-char (point-max))
            (setq dar--start-point (point))
            (newline)
            (insert text)
            (setq dar--inserted t)
            (when (equal ext "org")
              (org-toggle-link-display)
              (org-toggle-link-display)
              (org-toggle-pretty-entities)
              (when dired-auto-readme-display-images
                (org-display-inline-images t t))
              (when (and dired-auto-readme-display-pretty-tables
                         (require 'org-pretty-table))
                (org-pretty-table-mode 1)))
            (font-lock-fontify-region 1 dar--start-point))
          (setq inhibit-read-only nil)
          (throw 'break t))))))

I tested with the following setup:

(setq dired-auto-readme-display-pretty-tables nil)
(add-hook 'dired-after-readin-hook 'dired-git-info-auto-enable)
(dired-auto-readme-mode 1)
clemera commented 3 years ago

Oh but it breaks things afterwards :D Just the initial view is preserved, after looking at the code this makes also more sense

amno1 commented 3 years ago

Clemens Radermacher notifications@github.com writes:

As discussed on reddit, when I played with it I noticed the following preserves the overlays and doesn't seem to break anything:

(defun dar--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 (text (ext (file-name-extension file))) (with-current-buffer (get-buffer-create file) (insert-file-contents file) (cond ((or (equal ext "md") (equal ext "markdown")) (when (require 'markdown-mode) (call-interactively 'markdown-mode))) ((equal ext "org") (when (require 'org) (goto-char (point-min)) (call-interactively 'org-mode)))) (font-lock-ensure) (goto-char (point-min)) (while (not (eobp)) (insert " ") (forward-line 1)) (setq text (buffer-substring (point-min) (point-max))) (kill-buffer)) (make-variable-buffer-local 'dar--orig-buff) (setq dar--orig-buff (buffer-substring (point-min) (point-max))) (font-lock-mode -1) (make-variable-buffer-local 'dar--start-point) ;; (delete-region (point-min) (point-max)) ;; (insert dar--orig-buff) (goto-char (point-max)) (setq dar--start-point (point)) (newline) (insert text) (setq dar--inserted t) (when (equal ext "org") (org-toggle-link-display) (org-toggle-link-display) (org-toggle-pretty-entities) (when dired-auto-readme-display-images (org-display-inline-images t t)) (when (and dired-auto-readme-display-pretty-tables (require 'org-pretty-table)) (org-pretty-table-mode 1))) (font-lock-fontify-region 1 dar--start-point)) (setq inhibit-read-only nil) (throw 'break t))))))

Seems that you are correct. I just tested.

I am quite sure I played with very very similar version first; but I might have made error elsewhere so it didn't work.

I'll update the code. Thanks for the contribution!

clemera commented 3 years ago

Please see my comment above, after using this and for example mark things in the dired buffer you won't get the dired highlighting anymore.

amno1 commented 3 years ago

Clemens Radermacher notifications@github.com writes:

As discussed on reddit, when I played with it I noticed the following preserves the overlays and doesn't seem to break anything:

(defun dar--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 (text (ext (file-name-extension file))) (with-current-buffer (get-buffer-create file) (insert-file-contents file) (cond ((or (equal ext "md") (equal ext "markdown")) (when (require 'markdown-mode) (call-interactively 'markdown-mode))) ((equal ext "org") (when (require 'org) (goto-char (point-min)) (call-interactively 'org-mode)))) (font-lock-ensure) (goto-char (point-min)) (while (not (eobp)) (insert " ") (forward-line 1)) (setq text (buffer-substring (point-min) (point-max))) (kill-buffer)) (make-variable-buffer-local 'dar--orig-buff) (setq dar--orig-buff (buffer-substring (point-min) (point-max))) (font-lock-mode -1) (make-variable-buffer-local 'dar--start-point) ;; (delete-region (point-min) (point-max)) ;; (insert dar--orig-buff) (goto-char (point-max)) (setq dar--start-point (point)) (newline) (insert text) (setq dar--inserted t) (when (equal ext "org") (org-toggle-link-display) (org-toggle-link-display) (org-toggle-pretty-entities) (when dired-auto-readme-display-images (org-display-inline-images t t)) (when (and dired-auto-readme-display-pretty-tables (require 'org-pretty-table)) (org-pretty-table-mode 1))) (font-lock-fontify-region 1 dar--start-point)) (setq inhibit-read-only nil) (throw 'break t))))))

I tested with the following setup:

(setq dired-auto-readme-display-pretty-tables nil) (add-hook 'dired-after-readin-hook 'dired-git-info-auto-enable) (dired-auto-readme-mode 1)

Now I remember: with this version, after disabling the mode, there is some scrap parts of readme file left in dired buffer. It seems like for some reason, the dired buffer itself gets modified, so dar--start-point is not correct. Those scrap parts are cosmetics, they go away after refresh (revert-buffer with 'g').

Here is a test version:

+begin_src emacs-lisp

;; internal vars (defvar dar--inserted) (defvar dar--start-point) (make-variable-buffer-local 'dar--inserted) (make-variable-buffer-local 'dar--start-point)

(defun dar--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 (ext (file-name-extension file))) (with-current-buffer (get-buffer-create file) (insert-file-contents file) (cond ((or (equal ext "md") (equal ext "markdown")) (when (require 'markdown-mode) (call-interactively 'markdown-mode))) ((equal ext "org") (when (require 'org) (goto-char (point-min)) (call-interactively 'org-mode)))) (font-lock-ensure) (goto-char (point-min)) (while (not (eobp)) (insert " ") (forward-line 1)) (setq readme (buffer-substring (point-min) (point-max))) (kill-buffer)) (font-lock-mode -1) (goto-char (point-max)) (setq dar--start-point (point)) (newline) (insert readme) (setq dar--inserted t) (when (equal ext "org") (org-toggle-link-display) (org-toggle-link-display) ;; need twice??? (org-toggle-pretty-entities) (when dired-auto-readme-display-images (org-display-inline-images t t)) (when (and dired-auto-readme-display-pretty-tables (require 'org-pretty-table)) (org-pretty-table-mode 1))) (font-lock-fontify-region 1 dar--start-point)) (setq inhibit-read-only nil) (throw 'break t))))))

(defun dar--remove () "Remove README file from the current dired buffer." (setq inhibit-read-only t) (save-excursion (delete-region dar--start-point (point-max)) (kill-local-variable 'dar--inserted) (kill-local-variable 'dar--start-point) (font-lock-mode +1)) (setq inhibit-read-only nil))

+end_src

clemera commented 3 years ago

You would also need to keep font-lock-mode on but then you loose the highlighting of the inserted text, I see the problem now but I can't think of a solution.

clemera commented 3 years ago

Maybe you could think about to popup another window instead of integrating it into the dired buffer.

amno1 commented 3 years ago

Clemens Radermacher notifications@github.com writes:

Oh it breaks, things :D Just the initial view is preserved, after looking at the code this makes also more sense

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe. I am not sure where we are now :-)

There is already peep-dired for displaying files in another window, and it could be very simple script to hook into dired after readme to open a file in another window, but that wasn't really the behaviour I wanted because then I have to switch to another window to mark, select things etc. But generally, yes, that would be a trivial thing to implement.

Anyway, with the code I sent, marking in dired works fine for me.

I am sure there are 1001 way to break this; but as long as it works "good-enough" I am fine.

My use-case: dired-auto-readme-mode is on globally, I don't really enable/disable it, and this scenario it seems to work, more or less both versions.

clemera commented 3 years ago

Marking works functionality wise but you don't get the highlighting any more, there are also problems with commands i to insert a directory. But if it works fine for your purposes nothing wrong keeping it that way of course.

amno1 commented 3 years ago

Yes I see now. Initially marking works, but then it stops. Requires refresh to display hightlight. Same for un-marking.

I was trying yesterday to automatically revert-buffer (what 'g' does), but I got other problems with that.

amno1 commented 3 years ago

Interesting - when I run Emacs with -Q option, then refresh completely removes buffer. But not in normal session with my init file :-).