Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with auto-mode-alist and x509-mode or x509-dwim #45

Open
pierre-rouleau opened this issue Nov 26, 2024 · 2 comments
Open

Problem with auto-mode-alist and x509-mode or x509-dwim #45

pierre-rouleau opened this issue Nov 26, 2024 · 2 comments

Comments

@pierre-rouleau
Copy link

Hi,

Thanks for this code, it's really useful!

I am trying to associate the various file extensions (such as .crt) with x509-mode (or x509-dwim) such that when I open the .crt file the decoding is done right away and the appropriate rendering is shown in the buffer.

I used

(add-to-list 'auto-mode-alist '("\\.crt\\'" . x509-dwim))

And that seems to work fine. However I noticed a couple of problems:

  1. If the .crt file is inside a .zip file which open in archive-mode, the x509-dwim function is called, the rendering is done and then the buffer reverts back to Fundamental-mode.
  2. After opening a .crt file once, and then killing the buffer, if I open the same file again it now opens in Fundamental-mode.

Overall it feels as if there was an extra call to x509-mode which takes the buffer back to Fundamental-mode.

Should this be working as one would expect, or is the design of the code precluding the use of auto-mode-alist association?

@jobbflykt
Copy link
Owner

auto-mode-alist is intended to apply a mode in the buffer itself. In the case of opening a .crt-file and running x509-dwim, what happens is that the original .crt-file remains open. When the x509 buffer is closed, the original file remains open in a (buried) buffer. When opening the same file again, that buffer is simply switched to and auto-mode-alist does not come into play. The problem is that x509-dwim is not a mode itself applied to the opened file and therefor it doesn't quite work with auto-mode-alist. Unfortunately I don't know how to make this better but it might help you to know what's going on.

When opening a file from an archive the behavior is even stranger. It seems the buffer with the archive file is switched to after x509-dwim is run. You can see this by manually switching to the corresponding x509 buffer, typically named something beginning with "*x-". This is probably caused by how archive files are opened. To help the situation somewhat, the generated x509 buffer can be automatically switched to in the archive-extract-hook.

(add-to-list 'auto-mode-alist '("\\.crt\\'" . x509-dwim))

(defun my-archive-extract-hook()
  "Switch to a x509 buffer with same basename as the current buffer."
  (if-let* ((x-buffer (get-buffer (format "*x-%s*" (buffer-name)))))
      (switch-to-buffer x-buffer)))
(add-to-list 'archive-extract-hook 'my-archive-extract-hook)

@pierre-rouleau
Copy link
Author

Thank you for the explanation! I'll look into it later after first learning more about the archive-mode and reading your code so I can understand better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants