The problem I am trying to solve is determining how many columns of the current buffer are hidden due to horizontal scrolling. The documentation states:

The horizontal scroll position is measured in units of the normal character width, which is the width of space in the default font. Thus, if the value is 5, that means the window contents are scrolled left by 5 times the normal character width. How many characters actually disappear off to the left depends on their width, and could vary from line to line.

Thus, when the font in the buffer is the default font and the latter is mono-space, the number of hidden columns is given by (window-hscroll). In my case, the buffer's font, although mono-space, differs from the default font. So, how can I get something more relevant than (window-hscroll) in this case? Or, alternatively, can I trick window-hscroll or, even better, the functions that perform the actual scrolling into considering the buffer's font to be the default?


I use the buffer to display a large graph produced by Graph::Easy. I need to be able to scroll the window so as to make a given node visible and centered. The lines have been padded with spaces from both ends, so that all lines are of the same (and sufficient) length. Also, the font is mono-space, so it does not matter which line to pick for the calculation.

  • Do we mean how many columns of the current line are hidden, instead of how many columns of the current buffer are hidden? Are we only interested in the longest line? And, aren't we only interested in the screen lines that are presently visible? You might want to provide some background as to what it is that you are trying to accomplish since that would open the door to a solution you have not thought of as part of your question. – lawlist Sep 14 '17 at 17:46
  • @lawlist I edited the question. – AlwaysLearning Sep 14 '17 at 18:38
  • A lo-tech solution would be (save-excursion (goto-char (line-end-position)) (current-column)) to extract the last column of the current line. And you can correlate that with the last and/or first visible column and/or window-body-width, etc. I haven't played with window-hscroll, so I have no comment in relation thereto. – lawlist Sep 14 '17 at 22:56
  • @lawlist I do not see how this relates to the problem. Note that the root of the problem is that the space character in default font (which is the unit of horizontal scroll) and the space character in the buffer's current font occupy different horizontal space. – AlwaysLearning Sep 15 '17 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.