(lsdb-read) [Emacs]: Don't create temp buffer.
[elisp/lsdb.git] / lsdb.el
diff --git a/lsdb.el b/lsdb.el
index f4a442e..718a214 100644 (file)
--- a/lsdb.el
+++ b/lsdb.el
@@ -229,6 +229,11 @@ Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
   :group 'lsdb
   :type 'string)
 
+(defcustom lsdb-verbose t
+  "If non-nil, confirm user to submit changes to lsdb-hash-table."
+  :type 'boolean
+  :group 'lsdb)
+
 ;;;_. Faces
 (defface lsdb-header-face
   '((t (:underline t)))
@@ -366,11 +371,8 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
        (save-excursion
         (goto-char marker)
         (if (looking-at "^#s(")
-            (with-temp-buffer
-              (buffer-disable-undo)
-              (insert-buffer-substring (marker-buffer marker) marker)
-              (goto-char (point-min))
-              (delete-char 2)
+            (progn
+              (forward-char 2) ;skip "#s"
               (let ((object (read (current-buffer)))
                     hash-table data)
                 (if (eq 'hash-table (car object))
@@ -383,7 +385,8 @@ This is the current number of slots in HASH-TABLE, whether occupied or not."
                       (while data
                         (lsdb-puthash (pop data) (pop data) hash-table))
                       hash-table)
-                  object)))))))))
+                  object)))
+          (read marker)))))))
 
 (defun lsdb-load-hash-tables ()
   "Read the contents of `lsdb-file' into the internal hash tables."
@@ -974,6 +977,14 @@ Modify whole identification by side effect."
     (unless (equal entry-name "")
       (intern (downcase entry-name)))))
 
+(defun lsdb-delete-entry (record entry)
+  "Delete given ENTRY from RECORD."
+  (setcdr record (delq entry (cdr record)))
+  (lsdb-puthash (car record) (cdr record)
+               lsdb-hash-table)
+  (run-hook-with-args 'lsdb-update-record-functions record)
+  (setq lsdb-hash-tables-are-dirty t))
+
 (defun lsdb-mode-add-entry (entry-name)
   "Add an entry on the current line."
   (interactive
@@ -1008,42 +1019,41 @@ Modify whole identification by side effect."
                 (point))
               (list 'lsdb-record record)))))))))
 
-(defun lsdb-mode-delete-entry (&optional entry-name dont-update)
+(defun lsdb-mode-delete-entry-1 (entry)
+  "Delete text contents of the ENTRY from the current buffer."
+  (save-restriction
+    (lsdb-narrow-to-record)
+    (let ((case-fold-search t)
+         (inhibit-read-only t)
+         buffer-read-only)
+      (goto-char (point-min))
+      (if (re-search-forward
+          (concat "^\t" (capitalize (symbol-name (car entry))) ":")
+          nil t)
+         (delete-region (match-beginning 0)
+                        (if (re-search-forward
+                             "^\t[^\t][^:]+:" nil t)
+                            (match-beginning 0)
+                          (point-max)))))))
+
+(defun lsdb-mode-delete-entry ()
   "Delete the entry on the current line."
   (interactive)
   (let ((record (lsdb-current-record))
-       entry)
+       entry-name entry)
     (unless record
       (error "There is nothing to follow here"))
-    (unless entry-name
-      (setq entry-name (or (lsdb-current-entry)
-                          (lsdb-read-entry
-                           record "Which entry to delete: "))))
-    (setq entry (assq entry-name (cdr record)))
+    (setq entry-name (or (lsdb-current-entry)
+                        (lsdb-read-entry record "Which entry to delete: "))
+         entry (assq entry-name (cdr record)))
     (when (and entry
-              (not dont-update))
-      (setcdr record (delq entry (cdr record)))
-      (lsdb-puthash (car record) (cdr record)
-                   lsdb-hash-table)
-      (run-hook-with-args 'lsdb-update-record-functions record)
-      (setq lsdb-hash-tables-are-dirty t))
-    (save-restriction
-      (lsdb-narrow-to-record)
-      (let ((case-fold-search t)
-           (inhibit-read-only t)
-           buffer-read-only)
-       (goto-char (point-min))
-       (if (re-search-forward
-            (concat "^\t" (capitalize (symbol-name
-                                       (or entry-name
-                                           (lsdb-current-entry))))
-                    ":")
-            nil t)
-           (delete-region (match-beginning 0)
-                          (if (re-search-forward
-                               "^\t[^\t][^:]+:" nil t)
-                              (match-beginning 0)
-                            (point-max))))))))
+              (or (not (interactive-p))
+                  (not lsdb-verbose)
+                  (y-or-n-p
+                   (format "Do you really want to delete entry `%s' of `%s'?"
+                           entry-name (car record)))))
+      (lsdb-delete-entry record entry)
+      (lsdb-mode-delete-entry-1 entry))))
 
 (defun lsdb-mode-edit-entry ()
   "Edit the entry on the current line."
@@ -1073,7 +1083,7 @@ Modify whole identification by side effect."
              (setcdr entry form)
              (run-hook-with-args 'lsdb-update-record-functions record)
              (setq lsdb-hash-tables-are-dirty t)
-             (lsdb-mode-delete-entry ',entry-name t)
+             (lsdb-mode-delete-entry-1 entry)
              (beginning-of-line)
              (add-text-properties
               (point)
@@ -1089,6 +1099,7 @@ Modify whole identification by side effect."
       (message "(No changes need to be saved)")
     (when (or (interactive-p)
              dont-ask
+             (not lsdb-verbose)
              (y-or-n-p "Save the LSDB now? "))
       (lsdb-save-hash-tables)
       (setq lsdb-hash-tables-are-dirty nil)