magnars/s.el

Functions not visibile despite "require" mention

pkhoueiry opened this issue · 6 comments

Dear Magnar,
Many thanks for the wonderful packages that you provide.
I came across "s.el" that I downloaded via elap. I find it contains many interesting functions. Despite the mention (require 's), the functions are not visibile from within emacs buffers.

However, as example, M-x describe-function RET s-collapse-whitespace works well

Is there a way to make these functions applicable on marked text in emacs buffers ?

Here are the two lines relative to s.el in my .emacs
(add-to-list 'load-path "~/.emacs.d/elpa/s-20140714.707")
(require 's)

Many thanks in advance ...
P.K

rnkn commented

s is more a library of functions for use within other packages, not really intended for direct use by the user. You'll notice that none of the functions have (interactive) in the body, meaning they cannot be called via M-x or assigned to a keybinding.

In order to use s functions on buffer text, you'll need to write functions that do this. With your use case, you might be better off using C-M-% <query-replace-regexp> or, here is example or how to use s-collapse-whitespace in an interactive function:

(defun -s-collapse-whitespace (beg end)
  (interactive "r")
  (let ((s (buffer-substring beg end)))
    (delete-region beg end)
    (insert (s-collapse-whitespace s))))

n.b. - prefix prevents overwriting the s function.

Dear rnkn,
Thanks for your reply. With time I got the point that "s" is meant to be called from scripts (with lack of interactive) but due to my limited Lisp knowledge, I couldn't figure out how to adapt it to my need.

Your solution is very good. As the "s" package contains so many useful and handy functions, I will follow your example to adapt many of them...

BTW, is there a fast way to make them all "interactive" or do we have to re-write them one by one ?

Best and thanks again,

rnkn commented

I think you might be better off with a combination of C-M-% and regexp, e.g. the above could easily be accomplished by selecting the text then C-M-% "[ ]+" RET " " (without quotes). s really is more for internal use.

I am facing the same issue as pkhoueiry. When i do describe-function for s-index-of, it works. but, when i call it from my interactive script, it failed. again, i went with rnkn's idea of defining a new function with interactive form added. even post that fix, it didn't work & it says, "-s-index-of: Symbol's function definition is void: ".
my .emacs file change for s.el,,

(load-file "~/.emacs.d/others/s.el")

overwritten function:

(defun -s-index-of (needle s &optional ignore-case)
  (interactive "r")
  (let ((case-fold-search ignore-case))
    (string-match-p (regexp-quote needle) s)))

This does not look like the same issue. You're calling s from a script, as intended. Seems like there are some problems with loading, tho. I cant spot it from your code - it looks perfectly fine. The only strange thing is that you're using load-file instead of the normally used require, but that shouldn't cause the issues you're seeing.

hi magnars, my previous comment is a non-issue... your reply helped me understand it better.
i see your s.el is very popular & i liked it. but one commonly used function, last-index-of a character (or) pattern in a string is missing in the api catalog.
i tried to implement it using elisp string-match, but since it can't be tuned to search from backward, i couldn't do it.