makefile-edit.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ;;; makefile-edit.el --- Makefile editing/scanning commands.
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Utilities for editing a Makefile for EDE Makefile management commands.
  18. ;;
  19. ;; Derived from project-am.el.
  20. ;;
  21. ;; Makefile editing and scanning commands
  22. ;;
  23. ;; Formatting of a makefile
  24. ;;
  25. ;; 1) Creating an automakefile, stick in a top level comment about
  26. ;; being created by emacs
  27. ;; 2) Leave order of variable contents alone, except for SOURCE
  28. ;; SOURCE always keep in the order of .c, .h, the other stuff.
  29. ;;; Things to do
  30. ;; makefile-fill-paragraph -- refill a macro w/ backslashes
  31. ;; makefile-insert-macro -- insert "foo = "
  32. ;;; Code:
  33. (defun makefile-beginning-of-command ()
  34. "Move to the beginning of the current command."
  35. (interactive)
  36. (if (save-excursion
  37. (forward-line -1)
  38. (makefile-line-continued-p))
  39. (forward-line -1))
  40. (beginning-of-line)
  41. (if (not (makefile-line-continued-p))
  42. nil
  43. (while (and (makefile-line-continued-p)
  44. (not (bobp)))
  45. (forward-line -1))
  46. (forward-line 1)))
  47. (defun makefile-end-of-command ()
  48. "Move to the end of the current command."
  49. (interactive)
  50. (end-of-line)
  51. (while (and (makefile-line-continued-p)
  52. (not (eobp)))
  53. (forward-line 1)
  54. (end-of-line)))
  55. (defun makefile-line-continued-p ()
  56. "Return non-nil if the current line ends in continuation."
  57. (save-excursion
  58. (end-of-line)
  59. (= (preceding-char) ?\\)))
  60. ;;; Programmatic editing of a Makefile
  61. ;;
  62. (defun makefile-move-to-macro (macro &optional next)
  63. "Move to the definition of MACRO. Return t if found.
  64. If NEXT is non-nil, move to the next occurrence of MACRO."
  65. (let ((oldpt (point)))
  66. (when (not next) (goto-char (point-min)))
  67. (if (re-search-forward (concat "^\\s-*" macro "\\s-*[+:?]?=") nil t)
  68. t
  69. (goto-char oldpt)
  70. nil)))
  71. (defun makefile-navigate-macro (stop-before)
  72. "In a list of files, move forward until STOP-BEFORE is reached.
  73. STOP-BEFORE is a regular expression matching a file name."
  74. (save-excursion
  75. (makefile-beginning-of-command)
  76. (let ((e (save-excursion
  77. (makefile-end-of-command)
  78. (point))))
  79. (if (re-search-forward stop-before nil t)
  80. (goto-char (match-beginning 0))
  81. (goto-char e)))))
  82. (defun makefile-macro-file-list (macro)
  83. "Return a list of all files in MACRO."
  84. (save-excursion
  85. (goto-char (point-min))
  86. (let ((lst nil))
  87. (while (makefile-move-to-macro macro t)
  88. (let ((e (save-excursion
  89. (makefile-end-of-command)
  90. (point))))
  91. (while (re-search-forward "\\s-**\\([-a-zA-Z0-9./_@$%(){}]+\\)\\s-*" e t)
  92. (let ((var nil)(varexp nil)
  93. (match (buffer-substring-no-properties
  94. (match-beginning 1)
  95. (match-end 1))))
  96. (if (not (setq var (makefile-extract-varname-from-text match)))
  97. (setq lst (cons match lst))
  98. (setq varexp (makefile-macro-file-list var))
  99. (dolist (V varexp)
  100. (setq lst (cons V lst))))))))
  101. (nreverse lst))))
  102. (defun makefile-extract-varname-from-text (text)
  103. "Extract the variable name from TEXT if it is a variable reference.
  104. Return nil if it isn't a variable."
  105. (save-match-data
  106. (when (string-match "\\$\\s(\\([A-Za-z0-9_]+\\)\\s)" text)
  107. (match-string 1 text))))
  108. (provide 'ede/makefile-edit)
  109. ;;; ede/makefile-edit.el ends here