Something like this? (From Chatgpt, it seems to work):

;;;_ Remove the "\\" automatically inserted at the end of the 1st line of any 
Org log note
(defun my-org--strip-trailing-backslashes-in-first-line (orig-fn &rest args)
  "Around advice for ORIG-FN that removes a trailing '\\\\' at the end
of the first line inserted by Org log notes (e.g. `org-add-note`)."
  (let (min-pos max-pos)
    (cl-labels
        ((tracker (beg end &rest _)
           ;; Record the full region modified by ORIG-FN in this buffer.
           (setq min-pos (if min-pos (min min-pos beg) beg))
           (setq max-pos (if max-pos (max max-pos end) end))))
      (unwind-protect
          (progn
            (add-hook 'after-change-functions #'tracker nil t) ; buffer-local
            (prog1 (apply orig-fn args)
              (when (and min-pos max-pos (< min-pos max-pos))
                (save-excursion
                  (save-restriction
                    (narrow-to-region min-pos max-pos)
                    (goto-char (point-min))
                    (end-of-line)
                    ;; If the first inserted line ends with "\\", remove it.
                    (when (looking-back "\\\\\\\\[ \t]*" 
(line-beginning-position))
                      (replace-match "")))))))
        (remove-hook 'after-change-functions #'tracker t)))))

;; Activate the advice for Org functions that insert log notes
(with-eval-after-load 'org
  (when (fboundp 'org-add-log-note)
    (advice-add 'org-add-log-note :around
                #'my-org--strip-trailing-backslashes-in-first-line))
  (when (fboundp 'org-store-log-note) ;; some Org versions use this
    (advice-add 'org-store-log-note :around
                #'my-org--strip-trailing-backslashes-in-first-line)))

Reply via email to