Coder Social home page Coder Social logo

string-inflection's Introduction

underscore -> UPCASE -> CamelCase conversion of names



Change History

Configuration Examples

Example 1

(require 'string-inflection)

;; C-q C-u is similar to the keybinding used by Vz Editor.
(global-unset-key (kbd "C-q"))
(global-set-key (kbd "C-q C-u") 'my-string-inflection-cycle-auto)

(defun my-string-inflection-cycle-auto ()
  "switching by major-mode"
  (interactive)
  (cond
   ;; for emacs-lisp-mode
   ((eq major-mode 'emacs-lisp-mode)
    (string-inflection-all-cycle))
   ;; for python
   ((eq major-mode 'python-mode)
    (string-inflection-python-style-cycle))
   ;; for java
   ((eq major-mode 'java-mode)
    (string-inflection-java-style-cycle))
   ;; for elixir
   ((eq major-mode 'elixir-mode)
    (string-inflection-elixir-style-cycle))
   (t
    ;; default
    (string-inflection-ruby-style-cycle))))

Example 2

(require 'string-inflection)

;; default
(global-set-key (kbd "C-c C-u") 'string-inflection-all-cycle)

;; for ruby
(add-hook 'ruby-mode-hook
          '(lambda ()
             (local-set-key (kbd "C-c C-u") 'string-inflection-ruby-style-cycle)))

;; for elixir
(add-hook 'elixir-mode-hook
          '(lambda ()
             (local-set-key (kbd "C-c C-u") 'string-inflection-elixir-style-cycle)))

;; for java
(add-hook 'java-mode-hook
          '(lambda ()
             (local-set-key (kbd "C-c C-u") 'string-inflection-java-style-cycle)))

;; for python
(add-hook 'python-mode-hook
          '(lambda ()
             (local-set-key (kbd "C-c C-u") 'string-inflection-python-style-cycle)))

How to Use

For each of the following, place the cursor at emacs_lisp and type C-q C-u, the results will be as follows:

In the case of string-inflection-ruby-style-cycle

emacs_lisp => EMACS_LISP => EmacsLisp => emacs_lisp

In the case of string-inflection-elixir-style-cycle

emacs_lisp => EmacsLisp => emacs_lisp

In the case of string-inflection-python-style-cycle

emacs_lisp => EMACS_LISP => EmacsLisp => emacs_lisp

In the case of string-inflection-java-style-cycle

emacsLisp => EMACS_LISP => EmacsLisp => emacsLisp

In the case of string-inflection-all-cycle

emacs_lisp => EMACS_LISP => EmacsLisp => emacsLisp => emacs-lisp => Emacs_Lisp => emacs_lisp

It is recommended that the major mode functions are used instead of string-inflection-all-cycle.

Standalone Functions

(string-inflection-underscore-function "EmacsLisp")           ; => "emacs_lisp"
(string-inflection-pascal-case-function "emacs_lisp")         ; => "EmacsLisp"
(string-inflection-camelcase-function "emacs_lisp")           ; => "emacsLisp"
(string-inflection-upcase-function "emacs_lisp")              ; => "EMACS_LISP"
(string-inflection-kebab-case-function "emacs_lisp")          ; => "emacs-lisp"
(string-inflection-capital-underscore-function "emacs_lisp")  ; => "Emacs_Lisp"

(string-inflection-pascal-case-p "EmacsLisp")                 ; => t
(string-inflection-pascal-case-p "emacs_lisp")                ; => nil
; etc...

Region usage

You can also use this library to convert a region’s casing.

For that, simply select a region and perform M-x string-inflection-kebab-case (or any such other function).

string-inflection's People

Contributors

akicho8 avatar awr1 avatar errissa avatar fvaresi avatar knu avatar lassik avatar laurencewarne avatar oldhammade avatar purcell avatar sdwolfz avatar vemv avatar vindarel avatar vkazanov avatar wjlroe avatar wqer555 avatar wyuenho avatar yasuyk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

string-inflection's Issues

Is there a way to make this work when the point is at the end of the string

When point is at the end of the word and I try to use string-inflection I get an error code 2 message and the string doesn't change.

Is it possible to make it work when the point is at the end of the word, this is useful when typing in mini buffer

Once I start typing some word, If I want to change the case I have to go to the beginning of the word. Ideally I want to stay at the current position(which is end here) and still change the case.

Please stop bundling third-party libraries

string-inflection is mirrored on the Emacsmirror, which is a large up-to-date collection of Emacs packages.

As the maintainer of the mirror I am trying to resolve feature conflicts that result from one package bundling libraries from another package. I suspect in most cases these libraries were included so that users would not have to find, download and install each dependency manually.

Unfortunately bundling also has negative side-effects: if the bundled libraries are also installed separately, then it is undefined which version actually gets loaded when the respective feature is required.

Initially that isn't a big problem but in many cases upstream changes are not included or only after a long delay. This can be very confusing for users who are not aware that some of the installed packages bundle libraries which are also installed separately. In other cases bugs are fixed in the bundled versions but the fixes are never submitted to upstream.

Also now that Emacs contains the package.el package manager there is a better way to not require users to manually deal with dependencies: add the package (and when that hasn't been done yet the dependencies) to the Melpa package repository. If make is required to install your make you might want to add it to the el-get (another popular package manager) package repository instead.

Alternatively if you want to keep bundling these libraries please move them to a directory only containing bundled libraries and add the file ".nosearch" to that directory. You can then load the library using something like this:

(or (require 'bundled nil t)
    (let ((load-path
           (cons (expand-file-name "fallback-libs"
                                   (or load-file-name buffer-file-name)
                                   load-path))))
      (require 'bundled)))

Of course if your version differs from the upstream version this might not be enough in which case you should make an effort to get your changes merged upstream.

string-inflection bundles at least the following libraries:

  • ert

Best regards,
Jonas

Proposed simplification

Hi:
I have seen the code and I wanted to simplify it a little bit the user customization. If you find it is useful you are free to modify it and use in the package's code:

(defcustom tests '(string-inflection-word-p
	      string-inflection-underscore-p
	      string-inflection-upcase-p
	      string-inflection-pascal-case-p
	      string-inflection-camelcase-p
	      string-inflection-kebab-case-p
	      ))
(defcustom funcs [
	     string-inflection-upcase-function
	     string-inflection-upcase-function
	     string-inflection-pascal-case-function
	     string-inflection-camelcase-function
	     string-inflection-kebab-case-function
	     string-inflection-capital-underscore-function
	     string-inflection-underscore-function
	     ])

(defun string-inflection-all-cycle-function (var)
  (interactive)
  (let ((temp tests)
	(it 0))
    (while temp
      (if (funcall (car temp) var)
	  (progn
	    (funcall (aref funcs it) var)
	    (setq temp nil))
	(setq it (1+ it))
	(setq temp (cdr temp))
	(unless temp
	  (funcall (aref funcs it) var))))))

This code has 2 main advantages:

  1. It is simpler to add/modify the functions in the cycle or their order
  2. It is trivial to modify this code to implement a reverse order function.

Kebab case (hyphen-separated)

This is kind of a feature request,

I have been using this package and I am very happy with it 👍
I would love to convert strings to kebab-case too

emacs_lisp => EMACS_LISP => EmacsLisp => emacsLisp => emacs_lisp => emacs-lisp

Would someone else also be interested in such a thing?
Would you well come a PR with such a feature?

All best & thanks

A couple of ways to expand the packege

Thanks for the package! I was just thinking what would be the best way to do this when I saw the Emacs haiku in twitter.

I can see a couple of ways to make this package even more comprehensive while keeping it still focused on string infliction. At the moment the inflictions are all strictly related to changing variable names in various programming languages. There are at least two more general cases:

  1. Handle dash (-) as a separator.
  2. Make it aware of the selection. If the selection contains space character(s) or any combination of space, dash or underscore [ _-], change them into underscore.

emacs lisp code -> emacs_lisp_code
emacs lisp-code -> emacs_lisp_code

It would be great if you could implement these features!

cycling all does not wrap around?

first of all, awesome package. :)

repeatedly calling the string-inflection-all-cycle function seems not to wrap around. instead it stays stuck on noInitialCamelCase after cycling through a few variants first. i tried starting with foo_bar. am i missing something?

string-inflection doesn't work on words with umlauts

(I love string-inflection, thanks a lot for it.)

To me it seems like there is an assumption baked into this package:
Words contain only ascii characters.
Therefore it breaks for words that have umlauts or similar in them:

  • Käse
  • KäsesoßenRührlöffel
  • Löwenkäfig-Tür-Schließer

should be using bounds-of-thing-at-point

Instead of doing your own forward scanning for words, you should be using the emacs concept of 'symbol'. What emacs calls a symbol basically corresponds to a variable name in all language modes. This has the advantage of behaving properly in all modes (for example string inflection does not work properly in emacs-lisp-mode) and even when using subword mode. It would also make string-inflection behave more consistently with other emacs commands. Currently if point is right after a symbol string-inflection will scan forward for the next word, but this is inconsistent with other emacs commands which would operate on the preceding word.

It would also simplify your code. Instead of trying to scan forward for a word, just call (bounds-of-thing-at-point 'symbol). If it can't find something just return an error.

Custom cycle

Not so much of an issue than a comment, or maybe a simple feature request…

I really like the idea and approach of string-inflection, it's really handy. Now, none of the proposed cycles (Ruby or Java) matches my own needs (which would be: foo_bar => fooBar => foo-bar => foo_bar). Following the source code for string-inflection-XXX-style-cycle, I ended up writing my own custom cycle function, bound to C-c C-u:

  (defun string-inflection-custom-cycle ()
    "foo_bar => fooBar => foo-bar => foo_bar"
    (interactive)
    (string-inflection-insert
     (string-inflection-custom-cycle-function (string-inflection-get-current-word))))

  (fset 'string-inflection-cycle 'string-inflection-custom-cycle)

  (defun string-inflection-custom-cycle-function (str)
    "foo_bar => fooBar => foo-bar => foo_bar"
    (cond
     ((string-inflection-underscore-p str)
      (string-inflection-lower-camelcase-function str))
     ((string-inflection-lower-camelcase-p str)
      (string-inflection-kebab-case-function str))
     (t
      (string-inflection-underscore-function str))))

Would it be possible to have a simpler mechanism to set our own custom cycles?

Consecutive uppercase letters

Is this intentional?

(string-inflection-underscore-function "ASingleLine")

returns "asingle_line". I would have expected it to return "a_single_line".

Is this because of acronyms like PHP? So in this example, it could assume AS to be an acronym without human knowledge of the context.

Would it be a ridiculous idea to have a dictionary of common acronyms to cover cases like this?

Deletion of forward slashes?

I used string-inflection-underscore on some C code and it removed the forward slashes from the multi-line comments. Is this intentional?

Camel Case seems to be mislabeled.

Looking through the docs it seems camel case and lower camel case are mislabeled.

Pascal case is a subset of Camel Case where the first letter is capitalized.

That is, userAccount is a camel case and UserAccount is a Pascal case.

The conventions of using these are different. You use camel case for variables and Pascal case for Class names or Constructors.

It is easy to remember. Pascal is a proper noun so capitalize the first letter.
camel is a common noun, so you do not capitalize the first letter.

TL;DR

  • lower-camel-case should be camel-case
  • camel-case should be pascal-case

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.