From 1f75240b42b499f08d8bea996ccdef4595d9d101 Mon Sep 17 00:00:00 2001
From: BlackLight <blacklight@autistici.org>
Date: Wed, 9 Feb 2011 18:16:44 +0100
Subject: [PATCH] Improving nano editor (thanks to tragic0mic)

---
 commands/nano.json | 92 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 72 insertions(+), 20 deletions(-)

diff --git a/commands/nano.json b/commands/nano.json
index 83eda06..44e6e5a 100644
--- a/commands/nano.json
+++ b/commands/nano.json
@@ -68,9 +68,11 @@
 		
 		shell.fname = arg;
 		shell.editorkeypressed = this.editorkeypressed;
+		shell.editorpaste = this.editorpaste;
+		shell.editorclick = this.editorclick;
 		shell.confirmkey = this.confirmkey;
 		shell.bufferSave = this.bufferSave;
-		shell.firstKey = true;  // Set when no key has been pressed yet
+		shell.firstKey = 1;  // Set when no key has been pressed yet
 
 		shell.default_editor_status = "[<b>^X</b> Exit] [<b>^O</b> WriteOut] [<b>^F</b> Where Is]";
 
@@ -105,9 +107,13 @@
 		shell.editor_head = document.getElementById ( 'editor_head' );
 
 		var editor = document.createElement ( 'textarea' );
+
 		editor.setAttribute ( 'class', 'editor_window' );
 		editor.setAttribute ( 'id', 'editor_window' );
 		editor.setAttribute ( 'onkeypress', 'shell.editorkeypressed ( event )' );
+		// editor.setAttribute ( 'onmousedown', 'shell.editorclick ( event )' ); /* This might be useful someday */
+		editor.setAttribute ( 'onpaste', 'shell.editorpaste ( )' );
+		editor.setAttribute ( 'oncut', 'shell.editorpaste( )' );
 
 		has_content = false;
 
@@ -117,7 +123,7 @@
 			{
 				if ( file.content.length > 0 )
 				{
-					var content = file.content.replace ( /<br\/?>/g, "\n" );
+					var content = file.content.replace ( /(<br ?\/?>)/g, "\n" );
 					content = content.replace ( /&lt;/g, '<' );
 					content = content.replace ( /&gt;/g, '>' );
 					editor.value = content;
@@ -146,29 +152,44 @@
 
 		container.appendChild ( status );
 		shell.editor_status = document.getElementById ( 'editor_status' );
+		shell.hitCount = 0;
 
 		return '';
 	},
 
 	"editorkeypressed" : function ( e )
 	{
+			  shell.hitCount++;
 		var evt = ( window.event ) ? window.event : e;
 		var key = ( evt.charCode ) ? evt.charCode : evt.keyCode;
 
-		if ( shell.firstKey && key == 13 )
+		if ( shell.firstKey == 1 && key == 13 )
 		{
 			evt.preventDefault();
-			shell.firstKey = false;
+			shell.firstKey = -1;
 			return false;
 		}
 
+		if(shell.firstKey == -1)
+		{
+				  shell.file_changed = true;
+				  if(shell.perms.write == false)
+							 shell.editor_status.innerHTML += " [read-only]";
+				  shell.editor_status.innerHTML += " [modified]";
+				  shell.firstKey = 0;
+				  shell.editor_window.value = shell.editor_window.value.replace(/(\n|\r)+$/, '');
+		}
+
 		key = String.fromCharCode ( key );
 
 		if (( key == 'x' || key == 'X' ) && evt.ctrlKey )
 		{
 			evt.preventDefault();
 
-			if ( shell.file_changed )
+			if(shell.originalContent != shell.editor_window.value)
+					  shell.file_changed = true;
+
+			if ( shell.file_changed && shell.hitCount > 2)
 			{
 				var can_write = false;
 
@@ -203,6 +224,9 @@
 		} else if (( key == 'o' || key == 'O' ) && evt.ctrlKey ) {
 			evt.preventDefault();
 
+			if( shell.originalContent != shell.editor_window.value )
+					  shell.file_changed = true;
+
 			if ( shell.file_changed )
 			{
 				var can_write = false;
@@ -235,23 +259,23 @@
 			return false;
 		}
 
-		if ( !shell.file_changed )
+		if( !shell.file_changed )
 		{
-			if ( shell.originalContent != shell.editor_window.value )
-			{
-				if ( shell.perms )
-				{
-					if ( shell.perms.write == false )
-					{
-						shell.default_editor_status += ' [read-only]';
-						shell.editor_status.innerHTML += ' [read-only]';
-					}
-				}
-
-				shell.editor_status.innerHTML += ' [modified]';
-				shell.file_changed = true;
-			}
+				  if ( shell.originalContent != shell.editor_window.value )
+				  {
+							 if ( shell.perms )
+					 			{
+					 				if ( shell.perms.write == false )
+					 				{
+					 					shell.default_editor_status += ' [read-only]';
+					 					shell.editor_status.innerHTML += ' [read-only]';
+					 				}
+					 			}
+					 			shell.editor_status.innerHTML += ' [modified]';
+					 			shell.file_changed = true;
+				  }
 		}
+		
 	},
 
 	"confirmkey" : function ( e, arg )
@@ -397,6 +421,34 @@
 		http.send ( params );
 		shell.originalContent = shell.editor_window.value;
 		shell.file_changed = false;
+	},
+
+	"editorpaste" : function ( )
+	{
+		if(shell.perms)
+		{
+			if(shell.perms.write == true)
+			{
+				if(shell.file_changed == false)
+				{
+					shell.file_changed = true;
+					shell.editor_status.innerHTML += ' [modified]';
+				}
+			}
+			else
+				if(shell.file_changed == false)
+				{
+					shell.file_changed = true;
+					shell.editor_status.innerHTML += ' [read-only] [modified]';
+				}
+		}
+		return;
+	},
+
+	"editorclick": function ( e )
+	{
+		if (e.button == 2); /* Do something with the right button */
+		return;
 	}
 }