Merge improvements from Emacs 23.
[remember-el.git] / remember-blosxom.el
bloba9b1b18886b3590e2481095f51b3452d8ea52397
1 ;;; remember-blosxom --- Blosxom support for remember.el
3 ;; Copyright 2004 Gary V. Vaughan (gary AT gnu DOT org)
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: remember-blosxom.el
7 ;; Version: 0.1
8 ;; Date: Wed, 19 May 2004
9 ;; Keywords: memory blog
10 ;; Author: Gary V. Vaughan (gary AT gnu DOT org)
11 ;; Maintainer: Gary V. Vaughan (gary AT gnu DOT org)
12 ;; Description: Remember to an emacs-wiki-blosxom.el story
13 ;; URL: http://tkd.kicks-ass.net/arch/gary@gnu.org--2004/remember--gary--1.0
15 ;; This file is not part of GNU Emacs.
17 ;; This is free software; you can redistribute it and/or modify it under
18 ;; the terms of the GNU General Public License as published by the Free
19 ;; Software Foundation; either version 2, or (at your option) any later
20 ;; version.
22 ;; This is distributed in the hope that it will be useful, but WITHOUT
23 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 ;; for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 ;; MA 02111-1307, USA.
32 ;;; Commentary:
34 ;; I maintain the hypertext parts of my website with John Wiegley's
35 ;; emacs-wiki.el, now maintained by Sacha Chua at
36 ;; http://sacha.free.net/notebook/emacs/emacs-wiki. You will need to
37 ;; install a copy of that file and remember.el from that site, and also
38 ;; emacs-wiki-blosxom.el, and read-file-name.el from the same place that
39 ;; you got this file before this can be of any use to you.
41 ;; To use, place this in your .emacs
43 ;; (require 'remember-blosxom)
44 ;; (setq remember-handler-functions '(remember-blosxom))
45 ;; (setq remember-annotation-functions nil)
47 ;; Then type M-x remember to remember new text, or
48 ;; C-u M-x remember to remember a region
50 ;;; Code:
52 (require 'emacs-wiki-blosxom)
53 (require 'read-file-name)
54 (require 'remember)
56 (defvar remember-blosxom-hook '(remember-blosxom-add-timestamp)
57 "*Functions to run after something has been appended to the blosxom page.")
59 (defvar remember-blosxom-timestamp-format "#date %Y-%m-%d %H:%M"
60 "*Format of timestamp for remember entries.")
62 (defun remember-blosxom-add-timestamp ()
63 "Add a timestamp at the start of the buffer if there is not one already.
64 This function can be added to `remember-blosxom-hook'."
65 (goto-char (point-min))
66 (unless (save-excursion (save-match-data
67 (re-search-forward "^#date " nil t)))
68 (insert (format-time-string remember-blosxom-timestamp-format
69 (current-time)) "\n")))
72 ;;;###autoload
73 (defun remember-blosxom ()
74 "Remember this text to a blosxom story.
75 This function can be added to `remember-handler-functions'."
76 (require 'emacs-wiki-blosxom)
77 (let* ((text (buffer-string))
78 (category-file (remember-blosxom-read-new-wiki-page))
79 (dir (file-name-directory category-file)))
80 (or (file-exists-p dir)
81 (make-directory dir 'parents))
82 (with-temp-buffer
83 (insert text)
84 (if (not (bolp))
85 (insert "\n"))
86 (run-hooks 'remember-blosxom-hook)
87 (write-file category-file)
88 (blosxom-mode))
89 (let ((buffer (find-file category-file)))
90 (emacs-wiki-publish-this-page)
91 (kill-buffer buffer))
92 t))
94 (custom-add-option 'remember-handler-functions 'remember-blosxom)
96 ;; Used to pass the current prompt string from
97 ;; `remember-blosxom-read-new-wiki-page' to `remember-blosxom-predicate'
98 (defvar remember-blosxom-prompt nil)
100 ;; Used to pass the current completion directory from
101 ;; `remember-blosxom-read-new-wiki-page' to `remember-blosxom-predicate'
102 (defvar remember-blosxom-category-directory nil)
104 ;; Used to remember previous user entries to
105 ;; `remember-blosxom-read-new-wiki-page'
106 (defvar remember-blosxom-history-list nil)
108 (defun remember-blosxom-read-new-wiki-page (&optional prompt default)
109 "Read a new wiki page name in the mini-buffer with completion.
110 Entries that refer to directories or existing wiki page names are not
111 accepted."
112 (with-emacs-wiki-project blosxom-project
113 (let* ((insert-default-directory nil)
114 (dir (car emacs-wiki-directories))
115 (prompt (or prompt "Blosxom Category: "))
116 (remember-blosxom-prompt prompt)
117 (remember-blosxom-category-directory dir)
118 initial file-name)
119 ;; while we read the name of an existing directory or wiki page...
120 (while (progn
121 (setq initial (completing-read-file-name
122 prompt dir default nil initial
123 'remember-blosxom-history-list
124 'remember-blosxom-predicate)
125 file-name (concat (directory-file-name dir)
126 "/" initial))
127 (file-exists-p file-name))
128 (message "%s%s [%s]" prompt initial
129 (if (file-directory-p file-name) "Directory" "File Exists"))
130 ;; ...break up the result for the next iteration and try again
131 (setq initial (cons initial (1+ (length initial)))
132 remember-blosxom-category-directory
133 (file-name-directory file-name))
134 (sit-for 1))
135 (concat (directory-file-name dir) "/"
136 (if (consp initial) (car initial) initial)))))
138 (defun remember-blosxom-predicate (cons-cell)
139 "Return nil if the car of CONS-CELL is an existing (non-directory) file.
140 This function is used as the PREDICATE argument of `completing-read-file-name'
141 when called from `remember-blosxom-read-new-wiki-page' to prune out existing
142 wiki page names from the possible completions."
143 (let* ((directory remember-blosxom-category-directory)
144 (prompt remember-blosxom-prompt)
145 (prompt-re (concat "^" (regexp-quote prompt)))
146 (raw-string (buffer-string))
147 (field-start (progn (string-match prompt-re raw-string)
148 (match-end 0)))
149 (minibuffer-string (substring raw-string field-start))
150 (prefix-string (file-name-directory minibuffer-string))
151 (file-name (concat (directory-file-name directory)
152 "/" prefix-string (car cons-cell))))
153 (not (and (file-exists-p file-name) (not (file-directory-p file-name))))))
155 (provide 'remember-blosxom)
157 ;;; remember-blosxom.el ends here