1

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.

  • Are you sure a text editor is the right tool? Note: a shell script is not the right tool to process text. – Fólkvangr Jul 25 at 11:30
  • 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 at 16:54
  • Consequently, your command should provide a result without relying on Emacs: this is not the case. – Fólkvangr Jul 26 at 6:13
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.

  • 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 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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