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 24.04.07, SANGOI DINO LEONARDO wrote:
> > From: Jörg Sommer> > Sent: Saturday, April 21, 2007 6:44 PM

> If you have a less shocking combination I will gladly put it as default.
> Anyways, I used 'custom_color()' to allow user customisation: you can put
> in your .jedrc a set of 'custom_color()' calls. So, for example, if you put
> this:

> custom_color("diff_block",   "lightgray", "brown");

> You will have lightgray on brown.

My background color is LightYellow or white, so I really need a different
scheme. I see two ways to avoid ugly combinations:

1. re-use existing color definitions by mapping diff syntax tokens to common
   ones (this is how it is done in the highlight only diff.sl mode:
  
      variable color_diff = "number";
      variable color_old_file = "error";
      variable color_new_file = "keyword";
      variable color_block_header = "preprocess";
      variable color_deleted = "error";
      variable color_added = "keyword";
      variable color_messages = "comment";
      
      dfa_define_highlight_rule("^diff .*",    color_diff, mode);
      ...
      
   + might work better "out of the box" for individually crafted color schemes
   - hard to customize

2. providing 2 default schemes for dark and light background

   see patch below
   
   + using custom_color() makes it easy to customize color settings the
     standard Jed way
   + homegrown color schemes can simply include the diff* colors
   + consistent set of colors for the diff syntax tokens
   
   - fails on Jed <= 0.99.16 (is this still an issue?)
   - might look suboptimal out of the box with some color schemes

Which method do you prefer?


> The last time I tested diffmode with and old jed version was a lot of time
> ago... so I don't know if it still works...

I just removed the backwards compatibility stuff and added
documentation that you need at least Jed >= 0.99.16 in my patch.



> > diffmode doesn't find the file, because it's in the wrong directory. How
> > can I change the directory? Do you have some kind of strip level like -p
> > for patch?

> Yes, the problem is that diffmode doesn't try hard enough to find the
> file... well, it does very little: a simple read_file().

If I remember right, read_file() uses the Jed internal "pwd" for
filename expansion. To expand a filename relative to the
buffer-directory (which seems more appropriate) one could add

  name = expand_filename(path_concat(buffer_dirname(), name));

with buffer_dirname() from bufutils.sl.

> A more advanced way should be nice, e.g. asking the user how many
> directories strip from the name, or what directories to add, or even better
> let the user browse for the file...

My patch asks the user for a path-adjustment if file_status() on the
extracted filename fails. (After this, the return value of read_file()
can be discarded, as it will throw an error on failure anyway.)

I also added a fallback solution for the case that one calls
diff_jump_to() from the top lines (above the first diff-block) of a diff.
In diff_top_of_block(), instead of error, the first block below is
considered.


Thanks

Guenter

This patch is done against the version mailed in the diffmode.zip
attachment:

--- ~/.jed/contribs/Dino/diffmode.sl	2007-03-05 19:17:00.000000000 +0100
+++ ~/.jed/lib/diffmode.sl	2007-04-25 16:01:30.000000000 +0200
@@ -22,31 +22,52 @@
 % 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 by setting 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"); 
+%
+% If the "normal" background color is dark but not "black", you can choose the
+% default dark color scheme with
+% 
+%        diff_set_colors_dark(<bg>);
+%
+% where <bg> is the background color. 
+% 
+% Changed color settings will only take effect after deleting the DFA
+% cache file "diffmode.dfa" and restarting Jed!
 %
 % Thanks to:
+% 
 % - John Davis, for this great editor.
 % - Abraham van der Merwe, I took some ideas (and maybe also some code...)
 %                          from his diff.sl.
@@ -54,12 +75,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,7 +98,39 @@
 % should tree pack single child nodes?
 custom_variable("DiffTree_Pack", 1);
 
+% Colours
+
+% default color schemes for dark and light background
+define diff_set_colors_dark(bg)
+{
+	custom_color("diff_cmd",     "lightgray", "blue");  % diff
+	custom_color("diff_oldfile", "red",       "blue");  % ---
+	custom_color("diff_newfile", "green",     "blue");  % +++
+	custom_color("diff_block",   "lightgray", "cyan");  % @@
+	custom_color("diff_deleted", "red",       bg);      % -
+	custom_color("diff_added",   "green",     bg);      % +
+	custom_color("diff_junk",    "lightgray", "red");   % Only / Binary
+}
+define diff_set_colors_light(bg)
+{
+	custom_color("diff_cmd",     "blue",      bg);      % diff
+	custom_color("diff_oldfile", "red",       "cyan");  % ---
+	custom_color("diff_newfile", "blue",      "cyan");  % +++
+	custom_color("diff_block",   "black", 	"magenta");  % @@
+	custom_color("diff_deleted", "red",       bg);      % -
+	custom_color("diff_added",   "blue" ,     bg);      % +
+	custom_color("diff_junk",    "gray", 	   bg);      % Only / Binary
+}
+
+% set matching colour scheme
+private variable bg;
+( , bg) = get_color("normal");
+if (bg == "black" or bg == "blue")
+  diff_set_colors_dark(bg);
+else
+  diff_set_colors_light(bg);
 
+  
 %%%% Diff low level helpers %{{{
 
 % return true if I'm looking_at() a header marker.
@@ -90,7 +152,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);
@@ -182,7 +244,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 +473,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);
@@ -440,55 +501,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);
@@ -511,6 +540,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
@@ -526,9 +556,12 @@
 	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();
 }


--------------------------
To unsubscribe send email to <jed-users-request@xxxxxxxxxxx> with
the word "unsubscribe" in the message body.
Need help? Email <jed-users-owner@xxxxxxxxxxx>.


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