3

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++.

ewh
  • 1,004
  • 9
  • 19
  • characters can have different displayed widths, eg. control characters might have a width of 2 or 4 in emacs - the [usual display table](https://www.gnu.org/software/emacs/manual/html_node/elisp/Usual-Display.html) has emacs defaults, perhaps just setting `tab-width` to 1 would be good enough for emacs? something like `emacs --eval "(setq tab-width 1)" ...` (untested) – Rorschach Jun 02 '21 at 03:43
  • @Rorschach Looks like the following works instead to get tab width to 1: `--eval "(setq-default tab-width 1)"`. Using this, the cursor will now be on the desired character position. Not sure how I feel about mucking with tab-width setting, but for now, seems like only thing that will provide desired behavior. – ewh Jun 02 '21 at 18:21
  • you could add a hook after emacs loads as an alternative, Ill add an example – Rorschach Jun 02 '21 at 18:52

2 Answers2

1

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.

Radu Coravu
  • 1,754
  • 1
  • 9
  • 8
  • Dealing with XML data that can have tab characters in it, so origin may not be controled by Oxygen. As for character offset, it does appear tabs do affect how `column` is interpreted. You can observe this in the editor itself by using the keyboard to move cursor over on a line with tabs in it. You will see the column number jump by the configured tab display width. Side note, my tool will use `element` instead if possible to focus cursor in the XML, but there are cases where only line:column are available. – ewh Jun 02 '21 at 18:11
  • 1
    Using a command line like this: oxygen.exe file:/...path/to/file.xml#line=5;column=2 on a "file.xml" which at line 5 starts with a tab character, will place the cursor after the first tab character. Indeed the editor status will show a larger column number (5 in this case) but the placement of the caret will be done as if the tab is a single character. – Radu Coravu Jun 03 '21 at 12:46
  • Looksl ike you are correct. I misinterpreted the status information to what I was expecting, but from what I observe, oxygen is placing the cursor in the expected location. I initially thought that was case, but was thrown off by the status line. FYI, Vim provides both the character position and rendered column position. – ewh Jun 03 '21 at 16:25
1

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 
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Excellent! That does appear to work nicely. Looks like emacs problem is solved, but unsure if there are solutions for the other editors. – ewh Jun 02 '21 at 19:18