From 4cfa1be8165283c65f4b8bcc3d4bf6cc284b580e Mon Sep 17 00:00:00 2001
From: Jorge Garcia <jorge.garcia.gonzalez@gmail.com>
Date: Tue, 29 Mar 2016 11:21:03 -0600
Subject: [PATCH] Add emacsclient support to open with file:linum syntax

---
 doc/man/emacsclient.1 |   17 +++++++++++++++++
 lib-src/emacsclient.c |   40 ++++++++++++++++++++++++++++++++++------
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/doc/man/emacsclient.1 b/doc/man/emacsclient.1
index e62fe93..4e5e82b 100644
--- a/doc/man/emacsclient.1
+++ b/doc/man/emacsclient.1
@@ -52,6 +52,23 @@ If you set the variable "server-window" to a window or a frame, "C-x
 The programs follow the usual GNU command line syntax, with long
 options starting with two dashes ("\-").
 .TP
+.BI + number
+Go to the line specified by
+.I number
+(do not insert a space between the "+" sign and
+the number).
+This applies only to the next file specified.
+.TP
+.BI + line:column
+Go to the specified
+.I line
+and
+.IR column .
+.TP
+.B \-j, \-\-files-with-line-col FILENAME[:LINE[:COLUMN]] ...
+Go to line specified by LINE and column if specified.
+You can specify a list of files separated by space.
+.TP
 .B \-a, \-\-alternate-editor=EDITOR
 if the Emacs server is not running, run the specified editor instead.
 This can also be specified via the ALTERNATE_EDITOR environment variable.
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index c3e5635..21c3b54 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -151,6 +151,9 @@ int emacs_pid = 0;
    be used for the new frame.  */
 const char *frame_parameters = NULL;
 
+/* Nonzero means filename:line:column syntax support is enable  */
+int filename_with_line_col = 0;
+
 static _Noreturn void print_help_and_exit (void);
 
 
@@ -172,6 +175,7 @@ struct option longopts[] =
   { "server-file",	required_argument, NULL, 'f' },
   { "display",	required_argument, NULL, 'd' },
   { "parent-id", required_argument, NULL, 'p' },
+  { "files-with-line-col", no_argument, NULL, 'j' },
   { 0, 0, 0, 0 }
 };
 
@@ -461,9 +465,9 @@ decode_options (int argc, char **argv)
     {
       int opt = getopt_long_only (argc, argv,
 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
-			     "VHneqa:s:f:d:F:tc",
+			     "jVHneqa:s:f:d:F:tc",
 #else
-			     "VHneqa:f:d:F:tc",
+				  "jVHneqa:f:d:F:tc",
 #endif
 			     longopts, 0);
 
@@ -537,6 +541,9 @@ decode_options (int argc, char **argv)
         case 'F':
           frame_parameters = optarg;
           break;
+	case 'l':
+	  filename_with_line_col = 1;
+	  break;
 
 	default:
 	  message (true, "Try '%s --help' for more information\n", progname);
@@ -637,8 +644,9 @@ The following OPTIONS are accepted:\n\
 			Editor to fallback to if the server is not running\n"
 "			If EDITOR is the empty string, start Emacs in daemon\n\
 			mode and try connecting again\n"
-"\n\
-Report bugs with M-x report-emacs-bug.\n");
+"-j, --files-with-line-col\n\
+			Visit file at line,column using syntax FILENAME[:LINE[:COLUMN]]\n"
+"Report bugs with M-x report-emacs-bug.\n");
   exit (EXIT_SUCCESS);
 }
 
@@ -1593,7 +1601,7 @@ main (int argc, char **argv)
   char string[BUFSIZ+1];
   int start_daemon_if_needed;
   int exit_status = EXIT_SUCCESS;
-
+  char *lineDst;
   main_argv = argv;
   progname = argv[0];
 
@@ -1747,6 +1755,23 @@ main (int argc, char **argv)
                   continue;
                 }
             }
+
+	  if(filename_with_line_col != 0)
+	    {
+	      lineDst = index(argv[i],':');
+	      if ( lineDst != NULL)
+		{
+		  char *p = lineDst + 1;
+		  while (isdigit ((unsigned char) *p) || *p == ':') p++;
+		  if (*p == 0)
+		    {
+		      send_to_emacs (emacs_socket, "-position ");
+		      *lineDst='+';
+		      quote_argument (emacs_socket, lineDst);
+		      send_to_emacs (emacs_socket, " ");
+		    }
+		}
+	    }
 #ifdef WINDOWSNT
 	  else if (! file_name_absolute_p (argv[i])
 		   && (isalpha (argv[i][0]) && argv[i][1] == ':'))
@@ -1768,7 +1793,10 @@ main (int argc, char **argv)
 	    }
 #endif
 
-          send_to_emacs (emacs_socket, "-file ");
+	  send_to_emacs (emacs_socket, "-file ");
+	  if(lineDst != NULL){
+	    *lineDst = 0;
+	  }
           quote_argument (emacs_socket, argv[i]);
           send_to_emacs (emacs_socket, " ");
         }
-- 
1.7.9.5

