summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeo Vivier <zaeph@zaeph.net>2022-09-21 23:11:01 +0200
committerLeo Vivier <zaeph@zaeph.net>2022-09-21 23:15:23 +0200
commit17ca2c4fb44096e509714e4edd0a56797d18e530 (patch)
tree8c22a5a2f2c982ead856d319141cdcc27cd7bcbf
parent8a1f2ca2faa3ce9598a2881adc7311c9e87d875f (diff)
downloademacsconf-wiki-17ca2c4fb44096e509714e4edd0a56797d18e530.tar.xz
emacsconf-wiki-17ca2c4fb44096e509714e4edd0a56797d18e530.zip
Add actions to trade safety for convenience
Diffstat (limited to '')
-rw-r--r--2022/organizers-notebook.org115
1 files changed, 109 insertions, 6 deletions
diff --git a/2022/organizers-notebook.org b/2022/organizers-notebook.org
index 0a11df9b..316f3966 100644
--- a/2022/organizers-notebook.org
+++ b/2022/organizers-notebook.org
@@ -8,18 +8,121 @@
#+TOC: headlines 4
-* How to work with this file :noexport:
+* Working with this file :noexport:
+You can open the following links (~C-c C-o~ or ~<mouse-1>~) to run the associated actions:
-You can open these links (~C-c C-o~) to run their associated actions:
+General:
+- [[elisp:(org-babel-ref-resolve "general-setup()")][General setup]] (to get the main function definitions)
+- [[elisp:(org-babel-ref-resolve "i-like-danger()")][I’m okay trading off security for convenience]] / [[elisp:(org-babel-ref-resolve "back-to-safety()")][Bring me back to safety]]
+ - [[elisp:(org-babel-ref-resolve "elisp-no-confirmation")][Do not ask for confirmation when evaluating elisp links or org-babel blocks]]
+- [[elisp:(org-babel-execute-buffer)][Execute buffer]] (to get *all* function definitions)
+
+Export:
- [[elisp:(progn (org-md-export-to-markdown) (org-babel-tangle))][Export and tangle]]
- [[elisp:(emacsconf-org-publish-this-page)][Export, tangle, commit, push]]
-- [[elisp:(org-babel-execute-buffer)][Execute buffer]]
- [[elisp:(org-babel-ref-resolve "md-export-on-save-toggle-on()")][Export on save]]
- [[elisp:(org-babel-ref-resolve "md-export-on-save-toggle-off()")][Stop exporting on save]]
+
+** Code
+*** General
+#+name: general-setup
+#+begin_src emacs-lisp :eval query
+(defvar emacsconf-danger-allow-dangerous-stuff nil
+ "When non-nil, trade safety for convenience.")
+
+(defvar emacsconf-danger-asked nil
+ "When non-nil, user has already been asked for security trade-off.")
+
+(defun emacsconf-danger--ask (forms)
+ "Ask to run dangerous FORMS.
+Return t if the answer is “yes”."
+ (when (y-or-n-p (format "FORMS:\n%s\n\nThis is dangerous. Run anyway? "
+ (prin1-to-string forms)))
+ (unless emacsconf-danger-asked
+ (if (y-or-n-p "Would you like to trade security for convenience for the rest of the session? ")
+ (setq-local emacsconf-danger-allow-dangerous-stuff t)
+ (setq-local emacsconf-danger-asked t)))
+ t))
+
+(defmacro emacsconf-danger--shield (error &rest forms)
+ "Protect user from dangerous FORMS.
+Throw an error if ERROR is non-nil, skip otherwise."
+ `(let ((shield (not (or emacsconf-danger-allow-dangerous-stuff
+ (emacsconf-danger--ask ',@forms)))))
+ (if shield
+ ,(when error
+ '(user-error "Dangerous action cancelled by user"))
+ ,@forms)))
+
+(defmacro emacsconf-danger-shield (&rest forms)
+ "Protect user from dangerous FORMS by throwing an ERROR."
+ `(emacsconf-danger--shield t ,@forms))
+
+(defmacro emacsconf-danger-skip (&rest forms)
+ "Protect user from dangerous FORMS by skipping them."
+ `(emacsconf-danger--shield nil ,@forms))
+
+(message "General setup has been loaded")
+#+end_src
+
+#+name: elisp-no-confirmation
+#+begin_src emacs-lisp
+(emacsconf-danger-shield
+ (setq-local org-confirm-babel-evaluate nil
+ org-confirm-elisp-link-function nil))
+(message "No longer asking for confirmation in this buffer")
+#+end_src
+
+#+name: i-like-danger
+#+begin_src emacs-lisp
+(setq-local emacsconf-danger-allow-dangerous-stuff t)
+
+(org-babel-ref-resolve "elisp-no-confirmation()")
+
+(message "Now allowing dangerous stuff. Buckle up, buckaroo!")
+#+end_src
+
+#+name: back-to-safety
+#+begin_src emacs-lisp
+(kill-local-variable 'emacsconf-danger-allow-dangerous-stuff)
+(kill-local-variable 'emacsconf-danger-asked)
+(kill-local-variable 'org-confirm-babel-evaluate)
+(kill-local-variable 'org-confirm-elisp-link-function)
+
+(message "Back to safety. Phew!")
+#+end_src
+
+*** Export
+#+name: md-export-on-save-setup
+#+begin_src emacs-lisp :eval query
+(defun emacsconf-export-md-on-save ()
+ "Export markdown on save.
+Meant to be used with `after-save-hook'."
+ (org-md-export-to-markdown)
+ (org-babel-tangle))
+
+(defvar emacsconf-export-md-on-save-configured t
+ "Non-nil when the setup code-block has been executed.")
+#+end_src
+
+#+name: md-export-on-save-toggle-on
+#+begin_src emacs-lisp :eval query
+(unless (bound-and-true-p emacsconf-export-md-on-save-configured)
+ (org-babel-ref-resolve "md-export-on-save-setup()"))
+
+(add-hook 'after-save-hook #'emacsconf-export-md-on-save nil t)
-You can use ~C-c /~ (~org-sparse-tree~) to filter this for TODOs,
-or ~C-c \~ (~org-match-sparse-tree~) to see anything tagged with your tag.
-~org-agenda~ with ~<~ for the file restriction may also be handy.
+(when (memq #'emacsconf-export-md-on-save after-save-hook)
+ (message "Hook is active"))
+#+end_src
+
+#+name: md-export-on-save-toggle-off
+#+begin_src emacs-lisp :eval query :outputs none
+(remove-hook 'after-save-hook #'emacsconf-export-md-on-save t)
+
+(unless (memq #'emacsconf-export-md-on-save after-save-hook)
+ (message "Hook is no longer active"))
+#+end_src
* Current tasks / status
:PROPERTIES: