Emacs 23.3.1: whitespace style - whitespace

I've just upgraded to Kubuntu 11.10. After that the way Emacs represents whitespace in whitespace minor mode got changed. It were shaded rectangulars and not Emacs puts dots in the place of white space:
I tried to change it through the M-x customize-group and then whitespace -- but there's no such thing as a dot. It says that whitespaces are represented by shading (see the pic above) - but they are not (see the same pic).
Here's the value of Whitespace Space face:
I also asked this question at superuser but since I got 0 replies there -- I decided to consult another community.
Edit 1:
Following the Luke's solution gives no coloring to space nor to tabs (unless I've done something wrong):
Edit 2:
Adding face here fixes Luke's solution. Thanks to Sergey.
(setq whitespace-style (quote
( face spaces tabs newline space-mark tab-mark newline-mark)))
Edit 3:
Currently I'm using:
(custom-set-variables
'(whitespace-line-column 9999999)
'(whitespace-tab-width 4 t)
'(whitespace-display-mappings '(
(space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
(space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
(newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
(tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
))
'(whitespace-style '(face spaces tabs newline space-mark tab-mark newline-mark))
)
(custom-set-faces
'(default ((t (:inherit nil :stipple nil :background "#ffffb1" :foreground "#141312" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 125 :width normal :foundry "monotype" :family "DejaVu Sans Mono"))))
'(whitespace-trailing ((t (:background "grey99"))))
)
at Emacs 24.3.50.1

There's probably a better way to do this, but adding this to your .emacs should work:
(setq whitespace-display-mappings
'(
(space-mark ?\ [? ]) ;; use space not dot
(space-mark ?\xA0 [?\u00A4] [?_])
(space-mark ?\x8A0 [?\x8A4] [?_])
(space-mark ?\x920 [?\x924] [?_])
(space-mark ?\xE20 [?\xE24] [?_])
(space-mark ?\xF20 [?\xF24] [?_])
(newline-mark ?\n [?$ ?\n])
(tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t])))
(custom-set-faces
'(whitespace-space
((((class color) (background dark)) (:background "red" :foreground "white"))
(((class color) (background light)) (:background "yellow" :foreground "black"))
(t (:inverse-video t)))))
The standard value of whitespace-display-mappings uses a 'middle dot' for a space, the code above uses a standard space. You can change the colours for whitespace-space as required.

All you need is just add 'face' keyword along others in whitespace-style.
E.g.:
(setq whitespace-style (quote
( face spaces tabs newline space-mark tab-mark newline-mark)))

After using Luke Girvin advice and starting emacs with -q flag - Luke's solution worked. I found that the problem was
;; make whitespace-mode use just basic coloring
(setq whitespace-style (quote
( spaces tabs newline space-mark tab-mark newline-mark)))
these lines in .emacs. So I removed them, and then have used customize-group -> whitespace to make things this way:
So ithe problem is solved. Thanks Luke!

Related

Why is my emacs barking (noise at the end of file)?

I'm fairly certain this was self-inflicted but I cannot for the life of me remember how.
Every time I hit the end of a buffer in emacs and press C-n emacs barks like a dog. I think the bark is probably a custom sound file somewhere made to replace the bell noise that's there by default. I don't know where the sound file or the configuration setting are, though.
Here is the only configuration file I have:
init.el
;; Added by Package.el. This must come before configurations of
;; installed packages. Don't delete this line. If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
(package-initialize)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(gdb-many-windows t)
'(gdb-show-main t)
'(inhibit-startup-screen t)
'(package-selected-packages '(slime nasm-mode org-roam zig-mode)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
;; INIT.EL
;; Maximize the window on startup
(add-to-list 'default-frame-alist '(fullscreen . maximized))
;; Turn off backups
(setq make-backup-files nil)
;; Auto revert changed buffers
(global-auto-revert-mode 1)
;; source: http://steve.yegge.googlepages.com/my-dot-emacs-file
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(if (get-buffer new-name)
(message "A buffer named '%s' already exists!" new-name)
(progn
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
;; source: https://stackoverflow.com/questions/38672928/how-to-set-emacs-up-for-assembly-programming-and-fix-indentation
(defun my-asm-mode-hook ()
;; you can use `comment-dwim' (M-;) for this kind of behaviour anyway
(local-unset-key (vector asm-comment-char))
;; (local-unset-key "<return>") ; doesn't work. "RET" in a terminal. http://emacs.stackexchange.com/questions/13286/how-can-i-stop-the-enter-key-from-triggering-a-completion-in-company-mode
(electric-indent-local-mode) ; toggle off
; (setq tab-width 4)
(setq indent-tabs-mode nil)
;; asm-mode sets it locally to nil, to "stay closer to the old TAB behaviour".
;; (setq tab-always-indent (default-value 'tab-always-indent))
(defun asm-calculate-indentation ()
(or
;; Flush labels to the left margin.
; (and (looking-at "\\(\\.\\|\\sw\\|\\s_\\)+:") 0)
(and (looking-at "[.#_[:word:]]+:") 0)
;; Same thing for `;;;' comments.
(and (looking-at "\\s<\\s<\\s<") 0)
;; %if nasm macro stuff goes to the left margin
(and (looking-at "%") 0)
(and (looking-at "c?global\\|section\\|default\\|align\\|INIT_..X") 0)
;; Simple `;' comments go to the comment-column
;(and (looking-at "\\s<\\(\\S<\\|\\'\\)") comment-column)
;; The rest goes at column 4
(or 4)))
)
(add-hook 'asm-mode-hook #'my-asm-mode-hook)
(setq inferior-lisp-program "sbcl")
(windmove-default-keybindings)
(add-hook 'c-mode-hook #'display-fill-column-indicator-mode)
I have two questions about this problem:
How do I disable this "feature"?
Where is emacs storing the configuration for this if not in init.el?
Most likely the sound is set somewhere in your OS as an alarm default.
Here's a great resource for further customizing whether and how the alarm triggers:
https://www.emacswiki.org/emacs/AlarmBell
To turn it off completely: (setq ring-bell-function 'ignore)

Trailing backslash error web-mode content type

I get this error when trying to set content type in web-mode: File mode specification error: (invalid-regexp Trailing backslash)
I have had a hard time debugging this. I'm very new to emacs so I need some help setting web-mode. I have been following the documentation in web-mode.org but it has been difficult to decypher. Thanks.
(use-package
web-mode
:defer 2
:ensure t
:mode ("\\.html?\\"
"\\.hbs$\\"
"\\.vue$\\"
"\\.css?\\"
"components/.*\\.js[x]?\\'"
"containers/.*\\.js[x]?\\'")
:config (progn
(setq web-mode-enable-auto-closing t
web-mode-enable-auto-opening t
web-mode-enable-auto-pairing t
web-mode-enable-auto-indentation t
web-mode-enable-auto-quoting t
;; right now paired with AutoComplete
web-mode-ac-sources-alist
'(("css" . (ac-source-css-property))
("vue" . (ac-source-words-in-buffer ac-source-abbrev))
("html" . (ac-source-words-in-buffer ac-source-abbrev)))
web-mode-content-types-alist
'(("jsx" . "components/.*\\.js[x]?\\'")
("jsx" . "containers/.*\\.js[x]?\\'")))))
;; usually I set them in containers/ or components/ directorie
;; and to keep seperate from plain JS
;; adjust indents for web-mode to 2 spaces
(defun my-web-mode-hook ()
"Hooks for Web mode. Adjust indents"
;;; http://web-mode.org/
(setq web-mode-markup-indent-offset 2)
(setq web-mode-css-indent-offset 2)
(setq web-mode-code-indent-offset 2))
(add-hook 'web-mode-hook 'my-web-mode-hook)
In the list of regexps after :mode, make sure that they all end with \\'. Currently two of them do, but four of them lost the final ' character.
:mode ("\\.html?\\'"
"\\.hbs$\\'"
"\\.vue$\\'"
"\\.css?\\'"
"components/.*\\.js[x]?\\'"
"containers/.*\\.js[x]?\\'")
\' is a special regexp construct that "matches the empty string, but only at the end of the buffer or string being matched against".

Clojure.core source: Why ~# (unquote-splicing operator) with a quoted double list inside, instead of ~ (unquote operator)

Preamble
I was looking throught the source code in clojure.core for no particular reason.
I started reading defmacro ns, here is the abridged source:
(defmacro ns
"...docstring..."
{:arglists '([name docstring? attr-map? references*])
:added "1.0"}
[name & references]
(let [...
; Argument processing here.
name-metadata (meta name)]
`(do
(clojure.core/in-ns '~name)
~#(when name-metadata
`((.resetMeta (clojure.lang.Namespace/find '~name) ~name-metadata)))
(with-loading-context
~#(when gen-class-call (list gen-class-call))
~#(when (and (not= name 'clojure.core) (not-any? #(= :refer-clojure (first %)) references))
`((clojure.core/refer '~'clojure.core)))
~#(map process-reference references))
(if (.equals '~name 'clojure.core)
nil
(do (dosync (commute ##'*loaded-libs* conj '~name)) nil)))))
Looking Closer
And then trying to read it I saw some strange macro patterns, in particular we can look at:
~#(when name-metadata
`((.resetMeta (clojure.lang.Namespace/find '~name) ~name-metadata)))
The clojure.core version
Here is a standalone working extraction from the macro:
(let [name-metadata 'name-metadata
name 'name]
`(do
~#(when name-metadata
`((.resetMeta (clojure.lang.Namespace/find '~name) ~name-metadata)))))
=> (do (.resetMeta (clojure.lang.Namespace/find (quote name)) name-metadata))
When I ran this could I couldn't help but wonder why there is a double list at the point `((.resetMeta.
My version
I found that by just removing the unquote-splicing (~#) the double list was unnecessary. Here is a working standalone example:
(let [name-metadata 'name-metadata
name 'name]
`(do
~(when name-metadata
`(.resetMeta (clojure.lang.Namespace/find '~name) ~name-metadata))))
=> (do (.resetMeta (clojure.lang.Namespace/find (quote name)) name-metadata))
My Question
Thus, why does clojure.core choose this seemingly extraneous way of doing things?
My Own Thoughts
Is this an artifact of convention?
Are there other similar instances where this is used in more complex ways?
~ always emits a form; ~# can potentially emit nothing at all. Thus sometimes one uses ~# to splice in a single expression conditionally:
;; always yields the form (foo x)
;; (with x replaced with its macro-expansion-time value):
`(foo ~x)`
;; results in (foo) is x is nil, (foo x) otherwise:
`(foo ~#(if x [x]))
That's what's going on here: the (.resetMeta …) call is emitted within the do form that ns expands to only if name-metadata is truthy (non-false, non-nil).
In this instance, it doesn't really matter – one could use ~, drop the extra brackets and accept that the macroexpansion of an ns form with no name metadata would have an extra nil in the do form. For the sake of a prettier expansion, though, it makes sense to use ~# and only emit a form to handle name metadata when it is actually useful.

Regex order when matching single square bracket

Hello to all of you,
I have a question regarding a specific regex in Elisp and specifically in Elisp. I'm trying to match a single square bracket and ielm has this:
(string-match "[\]\[]" "[") ; ===> 0
(string-match "[\[\]]" "[") ; ===> nil
(string-match "[\]\[]" "]") ; ===> 0
(string-match "[\[\]]" "]") ; ===> nil
(string-match "[\[\]]" "[]") ; ===> 0
(string-match "[\]\[]" "[]") ; ===> 0
(string-match "[\]\[]" "][") ; ===> 0
(string-match "[\]\[]" "][") ; ===> 0
Where as with JS, these all return true:
'['.match(/[\[\]]/) // ===>['[']
'['.match(/[\]\[]/) // ===>['[']
']'.match(/[\[\]]/) // ===>[']']
']'.match(/[\]\[]/) // ===>[']']
'[]'.match(/[\[\]]/) // ===>['[']
'[]'.match(/[\]\[]/) // ===>['[']
']['.match(/[\[\]]/) // ===>[']']
']['.match(/[\]\[]/) // ===>[']']
Here's a regex101: https://regex101.com/r/e8sLXr/1
I don't understand why the order of my square brackets in Elisp matters. I've tried using double backslashes but it doesn't help. Actually, it gives me more nils on these regexes whereas I thought the proper way to escape a backslack in a string for the regex to process was to double it: https://www.gnu.org/software/emacs/manual/html_node/elisp/Regexp-Example.html#Regexp-Example
Does anyone know what I'm missing a could help me ?
Cheers,
Thomas
EDIT: grammar
Firstly, let's ditch the backslashes. [ and ] are not special to strings(*), and therefore escaping them does not change them. So the following is equivalent, and easier to read:
(string-match "[][]" "[") ; ===> 0
(string-match "[][]" "]") ; ===> 0
(string-match "[][]" "[]") ; ===> 0
(string-match "[][]" "][") ; ===> 0
(string-match "[][]" "][") ; ===> 0
This pattern matches either ] or [, and all the strings being tested have one of those characters at the start; hence we match at position 0 in each case.
Critically, to include a ] in a character alternative it must be the first character. Hence the following did not do what you wanted:
(string-match "[[]]" "[") ; ===> nil
(string-match "[[]]" "]") ; ===> nil
(string-match "[[]]" "[]") ; ===> 0
This pattern matches exactly [], because [[] is a character alternative matching anything in the set comprising the single-character [; and that character alternative is then followed by ] (which, when it is not ending a character alternative, just matches itself).
You will want to read the "character alternative" details at:
C-hig (elisp)Regexp Special RET
(*) Note also that backslashes are not special to a regexp when they are within a character alternative.
Your regexps didn't have any backslashes -- because in double-quoted string format you would have needed to double the backslashes to include those in the regexp -- but if you had done that, and if they were also inside the character alternative, it would just mean that a backslash would be one of the characters matched by that set.
e.g. "[\\]\\[]" is the regexp [\]\[] which matches \[]
(Remembering that ] cannot appear in a character alternative unless it is the first character.)

Extracting URLs from an Emacs buffer?

How can I write an Emacs Lisp function to find all hrefs in an HTML file and extract all of the links?
Input:
<html>
<a href="http://www.stackoverflow.com" _target="_blank">StackOverFlow&lt/a>
<h1>Emacs Lisp</h1>
<a href="http://news.ycombinator.com" _target="_blank">Hacker News&lt/a>
</html>
Output:
http://www.stackoverflow.com|StackOverFlow
http://news.ycombinator.com|Hacker News
I've seen the re-search-forward function mentioned several times during my search. Here's what I think that I need to do based on what I've read so far.
(defun extra-urls (file)
...
(setq buffer (...
(while
(re-search-forward "http://" nil t)
(when (match-string 0)
...
))
I took Heinzi's solution and came up with the final solution that I needed. I can now take a list of files, extract all URL's and titles, and place the results in one output buffer.
(defun extract-urls (fname)
"Extract HTML href url's,titles to buffer 'new-urls.csv' in | separated format."
(setq in-buf (set-buffer (find-file fname))); Save for clean up
(beginning-of-buffer); Need to do this in case the buffer is already open
(setq u1 '())
(while
(re-search-forward "^.*<a href=\"\\([^\"]+\\)\"[^>]+>\\([^<]+\\)</a>" nil t)
(when (match-string 0) ; Got a match
(setq url (match-string 1) ) ; URL
(setq title (match-string 2) ) ; Title
(setq u1 (cons (concat url "|" title "\n") u1)) ; Build the list of URLs
)
)
(kill-buffer in-buf) ; Don't leave a mess of buffers
(progn
(with-current-buffer (get-buffer-create "new-urls.csv"); Send results to new buffer
(mapcar 'insert u1))
(switch-to-buffer "new-urls.csv"); Finally, show the new buffer
)
)
;; Create a list of files to process
;;
(mapcar 'extract-urls '(
"/tmp/foo.html"
"/tmp/bar.html"
))
If there is at most one link per line and you don't mind some very ugly regular expression hacking, run the following code on your buffer:
(defun getlinks ()
(beginning-of-buffer)
(replace-regexp "^.*<a href=\"\\([^\"]+\\)\"[^>]+>\\([^<]+\\)</a>.*$" "LINK:\\1|\\2")
(beginning-of-buffer)
(replace-regexp "^\\([^L]\\|\\(L[^I]\\)\\|\\(LI[^N]\\)\\|\\(LIN[^K]\\)\\).*$" "")
(beginning-of-buffer)
(replace-regexp "
+" "
")
(beginning-of-buffer)
(replace-regexp "^LINK:\\(.*\\)$" "\\1")
)
It replaces all links with LINK:url|description, deletes all lines containing anything else, deletes empty lines, and finally removes the "LINK:".
Detailed HOWTO: (1) Correct the bug in your example html file by replacing <href with <a href, (2) copy the above function into Emacs scratch, (3) hit C-x C-e after the final ")" to load the function, (4) load your example HTML file, (5) execute the function with M-: (getlinks).
Note that the linebreaks in the third replace-regexp are important. Don't indent those two lines.
You can use the 'xml library, examples of using the parser are found here. To parse your particular file, the following does what you want:
(defun my-grab-html (file)
(interactive "fHtml file: ")
(let ((res (car (xml-parse-file file)))) ; 'car because xml-parse-file returns a list of nodes
(mapc (lambda (n)
(when (consp n) ; don't operate on the whitespace, xml preserves whitespace
(let ((link (cdr (assq 'href (xml-node-attributes n)))))
(when link
(insert link)
(insert "|")
(insert (car (xml-node-children n))) ;# grab the text for the link
(insert "\n")))))
(xml-node-children res))))
This does not recursively parse the HTML to find all the links, but it should get you started in the direction of the general solution.

Resources