-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl-bookmarks.lisp
107 lines (79 loc) · 3.13 KB
/
cl-bookmarks.lisp
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
;;; browser independent classes and functions
(in-package :cl-bookmarks)
;;; debug flag
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter *cl-bookmarks-debug* nil)
(defparameter *cl-bookmarks-trace-sql* nil))
;;; bookmark class - browser independent
(defclass bookmark ()
((url :accessor url
:initarg :url
:initform (error "Must specify an URL"))
(title :accessor title
:initarg :title
:initform nil)
;; creation time - in lisp format
(c-time :accessor c-time
:initarg :c-time
:initform 0)
;; modification time - in lisp format
(m-time :accessor m-time
:initarg :m-time
:initform 0)
;; last visit time - in lisp format
(v-time :accessor v-time
:initarg :v-time
:initform 0)
;; list of tags
(tags :accessor tags
:initarg :tags
:initform nil))
(:documentation "Class containing basic elements for a bookmark"))
(defmethod print-object ((bookmark bookmark) stream)
"Print the bookmark object to the specified stream"
(format stream "<bookm: ~a, url=~a~% vt=~a, ct=~a, mt=~a~% tags: ~a>"
(shorten (title bookmark)) (shorten (url bookmark) 35)
(v-time bookmark) (c-time bookmark) (m-time bookmark)
(tags bookmark)))
(defgeneric bookm-add-tag (bookmark tag))
(defmethod bookm-add-tag ((bookmark bookmark) tag)
"A null tag is ignored."
(when tag
(if (null (tags bookmark))
(setf (tags bookmark) (list tag))
(nconc (tags bookmark) (list tag)))))
(defgeneric bookm-has-tag-p (bookmark tag))
(defmethod bookm-has-tag-p ((bookmark bookmark) tag)
(position tag (tags bookmark) :test #'string-equal))
(defgeneric bookm-export (bookmark output-stream long-format))
(defmethod bookm-export ((bookm bookmark) output-stream long-format)
"LONG-FORMAT : include create & modify time"
(format output-stream "~a~%~a~%tags: ~{~a~^, ~}~%"
(url bookm) (or (title bookm) "No title") (sort (tags bookm) 'string<))
(when long-format
(format output-stream "create-time: ~a (~a)~%modification-time: ~a (~a)~%"
(lisp-time-str (c-time bookm)) (c-time bookm)
(lisp-time-str (m-time bookm)) (m-time bookm)))
(format output-stream "~%"))
(defun unix-to-lisp-time (unix-time)
"Convert an integer value representing a unix time to lisp time"
(+ unix-time (encode-universal-time 0 0 0 1 1 1970)))
(defun date-str (lisp-time)
(multiple-value-bind (sec min hour day month year)
(decode-universal-time lisp-time)
(declare (ignore sec min hour))
(format nil "~2,'0d-~2,'0d-~2,'0d" year month day)))
(defun lisp-time-str (lisp-time)
(multiple-value-bind (sec min hour day month year)
(decode-universal-time lisp-time)
(format nil "~2,'0d-~2,'0d-~2,'0d,~2,'0d:~2,'0d:~2,'0d" year month day hour min sec)))
(defun shorten (str &optional (len 17))
"Shorten a string to the first len elements"
(cond
((> (length str) len) (concatenate 'string (subseq str 0 len) "..."))
(t str)))
;;; * emacs display settings *
;;; Local Variables:
;;; default-tab-width: 4
;;; indent-tabs-mode: nil
;;; End: