[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-dev] additions to ScelDocument and EmacsDocument classes



I am working on adding some functionality to the Document class under scel.

This patch moves the quoted lisp code out of the EmacsDocument.string method and into the emacs source, where it can be more fluently edited and they will be compiled for a slight performance improvement.

I also implemented ScelDocument.selectRange and ScelDocument.selectedText. selectRange I think will work exactly as expected, (it only highlights the text if you have transient mark mode turned on though, I figured this was a good concession to standard emacs culture). selectedText has the same issue that string does of needing to be asynchronous, but however inconvenient the method is now there.

(brainstorm regarding async methods: what about using dbus - emacs would push the current region (the emacs analog to selection) as it changes in any sclang mode documents. This would not be generally usable until the mainstream distros are shipping emacs23 and compiling with dbus turned on by default.

Another (probably easier) option would be to use a shared struct in /dev/shm.

I will hopefully be getting to setFont, background, background_, stringColor_, setTextColor and setBackgroundColor after I finish with the range/region related functionality.

Thanks to Marije, Dan, and Stefan for your help with my questions about scel.

I also have attached a more comprehensive svg map of scel.
Index: editors/scel/el/sclang-mode.el
===================================================================
--- editors/scel/el/sclang-mode.el	(revision 9641)
+++ editors/scel/el/sclang-mode.el	(working copy)
@@ -594,14 +594,13 @@
 
 (sclang-set-command-handler
  '_documentPutString
-(lambda (arg)
+ (lambda (arg)
    (multiple-value-bind (id str) arg
      (let ((doc (and (integerp id) (sclang-get-document id))))
        (when doc
 	 (with-current-buffer doc
-	 (insert str)
-	 )
-       nil)))))
+	   (insert str))
+	 nil)))))
 
 (sclang-set-command-handler
  '_documentPopTo
@@ -610,6 +609,54 @@
      (and doc (display-buffer doc)))
    nil))
 
+(sclang-set-command-handler
+ '_string
+ (lambda (arg)
+  (multiple-value-bind (id range-start range-size) arg
+    (let ((doc (and (integerp id) (sclang-get-document id)))
+	  (range-end (+ range-start range-size)))
+      (unless doc
+	(error "invalid document id from sclang in _string handler"))
+      (when doc
+	(with-current-buffer doc
+	  (mapc (lambda (sym)
+		  (when (< (point-max) (symbol-value sym))
+		    (set sym (point-max)))
+		  (when (> 1 (symbol-value sym))
+		    (set sym 1)))
+		'(range-start range-end))
+	  (buffer-substring-no-properties range-start range-end)))))))
+
+(sclang-set-command-handler
+ '_selectRange
+ (lambda (arg)
+   (multiple-value-bind (id range-start range-size) arg
+     (let ((doc (and (integerp id) (sclang-get-document id)))
+	   (range-end (+ range-start range-size)))
+       (unless doc
+	 (error "invalid document id from sclang in _selectRange handler"))
+       (when doc
+	 (switch-to-buffer (get-buffer doc))
+	 (mapc (lambda (sym)
+		 (when (> (symbol-value sym) (point-max))
+		   (set sym (point-max)))
+		 (when (< (symbol-value sym) 1)
+		   (set sym 1)))
+	       '(range-start range-end))
+	 (push-mark range-start t t)
+	 (goto-char range-end)
+	 nil)))))
+
+(sclang-set-command-handler
+ '_selectedText
+ (lambda (arg)
+   (let ((doc (and (integerp arg) (sclang-get-document arg))))
+     (unless doc
+       (error "invalid document id from sclang in _selectedText handler"))
+     (with-current-buffer (get-buffer doc)
+       (buffer-substring-no-properties (point) (mark))))))
+
+
 ;; =====================================================================
 ;; sclang-mode
 ;; =====================================================================
Index: editors/scel/sc/ScelDocument.sc
===================================================================
--- editors/scel/sc/ScelDocument.sc	(revision 9641)
+++ editors/scel/sc/ScelDocument.sc	(working copy)
@@ -295,20 +295,27 @@
 		^this.string( rangestart, rangesize );
 	}
 
+	selectRange { | start=0, length=0 |
+		thisdoc.selectRange( start, length );
+		^nil;
+	}
+
+	selectedText {
+		currentString = nil;
+		thisdoc.selectedText( { |v| currentString = v } );
+		"Asynchronous: retrieve the result with .currentString".postln;
+		^nil
+	}
+
 	// not implemented:
-	selectRange { arg start=0, length=0; }
 	background_ {arg color, rangestart= -1, rangesize = 0;
 	}
-	stringColor_ {arg color, rangeStart = -1, rangeSize = 0;
-	}
 
+	stringColor_ {arg color, rangeStart = -1, rangeSize = 0;}
 	prGetBounds { | bounds | ^bounds }
 	prSetBounds { }
 	setFont { }
 	setTextColor { }
-	selectedText {
-		^""
-	}
 	prinsertText { arg dataptr, txt;
 	}
 	insertTextRange { arg string, rangestart, rangesize;
Index: editors/scel/sc/EmacsDocument.sc
===================================================================
--- editors/scel/sc/EmacsDocument.sc	(revision 9641)
+++ editors/scel/sc/EmacsDocument.sc	(working copy)
@@ -125,8 +125,8 @@
 		Emacs.sendToLisp(\_documentSyntaxColorize, this);
 	}
 
-	selectRange { arg start=0, length=0;
-		//_TextWindow_SelectRange
+	selectRange { | start = 0, length = 0 |
+		Emacs.sendToLisp(\_selectRange, [this, start, length]);
 	}
 	prisEditable_{ | flag = true |
 		Emacs.sendToLisp(\_documentSetEditable, [this, flag]);
@@ -135,16 +135,12 @@
 		Emacs.sendToLisp(\_documentRemoveUndo, this);
 	}
 
-	string{ arg rangestart, returnFunc, rangesize = 1;
-		var rangeend, resultString;
-		if ( rangestart.isNil,{
-			rangestart = '(point-min)';
-			rangeend = '(point-max)';
-		},{
-			rangeend = rangestart + rangesize;
-		});
-		Emacs.evalLispExpression(['with-current-buffer', title, [ 'buffer-substring-no-properties', rangestart, rangeend ]].asLispString;, { |result| returnFunc.value( result ); } );
-		^nil;
+	string { | rangestart, returnFunc, rangesize = 1 |
+	  Emacs.sendToLisp(\_string,
+		[this, rangestart, rangesize],
+		{|result| returnFunc.value( result )}
+	  );
+	  ^nil;
 	}
 
 	string_{|string, rangestart = -1, rangesize = 1|
@@ -273,6 +269,13 @@
 
 	*prBasicNew { ^super.new }
 
+	selectedText { | returnFunc |
+		Emacs.sendToLisp(\_selectedText, this,
+			{|result| returnFunc.value( result )}
+		);
+		^nil;
+	}
+
 	// unimplemented methods
 /*
 	prGetBounds { | bounds | ^bounds }
@@ -282,9 +285,6 @@
 	text {
 		^""
 	}
-	selectedText {
-		^""
-	}
 	rangeText { arg rangestart=0, rangesize=1;
 		^""
 	}

image/svg