How to invoke an editor to specific line and character position relative to the line?

427 Views Asked by At

I am working on an application that supports the ability to launch an external editor on XML validation errors, where the validation error will include the specific line and character offset in the line the error occurs. For example, "74:62" represents the 74th line and 62nd character of that line, which is also said as, "line 74, column 62".

The problem I am having is that editors treat "column" differently. For Vim, column 62 is a character position. While in Oxygen XML Editor, Notepad++, and Emacs, column is a rendered position.

Why does this distinction matter? If the target line has tab characters, Notepad++ and Emacs "column" no longer represents character offset, and for those editors the cursor gets located differently: Notepad++ treats tab as 4 columns and Emacs as 8 (by default).

Here are the commands I use for each editor, where 'L' is the line number, 'C' is the column number, and 'FILE' is the file to open:

  • gvim "+call cursor(L,C)" FILE
  • oxygen FILE#line=L;column=C
  • notepad++ -nL -cC -lxml FILE
  • emacs +L:C FILE (See @Rorschach's answer below for method that does work)

For Notepad++ and Emacs, are there command-line invocations that will place the cursor at a character position relative to the line?

Edit

Oxygen XML Editor behaves as expected, where column parameter is interepreted as a character offset from line. See @Rorschach's answer below regarding Emacs, I do not have a solution for Notepad++.

2

There are 2 best solutions below

1
On BEST ANSWER

As a workaround for different character display widths in emacs you could add a hook to run after the file loads.

# assuming there are some variables (eg. shell) 
# holding the line/column as in the normal command
L=4
C=2
emacs --eval "(add-hook 'emacs-startup-hook (lambda () (goto-line $L) (forward-char $C)))" input_file.xml 
3
On

I work for Oxygen XML Editor and as far as I know Oxygen places the caret at that specific character offset, so we do not use the visual tab width representation for anything in this context. One thing you should be aware is that in Oxygen by default if you press the "Tab" key you actually insert 4 spaces, if you want to insert the "Tab" character instead, in the Preferences->"Editor / Format" page there is a setting named "Indent with tabs" which needs to be checked.