all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Function to replace spaces with underscores
@ 2013-09-17  7:39 pico77
  2013-09-17  8:20 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: pico77 @ 2013-09-17  7:39 UTC (permalink / raw)
  To: Help-gnu-emacs


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
Pietro



;; SPACE TO UNDERSCORE
;; Usage: select a line than do M-x s2u 
;;
 (defun s2u ()
  "Cyclically replace {underscore, space, hypen} chars current line or text
selection.
When called repeatedly, this command cycles the {“ ”, “_”, “-”} characters."
  (interactive)
  ;; 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)
        (progn
          (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)
            0))
    (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: http://emacs.1067599.n5.nabble.com/Function-to-replace-spaces-with-underscores-tp297516.html
Sent from the Emacs - Help mailing list archive at Nabble.com.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-09-17 19:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.2297.1379403955.10748.help-gnu-emacs@gnu.org>
2013-09-17 18:04 ` Function to replace spaces with underscores Pascal J. Bourguignon
2013-09-17 19:27   ` Barry Margolin
2013-09-17  7:39 pico77
2013-09-17  8:20 ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.