Which 15.6 Results of Evaluation options are needed?
Collection: value
Why? Because output
puts it into scripting mode and you need to write to the standard output stream for the results.
My initial problems with value
was that I was trying to return an elisp list as the result of the function, and org-mode was pretty printing the list onto a single line needing me to find How do I prevent extremely long lines making Emacs slow?
I was relying on org determining the correct type and format for me, which may have contributed to misunderstandings.
Type: unspecified
Manually setting this to each of the values: ‘table’, ‘vector’, ‘list’, ‘scalar’, ‘verbatim’ produced incorrect results.
Format: org
with wrap: SRC shell
The output is a list of shell commands, so it needs to be wrapped in a #+begin_src
block. The wrap header changes this from the default #+begin_org
to #+begin_src
and appends shell
to the wrapped block.
Results of Function: multi-line string
This bit caused a lot of problems.
I assumed I could build up my list structure and org-mode would do the right thing. Having the long lines issue making Emacs slow also didn't help trial-and-error coding.
Example code
Please excuse my poor Emacs-fu. This code works, I have no idea whether it conforms to anything remotely like decent elisp style.
#+NAME: generate-jira-create-issue-commands
#+BEGIN_SRC emacs-lisp :results value org :wrap SRC shell
(require 's)
(let* ((base-url "https://jira.example.com/rest/api/2")
(resource-name "issue")
(defaults '(("projectId" . 12345) ; your project id in your JIRA instance
("issuetypeId" . 3) ; JIRA "Task"
("assigneeName" . "<your user id>")))
(commands '())
(headings '())
(api-url (s-format "${base-url}/${resource-name}" 'aget `(("base-url" . ,base-url) ("resource-name" . ,resource-name))))
(jira-create-issue-template "curl --insecure -b ~/jira-session.txt --request POST --header \"Content-Type: application/json\" --data '{\"fields\": {\"project\": {\"id\": \"${projectId}\"}, \"summary\": \"${summary}\", \"description\": \"${description}\", \"assignee\": {\"name\": \"${assigneeName}\"}, \"issuetype\": {\"id\": \"${issuetypeId}\"}}}' ${api-url}"))
(org-element-map
(org-element-parse-buffer)
'headline
(lambda (heading)
(push (org-element-property :raw-value heading) headings)))
(dolist (heading headings)
(let* ((template-values `(("api-url" . ,api-url)
("summary" . ,heading)
("description" . ".")
,@defaults))
(command (s-format jira-create-issue-template 'aget template-values)))
(push command commands)
(push (concat "# " heading) commands)))
(s-join "\n" commands))
#+END_SRC
I can then run C-c C-c inside this block to process the entire org buffer and the RESULTS
will contain all the curl commands I need to run wrapped in #+begin_SRC shell
.
#+RESULTS: generate-jira-create-issue-commands
#+begin_SRC shell
# Heading 1
curl --insecure -b ~/jira-session.txt --request POST --header "Content-Type: application/json" --data '{"fields": {"project": {"id": "12345"}, "summary": "Heading 1", "description": ".", "assignee": {"name": "<your user id>"}, "issuetype": {"id": "3"}}}' https://jira.example.com/rest/api/2/issue
...
# Heading N
curl --insecure -b ~/jira-session.txt --request POST --header "Content-Type: application/json" --data '{"fields": {"project": {"id": "12345"}, "summary": "Heading N", "description": ".", "assignee": {"name": "<your user id>"}, "issuetype": {"id": "3"}}}' https://jira.example.com/rest/api/2/issue
#+end_SRC
I can then run C-c C-c inside the RESULTS
block to run the curl commands.