0

I have written the following function to shorten a string:

 (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) 

However, I would like to modify this to work with two key bindings, C-c M-f and C-c M-f, which works in the same way that M-BKSP vs M-d works. That is, C-c M-f will shorten the word after the cursor, and C-c M-b shortens the word before the cursor.

1 Answer 1

0

I've written the following solution to my question, but I am sure someone else will have a better way of doing it.

(defun shorten-hash (word-motion-func) "Shorten string (forward or backwards) to eight characters; particularly for shortening hashes" (let* ((point-start (point)) (point-stop (progn (funcall word-motion-func) (point))) (word (buffer-substring point-start point-stop))) (if (> (length word) 8) (progn (delete-region point-start point-stop) (insert (substring word 0 8))) (message "Cannot shorten word to eight characters")))) (defun shorten-hash-forward () (interactive) (shorten-hash 'forward-word)) (defun shorten-hash-backward () (interactive) (shorten-hash 'backward-word)) (global-set-key (kbd "C-c M-f") 'shorten-hash-forward) (global-set-key (kbd "C-c M-b") 'shorten-hash-backward) 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.