From 7e6e98e5683f2b0d8d335f53100b6b3623823df3 Mon Sep 17 00:00:00 2001
From: Yuchen Pei <id@ypei.org>
Date: Sat, 2 Sep 2023 13:45:55 +1000
Subject: A few more org utilities, and small fixes in editing / goto places

added my-org-swap-referral-with-headline
added my-org-clean-up-entry
fixed avy binding
org capture todo: contain initial content (if any) in an example block
---
 emacs/.emacs.d/init/ycp-complete.el |  3 +--
 emacs/.emacs.d/init/ycp-editing.el  |  7 ++++++-
 emacs/.emacs.d/init/ycp-gnus.el     |  1 +
 emacs/.emacs.d/init/ycp-org.el      | 10 ++++++++--
 emacs/.emacs.d/lisp/my/my-org.el    | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/emacs/.emacs.d/init/ycp-complete.el b/emacs/.emacs.d/init/ycp-complete.el
index 1c9e7ef..c527329 100644
--- a/emacs/.emacs.d/init/ycp-complete.el
+++ b/emacs/.emacs.d/init/ycp-complete.el
@@ -194,7 +194,7 @@
 
 (my-package imenu
   (:delay 5)
-  (my-keybind global-map "M-s i" #'imenu)
+  (my-keybind global-map "M-g i" #'imenu)
   )
 
 ;;; consult
@@ -219,7 +219,6 @@
     "C-x b" #'consult-buffer
     "C-z" #'consult-buffer
     "C-x l" #'consult-locate
-    "M-g M-g" #'consult-goto-line
     "M-K" #'consult-keep-lines            ; M-S-k is similar to M-S-5 (M-%)
     "M-F" #'consult-focus-lines           ; same principle
     "M-s M-b" #'consult-buffer
diff --git a/emacs/.emacs.d/init/ycp-editing.el b/emacs/.emacs.d/init/ycp-editing.el
index 5634f86..65189de 100644
--- a/emacs/.emacs.d/init/ycp-editing.el
+++ b/emacs/.emacs.d/init/ycp-editing.el
@@ -116,9 +116,14 @@
 (my-package avy
   (:install t)
   (:delay 15)
-  (my-keybind global-map "C-." #'avy-goto-char-timer)
+  (my-keybind global-map
+    "C-." #'avy-goto-char-timer
+    "M-g M-g" #'avy-goto-line)
   (setq avy-keys '(97 115 100 102 103 104 106 107 108)))
 
+(my-package flyspell
+  (my-keybind flyspell-mode-map "C-." nil))
+
 (my-package pyim
   (:delay 30)
   (:install t))
diff --git a/emacs/.emacs.d/init/ycp-gnus.el b/emacs/.emacs.d/init/ycp-gnus.el
index 4c68f2b..e104d25 100644
--- a/emacs/.emacs.d/init/ycp-gnus.el
+++ b/emacs/.emacs.d/init/ycp-gnus.el
@@ -182,6 +182,7 @@
   (:delay 5)
   (bbdb-initialize 'gnus 'message)
   (bbdb-mua-auto-update-init 'gnus 'message)
+  (setq bbdb-dedicated-window t)
   (setq bbdb-message-all-addresses t)
   (setq bbdb-mua-pop-up-window-size .15)
   (setq bbdb-new-mails-primary nil)
diff --git a/emacs/.emacs.d/init/ycp-org.el b/emacs/.emacs.d/init/ycp-org.el
index 81f30ee..2f93b3d 100644
--- a/emacs/.emacs.d/init/ycp-org.el
+++ b/emacs/.emacs.d/init/ycp-org.el
@@ -230,7 +230,12 @@
 :Referral: %a
 :END:
 
-%i
+%(unless (string-empty-p \"%i\")
+         (format
+          \"#+begin_example
+%s
+#+end_example\"
+          (org-escape-code-in-string \"%i\")))
 "
 	         :prepend t)
 	        ("ya" "Blank audio" entry
@@ -296,7 +301,8 @@
   (setq org-use-speed-commands t)
   (setq org-speed-commands
         '(("User commands")
-          ("g" . org-delete-property)
+          ("T" . my-org-swap-referral-with-headline)
+          ("D" . my-org-clean-up-entry)
           ("W" . my-org-refile-logbook)
           ("+" . my-org-vote-up)
           ("-" . my-org-vote-down)
diff --git a/emacs/.emacs.d/lisp/my/my-org.el b/emacs/.emacs.d/lisp/my/my-org.el
index 334f708..1a44a10 100644
--- a/emacs/.emacs.d/lisp/my/my-org.el
+++ b/emacs/.emacs.d/lisp/my/my-org.el
@@ -1120,6 +1120,39 @@ When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
           (list "ITEM" "Referral" "Wikipedia-link" "IMDB-link")))
    "\t"))
 
+(defun my-org-swap-referral-with-headline ()
+  "Swap Referral property with headline of org node at point.
+
+Remove the Referral property if empty."
+  (interactive)
+  (let ((ref (or (org-entry-get (point) "Referral") ""))
+        (headline (org-entry-get (point) "ITEM")))
+    (if (string-empty-p headline)
+        (org-delete-property "Referral")
+      (org-entry-put (point) "Referral" headline))
+    (org-edit-headline ref)))
+
+(defun my-org-clean-up-entry ()
+  "Clean up an org entry.
+
+Delete empty standard properties.
+Sync attachment.
+Remove trailing whitespaces.
+Flush lines with only some common symbols."
+  (interactive)
+  (my-org-delete-empty-properties)
+  (org-attach-sync)
+  (save-restriction
+    (org-narrow-to-subtree)
+    (flush-lines "^[ \t-]*$")
+    (delete-trailing-whitespace)))
+
+(defun my-org-delete-empty-properties ()
+  "Delete empty (standard) properties at point."
+  (interactive)
+  (pcase-dolist (`(,name . ,value) (org-entry-properties (point) 'standard))
+    (when (string-empty-p value) (org-entry-delete (point) name))))
+
 (defvar org-entries-tsv-buffer "*org-entries-tsv*")
 (defun my-org-entries-at-point-to-tsv (beg end)
   (interactive "r")
-- 
cgit v1.2.3