emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nik Clayton <nik@ngo.org.uk>
To: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: company-mode completions for noweb references
Date: Tue, 6 Nov 2018 19:32:20 +0100	[thread overview]
Message-ID: <CAKJTzL7gTHVpEcHCpJgzLPn1OmDY-qjPnV7w-mLBO77qyZ6TFQ@mail.gmail.com> (raw)

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

Hoi,

I've been writing a few things using Org where I'm making frequent use of
noweb references in SRC blocks, and decided that completion would be handy
for them.

This function will complete "^<<" with a list of the defined named blocks
in the current file, if you're in a SRC block.

It could be made smarter (e.g., only offer the completion if ":noweb yes"
is set, only offer to complete from named blocks with the same language as
the current block), but I thought I'd throw it out here in case anyone else
finds it useful.

Best, N

(defun my/org-src-block-name-backend (command &optional arg &rest ignored)
  "Complete `<<' with the names of defined SRC blocks."
  (interactive (list 'interactive))
  (cl-case command
    (interactive (company-begin-backend 'my/org-src-block-name-backend))
    (init (require 'org-element))
    (prefix (and (eq major-mode 'org-mode)
         (eq 'src-block (car (org-element-at-point)))
         (cons (company-grab-line "^<<\\(\\w*\\)" 1) t)))
    (candidates
     (org-element-map (org-element-parse-buffer) 'src-block
       (lambda (src-block)
     (let ((name (org-element-property :name src-block)))
       (when name
         (propertize
          name
          :value (org-element-property :value src-block)
          :annotation (org-element-property :raw-value (org-element-lineage
src-block '(headline)))))))))
    (sorted t)            ; Show candidates in same order as doc
    (ignore-case t)
    (duplicates nil)               ; No need to remove duplicates
    (post-completion               ; Close the reference with ">>"
     (insert ">>"))
    ;; Show the contents of the block in a doc-buffer. If you have
    ;; company-quickhelp-mode enabled it will show in a popup
    (doc-buffer (company-doc-buffer (get-text-property 0 :value arg)))
    (annotation (format " [%s]" (get-text-property 0 :annotation arg)))))

(add-to-list 'company-backends 'my/org-src-block-name-backend)

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

             reply	other threads:[~2018-11-06 18:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-06 18:32 Nik Clayton [this message]
2018-11-07  6:33 ` company-mode completions for noweb references Eric S Fraga

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=CAKJTzL7gTHVpEcHCpJgzLPn1OmDY-qjPnV7w-mLBO77qyZ6TFQ@mail.gmail.com \
    --to=nik@ngo.org.uk \
    --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).