From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thorsten Jolitz Subject: Re: some lisp help (for complete 0-knowledge lisp emacs user :)) Date: Tue, 29 Jul 2014 14:21:32 +0200 Message-ID: <87d2cos6cz.fsf@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:36936) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XC6Pr-00045R-PU for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 08:22:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XC6Pj-0004cV-BN for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 08:22:03 -0400 Received: from plane.gmane.org ([80.91.229.3]:33898) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XC6Pj-0004cR-3x for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 08:21:55 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XC6Ph-0008OY-GK for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 14:21:53 +0200 Received: from e178189064.adsl.alicedsl.de ([85.178.189.64]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 29 Jul 2014 14:21:53 +0200 Received: from tjolitz by e178189064.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 29 Jul 2014 14:21:53 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Xebar Saram 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