-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjumplist.el
165 lines (133 loc) · 4.81 KB
/
jumplist.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
;;; jumplist.el --- Jump like vim jumplist or ex jumplist
;; Copyright (C) 2015 ganmacs
;; Author: ganmacs <ganmacs_at_gmail.com>
;; Maintainer: ganmacs <ganmacs_at_gmail.com>
;; URL: https://github.com/ganmacs/jumplist
;; Version: 0.0.2
;; Package-Requires: ((cl-lib "0.5"))
;; Keywords: jumplist vim
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'cl-lib)
(defgroup jumplist nil
"jumplist configuration options."
:prefix "jumplist"
:group 'convenience)
(defcustom jumplist-max-length 100
"Max length of jumplist."
:type 'integer
:group 'jumplist)
(defcustom jumplist-ex-mode 'nil
"Original vim like jumplist or not."
:type 'boolean
:group 'jumplist)
(defcustom jumplist-hook-commands '(end-of-buffer beginning-of-buffer find-file)
"Commands to hook."
:type 'list
:group 'jumplist)
(defvar jumplist--list '()
"Jumplist that save file info.")
(defvar jumplist--idx 0
"Index of jumplist.")
(defvar jumplist--jumping nil
"Jumplist state.")
(defun jumplist--do-jump (buff)
"Do jump to target file and point from BUFF."
(find-file (car buff))
(goto-char (cdr buff)))
(defun jumplist--reset-idx ()
"Reset `jumplist--idx'."
(setq jumplist--idx 0))
(defun jumplist--last? ()
"Check `jumplist--idx' is last of list."
(= jumplist--idx (- (length jumplist--list) 1)))
(defun jumplist--first? ()
"Check `jumplist--idx' is first of list."
(= jumplist--idx 0))
(defun jumplist--dec-idx ()
"Descrement `jumplist--idx'."
(setq jumplist--idx (- jumplist--idx 1)))
(defun jumplist--inc-idx ()
"Increment `jumplist--idx'."
(setq jumplist--idx (+ jumplist--idx 1)))
(defun jumplist--drop! (idx)
"Drop item form list of IDX."
(setq jumplist--list (nthcdr jumplist--idx jumplist--list)))
(defun jumplist--push (pointer)
"Push POINTER to `jumplist'."
(while (> (length jumplist--list) jumplist-max-length)
(nbutlast jumplist--list 1))
(push pointer jumplist--list))
(defun jumplist--same-position? (pointer)
(let ((new-point (cdr pointer))
(top-point (cdar jumplist--list)))
(cond ((not new-point) nil)
((not top-point) nil)
((eq (marker-position new-point) (marker-position top-point)) 't))))
(defun jumplist--set ()
"The record data structure is (file-name . pointer)."
(interactive)
(if (buffer-file-name)
(let ((pointer (cons (buffer-file-name) (point-marker))))
(unless (jumplist--same-position? pointer)
(when (and jumplist-ex-mode jumplist--jumping)
(jumplist--drop! jumplist--idx)
(setq jumplist--jumping nil)
(jumplist--reset-idx))
(unless (jumplist--same-position? pointer)
(jumplist--push pointer))))))
(defun jumplist--do-command? (command do-hook-command-list)
(if do-hook-command-list
(or
(eq command (car do-hook-command-list))
(jumplist--do-command? command (cdr do-hook-command-list)))))
(defun jumplist--command-hook ()
"Pre command hook that call `jumplist--set' when registerd command hook called."
(cond
((jumplist--do-command? this-command jumplist-hook-commands) (jumplist--set))
((and jumplist--jumping ; when jump and move
(not (memq this-command '(jumplist-previous jumplist-next))))
(jumplist--set))))
(add-hook 'pre-command-hook 'jumplist--command-hook)
;;;###autoload
(defun jumplist-previous ()
"Jump back."
(interactive)
(if (or (not jumplist--list)
(and (not (jumplist--first?))
(jumplist--last?)))
(message "No further undo point.")
(if jumplist-ex-mode
(unless jumplist--jumping
(jumplist--set)
(setq jumplist--jumping 't)))
(jumplist--inc-idx)
(let ((buff (nth jumplist--idx jumplist--list)))
(jumplist--do-jump buff))))
;;;###autoload
(defun jumplist-next ()
"Jump forward."
(interactive)
(if (or (not jumplist--list)
(jumplist--first?))
(message "No further redo point.")
(if jumplist-ex-mode
(unless jumplist--jumping
(jumplist--set)
(setq jumplist--jumping 't)))
(jumplist--dec-idx)
(let ((buff (nth jumplist--idx jumplist--list)))
(jumplist--do-jump buff))))
(provide 'jumplist)
;;; jumplist.el ends here