Why when changelog is opened does it not display as an org-mode file correctly? It displays the raw contents of the file.
code
(defconst my/sidebar-buffer-name " *my-sidebar*"
"The name of the buffer for the custom sidebar.")
(defconst my/sidebar-width 30
"The width in characters for the custom sidebar.")
(defun my/create-changelog-template-file (path project-name)
"Creates a new changelog.log file at PATH by running `my/insert-timestamp-log-here`."
(let ((temp-buffer (generate-new-buffer " *temp-changelog*")))
(unwind-protect
;; --- Main body of work (the "protected" form) ---
(with-current-buffer temp-buffer
;; Call the function directly with level 3. No need for `current-prefix-arg`.
(my/insert-timestamp-log-here 3)
;; Now, write the contents of our temporary buffer to the actual file on disk.
(write-region (point-min) (point-max) path))
(kill-buffer temp-buffer))))
(defun my/sidebar--setup-buffer ()
(with-current-buffer (get-buffer-create my/sidebar-buffer-name)
(let (buffer-read-only)
(erase-buffer)
(let* ((project-name (+workspace-current-name))
(changelog-path (file-name-concat my/org-projects-dir project-name "changelog.org")))
(if (file-exists-p changelog-path)
;; SCENARIO 1: The file already exists. Display it.
(insert-file-contents changelog-path)
;; SCENARIO 2: The file does NOT exist. Ask the user.
(if (yes-or-no-p (format "Changelog not found for %s. Create it now?" project-name))
;; User said "yes"
(progn
;; Create the file using our new template function.
(my/create-changelog-template-file changelog-path project-name)
;; Now that the file is created, display its content.
(insert-file-contents changelog-path))
;; User said "no"
(insert (format "No changelog found for project: %s" project-name))))))
(goto-char (point-min))))
(defun my/toggle-right-sidebar ()
"Toggle a sidebar on the far right of the frame using a side window."
(interactive)
(let ((sidebar-window (get-buffer-window my/sidebar-buffer-name)))
(if (and sidebar-window (window-live-p sidebar-window))
;; If the window exists, simply delete it.
(delete-window sidebar-window)
;; --- If the window does not exist, use display-buffer-in-side-window ---
(let ((sidebar-buffer (get-buffer-create my/sidebar-buffer-name)))
(display-buffer-in-side-window
sidebar-buffer
`((side . right) ; Place on the right side of the frame.
(window-width . ,my/sidebar-width) ; Set the width.
(slot . 0) ; Use the main slot on that side.
(window-parameters . ((no-other-window . t) ; Prevent C-x o from leaving it
(mode-line-format . nil)))))) ; Hide modeline
;; Call the setup function after the buffer is displayed.
(my/sidebar--setup-buffer)))))
(defun my/focus-right-sidebar ()
"If the sidebar is open, move the cursor to it."
(interactive)
(let ((sidebar-window (get-buffer-window my/sidebar-buffer-name)))
(when (and sidebar-window (window-live-p sidebar-window))
(select-window sidebar-window))))
Assistant
The issue is that the changelog file contents are not being displayed in org-mode because the buffer's major mode is not set to org-mode after inserting the file contents. The solution is to set the buffer's major mode to org-mode after inserting the file contents. However, to make it more robust and handle cases where the file might not actually be an org file (despite the .org extension), we can use a temporary buffer to open the file with find-file-noselect, which sets the mode correctly based on the file's properties, and then copy both the contents and the mode to the sidebar buffer.
Here's the final code with this approach:
elisp
(defun my/sidebar--setup-buffer ()
(with-current-buffer (get-buffer-create my/sidebar-buffer-name)
(let (buffer-read-only)
(erase-buffer)
(let* ((project-name (+workspace-current-name))
(changelog-path (file-name-concat my/org-projects-dir project-name "changelog.org")))
(if (file-exists-p changelog-path)
;; SCENARIO 1: The file already exists. Display it.
(with-temp-buffer
(find-file-noselect changelog-path)
(let ((file-buffer (current-buffer)))
(with-current-buffer my/sidebar-buffer-name
(erase-buffer)
(insert-buffer-substring file-buffer)
(set-buffer-modified-p nil)
(funcall (buffer-local-value 'major-mode file-buffer)))))
;; SCENARIO 2: The file does NOT exist. Ask the user.
(if (yes-or-no-p (format "Changelog not found for %s. Create it now?" project-name))
;; User said "yes"
(progn
;; Create the file using our new template function.
(my/create-changelog-template-file changelog-path project-name)
;; Now that the file is created, display its content.
(with-temp-buffer
(find-file-noselect changelog-path)
(let ((file-buffer (current-buffer)))
(with-current-buffer my/sidebar-buffer-name
(erase-buffer)
(insert-buffer-substring file-buffer)
(set-buffer-modified-p nil)
(funcall (buffer-local-value 'major-mode file-buffer))))))
;; User said "no"
(insert (format "No changelog found for project: %s" project-name))))))
(goto-char (point-min)))))
This ensures that the sidebar buffer displays the changelog file contents correctly with the appropriate major mode, without changing the buffer's visited file name.
User
Is there a method to display 2 buffers on top of each other in the sidebar? Say another function that if activated splits the sidebar in half? Also, I thought Emacs automatically activated org-mode for files that end in the .org suffix?
Assistant
To address your questions, we've implemented a function my/toggle-sidebar-split that toggles the sidebar between a single window and a vertically split window with two buffers. The first buffer displays the changelog, and the second buffer displays the project notes (or a message if the notes file doesn't exist).
Regarding the second question, the issue was that insert-file-contents doesn't trigger the automatic major mode setting based on file extensions. We fixed this by using find-file-noselect in a temporary buffer to open the file, which sets the major mode correctly, and then copying the contents and the major mode to our sidebar buffer.
Here's the final code with all the changes:
elisp
Dataset releases
Not yet included in a published release.
(defconst my/sidebar-buffer-name " *my-sidebar*"
"The name of the buffer for the custom sidebar.")
(defconst my/sidebar-buffer-name-2 "*my-sidebar-2*"
"The name of the second buffer for the custom sidebar.")
(defconst my/sidebar-width 30
"The width in characters for the custom sidebar.")
(defun my/setup-buffer-with-file (buffer-name file-path &optional create-file-fn)
"Set up BUFFER-NAME with the contents of FILE-PATH, setting the major mode appropriately.
If FILE-PATH does not exist and CREATE-FILE-FN is provided, call it to create the file."
(with-current-buffer (get-buffer-create buffer-name)
(let (buffer-read-only)
(erase-buffer)
(if (file-exists-p file-path)
(with-temp-buffer
(find-file-noselect file-path)
(let ((file-buffer (current-buffer)))
(with-current-buffer buffer-name
(erase-buffer)
(insert-buffer-substring file-buffer)
(set-buffer-modified-p nil)
(funcall (buffer-local-value 'major-mode file-buffer)))))
(if create-file-fn
(progn
(funcall create-file-fn file-path)
(with-temp-buffer
(find-file-noselect file-path)
(let ((file-buffer (current-buffer)))
(with-current-buffer buffer-name
(erase-buffer)
(insert-buffer-substring file-buffer)
(set-buffer-modified-p nil)
(funcall (buffer-local-value 'major-mode file-buffer))))))
(insert (format "No file found at: %s" file-path)))))))
(defun my/create-changelog-template-file (path project-name)
"Creates a new changelog.log file at PATH by running `my/insert-timestamp-log-here`."
(let ((temp-buffer (generate-new-buffer " *temp-changelog*")))
(unwind-protect
;; --- Main body of work (the "protected" form) ---
(with-current-buffer temp-buffer
;; Call the function directly with level 3. No need for `current-prefix-arg`.
(my/insert-timestamp-log-here 3)
;; Now, write the contents of our temporary buffer to the actual file on disk.
(write-region (point-min) (point-max) path))
(kill-buffer temp-buffer))))
(defun my/sidebar--setup-buffer ()
(let* ((project-name (+workspace-current-name))
(changelog-path (file-name-concat my/org-projects-dir project-name "changelog.org")))
(my/setup-buffer-with-file
my/sidebar-buffer-name
changelog-path
(lambda (path)
(when (yes-or-no-p (format "Changelog not found for %s. Create it now?" project-name))
(my/create-changelog-template-file path project-name))))))
(defun my/toggle-right-sidebar ()
"Toggle a sidebar on the far right of the frame using a side window."
(interactive)
(let ((sidebar-window (get-buffer-window my/sidebar-buffer-name)))
(if (and sidebar-window (window-live-p sidebar-window))
;; If the window exists, simply delete it.
(delete-window sidebar-window)
;; --- If the window does not exist, use display-buffer-in-side-window ---
(let ((sidebar-buffer (get-buffer-create my/sidebar-buffer-name)))
(display-buffer-in-side-window
sidebar-buffer
`((side . right) ; Place on the right side of the frame.
(window-width . ,my/sidebar-width) ; Set the width.
(slot . 0) ; Use the main slot on that side.
(window-parameters . ((no-other-window . t) ; Prevent C-x o from leaving it
(mode-line-format . nil)))))) ; Hide modeline
;; Call the setup function after the buffer is displayed.
(my/sidebar--setup-buffer)))))
(defun my/toggle-sidebar-split ()
"Toggle the sidebar between split and unsplit states."
(interactive)
(let ((sidebar-window (get-buffer-window my/sidebar-buffer-name)))
(when (and sidebar-window (window-live-p sidebar-window))
(let ((child-windows (window-list sidebar-window)))
(if (> (length child-windows) 1)
;; If the window is already split, delete the child windows
(progn
(delete-window (car (last child-windows)))
(delete-window (car child-windows)))
;; If the window is not split, split it vertically
(let ((window-width (window-width sidebar-window)))
(split-window-vertically)
(let ((new-window (next-window)))
(set-window-buffer new-window (get-buffer-create my/sidebar-buffer-name-2))
(let* ((project-name (+workspace-current-name))
(notes-path (file-name-concat my/org-projects-dir project-name "notes.org")))
(my/setup-buffer-with-file my/sidebar-buffer-name-2 notes-path))
;; Ensure both windows have the same width
(set-window-width sidebar-window window-width)
(set-window-width new-window window-width))))))))
(defun my/focus-right-sidebar ()
"If the sidebar is open, move the cursor to it."
(interactive)
(let ((sidebar-window (get-buffer-window my/sidebar-buffer-name)))
(when (and sidebar-window (window-live-p sidebar-window))
(select-window sidebar-window))))
This implementation allows you to toggle the sidebar between a single window and a vertically split window with two buffers. The first buffer displays the changelog, and the second buffer displays the project notes (or a message if the notes file doesn't exist). The major mode is set correctly based on the file extension, and the buffers are set up consistently using the helper function my/setup-buffer-with-file.