Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

57 řádky
2.3 KiB

  1. ;;; Clang-format emacs integration for use with C/Objective-C/C++.
  2. ;; This defines a function clang-format-region that you can bind to a key.
  3. ;; A minimal .emacs would contain:
  4. ;;
  5. ;; (load "<path-to-clang>/tools/clang-format/clang-format.el")
  6. ;; (global-set-key [C-M-tab] 'clang-format-region)
  7. ;;
  8. ;; Depending on your configuration and coding style, you might need to modify
  9. ;; 'style' in clang-format, below.
  10. (require 'json)
  11. ;; *Location of the clang-format binary. If it is on your PATH, a full path name
  12. ;; need not be specified.
  13. (defvar clang-format-binary "clang-format")
  14. (defun clang-format-region ()
  15. "Use clang-format to format the currently active region."
  16. (interactive)
  17. (let ((beg (if mark-active
  18. (region-beginning)
  19. (min (line-beginning-position) (1- (point-max)))))
  20. (end (if mark-active
  21. (region-end)
  22. (line-end-position))))
  23. (clang-format beg end)))
  24. (defun clang-format-buffer ()
  25. "Use clang-format to format the current buffer."
  26. (interactive)
  27. (clang-format (point-min) (point-max)))
  28. (defun clang-format (begin end)
  29. "Use clang-format to format the code between BEGIN and END."
  30. (let* ((orig-windows (get-buffer-window-list (current-buffer)))
  31. (orig-window-starts (mapcar #'window-start orig-windows))
  32. (orig-point (point))
  33. (style "file"))
  34. (unwind-protect
  35. (call-process-region (point-min) (point-max) clang-format-binary
  36. t (list t nil) nil
  37. "-offset" (number-to-string (1- begin))
  38. "-length" (number-to-string (- end begin))
  39. "-cursor" (number-to-string (1- (point)))
  40. "-assume-filename" (buffer-file-name)
  41. "-style" style)
  42. (goto-char (point-min))
  43. (let ((json-output (json-read-from-string
  44. (buffer-substring-no-properties
  45. (point-min) (line-beginning-position 2)))))
  46. (delete-region (point-min) (line-beginning-position 2))
  47. (goto-char (1+ (cdr (assoc 'Cursor json-output))))
  48. (dotimes (index (length orig-windows))
  49. (set-window-start (nth index orig-windows)
  50. (nth index orig-window-starts)))))))