emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] Hard-coded begin/end in org-insert-structure-template [9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)]
@ 2022-03-20 11:43 Rudolf Adamkovič
  2022-03-20 12:25 ` [PATCH] " Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Rudolf Adamkovič @ 2022-03-20 11:43 UTC (permalink / raw)
  To: emacs-orgmode



Remember to cover the basics, that is, what you expected to happen and
what in fact did happen.  You don't know how to make a good report?  See

     https://orgmode.org/manual/Feedback.html#Feedback

Your bug report will be posted to the Org mailing list.
------------------------------------------------------------------------
Hello smart people!

Inspired by Protesilaos Stavrou, I started using uppercase Org keywords.
I like the results so far, but I also noticed some inflexibility in the
Org mode.  Specifically, the `org-insert-structure-template' procedure
hard-codes lowercase "begin_" and "end_".  Thus, even if one customizes
the `org-structure-template-alist' with uppercase keywords, Org still
inserts them as "begin_SRC".  Given that Org lets the user pick their
preferred style, I consider this a bug.

Rudy

Emacs  : GNU Emacs 29.0.50 (build 14, x86_64-apple-darwin21.2.0, NS appkit-2113.20 Version 12.1 (Build 21C52))
 of 2022-03-15
Package: Org mode version 9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)
-- 
"Thinking is a momentary dismissal of irrelevancies."
-- Richard Buckminster Fuller, 1969

Rudolf Adamkovič <salutis@me.com> [he/him]
Studenohorská 25
84103 Bratislava
Slovakia


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] Re: [BUG] Hard-coded begin/end in org-insert-structure-template [9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)]
  2022-03-20 11:43 [BUG] Hard-coded begin/end in org-insert-structure-template [9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)] Rudolf Adamkovič
@ 2022-03-20 12:25 ` Ihor Radchenko
  2022-03-22 20:44   ` Rudolf Adamkovič
  0 siblings, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2022-03-20 12:25 UTC (permalink / raw)
  To: Rudolf Adamkovič; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 761 bytes --]

Rudolf Adamkovič <salutis@me.com> writes:

> Inspired by Protesilaos Stavrou, I started using uppercase Org keywords.
> I like the results so far, but I also noticed some inflexibility in the
> Org mode.  Specifically, the `org-insert-structure-template' procedure
> hard-codes lowercase "begin_" and "end_".  Thus, even if one customizes
> the `org-structure-template-alist' with uppercase keywords, Org still
> inserts them as "begin_SRC".  Given that Org lets the user pick their
> preferred style, I consider this a bug.

I would not call it a bug, but the situation can certainly be improved.

We can auto-magically determine whether to use BEGIN_ or begin_
depending on the case in template type. Tentative patch attached.

Best,
Ihor


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Auto-Upcase-downcase-begin-end-in-structure-template.patch --]
[-- Type: text/x-patch, Size: 4808 bytes --]

From bdf8340ab8b41f0c9f26f5b0bdadbf079b8f6d66 Mon Sep 17 00:00:00 2001
Message-Id: <bdf8340ab8b41f0c9f26f5b0bdadbf079b8f6d66.1647778844.git.yantar92@gmail.com>
From: Ihor Radchenko <yantar92@gmail.com>
Date: Sun, 20 Mar 2022 20:15:21 +0800
Subject: [PATCH] Auto-Upcase/downcase #+begin/#+end in structure templates

* lisp/org-tempo.el (org-tempo-add-block):
* lisp/org.el (org-insert-structure-template): When inserting
 #+begin_type/#+end_type, follow type's case.  TYPE will become
 #+BEGIN_TYPE and type will become #+bein_type.

(org-insert-structure-template): Make sure that we use
case-insensitive match even when user changes case-fold-search value.

(org-structure-template-alist): Clarify selection of #+BEGIN/END
vs. #+begin/end in the docstring.
---
 lisp/org-tempo.el | 13 ++++++++++---
 lisp/org.el       | 19 +++++++++++++------
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/lisp/org-tempo.el b/lisp/org-tempo.el
index b34007bf7..cd5ef9e8e 100644
--- a/lisp/org-tempo.el
+++ b/lisp/org-tempo.el
@@ -119,11 +119,18 @@ (defun org-tempo-add-block (entry)
   "Add block entry from `org-structure-template-alist'."
   (let* ((key (format "<%s" (car entry)))
 	 (name (cdr entry))
-	 (special (member name '("src" "export"))))
+	 (special (member name '("src" "export")))
+         (upcase? (string= (car (split-string name))
+                           (upcase (car (split-string name))))))
     (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
-			   `(,(format "#+begin_%s%s" name (if special " " ""))
+			   `(,(format "#+%s_%s%s"
+                                      (if upcase? "BEGIN" "begin")
+                                      name
+                                      (if special " " ""))
 			     ,(when special 'p) '> n ,(unless special 'p) n
-			     ,(format "#+end_%s" (car (split-string name " ")))
+			     ,(format "#+%s_%s"
+                                      (if upcase? "END" "end")
+                                      (car (split-string name " ")))
 			     >)
 			   key
 			   (format "Insert a %s block" name)
diff --git a/lisp/org.el b/lisp/org.el
index 9455c15c8..26617d7c8 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -9492,7 +9492,9 @@ (defcustom org-structure-template-alist
   "An alist of keys and block types.
 `org-insert-structure-template' will display a menu with this
 list of templates to choose from.  The block type is inserted,
-with \"#+BEGIN_\" and \"#+END_\" added automatically.
+with \"#+begin_\" and \"#+end_\" added automatically.  If the block
+type is written using upper case letter, \"#+BEGIN_\" and \"#+END_\"
+are added instead.
 
 The menu keys are defined by the car of each entry in this alist.
 If two entries have the keys \"a\" and \"aa\" respectively, the
@@ -9624,18 +9626,23 @@ (defun org-insert-structure-template (type)
 Select a block from `org-structure-template-alist' then type
 either RET, TAB or SPC to write the block type.  With an active
 region, wrap the region in the block.  Otherwise, insert an empty
-block."
+block.
+
+When foo is written as FOO, upcase the #+BEGIN/END as well."
   (interactive
    (list (pcase (org--insert-structure-template-mks)
 	   (`("\t" . ,_) (read-string "Structure type: "))
 	   (`(,_ ,choice . ,_) choice))))
-  (let* ((region? (use-region-p))
+  (let* ((case-fold-search t) ; Make sure that matches are case-insnsitive.
+         (region? (use-region-p))
 	 (region-start (and region? (region-beginning)))
 	 (region-end (and region? (copy-marker (region-end))))
 	 (extended? (string-match-p "\\`\\(src\\|export\\)\\'" type))
 	 (verbatim? (string-match-p
 		     (concat "\\`" (regexp-opt '("example" "export" "src")))
-		     type)))
+		     type))
+         (upcase? (string= (car (split-string type))
+                           (upcase (car (split-string type))))))
     (when region? (goto-char region-start))
     (let ((column (current-indentation)))
       (if (save-excursion (skip-chars-backward " \t") (bolp))
@@ -9643,7 +9650,7 @@ (defun org-insert-structure-template (type)
 	(insert "\n"))
       (save-excursion
 	(indent-to column)
-	(insert (format "#+begin_%s%s\n" type (if extended? " " "")))
+	(insert (format "#+%s_%s%s\n" (if upcase? "BEGIN" "begin") type (if extended? " " "")))
 	(when region?
 	  (when verbatim? (org-escape-code-in-region (point) region-end))
 	  (goto-char region-end)
@@ -9652,7 +9659,7 @@ (defun org-insert-structure-template (type)
 	  (end-of-line))
 	(unless (bolp) (insert "\n"))
 	(indent-to column)
-	(insert (format "#+end_%s" (car (split-string type))))
+	(insert (format "#+%s_%s" (if upcase? "END" "end") (car (split-string type))))
 	(if (looking-at "[ \t]*$") (replace-match "")
 	  (insert "\n"))
 	(when (and (eobp) (not (bolp))) (insert "\n")))
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Re: [BUG] Hard-coded begin/end in org-insert-structure-template [9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)]
  2022-03-20 12:25 ` [PATCH] " Ihor Radchenko
@ 2022-03-22 20:44   ` Rudolf Adamkovič
  2022-03-23 11:59     ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Rudolf Adamkovič @ 2022-03-22 20:44 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@gmail.com> writes:

> We can auto-magically determine whether to use BEGIN_ or begin_
> depending on the case in template type. Tentative patch attached.

Interesting!

> +with \"#+begin_\" and \"#+end_\" added automatically.  If the block
> +type is written using upper case letter, \"#+BEGIN_\" and \"#+END_\"
> +are added instead.

FYI: A typo in "letter" (expected "letters")

(We could also simplify to: "if the block type consists of just
uppercase letter".)

> +  (let* ((case-fold-search t) ; Make sure that matches are case-insnsitive.
> +         (region? (use-region-p))

FYI: A typo in "case-insnsitive" (expected "case-insensitive")

Looking forward to seeing this merged!

Rudy
-- 
"I love deadlines.  I love the whooshing noise they make as they go by."
-- Douglas Adams, The Salmon of Doubt, 2002

Rudolf Adamkovič <salutis@me.com> [he/him]
Studenohorská 25
84103 Bratislava
Slovakia


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Re: [BUG] Hard-coded begin/end in org-insert-structure-template [9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)]
  2022-03-22 20:44   ` Rudolf Adamkovič
@ 2022-03-23 11:59     ` Ihor Radchenko
  2022-03-25  6:34       ` Rudolf Adamkovič
  0 siblings, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2022-03-23 11:59 UTC (permalink / raw)
  To: Rudolf Adamkovič; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 640 bytes --]

Rudolf Adamkovič <salutis@me.com> writes:

>> +with \"#+begin_\" and \"#+end_\" added automatically.  If the block
>> +type is written using upper case letter, \"#+BEGIN_\" and \"#+END_\"
>> +are added instead.
>
> FYI: A typo in "letter" (expected "letters")
>
> (We could also simplify to: "if the block type consists of just
> uppercase letter".)
>
>> +  (let* ((case-fold-search t) ; Make sure that matches are case-insnsitive.
>> +         (region? (use-region-p))
>
> FYI: A typo in "case-insnsitive" (expected "case-insensitive")

Thanks for proofreading! Attaching the fixed version of the patch.

Best,
Ihor


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Auto-Upcase-downcase-begin-end-in-structure-template.patch --]
[-- Type: text/x-patch, Size: 4983 bytes --]

From b39925decd0e5ee5a0ce88b3fcea3a9647d35001 Mon Sep 17 00:00:00 2001
Message-Id: <b39925decd0e5ee5a0ce88b3fcea3a9647d35001.1648036699.git.yantar92@gmail.com>
From: Ihor Radchenko <yantar92@gmail.com>
Date: Sun, 20 Mar 2022 20:15:21 +0800
Subject: [PATCH] Auto-Upcase/downcase #+begin/#+end in structure templates

* lisp/org-tempo.el (org-tempo-add-block):
* lisp/org.el (org-insert-structure-template): When inserting
 #+begin_type/#+end_type, follow type's case.  TYPE will become
 #+BEGIN_TYPE and type will become #+bein_type.

(org-insert-structure-template): Make sure that we use
case-insensitive match even when user changes case-fold-search value.

(org-structure-template-alist): Clarify selection of #+BEGIN/END
vs. #+begin/end in the docstring.
---
 lisp/org-tempo.el | 13 ++++++++++---
 lisp/org.el       | 23 +++++++++++++++--------
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/lisp/org-tempo.el b/lisp/org-tempo.el
index b34007bf7..cd5ef9e8e 100644
--- a/lisp/org-tempo.el
+++ b/lisp/org-tempo.el
@@ -119,11 +119,18 @@ (defun org-tempo-add-block (entry)
   "Add block entry from `org-structure-template-alist'."
   (let* ((key (format "<%s" (car entry)))
 	 (name (cdr entry))
-	 (special (member name '("src" "export"))))
+	 (special (member name '("src" "export")))
+         (upcase? (string= (car (split-string name))
+                           (upcase (car (split-string name))))))
     (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
-			   `(,(format "#+begin_%s%s" name (if special " " ""))
+			   `(,(format "#+%s_%s%s"
+                                      (if upcase? "BEGIN" "begin")
+                                      name
+                                      (if special " " ""))
 			     ,(when special 'p) '> n ,(unless special 'p) n
-			     ,(format "#+end_%s" (car (split-string name " ")))
+			     ,(format "#+%s_%s"
+                                      (if upcase? "END" "end")
+                                      (car (split-string name " ")))
 			     >)
 			   key
 			   (format "Insert a %s block" name)
diff --git a/lisp/org.el b/lisp/org.el
index 9455c15c8..529146097 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -9490,9 +9490,11 @@ (defcustom org-structure-template-alist
     ("s" . "src")
     ("v" . "verse"))
   "An alist of keys and block types.
-`org-insert-structure-template' will display a menu with this
-list of templates to choose from.  The block type is inserted,
-with \"#+BEGIN_\" and \"#+END_\" added automatically.
+`org-insert-structure-template' will display a menu with this list of
+templates to choose from.  The block type is inserted, with
+\"#+begin_\" and \"#+end_\" added automatically.  If the block type
+consists of just uppercase letters, \"#+BEGIN_\" and \"#+END_\" are
+added instead.
 
 The menu keys are defined by the car of each entry in this alist.
 If two entries have the keys \"a\" and \"aa\" respectively, the
@@ -9624,18 +9626,23 @@ (defun org-insert-structure-template (type)
 Select a block from `org-structure-template-alist' then type
 either RET, TAB or SPC to write the block type.  With an active
 region, wrap the region in the block.  Otherwise, insert an empty
-block."
+block.
+
+When foo is written as FOO, upcase the #+BEGIN/END as well."
   (interactive
    (list (pcase (org--insert-structure-template-mks)
 	   (`("\t" . ,_) (read-string "Structure type: "))
 	   (`(,_ ,choice . ,_) choice))))
-  (let* ((region? (use-region-p))
+  (let* ((case-fold-search t) ; Make sure that matches are case-insensitive.
+         (region? (use-region-p))
 	 (region-start (and region? (region-beginning)))
 	 (region-end (and region? (copy-marker (region-end))))
 	 (extended? (string-match-p "\\`\\(src\\|export\\)\\'" type))
 	 (verbatim? (string-match-p
 		     (concat "\\`" (regexp-opt '("example" "export" "src")))
-		     type)))
+		     type))
+         (upcase? (string= (car (split-string type))
+                           (upcase (car (split-string type))))))
     (when region? (goto-char region-start))
     (let ((column (current-indentation)))
       (if (save-excursion (skip-chars-backward " \t") (bolp))
@@ -9643,7 +9650,7 @@ (defun org-insert-structure-template (type)
 	(insert "\n"))
       (save-excursion
 	(indent-to column)
-	(insert (format "#+begin_%s%s\n" type (if extended? " " "")))
+	(insert (format "#+%s_%s%s\n" (if upcase? "BEGIN" "begin") type (if extended? " " "")))
 	(when region?
 	  (when verbatim? (org-escape-code-in-region (point) region-end))
 	  (goto-char region-end)
@@ -9652,7 +9659,7 @@ (defun org-insert-structure-template (type)
 	  (end-of-line))
 	(unless (bolp) (insert "\n"))
 	(indent-to column)
-	(insert (format "#+end_%s" (car (split-string type))))
+	(insert (format "#+%s_%s" (if upcase? "END" "end") (car (split-string type))))
 	(if (looking-at "[ \t]*$") (replace-match "")
 	  (insert "\n"))
 	(when (and (eobp) (not (bolp))) (insert "\n")))
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Re: [BUG] Hard-coded begin/end in org-insert-structure-template [9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)]
  2022-03-23 11:59     ` Ihor Radchenko
@ 2022-03-25  6:34       ` Rudolf Adamkovič
  2022-06-18  6:28         ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Rudolf Adamkovič @ 2022-03-25  6:34 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@gmail.com> writes:

I patched a local checkout of the main branch, and I can confirm that
your works well here.  I also added [1] to my configuration file to
upcase all the structure templates automatically.  I hope we will have
this bundled with Org proper.  Fantastic work!

[1] Code that upcases the structure templates:

(setq org-structure-template-alist
      (mapcar
       (lambda (pair)
         (cons (car pair)
               (upcase (cdr pair))))
       org-structure-template-alist))

Rudy
-- 
"Strange as it may sound, the power of mathematics rests on its evasion
of all unnecessary thought and on its wonderful saving of mental
operations."
-- Ernst Mach, 1838-1916

Rudolf Adamkovic <salutis@me.com> [he/him]
Studenohorska 25
84103 Bratislava
Slovakia


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Re: [BUG] Hard-coded begin/end in org-insert-structure-template [9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)]
  2022-03-25  6:34       ` Rudolf Adamkovič
@ 2022-06-18  6:28         ` Ihor Radchenko
  0 siblings, 0 replies; 6+ messages in thread
From: Ihor Radchenko @ 2022-06-18  6:28 UTC (permalink / raw)
  To: Rudolf Adamkovič; +Cc: emacs-orgmode

Rudolf Adamkovič <salutis@me.com> writes:

> Ihor Radchenko <yantar92@gmail.com> writes:
>
> I patched a local checkout of the main branch, and I can confirm that
> your works well here.  I also added [1] to my configuration file to
> upcase all the structure templates automatically.  I hope we will have
> this bundled with Org proper.  Fantastic work!

Applied onto main via 9632401dc also adding NEWS entry.

Best,
Ihor


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-06-18  6:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-20 11:43 [BUG] Hard-coded begin/end in org-insert-structure-template [9.5.2 (release_9.5.2-24-g668205 @ /Users/salutis/src/emacs/nextstep/Emacs.app/Contents/Resources/lisp/org/)] Rudolf Adamkovič
2022-03-20 12:25 ` [PATCH] " Ihor Radchenko
2022-03-22 20:44   ` Rudolf Adamkovič
2022-03-23 11:59     ` Ihor Radchenko
2022-03-25  6:34       ` Rudolf Adamkovič
2022-06-18  6:28         ` Ihor Radchenko

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).