From: Adam Solove <asolove@gmail.com>
To: emacs-orgmode@gnu.org
Subject: First extension: cycling through links
Date: Sat, 28 Jun 2008 14:31:36 -0700 [thread overview]
Message-ID: <m2abh5fdqf.fsf@gmail.com> (raw)
Hi all,
First, I wanted to say thanks to Carsten and the community here for
org-mode. I'm a graduate student in Chinese literature and after trying
out many outliners and information management systems I've settled very
happily on org-mode and found it to be amazingly useful and especially
well-documented.
I wanted to post a snippet of code I've been using because I haven't
read through the entire org-mode source, haven't ever written emacs
extensions before, and would love to know if there is an easier or
more idiomatic way of doing the following.
I usually create a glossary with radio targets at the end of a project,
allowing me to quickly see autolinks from any other text I might be
working on. Sometimes I want to do the opposite: from a radio target or
link, cycle through all the places in the text that are autolinked to
the same target. The code below does a sparse tree and then, since I
often have lots of occurences spread over more than a page, lets you
cycle forward and back through the actual occurences.
I would love if someone could let me know any defects or infelicities
with my method.
Thanks,
Adam
;; keys:
(org-defkey org-mode-map "\C-]" 'org-backlinks-cycle-forward)
(org-defkey org-mode-map "\C-[" 'org-backlinks-cycle-backward)
;; org-backlinks.el:
(defvar org-backlinks-regexp nil
"Regexp to find most recent target called with org-backlinks-...")
(make-variable-buffer-local 'org-backlinks-regexp)
(defun org-backlinks-cycle-forward ()
"Cycle forward through links to the link at point"
(interactive)
(org-backlinks-cycle 're-search-forward (+ (point-min) 1)))
(defun org-backlinks-cycle-backward ()
"Cycle backwards through links to the link at point"
(interactive)
(org-backlinks-cycle 're-search-backward (- (point-max) 1)))
(defun org-backlinks-cycle (re-search backup)
"Find links pointing to the radio target near point, or else the last
radio target at which the command was called. Can be called repeatedly."
(let ((new-regexp (org-make-target-link-regexp
(ensure-list (org-find-link-path-at-point)))))
(when (and new-regexp (not (string= new-regexp org-backlinks-regexp)))
(setq org-backlinks-regexp new-regexp)
(org-occur org-backlinks-regexp))
(when org-backlinks-regexp
(unless (funcall re-search org-backlinks-regexp nil t)
(goto-char backup) ; ugly, but so that we can -1 later
(org-backlinks-cycle re-search backup)))))
(defun org-find-link-path-at-point ()
"Returns string text of link at point, or nil"
(text-at (org-find-link-at-point)))
(defun org-find-link-at-point ()
"Returns positions bounding the beginning and end of link at point, else nil"
(let ((pos (- (point) 1))) ; since search will leave us off just after the link
(when (get-text-property pos 'org-linked-text)
(cons (previous-single-property-change pos 'org-linked-text)
(next-single-property-change pos 'org-linked-text)))))
(defun org-find-target-name-near-point ()
"Returns the text name of the nearbye target, or nil if none"
(text-at (org-find-target-near-point)))
(defun org-find-target-near-point ()
"Returns markers bounding the beginning and
end of nearby target, else nil"
(or (org-in-regexp org-radio-target-regexp)
(org-in-regexp org-target-regexp))
(cons (match-beginning 1) (match-end 1)))
;; Utility functions I had defined elsewhere...
(defun apply-cons (fn cons)
"Apply a function to the two items in a cons cell"
(apply fn (list (car cons) (cdr cons))))
(defun text-at (cons)
"Returns the text between the two markers/positions
in the provided cons, else nil"
(and cons
(apply-cons 'buffer-substring-no-properties cons)))
(defun ensure-list (item)
(if (listp item)
item
(list item)))
reply other threads:[~2008-06-28 21:35 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m2abh5fdqf.fsf@gmail.com \
--to=asolove@gmail.com \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).