LoginSignup
7
8

More than 5 years have passed since last update.

major-mode 見て code beautify する elisp

Last updated at Posted at 2014-03-19

emacs の major-mode みて、perltify つかったり js-beautify つかったりを自動で分ける。

(defun my-tidy-region (begin end)
  (interactive "r")
  (save-match-data
    (save-restriction
      (my-tidy begin end)
      )))

(defun my-tidy-buffer ()
  (interactive)
  (let (cpoint)
    (setq cpoint (point))
    (save-excursion
      (save-match-data
        (save-restriction
          (mark-whole-buffer)
          (my-tidy (point-min) (point-max)))))
    (goto-char cpoint)
    ))

(defun my-tidy (begin end)
  (let ((current-major-mode major-mode) cmd)
    (setenv "LC_ALL" "C")
    (setenv "LANG" "C")
    (message (format "%s" current-major-mode))
    (cond
     ((string= "cperl-mode" current-major-mode) (setq cmd "perltidy -q"))
     ((string= "js3-mode" current-major-mode) (setq cmd "js-beautify.js -f - --good-stuff"))
     )
    (if (not (null cmd))
        (shell-command-on-region begin end cmd nil t)
      (progn (indent-region begin end)
             (delete-trailing-whitespace)
             (delete-blank-lines)
             (untabify (point-min) (point-max))
             )
      )))

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8