emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: stardiviner <numbchild@gmail.com>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: Thierry Banel <tbanelwebmin@free.fr>, emacs-orgmode@gnu.org
Subject: Re: [Feature Request] Add an dispatcher command (keybinding) for inserting dynamic blocks
Date: Thu, 27 Dec 2018 23:25:38 +0800	[thread overview]
Message-ID: <877efvkn4d.fsf@gmail.com> (raw)
In-Reply-To: <874lazb5gb.fsf@nicolasgoaziou.fr>

[-- Attachment #1: Type: text/plain, Size: 3322 bytes --]


Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> Hello,
>
> stardiviner <numbchild@gmail.com> writes:
>
>> The re-generated patch is in attachment.
>
> Thank you!

:)

>
>> ---
>>  doc/org-manual.org  |  9 ++++++---
>>  etc/ORG-NEWS        | 14 ++++++++++++++
>>  lisp/org-clock.el   |  5 +++++
>>  lisp/org-colview.el |  6 +++++-
>>  lisp/org-keys.el    |  6 ++----
>>  lisp/org.el         | 46 +++++++++++++++++++++++++++++++++++++++++++++
>
> You forgot to mark removed functions, e.g., `org-clock-report', as
> obsolete in "org-compat.el".
>
>>  Org supports /dynamic blocks/ in Org documents.  They are inserted
>>  with begin and end markers like any other code block, but the contents
>> -are updated automatically by a user function.  For example, {{{kbd(C-c
>> -C-x C-r)}}} inserts a dynamic table that updates the work time (see
>> -[[*Clocking Work Time]]).
>> +are updated automatically by a user function.  You can use dispatch
>> +command ~org-dynamic-block-insert-dispatch~, which is bind to
>> +keybinding {{{kbd(C-c C-x i)}}} by default.
>
> which is bound to {{{kbd(C-c C-x i)}}} by default.
>
> Don't forget the #+kindex and #+findex entries above.

Done, I have not really looked the "doc/org-manual.org" seriously. So
have not realized need to add #+kindex and #findex. Added now.

>
>> +(defcustom org-dynamic-block-parameters nil
>> +  "An alist of properties that defines all the Org dynamic blocks."
>
> What is the key of the alist? The possible keywords for the plist? This
> should appear in the docstring.

I improved the docstring. It should be clear now.

>
> So far, only :function is supported. If there is no plan for extending
> that, maybe a plain alist, e.g., ("type" . function) should suffice?
> I.e., isn't mimicking `org-link-set-parameters' a bit too much? WDYT?
>

Indeed, I mimicked `org-link-set-parameters' almost all. About this, I
originally just lazy, now I have a thought about this alist of plist
design, I think adding snippet-like text template string. This idea is
still coarse. I will update this idea in my org agenda todo list.

And I have another reason, I think use same structure can make user feel
comfortable, because it is same as `org-link-set-parameters`. It's a
good reason. If you this is acceptable, I think I would like to keep
this.

>> +(defun org-dynamic-block-insert-dblock (dblock-type)
>> +  "Select and insert an Org type dynamic block DBLOCK-TYPE.
>
> Insert a dynamic block of type DBLOCK-TYPE.

Modified.

>
>> +This is a dispatching function which prompts for the type of
>> +dynamic block to insert."
>
> When used interactively, select the dynamic block types among defined
> types, per `org-dynamic-block-set-parameters'.

Dito

>
> Also, once we agree on the design, would you mind writing some tests?

About test, would you allow me to write it later? I remember I watched
the org clock dynamic block test once, the literal string is complex,
and I remember I used to wrote a test about similar thing. Make me feel
hard. Give me a little time. I will add it. I promise. Can you merge
this at first? (Keep this commit behind, to merge all new commits, might
need to solve conflicts, Huu.....) WDYT?

>
> Regards,

:) I found you're kind than I think. If I'm wrong, don't tell me.

At last, present my updated patch. Merry Christmas.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-org.el-org-dynamic-block-insert-dblock-New-func.patch --]
[-- Type: text/x-patch, Size: 10090 bytes --]

From e3397a665261bb2c9f9396b16e0100132a3275d5 Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
Date: Sun, 23 Dec 2018 13:35:38 +0800
Subject: [PATCH] * lisp/org.el (org-dynamic-block-insert-dblock): New
 function.

  (org-dynamic-block-parameters, org-dynamic-block-functions,
  org-dynamic-block-types, org-dynamic-block-set-parameters,
  org-dynamic-block-get-parameter): New variables, New functions.

* lisp/org-keys.el: (org-dynamic-block-insert-dblock) New
  functions. (org-clock-report, org-columns-insert-dblock) Remove
  functions.

* doc/org-manual.org: Add manual for dispatch command
  ~org-dynamic-block-insert-dblock~.
---
 doc/org-manual.org  | 13 +++++++++---
 etc/ORG-NEWS        | 14 +++++++++++++
 lisp/org-clock.el   |  5 +++++
 lisp/org-colview.el |  6 +++++-
 lisp/org-keys.el    |  6 ++----
 lisp/org.el         | 50 +++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 86 insertions(+), 8 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index aad190b3b..817b0ac25 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -19926,9 +19926,16 @@ users mailing list, at mailto:emacs-orgmode@gnu.org.
 
 Org supports /dynamic blocks/ in Org documents.  They are inserted
 with begin and end markers like any other code block, but the contents
-are updated automatically by a user function.  For example, {{{kbd(C-c
-C-x C-r)}}} inserts a dynamic table that updates the work time (see
-[[*Clocking Work Time]]).
+are updated automatically by a user function.  You can use dispatch
+command ~org-dynamic-block-insert-dblock~, which is bound to
+keybinding {{{kbd(C-c C-x i)}}} by default.
+
+#+kindex: C-c C-x i
+#+findex: org-dynamic-block-insert-dblock
+Select one type of dynamic block to insert.
+
+For example, {{{kbd(C-c C-x i c l o c k t a b l e RET)}}} inserts a
+dynamic table that updates the work time (see [[*Clocking Work Time]]).
 
 Dynamic blocks can have names and function parameters.  The syntax is
 similar to source code block specifications:
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index f9bea4b56..16e8884f5 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -40,6 +40,14 @@ arguments no longer imply a "file" result is expected.
 See [[git:3367ac9457]] for details.
 
 ** New features
+*** Add a dispatcher command to insert dynamic blocks
+
+You can add dynamic block into ~org-dynamic-block-parameters~ with
+function ~org-dynamic-block-set-parameters~ just like
+~org-link-set-parameters~. All dynamic blocks in
+~org-dynamic-block-set-parameters~ can be used by
+~org-dynamic-block-insert-dblock~ command.
+
 *** Babel
 **** Add LaTeX output support in PlantUML
 *** New property =HTML_HEADLINE_CLASS= in HTML export
@@ -79,6 +87,12 @@ system than the main Org document.  For example:
 the corresponding direction by swapping with the adjacent cell.
 
 ** New functions
+*** ~org-dynamic-block-insert-dblock~
+
+Use default keybinding =<C-c C-x i>= to run command
+~org-dynamic-block-insert-dblock~.  It will prompt user to select
+dynamic block in ~org-dynamic-block-parameters~.
+
 *** ~org-table-cell-up~
 *** ~org-table-cell-down~
 *** ~org-table-cell-left~
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 1ebfa0201..531a76d63 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -36,6 +36,7 @@
 (declare-function org-element-property "org-element" (property element))
 (declare-function org-element-type "org-element" (element))
 (declare-function org-table-goto-line "org-table" (n))
+(declare-function org-dynamic-block-set-parameters "org" (type &rest rest))
 
 (defvar org-frame-title-format-backup frame-title-format)
 (defvar org-time-stamp-formats)
@@ -2052,6 +2053,10 @@ in the buffer and update it."
     (start (goto-char start)))
   (org-update-dblock))
 
+(org-dynamic-block-set-parameters
+ "clocktable"
+ :function 'org-clock-report)
+
 (defun org-day-of-week (day month year)
   "Returns the day of the week as an integer."
   (nth 6
diff --git a/lisp/org-colview.el b/lisp/org-colview.el
index 507c58a6a..f898c9695 100644
--- a/lisp/org-colview.el
+++ b/lisp/org-colview.el
@@ -41,6 +41,7 @@
 (declare-function org-element-property "org-element" (property element))
 (declare-function org-element-restriction "org-element" (element))
 (declare-function org-element-type "org-element" (element))
+(declare-function org-dynamic-block-set-parameters "org" (type &rest rest))
 
 (defvar org-agenda-columns-add-appointments-to-effort-sum)
 (defvar org-agenda-columns-compute-summary-properties)
@@ -1237,7 +1238,7 @@ When PRINTF is non-nil, use it to format the result."
   "Summarize CHECK-BOXES with a check-box cookie."
   (format "[%d/%d]"
 	  (cl-count-if (lambda (b) (or (equal b "[X]")
-				  (string-match-p "\\[\\([1-9]\\)/\\1\\]" b)))
+				   (string-match-p "\\[\\([1-9]\\)/\\1\\]" b)))
 		       check-boxes)
 	  (length check-boxes)))
 
@@ -1537,6 +1538,9 @@ PARAMS is a property list of parameters:
 		     (id)))))
   (org-update-dblock))
 
+(org-dynamic-block-set-parameters
+ "columnview"
+ :function 'org-columns-insert-dblock)
 
 \f
 ;;; Column view in the agenda
diff --git a/lisp/org-keys.el b/lisp/org-keys.el
index bed2f2ad4..5d12ce274 100644
--- a/lisp/org-keys.el
+++ b/lisp/org-keys.el
@@ -49,10 +49,8 @@
 (declare-function org-clock-in "org" (&optional select start-time))
 (declare-function org-clock-in-last "org" (&optional arg))
 (declare-function org-clock-out "org" (&optional switch-to-state fail-quietly at-time))
-(declare-function org-clock-report "org" (&optional arg))
 (declare-function org-clone-subtree-with-time-shift "org" (n &optional shift))
 (declare-function org-columns "org" (&optional global columns-fmt-string))
-(declare-function org-columns-insert-dblock "org" ())
 (declare-function org-comment-dwim "org" (arg))
 (declare-function org-copy "org" ())
 (declare-function org-copy-special "org" ())
@@ -67,6 +65,7 @@
 (declare-function org-cycle "org" (&optional arg))
 (declare-function org-cycle-agenda-files "org" ())
 (declare-function org-date-from-calendar "org" ())
+(declare-function org-dynamic-block-insert-dblock "org" (&optional arg))
 (declare-function org-dblock-update "org" (&optional arg))
 (declare-function org-deadline "org" (arg1 &optional time))
 (declare-function org-decrease-number-at-point "org" (&optional inc))
@@ -638,7 +637,7 @@ COMMANDS is a list of alternating OLDDEF NEWDEF command names."
 (org-defkey org-mode-map (kbd "C-c C-x C-j") #'org-clock-goto)
 (org-defkey org-mode-map (kbd "C-c C-x C-q") #'org-clock-cancel)
 (org-defkey org-mode-map (kbd "C-c C-x C-d") #'org-clock-display)
-(org-defkey org-mode-map (kbd "C-c C-x C-r") #'org-clock-report)
+(org-defkey org-mode-map (kbd "C-c C-x i") #'org-dynamic-block-insert-dblock)
 (org-defkey org-mode-map (kbd "C-c C-x C-u") #'org-dblock-update)
 (org-defkey org-mode-map (kbd "C-c C-x C-l") #'org-toggle-latex-fragment)
 (org-defkey org-mode-map (kbd "C-c C-x C-v") #'org-toggle-inline-images)
@@ -650,7 +649,6 @@ COMMANDS is a list of alternating OLDDEF NEWDEF command names."
 (org-defkey org-mode-map (kbd "C-c C-x e") #'org-set-effort)
 (org-defkey org-mode-map (kbd "C-c C-x E") #'org-inc-effort)
 (org-defkey org-mode-map (kbd "C-c C-x o") #'org-toggle-ordered-property)
-(org-defkey org-mode-map (kbd "C-c C-x i") #'org-columns-insert-dblock)
 (org-defkey org-mode-map (kbd "C-c C-,") #'org-insert-structure-template)
 (org-defkey org-mode-map (kbd "C-c C-x .") #'org-timer)
 (org-defkey org-mode-map (kbd "C-c C-x -") #'org-timer-item)
diff --git a/lisp/org.el b/lisp/org.el
index 1ee341f43..f87b898d1 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -11521,6 +11521,56 @@ If COMMAND is not given, use `org-update-dblock'."
 	(unless (re-search-forward org-dblock-end-re nil t)
 	  (error "Dynamic block not terminated"))))))
 
+(defcustom org-dynamic-block-parameters nil
+  "An alist of properties that defines all the Org dynamic blocks.
+The key of alist is a string of the dynamic block type name.
+The value of alist is a property list.
+The property list is constructured by key `:function' and a function name. "
+  :type '(alist :tag "dynamic block name"
+		:key-type string
+                :value-type plist)
+  :safe #'listp
+  :group 'org-block
+  :package-version '(Org . "9.3"))
+
+(defun org-dynamic-block-get-parameter (type key)
+  "Get TYPE dynamic block property for KEY.
+TYPE is a string and KEY is a plist keyword."
+  (plist-get
+   (cdr (assoc type org-dynamic-block-parameters))
+   key))
+
+(defun org-dynamic-block-set-parameters (type &rest parameters)
+  "Set dynamic block TYPE properties to PARAMETERS.
+PARAMETERS should be :key val pairs.
+The key is usually is `:function', and the value is a function name symbol."
+  (let ((data (assoc type org-dynamic-block-parameters)))
+    (if data (setcdr data (org-combine-plists (cdr data) parameters))
+      (push (cons type parameters) org-dynamic-block-parameters))))
+
+(defun org-dynamic-block-types ()
+  "Return a list of known dynamic block types."
+  (mapcar #'car org-dynamic-block-parameters))
+
+(defun org-dynamic-block-functions ()
+  "Return a list of functions that are called to insert dynamic block."
+  (cl-loop for dblock in org-dynamic-block-parameters
+           with insert-func
+           do (setq insert-func (org-dynamic-block-get-parameter (car dblock) :function))
+           if insert-func
+           collect insert-func))
+
+(defun org-dynamic-block-insert-dblock (dblock-type)
+  "Insert a dynamic block of type DBLOCK-TYPE.
+When used interactively, select the dynamic block types among
+defined types, per `org-dynamic-block-set-parameters'."
+  (interactive (list (completing-read "dynamic block: "
+				      (mapcar #'car org-dynamic-block-parameters))))
+  (let ((func (org-dynamic-block-get-parameter dblock-type :function)))
+    (if (functionp func)
+        (funcall func)
+      (message "No such dynamic block: %s" func))))
+
 (defun org-dblock-update (&optional arg)
   "User command for updating dynamic blocks.
 Update the dynamic block at point.  With prefix ARG, update all dynamic
-- 
2.20.1


[-- Attachment #3: Type: text/plain, Size: 247 bytes --]



--
[ stardiviner ]
       I try to make every word tell the meaning what I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3

  reply	other threads:[~2018-12-27 23:59 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-16  1:15 [Feature Request] Add an dispatcher command (keybinding) for inserting dynamic blocks stardiviner
2018-11-16 17:59 ` Thierry Banel
2018-11-25 10:09   ` Nicolas Goaziou
2018-12-03  6:43     ` stardiviner
2018-12-20  2:10     ` stardiviner
2018-12-22 22:54       ` Nicolas Goaziou
2018-12-23  6:26         ` stardiviner
2018-12-23  6:28         ` stardiviner
2018-12-23  7:59           ` stardiviner
2018-12-27 10:59             ` Nicolas Goaziou
2018-12-27 15:25               ` stardiviner [this message]
2018-12-30  9:27                 ` Nicolas Goaziou
2019-01-02  0:12                   ` stardiviner
2019-01-02 14:57                     ` Nicolas Goaziou
2019-01-03 12:19                       ` stardiviner

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=877efvkn4d.fsf@gmail.com \
    --to=numbchild@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    --cc=tbanelwebmin@free.fr \
    /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).