-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms-aux.el
executable file
·108 lines (84 loc) · 3.63 KB
/
forms-aux.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
;;; forms-aux.el --- Per-record files and buffers -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Aaron Harris
;; Author: Aaron Harris <[email protected]>
;; Keywords: data forms
;; Dependencies: `forms'
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This module provides a mechanism by which `forms-mode' databases
;; can display a per-record "auxiliary file" in a separate window.
;;
;; To use this feature, set (in the database's control file) the
;; variable `forms-aux-field' to the number of a field containing
;; filenames. Then the command `forms-aux-open-file' will open the
;; file named in that field for the current record, displaying it in a
;; separate window.
;;
;; Only one auxiliary file can be opened at a time; if there is
;; already such a file opened, then `forms-aux-open-file' will kill
;; the buffer containing it. If the buffer is modified, all the usual
;; save prompts will apply.
;;
;; By default, `forms-aux-open-file' will not create files that do not
;; already exist. To change this, set `forms-aux-create-file-p' to a
;; non-nil value.
;;
;; If you wish to have the auxiliary file synchronized when changing
;; records, consider using the `forms-barb' module and putting
;; `forms-aux-open-file' in the hook `forms-barb-change-record-hook'.
;;; Code:
(require 'forms)
;;;; User Options
;;;;=============
;; So forms databases can more conveniently configure them, all of
;; these are buffer-local.
(defcustom forms-aux-create-file-p nil
"Whether `forms-aux-open-file' should create new files.
If this is non-nil, `forms-aux-open-file' will create whatever
file is named by the field numbered `forms-aux-field'.
Otherwise, it will just kill any existing auxiliary buffer and
not replace it."
:group 'forms
:type 'boolean)
(make-local-variable 'forms-aux-create-file-p)
;;;; Database Variables
;;;;===================
(defvar-local forms-aux-field nil
"Field number to open as an auxiliary file.
If nil, do not open any auxiliary file.")
;;;; State Variables
;;;;================
(defvar-local forms-aux-buffer nil
"The auxiliary file buffer for the current database.")
;;;; Implementation
;;;;===============
;;;###autoload
(defun forms-aux-open-file ()
"Open the auxiliary file for the current record.
The filename to open is the current record's value for the field
numbered by `forms-aux-field'. Open this file, if it exists, in
a separate window, and kill any other buffer previously created
by this function.
Return the newly opened buffer, but do not select it.
If `forms-aux-field' is nil, do nothing."
(interactive)
(when forms-aux-field
(when forms-aux-buffer (kill-buffer forms-aux-buffer))
(let* ((window (selected-window))
(filename (nth forms-aux-field forms-fields))
(aux-buffer (when (or forms-aux-create-file-p
(file-exists-p filename))
(find-file-other-window filename))))
(select-window window)
(setq forms-aux-buffer aux-buffer))))
(provide 'forms-aux)
;;; forms-aux.el ends here