Converting from camelcase to _ in emacs

前端 未结 7 1965

Is there an emacs function to convert a camel-cased word to underscore? Something, like:

longVariableName

M-x to-underscore

相关标签:
7条回答
  • 2021-02-05 03:19

    I use this when converting C# code to PHP.

    (defun un-camelcase-word-at-point ()
      "un-camelcase the word at point, replacing uppercase chars with
    the lowercase version preceded by an underscore.
    
    The first char, if capitalized (eg, PascalCase) is just
    downcased, no preceding underscore.
    "
      (interactive)
      (save-excursion
        (let ((bounds (bounds-of-thing-at-point 'word)))
          (replace-regexp "\\([A-Z]\\)" "_\\1" nil
                          (1+ (car bounds)) (cdr bounds))
          (downcase-region (car bounds) (cdr bounds)))))
    

    And then in my php-mode fn:

    (local-set-key "\M-\C-C"  'un-camelcase-word-at-point)
    
    0 讨论(0)
  • 2021-02-05 03:26

    Use the string-inflection package, available on MELPA, or at https://github.com/akicho8/string-inflection.

    Useful keyboard shortcuts, copied from https://www.emacswiki.org/emacs/CamelCase :

    ;; Cycle between snake case, camel case, etc.
    (require 'string-inflection)
    (global-set-key (kbd "C-c i") 'string-inflection-cycle)
    (global-set-key (kbd "C-c C") 'string-inflection-camelcase)        ;; Force to CamelCase
    (global-set-key (kbd "C-c L") 'string-inflection-lower-camelcase)  ;; Force to lowerCamelCase
    (global-set-key (kbd "C-c J") 'string-inflection-java-style-cycle) ;; Cycle through Java styles
    
    0 讨论(0)
  • 2021-02-05 03:28

    @ens' answer was close, but a little buggy for me on Emacs 26.1. I fixed the bug and added the ability, via C-u prefix arg, to specify if you want the first letter of camel case to be lower case:

    (defun toggle-camelcase-underscores (first-lower-p)
      "Toggle between camelcase and underscore notation for the
    symbol at point. If prefix arg, C-u, is supplied, then make first
    letter of camelcase lowercase."
      (interactive "P")
      (save-excursion
        (let* ((bounds (bounds-of-thing-at-point 'symbol))
               (start (car bounds))
               (end (cdr bounds))
               (currently-using-underscores-p (progn (goto-char start)
                                                     (re-search-forward "_" end t))))
          (if currently-using-underscores-p
              (progn
                (replace-string "_" " " nil start end)
                (upcase-initials-region start end)
                (replace-string " " "" nil start end)
                (when first-lower-p
                  (downcase-region start (1+ start))))
            (replace-regexp "\\([A-Z]\\)" "_\\1" nil (1+ start) end)
            (downcase-region start (cdr (bounds-of-thing-at-point 'symbol)))))))
    
    0 讨论(0)
  • 2021-02-05 03:29

    I use the following for toggling between camelcase and underscores:

    (defun toggle-camelcase-underscores ()
      "Toggle between camelcase and underscore notation for the symbol at point."
      (interactive)
      (save-excursion
        (let* ((bounds (bounds-of-thing-at-point 'symbol))
               (start (car bounds))
               (end (cdr bounds))
               (currently-using-underscores-p (progn (goto-char start)
                                                     (re-search-forward "_" end t))))
          (if currently-using-underscores-p
              (progn
                (upcase-initials-region start end)
                (replace-string "_" "" nil start end)
                (downcase-region start (1+ start)))
            (replace-regexp "\\([A-Z]\\)" "_\\1" nil (1+ start) end)
            (downcase-region start (cdr (bounds-of-thing-at-point 'symbol)))))))
    
    0 讨论(0)
  • 2021-02-05 03:33
    (progn (replace-regexp "\\([A-Z]\\)" "_\\1" nil (region-beginning) (region-end))
           (downcase-region (region-beginning) (region-end)))
    
    0 讨论(0)
  • 2021-02-05 03:41

    There is now another general way in 2018: magnars/s.el: The long lost Emacs string manipulation library. - github.com, some examples regarding OP's question:

    1. whatever case to snake case(underscore seperated):

      (s-snake-case "some words") ;; => "some_words"
      (s-snake-case "dashed-words") ;; => "dashed_words"
      (s-snake-case "camelCasedWords") ;; => "camel_cased_words"
      
    2. whatever case to lower camel case:

      (s-lower-camel-case "some words") ;; => "someWords"
      (s-lower-camel-case "dashed-words") ;; => "dashedWords"
      (s-lower-camel-case "under_scored_words") ;; => "underScoredWords"
      

    see more examples at its repo.

    0 讨论(0)
提交回复
热议问题