emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: emacs-orgmode@gnu.org
Subject: Hooking document specific src blocks into default actions
Date: Mon, 26 Feb 2018 12:28:16 +0000	[thread overview]
Message-ID: <87tvu46ilb.fsf@linaro.org> (raw)


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

             reply	other threads:[~2018-02-26 12:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-26 12:28 Alex Bennée [this message]
2018-02-26 17:44 ` Hooking document specific src blocks into default actions Alex Bennée

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tvu46ilb.fsf@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).