Make snippets for org header args

This commit is contained in:
Kiana Sheibani 2024-03-07 01:46:11 -05:00
parent 1dedd8c273
commit 4d2e6eed7b
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
12 changed files with 155 additions and 2 deletions

View file

@ -2569,6 +2569,79 @@ We can then define and bind alternate versions of ~org-todo~ and ~org-agenda-tod
:m "T" #'org-agenda-todo-date) :m "T" #'org-agenda-todo-date)
#+end_src #+end_src
*** Header Argument Snippets
Writing header arguments is confusing, so let's automate the process somewhat by adding snippets to fill in keywords. To do this, we'll need to write some utility functions for checking if we're in a source block header.
(This is based on the similar code in [[https://tecosaur.github.io/emacs-config/config.html#snippet-helpers][Tecosaur's config]], though I don't care much for auto-filling the source language.)
#+begin_src emacs-lisp
(defun ~/yas-org-src-header-p ()
"Determine whether `point' is within a src-block header or header-args."
(let ((context (org-element-context)))
(pcase (org-element-type context)
('src-block (< (point) ; before code part of the src-block
(save-excursion
(goto-char (org-element-property :begin context))
(forward-line 1)
(point))))
('inline-src-block (< (point) ; before code part of the inline-src-block
(save-excursion
(goto-char (org-element-property :begin context))
(search-forward "]{")
(point))))
('keyword (string-match-p "^header-args"
(org-element-property :value context))))))
(defun ~/yas-org-src-lang ()
"Try to find the current language of the src/header at `point'.
Return nil otherwise."
(let ((context (org-element-context)))
(pcase (org-element-type context)
('src-block (org-element-property :language context))
('inline-src-block (org-element-property :language context))
('keyword
(when (string-match "^header-args:\\([^ ]+\\)"
(org-element-property :value context))
(match-string 1 (org-element-property :value context)))))))
(defun ~/yas-org-prompt-header-arg (arg question values)
"Prompt the user to set ARG header property to one of VALUES with QUESTION.
The default value is identified and indicated. If either default is selected,
or no selection is made: nil is returned."
(let* ((src-block-p (not (looking-back
"^#\\+property:[ \t]+header-args:.*"
(line-beginning-position))))
(default
(or
(cdr (assoc arg
(if src-block-p
(nth 2 (org-babel-get-src-block-info t))
(org-babel-merge-params
org-babel-default-header-args
(let ((lang-headers
(intern (concat "org-babel-default-header-args:"
(~/yas-org-src-lang)))))
(when (boundp lang-headers) (eval lang-headers t)))))))
""))
default-value)
(setq values (mapcar
(lambda (value)
(if (string-match-p (concat "^" (regexp-quote value) "$")
default)
(setq default-value
(concat value " "
(propertize "(default)" 'face
'font-lock-doc-face)))
value))
values))
(let ((selection
(consult--read values :prompt question :default default-value)))
(unless (or (string-match-p "(default)$" selection)
(string= "" selection))
selection))))
#+end_src
** Tags ** Tags
Org mode offers a useful tag hierarchy system, configured via ~org-tag-alist~. We'll be using ~org-tag-persistent-alist~ instead so that our tag hierarchy can't be overwritten. Org mode offers a useful tag hierarchy system, configured via ~org-tag-alist~. We'll be using ~org-tag-persistent-alist~ instead so that our tag hierarchy can't be overwritten.
@ -2682,7 +2755,7 @@ Creating new nodes should be quick and easy, so we should stick to one template
(setq org-roam-capture-templates (setq org-roam-capture-templates
'(("d" "Default" plain "%?" '(("d" "Default" plain "%?"
:target (file+head "${file-maybe-pick-dir}" :target (file+head "${file-maybe-pick-dir}"
"#+title: ${title}\n#+filetags:") "#+title: ${title}")
:unnarrowed t)) :unnarrowed t))
org-roam-dailies-capture-templates org-roam-dailies-capture-templates
'(("d" "Default" entry "* %?" '(("d" "Default" entry "* %?"
@ -3026,7 +3099,6 @@ The Doom module is very outdated, so I'll be overriding it.
idris-repl-show-repl-on-startup nil ; Don't show repl on startup idris-repl-show-repl-on-startup nil ; Don't show repl on startup
) )
(add-hook! idris-mode #'turn-on-idris-simple-indent)
(add-hook! idris-mode :append #'lsp!) (add-hook! idris-mode :append #'lsp!)
(set-repl-handler! 'idris-mode (cmd! (idris-repl-buffer))) (set-repl-handler! 'idris-mode (cmd! (idris-repl-buffer)))

View file

View file

@ -0,0 +1,6 @@
# -*- mode: snippet -*-
# key: S
# name: header arg - session
# condition: (~/yas-org-src-header-p)
# --
:session "${1:`(file-name-base (or (buffer-file-name) "unnnamed"))`-session}" $0

View file

@ -0,0 +1,6 @@
# -*- mode: snippet -*-
# key: d
# name: header arg - dir
# condition: (~/yas-org-src-header-p)
# --
:dir `(file-relative-name (read-directory-name "Working directory: "))` $0

View file

@ -0,0 +1,9 @@
# -*- mode: snippet -*-
# key: e
# name: header arg - export
# condition: (~/yas-org-src-header-p)
# --
`(let ((out (~/yas-org-prompt-header-arg
:exports "Exports: "
'("code" "results" "both" "none"))))
(if out (concat ":exports " out " ") ""))`

View file

@ -0,0 +1,6 @@
# -*- mode: snippet -*-
# key: f
# name: header arg - file
# condition: (~/yas-org-src-header-p)
# --
:file $0

View file

@ -0,0 +1,9 @@
# -*- mode: snippet -*-
# key: n
# name: header arg - noweb
# condition: (~/yas-org-src-header-p)
# --
`(let ((out (~/yas-org-prompt-header-arg
:noweb "NoWeb: "
'("no" "yes" "tangle" "no-export" "strip-export" "eval"))))
(if out (concat ":noweb " out " ") ""))`

View file

@ -0,0 +1,15 @@
# -*- mode: snippet -*-
# key: r
# name: header arg - results
# condition: (~/yas-org-src-header-p)
# --
`(let ((out
(->>
'((:results "Result collection: " ("value" "output"))
(:results "Results type: " ("table" "vector" "list" "verbatim" "file"))
(:results "Results format: " ("code" "drawer" "html" "latex" "link" "graphics" "org" "pp" "raw"))
(:results "Result output: " ("silent" "replace" "append" "prepend")))
(--map (apply #'~/yas-org-prompt-header-arg it))
-non-nil
(s-join " "))))
(if (string= out "") "" (concat ":results " out " ")))`

View file

@ -0,0 +1,6 @@
# -*- mode: snippet -*-
# key: s
# name: header arg - silent
# condition: (~/yas-org-src-header-p)
# --
:results silent $0

View file

@ -0,0 +1,6 @@
# -*- mode: snippet -*-
# key: t
# name: header arg - tangle
# condition: (~/yas-org-src-header-p)
# --
:tangle $0

View file

@ -0,0 +1,9 @@
# -*- mode: snippet -*-
# key: v
# name: header arg - eval
# condition: (~/yas-org-src-header-p)
# --
`(let ((out (~/yas-org-prompt-header-arg
:eval "Evaluate: "
'("no" "query" "no-export" "query-export"))))
(if out (concat ":eval " out " ") ""))`

View file

@ -0,0 +1,9 @@
# -*- mode: snippet -*-
# key: w
# name: header arg - wrap
# condition: (~/yas-org-src-header-p)
# --
`(let ((out (~/yas-org-prompt-header-arg
:noweb "Wrap: "
'("example" "export" "comment" "src"))))
(if out (concat ":wrap " out " ") ""))`