Skip to content

Commit 344b237

Browse files
committed
Merge pull request #2 from thomasf/fixes
Fixes and improvements
2 parents 73a6b64 + 5a8c2d8 commit 344b237

File tree

1 file changed

+125
-32
lines changed

1 file changed

+125
-32
lines changed

code-library.el

Lines changed: 125 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,139 @@
3838
:prefix "code-library-")
3939

4040
(defcustom code-library-mode-file-alist '((c++-mode . "cpp.org")
41-
(emacs-lisp-mode . "elisp.org")
42-
(python-mode . "python.org")
43-
(perl-mode . "perl.org")
44-
(dos-mode . "bat.org")
45-
(sh-mode . "bash.org"))
46-
"Mapping the correspondence between major-mode and the snippet file"
41+
(dos-mode . "bat.org")
42+
(emacs-lisp-mode . "elisp.org")
43+
(perl-mode . "perl.org")
44+
(python-mode . "python.org")
45+
(sh-mode . "bash.org")
46+
(js-jsx-mode . "javascript.org")
47+
(js-mode . "javascript.org")
48+
(js2-jsx-mode . "javascript.org")
49+
(js2-mode . "javascript.org"))
50+
51+
"Mapping the correspondence between `major-mode' and the snippet file."
4752
:group 'code-library)
4853

4954
(defcustom code-library-directory "~/CodeLibrary/"
50-
"snippet files are stored in the directory"
55+
"Snippet files are stored in the directory."
5156
:group 'code-library)
5257

58+
(defcustom code-library-use-tags-command t
59+
"Automatically run `org-mode' tags prompt when saving a snippet."
60+
:group 'code-library)
61+
62+
(defcustom code-library-keep-indentation '(makefile-mode
63+
makefile-gmake-mode)
64+
"List of modes which will be keep tabs and indentation as is.
65+
66+
Normally code-library removed tabs to normalise indentation
67+
because code can come from a range of sources where the
68+
formatting and buffer local tab width can be in use."
69+
:group 'code-library)
70+
71+
(defun code-library-trim-left-margin ()
72+
"Remove common line whitespace prefix."
73+
(save-excursion
74+
(goto-char (point-min))
75+
(let ((common-left-margin) )
76+
(while (not (eobp))
77+
(unless (save-excursion
78+
(looking-at "[[:space:]]*$"))
79+
(back-to-indentation)
80+
(setq common-left-margin
81+
(min (or common-left-margin (current-column)) (current-column))))
82+
(forward-line))
83+
(when (and common-left-margin (> common-left-margin 0))
84+
(goto-char (point-min))
85+
(while (not (eobp))
86+
(delete-region (point)
87+
(+ (point)
88+
(min common-left-margin
89+
(save-excursion
90+
(back-to-indentation)
91+
(current-column)))))
92+
(forward-line))))))
93+
94+
(defsubst code-library-buffer-substring (beginning end &optional keep-indent)
95+
"Return the content between BEGINNING and END.
96+
97+
Tabs are converted to spaces according to mode.
98+
99+
The first line is whitespace padded if BEGINNING is positioned
100+
after the beginning of that line.
101+
102+
Common left margin whitespaces are trimmed.
103+
104+
If KEEP-INDENT is t, tabs and indentation will be kept."
105+
(let ((content (buffer-substring-no-properties beginning end))
106+
(content-tab-width tab-width)
107+
(content-column-start (save-excursion
108+
(goto-char beginning)
109+
(current-column))))
110+
(with-temp-buffer
111+
(let ((tab-width content-tab-width))
112+
(unless keep-indent
113+
(insert (make-string content-column-start ?\s)))
114+
(insert content)
115+
(unless keep-indent
116+
(untabify (point-min) (point-max))
117+
(code-library-trim-left-margin))
118+
(buffer-substring-no-properties (point-min) (point-max))))))
119+
120+
121+
(defun code-library-get-thing ()
122+
"Return what's supposed to be saved to the conde library as a string."
123+
(let* ((keep-indent (member major-mode code-library-keep-indentation))
124+
(bod (bounds-of-thing-at-point 'defun))
125+
(r (cond
126+
((region-active-p) (cons (region-beginning) (region-end)))
127+
(bod bod)
128+
(t (cons (point-min) (point-max))))))
129+
(code-library-buffer-substring (car r) (cdr r) keep-indent)))
130+
131+
(defun code-library-create-snippet (head)
132+
"Create and return a new org heading with source block.
133+
134+
HEAD is the org mode heading"
135+
(let ((content (code-library-get-thing))
136+
(code-major-mode (replace-regexp-in-string "-mode$" "" (symbol-name major-mode)))
137+
(tangle-file (if (buffer-file-name) (file-name-nondirectory (buffer-file-name)))))
138+
(with-temp-buffer
139+
(insert content)
140+
(org-escape-code-in-region (point-min) (point-max))
141+
(unless (bolp)
142+
(insert "\n"))
143+
(insert "#+END_SRC\n")
144+
(goto-char (point-min))
145+
(insert (format "* %s\n" head))
146+
(insert (format "#+BEGIN_SRC %s" code-major-mode))
147+
(when tangle-file
148+
(insert (format " :tangle %s" tangle-file)))
149+
(insert "\n")
150+
(buffer-string))))
151+
152+
53153
(defun code-library-save-code()
54-
"save the snippet."
154+
"Save the snippet to it's file location."
55155
(interactive)
56-
(let* ((code (if (region-active-p)
57-
(buffer-substring-no-properties (region-beginning) (region-end))
58-
(thing-at-point 'defun)))
156+
(let* ((head (read-string "Please enter this code description: " nil nil "Untitled"))
157+
(snippet (code-library-create-snippet head))
59158
(code-major-mode (replace-regexp-in-string "-mode$" "" (symbol-name major-mode)))
60-
(library-base-file (or (cdr (assoc major-mode code-library-mode-file-alist))
61-
(concat code-major-mode ".org")))
62-
(library-file (concat (file-name-as-directory code-library-directory) library-base-file))
63-
(export-file (file-name-nondirectory (buffer-file-name)))
64-
(head (read-string "Please enter this code description: ")))
65-
(save-excursion
66-
(find-file library-file)
67-
(end-of-buffer)
68-
(newline)
69-
(insert (concat "* " head))
70-
(newline-and-indent)
71-
(insert (format "#+BEGIN_SRC %s :tangle %s" code-major-mode export-file))
72-
(newline-and-indent)
73-
(newline-and-indent)
74-
(insert "#+END_SRC")
75-
(forward-line -1) ;上一行
76-
(org-edit-src-code)
77-
(insert code)
78-
(org-edit-src-exit)
79-
(org-set-tags-command) ;set tags
80-
(save-buffer))))
159+
(library-base-file (or (cdr (assoc major-mode code-library-mode-file-alist))
160+
(concat code-major-mode ".org")))
161+
(library-file (expand-file-name library-base-file
162+
(file-name-as-directory code-library-directory))))
163+
(with-current-buffer
164+
(find-file-noselect library-file)
165+
(save-excursion
166+
(goto-char (point-max))
167+
(beginning-of-line)
168+
(unless (looking-at "[[:space:]]*$")
169+
(insert "\n"))
170+
(insert snippet)
171+
(when code-library-use-tags-command
172+
(org-set-tags-command)))
173+
(save-buffer))))
81174

82175
(provide 'code-library)
83176

0 commit comments

Comments
 (0)