Re: [O] Feature suggestion and code review request: org-babel-cycle-src-block-header

2018-03-04 Thread John Kitchin
I guess this is a feature of deleting a region with the point in it. This
code, for example, does not preserve point.

#+BEGIN_SRC emacs-lisp
"<>"
(save-excursion
  (let* ((p1 (point))
(p2 (re-search-backward (concat "<" ">")))
(content (buffer-substring-no-properties p1 p2)))
(delete-region p1 p2)
(insert content)))
#+END_SRC


John

---
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu


On Sun, Mar 4, 2018 at 4:21 PM, Thorsten Jolitz  wrote:

> John Kitchin  writes:
>
> > Thanks for the examples.
> >
> > There is an interesting issue, the following does not save-excursion!
> >
> > (save-excursion
> > (org-dp-rewire 'src-block t t ;cont ins
> > t ;aff
> > nil ;elem
> > :parameters ":results output"))
> >
> > The point gets moved. Do you know why that happens?
>
> Hmm ... org-dp-rewire is mostly fidling around with lists, but in the
> end it acts conditionally on the 'replace' parameter:
>
> ,
> | (if (and (marker-position beg)
> |(marker-position end))
> |   (cl-case replace
> | (append (save-excursion (goto-char end) (insert strg)))
> | (prepend (goto-char beg) (insert strg))
> | (t (if (not replace)
> |strg
> |  (delete-region beg end)
> |  (goto-char end)
> |  (set-marker beg nil)
> |  (set-marker paff nil)
> |  (set-marker end nil)
> |  (save-excursion (insert strg)
> |   (if replace (insert strg) strg
> `
>
> append or prepend result, return it as string, or replace the rewired
> element.
> I guess the is a save-excursion missing here ...
>
> > John
> >
> > ---
> > Professor John Kitchin
> > Doherty Hall A207F
> > Department of Chemical Engineering
> > Carnegie Mellon University
> > Pittsburgh, PA 15213
> > 412-268-7803
> > @johnkitchin
> > http://kitchingroup.cheme.cmu.edu
> >
> > On Sat, Mar 3, 2018 at 12:26 PM, Thorsten Jolitz 
> > wrote:
> >
> >  Thorsten Jolitz  writes:
> >
> >  PS
> >  One more to show that one can not only easily modify a certain
> >  org element, but that its just as easy to convert it to another type
> >  of
> >  org element.
> >
> >  Use this (call M-x tj/obch)
> >
> >  #+BEGIN_SRC emacs-lisp
> >  (defun tj/obch ()
> >  "docstring"
> >  (interactive)
> >  (org-dp-rewire 'example-block t t ;cont ins
> >  '(:caption (("val2" "key2") ("val2" "key2"))
> >  :attr_xyz ("val1" "val2")) ;aff
> >  nil ;elem
> >  :language "common-lisp"
> >  :switches '(lambda (old elem) old )
> >  :parameters 'tj/toggle-params
> >  :value '(lambda (old elem)
> >  (let ((old1
> >  (string-remove-suffix "\n" old)))
> >  (concat "(+ 3 " old1 " 17)\n")))
> >  :preserve-indent '(lambda (old elem) old ) ) )
> >  #+END_SRC
> >
> >  with point on this source block header
> >
> >  ,
> >  | * test
> >  |
> >  | #+NAME: test1
> >  | #+BEGIN_SRC emacs-lisp :tangle yes :results none
> >  | (+ 1 1)
> >  | #+END_SRC
> >  `
> >
> >  to get this
> >
> >  ,
> >  | #+NAME: test1
> >  | #+CAPTION[key2]: val2
> >  | #+CAPTION[key2]: val2
> >  | #+ATTR_XYZ: val2
> >  | #+ATTR_XYZ: val1
> >  | #+BEGIN_EXAMPLE
> >  | (+ 3 (+ 1 1) 17)
> >  | #+END_EXAMPLE
> >  `
> >
> >  > John Kitchin  writes:
> >  >
> >  > Hallo,
> >  >
> >  >> This is a neat idea.
> >  >
> >  > This is quite a nice use/show case for org-dp too.
> >  >
> >  > I did not really try to solve the users feature request, just
> >  wanted to
> >  > demonstrate how different a possible solution looks using
> >  declarative
> >  > programming, leaving all the low-level parsing and interpreting
> >  work to
> >  > the org-element framework.
> >  >
> >  > 1. Example org-mode buffer
> >  >
> >  > ,
> >  > | * test
> >  > |
> >  > | #+NAME: test1
> >  > | #+BEGIN_SRC emacs-lisp :tangle yes :results none
> >  > | (+ 1 1)
> >  > | #+END_SRC
> >  > |
> >  > | #+NAME: test2
> >  > | #+BEGIN_SRC picolisp :tangle no :results raw
> >  > | (+ 2 2)
> >  > | #+END_SRC
> >  > `
> >  >
> >  > 2. Elisp to toggle the parameter values
> >  >
> >  > The org-dp part is this.
> >  >
> >  > Call the mapping cmd (M-x tj/obch-map) in the buffer (act on all
> >  > src-blocks), or put point on a src-block header and call M-x
> >  tj/obch to
> >  > just act on that scr-block.
> >  >
> >  > ,
> >  > | (defun tj/obch ()
> >  > | "docstring"
> >  > | (interactive)
> >  > | (org-dp-rewire 'src-block t t ;cont ins
> >  > | t ;aff
> >  > | nil ;elem
> >  > | :language '(lambda (old elem) old )
> >  > | :switches '(lambda (old elem) old )
> >  > | :parameters 'tj/toggle-params
> >  > | :value '(lambda (old elem) old )
> >  > | :preserve-indent '(lambda (old elem) old ) ) )
> >  > |
> >  > |
> >  > | (defun 

Re: [O] Feature suggestion and code review request: org-babel-cycle-src-block-header

2018-03-04 Thread Thorsten Jolitz
John Kitchin  writes:

> Thanks for the examples.
>
> There is an interesting issue, the following does not save-excursion!
>
> (save-excursion
> (org-dp-rewire 'src-block t t ;cont ins
> t ;aff
> nil ;elem
> :parameters ":results output"))
>
> The point gets moved. Do you know why that happens?

Hmm ... org-dp-rewire is mostly fidling around with lists, but in the
end it acts conditionally on the 'replace' parameter:

,
| (if (and (marker-position beg)
|(marker-position end))
|   (cl-case replace
| (append (save-excursion (goto-char end) (insert strg)))
| (prepend (goto-char beg) (insert strg))
| (t (if (not replace)
|strg
|  (delete-region beg end)
|  (goto-char end)
|  (set-marker beg nil)
|  (set-marker paff nil)
|  (set-marker end nil)
|  (save-excursion (insert strg)
|   (if replace (insert strg) strg
`

append or prepend result, return it as string, or replace the rewired
element.
I guess the is a save-excursion missing here ...

> John
>
> ---
> Professor John Kitchin 
> Doherty Hall A207F
> Department of Chemical Engineering
> Carnegie Mellon University
> Pittsburgh, PA 15213
> 412-268-7803
> @johnkitchin
> http://kitchingroup.cheme.cmu.edu
>
> On Sat, Mar 3, 2018 at 12:26 PM, Thorsten Jolitz 
> wrote:
>
>  Thorsten Jolitz  writes:
>
>  PS
>  One more to show that one can not only easily modify a certain
>  org element, but that its just as easy to convert it to another type
>  of
>  org element.
>
>  Use this (call M-x tj/obch)
>
>  #+BEGIN_SRC emacs-lisp
>  (defun tj/obch ()
>  "docstring"
>  (interactive)
>  (org-dp-rewire 'example-block t t ;cont ins
>  '(:caption (("val2" "key2") ("val2" "key2"))
>  :attr_xyz ("val1" "val2")) ;aff
>  nil ;elem
>  :language "common-lisp"
>  :switches '(lambda (old elem) old )
>  :parameters 'tj/toggle-params
>  :value '(lambda (old elem)
>  (let ((old1
>  (string-remove-suffix "\n" old)))
>  (concat "(+ 3 " old1 " 17)\n")))
>  :preserve-indent '(lambda (old elem) old ) ) )
>  #+END_SRC
>
>  with point on this source block header
>
>  ,
>  | * test
>  |
>  | #+NAME: test1
>  | #+BEGIN_SRC emacs-lisp :tangle yes :results none
>  | (+ 1 1)
>  | #+END_SRC
>  `
>
>  to get this
>
>  ,
>  | #+NAME: test1
>  | #+CAPTION[key2]: val2
>  | #+CAPTION[key2]: val2
>  | #+ATTR_XYZ: val2
>  | #+ATTR_XYZ: val1
>  | #+BEGIN_EXAMPLE
>  | (+ 3 (+ 1 1) 17)
>  | #+END_EXAMPLE
>  `
>
>  > John Kitchin  writes:
>  >
>  > Hallo,
>  >
>  >> This is a neat idea.
>  >
>  > This is quite a nice use/show case for org-dp too.
>  >
>  > I did not really try to solve the users feature request, just
>  wanted to
>  > demonstrate how different a possible solution looks using
>  declarative
>  > programming, leaving all the low-level parsing and interpreting
>  work to
>  > the org-element framework.
>  >
>  > 1. Example org-mode buffer
>  >
>  > ,
>  > | * test
>  > |
>  > | #+NAME: test1
>  > | #+BEGIN_SRC emacs-lisp :tangle yes :results none
>  > | (+ 1 1)
>  > | #+END_SRC
>  > |
>  > | #+NAME: test2
>  > | #+BEGIN_SRC picolisp :tangle no :results raw
>  > | (+ 2 2)
>  > | #+END_SRC
>  > `
>  >
>  > 2. Elisp to toggle the parameter values
>  >
>  > The org-dp part is this.
>  >
>  > Call the mapping cmd (M-x tj/obch-map) in the buffer (act on all
>  > src-blocks), or put point on a src-block header and call M-x
>  tj/obch to
>  > just act on that scr-block.
>  >
>  > ,
>  > | (defun tj/obch ()
>  > | "docstring"
>  > | (interactive)
>  > | (org-dp-rewire 'src-block t t ;cont ins
>  > | t ;aff
>  > | nil ;elem
>  > | :language '(lambda (old elem) old )
>  > | :switches '(lambda (old elem) old )
>  > | :parameters 'tj/toggle-params
>  > | :value '(lambda (old elem) old )
>  > | :preserve-indent '(lambda (old elem) old ) ) )
>  > |
>  > |
>  > | (defun tj/obch-map ()
>  > | "docstring"
>  > | (interactive)
>  > | (org-dp-map '(tj/obch) "#\\+BEGIN_SRC"))
>  > `
>  >
>  > You can play around with the other args to org-dp-rewire (apart
>  from
>  > :parameters) to find out how easy you can change (or remove/add)
>  other
>  > parts of the src-block without any work on the textual
>  representation.
>  >
>  > E.g. try this:
>  >
>  > #+BEGIN_SRC emacs-lisp
>  > (defun tj/obch ()
>  > "docstring"
>  > (interactive)
>  > (org-dp-rewire 'src-block t t ;cont ins
>  > nil ;aff
>  > nil ;elem
>  > :language "common-lisp"
>  > :switches '(lambda (old elem) old )
>  > :parameters 'tj/toggle-params
>  > :value '(lambda (old elem)
>  > (let ((old1
>  > (string-remove-suffix "\n" old)))
>  > (concat "(+ 3 " old1 " 17)\n")))
>  > :preserve-indent '(lambda (old elem) old ) ) )
>  > #+END_SRC
>  >
>  >
>  > to see this result in the example buffer after calling M-x
>  tj/obch-map:
>  >
>  > ,
>  > | * test
>  

Re: [O] Feature suggestion and code review request: org-babel-cycle-src-block-header

2018-03-04 Thread John Kitchin
Thanks for the examples.

There is an interesting issue, the following does not save-excursion!

(save-excursion
  (org-dp-rewire 'src-block t t ;cont ins
 t ;aff
 nil ;elem
 :parameters ":results output"))

The point gets moved. Do you know why that happens?

John

---
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu


On Sat, Mar 3, 2018 at 12:26 PM, Thorsten Jolitz  wrote:

> Thorsten Jolitz  writes:
>
> PS
> One more to show that one can not only easily modify a certain
> org element, but that its just as easy to convert it to another type of
> org element.
>
> Use this (call M-x tj/obch)
>
> #+BEGIN_SRC emacs-lisp
> (defun tj/obch ()
>   "docstring"
>   (interactive)
>   (org-dp-rewire 'example-block t t ;cont ins
>  '(:caption (("val2" "key2") ("val2" "key2"))
> :attr_xyz ("val1" "val2")) ;aff
>  nil ;elem
>  :language "common-lisp"
>  :switches '(lambda (old elem) old )
>  :parameters 'tj/toggle-params
>  :value '(lambda (old elem)
>(let ((old1
>   (string-remove-suffix "\n" old)))
>  (concat "(+ 3 " old1 " 17)\n")))
>  :preserve-indent '(lambda (old elem) old ) ) )
> #+END_SRC
>
> with point on this source block header
>
> ,
> | * test
> |
> | #+NAME: test1
> | #+BEGIN_SRC emacs-lisp :tangle yes :results none
> |   (+ 1 1)
> | #+END_SRC
> `
>
> to get this
>
> ,
> | #+NAME: test1
> | #+CAPTION[key2]: val2
> | #+CAPTION[key2]: val2
> | #+ATTR_XYZ: val2
> | #+ATTR_XYZ: val1
> | #+BEGIN_EXAMPLE
> | (+ 3 (+ 1 1) 17)
> | #+END_EXAMPLE
> `
>
>
>
>
> > John Kitchin  writes:
> >
> > Hallo,
> >
> >> This is a neat idea.
> >
> > This is quite a nice use/show case for org-dp too.
> >
> > I did not really try to solve the users feature request, just wanted to
> > demonstrate how different a possible solution looks using declarative
> > programming, leaving all the low-level parsing and interpreting work to
> > the org-element framework.
> >
> > 1. Example org-mode buffer
> >
> > ,
> > | * test
> > |
> > | #+NAME: test1
> > | #+BEGIN_SRC emacs-lisp :tangle yes :results none
> > |   (+ 1 1)
> > | #+END_SRC
> > |
> > | #+NAME: test2
> > | #+BEGIN_SRC picolisp :tangle no :results raw
> > |   (+ 2 2)
> > | #+END_SRC
> > `
> >
> > 2. Elisp to toggle the parameter values
> >
> > The org-dp part is this.
> >
> > Call the mapping cmd (M-x tj/obch-map) in the buffer (act on all
> > src-blocks), or put point on a src-block header and call M-x tj/obch to
> > just act on that scr-block.
> >
> > ,
> > | (defun tj/obch ()
> > |   "docstring"
> > |   (interactive)
> > |   (org-dp-rewire 'src-block t t ;cont ins
> > |  t ;aff
> > |  nil ;elem
> > |  :language '(lambda (old elem) old )
> > |  :switches '(lambda (old elem) old )
> > |  :parameters 'tj/toggle-params
> > |  :value '(lambda (old elem) old )
> > |  :preserve-indent '(lambda (old elem) old ) ) )
> > |
> > |
> > | (defun tj/obch-map ()
> > |   "docstring"
> > |   (interactive)
> > |   (org-dp-map '(tj/obch) "#\\+BEGIN_SRC"))
> > `
> >
> > You can play around with the other args to org-dp-rewire (apart from
> > :parameters) to find out how easy you can change (or remove/add) other
> > parts of the src-block without any work on the textual representation.
> >
> > E.g. try this:
> >
> > #+BEGIN_SRC emacs-lisp
> > (defun tj/obch ()
> >   "docstring"
> >   (interactive)
> >   (org-dp-rewire 'src-block t t ;cont ins
> >nil ;aff
> >nil ;elem
> >:language "common-lisp"
> >:switches '(lambda (old elem) old )
> >:parameters 'tj/toggle-params
> >:value '(lambda (old elem)
> >  (let ((old1
> > (string-remove-suffix "\n" old)))
> >  (concat "(+ 3 " old1 " 17)\n")))
> >:preserve-indent '(lambda (old elem) old ) ) )
> > #+END_SRC
> >
> >
> > to see this result in the example buffer after calling M-x tj/obch-map:
> >
> > ,
> > | * test
> > |
> > | #+BEGIN_SRC common-lisp :tangle no :results raw
> > |   (+ 3 (+ 1 1) 17)
> > | #+END_SRC
> > |
> > | #+BEGIN_SRC common-lisp :tangle yes :results none
> > |   (+ 3 (+ 2 2) 17)
> > | #+END_SRC
> > `
> >
> > PS
> > Here is the whole code.
> > The logic in 'tj/toggle-params is not really of interest here. The
> > important thing is, that all of these options are possible:
> >
> > - simply assign a value
> > - implement a lambda function in place (with two args)
> > - 

Re: [O] Feature suggestion and code review request: org-babel-cycle-src-block-header

2018-03-03 Thread Thorsten Jolitz
Thorsten Jolitz  writes:

PS
One more to show that one can not only easily modify a certain
org element, but that its just as easy to convert it to another type of
org element.

Use this (call M-x tj/obch)

#+BEGIN_SRC emacs-lisp  
(defun tj/obch () 
  "docstring"
  (interactive)
  (org-dp-rewire 'example-block t t ;cont ins
 '(:caption (("val2" "key2") ("val2" "key2"))
:attr_xyz ("val1" "val2")) ;aff 
 nil ;elem 
 :language "common-lisp"
 :switches '(lambda (old elem) old )
 :parameters 'tj/toggle-params
 :value '(lambda (old elem)
   (let ((old1 
  (string-remove-suffix "\n" old)))
 (concat "(+ 3 " old1 " 17)\n")))
 :preserve-indent '(lambda (old elem) old ) ) )
#+END_SRC

with point on this source block header 

,
| * test
| 
| #+NAME: test1
| #+BEGIN_SRC emacs-lisp :tangle yes :results none
|   (+ 1 1)
| #+END_SRC
`

to get this

,
| #+NAME: test1
| #+CAPTION[key2]: val2
| #+CAPTION[key2]: val2
| #+ATTR_XYZ: val2
| #+ATTR_XYZ: val1
| #+BEGIN_EXAMPLE
| (+ 3 (+ 1 1) 17)
| #+END_EXAMPLE
`




> John Kitchin  writes:
>
> Hallo,
>
>> This is a neat idea. 
>
> This is quite a nice use/show case for org-dp too.
>
> I did not really try to solve the users feature request, just wanted to
> demonstrate how different a possible solution looks using declarative
> programming, leaving all the low-level parsing and interpreting work to
> the org-element framework.
>
> 1. Example org-mode buffer
>
> ,
> | * test
> | 
> | #+NAME: test1
> | #+BEGIN_SRC emacs-lisp :tangle yes :results none
> |   (+ 1 1)
> | #+END_SRC
> | 
> | #+NAME: test2
> | #+BEGIN_SRC picolisp :tangle no :results raw
> |   (+ 2 2)
> | #+END_SRC
> `
>
> 2. Elisp to toggle the parameter values
>
> The org-dp part is this. 
>
> Call the mapping cmd (M-x tj/obch-map) in the buffer (act on all
> src-blocks), or put point on a src-block header and call M-x tj/obch to
> just act on that scr-block.
>
> ,
> | (defun tj/obch () 
> |   "docstring"
> |   (interactive)
> |   (org-dp-rewire 'src-block t t ;cont ins
> |  t ;aff 
> |  nil ;elem 
> |  :language '(lambda (old elem) old )
> |  :switches '(lambda (old elem) old )
> |  :parameters 'tj/toggle-params
> |  :value '(lambda (old elem) old )
> |  :preserve-indent '(lambda (old elem) old ) ) )
> | 
> | 
> | (defun tj/obch-map ()
> |   "docstring"
> |   (interactive)
> |   (org-dp-map '(tj/obch) "#\\+BEGIN_SRC"))
> ` 
>
> You can play around with the other args to org-dp-rewire (apart from
> :parameters) to find out how easy you can change (or remove/add) other
> parts of the src-block without any work on the textual representation.
>
> E.g. try this:
>
> #+BEGIN_SRC emacs-lisp  
> (defun tj/obch () 
>   "docstring"
>   (interactive)
>   (org-dp-rewire 'src-block t t ;cont ins
>nil ;aff 
>nil ;elem 
>:language "common-lisp"
>:switches '(lambda (old elem) old )
>:parameters 'tj/toggle-params
>:value '(lambda (old elem)
>  (let ((old1 
> (string-remove-suffix "\n" old)))
>  (concat "(+ 3 " old1 " 17)\n")))
>:preserve-indent '(lambda (old elem) old ) ) )
> #+END_SRC
>
>
> to see this result in the example buffer after calling M-x tj/obch-map:
>
> ,
> | * test
> | 
> | #+BEGIN_SRC common-lisp :tangle no :results raw
> |   (+ 3 (+ 1 1) 17)
> | #+END_SRC
> | 
> | #+BEGIN_SRC common-lisp :tangle yes :results none
> |   (+ 3 (+ 2 2) 17)
> | #+END_SRC
> `
>
> PS
> Here is the whole code.
> The logic in 'tj/toggle-params is not really of interest here. The
> important thing is, that all of these options are possible:
>
> - simply assign a value
> - implement a lambda function in place (with two args)
> - implement a named function (with two args) and use its name
>
> ,
> | :parameters ":tangle no"
> | :parameters '(lambda (old elem) (concat old " :results none") )
> | :parameters 'tj/toggle-params
> `
>
> #+BEGIN_SRC emacs-lisp  
> (defvar tj/change-p)
>
> ;; org-dp in action
> ;; wrap org-dp-rewire in utility cmd for readability
> (defun tj/obch () 
>   "docstring"
>   (interactive)
>   (org-dp-rewire 'src-block t t ;cont ins
>t ;aff 
>nil ;elem 
>:language '(lambda (old elem) old )
>:switches '(lambda (old elem) old )
>:parameters 'tj/toggle-params
>:value '(lambda (old elem) old )
>:preserve-indent '(lambda (old elem) old ) ) )
>
>
> (defun tj/obch-map ()
>   "docstring"
>   (interactive)
>   (org-dp-map '(tj/obch) 

Re: [O] Feature suggestion and code review request: org-babel-cycle-src-block-header

2018-03-03 Thread Thorsten Jolitz
John Kitchin  writes:

Hallo,

> This is a neat idea. 

This is quite a nice use/show case for org-dp too.

I did not really try to solve the users feature request, just wanted to
demonstrate how different a possible solution looks using declarative
programming, leaving all the low-level parsing and interpreting work to
the org-element framework.

1. Example org-mode buffer

,
| * test
| 
| #+NAME: test1
| #+BEGIN_SRC emacs-lisp :tangle yes :results none
|   (+ 1 1)
| #+END_SRC
| 
| #+NAME: test2
| #+BEGIN_SRC picolisp :tangle no :results raw
|   (+ 2 2)
| #+END_SRC
`

2. Elisp to toggle the parameter values

The org-dp part is this. 

Call the mapping cmd (M-x tj/obch-map) in the buffer (act on all
src-blocks), or put point on a src-block header and call M-x tj/obch to
just act on that scr-block.

,
| (defun tj/obch () 
|   "docstring"
|   (interactive)
|   (org-dp-rewire 'src-block t t ;cont ins
|t ;aff 
|nil ;elem 
|:language '(lambda (old elem) old )
|:switches '(lambda (old elem) old )
|:parameters 'tj/toggle-params
|:value '(lambda (old elem) old )
|:preserve-indent '(lambda (old elem) old ) ) )
| 
| 
| (defun tj/obch-map ()
|   "docstring"
|   (interactive)
|   (org-dp-map '(tj/obch) "#\\+BEGIN_SRC"))
` 

You can play around with the other args to org-dp-rewire (apart from
:parameters) to find out how easy you can change (or remove/add) other
parts of the src-block without any work on the textual representation.

E.g. try this:

#+BEGIN_SRC emacs-lisp  
(defun tj/obch () 
  "docstring"
  (interactive)
  (org-dp-rewire 'src-block t t ;cont ins
 nil ;aff 
 nil ;elem 
 :language "common-lisp"
 :switches '(lambda (old elem) old )
 :parameters 'tj/toggle-params
 :value '(lambda (old elem)
   (let ((old1 
  (string-remove-suffix "\n" old)))
   (concat "(+ 3 " old1 " 17)\n")))
 :preserve-indent '(lambda (old elem) old ) ) )
#+END_SRC

to see this result in the example buffer after calling M-x tj/obch-map:

,
| * test
| 
| #+BEGIN_SRC common-lisp :tangle no :results raw
|   (+ 3 (+ 1 1) 17)
| #+END_SRC
| 
| #+BEGIN_SRC common-lisp :tangle yes :results none
|   (+ 3 (+ 2 2) 17)
| #+END_SRC
`

PS
Here is the whole code.
The logic in 'tj/toggle-params is not really of interest here. The
important thing is, that all of these options are possible:

- simply assign a value
- implement a lambda function in place (with two args)
- implement a named function (with two args) and use its name

,
| :parameters ":tangle no"
| :parameters '(lambda (old elem) (concat old " :results none") )
| :parameters 'tj/toggle-params
`


#+BEGIN_SRC emacs-lisp  
(defvar tj/change-p)

;; org-dp in action
;; wrap org-dp-rewire in utility cmd for readability
(defun tj/obch () 
  "docstring"
  (interactive)
  (org-dp-rewire 'src-block t t ;cont ins
 t ;aff 
 nil ;elem 
 :language '(lambda (old elem) old )
 :switches '(lambda (old elem) old )
 :parameters 'tj/toggle-params
 :value '(lambda (old elem) old )
 :preserve-indent '(lambda (old elem) old ) ) )


(defun tj/obch-map ()
  "docstring"
  (interactive)
  (org-dp-map '(tj/obch) "#\\+BEGIN_SRC"))

;; helper functions for this use case, not really of interest
;; toggle src-block parameter values
(defun tj/toggle-params (old elem)
  "docstring"
  (let* ((params-lst (split-string old)))
(setq tj/change-p nil)
(mapconcat 'tj/replace-vals params-lst " ")) )

;; helper functon to actually replace old with new values
(defun tj/replace-vals (strg)
  "docstring"
  (let (res)
(if tj/change-p
(progn
  (cond 
   ((string-equal strg "yes")
(setq res "no"))
   ((string-equal strg "no")
(setq res "yes"))
   ((string-equal strg "none")
(setq res "raw"))
   ((string-equal strg "raw")
(setq res "none")) )
  (setq tj/change-p nil)
  res)
  (cond
   ((string-equal strg ":tangle")
(setq tj/change-p t))
   ((string-equal strg ":results")
(setq tj/change-p t)))
  strg)))
#+END_SRC


> I sometimes want to switch to silent, or between
> value and results. I don't know if you would consider the code below an
> improvement, but it seems to do what you want, and is shorter. It has
> less checking of things, and is more of a replace the header kind of
> approach.
>
> Personally, I think strings are the way to go here.
>
> #+BEGIN_SRC emacs-lisp :tangle yes :results none
> (require 's)
> (require 'dash)
>
> (defvar header-sequences '((emacs-lisp . (":tangle no :results none" ;;
> type 2 above
> 

Re: [O] Feature suggestion and code review request: org-babel-cycle-src-block-header

2018-03-03 Thread Akater

Thank you, I'll make use of 's. Not well versed in Elisp
libraries. save-excursion is certainly an improvement, too.


signature.asc
Description: PGP signature


Re: [O] Feature suggestion and code review request: org-babel-cycle-src-block-header

2018-03-02 Thread John Kitchin
This is a neat idea. I sometimes want to switch to silent, or between value
and results. I don't know if you would consider the code below an
improvement, but it seems to do what you want, and is shorter. It has less
checking of things, and is more of a replace the header kind of approach.

Personally, I think strings are the way to go here.

#+BEGIN_SRC emacs-lisp :tangle yes :results none
(require 's)
(require 'dash)

(defvar header-sequences '((emacs-lisp . (":tangle no :results none";;
type 2 above
  ":tangle yes :results none"   ;; type 3 above
  ":results type verbatim"  ;; type 1 above
  

(defun obch ()
  (interactive)
  (let* ((lang (car (org-babel-get-src-block-info t)))
(headers (cdr (assoc (intern-soft lang) header-sequences)))
header index)
(save-excursion
  (org-babel-goto-src-block-head)
  (re-search-forward lang)
  (setq header (buffer-substring-no-properties (point)
(line-end-position))
index (-find-index (lambda (s) (string= (s-trim s) (s-trim header)))
headers))
  (delete-region (point) (line-end-position))
  (insert " " (if index
  (nth (mod (+ 1 index) (length headers)) headers)
(car headers))
#+END_SRC


John

---
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu


On Wed, Feb 28, 2018 at 2:59 AM, Akater  wrote:

> When I have a chance, I enjoy the following development workflow: the
> code is written in org files and is tangled into conventional source
> code files more or less regularly.
>
> I find that source blocks mostly fall into three categories, numbered
> here for further reference:
> - examples/test cases/desiderata, like
>   `(my-implemented-or-desired-function x y)' (type 1)
> - drafts, failed attempts at implementations and other snippets better
>   left as is, or as a warning (type 2)
> - working implementations, to be tangled (type 3)
>
> Hence I end up using only a handful of header argument strings. An
> example corresponding to this 3-cases setup is found below. So it would
> be nice to have a function that cycles between those, much like we can
> cycle through org TODO sequence now using a standard function, and set
> up this sequence per Org file.
>
> I'm fairly bad at Emacs Lisp, so I'm interested in feedback about my
> implementation of cycling function. It operates with strings, mostly
> because I failed to make it work with lists of alists of header
> arguments as ob-core.el suggests. On the other hand, given that Emacs
> Lisp is more string-oriented than it is object-oriented, it might not be
> a really bad idea.
>
> So what do you think? How can this implementation be improved? (Sans
> using rotate and tracking position in a smarter way.) Does it make sense
> to include this feature in Org mode? Maybe I missed some existing
> well-estabilished solutions? This is something akin to “literate
> programming”; I'm not a fan of this idea---at least the way it is
> usually presented---but it is somewhat popular a topic. I have some
> other feature in mind I'd love to see implemented in Org-Babel:
> convenient export of src blocks of type 1 (see above) into unit tests
> (as test cases) and into documentation sources (as examples) but this
> one is heavily target-language dependent and probably deserves its own
> thread.
>
> #+begin_src emacs-lisp
> (cl-defun next-maybe-cycled (elem list  (test #'equal))
>   "Returns the element in `list' next to the first `elem' found. If `elem'
> is found at `list''s very tail, returns `list''s car. `next-maybe-cycled'
> provides no way to distinguish between \"found nil\" and \"found nothing\"."
>   (let ((sublist (cl-member elem list :test test)))
> (and sublist
>  (if (cdr sublist)
>  (cadr sublist)
>(car list)
>
> (defun shrink-whitespace (string)
>   "Transforms all whitespace instances into single spaces. Trims
> whitespace at beginning and end. No argument type checking."
>   (cl-reduce (lambda (string rule)
>(replace-regexp-in-string (car rule) (cdr rule) string))
>  '(("[[:blank:]]+" . " ") ("^[[:blank:]]*" . "")
> ("[[:blank:]]*$" . ""))
>  :initial-value string))
>
> (defun string-equal-modulo-whitespace (x y)
>   (string-equal (shrink-whitespace x) (shrink-whitespace y)))
>
> (defun org-babel-cycle-src-block-header-string (header-strings)
>   "Cycle through given `header-strings' if currently in Org Babel source
> code block. If current src-block header is not found in `header-strings',
> switch header to the car of `header-strings'.
>
> `header-strings' must be a non-empty list of strings. All whitespace in
> them is shrinked.
>
> If UNDO-ed, cursor position is not guaranteed to be preserved."
>   (interactive)
>   (cond
>((not (and header-strings (listp header-strings)))
> (error "No Org Babel 

Re: [O] Feature suggestion

2012-01-02 Thread Bastien
Hi York,

York Zhao gtdplatf...@gmail.com writes:

 May I suggest a feature that when the point is inside a TeX source block and
 C-c C-c is pressed, instead of showing the error No org-babel-execute
 function for tex!, can we process the current tex block to output the dvi or
 pdf file?

I'd rather define a org-babel-execute:tex function for that. 

Maybe look at org-babel-execute:* functions and suggest one?

Thanks!

-- 
 Bastien



Re: [O] Feature suggestion

2012-01-02 Thread York Zhao
 York Zhao gtdplatf...@gmail.com writes:

 May I suggest a feature that when the point is inside a TeX source block and
 C-c C-c is pressed, instead of showing the error No org-babel-execute
 function for tex!, can we process the current tex block to output the dvi or
 pdf file?

 I'd rather define a org-babel-execute:tex function for that.

 Maybe look at org-babel-execute:* functions and suggest one?

 Thanks!

Hi Bastien,

I think this is a good idea. By `org-babel-execute:tex' do you mean
`org-babel-execute-tex'?


York



Re: [O] Feature suggestion

2012-01-02 Thread Bastien
Hi York,

York Zhao gtdplatf...@gmail.com writes:

 I think this is a good idea. By `org-babel-execute:tex' do you mean
 `org-babel-execute-tex'?

No, I mean org-babel-execute:tex.

There is a whole family of Babel functions that let C-c C-c act 
upon a source block, with respect to the langage used.

Do `C-h f org-babel-execute: TAB' to see the list of available
function for your configuration.

Then you can pick up a langage you are familiar with in this 
list, jump to the Emacs lisp function and start getting familiar
with the code to write your own org-babel-execute:tex function.

But wait, there is already org-babel-execute:latex, so I think 
part of the problem is already solved.

Good exploration,

-- 
 Bastien



Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-12-31 Thread Bastien
Hi Brian,

Brian van den Broek van...@gmail.com writes:

 I very much appreciate orgmode's ability to parse the content of a
 yank for date and time information while entering a timestamp. I think
 it would be even cooler were orgmode able to parse the content of a
 selected region and extract the same information, replacing the region
 with the timestamp created on the basis of the text in the region.

I'm not sure I understand: right now, when you select a timestamp and
run `C-c .' on it, the selected timestamp is the default prompt.  Is
this what you have in mind?  If not, can you give a clearer example?

Thanks!

-- 
 Bastien



Re: [O] Feature suggestion

2011-11-06 Thread Herbert Sitz
Markus Grebenstein post at mgrebenstein.de writes:
 
 since I used Scrivener (Windows Beta) quite a while I'd love to have 
 more of fletcher penny's multimarkdown (or MMD- like Syntax) integrated 
 in orgmode to make it more versatile. Sadly I am not a lisp programmer 
 at all...
 

Markus --

What exactly are the features you're thinking about?

For example, I read this at the MMD site, but virtually all of these
are already implemented in Org-mode:

MultiMarkdown adds these features to the basic Markdown syntax:

footnotes
tables
citations and bibliography (works best in LaTeX using BibTeX)
math support
automatic cross-referencing ability
smart typography, with support for multiple languages
image attributes
table and image captions
definition lists
glossary entries (LaTeX only)
document metadata (e.g. title, author, etc.)
--
http://fletcherpenney.net/multimarkdown/features/

Regards,

Herb





Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-26 Thread Eric S Fraga
Brian van den Broek brian.van.den.br...@gmail.com writes:

[...]

 Various people upthread convinced me that my feature request wasn't
 really worth it. (I do hope it didn't cost you too much time!) So, I
 am content to drop it here :-)

but did you try a simple keyboard macro in the end as suggested by the
first response?  this requires no elisp programming.  if your actions
consist of selecting region and then applying org-time-stamp on it, the
following keystrokes should record the steps necessary to do what you
want *after* you have selected the desired region:

C-x ( C-w C-c . C-y RET C-x )

(untested sequence of keystrokes)

Your macro is now recorded and can be executed by C-x e once you have
selected a region.  You can even save that macro (check the info pages
for emacs) and bind it to a key.

HTH,
eric

-- 
: Eric S Fraga (GnuPG: 0xC89193D8FFFCF67D) in Emacs 24.0.90.1
: using Org-mode version 7.7 (release_7.7.381.g05ea.dirty)



Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-26 Thread Brian van den Broek
On 26 Oct 2011 09:56, Eric S Fraga e.fr...@ucl.ac.uk wrote:

 Brian van den Broek brian.van.den.br...@gmail.com writes:

 [...]

  Various people upthread convinced me that my feature request wasn't
  really worth it. (I do hope it didn't cost you too much time!) So, I
  am content to drop it here :-)

 but did you try a simple keyboard macro in the end as suggested by the
 first response?

snip

Hi Eric,

I did. That that solution was pointed out helped me see that my suggestion
had little value. :--)

Thanks, though, for making the effort to give explicit details!

Best,

Brian vdB


Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-25 Thread Brian van den Broek
On 24 October 2011 08:00, Bastien b...@altern.org wrote:
 Hi Brian,

 suvayu ali fatkasuvayu+li...@gmail.com writes:

 Ah I see it now, you want the org-timestamp command to work on a
 region. Maybe you can write your own function with lisp if you are
 doing this too often. Should be quite simple to try.

 Please check `org-loop-over-headlines-in-active-region' from latest
 git repo and see if using `org-schedule' in the region does what you
 want.  We can actually implement this for `org-timestamp' as well,
 if relevant.

 Thanks,

 --
  Bastien


Hi Bastien,

Thanks for replying and giving this your attention.

Various people upthread convinced me that my feature request wasn't
really worth it. (I do hope it didn't cost you too much time!) So, I
am content to drop it here :-)

That said, it seems only right to respond and let you know how things
sit. Feel free to let it die.

Unless I misunderstood, that does not do what I had in mind.

With a fresh git pull I invoked emacs with
   emacs --no-site-file -l minimalorgtestdotemacs test.org
where minimalorgtestdotemacs reads:

(add-to-list 'auto-mode-alist '(\\.org\\' . org-mode))
(global-set-key \C-cl 'org-store-link)
(global-set-key \C-cc 'org-capture)
(global-set-key \C-ca 'org-agenda)
(global-set-key \C-cb 'org-iswitchb)
(transient-mark-mode 1)
(setq org-loop-over-headlines-in-active-region t)


and test.org reads:

* test

  2003-01-26


Versions:
Org-mode version 7.7 (release_7.7.464.g679a0)
GNU Emacs 23.2.1 (i486-pc-linux-gnu, GTK+ Version 2.20.0) of
2010-12-11 on raven, modified by Debian

If I have point and mark on either end of 2003-01-26 and invoke M-x
org-schedule, nothing occurs. By analogy with my original request,
what was desired was for the region to automatically get fed to the
datetime prompt and thus for the test subtree to acquire the scheduled
date of 2003-01-26 without further intervention.

So, thanks again for the attention to the suggestion. I'm more than
happy to let it rest here.

Best,

Brian vdB



Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-24 Thread Bastien
Hi Brian,

suvayu ali fatkasuvayu+li...@gmail.com writes:

 Ah I see it now, you want the org-timestamp command to work on a
 region. Maybe you can write your own function with lisp if you are
 doing this too often. Should be quite simple to try.

Please check `org-loop-over-headlines-in-active-region' from latest
git repo and see if using `org-schedule' in the region does what you
want.  We can actually implement this for `org-timestamp' as well,
if relevant.

Thanks,

-- 
 Bastien



Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-08 Thread suvayu ali
On Fri, Oct 7, 2011 at 4:36 PM, Brian van den Broek
brian.van.den.br...@gmail.com wrote:
 What I was suggesting was it would be cool and a small time-saver if I
 could select the text Tuesday at 3pm and hit C-u C-c . and have the
 region replaced with the stamp 2011-10-11 Tue 15:00, entirely
 bypassing the kill and yank steps. (Achim correctly suggested that
 what I want to do could be done with a keyboard macro. I still think
 it would be neat.)

Ah I see it now, you want the org-timestamp command to work on a
region. Maybe you can write your own function with lisp if you are
doing this too often. Should be quite simple to try.

GL

-- 
Suvayu

Open source is the future. It sets us free.



Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-07 Thread suvayu ali
Hi Brian,

On Fri, Oct 7, 2011 at 12:38 AM, Brian van den Broek
brian.van.den.br...@gmail.com wrote:
 It just seemed that since orgmode parse text yanked into the dt prompt, it
 would be in keeping with the general spirit of the mode to parse selected
 text, too. But, as I said, it isn't a big deal.

I don't quite understand this, are you saying when I yank text into an
org buffer it converts any random time stamp like information into
proper org format timestamps? So if I were to kill the following text
from some buffer,

some random time stamp 2011/10/07

and yank it into an org buffer, it will be converted to

some random time stamp 2011-10-07

I have been using org for quite sometime, but I don't think there is
any such feature. And if there was, I would say that's too intrusive
to assume I want every time information to be stored as timestamps. Or
on the other hand, do you mean that the text doesn't change but the
text face does? I would think that is a fontlocking issue.

If I am misunderstanding, could you please illustrate with a small example?

Thanks,

-- 
Suvayu

Open source is the future. It sets us free.



Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-07 Thread Brian van den Broek
On 7 October 2011 10:12, suvayu ali fatkasuvayu+li...@gmail.com wrote:
 Hi Brian,

 On Fri, Oct 7, 2011 at 12:38 AM, Brian van den Broek
 brian.van.den.br...@gmail.com wrote:
 It just seemed that since orgmode parse text yanked into the dt prompt, it
 would be in keeping with the general spirit of the mode to parse selected
 text, too. But, as I said, it isn't a big deal.

 I don't quite understand this, are you saying when I yank text into an
 org buffer it converts any random time stamp like information into
 proper org format timestamps? So if I were to kill the following text
 from some buffer,

 some random time stamp 2011/10/07

 and yank it into an org buffer, it will be converted to

 some random time stamp 2011-10-07

 I have been using org for quite sometime, but I don't think there is
 any such feature. And if there was, I would say that's too intrusive
 to assume I want every time information to be stored as timestamps. Or
 on the other hand, do you mean that the text doesn't change but the
 text face does? I would think that is a fontlocking issue.

 If I am misunderstanding, could you please illustrate with a small example?


Hi Suvayu and all,

It is clear that I've not been clear. I'll explain and then let it go. :-)

Say I have a buffer with the following contents:

--
Some text in a buffer with orgmode enabled.

Blah blah, Tuesday at 3pm blah blah September 19, 2003, 14:00-15:00 blah blah
--

As it is now, if I kill the text Tuesday at 3pm, and with cursor
where I killed hit C-u C-c . to invoke org-time-stamp with prefix
argument, and then yank the just killed text into the resulting
prompt, and repeat for the text Sept. ... 15:00, I get

--
Some text in a buffer with orgmode enabled.

Blah blah, 2011-10-11 Tue 15:00 blah blah 2003-09-19 Fri
14:00-15:00 blah blah
--

(the exact way the org-time-stamp command interprets the text is a
function of the date that it is run and of the value of
org-read-date-prefer-future, and possibly other variables, too.)

What I was suggesting was it would be cool and a small time-saver if I
could select the text Tuesday at 3pm and hit C-u C-c . and have the
region replaced with the stamp 2011-10-11 Tue 15:00, entirely
bypassing the kill and yank steps. (Achim correctly suggested that
what I want to do could be done with a keyboard macro. I still think
it would be neat.)

The reason that I suggested it is that I'd tried it, half-expecting it
to work. It seems to me that orgmode is pretty good at making my life
easier and this is a (very small) way in which it could, but doesn't
currently. Certainly, if I am killing text to yank into the datetime
prompt, there is a high likelihood that what I really want to do is to
replace that text with a timestamp that org derives from it. So, why
not enable that in one swell foop?

I hope that has made my suggestion clear. I'm sorry I was unclear
earlier and thus created a bit of noise.

Best,

Brian vdB



Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-06 Thread Achim Gratz
Brian van den Broek brian.van.den.br...@gmail.com writes:
 It wouldn't stun me were orgmode already able to do what I have in
 mind, but trying it and consulting the fine manual didn't suggest that
 it does.
[snip]

I'm not exactly sure what you are trying to achieve, but it seems that
you could define a keyboard macro for that?


Regards,
Achim.
-- 
+[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]+

Factory and User Sound Singles for Waldorf Blofeld:
http://Synth.Stromeko.net/Downloads.html#WaldorfSounds




Re: [O] feature suggestion: apply datetime prompt magic to selected region

2011-10-06 Thread Brian van den Broek
On 6 Oct 2011 21:46, Achim Gratz strom...@nexgo.de wrote:

 Brian van den Broek brian.van.den.br...@gmail.com writes:
  It wouldn't stun me were orgmode already able to do what I have in
  mind, but trying it and consulting the fine manual didn't suggest that
  it does.
 [snip]

 I'm not exactly sure what you are trying to achieve, but it seems that
 you could define a keyboard macro for that?

Hi Achim,

Thanks for the reply.

Indeed one could and perhaps I ought to have; my data was irregular, so I
didn't have the hope of quickly creating a robust macro. Since I had to
select regions by hand, a macro didn't seem worth it for the kill,
invocation of the datetime prompt, and yank within.

It just seemed that since orgmode parse text yanked into the dt prompt, it
would be in keeping with the general spirit of the mode to parse selected
text, too. But, as I said, it isn't a big deal.

I've weak elisp and know nothing of orgs internals. But, if there's no
uptake, maybe that'd be a small enough coding task for ?e to take some steps
to learning :-)

Best,

Brian vdB