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