reddit / emacs

Help wanted. Is there way to compute count of character till end of line?

darkawower · 2022-02-26 14:47
I want to find the number of characters from end of line to the end of right fringe. &#x200B; https://preview.redd.it/b5b8t3ydt6k81.png?width=1674&format=png&auto=webp&s=e1ead7c54d4e8e5e8b0055fa45ab50052cf40104 It's not such a difficult task, but when I need to do the same calculation for long line after visual moving (when `truncate-lines`is disabled) , I ran into a lot of problems &#x200B; https://preview.redd.it/b60s899ut6k81.png?width=1494&format=png&auto=webp&s=958d63d139f6a4769b53cb0fff7773bc7ccaf751 The first idea was to get the remainder of the window-width divided by the current line length, (- (window-width) (% (length (thing-at-point 'line)) (window-width))) It just doesn't work, cause line break happened between spaces. The second idea is simulate line break logic, i tried to sum length: (defun split-with-preserved-spaces (string separator) (cl-loop with seplen = (length separator) with len = (length string) with start = 0 with next = seplen for end = (or (cl-search separator string :start2 next) len) for chunk = (substring string start end) collect chunk while (< end len) do (setf start end next (+ seplen end)))) (defun get-available-width-before-window-end () "Return count of chars before window end." (let* ((current-line (string-trim (thing-at-point 'line) nil "\n")) (splited-lines (split-with-preserved-spaces current-line " ")) (current-window-width (window-width)) (last-fake-line-length 0)) (dolist (l splited-lines) (setq last-fake-line-length (cond ((>= (length l) current-window-width) (% (length l) current-window-width)) ((>= (+ last-fake-line-length (length l)) current-window-width) (% (+ last-fake-line-length (length l)) current-window-width)) (t (+ last-fake-line-length (length l)))))) (min (- current-window-width last-fake-line-length) (% (window-width) (length current-line))))) This approach works in many cases, but sometimes it also counts the wrong number of characters, and I don't understand why at all. Is there a more simple way to count the number of characters to the end of the right fringe for long-lines?
fragglestickcar0 · 2022-02-26 15:43
(- (line-end-position) (point))
darkawower · 2022-02-26 15:46
>(- (line-end-position) (point)) I need to find the number of characters from the end-of-line position to the maximum window width, not from the current point to the end of the line.
fragglestickcar0 · 2022-02-26 16:01
Most emacs users appear incapable of asking, much less answering, a question clearly and concisely. This won't work if your line contains images, wide glyphs, or anything that's not a monospace character. (defun nchars-to-fringe () (- (window-width) (% (save-excursion (end-of-line) (current-column)) (window-width))))
darkawower · 2022-02-26 16:09
>(defun nchars-to-fringe ()(- (window-width)(% (save-excursion (end-of-line) (current-column))(window-width)))) does not work without truncate-line mode [https://postimg.cc/Wt9G5XF6](https://postimg.cc/Wt9G5XF6) also does not work when the line length equals the width of the window
darkawower · 2022-02-26 16:12
It returns 61, but it should return 23
crawsphi · 2022-02-26 18:40
I am not certain exactly how this can be solved, but I think this task is related: https://stackoverflow.com/questions/43281946/programmatically-detect-if-any-line-in-a-buffer-is-wrapping The functions `window-body-width` and `window-text-width` or `window-text-pixel-size` should be helpful to you.
onearmedphil · 2022-02-26 20:19
I’m really curious what you’re trying to accomplish high-level.
darkawower · 2022-02-26 20:23
I want to truncate an overlay that comes after a long line https://ibb.co/tJw2MhJ
tonicinhibition · 2022-02-26 21:07
Is that something like git-blame functionality?
darkawower · 2022-02-26 21:10
yep, this is it https://github.com/Artawower/blamer.el
fragglestickcar0 · 2022-02-26 23:08
I want to leave you (and every long-winded hand-waver on stackoverflow) twisting in the wind. But perhaps instead I can politicize this so that at least one questioner (you), and possibly more, would stop confounding answerers. Recently some ass clown tried pushing a formal elisp test protocol similar to a github gist. It was a dumb, overwrought idea, but it would have made your question well-defined and consequently, answerable. The key is include a test snippet. So you would formulate the question like: (with-temp-buffer ;; Under visual-line-mode, I want character distance ;; from the logical line end to the right fringe. ;; Please define nchars-to-fringe for me. (require 'cl-lib) (visual-line-mode) (save-excursion (insert (make-string (/ (window-width) 2) ?x) " " (make-string (window-width) ?x))) (cl-assert (zerop (nchars-to-fringe)) t)) And I could then answer: (defun nchars-to-fringe () (- (window-width) (save-excursion (end-of-line) (- (point) (beginning-of-visual-line))))) In nearly every case on stackoverflow, the questioner's tortuous "here's what I tried" is ignored.
darkawower · 2022-02-26 23:47
Thanks for answer, didn't know about `beginning-of-visual-line` function As for the clarity of the question... I tried to ask it the way I understand it, if I knew exactly what I was looking for, or how to phrase it better, I could find it on google. Be a little kinder, not all things are equally obvious to all people.
7890yuiop · 2022-02-27 08:47
You've written firstly "till end of line" and subsequently "to the end of the string" and apparently didn't mean *either* of those things, so it's hardly surprising if people were confused. Consider editing your text for better clarity.
darkawower · 2022-02-27 09:34
Oh, okay, that makes sense. In my native language it has a very similar meaning. My bad, I edited it.
jsled · 2022-02-27 13:15
Seriously, u/fragglestickcar0, there's just no need for this sort of vitriol; please keep your stick on the ice.
tonicinhibition · 2022-02-27 21:03
Very nice!
nv-elisp · 2022-02-27 23:45
> Recently some ass clown tried pushing a formal elisp test protocol similar to a github gist. It was a dumb, overwrought idea, but it would have made your question well-defined and consequently, answerable. Is that [yodel](https://www.github.com/progfolio/yodel) you're thinking of?
fragglestickcar0 · 2022-02-28 00:00
Yes! Pretty lame idea, don't you think?
nv-elisp · 2022-02-28 00:12
I wrote it. So obviously not. I'm open minded, though. Let me have it. Why is it a lame idea?
arthurno1 · 2022-02-28 22:44
> I need to find the number of characters from the end-of-line position to the maximum window width It is 0 (zero). Otherwise, it wouldn't be the end of the line. By the way, you said 'till' in the title. Make up your mind. > Is there way to compute count of character till end of line