From mboxrd@z Thu Jan 1 00:00:00 1970 From: Leonard Randall Subject: Links Drawer Date: Mon, 9 Jun 2014 16:19:45 +0100 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=047d7bd6c514c75ffd04fb68bd23 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:34858) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wu1MU-0003Kg-0Z for emacs-orgmode@gnu.org; Mon, 09 Jun 2014 11:19:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wu1MR-0005ir-Iq for emacs-orgmode@gnu.org; Mon, 09 Jun 2014 11:19:49 -0400 Received: from mail-oa0-x22f.google.com ([2607:f8b0:4003:c02::22f]:61279) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wu1MR-0005id-4r for emacs-orgmode@gnu.org; Mon, 09 Jun 2014 11:19:47 -0400 Received: by mail-oa0-f47.google.com with SMTP id n16so2020457oag.20 for ; Mon, 09 Jun 2014 08:19:46 -0700 (PDT) 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode --047d7bd6c514c75ffd04fb68bd23 Content-Type: text/plain; charset=UTF-8 Hi all, In a few functions that I use for organizing my research, I use links drawers. This enables me to write functions that automatically insert links in all entries matching particular criteria, or insert links directly from the headline or anywhere else in the entry. To do this I wrote a couple of functions. The first function is nearly identical to org-insert-properties-drawer (I only changed a bit of regexp). It simply inserts a LINKS drawer near the top of the entry below any clocking, scheduling, or deadline or PROPERTIES info. It is not for interactive use. The the other searches for a links drawer in the current entry and inserts one if none is found. It then inserts a link in the drawer using org-insert-link. Anyway I thought they might be useful for others, and I wondered if people thought that they should be part of org-mode. Here is the code. Don't hesitate to give any suggestions. (defun my-org-insert-links-drawer () "Insert a links drawer near the beginning of the current entry. Inserts drawer near beginning of entry after scheduling, deadline and clocking info and after standard logging, property and clock drawers." (org-back-to-heading t) (looking-at org-outline-regexp) (let ((indent (if org-adapt-indentation (- (match-end 0) (match-beginning 0)) 0)) (beg (point)) (re (concat "^[ \t]*" org-keyword-time-regexp)) end hiddenp) (outline-next-heading) (setq end (point)) (goto-char beg) (while (re-search-forward re end t)) (setq hiddenp (outline-invisible-p)) (end-of-line 1) (and (equal (char-after) ?\n) (forward-char 1)) (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:PROPERTIES:\\|:END:\\)") (if (member (match-string 1) '("CLOCK:" ":END:")) ;; just skip this line (beginning-of-line 2) ;; Drawer start, find the end (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t) (beginning-of-line 1))) (org-skip-over-state-notes) (skip-chars-backward " \t\n\r") (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n))) (forward-char 1)) (goto-char (point-at-eol)) (let ((inhibit-read-only t)) (insert "\n:LINKS:\n:END:")) (beginning-of-line 0) (org-indent-to-column indent) (beginning-of-line 2) (org-indent-to-column indent) (beginning-of-line 0) (if hiddenp (save-excursion (org-back-to-heading t) (hide-entry)) (org-flag-drawer t)))) (defun my-org-insert-link-in-drawer (&optional complete-file link-location default-description) "Find or create links drawer then insert link with `org-insert-link'. The arguments COMPlETE-FILE LINK-LOCATION and DEFAULT-DESCRIPTION are passsed on to `org-insert-link'. See its documentation for information on how these arguments are handled." (interactive) ;; make sure that we are currently in an entry. (if (not (org-before-first-heading-p)) ;; look for links drawer. (save-excursion (org-back-to-heading t) (let ((beg (point)) end hiddenp) (outline-next-heading) (setq end (point)) (backward-char 1) (setq hiddenp (outline-invisible-p)) (goto-char beg) (unless (and (re-search-forward "[ \t]*:LINKS:[ \t]*$" end t) (re-search-forward "[ \t]*:END:[ \t]*$" end t)) ;; if no links drawer found create one. (my-org-insert-links-drawer)) ;; go to links drawer. (goto-char beg) (re-search-forward "[ \t]*:LINKS:[ \t]*$") ;; open new line and insert link. (newline) (org-insert-link complete-file link-location default-description) (if hiddenp (hide-entry)))) ;; Give error if before first heading. (error "Before first heading"))) All best, Leonard --047d7bd6c514c75ffd04fb68bd23 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi all,
In a few functions that I = use for organizing my research, I use links drawers. This enables me to wri= te functions that automatically insert links in all entries matching partic= ular criteria, or insert links directly from the headline or anywhere else = in the entry. To do this I wrote a couple of functions. The first function = is nearly identical to org-insert-properties-drawer (I only changed a bit o= f regexp). It simply inserts a LINKS drawer near the top of the entry below= any clocking, scheduling, or deadline or PROPERTIES info. It is not for in= teractive use. The the other searches for a links drawer in the current ent= ry and inserts one if none is found. It=C2=A0 then inserts a link in the dr= awer using org-insert-link.


Anyway I thought they might be useful for others, and I wondered if= people thought that they should be part of org-mode.=C2=A0 Here is the cod= e. Don't hesitate to give any suggestions.


(defun my-org-ins= ert-links-drawer ()
=C2=A0 "Insert a links drawer near the beginning of the current entry.=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Inserts drawer near beginning of= entry after scheduling, deadline
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 and clocking info and after standard logging, property and clock
= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 drawers."
=C2=A0 (org-ba= ck-to-heading t)
=C2=A0 (looking-at org-outline-regexp)
=C2=A0 (let ((indent (if org-adapt-indentation
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 (- (match-end 0) (match-beginning 0))
=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 0))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (beg (point))=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (re (concat "^[ \t]*"= ; org-keyword-time-regexp))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 end hiddenp)
=C2=A0=C2=A0=C2= =A0 (outline-next-heading)
=C2=A0=C2=A0=C2=A0 (setq end (point))
=C2= =A0=C2=A0=C2=A0 (goto-char beg)
=C2=A0=C2=A0=C2=A0 (while (re-search-for= ward re end t))
=C2=A0=C2=A0=C2=A0 (setq hiddenp (outline-invisible-p))<= br>=C2=A0=C2=A0=C2=A0 (end-of-line 1)
=C2=A0=C2=A0=C2=A0 (and (equal (ch= ar-after) ?\n) (forward-char 1))
=C2=A0=C2=A0=C2=A0 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\= |CLOCK:\\|:PROPERTIES:\\|:END:\\)")
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = (if (member (match-string 1) '("CLOCK:" ":END:"))=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; just skip this = line
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (beginning-of-line 2= )
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; Drawer start, find the e= nd
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (re-search-forward "^= \\*+ \\|^[ \t]*:END:" nil t)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 (beginning-of-line 1)))
=C2=A0=C2=A0=C2=A0 (org-skip-over-state-n= otes)
=C2=A0=C2=A0=C2=A0 (skip-chars-backward " \t\n\r")
=C2=A0=C2=A0=C2=A0 (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n= )))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (forward-char 1))
=C2= =A0=C2=A0=C2=A0 (goto-char (point-at-eol))
=C2=A0=C2=A0=C2=A0 (let ((inh= ibit-read-only t)) (insert "\n:LINKS:\n:END:"))
=C2=A0=C2=A0= =C2=A0 (beginning-of-line 0)
=C2=A0=C2=A0=C2=A0 (org-indent-to-column indent)
=C2=A0=C2=A0=C2=A0 (beg= inning-of-line 2)
=C2=A0=C2=A0=C2=A0 (org-indent-to-column indent)
= =C2=A0=C2=A0=C2=A0 (beginning-of-line 0)
=C2=A0=C2=A0=C2=A0 (if hiddenp<= br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (save-excursion
=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (org-back-to-heading t)
= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (hide-entry))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (org-flag-drawer t))))

(defun my-org-= insert-link-in-drawer (&optional complete-file link-location default-de= scription)
=C2=A0 "Find or create links drawer then insert link wit= h `org-insert-link'.
The arguments COMPlETE-FILE LINK-LOCATION and DEFAULT-DESCRIPTION
are pa= sssed on to `org-insert-link'.=C2=A0 See its documentation for
infor= mation on how these arguments are handled."
=C2=A0 (interactive)=C2=A0 ;; make sure that we are currently in an entry.
=C2=A0 (if (not (org-before-first-heading-p))
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 ;; look for links drawer.
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (save-ex= cursion
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (org-back-to-heading = t)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (let ((beg (point))
=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= end hiddenp)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (ou= tline-next-heading)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq end (point))=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (backward-char 1)<= br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq hiddenp (ou= tline-invisible-p))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (goto-char beg)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (unless (and (re-search-forward "[ \t]*:LINKS:[ \t]*$" end t)=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (re-search-forwar= d "[ \t]*:END:[ \t]*$" end t))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; if no links drawer found create one= .
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (my= -org-insert-links-drawer))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 ;; go to links drawer.
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (goto-char beg)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (re-search-forward &= quot;[ \t]*:LINKS:[ \t]*$")
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 ;; open new line and insert link.
=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (newline)
=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 (org-insert-link complete-file link-location de= fault-description)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (if hiddenp
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (hide-entry))))
=C2=A0=C2=A0=C2=A0 ;; Give error if before first hea= ding.
=C2=A0=C2=A0=C2=A0 (error "Before first heading")))
<= br>

All best,
Leonard
--047d7bd6c514c75ffd04fb68bd23--