0

I have written the following function to shorten a string, which is useful particularly when shortening commit hashes:

 (defun shorten-hash ()
  "Shorten string (forward or backwards) to eight characters;
   particularly for shortening hashes"
  (interactive)
  (let* ((bounds (bounds-of-thing-at-point 'word))
         (word (if bounds
                   (buffer-substring (car bounds) (cdr bounds))
                 nil))
         (substring (if (and word (>= (- (cdr bounds) (car bounds)) 7))
                        (substring word 0 8)
                      nil)))
    (if substring
        (progn
          (delete-region (car bounds) (cdr bounds))
          (insert substring))
      (message "Cannot shorten word to eight characters"))))

(global-set-key (kbd "C-c M-f") 'shorten-hash)
(global-set-key (kbd "C-c M-b") 'shorten-hash)

However, I thought I'd need to use two different functions to work on words in front of and before the cursor. However, it just seems to work. Why is that?

1 Answer 1

0

Your question is "Why is that?"?

bounds-of-thing-at-point returns the beginning and end of the thing at point, in this case the word at point. The word can begin before point, as long as it doesn't end before point (actually, it can end just before point). The word can't begin after point.

2
  • 1
    Hi @JakeIreland, This site works on a concept of one question and one answer, and this seems to answer your question. If you have another question, please ask a new one. In this particular scenario, do be sure you explain, in the new question, what you mean by "expected functionality".
    – Trevoke
    Commented Oct 9, 2023 at 2:26
  • 1
    @Trevoke thanks Commented Oct 9, 2023 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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