[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[O] Elisp function for wrapping sexp or region in src-block (was Re: som
From: |
Thorsten Jolitz |
Subject: |
[O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help) |
Date: |
Tue, 29 Jul 2014 15:30:24 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
Xebar Saram <address@hidden> writes:
> Thx and sorry about not posting to the list, was a gmail mistake (i
> really *need* to start with gnus..just to intimidating :))
>
> wrapping in parens by marking the region kinda defeats the purpose
> sine the original function i posted (in the original post) already
> wraps the marked text in an orgmode block :)
>
> thanks and sorry for PM
no problem since I really want this function too and was a bit
frustrated often enough that it does not exist in Org-mode, I made it
more flexible and generic now, for daily use:
#+begin_src emacs-lisp
(defun tj/wrap-sexp-or-reg-in-src-block (&optional lang lines)
"Wrap sexp at point or region in src block"
(interactive
(when current-prefix-arg
(list
(ido-completing-read "Org-Babel language: "
(mapcar
(lambda (--lang)
(symbol-name (car --lang)))
org-babel-load-languages)
nil nil nil nil "emacs-lisp")
(read-number "Number of lines to wrap: " 1))))
(let* ((language (or lang "emacs-lisp"))
(marker (point-marker))
(beg (point))
(end (if lines
(save-excursion
(forward-line lines) (point))
(save-excursion
(forward-sexp) (point))))
(cut-strg (buffer-substring beg end)))
(delete-region beg end)
(goto-char (marker-position marker))
(insert
(format
"\n#+begin_src %s\n%s\n#+end_src\n"
language cut-strg))
(set-marker marker nil)))
#+end_src
try it with
,----
| C-u M-x tj/wrap-sexp-or-reg-in-src-block
`----
and you get what you want (negative line-number wrap backwards).
> On Tue, Jul 29, 2014 at 3:42 PM, Thorsten Jolitz <address@hidden>
> wrote:
>
> Xebar Saram <address@hidden> writes:
>
> why a PM???
>
>
> > Thx Thorsten
> >
> > this works very well for code blocks. it seems though that for
> normal
> > text paragraphs it dosent work that well. may i suggest an idea?
> can
> > the function perhaps prompt the user (when you launch the
> function)
> > for number of lines to wrap and then issue the wrap based on
> that?
> > that could perhaps be the most accurate way to do that? or maybe
> > perhaps have a separate function to just wrap current line?
> >
> > thx alot, appreciate as always your excellent help
>
>
> just wrap your paragraph in parens and call the command with point
> on
> the beginning paren, and it will work - for any number of lines.
> easy to do with package smartparens.el:
>
> mark region and type '(', region is wrapped automatically.
>
>
>
> > On Tue, Jul 29, 2014 at 3:21 PM, Thorsten Jolitz
> <address@hidden>
> > wrote:
> >
> >
> > Xebar Saram <address@hidden> writes:
> >
> > > Hi list
> > >
> > > so i know i should start to really learn lisp but real life
> (uni
> > work,
> > > family etc) doesn't really allow me so im marking my 1st year
> > > anniversary of using emacs without grasping lisp (plus i am in
> > > generally coding inept :))
> > >
> > > in any case.... :) i have this handy lisp snippet that i find
> > very
> > > useful and maybe some others in the list will find as well. it
> > allows
> > > to quick convert lines of text into org src/example blocks:
> > >
> > > (defun z-wrap-cblock-lisp ()
> > > "Wrap region in quote block"
> > > (interactive)
> > > (save-excursion
> > > (save-restriction
> > > (and
> > > (region-active-p)
> > > (use-region-p)
> > > (narrow-to-region (region-beginning) (region-end)))
> > > (goto-char (point-min))
> > > (insert "#+BEGIN_SRC emacs-lisp :results none\n")
> > > (goto-char (point-max))
> > > (insert "#+END_SRC\n")
> > > (deactivate-mark))))
> > >
> > > this works well but has some annoying flaws like you have to
> > first
> > > manually mark the region you want to convert etc
> > >
> > > i was wondering:
> > >
> > > a)can some lisp coding master show me how to make this
> function
> > auto
> > > select the line//X user defined lines/smart paragraph
> selection
> > before
> > > it wraps it in the org block
> > > b)any improvement ideas from the community?
> >
> >
> > Try this:
> >
> > #+begin_src emacs-lisp
> > (defun tj/wrap-sexp-in-elisp-src-block ()
> > "Wrap sexp at point in elisp src block"
> > (interactive)
> > (let* ((marker (point-marker))
> > (beg (point))
> > (end (save-excursion
> > (forward-sexp) (point)))
> > (cut-strg (buffer-substring beg end)))
> > (delete-region beg end)
> > (goto-char (marker-position marker))
> > (insert
> > (format
> > "\n#+begin_src emacs-lisp\n%s\n#+end_src\n"
> > cut-strg))
> > (set-marker marker nil)))
> > #+end_src
> >
> > #+results:
> > : tj/wrap-sexp-in-elisp-src-block
> >
> > MWE:
> >
> > (defun foo ()
> > "do foo"
> > (message "foo"))
> >
> > 1. with point at the opening paren:
> >
> >
> > #+begin_src emacs-lisp
> > (defun foo ()
> > "do foo"
> > (message "foo"))
> > #+end_src
> >
> > 2. with point at the 'd' of defun:
> >
> > (
> > #+begin_src emacs-lisp
> > defun
> > #+end_src
> > foo ()
> > "do foo"
> > (message "foo"))
> >
> > 3. with point between 'do foo':
> >
> > (defun foo ()
> > "do
> > #+begin_src emacs-lisp
> > foo
> > #+end_src
> > "
> > (message "foo"))
> >
> >
> > This is actually quite useful, will add it to my init file
> (since
> > I
> > frequently get 'org-babel-demarcate-block: Args out of range'
> > errors
> > when trying to wrap a region).
> >
> > Note that the behaviour depends a bit on the major-mode. In
> > emacs-lisp
> > mode, a string is seen as one sexp, thus with point at the first
> > double-quote of
> >
> > "hello world"
> >
> > I get
> >
> > #+begin_src emacs-lisp
> > "hello world"
> > #+end_src
> >
> > while in this message-mode buffer I get
> >
> > #+begin_src emacs-lisp
> > "hello
> > #+end_src
> > world"
> >
> > --
> > cheers,
> > Thorsten
> >
> >
> >
> >
>
>
> --
> cheers,
> Thorsten
>
>
--
cheers,
Thorsten
- [O] some lisp help (for complete 0-knowledge lisp emacs user :)), Xebar Saram, 2014/07/29
- Re: [O] some lisp help (for complete 0-knowledge lisp emacs user :)), Thorsten Jolitz, 2014/07/29
- Message not available
- Message not available
- Re: [O] some lisp help (for complete 0-knowledge lisp emacs user :)), Xebar Saram, 2014/07/29
- [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help),
Thorsten Jolitz <=
- Re: [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help), Thorsten Jolitz, 2014/07/29
- Re: [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help), Xebar Saram, 2014/07/29
- Re: [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help), Thorsten Jolitz, 2014/07/29
- Re: [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help), Xebar Saram, 2014/07/29
- Re: [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help), Xebar Saram, 2014/07/29
- Re: [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help), Xebar Saram, 2014/07/29
- Re: [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help), Thorsten Jolitz, 2014/07/29