ProjectManagement
From Niki
When you want to use a simple set of macros for project management, you have a couple of options :
- http://www.mailnet.co.uk/~marc/msl.html#18 : Unfortunately these doesn't seem to work out-of-the-box with current NEdit versions, and they're somewhat too simplistic.
- http://nedit.gmxhome.de/macros.html : To make full use of these, it is required that you patch and recompile NEdit, that's a bit too much of a hassle for some (including me :)
That's why I modified and enhanced a bit the former macros instead. This macro set needs a $HOME/.nedit/projects/ folder to store the projects.
It provides the following commands :
- Open project
- Browse projects (select a project and either open, delete or rename it)
- Close project
- Save project
- Save project as...
- save all (save project and project files)
- save all and close
When you save the project, all files currently open are recorded as part of the project. To remove a file from the project, simply close it, then save the project. To add a file, open it, save the project. isn't it easy?
Here are the macros :
# initialize global variables
define project_init {
# reset project name
$project_name = ""
# reset project data list
$project_data = ""
# default project file extension
$def_project_ext = ".np"
# default project file directory
$def_project_dir = "~/.nedit/projects/"
}#define
# checks if file is part or project, returns 0 if it doesn't
# >0 otherwize
define file_in_project{
if ($n_args != 1) {
return 0
} else {
return search_string($project_data, "^" $1 "$", 0, "regex") + 1
}#if
}#define
# prompts for project name
define project_dialog {
tmpstr = string_dialog("Project Name:","Ok","Cancel")
#don't lose project file name if cancelled
if ($string_dialog_button < 2) {
$project_name = tmpstr
}#if
}#define
# Emmanuel Florac 11/10/05
# select project from list
define project_select_dialog {
# list .np files from project directory
project_list = shell_command( "ls " $def_project_dir "*" $def_project_ext , "")
# remove path and extension
pjl1 = replace_in_string(project_list, $def_project_dir, "" )
project_list = replace_in_string(pjl1, ".np", "" )
if ( project_list != "" ) {
tmpstr =list_dialog("Please select a project :", project_list, "Open", "Delete", "Rename", "Cancel")
# open
if ($list_dialog_button == 1) {
$project_name = tmpstr
} else {
$managed_project = tmpstr
}
#if
} else {
# noproject
dialog("No Projects found!")
$list_dialog_button = 0
}
}# define
# browser projects
define browse_project {
project_select_dialog()
$mybutton = $list_dialog_button
# open
if ( $mybutton == 1) {
do_open_project()
}
# delete
if ( $mybutton == 2) {
delete_project()
}
#rename
if ( $mybutton == 3) {
rename_project()
}
}
# delete a project
define delete_project {
delbutton = dialog( "Delete project '" $managed_project "', are you sure?", "OK", "Cancel" )
if ( delbutton < 2 ) {
# clear current project name if the file's deleted
if ( $managed_project == $project_name) {
project_init()
}
shell_command("rm " $def_project_dir $project_name $managed_project $def_project_ext , "")
}
}
# rename a project
define rename_project {
renbutton = string_dialog( "Rename project '" $managed_project "', to :", "OK", "Cancel" )
if ( $string_dialog_button < 2 ) {
shell_command("mv " $def_project_dir $project_name $managed_project $def_project_ext " " $def_project_dir $project_name renbutton $def_project_ext, "")
# clear current project name if the file's deleted
if ( $managed_project == $project_name) {
$project_name = renbutton
dialog("test:" $project_name)
}
}
}
#########################
# opens a project by name
define open_project {
project_dialog()
$mybutton = $string_dialog_button
do_open_project()
}
# actually opens project file
define do_open_project {
if ($mybutton == 1) {
name = $def_project_dir $project_name $def_project_ext
$project_data = read_file(name)
if ($read_status != 1) {
dialog("Project " $project_name " not found", "Dismiss")
} else {
# process all data, assumes one fully qualified file name per line
# skip first line, which is comment
# sauter aussi la dernière ligne (\n seul)
start_name = search_string($project_data, "$", 0, "regex") + 1
end_name = search_string($project_data, "$", start_name, "regex")
while (end_name > 0) {
the_name = substring($project_data, start_name, end_name)
if ( the_name != "" ) {
open(the_name)
}
start_name = end_name + 1
end_name = search_string($project_data, "$", start_name, "regex")
}#while
}#else
}#if read_status
}#define
# saves a project
define save_project {
# must have name
if ($project_name == "") {
project_dialog()
}#if
# construct file name
name = $def_project_dir $project_name $def_project_ext
# write comment line, also serves to clear file
write_file("# nedit project file, DO NOT EDIT\n", name)
# write all fully qualified file names
for (w = focus_window("last"); w != ""; w = focus_window("next")) {
append_file($file_path $file_name "\n", name)
}#for
}#define
# saves a project under a new name
define save_project_as {
project_dialog()
if (($string_dialog_button == 1) && ($project_name != "" )) {
dialog("test")
save_project()
}#if
}#define
# closes project
define close_project {
launch = $file_path $file_name
for (w = focus_window("last"); w != ""; w = focus_window("next")) {
if (file_in_project(w)) {
close()
}#if
}#for
# reset vars
project_init()
}#define
# saves all project files
define save_all {
for (w = focus_window("last"); w != ""; w = focus_window("next"))
if (file_in_project(w)) {
save()
}#if
}#define
And here are the menu definitions, that you may import with nedit -import <file> :
nedit.macroCommands: \
Project>Open project:::: {\n\
open_project()\n\
}\n\
Project>Choose project:Shift+Ctrl+B::: {\n\
browse_project()\n\
}\n\
Project>Close Project:::: {\n\
close_project()\n\
}\n\
Project>Save Project:::: {\n\
save_project()\n\
}\n\
Project>Save Project as...:::: {\n\
save_project_as()\n\
}\n\
Project>Save all:::: {\n\
save_all()\n\
}\n\
Project>Save all and Close:::: {\n\
save_all()\n\
close_project()\n\
}\n\
