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.

1
  • 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". Commented Oct 9, 2023 at 2:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.