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
To resolve the issue where the changelog content appears in Fundamental mode instead of Org mode, the root cause lies in how the buffer is set up. The current implementation uses insert-file-contents to load the file content into a newly created buffer, but it does not explicitly set the major mode to org-mode. This results in the content being displayed as raw text, without any Org-specific syntax highlighting or formatting.
✅ Solution
You need to explicitly switch the buffer to org-mode after erasing it but before inserting the file contents. This ensures that Org mode is active when the file contents are inserted, allowing for proper formatting and rendering of the Org file.
Here's the corrected version of the my/sidebar--setup-buffer function:
elisp
(defun my/sidebar--setup-buffer ()
(with-current-buffer (get-buffer-create my/sidebar-buffer-name)
(let (buffer-read-only)
(erase-buffer)
;; Switch to Org mode before inserting content
(org-mode)
(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: File exists, insert contents
(insert-file-contents changelog-path)
;; Scenario 2: File does not exist, prompt to create
(if (yes-or-no-p (format "Changelog not found for %s. Create it now?" project-name))
(progn
(my/create-changelog-template-file changelog-path project-name)
(insert-file-contents changelog-path))
(insert (format "No changelog found for project: %s" project-name)))))
(goto-char (point-min)))))
🔍 Why This Works
insert-file-contents only inserts the raw text of the file into the buffer. It does .
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 both parts of your question:
✅ 1. Displaying Two Buffers in the Sidebar (Split Vertically)
Yes, you can split the sidebar into two vertical sections (upper and lower) and display different buffers in each. Here’s how to implement it:
🔧 Example Implementation:
elisp
(defconst my/sidebar-upper-buffer-name " *my-sidebar-upper*"
"Buffer name for the upper half of the sidebar.")
(defconst my/sidebar-lower-buffer-name " *my-sidebar-lower*"
"Buffer name for the lower half of the sidebar.")
(defun my/split-sidebar ()
"Split the sidebar into two vertical windows (upper and lower)."
(interactive)
(let ((sidebar-window (get-buffer-window my/sidebar-buffer-name)))
(unless sidebar-window
(user-error "Sidebar is not open"))
;; Split the existing sidebar window vertically
(let* ((upper-window sidebar-window)
(lower-window (split-window upper-window nil 'below)))
;; Set dedicated buffers for each half
(set-window-buffer upper-window my/sidebar-upper-buffer-name)
(set-window-buffer lower-window my/sidebar-lower-buffer-name)
;; Optional: Adjust heights
(set-window-dedicated-p upper-window t)
(set-window-dedicated-p lower-window t)
(window-resize lower-window (- (window-total-height upper-window)) t))))
(defun my/setup-sidebar-buffers ()
"Populate the upper and lower sidebar buffers."
(my/setup-upper-buffer)
(my/setup-lower-buffer))
(defun my/setup-upper-buffer ()
"Populate the upper buffer (e.g., changelog.org)."
(with-current-buffer (get-buffer-create my/sidebar-upper-buffer-name)
(let (buffer-read-only)
(erase-buffer)
(org-mode)
(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)
(insert-file-contents changelog-path)
(insert (format "No changelog found for %s" project-name))))
(goto-char (point-min)))))
(defun my/setup-lower-buffer ()
"Populate the lower buffer (e.g., a TODO list)."
(with-current-buffer (get-buffer-create my/sidebar-lower-buffer-name)
(let (buffer-read-only)
(erase-buffer)
(org-mode)
(insert "* TODO List\n\n- [ ] Task 1\n- [ ] Task 2\n- [ ] Task 3")
(goto-char (point-min)))))
Dataset releases
Not yet included in a published release.
not activate any major mode
By calling (org-mode) immediately after erase-buffer, you ensure that the buffer is in Org mode before any content is inserted.
This allows Org mode's font-locking, indentation, and outline features to apply to the inserted content, resulting in the expected formatted display.
🧪 Optional Enhancement (Buffer File Name)
If you want to further ensure compatibility with Org mode features (like exporting), you can optionally set the buffer’s file name to simulate that it’s visiting the actual Org file:
elisp
(set-visited-file-name changelog-path)
However, this is optional and depends on whether you want the buffer to behave like a regular file-visiting buffer.
✅ Final Note
This change ensures that the changelog file is displayed correctly in Org mode when opened in the sidebar, providing the expected visual formatting and Org-specific behavior.
📌 Usage:
Call my/toggle-right-sidebar to open the sidebar.
Then call my/split-sidebar to split it into two sections.
You can customize my/setup-upper-buffer and my/setup-lower-buffer to load any content you want.
❓ 2. Why Isn’t Org Mode Activated Automatically?
Emacs does automatically activate org-mode for .org files when you open them via find-file or similar commands that "visit" the file. However, in your original code, you’re using insert-file-contents to copy the file’s text into a buffer, not actually visiting the file. This bypasses the auto-mode-alist logic, so no major mode is set.
🔍 Why This Happens:
insert-file-contents only inserts raw text.
Auto-mode-alist (which maps .org to org-mode) is triggered only when visiting a file (e.g., with find-file).