Re: Get list of top-level headings

2021-05-19 Thread Jonathan Gregory

Hi

On 19 May 2021, John Kitchin wrote:


I think this is all you need to get a list of titles of level 1
headings as strings

(org-map-entries (lambda () (fifth (org-heading-components)))
"LEVEL=1")

this also works for me:

#+BEGIN_SRC emacs-lisp
(org-map-entries (lambda () (org-element-property :title
(org-element-at-point)) ) "LEVEL=1")
#+END_SRC


This is a better approach indeed. No need to create a new list, 
although I get faster results using:


(while (re-search-backward org-complex-heading-regexp nil t)


--
Jonathan



Re: Get list of top-level headings

2021-05-19 Thread Jonathan Gregory

Hello Florian

On 19 May 2021, Florian Lindner wrote:


Hello,

I, an Emacs Lisp newbie, want to get a list of all top-level 
headings

of the current buffer. My approach so far is:

(defun test-org-map()
  (interactive)
  (setq headings '())
  (org-map-entries (lambda ()
 (setq current-header-item 
 (org-element-property :

title (org-element-at-point))
 (message "Header: %s" current-header-item)
 (message "Is String: %s" (stringp
(org-element-property :title (org-element-at-point
 (setq headings (append current-header-item
headings))
 )
   "LEVEL=1"
   )
  (dolist (heading headings)
(message "Header Item: %s" heading)
)
  )

This gives the otput:

Header: AAA
Is String: t
Header: BBB
Is String: t
Header Item: 66 [3 times]
Header Item: 65 [3 times]

so basically the (org-element-property :title 
(org-element-at-point)
does exactly what I want, but building the list does not what I 
want.

I suppose that comes from a fundamental misunderstanding of how
strings work in elisp.

I would appreciate a short explanation (or pointers) why this 
does not
work. And of course, I am very open to completely different, 
likely

better, approches to that simply problem!

Thanks,
Florian


The org-map-entries function calls FUNC at each headline, so you 
have to (1) find the headline/title and (2) add it to your list. 
One way to do this is with the push macro.


--8<---cut here---start->8---
(defun test-org-map ()
 (interactive)
 (let (headlines)
   (org-map-entries
(lambda ()
  (let* ((element (org-element-at-point))
  (headline (org-element-property :title element)))
 (push headline headlines)))
"LEVEL=1")
   (print (nreverse headlines
--8<---cut here---end--->8---
   
Or by searching the buffer:


--8<---cut here---start->8---
(defun test-org-map ()
 (interactive)
 (let (headlines)
   (save-excursion
 (goto-char (point-max))
 (while (re-search-backward org-complex-heading-regexp nil t)
(let ((headline (match-string-no-properties 4)))
  (when (= (org-current-level) 1)
(push headline headlines
 (print headlines
--8<---cut here---end--->8---

BTW you're missing a closing parenthesis in:

(setq current-header-item (org-element-property :title 
(org-element-at-point)))


Maybe that's why you're getting errors.

--
Jonathan



Re: [PATCH] ob-lilypond: allow user configuration of header-args

2021-01-12 Thread Jonathan Gregory

Hi Jamie

On 10 Jan 2021, Jamie Bayne wrote:


Hi all,

I hit the problem described in 
https://www.mail-archive.com/emacs-orgmode@gnu.org/msg127317.html 
- I couldn't see that anyone else had fixed it so I had a go.


The problem is that, unusually, ob-lilypond has some conditional 
logic on its header-args. Its solution is to set the header-args 
internally when required, which overrides any user-specified 
value.


The quickest way I could see to preserve the conditional logic 
was to add a new variable to hold the user configuration, which 
I called ob-lilypond-header-args. Now the conditional logic sets 
org-babel-header-args:lilypond to the new variable's value when 
appropriate.


Another way would be to create a context which sets the variable 
when needed and then resets it. I opted against this as I'm not 
sure it would have the same behaviour in all cases. Happy to 
have a go if it would be preferable, though.


Cheers, Jamie


I'm not sure I understand the second proposal. Either way, the 
solution looks reasonable.


Thanks for your help

--
Jonathan



Re: Org-babel-lilypond always renders full pages

2020-04-02 Thread Jonathan Gregory
Hi

On 02 Apr 2020, stardiviner  wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
>
>
> Jonathan Gregory  writes:
>
>> Hi
>>
>> On 30 Mar 2020, stardiviner  wrote:
>>
>>> -BEGIN PGP SIGNED MESSAGE-
>>> Hash: SHA256
>>>
>>>
>>> stardiviner  writes:
>>>
>>>> -BEGIN PGP SIGNED MESSAGE-
>>>> Hash: SHA256
>>>>
>>>>
>>>> You might want to try this:
>>>>
>>>> #+begin_src emacs-lisp
>>>> (add-to-list 'org-babel-default-header-args:lilypond
>>>>  '((:prologue . "\paper{
>>>>   indent=0\mm
>>>>   line-width=120\mm
>>>>   oddFooterMarkup=##f
>>>>   oddHeaderMarkup=##f
>>>>   bookTitleMarkup = ##f
>>>>   scoreTitleMarkup = ##f
>>>> }")))
>>>> #+end_src
>>>>
>>>
>>> I found this custom setting lilypond header arguments will not work. 
>>> Because this code function:
>>>
>>> #+begin_src emacs-lisp
>>> (defun org-babel-lilypond-get-header-args (mode)
>>>   "Default arguments to use when evaluating a lilypond source block.
>>> These depend upon whether we are in Arrange mode i.e. MODE is t."
>>>   (cond (mode
>>>  '((:tangle . "yes")
>>>(:noweb . "yes")
>>>(:results . "silent")
>>>(:cache . "yes")
>>>(:comments . "yes")))
>>> (t
>>>  '((:results . "file")
>>>(:exports . "results")
>>>
>>> (defun org-babel-lilypond-set-header-args (mode)
>>>   "Set org-babel-default-header-args:lilypond
>>> dependent on ORG-BABEL-LILYPOND-ARRANGE-MODE."
>>>   (setq org-babel-default-header-args:lilypond
>>> (org-babel-lilypond-get-header-args mode)))
>>> #+end_src
>>>
>>> It always reset and return one result of two conditions.
>>>
>>> I think this is a bug.
>>
>> So are all org-babel-default-header-args:LANG custom variables? In the
>> ob-lilypond.el library the headers are hard-coded.
>>
>> [...]
>
> Yes, it's hard-coded in that function definition. So setting that header
> arguments variable will not work at all.

OK, I had a closer look and none of the other libraries set
org-babel-default-header-args:LANG in the file itself. IOW ob-lilypond
is the only one that does this, and it does this to allow toggling
between two modes (basic and arrange) and setting the header args
relative to the mode used. But there's a cost to resetting
org-babel-default-header-args:lilypond which is it prevents users from
making modifications to it.

I'm not sure how to deal with this. Maybe others can chime in.

--
Jonathan



Re: Org-babel-lilypond always renders full pages

2020-03-31 Thread Jonathan Gregory
Hi

On 30 Mar 2020, stardiviner  wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
>
>
> stardiviner  writes:
>
>> -BEGIN PGP SIGNED MESSAGE-
>> Hash: SHA256
>>
>>
>> You might want to try this:
>>
>> #+begin_src emacs-lisp
>> (add-to-list 'org-babel-default-header-args:lilypond
>>  '((:prologue . "\paper{
>>   indent=0\mm
>>   line-width=120\mm
>>   oddFooterMarkup=##f
>>   oddHeaderMarkup=##f
>>   bookTitleMarkup = ##f
>>   scoreTitleMarkup = ##f
>> }")))
>> #+end_src
>>
>
> I found this custom setting lilypond header arguments will not work. Because 
> this code function:
>
> #+begin_src emacs-lisp
> (defun org-babel-lilypond-get-header-args (mode)
>   "Default arguments to use when evaluating a lilypond source block.
> These depend upon whether we are in Arrange mode i.e. MODE is t."
>   (cond (mode
>  '((:tangle . "yes")
>(:noweb . "yes")
>(:results . "silent")
>(:cache . "yes")
>(:comments . "yes")))
> (t
>  '((:results . "file")
>(:exports . "results")
>
> (defun org-babel-lilypond-set-header-args (mode)
>   "Set org-babel-default-header-args:lilypond
> dependent on ORG-BABEL-LILYPOND-ARRANGE-MODE."
>   (setq org-babel-default-header-args:lilypond
> (org-babel-lilypond-get-header-args mode)))
> #+end_src
>
> It always reset and return one result of two conditions.
>
> I think this is a bug.

So are all org-babel-default-header-args:LANG custom variables? In the
ob-lilypond.el library the headers are hard-coded.

[...]

--
Jonathan



Re: Org-babel-lilypond always renders full pages

2020-03-31 Thread Jonathan Gregory
Hi Oliver

On 30 Mar 2020, Oliver Heck  wrote:

> Hi Jonathan,
>
> that works fine. Thank you!
>
> Can I set this as default header somewhere in the org file or will I
> have to include it to every snippet (I will have a lot of them).
>
> Oliver

You can use the Noweb Reference Syntax[1]

#+name: paper
#+begin_src text :exports none
\paper{ oddFooterMarkup=##f }
#+end_src

#+name: Lilypond
#+begin_src lilypond :file test.png
<>
\relative c'' { c d e f }
#+end_src

You can also append the extra header to the
org-babel-default-header-args:lilypond variable:

(advice-add 'org-babel-lilypond-set-header-args :filter-return
(lambda (_mode)
  (setq org-babel-default-header-args:lilypond
(append org-babel-default-header-args:lilypond
'((:epilogue . "\\paper{ oddFooterMarkup=##f 
}"))


> On 30.03.20 01:58, Jonathan Gregory wrote:
>> Hi
>>
>> On 29 Mar 2020, Oliver Heck  wrote:
>>
>>> Hi,
>>>
>>> I am trying to use org-babel-lilypond and basically got it running.
>>> But somehow I always get full lilypond pages back instead of a small
>>> snippet.
>>> This is what I have in my org-file:
>>>
>>> #+NAME: Lilypond
>>> #+BEGIN_SRC lilypond :file test.png
>>>\relative c'' { c d e f }
>>> #+END_SRC
>>>
>>>
>>> I read through the documentation on
>>> https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html
>>> but cannot find a clue.
>>>
>>> Any idea what I am doing wrong here?
>>>
>>> Cheers,
>>> Oliver
>>
>> The lilypond manual suggests using \paper variables to reduce the white
>> space around the score. In particular, you should set oddFooterMarkup
>> and oddHeaderMarkup to false.
>>
>> \paper{
>>indent=0\mm
>>line-width=120\mm
>>oddFooterMarkup=##f
>>oddHeaderMarkup=##f
>>bookTitleMarkup = ##f
>>scoreTitleMarkup = ##f
>> }
>>
>> http://lilypond.org/doc/v2.18/Documentation/usage/lilypond-output-in-other-programs
>>
>> --
>> Jonathan
>>
>
> --


--
Jonathan

Footnotes:
[1]  https://orgmode.org/manual/Noweb-Reference-Syntax.html



Re: Org-babel-lilypond always renders full pages

2020-03-29 Thread Jonathan Gregory
Hi

On 29 Mar 2020, Oliver Heck  wrote:

> Hi,
>
> I am trying to use org-babel-lilypond and basically got it running.
> But somehow I always get full lilypond pages back instead of a small
> snippet.
> This is what I have in my org-file:
>
> #+NAME: Lilypond
> #+BEGIN_SRC lilypond :file test.png
>   \relative c'' { c d e f }
> #+END_SRC
>
>
> I read through the documentation on
> https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html
> but cannot find a clue.
>
> Any idea what I am doing wrong here?
>
> Cheers,
> Oliver

The lilypond manual suggests using \paper variables to reduce the white
space around the score. In particular, you should set oddFooterMarkup
and oddHeaderMarkup to false.

\paper{
  indent=0\mm
  line-width=120\mm
  oddFooterMarkup=##f
  oddHeaderMarkup=##f
  bookTitleMarkup = ##f
  scoreTitleMarkup = ##f
}

http://lilypond.org/doc/v2.18/Documentation/usage/lilypond-output-in-other-programs

--
Jonathan



Re: [O] Orgmode Latex Export with Babel/LilyPond

2020-01-13 Thread Jonathan Gregory
Hello

On 12 Jan 2020, adam  wrote:

> On Sun, 2020-01-12 at 10:43 +1300, adam wrote:
>> On Sun, 2020-01-12 at 09:04 +1300, adam wrote:
>> > 
>> > On Sat, 2020-01-11 at 12:30 -0300, Jonathan Gregory wrote:
>> > > 
>> > > 
>> > > 
>> > > On 11 Jan 2020, adam  wrote:
>> > > 
>> > > > 
>> > > > 
>> > > > 
>> > > > Still no success in tangling the examples  modal-cycle.org  
>> > > > modal-cycle2.org  
>> > > > shown here, 
>> > > > https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html
>> > > >  
>> > > > 
>> > > > My current problem is Emacs rejecting the addition of either Lilypond 
>> > > > or 
>> > > > lilypond, in the  org-babel-do-load-languages  
>> > > > 
>> > > >(org-babel-do-load-languages
>> > > >  'org-babel-load-languages
>> > > >  '(
>> > > >(emacs-lisp . t)
>> > > >(shell . t)
>> > > >(org . t)
>> > > >(Lilypond . t)
>> > > >)) 
>> > > > 
>> > > > including either in the last line causes an error at Emacs startup, 
>> > > > reported as, 
>> > > > 
>> > > >Warning (initialization): An error occurred while loading 
>> > > > ‘/home/user/.emacs’:
>> > > >Symbol's value as variable is void: > > > > 
>> > > > 
>> > > > Earlier in my .emacs init file, I had hopefully defined lilypond, thus 
>> > > > 
>> > > >(setq ly-nix-ly-path "lilypond")
>> > > > 
>> > > >(add-to-list 'load-path "/usr/share/emacs/site-lisp/") 
>> > > > 
>> > > >(autoload 'LilyPond-mode "lilypond-mode")
>> > > > 
>> > > >(setq auto-mode-alist
>> > > >  (cons '("\\.ly$" . LilyPond-mode) auto-mode-alist))
>> > > > 
>> > > >(add-hook 'LilyPond-mode-hook (lambda () (turn-on-font-lock))) 
>> > > > 
>> > > > 
>> > > > In  /usr/share/emacs/site-lisp/   many lilypond related .el files 
>> > > > are located,
>> > > > 
>> > > >lilypond-font-lock.el
>> > > >lilypond-indent.el
>> > > >lilypond-init.el
>> > > >lilypond-mode.el
>> > > >lilypond-song.el
>> > > >lilypond-what-beat.el
>> > > >lilypond-words.el
>> > > >ltx-help.el
>> > > >ob-lilypond.el
>> > > >ob-Lilypond.el
>> > > >ob-lisp.el
>> > > >org-tests.el
>> > > > 
>> > > > 
>> > > > $ which lilypond is unhelpful,
>> > > > 
>> > > >/usr/bin/lilypond 
>> > > > 
>> > > > 
>> > > > The lilypond installation is at, 
>> > > > 
>> > > >/usr/share/lilypond/2.18.2/ 
>> > > > 
>> > > > 
>> > > > Any advice or suggestions would be most welcome. 
>> > > Version 9.1.9 comes with ob-lilypond.el. There's no ob-babel-lilypond.el 
>> > > AFAIK.
>> > > Also,
>> > > where is ly-nix-ly-path and other ly-* variables defined? I don't see 
>> > > these
>> > > variables.
>> > > 
>> > Thank you. I'll look inside ob-lilypond.el  for clues, variables to be 
>> > defined.  
>> > 
>> > 
>> > Org-mode version 9.1.9 (release_9.1.9-65 ...) @ 
>> > /usr/share/emacs/26.3/lisp/org 
>> > on Ubuntu 18.04
>> > 
>> > $ find / -name "ob*.el" locates only the ob-lilypond.el  I downloaded 
>> > from github
>> >  
>> >  
>> > Maybe I need a (require 'lilypond) somewhere, I was thinking. 
>> > 
>> > 
>> > Its a new system here. Emacs was installed with Ubuntu's software manager, 
>> > lilypond 
>> > was installed with $ sudo apt install lilypond Neither were built from 
>> > source. 
>> > 
>> 
>> OK, my bad.  When I look inside  ob-lilypond.el  I find I pulled a page of 
>> markup
>> stuff. 
>> Will grab a proper  ob-lilypond.elThat will improve matters.   
>
>
> Improvement with correct  ob-lilypond.el   Now the 
> (org-babel-do-load-languages ..) 
> doesn't cause Emacs to report error at Emacs start-up. 
>
> Presently, with examples  modal-cycle.org  modal-cycle-2.org  
> modes-in-key-of-C.org 
> I can  C-c C-e l p  export to PDF, but there's no music symbols. 
>
> Also I have no M-x ly-*  commands available. 
>
> Org Customize Option, babel, lilypond, for  org-babel-lilypond-commands  is 
> set to  nil   

That page was published 9 years ago by Martyn Jago. Some of the code in it no 
longer works with recent versions of Org and LilyPond. Also, the source files 
used in the examples reside in the author's github page (not editable in worg), 
so if you want to try them out you may have to make the adjustments yourself 
prior to running the code. In any case, I pushed a few edits which I think 
makes the tutorial easier to follow.

-- 
Jonathan



Re: [O] Orgmode Latex Export with Babel/LilyPond

2020-01-11 Thread Jonathan Gregory



On 11 Jan 2020, adam  wrote:

> Still no success in tangling the examples  modal-cycle.org  modal-cycle2.org  
> shown here, 
> https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html 
>
> My current problem is Emacs rejecting the addition of either Lilypond or 
> lilypond, in the  org-babel-do-load-languages  
>
>(org-babel-do-load-languages
>  'org-babel-load-languages
>  '(
>(emacs-lisp . t)
>(shell . t)
>(org . t)
>(Lilypond . t)
>)) 
>
> including either in the last line causes an error at Emacs startup, reported 
> as, 
>
>Warning (initialization): An error occurred while loading 
> ‘/home/user/.emacs’:
>Symbol's value as variable is void: 
>
> Earlier in my .emacs init file, I had hopefully defined lilypond, thus 
>
>(setq ly-nix-ly-path "lilypond")
>
>(add-to-list 'load-path "/usr/share/emacs/site-lisp/") 
>
>(autoload 'LilyPond-mode "lilypond-mode")
>
>(setq auto-mode-alist
>  (cons '("\\.ly$" . LilyPond-mode) auto-mode-alist))
>
>(add-hook 'LilyPond-mode-hook (lambda () (turn-on-font-lock))) 
>
>
> In  /usr/share/emacs/site-lisp/   many lilypond related .el files 
> are located,
>
>lilypond-font-lock.el
>lilypond-indent.el
>lilypond-init.el
>lilypond-mode.el
>lilypond-song.el
>lilypond-what-beat.el
>lilypond-words.el
>ltx-help.el
>ob-lilypond.el
>ob-Lilypond.el
>ob-lisp.el
>org-tests.el
>
>
> $ which lilypond is unhelpful,
>
>/usr/bin/lilypond 
>
>
> The lilypond installation is at, 
>
>/usr/share/lilypond/2.18.2/ 
>
>
> Any advice or suggestions would be most welcome. 

Version 9.1.9 comes with ob-lilypond.el. There's no ob-babel-lilypond.el AFAIK. 
Also, where is ly-nix-ly-path and other ly-* variables defined? I don't see 
these variables.

-- 
Jonathan



Re: [O] Orgmode Latex Export with Babel/LilyPond

2020-01-11 Thread Jonathan Gregory



On 20 Apr 2019, Jakob Schöttl  wrote:

> Hi,
>
> I'm trying (second attempt), to setup orgmode to export PDFs with
> images generated by Babel/LilyPond.
>
> I followed the setup instructions here:
>
> https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html
>
> I have
>
> a recent emacs (Arch Linux),
>
> ~/.emacs file with
>
> (org-babel-do-load-languages
>   'org-babel-load-languages
>   '((lilypond t)))
>
> (although I saw many other snippets where there is a "." between the
> (lilypond t)). I tried both variants.
>
> I tried also tried (require 'lilypond) instead
> org-babel-do-load-languages which caused an error.
>
> I pressed C-c C-e l p -> "PDF file produced."
>
> But no images are generated and no images appear in the PDF. Only
> plain source code.
>
> Any ideas?
> Thank you!
>
> - Jakob

I tried the first example with emacs -Q using Org versions 9.1.9 and 9.3.1, and 
there were a few things I had to change to generate the image correctly after 
C-c C-e l p:

- Add `:results file :exports results` to the header,
- change \relative c’ to \relative c', and
- add the missing the #+end_src part.

The configuration I used:

(add-to-list 'load-path "/usr/share/emacs/site-lisp")
(require 'lilypond-mode)

(org-babel-do-load-languages
 'org-babel-load-languages
 '((lilypond . t)))

-- 
Jonathan