Pass HIDDEN via universal argument, not reading directly.
[pelican-mode.git] / pelican-mode.el
index 8a53c27..3e710be 100644 (file)
 ;;    ancestor is irrelevant.
 ;;  * If the next component is ``pages'', that indicates a page
 ;;    rather than an article.
 ;;    ancestor is irrelevant.
 ;;  * If the next component is ``pages'', that indicates a page
 ;;    rather than an article.
+;;
+;; To enable by default on all text files in a Pelican site:
+;;
+;;     (require 'pelican-mode)
+;;     (pelican-global-mode)
+;;
+;; Or, register `pelican-mode' or `pelican-mode-enable-if-site'
+;; as hook functions for more direct control.
 
 
 ;;; Code:
 
 
 ;;; Code:
@@ -72,6 +80,7 @@ about metadata fields and special values."
 (defcustom pelican-mode-formats
   '((markdown-mode . pelican-mode-set-field-markdown-mode)
     (adoc-mode . pelican-mode-set-field-adoc-mode)
 (defcustom pelican-mode-formats
   '((markdown-mode . pelican-mode-set-field-markdown-mode)
     (adoc-mode . pelican-mode-set-field-adoc-mode)
+    (org-mode . pelican-mode-set-field-org-mode)
     (rst-mode . pelican-mode-set-field-rst-mode))
   "Functions to handle setting metadata, based on major mode.
 
     (rst-mode . pelican-mode-set-field-rst-mode))
   "Functions to handle setting metadata, based on major mode.
 
@@ -84,7 +93,7 @@ arguments, field and value strings."
   "Generate a pelican-mode-compatible timestamp for TIME."
   (format-time-string "%Y-%m-%d %H:%M" time))
 
   "Generate a pelican-mode-compatible timestamp for TIME."
   (format-time-string "%Y-%m-%d %H:%M" time))
 
-(defun pelican-mode-insert-header (&rest fields)
+(defun pelican-mode-set-fields (&rest fields)
   "Insert a Pelican header for an article with metadata FIELDS."
   (mapc (apply-partially #'apply #'pelican-mode-set-field)
         (seq-partition fields 2)))
   "Insert a Pelican header for an article with metadata FIELDS."
   (mapc (apply-partially #'apply #'pelican-mode-set-field)
         (seq-partition fields 2)))
@@ -92,19 +101,23 @@ arguments, field and value strings."
 (defun pelican-mode-insert-draft-article-header (title tags)
   "Insert a Pelican header for a draft with a TITLE and TAGS."
   (interactive "sArticle title: \nsTags: ")
 (defun pelican-mode-insert-draft-article-header (title tags)
   "Insert a Pelican header for a draft with a TITLE and TAGS."
   (interactive "sArticle title: \nsTags: ")
-  (apply #'pelican-mode-insert-header
-         `(:title ,title ,@pelican-mode-default-article-fields :tags ,tags)))
+  (apply #'pelican-mode-set-fields
+         `(:title ,title
+           ,@pelican-mode-default-article-fields
+           :tags ,tags)))
 
 (defun pelican-mode-insert-page-header (title &optional hidden)
 
 (defun pelican-mode-insert-page-header (title &optional hidden)
-  "Insert a Pelican header for a page with a TITLE, potentially HIDDEN."
-  (interactive
-   (list (read-string "Page title: ")
-         (y-or-n-p "Hidden? ")))
-  (apply #'pelican-mode-insert-header
-         `(:title ,title ,@pelican-mode-default-page-fields
-                  :hidden ,(when hidden "hidden"))))
-
-(defun pelican-mode-insert-auto-header ()
+  "Insert a Pelican header for a page with a TITLE.
+
+If HIDDEN is non-nil, the page is marked hidden; otherwise it
+has no status."
+  (interactive "sPage title: \nP")
+  (apply #'pelican-mode-set-fields
+         (append
+          (list :title title :status (when hidden "hidden"))
+          pelican-mode-default-page-fields)))
+
+(defun pelican-mode-insert-header ()
   "Insert a Pelican header for a page or article."
   (interactive)
   (call-interactively
   "Insert a Pelican header for a page or article."
   (interactive)
   (call-interactively
@@ -160,21 +173,43 @@ arguments, field and value strings."
               (replace-match text)
             (insert text)))))))
 
               (replace-match text)
             (insert text)))))))
 
+(defun pelican-mode-set-field-org-mode (field value)
+  "Set Org global metadata FIELD to VALUE."
+  ;; None of org-mode's functions I can find for setting properties
+  ;; operate on the global list, only a single property drawer.
+  (setq field (upcase field))
+  (setq field
+        (format (if (member field '("TITLE" "DATE" "CATEGORY" "AUTHOR"))
+                    "#+%s:"
+                  "#+PROPERTY: %s")
+                field))
+  (let ((text (when value (format "%s %s\n" field value))))
+    (if (re-search-forward (format "^%s .*\n" (regexp-quote field)) nil t)
+        (replace-match (or text ""))
+      (when text
+        (if (re-search-forward "^$" nil t)
+            (replace-match text)
+          (insert text))))))
+
 (defun pelican-mode-set-field (field value)
   "Set FIELD to VALUE.
 
 FIELD may be a string or a symbol; if it is a symbol, the
 symbol name is used (removing a leading ':' if present).
 
 (defun pelican-mode-set-field (field value)
   "Set FIELD to VALUE.
 
 FIELD may be a string or a symbol; if it is a symbol, the
 symbol name is used (removing a leading ':' if present).
 
-VALUE may be any value; except for the following special values,
-the unquoted printed representation of it is used:
+When called from Lisp, VALUE may be any value; except for the
+following special values, the unquoted printed representation of
+it is used:
 
 - `now' means the current time; see `pelican-mode-timestamp'.
 
 - `slug' means the file's path relative to the document root sans
   extension; see `pelican-mode-default-slug'.
 
 
 - `now' means the current time; see `pelican-mode-timestamp'.
 
 - `slug' means the file's path relative to the document root sans
   extension; see `pelican-mode-default-slug'.
 
-- nil or an empty string removes the field."
+- nil or an empty string removes the field.
+
+The buffer must be in a format listed in `pelican-mode-formats'
+for this function to work correctly."
   (interactive "sField: \nsValue: ")
   (setq value (pcase value
                 ('now (pelican-mode-timestamp))
   (interactive "sField: \nsValue: ")
   (setq value (pcase value
                 ('now (pelican-mode-timestamp))
@@ -268,10 +303,11 @@ for editing articles or pages:
 
 \\{pelican-mode-map}"
   :lighter " Pelican"
 
 \\{pelican-mode-map}"
   :lighter " Pelican"
-  :keymap `((,(kbd "C-c P n") . pelican-mode-insert-auto-header)
+  :keymap `((,(kbd "C-c P f") . pelican-set-field)
+            (,(kbd "C-c P h") . pelican-make-html)
+            (,(kbd "C-c P n") . pelican-mode-insert-header)
             (,(kbd "C-c P p") . pelican-mode-publish-draft)
             (,(kbd "C-c P t") . pelican-mode-update-date)
             (,(kbd "C-c P p") . pelican-mode-publish-draft)
             (,(kbd "C-c P t") . pelican-mode-update-date)
-            (,(kbd "C-c P h") . pelican-make-html)
             (,(kbd "C-c P u") . pelican-make-rsync-upload)))
 
 ;;;###autoload
             (,(kbd "C-c P u") . pelican-make-rsync-upload)))
 
 ;;;###autoload