1 ;;; tinyappend.el --- A simple text gathering to buffer utility.
3 ;; This file is not part of Emacs
7 ;; Copyright (C) 1994-2007 Jari Aalto
8 ;; Keywords: extensions
10 ;; Maintainer: Jari Aalto
12 ;; To get information on this program, call M-x tinyappend-version
13 ;; Look at the code with folding.el
17 ;; This program is free software; you can redistribute it and/or modify it
18 ;; under the terms of the GNU General Public License as published by the Free
19 ;; Software Foundation; either version 2 of the License, or (at your option)
22 ;; This program is distributed in the hope that it will be useful, but
23 ;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with program; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
32 ;; Visit <http://www.gnu.org/copyleft/gpl.html> for more information
39 ;; ....................................................... &t-install ...
40 ;; Put this file on your Emacs-Lisp load path, add following into your
42 ;; (require 'tinyappend)
44 ;; Autoload, prefer this one, your emacs starts quicker.
46 ;; (autoload 'tinyappend-beg "tinyappend" "" t)
47 ;; (autoload 'tinyappend-end "tinyappend" "" t)
49 ;; If you do not want the default key bindings, add this before the
52 ;; (setq tinyappend-:load-hook nil)
54 ;; If you have any questions, suggestions, use this function
56 ;; M-x tinyappend-submit-bug-report
62 ;; ..................................................... &t-commentary ...
66 ;; Preface, March 1994
68 ;; This package does nothing fancy, it gathers text from buffers with
69 ;; few key bindings. Later you can then peek on that buffer, arrange
70 ;; text etc. `C-x' `a' is handy when appending data to buffer, but
71 ;; it's annoying that you have to give "buffer name" all the time This
72 ;; one adds to buffer "*append*" automatically, creating one if it
75 ;; I'd strongly recommend you to keep `transient-mark-mode' (Emacs) on
76 ;; all the time, so that you can see if you're adding a selected
77 ;; region into the *append* buffer. If the region is not active, these
78 ;; functions normally add the current line to the *append* buffer.
80 ;; Suggested default bindings
82 ;; C-c + Append to the end
83 ;; C-c _ (underscore) Append to the beginning
84 ;; C-c - Kill (empty) *append* buffer
85 ;; C-c | Yank text from append buffer
96 ;;; ......................................................... &require ...
100 (eval-when-compile (ti::package-use-dynamic-compilation))
102 (ti::package-defgroup-tiny TinyAppend tinyappend-: extensions
103 "Gather text to separate cut buffer.")
106 ;;{{{ setup: variables
108 ;;; ......................................................... &v-hooks ...
110 (defcustom tinyappend-:load-hook nil
111 "*Hook run when file has been loaded."
115 ;;; ........................................................ &v-public ...
116 ;;; User configurable
118 (defcustom tinyappend-:buffer "~/.append"
119 "*Buffer where to save text.
120 If this variable has star at the beginning of name, like *append*,
121 it is considered that the buffer doesn't need saving to any file.
123 Otherwise if file with the same name exists when tinyappend.el is
124 being loaded, the buffer will hold the contents of the file
125 _only_ if buffer is empty initially."
132 ;;; ....................................................... &v-version ...
134 ;;;###autoload (autoload 'tinyappend-version "tinyappend" "Display commentary." t)
136 (ti::macrof-version-bug-report
139 tinyappend-:version-id
140 "$Id: tinyappend.el,v 2.40 2007/05/01 17:20:42 jaalto Exp $"
142 tinyappend-:load-hook
143 tinyappend-:buffer)))
147 ;;; ########################################################### &funcs ###
151 ;;; ----------------------------------------------------------------------
153 (defun tinyappend-install-default-key-bindings ()
154 "Install default key bindings."
156 (global-set-key "\C-c=" 'tinyappend-end) ;; non-shift key
157 (global-set-key "\C-c-" 'tinyappend-beg) ;; non-shift key
158 (global-set-key "\C-c_" 'tinyappend-kill)
159 (global-set-key "\C-c|" 'tinyappend-yank))
161 ;;; ----------------------------------------------------------------------
163 (defmacro tinyappend-line-area-args (msg)
164 "Return region of current line: (beg end MSG) including newline."
167 (line-beginning-position)
170 (ignore-errors (forward-char 1)) ;get newline, unless EOB
174 ;;; ----------------------------------------------------------------------
176 (defun tinyappend-get-buffer ()
177 "Create `tinyappend-:buffer' and initializes its content from file if it exists.
183 (set-buffer (get-buffer-create tinyappend-:buffer))
184 (when (not (char= (aref tinyappend-:buffer 0) ?\* ))
185 (if (and (file-exists-p tinyappend-:buffer) ;history file exists ?
186 (ti::buffer-empty-p))
187 (insert-file-contents tinyappend-:buffer))
188 ;; link to file so that Emacs asks to save the buffer
189 ;; when you quit with C-x C-c
190 (setq buffer-file-name (expand-file-name tinyappend-:buffer))
191 (rename-buffer tinyappend-:buffer)))
192 (get-buffer tinyappend-:buffer))
197 ;;; ----------------------------------------------------------------------
199 (defun tinyappend-append-to-buffer (beg end &optional arg msg verb)
200 "Store BEG END with ARG and MSG to `tinyappend-:buffer'.
201 Default is to store the end of buffer. Prefix argument ARG means:
203 0 = kill append buffer
204 \\[universal-argument] = adds to the beginning
206 VERB allows verbose messages."
208 (setq msg (or msg ""))
211 (copy-region-as-kill beg end))
212 (set-buffer (tinyappend-get-buffer))
214 (cond ; According to prefix
215 ((eq arg 0) ; yank to the beginning
216 (kill-buffer (current-buffer))
217 (and verb (message (concat "TIA buffer killed"))))
218 ((and (not (null arg)) ; yank to the beginning
220 (goto-char (point-min)) (yank)
222 (message (format "*appended %s BEG*" msg))))
224 (goto-char (point-max)) (yank)
226 (message (format "*appended %s*" msg)))))))
228 ;;; ----------------------------------------------------------------------
231 (defun tinyappend-end (&optional beg end msg)
232 "Store region BEG END with MSG or current line to the end of `tinyappend-:buffer'."
234 (if (region-active-p)
235 (list (region-beginning) (region-end) "region")
236 (tinyappend-line-area-args "line")))
237 (tinyappend-append-to-buffer beg end nil msg 'verb))
239 ;;; ----------------------------------------------------------------------
242 (defun tinyappend-beg (&optional beg end msg)
243 "Store BEG END with MSG or current line to the beginning of `tinyappend-:buffer'."
245 (if (region-active-p)
246 (list (region-beginning) (region-end) "region")
247 (tinyappend-line-area-args "line")))
248 (tinyappend-append-to-buffer beg end '(4) msg 'verb))
250 ;;; ----------------------------------------------------------------------
253 (defun tinyappend-kill ()
254 "Kill `tinyappend-:buffer' buffer."
256 (if (get-buffer tinyappend-:buffer)
257 (kill-buffer tinyappend-:buffer)))
259 ;;; ----------------------------------------------------------------------
262 (defun tinyappend-yank (&optional kill)
263 "Yank `tinyappend-:buffer' to the current position. Optionally KILL `tinyappend-:buffer'."
265 (if (null (get-buffer tinyappend-:buffer))
266 (message (concat "Can't yank, there is no buffer: " tinyappend-:buffer))
267 (insert-buffer tinyappend-:buffer)
273 (if (not (get-file-buffer tinyappend-:buffer))
274 (tinyappend-get-buffer))
276 (provide 'tinyappend)
277 (run-hooks 'tinyappend-:load-hook)
279 ;;; tinyappend.el ends here