2

I have this small snippet of Elisp code that decodes kernel traces:

(defvar projbase "/my/proj/base")
(setq my_shell_output
  (substring 
   (shell-command-to-string (concat "/home/demetra/scripts/eaddrs2line.sh "
                                    projbase " " <each-line> ))
   0 -1))

How to write an Elisp function that calls this snippet of code for each line in a region and accumulate the results in a list ? The return is of format "module-name function-name file-path/file-name line-num".

After this is done I can work on the next task to show the list in a helm buffer where going to each line will open the file-path/file-name at line-num.

1
  • I chose this path because, the shell script can be used both from command line (for non-emacs users) as well as emacs editor. I'm trying to use it in emacs because that's where I view the source files.
    – maindoor
    Jul 25, 2019 at 16:54

1 Answer 1

1

A few functions that might be useful here:

  • (buffer-substring-no-properties BEG END) :: collect the buffer contents between BEG and END, two locations in the buffer (such as the beginning and end of a region), as a string
  • (split-string STRING "\n") :: split STRING into a list of strings, broken at each linebreak
  • (mapcar FUNCTION LIST) :: apply the function FUNCTION to every element in a LIST, and collect the results in a list.

In your case, something like this:

(mapcar (lambda (X) <SHELL-COMMAND-TO-STRING on X>)
        (split-string (buffer-substring-no-properties BEG END) "\n"))

Replacing <SHELL-COMMAND-TO-STRING on X> with the appropriate code. You'll still need to figure out how to get the values of BEG and END (maybe using (interactive "r")), and also you'll need to store/use the list returned by the mapcar.

1
  • thank you your snippet was very useful. Is there anyway I can ignore empty lines in selected region during (buffer-substring-no-properties BEG END)?
    – maindoor
    Jul 26, 2019 at 5:26

Your Answer

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

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