]> git.donarmstrong.com Git - lib.git/blob - emacs_el/tiny-tools/tiny/tinyappend.el
add tiny-tools
[lib.git] / emacs_el / tiny-tools / tiny / tinyappend.el
1 ;;; tinyappend.el --- A simple text gathering to buffer utility.
2
3 ;; This file is not part of Emacs
4
5 ;;{{{ Id
6
7 ;; Copyright (C) 1994-2007 Jari Aalto
8 ;; Keywords:     extensions
9 ;; Author:       Jari Aalto
10 ;; Maintainer:   Jari Aalto
11 ;;
12 ;; To get information on this program, call M-x tinyappend-version
13 ;; Look at the code with folding.el
14
15 ;; COPYRIGHT NOTICE
16 ;;
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)
20 ;; any later version.
21 ;;
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
25 ;; for more details.
26 ;;
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.
31 ;;
32 ;; Visit <http://www.gnu.org/copyleft/gpl.html> for more information
33
34 ;;}}}
35 ;;{{{ Install
36
37 ;;; Install:
38
39 ;; ....................................................... &t-install ...
40 ;; Put this file on your Emacs-Lisp load path, add following into your
41 ;;
42 ;;      (require 'tinyappend)
43 ;;
44 ;;  Autoload, prefer this one, your emacs starts quicker.
45 ;;
46 ;;      (autoload 'tinyappend-beg "tinyappend"  "" t)
47 ;;      (autoload 'tinyappend-end "tinyappend"  "" t)
48 ;;
49 ;;  If you do not want the default key bindings, add this before the
50 ;;  require command
51 ;;
52 ;;      (setq tinyappend-:load-hook nil)
53 ;;
54 ;;  If you have any questions, suggestions, use this function
55 ;;
56 ;;      M-x tinyappend-submit-bug-report
57
58 ;;}}}
59
60 ;;{{{ Documentation
61
62 ;; ..................................................... &t-commentary ...
63
64 ;;; Commentary:
65
66 ;;  Preface, March 1994
67 ;;
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
73 ;;      doesn't exist.
74 ;;
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.
79 ;;
80 ;;  Suggested default bindings
81 ;;
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
86 ;;
87
88 ;;}}}
89
90 ;;; Change Log:
91
92 ;;; Code:
93
94 ;;{{{ setup: require
95
96 ;;; ......................................................... &require ...
97
98 (require 'tinylibm)
99
100 (eval-when-compile (ti::package-use-dynamic-compilation))
101
102 (ti::package-defgroup-tiny TinyAppend tinyappend-: extensions
103   "Gather text to separate cut buffer.")
104
105 ;;}}}
106 ;;{{{ setup: variables
107
108 ;;; ......................................................... &v-hooks ...
109
110 (defcustom tinyappend-:load-hook nil
111   "*Hook run when file has been loaded."
112   :type 'hook
113   :group 'TinyAppend)
114
115 ;;; ........................................................ &v-public ...
116 ;;; User configurable
117
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.
122
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."
126   :type 'string
127   :group 'TinyAppend)
128
129 ;;}}}
130 ;;{{{ version
131
132 ;;; ....................................................... &v-version ...
133
134 ;;;###autoload (autoload 'tinyappend-version "tinyappend" "Display commentary." t)
135 (eval-and-compile
136   (ti::macrof-version-bug-report
137    "tinyappend.el"
138    "tinyappend"
139    tinyappend-:version-id
140    "$Id: tinyappend.el,v 2.40 2007/05/01 17:20:42 jaalto Exp $"
141    '(tili-:version-id
142      tinyappend-:load-hook
143      tinyappend-:buffer)))
144
145 ;;}}}
146
147 ;;; ########################################################### &funcs ###
148
149 ;;{{{ code: misc
150
151 ;;; ----------------------------------------------------------------------
152 ;;;
153 (defun tinyappend-install-default-key-bindings ()
154   "Install default key bindings."
155   (interactive)
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))
160
161 ;;; ----------------------------------------------------------------------
162 ;;;
163 (defmacro tinyappend-line-area-args (msg)
164   "Return region of current line: (beg end MSG) including newline."
165   (`
166    (list
167     (line-beginning-position)
168     (save-excursion
169       (end-of-line)
170       (ignore-errors (forward-char 1))  ;get newline, unless EOB
171       (point))
172     (, msg))))
173
174 ;;; ----------------------------------------------------------------------
175 ;;;
176 (defun tinyappend-get-buffer ()
177   "Create `tinyappend-:buffer' and initializes its content from file if it exists.
178
179 Return:
180   buffer pointer"
181   (interactive)
182   (save-excursion
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))
193
194 ;;}}}
195 ;;{{{ code: main
196
197 ;;; ----------------------------------------------------------------------
198 ;;;
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:
202
203   0   = kill append buffer
204   \\[universal-argument] = adds to the beginning
205
206 VERB allows verbose messages."
207   (ti::verb)
208   (setq msg (or msg  ""))
209   (save-excursion
210     (if (not (eq 0 arg))
211         (copy-region-as-kill beg end))
212     (set-buffer (tinyappend-get-buffer))
213
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
219            (listp arg))
220       (goto-char (point-min)) (yank)
221       (if verb
222           (message (format "*appended %s BEG*" msg))))
223      (t
224       (goto-char (point-max)) (yank)
225       (if verb
226           (message  (format "*appended %s*" msg)))))))
227
228 ;;; ----------------------------------------------------------------------
229 ;;;
230 ;;;###autoload
231 (defun tinyappend-end (&optional beg end msg)
232   "Store region BEG END with MSG or current line to the end of `tinyappend-:buffer'."
233   (interactive
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))
238
239 ;;; ----------------------------------------------------------------------
240 ;;;
241 ;;;###autoload
242 (defun tinyappend-beg (&optional beg end msg)
243   "Store BEG END with MSG or current line to the beginning of `tinyappend-:buffer'."
244   (interactive
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))
249
250 ;;; ----------------------------------------------------------------------
251 ;;;
252 ;;;###autoload
253 (defun tinyappend-kill ()
254   "Kill `tinyappend-:buffer' buffer."
255   (interactive)
256   (if (get-buffer tinyappend-:buffer)
257       (kill-buffer tinyappend-:buffer)))
258
259 ;;; ----------------------------------------------------------------------
260 ;;;
261 ;;;###autoload
262 (defun tinyappend-yank (&optional kill)
263   "Yank `tinyappend-:buffer' to the current position. Optionally KILL `tinyappend-:buffer'."
264   (interactive "P")
265   (if (null (get-buffer tinyappend-:buffer))
266       (message (concat "Can't yank, there is no buffer: " tinyappend-:buffer))
267     (insert-buffer tinyappend-:buffer)
268     (if kill
269         (tinyappend-kill))))
270
271 ;;}}}
272
273 (if (not (get-file-buffer tinyappend-:buffer))
274     (tinyappend-get-buffer))
275
276 (provide   'tinyappend)
277 (run-hooks 'tinyappend-:load-hook)
278
279 ;;; tinyappend.el ends here