(defun do-lines (fun &optional start end) "Invoke function FUN on the text of each line from START to END." (interactive (let ((fn (intern (completing-read "Function: " obarray 'functionp t)))) (if (use-region-p) (list fn (region-beginning) (region-end)) (list fn (point-min) (point-max))))) (save-excursion (goto-char start) (while (< (point) end) (funcall fun (buffer-substring (line-beginning-position) (line-end-position))) (forward-line 1))))
Update after your comment -
Now it seems that you do not want to enter the name of the function, but press the key, and a binding to this key is applied to each line in the region (or buffer).
Something like the following will do this. However, keep in mind that a team often has special lines of behavior. For example, if you manage to press the Ck ( kill-lines ) key, then it already moves forward after each killed line. Since do-lines does not know which function (command) you will call, it goes to the next line after each call. For a command such as kill-lines , this will not do what it will: it will lead to the advancement of two lines, not one, thereby skipping lines. IOW, keep in mind that the code for do-lines cannot compensate for what a particular function can call, which it can call, which might not be as expected. Instead, he does what he says.
(defun do-lines (command &optional start end) "Invoke COMMAND on the text of each line from START to END." (interactive (let* ((key (read-key-sequence-vector "Hit key sequence: ")) (cmd (lookup-key global-map key t))) (when (numberp cmd) (error "Not a valid key sequence")) (unless (commandp cmd) (error "Key `%s' is not defined" (key-description key))) (if (use-region-p) (list cmd (region-beginning) (region-end)) (list cmd (point-min) (point-max))))) (setq start (copy-marker start) end (copy-marker end)) (save-excursion (goto-char start) (while (< (point) end) (funcall command (buffer-substring (line-beginning-position) (line-end-position))) (forward-line 1))))
source share