2024-01-23 08:01:12 -09:00
|
|
|
;;; mir-orgstuff.el --- Hacking on top of Org mode.
|
2022-09-16 17:45:59 -08:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Some of the things I want to do with org aren't built-in or intended. This
|
|
|
|
;; file is the cum total of my intent to implement these things.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(defvar org-directory (expand-file-name "org/" "~"))
|
2022-09-24 19:02:57 -08:00
|
|
|
(defvar org-agenda-files '("~/org/"))
|
2022-09-16 17:45:59 -08:00
|
|
|
(defvar org-vw-dir org-directory) ;; whoops
|
2022-09-24 19:02:57 -08:00
|
|
|
(defvar org-wiki-regex "\\`[^.].*\\.org\\'")
|
2022-09-16 17:45:59 -08:00
|
|
|
|
|
|
|
(defun org-vw-get-filename (word)
|
|
|
|
"Given WORD, generate the absolute filename for that org-vw entry."
|
|
|
|
(expand-file-name (format "%s.org" (downcase word)) org-vw-dir))
|
|
|
|
;; (org-vw-get-filename "wordWERD")
|
|
|
|
|
|
|
|
(defun org-vw-make-newlink ()
|
|
|
|
"Make the word at point the link to an org file, like in VimWiki."
|
|
|
|
(interactive)
|
|
|
|
(let* ((oldpoint (point))
|
|
|
|
(current-word (thing-at-point 'word 'no-properties))
|
|
|
|
(cw-file-name (org-vw-get-filename current-word)))
|
|
|
|
(backward-word)
|
|
|
|
(kill-word 1)
|
|
|
|
(org-insert-link nil cw-file-name current-word)
|
|
|
|
(goto-char oldpoint)))
|
|
|
|
;; (org-insert-link nil "~/" "a file or sth")
|
|
|
|
|
|
|
|
(defun org-vw-show-markup () ;;broken, just use org-appear or sth
|
|
|
|
"When on a line containing hidden characters, show them."
|
|
|
|
(interactive)
|
|
|
|
(let ((point (point))
|
|
|
|
(bol (point-at-bol))
|
|
|
|
(eol (point-at-eol)))
|
|
|
|
(remove-text-properties bol eol
|
|
|
|
'(invisible nil))
|
|
|
|
t))
|
|
|
|
(defun org-vw-back ()
|
|
|
|
"Go back to the previous org file and bury this buffer."
|
|
|
|
(interactive)
|
|
|
|
(if (buffer-modified-p)
|
|
|
|
(if (y-or-n-p "Do you want to save this file?")
|
|
|
|
(save-buffer)))
|
|
|
|
(bury-buffer)
|
|
|
|
(other-window 1))
|
|
|
|
|
2023-05-29 16:20:23 -08:00
|
|
|
(defvar org-vw-pre-hook nil
|
|
|
|
"Hook to run before org-vw-mode has been loaded.")
|
2022-09-16 17:45:59 -08:00
|
|
|
(define-minor-mode org-vw-mode
|
|
|
|
"Org VimWiki mode."
|
|
|
|
:lighter " VW"
|
|
|
|
:keymap (make-sparse-keymap)
|
2023-05-29 16:20:23 -08:00
|
|
|
(run-hooks org-vw-pre-hook)
|
2023-03-10 18:42:45 -09:00
|
|
|
;; TODO: This is a hack, because hyperbole is scary. Ideal implementation
|
|
|
|
;; would add a proper case rather than this weird fallback.
|
2022-09-24 18:59:50 -08:00
|
|
|
(require 'hyperbole)
|
2022-09-16 17:45:59 -08:00
|
|
|
(make-local-variable action-key-default-function)
|
|
|
|
(setq-local action-key-default-function #'org-vw-make-newlink)
|
|
|
|
;; General:
|
|
|
|
(general-define-key
|
|
|
|
:keymaps 'local
|
|
|
|
:states 'normal
|
2023-05-29 16:20:23 -08:00
|
|
|
"DEL" 'org-vw-back)
|
2024-01-23 08:01:12 -09:00
|
|
|
(dolist (key mir/org-dwim-char-chars)
|
2023-05-29 16:20:23 -08:00
|
|
|
(general-define-key
|
|
|
|
:keymaps 'local
|
2024-01-23 08:01:12 -09:00
|
|
|
key 'mir/org-dwim-char))
|
2023-05-29 16:20:23 -08:00
|
|
|
)
|
2022-09-16 17:45:59 -08:00
|
|
|
;; (add-to-list 'auto-mode-alist `(,org-wiki-regex . org-vw-mode))
|
|
|
|
(add-hook 'org-mode-hook #'org-vw-mode)
|
|
|
|
|
2022-12-09 10:22:50 -09:00
|
|
|
;; ------------------ ABANDON ALL SANITY, YE WHO ENTER HERE --------------------
|
|
|
|
|
2024-01-23 08:01:12 -09:00
|
|
|
(defvar mir/org-dwim-char-chars '("_" "/" "*" "+" "~" "="))
|
|
|
|
(defun mir/org-dwim-char (&optional char)
|
2022-12-09 10:22:50 -09:00
|
|
|
"If a region is active (visual mode), surround selection with CHAR.
|
|
|
|
|
|
|
|
If in a word, surround it with CHAR (like `evil-surround') or, if word is
|
|
|
|
already surrounded with CHAR, un-surround it. If on the first character of a
|
|
|
|
word, run the default function that Evil binds the key CHAR to.
|
|
|
|
|
|
|
|
If the next character is the same as CHAR, move cursor past it \(like
|
|
|
|
`electric-pair').
|
|
|
|
|
|
|
|
If the line is a block delimiter or heading, or when in a protected block \(see
|
|
|
|
variable `org-protecting-blocks') or properties drawer, just insert the
|
|
|
|
character CHAR.
|
|
|
|
|
|
|
|
Otherwise, insert two of CHAR and put point between them like `electric-pair'."
|
|
|
|
(interactive) ; TODO: can we make CHAR an arg to `interactive'?
|
2023-05-29 16:20:23 -08:00
|
|
|
(let* ((char (or char (string-to-char (this-command-keys))))
|
|
|
|
(word (thing-at-point 'word 'no-properties))
|
|
|
|
;;; test conditions here to be eval'd, separating flow from data
|
2023-06-12 19:47:40 -08:00
|
|
|
;; at beginning of line
|
|
|
|
(beginning-of-line-test '(bolp))
|
2023-05-29 16:20:23 -08:00
|
|
|
;; at beginning of word
|
2023-06-12 19:47:40 -08:00
|
|
|
(beginning-of-word-test `(or ,beginning-of-line-test
|
|
|
|
(eq (char-before (point)) ?\ )))
|
2023-05-29 16:20:23 -08:00
|
|
|
;; word is surrounded by `char'
|
|
|
|
(surrounded-by-char-test
|
|
|
|
'(and (eq (char-before (beginning-of-thing 'word)) char)
|
|
|
|
(eq (char-after (end-of-thing 'word)) char)))
|
|
|
|
;; char before and char after are the same
|
|
|
|
(double-char-test (eq char (char-after)))
|
|
|
|
;; Weird Org exceptions because org
|
|
|
|
(org-exceptions-test
|
2023-06-12 19:47:40 -08:00
|
|
|
'(or (and (bolp)
|
|
|
|
(string-match-p (rx bol (or "#" "*"))
|
|
|
|
(thing-at-point 'line 'no-properties)))
|
2023-05-29 16:20:23 -08:00
|
|
|
(org-in-block-p org-protecting-blocks)
|
2023-06-12 19:47:40 -08:00
|
|
|
(org-at-property-p))))
|
2022-12-09 10:22:50 -09:00
|
|
|
(cond
|
|
|
|
((eq evil-state 'visual)
|
|
|
|
(let* ((beg (region-beginning))
|
|
|
|
(end (region-end)))
|
|
|
|
(evil-surround-region beg end evil-visual-selection char nil)))
|
|
|
|
((eq evil-state 'normal)
|
2023-05-29 16:20:23 -08:00
|
|
|
(if (eval beginning-of-word-test)
|
2022-12-09 10:22:50 -09:00
|
|
|
;; run original function bound to key
|
|
|
|
(let ((charstr (make-string 1 char)))
|
2023-05-29 16:20:23 -08:00
|
|
|
(call-interactively (alist-get charstr org-vw-old-binds-alist
|
2023-06-12 19:47:40 -08:00
|
|
|
nil nil #'string=)))
|
2022-12-09 10:22:50 -09:00
|
|
|
(save-excursion
|
2023-05-29 16:20:23 -08:00
|
|
|
(if (eval surrounded-by-char-test)
|
2022-12-09 10:22:50 -09:00
|
|
|
(progn
|
|
|
|
(search-backward (char-to-string char)) (delete-char 1)
|
|
|
|
(search-forward (char-to-string char)) (delete-char -1))
|
|
|
|
(evil-with-single-undo
|
|
|
|
(beginning-of-thing 'word)
|
|
|
|
(insert char)
|
|
|
|
(end-of-thing 'word)
|
|
|
|
(insert char))))))
|
2023-05-29 16:20:23 -08:00
|
|
|
((eval double-char-test) (right-char))
|
|
|
|
((eval org-exceptions-test) (insert char))
|
2023-06-12 19:47:40 -08:00
|
|
|
((and (eval beginning-of-line-test) (eq char ?*))
|
|
|
|
(insert char))
|
2022-12-09 10:22:50 -09:00
|
|
|
(t (progn
|
|
|
|
(insert (make-string 2 char))
|
|
|
|
(left-char))))))
|
2023-05-29 16:20:23 -08:00
|
|
|
(defun org-vw-snapshot-bindings ()
|
2024-01-23 08:01:12 -09:00
|
|
|
"Make an alist of the functions formerly ran by keys to be bound to `mir/org-dwim-char'.
|
2023-05-29 16:20:23 -08:00
|
|
|
|
|
|
|
Store it in the variable `org-vw-old-binds-alist'."
|
|
|
|
(unless org-vw-old-binds-alist
|
|
|
|
(defvar-local org-vw-old-binds-alist
|
|
|
|
(mapcar (lambda (key)
|
|
|
|
(cons key (key-binding key)))
|
2024-01-23 08:01:12 -09:00
|
|
|
mir/org-dwim-char-chars))))
|
2023-05-29 16:20:23 -08:00
|
|
|
(add-hook 'org-vw-pre-hook #'org-vw-snapshot-bindings nil 'local)
|
2023-06-12 19:47:40 -08:00
|
|
|
|
|
|
|
;; (defun org-vw-backspace)
|
|
|
|
|
2022-12-09 10:22:50 -09:00
|
|
|
;; (setq debug-on-error t)
|
|
|
|
;; --------- LET THY BRAIN NO LONGER TREMBLE, FOR I AM BECOME COMPLETE ---------
|
|
|
|
|
2024-01-23 08:01:12 -09:00
|
|
|
(provide 'mir-orgstuff)
|
|
|
|
;;; mir-orgstuff.el ends here
|