emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] lisp: scoping vars in repetitive defuns


From: Nick Dokos
Subject: Re: [O] lisp: scoping vars in repetitive defuns
Date: Tue, 17 Sep 2019 09:42:30 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Matt Price <address@hidden> writes:

> I have a number of convenience functions define to help me with grading 
> assignments. As I go through the semester, i update all of these functions 
> modestly so that they'rehelpful for grading the current
> assignment. 
>
> I big chunk of these simple functions is taken up just declaring variables 
> with (let (())) forms.  Each function uses some ofhte same variables, e.g:
>
> (defun dh-find-files ()
>   (interactive)
>   (let* ((base (org-entry-get (point) "ORG_LMS_ASSIGNMENT_DIRECTORY"))
>          (gh (org-entry-get (point) "GITHUB"))
>         (f2o `( ,(concat "Reflection/" gh ".md") ,(concat "students/" gh 
> ".json")))) ;;;; "01/index.html" "02/index.html" "03/style.css" "04/style.css"
>     (message "%s" f2o)
>     ;; make more flexible for resubmits
>     (shell-command (concat "cd " base " && git checkout " gh "-master"))
>     (dolist (x f2o)
>       (if (file-exists-p (concat base "/" x))
>           (find-file-other-window (concat base "/" x) )
>         (message "File %s does not exist, not opening." x)))))
>
> (defun dh-tests ()
>   (interactive)
>   (let* ((base (org-entry-get (point) "ORG_LMS_ASSIGNMENT_DIRECTORY" ))
>          (gh (org-entry-get (point) "GITHUB")))
>     (with-temp-buffer (shell-command (concat "cd " base " && npm test") t)) 
> ;; the "t" lets us suppress buffer
>     (browse-url-of-file (concat base "/TestResults/testresults.html"))
>     ;; (dh-mocha-run)
>    
>     ))
>
> ----------
>
> This semester I changed some elements of my workflow and I had to update all 
> the (org-entry-get) calls to new values.  It makes me think the code is less 
> maintainable than it could be.  I would like to do
> something like this:
>
> (lexical-let ((base `(org-entry-get (point) "ORG_LMS_ASSIGNMENT_DIRECTORY")
>                    (gh `(org-entry-get (point) "GITHUB")) )
>     (defun dh-find-files ()
> (with-temp-buffer (shell-command (concat "cd " base " && npm test") t)) ;; 
> the "t" lets us suppress buffer
>     (browse-url-of-file (concat base "/TestResults/testresults.html")))))
>
> Obviously it doesn't work this way. But is there any way to set macros like 
> this to be expanded later inside a function definition? I feel certain there 
> must be...

Are you overthinking this perhaps? Wouldn't variables work? Like e.g.:

(defvar org-lms-assignment-dir-prop "ORG_LMS_ASSIGNMENT_DIRECTORY")

(defun dh-find-files()
  ...
  (let ((base (org-entry-get (point) org-lms-assignment-dir)
      ...))))

and then you only have one place to change it?

It's quite possible of course that I'm misunderstanding what you are looking 
for.

>
> Thanks,
>
> Matt
>

-- 
Nick

"There are only two hard problems in computer science: cache
invalidation, naming things, and off-by-one errors." -Martin Fowler




reply via email to

[Prev in Thread] Current Thread [Next in Thread]