emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Links Drawer
@ 2014-06-09 15:19 Leonard Randall
  0 siblings, 0 replies; only message in thread
From: Leonard Randall @ 2014-06-09 15:19 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 3954 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 4955 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-06-09 15:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-09 15:19 Links Drawer Leonard Randall

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).