Skip to content

Commit 92c9f1a

Browse files
General update
1 parent 0494d16 commit 92c9f1a

File tree

1 file changed

+98
-183
lines changed

1 file changed

+98
-183
lines changed

comstrip.lisp

Lines changed: 98 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,101 @@
22
;; Author: Timothy C. Quinn
33
;; Home: https://github.com/JavaScriptDude/EmacsComStrip
44
;; Usage:
5-
;; cd <path containing comstrip.lisp>
6-
;; emacs --file=<path_to_source_code> --quick --batch --eval '(load "./comstrip.lisp")'
7-
;; # This tool will read in <source_code>, use emacs to strip comments out and the write to /tmp/<file_name>_comstrip
5+
;; emacs --file=<your_source_code> --quick --batch --eval '(load "<path_to_file>/comstrip.lisp")'
6+
;; eg emacs --file=~/_t/ffr.py --quick --batch --eval '(load "/dpool/vcmain/dev/lisp/comstrip/comstrip.lisp")'
7+
;; Options:
8+
;; --cs-suffix <suffix> - (opt) Add Suffix to file name before <timestamp>.<ext>
9+
;; # This tool will read in <source_code>, use emacs to strip comments out
10+
;; and write to /tmp/cs_<file_base>_[<suffix>_]<timestamp>.<ext>
811
;; TODO:
9-
;; [.] Validate args. If no file is passed in, exit
10-
;; [.] Cleanly exist on error conditions
11-
;; [.] Write a wrapper for this that will do futher stripping (eg. trailing whitespace)
12-
;; [.]
12+
;; [~] Write a python wrapper for this that will do futher stripping (eg. trailing whitespace) and automation
1313

1414
(defun main (args)
15-
(pc "main() called")
15+
;; (pc (format "main() called args = %s" args))
16+
17+
(setq _suffix nil)
18+
19+
;; Process cli args
20+
(while command-line-args-left
21+
(setq k (car command-line-args-left))
22+
(setq command-line-args-left (cdr command-line-args-left))
23+
(setq command-line-args (delete k command-line-args))
24+
(cond
25+
((string-equal k "--cs-suffix")
26+
(setq v (intern (car command-line-args-left)))
27+
(setq command-line-args-left (cdr command-line-args-left))
28+
(setq command-line-args (delete v command-line-args))
29+
(setq _suffix v)
30+
)
31+
)
32+
)
1633

17-
;; process args
18-
;; (pc (format "args: %s" args))
34+
;; Normalize CLI args
35+
(if (or (not _suffix) (string-equal _suffix ""))
36+
(setq _suffix "")
37+
(setq _suffix (format "_%s" _suffix)))
1938

2039
(setq in_file_full (buffer-file-name))
21-
;; (pc (format "in_file_full: %s" in_file_full))
40+
(unless in_file_full
41+
(error "No --file specified. Please put --file=<file> at beginning of arguments"))
2242

23-
(pc (format "Buffer size: %d" (point-max)))
43+
(setq in_filename (file-name-nondirectory in_file_full))
44+
(setq in_file_ext (file-name-extension in_filename))
45+
(setq in_file_base (file-name-sans-extension in_filename))
2446

25-
(setq in_dir (file-name-directory in_file_full))
26-
(setq in_file (file-name-nondirectory in_file_full))
27-
;; (setq in_file_ext (file-name-sans-extension in_file))
28-
;; (setq in_file_base (substring in_file 0 (- (length in_file) (length in_file_ext))))
29-
(pc (format "in_dir: %s, in_file: %s" in_dir in_file))
47+
(setq file_out_full (format "/tmp/cs_%s%s_%s.%s" in_file_base _suffix (format-time-string "%H%M%S%3N") in_file_ext))
3048

31-
(setq _file_out (format "/tmp/%s_comstrip" in_file))
32-
(pc (format "_file_out: %s" _file_out))
49+
;; (error (format "file_out_full: %s" file_out_full))
3350

51+
;; (setq suffix (if (and (= (length args) 2) (= (nth 0 args) "--suffix"))
52+
;; (nth 1 args)
53+
;; nil
54+
;; ))
3455

35-
;; Tooggle comments
36-
;; (hide/show-comments 'hide (point-min) (point-max))
37-
(hide/show-comments 'hide (point-min) (point-max))
56+
;; (pc (format "suffix: %s" (if suffix suffix "-")))
3857

39-
;; (pc "Comments toggled")
40-
41-
(scrape_comments (point-min) (point-max) _file_out)
4258

43-
;; (hide/show-comments-copy (point-min) (point-max))
59+
;; try:
60+
(condition-case err
61+
(let ()
62+
63+
64+
;; (pc (format "Buffer size: %d" (point-max)))
65+
66+
; Check buffer size
67+
(if (= (point-max) 1)
68+
(let ()
69+
(if (not (file-exists-p in_file_full))(error "File not found"))
70+
(error "Empty file or unreadable")
71+
)
72+
)
73+
74+
;; Toggle comments
75+
(hide/show-comments 'hide (point-min) (point-max))
76+
77+
;; Save code without comments to new file
78+
(scrape_comments (point-min) (point-max) file_out_full)
79+
)
80+
81+
;; catch:
82+
(error
83+
(setq msg (error-message-string err))
84+
(setq msg (s-replace "\"" "\\\"" msg))
85+
(setq msg (s-replace "\n" "\\\n" msg))
86+
(setq msg (s-replace "\r" "" msg))
87+
(setq msg (s-replace "\t" " " msg))
88+
(setq file_in (buffer-file-name))
89+
(if (not file_in) (setq file_in "-"))
90+
(error ".:CSMSG_S:.failed\t%s\t%s}.:CSMSG_E:." file_in msg)))
4491

45-
;; (pc "Program ended\n")
92+
;; Clean exit
93+
(pc (format ".:CSMSG_S:.ok\t%s\t%s.:CSMSG_E:." in_file_full file_out_full))
4694

4795
)
4896

4997

50-
(defun scrape_comments (beg end file_out)
51-
"Copy the entire buffer or selected."
98+
(defun scrape_comments (beg end file_out_full)
99+
"Save code without comments to output file."
52100
(interactive (list (point-min) (point-max)))
53101
(let ((result ""))
54102
(while (/= beg end)
@@ -59,160 +107,50 @@
59107
(setq result (concat result (buffer-substring beg next)))
60108
(setq beg next))
61109
)
62-
;; (kill-new result)
63110
;; (pc "=======================")
64111
;; (princ result #'external-debugging-output)
65112
;; (pc "=======================")
66-
;; (pc (format "suppress out to %s" file_out))
67-
68-
;; (pc (format "Output written to %s" file_out))
69113

70114
;; Write code to new file
71115
(generate-new-buffer "nocomm")
72116
(set-buffer "nocomm")
73117
(insert result)
74-
(set-visited-file-name file_out)
118+
(set-visited-file-name file_out_full)
75119
(save-buffer)
76-
(pc (format "See %s for results." file_out))
77120

121+
;; (pc (format "See %s for results." file_out_full))
78122
)
79123
)
80124

81125
;; eg (pc "foobar")
82126
;; (pc (format "foobar %s" "bazbar"))
127+
;; (pc (list "foo" (require 'newcomment nil t)))
83128
(defun pc (s)
84129
"Print to console"
85130
(princ (format "%s\n" s) #'external-debugging-output))
86131

132+
;; eg (pc "foobar")
133+
;; (pc (format "foobar %s" "bazbar"))
134+
(defun _exit (&optional s)
135+
"Print and exit"
136+
(if s
137+
;;then
138+
(error (format "_exit called - %s\n" s))
139+
::else
140+
(error "_exit called")
141+
)
142+
)
143+
144+
(defun s-replace (old new s)
145+
"Replaces OLD with NEW in S."
146+
(declare (pure t) (side-effect-free t))
147+
(replace-regexp-in-string (regexp-quote old) new s t t))
87148

88149

89150
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90-
;; Start hide-comnt - https://www.emacswiki.org/emacs/HideOrIgnoreComments
151+
;; CODE SNIP from hide-comnt - https://www.emacswiki.org/emacs/HideOrIgnoreComments
91152
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92-
;;; hide-comnt.el --- Hide/show comments in code.
93-
;;
94-
;; Filename: hide-comnt.el
95-
;; Description: Hide/show comments in code.
96-
;; Author: Drew Adams
97-
;; Maintainer: Drew Adams
98-
;; Copyright (C) 2011-2012, Drew Adams, all rights reserved.
99-
;; Created: Wed May 11 07:11:30 2011 (-0700)
100-
;; Version:
101-
;; Last-Updated: Thu Aug 23 13:37:49 2012 (-0700)
102-
;; By: dradams
103-
;; Update #: 40
104-
;; URL: http://www.emacswiki.org/cgi-bin/wiki/hide-comnt.el
105-
;; Doc URL: http://www.emacswiki.org/emacs/HideOrIgnoreComments
106-
;; Keywords: comment, hide, show
107-
;; Compatibility: GNU Emacs: 21.x, 22.x, 23.x
108-
;;
109-
;; Features that might be required by this library:
110-
;;
111-
;; None
112-
;;
113-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114-
;;
115-
;;; Commentary:
116-
;;
117-
;; Hide/show comments in code.
118-
;;
119-
;;
120-
;; Macros defined here:
121-
;;
122-
;; `with-comments-hidden'.
123-
;;
124-
;; Commands defined here:
125-
;;
126-
;; `hide/show-comments', `hide/show-comments-toggle'.
127-
;;
128-
;; User options defined here:
129-
;;
130-
;; `ignore-comments-flag'.
131-
;;
132-
;;
133-
;; Put this in your init file (`~/.emacs'):
134-
;;
135-
;; (require 'hide-comnt)
136-
;;
137-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138-
;;
139-
;;; Change Log:
140-
;;
141-
;; 2012/05/10 dadams
142-
;; Added: hide/show-comments-toggle. Thx to Denny Zhang for the suggestion.
143-
;; 2011/11/23 dadams
144-
;; hide/show-comments: Bug fix - ensure CEND is not past eob.
145-
;; 2011/05/11 dadams
146-
;; Created: moved code here from thing-cmds.el.
147-
;;
148-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149-
;;
150-
;; This program is free software; you can redistribute it and/or
151-
;; modify it under the terms of the GNU General Public License as
152-
;; published by the Free Software Foundation; either version 3, or
153-
;; (at your option) any later version.
154-
;;
155-
;; This program is distributed in the hope that it will be useful,
156-
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
157-
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
158-
;; General Public License for more details.
159-
;;
160-
;; You should have received a copy of the GNU General Public License
161-
;; along with this program; see the file COPYING. If not, write to
162-
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
163-
;; Floor, Boston, MA 02110-1301, USA.
164-
;;
165-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166-
;;
167-
;;; Code:
168-
169-
170-
;;;###autoload
171-
(defcustom ignore-comments-flag t
172-
"Non-nil means macro `with-comments-hidden' hides comments."
173-
:type 'boolean :group 'matching)
174-
175-
176-
(defmacro with-comments-hidden (start end &rest body)
177-
"Evaluate the forms in BODY while comments are hidden from START to END.
178-
But if `ignore-comments-flag' is nil, just evaluate BODY,
179-
without hiding comments. Show comments again when BODY is finished.
180-
181-
See `hide/show-comments', which is used to hide and show the comments.
182-
Note that prior to Emacs 21, this never hides comments."
183-
(let ((result (make-symbol "result"))
184-
(ostart (make-symbol "ostart"))
185-
(oend (make-symbol "oend")))
186-
`(let ((,ostart ,start)
187-
(,oend ,end)
188-
,result)
189-
(unwind-protect
190-
(setq ,result (progn (when ignore-comments-flag
191-
(hide/show-comments 'hide ,ostart ,oend))
192-
,@body))
193-
(when ignore-comments-flag (hide/show-comments 'show ,ostart ,oend))
194-
,result))))
195-
196-
;;;###autoload
197153
(defun hide/show-comments (&optional hide/show start end)
198-
"Hide or show comments from START to END.
199-
Interactively, hide comments, or show them if you use a prefix arg.
200-
Interactively, START and END default to the region limits, if active.
201-
Otherwise, including non-interactively, they default to `point-min'
202-
and `point-max'.
203-
204-
Uses `save-excursion', restoring point.
205-
206-
Be aware that using this command to show invisible text shows *all*
207-
such text, regardless of how it was hidden. IOW, it does not just
208-
show invisible text that you previously hid using this command.
209-
210-
From Lisp, a HIDE/SHOW value of `hide' hides comments. Other values
211-
show them.
212-
213-
This function does nothing in Emacs versions prior to Emacs 21,
214-
because it needs `comment-search-forward'."
215-
216154
(interactive
217155
(cons (if current-prefix-arg 'show 'hide)
218156
(if (or (not mark-active) (null (mark)) (= (point) (mark)))
@@ -239,31 +177,8 @@ because it needs `comment-search-forward'."
239177
(put-text-property cbeg cend 'invisible t)
240178
(put-text-property cbeg cend 'invisible nil)))))
241179
(set-buffer-modified-p bufmodp)))))
242-
243-
(defun hide/show-comments-toggle (&optional start end)
244-
"Toggle hiding/showing of comments in the active region or whole buffer.
245-
If the region is active then toggle in the region. Otherwise, in the
246-
whole buffer.
247-
248-
Interactively, START and END default to the region limits, if active.
249-
Otherwise, including non-interactively, they default to `point-min'
250-
and `point-max'.
251-
252-
See `hide/show-comments' for more information."
253-
(interactive (if (or (not mark-active) (null (mark)) (= (point) (mark)))
254-
(list (point-min) (point-max))
255-
(if (< (point) (mark)) (list (point) (mark)) (list (mark) (point)))))
256-
(if (save-excursion (goto-char start) (and (comment-search-forward end 'NOERROR)
257-
(get-text-property (point) 'invisible)))
258-
(hide/show-comments 'show start end)
259-
(hide/show-comments 'hide start end)))
260-
261-
;;;;;;;;;;;;;;;;;;;;;;;;
262-
;; commenting out provides as its redundant here
263-
;; (provide 'hide-comnt)
264-
265180
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266-
;;; hide-comnt.el ends here
181+
;;; END CODE SNIP from hide-comnt
267182
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268183

269184

0 commit comments

Comments
 (0)