emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Have SRC_BLOCK :padline accept numbers
@ 2017-03-28 19:47 Daniel P Gomez
  2017-03-29 12:27 ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel P Gomez @ 2017-03-28 19:47 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 703 bytes --]

A use case is tangling python source code snippets containing class
definitions.

(
http://emacs.stackexchange.com/questions/31738/org-mode-babel-ensure-two-empty-lines-between-tangled-code-blocks-for-python
)

Per PEP8, the use of blank lines stipulates:
"Surround top-level function and class definitions with two blank lines."

To achieve two blank lines before a class definition, one would have to set
:padline 2.

I've written a small patch (attached here), following the contribution
guidelines on the org-mode website. The "patched" repository can be found
here: https://github.com/dangom/org-mode/tree/feature_padline

Please let me know if there is anything else I should do.

Thanks,

Daniel

[-- Attachment #1.2: Type: text/html, Size: 1077 bytes --]

[-- Attachment #2: 0001-Add-support-for-padline-with-numbers-in-ob-tangle.patch --]
[-- Type: application/octet-stream, Size: 1300 bytes --]

From f5e67856b6cefb7c5e9c1b6bd74321d3b47f1b05 Mon Sep 17 00:00:00 2001
From: Daniel Gomez <d.gomez@posteo.org>
Date: Tue, 28 Mar 2017 21:20:23 +0200
Subject: [PATCH] Add support for :padline with numbers in ob-tangle.

---
 lisp/ob-tangle.el | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index b33fcaee4..85ea18a57 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -284,9 +284,16 @@ used to limit the exported source code blocks by language."
 			      (insert-file-contents file-name))
 			    (goto-char (point-max))
 			    ;; Handle :padlines unless first line in file
-			    (unless (or (string= "no" (cdr (assq :padline (nth 4 spec))))
-					(= (point) (point-min)))
-			      (insert "\n"))
+			    (let ((padlines (format "%s" (cdr (assq :padline (nth 4 spec))))))
+			      (cond
+			       ((and (string= "nil" padlines) (not (= (point) (point-min))))
+				(insert "\n"))
+			       ((string= "no" padlines)
+				nil)
+			       ((numberp (string-to-int padlines))
+				(dotimes (i (string-to-int padlines)) (insert "\n")))
+			       (t
+				(insert "\n"))))
 			    (insert content)
 			    (write-region nil nil file-name))))
 		      ;; if files contain she-bangs, then make the executable
-- 
2.12.2


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

* Re: Have SRC_BLOCK :padline accept numbers
  2017-03-28 19:47 Have SRC_BLOCK :padline accept numbers Daniel P Gomez
@ 2017-03-29 12:27 ` Nicolas Goaziou
  2017-03-29 16:03   ` Daniel P Gomez
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2017-03-29 12:27 UTC (permalink / raw)
  To: Daniel P Gomez; +Cc: emacs-orgmode

Hello,

Daniel P Gomez <gomez.danp@gmail.com> writes:

> I've written a small patch (attached here), following the contribution
> guidelines on the org-mode website. The "patched" repository can be found
> here: https://github.com/dangom/org-mode/tree/feature_padline

Thank you.

> Please let me know if there is anything else I should do.

Some comments follow.

> From f5e67856b6cefb7c5e9c1b6bd74321d3b47f1b05 Mon Sep 17 00:00:00 2001
> From: Daniel Gomez <d.gomez@posteo.org>
> Date: Tue, 28 Mar 2017 21:20:23 +0200
> Subject: [PATCH] Add support for :padline with numbers in ob-tangle.
>
You need to add list modified functions here, with the actual
modifiaction, e.g.,

* lisp/ob-tangle.el (the-function-I-modified): Been there, done that.

Also, if you haven't signed FSF papers yet, you need to add "TINYCHANGE"
at the end of the commit message.

> -			    (unless (or (string= "no" (cdr (assq :padline (nth 4 spec))))
> -					(= (point) (point-min)))
> -			      (insert "\n"))
> +			    (let ((padlines (format "%s" (cdr (assq :padline (nth 4 spec))))))
> +			      (cond
> +			       ((and (string= "nil" padlines) (not (= (point) (point-min))))

(not (= (point) (point-min))) -> (not (bobp))

> +				(insert "\n"))
> +			       ((string= "no" padlines)
> +				nil)
> +			       ((numberp (string-to-int padlines))

(numberp (string-to-int padlines)) -> (string-match-p "\\`[0-9]+\\'" padlines)

> +				(dotimes (i (string-to-int padlines)) (insert "\n")))

string-to-int -> string-to-number


Regards,

-- 
Nicolas Goaziou

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

* Re: Have SRC_BLOCK :padline accept numbers
  2017-03-29 12:27 ` Nicolas Goaziou
@ 2017-03-29 16:03   ` Daniel P Gomez
  2017-03-30  7:50     ` Nicolas Goaziou
  2017-04-18  8:37     ` Nicolas Goaziou
  0 siblings, 2 replies; 5+ messages in thread
From: Daniel P Gomez @ 2017-03-29 16:03 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

Dear Nicolas,

Thanks for the constructive feedback.

I've amended the commit including the changes you've mentioned.
I did not change the following, though:

(numberp (string-to-int padlines)) -> (string-match-p "\\`[0-9]+\\'" padlines)

because that changed the behaviour of org-babel-tangle. Having a
string-match-p as a
match condition broke the default of adding no padlines in the
first tangled SRC block (when passed with no arguments).

I also noticed that adding this functionality to ob-tangle breaks eldoc
for me. I guess all org babel header arguments have to be strings?

Debugger entered--Lisp error: (wrong-type-argument stringp 2)
((:padline . 2))
((:results . "replace output") (:exports . "both") (:padline . 2)
(:eval . "never-export") (:tangle . "no") (:hlines . "no") (:noweb .
"no") (:cache . "no") (:session . "none")) " ")
  org-eldoc-get-src-header()
  org-eldoc-documentation-function()
  eldoc-print-current-symbol-info()

GNU Emacs 25.2.1 (x86_64-apple-darwin16.4.0, NS appkit-1504.81 Version
10.12.3 (Build 16D32))
 of 2017-03-22
Copyright (C) 2017 Free Software Foundation, Inc.

Yours,

Daniel

Nicolas Goaziou writes:

> Hello,
>
> Daniel P Gomez <gomez.danp@gmail.com> writes:
>
>> I've written a small patch (attached here), following the contribution
>> guidelines on the org-mode website. The "patched" repository can be found
>> here: https://github.com/dangom/org-mode/tree/feature_padline
>
> Thank you.
>
>> Please let me know if there is anything else I should do.
>
> Some comments follow.
>
>> From f5e67856b6cefb7c5e9c1b6bd74321d3b47f1b05 Mon Sep 17 00:00:00 2001
>> From: Daniel Gomez <d.gomez@posteo.org>
>> Date: Tue, 28 Mar 2017 21:20:23 +0200
>> Subject: [PATCH] Add support for :padline with numbers in ob-tangle.
>>
> You need to add list modified functions here, with the actual
> modifiaction, e.g.,
>
> * lisp/ob-tangle.el (the-function-I-modified): Been there, done that.
>
> Also, if you haven't signed FSF papers yet, you need to add "TINYCHANGE"
> at the end of the commit message.
>
>> -    (unless (or (string= "no" (cdr (assq :padline (nth 4 spec))))
>> - (= (point) (point-min)))
>> -      (insert "\n"))
>> +    (let ((padlines (format "%s" (cdr (assq :padline (nth 4 spec))))))
>> +      (cond
>> +       ((and (string= "nil" padlines) (not (= (point) (point-min))))
>
> (not (= (point) (point-min))) -> (not (bobp))
>
>> + (insert "\n"))
>> +       ((string= "no" padlines)
>> + nil)
>> +       ((numberp (string-to-int padlines))
>
> (numberp (string-to-int padlines)) -> (string-match-p "\\`[0-9]+\\'" padlines)
>
>> + (dotimes (i (string-to-int padlines)) (insert "\n")))
>
> string-to-int -> string-to-number
>
>
> Regards,


-- 
Daniel P. Gomez

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

* Re: Have SRC_BLOCK :padline accept numbers
  2017-03-29 16:03   ` Daniel P Gomez
@ 2017-03-30  7:50     ` Nicolas Goaziou
  2017-04-18  8:37     ` Nicolas Goaziou
  1 sibling, 0 replies; 5+ messages in thread
From: Nicolas Goaziou @ 2017-03-30  7:50 UTC (permalink / raw)
  To: Daniel P Gomez; +Cc: emacs-orgmode

Hello,

Daniel P Gomez <gomez.danp@gmail.com> writes:

> I've amended the commit including the changes you've mentioned.

Thank you.

> I did not change the following, though:
>
> (numberp (string-to-int padlines)) -> (string-match-p "\\`[0-9]+\\'" padlines)
>
> because that changed the behaviour of org-babel-tangle. Having a
> string-match-p as a
> match condition broke the default of adding no padlines in the
> first tangled SRC block (when passed with no arguments).

I don't understand this. `string-to-int' and `string-match-p' both have
the same requirements about parameters type, don't they?

> I also noticed that adding this functionality to ob-tangle breaks eldoc
> for me. I guess all org babel header arguments have to be strings?

Probably.


Regards,

-- 
Nicolas Goaziou

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

* Re: Have SRC_BLOCK :padline accept numbers
  2017-03-29 16:03   ` Daniel P Gomez
  2017-03-30  7:50     ` Nicolas Goaziou
@ 2017-04-18  8:37     ` Nicolas Goaziou
  1 sibling, 0 replies; 5+ messages in thread
From: Nicolas Goaziou @ 2017-04-18  8:37 UTC (permalink / raw)
  To: Daniel P Gomez; +Cc: emacs-orgmode

Hello,

Daniel P Gomez <gomez.danp@gmail.com> writes:

> I've amended the commit including the changes you've mentioned.
> I did not change the following, though:
>
> (numberp (string-to-int padlines)) -> (string-match-p "\\`[0-9]+\\'" padlines)
>
> because that changed the behaviour of org-babel-tangle. Having a
> string-match-p as a
> match condition broke the default of adding no padlines in the
> first tangled SRC block (when passed with no arguments).

`string-to-int' will fail the same way as `string-match-p' if `padlines'
is nil. So I do not see how the behaviour could differ. Could you
elaborate on the issue?

> I also noticed that adding this functionality to ob-tangle breaks eldoc
> for me. I guess all org babel header arguments have to be strings?

I didn't check, but that seems a reasonable assumption.

BTW, you didn't include the patch, did you?

Regards,

-- 
Nicolas Goaziou                                                0x80A93738

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

end of thread, other threads:[~2017-04-18  9:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-28 19:47 Have SRC_BLOCK :padline accept numbers Daniel P Gomez
2017-03-29 12:27 ` Nicolas Goaziou
2017-03-29 16:03   ` Daniel P Gomez
2017-03-30  7:50     ` Nicolas Goaziou
2017-04-18  8:37     ` Nicolas Goaziou

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).