1

I have the following emacs script from here

#!/usr/local/bin/emacs --script
;;; rcat --- cat
;;; Commentary:
;;; cat function in elisp

;;; Code:

;;-*- mode: emacs-lisp;-*-

(defun process (string)
  "Just reverse the STRING."
  (concat (nreverse (string-to-list string))))

(condition-case nil
    (let (line)
      ;;commented out b/c not relevant for `cat`, but potentially useful
      (princ "argv is ")
      (princ argv)
      (princ "\n")
      (princ "command-line-args is" )
      (princ command-line-args)
      (princ "\n")

      (while (setq line (read-from-minibuffer ""))
        (princ (process line))
        (princ "\n")))
  (error nil))

(provide 'rcat)
;;; rcat ends here

It works well but I want to use the s package for string manipulation, and convert my script to this:

#!/usr/local/bin/emacs --batch
;;; rcat --- cat
;;; Commentary:
;;; reverse lines cat function in elisp

;;; Code:

;;-*- mode: emacs-lisp;-*-
(require 's)

(condition-case nil
    (let (line)
      ;;commented out b/c not relevant for `cat`, but potentially useful
      (princ "argv is ")
      (princ argv)
      (princ "\n")
      (princ "command-line-args is" )
      (princ command-line-args)
      (princ "\n")

      (while (setq line (read-from-minibuffer ""))
        (princ (s-reverse line))
        (princ "\n")))
  (error nil))

(provide 'rcat)
;;; rcat ends here

The first code works well:

./rcat 1 2 3 4 < stuff.txt                           
argv is (1 2 3 4)
command-line-args is(/usr/local/Cellar/emacs/HEAD-5583e64_1/Emacs.app/Contents/MacOS/Emacs -scriptload ./rcat 1 2 3 4)
dcba
4321

But executing the second, I get:

Cannot open load file: No such file or directory, s

I'm not sure if using cask, batch-mode or something should work.

1

2 Answers 2

4

You have to tell Emacs where to find the s library:

(add-to-list 'load-path "/path/to/s/")

Or if you put s in the same directory:

(add-to-list 'load-path ".")
3

--batch doesn't process your init file, so it has no idea where your personal libraries are.

Instead of (require 's) (which, as per clemera's answer, means you need to set load-path), you could simply (load "path/to/s.elc") for any library you need (or s.el if it's not byte-compiled; or just s for Emacs to pick the most appropriate extension automatically).

2
  • Loading it directly gives Debugger entered--Lisp error: (file-error "Cannot open load file" "Is a directory" "xxxxxxxxxxxxxx")
    – absuu
    Commented Mar 11, 2023 at 11:49
  • 1
    You have to load the .elc or .el file. I've edited the answer to make that more obvious. You can leave off the extension (as I'd done originally), but it's still the elisp file you need to indicate; not the containing directory.
    – phils
    Commented Mar 12, 2023 at 4:32

Your Answer

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

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