Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.8 KiB

  1. ;;; php-util-buffer.el --- Utility function for buffer manipulation -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020 Friends of Emacs-PHP development
  3. ;; Copyright 2013 The go-mode Authors. All rights reserved.
  4. ;; Author: Dominik Honnef
  5. ;; Maintainer: USAMI Kenta <tadsan@zonu.me>
  6. ;; URL: https://github.com/emacs-php/php-mode
  7. ;; Keywords: lisp
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Functions for patching buffer.
  20. ;;; Original License:
  21. ;; Copyright (c) 2014 The go-mode Authors. All rights reserved.
  22. ;; Redistribution and use in source and binary forms, with or without
  23. ;; modification, are permitted provided that the following conditions are
  24. ;; met:
  25. ;; * Redistributions of source code must retain the above copyright
  26. ;; notice, this list of conditions and the following disclaimer.
  27. ;; * Redistributions in binary form must reproduce the above
  28. ;; copyright notice, this list of conditions and the following disclaimer
  29. ;; in the documentation and/or other materials provided with the
  30. ;; distribution.
  31. ;; * Neither the name of the copyright holder nor the names of its
  32. ;; contributors may be used to endorse or promote products derived from
  33. ;; this software without specific prior written permission.
  34. ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. ;;; Code:
  46. ;; gofmt apply-rcs-patch Function
  47. ;; These functions are copied by go-mode(gofmt).
  48. (defun php-util-buffer--delete-whole-line (&optional arg)
  49. "Delete the current line without putting it in the `kill-ring'.
  50. Derived from function `kill-whole-line'. ARG is defined as for that
  51. function."
  52. (setq arg (or arg 1))
  53. (if (and (> arg 0)
  54. (eobp)
  55. (save-excursion (forward-visible-line 0) (eobp)))
  56. (signal 'end-of-buffer nil))
  57. (if (and (< arg 0)
  58. (bobp)
  59. (save-excursion (end-of-visible-line) (bobp)))
  60. (signal 'beginning-of-buffer nil))
  61. (cond ((zerop arg)
  62. (delete-region (progn (forward-visible-line 0) (point))
  63. (progn (end-of-visible-line) (point))))
  64. ((< arg 0)
  65. (delete-region (progn (end-of-visible-line) (point))
  66. (progn (forward-visible-line (1+ arg))
  67. (unless (bobp)
  68. (backward-char))
  69. (point))))
  70. (t
  71. (delete-region (progn (forward-visible-line 0) (point))
  72. (progn (forward-visible-line arg) (point))))))
  73. (defun php-util-buffer-apply-rcs-patch (target-buffer patch-buffer)
  74. "Apply an RCS-formatted diff from `PATCH-BUFFER' to the `TARGET-BUFFER'."
  75. (let (
  76. ;; Relative offset between buffer line numbers and line numbers
  77. ;; in patch.
  78. ;;
  79. ;; Line numbers in the patch are based on the source file, so
  80. ;; we have to keep an offset when making changes to the
  81. ;; buffer.
  82. ;;
  83. ;; Appending lines decrements the offset (possibly making it
  84. ;; negative), deleting lines increments it. This order
  85. ;; simplifies the forward-line invocations.
  86. (line-offset 0)
  87. (column (current-column)))
  88. (save-excursion
  89. (with-current-buffer patch-buffer
  90. (goto-char (point-min))
  91. (while (not (eobp))
  92. (unless (looking-at "^\\([ad]\\)\\([0-9]+\\) \\([0-9]+\\)")
  93. (error "Invalid rcs patch or internal error in php-util-buffer-apply-rcs-patch"))
  94. (forward-line)
  95. (let ((action (match-string 1))
  96. (from (string-to-number (match-string 2)))
  97. (len (string-to-number (match-string 3))))
  98. (cond
  99. ((equal action "a")
  100. (let ((start (point)))
  101. (forward-line len)
  102. (let ((text (buffer-substring start (point))))
  103. (with-current-buffer target-buffer
  104. (cl-decf line-offset len)
  105. (goto-char (point-min))
  106. (forward-line (- from len line-offset))
  107. (insert text)))))
  108. ((equal action "d")
  109. (with-current-buffer target-buffer
  110. (goto-char (point-min))
  111. (forward-line (1- (- from line-offset)))
  112. (cl-incf line-offset len)
  113. (php-util-buffer--delete-whole-line len)))
  114. (t
  115. (error "Invalid rcs patch or internal error in php-util-buffer--apply-rcs-patch")))))))
  116. (move-to-column column)))
  117. ;; Copy of go-mode.el ends here
  118. (provide 'php-util-buffer)
  119. ;;; php-util-buffer.el ends here