* Hooking document specific src blocks into default actions
@ 2018-02-26 12:28 Alex Bennée
2018-02-26 17:44 ` Alex Bennée
0 siblings, 1 reply; 2+ messages in thread
From: Alex Bennée @ 2018-02-26 12:28 UTC (permalink / raw)
To: emacs-orgmode
Hi,
I've been using these for a while but I recently wanted to add a
function at the head of the ctrl-c-ctrl-c processing:
(defvar my-org-default-action nil
"Default action for this document to run on `org-ctrl-c-ctrl-c'.
\\<org-mode-map>
This will run via `org-ctrl-c-ctrl-c-hook' and should return a
non-nil result if it processed something. As such it can override
default `org-mode' behaviour for \\[org-ctrl-c-ctrl-c]. If you
want something to run at the end then you need to use
`my-org-default-code-block'")
(make-variable-buffer-local 'my-org-default-action)
(defun my-org--do-action (func-or-string)
"Evaluate a code block or call a function `FUNC-OR-STRING' from org-file."
(let ((current-point (point)))
(cond
((stringp func-or-string)
(save-excursion
(org-babel-goto-named-src-block func-or-string)
(org-babel-when-in-src-block
(org-babel-eval-wipe-error-buffer)
(org-babel-execute-src-block current-prefix-arg nil
'((:called-from . current-point))))))
((functionp func-or-string)
(funcall func-or-string))
(t (error "What to do with: %s" func-or-string)))))
(defun my-org-run-default-action ()
"Execute default action for this org file."
(interactive)
(when my-org-default-action
(my-org--do-action my-org-default-action)))
(add-to-list 'org-ctrl-c-ctrl-c-hook
'my-org-run-default-action)
However I've run into a small problem that when I execute the source
block I end up with 't every time as we successfully executed the block
even if it didn't end up doing anything at the time.
Is there a better way to invoke source blocks from the current
org-document than this? One where the eventual result can be returned
into the calling lisp so org-ctrl-c-ctrl-c-hook can move on if we didn't
do anything?
--
Alex Bennée
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Hooking document specific src blocks into default actions
2018-02-26 12:28 Hooking document specific src blocks into default actions Alex Bennée
@ 2018-02-26 17:44 ` Alex Bennée
0 siblings, 0 replies; 2+ messages in thread
From: Alex Bennée @ 2018-02-26 17:44 UTC (permalink / raw)
To: emacs-orgmode
Alex Bennée <alex.bennee@linaro.org> writes:
> Hi,
>
> I've been using these for a while but I recently wanted to add a
> function at the head of the ctrl-c-ctrl-c processing:
>
> (defvar my-org-default-action nil
> "Default action for this document to run on `org-ctrl-c-ctrl-c'.
> \\<org-mode-map>
> This will run via `org-ctrl-c-ctrl-c-hook' and should return a
> non-nil result if it processed something. As such it can override
> default `org-mode' behaviour for \\[org-ctrl-c-ctrl-c]. If you
> want something to run at the end then you need to use
> `my-org-default-code-block'")
> (make-variable-buffer-local 'my-org-default-action)
>
> (defun my-org--do-action (func-or-string)
> "Evaluate a code block or call a function `FUNC-OR-STRING' from org-file."
> (let ((current-point (point)))
> (cond
> ((stringp func-or-string)
> (save-excursion
> (org-babel-goto-named-src-block func-or-string)
> (org-babel-when-in-src-block
> (org-babel-eval-wipe-error-buffer)
> (org-babel-execute-src-block current-prefix-arg nil
> '((:called-from . current-point))))))
> ((functionp func-or-string)
> (funcall func-or-string))
> (t (error "What to do with: %s" func-or-string)))))
OK the problem here was org-babel-when-in-src-block wraps the result. So
now I have a simpler:
(defun my-org--do-action (func-or-string)
"Evaluate a code block or call a function `FUNC-OR-STRING' from org-file."
(let ((action-result))
(cond
((stringp func-or-string)
(save-excursion
(org-babel-goto-named-src-block func-or-string)
(when (memq (org-element-type (org-element-context))
'(inline-src-block src-block))
(org-babel-eval-wipe-error-buffer)
(setq action-result (org-babel-execute-src-block t)))))
((functionp func-or-string)
(setq action-result (funcall func-or-string)))
(t (error "What to do with: %s" func-or-string)))
(if action-result
(message "%s: %s" func-or-string action-result)
nil)))
This works well, so on to the next problem. I have in my org file:
# -*- my-org-default-action: "team-default-action" -*-
with:
#+name: team-default-action
#+begin_src emacs-lisp
(let ((called-point (car org-mark-ring))
(processed))
(save-excursion
(message "Checking at: %s" called-point)
(goto-char called-point)
(when (org-at-heading-p)
(let ((heading (nth 4 (org-heading-components)))
(virt-rx (rx "[virt:" (group (one-or-more digit)) "]")))
(when (string-match virt-rx heading)
(let ((ticket (match-string 1 heading)))
(message "found: %s with %s" heading ticket)
(org-sbe "update-ticket"
(virt-ticket ticket))
(setq processed (format "Updated ticket: %s/%s" heading ticket)))))))
(unless processed
(message "No default action"))
processed)
#+end_src
However as this block needs to call the various helper functions I'm
using the org-sbe macro. However I can't work out how to evaluate
"ticket" in a way to keep the macro happy. Obviously it is designed for
use in tables so I'm possibly abusing it beyond what it can do.
Any ideas?
--
Alex Bennée
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-02-26 17:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-26 12:28 Hooking document specific src blocks into default actions Alex Bennée
2018-02-26 17:44 ` Alex Bennée
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.