2

I'm working on a utility that communicates with an external process using line and column positions in the current file.

I have configured Haskell mode to use compose-region to display certain parts of the syntax in Unicode, which can replace multiple characters with a single one (ie turning -> into ). Unfortunately, this means that the line and column position in the buffer no longer correspond to their position in the underlying file, but instead map to the position in the buffer, counting the composed region as a single character.

Here's the code I currently use to get the line and column:

(list (line-number-at-pos) (current-column))

How can I get the position ignoring these composed characters, in terms of the underlying file?

  • If its feasible, I would suggest that you refer to (point) instead of columns and perhaps consider using overlays instead. (save-excursion (goto-char (point-min)) (while (re-search-forward "->" nil t) (overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'display "→"))) – lawlist May 13 '15 at 4:45
  • @lawlist: Unfortunately, the symbols are part of haskell-mode, so it's a bit harder to change it to use overlays, although it might be worth a look. Also, I'd need to convert (point) to lines and columns to work with the external tool. (I guess I could change it to support that style of position info, but it's also mostly outside of my purview.) – Tikhon Jelvis May 13 '15 at 4:55
1

Assuming there are not TAB characters (which is usually true in Haskell files), then (- (point) (line-beginning-position)) should give you the "column" info you need (and (forward-char N) should advance by the right number of "columns").

  • Awesome, looks like that works. Thanks! If there are any problems with tabs, I figure the tabs users brought them upon themselves ;). – Tikhon Jelvis May 14 '15 at 1:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.