Re: [O] Go to heading using LISP
On Mon, Jun 10, 2013 at 11:14 PM, Alexander Wingård < alexander.wing...@gmail.com> wrote: > > Maybe some day I will learn some LISP and teach it to navigate the > hierarchical structure. > > I actually got curious and gave this a try and here's what I came up with: test.org: * a ** b *** h ** b *** q h ** c *** d Elisp: (defun goto-notes () (interactive) (find-file "~/test.org") (org-goto-subtree '("a" "b" "q" "h")) (org-show-context) (org-show-entry) (show-children)) (defun org-goto-subtree (path) (let ((level 1)) (org-element-map (org-element-parse-buffer 'headline) 'headline (lambda (x) (if (< (org-element-property :level x) level) (setq level (org-element-property :level x))) (if (and (= level (org-element-property :level x)) (string= (nth (- level 1) path) (org-element-property :raw-value x))) (progn (setq level (+ level 1)) (if (> level (list-length path)) (goto-char (org-element-property :begin x)) nil t))) https://gist.github.com/AlexanderWingard/5814843 My very first attempt at programming Elisp so any feedback is appreciated. Best regards Alexander
Re: [O] Go to heading using LISP
On 10 jun 2013, at 21:00, Myles English wrote: > > Hi Alexander, > > Alexander Wingård writes: > >> I want to create special key-bindings that use the org-refile goto >> interface to jump to specific headings. > > It doesn't use org-refile but this is what I use: > > (defun my-goto-heading(file heading-text) > "Visit file `file' and goto headline `heading-text'" > (find-file file) > (org-element-map (org-element-parse-buffer 'headline) 'headline > (lambda (x) > (if (string= (org-element-property :raw-value x) heading-text) > (goto-char (org-element-property :begin x)) > nil)) > nil t)) ;; stop at first find > Thanks alot! This is exactly what I was asking for. This will suffice for me now. Maybe some day I will learn some LISP and teach it to navigate the hierarchical structure. Best Regards /Alexander
Re: [O] Go to heading using LISP
Hi Alexander, Alexander Wingård writes: > I want to create special key-bindings that use the org-refile goto > interface to jump to specific headings. It doesn't use org-refile but this is what I use: (defun my-goto-heading(file heading-text) "Visit file `file' and goto headline `heading-text'" (find-file file) (org-element-map (org-element-parse-buffer 'headline) 'headline (lambda (x) (if (string= (org-element-property :raw-value x) heading-text) (goto-char (org-element-property :begin x)) nil)) nil t)) ;; stop at first find (defun gtd() (interactive) (my-goto-heading (concat org-directory "/gtd.org") "Daily work") (org-show-entry) ;;(org-show-subtree) (reposition-window) (org-agenda-list)) In the gtd function I also set org-agenda-files but left it out for clarity. Myles
Re: [O] Go to heading using LISP
Eric Abrahamsen writes: > if you're just looking for a lisp function to jump to a headline One can use org-ctags and use Emacs and etags interface (M-. etc) to jump to a headline. One can also choose the headline via completion.
Re: [O] Go to heading using LISP
Alexander Wingård writes: > Eric Abrahamsen ericabrahamsen.net> writes: > >> >> Alexander Wingård gmail.com> writes: >> >> > Hi! >> > >> > I want to create special key-bindings that use the org-refile goto >> > interface to jump to specific headings. > More specific example, let's say I want to bind keys in my .emacs file: > > "C-c b" -> find "gtd.org" and jump to "Projects/Work/Bugs" > "C-c m" -> find "gtd.org" and jump to "Projects/Work/Meetings" >> > >> > My initial attempt was: >> > (org-refile 4 "gtd.org" "Projects/Work/Bugs") >> > >> > But it seems specifying RFLOC is not that simple. >> > >> > Someone have any idea how to achieve this or another way to jump to a >> > heading? >> >> Is the `org-goto' interface close enough? It only does the current >> buffer, but you can set org-goto-interface to make it behave a fair bit >> like refile... > I've looked at that function aswell but it seems even harder to achieve > what I want with: Call it from lisp without any user interaction. If you think > that is possible I would love an example. Ah, I see. Well, if you're just looking for a lisp function to jump to a headline, you could use `org-refile-get-targets' to produce a full table of all the possible refile targets, and then choose among them. (assoc "gtd.org/Projects/Work/Bugs" (org-refile-get-targets)) will find the right heading, then I guess the value of point is in that heading somewhere -- you can visit the file and goto-point. But if you're just looking to bind a bunch of keys to "jump to specific heading" commands, I'd use custom ids. Give all the headings in question ID properties, and then just use something like this: (global-set-key (kbd "C-c b") (lambda () (org-id-open "f1d05552-1dce-4004-a914-d2b092d8470e"))) Hope that helps, Eric
Re: [O] Go to heading using LISP
Eric Abrahamsen ericabrahamsen.net> writes: > > Alexander Wingård gmail.com> writes: > > > Hi! > > > > I want to create special key-bindings that use the org-refile goto > > interface to jump to specific headings. More specific example, let's say I want to bind keys in my .emacs file: "C-c b" -> find "gtd.org" and jump to "Projects/Work/Bugs" "C-c m" -> find "gtd.org" and jump to "Projects/Work/Meetings" > > > > My initial attempt was: > > (org-refile 4 "gtd.org" "Projects/Work/Bugs") > > > > But it seems specifying RFLOC is not that simple. > > > > Someone have any idea how to achieve this or another way to jump to a > > heading? > > Is the `org-goto' interface close enough? It only does the current > buffer, but you can set org-goto-interface to make it behave a fair bit > like refile... I've looked at that function aswell but it seems even harder to achieve what I want with: Call it from lisp without any user interaction. If you think that is possible I would love an example. > > Yours > Eric > >
Re: [O] Go to heading using LISP
Alexander Wingård writes: > Hi! > > I want to create special key-bindings that use the org-refile goto > interface to jump to specific headings. > > My initial attempt was: > (org-refile 4 "gtd.org" "Projects/Work/Bugs") > > But it seems specifying RFLOC is not that simple. > > Someone have any idea how to achieve this or another way to jump to a > heading? Is the `org-goto' interface close enough? It only does the current buffer, but you can set org-goto-interface to make it behave a fair bit like refile... Yours Eric