From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thorsten Jolitz Subject: [RFC] Add commmand for wrapping sexp/region in src-blocks to Org? Date: Tue, 29 Jul 2014 17:36:34 +0200 Message-ID: <87k36wqirh.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:51552) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XC9SV-0007Q9-Cr for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 11:37:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XC9SM-0001wa-NZ for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 11:36:59 -0400 Received: from plane.gmane.org ([80.91.229.3]:33635) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XC9SM-0001wQ-Go for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 11:36:50 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XC9SJ-0006iA-EZ for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 17:36:47 +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 17:36:47 +0200 Received: from tjolitz by e178189064.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 29 Jul 2014 17:36:47 +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 Hi List, I often missed the following command for wrapping existing content into src-blocks in the past, besides knowing about `org-babel-demarcate-block' and easy template insertion. Would it make sense to add it to Org-mode? I made it cover several use cases: - Wrap sexp at point - perfect when on first paren of a nested list, or when a string should be wrapped in a programming-mode, or a word in a text-mode. - Wrap region between point and +/- N lines forward/backward (very fast because no point movement is involved). - Wrap active region. Usage: 1. wrap active region or (if none) sexp-at-point in emacs-lisp src-block ,---- | M-x org-wrap-in-src-block `---- 2. wrap active region or (if none) sexp-at-point in src-block, prompt user for Org-Babel language ,---- | C-u M-x org-wrap-in-src-block `---- 3. prompt user for Org-Babel language and number of lines to wrap (forward or backward) ,---- | C-u C-u M-x org-wrap-in-src-block `---- #+begin_src emacs-lisp (defun org-wrap-in-src-block (&optional lang lines) "Wrap sexp-at-point or region in src-block. Use Org-Babel LANGuage for the src-block if given, Emacs-Lisp otherwise. A region instead of the sexp-at-point is wrapped if either - optional argument LINES is an (positive or negative) integer - or the region is active In the first case the region is determined by moving +/- LINES forward/backward from point using `forward-line', in the second case the active region is used. When called with prefix argument 'C-u', prompt the user for the Org-Babel language to use. When called with two prefix arguments 'C-u C-u', prompt the user for both the Org-Babel language to use and the number of lines to be wrapped." (interactive (cond ((equal current-prefix-arg nil) nil) ((equal current-prefix-arg '(4)) (list (ido-completing-read "Org-Babel language: " (mapcar (lambda (--lang) (symbol-name (car --lang))) org-babel-load-languages) nil nil nil nil "emacs-lisp"))) ((equal current-prefix-arg '(16)) (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")) (beg (or (and (not lines) (region-active-p) (region-beginning)) (point))) (marker (save-excursion (goto-char beg) (point-marker))) (bol (save-excursion (goto-char beg) (bolp))) (end (cond (lines (save-excursion (forward-line lines) (point))) ((region-active-p)(region-end)) (t (save-excursion (forward-sexp) (point))))) (cut-strg (buffer-substring beg end))) (delete-region beg end) (goto-char (marker-position marker)) (insert (format "%s#+begin_src %s\n%s%s#+end_src\n" (if (or (and lines (< lines 0)) bol) "" "\n") language cut-strg (if lines "" "\n"))) (set-marker marker nil))) #+end_src -- cheers, Thorsten