From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: Structured links to headings with endless depth Date: Tue, 13 Mar 2018 20:49:46 -0700 Message-ID: References: <1520849353.1793.66.camel@gmail.com> <87tvtlsi2f.fsf@gmail.com> <1520851190.1793.74.camel@gmail.com> <87lgexfnl6.fsf@christianmoe.com> <1520862394.1793.83.camel@gmail.com> <87r2opcrkm.fsf@nicolasgoaziou.fr> <1520867316.1793.87.camel@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="089e08238778368aa105675744dd" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:34730) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evxQE-0006fR-MD for Emacs-orgmode@gnu.org; Tue, 13 Mar 2018 23:49:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evxQD-0004K7-Di for Emacs-orgmode@gnu.org; Tue, 13 Mar 2018 23:49:50 -0400 Received: from mail-wr0-x230.google.com ([2a00:1450:400c:c0c::230]:41451) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1evxQD-0004J3-1W for Emacs-orgmode@gnu.org; Tue, 13 Mar 2018 23:49:49 -0400 Received: by mail-wr0-x230.google.com with SMTP id f14so3271079wre.8 for ; Tue, 13 Mar 2018 20:49:48 -0700 (PDT) In-Reply-To: <1520867316.1793.87.camel@gmail.com> 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: ST Cc: Eric S Fraga , org-mode-email , Christian Moe , Nicolas Goaziou --089e08238778368aa105675744dd Content-Type: text/plain; charset="UTF-8" This is a tricky problem to generally solve. I think this does it approximately well enough. It is lightly tested and works by exactly matching headlines at subsequent levels. It will be problematic if you have headlines with : in them, and it assumes there is a level 1 headline to start in. #+BEGIN_SRC emacs-lisp (defun xpath-follow (path) (let* ((fields (split-string path "::")) (fname (car fields)) (paths (split-string (cadr fields) ":")) (level 0) (current-point (point)) cp hls n found) (org-mark-ring-push) (find-file fname) (save-restriction (while paths (setq cp (pop paths)) (incf level) (setq hls (org-element-map (org-element-parse-buffer) 'headline (lambda (hl) (when (eq level (org-element-property :level hl)) hl)))) (setq n (-find-index (lambda (hl) (string= cp (org-no-properties (org-element-property :raw-value hl)))) hls)) (if (not n) (progn (goto-char current-point) (user-error "%s not found" cp)) (goto-char (org-element-property :begin (nth n hls))) (org-narrow-to-subtree)))))) (org-link-set-parameters "xpath" :follow 'xpath-follow) #+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 Mon, Mar 12, 2018 at 8:08 AM, ST wrote: > Hello, > > Ss Christian has pointed out - introduce a separate CUSTOM_ID for text > with fixed structure and rather short verses is too heavy. > > I do need to write a custom link type, if this use case is not of common > interest for the orgmode community. > > Thank you, > > > On Mon, 2018-03-12 at 15:10 +0100, Nicolas Goaziou wrote: > > Hello, > > > > ST writes: > > > > > I'm not that experienced in writing in lisp. Is it difficult to create > > > such custom link type? What is the closest link type that you would > > > recommend to take as starting point (link on code, if possible)? > > > > I'm not answering your question, but I suggest to use a CUSTOM_ID > > instead. This is readily available. > > > > Regards, > > > > > --089e08238778368aa105675744dd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
This is a tricky problem to generally solve. I think this = does it approximately well enough. It is lightly tested and works by exactl= y matching headlines at subsequent levels. It will be problematic if you ha= ve headlines with : in them, and it assumes there is a level 1 headline to = start in.

#+BEGIN_SRC emacs-lisp
(defun x= path-follow (path)
=C2=A0 (let* ((fields (split-string path "= ;::"))
(fname (car = fields))
(paths (split-s= tring (cadr fields) ":"))
(level 0)
(c= urrent-point (point))
cp= hls n found)=C2=A0
=C2=A0 =C2=A0 (org-mark-ring-push)
= =C2=A0 =C2=A0 (find-file fname)
=C2=A0 =C2=A0 (save-restriction
=C2=A0 =C2=A0 =C2=A0 (while paths
(setq cp (pop paths))
(incf level)
(setq hls (org-element-map (org-element-parse-buffer) 'headline
=C2=A0 =C2=A0 (lambda (hl)
=C2=A0 =C2=A0 =C2=A0 (whe= n (eq level (org-element-property :level hl))
hl))))
(setq n (-find-index (lambda (hl)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(string=3D cp (org-no-properties=
=C2=A0 =C2=A0 (org-e= lement-property :raw-value hl))))
=C2=A0 =C2=A0 =C2=A0hls))
(if (not n)
= =C2=A0 =C2=A0 (progn
=C2= =A0 =C2=A0 =C2=A0 (goto-char current-point)
=C2=A0 =C2=A0 =C2=A0 (user-error "%s not found"= ; cp))
=C2=A0 (goto-char = (org-element-property :begin (nth n hls)))
=C2=A0 (org-narrow-to-subtree))))))

<= div>(org-link-set-parameters
=C2=A0"xpath"
= =C2=A0:follow 'xpath-follow)
#+END_SRC


John

-----------------------------------
Professor= John Kitchin=C2=A0
Doherty Hall A207F
Department of Chemical Enginee= ring
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803<= /div>

On Mon, Mar 12, 2018 at 8:08 AM, ST <smntov@= gmail.com> wrote:
Hello,
Ss Christian has pointed out - introduce a separate CUSTOM_ID for text
with fixed structure and rather short verses is too heavy.

I do need to write a custom link type, if this use case is not of common interest for the orgmode community.

Thank you,


On Mon, 2018-03-12 at 15:10 +0100, Nicolas Goaziou wrote:
> Hello,
>
> ST <smntov@gmail.com> wr= ites:
>
> > I'm not that experienced in writing in lisp. Is it difficult = to create
> > such custom link type? What is the closest link type that you wou= ld
> > recommend to take as starting point (link on code, if possible)?<= br> >
> I'm not answering your question, but I suggest to use a CUSTOM_ID<= br> > instead. This is readily available.
>
> Regards,
>



--089e08238778368aa105675744dd--