Browse Source

Xemacs incompatibilities

Carsten Dominik <carsten.dominik@gmail.com> writes:

> On May 17, 2010, at 4:39 PM, Michael Sperber wrote:
>
>> In particular, fixing the require won't be enough: org-babel-python.el
>> uses `run-python' and interacts with the inferior Python, whereas
>> python-mode.el defines `py-shell'.
>>
>> Should I try to abstract over the differences?
>
> Yes, this would be much appreciated. (I think, Eric or Dan?)

OK, I've attached a patch that makes `org-babel-python' work on XEmacs.
Most of the issues are pure XEmacs issues.  Notes:

- XEmacs doesn't have [[:digit:]] - I hope to rectify this in the
  future, but it seems there's no downside in this particular case to
  replacing by [0-9].
- XEmacs doesn't have `move-end-of-line', but does have `end-of-line'.
  I don't understand the intent of having both of these, but the code
  seems fine with `end-of-line'.
- It seems there are way too few `require's throughout org-babel.  I
  don't know if it's OK to add the ones I needed.
- `org-babel-python-evaluate' looked broken as-is: It doesn't use the
  `body' argument properly, the result is (I think) processed in the
  wrong order and not properly split into lines.  I've fixed all these,
  but a review is probably in order.
Michael Sperber 15 years ago
parent
commit
6fc2ea3b9b

+ 1 - 1
contrib/babel/lisp/langs/org-babel-R.el

@@ -89,7 +89,7 @@ called by `org-babel-execute-src-block'."
 	   vars)))
 	   vars)))
     (org-babel-comint-in-buffer session
     (org-babel-comint-in-buffer session
       (mapc (lambda (var)
       (mapc (lambda (var)
-              (move-end-of-line 1) (insert var) (comint-send-input nil t)
+              (end-of-line 1) (insert var) (comint-send-input nil t)
               (org-babel-comint-wait-for-output session)) var-lines))
               (org-babel-comint-wait-for-output session)) var-lines))
     session))
     session))
 
 

+ 1 - 1
contrib/babel/lisp/langs/org-babel-octave.el

@@ -92,7 +92,7 @@ specifying a variable of the same value."
                      vars)))
                      vars)))
     (org-babel-comint-in-buffer session
     (org-babel-comint-in-buffer session
       (mapc (lambda (var)
       (mapc (lambda (var)
-              (move-end-of-line 1) (insert var) (comint-send-input nil t)
+              (end-of-line 1) (insert var) (comint-send-input nil t)
               (org-babel-comint-wait-for-output session)) var-lines))
               (org-babel-comint-wait-for-output session)) var-lines))
     session))
     session))
 
 

+ 34 - 13
contrib/babel/lisp/langs/org-babel-python.el

@@ -30,6 +30,8 @@
 
 
 ;;; Code:
 ;;; Code:
 (require 'org-babel)
 (require 'org-babel)
+(require 'org-babel-tangle)
+(require 'org-babel-comint)
 (require (if (featurep 'xemacs) 'python-mode 'python))
 (require (if (featurep 'xemacs) 'python-mode 'python))
 
 
 (org-babel-add-interpreter "python")
 (org-babel-add-interpreter "python")
@@ -75,7 +77,7 @@ called by `org-babel-execute-src-block'."
                      vars)))
                      vars)))
     (org-babel-comint-in-buffer session
     (org-babel-comint-in-buffer session
       (mapc (lambda (var)
       (mapc (lambda (var)
-              (move-end-of-line 1) (insert var) (comint-send-input nil t)
+              (end-of-line 1) (insert var) (comint-send-input)
               (org-babel-comint-wait-for-output session)) var-lines))
               (org-babel-comint-wait-for-output session)) var-lines))
     session))
     session))
 
 
@@ -123,7 +125,21 @@ then create.  Return the initialized session."
   (save-window-excursion
   (save-window-excursion
     (let* ((session (if session (intern session) :default))
     (let* ((session (if session (intern session) :default))
            (python-buffer (org-babel-python-session-buffer session)))
            (python-buffer (org-babel-python-session-buffer session)))
-      (run-python)
+      (cond
+       ((fboundp 'run-python) ; python.el
+	(run-python))
+       ((fboundp 'py-shell) ; python-mode.el
+	;; `py-shell' creates a buffer whose name is the value of
+	;; `py-which-bufname' with '*'s at the beginning and end
+	(let* ((bufname (if python-buffer
+			    (replace-regexp-in-string "^\\*\\([^*]+\\)\\*$" "\\1" python-buffer) ; zap surrounding *
+			  (concat "Python-" (symbol-name session))))
+	       (py-which-bufname bufname)) ; avoid making a mess with buffer-local
+	  (py-shell)
+	  (setq python-buffer (concat "*" bufname "*"))))
+       (t
+	(error "No function available for running an inferior python.")))
+	
       (setq org-babel-python-buffers (cons (cons session python-buffer)
       (setq org-babel-python-buffers (cons (cons session python-buffer)
 					   (assq-delete-all session org-babel-python-buffers)))
 					   (assq-delete-all session org-babel-python-buffers)))
       session)))
       session)))
@@ -200,19 +216,24 @@ last statement in BODY, as elisp."
     (org-babel-comint-in-buffer buffer
     (org-babel-comint-in-buffer buffer
       (let* ((raw (org-babel-comint-with-output buffer org-babel-python-eoe-indicator t
       (let* ((raw (org-babel-comint-with-output buffer org-babel-python-eoe-indicator t
                     ;; for some reason python is fussy, and likes enters after every input
                     ;; for some reason python is fussy, and likes enters after every input
-                    (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
-                          (split-string (org-babel-trim full-body) "[\r\n]+"))
-                    (comint-send-input nil t) (comint-send-input nil t)
-                    (if (member "pp" result-params)
-                        (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
-                              org-babel-python-pp-last-value-eval)
-                      (insert org-babel-python-last-value-eval))
-                    (comint-send-input nil t) (comint-send-input nil t)
-                    (insert org-babel-python-eoe-indicator)
-                    (comint-send-input nil t)))
+		    (let ((comint-process-echoes nil))
+		      (mapc (lambda (statement) (insert statement) (comint-send-input))
+			    (split-string (org-babel-trim body) "[\r\n]+"))
+		      (comint-send-input) (comint-send-input)
+		      (if (member "pp" result-params)
+			  (mapc (lambda (statement) (insert statement) (comint-send-input))
+				org-babel-python-pp-last-value-eval)
+			(insert org-babel-python-last-value-eval))
+		      (comint-send-input) (comint-send-input)
+		      (insert org-babel-python-eoe-indicator)
+		      (comint-send-input))))
+	     (raw (apply #'append ; split further
+			 (mapcar #'(lambda (r)
+				     (split-string r "[\r\n]+"))
+				 raw)))
              (results (delete org-babel-python-eoe-indicator
              (results (delete org-babel-python-eoe-indicator
                               (cdr (member org-babel-python-eoe-indicator
                               (cdr (member org-babel-python-eoe-indicator
-                                           (reverse (mapcar #'org-babel-trim raw)))))))
+                                           (mapcar #'org-babel-trim raw))))))
         (unless (or (member "code" result-params) (member "pp" result-params))
         (unless (or (member "code" result-params) (member "pp" result-params))
           (setq results (mapcar #'org-babel-python-read-string results)))
           (setq results (mapcar #'org-babel-python-read-string results)))
         (case result-type
         (case result-type

+ 1 - 1
contrib/babel/lisp/org-babel-lob.el

@@ -90,7 +90,7 @@ should be renamed to bring out this similarity, perhaps involving
 the word 'call'."
 the word 'call'."
   (let ((case-fold-search t))
   (let ((case-fold-search t))
     (save-excursion
     (save-excursion
-      (move-beginning-of-line 1)
+      (beginning-of-line 1)
       (if (looking-at org-babel-lob-one-liner-regexp)
       (if (looking-at org-babel-lob-one-liner-regexp)
           (mapcar #'org-babel-clean-text-properties 
           (mapcar #'org-babel-clean-text-properties 
 		  (list (format "%s(%s)" (match-string 1) (match-string 2))
 		  (list (format "%s(%s)" (match-string 1) (match-string 2))

+ 3 - 3
contrib/babel/lisp/org-babel-tangle.el

@@ -160,8 +160,8 @@ references."
   (goto-char (point-min))
   (goto-char (point-min))
   (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
   (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
              (re-search-forward "<<[^[:space:]]*>>" nil t))
              (re-search-forward "<<[^[:space:]]*>>" nil t))
-    (delete-region (save-excursion (move-beginning-of-line 1) (point))
-                   (save-excursion (move-end-of-line 1) (forward-char 1) (point)))))
+    (delete-region (save-excursion (beginning-of-line 1) (point))
+                   (save-excursion (end-of-line 1) (forward-char 1) (point)))))
 
 
 (defun org-babel-tangle-collect-blocks (&optional lang)
 (defun org-babel-tangle-collect-blocks (&optional lang)
   "Collect all source blocks in the current org-mode file.
   "Collect all source blocks in the current org-mode file.
@@ -221,7 +221,7 @@ form
                          (when commentable
                          (when commentable
                            (insert "\n")
                            (insert "\n")
                            (comment-region (point) (progn (insert text) (point)))
                            (comment-region (point) (progn (insert text) (point)))
-                           (move-end-of-line nil)
+                           (end-of-line nil)
                            (insert "\n"))))
                            (insert "\n"))))
     (let ((link (first spec))
     (let ((link (first spec))
           (source-name (second spec))
           (source-name (second spec))

+ 13 - 13
contrib/babel/lisp/org-babel.el

@@ -246,7 +246,7 @@ block."
           (if (and (not arg) new-hash (equal new-hash old-hash))
           (if (and (not arg) new-hash (equal new-hash old-hash))
               (save-excursion ;; return cached result
               (save-excursion ;; return cached result
                 (goto-char (org-babel-where-is-src-block-result nil info))
                 (goto-char (org-babel-where-is-src-block-result nil info))
-                (move-end-of-line 1) (forward-char 1)
+                (end-of-line 1) (forward-char 1)
                 (setq result (org-babel-read-result))
                 (setq result (org-babel-read-result))
                 (message (replace-regexp-in-string "%" "%%"
                 (message (replace-regexp-in-string "%" "%%"
                                                    (format "%S" result))) result)
                                                    (format "%S" result))) result)
@@ -305,7 +305,7 @@ session.  After loading the body this pops open the session."
     (pop-to-buffer
     (pop-to-buffer
      (funcall (intern (concat "org-babel-load-session:" lang))
      (funcall (intern (concat "org-babel-load-session:" lang))
               session body params))
               session body params))
-    (move-end-of-line 1)))
+    (end-of-line 1)))
 
 
 (defun org-babel-switch-to-session (&optional arg info)
 (defun org-babel-switch-to-session (&optional arg info)
   "Switch to the session of the current source-code block.
   "Switch to the session of the current source-code block.
@@ -333,7 +333,7 @@ of the source block to the kill ring."
     (pop-to-buffer
     (pop-to-buffer
      (funcall (intern (format "org-babel-%s-initiate-session" lang))
      (funcall (intern (format "org-babel-%s-initiate-session" lang))
               session params))
               session params))
-    (move-end-of-line 1)))
+    (end-of-line 1)))
 
 
 (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
 (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
 
 
@@ -349,7 +349,7 @@ results already exist."
       (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
       (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
                      (progn (org-babel-execute-src-block)
                      (progn (org-babel-execute-src-block)
                             (org-babel-where-is-src-block-result))))
                             (org-babel-where-is-src-block-result))))
-      (move-end-of-line 1) (forward-char 1)
+      (end-of-line 1) (forward-char 1)
       ;; open the results
       ;; open the results
       (if (looking-at org-bracket-link-regexp)
       (if (looking-at org-bracket-link-regexp)
           ;; file results
           ;; file results
@@ -768,8 +768,8 @@ If the point is not on a source block then return nil."
         (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point))
         (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point))
         (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point))
         (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point))
         (< top initial) (< initial bottom)
         (< top initial) (< initial bottom)
-        (goto-char top) (move-beginning-of-line 1)
-        (looking-at org-babel-src-block-regexp)
+        (progn (goto-char top) (beginning-of-line 1)
+	       (looking-at org-babel-src-block-regexp))
         (point))))))
         (point))))))
 
 
 (defun org-babel-goto-named-source-block (&optional name)
 (defun org-babel-goto-named-source-block (&optional name)
@@ -802,7 +802,7 @@ buffer or nil if no such result exists."
     (when (re-search-forward
     (when (re-search-forward
            (concat org-babel-result-regexp
            (concat org-babel-result-regexp
                    "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
                    "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
-      (move-beginning-of-line 0) (point))))
+      (beginning-of-line 0) (point))))
 
 
 (defun org-babel-where-is-src-block-result (&optional insert info hash)
 (defun org-babel-where-is-src-block-result (&optional insert info hash)
   "Return the point at the beginning of the result of the current
   "Return the point at the beginning of the result of the current
@@ -818,13 +818,13 @@ following the source block."
       (when head (goto-char head))
       (when head (goto-char head))
       (or (and name (org-babel-find-named-result name))
       (or (and name (org-babel-find-named-result name))
           (and (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
           (and (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
-               (progn (move-end-of-line 1)
+               (progn (end-of-line 1)
 		      (if (eobp) (insert "\n") (forward-char 1))
 		      (if (eobp) (insert "\n") (forward-char 1))
 		      (setq end (point))
 		      (setq end (point))
                       (or (and (not name)
                       (or (and (not name)
 			       (progn ;; unnamed results line already exists
 			       (progn ;; unnamed results line already exists
 				 (re-search-forward "[^ \f\t\n\r\v]" nil t)
 				 (re-search-forward "[^ \f\t\n\r\v]" nil t)
-				 (move-beginning-of-line 1)
+				 (beginning-of-line 1)
                                  (looking-at
                                  (looking-at
                                   (concat org-babel-result-regexp "\n"))))
                                   (concat org-babel-result-regexp "\n"))))
 			  ;; or (with optional insert) back up and
 			  ;; or (with optional insert) back up and
@@ -837,7 +837,7 @@ following the source block."
                                             (when hash (concat "["hash"]"))
                                             (when hash (concat "["hash"]"))
                                             ":"
                                             ":"
                                             (when name (concat " " name)) "\n"))
                                             (when name (concat " " name)) "\n"))
-                            (move-beginning-of-line 0)
+                            (beginning-of-line 0)
                             (if hash (org-babel-hide-hash)) t)))
                             (if hash (org-babel-hide-hash)) t)))
                (point))))))
                (point))))))
 
 
@@ -1036,7 +1036,7 @@ directory then expand relative links."
 	    ((< size org-babel-min-lines-for-block-output)
 	    ((< size org-babel-min-lines-for-block-output)
 	     (goto-char beg)
 	     (goto-char beg)
 	     (dotimes (n size)
 	     (dotimes (n size)
-	       (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
+	       (beginning-of-line 1) (insert ": ") (forward-line 1)))
 	    (t
 	    (t
 	     (goto-char beg)
 	     (goto-char beg)
 	     (insert (if results-switches
 	     (insert (if results-switches
@@ -1193,7 +1193,7 @@ block but are passed literally to the \"example-block\"."
             (setq prefix
             (setq prefix
                   (buffer-substring (match-beginning 0)
                   (buffer-substring (match-beginning 0)
                                     (save-excursion
                                     (save-excursion
-                                      (move-beginning-of-line 1) (point)))))
+                                      (beginning-of-line 1) (point)))))
           ;; add interval to new-body (removing noweb reference)
           ;; add interval to new-body (removing noweb reference)
           (goto-char (match-beginning 0))
           (goto-char (match-beginning 0))
           (nb-add (buffer-substring index (point)))
           (nb-add (buffer-substring index (point)))
@@ -1261,7 +1261,7 @@ This is taken almost directly from `org-read-prop'."
 
 
 (defun org-babel-number-p (string)
 (defun org-babel-number-p (string)
   "Return t if STRING represents a number"
   "Return t if STRING represents a number"
-  (if (and (string-match "^-?[[:digit:]]*\\.?[[:digit:]]*$" string)
+  (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string)
            (= (match-end 0) (length string)))
            (= (match-end 0) (length string)))
       (string-to-number string)))
       (string-to-number string)))