From 96f00fb630dc8c3aa0182e5c5434d2c208af4b22 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sat, 19 Apr 2025 16:53:04 +0200 Subject: [PATCH] Ignore direct input when at the start of an Org headline This is to guard headlines to become invalid due to my erratic (mis)typing behavior. --- init.el | 15 ++++++++++----- site-lisp/db-org.el | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/init.el b/init.el index 1bc282b..253745f 100644 --- a/init.el +++ b/init.el @@ -646,6 +646,7 @@ db/org-execute-babel-in-buffer-and-iterate-tables db/make-org-capture-frame db/org-cleanup-continuous-clocks + db/org-update-headline-log-note db/find-csv-in-org db/export-diary db/org-insert-checklist @@ -675,6 +676,7 @@ db/org-mark-current-default-task db/org-clocktable-write-with-threshold org-dblock-write:db/org-backlinks + db/org-ignore-insert-on-headline-start org-password-manager-get-password-by-id db/org-bookmark-open db/org-bookmark-store-link @@ -744,10 +746,9 @@ "%80ITEM(Task) %10Effort(Effort) %10CLOCKSUM" ;; We activate speed keys primarily to avoid invalidating headlines by accidentally - ;; typing non-star characters when at the start of a headline. - org-use-speed-commands #'(lambda () - (and (looking-at org-outline-regexp) - (looking-back "^\\**" nil)))) + ;; typing non-star characters when at the start of a headline. See + ;; `db/org-ignore-insert-on-headline-start'. + org-use-speed-commands t) ;; Keywords and Tags @@ -920,7 +921,11 @@ ;; Completely redo agenda buffer when jumping to today; this ensures that agenda views ;; from previous days get updated as expected. (define-advice org-agenda-goto-today (:before () redo-all) - (org-agenda-redo-all)))) + (org-agenda-redo-all)) + + ;; Inhibit direct input when point is at the beginning of a headline, i.e., before the + ;; first space that separates the stars from the content of the headline. + (add-to-list 'org-speed-command-hook 'db/org-ignore-insert-on-headline-start))) (use-package org-cycle :autoload (org-cycle-hide-drawers) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index 51cbff6..59f07c4 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -1243,6 +1243,20 @@ cache if that's in use." (when (derived-mode-p 'org-agenda-mode) (org-agenda-redo))) +(defun db/org-ignore-insert-on-headline-start (keys) + "Return error function when point is at the stars of a headline. + +This function is meant to be added to `org-speed-command-hook'. KEYS is +ignored. + +The implementat has been adapted from `org-speed-command-activate' to +ignore all keys pressed at the beginning of a headline." + (ignore keys) + (when (and (looking-at org-outline-regexp) + (looking-back "^\\**" nil)) + #'(lambda () + (user-error "No direct input allowed in headline")))) + (defun org-password-manager-get-password-by-id (id &optional return-as-value) "Retrieve password from Org item identified by ID.