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?