From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alex =?utf-8?Q?Benn=C3=A9e?= Subject: Re: Hooking document specific src blocks into default actions Date: Mon, 26 Feb 2018 17:44:43 +0000 Message-ID: <87sh9n7iic.fsf@linaro.org> References: <87tvu46ilb.fsf@linaro.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:57161) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqMpV-00063c-HA for emacs-orgmode@gnu.org; Mon, 26 Feb 2018 12:44:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqMpS-0006G9-UF for emacs-orgmode@gnu.org; Mon, 26 Feb 2018 12:44:49 -0500 Received: from mail-wr0-x22a.google.com ([2a00:1450:400c:c0c::22a]:35617) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eqMpS-0006EY-M0 for emacs-orgmode@gnu.org; Mon, 26 Feb 2018 12:44:46 -0500 Received: by mail-wr0-x22a.google.com with SMTP id l43so22156224wrc.2 for ; Mon, 26 Feb 2018 09:44:46 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id 55sm14009382wrw.87.2018.02.26.09.44.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Feb 2018 09:44:44 -0800 (PST) Received: from zen (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTPS id A487D3E025C for ; Mon, 26 Feb 2018 17:44:43 +0000 (GMT) In-reply-to: <87tvu46ilb.fsf@linaro.org> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org Alex Benn=C3=A9e 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'. > \\ > 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-f= ile." > (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-fil= e." (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 ticke= t))))))) (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=C3=A9e