1 ;;; loc-changes.el --- Helps users and programs keep track of positions even after buffer changes.
3 ;; Author: Rocky Bernstein
5 ;; URL: http://github.com/rocky/emacs-loc-changes
6 ;; Compatibility: GNU Emacs 24.x
8 ;; Copyright (C) 2013-2014 Rocky Bernstein <rocky@gnu.org>
10 ;; This program is free software: you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation, either version 3 of the
13 ;; License, or (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see
22 ;; <http://www.gnu.org/licenses/>.
26 ;; This package lets users or programs set marks in a buffer prior to
27 ;; changes so that we can track the original positions after the
30 ;; One common use is say when debugging a program. The debugger has its static
31 ;; notion of the file and positions inside that. However it may be convenient
32 ;; for a programmer to edit the program but not restart execution of the program.
34 ;; Another use might be in a compilation buffer for errors and
35 ;; warnings which refer to file and line positions.
37 ;; Should be available via Melpa.
42 (make-variable-buffer-local 'loc-changes-alist)
43 (defvar loc-changes-alist '()
44 "A buffer-local association-list (alist) of line numbers and
45 their corresponding markers in the buffer. The 'key' is the line number; the value
50 (defun loc-changes-goto-line (line-number &optional column-number)
51 "Position `point' at LINE-NUMBER of the current buffer. If
52 COLUMN-NUMBER is given, position `point' at that column just
53 before that column number within the line. Note that the beginning of
54 the line starts at column 0, so the column number display will be one less
55 than COLUMN-NUMBER. For example COLUMN-NUMBER 1 will set before the first
56 column on the line and show 0.
58 The Emacs `goto-line' docstring says it is the wrong to use that
59 function in a Lisp program. So here is something that I proclaim
60 is okay to use in a Lisp program."
62 (if (and current-prefix-arg (not (consp current-prefix-arg)))
63 (list (prefix-numeric-value current-prefix-arg))
64 ;; Look for a default, a number in the buffer at point.
67 (skip-chars-backward "0-9")
68 (if (looking-at "[0-9]")
70 (buffer-substring-no-properties
72 (progn (skip-chars-forward "0-9")
74 ;; Decide if we're switching buffers.
76 (if (consp current-prefix-arg)
77 (other-buffer (current-buffer) t)))
80 (concat " in " (buffer-name buffer))
82 ;; Read the argument, offering that number (if any) as default.
83 (list (read-number (format "Goto line%s: " buffer-prompt)
84 (list default (line-number-at-pos)))
86 (unless (wholenump line-number)
87 (error "Expecting line-number parameter `%s' to be a whole number"
89 (unless (> line-number 0)
90 (error "Expecting line-number parameter `%d' to be greater than 0"
92 (let ((last-line (line-number-at-pos (point-max))))
93 (unless (<= line-number last-line)
95 "Line number %d should not exceed %d, the number of lines in the buffer"
96 line-number last-line))
97 (goto-char (point-min))
98 (forward-line (1- line-number))
104 (cond ((not (wholenump column-number))
106 "Column ignored. Expecting column-number parameter `%s' to be a whole number"
108 ((<= column-number 0)
110 "Column ignored. Expecting column-number parameter `%d' to be a greater than 1"
112 ((>= column-number last-column)
114 "Column ignored. Expecting column-number parameter `%d' to be a less than %d"
115 column-number last-column))
116 (t (forward-char (1- column-number)))))
122 (defun loc-changes-add-elt (pos)
123 "Add an element `loc-changes-alist'. The car will be POS and a
124 marker for it will be created at the point."
125 (setq loc-changes-alist
126 (cons (cons pos (point-marker)) loc-changes-alist)))
129 (defun loc-changes-add-and-goto (line-number &optional opt-buffer)
130 "Add a marker at LINE-NUMBER and record LINE-NUMBER and its
131 marker association in `loc-changes-alist'."
133 (if (and current-prefix-arg (not (consp current-prefix-arg)))
134 (list (prefix-numeric-value current-prefix-arg))
135 ;; Look for a default, a number in the buffer at point.
138 (skip-chars-backward "0-9")
139 (if (looking-at "[0-9]")
141 (buffer-substring-no-properties
143 (progn (skip-chars-forward "0-9")
145 ;; Decide if we're switching buffers.
147 (if (consp current-prefix-arg)
148 (other-buffer (current-buffer) t)))
151 (concat " in " (buffer-name buffer))
153 ;; Read the argument, offering that number (if any) as default.
154 (list (read-number (format "Goto line%s: " buffer-prompt)
155 (list default (line-number-at-pos)))
158 (let ((buffer (or opt-buffer (current-buffer))))
159 (with-current-buffer buffer
160 (loc-changes-goto-line line-number)
161 (loc-changes-add-elt line-number)
166 (defun loc-changes-clear-buffer (&optional opt-buffer)
167 "Remove all location-tracking associations in BUFFER."
168 (interactive "bbuffer: ")
169 (let ((buffer (or opt-buffer (current-buffer)))
171 (with-current-buffer buffer
172 (setq loc-changes-alist '())
177 (defun loc-changes-reset-position (&optional opt-buffer no-insert)
178 "Update `loc-changes-alist' so that the line number of point is
179 used to when aline number is requested.
181 Updates any existing line numbers referred to in marks at this
184 This may be useful for example in debugging if you save the
185 buffer and then cause the debugger to reread/reevaluate the file
186 so that its positions are will be reflected."
188 (let* ((line-number (line-number-at-pos (point)))
189 (elt (assq line-number loc-changes-alist)))
190 (let ((buffer (or opt-buffer (current-buffer)))
192 (with-current-buffer buffer
196 (loc-changes-add-elt line-number)
203 (defun loc-changes-goto (position &optional opt-buffer no-update)
204 "Go to the position inside BUFFER taking into account the
205 previous location marks. Normally if the position hasn't been
206 seen before, we will add a new mark for this position. However if
207 NO-UPDATE is set, no mark is added."
208 (unless (wholenump position)
209 (error "Expecting line-number parameter `%s' to be a whole number"
211 (let ((elt (assq position loc-changes-alist)))
213 (let ((marker (cdr elt)))
214 (unless (markerp marker)
215 (error "Internal error: loc-changes-alist is not a marker"))
216 (goto-char (marker-position marker)))
218 (loc-changes-goto-line position)
219 (loc-changes-add-and-goto position))
224 (provide 'loc-changes)
226 ;;; loc-changes.el ends here