Debian Codegraph @

clang/tools/clang-rename/clang-rename.el

raw
1 ;;; clang-rename.el --- Renames every occurrence of a symbol found at <offset>. -*- lexical-binding: t; -*-
2
3 ;; Version: 0.1.0
4 ;; Keywords: tools, c
5
6 ;;; Commentary:
7
8 ;; To install clang-rename.el make sure the directory of this file is in your
9 ;; `load-path' and add
10 ;;
11 ;; (require 'clang-rename)
12 ;;
13 ;; to your .emacs configuration.
14
15 ;;; Code:
16
17 (defgroup clang-rename nil
18 "Integration with clang-rename"
19 :group 'c)
20
21 (defcustom clang-rename-binary "clang-rename"
22 "Path to clang-rename executable."
23 :type '(file :must-match t)
24 :group 'clang-rename)
25
26 ;;;###autoload
27 (defun clang-rename (new-name)
28 "Rename all instances of the symbol at point to NEW-NAME using clang-rename."
29 (interactive "sEnter a new name: ")
30 (save-some-buffers :all)
31 ;; clang-rename should not be combined with other operations when undoing.
32 (undo-boundary)
33 (let ((output-buffer (get-buffer-create "*clang-rename*")))
34 (with-current-buffer output-buffer (erase-buffer))
35 (let ((exit-code (call-process
36 clang-rename-binary nil output-buffer nil
37 (format "-offset=%d"
38 ;; clang-rename wants file (byte) offsets, not
39 ;; buffer (character) positions.
40 (clang-rename--bufferpos-to-filepos
41 ;; Emacs treats one character after a symbol as
42 ;; part of the symbol, but clang-rename doesn’t.
43 ;; Use the beginning of the current symbol, if
44 ;; available, to resolve the inconsistency.
45 (or (car (bounds-of-thing-at-point 'symbol))
46 (point))
47 'exact))
48 (format "-new-name=%s" new-name)
49 "-i" (buffer-file-name))))
50 (if (and (integerp exit-code) (zerop exit-code))
51 ;; Success; revert current buffer so it gets the modifications.
52 (progn
53 (kill-buffer output-buffer)
54 (revert-buffer :ignore-auto :noconfirm :preserve-modes))
55 ;; Failure; append exit code to output buffer and display it.
56 (let ((message (clang-rename--format-message
57 "clang-rename failed with %s %s"
58 (if (integerp exit-code) "exit status" "signal")
59 exit-code)))
60 (with-current-buffer output-buffer
61 (insert ?\n message ?\n))
62 (message "%s" message)
63 (display-buffer output-buffer))))))
64
65 (defalias 'clang-rename--bufferpos-to-filepos
66 (if (fboundp 'bufferpos-to-filepos)
67 'bufferpos-to-filepos
68 ;; Emacs 24 doesn’t have ‘bufferpos-to-filepos’, simulate it using
69 ;; ‘position-bytes’.
70 (lambda (position &optional _quality _coding-system)
71 (1- (position-bytes position)))))
72
73 ;; ‘format-message’ is new in Emacs 25.1. Provide a fallback for older
74 ;; versions.
75 (defalias 'clang-rename--format-message
76 (if (fboundp 'format-message) 'format-message 'format))
77
78 (provide 'clang-rename)
79
80 ;;; clang-rename.el ends here