11

I have tried in two different ways to write my function. I decided to write a small function to convert to camel case and back with this elisp string library. At first via searching I found this tutorial on replacing things at point and made this function:

; use string manipulation library to switch between camel and snake (s.el)
(defun my_test ()
  "test"
  (interactive)
  ;; get current selection or word
  (let (bds p1 p2 inputStr resultStr)
    ;; get boundary
    (if (use-region-p)
        (setq bds (cons (region-beginning) (region-end) ))
      (setq bds (bounds-of-thing-at-point 'word)) )
    (setq p1 (car bds) )
    (setq p2 (cdr bds) )
    ;; grab the string
    (setq inputStr (buffer-substring-no-properties p1 p2)  )
    (setq resultStr (s-lower-camel-case inputStr))
    (message inputStr)

    (delete-region p1 p2 ) ; delete the region
    (insert resultStr) ; insert new string
    )
)

This does not modify resultStr as expected and just repasts inputStr in there.

What I don't understand about this is that when I eval (with M-:) (setq resultStr (s-lower-camel-case "other_string")) I get the expected result ("otherString")

I even tried a different (and better for my purposes) way of writing the function inspired by this SO question:

(defun change-word-at-point (fun)
  (cl-destructuring-bind (beg . end)
      (bounds-of-thing-at-point 'word)
    (let ((str (buffer-substring-no-properties beg end)))
      (delete-region beg end)
      (insert (funcall fun str)))))

(defun my_test_camel ()
  (interactive)
  (change-word-at-point 's-lower-camel-case))

which suffers from the same problem. This makes me think that there is something wrong with the s-lower-camel-case function (or how I am calling it) but that works when called from eval as mentioned above

EDIT: modified first function to include let syntax, see comments

EDIT #2: Both of these functions work correctly, the answer has been accepted as it provides a better alternative with the information on symbol and the correct way of writing it. My problem was testing which was due to haskell-mode. New question is here

6
  • Note, as mentioned before the s-lower-camel-case function is from s.el located here
    – Mike H-R
    Aug 7, 2014 at 17:04
  • I don't think delete-region moves point, while insert inserts its arguments at point. Could that be part of the problem?
    – user725091
    Aug 7, 2014 at 17:10
  • 1
    Also, in your first version of the function, you should probably bind local variables using (let* ((bds …) (p1 …) (p2 …)) … instead of setq (which will operate on the global variable by that name when there is no surrounding let binding).
    – user725091
    Aug 7, 2014 at 17:11
  • I think you're right in that the region is deleted, and then the text is inserted so the point will jump to the end of the new text. However the text inserted is still wrong and that's not much of an issue for me.
    – Mike H-R
    Aug 7, 2014 at 17:12
  • @JonO. Also, you are right, I removed that while debugging things, I'll edit to reinsert it and fix this, thanks.
    – Mike H-R
    Aug 7, 2014 at 17:12

1 Answer 1

14

Here's an alternate definition. The comment was correct that you need to do local bindings via let. Note that this version uses the region if it's active, or else uses bounds-of-thing-at-point to get the word at point if no region is active:

(defun word-or-region-to-lcc ()
  "Convert word at point (or selected region) to lower camel case."
  (interactive)
  (let* ((bounds (if (use-region-p)
                     (cons (region-beginning) (region-end))
                   (bounds-of-thing-at-point 'symbol)))
         (text   (buffer-substring-no-properties (car bounds) (cdr bounds))))
    (when bounds
      (delete-region (car bounds) (cdr bounds))
      (insert (s-lower-camel-case text)))))

If you didn't care about the option to use region, you could bind text locally to (thing-at-point 'symbol) instead of the call to buffer-substring-no-properties.

UPDATE. It turns out you can use (thing-at-point 'symbol) rather than (thing-at-point 'word) to get the full symbol for snake case.

6
  • This has the same problems as my implementation in that it simply replaces the text with itself (not modifying it). Is there some reason a function could work fine when I call it with eval (with M-: and not work when called from an interactive function?
    – Mike H-R
    Aug 7, 2014 at 17:29
  • Just to check, does it work for you? have I got some weird configuration issue that is messing this up?
    – Mike H-R
    Aug 7, 2014 at 17:30
  • It works for me: putting point somewhere in MyVariableHere and calling the function yields myVariableHere. Are you trying to go from my_variable_here to myVariableHere? If so, you probably need to use the region to select the whole thing, as _ may be a word boundary.
    – Dan
    Aug 7, 2014 at 17:32
  • hmmm... it works in scratch but it doesn't work in haskell-mode which is where I had been testing it (same with all the other functions) as the buffer was open. That is very odd. I don't suppose you have any idea how that could happen?
    – Mike H-R
    Aug 7, 2014 at 17:38
  • I don't use haskell-mode so can't comment other than to wonder about what counts as word boundaries. See also updated answer which allows you to use it on snake case without needing to select the region.
    – Dan
    Aug 7, 2014 at 17:40

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.