From 5087de0d70151c33d66eb13dda84d78a361d7053 Mon Sep 17 00:00:00 2001 From: TEC Date: Fri, 1 Oct 2021 02:02:22 +0800 Subject: [PATCH] ob-tangle: Accept more :tangle-mode forms * lisp/ob-tangle.el (org-babel-tangle): Accept many more forms for :tangle-mode, including octal strings (#o755, 0755, 755), ls forms (rwx, rw-r--r--), and chmod forms (a=rw,u+x). The interpretation of the input is now handled by the new function `org-babel-interpret-file-mode' which references the new variable `org-babel-tangle-default-mode' when considering relative mode forms. --- lisp/ob-tangle.el | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el index 2dd1d031c..28a235429 100644 --- a/lisp/ob-tangle.el +++ b/lisp/ob-tangle.el @@ -140,6 +140,14 @@ (defcustom org-babel-process-comment-text 'org-remove-indentation :version "24.1" :type 'function) +(defcustom org-babel-tangle-default-mode #o544 + "The default mode used for tangled files, as an integer. +The default value 356 correspands to the octal #o544, which is +read-write permissions for the user, read-only for everyone else." + :group 'org-babel + :version "9.6" + :type 'integer) + (defun org-babel-find-file-noselect-refresh (file) "Find file ensuring that the latest changes on disk are represented in the file." @@ -255,7 +263,7 @@ (defun org-babel-tangle (&optional arg target-file lang-re) (when she-bang (unless tangle-mode (setq tangle-mode #o755))) (when tangle-mode - (add-to-list 'modes tangle-mode)) + (add-to-list 'modes (org-babel-interpret-file-mode tangle-mode))) ;; Possibly create the parent directories for file. (let ((m (funcall get-spec :mkdirp))) (and m fnd (not (string= m "no")) @@ -298,6 +306,38 @@ (defun org-babel-tangle (&optional arg target-file lang-re) path-collector)) path-collector)))) +(defun org-babel-interpret-file-mode (mode) + "Determine the integer representation of a file MODE specification. +The following forms are currently recognised: +- an integer (returned without modification) +- \"#o755\" (elisp-style octal) +- \"0755\" (c style octal) +- \"755\" (chmod style octal) +- \"rwxrw-r--\" (ls style specification) +- \"a=rw,u+x\" (chmod style) * +- \"rwx\" (interpreted as \"u=rwx\") * + +* The interpretation of these forms relies on `file-modes-symbolic-to-number', + and uses `org-babel-tangle-default-mode' as the base mode." + (cond + ((integerp mode) mode) + ((not (stringp mode)) + (error "File mode %S not recognised as a valid format." mode)) + ((string-match-p "^0?[0-7][0-7][0-7]$" mode) + (string-to-number mode 8)) + ((string-match-p "^#o[0-7][0-7][0-7]$" mode) + (string-to-number (substring mode 2) 8)) + ((string-match-p "^[ugoa]*\\(?:[+-=][rwxXstugo]*\\)+\\(,[ugoa]*\\(?:[+-=][rwxXstugo]*\\)+\\)*$" mode) + (file-modes-symbolic-to-number mode org-babel-tangle-default-mode)) + ((string-match-p "^[rwx-]\\{3\\}$" mode) + (file-modes-symbolic-to-number (concat "u=" mode) org-babel-tangle-default-mode)) + ((string-match-p "^[rwx-]\\{9\\}$" mode) + (file-modes-symbolic-to-number (concat "u=" (substring mode 0 3) + ",g=" (substring mode 3 6) + ",a=" (substring mode 6 9)) + 0)) + (t (error "File mode %S not recognised as a valid format." mode)))) + (defun org-babel-tangle-clean () "Remove comments inserted by `org-babel-tangle'. Call this function inside of a source-code file generated by -- 2.33.0