-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsult-xdg-recent-files.el
204 lines (166 loc) · 8.07 KB
/
consult-xdg-recent-files.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
;;; consult-xdg-recent-files.el --- Open files recently used by other xdg compliant programs -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Hauke Rehfeld
;; Author: Hauke Rehfeld <emacs@haukerehfeld.de>
;; URL: https://github.com/hrehfeld/consult-xdg-recent-files
;; Version: 0.1-pre
;; Package-Requires: ((emacs "27.1"))
;; Keywords: consult xdg recent-files convenience files unix matching
;; 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:
;; just a slightly cleaned up version of
;; https://github.com/minad/consult/wiki#including-file-recently-used-by-other-programs
;; If you find yourself using other programs with Emacs, it can be helpful to include files used by other programs in the candidate lists of commands like consult-recent-file and consult-buffer. That way, you never have any mental hiccups when trying to open files in Emacs that you recently opened in a different program. Instead, you simply use the same interface with which you are already familiar.
;;
;; The way to access this information is generally specific to each system. Please update this section for other systems, if you find this feature useful.
;;
;; In Linux (or, more specifically, on systems that comply with the XDG specification), these files are listed in the file recently-used.xbel, which is found in the directory ~/.local/share or the location described by the environment variable XDG_DATA_HOME.
;;
;; We can access the data in this file using libraries built-in with Emacs, namely url-util.el, dom.el, and one of xml.c or xml.el.
;;;; Installation
;;;;; MELPA
;; If you installed from MELPA, you're done.
;;;;; Manual
;; Install these required packages:
;; + foo
;; + bar
;; Then put this file in your load-path, and put this in your init
;; file:
;; (require 'consult-xdg-recent-files)
;;;; straight.el
;; (my-use-package consult-xdg-recent-files
;; :demand (my-use-package-demand-random-time)
;; :ensure t
;; :straight (consult-xdg-recent-files :type git :host github :repo "hrehfeld/consult-xdg-recent-files" :protocol ssh)
;; :config
;; (add-to-list 'consult-buffer-sources consult-xdg-recent-files--source-system-file)
;; )
;;;; Usage
;; Example: using the "mixed" source in `consult-buffer':
;; (setq consult-buffer-sources
;; '( consult--source-hidden-buffer
;; consult--source-buffer
;; consult--source-mixed-file
;; consult--source-bookmark
;; consult--source-project-buffer
;; consult--source-project-file))
;; or just
;; (add-to-list 'consult-buffer-sources consult-xdg-recent-files--source-system-file)
;;; History:
;;;; Credits
;;; Code:
;;;; Requirements
(require 'dom)
(require 'url-util)
(require 'xml)
(require 'cl-lib)
;;;; Commands
;;;; Functions
;;;;; Public
;;;;; Private
(defun consult-xdg-recent-files--xdg-recent-file-list ()
"Get a list of recently used files on XDG-compliant systems.
This function extracts a list of files from the file
`recently-used.xbel' in the folder `xdg-data-home'.
For more information on this specification, see
https://www.freedesktop.org/wiki/Specifications/desktop-bookmark-spec/"
(let ((data-file (expand-file-name "recently-used.xbel" (xdg-data-home)))
(xml-parsing-func (if (libxml-available-p)
#'libxml-parse-xml-region
#'xml-parse-region)))
(if (file-readable-p data-file)
(delq nil
(mapcar (lambda (bookmark-node)
(when-let* ((href (dom-attr bookmark-node 'href))
(local-path (and (string-prefix-p "file://" href)
(string-remove-prefix "file://" href))))
(let ((full-file-name (decode-coding-string
(url-unhex-string local-path)
'utf-8)))
(when (file-exists-p full-file-name)
full-file-name))))
(nreverse (dom-by-tag (with-temp-buffer
(insert-file-contents data-file)
(funcall xml-parsing-func
(point-min)
(point-max)))
'bookmark))))
(message "consult-xdg-recent-files: List of XDG recent files not found")
nil)))
(defun consult-xdg-recent-files--recent-system-files ()
"Return a list of files recently used by the system."
(cl-case system-type
(gnu/linux
(consult-xdg-recent-files--xdg-recent-file-list))
(t
(message "consult-xdg-recent-files: \"%s\" system currently unsupported"
system-type)
nil)))
(defun consult-xdg-recent-files--recent-files-sort (file-list)
"Sort the FILE-LIST by modification time, from most recent to least recent."
(thread-last
file-list
;; Use modification time, since getting file access time seems to count as
;; accessing the file, ruining future uses.
(mapcar (lambda (f)
(cons f (file-attribute-modification-time (file-attributes f)))))
(seq-sort (pcase-lambda (`(,f1 . ,t1) `(,f2 . ,t2))
;; Want existing, most recent, local files first.
(cond ((or (not (file-exists-p f1))
(file-remote-p f1))
nil)
((or (not (file-exists-p f2))
(file-remote-p f2))
t)
(t (time-less-p t2 t1)))))
(mapcar #'car)))
(defun consult-xdg-recent-files--recent-files-mixed-candidates ()
"Return a list of files recently used by Emacs and the system.
These files are sorted by modification time, from most recent to least."
(thread-last
(consult-xdg-recent-files--recent-system-files)
(seq-filter #'recentf-include-p)
(append (mapcar #'substring-no-properties recentf-list))
delete-dups
(consult-xdg-recent-files--recent-files-sort)))
(defvar consult-xdg-recent-files--source-system-file
`(:name "System file"
:narrow ?F
:category file
:face consult-file
:history file-name-history
:action ,#'consult--file-action
:items
,(lambda ()
(let ((ht (consult--buffer-file-hash)))
(mapcar #'abbreviate-file-name
(seq-remove (lambda (x) (gethash x ht))
(consult-xdg-recent-files--recent-system-files))))))
"Recent system file candidate source for `consult-buffer'.")
(defvar consult-xdg-recent-files--source-mixed-file
`(:name "File"
:narrow ?f
:category file
:face consult-file
:history file-name-history
:action ,#'consult--file-action
:items
,(lambda ()
(let ((ht (consult--buffer-file-hash)))
(mapcar #'abbreviate-file-name
(seq-remove (lambda (x) (gethash x ht))
(consult-xdg-recent-files--recent-files-mixed-candidates))))))
"File candidate source for `consult-buffer', including system files.
This is meant as a replacement for `consult-xdg-recent-files--source-file'.")
;;;; Footer
(provide 'consult-xdg-recent-files)
;;; consult-xdg-recent-files.el ends here