[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Function to replace spaces with underscores

From: pico77
Subject: Function to replace spaces with underscores
Date: Tue, 17 Sep 2013 00:39:35 -0700 (PDT)

Dear all, 

at the end of this post there is a function that I load in my .emacs file.
The main goal is to take a selected line and remove all spaces with
underscore. This is very useful to produce file-names from copy/past
consistently without spaces. But there is a problem with that. 

If I open emacs select a line ad do M-x s2u then it works fine, but if I do
it further it requires to apply the function 3 times before to get it done.
So the problem is really that probably the function is programmed in a way
that after the first applucation of s2u there is something which has to be
cleaned up before to re-apply the same function. 

I am not a lisp/emacs expert and the function was passed by a friend who is
now not able to solve the problem. I hope somebody out there can help to
find the bug!

Best Regards

;; Usage: select a line than do M-x s2u 
 (defun s2u ()
  "Cyclically replace {underscore, space, hypen} chars current line or text
When called repeatedly, this command cycles the {“ ”, “_”, “-”} characters."
  ;; this function sets a property 「'state」. Possible values are 0 to length
of charArray.
  (let (mainText charArray p1 p2 currentState nextState changeFrom
             changeTo startedWithRegion-p )

    (if (region-active-p)
          (setq startedWithRegion-p t )
          (setq p1 (region-beginning))
          (setq p2 (region-end))
      (progn (setq startedWithRegion-p nil ) 
             (setq p1 (line-beginning-position))
             (setq p2 (line-end-position)) ) )

    (setq charArray [" " "_" "-"])

    (setq currentState
          (if (get 's2u 'state) 
              (get 's2u 'state)
    (setq nextState (% (+ currentState 1) (length charArray)))

    (setq changeFrom (elt charArray currentState ))
    (setq changeTo (elt charArray nextState ))

    (setq mainText (replace-regexp-in-string changeFrom changeTo
(buffer-substring-no-properties p1 p2)) )
    (delete-region p1 p2)
    (insert mainText)
    (put 's2u 'state nextState)

    (when startedWithRegion-p 
      (goto-char p2)
      (set-mark p1)
      (setq deactivate-mark nil) ) ) )

View this message in context:
Sent from the Emacs - Help mailing list archive at

reply via email to

[Prev in Thread] Current Thread [Next in Thread]