From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jambunathan K Subject: Re: [BABEL] [PROPOSAL] Seemless editing of Babel Blocks Date: Mon, 16 Aug 2010 14:50:23 +0530 Message-ID: <8139ueykvc.fsf_-_@gmail.com> References: <4C459236.3@gmail.com> <87k4opu5fk.fsf@gmail.com> <81hbivx88y.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from [140.186.70.92] (port=48878 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Okvs5-00008B-75 for emacs-orgmode@gnu.org; Mon, 16 Aug 2010 05:20:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Okvry-0003wU-7V for emacs-orgmode@gnu.org; Mon, 16 Aug 2010 05:20:45 -0400 Received: from mail-pw0-f41.google.com ([209.85.160.41]:53049) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Okvrx-0003wP-TC for emacs-orgmode@gnu.org; Mon, 16 Aug 2010 05:20:38 -0400 Received: by pwi3 with SMTP id 3so1889910pwi.0 for ; Mon, 16 Aug 2010 02:20:37 -0700 (PDT) In-Reply-To: <81hbivx88y.fsf@gmail.com> (Jambunathan K.'s message of "Mon, 16 Aug 2010 14:08:21 +0530") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Eric Schulte Cc: emacs-orgmode@gnu.org 1 Preface ~~~~~~~~~~ Continuing on my earlier note "Offer surrounding context in org-src buffers", I have another idea. The idea is a bit fanciful and questionable. I also have a prototype code that is quite gross. (Don't say that I haven't warned) 2 Motivation ~~~~~~~~~~~~~ The primary motivation is that I find transitioning from and to an org-src buffer a bit of a psychological drag. It would be good if I don't leave the current buffer as such and still be able to edit babel blocks. 3 Setting the context ~~~~~~~~~~~~~~~~~~~~~~ I believe when I am doing literate programming, I am likely to work in phases. Note: I haven't done literate programming in even lighter sense of the word. My understanding down below could be naive and may not be as nuanced as that of a practitioner. 3.1 Phase-1 ============ Fix up the overall design/structure. Here I am likely to work with a text file (orgmode buffer in our case). 3.2 Phase-2: ============= Churn out code by filling out top-level blocks identified in Phase-1. Here I am likely to work with a code file (org-src buffer in our case). 3.3 Phase-3 ============ Compile, Test and Fix. Here I am likely to switch quite frequently between the org and org-src blocks. I believe good amount of time would be spent in Phase-3. It is in this phase that offering of 'surrounding context' becomes crucial in org-src buffers. (Here is an item to the WISH list) 4 Crux of the idea ~~~~~~~~~~~~~~~~~~~ Think of an org buffer as a chain of alternating src and non-src blocks. org-buffer = [non-src, src]+ More specifically, when there is a mix of say python and emacs-lisp blocks and if I have my emacs-lisp goggles on, I could subsume python blocks as non-src blocks. Offer a command say 'org-to-org-src-view' which when invoked switches the org-mode buffer to target language mode and comments out all the non-src blocks. Offer a reverse command 'org-src-to-org-view' that switches the buffer to org-mode and uncomments the non-src blocks. This way I never leave the buffer and I don't have to contend anymore with pop-ups (and importantly syncing of parent and daughter buffers). Yet I have 'all' the contexts available for my viewing and editing pleasure. 5 Gross code ~~~~~~~~~~~~~ diff --git a/lisp/ob.el b/lisp/ob.el index b5b9d8f..613139e 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -662,19 +662,52 @@ portions of results lines." (lambda () (org-add-hook 'change-major-mode-hook 'org-babel-show-result-all 'append 'local))) -(defmacro org-babel-map-src-blocks (file &rest body) + +(defun org-to-org-src-view () + "" + (interactive) + + (emacs-lisp-mode) + (org-babel-map-src-blocks (buffer-file-name) + ( + (comment-region beg-org end-org) + ) + ) + ) + +(defmacro org-babel-map-src-blocks (file body1 &rest body) "Evaluate BODY forms on each source-block in FILE." (declare (indent 1)) `(let ((visited-p (get-file-buffer (expand-file-name ,file))) - to-be-removed) + to-be-removed + (beg-org (make-marker)) + (end-org (make-marker)) + (beg-babel (make-marker)) + (end-babel (make-marker)) + ) + (save-window-excursion (find-file ,file) (setq to-be-removed (current-buffer)) + + (move-marker end-babel (point-min)) (goto-char (point-min)) + (while (re-search-forward org-babel-src-block-regexp nil t) - (goto-char (match-beginning 0)) - (save-match-data ,@body) - (goto-char (match-end 0)))) + + (move-marker beg-org end-babel) + (move-marker end-org (match-beginning 0)) + (move-marker beg-babel (match-beginning 0)) + (move-marker end-babel (match-end 0)) + + (goto-char beg-org) + ,@body1 + + (goto-char beg-babel) + ,@body + + + (goto-char end-babel))) (unless visited-p (kill-buffer to-be-removed)))) 6 Illustration ~~~~~~~~~~~~~~~ For the sake of illustration, consider an org-mode buffer like this. * Heading0 ** Heading00 Print Heading00. #+begin_src emacs-lisp (message "Heading00") #+end_src ** Heading01 Print Heading01. #+begin_src emacs-lisp (message "Heading01") #+end_src org-to-org-src-view on this buffer puts it in emacs-lisp mode with the following content. ;; * Heading0 ;; ** Heading00 ;; Print Heading00. #+begin_src emacs-lisp (message "Heading00") #+end_src ;; ** Heading01 ;; Print Heading01. #+begin_src emacs-lisp (message "Heading01") #+end_src For the sake of brevity, I have left out the commenting of meta-lines in the prototype code. Thanks for your consideration, Jambunathan K.