How to highlight some words in Eshell? - emacs

I use Eshell in Emacs to run a program and I could check the output in the Shell , but I want to highlight some words like ‘errors’ ‘info’,or other words like that. How could I do that ?

(defun font-lock-comment-annotations ()
(font-lock-add-keywords
nil
'(("\\<\\(error\\)" 1 font-lock-warning-face t)
("\\<\\(info\\)" 1 'org-todo t)
))
(add-hook 'eshell-mode-hook 'font-lock-comment-annotations)
Give it a try :)

You should try nschum/highlight-symbol.el, like this:

Related

emacs how to use call-interactively with parameter

If I want to make my own function which among other thing calls wg-save (workgroups.el - save workgroups) then I do something like this:
(defun foo ()
(interactive)
...
(call-interactively 'wg-save)
)
(global-set-key (kbd "my binding") 'foo)
What about the following scenario (I will use eyebrowse.el as an example):
eyebrowse uses C-c C-w 'number' to move to different window configurations e.g. C-c C-w 1 to move to 1 or C-c C-w 2 to move to 2.
How can I write a similar function like the 'foo' since now I need to pass to 'call-interactively' a 'number' as parameter?
EDIT: C-c C-w 1 calls eyebrowse-switch-to-window-config-1.
So I need to make a 'foo' function like the above that will 'call-interactively'
'eyebrowse-switch-to-window-config-1' when the key binding is 'C-c C-w 1', 'eyebrowse-switch-to-window-config-2' when the key binding is 'C-c C-w 2' etc.
Something like the following (if it makes sense):
(defun foo ()
(interactive)
...
(call-interactively 'eyebrowse-switch-to-window-config-"number")
)
(global-set-key (kbd "C-c C-w 'number'") 'foo)
From the documentation, read with C-h f call-interactively RET:
Optional third arg KEYS, if given, specifies the sequence of events to
supply, as a vector, if the command inquires which events were used to
invoke it. If KEYS is omitted or nil, the return value of
`this-command-keys-vector' is used.
So to pass arguments to call-interactively, give it a vector with the arguments, like so
(call-interactively 'my-fn t (vector arg1 arg2))
That way you don't need to call eyebrowse-switch-to-config-window-nwhere n is a number, you call the function they rely on, eyebrowse-switch-to-window-config, and give it a number as argument.
You can get a number for argument like this: (see help of "interactive" )
(defun foo (arg)
(interactive "nWindow? ")
(call-interactively 'my-fn t (vector arg))
)
But did you read the source of eyebrowse ? It will give ideas.
(defun eyebrowse-switch-to-window-config-0 ()
"Switch to window configuration 0."
(interactive)
(eyebrowse-switch-to-window-config 0))
You can do something like this I believe:
(defun foo (NUM)
"Enter the NUM of the screen to go to.
If the key pressed is not a number go to screen 0."
(interactive
(list (read-key-sequence "Which window: ")))
((eyebrowse-switch-to-window-config 3) (string-to-number NUM)))
The key is the read-key-sequence function
To save time and space, you can use a loop to define all the keybindings you'll use.
For example, for eyebrowse instead of defining each keybinding you could do this instead:
(load-library "eyebrowse")
(dotimes (i 10)
(global-set-key
(kbd (concat "C-c C-w " (number-to-string i)))
`(lambda ()
(interactive)
(eyebrowse-switch-to-window-config ,i))))

How do I replace a hard-coded string with a variable in elisp?

I'm trying write an elisp function with a string that is set based on the filetype of the current buffer (but isn't just the filetype). For example, how would I replace this:
(defun run ()
(interactive)
(async-shell-command (concat "./" (file-name-base buffer-file-name)))
)
with this:
(defun run ()
(interactive)
(async-shell-command (concat cmd (file-name-base buffer-file-name)))
)
where cmd = "./"
I've tried to use defvar, but I get an error whenever I try to use a string, but this:
(defvar flowers)
(set 'flowers '(rose)
makes flycheck give me the error, "global/dynamic var `flowers' lacks a prefix" (and it doesn't work in my function).
So I can only presume you didn't actually try running your own code? You're trying to make a modified version of something which doesn't work in the first place.
Just replace the string with the variable name, but not (in either case) in parentheses, because ("foo") and (someVariable) are both attempts to call a function.
(defun foo ()
(interactive)
(shell-command "foo")
)
(defun foo ()
(interactive)
(shell-command someVariable)
)

imenu does not work for python mode or c++ mode

The imenu works for me in emacs-lisp-mode-hook. But when I try the following for the python or c++ mode, it does not work:
(add-hook 'python-mode-hook
(lambda ()
(add-to-list
'imenu-generic-expression
'("Sections" "^#### [ \\(.+\\) ]$" 1))
(imenu-add-to-menubar "Position")))
(add-hook 'c++-mode-hook
(lambda ()
(add-to-list
'imenu-generic-expression
'("Sections" "^//// [ \\(.+\\) ]$" 1))
(imenu-add-to-menubar "Position")))
Does anyone know why?
The issue is that both these modes (assuming you are using the builtin python-mode), set imenu-create-index-function (the function used to create index) to imenu-default-create-index-function. This function in turn tries to use the values of imenu-prev-index-position-function, imenu-extract-index-name-function to extract the imenu, it does not use the imenu-generic-expression if the the two variables are set (see the definition of imenu-default-create-index-function, in imenu.el). That is why the values you are setting is not used. What we can do here is define a custom function that uses both imenu-default-create-index-function and imenu-generic-expression, combines their results and returns it. Then we can set the value of imenu-create-index-function to our custom function. Below is an example of such a custom function
(defun my-merge-imenu ()
(interactive)
(let ((mode-imenu (imenu-default-create-index-function))
(custom-imenu (imenu--generic-function imenu-generic-expression)))
(append mode-imenu custom-imenu)))
Then we can set this as the value of imenu-create-index-function in major-mode hooks, eg. for python-mode
(add-hook 'python-mode-hook
(lambda ()
(add-to-list
'imenu-generic-expression
'("Sections" "^#### \\[ \\(.*\\) \\]$" 1))
(imenu-add-to-menubar "Position")
(setq imenu-create-index-function 'my-merge-imenu)))
Similar solution should work for c++-mode.
A note about the regular expressions used: There is an error in your regular expression, since [ is a metacharacter you will need to escape it to match normal [, you will also need to escape \ since you are in a string so the correct regex string will be "^#### \\[ \\(.*\\) \\]$"

print only text discarding text properties

I have the following function to print the line where point is to the *scratch* buffer,
(defun print-line ()
(print (thing-at-point 'line) (get-buffer "*scratch*")))
but it prints even the fontified info like this
#(" OFFICE
" 0 2 (fontified t org ...
How to discard the printing of the fontified info.
To expand on Daimrod's mention of buffer-substring-no-properties...
M-x apropos RET no-properties RET
buffer-substring-no-properties
Function: Return the characters of part of the buffer, without the
text properties.
field-string-no-properties
Function: Return the contents of the field around POS, without text
properties.
insert-buffer-substring-no-properties
Function: Insert before point a substring of BUFFER, without text
properties.
match-string-no-properties
Function: Return string of text matched by last search, without text
properties.
minibuffer-contents-no-properties
Function: Return the user input in a minibuffer as a string, without
text-properties.
substring-no-properties
Function: Return a substring of STRING, without text properties.
You can read about text properties in the manual:
M-: (info "(elisp) Text Properties") RET
I needed something similar for eredis when manipulating strings from an org-table. You can use `set-text-properties' to get rid of them when displaying the string.
(defun strip-text-properties(txt)
(set-text-properties 0 (length txt) nil txt)
txt)
(defun print-line ()
(print (strip-text-properties
(thing-at-point 'line))
(get-buffer "*scratch*")))
I've tried some things but it's weird, I don't really understand how text properties work.
For example:
(type-of (thing-at-point 'line)) => string
As you've said if one tries to print it, the properties are printed as well, but if one tries to insert it:
(insert (format "%s" (thing-at-point 'line)))
Only the string is printed, not the properties.
So it seems to me that those properties are just bound to the string but you can manipulate the string as usual:
(lenght (thing-at-point 'line))
(substring (thing-at-point 'line) 0 2)
However, if all you want is the line, and the line only you can use buffer-substring-no-properties:
(defun print-line ()
(print (buffer-substring-no-properties (point-at-bol) (point-at-eol))))

Emacs auto compelete paren, indent and new line - how to?

In C - I want that when I type a { and then } emacs will insert a new line between them and then set the cursor in between them. For example:
int main() {
now I type } and the following happens:
int main()
{
//cursor is here
}
Edit: forgot to mention - I want emacs to know that when defining a function that it should do what was described above but when doing a for loop, or if statement for example I want it to do the following:
if (bla bla) {
type } and... :
if (bla bla) {
//cursor here
}
If you don't mind that the behaviour will be only almost, but not exactly the way you described it, there is a built-in way to do that. It's the auto-newline feature, that can be activated with the key combination C-c C-a or this line your .emacs:
(c-toggle-auto-newline 1)
The difference is that it will do the reformatting right after entering the opening brace {. When you finally enter the closing brace, it will indent it the right way, too.
You also need to set the right CC Mode style. The style "cc-mode" seems to define things the way you described it. You can activate it with the key combination C-c . and then choosing cc-mode, or the .emacs line
(c-set-style "cc-mode")
The c-mode functions are autoloaded and will therefore usually not be available while loading the .emacs file. Therefore you should wrap them in a hook for c-mode, like this
(add-hook 'c-mode-hook
(lambda ()
(c-toggle-auto-newline 1)
(c-set-style "cc-mode")))
As for the { stuff:
(define-minor-mode c-helpers-minor-mode
"This mode contains little helpers for C developement"
nil
""
'(((kbd "{") . insert-c-block-parentheses))
)
(defun insert-c-block-parentheses ()
(interactive)
(insert "{")
(newline)
(newline)
(insert "}")
(indent-for-tab-command)
(previous-line)
(indent-for-tab-command)
)
Paste the above into your .emacs. You can activate it with c-helpers-minor-mode.
Edit: The above inserts everything by just pressing {. The script below should do it if you type {}:
(defun insert-latex-brackets (opening closing) ; prototype function for all enclosing things
(interactive)
(insert opening)
(insert " ")
(insert closing)
(backward-char (+ 1 (length closing )))
)
(defun check-char-and-insert (char opening closing)
(interactive)
(if (equal char (char-to-string (char-before (point))))
(progn (delete-backward-char 1)
(insert-latex-brackets opening closing))
(insert char)
)
)
(local-set-key (kbd "}") 'check-char-and-insert)
One last note: You could try using yasnippet, which can be a real time saver used properly.