Skip to content

Commit 94f642d

Browse files
Create comstrip.lisp
1 parent afb24e3 commit 94f642d

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed

comstrip.lisp

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
;; comstrip.lisp
2+
;; Author: Timothy C. Quinn
3+
;; Home: https://github.com/JavaScriptDude/EmacsComStrip
4+
;; 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
8+
;; 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+
;; [.]
13+
14+
(defun main (args)
15+
(pc "main() called")
16+
17+
;; process args
18+
;; (pc (format "args: %s" args))
19+
20+
(setq in_file_full (buffer-file-name))
21+
;; (pc (format "in_file_full: %s" in_file_full))
22+
23+
(pc (format "Buffer size: %d" (point-max)))
24+
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))
30+
31+
(setq _file_out (format "/tmp/%s_comstrip" in_file))
32+
(pc (format "_file_out: %s" _file_out))
33+
34+
35+
;; Tooggle comments
36+
;; (hide/show-comments 'hide (point-min) (point-max))
37+
(hide/show-comments 'hide (point-min) (point-max))
38+
39+
;; (pc "Comments toggled")
40+
41+
(scrape_comments (point-min) (point-max) _file_out)
42+
43+
;; (hide/show-comments-copy (point-min) (point-max))
44+
45+
;; (pc "Program ended\n")
46+
47+
)
48+
49+
50+
(defun scrape_comments (beg end file_out)
51+
"Copy the entire buffer or selected."
52+
(interactive (list (point-min) (point-max)))
53+
(let ((result ""))
54+
(while (/= beg end)
55+
(when (get-char-property beg 'invisible)
56+
(setq beg (next-single-char-property-change beg 'invisible nil end)))
57+
58+
(let ((next (next-single-char-property-change beg 'invisible nil end)))
59+
(setq result (concat result (buffer-substring beg next)))
60+
(setq beg next))
61+
)
62+
;; (kill-new result)
63+
;; (pc "=======================")
64+
;; (princ result #'external-debugging-output)
65+
;; (pc "=======================")
66+
;; (pc (format "suppress out to %s" file_out))
67+
68+
;; (pc (format "Output written to %s" file_out))
69+
70+
;; Write code to new file
71+
(generate-new-buffer "nocomm")
72+
(set-buffer "nocomm")
73+
(insert result)
74+
(set-visited-file-name file_out)
75+
(save-buffer)
76+
(pc (format "See %s for results." file_out))
77+
78+
)
79+
)
80+
81+
;; eg (pc "foobar")
82+
;; (pc (format "foobar %s" "bazbar"))
83+
(defun pc (s)
84+
"Print to console"
85+
(princ (format "%s\n" s) #'external-debugging-output))
86+
87+
88+
89+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90+
;; Start hide-comnt - https://www.emacswiki.org/emacs/HideOrIgnoreComments
91+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
197+
(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+
216+
(interactive
217+
(cons (if current-prefix-arg 'show 'hide)
218+
(if (or (not mark-active) (null (mark)) (= (point) (mark)))
219+
(list (point-min) (point-max))
220+
(if (< (point) (mark)) (list (point) (mark)) (list (mark) (point))))))
221+
222+
(when (require 'newcomment nil t) ; `comment-search-forward', Emacs 21+.
223+
(unless start (setq start (point-min)))
224+
(unless end (setq end (point-max)))
225+
(unless (<= start end) (setq start (prog1 end (setq end start))))
226+
227+
(let ((bufmodp (buffer-modified-p))
228+
(buffer-read-only nil)
229+
cbeg cend)
230+
(unwind-protect
231+
(save-excursion
232+
(goto-char start)
233+
(while (and (< start end) (setq cbeg (comment-search-forward end 'NOERROR)))
234+
(setq cend (if (string= "" comment-end)
235+
(min (1+ (line-end-position)) (point-max))
236+
(search-forward comment-end end 'NOERROR)))
237+
(when (and cbeg cend)
238+
(if (eq 'hide hide/show)
239+
(put-text-property cbeg cend 'invisible t)
240+
(put-text-property cbeg cend 'invisible nil)))))
241+
(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+
265+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266+
;;; hide-comnt.el ends here
267+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268+
269+
270+
(main command-line-args-left)

0 commit comments

Comments
 (0)