]> code.delx.au - gnu-emacs-elpa/blob - ztree-util.el
Merge pull request #6 from jpkotta/master
[gnu-emacs-elpa] / ztree-util.el
1 ;;; ztree-util.el --- Auxulary utilities for the ztree package
2
3 ;; Copyright (C) 2013 Alexey Veretennikov
4 ;;
5 ;; Author: Alexey Veretennikov <alexey dot veretennikov at gmail dot com>
6 ;; Created: 2013-11-1l
7 ;; Version: 1.0.0
8 ;; Keywords: files
9 ;; URL: https://github.com/fourier/ztree
10 ;; Compatibility: GNU Emacs GNU Emacs 24.x
11 ;;
12 ;; This file is NOT part of GNU Emacs.
13 ;;
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License
16 ;; as published by the Free Software Foundation; either version 2
17 ;; of the License, or (at your option) any later version.
18 ;;
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23 ;;
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26 ;;
27 ;;; Commentary:
28
29 ;;; Code:
30 (defun ztree-find (where which)
31 "find element of the list `where` matching predicate `which`"
32 (catch 'found
33 (dolist (elt where)
34 (when (funcall which elt)
35 (throw 'found elt)))
36 nil))
37
38 (defun ztree-filter (condp lst)
39 "Filter out elements of the list `lst` not satisfying predicate `condp`.
40 Taken from http://www.emacswiki.org/emacs/ElispCookbook#toc39"
41 (delq nil
42 (mapcar (lambda (x) (and (funcall condp x) x)) lst)))
43
44
45 (defun printable-string (string)
46 "Strip newline character from file names, like 'Icon\n'"
47 (replace-regexp-in-string "\n" "" string))
48
49 (defun file-short-name (file)
50 "Base file/directory name. Taken from
51 http://lists.gnu.org/archive/html/emacs-devel/2011-01/msg01238.html"
52 (printable-string (file-name-nondirectory (directory-file-name file))))
53
54
55 (defun newline-and-begin ()
56 (newline)
57 (beginning-of-line))
58
59 (defun car-atom (value)
60 "Returns value if value is an atom, otherwise (car value) or nil.
61 Used since car-safe returns nil for atoms"
62 (if (atom value) value (car value)))
63
64
65 (defun insert-with-face (text face)
66 "Insert text with the face provided"
67 (let ((start (point)))
68 (insert text)
69 (put-text-property start (point) 'face face)))
70
71
72 (defmacro defrecord (record-name record-fields)
73 "Create a record (structure) and getters/setters.
74
75 Record is the following set of functions:
76 - Record constructor with name \"record-name\"-create and list of
77 arguments which will be assigned to record-fields
78 - Record getters with names \"record-name\"-\"field\" accepting one
79 argument - the record; \"field\" is from \"record-fields\" symbols
80 - Record setters with names \"record-name\"-set-\"field\" accepting two
81 arguments - the record and the field value
82
83 Example:
84 \(defrecord person (name age))
85
86 will be expanded to the following functions:
87
88 \(defun person-create (name age) (...)
89 \(defun person-name (record) (...)
90 \(defun person-age (record) (...)
91 \(defun person-set-name (record value) (...)
92 \(defun person-set-age (record value) (...)"
93 (let ((ctor-name (intern (concat (symbol-name record-name) "-create")))
94 (rec-var (make-symbol "record")))
95 `(progn
96 ;; constructor with the name "record-name-create"
97 ;; with arguments list "record-fields" expanded
98 (defun ,ctor-name (,@record-fields)
99 (let ((,rec-var))
100 ,@(mapcar #'(lambda (x)
101 (list 'setq rec-var (list 'plist-put rec-var (list 'quote x) x)))
102 record-fields)))
103 ;; getters with names "record-name-field" where the "field"
104 ;; is from record-fields
105 ,@(mapcar #'(lambda (x)
106 (let ((getter-name (intern (concat (symbol-name record-name)
107 "-"
108 (symbol-name x)))))
109 `(progn
110 (defun ,getter-name (,rec-var)
111 (plist-get ,rec-var ',x)
112 ))))
113 record-fields)
114 ;; setters wit names "record-name-set-field where the "field"
115 ;; is from record-fields
116 ;; arguments for setters: (record value)
117 ,@(mapcar #'(lambda (x)
118 (let ((setter-name (intern (concat (symbol-name record-name)
119 "-set-"
120 (symbol-name x)))))
121 `(progn
122 (defun ,setter-name (,rec-var value)
123 (setq ,rec-var (plist-put ,rec-var ',x value))
124 ))))
125 record-fields))))
126
127
128 (provide 'ztree-util)
129
130 ;;; ztree-util.el ends here