user912475
user912475

Reputation:

How to launch a shell script with an emacs keybinding, passing the word under the cursor as a variable

Executing the script below on osx via emacs, didn't work, I got a permission denied message, the answer to this question solved that problem: https://stackoverflow.com/a/12276562/912475

Tldr: How can I set up a system to automatically pass the word under the cursor in emacs directly to my shell script as a variable and then run that script?

I've created a somewhat rudimentary system for "linking" to folders from plain text files in a robust way. It uses a timestamp that's generated by a script and then set as the "value" of the clipboard. From the clipboard, the timestamp is then pasted into a txt with related notes and into the name field of a folder.

To find the folders when reading the txt I use an emacs function with a keybinding that makes it possible to copy the timestamp (as a word, it's all numbers) to the clipboard and search for it in spotlight (on osx). What I'd like to do instead is to automatically launch a shell script that searches for a directory whose name ends with that string and then opens it. I already have a script that does something like that, but I don't really know how to tie the elisp function and shell script together. I'd greatly appreciate a solution that works on either osx and linux (I use both). It will probably be easy to "port" a solution that works on either one for use with the other.

This is the emacs function for copying a word under the cursor:

;;; function for copying a word under the cursor http://www.emacswiki.org/emacs/CopyWithoutSelection
    (global-set-key (kbd "C-c o")         (quote copy-word))

     (defun copy-word (&optional arg)
      "Copy words at point into kill-ring"
       (interactive "P")
       (copy-thing 'backward-word 'forward-word arg)
       ;;(paste-to-mark arg)
     )

This is the script to find and open the directory whose name ends with the timestamp:

#!/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:$PATH #need this to make the gnu coreutils work on osx
file_number="20130812193913"
path_to_open=$(gfind ~/x/ | grep -e $file_number$) # $ means the end of the line, makes it possible to search for directories without finding their content
open "${path_to_open}"

An edited version of the script that accepts arguments from the commandline like this:

me$ sh script_path.sh 20130812193913

The script:

#!/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:$PATH #need this to make the gnu coreutils work on osx
file_number=$1
echo $file_number
path_to_open=$(gfind ~/x/ | grep -e $file_number$) # $ means the end of the line, makes it possible to search for directories without finding their content
open "${path_to_open}"

See: http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html

Upvotes: 1

Views: 634

Answers (1)

abo-abo
abo-abo

Reputation: 20342

You could try something like this:

(defvar script-name "/foo/bar/my-script")

(defun call-my-script-with-word ()
  (interactive)
  (shell-command
   (concat script-name 
           " "
           (thing-at-point 'word))))
(global-set-key (kbd "C-c o") 'call-my-script-with-word)

Upvotes: 2

Related Questions