2013-03-23 00:43:44 +01:00
|
|
|
|
;; Copyright (C) 2013 Tom Willemsen <tom at ryuslash dot org>
|
|
|
|
|
|
|
|
|
|
;; This file is part of CLark
|
|
|
|
|
|
|
|
|
|
;; CLark 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.
|
|
|
|
|
|
|
|
|
|
;; CLark 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 CLark. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2013-03-20 21:58:10 +01:00
|
|
|
|
(in-package :org.ryuslash.clark)
|
|
|
|
|
|
|
|
|
|
(export '(clark))
|
|
|
|
|
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(defmacro sql (&body body)
|
|
|
|
|
(apply 'concatenate 'string
|
|
|
|
|
(mapcar (lambda (itm) (format nil "~A " itm)) body)))
|
|
|
|
|
|
2013-03-20 21:58:10 +01:00
|
|
|
|
(defvar *db* nil
|
|
|
|
|
"The database connection.")
|
|
|
|
|
|
2013-03-22 20:11:31 +01:00
|
|
|
|
(defvar *help-messages* nil
|
2013-03-21 23:41:13 +01:00
|
|
|
|
"Help texts for commands.")
|
|
|
|
|
|
2013-03-22 20:11:31 +01:00
|
|
|
|
(defvar *max-command-name-length* 0
|
2013-03-23 16:53:08 +01:00
|
|
|
|
"Length of the longest command name.")
|
2013-03-22 20:11:31 +01:00
|
|
|
|
|
2013-03-23 17:58:31 +01:00
|
|
|
|
(defvar *script* nil
|
|
|
|
|
"Whether or not to output in a machine-readable format.")
|
|
|
|
|
|
2013-03-23 16:37:41 +01:00
|
|
|
|
(defmacro call-command (name &rest args)
|
|
|
|
|
(let ((command-name (make-command-name (symbol-name name))))
|
2013-03-28 00:39:29 +01:00
|
|
|
|
`(,command-name ,@args)))
|
2013-03-23 16:37:41 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defmacro defcommand (name (&rest args) sdoc ldoc
|
2013-03-23 18:40:54 +01:00
|
|
|
|
&body body)
|
2013-03-22 20:11:31 +01:00
|
|
|
|
"Define a new command usable on the command-line."
|
2013-03-23 18:40:54 +01:00
|
|
|
|
(let* ((sname (string-downcase (symbol-name name)))
|
|
|
|
|
(command-name (make-command-name (symbol-name name))))
|
2013-03-22 20:11:31 +01:00
|
|
|
|
`(progn
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defun ,command-name (,@args)
|
2013-03-23 16:31:22 +01:00
|
|
|
|
,sdoc
|
2013-03-28 00:39:29 +01:00
|
|
|
|
,@body)
|
2013-03-22 20:11:31 +01:00
|
|
|
|
(setf *help-messages*
|
2013-03-23 18:40:54 +01:00
|
|
|
|
(nconc *help-messages* '((,sname ,sdoc ,ldoc)))
|
2013-03-22 20:11:31 +01:00
|
|
|
|
*max-command-name-length*
|
|
|
|
|
(max *max-command-name-length* (length ,sname))))))
|
|
|
|
|
|
2013-03-21 00:54:06 +01:00
|
|
|
|
(defconstant *version* "0.1.0"
|
|
|
|
|
"Clark's version.")
|
|
|
|
|
|
2013-03-23 20:46:19 +01:00
|
|
|
|
(defun add-tags (url-or-id tags)
|
2013-03-21 22:30:55 +01:00
|
|
|
|
"Add tags to the bookmark_tag table and possibly to tag."
|
2013-03-23 20:46:19 +01:00
|
|
|
|
(when url-or-id
|
|
|
|
|
(if (integerp url-or-id)
|
|
|
|
|
(map nil (lambda (tag)
|
|
|
|
|
(let ((tag-id (handler-case (insert-tag tag)
|
|
|
|
|
(sqlite-error () (get-tag-id tag)))))
|
|
|
|
|
(insert-bookmark-tag url-or-id tag-id))) tags)
|
|
|
|
|
(add-tags (get-bookmark-id url-or-id) tags))))
|
|
|
|
|
|
|
|
|
|
(defun clear-tags (url-or-id)
|
|
|
|
|
"Remove all tags from the bookmark URL."
|
|
|
|
|
(when url-or-id
|
|
|
|
|
(if (integerp url-or-id)
|
|
|
|
|
(execute-non-query
|
2013-03-30 15:46:54 +01:00
|
|
|
|
*db* (sql delete from "bookmark_tag" where "bookmark_id" = ?)
|
|
|
|
|
url-or-id)
|
2013-03-23 20:46:19 +01:00
|
|
|
|
(clear-tags (get-bookmark-id url-or-id)))))
|
2013-03-21 22:30:55 +01:00
|
|
|
|
|
2013-03-23 13:24:43 +01:00
|
|
|
|
(defun ensure-db-exists (name)
|
2013-03-20 21:58:10 +01:00
|
|
|
|
"Connect to the database, possibly creating it."
|
|
|
|
|
(let ((db-exists (probe-file name)))
|
2013-03-21 00:54:06 +01:00
|
|
|
|
(setf *db* (connect name))
|
2013-03-20 21:58:10 +01:00
|
|
|
|
(unless db-exists
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(execute-non-query
|
|
|
|
|
*db* (sql create table "bookmark" ("url" varchar (255) unique\,
|
|
|
|
|
"date" integer\,
|
|
|
|
|
"name" varchar (255)\,
|
|
|
|
|
"description" text)))
|
|
|
|
|
(execute-non-query
|
|
|
|
|
*db* (sql create table "tag" ("name" varchar (255) unique)))
|
|
|
|
|
(execute-non-query
|
|
|
|
|
*db* (sql create table "bookmark_tag"
|
|
|
|
|
("bookmark_id" integer references "bookmark(rowid)"\,
|
|
|
|
|
"tag_id" integer references "tag(rowid)"\,
|
|
|
|
|
primary key ("bookmark_id"\, "tag_id")))))))
|
2013-03-21 00:54:06 +01:00
|
|
|
|
|
|
|
|
|
(defun get-bookmarks ()
|
|
|
|
|
"Get a list of all bookmarks.
|
|
|
|
|
|
2013-03-23 17:57:35 +01:00
|
|
|
|
The result contains the url, name and the description of the bookmark."
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(execute-to-list
|
|
|
|
|
*db* (sql select "url, name, description" from "bookmark")))
|
2013-03-20 21:58:10 +01:00
|
|
|
|
|
2013-03-23 20:46:19 +01:00
|
|
|
|
(defun get-bookmark-id (url)
|
|
|
|
|
"Get the id of the bookmark for URL."
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(execute-single
|
|
|
|
|
*db* (sql select "rowid" from "bookmark" where "url" = ?) url))
|
2013-03-23 20:46:19 +01:00
|
|
|
|
|
2013-03-23 13:24:43 +01:00
|
|
|
|
(defun get-db-location ()
|
|
|
|
|
"Get the location of the database."
|
|
|
|
|
(pathname
|
|
|
|
|
(apply 'concatenate 'string
|
|
|
|
|
(or (sb-ext:posix-getenv "XDG_DATA_HOME")
|
|
|
|
|
(list (sb-ext:posix-getenv "HOME") "/.local/share"))
|
|
|
|
|
'("/clark/bookmarks.db"))))
|
|
|
|
|
|
2013-03-24 21:57:59 +01:00
|
|
|
|
(defun get-rc-location ()
|
|
|
|
|
"Get the location of the RC file."
|
|
|
|
|
(pathname
|
|
|
|
|
(apply 'concatenate 'string
|
|
|
|
|
(or (sb-ext:posix-getenv "XDG_CONFIG_HOME")
|
|
|
|
|
(list (sb-ext:posix-getenv "HOME") "/.config"))
|
|
|
|
|
'("/clark/rc.lisp"))))
|
|
|
|
|
|
2013-03-21 22:30:55 +01:00
|
|
|
|
(defun get-tag-id (name)
|
|
|
|
|
"Get the rowid of tag NAME."
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(execute-single
|
|
|
|
|
*db* (sql select "rowid" from "tag" where "name" = ?) name))
|
2013-03-21 22:30:55 +01:00
|
|
|
|
|
2013-03-23 16:31:22 +01:00
|
|
|
|
(defun help-message ()
|
|
|
|
|
(format t (concatenate
|
|
|
|
|
'string
|
2013-03-23 20:58:00 +01:00
|
|
|
|
"Usage: clark [options] [<command> [<options> ...]]~%"
|
|
|
|
|
"~%"
|
|
|
|
|
"Possible options:~%"
|
|
|
|
|
"~%"
|
|
|
|
|
" --script Output in a machine-readable format.~%"
|
2013-03-23 16:31:22 +01:00
|
|
|
|
"~%"
|
|
|
|
|
"Possible commands:~%"
|
|
|
|
|
"~%"))
|
|
|
|
|
(map nil (lambda (hlp)
|
|
|
|
|
(destructuring-bind (name short long) hlp
|
|
|
|
|
(declare (ignore long))
|
|
|
|
|
(format t " ~vA ~A~%" *max-command-name-length*
|
|
|
|
|
name short))) *help-messages*)
|
|
|
|
|
(format t "~%~A~%"
|
|
|
|
|
(concatenate 'string "Use `clark help <command>' to get more "
|
|
|
|
|
"information on a command.")))
|
|
|
|
|
|
2013-03-21 22:30:55 +01:00
|
|
|
|
(defun insert-bookmark (url name description)
|
|
|
|
|
"Insert URL, NAME and DESCRIPTION into the bookmark table."
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(execute-non-query
|
|
|
|
|
*db* (sql insert into "bookmark" values (?\, ?\, ?\, ?))
|
|
|
|
|
url (get-universal-time) name description))
|
2013-03-21 22:30:55 +01:00
|
|
|
|
|
|
|
|
|
(defun insert-bookmark-tag (bookmark-id tag-id)
|
|
|
|
|
"Insert BOOKMARK-ID and TAG-ID into the bookmark_tag table."
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(execute-non-query
|
|
|
|
|
*db* (sql insert into "bookmark_tag" values (?\, ?))
|
|
|
|
|
bookmark-id tag-id))
|
2013-03-21 22:30:55 +01:00
|
|
|
|
|
|
|
|
|
(defun insert-tag (name)
|
|
|
|
|
"Insert tag NAME into the database and return its rowid."
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(execute-non-query *db* (sql insert into "tag" values (?)) name)
|
|
|
|
|
(last-insert-rowid *db*))
|
2013-03-21 22:30:55 +01:00
|
|
|
|
|
2013-03-23 13:24:43 +01:00
|
|
|
|
(defun load-db ()
|
|
|
|
|
"Load the database."
|
|
|
|
|
(let ((db-location (get-db-location)))
|
|
|
|
|
(ensure-directories-exist db-location)
|
|
|
|
|
(ensure-db-exists db-location)))
|
|
|
|
|
|
2013-03-24 21:57:59 +01:00
|
|
|
|
(defun load-rc ()
|
|
|
|
|
"Load the RC file."
|
|
|
|
|
(let ((*package* (in-package :org.ryuslash.clark)))
|
|
|
|
|
(load (get-rc-location) :if-does-not-exist nil)))
|
|
|
|
|
|
2013-03-22 20:11:31 +01:00
|
|
|
|
(eval-when (:compile-toplevel :load-toplevel)
|
|
|
|
|
(defun make-command-name (base)
|
|
|
|
|
"Turn BASE into the name of a possible command."
|
|
|
|
|
(intern (concatenate 'string (string-upcase base) "-COMMAND")
|
|
|
|
|
:org.ryuslash.clark)))
|
2013-03-21 21:29:43 +01:00
|
|
|
|
|
2013-03-21 21:39:56 +01:00
|
|
|
|
(defun parse-args (args)
|
|
|
|
|
"Parse command-line arguments ARGS.
|
|
|
|
|
|
|
|
|
|
The executable name should already have been removed."
|
2013-03-23 17:58:31 +01:00
|
|
|
|
(loop while (and args (char= (char (car args) 0) #\-))
|
|
|
|
|
do (case (intern (string-upcase (string-left-trim "-" (car args)))
|
|
|
|
|
:org.ryuslash.clark)
|
|
|
|
|
(script (setf *script* t args (cdr args)))))
|
|
|
|
|
(if args
|
|
|
|
|
(let ((cmd-name (make-command-name (car args))))
|
|
|
|
|
(if (fboundp cmd-name)
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(handler-case (apply cmd-name (cdr args))
|
|
|
|
|
(sb-int:simple-program-error (err)
|
|
|
|
|
(if (string-equal (format nil "~A" err)
|
|
|
|
|
"invalid number of arguments" :end1 27)
|
|
|
|
|
(let ((*standard-output* *error-output*))
|
|
|
|
|
(format t "Wrong number of arguments given.~%")
|
|
|
|
|
(call-command help (car args)))
|
|
|
|
|
(signal err))))
|
2013-03-27 21:38:15 +01:00
|
|
|
|
(let ((*standard-output* *error-output*))
|
2013-03-23 17:58:31 +01:00
|
|
|
|
(format t "Unknown command: ~A~%" (car args))
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(call-command help "help"))))
|
2013-03-23 17:58:31 +01:00
|
|
|
|
(map nil #'print-bookmark (get-bookmarks))))
|
2013-03-21 21:39:56 +01:00
|
|
|
|
|
2013-03-21 21:29:43 +01:00
|
|
|
|
(defun print-bookmark (bm)
|
|
|
|
|
"Print information about bookmark BM.
|
|
|
|
|
|
2013-03-23 17:58:31 +01:00
|
|
|
|
BM should be a list containing the url, name and description of the
|
|
|
|
|
bookmark."
|
|
|
|
|
(destructuring-bind (url name description) bm
|
|
|
|
|
(if *script*
|
2013-03-28 00:52:48 +01:00
|
|
|
|
(format t "~A~A~A" url name description)
|
2013-03-23 17:58:31 +01:00
|
|
|
|
(format t "~A~% ~A~% ~A~%~%" url name description))))
|
2013-03-21 21:29:43 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defcommand add (url name description &rest tags)
|
2013-03-23 13:38:19 +01:00
|
|
|
|
"Add a new bookmark."
|
2013-03-23 16:31:22 +01:00
|
|
|
|
"Usage: clark add <url> <name> <description> [<tags> ...]
|
|
|
|
|
|
|
|
|
|
Add URL with NAME, DESCRIPTION and TAGS to the database. TAGS may be
|
|
|
|
|
omitted or any number of tag names."
|
2013-03-22 20:11:31 +01:00
|
|
|
|
(with-transaction *db*
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(insert-bookmark url name description)
|
|
|
|
|
(add-tags (last-insert-rowid *db*) tags)))
|
2013-03-22 20:11:31 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defcommand edit (url &rest rest)
|
2013-03-23 19:24:48 +01:00
|
|
|
|
"Edit a bookmark."
|
|
|
|
|
"Usage: clark edit <url> [--name <name>] \\
|
|
|
|
|
[--description <description>]
|
|
|
|
|
|
|
|
|
|
Edit the information for URL, specifying which part(s) to edit. Each
|
|
|
|
|
option will replace the previous value for that part."
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(let ((name-lst (member "--name" rest :test #'string=))
|
|
|
|
|
(desc-lst (member "--description" rest :test #'string=))
|
2013-03-23 19:24:48 +01:00
|
|
|
|
query qargs)
|
|
|
|
|
(when name-lst
|
|
|
|
|
(setf query (concatenate 'string query "name = ? ")
|
|
|
|
|
qargs (nconc qargs (list (cadr name-lst)))))
|
|
|
|
|
(when desc-lst
|
|
|
|
|
(setf query (concatenate 'string query (when qargs ", ")
|
|
|
|
|
"description = ? ")
|
|
|
|
|
qargs (nconc qargs (list (cadr desc-lst)))))
|
|
|
|
|
(when qargs
|
|
|
|
|
(apply #'execute-non-query *db*
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(format
|
|
|
|
|
nil (sql update "bookmark" set "~A" where "url" = ?) query)
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(append qargs (list url))))))
|
2013-03-23 19:24:48 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defcommand exists (url)
|
2013-03-23 13:38:19 +01:00
|
|
|
|
"Check if a bookmark exists in the database."
|
2013-03-23 16:31:22 +01:00
|
|
|
|
"Usage: clark exists <url>
|
|
|
|
|
|
|
|
|
|
Check if URL exists in the database. Prints `yes' when found and `no'
|
|
|
|
|
otherwise."
|
2013-03-30 15:46:54 +01:00
|
|
|
|
(format
|
|
|
|
|
t "~:[no~;yes~]~%"
|
|
|
|
|
(execute-single
|
|
|
|
|
*db* (sql select "rowid" from "bookmark" where "url" = ?) url)))
|
2013-03-23 13:38:19 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defcommand help (&optional command)
|
2013-03-23 13:38:19 +01:00
|
|
|
|
"Show help message."
|
2013-03-23 16:31:22 +01:00
|
|
|
|
help-message
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(if command
|
2013-03-23 16:31:22 +01:00
|
|
|
|
(let ((ldoc
|
|
|
|
|
(nth 2 (car (member
|
2013-03-28 00:39:29 +01:00
|
|
|
|
command *help-messages*
|
2013-03-23 16:31:22 +01:00
|
|
|
|
:test #'(lambda (x y) (equal x (car y))))))))
|
|
|
|
|
(cond
|
2013-03-28 00:39:29 +01:00
|
|
|
|
((null ldoc) (format t "Unkown command: ~A~%" command))
|
2013-03-23 16:31:22 +01:00
|
|
|
|
((and (symbolp ldoc) (fboundp ldoc)) (funcall ldoc))
|
|
|
|
|
(t (format t "~A~%" ldoc))))
|
2013-03-23 20:57:13 +01:00
|
|
|
|
(call-command help "help")))
|
2013-03-22 20:11:31 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defcommand remove (url)
|
2013-03-23 20:52:49 +01:00
|
|
|
|
"Remove a bookmark from the database."
|
|
|
|
|
"Usage: clark remove <url>
|
|
|
|
|
|
|
|
|
|
Remove URL from the database."
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(clear-tags url)
|
2013-03-23 20:52:49 +01:00
|
|
|
|
(execute-non-query
|
2013-03-30 15:46:54 +01:00
|
|
|
|
*db* (sql delete from "bookmark" where "url" = ?) url))
|
2013-03-23 20:52:49 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defcommand search (str)
|
2013-03-23 13:38:19 +01:00
|
|
|
|
"Search through bookmarks."
|
2013-03-23 16:31:22 +01:00
|
|
|
|
"Usage: clark search <str>
|
|
|
|
|
|
|
|
|
|
Search the database for STR. Matches are made for substrings of a
|
|
|
|
|
bookmark's name or an exact match for a tag."
|
2013-03-23 17:58:31 +01:00
|
|
|
|
(map nil #'print-bookmark
|
|
|
|
|
(execute-to-list
|
2013-03-30 15:46:54 +01:00
|
|
|
|
*db* (sql select "url, name, description"
|
|
|
|
|
from "bookmark"
|
|
|
|
|
where "name" like ?
|
|
|
|
|
or ? in (select "name"
|
|
|
|
|
from "tag"
|
|
|
|
|
join "bookmark_tag" on ("tag_id = tag.rowid")
|
|
|
|
|
where "bookmark_id" = "bookmark.rowid"))
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(format nil "%~A%" str) str)))
|
2013-03-22 02:23:42 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defcommand set-tags (url &rest tags)
|
2013-03-23 20:46:19 +01:00
|
|
|
|
"Set a bookmark's tags."
|
|
|
|
|
"Usage: clark set-tags <url> [<tags> ...]
|
|
|
|
|
|
|
|
|
|
Set bookmark URL's tags to the given list, overwriting the previous
|
|
|
|
|
list of tags."
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(clear-tags url)
|
|
|
|
|
(add-tags url tags))
|
2013-03-23 20:46:19 +01:00
|
|
|
|
|
2013-03-28 00:39:29 +01:00
|
|
|
|
(defcommand version ()
|
2013-03-23 13:38:19 +01:00
|
|
|
|
"Show version."
|
2013-03-23 16:31:22 +01:00
|
|
|
|
"Usage: clark version
|
|
|
|
|
|
|
|
|
|
Print the version number and exit."
|
2013-03-21 00:54:06 +01:00
|
|
|
|
(format t "clark version ~A~%" *version*))
|
|
|
|
|
|
|
|
|
|
(defun clark (args)
|
2013-03-21 21:29:43 +01:00
|
|
|
|
"Main function.
|
|
|
|
|
|
|
|
|
|
Connect to the database, parse command-line arguments, execute and
|
|
|
|
|
then disconnect."
|
2013-03-24 21:57:59 +01:00
|
|
|
|
(load-rc)
|
2013-03-23 13:24:43 +01:00
|
|
|
|
(load-db)
|
2013-03-23 17:58:31 +01:00
|
|
|
|
(parse-args (cdr args))
|
2013-03-20 21:58:10 +01:00
|
|
|
|
(disconnect *db*))
|