diff options
author | Tom Willemse | 2013-10-06 02:29:12 +0200 |
---|---|---|
committer | Tom Willemse | 2013-10-06 02:29:23 +0200 |
commit | b4769c70c4f3bc196595ff9795acc10ca7ddc1a6 (patch) | |
tree | 241606c52778d922906f55d2aed7651a71bd93b7 | |
parent | e4dc1824b28e00f3aa6f18201e719e1e05f6d397 (diff) | |
download | clark-b4769c70c4f3bc196595ff9795acc10ca7ddc1a6.tar.gz clark-b4769c70c4f3bc196595ff9795acc10ca7ddc1a6.zip |
Move command definitions to separate file
Hoping to keep the clark source file clean.
-rw-r--r-- | lisp/Makefile | 2 | ||||
-rw-r--r-- | lisp/clark.asd | 1 | ||||
-rw-r--r-- | lisp/clark.lisp | 110 | ||||
-rw-r--r-- | lisp/commands.lisp | 130 |
4 files changed, 132 insertions, 111 deletions
diff --git a/lisp/Makefile b/lisp/Makefile index ae6a1bf..f0ef043 100644 --- a/lisp/Makefile +++ b/lisp/Makefile @@ -36,5 +36,5 @@ $(install-binaries): install-%: % $(uninstall-binaries): uninstall-%: rm -f "$(DESTDIR)/bin/$*" -clark: clark.lisp queries.lisp make-image.lisp +clark: clark.lisp commands.lisp queries.lisp make-image.lisp $(LISP) --load make-image.lisp diff --git a/lisp/clark.asd b/lisp/clark.asd index 48ac718..c4bb0ec 100644 --- a/lisp/clark.asd +++ b/lisp/clark.asd @@ -31,4 +31,5 @@ :depends-on (:sqlite) :components ((:file "package") (:file "clark") + (:file "commands") (:file "queries"))) diff --git a/lisp/clark.lisp b/lisp/clark.lisp index 4d7deea..f5b21f6 100644 --- a/lisp/clark.lisp +++ b/lisp/clark.lisp @@ -190,116 +190,6 @@ bookmark." (format t "~A~A~A" url name description) (format t "~A~% ~A~% ~A~%~%" url name description)))) -(defcommand add (url name description &rest tags) - "Add a new bookmark." - "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." - (if (not (bookmark-exists-p url)) - (with-transaction *db* - (insert-bookmark url name description) - (add-tags (last-insert-rowid *db*) tags)) - (format t "~A has already been bookmarked~%" url))) - -(defcommand edit (url &rest rest) - "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." - (let ((name-lst (member "--name" rest :test #'string=)) - (desc-lst (member "--description" rest :test #'string=)) - 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* - (format - nil (sql update "bookmark" set "~A" where "url" = ?) query) - (append qargs (list url)))))) - -(defcommand exists (url) - "Check if a bookmark exists in the database." - "Usage: clark exists <url> - -Check if URL exists in the database. Prints `yes' when found and `no' -otherwise." - (format t "~:[no~;yes~]~%" (bookmark-exists-p url))) - -(defcommand help (&optional command) - "Show help message." - help-message - (if command - (let ((ldoc - (nth 2 (car (member - command *help-messages* - :test #'(lambda (x y) (equal x (car y)))))))) - (cond - ((null ldoc) - (with-error-and-help - 1 "help" "Unknown command: ~a~%" command)) - ((and (symbolp ldoc) (fboundp ldoc)) (funcall ldoc)) - (t (format t "~A~%" ldoc)))) - (call-command help "help"))) - -(defcommand remove (url) - "Remove a bookmark from the database." - "Usage: clark remove <url> - -Remove URL from the database." - (clear-tags url) - (delete-bookmark url)) - -(defcommand search (str) - "Search through bookmarks." - "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." - (map nil #'print-bookmark (bookmark-search str))) - -(defcommand set-tags (url &rest tags) - "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." - (clear-tags url) - (add-tags url tags)) - -(defun random-item (lst) - (nth (random (length lst) (make-random-state t)) lst)) - -(defcommand random (&optional tag) - "Pick a random bookmark, possibly from TAG." - "Usage: clark random [<tag>] - -Get a random bookmark. If TAG is given limit the result to a bookmark -having the tag TAG." - (format t "~a~%" (random-item (url-list tag)))) - -(defcommand remove-tags (url &rest tags) - "Remove given tags from bookmark's tag list." - "Usage: clark remove-tags <url> [<tags> ...] - -Remove the given TAGS from URL's tag list. If no tags are given, -remove all tags from bookmark." - (delete-tags url tags)) - -(defcommand version () - "Show version." - "Usage: clark version - -Print the version number and exit." - (format t "clark version ~A~%" version)) - (defun clark (args) "Main function. diff --git a/lisp/commands.lisp b/lisp/commands.lisp new file mode 100644 index 0000000..dedd46c --- /dev/null +++ b/lisp/commands.lisp @@ -0,0 +1,130 @@ +;; 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: + +(in-package :org.ryuslash.clark) + +(defcommand add (url name description &rest tags) + "Add a new bookmark." + "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." + (if (not (bookmark-exists-p url)) + (with-transaction *db* + (insert-bookmark url name description) + (add-tags (last-insert-rowid *db*) tags)) + (format t "~A has already been bookmarked~%" url))) + +(defcommand edit (url &rest rest) + "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." + (let ((name-lst (member "--name" rest :test #'string=)) + (desc-lst (member "--description" rest :test #'string=)) + 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* + (format + nil (sql update "bookmark" set "~A" where "url" = ?) query) + (append qargs (list url)))))) + +(defcommand exists (url) + "Check if a bookmark exists in the database." + "Usage: clark exists <url> + +Check if URL exists in the database. Prints `yes' when found and `no' +otherwise." + (format t "~:[no~;yes~]~%" (bookmark-exists-p url))) + +(defcommand help (&optional command) + "Show help message." + help-message + (if command + (let ((ldoc + (nth 2 (car (member + command *help-messages* + :test #'(lambda (x y) (equal x (car y)))))))) + (cond + ((null ldoc) + (with-error-and-help + 1 "help" "Unknown command: ~a~%" command)) + ((and (symbolp ldoc) (fboundp ldoc)) (funcall ldoc)) + (t (format t "~A~%" ldoc)))) + (call-command help "help"))) + +(defcommand remove (url) + "Remove a bookmark from the database." + "Usage: clark remove <url> + +Remove URL from the database." + (clear-tags url) + (delete-bookmark url)) + +(defcommand search (str) + "Search through bookmarks." + "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." + (map nil #'print-bookmark (bookmark-search str))) + +(defcommand set-tags (url &rest tags) + "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." + (clear-tags url) + (add-tags url tags)) + +(defun random-item (lst) + (nth (random (length lst) (make-random-state t)) lst)) + +(defcommand random (&optional tag) + "Pick a random bookmark, possibly from TAG." + "Usage: clark random [<tag>] + +Get a random bookmark. If TAG is given limit the result to a bookmark +having the tag TAG." + (format t "~a~%" (random-item (url-list tag)))) + +(defcommand remove-tags (url &rest tags) + "Remove given tags from bookmark's tag list." + "Usage: clark remove-tags <url> [<tags> ...] + +Remove the given TAGS from URL's tag list. If no tags are given, +remove all tags from bookmark." + (delete-tags url tags)) + +(defcommand version () + "Show version." + "Usage: clark version + +Print the version number and exit." + (format t "clark version ~A~%" version)) |