emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Links Drawer


From: Leonard Randall
Subject: [O] Links Drawer
Date: Mon, 9 Jun 2014 16:19:45 +0100

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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]