Page 1 of 1

[Issue] Display line number, column number in status bar

Posted: Sun Nov 22, 2020 10:27 am
by erbsenzahl
Hello,

in the German forum for LibreOffice (https://www.libreoffice-forum.de/viewto ... =5&t=33002) there is a question unsolved:

Can Writer show the current cursor position as line/column in the status bar like Notepad++ (or Microsoft Word) can do?

Image

Any suggestions?

Thanks for reading, cheers.

Re: Writer: display line number, column number in status bar

Posted: Sun Nov 22, 2020 12:28 pm
by Hagar Delest
Not yet implemented (nor in LibreOffice): [Issue] Way to view column/row numbers in Writer?
Issue 18004.

Re: [Issue] Display line number, column number in status bar

Posted: Sun Nov 22, 2020 2:49 pm
by JeJe
You could turn on the line numbering for the line.

Re: [Issue] Display line number, column number in status bar

Posted: Sun Nov 22, 2020 3:02 pm
by JeJe
A simple but probably tedious to use macro can give the column number in a message box.

CAUTION: may not work/give errors in circumstances such as the cursor not being a normal text one.

Code: Select all

Sub GetCursorColumn
on error goto hr
vc = thiscomponent.currentcontroller.viewcursor
vc.gotoStartOfLine(true)
lenst= len(vc.string)
vc.collapsetoend
msgbox "Column: " & lenst
hr:
End Sub
Edit: slight improvement, collapsed selection before showing message box

Re: [Issue] Display line number, column number in status bar

Posted: Sun Nov 22, 2020 3:34 pm
by JeJe
Unfortunately the ruler doesn't allow you to have Column marking.

A bit off beat... but if you're using a fixed width (non-proportional) font you can make your own and put it in the header.

Re: [Issue] Display line number, column number in status bar

Posted: Mon Nov 23, 2020 1:31 am
by Lupp
Q&D:

Code: Select all

Sub lineAndPosInText(Optional pDoc)
REM If the text object of the current selection contains interceptive tables,
REM the lines of the leftmost column should be counted as if they are lines
REM of ordinary paragraphs - independent of the heights of the cells.
REM TextTable cause problems again and again. Not sure if my stetement is reloable. 
If isMissing(pDoc) Then pDoc = ThisComponent
oldSel = pDoc.CurrentSelection
currPos = oldSel(0).End
cCtrl = pDoc.CurrentController
vc = cCtrl.ViewCursor
currText = vc.Text
inTableCell = tryCellName(currText)
inTable = (inTableCell<>"")
inFrame = currText.supportsService("com.sun.star.text.BaseFrame")
vc.gotoRange(currPos, False)
vc.goToStartOfLine(False)
REM Strange! The ViewCursor.String is empty after vc.goToStartOfLine(True) if in a TextTable cell.
REM Therefore:
tcPos = currText.createTextCursorByRange(currPos)
tcPos.gotoRange(vc.Start, True)
charsLeft = Len(tcPos.String)
trgLineStart = vc.Start
If EqualUnoObjects(currText, pDoc.Text) Then
  REM For the BodyText line counting is done per page.
  vc.jumpToStartOfPage()
Else
  REM Otherwise for the current text not regarding pages.
  cCtrl.select(currText.Start)
End If
LinesAbove = 0
REM TextTable are a mess!!
REM The following may NOT work if nested TextTable objects exist.
REM Will not work araound! Someone else?
While currText.compareRegionStarts(vc.Start, trgLineStart)>0
  LinesAbove = LinesAbove + 1
  vc.goDown(1, False)
Wend
MsgBox("Line:" & LinesAbove + 1 & "; Position in line:" & charsLeft + 1 & Chr(10) & _
       IIf(inTableCell<>"", "Table cell " & inTableCell, "") & IIf(inFrame, "Textframe!", ""))
cCtrl.select(oldSel)
End Sub

Function tryCellName(pObj)
tryCellName = ""
On Local Error Goto fail
tryCellName = pObj.CellName
fail:
End Function
We may feel sure there are only simple cases without nested tables or frames or whatever, but we cannot be sure.
That "rare complications" to be expected make reliable solutions rare.

Re: [Issue] Display line number, column number in status bar

Posted: Mon Nov 23, 2020 2:44 am
by JeJe
This is Lupp's code slightly modified to add his result to the status bar, wait half a second and return the status bar to normal. You just need a shortcut to run the macro.
Change the 500 number to a different value if you want more time to look at the result.

Code: Select all

Sub lineAndPosInText2()
REM If the text object of the current selection contains interceptive tables,
REM the lines of the leftmost column should be counted as if they are lines
REM of ordinary paragraphs - independent of the heights of the cells.
REM TextTable cause problems again and again. Not sure if my stetement is reloable.
'If isMissing(pDoc) Then 

pDoc = ThisComponent
oldSel = pDoc.CurrentSelection
currPos = oldSel(0).End
cCtrl = pDoc.CurrentController
vc = cCtrl.ViewCursor
currText = vc.Text
inTableCell = tryCellName(currText)
inTable = (inTableCell<>"")
inFrame = currText.supportsService("com.sun.star.text.BaseFrame")
vc.gotoRange(currPos, False)
vc.goToStartOfLine(False)
REM Strange! The ViewCursor.String is empty after vc.goToStartOfLine(True) if in a TextTable cell.
REM Therefore:
tcPos = currText.createTextCursorByRange(currPos)
tcPos.gotoRange(vc.Start, True)
charsLeft = Len(tcPos.String)
trgLineStart = vc.Start
If EqualUnoObjects(currText, pDoc.Text) Then
  REM For the BodyText line counting is done per page.
  vc.jumpToStartOfPage()
Else
  REM Otherwise for the current text not regarding pages.
  cCtrl.select(currText.Start)
End If
LinesAbove = 0
REM TextTable are a mess!!
REM The following may NOT work if nested TextTable objects exist.
REM Will not work araound! Someone else?
While currText.compareRegionStarts(vc.Start, trgLineStart)>0
  LinesAbove = LinesAbove + 1
  vc.goDown(1, False)
Wend
cCtrl.select(oldSel)

lbl = ( "Line:" & LinesAbove + 1 & "; Position in line:" & charsLeft + 1 & Chr(10) & _
       IIf(inTableCell<>"", "Table cell " & inTableCell, "") & IIf(inFrame, "Textframe!", ""))
   sbar = pdoc.getCurrentController.StatusIndicator 'add to the status ar
   sbar.start( lbl,0 )
    Wait 500 'wait a bit so you can look at it CHANGE 500 TO PREFERRED TIME 500 = HALF A SECOND 1000 = 1 SECOND
	sbar.end()

End Sub

Function tryCellName(pObj)
tryCellName = ""
On Local Error Goto fail
tryCellName = pObj.CellName
fail:
End Function



Re: [Issue] Display line number, column number in status bar

Posted: Mon Nov 23, 2020 3:31 pm
by JeJe
I need a second to read it, and it helps if its abbreviated a little.

Code: Select all

Sub lineAndPosInText2()
REM If the text object of the current selection contains interceptive tables,
REM the lines of the leftmost column should be counted as if they are lines
REM of ordinary paragraphs - independent of the heights of the cells.
REM TextTable cause problems again and again. Not sure if my stetement is reloable.
'If isMissing(pDoc) Then 

pDoc = ThisComponent
oldSel = pDoc.CurrentSelection
currPos = oldSel(0).End
cCtrl = pDoc.CurrentController
vc = cCtrl.ViewCursor
currText = vc.Text
inTableCell = tryCellName(currText)
inTable = (inTableCell<>"")
inFrame = currText.supportsService("com.sun.star.text.BaseFrame")
vc.gotoRange(currPos, False)
vc.goToStartOfLine(False)
REM Strange! The ViewCursor.String is empty after vc.goToStartOfLine(True) if in a TextTable cell.
REM Therefore:
tcPos = currText.createTextCursorByRange(currPos)
tcPos.gotoRange(vc.Start, True)
charsLeft = Len(tcPos.String)
trgLineStart = vc.Start
If EqualUnoObjects(currText, pDoc.Text) Then
  REM For the BodyText line counting is done per page.
  vc.jumpToStartOfPage()
Else
  REM Otherwise for the current text not regarding pages.
  cCtrl.select(currText.Start)
End If
LinesAbove = 0
REM TextTable are a mess!!
REM The following may NOT work if nested TextTable objects exist.
REM Will not work araound! Someone else?
While currText.compareRegionStarts(vc.Start, trgLineStart)>0
  LinesAbove = LinesAbove + 1
  vc.goDown(1, False)
Wend
cCtrl.select(oldSel)

lbl = ( "Line:" & LinesAbove + 1 & ", Col:" & charsLeft + 1 & Chr(10) & _
       IIf(inTableCell<>"", "Table cell " & inTableCell, "") & IIf(inFrame, "Textframe!", ""))
   sbar = pdoc.getCurrentController.StatusIndicator
   sbar.start( lbl,0 )
      Wait 1000
sbar.end()

End Sub

Function tryCellName(pObj)
tryCellName = ""
On Local Error Goto fail
tryCellName = pObj.CellName
fail:
End Function