diff options
| author | Jens Schmidt | 2023-05-22 21:47:13 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2023-05-31 15:53:35 +0300 |
| commit | 2e20e318da23ce74ac6f736b557e10aa402c5cf9 (patch) | |
| tree | a60b8f0642cf83bcff392ff43258e40a6e19cdfb | |
| parent | 372bc1278c2d7c2440bb3d528f4c4e03402d21b5 (diff) | |
| download | emacs-2e20e318da23ce74ac6f736b557e10aa402c5cf9.tar.gz emacs-2e20e318da23ce74ac6f736b557e10aa402c5cf9.zip | |
Brush up doc strings and terminology in plstore.el
* lisp/plstore.el (plstore-encoded, plstore-passphrase-callback-function)
(plstore--init-from-buffer, plstore-revert, plstore-close)
(plstore--merge-secret, plstore--decrypt, plstore--match, plstore-find)
(plstore-get, plstore-put, plstore-delete, plstore--insert-buffer)
(plstore-save, plstore--encode, plstore--decode)
(plstore--write-contents-functions, plstore-mode-decoded)
(plstore-mode): Brush up doc strings and documentation in general.
Fix terminology, in particular spurious occurences of all uppercase
"PLSTORE". (Bug#63627)
| -rw-r--r-- | lisp/plstore.el | 70 |
1 files changed, 52 insertions, 18 deletions
diff --git a/lisp/plstore.el b/lisp/plstore.el index 1a0dacffb01..d18d461d7d1 100644 --- a/lisp/plstore.el +++ b/lisp/plstore.el | |||
| @@ -78,7 +78,7 @@ | |||
| 78 | ;; Editing: | 78 | ;; Editing: |
| 79 | ;; | 79 | ;; |
| 80 | ;; This file also provides `plstore-mode', a major mode for editing | 80 | ;; This file also provides `plstore-mode', a major mode for editing |
| 81 | ;; the PLSTORE format file. Visit a non-existing file and put the | 81 | ;; the plstore format file. Visit a non-existing file and put the |
| 82 | ;; following line: | 82 | ;; following line: |
| 83 | ;; | 83 | ;; |
| 84 | ;; (("foo" :host "foo.example.org" :secret-user "user")) | 84 | ;; (("foo" :host "foo.example.org" :secret-user "user")) |
| @@ -235,10 +235,13 @@ symmetric encryption will be used." | |||
| 235 | 235 | ||
| 236 | (put 'plstore-encrypt-to 'permanent-local t) | 236 | (put 'plstore-encrypt-to 'permanent-local t) |
| 237 | 237 | ||
| 238 | (defvar plstore-encoded nil) | 238 | (defvar plstore-encoded nil |
| 239 | "Non-nil if the current buffer shows the decoded alist.") ; [sic!] | ||
| 239 | 240 | ||
| 240 | (put 'plstore-encoded 'permanent-local t) | 241 | (put 'plstore-encoded 'permanent-local t) |
| 241 | 242 | ||
| 243 | ;;; EasyPG callback functions. | ||
| 244 | |||
| 242 | (defvar plstore-cache-passphrase-for-symmetric-encryption nil) | 245 | (defvar plstore-cache-passphrase-for-symmetric-encryption nil) |
| 243 | (defvar plstore-passphrase-alist nil) | 246 | (defvar plstore-passphrase-alist nil) |
| 244 | 247 | ||
| @@ -255,11 +258,11 @@ symmetric encryption will be used." | |||
| 255 | (cons entry | 258 | (cons entry |
| 256 | plstore-passphrase-alist))) | 259 | plstore-passphrase-alist))) |
| 257 | (setq passphrase | 260 | (setq passphrase |
| 258 | (read-passwd (format "Passphrase for PLSTORE %s: " | 261 | (read-passwd (format "Passphrase for plstore %s: " |
| 259 | (plstore--get-buffer plstore)))) | 262 | (plstore--get-buffer plstore)))) |
| 260 | (setcdr entry (copy-sequence passphrase)) | 263 | (setcdr entry (copy-sequence passphrase)) |
| 261 | passphrase))) | 264 | passphrase))) |
| 262 | (read-passwd (format "Passphrase for PLSTORE %s: " | 265 | (read-passwd (format "Passphrase for plstore %s: " |
| 263 | (plstore--get-buffer plstore))))) | 266 | (plstore--get-buffer plstore))))) |
| 264 | 267 | ||
| 265 | (defun plstore-progress-callback-function (_context _what _char current total | 268 | (defun plstore-progress-callback-function (_context _what _char current total |
| @@ -269,6 +272,8 @@ symmetric encryption will be used." | |||
| 269 | (message "%s...%d%%" handback | 272 | (message "%s...%d%%" handback |
| 270 | (if (> total 0) (floor (* (/ current (float total)) 100)) 0)))) | 273 | (if (> total 0) (floor (* (/ current (float total)) 100)) 0)))) |
| 271 | 274 | ||
| 275 | ;;; Core functions. | ||
| 276 | |||
| 272 | (defun plstore--get-buffer (arg) | 277 | (defun plstore--get-buffer (arg) |
| 273 | (aref arg 0)) | 278 | (aref arg 0)) |
| 274 | 279 | ||
| @@ -307,6 +312,7 @@ symmetric encryption will be used." | |||
| 307 | (vector buffer alist encrypted-data secret-alist merged-alist)) | 312 | (vector buffer alist encrypted-data secret-alist merged-alist)) |
| 308 | 313 | ||
| 309 | (defun plstore--init-from-buffer (plstore) | 314 | (defun plstore--init-from-buffer (plstore) |
| 315 | "Parse current buffer and initialize PLSTORE from it." | ||
| 310 | (goto-char (point-min)) | 316 | (goto-char (point-min)) |
| 311 | (when (looking-at ";;; public entries") | 317 | (when (looking-at ";;; public entries") |
| 312 | (forward-line) | 318 | (forward-line) |
| @@ -337,16 +343,20 @@ symmetric encryption will be used." | |||
| 337 | store))) | 343 | store))) |
| 338 | 344 | ||
| 339 | (defun plstore-revert (plstore) | 345 | (defun plstore-revert (plstore) |
| 340 | "Replace current data in PLSTORE with the file on disk." | 346 | "Replace current data in PLSTORE from its associated file." |
| 341 | (with-current-buffer (plstore--get-buffer plstore) | 347 | (with-current-buffer (plstore--get-buffer plstore) |
| 342 | (revert-buffer t t) | 348 | (revert-buffer t t) |
| 343 | (plstore--init-from-buffer plstore))) | 349 | (plstore--init-from-buffer plstore))) |
| 344 | 350 | ||
| 345 | (defun plstore-close (plstore) | 351 | (defun plstore-close (plstore) |
| 346 | "Destroy a plstore instance PLSTORE." | 352 | "Destroy plstore instance PLSTORE." |
| 347 | (kill-buffer (plstore--get-buffer plstore))) | 353 | (kill-buffer (plstore--get-buffer plstore))) |
| 348 | 354 | ||
| 349 | (defun plstore--merge-secret (plstore) | 355 | (defun plstore--merge-secret (plstore) |
| 356 | "Determine the merged alist of PLSTORE. | ||
| 357 | Create the merged alist as a copy of the template alist with all | ||
| 358 | placeholder properties that have corresponding properties in the | ||
| 359 | secret alist replaced by their plain-text secret properties." | ||
| 350 | (let ((alist (plstore--get-secret-alist plstore)) | 360 | (let ((alist (plstore--get-secret-alist plstore)) |
| 351 | modified-alist | 361 | modified-alist |
| 352 | modified-plist | 362 | modified-plist |
| @@ -365,19 +375,26 @@ symmetric encryption will be used." | |||
| 365 | modified-entry (assoc (car entry) modified-alist) | 375 | modified-entry (assoc (car entry) modified-alist) |
| 366 | modified-plist (cdr modified-entry)) | 376 | modified-plist (cdr modified-entry)) |
| 367 | (while plist | 377 | (while plist |
| 378 | ;; Search for a placeholder property in the merged alist | ||
| 379 | ;; corresponding to the current secret property. | ||
| 368 | (setq placeholder | 380 | (setq placeholder |
| 369 | (plist-member | 381 | (plist-member |
| 370 | modified-plist | 382 | modified-plist |
| 371 | (intern (concat ":secret-" | 383 | (intern (concat ":secret-" |
| 372 | (substring (symbol-name (car plist)) 1))))) | 384 | (substring (symbol-name (car plist)) 1))))) |
| 385 | ;; Replace its name with the real, secret property name. | ||
| 373 | (if placeholder | 386 | (if placeholder |
| 374 | (setcar placeholder (car plist))) | 387 | (setcar placeholder (car plist))) |
| 388 | ;; Update its value to the plain-text secret property value. | ||
| 375 | (setq modified-plist | 389 | (setq modified-plist |
| 376 | (plist-put modified-plist (car plist) (car (cdr plist)))) | 390 | (plist-put modified-plist (car plist) (car (cdr plist)))) |
| 377 | (setq plist (nthcdr 2 plist))) | 391 | (setq plist (nthcdr 2 plist))) |
| 378 | (setcdr modified-entry modified-plist)))) | 392 | (setcdr modified-entry modified-plist)))) |
| 379 | 393 | ||
| 380 | (defun plstore--decrypt (plstore) | 394 | (defun plstore--decrypt (plstore) |
| 395 | "Decrypt the encrypted data of PLSTORE. | ||
| 396 | Update its internal alists and other data structures | ||
| 397 | accordingly." | ||
| 381 | (if (plstore--get-encrypted-data plstore) | 398 | (if (plstore--get-encrypted-data plstore) |
| 382 | (let ((context (epg-make-context 'OpenPGP)) | 399 | (let ((context (epg-make-context 'OpenPGP)) |
| 383 | plain) | 400 | plain) |
| @@ -404,6 +421,11 @@ symmetric encryption will be used." | |||
| 404 | (plstore--set-encrypted-data plstore nil)))) | 421 | (plstore--set-encrypted-data plstore nil)))) |
| 405 | 422 | ||
| 406 | (defun plstore--match (entry keys skip-if-secret-found) | 423 | (defun plstore--match (entry keys skip-if-secret-found) |
| 424 | "Return whether plist KEYS matches ENTRY. | ||
| 425 | ENTRY should be a key of the merged alist of a PLSTORE. This | ||
| 426 | function returns nil if KEYS do not match ENTRY, t if they match, | ||
| 427 | and symbol `secret' if the secret alist needs to be consulted to | ||
| 428 | perform a match." | ||
| 407 | (let ((result t) key-name key-value prop-value secret-name) | 429 | (let ((result t) key-name key-value prop-value secret-name) |
| 408 | (while keys | 430 | (while keys |
| 409 | (setq key-name (car keys) | 431 | (setq key-name (car keys) |
| @@ -425,11 +447,10 @@ symmetric encryption will be used." | |||
| 425 | result)) | 447 | result)) |
| 426 | 448 | ||
| 427 | (defun plstore-find (plstore keys) | 449 | (defun plstore-find (plstore keys) |
| 428 | "Perform search on PLSTORE with KEYS. | 450 | "Return all PLSTORE entries matching plist KEYS." |
| 429 | KEYS is a plist." | ||
| 430 | (let (entries alist entry match decrypt plist) | 451 | (let (entries alist entry match decrypt plist) |
| 431 | ;; First, go through the merged plist alist and collect entries | 452 | ;; First, go through the merged alist and collect entries matched |
| 432 | ;; matched with keys. | 453 | ;; by the keys. |
| 433 | (setq alist (plstore--get-merged-alist plstore)) | 454 | (setq alist (plstore--get-merged-alist plstore)) |
| 434 | (while alist | 455 | (while alist |
| 435 | (setq entry (car alist) | 456 | (setq entry (car alist) |
| @@ -445,7 +466,7 @@ KEYS is a plist." | |||
| 445 | plist nil)) | 466 | plist nil)) |
| 446 | (setq plist (nthcdr 2 plist))) | 467 | (setq plist (nthcdr 2 plist))) |
| 447 | (setq entries (cons entry entries))))) | 468 | (setq entries (cons entry entries))))) |
| 448 | ;; Second, decrypt the encrypted plist and try again. | 469 | ;; Second, decrypt the plstore and try again. |
| 449 | (when decrypt | 470 | (when decrypt |
| 450 | (setq entries nil) | 471 | (setq entries nil) |
| 451 | (plstore--decrypt plstore) | 472 | (plstore--decrypt plstore) |
| @@ -459,7 +480,8 @@ KEYS is a plist." | |||
| 459 | (nreverse entries))) | 480 | (nreverse entries))) |
| 460 | 481 | ||
| 461 | (defun plstore-get (plstore name) | 482 | (defun plstore-get (plstore name) |
| 462 | "Get an entry with NAME in PLSTORE." | 483 | "Return the entry named NAME in PLSTORE. |
| 484 | Return nil if there is none." | ||
| 463 | (let ((entry (assoc name (plstore--get-merged-alist plstore))) | 485 | (let ((entry (assoc name (plstore--get-merged-alist plstore))) |
| 464 | plist) | 486 | plist) |
| 465 | (setq plist (cdr entry)) | 487 | (setq plist (cdr entry)) |
| @@ -473,7 +495,7 @@ KEYS is a plist." | |||
| 473 | entry)) | 495 | entry)) |
| 474 | 496 | ||
| 475 | (defun plstore-put (plstore name keys secret-keys) | 497 | (defun plstore-put (plstore name keys secret-keys) |
| 476 | "Put an entry with NAME in PLSTORE. | 498 | "Put an entry named NAME in PLSTORE. |
| 477 | KEYS is a plist containing non-secret data. | 499 | KEYS is a plist containing non-secret data. |
| 478 | SECRET-KEYS is a plist containing secret data." | 500 | SECRET-KEYS is a plist containing secret data." |
| 479 | (let (entry | 501 | (let (entry |
| @@ -512,7 +534,7 @@ SECRET-KEYS is a plist containing secret data." | |||
| 512 | (plstore--merge-secret plstore))) | 534 | (plstore--merge-secret plstore))) |
| 513 | 535 | ||
| 514 | (defun plstore-delete (plstore name) | 536 | (defun plstore-delete (plstore name) |
| 515 | "Delete an entry with NAME from PLSTORE." | 537 | "Delete the first entry named NAME from PLSTORE." |
| 516 | (let ((entry (assoc name (plstore--get-alist plstore)))) | 538 | (let ((entry (assoc name (plstore--get-alist plstore)))) |
| 517 | (if entry | 539 | (if entry |
| 518 | (plstore--set-alist | 540 | (plstore--set-alist |
| @@ -531,6 +553,8 @@ SECRET-KEYS is a plist containing secret data." | |||
| 531 | 553 | ||
| 532 | (defvar pp-escape-newlines) | 554 | (defvar pp-escape-newlines) |
| 533 | (defun plstore--insert-buffer (plstore) | 555 | (defun plstore--insert-buffer (plstore) |
| 556 | "Insert the file representation of PLSTORE at point. | ||
| 557 | Assumes that PLSTORE has been decrypted." | ||
| 534 | (insert ";;; public entries -*- mode: plstore -*- \n" | 558 | (insert ";;; public entries -*- mode: plstore -*- \n" |
| 535 | (pp-to-string (plstore--get-alist plstore))) | 559 | (pp-to-string (plstore--get-alist plstore))) |
| 536 | (if (plstore--get-secret-alist plstore) | 560 | (if (plstore--get-secret-alist plstore) |
| @@ -565,12 +589,14 @@ If no one is selected, symmetric encryption will be performed. " | |||
| 565 | (insert ";;; secret entries\n" (pp-to-string cipher))))) | 589 | (insert ";;; secret entries\n" (pp-to-string cipher))))) |
| 566 | 590 | ||
| 567 | (defun plstore-save (plstore) | 591 | (defun plstore-save (plstore) |
| 568 | "Save the contents of PLSTORE associated with a FILE." | 592 | "Save PLSTORE to its associated file." |
| 569 | (with-current-buffer (plstore--get-buffer plstore) | 593 | (with-current-buffer (plstore--get-buffer plstore) |
| 570 | (erase-buffer) | 594 | (erase-buffer) |
| 571 | (plstore--insert-buffer plstore) | 595 | (plstore--insert-buffer plstore) |
| 572 | (save-buffer))) | 596 | (save-buffer))) |
| 573 | 597 | ||
| 598 | ;;; plstore mode. | ||
| 599 | |||
| 574 | ;; The functions related to plstore mode unfortunately introduce yet | 600 | ;; The functions related to plstore mode unfortunately introduce yet |
| 575 | ;; another alist format ("decoded alist"). After executing the "foo", | 601 | ;; another alist format ("decoded alist"). After executing the "foo", |
| 576 | ;; "bar", "baz" example from above the decoded alist of the plstore | 602 | ;; "bar", "baz" example from above the decoded alist of the plstore |
| @@ -587,6 +613,7 @@ If no one is selected, symmetric encryption will be performed. " | |||
| 587 | ;; `plstore-encoded' is non-nil if a buffer shows the decoded form. | 613 | ;; `plstore-encoded' is non-nil if a buffer shows the decoded form. |
| 588 | 614 | ||
| 589 | (defun plstore--encode (plstore) | 615 | (defun plstore--encode (plstore) |
| 616 | "Return the printed representation of the decoded alist of PLSTORE." | ||
| 590 | (plstore--decrypt plstore) | 617 | (plstore--decrypt plstore) |
| 591 | (let ((merged-alist (plstore--get-merged-alist plstore))) | 618 | (let ((merged-alist (plstore--get-merged-alist plstore))) |
| 592 | (concat "(" | 619 | (concat "(" |
| @@ -611,6 +638,9 @@ If no one is selected, symmetric encryption will be performed. " | |||
| 611 | ")"))) | 638 | ")"))) |
| 612 | 639 | ||
| 613 | (defun plstore--decode (string) | 640 | (defun plstore--decode (string) |
| 641 | "Create a plstore instance from STRING. | ||
| 642 | STRING should be the printed representation of a decoded alist of | ||
| 643 | some plstore." | ||
| 614 | (let* ((alist (car (read-from-string string))) | 644 | (let* ((alist (car (read-from-string string))) |
| 615 | (pointer alist) | 645 | (pointer alist) |
| 616 | secret-alist | 646 | secret-alist |
| @@ -618,7 +648,7 @@ If no one is selected, symmetric encryption will be performed. " | |||
| 618 | entry) | 648 | entry) |
| 619 | (while pointer | 649 | (while pointer |
| 620 | (unless (stringp (car (car pointer))) | 650 | (unless (stringp (car (car pointer))) |
| 621 | (error "Invalid PLSTORE format %s" string)) | 651 | (error "Invalid plstore format %s" string)) |
| 622 | (setq plist (cdr (car pointer))) | 652 | (setq plist (cdr (car pointer))) |
| 623 | (while plist | 653 | (while plist |
| 624 | (when (string-match "\\`:secret-" (symbol-name (car plist))) | 654 | (when (string-match "\\`:secret-" (symbol-name (car plist))) |
| @@ -638,6 +668,10 @@ If no one is selected, symmetric encryption will be performed. " | |||
| 638 | (plstore--make nil alist nil secret-alist))) | 668 | (plstore--make nil alist nil secret-alist))) |
| 639 | 669 | ||
| 640 | (defun plstore--write-contents-functions () | 670 | (defun plstore--write-contents-functions () |
| 671 | "Convert the decoded form of a plstore in the current buffer. | ||
| 672 | Convert it to the regular file representation of a plstore if | ||
| 673 | needed. This function is used on hook `write-contents-functions' | ||
| 674 | in plstore mode buffers." | ||
| 641 | (when plstore-encoded | 675 | (when plstore-encoded |
| 642 | (let ((store (plstore--decode (buffer-string))) | 676 | (let ((store (plstore--decode (buffer-string))) |
| 643 | (file (buffer-file-name))) | 677 | (file (buffer-file-name))) |
| @@ -675,7 +709,7 @@ If no one is selected, symmetric encryption will be performed. " | |||
| 675 | (erase-buffer) | 709 | (erase-buffer) |
| 676 | (insert | 710 | (insert |
| 677 | (substitute-command-keys "\ | 711 | (substitute-command-keys "\ |
| 678 | ;;; You are looking at the decoded form of the PLSTORE file.\n\ | 712 | ;;; You are looking at the decoded form of the plstore file.\n\ |
| 679 | ;;; To see the original form content, do \\[plstore-mode-toggle-display]\n\n")) | 713 | ;;; To see the original form content, do \\[plstore-mode-toggle-display]\n\n")) |
| 680 | (insert (plstore--encode store)) | 714 | (insert (plstore--encode store)) |
| 681 | (set-buffer-modified-p nil) | 715 | (set-buffer-modified-p nil) |
| @@ -690,7 +724,7 @@ If no one is selected, symmetric encryption will be performed. " | |||
| 690 | 724 | ||
| 691 | ;;;###autoload | 725 | ;;;###autoload |
| 692 | (define-derived-mode plstore-mode emacs-lisp-mode "PLSTORE" | 726 | (define-derived-mode plstore-mode emacs-lisp-mode "PLSTORE" |
| 693 | "Major mode for editing PLSTORE files." | 727 | "Major mode for editing plstore files." |
| 694 | (make-local-variable 'plstore-encoded) | 728 | (make-local-variable 'plstore-encoded) |
| 695 | (add-hook 'write-contents-functions #'plstore--write-contents-functions) | 729 | (add-hook 'write-contents-functions #'plstore--write-contents-functions) |
| 696 | (define-key plstore-mode-map "\C-c\C-c" #'plstore-mode-toggle-display) | 730 | (define-key plstore-mode-map "\C-c\C-c" #'plstore-mode-toggle-display) |