Relative symlinks

I wanted an easy way to make relative symlinks in MC.  I searched the
source directory and found there used to be such a feature, but it was
removed about four years ago.  For good reason; it was ugly, convoluted,
completely undocumented, and it was broken as well.

So I decided to rewrite it.

However, I have a slight handicap-- I don't know C.  So you may not want
to take this patch too seriously!  But at least it might inspire one of
the developers to do it right.  

But in fact it turned out to be very simple.  I added a
'relative_symlink_cmd' function modeled on 'symlink_cmd'.  The existing
'do_link' takes two possible values for the first parameter-- 0 = hard
link, 1 = symlink.  I added another possibility, 2 = relative link.  So
I don't think there is a risk of it breaking any existing features.

The old other_symlink feature used <Ctrl-x Ctrl-l>. I used <Ctrl=X v>
because it's easier for my to type.  I doubt if that change will bother
anyone. I did not add it to the File Menu because that menu already
fills a 25-line screen.

The only small glitch I've noticed is that diff_two_paths doesn't
completely optimize a relative path if the file and its link are both in
the same directory. I'll post another message about that.

Criticism and suggestions are welcome.


--- doc/
+++ doc/
@@ -297,6 +297,13 @@
 .B C-x s
 run the symbolic link command.
+.B C-x v
+run the relative symbolic link command. See the
+File Menu
+.\"File Menu"
+section for more information about symbolic links.
 .B C-x i
 set the other panel display mode to information.
@@ -966,6 +973,23 @@
 .I "Show mini-status"
 option is enabled. Use symbolic links when you want to avoid the
 confusion that can be caused by hard links.
+When you press (C-x s) Midnight Commander will automatically fill in the
+complete path+filename of the original file and suggest a name for the link.
+You can change either one.
+Sometimes you may want to change the absolute path of the original into
+a relative path. An absolute path starts from the root directory:
+.I /home/frodo/mc/mc -> /home/frodo/new/mc
+A relative link describes the original file's location starting from the
+location of the link itself:
+.I /home/frodo/mc/mc -> ../new/mc
+You can force Midnight Commander to suggest a relative path by pressing
+(C-x v) instead of (C-x s).
 .B Rename/Move (F6)
--- src/cmd.c
+++ src/cmd.c
@@ -914,7 +914,8 @@ do_link (int symbolic_link, const char *
 	char *s;
 	char *d;
-	/* suggest the full path for symlink */
+	/* suggest the full path for symlink, and either the full or
+	relative path to the file it points to */
 	s = concat_dir_and_file (current_panel->cwd, fname);
 	if (get_other_type () == view_listing) {
@@ -923,6 +924,10 @@ do_link (int symbolic_link, const char *
 	    d = g_strdup (fname);
+	if ( 2 == symbolic_link) {
+	    s = diff_two_paths ((other_panel->cwd),s);
+	}
 	symlink_dialog (s, d, &dest, &src);
 	g_free (d);
 	g_free (s);
@@ -954,6 +959,16 @@ void symlink_cmd (void)
     if (filename) {
 	do_link (1, filename);
+    }
+void relative_symlink_cmd (void)
+    char *filename = NULL;
+    filename = selection (current_panel)->fname;
+    if (filename) {
+	do_link (2, filename);
--- src/cmd.h
+++ src/cmd.h
@@ -40,6 +40,7 @@
 void tree_cmd (void);
 void link_cmd (void);
 void symlink_cmd (void);
+void relative_symlink_cmd (void);
 void edit_symlink_cmd (void);
 void reverse_selection_cmd (void);
 void unselect_cmd (void);

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