emacs lisp: cycle hyphen underscore, lisp properties

Updated article.


(defun cycle-hyphen-underscore-space ()
  "Cyclically replace {underscore, space, hypen} chars current line or text selection.
When called repeatedly, this command cycles the {“ ”, “_”, “-”} characters."
  ;; this function sets a property 「'state」. Possible values are 0 to length of charList.
  (let (mainText charList 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 charList (list " " "_" "-" ))

    (setq currentState
          (if (get 'cycle-hyphen-underscore-space 'state) 
              (get 'cycle-hyphen-underscore-space 'state) 0))
    (setq nextState (% (+ currentState (length charList) 1) (length charList)))

    (setq changeFrom (nth currentState charList))
    (setq changeTo (nth nextState charList))

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

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

explanation and detail at http://xahlee.org/emacs/elisp_change_space-hyphen_underscore.html

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs