emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Bernt Hansen <bernt@norang.ca>
To: Patrick Drechsler <patrick@pdrechsler.de>
Cc: emacs-orgmode@gnu.org
Subject: Re: shortcut keys for mark ups
Date: Wed, 07 Jan 2009 17:16:02 -0500	[thread overview]
Message-ID: <87fxjuzrrx.fsf@gollum.intra.norang.ca> (raw)
In-Reply-To: <gk338e$rqr$1@ger.gmane.org> (Patrick Drechsler's message of "Wed\, 07 Jan 2009 21\:27\:59 +0100")

Patrick Drechsler <patrick@pdrechsler.de> writes:

> Bernt Hansen schrieb:
> [snipped nice code]
>
> How can I extend this function to wrap the current selection into a
>
> #+BEGIN_SRC
> ...code...
> #+END_SRC
>
> string while also interactively asking the user for a language string?
>
> -----------------------------------------------------------------
> ;;  ============================================================
> ;; Puts current selection between #+BEGIN_SRC and #+END_SRC
> ;; and ask for language
> ;;  ============================================================
> ;; TODO:
> ;;  1. Retrieve beginning (C-SPACE) and
> ;;     end of selection (current position)
> ;;  2. Ask for language and insert that string
> ;;     after `#+BEGIN_SRC '
> (defun my-wrap-src ()
>   (interactive)
>   (save-excursion
>     (beginning-of-line)
>     (insert "#+BEGIN_SRC\n")
>     (end-of-line)
>     (insert "\n#+END_SRC")))
> (define-key org-mode-map (kbd "<f9> s") 'my-wrap-src)
> -----------------------------------------------------------------
>
> Example org code before function call:
>
> -----------------------------------------------------------------
> * Sample Code
> public class Bla {
> 	public static void main(String[] args) {
> 		System.out.println("Hello");
> 	}
> }
> -----------------------------------------------------------------
>
> With the code above:
>
> - Mark the region: C-SPACE on the letter `p' of the string `public'
> and moving the cursor behind the last closing curly bracket.
>
> - String after being asked for a language: `java'
>
> Example org code after function call:
>
> -----------------------------------------------------------------
> * Sample Code
> #+BEGIN_SRC java
> public class Bla {
> 	public static void main(String[] args) {
> 		System.out.println("Hello");
> 	}
> }
> #+END_SRC
> -----------------------------------------------------------------

Something like this maybe?  It's not 100% to spec. I find regions that
are entire lines easier to work with so the end point would be the
beginning of the first line not in the region (not after the })
For me - triple clicking and dragging selects entire lines which works
well for this.

------------------------------------------------------------------------
(defun my-wrap-src ()
  (interactive)
  (let ((beg (region-beginning))
	(end (region-end))
	(src (read-from-minibuffer "Source type: " "java")))
    (save-excursion
      (goto-char (region-end))
      (insert "#+END_SRC\n")
      (goto-char (region-beginning))
      (insert "#+BEGIN_SRC " src "\n"))))
------------------------------------------------------------------------

If you want your behaviour you probably jsut have to change 
      (insert "#+END_SRC\n")
to
      (insert "\n#+END_SRC")
but that's untested.

HTH,
-Bernt

  reply	other threads:[~2009-01-07 22:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-06 16:35 Xin Shi
2009-01-06 17:48 ` Bernt Hansen
2009-01-06 18:50   ` Xin Shi
2009-01-06 19:00     ` Carsten Dominik
2009-01-06 19:25     ` Richard Riley
2009-01-07 20:27   ` Patrick Drechsler
2009-01-07 22:16     ` Bernt Hansen [this message]
2009-01-07 23:05       ` Patrick Drechsler
2009-01-06 18:24 ` Carsten Dominik
2009-01-06 18:47   ` Xin Shi

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=87fxjuzrrx.fsf@gollum.intra.norang.ca \
    --to=bernt@norang.ca \
    --cc=emacs-orgmode@gnu.org \
    --cc=patrick@pdrechsler.de \
    --subject='Re: shortcut keys for mark ups' \
    /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

Code repositories for project(s) associated with this 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).