jed-users mailing list

[2007 Date Index] [2007 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [jed] help for diff mode (syntax color via dfa)


On 26.04.07, Lechee.Lai@xxxxxxxxxxxx wrote:
> I've piece of patch useful for me how about U

Very nice, thanks. I put a modified version in the attached patch.

I revised my last patch (it is smaller now). It contains the following
changes:

* set default colors re-using existing color definitions
  (better defaults for non-black color schemes but still customisable)
  
  The method is to have custom definitions like 
  
     custom_color("diff_block",   get_color("preprocess"));  % @@
     
  It works nice for me but might need tweaking for other color schemes.
  
* removed pre Jed 0.99.16 compatibility hack
  
  (Even Debian stable now has Jed >= 0.99.16)

* diff_top_of_file(), diff_top_of_block(): try below if there is no top 
  above 
  
  -> lets diff_jump_to() also work from the top of the diff buffer
  
* diff_jump_to(): ask for path if file not found

  Ask in the minibuffer to correct the path. Jed's standard path expansion
  mechanisms (SPACE and TAB) work as expected.
  
* new: diff_jump() (after a patch by Lechee Lai)

  "Intelligently" jump to "right" source, ('+' to new, '-' to old)
  
  Bound to Enter. Maybe Alt-Enter is the better binding, as we allow editing
  in the buffer but this is another discussion.
  
Dino, do you think we could put the diffmode at Jedmodes? 


Günter

--- /home/milde/.jed/contribs/Dino/diffmode.sl	2007-04-27 09:49:01.000000000 +0200
+++ /home/milde/.jed/lib/diffmode.sl	2007-04-27 10:19:43.000000000 +0200
@@ -10,6 +10,15 @@
 % $Revision: 44 $
 % $Author: sangoid $
 % $Date: 2005-10-10 10:55:32 +0200 (Mon, 10 Oct 2005) $
+% 
+% 2007-04-27 <milde users.sf.net>
+%  * set default colors re-using existing color definitions
+%    (better defaults for non-black color schemes but still customisable)
+%  * removed pre Jed 0.99.16 compatibility hack
+%  * diff_top_of_file(), diff_top_of_block(): try below if there is no top 
+%    above -> lets diff_jump_to() also work from the top of the diff buffer
+%  * diff_jump_to(): ask for path if file not found
+%  * new: diff_jump() (after a patch by Lechee Lai)
 %
 % It does highlighting a bit like mcedit (the midnight commander editor) does.
 % (It uses dfa syntax highlighting).
@@ -22,30 +31,40 @@
 % This works only on unified diffs right now, and probably forever (mostly
 % because I always use unified diffs).
 %
-% Usage:
-%    put diffmode.sl, treemode.sl, and walk.sl somewhere in your
-%    JED_LIBRARY_PATH.
+% Usage
+% -----
+% 
+% Put diffmode.sl, treemode.sl, and walk.sl in your Jed library path 
+% (cf. get_jed_library_path()).
 %
-%    add to you .jedrc something like:
+% Add to your .jedrc or jed.rc file something like
+%  
 %        autoload("diff_mode", "diffmode");
 %        add_mode_for_extension("diff", "diff");
 %        add_mode_for_extension("diff", "patch");
 %
-% And every file with extension '.diff' or '.patch' will automagically
-% get the diff mode applied.
+% and every file with extension '.diff' or '.patch' will automagically
+% get the "diff" mode applied.
 %
+% Customization
+% -------------
+% 
 % Optionally you can set these variables:
+% 
 % Diff_Use_Tree   -> whether to use the (experimental) tree mode viewer for
 %                    patches containing more than one file. If zero, tree mode
 %                    is not used, else the value is the minimum number of files
 %                    needed inside a patch to trigger the tree mode viewer.
 %                    (default = 4, just a randomly choosen number)
+% 
 % DiffTree_Indent -> how much to indent lines in tree mode viewer. Default = 8
 %
-% If you are using jed 0.9.16 or newer, you can also redefine the colors used
-% for syntax highlighting, just defining colors for "diff_block", "diff_junk",
-% "diff_deleted", "diff_added", "diff_fileold", "diff_filenew", "diff_cmd"
-%
+% You can customize the syntax highlight colors for "diff_block", "diff_junk",
+% "diff_deleted", "diff_added", "diff_oldfile", "diff_newfile", "diff_cmd" in
+% jed.rc, e.g.
+% 
+%        set_color(diff_deleted, "blue", "red"); 
+% 
 % Thanks to:
 % - John Davis, for this great editor.
 % - Abraham van der Merwe, I took some ideas (and maybe also some code...)
@@ -54,12 +73,21 @@
 %                 a lot of slang code I'm using)
 %}}}
 
-_debug_info = 1;
-_traceback = 1;
+% _debug_info = 1; % not used in current SLang 2
+% _traceback = 1;  % rather set this in your jed.rc
+
+% Requirements
+% ------------
 
+% Jed >= 0.99.16     % custom_color()
+% SLang 2	     % "raw string literal"R
+require("keydefs");  % standard mode, not loaded by default
 require("treemode");
 require("walk");
 
+% Customizable settings
+% ---------------------
+
 % Set Diff_Use_Tree to 0 to avoid using a tree view for the diff.
 % Otherwise, set it to the minimun number of files covered by the diff
 % needed to use the tree view.
@@ -68,6 +96,16 @@
 % should tree pack single child nodes?
 custom_variable("DiffTree_Pack", 1);
 
+% Default color scheme
+
+custom_color("diff_cmd",     get_color("operator"));    % diff
+custom_color("diff_oldfile", get_color("bold"));        % ---
+custom_color("diff_newfile", get_color("number"));      % +++
+custom_color("diff_block",   get_color("preprocess"));  % @@
+custom_color("diff_deleted", get_color("error"));       % -
+custom_color("diff_added",   get_color("keyword"));     % +
+custom_color("diff_junk",    get_color("comment"));     % Only / Binary
+  
 
 %%%% Diff low level helpers %{{{
 
@@ -90,7 +128,7 @@
 	variable pos, len;
 	
 	% Uhmm, there's a better way to do this?
-	if (string_match(l, "@@ \\-\\(\\d+\\),\\(\\d+\\) \\+\\(\\d+\\),\\(\\d+\\) @@", 1) == 0)
+	if (string_match(l, "@@ \-\(\d+\),\(\d+\) \+\(\d+\),\(\d+\) @@"R, 1) == 0)
 		error("malformed block header <"+l+">");
 	
 	(pos, len) = string_match_nth(1);
@@ -146,7 +184,7 @@
 {
 	push_mark();
 	% search the diff marker.
-	if (bol_bsearch("--- ") == 0) {
+	if (andelse{bol_bsearch("--- ") == 0}{bol_fsearch("--- ") == 0}) {
 		pop_mark(1);
 		error("start of file not found.");
 	}
@@ -182,7 +220,7 @@
 define diff_top_of_block()
 {
 	push_mark();
-	if (bol_bsearch("@@ ") == 0) {
+	if (andelse{bol_bsearch("@@ ") == 0}{bol_fsearch("@@ ") == 0}) {
 		pop_mark(1);
 		error("start of block not found.");
 	}
@@ -411,7 +449,6 @@
 %%%% Standard mode things: keymap, syntax, highlighting %{{{
 static variable Diff = "Diff";
 
-require("keydefs");
 define diff_add_saurus_bindings()
 {
 	definekey("diff_top_of_file", Key_Shift_F11, Diff);
@@ -422,7 +459,8 @@
 	definekey("diff_redo_file()", Key_F11, Diff);
 	definekey("diff_remove_block(1)", Key_F8, Diff);
 	definekey("diff_remove_only_lines()", Key_F9, Diff);
-	definekey("diff_jump_to(1)", "", Diff);
+	definekey("diff_jump_to(1)", "^V", Diff);
+        definekey("diff_jump", "\r", Diff);  % Return: "intelligent" jump
 	%%%% Other Functions
 	% diff_mark_file(skipheader);
 	% diff_mark_block(skipheader);
@@ -439,55 +477,23 @@
 
 #ifdef HAS_DFA_SYNTAX
 
-if ( _jed_version >= 9916) {
-	custom_color("diff_block",   "lightgray", "cyan");      % @@
-	custom_color("diff_junk",    "lightgray", "red");       % Only / Binary
-	custom_color("diff_deleted", "red",       "black");     % -
-	custom_color("diff_added",   "green",     "black");     % +
-	custom_color("diff_fileold", "red",       "blue");%"lightgray"); % ---
-	custom_color("diff_filenew", "green",     "blue"); % +++
-	custom_color("diff_cmd",     "lightgray",     "blue"); % diff
-} else {
-	%%% For backward compatibility
-	set_color("keyword3", "lightgray", "blue");      % @@
-	set_color("keyword4", "lightgray", "red");       % Only / Binary
-	set_color("keyword5", "red",       "black");     % -
-	set_color("keyword6", "green",     "black");     % +
-	set_color("keyword7", "red",       "lightgray"); % ---
-	set_color("keyword8", "green",     "lightgray"); % +++
-	set_color("keyword9", "black",     "lightgray"); % diff
-}
-
 static define setup_dfa_callback (name)
 {
-	if ( _jed_version >= 9916) {
-		%%% DFA_CACHE_BEGIN %%%
-		dfa_enable_highlight_cache("diffmode.dfa", name);
-		dfa_define_highlight_rule("^diff .*$",      "diff_cmd",     name);
-		dfa_define_highlight_rule("^\\+\\+\\+ .*$", "diff_filenew", name);
-		dfa_define_highlight_rule("^\\-\\-\\- .*$", "diff_fileold", name);
-		dfa_define_highlight_rule("^\\+.*$",        "diff_added",   name);
-		dfa_define_highlight_rule("^\\-.*$",        "diff_deleted", name);
-		dfa_define_highlight_rule("^Only .*$",      "diff_junk",    name);
-		dfa_define_highlight_rule("^Binary .*$",    "diff_junk",    name);
-		dfa_define_highlight_rule("^@@ .*$",        "diff_block",   name);
-		dfa_build_highlight_table(name);
-		%%% DFA_CACHE_END %%%
-	} else {
-		% I don't keep a cache here, just to avoid problems when upgrading jed...
-		% (and because I can't put more than one pair of DFA_CACHE markers in a
-		%  single file).
-		% dfa_enable_highlight_cache("diffmode.dfa", name);
-		dfa_define_highlight_rule("^diff .*$", "keyword9", name);
-		dfa_define_highlight_rule("^\\+\\+\\+ .*$", "keyword8", name);
-		dfa_define_highlight_rule("^\\-\\-\\- .*$", "keyword7", name);
-		dfa_define_highlight_rule("^\\+.*$", "keyword6", name);
-		dfa_define_highlight_rule("^\\-.*$", "keyword5", name);
-		dfa_define_highlight_rule("^Only .*$", "keyword4", name);
-		dfa_define_highlight_rule("^Binary .*$", "keyword4", name);
-		dfa_define_highlight_rule("^@@ .*$", "keyword3", name);
-		dfa_build_highlight_table(name);
-	}
+	%%% DFA_CACHE_BEGIN %%%
+	dfa_enable_highlight_cache("diffmode.dfa", name);
+	dfa_define_highlight_rule("^diff .*$",      "diff_cmd",     name);
+	dfa_define_highlight_rule("^\+\+\+ .*$"R,   "diff_newfile", name);
+	dfa_define_highlight_rule("^--- .*$", 	    "diff_oldfile", name);
+	dfa_define_highlight_rule("^\+.*$"R,        "diff_added",   name);
+	dfa_define_highlight_rule("^\-.*$"R,        "diff_deleted", name);
+	dfa_define_highlight_rule("^Only .*$",      "diff_junk",    name);
+	dfa_define_highlight_rule("^Binary .*$",    "diff_junk",    name);
+	dfa_define_highlight_rule("^@@ .*$",        "diff_block",   name);
+	dfa_build_highlight_table(name);
+	% non-unified diffs:
+	dfa_define_highlight_rule("^> .*",          "diff_added",   name);
+	dfa_define_highlight_rule("^< .*",          "diff_deleted", name);
+	%%% DFA_CACHE_END %%%
 }
 
 dfa_set_init_callback(&setup_dfa_callback, Diff);
@@ -510,6 +516,7 @@
 	variable delta = what_line();
 	% FIXME: Consider Added and removed lines while computing delta.
 	walk_mark_current_position();
+	push_spot(); % save current position
 	diff_top_of_block();
 	delta -= what_line();
 	line_as_string(); % on stack
@@ -525,15 +532,36 @@
 	name = line_as_string();
 	newpos = is_substr(name, "\t") - 2;
 	name = name[[4:newpos]];
-	read_file(name);
+	pop_spot(); % restore point position
+	!if (file_status(name) == 1)
+	   name = read_with_completion("Please adjust path:", "", name, 'f');
+	() = read_file(name);
 	goto_line(oldpos);
-	message(name+":"+string(oldpos));
+	vmessage("%s:%d", name, oldpos);	   
 	walk_goto_current_position();
 	walk_store_marked_position();
 }
 
 %%%%}}}
 
+%%%% Jump to "right" source, ('+' to new, '-' to old) %{{{
+define diff_jump()
+{
+   variable ch;
+   push_spot();
+   bol();
+   ch = what_char();
+   pop_spot();
+   switch (ch)
+     { case '-': diff_jump_to(0); }
+     { case '+': diff_jump_to(1); }
+   % { message ("not on a -/+ marked line"); }
+     { diff_jump_to(0); } % there is a binding for jump_to(1)
+   % TODO: ask in minibuffer for new/old?
+}
+%%%%}}}
+
+
 %%%% Menu %{{{
 %%%%
 

[2007 date index] [2007 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]