1 ;;; ztree-util.el --- Auxulary utilities for the ztree package
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Author: Alexey Veretennikov <alexey dot veretennikov at gmail dot com>
9 ;; Keywords: files tools
10 ;; URL: https://github.com/fourier/ztree
11 ;; Compatibility: GNU Emacs GNU Emacs 24.x
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (defun ztree-find (where which)
32 "Find element of the list WHERE matching predicate WHICH."
35 (when (funcall which elt)
39 (defun ztree-filter (condp lst)
40 "Filter out elements not satisfying predicate CONDP in the list LST.
41 Taken from http://www.emacswiki.org/emacs/ElispCookbook#toc39"
43 (mapcar (lambda (x) (and (funcall condp x) x)) lst)))
46 (defun printable-string (string)
47 "Strip newline character from file names, like 'Icon\n.
48 Argument STRING string to process.'."
49 (replace-regexp-in-string "\n" "" string))
51 (defun file-short-name (file)
52 "By given FILE name return base file/directory name.
53 Taken from http://lists.gnu.org/archive/html/emacs-devel/2011-01/msg01238.html"
54 (printable-string (file-name-nondirectory (directory-file-name file))))
57 (defun newline-and-begin ()
58 "Move a point to the beginning of the next line."
62 (defun car-atom (value)
63 "Return VALUE if value is an atom, otherwise (car value) or nil.
64 Used since `car-safe' returns nil for atoms"
65 (if (atom value) value (car value)))
68 (defun insert-with-face (text face)
69 "Insert TEXT with the FACE provided."
70 (let ((start (point)))
72 (put-text-property start (point) 'face face)))
75 (defmacro defrecord (record-name record-fields)
76 "Create a record (structure) and getters/setters.
78 Record is the following set of functions:
79 - Record constructor with name \"RECORD-NAME\"-create and list of
80 arguments which will be assigned to RECORD-FIELDS
81 - Record getters with names \"record-name\"-\"field\" accepting one
82 argument - the record; \"field\" is from \"record-fields\" symbols
83 - Record setters with names \"record-name\"-set-\"field\" accepting two
84 arguments - the record and the field value
87 \(defrecord person (name age))
89 will be expanded to the following functions:
91 \(defun person-create (name age) (...)
92 \(defun person-name (record) (...)
93 \(defun person-age (record) (...)
94 \(defun person-set-name (record value) (...)
95 \(defun person-set-age (record value) (...)"
96 (let ((ctor-name (intern (concat (symbol-name record-name) "-create")))
97 (rec-var (make-symbol "record")))
99 ;; constructor with the name "record-name-create"
100 ;; with arguments list "record-fields" expanded
101 (defun ,ctor-name (,@record-fields)
103 ,@(mapcar #'(lambda (x)
104 (list 'setq rec-var (list 'plist-put rec-var (list 'quote x) x)))
106 ;; getters with names "record-name-field" where the "field"
107 ;; is from record-fields
108 ,@(mapcar #'(lambda (x)
109 (let ((getter-name (intern (concat (symbol-name record-name)
113 (defun ,getter-name (,rec-var)
114 (plist-get ,rec-var ',x)
117 ;; setters wit names "record-name-set-field where the "field"
118 ;; is from record-fields
119 ;; arguments for setters: (record value)
120 ,@(mapcar #'(lambda (x)
121 (let ((setter-name (intern (concat (symbol-name record-name)
125 (defun ,setter-name (,rec-var value)
126 (setq ,rec-var (plist-put ,rec-var ',x value))
131 (provide 'ztree-util)
133 ;;; ztree-util.el ends here