From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: [Feature Request] Add an dispatcher command (keybinding) for inserting dynamic blocks Date: Sun, 30 Dec 2018 10:27:13 +0100 Message-ID: <87a7kn1i1a.fsf@nicolasgoaziou.fr> References: <871s7lvn4n.fsf@gmail.com> <3332dc93-afb1-0854-6298-06e0939aff3a@free.fr> <87wop1zcwo.fsf@nicolasgoaziou.fr> <87k1k57xt7.fsf@gmail.com> <877eg1yxy6.fsf@nicolasgoaziou.fr> <874lb4sqnk.fsf@gmail.com> <87ftuoznas.fsf@gmail.com> <874lazb5gb.fsf@nicolasgoaziou.fr> <877efvkn4d.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([208.118.235.92]:53050) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gdXNQ-0005Rx-Ru for emacs-orgmode@gnu.org; Sun, 30 Dec 2018 04:27:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gdXNN-0001ct-NX for emacs-orgmode@gnu.org; Sun, 30 Dec 2018 04:27:20 -0500 Received: from relay12.mail.gandi.net ([217.70.178.232]:39695) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gdXNN-0001YN-Eg for emacs-orgmode@gnu.org; Sun, 30 Dec 2018 04:27:17 -0500 In-Reply-To: <877efvkn4d.fsf@gmail.com> (stardiviner's message of "Thu, 27 Dec 2018 23:25:38 +0800") 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: stardiviner Cc: Thierry Banel , emacs-orgmode@gnu.org Hello, stardiviner writes: > 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. We are not sure this would end up as a good idea anyway. Meanwhile, I think the alist of plists idea is a bit complicated. > 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. Org uses a lot of data types in its defcustoms. I don't think there is more comfort in sticking to a particular one. OTOH, a simpler structure means simpler code. So if we have no other property than :function, I'd rather have a simple alist (KEY . FUNCTION). If we ever need more properties, we can change the structure, as long as it is in master, it is not set in stone. > About test, would you allow me to write it later? No problem. They do not need to be complex, tho. > At last, present my updated patch. Merry Christmas. Thank you. > -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]]). by a user function. #+kindex: C-c C-x x #+findex: org-dynamic-block-insert-dblock You can insert a dynamic block with ~org-dynamic-block-insert-dblock~, which is bound to {{{kbd(C-c C-x i)}}} by default. For example, {{{kbd(C-c C-x i c l o c k t a b l e RET)}}} inserts a table that updates the work time (see [[*Clocking Work Time]]). > +(org-dynamic-block-set-parameters > + "clocktable" > + :function 'org-clock-report) The function could be, e.g., (org-dynamic-block-define "clocktable" #'org-clock-report) > +(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)) Simply (cdr (assoc type org-dynamic-block-parameters)) if you simplify the structure. > +(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)))) Ditto. It could be (defun org-dynamic-block-define (type fun) (push (cons type fun) 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)) Is this function necessary? > +(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)))) (mapcar #'car ...) -> (org-dynamic-block-types) > + (let ((func (org-dynamic-block-get-parameter dblock-type :function))) See above. WDYT? Regards, -- Nicolas Goaziou