From 4d2e6eed7b79fe6c5443c0060656a697539190cd Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Thu, 7 Mar 2024 01:46:11 -0500 Subject: [PATCH] Make snippets for org header args --- config.org | 76 +++++++++++++++++++++++++++++- snippets/org-mode/.yas-make-groups | 0 snippets/org-mode/header args/S | 6 +++ snippets/org-mode/header args/d | 6 +++ snippets/org-mode/header args/e | 9 ++++ snippets/org-mode/header args/f | 6 +++ snippets/org-mode/header args/n | 9 ++++ snippets/org-mode/header args/r | 15 ++++++ snippets/org-mode/header args/s | 6 +++ snippets/org-mode/header args/t | 6 +++ snippets/org-mode/header args/v | 9 ++++ snippets/org-mode/header args/w | 9 ++++ 12 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 snippets/org-mode/.yas-make-groups create mode 100644 snippets/org-mode/header args/S create mode 100644 snippets/org-mode/header args/d create mode 100644 snippets/org-mode/header args/e create mode 100644 snippets/org-mode/header args/f create mode 100644 snippets/org-mode/header args/n create mode 100644 snippets/org-mode/header args/r create mode 100644 snippets/org-mode/header args/s create mode 100644 snippets/org-mode/header args/t create mode 100644 snippets/org-mode/header args/v create mode 100644 snippets/org-mode/header args/w diff --git a/config.org b/config.org index 734c69d..fae63ff 100644 --- a/config.org +++ b/config.org @@ -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) #+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 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 '(("d" "Default" plain "%?" :target (file+head "${file-maybe-pick-dir}" - "#+title: ${title}\n#+filetags:") + "#+title: ${title}") :unnarrowed t)) org-roam-dailies-capture-templates '(("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 ) - (add-hook! idris-mode #'turn-on-idris-simple-indent) (add-hook! idris-mode :append #'lsp!) (set-repl-handler! 'idris-mode (cmd! (idris-repl-buffer))) diff --git a/snippets/org-mode/.yas-make-groups b/snippets/org-mode/.yas-make-groups new file mode 100644 index 0000000..e69de29 diff --git a/snippets/org-mode/header args/S b/snippets/org-mode/header args/S new file mode 100644 index 0000000..284e1e5 --- /dev/null +++ b/snippets/org-mode/header args/S @@ -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 diff --git a/snippets/org-mode/header args/d b/snippets/org-mode/header args/d new file mode 100644 index 0000000..0e69f7f --- /dev/null +++ b/snippets/org-mode/header args/d @@ -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 \ No newline at end of file diff --git a/snippets/org-mode/header args/e b/snippets/org-mode/header args/e new file mode 100644 index 0000000..f487172 --- /dev/null +++ b/snippets/org-mode/header args/e @@ -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 " ") ""))` \ No newline at end of file diff --git a/snippets/org-mode/header args/f b/snippets/org-mode/header args/f new file mode 100644 index 0000000..ba37ef7 --- /dev/null +++ b/snippets/org-mode/header args/f @@ -0,0 +1,6 @@ +# -*- mode: snippet -*- +# key: f +# name: header arg - file +# condition: (~/yas-org-src-header-p) +# -- +:file $0 \ No newline at end of file diff --git a/snippets/org-mode/header args/n b/snippets/org-mode/header args/n new file mode 100644 index 0000000..fb8f239 --- /dev/null +++ b/snippets/org-mode/header args/n @@ -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 " ") ""))` \ No newline at end of file diff --git a/snippets/org-mode/header args/r b/snippets/org-mode/header args/r new file mode 100644 index 0000000..3b9917b --- /dev/null +++ b/snippets/org-mode/header args/r @@ -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 " ")))` \ No newline at end of file diff --git a/snippets/org-mode/header args/s b/snippets/org-mode/header args/s new file mode 100644 index 0000000..c89a6db --- /dev/null +++ b/snippets/org-mode/header args/s @@ -0,0 +1,6 @@ +# -*- mode: snippet -*- +# key: s +# name: header arg - silent +# condition: (~/yas-org-src-header-p) +# -- +:results silent $0 diff --git a/snippets/org-mode/header args/t b/snippets/org-mode/header args/t new file mode 100644 index 0000000..36f3d23 --- /dev/null +++ b/snippets/org-mode/header args/t @@ -0,0 +1,6 @@ +# -*- mode: snippet -*- +# key: t +# name: header arg - tangle +# condition: (~/yas-org-src-header-p) +# -- +:tangle $0 diff --git a/snippets/org-mode/header args/v b/snippets/org-mode/header args/v new file mode 100644 index 0000000..3e2ce3e --- /dev/null +++ b/snippets/org-mode/header args/v @@ -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 " ") ""))` \ No newline at end of file diff --git a/snippets/org-mode/header args/w b/snippets/org-mode/header args/w new file mode 100644 index 0000000..80c19e1 --- /dev/null +++ b/snippets/org-mode/header args/w @@ -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 " ") ""))` \ No newline at end of file