1. Nicolas M. Thiéry
  2. sage-mode

Commits

Ivan Andrus  committed 6e88039 Merge

Merged in jsrnielsen/sage-mode-jsrn/sage-blocks (pull request #4)

Print "titles" of blocks in the inferior shell when they are sent

  • Participants
  • Parent commits de70707, 246877e
  • Branches default

Comments (0)

Files changed (1)

File emacs/sage-blocks.el

View file
 ;; A block is defined by a line beginning with `sage-block-delimiter'.
 
 ;;; Code:
-
 (defcustom sage-block-delimiter "###"
   "Any line matching the regular expression `sage-block-delimiter' at the
   beginning of the line is considered a start of a block.
   :type 'string
   :group 'sage)
 
+(defcustom sage-block-title-decorate " ---- "
+  "When printing titles of blocks, put this decoration around the
+title for easy recognition"
+  :type 'string
+  :group 'sage)
+
 ;;
 ;; Functionality for Sage source files
 ;;
   ;; Border-case: if we're standing on a delimiter, sage-backward-block will go
   ;; to previous delimiter, but we should send from this delimiter and forwards.
   (sage-forward-block 1)
-  (let ((p (point)))
-    (sage-backward-block 1)
-    (sage-send-region (point) p)
-    (goto-char p)))
-
+  (let ((enddelim (point)))
+    (save-excursion
+      (sage-backward-block 1)
+      (setq backdelim (point)))
+    (let ((this-buf (current-buffer)))
+      ;; Copy the region to a temp buffer.
+      ;; Possibly change the first line if it contains a title
+      (with-temp-buffer
+	(insert-buffer-substring this-buf backdelim enddelim)
+	(goto-char (point-min))
+	(when (looking-at sage-block-delimiter)
+	  (progn
+	    (goto-char (match-end 0))
+	    (setq title (buffer-substring (point)
+					  (progn (end-of-line) (point))))
+	    (when (string-match "^ *\\([^ ].*[^ ]\\) *$" title)
+	      (setq title (match-string 1 title)))
+	    (unless (equal title "")
+	      (insert (concat "\nprint(\"" sage-block-title-decorate title sage-block-title-decorate "\")")))))
+	(sage-send-region (point-min) (point-max)))))
+    )
 
 (defun sage-blocks-default-keybindings ()
   "Bind default keys for working with sage blocks.