emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: pierre.techoueyres@free.fr (Pierre Téchoueyres)
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: org-mode <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Add new keyword :coding for #+include directive
Date: Wed, 18 Apr 2018 20:40:32 +0200	[thread overview]
Message-ID: <87muy0b4kw.fsf@killashandra.ballybran.fr> (raw)
In-Reply-To: <874lkakz58.fsf@nicolasgoaziou.fr> (Nicolas Goaziou's message of "Tue, 17 Apr 2018 10:36:35 +0200")

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

Hello,

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> Hello,
> ...
>> This allow you to specify something like
>>
>> #+begin_example
>> ,#+INCLUDE: "myfile.cmd" src cmd :coding "cp850-dos"
>> #+end_example
>
> The quotes are not necessary. AFAICT, coding systems do not contain
> spaces.
Ok, I've reworked the rexexp to suppress them. New patch attached


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: include-with-coding --]
[-- Type: text/x-patch, Size: 6525 bytes --]

> From fcf191842bea64442d69a0fcfa927a046d8fbd92 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pierre=20T=C3=A9choueyres?= <pierre.techoueyres@free.fr>
Date: Mon, 16 Apr 2018 21:20:17 +0200
Subject: [PATCH] Add new keyword :coding for #+include directive

* lisp/ox.el (org-export-expand-include-keyword): Add new keyword
  `:coding' for specify the file encoding whith the `#+include:'
  directive.

This allow to use somting like :
when your org-file is encoded in utf-8.
---
 etc/ORG-NEWS |  10 ++++-
 lisp/ox.el   | 117 +++++++++++++++++++++++++++++++----------------------------
 2 files changed, 71 insertions(+), 56 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 013c7b139..f285b7337 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -108,6 +108,14 @@ You can use =ob-scala.el= as packaged in scala-mode, available from the
 MELPA repository.
 
 ** New features
+*** New keyword for ~#+include:~ directive
+Add ~:coding "codign-system"~ keyword to allow include of files from
+different codign system than the main org-file.
+For example:
+#+begin_example
+,#+INCLUDE: "myfile.cmd" src cmd :coding cp850-dos
+#+end_example
+
 *** iCalendar export uses inheritance for TIMEZONE and LOCATION properties
 Both these properties can be inherited during iCalendar export,
 depending on the value of ~org-use-property-inheritance~.
@@ -514,7 +522,7 @@ want to take over maintenance of this compatibility, please contact
 our mailing list.
 
 *** New syntax for export blocks
-
+    
 Export blocks are explicitly marked as such at the syntax level to
 disambiguate their parsing from special blocks.  The new syntax is
 
diff --git a/lisp/ox.el b/lisp/ox.el
index ea7d1dc81..f4c5660ff 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -3326,6 +3326,12 @@ storing and resolving footnotes.  It is created automatically."
 			  value)
 			 (prog1 (match-string 1 value)
 			   (setq value (replace-match "" nil nil value)))))
+		   (coding
+		    (intern (or (and (string-match
+				      ":coding +\\<\\([a-z0-9\\-]+\\)\\>" value)
+				     (prog1 (match-string 1 value)
+				       (setq value (replace-match "" nil nil value))))
+				(symbol-name buffer-file-coding-system))))
 		   (env (cond
 			 ((string-match "\\<example\\>" value) 'literal)
 			 ((string-match "\\<export\\(?: +\\(.*\\)\\)?" value)
@@ -3348,63 +3354,64 @@ storing and resolving footnotes.  It is created automatically."
 			       (match-string 1 value))))
 	      ;; Remove keyword.
 	      (delete-region (point) (line-beginning-position 2))
-	      (cond
-	       ((not file) nil)
-	       ((not (file-readable-p file))
-		(error "Cannot include file %s" file))
-	       ;; Check if files has already been parsed.  Look after
-	       ;; inclusion lines too, as different parts of the same
-	       ;; file can be included too.
-	       ((member (list file lines) included)
-		(error "Recursive file inclusion: %s" file))
-	       (t
+	      (let ((coding-system-for-read coding))
 		(cond
-		 ((eq env 'literal)
-		  (insert
-		   (let ((ind-str (make-string ind ?\s))
-			 (arg-str (if (stringp args) (format " %s" args) ""))
-			 (contents
-			  (org-escape-code-in-string
-			   (org-export--prepare-file-contents file lines))))
-		     (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
-			     ind-str block arg-str contents ind-str block))))
-		 ((stringp block)
-		  (insert
-		   (let ((ind-str (make-string ind ?\s))
-			 (contents
-			  (org-export--prepare-file-contents file lines)))
-		     (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
-			     ind-str block contents ind-str block))))
+		 ((not file) nil)
+		 ((not (file-readable-p file))
+		  (error "Cannot include file %s" file))
+		 ;; Check if files has already been parsed.  Look after
+		 ;; inclusion lines too, as different parts of the same
+		 ;; file can be included too.
+		 ((member (list file lines) included)
+		  (error "Recursive file inclusion: %s" file))
 		 (t
-		  (insert
-		   (with-temp-buffer
-		     (let ((org-inhibit-startup t)
-			   (lines
-			    (if location
-				(org-export--inclusion-absolute-lines
-				 file location only-contents lines)
-			      lines)))
-		       (org-mode)
-		       (insert
-			(org-export--prepare-file-contents
-			 file lines ind minlevel
-			 (or
-			  (gethash file file-prefix)
-			  (puthash file (cl-incf current-prefix) file-prefix))
-			 footnotes)))
-		     (org-export-expand-include-keyword
-		      (cons (list file lines) included)
-		      (file-name-directory file)
-		      footnotes)
-		     (buffer-string)))))
-		;; Expand footnotes after all files have been
-		;; included.  Footnotes are stored at end of buffer.
-		(unless included
-		  (org-with-wide-buffer
-		   (goto-char (point-max))
-		   (maphash (lambda (k v)
-			      (insert (format "\n[fn:%s] %s\n" k v)))
-			    footnotes))))))))))))
+		  (cond
+		   ((eq env 'literal)
+		    (insert
+		     (let ((ind-str (make-string ind ?\s))
+			   (arg-str (if (stringp args) (format " %s" args) ""))
+			   (contents
+			    (org-escape-code-in-string
+			     (org-export--prepare-file-contents file lines))))
+		       (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
+			       ind-str block arg-str contents ind-str block))))
+		   ((stringp block)
+		    (insert
+		     (let ((ind-str (make-string ind ?\s))
+			   (contents
+			    (org-export--prepare-file-contents file lines)))
+		       (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
+			       ind-str block contents ind-str block))))
+		   (t
+		    (insert
+		     (with-temp-buffer
+		       (let ((org-inhibit-startup t)
+			     (lines
+			      (if location
+				  (org-export--inclusion-absolute-lines
+				   file location only-contents lines)
+				lines)))
+			 (org-mode)
+			 (insert
+			  (org-export--prepare-file-contents
+			   file lines ind minlevel
+			   (or
+			    (gethash file file-prefix)
+			    (puthash file (cl-incf current-prefix) file-prefix))
+			   footnotes)))
+		       (org-export-expand-include-keyword
+			(cons (list file lines) included)
+			(file-name-directory file)
+			footnotes)
+		       (buffer-string)))))
+		  ;; Expand footnotes after all files have been
+		  ;; included.  Footnotes are stored at end of buffer.
+		  (unless included
+		    (org-with-wide-buffer
+		     (goto-char (point-max))
+		     (maphash (lambda (k v)
+				(insert (format "\n[fn:%s] %s\n" k v)))
+			      footnotes)))))))))))))
 
 (defun org-export--inclusion-absolute-lines (file location only-contents lines)
   "Resolve absolute lines for an included file with file-link.
-- 
2.14.3


[-- Attachment #3: Type: text/plain, Size: 411 bytes --]



> ...
> Is it really an Org problem? E.g., couldn't you put a coding: cookie in
> your ".cmd" file? IMO, the coding system depends on the includee, not
> the includer.

I tend to aggree with you that TRTDT is to put cookies or something
inside the included file. But :
  a) This seem to not work as expected (see exemples joinned).
  b) Sometimes you can't modify the included file (ex: remote file access).


[-- Attachment #4: test.org --]
[-- Type: text/x-org, Size: 374 bytes --]

# -*- coding: utf-8 -*-
#+title: test with different encodings.

* Pierre Téchoueyres
  bad
  #+include: "./file.1.txt" src bat
  or
  #+include: "./file.2.txt" src sh
 

  and good ? 
  #+include: "./file.1.txt" src bat :coding cp850-dos
  or
  #+include: "./file.2.txt" src sh :coding iso-8859-15-unix
 

# Local Variables:
# coding: utf-8-unix
# End:

[-- Attachment #5: file.1.txt --]
[-- Type: text/plain, Size: 133 bytes --]

::- -*- coding: cp850-dos -*-
@echo off

echo Hello Pierre Téchoueyres

rem Local Variables:
rem coding: cp850-dos
rem End:

[-- Attachment #6: file.2.txt --]
[-- Type: text/plain, Size: 140 bytes --]

#!/bin/sh # -*- coding: iso-8859-15-unix -*-

echo "Hello Pierre Téchoueyres"

# Local Variables:
# coding: iso-8859-15-unix
# End:

[-- Attachment #7: Type: text/plain, Size: 101 bytes --]


For a I can try to jump into the rabbit hole and find a solution (but
 for now I'm lost)


Regards,

  reply	other threads:[~2018-04-18 21:13 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-16 19:52 [PATCH] Add new keyword :coding for #+include directive Pierre Téchoueyres
2018-04-16 21:30 ` Pierre Téchoueyres
2018-04-17  8:36   ` Nicolas Goaziou
2018-04-18 18:40     ` Pierre Téchoueyres [this message]
2018-04-20 23:08       ` Pierre Téchoueyres
2018-04-23 10:27         ` Nicolas Goaziou
2018-04-23 19:44           ` Pierre Téchoueyres
2018-04-24 21:59             ` Nicolas Goaziou
2018-04-24 22:57               ` Pierre Téchoueyres
2018-05-04 22:41                 ` Pierre Téchoueyres
2018-05-08 17:31                   ` Nicolas Goaziou
2018-05-14 23:44                     ` Pierre Téchoueyres
2018-05-19 13:13                       ` Nicolas Goaziou
2018-05-19 16:09                         ` Pierre Téchoueyres
2018-05-20  7:33                           ` Nicolas Goaziou
2018-05-24 21:29                             ` Pierre Téchoueyres
2018-06-02 10:13                               ` Nicolas Goaziou
2018-06-08 19:21                                 ` Pierre Téchoueyres
2018-06-13 13:39                                   ` Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87muy0b4kw.fsf@killashandra.ballybran.fr \
    --to=pierre.techoueyres@free.fr \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).